Elegant algo Andrew!
I was curious why your algorithm had flat returns until 2011 and it turns out that XIV began trading on Dec 10, 2010 and TMF started trading on April 29, 2009. I adjusted the backtest to run from Dec 10, 2010 when both of the stocks were on the market to reflect more accurate metrics. Pretty darn close to what you had!
If you're interested in iterating it further, take a stab at improving the drawdown. 30% is steep, would you be able to handle those downsides? Perhaps try this calculation: if your returns for the last X days were negative, close the position and when the returns are positive for Y days, re-enter the market.
It might be helpful from an intuition standpoint to compute and plot your effective leverage and market exposure, I'm not that familiar with XIV and TMF, but it looks like they are levered ETFs. You can use the record() function to plot custom variables like leverage or exposure over time.
Finally, if you're looking to live trade, you will need to check the margin on your brokerage account because this algo occasionally dips into negative cash (see attached backtest). This is due to the rounding of order_target_percent as it tries to adjust to your desired position. If you want to have a cash buffer (say 5%), then you can do something like this:
context.weights = 0.95/len(context.sids)
order_target_percent(sid, context.weights)
Thanks for sharing!
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.