Trendstream (open) by KingDonut

KingDonut · study · 178 行 · 点赞 7 · TradingView 原页

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

Pine Script

// This Pine Script™ is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © KingDonut
 
//@version=6
indicator("Trendstream (open) by KingDonut", shorttitle="Trendstream (open) by KD", overlay=true)
 
// ══════════════════════════════════════════════════════════════════════════════
// SHOW / HIDE
// ══════════════════════════════════════════════════════════════════════════════
gShow = "Show / Hide"
showTS  = input.bool(true, "Trendstream", group=gShow, tooltip="Turns the Trendstream (the two coloured lines and the shaded gap between them) on or off.")
showDMI = input.bool(true, "DMI Candles", group=gShow, tooltip="Turns the DMI candle colouring on or off.")
 
// ══════════════════════════════════════════════════════════════════════════════
// TRENDSTREAM SETTINGS
// ══════════════════════════════════════════════════════════════════════════════
gTS = "Trendstream"
atr1Len  = input.int(5,     "ATR 1 Length",     minval=1, group=gTS, tooltip="How many bars the first trend line looks back to measure price movement (volatility). Smaller = reacts faster. Larger = calmer.")
atr1Mult = input.float(2.62, "ATR 1 Multiplier", minval=0.1, step=0.01, group=gTS, tooltip="How far the first trend line sits from price. Bigger = wider line that gets hit less often. Smaller = tighter line that flips more often.")
atr2Len  = input.int(21,    "ATR 2 Length",     minval=1, group=gTS, tooltip="How many bars the second trend line looks back to measure price movement (volatility). Smaller = reacts faster. Larger = calmer.")
atr2Mult = input.float(1.62, "ATR 2 Multiplier", minval=0.1, step=0.01, group=gTS, tooltip="How far the second trend line sits from price. Bigger = wider line that gets hit less often. Smaller = tighter line that flips more often.")
 
gSm = "Trendstream Smoothing"
smType = input.string("ALMA", "Smoothing Type", options=["SMA", "EMA", "HMA", "VWMA", "ALMA", "SMMA", "WMA", "DEMA", "TEMA", "ZLEMA", "T3", "LSMA", "McGinley"], group=gSm, tooltip="The type of moving average used to smooth the two lines. Fast types (HMA, ZLEMA, TEMA) follow price closely. Slow types (SMMA, SMA, T3) are calmer. VWMA needs volume data. If there is none it falls back to SMA.")
smLen    = input.int(21, "Smoothing Length", minval=2, group=gSm, tooltip="How many bars are used to smooth the lines. Smaller = faster and twitchier. Larger = smoother but later.")
smOffset = input.float(0.76, "ALMA Offset", minval=0.0, maxval=1.0, step=0.01, group=gSm, tooltip="Only used when Smoothing Type is ALMA. Moves the smoothing towards the newest bars (closer to 1) or the older bars (closer to 0). Higher = faster, lower = smoother.")
smSigma  = input.float(5.0, "ALMA Sigma", minval=0.1, step=0.1, group=gSm, tooltip="Only used when Smoothing Type is ALMA. Controls how sharply the smoothing is focused. Higher = a narrower focus and a more reactive line. Lower = a wider, smoother line.")
smT3Vf   = input.float(0.7, "T3 Volume Factor", minval=0.0, maxval=1.0, step=0.05, group=gSm, tooltip="Only used when Smoothing Type is T3. Higher = faster but with a little overshoot. Lower = smoother and slower.")
 
gLook = "Trendstream Look"
lineWidth    = input.int(2,  "Line Width",           minval=1, maxval=5,   group=gLook, tooltip="How thick the two Trendstream lines are.")
fillStrength = input.int(55, "Fill Strength",        minval=0, maxval=100, group=gLook, tooltip="How solid the shading between the two lines is. 0 = invisible, 100 = fully solid.")
blendBars    = input.int(5,  "Colour Blend (bars)",  minval=1, group=gLook, tooltip="How many bars the colour takes to change between bull, muddy and bear. Smaller = colour changes quickly. Larger = colour fades slowly.")
 
gTSC = "Trendstream Colours"
tsMode = input.string("Standard", "Colour Mode", options=["Standard", "Colour Blind", "Custom"], group=gTSC, tooltip="Standard = the default blues. Colour Blind = blue, grey and orange, which are easy to tell apart for most types of colour blindness. Custom = use the three colours below.")
cBull  = input.color(color.rgb(110, 203, 245), "Custom Bull (both trends up)",    group=gTSC, tooltip="Only used when Colour Mode is Custom. The colour when both trend lines point up.")
cMud   = input.color(color.rgb(105, 140, 150), "Custom Muddy (trends split)",     group=gTSC, tooltip="Only used when Colour Mode is Custom. The colour when the two trend lines disagree.")
cBear  = input.color(color.rgb(30, 50, 200),   "Custom Bear (both trends down)", group=gTSC, tooltip="Only used when Colour Mode is Custom. The colour when both trend lines point down.")
 
// ══════════════════════════════════════════════════════════════════════════════
// DMI CANDLES SETTINGS
// ══════════════════════════════════════════════════════════════════════════════
gDMI = "DMI Candles"
dmiLength = input.int(21, "DMI 21 Setting", minval=1, group=gDMI, tooltip="How many bars the DMI looks back to compare buying pressure (DI+) with selling pressure (DI-). Smaller = faster and noisier. Larger = slower and smoother.")
dmiLevel  = input.int(21, "21 Trigger Level", minval=1, group=gDMI, tooltip="How strong the leading side (DI+ or DI-) must be before the candle turns up or down colour. Higher = stricter, so more candles stay neutral. Lower = more candles get coloured.")
 
gDMIC = "DMI Candles Colours"
dmiMode  = input.string("Standard", "Colour Mode", options=["Standard", "Colour Blind", "Custom"], group=gDMIC, tooltip="Standard = green, red and yellow. Colour Blind = blue, orange and grey. Custom = use the three colours below.")
dUpCust   = input.color(color.green,  "Custom Up (DI+ leads)",   group=gDMIC, tooltip="Only used when Colour Mode is Custom. Candle colour when buyers lead and the trigger level is met.")
dDownCust = input.color(color.red,    "Custom Down (DI- leads)", group=gDMIC, tooltip="Only used when Colour Mode is Custom. Candle colour when sellers lead and the trigger level is met.")
dNeutCust = input.color(color.yellow, "Custom Neutral (other)",  group=gDMIC, tooltip="Only used when Colour Mode is Custom. Candle colour when neither side meets the trigger level.")
 
// ══════════════════════════════════════════════════════════════════════════════
// FUNCTIONS
// ══════════════════════════════════════════════════════════════════════════════
// Trend line engine (Wilder ATR, HL2 source)
f_engine(src, atrLen, mult) =>
    tr_ = ta.tr(true)
    atr = ta.rma(tr_, atrLen)
 
    float upperBand = src + mult * atr
    float lowerBand = src - mult * atr
    float prevLowerBand = nz(lowerBand[1])
    float prevUpperBand = nz(upperBand[1])
 
    lowerBand := lowerBand > prevLowerBand or close[1] < prevLowerBand ? lowerBand : prevLowerBand
    upperBand := upperBand < prevUpperBand or close[1] > prevUpperBand ? upperBand : prevUpperBand
 
    int direction = na
    float line = na
    float prevLine = line[1]
    if na(atr[1])
        direction := 1
    else if prevLine == prevUpperBand
        direction := close > upperBand ? -1 : 1
    else
        direction := close < lowerBand ? 1 : -1
    line := direction == -1 ? lowerBand : upperBand
    [line, direction]
 
// Smoothing: every type is calculated each bar, then one is picked
f_smooth(float src, int len, string kind, float aOff, float aSig, float t3v) =>
    float sma_ = ta.sma(src, len)
    float ema1 = ta.ema(src, len)
    float ema2 = ta.ema(ema1, len)
    float ema3 = ta.ema(ema2, len)
    float ema4 = ta.ema(ema3, len)
    float ema5 = ta.ema(ema4, len)
    float ema6 = ta.ema(ema5, len)
    float hma_ = ta.hma(src, len)
    float vw_  = ta.vwma(src, len)
    float vwma_ = na(vw_) ? sma_ : vw_
    float alma_ = ta.alma(src, len, aOff, aSig)
    float smma_ = ta.rma(src, len)
    float wma_  = ta.wma(src, len)
    float dema_ = 2 * ema1 - ema2
    float tema_ = 3 * (ema1 - ema2) + ema3
    int   lag   = math.floor((len - 1) / 2)
    float zlema_ = ta.ema(src + (src - nz(src[lag], src)), len)
    float c1 = -t3v * t3v * t3v
    float c2 = 3 * t3v * t3v + 3 * t3v * t3v * t3v
    float c3 = -6 * t3v * t3v - 3 * t3v - 3 * t3v * t3v * t3v
    float c4 = 1 + 3 * t3v + t3v * t3v * t3v + 3 * t3v * t3v
    float t3_ = c1 * ema6 + c2 * ema5 + c3 * ema4 + c4 * ema3
    float lsma_ = ta.linreg(src, len, 0)
    var float mg = na
    float prevMg = nz(mg[1], src)
    float ratio = math.max(src / prevMg, 0.0001)
    mg := na(mg[1]) ? src : prevMg + (src - prevMg) / (0.6 * len * math.pow(ratio, 4))
 
    float result = switch kind
        "SMA"      => sma_
        "EMA"      => ema1
        "HMA"      => hma_
        "VWMA"     => vwma_
        "ALMA"     => alma_
        "SMMA"     => smma_
        "WMA"      => wma_
        "DEMA"     => dema_
        "TEMA"     => tema_
        "ZLEMA"    => zlema_
        "T3"       => t3_
        "LSMA"     => lsma_
        "McGinley" => mg
        => alma_
    result
 
// ══════════════════════════════════════════════════════════════════════════════
// TRENDSTREAM CALCULATIONS
// ══════════════════════════════════════════════════════════════════════════════
[raw1, dir1] = f_engine(hl2, atr1Len, atr1Mult)
[raw2, dir2] = f_engine(hl2, atr2Len, atr2Mult)
 
lineA = f_smooth(math.max(raw1, raw2), smLen, smType, smOffset, smSigma, smT3Vf)
lineB = f_smooth(math.min(raw1, raw2), smLen, smType, smOffset, smSigma, smT3Vf)
 
// Trend score: +2 both up, 0 split, -2 both down
rawScore = (dir1 < 0 ? 1 : -1) + (dir2 < 0 ? 1 : -1)
score    = ta.ema(rawScore, blendBars)
 
// Colour presets
bullCol = tsMode == "Standard" ? color.rgb(110, 203, 245) : tsMode == "Colour Blind" ? color.rgb(86, 180, 233)  : cBull
mudCol  = tsMode == "Standard" ? color.rgb(105, 140, 150) : tsMode == "Colour Blind" ? color.rgb(150, 150, 150) : cMud
bearCol = tsMode == "Standard" ? color.rgb(30, 50, 200)   : tsMode == "Colour Blind" ? color.rgb(230, 159, 0)   : cBear
 
streamCol = score >= 0
     ? color.from_gradient(score, 0, 2, mudCol, bullCol)
     : color.from_gradient(score, -2, 0, bearCol, mudCol)
 
// Stream depth
fillTransp = 100 - fillStrength
topTransp  = math.min(100, fillTransp + 30)
topColor   = color.new(streamCol, topTransp)
botColor   = color.new(streamCol, fillTransp)
 
streamTop = math.max(lineA, lineB)
streamBot = math.min(lineA, lineB)
 
// ══════════════════════════════════════════════════════════════════════════════
// TRENDSTREAM PLOTS
// ══════════════════════════════════════════════════════════════════════════════
p1 = plot(lineA, "Trendstream Upper Line", color=showTS ? streamCol : na, linewidth=lineWidth)
p2 = plot(lineB, "Trendstream Lower Line", color=showTS ? streamCol : na, linewidth=lineWidth)
fill(p1, p2, streamTop, streamBot, showTS ? topColor : na, showTS ? botColor : na, title="Trendstream Gap")
 
// ══════════════════════════════════════════════════════════════════════════════
// DMI CANDLES
// ══════════════════════════════════════════════════════════════════════════════
[diPlus, diMinus, _] = ta.dmi(dmiLength, dmiLength)
 
upCol   = dmiMode == "Standard" ? color.green  : dmiMode == "Colour Blind" ? color.rgb(0, 114, 178)   : dUpCust
downCol = dmiMode == "Standard" ? color.red    : dmiMode == "Colour Blind" ? color.rgb(230, 159, 0)   : dDownCust
neutCol = dmiMode == "Standard" ? color.yellow : dmiMode == "Colour Blind" ? color.rgb(170, 170, 170) : dNeutCust
 
candleColor = diPlus > diMinus and diPlus > dmiLevel ? upCol : diMinus > diPlus and diMinus > dmiLevel ? downCol : neutCol
 
barcolor(showDMI ? candleColor : na, title="DMI Candles")

← 返回列表