二重超级趋势

Author: 雨幕(youquant), Date: 2022-05-24 09:47:17
Tags: ATR

简单的SuperTrend(超级趋势)信号指标使您可以选择在一个策略上显示两个SuperTrend(超级趋势)以及其它自定义功能。使用二重的方式灵感来自于Income Sharks。

回测测试

img

img

img


/*backtest
start: 2022-03-08 00:00:00
end: 2022-05-23 00:00:00
period: 30m
basePeriod: 5m
exchanges: [{"eid":"Futures_CTP","currency":"FUTURES"}]
args: [["v_input_3",16],["ContractType","rb2205",360008]]
*/

//@version=5
indicator(title='Concept Dual SuperTrend', shorttitle='ConceptDST', overlay=true, format=format.price, precision=2, timeframe='')

src = input(hl2, title='数据源使用:开盘、收盘、最高、最低 ..')
multiplier = input.float(title='ATR快线乘数', step=0.1, defval=2)
multiplier2 = input.float(title='ATR慢线乘数', step=0.1, defval=3)
periods = input(title='ATR快线', defval=10)
periods2 = input(title='ATR慢线', defval=16)
transp_st = input.float(title='线透明度', step=1, minval=0, maxval=100, defval=70)
transp_hl = input.float(title='着色透明度', step=1, minval=0, maxval=100, defval=90)
changeATR = input(title='更改ATR算法?', defval=true)
highlighting = input(title='显示阴影? (快线)', defval=true)
highlighting2 = input(title='显示阴影? (慢线)', defval=true)
showlabels = input(title='显示买/卖标签?', defval=true)


//------------------------------------------------

// Calculations (Fast)
atrA = ta.sma(ta.tr, periods)
atr = changeATR ? ta.atr(periods) : atrA
up = src - multiplier * atr
upA = nz(up[1], up)
up := close[1] > upA ? math.max(up, upA) : up
dn = src + multiplier * atr
dnA = nz(dn[1], dn)
dn := close[1] < dnA ? math.min(dn, dnA) : dn

// Trend (Fast)
trend = 1
trend := nz(trend[1], trend)
trend := trend == -1 and close > dnA ? 1 : trend == 1 and close < upA ? -1 : trend

upPlot = plot(trend == 1 ? up : na, title='Uptrend (Slow)', style=plot.style_linebr, linewidth=2, color=color.new(color.green, transp_st))
buySignal = trend == 1 and trend[1] == -1
plotshape(buySignal ? up : na, title='Uptrend Start (Slow)', location=location.absolute, style=shape.circle, size=size.tiny, color=color.new(color.green, transp_st))
plotshape(buySignal and showlabels ? up : na, title='Buy', text='Buy', location=location.absolute, style=shape.labelup, size=size.tiny, color=color.new(color.green, 0), textcolor=color.new(color.white, 0))

dnPlot = plot(trend == 1 ? na : dn, title='Downtrend (Slow)', style=plot.style_linebr, linewidth=2, color=color.new(color.red, transp_st))
sellSignal = trend == -1 and trend[1] == 1
plotshape(sellSignal ? dn : na, title='Downtrend Start (Slow)', location=location.absolute, style=shape.circle, size=size.tiny, color=color.new(color.red, transp_st))
plotshape(sellSignal and showlabels ? dn : na, title='Sell', text='Sell', location=location.absolute, style=shape.labeldown, size=size.tiny, color=color.new(color.red, 0), textcolor=color.new(color.white, 0))

mPlot = plot(ohlc4, title='', style=plot.style_circles, linewidth=0, color=na)
longFillColor = highlighting ? trend == 1 ? color.green : color.black : color.black
shortFillColor = highlighting ? trend == -1 ? color.red : color.black : color.black
fill(mPlot, upPlot, title='Uptrend Shading (Slow)', color=color.new(longFillColor, transp_hl))
fill(mPlot, dnPlot, title='Downtrend Shading (Slow)', color=color.new(shortFillColor, transp_hl))

//------------------------------------------------

// Calculations (Slow)
atrA2 = ta.sma(ta.tr, periods2)
atr2 = changeATR ? ta.atr(periods2) : atrA2
up2 = src - multiplier2 * atr2
upA2 = nz(up2[1], up2)
up2 := close[1] > upA2 ? math.max(up2, upA2) : up2
dn2 = src + multiplier2 * atr2
dnA2 = nz(dn2[1], dn2)
dn2 := close[1] < dnA2 ? math.min(dn2, dnA2) : dn2

// Trend (Slow)
trend2 = 1
trend2 := nz(trend2[1], trend2)
trend2 := trend2 == -1 and close > dnA2 ? 1 : trend2 == 1 and close < upA2 ? -1 : trend2

upPlot2 = plot(trend2 == 1 ? up2 : na, title='Uptrend (Slow)', style=plot.style_linebr, linewidth=2, color=color.new(color.green, transp_st))
buySignal2 = trend2 == 1 and trend2[1] == -1
plotshape(buySignal2 ? up2 : na, title='Uptrend Start (Slow)', location=location.absolute, style=shape.circle, size=size.tiny, color=color.new(color.green, transp_st))
plotshape(buySignal2 and showlabels ? up2 : na, title='Buy', text='Buy', location=location.absolute, style=shape.labelup, size=size.tiny, color=color.new(color.green, 0), textcolor=color.new(color.white, 0))

dnPlot2 = plot(trend2 == 1 ? na : dn2, title='Downtrend (Slow)', style=plot.style_linebr, linewidth=2, color=color.new(color.red, transp_st))
sellSignal2 = trend2 == -1 and trend2[1] == 1
plotshape(sellSignal2 ? dn2 : na, title='Downtrend Start (Slow)', location=location.absolute, style=shape.circle, size=size.tiny, color=color.new(color.red, transp_st))
plotshape(sellSignal2 and showlabels ? dn2 : na, title='Sell', text='Sell', location=location.absolute, style=shape.labeldown, size=size.tiny, color=color.new(color.red, 0), textcolor=color.new(color.white, 0))

mPlot2 = plot(ohlc4, title='', style=plot.style_circles, linewidth=0, color=na)
longFillColor2 = highlighting2 ? trend2 == 1 ? color.green : color.black : color.black
shortFillColor2 = highlighting2 ? trend2 == -1 ? color.red : color.black : color.black
fill(mPlot2, upPlot2, title='Uptrend Shading (Slow)', color=color.new(longFillColor2, transp_hl))
fill(mPlot2, dnPlot2, title='Downtrend Shading (Slow)', color=color.new(shortFillColor2, transp_hl))



if buySignal2
    strategy.entry("Enter Long", strategy.long)
else if sellSignal2
    strategy.entry("Enter Short", strategy.short)

相关内容

更多内容