My algo was disqualified because it exceeded a leverage of 3. Searching through the community posts on this, I found ways to help me like to use:
leverage = context.account.leverage
record(leverage=leverage)
for stock in data:
# Check the account leverage, leaving a buffer for open orders
# Liquidate the short position if the leverage is approaching the 3x limit
if leverage > context.leverage_buffer:
log.info("Approaching leverage limit. Current leverage is %s" % (leverage))
# Need to liquidate short position
if context.entered_short == True:
log.info("Liquidating position %s" % (stock))
order_target_percent(stock, 0)
return
However, this would like permanently stop your algo from running ever again or so it seems for me. For my algo, it was running fine for a super short time until it reached a return of 19.4% then it just stayed at that return percentage permanently until the backtest was done, I was really disappointed because without that code I was able to get over 400% returns in just under 2 months! Can someone please help me and tell me how to keep my leverage under 3 for the whole backtest without stopping the algo from running permanently?