The purpose of this strategy is to identify potential trend reversal points by observing the crossover between the 20-period Exponential Moving Average (EMA) and the 20-period Simple Moving Average (SMA). It decides to go long or go short based on the direction of the crossover.
The strategy uses the crossover and crossunder functions from the ta library to detect moving average crossovers.
The strategy combines the trend following capability of moving averages and the signal generation of crossover events, having the following advantages:
The strategy also has the following risks:
Solutions:
The strategy can also be improved in the following aspects:
The strategy is relatively simple and practical overall, identifying potential trend reversal points through moving average crossover theory. But there is also room for improvement via additional indicators, dynamic parameters, stop losses, algorithmic trading etc. to make the strategy more robust, reliable and automated. In summary, it provides a good template for getting started with quantitative trading.
/*backtest start: 2022-12-28 00:00:00 end: 2024-01-03 00:00:00 period: 1d basePeriod: 1h exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}] */ //@version=5 strategy("EMA-SMA Crossover Strategy", overlay=true) // Define the length of the moving averages emaLength = 20 smaLength = 20 // Calculate moving averages emaValue = ta.ema(close, emaLength) smaValue = ta.sma(close, smaLength) // Buy condition buyCondition = ta.crossover(emaValue, smaValue) and close > emaValue // Short sell condition sellCondition = ta.crossunder(emaValue, smaValue) and close < emaValue // Exit conditions for both Buy and Short sell exitBuyCondition = ta.crossunder(emaValue, smaValue) exitSellCondition = ta.crossover(emaValue, smaValue) // Strategy logic if (buyCondition) strategy.entry("Buy", strategy.long) if (sellCondition) strategy.entry("Sell", strategy.short) if (exitBuyCondition) strategy.close("Buy") if (exitSellCondition) strategy.close("Sell") // Plot the moving averages plot(emaValue, color=color.blue, title="20 EMA") plot(smaValue, color=color.red, title="20 SMA")