Arjoo Indicator

sumantrivedi0107 · study · 620 行 · 点赞 27 · TradingView 原页

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

Pine Script

//@version=6
// Multi-Timeframe FVG & POI Zones (Saurabh's rule)
// Checked on closed candles of each selected timeframe (4H, 1H, 30m, 15m, 5m):
//   Downtrend: candle 1 is a DOWN candle, skip candle 2, and candle 3's high wick stays below candle 1's low wick.
//   Uptrend:   candle 1 is an UP candle, skip candle 2, and candle 3's low wick stays above candle 1's high wick.
//   Wicks are used: if candle 1's and candle 3's wicks touch or overlap, it is not a valid FVG.
//   When valid: candle 1 = POI (point of interest), candle 2 = FVG (the gap between candle 1 and 3).
// SURE-SHOT POI: a 4H POI becomes "confirmed" when POIs of the same direction from the lower
//   timeframes (1H, 30m, 15m, 5m) form inside the same price range after it.
// Swing structure (on the chart timeframe):
//   STH = short-term high: a candle whose high is above the candle before and after it.
//   ITH = intermediate-term high: an STH that is higher than the STH before and after it.
//   STL / ITL are the same for lows.
// Zones are drawn on your chart and extend right until price mitigates them.
// A timeframe is shown only if it is equal to or higher than the chart timeframe.
// Non-repainting: a zone is drawn only after its candle has closed.
indicator("MTF FVG & POI Zones", overlay = true, max_boxes_count = 500, max_labels_count = 500, max_lines_count = 500)
 
// ---------- Timeframes ----------
use4h  = input.bool(true, "4H",  inline = "tf1", group = "Timeframes")
tf4h   = input.timeframe("240", "", inline = "tf1", group = "Timeframes")
use1h  = input.bool(true, "1H",  inline = "tf2", group = "Timeframes")
tf1h   = input.timeframe("60",  "", inline = "tf2", group = "Timeframes")
use30  = input.bool(true, "30m", inline = "tf3", group = "Timeframes")
tf30   = input.timeframe("30",  "", inline = "tf3", group = "Timeframes")
use15  = input.bool(true, "15m", inline = "tf4", group = "Timeframes")
tf15   = input.timeframe("15",  "", inline = "tf4", group = "Timeframes")
use5   = input.bool(true, "5m",  inline = "tf5", group = "Timeframes")
tf5    = input.timeframe("5",   "", inline = "tf5", group = "Timeframes")
 
// ---------- Settings ----------
showBull   = input.bool(true, "Show uptrend (Buy) zones", group = "Settings")
showBear   = input.bool(true, "Show downtrend (Sell) zones", group = "Settings")
showPoi    = input.bool(true, "Show POI (candle 1)", group = "Settings")
showFvg    = input.bool(false, "Show FVG (candle 2 gap)", group = "Settings")
needColor  = input.bool(true, "Candle 1 must be down (downtrend) / up (uptrend)", group = "Settings")
minGapAtr  = input.float(0.0, "Min FVG size (x ATR 14 of that timeframe), 0 = all", minval = 0, step = 0.05, group = "Settings")
mitMode    = input.string("Full fill", "Mitigated when price", options = ["Touch", "50%", "Full fill"], group = "Mitigation")
onMit      = input.string("Stop extending", "When mitigated", options = ["Stop extending", "Delete"], group = "Mitigation")
maxZones   = input.int(10, "Max zones kept per timeframe (each for Buy and Sell)", minval = 1, maxval = 48, group = "Mitigation")
 
sureNeed   = input.int(4, "Lower timeframes that must agree (1H, 30m, 15m, 5m)", minval = 1, maxval = 4, group = "Sure-shot POI")
sureMatch  = input.string("Inside", "Lower POI must be", options = ["Inside", "Overlapping"], group = "Sure-shot POI",
     tooltip = "Inside: the lower timeframe POI lies fully within the 4H POI. Overlapping: it only needs to touch the 4H POI range.")
overlapMode = input.string("Smallest timeframe only", "When POIs of several timeframes overlap",
     options = ["Smallest timeframe only", "Show all"], group = "Sure-shot POI")
showCount  = input.bool(true, "Show agreement count on 4H POI (e.g. 2/4)", group = "Sure-shot POI")
sureBuyCol  = input.color(color.rgb(189, 189, 189, 65), "Sure-shot Buy POI",  group = "Sure-shot POI")
sureSellCol = input.color(color.rgb(189, 189, 189, 65), "Sure-shot Sell POI", group = "Sure-shot POI")
 
bullFvgCol = input.color(color.rgb(158, 158, 158, 85), "Buy FVG (light grey)",  group = "Colors")
bearFvgCol = input.color(color.rgb(158, 158, 158, 85), "Sell FVG (light grey)", group = "Colors")
bullPoiCol = input.color(color.rgb(189, 189, 189, 75), "Buy POI (light grey)", group = "Colors")
bearPoiCol = input.color(color.rgb(189, 189, 189, 75), "Sell POI (light grey)", group = "Colors")
textCol    = input.color(color.white, "Label text", group = "Colors")
mitCol     = input.color(color.rgb(189, 189, 189, 93), "Mitigated zone", group = "Colors")
showText   = input.bool(true, "Show labels inside boxes", group = "Colors")
 
// ---------- Helpers ----------
type Zone
    box  bx
    bool bull
    bool active
    int  made
    bool isPoi
    bool sure
    string txt
    bool a1 = false
    bool a2 = false
    bool a3 = false
    bool a4 = false
    bool hidden = false
    string tags = ""
    bool sureP = false
    label lb = na
 
tfText(string t) =>
    s = timeframe.in_seconds(t)
    s >= 604800 ? str.tostring(math.round(s / 604800)) + "W" :
     s >= 86400 ? str.tostring(math.round(s / 86400)) + "D" :
     s >= 3600  ? str.tostring(math.round(s / 3600)) + "H" :
     str.tostring(math.round(s / 60)) + "m"
 
addZone(array<Zone> zones, int left, float top, float bottom, bool bull, color col, string txt, bool isPoi, int barMs) =>
    b = box.new(left, top, time, bottom, xloc = xloc.bar_time, border_color = color.new(col, 20),
         border_style = line.style_dotted, bgcolor = color.new(color.white, 100), text = "", text_size = size.small,
         text_color = textCol, text_halign = text.align_right)
    // Label sits just below the POI candle, so it is clear which candle and timeframe it belongs to
    lb = label.new(left + math.round(barMs / 2), bottom, showText ? txt : "", xloc = xloc.bar_time,
         style = label.style_label_up, color = color.new(color.white, 100), textcolor = textCol, size = size.small)
    zones.push(Zone.new(b, bull, true, bar_index, isPoi, false, txt, lb = lb))
    // Keep Buy and Sell zones separately, so one side never pushes the other off the chart
    int same = 0
    for z in zones
        if z.bull == bull
            same += 1
    if same > maxZones * 2
        for i = 0 to zones.size() - 1
            if zones.get(i).bull == bull
                old = zones.remove(i)
                old.bx.delete()
                old.lb.delete()
                break
 
// Detects POI/FVG on one timeframe and draws new zones. Returns [newBuy, newSell].
detect(string tf, bool use, array<Zone> zones) =>
    [h1, l1, o1, c1, t1, t2, h3, l3, atrT] = request.security(syminfo.tickerid, tf,
         [high[3], low[3], open[3], close[3], time[3], time[2], high[1], low[1], ta.atr(14)[1]],
         lookahead = barmerge.lookahead_on)
    allowed = use and timeframe.in_seconds(tf) >= timeframe.in_seconds()
    newBar  = timeframe.change(tf)
    lbl     = tfText(tf)
    barMs   = timeframe.in_seconds(tf) * 1000
    buy  = allowed and newBar and showBull and l3 > h1 and (l3 - h1) >= minGapAtr * atrT and (not needColor or c1 > o1)
    sell = allowed and newBar and showBear and h3 < l1 and (l1 - h3) >= minGapAtr * atrT and (not needColor or c1 < o1)
    if buy
        if showPoi
            addZone(zones, t1, h1, l1, true, bullPoiCol, lbl + " Buy POI", true, barMs)
        if showFvg
            addZone(zones, t2, l3, h1, true, bullFvgCol, lbl + " Buy FVG", false, barMs)
    if sell
        if showPoi
            addZone(zones, t1, h1, l1, false, bearPoiCol, lbl + " Sell POI", true, barMs)
        if showFvg
            addZone(zones, t2, l1, h3, false, bearFvgCol, lbl + " Sell FVG", false, barMs)
    [buy, sell]
 
// Extends active zones and checks mitigation. Returns [touchedBuy, touchedSell].
update(array<Zone> zones) =>
    bool tBuy  = false
    bool tSell = false
    if zones.size() > 0
        for i = zones.size() - 1 to 0
            z = zones.get(i)
            if z.active and z.made != bar_index
                top = z.bx.get_top()
                bot = z.bx.get_bottom()
                mid = (top + bot) / 2
                touched = z.bull ? low <= top : high >= bot
                hit = switch mitMode
                    "Touch" => touched
                    "50%"   => z.bull ? low <= mid : high >= mid
                    => z.bull ? low <= bot : high >= top
                if touched
                    if z.bull
                        tBuy := true
                    else
                        tSell := true
                if hit
                    z.active := false
                    if onMit == "Delete"
                        z.bx.delete()
                        z.lb.delete()
                        zones.remove(i)
                    else
                        z.bx.set_right(time)
                        if not z.hidden
                            z.bx.set_border_color(color.new(color.gray, 75))
                else
                    z.bx.set_right(time)
    [tBuy, tSell]
 
// ---------- Run on each timeframe ----------
var array<Zone> z4h = array.new<Zone>()
var array<Zone> z1h = array.new<Zone>()
var array<Zone> z30 = array.new<Zone>()
var array<Zone> z15 = array.new<Zone>()
var array<Zone> z5  = array.new<Zone>()
 
[nb1, ns1] = detect(tf4h, use4h, z4h)
[nb2, ns2] = detect(tf1h, use1h, z1h)
[nb3, ns3] = detect(tf30, use30, z30)
[nb4, ns4] = detect(tf15, use15, z15)
[nb5, ns5] = detect(tf5,  use5,  z5)
 
[tb1, ts1] = update(z4h)
[tb2, ts2] = update(z1h)
[tb3, ts3] = update(z30)
[tb4, ts4] = update(z15)
[tb5, ts5] = update(z5)
 
newBuy   = nb1 or nb2 or nb3 or nb4 or nb5
newSell  = ns1 or ns2 or ns3 or ns4 or ns5
touchBuy  = tb1 or tb2 or tb3 or tb4 or tb5
touchSell = ts1 or ts2 or ts3 or ts4 or ts5
 
// ---------- Sure-shot POI (4H POI confirmed by lower timeframes) ----------
// Returns 1 if the lower timeframe has a same-direction POI in the 4H POI range, formed after it.
agrees(array<Zone> lower, Zone base) =>
    int found = 0
    if lower.size() > 0
        bt = base.bx.get_top()
        bb = base.bx.get_bottom()
        bl = base.bx.get_left()
        for z in lower
            if z.isPoi and z.bull == base.bull and z.bx.get_left() >= bl
                zt = z.bx.get_top()
                zb = z.bx.get_bottom()
                ok = sureMatch == "Inside" ? (zt <= bt and zb >= bb) : (zt >= bb and zb <= bt)
                if ok
                    found := 1
                    break
    found
 
bool newSureBuy  = false
bool newSureSell = false
if z4h.size() > 0
    for b in z4h
        if b.isPoi and b.active and not b.sure
            // Once a lower timeframe agrees, it stays counted
            b.a1 := b.a1 or agrees(z1h, b) == 1
            b.a2 := b.a2 or agrees(z30, b) == 1
            b.a3 := b.a3 or agrees(z15, b) == 1
            b.a4 := b.a4 or agrees(z5,  b) == 1
            n = (b.a1 ? 1 : 0) + (b.a2 ? 1 : 0) + (b.a3 ? 1 : 0) + (b.a4 ? 1 : 0)
            if n >= sureNeed and b.hidden
                b.sure := true
                if b.bull
                    newSureBuy := true
                else
                    newSureSell := true
            else if n >= sureNeed
                b.sure := true
                b.bx.set_border_color(color.new(color.white, 20))
                b.bx.set_border_width(2)
                b.lb.set_text(showText ? "4H SURE-SHOT " + (b.bull ? "BUY" : "SELL") + " POI (" + str.tostring(n) + "/4)" : "")
                b.lb.set_textcolor(textCol)
                if b.bull
                    newSureBuy := true
                else
                    newSureSell := true
            else if showCount and showText and not b.hidden
                b.lb.set_text(b.txt + " " + str.tostring(n) + "/4")
 
// ---------- Overlapping POIs: keep only the smallest timeframe ----------
// A bigger-timeframe POI is hidden when a smaller-timeframe POI of the same direction forms
// inside it later. The smaller POI's label lists the bigger timeframes it lines up with.
link(array<Zone> lower, array<Zone> higher, string hl, bool is4h) =>
    if overlapMode == "Smallest timeframe only" and lower.size() > 0 and higher.size() > 0
        for z in lower
            if z.isPoi and z.active and not z.hidden
                zt = z.bx.get_top()
                zb = z.bx.get_bottom()
                zl = z.bx.get_left()
                for h in higher
                    if h.isPoi and h.bull == z.bull and (h.active or h.hidden) and h.bx.get_left() <= zl
                        ht = h.bx.get_top()
                        hb = h.bx.get_bottom()
                        ok = sureMatch == "Inside" ? (zt <= ht and zb >= hb) : (zt >= hb and zb <= ht)
                        if ok
                            if not str.contains(z.tags + " ", " " + hl + " ")
                                z.tags := z.tags + " " + hl
                            if is4h and h.sure
                                z.sureP := true
                            if not h.hidden
                                h.hidden := true
                                h.bx.set_bgcolor(color.new(color.white, 100))
                                h.bx.set_border_color(color.new(color.white, 100))
                                h.lb.set_text("")
    true
 
finish(array<Zone> lower) =>
    if lower.size() > 0
        for z in lower
            if z.isPoi and z.active and not z.hidden and z.tags != "" and showText
                z.lb.set_text((z.sureP ? "SURE-SHOT " : "") + z.txt + "  +" + z.tags)
                if z.sureP
                    z.bx.set_border_color(color.new(color.white, 20))
                    z.bx.set_border_width(2)
                    z.lb.set_textcolor(textCol)
    true
 
link(z5, z15, tfText(tf15), false)
link(z5, z30, tfText(tf30), false)
link(z5, z1h, tfText(tf1h), false)
link(z5, z4h, tfText(tf4h), true)
link(z15, z30, tfText(tf30), false)
link(z15, z1h, tfText(tf1h), false)
link(z15, z4h, tfText(tf4h), true)
link(z30, z1h, tfText(tf1h), false)
link(z30, z4h, tfText(tf4h), true)
link(z1h, z4h, tfText(tf4h), true)
finish(z5)
finish(z15)
finish(z30)
finish(z1h)
 
// ---------- ITH / ITL / STH / STL (chart timeframe) ----------
showSt  = input.bool(true, "Show STH / STL", group = "Swing structure")
showIt  = input.bool(true, "Show ITH / ITL", group = "Swing structure")
stCol   = input.color(color.gray, "STH / STL color", group = "Swing structure")
ithCol  = input.color(color.rgb(229, 115, 115), "ITH color", group = "Swing structure")
itlCol  = input.color(color.rgb(102, 187, 106), "ITL color", group = "Swing structure")
 
// ---------- Liquidity at ITH / ITL ----------
// BSL (buy-side liquidity) rests above each ITH, SSL (sell-side liquidity) below each ITL.
// The line extends until price sweeps it.
showLiq   = input.bool(true, "Show liquidity lines at ITH / ITL", group = "Liquidity")
maxLiq    = input.int(8, "Max liquidity lines kept (each side)", minval = 1, maxval = 50, group = "Liquidity")
liqCol    = input.color(color.rgb(189, 189, 189, 30), "Liquidity line", group = "Liquidity")
sweptCol  = input.color(color.rgb(189, 189, 189, 75), "Swept line", group = "Liquidity")
 
type Liq
    line  ln
    label lb
    bool  isHigh
    bool  active
 
var array<Liq> liqs = array.new<Liq>()
 
newLiq(int x, float price, bool isHigh) =>
    if showLiq
        ln = line.new(x, price, bar_index, price, color = liqCol, style = line.style_dotted, width = 1)
        lb = label.new(bar_index, price, isHigh ? "BSL" : "SSL", style = label.style_none,
             textcolor = textCol, size = size.tiny)
        liqs.push(Liq.new(ln, lb, isHigh, true))
        int same = 0
        for q in liqs
            if q.isHigh == isHigh
                same += 1
        if same > maxLiq
            for i = 0 to liqs.size() - 1
                if liqs.get(i).isHigh == isHigh
                    old = liqs.remove(i)
                    old.ln.delete()
                    old.lb.delete()
                    break
 
var float lastIth = na
var float lastItl = na
var int lastIthX = na
var int lastItlX = na
var float sh1 = na
var float sh2 = na
var label shL1 = na
var float sl1 = na
var float sl2 = na
var label slL1 = na
bool newIth = false
bool newItl = false
 
isSth = high[1] > high[2] and high[1] > high
isStl = low[1] < low[2] and low[1] < low
 
if isSth and (showSt or showIt)
    // The previous STH becomes an ITH if it is higher than the STH before it and this new one
    if showIt and not na(sh2) and sh1 > sh2 and sh1 > high[1]
        shL1.set_text("ITH")
        shL1.set_textcolor(ithCol)
        shL1.set_size(size.normal)
        newLiq(shL1.get_x(), sh1, true)
        lastIth := sh1
        lastIthX := shL1.get_x()
        newIth := true
    lbl = label.new(bar_index - 1, high[1], showSt ? "STH" : "", style = label.style_label_down,
         color = color.new(color.white, 100), textcolor = stCol, size = size.tiny)
    sh2 := sh1
    sh1 := high[1]
    shL1 := lbl
 
if isStl and (showSt or showIt)
    if showIt and not na(sl2) and sl1 < sl2 and sl1 < low[1]
        slL1.set_text("ITL")
        slL1.set_textcolor(itlCol)
        slL1.set_size(size.normal)
        newLiq(slL1.get_x(), sl1, false)
        lastItl := sl1
        lastItlX := slL1.get_x()
        newItl := true
    lbl = label.new(bar_index - 1, low[1], showSt ? "STL" : "", style = label.style_label_up,
         color = color.new(color.white, 100), textcolor = stCol, size = size.tiny)
    sl2 := sl1
    sl1 := low[1]
    slL1 := lbl
 
bool bslSwept = false
bool sslSwept = false
if liqs.size() > 0
    for q in liqs
        if q.active
            price = q.ln.get_y1()
            if q.ln.get_x1() < bar_index - 1 and (q.isHigh ? high > price : low < price)
                q.active := false
                q.ln.set_x2(bar_index)
                q.ln.set_color(sweptCol)
                q.lb.set_x(bar_index)
                q.lb.set_text(q.isHigh ? "BSL swept" : "SSL swept")
                if q.isHigh
                    bslSwept := true
                else
                    sslSwept := true
            else
                q.ln.set_x2(bar_index)
                q.lb.set_x(bar_index)
 
// ---------- Volume imbalance (chart timeframe) ----------
// Two candles in the same direction whose BODIES do not overlap but whose WICKS do.
// Bullish VI: gap between previous close and current open. Bearish is the mirror.
showVi   = input.bool(true, "Show volume imbalance (VI)", group = "Volume imbalance")
maxVi    = input.int(10, "Max VI zones kept", minval = 1, maxval = 100, group = "Volume imbalance")
viCol    = input.color(color.rgb(189, 189, 189, 40), "VI outline", group = "Volume imbalance")
 
type Vi
    box   bx
    label lb
    bool  bull
    bool  active
 
var array<Vi> vis = array.new<Vi>()
 
bullVi = close > open and close[1] > open[1] and open > close[1] and low <= high[1]
bearVi = close < open and close[1] < open[1] and open < close[1] and high >= low[1]
 
if showVi and (bullVi or bearVi)
    top = bullVi ? open : close[1]
    bot = bullVi ? close[1] : open
    vb = box.new(bar_index - 1, top, bar_index, bot, border_color = viCol, border_style = line.style_dashed,
         bgcolor = color.new(color.white, 100))
    vl = label.new(bar_index, bot, bullVi ? "Buy VI" : "Sell VI", style = label.style_label_up,
         color = color.new(color.white, 100), textcolor = textCol, size = size.tiny)
    vis.push(Vi.new(vb, vl, bullVi, true))
    if vis.size() > maxVi
        old = vis.shift()
        old.bx.delete()
        old.lb.delete()
 
bool viTouched = false
if vis.size() > 0
    for v in vis
        if v.active and v.bx.get_right() < bar_index
            t = v.bx.get_top()
            b0 = v.bx.get_bottom()
            filled = v.bull ? low <= b0 : high >= t
            if (v.bull ? low <= t : high >= b0)
                viTouched := true
            if filled
                v.active := false
                v.bx.set_right(bar_index)
                v.bx.set_border_color(color.new(color.gray, 80))
            else
                v.bx.set_right(bar_index)
        else if v.active
            v.bx.set_right(bar_index)
 
// ---------- Equilibrium (50% of the range between the last ITH and ITL) ----------
showEq   = input.bool(true, "Show equilibrium (EQ) line", group = "Equilibrium")
eqCol    = input.color(color.rgb(189, 189, 189, 20), "EQ line", group = "Equilibrium")
 
var line  eqLine = na
var label eqLbl  = na
if showEq and barstate.islast and not na(lastIth) and not na(lastItl)
    eq = (lastIth + lastItl) / 2
    zone = close > eq ? "Premium" : "Discount"
    eqLine.delete()
    eqLbl.delete()
    eqLine := line.new(bar_index - 50, eq, bar_index + 10, eq, color = eqCol, style = line.style_dashed, width = 1)
    eqLbl  := label.new(bar_index + 10, eq, "EQ 50%  (price in " + zone + ")", style = label.style_label_left,
         color = color.new(color.white, 100), textcolor = textCol, size = size.small)
 
// ---------- OTE (optimal trade entry: 62% to 79% retracement of the last ITH-ITL leg) ----------
// Up leg (ITL then ITH): Buy OTE is 62-79% down from the ITH. Down leg (ITH then ITL): Sell OTE is 62-79% up from the ITL.
showOte  = input.bool(true, "Show OTE zone", group = "OTE")
oteCol   = input.color(color.rgb(189, 189, 189, 20), "OTE outline", group = "OTE")
 
var box   oteBox = na
var line  oteMid = na
var label oteLbl = na
if showOte and barstate.islast and not na(lastIth) and not na(lastItl)
    rng  = lastIth - lastItl
    upLeg = lastIthX > lastItlX
    o62 = upLeg ? lastIth - 0.62 * rng : lastItl + 0.62 * rng
    o705 = upLeg ? lastIth - 0.705 * rng : lastItl + 0.705 * rng
    o79 = upLeg ? lastIth - 0.79 * rng : lastItl + 0.79 * rng
    x0 = math.max(lastIthX, lastItlX)
    oteBox.delete()
    oteMid.delete()
    oteLbl.delete()
    oteBox := box.new(x0, math.max(o62, o79), bar_index + 10, math.min(o62, o79), border_color = oteCol,
         border_style = line.style_dotted, bgcolor = color.new(color.white, 100))
    oteMid := line.new(x0, o705, bar_index + 10, o705, color = oteCol, style = line.style_dotted)
    oteLbl := label.new(bar_index + 10, o705, (upLeg ? "Buy" : "Sell") + " OTE 62-79% (70.5%)", style = label.style_label_left,
         color = color.new(color.white, 100), textcolor = textCol, size = size.small)
 
// ---------- ICT killzones (forex sessions, New York time) ----------
showKz   = input.bool(true, "Show killzones", group = "Killzones (New York time)")
kzAsia   = input.session("2000-2359", "Asia",         group = "Killzones (New York time)")
kzLon    = input.session("0200-0500", "London",       group = "Killzones (New York time)")
kzNy     = input.session("0700-1000", "New York",     group = "Killzones (New York time)")
kzLc     = input.session("1000-1200", "London close", group = "Killzones (New York time)")
kzShade  = input.color(color.rgb(189, 189, 189, 93), "Killzone shading", group = "Killzones (New York time)")
tzNy     = "America/New_York"
 
kzOk   = showKz and timeframe.isintraday and timeframe.in_seconds() <= 3600
inAsia = kzOk and not na(time(timeframe.period, kzAsia, tzNy))
inLon  = kzOk and not na(time(timeframe.period, kzLon, tzNy))
inNy   = kzOk and not na(time(timeframe.period, kzNy, tzNy))
inLc   = kzOk and not na(time(timeframe.period, kzLc, tzNy))
bgcolor(inAsia or inLon or inNy or inLc ? kzShade : na, title = "Killzones")
 
kzLabel(bool inside, string name) =>
    if inside and not inside[1]
        label.new(bar_index, high, name, yloc = yloc.abovebar, style = label.style_none, textcolor = textCol, size = size.tiny)
kzLabel(inAsia, "Asia KZ")
kzLabel(inLon,  "London KZ")
kzLabel(inNy,   "NY KZ")
kzLabel(inLc,   "London close KZ")
 
// ---------- CBDR (Central Bank Dealers Range, 14:00-20:00 New York) ----------
// After the CBDR closes, its high/low and 1x / 2x range projections are drawn for the next day.
showCbdr = input.bool(true, "Show CBDR and projections", group = "CBDR")
cbdrSess = input.session("1400-2000", "CBDR session", group = "CBDR")
cbdrCol  = input.color(color.rgb(189, 189, 189, 30), "CBDR lines", group = "CBDR")
 
inCbdr = showCbdr and timeframe.isintraday and timeframe.in_seconds() <= 3600 and not na(time(timeframe.period, cbdrSess, tzNy))
var float cH = na
var float cL = na
var int   cX = na
var array<line>  cLines = array.new<line>()
var array<label> cLbls  = array.new<label>()
 
if inCbdr and not inCbdr[1]
    cH := high
    cL := low
    cX := bar_index
else if inCbdr
    cH := math.max(cH, high)
    cL := math.min(cL, low)
 
if not inCbdr and inCbdr[1] and not na(cH)
    // Freeze the previous day's set, start a new one
    cLines.clear()
    cLbls.clear()
    r = cH - cL
    levels = array.from(cH, cL, cH + r, cL - r, cH + 2 * r, cL - 2 * r)
    names  = array.from("CBDR high", "CBDR low", "+1 SD", "-1 SD", "+2 SD", "-2 SD")
    for i = 0 to 5
        lv = levels.get(i)
        cLines.push(line.new(cX, lv, bar_index, lv, color = cbdrCol, style = i < 2 ? line.style_dashed : line.style_dotted))
        cLbls.push(label.new(bar_index, lv, names.get(i), style = label.style_label_left,
             color = color.new(color.white, 100), textcolor = textCol, size = size.tiny))
 
if cLines.size() > 0 and not inCbdr
    for ln in cLines
        ln.set_x2(bar_index)
    for lb in cLbls
        lb.set_x(bar_index)
 
// ---------- Bias per timeframe (from ITH / ITL breaks) ----------
// Bullish after a close above the last ITH, Bearish after a close below the last ITL.
showBias = input.bool(true, "Show bias table", group = "Bias")
biasPos  = input.string("Top right", "Table position", options = ["Top right", "Bottom right", "Top left", "Bottom left"], group = "Bias")
 
biasCalc() =>
    var float s1 = na
    var float s2 = na
    var float ith = na
    var float q1 = na
    var float q2 = na
    var float itl = na
    var int b = 0
    if high[1] > high[2] and high[1] > high
        if not na(s2) and s1 > s2 and s1 > high[1]
            ith := s1
        s2 := s1
        s1 := high[1]
    if low[1] < low[2] and low[1] < low
        if not na(q2) and q1 < q2 and q1 < low[1]
            itl := q1
        q2 := q1
        q1 := low[1]
    if not na(ith) and close > ith
        b := 1
        ith := na
    if not na(itl) and close < itl
        b := -1
        itl := na
    b
 
getBias(string tf) =>
    request.security(syminfo.tickerid, tf, biasCalc()[1], lookahead = barmerge.lookahead_on)
 
b4h = getBias(tf4h)
b1h = getBias(tf1h)
b30 = getBias(tf30)
b15 = getBias(tf15)
b5  = getBias(tf5)
 
biasCell(table t, int row, string tf, int b) =>
    table.cell(t, 0, row, tfText(tf), text_color = color.white, text_size = size.small)
    table.cell(t, 1, row, b == 1 ? "Bullish" : b == -1 ? "Bearish" : "-",
         text_color = b == 1 ? color.rgb(129, 199, 132) : b == -1 ? color.rgb(229, 115, 115) : color.gray,
         text_size = size.small)
 
if showBias and barstate.islast
    pos = switch biasPos
        "Bottom right" => position.bottom_right
        "Top left"     => position.top_left
        "Bottom left"  => position.bottom_left
        => position.top_right
    var table bt = table.new(pos, 2, 6, bgcolor = color.rgb(30, 30, 30, 10), border_color = color.rgb(80, 80, 80), border_width = 1)
    table.cell(bt, 0, 0, "TF", text_color = color.gray, text_size = size.small)
    table.cell(bt, 1, 0, "Bias", text_color = color.gray, text_size = size.small)
    biasCell(bt, 1, tf4h, b4h)
    biasCell(bt, 2, tf1h, b1h)
    biasCell(bt, 3, tf30, b30)
    biasCell(bt, 4, tf15, b15)
    biasCell(bt, 5, tf5,  b5)
 
// ---------- Alerts ----------
alertcondition(newSureBuy,  "SURE-SHOT Buy POI",  "SURE-SHOT Buy POI confirmed on {{ticker}} at {{close}}")
alertcondition(newSureSell, "SURE-SHOT Sell POI", "SURE-SHOT Sell POI confirmed on {{ticker}} at {{close}}")
alertcondition(bslSwept, "BSL swept", "Buy-side liquidity swept on {{ticker}} at {{close}}")
alertcondition(sslSwept, "SSL swept", "Sell-side liquidity swept on {{ticker}} at {{close}}")
alertcondition(viTouched, "Price in VI", "{{ticker}} touched a volume imbalance at {{close}}")
alertcondition(newIth, "New ITH", "New ITH on {{ticker}}")
alertcondition(newItl, "New ITL", "New ITL on {{ticker}}")
alertcondition(newBuy,    "New Buy POI/FVG",  "New Buy POI/FVG on {{ticker}}")
alertcondition(newSell,   "New Sell POI/FVG", "New Sell POI/FVG on {{ticker}}")
alertcondition(touchBuy,  "Price in Buy zone",  "{{ticker}} touched a Buy POI/FVG at {{close}}")
alertcondition(touchSell, "Price in Sell zone", "{{ticker}} touched a Sell POI/FVG at {{close}}")

← 返回列表