
This strategy is a trading system based on the crossing signals of 1/2/4-period Simple Moving Averages (SMA). It captures market trend reversal points by observing the simultaneous crossing of short-period and medium-period moving averages over the long-period moving average, achieving trend following and timely stop-loss. The strategy design is concise, efficient, and easy to understand and implement.
The core of the strategy utilizes three SMAs of different periods (1/2/4), determining buy signals when both short-period (1-period) and medium-period (2-period) moving averages simultaneously cross above the long-period (4-period) moving average. Conversely, sell signals are generated when both shorter moving averages cross below the longer one. This multiple confirmation mechanism effectively reduces false signals and improves trading accuracy. In implementation, ta.crossover() and ta.crossunder() functions are used to detect crossovers, initiating long positions when buy conditions are met and short positions when sell conditions are satisfied.
This strategy captures market trends through multiple moving average crossovers, with clear design concepts and simple yet effective implementation. While it has certain lag and false signal risks, reasonable parameter optimization and supplementary indicators can build a more comprehensive trading system. The strategy offers strong extensibility and serves as an excellent foundation for further optimization and refinement.
/*backtest
start: 2024-10-20 00:00:00
end: 2025-02-18 08:00:00
period: 1d
basePeriod: 1d
exchanges: [{"eid":"Binance","currency":"BTC_USDT"}]
*/
//@version=6
strategy("1/2/4 Moving Average STR 1.0.0", overlay=true)
o_length = input(1, title="1 Closed")
t_length = input(2, title="2 Closed")
f_length = input(4, title="4 Closed")
// Calculate the simple moving averages.
ma_o = ta.sma(close, o_length)
ma_t = ta.sma(close, t_length)
ma_f = ta.sma(close, f_length)
// Plot the moving averages on the chart.
plot(ma_o, color=color.green, title="1 MA")
plot(ma_t, color=color.red, title="2 MA")
plot(ma_f, color=color.blue, title="4 MA")
// Assign the crossover and crossunder results to global variables.
crossover_o = ta.crossover(ma_o, ma_f)
crossover_t = ta.crossover(ma_t, ma_f)
crossunder_o = ta.crossunder(ma_o, ma_f)
crossunder_t = ta.crossunder(ma_t, ma_f)
// Generate signals based on the global crossover variables.
// Buy signal: both 1 and 2 SMAs cross over the 4 SMA on the same bar.
buy_signal = crossover_o and crossover_t
// Sell signal: both 1 and 2 SMAs cross under the 4 SMA on the same bar.
sell_signal = crossunder_o and crossunder_t
// Enter trades based on the signals.
// For a long position, enter on a buy signal and exit when a sell signal occurs.
if buy_signal
strategy.entry("Long", strategy.long)
if sell_signal
strategy.close("Long")
// For a short position, enter on a sell signal and exit when a buy signal occurs.
if sell_signal
strategy.entry("Short", strategy.short)
if buy_signal
strategy.close("Short")