Fractal Bias H1/M15 + M5 Sweep Alerts

cuandrew · study · 140 行 · 点赞 34 · TradingView 原页

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

Pine Script

//@version=6
indicator("Fractal Bias H1/M15 + M5 Sweep Alerts", overlay = true, dynamic_requests = true)
 
bool alertShorts = input.bool(true, "Short-Vorbereitung melden", group = "M5-Sweep-Alarme")
bool alertLongs = input.bool(true, "Long-Vorbereitung melden", group = "M5-Sweep-Alarme")
 
// Fuer normale, zeitbasierte Kerzencharts.
// 1 = Long, -1 = Short, 0 = kein Bruch in der verfuegbaren Historie.
// Neue, ungebrochene Fraktale setzen die gespeicherte Richtung NICHT zurueck.
f_bias() =>
    var float upper = na
    var float lower = na
    var int direction = 0
 
    // Strikte 5er-Fraktale: zwei niedrigere Hochs / hoehere Tiefs je Seite.
    bool upFractal = high[2] > high[4] and high[2] > high[3] and high[2] > high[1] and high[2] > high
    bool downFractal = low[2] < low[4] and low[2] < low[3] and low[2] < low[1] and low[2] < low
 
    if upFractal
        upper := high[2]
    if downFractal
        lower := low[2]
 
    // Dochte und Schlusskurse exakt auf dem Level reichen nicht aus.
    // Verbrauchte Levels entfernen, damit nur neue Brueche zaehlen.
    if not na(upper) and close > upper
        direction := 1
        upper := na
    if not na(lower) and close < lower
        direction := -1
        lower := na
    direction
 
f_snapshot() =>
    int direction = f_bias()
    [direction, direction[1]]
 
f_readBias(string targetTf) =>
    var int result = 0
    if timeframe.in_seconds() < timeframe.in_seconds(targetTf)
        // Bestaetigter HTF-Wert ab Beginn der naechsten Zielkerze.
        result := nz(request.security(syminfo.tickerid, targetTf, f_bias()[1], lookahead = barmerge.lookahead_on, calc_bars_count = 100000), 0)
    else if timeframe.in_seconds() == timeframe.in_seconds(targetTf)
        // Historie auch hier separat anfordern, unabhaengig vom sichtbaren Ausschnitt.
        [current, previous] = request.security(syminfo.tickerid, targetTf, f_snapshot(), lookahead = barmerge.lookahead_off, calc_bars_count = 100000)
        result := barstate.isconfirmed ? current : nz(previous, 0)
    else
        // Auch auf H4, Tageschart usw. alle verfuegbaren Unterkerzen auswerten.
        [directions, closingTimes] = request.security_lower_tf(syminfo.tickerid, targetTf, [f_bias(), time_close], calc_bars_count = 100000)
        int cutoff = barstate.isrealtime ? timenow : time_close
        int i = array.size(directions) - 1
        while i >= 0
            if array.get(closingTimes, i) <= cutoff
                result := array.get(directions, i)
                break
            i -= 1
    result
 
f_label(int direction) =>
    direction == 1 ? "Long" : direction == -1 ? "Short" : "Nicht bestimmbar"
 
// Laeuft ausschliesslich im M5-Kontext. Ein Sweep ist hier das erste
// strikte Ueber-/Unterschreiten, ohne Rueckkehr- oder Schlusskursbedingung.
f_m5Sweeps() =>
    var float upper = na
    var float lower = na
    var int upperId = na
    var int lowerId = na
    var int sweptUpperId = na
    var int sweptLowerId = na
    var float sweptUpperPrice = na
    var float sweptLowerPrice = na
 
    // Nur abgeschlossene Kerzen fuer die Fraktalbildung verwenden.
    // [3] ist die Mitte; [2] und [1] sind die beiden fertigen rechten Kerzen.
    bool confirmedUpper = high[3] > high[5] and high[3] > high[4] and high[3] > high[2] and high[3] > high[1]
    bool confirmedLower = low[3] < low[5] and low[3] < low[4] and low[3] < low[2] and low[3] < low[1]
    if confirmedUpper
        upper := high[3]
        upperId := time[3]
    if confirmedLower
        lower := low[3]
        lowerId := time[3]
 
    // high/low der laufenden M5-Kerze: Die Lunte reicht bereits aus.
    if not na(upper) and high > upper
        sweptUpperId := upperId
        sweptUpperPrice := upper
        upper := na
    if not na(lower) and low < lower
        sweptLowerId := lowerId
        sweptLowerPrice := lower
        lower := na
    [sweptUpperId, sweptLowerId, sweptUpperPrice, sweptLowerPrice]
 
if not chart.is_standard or timeframe.isticks
    runtime.error("Bitte einen normalen, zeitbasierten Kerzenchart verwenden.")
 
int h1 = f_readBias("60")
int m15 = f_readBias("15")
bool ready = h1 != 0 and m15 != 0
bool aligned = ready and h1 == m15
color rowColor = aligned ? color.rgb(27, 135, 65) : color.rgb(190, 45, 45)
 
// Fuer die Live-Alarme wird bewusst die LAUFENDE M5-Kerze abgefragt.
// Auf groesseren Charts liefert security() live die juengste Unterkerze.
// Historische Alarmmarker werden damit nicht rekonstruiert.
[upperSweepId, lowerSweepId, upperSweepPrice, lowerSweepPrice] = request.security(syminfo.tickerid, "5", f_m5Sweeps(), lookahead = barmerge.lookahead_off, calc_bars_count = 100000)
 
// varip verhindert Wiederholungen auf weiteren Ticks derselben Chartkerze.
// Beim Start vorhandene Sweeps nicht nachtraeglich melden.
varip bool liveInitialized = false
varip int seenUpperId = na
varip int seenLowerId = na
if barstate.isrealtime
    if not liveInitialized
        seenUpperId := upperSweepId
        seenLowerId := lowerSweepId
        liveInitialized := true
    else
        bool newUpperSweep = not na(upperSweepId) and (na(seenUpperId) or upperSweepId != seenUpperId)
        bool newLowerSweep = not na(lowerSweepId) and (na(seenLowerId) or lowerSweepId != seenLowerId)
        if newUpperSweep
            // Auch ohne passenden Bias verbrauchen: kein spaeter Alarm.
            seenUpperId := upperSweepId
            if alertShorts and h1 == -1 and m15 == -1
                alert(syminfo.ticker + " | SHORT-Vorbereitung: H1 und M15 Short. Oberes M5-Fraktal bei " + str.tostring(upperSweepPrice, format.mintick) + " gesweept. Jetzt auf M1 nach einem Short-Entry suchen.", alert.freq_all)
        if newLowerSweep
            seenLowerId := lowerSweepId
            if alertLongs and h1 == 1 and m15 == 1
                alert(syminfo.ticker + " | LONG-Vorbereitung: H1 und M15 Long. Unteres M5-Fraktal bei " + str.tostring(lowerSweepPrice, format.mintick) + " gesweept. Jetzt auf M1 nach einem Long-Entry suchen.", alert.freq_all)
 
var table panel = table.new(position.top_right, 2, 3, border_width = 1, border_color = color.new(color.white, 60))
if barstate.islast
    table.cell(panel, 0, 0, "Zeitrahmen", text_color = color.white, bgcolor = color.rgb(40, 40, 40))
    table.cell(panel, 1, 0, "Bias", text_color = color.white, bgcolor = color.rgb(40, 40, 40))
    table.cell(panel, 0, 1, "H1", text_color = color.white, bgcolor = rowColor)
    table.cell(panel, 1, 1, f_label(h1), text_color = color.white, bgcolor = rowColor)
    table.cell(panel, 0, 2, "M15", text_color = color.white, bgcolor = rowColor)
    table.cell(panel, 1, 2, f_label(m15), text_color = color.white, bgcolor = rowColor)

← 返回列表