The moving average crossover strategy generates buy and sell signals by calculating the crossover between two moving averages of different periods. A long signal is generated when the shorter period MA crosses above the longer period MA, while a short signal is generated on the downward crossover.
For example, going long when the 5-day MA crosses above the 21-day MA, and closing the long when the 5-day MA crosses back below the 21-day MA.
The trading logic is:
Different MA period combinations can suit short or long term trends.
The MA crossover strategy uses MA crosses to generate signals, with adjustable periods to fit market cycles. A simple trend following approach, but lagging MAs and whipsaw risk need caution. Consider combining with other indicators for filtration and optimization.
/*backtest start: 2023-08-14 00:00:00 end: 2023-09-13 00:00:00 period: 2h basePeriod: 15m exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}] */ //@version=3 strategy("My Strategy", overlay=true) longCondition = crossover(sma(close, 5), sma(close, 21)) if (longCondition) strategy.entry("My Long Entry Id", strategy.long) shortCondition = crossunder(sma(close, 14), sma(close, 28)) if (shortCondition) strategy.entry("My Short Entry Id", strategy.short)