本策略基于三条移动平均线的金叉死叉形态进行交易。当快速移动平均线上穿中速线并中速线上穿慢速线时做多;当快速移动平均线下穿中速线并中速线下穿慢速线时做空。
具体来说,该策略利用三条不同周期移动平均线间的交叉进行交易。快速线代表当前短期趋势,中速线代表中期趋势,慢速线代表长期趋势。当短中长三条均线顺序发生向上交叉时,说明趋势启动,做多;当发生向下交叉时,说明趋势反转,做空。还可设置入场延迟来过滤短期假突破。
可以通过调整持仓时间,优化均线参数,引入止损策略等方式来管理风险。
本策略基于三均线交叉判断趋势方向进行持仓。优点是交易信号简单明确,可配置性强;缺点是容易滞后且需要参数优化。可通过参数调优、止损策略等提高效果,并控制回撤风险。该策略帮助交易者掌握移动平均线的应用及多均线交叉的交易思路。
/*backtest
start: 2023-08-21 00:00:00
end: 2023-09-20 00:00:00
period: 3h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
// © DaynTrading
//@version=4
// strategy(
// title="Simple Moving Average Cross",
// overlay=true,
// initial_capital=5000,
// default_qty_type=strategy.percent_of_equity,
// default_qty_value=2,
// commission_type=strategy.commission.percent,
// commission_value=0.075,
// pyramiding=0
// )
sma_top_input = input(title="SMA Top", type=input.integer, defval=20)
sma_mid_input = input(title="SMA Mid", type=input.integer, defval=50)
sma_low_input = input(title="SMA Low", type=input.integer, defval=200)
bars_long = input(title="Long: After trigger, how many bars to wait?", type=input.integer, defval=5)
bars_short = input(title="Short: After trigger, how many bars to wait?", type=input.integer, defval=5)
sma_top = sma(close, sma_top_input)
sma_mid = sma(close, sma_mid_input)
sma_low = sma(close, sma_low_input)
long = sma_top > sma_mid and sma_mid > sma_low
short = sma_top < sma_mid and sma_mid < sma_low
long_condition = long and long[bars_long] and not long[bars_long + 1]
short_condition = short and short[bars_short] and not short[bars_short + 1]
close_long = sma_top < sma_mid and sma_mid < sma_low and not long[bars_long + 1]
close_short = sma_top > sma_mid and sma_mid > sma_low and not short[bars_short + 1]
plot(sma_top, title="SMA Top", color=#95f252, linewidth=2)
plot(sma_mid, title="SMA Mid", color=#FF1493, linewidth=2)
plot(sma_low, title="SMA Low", color=#6a0dad, linewidth=2)
strategy.entry("LongPosition", strategy.long, when = long_condition)
strategy.entry("ShortPosition", strategy.short, when = short_condition)
strategy.close("LongPosition", when = close_short)
strategy.close("ShortPosition", when = close_long)