ATS Ribbon

onderarslantas107 · study · 602 行 · 点赞 39 · TradingView 原页

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

Pine Script

//@version=6
indicator("ATS Ribbon Lab | 1m - 1D", shorttitle="ATS RIBBON", overlay=true, dynamic_requests=true)
 
// Independent ATS core per timeframe.
// No LAB bonus, nested MTF blend, or virtual trades.
 
//━━━━━━━━━━━━━━━━ ATS CORE
string g1 = "1 | ATS Core"
 
lang = input.string(
     "TR", "Dil / Language",
     options=["TR", "EN"], group=g1)
 
trendLen = input.int(55, "Trend EMA", minval=2, group=g1)
momLen = input.int(10, "Momentum", minval=1, group=g1)
zLen = input.int(34, "Z Lookback", minval=2, group=g1)
smoothLen = input.int(8, "Organ Smooth", minval=1, group=g1)
 
matcPct = input.float(
     2.0, "MATC Band %",
     minval=0.1, step=0.1, group=g1)
 
matcSmooth = input.int(
     5, "MATC Smooth", minval=1, group=g1)
 
matcMirror = input.float(
     1.3, "MATC Mirror",
     minval=0.1, step=0.1, group=g1)
 
atrLen = input.int(
     14, "ATR Length", minval=1, group=g1)
 
atrMult = input.float(
     2.4, "ATS Line ATR",
     minval=0.1, step=0.1, group=g1)
 
bullTh = input.float(
     60, "Yukari guc / Bull threshold",
     minval=50.1, maxval=99.9, group=g1)
 
bearTh = input.float(
     40, "Asagi guc / Bear threshold",
     minval=0.1, maxval=49.9, group=g1)
 
usePvmBoost = input.bool(true, "PVM", group=g1)
 
pvmWeight = input.float(
     0.25, "PVM Weight",
     minval=0, maxval=0.6, step=0.05, group=g1)
 
//━━━━━━━━━━━━━━━━ RIBBON
string g2 = "2 | Ribbon"
 
showFast = input.bool(
     true, "Hizli / Fast: 1, 2, 3, 5m", group=g2)
 
showShort = input.bool(
     true, "Kisa / Short: 10, 15, 20, 30m", group=g2)
 
showMid = input.bool(
     true, "Orta / Mid: 45, 60, 90, 120m", group=g2)
 
showMacro = input.bool(
     true, "Ana / Macro: 180, 240, 360, 720m, 1D", group=g2)
 
consensus = input.float(
     75, "Kume yon uyumu % / Group consensus %",
     minval=60, maxval=100, step=5, group=g2)
 
showCenters = input.bool(
     true, "Kume merkezleri / Group centers", group=g2)
 
fillAlpha = input.int(
     90, "Dolgu seffafligi / Fill transparency",
     minval=75, maxval=98, group=g2)
 
showPanel = input.bool(true, "Mini panel", group=g2)
 
paintBars = input.bool(
     false, "Mumlari genel yone boya / Color candles", group=g2)
 
color bullC = #00E676
color bearC = #FF1744
color neutralC = #8F9AAA
 
//━━━━━━━━━━━━━━━━ HELPERS
t(string en, string tr) =>
    lang == "TR" ? tr : en
 
if barstate.isfirst and (not chart.is_standard or timeframe.isticks)
    runtime.error(t(
         "Use a standard time-based chart.",
         "Standart zaman tabanli grafik kullanin."))
 
f_clamp(float value, float lo, float hi) =>
    math.max(lo, math.min(hi, value))
 
f_z(float src, int length) =>
    float mean = ta.sma(src, length)
    float dev = ta.stdev(src, length)
    not na(src) and not na(mean) and not na(dev) ? (dev > 0 ? (src - mean) / dev : 0.0) : na
 
f_pvm_value(float trend, float momentum, float flow, bool volumeOK, float intensity) =>
    float denominator = volumeOK ? 1.0 : 0.65
    float direction = (0.40 * trend + 0.25 * momentum + (volumeOK ? 0.35 * flow : 0.0)) / denominator
    50.0 + 50.0 * f_clamp(direction * (1.0 + 0.5 * intensity), -1.0, 1.0)
 
f_mix(float score, float pvm) =>
    usePvmBoost and pvmWeight > 0 ? score * (1.0 - pvmWeight) + pvm * pvmWeight : score
 
//━━━━━━━━━━━━━━━━ ORIGINAL ATS ENGINE
f_engine() =>
    float emaTrend = ta.ema(close, trendLen)
    float trendRaw = ta.change(emaTrend)
    float momRaw = ta.roc(close, momLen)
 
    float atrLocal = ta.atr(atrLen)
    float ema20 = ta.ema(close, 20)
 
    float priceDirection = close > ema20 ? 1.0 : close < ema20 ? -1.0 : 0.0
    float atrRaw = ta.change(atrLocal) * priceDirection
 
    float change = ta.change(close)
 
    float signedVolume = not na(volume) and not na(change) ? math.sign(change) * volume : na
 
    float volumeBase = ta.ema(volume, 34)
    float volumeSeen = ta.lowest(na(volume) or volume < 0 ? 0.0 : 1.0, zLen)
 
    bool volumeOK = bar_index >= math.max(zLen, 34) and volumeSeen == 1 and not na(volumeBase) and volumeBase > 0
 
    float trendZ = f_z(trendRaw, zLen)
    float momZ = f_z(momRaw, zLen)
    float obvZ = f_z(signedVolume, zLen)
    float atrZ = f_z(atrRaw, zLen)
 
    float organRaw = (trendZ * 2.0 + momZ * 1.5 + atrZ * 0.5 + (volumeOK ? obvZ : 0.0)) * (volumeOK ? 1.0 : 1.25)
 
    float organ = ta.ema(organRaw, smoothLen)
    float organPct = f_clamp(organ / 6.0 * 50.0 + 50.0, 0.0, 100.0)
 
    float src = hl2
    float dist = src * matcPct / 100.0
 
    float resist = ta.ema(src + dist * matcMirror, matcSmooth)
    float support = ta.ema(src - dist * matcMirror, matcSmooth)
 
    float matcMid = (resist + support) / 2.0
    float matcRange = math.max(resist - support, syminfo.mintick)
 
    float matcScore = f_clamp((close - matcMid) / matcRange * 50.0 + 50.0, 0.0, 100.0)
 
    float emaFast = ta.ema(close, 21)
    float emaSlow = ta.ema(close, 55)
 
    float ribbon = emaFast > emaSlow ? 65.0 : emaFast < emaSlow ? 35.0 : 50.0
 
    float rsi = ta.rsi(close, 14)
    float rsiScore = bar_index >= 14 ? nz(rsi, 50.0) : na
 
    int warmup = math.max(
         55,
         math.max(
             trendLen,
             math.max(zLen + momLen, atrLen + zLen))) + smoothLen
 
    bool coreReady = bar_index >= warmup and not na(organRaw) and not na(organ) and not na(matcScore) and not na(rsiScore)
 
    float score = coreReady ? organPct * 0.40 + matcScore * 0.25 + ribbon * 0.20 + rsiScore * 0.15 : na
 
    float atr14 = ta.atr(14)
    float atrBase = ta.ema(atr14, 34)
    float safeAtr = math.max(atr14, syminfo.mintick)
 
    float trendDirection = f_clamp((emaFast - emaSlow) / safeAtr, -1.0, 1.0)
 
    float momentumDirection = f_clamp((close - close[14]) / (safeAtr * math.sqrt(14.0)), -1.0, 1.0)
 
    float flowMean = ta.ema(signedVolume, 34)
 
    float flow = volumeOK ? f_clamp(flowMean / volumeBase, -1.0, 1.0) : 0.0
 
    float expansion = atr14 / math.max(atrBase, syminfo.mintick)
    float relVolume = volumeOK ? volume / volumeBase : 1.0
 
    float atrIntensity = f_clamp(expansion - 1.0, 0.0, 1.0)
    float volIntensity = f_clamp(relVolume - 1.0, 0.0, 1.0)
 
    float intensity = volumeOK ? (atrIntensity + volIntensity) / 2.0 : atrIntensity
 
    float pvmRaw = f_pvm_value(
         trendDirection,
         momentumDirection,
         flow,
         volumeOK,
         intensity)
 
    float pvmSmooth = ta.ema(pvmRaw, 9)
    float pvm = coreReady and not na(pvmRaw) ? pvmSmooth : na
 
    [score, pvm, volumeOK]
 
//━━━━━━━━━━━━━━━━ INDEPENDENT ATS LINE
f_track() =>
    [score, pvm, volumeOK] = f_engine()
 
    float power = f_clamp(f_mix(score, pvm), 0, 100)
    float a = ta.atr(atrLen)
    float basis = ta.ema(close, 34)
 
    float lower = math.min(
         basis-a*atrMult,
         close-syminfo.mintick)
 
    float upper = math.max(
         basis+a*atrMult,
         close+syminfo.mintick)
 
    var int direction = 0
    var float level = na
 
    if not na(power) and not na(a) and not na(basis)
        int next = power >= bullTh ? 1 : power <= bearTh ? -1 : direction
 
        if next != direction
            level := next == 1 ? lower : upper
 
        else if next == 1
            level := math.max(nz(level, lower), lower)
 
        else if next == -1
            level := math.min(nz(level, upper), upper)
 
        direction := next
 
    [level, direction, power]
 
//━━━━━━━━━━━━━━━━ CONFIRMED MTF
f_previous() =>
    [level, direction, power] = f_track()
    [level[1], direction[1], power[1]]
 
f_timed() =>
    [level, direction, power] = f_track()
    [level, direction, power, time_close]
 
f_tf(simple string tf, float localLine, int localDir, float localPower) =>
    if timeframe.in_seconds(tf) == timeframe.in_seconds()
        [localLine, localDir, localPower]
 
    else if timeframe.in_seconds(tf) > timeframe.in_seconds()
        request.security(
             syminfo.tickerid,
             tf,
             f_previous(),
             gaps=barmerge.gaps_off,
             lookahead=barmerge.lookahead_on)
 
    else
        [levels, dirs, powers, ends] = request.security_lower_tf(
             syminfo.tickerid,
             tf,
             f_timed())
 
        int k = array.size(levels)-1
 
        while k >= 0
            if not na(array.get(ends, k)) and array.get(ends, k) <= time_close
                break
            k -= 1
 
        [k >= 0 ? array.get(levels, k) : na, k >= 0 ? array.get(dirs, k) : 0, k >= 0 ? array.get(powers, k) : na]
 
[localLine, localDir, localPower] = f_track()
 
// FAST
[a1, d1, p1] = f_tf("1", localLine, localDir, localPower)
[a2, d2, p2] = f_tf("2", localLine, localDir, localPower)
[a3, d3, p3] = f_tf("3", localLine, localDir, localPower)
[a5, d5, p5] = f_tf("5", localLine, localDir, localPower)
 
// SHORT
[a10, d10, p10] = f_tf("10", localLine, localDir, localPower)
[a15, d15, p15] = f_tf("15", localLine, localDir, localPower)
[a20, d20, p20] = f_tf("20", localLine, localDir, localPower)
[a30, d30, p30] = f_tf("30", localLine, localDir, localPower)
 
// MID
[a45, d45, p45] = f_tf("45", localLine, localDir, localPower)
[a60, d60, p60] = f_tf("60", localLine, localDir, localPower)
[a90, d90, p90] = f_tf("90", localLine, localDir, localPower)
[a120, d120, p120] = f_tf("120", localLine, localDir, localPower)
 
// MACRO
[a180, d180, p180] = f_tf("180", localLine, localDir, localPower)
[a240, d240, p240] = f_tf("240", localLine, localDir, localPower)
[a360, d360, p360] = f_tf("360", localLine, localDir, localPower)
[a720, d720, p720] = f_tf("720", localLine, localDir, localPower)
[aD, dD, pD] = f_tf("1D", localLine, localDir, localPower)
 
//━━━━━━━━━━━━━━━━ GROUP ENGINE
// Full membership is required before a group ribbon is displayed.
f_group(array<float> levels, array<int> directions, array<float> powers) =>
    float lo = na
    float hi = na
    float total = 0.0
    float totalPower = 0.0
 
    int up = 0
    int down = 0
    int n = 0
    int expected = array.size(levels)
 
    for i = 0 to expected-1
        float level = array.get(levels, i)
        int dir = array.get(directions, i)
        float power = array.get(powers, i)
 
        if not na(level) and not na(power) and dir != 0
            lo := na(lo) ? level : math.min(lo, level)
            hi := na(hi) ? level : math.max(hi, level)
 
            total += level
            totalPower += power
 
            up += dir == 1 ? 1 : 0
            down += dir == -1 ? 1 : 0
            n += 1
 
    int required = int(math.ceil(expected*consensus/100))
 
    int direction = n == expected ? (up >= required ? 1 : down >= required ? -1 : 0) : 0
 
    [n == expected ? lo : na, n == expected ? hi : na, n == expected ? total/n : na, direction, up, down, n, n > 0 ? totalPower/n : na]
 
[lo1, hi1, mid1, dir1, up1, dn1, n1, pow1] = f_group(
     array.from(a1,a2,a3,a5),
     array.from(d1,d2,d3,d5),
     array.from(p1,p2,p3,p5))
 
[lo2, hi2, mid2, dir2, up2, dn2, n2, pow2] = f_group(
     array.from(a10,a15,a20,a30),
     array.from(d10,d15,d20,d30),
     array.from(p10,p15,p20,p30))
 
[lo3, hi3, mid3, dir3, up3, dn3, n3, pow3] = f_group(
     array.from(a45,a60,a90,a120),
     array.from(d45,d60,d90,d120),
     array.from(p45,p60,p90,p120))
 
[lo4, hi4, mid4, dir4, up4, dn4, n4, pow4] = f_group(
     array.from(a180,a240,a360,a720,aD),
     array.from(d180,d240,d360,d720,dD),
     array.from(p180,p240,p360,p720,pD))
 
//━━━━━━━━━━━━━━━━ CONFIRMED DISPLAY STATE
var array<float> lows = array.new<float>(4, na)
var array<float> highs = array.new<float>(4, na)
var array<float> mids = array.new<float>(4, na)
 
var array<int> dirs = array.new<int>(4, 0)
var array<int> ups = array.new<int>(4, 0)
var array<int> downs = array.new<int>(4, 0)
var array<int> counts = array.new<int>(4, 0)
 
var array<float> strengths = array.new<float>(4, na)
 
var int overall = 0
bool bullEvent = false
bool bearEvent = false
 
if barstate.isconfirmed
    lows := array.from(lo1,lo2,lo3,lo4)
    highs := array.from(hi1,hi2,hi3,hi4)
    mids := array.from(mid1,mid2,mid3,mid4)
 
    dirs := array.from(dir1,dir2,dir3,dir4)
    ups := array.from(up1,up2,up3,up4)
    downs := array.from(dn1,dn2,dn3,dn4)
    counts := array.from(n1,n2,n3,n4)
 
    strengths := array.from(pow1,pow2,pow3,pow4)
 
    int old = overall
    bool complete = n1+n2+n3+n4 == 17
 
    overall := complete and dir1 == 1 and dir2 == 1 and dir3 == 1 and dir4 == 1 ? 1 : complete and dir1 == -1 and dir2 == -1 and dir3 == -1 and dir4 == -1 ? -1 : 0
 
    bullEvent := overall == 1 and old != 1
    bearEvent := overall == -1 and old != -1
 
//━━━━━━━━━━━━━━━━ COLORS
f_color(int direction) =>
    direction == 1 ? bullC : direction == -1 ? bearC : neutralC
 
color c1 = f_color(array.get(dirs,0))
color c2 = f_color(array.get(dirs,1))
color c3 = f_color(array.get(dirs,2))
color c4 = f_color(array.get(dirs,3))
 
//━━━━━━━━━━━━━━━━ FAST RIBBON
p1L = plot(
     showFast ? array.get(lows,0) : na,
     "Fast Lower",
     color=color.new(c1,60),
     linewidth=1,
     style=plot.style_linebr)
 
p1H = plot(
     showFast ? array.get(highs,0) : na,
     "Fast Upper",
     color=color.new(c1,60),
     linewidth=1,
     style=plot.style_linebr)
 
fill(
     p1L,p1H,
     color=showFast ? color.new(c1,fillAlpha) : na,
     fillgaps=false)
 
plot(
     showFast and showCenters ? array.get(mids,0) : na,
     "Fast Center",
     color=c1,
     linewidth=2,
     style=plot.style_linebr)
 
//━━━━━━━━━━━━━━━━ SHORT RIBBON
p2L = plot(
     showShort ? array.get(lows,1) : na,
     "Short Lower",
     color=color.new(c2,60),
     linewidth=1,
     style=plot.style_linebr)
 
p2H = plot(
     showShort ? array.get(highs,1) : na,
     "Short Upper",
     color=color.new(c2,60),
     linewidth=1,
     style=plot.style_linebr)
 
fill(
     p2L,p2H,
     color=showShort ? color.new(c2,fillAlpha) : na,
     fillgaps=false)
 
plot(
     showShort and showCenters ? array.get(mids,1) : na,
     "Short Center",
     color=c2,
     linewidth=2,
     style=plot.style_linebr)
 
//━━━━━━━━━━━━━━━━ MID RIBBON
p3L = plot(
     showMid ? array.get(lows,2) : na,
     "Mid Lower",
     color=color.new(c3,60),
     linewidth=1,
     style=plot.style_linebr)
 
p3H = plot(
     showMid ? array.get(highs,2) : na,
     "Mid Upper",
     color=color.new(c3,60),
     linewidth=1,
     style=plot.style_linebr)
 
fill(
     p3L,p3H,
     color=showMid ? color.new(c3,fillAlpha) : na,
     fillgaps=false)
 
plot(
     showMid and showCenters ? array.get(mids,2) : na,
     "Mid Center",
     color=c3,
     linewidth=2,
     style=plot.style_linebr)
 
//━━━━━━━━━━━━━━━━ MACRO RIBBON
p4L = plot(
     showMacro ? array.get(lows,3) : na,
     "Macro Lower",
     color=color.new(c4,60),
     linewidth=1,
     style=plot.style_linebr)
 
p4H = plot(
     showMacro ? array.get(highs,3) : na,
     "Macro Upper",
     color=color.new(c4,60),
     linewidth=1,
     style=plot.style_linebr)
 
fill(
     p4L,p4H,
     color=showMacro ? color.new(c4,fillAlpha) : na,
     fillgaps=false)
 
plot(
     showMacro and showCenters ? array.get(mids,3) : na,
     "Macro Center",
     color=c4,
     linewidth=3,
     style=plot.style_linebr)
 
// Optional candle coloring.
barcolor(
     paintBars ?
     (overall == 0 ? na : f_color(overall)) :
     na)
 
//━━━━━━━━━━━━━━━━ MINI PANEL
var table panel = table.new(
     position.top_right,
     3,6,
     bgcolor=#141C29,
     frame_color=color.new(neutralC,70),
     frame_width=1)
 
f_row(int row, string title, int index, int expected) =>
    int n = array.get(counts,index)
    int direction = array.get(dirs,index)
    int up = array.get(ups,index)
    int down = array.get(downs,index)
 
    string state = n < expected ? t("WAIT", "BEKLE") : direction == 1 ? t("UP", "YUKARI") : direction == -1 ? t("DOWN", "ASAGI") : t("MIXED", "KARISIK")
 
    string votes = n < expected ? str.tostring(n)+"/"+str.tostring(expected) : "↑"+str.tostring(up)+" ↓"+str.tostring(down)
 
    table.cell(
         panel,0,row,title,
         text_color=neutralC,
         text_size=size.tiny,
         text_halign=text.align_left)
 
    table.cell(
         panel,1,row,state,
         text_color=f_color(direction),
         text_size=size.tiny)
 
    table.cell(
         panel,2,row,votes,
         text_color=neutralC,
         text_size=size.tiny)
 
if barstate.islast
    if showPanel
        table.cell(
             panel,0,0,"ATS RIBBON",
             text_color=color.white,
             text_size=size.tiny)
 
        table.cell(
             panel,1,0,t("DIRECTION","YON"),
             text_color=neutralC,
             text_size=size.tiny)
 
        table.cell(
             panel,2,0,t("VOTES","UYUM"),
             text_color=neutralC,
             text_size=size.tiny)
 
        f_row(1,t("Fast 1-5m","Hizli 1-5dk"),0,4)
        f_row(2,t("Short 10-30m","Kisa 10-30dk"),1,4)
        f_row(3,t("Mid 45-120m","Orta 45-120dk"),2,4)
        f_row(4,t("Macro 3h-1D","Ana 3sa-1G"),3,5)
 
        bool ready = array.sum(counts) == 17
 
        table.cell(
             panel,0,5,t("ALL GROUPS","TUM KUMELER"),
             text_color=color.white,
             text_size=size.tiny)
 
        table.cell(
             panel,1,5,
             not ready ? t("WAIT","BEKLE") : overall == 1 ? t("UP","YUKARI") : overall == -1 ? t("DOWN","ASAGI") : t("MIXED","KARISIK"),
             text_color=f_color(overall),
             text_size=size.tiny)
 
        table.cell(
             panel,2,5,
             str.tostring(array.sum(counts))+"/17",
             text_color=neutralC,
             text_size=size.tiny)
 
    else
        table.clear(panel,0,0,2,5)
 
//━━━━━━━━━━━━━━━━ ALERTS
alertcondition(
     bullEvent,
     "ATS 4 GROUP UP",
     "ATS Ribbon: all four groups UP | {{ticker}} | {{interval}}")
 
alertcondition(
     bearEvent,
     "ATS 4 GROUP DOWN",
     "ATS Ribbon: all four groups DOWN | {{ticker}} | {{interval}}")

← 返回列表