Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Issue with Leverage

I'm trying to get my risk parameters taken care of and have gotten 5 out of 7. The leverage for some reason is acting funky. It seems to be closing out on the very last day for no reason that I can think of. The program is using external data with new portfolio positions available at the end of the month for positions to be executed on the first trading day of the month. Can anyone give me some aid in helping me understand why this is occuring?

2 responses

The drop in leverage is caused by the MaxTurnover constraint. Turnover is defined as dollars traded as a percent of the total portfolio value. If the optimize function tries to open a large number of positions, this constraint will prevent them from opening to minimize turnover. The result of this less trading is more cash in one's account. More cash means less leverage.

This is exacerbated due to the algorithm only trading once a month. There seems to be very few securities which are the same from month to month. This 'churn' causes a high turnover on the first of the month. The MaxTurnover constraint steps in and limits the churn, but also increases cash therefore reducing leverage. The 're-buy' function called on the following day orders just about the same securities as the day before. With less 'churn' on the second day the MaxTurnover constraint doesn't kick in so leverage goes back closer to 1.

How to troubleshoot this? I find it very helpful to use the calculate_optimal_portfolio first and then pass the result to the order_optimal_portfolio method. Something like this

    weights = opt.calculate_optimal_portfolio(objective=target_weights,  
                                 constraints=[constrain_pos_size,  
                                              max_leverage,  
                                              dollar_neutral,  
                                              #max_turnover,  
                                              ]  
                                              )  
    algo.order_optimal_portfolio(objective=opt.TargetWeights(weights),  
                                 constraints=[]  
                                 )  
    record(weight1=weights.abs().sum())

By doing this, one can inspect the results of the optimizer and potentially make adjustments. Using the order_optimal_portfolio alone doesn't give visibility to the optimized weights. One can then record the sum of the weights of all the securities being ordered.

The attached backtest is very similar to the original. In place of the factor based on self serve data I used a dummy factor based upon price. The algo however exhibits the same drop in leverage at the first of each month. Note especially the two recorded variables 'weight1' and 'weight2' which are the sum of the optimizer weights on the first and second day of the month respectively. Notice that 'weight1' often is less than 1 while on the next day 'weight2' is usually very close to 1. This is the impact of the MaxTurnover constraint limiting the orders in order to limit turnover.

Generally, it's not encouraged to use the MaxTurnover constraint. It's typically better to control turnover systemically in the strategy. In this case, since the requirement is to hold positions 3-40 days on average, one could probably meet that just by trading monthly and carrying over very few securities each month.

The attached backtest shows a similar algo using the MaxTurnover constraint and exhibiting the same periodic drop in leverage.

The next backtest (in the following reply) shows the same algo but WITHOUT the MaxTurnover constraint. Everything else is the same. Notice it keeps leverage close to 1 without the periodic dip.

Disclaimer

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.

The same algo as above but without the MaxTurnover constraint . Notice there are no periodic dips in leverage.

Hope this helps.