This is a simple SMA Crossover strategy that longs/shorts the SPY during strong trends, and liquidates during times of high market volatility, to withstand whipsaws (false signals). This liquidation function is especially helpful during 2011 and 2015, as it would have suffered from greater drawdowns otherwise.
The 3 main components are as follows:
1. When the 50 day SMA goes above the 200 day SMA, long the SPY.
2. When the 50 day SMA goes below the 200 day SMA, short the SPY.
3. If the unrealized losses on any given day exceed $2000, liquidate. This was done by using:
if (profitloss <= -2000)
with profitloss being:
var profitloss = Portfolio[_symbol].UnrealizedProfit;
Please feel free to make any suggestions on how this can be improved.
Ruslan
Here is the backtest with $10,000 starting capital:
Notice how the amount for the liquidation component was changed from $2,000 to around $400 to reflect similar returns.
If you changed the starting capital to 1 million, for example, then you would use 50k.
if (profitloss <= -50000)
Evil Corp
Ruslan Shamailov , I have some suggestion as a trader on your code, can you change the times frame from dailys to other periods such as hours, 30min, 15min, 5min intervals? ... also using SMA, use for example SMA(X)=SM(Y) or if better if you can this, if SMA(X) <SM(Y) for short signal, and SMA(Y)>SMA(X) Bull signal.
Amulya Jitendra Agarwal
What does:
// only once per day
if (_previous.Date == Time.Date) return;
supposed to do?Â
Rahul Chowdhury
Hey Amulya,.
if (_previous.Date == Time.Date) return;
Restricts the algorithm to trade only once a day. _previous is set to today at the end of OnData.
public void OnData(TradeBars data) { // only once per day if (_previous.Date == Time.Date) return; ...... _previous = Time; }
Best
Rahul
Amulya Jitendra Agarwal
Thank you Rahul,. So if I do not have any restrictions on frequency of trading then I think I can delete that, right? Just confirming. Also, is there any way in which we can set the stop loss and use SetHoldings()? The stoplimitorder() sometimes doesn't get filled. FULL code and issue here:Â https://www.quantconnect.com/forum/discussion/8309/moving-average-cross-in-python-with-stop-loss/p1
PokerWinner
Do you know where I can find a Python version of this algorithm?Â
Vladimir
Pietro Petitti,
If you open a new thread with your question, I will try to port it to pythonÂ
Varad Kabade
Hi Pietro Petitti,
Please find the Python version of the above algorithm in the attached backtest. Note that we have not made the changes regarding the Indicator period; it is the same as the original algorithm.
Best,
Varad Kabade
PokerWinner
Great, thanks VarandÂ
Mykola Dobrochynskyy
Hi,
here is a new Variant with EMA-Indicators.
Regards
Mykola
AlMoJo
Hello guys,
Â
Here is another one that has as a long condition: sma_value[0] ≥ sma_value[1]  (actual SMA value higher than precedent).
Â
Cheers :)
Ruslan
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!