Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How do you keep your leverage under 3 to make your algorithm legible for the contest

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?

2 responses

Without any information on your algo it is difficult to know why you exceed the leverage. This however happens at the level of the allocation, not the alpha research.

One reason I could see is that you are very close to the max leverage with short position and when the market increases and your algo drops, your leverage increases and exceed the limit. For example if you have a portfolio valued at $100 and you short 3 shares of a stock priced at $100 your leverage is 3. If the stock price increase to $105 your portfolio will be valued at $85. Thus your leverage will be equal to 3 * 105 / 85 = 3.71 without any action from you.

To avoid this you can use the framework I presented here to adjust the position in real time.

The idea would be to encapsulate all the logic of your algo in an alpha generator that comes back with an allocation (in my previous example -1 on the security you want to short but it could be any allocation with as many securities as you want as long as the sum of absolute value of weights is equal to 1. (ie normalization of leverage so that all leverage is managed by portfolio manager).

Now the portfolio manager will adjust the target portfolio to maintain constant leverage even if you don't take action. By default in the framework it is 1 but you can modify it to any leverage you want (for example 2.5) by adding this line between lines 136 and 137

alloc_alpha = 2.5 * alloc_alpha

I would suggest to keep some margin and not put the leverage at 3 exactly to let time to the orders to be executed in case you're above and need to adjust.

Be aware that running on min will give you different results then running on hour bars. your back-test may be under 3 with 1h bars but may well jump above 3 with min bars.