FVG Multi

Tekay88 · study · 330 行 · 点赞 20 · TradingView 原页

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

Pine Script

// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Tekay88
 
//@version=6
indicator("FVG Pro – LTF/HTF + OB + Levels", shorttitle = "FVG Pro", overlay = true, max_boxes_count = 500, max_lines_count = 500, max_labels_count = 500)
 
// ═════════ LTF FVG ═════════
g1 = "LTF FVG"
showLtf    = input.bool(true, "LTF FVGs anzeigen", group = g1)
showBull   = input.bool(true, "Long", inline = "dir", group = g1)
showBear   = input.bool(true, "Short", inline = "dir", group = g1)
bullFill   = input.color(color.new(#7FA07F, 50), "Long Füllung", inline = "bc", group = g1)
bullBord   = input.color(#5F7F5F, "Rand", inline = "bc", group = g1)
bearFill   = input.color(color.new(#7B1F2E, 55), "Short Füllung", inline = "sc", group = g1)
bearBord   = input.color(#5A1020, "Rand", inline = "sc", group = g1)
ltfMax     = input.int(20, "Max. aktive LTF FVGs", minval = 1, maxval = 150, group = g1)
sizeMode   = input.string("Ticks", "Mindestgrösse", options = ["Keine", "Ticks", "ATR"], group = g1)
minTicks   = input.int(4, "Min. Ticks", minval = 1, group = g1)
atrMult    = input.float(0.25, "Min. ATR-Faktor (ATR 14)", step = 0.05, group = g1)
ltfMitMode = input.string("Vollständig", "Erledigt bei", options = ["Berührung", "50% (CE)", "Vollständig"], group = g1)
ltfDel     = input.bool(true, "Erledigte LTF FVGs löschen", group = g1)
mitCol     = input.color(color.new(color.gray, 85), "Farbe erledigte FVGs", group = g1)
ltfCE      = input.bool(false, "50%-Linie (CE)", inline = "ce", group = g1)
ltfCECol   = input.color(color.new(color.gray, 30), "", inline = "ce", group = g1)
ltfLenMode = input.string("Kurz", "Box-Länge", options = ["Durchgehend", "Kurz"], group = g1)
ltfLen     = input.int(10, "Kurz: Länge (Kerzen)", minval = 1, group = g1)
ltfMaxAge  = input.int(0, "Max. Alter in Kerzen (0 = aus)", minval = 0, group = g1)
 
// ═════════ ORDER BLOCKS ═════════
g2 = "Order Blocks"
showOB = input.bool(true, "OB vor FVG anzeigen", group = g2)
onlyOB = input.bool(false, "Nur FVGs mit OB anzeigen", group = g2)
obLook = input.int(3, "OB-Suche (Kerzen vor dem FVG)", minval = 1, maxval = 10, group = g2)
obFill = input.color(color.new(#D2B48C, 80), "OB Füllung", inline = "ob", group = g2)
obBord = input.color(#C9A87A, "Rand", inline = "ob", group = g2)
obTxt  = input.color(color.white, "Text", inline = "ob", group = g2)
 
// ═════════ HTF FVG ═════════
g3 = "HTF FVG (H4 / Daily)"
showH4      = input.bool(true, "H4 FVGs", group = g3)
h4Fill      = input.color(color.new(color.white, 100), "H4 Füllung", inline = "h4", group = g3)
h4Bord      = input.color(color.white, "Rand", inline = "h4", group = g3)
h4Mid       = input.color(#E040FB, "Mitte", inline = "h4", group = g3)
showD       = input.bool(true, "Daily FVGs", group = g3)
dFill       = input.color(color.new(color.white, 100), "Daily Füllung", inline = "d", group = g3)
dBord       = input.color(color.white, "Rand", inline = "d", group = g3)
dMid        = input.color(#424242, "Mitte", inline = "d", group = g3)
htfTxtCol   = input.color(#2962FF, "Textfarbe", group = g3)
htfBW       = input.int(2, "Randdicke", minval = 1, maxval = 4, group = g3)
htfShowMid  = input.bool(true, "Mittellinie (CE) zeigen", group = g3)
htfMinTicks = input.int(4, "Min. Grösse (Ticks)", minval = 0, group = g3)
htfMitMode  = input.string("Vollständig", "Erledigt bei", options = ["Berührung", "50% (CE)", "Vollständig"], group = g3)
htfDel      = input.bool(true, "Erledigte HTF FVGs löschen", group = g3)
htfMax      = input.int(8, "Max. aktive FVGs pro HTF", minval = 1, maxval = 50, group = g3)
 
// ═════════ TAGES-LEVELS ═════════
g4 = "Tages-Levels"
showToday  = input.bool(true, "Tageshoch / -tief", inline = "t", group = g4)
todayCol   = input.color(#FF9800, "", inline = "t", group = g4)
showPrev   = input.bool(true, "Vortageshoch / -tief", inline = "p", group = g4)
prevCol    = input.color(#2962FF, "", inline = "p", group = g4)
lvlW       = input.int(1, "Liniendicke", minval = 1, maxval = 4, group = g4)
showLvlLbl = input.bool(true, "Beschriftung", group = g4)
 
// ═════════ ALARME ═════════
g5 = "Alarme"
alLvl      = input.bool(true, "FVG an Tageshoch/-tief (heute + Vortag)", group = g5)
alOnlyOb   = input.bool(false, "…nur wenn FVG mit OB", group = g5)
alTol      = input.int(8, "Level-Toleranz (Ticks)", minval = 0, group = g5)
alBounce   = input.bool(true, "First-Touch-Abpraller an H4/Daily FVG", group = g5)
bounceBars = input.int(3, "Abpraller innerhalb X Kerzen nach Touch", minval = 0, group = g5)
useSess    = input.bool(true, "Alarme nur im Zeitfenster", group = g5)
sess       = input.session("0800-2200", "Zeitfenster (Zürich-Zeit)", group = g5)
showMarks  = input.bool(true, "Signale im Chart markieren", group = g5)
 
// ═════════ BASIS ═════════
int ms     = timeframe.in_seconds() * 1000
float tick = syminfo.mintick
atrVal     = ta.atr(14)
patLo      = ta.lowest(low, obLook + 2)
patHi      = ta.highest(high, obLook + 2)
inSess     = not useSess or not na(time(timeframe.period, sess, "Europe/Zurich"))
newDay     = timeframe.change("D")
newH4      = timeframe.change("240")
int extT   = time + 10 * ms
 
type Zone
    box    bx
    line   mid
    box    ob
    float  top
    float  bot
    bool   bull
    int    born
    string kind
    bool   touched  = false
    int    touchBar = na
    bool   bounced  = false
 
var array<Zone> ltf = array.new<Zone>()
var array<Zone> htf = array.new<Zone>()
 
sizeOk(float gap) =>
    switch sizeMode
        "Ticks" => gap >= minTicks * tick
        "ATR"   => gap >= atrVal * atrMult
        => true
 
mitLevel(Zone z, string mode) =>
    mode == "Berührung" ? (z.bull ? z.top : z.bot) : mode == "50% (CE)" ? (z.top + z.bot) / 2 : (z.bull ? z.bot : z.top)
 
isDone(Zone z, string mode) =>
    float lvl = mitLevel(z, mode)
    z.bull ? low <= lvl : high >= lvl
 
killZone(Zone z) =>
    box.delete(z.bx)
    line.delete(z.mid)
    box.delete(z.ob)
 
freezeZone(Zone z) =>
    box.set_right(z.bx, time)
    box.set_bgcolor(z.bx, mitCol)
    box.set_border_color(z.bx, mitCol)
    if not na(z.mid)
        line.set_x2(z.mid, time)
 
// ═════════ TAGES-LEVELS ═════════
var float dHi = na
var float dLo = na
var float pdh = na
var float pdl = na
var int dStart = na
var line tHiL = na
var line tLoL = na
var line pHiL = na
var line pLoL = na
var label tHiLb = na
var label tLoLb = na
var label pHiLb = na
var label pLoLb = na
 
mkLine(float y, color c, string st) =>
    line.new(dStart, y, extT, y, xloc = xloc.bar_time, color = c, style = st, width = lvlW)
mkLbl(float y, string txt, color c) =>
    label.new(extT, y, txt, xloc = xloc.bar_time, color = color.new(color.black, 100), style = label.style_label_left, textcolor = c, size = size.small)
updLine(line l, float y) =>
    if not na(l)
        line.set_y1(l, y)
        line.set_xy2(l, extT, y)
updLbl(label lb, float y) =>
    if not na(lb)
        label.set_xy(lb, extT, y)
 
if newDay
    pdh    := dHi
    pdl    := dLo
    dHi    := high
    dLo    := low
    dStart := time
    line.delete(tHiL), line.delete(tLoL), line.delete(pHiL), line.delete(pLoL)
    label.delete(tHiLb), label.delete(tLoLb), label.delete(pHiLb), label.delete(pLoLb)
    if showToday
        tHiL := mkLine(dHi, todayCol, line.style_solid)
        tLoL := mkLine(dLo, todayCol, line.style_solid)
        if showLvlLbl
            tHiLb := mkLbl(dHi, "Tageshoch", todayCol)
            tLoLb := mkLbl(dLo, "Tagestief", todayCol)
    if showPrev and not na(pdh)
        pHiL := mkLine(pdh, prevCol, line.style_dashed)
        pLoL := mkLine(pdl, prevCol, line.style_dashed)
        if showLvlLbl
            pHiLb := mkLbl(pdh, "Vortageshoch", prevCol)
            pLoLb := mkLbl(pdl, "Vortagestief", prevCol)
else
    dHi := na(dHi) ? high : math.max(dHi, high)
    dLo := na(dLo) ? low : math.min(dLo, low)
 
updLine(tHiL, dHi), updLine(tLoL, dLo), updLine(pHiL, pdh), updLine(pLoL, pdl)
updLbl(tHiLb, dHi), updLbl(tLoLb, dLo), updLbl(pHiLb, pdh), updLbl(pLoLb, pdl)
 
touchLvl(float lvl) =>
    float tol = alTol * tick
    not na(lvl) and patLo <= lvl + tol and patHi >= lvl - tol
 
// ═════════ SIGNAL-FLAGS ═════════
bool sigLvlBull = false
bool sigLvlBear = false
bool sigBncBull = false
bool sigBncBear = false
 
// ═════════ LTF: UPDATE ═════════
if ltf.size() > 0
    for i = ltf.size() - 1 to 0
        z = ltf.get(i)
        int age = bar_index - z.born
        if age > 0
            if isDone(z, ltfMitMode)
                if ltfDel
                    killZone(z)
                else if ltfLenMode == "Kurz" and age > ltfLen
                    box.set_bgcolor(z.bx, mitCol)
                    box.set_border_color(z.bx, mitCol)
                else
                    freezeZone(z)
                ltf.remove(i)
            else if ltfMaxAge > 0 and age > ltfMaxAge
                killZone(z)
                ltf.remove(i)
            else if ltfLenMode == "Durchgehend" or age <= ltfLen
                box.set_right(z.bx, time)
                if not na(z.mid)
                    line.set_x2(z.mid, time)
 
// ═════════ LTF: NEUE FVGs + OB ═════════
if showLtf and barstate.isconfirmed
    bool bullGap = showBull and low > high[2] and sizeOk(low - high[2])
    bool bearGap = showBear and high < low[2] and sizeOk(low[2] - high)
    if bullGap or bearGap
        bool isBull = bullGap
        int obIdx = na
        for k = 2 to 1 + obLook
            if isBull ? close[k] < open[k] : close[k] > open[k]
                obIdx := k
                break
        bool hasOb = not na(obIdx)
        if not onlyOB or hasOb
            float top = isBull ? low : low[2]
            float bot = isBull ? high[2] : high
            box b = box.new(time[1], top, time, bot, xloc = xloc.bar_time, bgcolor = isBull ? bullFill : bearFill, border_color = isBull ? bullBord : bearBord, border_width = 1)
            line m = na
            if ltfCE
                m := line.new(time[1], (top + bot) / 2, time, (top + bot) / 2, xloc = xloc.bar_time, color = ltfCECol, style = line.style_dashed)
            box o = na
            if showOB and hasOb
                o := box.new(time[obIdx], high[obIdx], time, low[obIdx], xloc = xloc.bar_time, bgcolor = obFill, border_color = obBord, text = "OB", text_color = obTxt, text_size = size.tiny, text_valign = text.align_bottom)
            ltf.push(Zone.new(b, m, o, top, bot, isBull, bar_index, "LTF"))
            if ltf.size() > ltfMax
                killZone(ltf.shift())
 
            // Alarm: FVG (+OB) an Tageshoch/-tief
            if alLvl and inSess and (not alOnlyOb or hasOb)
                bool hit = isBull ? (touchLvl(dLo) or touchLvl(pdl)) : (touchLvl(dHi) or touchLvl(pdh))
                if hit
                    string nm = isBull ? (touchLvl(pdl) ? "Vortagestief" : "Tagestief") : (touchLvl(pdh) ? "Vortageshoch" : "Tageshoch")
                    if isBull
                        sigLvlBull := true
                    else
                        sigLvlBear := true
                    alert((isBull ? "🟢 LONG FVG" : "🔴 SHORT FVG") + (hasOb ? " + OB" : "") + " am " + nm + " | " + syminfo.ticker + " @ " + str.tostring(close, format.mintick), alert.freq_once_per_bar_close)
 
// ═════════ HTF FVG ═════════
[h4BullC, h4BearC, h4BTop, h4BBot, h4STop, h4SBot, h4T] = request.security(syminfo.tickerid, "240", [low[1] > high[3], high[1] < low[3], low[1], high[3], low[3], high[1], time[2]], lookahead = barmerge.lookahead_on)
[dBullC, dBearC, dBTop, dBBot, dSTop, dSBot, dT] = request.security(syminfo.tickerid, "D", [low[1] > high[3], high[1] < low[3], low[1], high[3], low[3], high[1], time[2]], lookahead = barmerge.lookahead_on)
 
int chartSec = timeframe.in_seconds()
h4On = showH4 and chartSec < timeframe.in_seconds("240")
dOn  = showD and chartSec < timeframe.in_seconds("D")
 
htfTrim(string k) =>
    int cnt = 0
    int first = -1
    for i = 0 to htf.size() - 1
        if htf.get(i).kind == k
            cnt += 1
            if first == -1
                first := i
    if cnt > htfMax and first >= 0
        killZone(htf.get(first))
        htf.remove(first)
 
addHtf(float top, float bot, bool bull, int tL, string k, color f, color bd, color mc, string txt) =>
    box b = box.new(tL, top, extT, bot, xloc = xloc.bar_time, bgcolor = f, border_color = bd, border_width = htfBW, text = txt, text_color = htfTxtCol, text_size = size.small, text_halign = text.align_right, text_valign = text.align_center)
    line m = na
    if htfShowMid
        m := line.new(tL, (top + bot) / 2, extT, (top + bot) / 2, xloc = xloc.bar_time, color = mc, style = line.style_dashed)
    htf.push(Zone.new(b, m, na, top, bot, bull, bar_index, k))
    htfTrim(k)
 
if h4On and newH4
    if h4BullC and h4BTop - h4BBot >= htfMinTicks * tick
        addHtf(h4BTop, h4BBot, true, h4T, "H4", h4Fill, h4Bord, h4Mid, "H4 FVG")
    if h4BearC and h4STop - h4SBot >= htfMinTicks * tick
        addHtf(h4STop, h4SBot, false, h4T, "H4", h4Fill, h4Bord, h4Mid, "H4 FVG")
 
if dOn and newDay
    if dBullC and dBTop - dBBot >= htfMinTicks * tick
        addHtf(dBTop, dBBot, true, dT, "Daily", dFill, dBord, dMid, "Daily FVG")
    if dBearC and dSTop - dSBot >= htfMinTicks * tick
        addHtf(dSTop, dSBot, false, dT, "Daily", dFill, dBord, dMid, "Daily FVG")
 
// HTF: First Touch, Abpraller, Mitigation
if htf.size() > 0
    for i = htf.size() - 1 to 0
        z = htf.get(i)
        bool entered = z.bull ? low <= z.top : high >= z.bot
        if not z.touched and entered
            z.touched  := true
            z.touchBar := bar_index
        if z.touched and not z.bounced and barstate.isconfirmed and bar_index - z.touchBar <= bounceBars
            bool rej = z.bull ? close > z.top : close < z.bot
            if rej
                z.bounced := true
                if alBounce and inSess
                    if z.bull
                        sigBncBull := true
                    else
                        sigBncBear := true
                    alert((z.bull ? "🟢 ABPRALLER LONG" : "🔴 ABPRALLER SHORT") + " am " + z.kind + " FVG (First Touch) | " + syminfo.ticker + " @ " + str.tostring(close, format.mintick), alert.freq_once_per_bar_close)
        if isDone(z, htfMitMode)
            if htfDel
                killZone(z)
            else
                freezeZone(z)
            htf.remove(i)
        else
            box.set_right(z.bx, extT)
            if not na(z.mid)
                line.set_x2(z.mid, extT)
 
// ═════════ MARKER + ALERTCONDITIONS ═════════
plotshape(showMarks and sigLvlBull, "Long FVG am Level", shape.triangleup, location.belowbar, color.new(#00C853, 0), size = size.small)
plotshape(showMarks and sigLvlBear, "Short FVG am Level", shape.triangledown, location.abovebar, color.new(#D50000, 0), size = size.small)
plotshape(showMarks and sigBncBull, "Abpraller Long HTF", shape.diamond, location.belowbar, color.new(#00B8D4, 0), size = size.tiny)
plotshape(showMarks and sigBncBear, "Abpraller Short HTF", shape.diamond, location.abovebar, color.new(#FF6D00, 0), size = size.tiny)
 
alertcondition(sigLvlBull, "Long FVG am Tageslevel", "🟢 Long FVG an Tageshoch/-tief auf {{ticker}}")
alertcondition(sigLvlBear, "Short FVG am Tageslevel", "🔴 Short FVG an Tageshoch/-tief auf {{ticker}}")
alertcondition(sigBncBull, "Abpraller Long an HTF FVG", "🟢 First-Touch-Abpraller an H4/Daily FVG auf {{ticker}}")
alertcondition(sigBncBear, "Abpraller Short an HTF FVG", "🔴 First-Touch-Abpraller an H4/Daily FVG auf {{ticker}}")

← 返回列表