How do you stop the back test from using leverage?
How do you stop the back test from using leverage?
dont think it is possible you may have something like
if context.account.leverage > 3.0:
print "STOP"
If you're using order() or order_target() then you'll have to manage your cash, position value and intended trade balance and ensure that the total liability never exceeds your current account total.
Or, you can use order_target_percent() and make sure that your new positions, plus your existing positions always sum to a total of 1.0 or 100% of your account balance.
If you have 10 securities you'd like to buy today, then order_target_percent(security, 1.0 / 10.0) would buy 10% per leg. If you close them all tomorrow morning then you can keep performing the same order entry. Effectively you have to divvy up your 100% into pieces you dole out to your instruments. And always make sure you take into account existing positions and new ones. Lots of examples of order_target_percent out there in the forum.
Note that order_target_percent is not smart enough to account for open orders, so as a safeguard, you can use:
# skip bar if any orders are open
for stock in context.stocks:
if bool(get_open_orders(stock)):
return
This assumes that all of your securities are listed in context.stocks.
The problem is that without checking for open orders, if there is slippage, you can end up exceeding an allocation of 100%, which would show up as context.account.leverage > 1.0.