本策略运用双均线指标和三指数均线指标,结合随机指标,形成一个较为稳定可靠的趋势追踪交易策略。其主要思想是在均线指标判断出现金叉或死叉时,发出交易信号;而随机指标则用于辅助判断超买超卖情况,避免在市场剧烈波动时产生错误信号。
该策略主要由四个部分组成:
双均线指标:分别计算50周期和100周期的指数移动平均线(EMA),当短期EMA上穿长期EMA时产生买入信号,下穿时产生卖出信号。
三指数指标:分别计算50周期、100周期和200周期的指数移动平均线,用于判断市场趋势方向。当50EMA>100EMA>200EMA时为多头市场,50EMA<100EMA<200EMA时为空头市场。
随机指标:计算RSI的6日K值和D值,判断超买超卖情况。K值上穿D值时为超卖,下穿时为超买。
交易信号:只有在双均线指标产生信号的同时,市场也符合三指数均线的多头或空头状态,且随机指标未显示超买超卖时,才发出真正的交易指令。
这套策略综合运用均线指标和随机指标的优点,在发出交易信号时既考虑了趋势方向的判断,也参考了市场的超买超卖状态,从而可以较好的过滤噪音,追踪较为明确的趋势。另外,它采用三指数均线来判断整体趋势,使得信号更加可靠。这套策略简单易懂,容易实施,也易于优化。
该策略最大的风险在于它依赖指标判断,当指标发出错误信号时容易导致交易失败。此外,采用较长周期均线指标判断整体趋势时,也可能错过短期机会。主要的风险对策如下:
优化指标参数,调整双均线和三指数均线的周期组合,使其更加匹配市场特征。
结合更多指标进行CANCEL操作,在判断市场出现剧烈波动时中止当前交易。
采用短线多头策略进行辅助,在长线多头市场中利用短期机会进行盈利。
该策略主要可以从以下几个方面进行优化:
调整双均线和三指数均线的周期参数,优化指标顺应市场特征。
增加VOLUME和MACD等指标判断,避免价格异常造成错误信号。
利用candle模式更好确认趋势,避免短期回撤后的错误信号。
扩展至股票、外汇等更多品种,检验策略的适应性。
结合VIX指标判断整体市场波动率,控制仓位规模。
本策略利用双均线指标发出交易信号,三指数均线和随机指标进行辅助判断,从而构建一个较为稳定的趋势追踪策略。它简单易懂,易于实施,与市场特征匹配度高,收益较为稳定,是一套值得推荐的量化策略。通过针对性优化,有望获得更好的效果。
/*backtest
start: 2023-12-07 00:00:00
end: 2023-12-12 08:00:00
period: 1m
basePeriod: 1m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=5
strategy(title='5212 EMA Strategy', shorttitle='5212 EMA', overlay=true, pyramiding=0, default_qty_type=strategy.percent_of_equity, default_qty_value=10, calc_on_every_tick=false)
//**Backtest Date sof
useStartPeriodTime = input.bool(true , 'Start Date & Time' , group='Date Range' , inline='Start Period')
startPeriodTime = input(timestamp('16 Apr 2021') , '' , group='Date Range' , inline='Start Period')
useEndPeriodTime = input.bool(false , 'End Date & Time' , group='Date Range' , inline='End Period')
endPeriodTime = input(timestamp('31 Dec 2222') , '' , group='Date Range' , inline='End Period')
enableHighlight = input.bool(false , 'Highlight' , group='Date Range' , inline='Highlight')
highlightType = input.string('Anchors' , '' , group='Date Range' , inline='Highlight' , options=['Anchors', 'Background'])
highlightColor = input.color(color.white , '' , group='Date Range' , inline='Highlight')
start = useStartPeriodTime ? startPeriodTime >= time : false
end = useEndPeriodTime ? endPeriodTime <= time : false
calcPeriod = true
// var line startAnchor = line.new(na, na, na, na, xloc.bar_time, extend.both, highlightColor, width=2)
// var line endAnchor = line.new(na, na, na, na, xloc.bar_time, extend.both, highlightColor, width=2)
// useBgcolor = false
// if enableHighlight
// if highlightType == 'Anchors'
// if useStartPeriodTime
// line.set_xy1(startAnchor, startPeriodTime, low)
// line.set_xy2(startAnchor, startPeriodTime, high)
// if useEndPeriodTime
// line.set_xy1(endAnchor, calcPeriod ? time : line.get_x1(endAnchor), low)
// line.set_xy2(endAnchor, calcPeriod ? time : line.get_x1(endAnchor), high)
// if highlightType == 'Background'
// useBgcolor := true
// useBgcolor
// bgcolor(useBgcolor and calcPeriod ? color.new(highlightColor,90) : na, editable=false)
//**Backtest Date eof
src =input(close , 'Source' , group='Support')
showEMA = input(true , 'Show EMA' , group='Support')
//**Stochastic RSI sof
smoothK = input.int(6 , "K" , group='Stochastic RSI' , minval=1)
smoothD = input.int(6 , "D" , group='Stochastic RSI' , minval=1)
lengthRSI = input.int(28 , "RSI Length" , group='Stochastic RSI' , minval=1)
lengthStoch = input.int(28 , "Stoch Length" , group='Stochastic RSI' , minval=1)
rsi1 = ta.rsi(src, lengthRSI)
k = ta.sma(ta.stoch(rsi1, rsi1, rsi1, lengthStoch), smoothK)
d = ta.sma(k, smoothD)
//**STochastic RSI eof
//** EMA sof
emain01 = input.int(50 , "EMAma Girang" , group='Moving Average Exponential' , minval=1)
emain02 = input.int(100 , "EMAma Muda" , group='Moving Average Exponential' , minval=1)
emain03 = input.int(200 , "EMAma Tua" , group='Moving Average Exponential' , minval=1)
ema01 = ta.ema(src, emain01)
ema02 = ta.ema(src, emain02)
ema03 = ta.ema(src, emain03)
plot(showEMA ? ema01 : na, 'EMAma Girang' , color = color.new(color.orange, 0))
plot(showEMA ? ema02 : na, 'EMAma Muda' , color = color.new(color.blue, 0))
plot(showEMA ? ema03 : na, 'EMAma Tua' , color = color.new(color.red, 0))
//** EMA eof
//**Condition sof
emaLong = ema01 > ema02 and ema02 > ema03 and low > ema03
emaShort = ema01 < ema02 and ema02 < ema03 and high < ema03
longCond = ta.crossover(k,d) and k <= 23 and emaLong
shortCond = ta.crossunder(k,d) and k >= 77 and emaShort
longClose = ta.crossunder(k,d) and k <= 77
shortClose = ta.crossover(k,d) and k >= 23
longCross = ta.crossover(ema01, ema02)
shortCross = ta.crossunder(ema01, ema02)
//**Condition eof
//**Strategy sof
if calcPeriod and longCond
strategy.entry('long', strategy.long, when=longCond, comment='EN Long')
strategy.close('long', when=shortClose, comment='EX Long')
strategy.close('long', when=shortCross, comment='MD Short')
if calcPeriod and shortCond
strategy.entry('short', strategy.short, when=shortCond, comment='EN Short')
strategy.close('short', when=longClose, comment='EX Short')
strategy.close('short', when=longCross, comment='MD Long')
if calcPeriod == false and ta.crossover(ema01, ema02) or ta.crossunder(ema01, ema02)
strategy.cancel('long')
strategy.cancel('short')
//**Strategy eof
//**Label sof
entryText = str.tostring(strategy.position_avg_price, '##.###')
longText = 'Long Entry : ' + entryText
shortText = 'Short Entry : ' + entryText
noTrade = 'Sleeping Mode'
LongTrade = strategy.position_size > 0
ShortTrade = strategy.position_size < 0
Tekslabel = LongTrade ? longText : ShortTrade ? shortText : noTrade
xPosition = timenow + math.round(ta.change(time)*1)
yPosition = ta.highest(1)
labelColor = LongTrade ? color.new(color.aqua, 0) : ShortTrade ? color.new(color.red, 0) : color.new(color.gray, 0)
textColor = LongTrade ? color.new(color.black, 0) : ShortTrade ? color.new(color.white, 0) : color.new(color.white, 0)
// lab_l = label.new(
// xPosition, yPosition, Tekslabel,
// color=labelColor,
// textcolor=textColor,
// style = label.style_label_left,
// textalign=text.align_left,
// xloc=xloc.bar_time, yloc = yloc.price)
// label.delete(lab_l[1])
//**Strategy eof