本策略通过动态选择不同类型的移动平均线,并结合多个时间周期,实现交易信号的产生。
该策略允许选择 SMA、EMA、TEMA、WMA、HMA 五种移动平均线指标,并设置均线的周期长度。策略会根据选择动态绘制不同类型的均线。当收盘价上涨突破均线时,做多;当收盘价下跌突破均线时,做空。
具体来说,策略首先根据输入参数定义回测周期。然后计算五种均线指标:
根据选择,绘制相应的均线。当收盘价高于均线时,做多;收盘价低于均线时,做空。
该策略通过组合使用不同类型的均线,可以平滑价格数据,过滤市场噪音,产生较为可靠的交易信号。而允许自定义均线周期长度,可以针对不同周期的趋势进行交易。
可以通过优化以下方面来降低风险:
该策略可以从以下几个方向进行优化:
例如可以加入量能指标,只有在成交量放大的情况下才产生交易信号,过滤掉一些假突破。
可以设置通道,只有在价格突破通道时才入场;设置止损线,价格触碰止损线后平仓。这可以减少不必要的损失。
可以根据市场状况动态调整均线周期,在趋势更明显时使用长周期均线,在盘整时使用短周期均线。
可以根据回撤情况调整仓位大小,在回撤时减小仓位,在盈利时适度加大仓位。
该策略通过组合应用多种均线指标,结合多个时间周期,形成较为稳定的趋势跟踪效果。策略优化空间较大,可以从入场过滤、出场方式、参数优化等方面进行改进,使策略在实盘中获得更好的效果。
/*backtest
start: 2022-10-20 00:00:00
end: 2023-10-26 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=3
strategy("MA_strategy ", shorttitle="MA_strategy", overlay=true, initial_capital=100000)
qty = input(100000000, "Buy quantity")
testStartYear = input(2017, "Backtest Start Year")
testStartMonth = input(1, "Backtest Start Month")
testStartDay = input(1, "Backtest Start Day")
testStartHour = input(0, "Backtest Start Hour")
testStartMin = input(0, "Backtest Start Minute")
testPeriodStart = timestamp(testStartYear,testStartMonth,testStartDay,testStartHour,testStartMin)
testStopYear = input(2099, "Backtest Stop Year")
testStopMonth = input(1, "Backtest Stop Month")
testStopDay = input(30, "Backtest Stop Day")
testPeriodStop = timestamp(testStopYear,testStopMonth,testStopDay,0,0)
testPeriodBackground = input(title="Color Background?", type=bool, defval=true)
testPeriodBackgroundColor = testPeriodBackground and (time >= testPeriodStart) and (time <= testPeriodStop) ? #00FF00 : na
bgcolor(testPeriodBackgroundColor, transp=97)
testPeriod() =>
time >= testPeriodStart and time <= testPeriodStop ? true : false
ma1 = input( "SMA",title="Select MA", options=["SMA", "EMA","TEMA", "WMA","HMA"])
len1 = input(7, minval=1, title="Period")
s=sma(close,len1)
e=ema(close,len1)
xEMA1 = ema(close, len1)
xEMA2 = ema(xEMA1, len1)
xEMA3 = ema(xEMA2, len1)
t = 3 * xEMA1 - 3 * xEMA2 + xEMA3
f_hma(_src, _length)=>
_return = wma((2 * wma(_src, _length / 2)) - wma(_src, _length), round(sqrt(_length)))
h = f_hma(close, len1)
w = wma(close, len1)
ma = ma1 == "SMA"?s:ma1=="EMA"?e:ma1=="WMA"?w:ma1=="HMA"?h:ma1=="TEMA"?t:na
buy= close>ma
sell= close<ma
alertcondition(buy, title='buy', message='buy')
alertcondition(sell, title='sell', message='sell')
ordersize=floor(strategy.equity/close)
if testPeriod()
strategy.entry("long",strategy.long,ordersize,when=buy)
strategy.close("long", when = sell )