I just started, so can someone please share a simple example algorithm or some code with me that caps the leverage at 1.0 without using the order_optimizer? Thanks
I just started, so can someone please share a simple example algorithm or some code with me that caps the leverage at 1.0 without using the order_optimizer? Thanks
Account leverage is defined as the sum of the absolute value of long and short positions divided by the portfolio value. For a leverage of one, the sum of the absolute values of position weights must equal 1.
So, if one has a series with all the securities and weights one wishes to hold, then divide the series by the sum of the absolute value of the weights to ensure the gross leverage is 1 something like this
weights = weights / weights.abs().sum
The general ordering logic would look like this.
# Make two series of the longs and shorts and associated weights
# Ensure that all the short weights are negative (this is what tells order to short them)
# This would come from the algo's selection and weighting process
# Make sure to include any current securities if those are to be held
# Combine the two series
weights = pd.concat([longs, shorts])
# Divide by the sum of the abs value of the weights to ensure the gross leverage is 1
weights = weights / weights.abs().sum
# Order the stocks
for stock, weight in weights.iteritems():
if data.can_trade(stock):
order_target_percent(stock, weight)
# Close any positions not in our long or short list
for stock, position in context.portfolio.positions.iteritems():
if data.can_trade(stock) and stock not in weights.index:
order_target_percent(stock, 0)
Using the order_optimal_portfolio
method is almost exactly the same but takes a few less lines of code and can be easily enhanced to incorporate other constraints. The attached algo shows a simple algo which incorporates both order approaches.
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.