One of the simplest and best-known trading strategies used by traders for a long time is the SMA crossovers. When a simple moving average (SMA) of a faster period crosses over a slower period SMA for long positions this is interpreted as a buy signal. When the slower period SMA crosses over the faster period then this is interpreted as a sell signal. You may have heard about the golden cross and the death cross. They are specific fast and slow SMA periods. Traditionally the fast period is 50 and slow is 200 for checking the golden cross and the death cross. In this article, I look for the best short and long time periods using their crossover as buy and long signals. I want to remind you that this is not investment or trading advice, backtest results are often unreliablie.
We only look at long positions:
- SMA(short) crosses over SMA(long)
- For example: SMA(50) crossover SMA(200)
- SMA(long) crosses over SMA(short)
- For example: SMA(200) crossover SMA(50)
The Pine Script Code
strategy("SMA Crossover Best", overlay=true, shorttitle = "SMACB", initial_capital = 1000, default_qty_value = 100, default_qty_type = strategy.percent_of_equity, commission_value = 0.02) n1 = 50 n2 = 200 n3 = 50 n4 = 200 sma1 = sma(close,n1) sma2 = sma(close,n2) sma3 = sma(close,n3) sma4 = sma(close,n4) CrossedUp = crossover(sma1,sma2) CrossedDown = crossover(sma4,sma3) notInTrade = strategy.position_size <= 0 inTrade = strategy.position_size > 0 timePeriod = time >= timestamp(syminfo.timezone, 2010, 12, 15, 0, 0) if (timePeriod and notInTrade and CrossedUp) strategy.entry("long", strategy.long, when=notInTrade) if (inTrade and CrossedDown) strategy.close("long") plot(sma1,color=color.red) plot(sma2,color=color.blue) plot(sma3,color=color.red) plot(sma4,color=color.yellow)
- We define four variables for the fast and slow periods of SMAs. n1, n2, n3, and n4
- Then we calculate the SMA values for these periods.
- Next we determine when the SMA’s cross over.
- Crossed up means the buy signal
- Crossed down means the sell signal
- We define the variable timeperiod so that if we want to we can limit our backtest within a time frame we specify
- We also define the variable inTrade and notInTrade to determine when we are in a trade
- Then we have the buy conditions: when we are not in trade and SMA crossover up happens we place the buy order
- Finally we have the sell conditions: when we are in trade and SMA crossover down happends we close our position
- Then we add the script to the chart.
The idea of optimization
We want to find the optimal fast and slow periods for the SMAs. We will define four distinct periods and call them n1, n2, n3, and n4. Using Backtesting.py I obtained the optimal values for these parameters.
n1 n2 n3 n4 20 50 20 70 3.599425e+10 60 20 70 3.219502e+10 50 20 80 2.938505e+10 60 20 80 2.774712e+10 30 60 20 80 2.763884e+10 20 50 10 80 2.753161e+10 30 60 20 70 2.720691e+10 10 40 10 60 2.676517e+10 30 40 20 70 2.673867e+10 10 40 10 80 2.661913e+10
- n1 is the short periods for entry
- n2 is the long period for entry
- n3 is the short period for exit
- n4 is the long periods for exit
The best periods for daily long only trades are:
n1 = 20 n2 = 50 n3 = 20 n4 = 70
You can replace n1, n2, n3 and n4 with these optimal values to do the backtest yourself.
Start 2011-12-31 00:00:00 End 2021-07-02 00:00:00 Duration 3471 days 00:00:00 Exposure Time [%] 67.685212 Equity Final [$] 35994250055.293922 Equity Peak [$] 39778719022.693924 Return [%] 3599325.005529 Buy & Hold Return [%] 716650.655022 Return (Ann.) [%] 201.575534 Volatility (Ann.) [%] 249.242479 Sharpe Ratio 0.808753 Sortino Ratio 4.263941 Calmar Ratio 2.730627 Max. Drawdown [%] -73.820226 Avg. Drawdown [%] -9.161531 Max. Drawdown Duration 1055 days 00:00:00 Avg. Drawdown Duration 37 days 00:00:00 # Trades 32 Win Rate [%] 59.375 Best Trade [%] 923.238077 Worst Trade [%] -21.650557 Avg. Trade [%] 38.797742 Max. Trade Duration 204 days 00:00:00 Avg. Trade Duration 73 days 00:00:00 Profit Factor 21.872544 Expectancy [%] 86.566846 SQN 1.233446
- We made five times the buy and hold return
- The win rate was about 60%.
- We only made 32 trades and our exposure time was about 68%.
- Note the huge -73% maximum drawdown.