SuperTrend+

Electrified · study · 114 行 · 点赞 3,614 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Electrified (electrifiedtrading)
 
//@version=4
study("SuperTrend+", overlay = true, format=format.price, precision=2, resolution="")
 
ATR = "Average True Range", CONFIRM = "Confirmation", DISPLAY = "Display"
WMA = "WMA", EMA = "EMA", SMA = "SMA", VWMA = "VWMA", VAWMA = "VAWMA"
 
mode = input(VAWMA, "Mode", options=[SMA,EMA,WMA,VWMA,VAWMA], group=ATR, tooltip="Selects which averaging function will be used to determine the ATR value.")
atrPeriod = input(120, "Period", group=ATR, tooltip="The number of bars to use in calculating the ATR value.")
atrM = input(3.0, title="Multiplier", step=0.5, group=ATR, tooltip="The multiplier used when defining the super trend limits.")
 
closeBars = input(2, "Closed Bars", minval = 0, group=CONFIRM, tooltip="The number of closed bars that have to exceed the super-trend value before the trend reversal is confirmed.")
 
showsignals = input(false, title="Show Buy/Sell Signals ?", group=DISPLAY)
highlighting = input(true, "Highlighter On/Off ?", group=DISPLAY)
 
vawma(src, len) =>
    sum = 0.0
    vol = 0.0
    for m = 1 to len // m = triangular multiple
        i = len - m
        v = volume[i] * m
        vol := vol + v
        sum := sum + src[i] * v
    sum/vol
////
 
getMA(series, mode, len) =>
  mode==WMA ? wma(series, len) :
  mode==EMA ? ema(series, len) :
  mode==VWMA ? vwma(series, len) :
  mode==VAWMA ? vawma(series, len) :
  sma(series, len)
////
 
superTrendCore(m, h, l, atr) =>
    up = l-(m*atr)
    up1 = nz(up[1], up)
    up := l[1] > up1 ? max(up, up1) : up
    dn = h+(m*atr)
    dn1 = nz(dn[1], dn)
    dn := h[1] < dn1 ? min(dn, dn1) : dn
 
    var lastU = up1
    var lastD = dn1
    var trend = 0
    var confirmation = 0
    var unconfirmed = 0
 
    // cover gap cases
    if(trend != +1 and l > lastD)
        trend := +1
    else if(trend != -1 and h < lastU)
        trend := -1
    
    // confirmed cases
    else if(trend != +1 and h > lastD)
        unconfirmed += 1
        if(confirmation<closeBars and close[1]>lastD)
            confirmation += 1
        if(confirmation>=closeBars)
            trend := +1       
    else if(trend != -1 and l < lastU)
        unconfirmed += 1
        if(confirmation<closeBars and close[1]<lastU)
            confirmation += 1
        if(confirmation>=closeBars)
            trend := -1
 
    if(trend[1]!=trend)
        lastU := up1
        lastD := dn1
        confirmation := 0
        unconfirmed := 0
    else
        lastU := trend==+1 ? max(lastU, up1) : up1
        lastD := trend==-1 ? min(lastD, dn1) : dn1
        // Trend is clearly continuing?  Reset confirmation.
        if(trend==+1 and dn1>dn1[1] or trend==-1 and up1<up1[1])
            confirmation := 0
            unconfirmed := 0
 
    [trend, lastU, lastD, unconfirmed]
///
 
 
superTrend(m, p, mode) =>
    superTrendCore(m, high, low, getMA(tr, mode, p))
///
 
[trend, up, dn, unconfirmed] = superTrend(atrM, atrPeriod, mode)
 
upPlot = plot(trend==1 ? up : na, title="Up Trend", style=plot.style_linebr, linewidth=2, color=unconfirmed==0?color.green:color.yellow)
buySignal = trend == 1 and trend[1] == -1
plotshape(buySignal ? up : na, title="UpTrend Begins", location=location.absolute, style=shape.circle, size=size.tiny, color=color.green)
plotshape(buySignal and showsignals ? up : na, title="Buy", text="Buy", location=location.absolute, style=shape.labelup, size=size.tiny, color=color.green, textcolor=color.white)
dnPlot = plot(trend==1 ? na : dn, title="Down Trend", style=plot.style_linebr, linewidth=2, color=unconfirmed==0?color.red:color.yellow)
sellSignal = trend == -1 and trend[1] == 1
plotshape(sellSignal ? dn : na, title="DownTrend Begins", location=location.absolute, style=shape.circle, size=size.tiny, color=color.red)
plotshape(sellSignal and showsignals ? dn : na, title="Sell", text="Sell", location=location.absolute, style=shape.labeldown, size=size.tiny, color=color.red, textcolor=color.white)
mPlot = plot(ohlc4, title="", style=plot.style_circles, linewidth=0)
longFillColor = highlighting ? (trend == 1 ? color.green : color.white) : color.white
shortFillColor = highlighting ? (trend == -1 ? color.red : color.white) : color.white
fill(mPlot, upPlot, title="UpTrend Highligter", color=longFillColor)
fill(mPlot, dnPlot, title="DownTrend Highligter", color=shortFillColor)
changeCond = trend != trend[1]
warnCond = unconfirmed[1]==0 and unconfirmed > 0
 
alertcondition(warnCond or changeCond, title="1) SuperTrend Warning", message="SuperTrend Warning ({{ticker}})")
alertcondition(changeCond, title="2) SuperTrend Reversal", message="SuperTrend Reversal ({{ticker}})")
alertcondition(buySignal, title="3) SuperTrend Up ▲ (+)", message="SuperTrend Up ▲ (+) ({{ticker}})")
alertcondition(sellSignal, title="4) SuperTrend Down ▼ (-)", message="SuperTrend Down ▼ (-) ({{ticker}})")

← 返回列表