该策略通过计算两条不同参数设置的移动平均线,并比较其交叉情况来判断价格趋势方向,从而实现趋势追踪交易。当快速移动平均线从下方突破慢速移动平均线时,判断为看涨信号;当快速移动平均线从上方下破慢速移动平均线时,判断为看跌信号。该策略可以通过参数设置实现对不同周期的趋势判断。
本策略使用两组不同参数设置的移动平均线进行比较,第一个移动平均线参数由len1和type1设置,第二个移动平均线参数由len2和type2设置。其中len1和len2分别代表两条移动平均线的周期长度,type1和type2代表移动平均线的算法类型。
当快速移动平均线从下方突破慢速移动平均线形成金叉时,判断为看涨信号;当快速移动平均线从上方下破慢速移动平均线形成死叉时,判断为看跌信号。
根据交叉信号的方向,执行做多或做空操作。当看涨信号触发时,如果needlong参数为true,则按照default_qty_value的数量或仓位percentage_of_equity的百分比做多;当看跌信号触发时,如果needshort参数为true,则按照default_qty_value的数量或仓位percentage_of_equity的百分比做空。
移动平均线具有滞后性,可能错过价格反转点 解决方法:适当缩短移动平均线周期,或与其他指标组合使用
不适用于具有高波动率和频繁反转的市场 解决方法:增加过滤条件,避免在震荡行情中交易
存在一定的假信号风险 解决方法:加入其他过滤指标进行组合,提高信号的可靠性
本策略通过比较两条移动平均线的交叉情况判断价格趋势,并进行相应的看涨看跌操作,从而实现对趋势的捕捉和跟踪获利。策略优点是信号规则简单清晰,参数可调节,适用性强,能对多种市场环境进行优化调整。需要注意防范移动平均线滞后以及震荡行情的风险,可通过加入其它指标进行过滤以提高信号质量。
/*backtest start: 2024-01-01 00:00:00 end: 2024-01-31 23:59:59 period: 1h basePeriod: 15m exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}] */ //@version=2 strategy(title = "Noro's MAs Cross Tests v1.0", shorttitle = "MAs Cross tests 1.0", overlay=true, default_qty_type = strategy.percent_of_equity, default_qty_value = 100.0, pyramiding = 0) needlong = input(true, "long") needshort = input(true, "short") len2 = input(15, defval = 15, minval = 2, maxval = 1000, title = "Fast MA length") type2 = input(1, defval = 1, minval = 1, maxval = 7, title = "Fast MA Type") src2 = input(close, defval = close, title = "Fast MA Source") len1 = input(30, defval = 30, minval = 2, maxval = 1000, title = "Slow MA length") type1 = input(1, defval = 1, minval = 1, maxval = 7, title = "Slow MA Type") src1 = input(close, defval = close, title = "Slow MA Source") col = input(false, defval = false, title = "Color of bar") o = input(false, title = "1 SMA, 2 EMA, 3 VWMA, 4 DEMA, 5 TEMA, 6 KAMA, 7 Price Channel") //DEMA 1 dema1 = 2 * ema(src1, len1) - ema(ema(close, len1), len1) //TEMA 1 xEMA1 = ema(src1, len1) xEMA2 = ema(xEMA1, len1) xEMA3 = ema(xEMA2, len1) tema1 = 3 * xEMA1 - 3 * xEMA2 + xEMA3 //KAMA 1 xvnoise = abs(src1 - src1[1]) nfastend = 0.20 nslowend = 0.05 nsignal = abs(src1 - src1[len1]) nnoise = sum(xvnoise, len1) nefratio = iff(nnoise != 0, nsignal / nnoise, 0) nsmooth = pow(nefratio * (nfastend - nslowend) + nslowend, 2) kama1 = nz(kama1[1]) + nsmooth * (src1 - nz(kama1[1])) //PriceChannel 1 lasthigh1 = highest(src1, len1) lastlow1 = lowest(src1, len1) center1 = (lasthigh1 + lastlow1) / 2 //DEMA 2 dema2 = 2 * ema(src2, len2) - ema(ema(close, len2), len2) //TEMA 2 xEMA12 = ema(src2, len2) xEMA22 = ema(xEMA12, len2) xEMA32 = ema(xEMA22, len2) tema2 = 3 * xEMA12 - 3 * xEMA22 + xEMA32 //KAMA 2 xvnoise2 = abs(src2 - src2[1]) nfastend2 = 0.20 nslowend2 = 0.05 nsignal2 = abs(src2 - src2[len2]) nnoise2 = sum(xvnoise2, len2) nefratio2 = iff(nnoise2 != 0, nsignal2 / nnoise2, 0) nsmooth2 = pow(nefratio2 * (nfastend2 - nslowend2) + nslowend2, 2) kama2 = nz(kama2[1]) + nsmooth2 * (src2 - nz(kama2[1])) //PriceChannel 2 lasthigh2 = highest(src2, len2) lastlow2 = lowest(src2, len2) center2 = (lasthigh2 + lastlow2) / 2 //MAs ma1 = type1 == 1 ? sma(src1, len1) : type1 == 2 ? ema(src1, len1) : type1 == 3 ? vwma(src1, len1) : type1 == 4 ? dema1 : type1 == 5 ? tema1 : type1 == 6 ? kama1 : type1 == 7 ? center1 : 0 ma2 = type2 == 1 ? sma(src2, len2) : type2 == 2 ? ema(src2, len2) : type2 == 3 ? vwma(src2, len2) : type2 == 4 ? dema2 : type2 == 5 ? tema2 : type2 == 6 ? kama2 : type2 == 7 ? center2 : 0 plot(ma1, color = blue, linewidth = 3, transp = 0) plot(ma2, color = red, linewidth = 3, transp = 0) //Signals trend = ma2 > ma1 ? 1 : ma2 < ma1 ? -1 : trend[1] up = trend == 1 and ((close < open and close[1] < open[1]) or col == false) dn = trend == -1 and ((close > open and close[1] > open[1]) or col == false) if up strategy.entry("Long", strategy.long, needlong == false ? 0 : na) if dn strategy.entry("Short", strategy.short, needshort == false ? 0 : na)