Following is a backtest of a simple strategy (which has not performed well in 2005-2020, even though it's based on a profitable strategy, which is surprising).
The rules are:
Universe: SPY and IEF
Timeframe: Daily
Position size: 50%
Buy rules: After the market closes, buy on Market-On-Open order if the 3-day cumulative RSI(2) < 15.
Use a stoploss with 2*ATR(1) below the open price (which is the same as fill price)
Sell rules: After the market closes, sell if RSI(2) > 70 using MOO order.
Needing almost 80 lines of code for this simple strategy seems a bit too much. Can the code be made more efficient/smaller?
Also: is there an easy way to 'attach' a stop order to a market order such that when the position gets closed,
the stop order is automatically cancelled?
Derek Melchin
Hi Hsiao,
We can make the algorithm more efficient by subscribing to daily data. By doing so, we can remove the time check that's in OnData.
if not data.ContainsKey(self.spy) or \ not data.ContainsKey(self.ief) or \ not self.Time.hour == 16 or not self.Time.minute == 0: return
Additionally, we can remove the History call that's inside OnOrderEvent. Since this history call is just calculating the 1-period ATR, we can setup an indicator for this.
Also note that the indicators in the algorithm above are not warmed up, so the strategy doesn't begin trading right away. In the attached backtest, I've moved all the data unique to each security into its own class. This provides a space for us to warm up the indicators and rolling window.
Refer to the attached backtest.
Best,
Derek Melchin
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 QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect 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. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Hsiao Shi
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 QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect 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. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!