Hi guys, I was wondering how I could control the leverage used in a backtest when using the 'set_universe' function. Thanks for the help!
Hi guys, I was wondering how I could control the leverage used in a backtest when using the 'set_universe' function. Thanks for the help!
Owen,
At its most basic level leverage is when you have to borrow funds to complete an order, so any time you have to borrow money your leverage goes up. Consequently, set_universe
has no bearing on the amount of leverage your algo takes on, it is usually down to you order execution. Here a are a couple tips to help avoid over leveraging your account(Note: leverage is not bad).
Use order_target_percent()
this allows greater control over what your portfolio allocation is to a given security, and is much better and more robust than calculating the number of shares to order manually.
Check for open orders using the get_open_orders()
method. If your order hasn't been filled yet and you make another order for that security you can risk over leveraging your account, so define a set of behaviors for what to do with open orders such as exiting that bar of trade execution.
Record your leverage and if you see any spikes do some debugging to see what that specific issue is, it may be obscure. For example last night I had leverage spikes that were a result of a double-negative portfolio weight.
Finally, be aware of your portfolio's holdings, sometimes if a security gets delisted it remains in your portfolio (something we are working on fixing), among other things this is probably a good idea anyway.
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.
Thanks! I use order_target_percent and get_open_orders but my leverage, for some reason, is always in the range of 23-800. Here is the code
if bool(get_open_orders(stock)):
return
if current_price <= buy_long_band and cash >= 5000 and notional < context.max_notional:
order_target_percent(stock, .1)
log.debug("Buying long" + str(stock))
elif current_price >= close_band:
order_target(stock, 0)
log.debug("Selling long" + str(stock))
elif current_price < long_stop_band:
order_target(stock, 0)
log.debug("Lost too much money, stopping position in" + str(stock))
if bool(get_open_orders(stock)):
return
if context.order_placed == True and current_price <= average_band or current_price > short_stop_band:
order_target(stock, 1000)
log.debug("Covering short position in" + str(stock))
context.order_placed = None
elif context.order_placed == False and current_price > short_band and notional > context.min_notional:
order_target(stock, 1000)
log.debug("Short selling 10% of " + str(stock))
context.order_placed = True