该策略是一个基于1/2/4周期简单移动平均线(SMA)交叉信号的交易系统。通过观察短周期和中周期均线对长周期均线的同向穿越来捕捉市场趋势的转折点,实现趋势跟踪和及时止损。策略设计简洁高效,易于理解和实施。
策略核心是利用三条不同周期(1/2/4)的简单移动平均线,通过判断短周期(1期)和中周期(2期)均线是否同时向上穿越长周期(4期)均线来确定买入信号;反之,当短周期和中周期均线同时向下穿越长周期均线时产生卖出信号。这种多重确认机制可以有效降低虚假信号,提高交易的准确性。具体实现上,使用ta.crossover()和ta.crossunder()函数检测均线交叉,当买入条件满足时开立多头仓位,当卖出条件满足时平仓并开立空头仓位。
该策略通过多重移动平均线的交叉来捕捉市场趋势,设计理念清晰,实现方式简单有效。虽然存在一定的滞后性和假信号风险,但通过合理的参数优化和附加指标的补充,可以构建出一个较为完善的交易系统。策略的可扩展性强,适合作为基础框架进行进一步的优化和完善。
/*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")