Harmonic Patterns All-in-One Scanner [TradingFinder]XABCD 3Drive

TradingFinder · study · 928 行 · 点赞 296 · 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/
// © TradingFinder
 
//@version=6
indicator("Harmonic Patterns All-in-One Scanner [TradingFinder]XABCD 3Drive", "Auto Harmonic", overlay = true, max_bars_back = 5000, max_lines_count = 500, max_labels_count = 500, dynamic_requests = true)
 
PP = input.int(3, "ZigZag Pivot Period", minval = 1, group = "Logical Setting")
ShVF = input.bool(false, "Show Valid Format", group = "Logical Setting")
FLPC = input.bool(false, "Formation Last Pivot Confirm", group = "Logical Setting")
FLPCPeriod = input.int(2, "Last Pivot Confirm Period", minval = 0, group = "Logical Setting")
ScanHistory = input.int(3000, "History Bars Per Symbol", minval = 500, maxval = 5000, group = "Logical Setting")
 
ScanBull = input.bool(true, "Bullish", group = "Harmonic Types")
ScanBear = input.bool(true, "Bearish", group = "Harmonic Types")
UseGartley = input.bool(true, "Gartley", group = "Harmonic Types")
UseButterfly = input.bool(true, "Butterfly", group = "Harmonic Types")
UseShark = input.bool(true, "Shark", group = "Harmonic Types")
UseBat = input.bool(true, "Bat", group = "Harmonic Types")
UseDeepCrab = input.bool(true, "Deep Crab", group = "Harmonic Types")
UseCypher = input.bool(true, "Cypher", group = "Harmonic Types")
UseAltShark = input.bool(true, "Alternative Shark", group = "Harmonic Types")
UseFiveZero = input.bool(true, "5-0", group = "Harmonic Types")
UseABCD = input.bool(true, "ABCD", group = "Harmonic Types")
UseThreeDrive = input.bool(true, "3 Drive", group = "Harmonic Types")
UseAltBat = input.bool(true, "Alternate Bat", group = "Harmonic Types")
UseNenStar = input.bool(true, "Nen-Star", group = "Harmonic Types")
UseDragon = input.bool(true, "Dragon", group = "Harmonic Types")
 
 
 
S01 = input.symbol("OANDA:XAUUSD", "Symbol", group = "Symbol 1")
TF01 = input.timeframe("5", "Timeframe", group = "Symbol 1")
 
S02 = input.symbol("OANDA:XAGUSD", "Symbol", group = "Symbol 2")
TF02 = input.timeframe("5", "Timeframe", group = "Symbol 2")
 
S03 = input.symbol("CAPITALCOM:US30", "Symbol", group = "Symbol 3")
TF03 = input.timeframe("5", "Timeframe", group = "Symbol 3")
 
S04 = input.symbol("SP:SPX", "Symbol", group = "Symbol 4")
TF04 = input.timeframe("5", "Timeframe", group = "Symbol 4")
 
S05 = input.symbol("NASDAQ:NDX", "Symbol", group = "Symbol 5")
TF05 = input.timeframe("5", "Timeframe", group = "Symbol 5")
 
S06 = input.symbol("OANDA:EURUSD", "Symbol", group = "Symbol 6")
TF06 = input.timeframe("5", "Timeframe", group = "Symbol 6")
 
S07 = input.symbol("OANDA:GBPUSD", "Symbol", group = "Symbol 7")
TF07 = input.timeframe("5", "Timeframe", group = "Symbol 7")
 
S08 = input.symbol("OANDA:USDJPY", "Symbol", group = "Symbol 8")
TF08 = input.timeframe("5", "Timeframe", group = "Symbol 8")
 
S09 = input.symbol("BITSTAMP:BTCUSD", "Symbol", group = "Symbol 9")
TF09 = input.timeframe("5", "Timeframe", group = "Symbol 9")
 
S10 = input.symbol("COINBASE:ETHUSD", "Symbol", group = "Symbol 10")
TF10 = input.timeframe("5", "Timeframe", group = "Symbol 10")
 
 
ShowChartPatterns = input.bool(true, "Draw detected patterns", group = "Display")
// MaxChartPatterns = input.int(20, "Max patterns on chart", minval = 1, maxval = 50, group = "Display")
LineWidth = input.int(2, "Line width", minval = 1, maxval = 4, group = "Display")
LabelSizeInput = input.string("Small", "Label size", options = ["Tiny", "Small", "Normal", "Large"], group = "Display")
TableSize = input.string(size.normal, "Table Size", options = [size.auto, size.tiny, size.small, size.normal, size.large, size.huge], group = "Display")
TablePosition = input.string(position.top_right, "Table Position", options = [position.top_left, position.top_center, position.top_right, position.middle_left, position.middle_center, position.middle_right, position.bottom_left, position.bottom_center, position.bottom_right], group = "Display")
FillTransparency = input.int(93, "Fill Transparency", minval = 0, maxval = 100, group = "Display")
BullColor = input.color(#0cd4b6, "Bullish", inline = "Colors", group = "Display")
BearColor = input.color(#df7906, "Bearish", inline = "Colors", group = "Display")
 
 
ChartAlerts = input.bool(false, "Alert on new chart signal", group = "Alert")
ScreenerAlerts = input.bool(false, "Alert on new screener signals", group = "Alert")
 
InRange(v, lo, hi) => not na(v) and v >= lo and v <= hi
Ratio(a, b) => b != 0.0 ? math.abs(a / b) : na
LabelSize(s) => s == "Tiny" ? size.tiny : s == "Normal" ? size.normal : s == "Large" ? size.large : size.small
PatternName(id) => id == 1 ? "Gartley" : id == 2 ? "Butterfly" : id == 3 ? "Shark" : id == 4 ? "Bat" : id == 5 ? "Deep Crab" : id == 6 ? "Cypher" : id == 7 ? "Alternative Shark" : id == 8 ? "5-0" : id == 9 ? "ABCD" : id == 10 ? "3 Drive" : id == 11 ? "Alternate Bat" : id == 12 ? "Nen-Star" : id == 13 ? "Dragon" : "—"
Family(id) => id == 8 ? 2 : id == 9 ? 3 : id == 10 ? 4 : id > 0 ? 1 : 0
Elapsed(signalTime, referenceTime) =>
    if na(signalTime) or na(referenceTime)
        "—"
    else
        mins = math.max(0, int(math.floor((referenceTime - signalTime) / 60000.0)))
        weeks = int(math.floor(mins / 10080.0))
        remW = mins - weeks * 10080
        days = int(math.floor(remW / 1440.0))
        remD = remW - days * 1440
        hours = int(math.floor(remD / 60.0))
        minutes = remD - hours * 60
        txt = weeks > 0 ? str.tostring(weeks) + "w " + str.tostring(days) + "d " + str.tostring(hours) + "h " + str.tostring(minutes) + "m" : days > 0 ? str.tostring(days) + "d " + str.tostring(hours) + "h " + str.tostring(minutes) + "m" : hours > 0 ? str.tostring(hours) + "h " + str.tostring(minutes) + "m" : str.tostring(minutes) + "m"
        txt + " ago"
 
TFText(tf) =>
    tf == "1" ? "1 Minute" : tf == "3" ? "3 Minutes" : tf == "5" ? "5 Minutes" : tf == "15" ? "15 Minutes" : tf == "30" ? "30 Minutes" : tf == "45" ? "45 Minutes" : tf == "60" ? "1 Hour" : tf == "120" ? "2 Hours" : tf == "180" ? "3 Hours" : tf == "240" ? "4 Hours" : tf == "D" ? "1 Day" : tf == "W" ? "1 Week" : tf == "M" ? "1 Month" : tf
 
Exchange(sym) =>
    pos = str.pos(sym, ":")
    pos >= 0 ? str.substring(sym, 0, pos) : "—"
 
Ticker(sym) =>
    pos = str.pos(sym, ":")
    pos >= 0 ? str.substring(sym, pos + 1, str.length(sym)) : sym
 
PatternShort(id) => id == 5 ? "Deep Crab" : id == 7 ? "Alt Shark" : id == 10 ? "3 Drive" : id == 11 ? "Alt Bat" : id == 12 ? "Nen-Star" : PatternName(id)
 
ZigZag(pp) =>
    var types = array.new_string()
    var values = array.new_float()
    var indexes = array.new_int()
    highPivot = ta.pivothigh(pp, pp)
    lowPivot = ta.pivotlow(pp, pp)
    hasHigh = not na(highPivot)
    hasLow = not na(lowPivot)
    highValue = ta.valuewhen(hasHigh, high[pp], 0)
    lowValue = ta.valuewhen(hasLow, low[pp], 0)
    highIndex = ta.valuewhen(hasHigh, bar_index[pp], 0)
    lowIndex = ta.valuewhen(hasLow, bar_index[pp], 0)
    if hasHigh and hasLow
        if array.size(types) >= 1
            lastType = array.get(types, array.size(types) - 1)
            if lastType == "L" or lastType == "LL"
                if lowPivot < array.get(values, array.size(types) - 1)
                    array.remove(types, array.size(types) - 1)
                    array.remove(values, array.size(values) - 1)
                    array.remove(indexes, array.size(indexes) - 1)
                    array.push(types, array.size(types) > 2 ? array.get(values, array.size(types) - 2) < lowValue ? "HL" : "LL" : "L")
                    array.push(values, lowValue)
                    array.push(indexes, lowIndex)
                else
                    array.push(types, array.size(types) > 2 ? array.get(values, array.size(types) - 2) < highValue ? "HH" : "LH" : "H")
                    array.push(values, highValue)
                    array.push(indexes, highIndex)
            else if lastType == "H" or lastType == "HH"
                if highPivot > array.get(values, array.size(types) - 1)
                    array.remove(types, array.size(types) - 1)
                    array.remove(values, array.size(values) - 1)
                    array.remove(indexes, array.size(indexes) - 1)
                    array.push(types, array.size(types) > 2 ? array.get(values, array.size(types) - 2) < highValue ? "HH" : "LH" : "H")
                    array.push(values, highValue)
                    array.push(indexes, highIndex)
                else
                    array.push(types, array.size(types) > 2 ? array.get(values, array.size(types) - 2) < lowValue ? "HL" : "LL" : "L")
                    array.push(values, lowValue)
                    array.push(indexes, lowIndex)
            else if lastType == "LH"
                if highPivot < array.get(values, array.size(types) - 1)
                    array.push(types, array.size(types) > 2 ? array.get(values, array.size(types) - 2) < lowValue ? "HL" : "LL" : "L")
                    array.push(values, lowValue)
                    array.push(indexes, lowIndex)
                else if highPivot > array.get(values, array.size(types) - 1)
                    if close < array.get(values, array.size(types) - 1)
                        array.remove(types, array.size(types) - 1)
                        array.remove(values, array.size(values) - 1)
                        array.remove(indexes, array.size(indexes) - 1)
                        array.push(types, array.size(types) > 2 ? array.get(values, array.size(types) - 2) < highValue ? "HH" : "LH" : "H")
                        array.push(values, highValue)
                        array.push(indexes, highIndex)
                    else if close > array.get(values, array.size(types) - 1)
                        array.push(types, array.size(types) > 2 ? array.get(values, array.size(types) - 2) < lowValue ? "HL" : "LL" : "L")
                        array.push(values, lowValue)
                        array.push(indexes, lowIndex)
            else if lastType == "HL"
                if lowPivot > array.get(values, array.size(types) - 1)
                    array.push(types, array.size(types) > 2 ? array.get(values, array.size(types) - 2) < highValue ? "HH" : "LH" : "H")
                    array.push(values, highValue)
                    array.push(indexes, highIndex)
                else if lowPivot < array.get(values, array.size(types) - 1)
                    if close > array.get(values, array.size(types) - 1)
                        array.remove(types, array.size(types) - 1)
                        array.remove(values, array.size(values) - 1)
                        array.remove(indexes, array.size(indexes) - 1)
                        array.push(types, array.size(types) > 2 ? array.get(values, array.size(types) - 2) < lowValue ? "HL" : "LL" : "L")
                        array.push(values, lowValue)
                        array.push(indexes, lowIndex)
                    else if close < array.get(values, array.size(types) - 1)
                        array.push(types, array.size(types) > 2 ? array.get(values, array.size(types) - 2) < highValue ? "HH" : "LH" : "H")
                        array.push(values, highValue)
                        array.push(indexes, highIndex)
    else if hasHigh
        if array.size(types) == 0
            array.insert(types, 0, "H")
            array.insert(values, 0, highValue)
            array.insert(indexes, 0, highIndex)
        else
            lastType = array.get(types, array.size(types) - 1)
            if lastType == "L" or lastType == "HL" or lastType == "LL"
                if highPivot > array.get(values, array.size(types) - 1)
                    array.push(types, array.size(types) > 2 ? array.get(values, array.size(types) - 2) < highValue ? "HH" : "LH" : "H")
                    array.push(values, highValue)
                    array.push(indexes, highIndex)
                else if highPivot < array.get(values, array.size(types) - 1)
                    array.remove(types, array.size(types) - 1)
                    array.remove(values, array.size(values) - 1)
                    array.remove(indexes, array.size(indexes) - 1)
                    array.push(types, array.size(types) > 2 ? array.get(values, array.size(types) - 2) < lowValue ? "HL" : "LL" : "L")
                    array.push(values, lowValue)
                    array.push(indexes, lowIndex)
            else if lastType == "H" or lastType == "HH" or lastType == "LH"
                if array.get(values, array.size(values) - 1) < highValue
                    array.remove(types, array.size(types) - 1)
                    array.remove(values, array.size(values) - 1)
                    array.remove(indexes, array.size(indexes) - 1)
                    array.push(types, array.size(types) > 2 ? array.get(values, array.size(types) - 2) < highValue ? "HH" : "LH" : "H")
                    array.push(values, highValue)
                    array.push(indexes, highIndex)
    else if hasLow
        if array.size(types) == 0
            array.insert(types, 0, "L")
            array.insert(values, 0, lowValue)
            array.insert(indexes, 0, lowIndex)
        else
            lastType = array.get(types, array.size(types) - 1)
            if lastType == "H" or lastType == "HH" or lastType == "LH"
                if lowPivot < array.get(values, array.size(types) - 1)
                    array.push(types, array.size(types) > 2 ? array.get(values, array.size(types) - 2) < lowValue ? "HL" : "LL" : "L")
                    array.push(values, lowValue)
                    array.push(indexes, lowIndex)
                else if lowPivot > array.get(values, array.size(types) - 1)
                    array.remove(types, array.size(types) - 1)
                    array.remove(values, array.size(values) - 1)
                    array.remove(indexes, array.size(indexes) - 1)
                    array.push(types, array.size(types) > 2 ? array.get(values, array.size(types) - 2) < highValue ? "HH" : "LH" : "H")
                    array.push(values, highValue)
                    array.push(indexes, highIndex)
            else if lastType == "L" or lastType == "HL" or lastType == "LL"
                if array.get(values, array.size(values) - 1) > lowValue
                    array.remove(types, array.size(types) - 1)
                    array.remove(values, array.size(values) - 1)
                    array.remove(indexes, array.size(indexes) - 1)
                    array.push(types, array.size(types) > 2 ? array.get(values, array.size(types) - 2) < lowValue ? "HL" : "LL" : "L")
                    array.push(values, lowValue)
                    array.push(indexes, lowIndex)
    float cP = na
    int cI = na
    float bP = na
    int bI = na
    float aP = na
    int aI = na
    float xP = na
    int xI = na
    float oP = na
    int oI = na
    while array.size(types) > 32
        array.shift(types)
        array.shift(values)
        array.shift(indexes)
    if array.size(types) > 11
        z = array.size(types)
        cP := array.get(values, z - 1)
        cI := array.get(indexes, z - 1)
        bP := array.get(values, z - 2)
        bI := array.get(indexes, z - 2)
        aP := array.get(values, z - 3)
        aI := array.get(indexes, z - 3)
        xP := array.get(values, z - 4)
        xI := array.get(indexes, z - 4)
        oP := array.get(values, z - 5)
        oI := array.get(indexes, z - 5)
    [cP, cI, bP, bI, aP, aI, xP, xI, oP, oI]
 
AddSymbol(arr, symbol) =>
    clean = str.trim(symbol)
    if str.length(clean) > 0 and not array.includes(arr, clean)
        array.push(arr, clean)
 
NewEvent(keys, slot, match, key) =>
    event = false
    if match and not na(key)
        oldKey = array.get(keys, slot)
        if na(oldKey) or oldKey != key
            array.set(keys, slot, key)
            event := true
    event
 
Engine() =>
    var eventKeys = array.new_int(26, na)
    var int trackedC = na
    var float hiSinceC = na
    var float loSinceC = na
    var lastId = 0
    var lastDir = 0
    var int lastSignalTime = na
    var float lastD = na
    var float lastEntry = na
    var lastConfirmed = false
    var pendingConfirm = false
    var lastOverlap = 0
    var int lastAnchor = na
    [cP, cI, bP, bI, aP, aI, xP, xI, oP, oI] = ZigZag(PP)
 
    if not na(cI)
        if na(trackedC) or trackedC != cI
            trackedC := cI
            hiSinceC := high
            loSinceC := low
        else
            hiSinceC := math.max(hiSinceC, high)
            loSinceC := math.min(loSinceC, low)
 
    n = FLPC ? math.min(FLPCPeriod, math.max(PP - 1, 0)) : 0
    dBull = bar_index >= n ? low[n] : na
    dBear = bar_index >= n ? high[n] : na
    confirmLow = ta.lowest(low, n + 1)
    confirmHigh = ta.highest(high, n + 1)
    dConfirmBull = not FLPC or confirmLow == dBull
    dConfirmBear = not FLPC or confirmHigh == dBear
    dI = bar_index - n
    orderX = not na(xI) and dI > cI and cI > bI and bI > aI and aI > xI
    orderO = not na(oI) and orderX and xI > oI
    orderA = not na(aI) and dI > cI and cI > bI and bI > aI
 
    xa = not na(xP) ? math.abs(xP - aP) : na
    ab = not na(aP) ? math.abs(aP - bP) : na
    bc = not na(bP) ? math.abs(bP - cP) : na
    abxa = Ratio(aP - bP, xP - aP)
    bcab = Ratio(bP - cP, aP - bP)
    cdbcBull = Ratio(cP - dBull, bP - cP)
    cdbcBear = Ratio(cP - dBear, bP - cP)
    adxaBull = Ratio(aP - dBull, xP - aP)
    adxaBear = Ratio(aP - dBear, xP - aP)
    adExtremeBull = Ratio(aP - loSinceC, xP - aP)
    adExtremeBear = Ratio(aP - hiSinceC, xP - aP)
 
    xGeomBull = orderX and dConfirmBull and xP < aP and xP < bP
    xGeomBear = orderX and dConfirmBear and xP > aP and xP > bP
 
    gBull = UseGartley and ScanBull and xGeomBull and InRange(abxa, 0.58, 0.63) and InRange(bcab, 0.382, 0.886) and InRange(cdbcBull, 1.13, 1.618) and InRange(adxaBull, 0.77, 0.82) and (not ShVF or adExtremeBull <= 0.82)
    gBear = UseGartley and ScanBear and xGeomBear and InRange(abxa, 0.58, 0.63) and InRange(bcab, 0.382, 0.886) and InRange(cdbcBear, 1.13, 1.618) and InRange(adxaBear, 0.77, 0.82) and (not ShVF or adExtremeBear <= 0.82)
    bfBull = UseButterfly and ScanBull and xGeomBull and InRange(abxa, 0.77, 0.82) and InRange(bcab, 0.382, 0.886) and InRange(cdbcBull, 1.618, 2.24) and InRange(adxaBull, 1.27, 1.41) and (not ShVF or adExtremeBull <= 1.41)
    bfBear = UseButterfly and ScanBear and xGeomBear and InRange(abxa, 0.77, 0.82) and InRange(bcab, 0.382, 0.886) and InRange(cdbcBear, 1.618, 2.24) and InRange(adxaBear, 1.27, 1.41) and (not ShVF or adExtremeBear <= 1.41)
    shBull = UseShark and ScanBull and xGeomBull and InRange(abxa, 0.10, 0.90) and InRange(bcab, 1.13, 1.618) and InRange(cdbcBull, 1.618, 2.24) and InRange(adxaBull, 0.886, 1.13) and (not ShVF or adExtremeBull <= 1.13)
    shBear = UseShark and ScanBear and xGeomBear and InRange(abxa, 0.10, 0.90) and InRange(bcab, 1.13, 1.618) and InRange(cdbcBear, 1.618, 2.24) and InRange(adxaBear, 0.886, 1.13) and (not ShVF or adExtremeBear <= 1.13)
    batBull = UseBat and ScanBull and xGeomBull and InRange(abxa, 0.382, 0.50) and InRange(bcab, 0.382, 0.886) and InRange(cdbcBull, 1.618, 2.618) and InRange(adxaBull, 0.86, 0.899) and (not ShVF or adExtremeBull <= 0.899)
    batBear = UseBat and ScanBear and xGeomBear and InRange(abxa, 0.382, 0.50) and InRange(bcab, 0.382, 0.886) and InRange(cdbcBear, 1.618, 2.618) and InRange(adxaBear, 0.86, 0.899) and (not ShVF or adExtremeBear <= 0.899)
    dcBull = UseDeepCrab and ScanBull and xGeomBull and InRange(abxa, 0.860, 0.895) and InRange(bcab, 0.382, 0.886) and InRange(cdbcBull, 2.0, 3.618) and InRange(adxaBull, 1.58, 1.63) and (not ShVF or adExtremeBull <= 1.63)
    dcBear = UseDeepCrab and ScanBear and xGeomBear and InRange(abxa, 0.860, 0.895) and InRange(bcab, 0.382, 0.886) and InRange(cdbcBear, 2.0, 3.618) and InRange(adxaBear, 1.58, 1.63) and (not ShVF or adExtremeBear <= 1.63)
    cyBull = UseCypher and ScanBull and xGeomBull and InRange(abxa, 0.382, 0.618) and InRange(bcab, 1.13, 1.414) and InRange(cdbcBull, 1.272, 2.0) and InRange(adxaBull, 0.756, 0.796) and (not ShVF or adExtremeBull <= 0.796)
    cyBear = UseCypher and ScanBear and xGeomBear and InRange(abxa, 0.382, 0.618) and InRange(bcab, 1.13, 1.414) and InRange(cdbcBear, 1.272, 2.0) and InRange(adxaBear, 0.756, 0.796) and (not ShVF or adExtremeBear <= 0.796)
    asBull = UseAltShark and ScanBull and xGeomBull and InRange(abxa, 0.382, 0.618) and InRange(bcab, 1.13, 1.618) and InRange(cdbcBull, 1.618, 2.24) and InRange(adxaBull, 0.886, 1.13) and (not ShVF or adExtremeBull <= 1.13)
    asBear = UseAltShark and ScanBear and xGeomBear and InRange(abxa, 0.382, 0.618) and InRange(bcab, 1.13, 1.618) and InRange(cdbcBear, 1.618, 2.24) and InRange(adxaBear, 0.886, 1.13) and (not ShVF or adExtremeBear <= 1.13)
    altBatBull = UseAltBat and ScanBull and xGeomBull and InRange(abxa, 0.345, 0.399) and InRange(bcab, 0.382, 0.886) and InRange(cdbcBull, 2.0, 3.618) and InRange(adxaBull, 1.0, 1.2) and (not ShVF or adExtremeBull <= 1.2)
    altBatBear = UseAltBat and ScanBear and xGeomBear and InRange(abxa, 0.345, 0.399) and InRange(bcab, 0.382, 0.886) and InRange(cdbcBear, 2.0, 3.618) and InRange(adxaBear, 1.0, 1.2) and (not ShVF or adExtremeBear <= 1.2)
    nenBull = UseNenStar and ScanBull and xGeomBull and InRange(abxa, 0.382, 0.786) and InRange(bcab, 1.13, 1.272) and InRange(cdbcBull, 1.272, 2.618) and InRange(adxaBull, 1.13, 1.414) and (not ShVF or adExtremeBull <= 1.414)
    nenBear = UseNenStar and ScanBear and xGeomBear and InRange(abxa, 0.382, 0.786) and InRange(bcab, 1.13, 1.272) and InRange(cdbcBear, 1.272, 2.618) and InRange(adxaBear, 1.13, 1.414) and (not ShVF or adExtremeBear <= 1.414)
    dragonBull = UseDragon and ScanBull and xGeomBear and InRange(abxa, 0.38, 0.62) and InRange(bcab, 0.8, 1.1) and InRange(cdbcBear, 0.4, 0.8) and InRange(adxaBear, 0.2, 0.4) and (not ShVF or adExtremeBear <= 0.4)
    dragonBear = UseDragon and ScanBear and xGeomBull and InRange(abxa, 0.38, 0.62) and InRange(bcab, 0.8, 1.1) and InRange(cdbcBull, 0.4, 0.8) and InRange(adxaBull, 0.2, 0.4) and (not ShVF or adExtremeBull <= 0.4)
 
    fiveAbXa = abxa
    fiveBcAb = bcab
    fiveCdBcBull = cdbcBull
    fiveCdBcBear = cdbcBear
    fiveExtremeBull = Ratio(cP - loSinceC, bP - cP)
    fiveExtremeBear = Ratio(cP - hiSinceC, bP - cP)
    fiveGeomBull = orderO and dConfirmBull and xP < oP and xP < aP
    fiveGeomBear = orderO and dConfirmBear and xP > oP and xP > aP
    fiveBull = UseFiveZero and ScanBull and fiveGeomBull and InRange(fiveAbXa, 1.13, 1.618) and InRange(fiveBcAb, 1.618, 2.24) and InRange(fiveCdBcBull, 0.45, 0.55) and (not ShVF or fiveExtremeBull <= 0.55)
    fiveBear = UseFiveZero and ScanBear and fiveGeomBear and InRange(fiveAbXa, 1.13, 1.618) and InRange(fiveBcAb, 1.618, 2.24) and InRange(fiveCdBcBear, 0.45, 0.55) and (not ShVF or fiveExtremeBear <= 0.55)
 
    abcdBcAb = bcab
    abcdCdBcBull = cdbcBull
    abcdCdBcBear = cdbcBear
    cdBull = math.abs(cP - dBull)
    cdBear = math.abs(cP - dBear)
    abcdBull = UseABCD and ScanBull and orderA and dConfirmBull and aP > bP and InRange(abcdBcAb, 0.382, 0.886) and InRange(abcdCdBcBull, 1.13, 2.618) and cdBull >= ab * 0.975 and cdBull <= ab * 1.025 and (not ShVF or Ratio(cP - loSinceC, bP - cP) <= 2.618)
    abcdBear = UseABCD and ScanBear and orderA and dConfirmBear and aP < bP and InRange(abcdBcAb, 0.382, 0.886) and InRange(abcdCdBcBear, 1.13, 2.618) and cdBear >= ab * 0.975 and cdBear <= ab * 1.025 and (not ShVF or Ratio(cP - hiSinceC, bP - cP) <= 2.618)
 
    drive21 = abxa
    drive32Bull = Ratio(dBull - cP, cP - bP)
    drive32Bear = Ratio(dBear - cP, cP - bP)
    driveGeomBull = orderO and dConfirmBull and xP < oP and oP > aP and cP < aP and (not ShVF or (oP - xP) >= (aP - bP) * 1.33)
    driveGeomBear = orderO and dConfirmBear and xP > oP and oP < aP and cP > aP and (not ShVF or (xP - oP) >= (bP - aP) * 1.33)
    driveBull = UseThreeDrive and ScanBull and driveGeomBull and InRange(drive21, 1.27, 1.618) and InRange(drive32Bull, 1.27, 1.618) and (not ShVF or Ratio(loSinceC - cP, cP - bP) <= 1.618)
    driveBear = UseThreeDrive and ScanBear and driveGeomBear and InRange(drive21, 1.27, 1.618) and InRange(drive32Bear, 1.27, 1.618) and (not ShVF or Ratio(hiSinceC - cP, cP - bP) <= 1.618)
 
    eGB = NewEvent(eventKeys, 0, gBull and barstate.isconfirmed, xI)
    eGS = NewEvent(eventKeys, 1, gBear and barstate.isconfirmed, xI)
    eBFB = NewEvent(eventKeys, 2, bfBull and barstate.isconfirmed, xI)
    eBFS = NewEvent(eventKeys, 3, bfBear and barstate.isconfirmed, xI)
    eSHB = NewEvent(eventKeys, 4, shBull and barstate.isconfirmed, xI)
    eSHS = NewEvent(eventKeys, 5, shBear and barstate.isconfirmed, xI)
    eBATB = NewEvent(eventKeys, 6, batBull and barstate.isconfirmed, xI)
    eBATS = NewEvent(eventKeys, 7, batBear and barstate.isconfirmed, xI)
    eDCB = NewEvent(eventKeys, 8, dcBull and barstate.isconfirmed, xI)
    eDCS = NewEvent(eventKeys, 9, dcBear and barstate.isconfirmed, xI)
    eCYB = NewEvent(eventKeys, 10, cyBull and barstate.isconfirmed, xI)
    eCYS = NewEvent(eventKeys, 11, cyBear and barstate.isconfirmed, xI)
    eASB = NewEvent(eventKeys, 12, asBull and barstate.isconfirmed, xI)
    eASS = NewEvent(eventKeys, 13, asBear and barstate.isconfirmed, xI)
    e50B = NewEvent(eventKeys, 14, fiveBull and barstate.isconfirmed, oI)
    e50S = NewEvent(eventKeys, 15, fiveBear and barstate.isconfirmed, oI)
    eAB = NewEvent(eventKeys, 16, abcdBull and barstate.isconfirmed, aI)
    eAS = NewEvent(eventKeys, 17, abcdBear and barstate.isconfirmed, aI)
    e3B = NewEvent(eventKeys, 18, driveBull and barstate.isconfirmed, oI)
    e3S = NewEvent(eventKeys, 19, driveBear and barstate.isconfirmed, oI)
    eALTB = NewEvent(eventKeys, 20, altBatBull and barstate.isconfirmed, xI)
    eALTS = NewEvent(eventKeys, 21, altBatBear and barstate.isconfirmed, xI)
    eNENB = NewEvent(eventKeys, 22, nenBull and barstate.isconfirmed, xI)
    eNENS = NewEvent(eventKeys, 23, nenBear and barstate.isconfirmed, xI)
    eDRB = NewEvent(eventKeys, 24, dragonBull and barstate.isconfirmed, xI)
    eDRS = NewEvent(eventKeys, 25, dragonBear and barstate.isconfirmed, xI)
 
    eventId = 0
    eventDir = 0
    overlap = 0
    if eGB
        overlap += 1
        if eventId == 0
            eventId := 1
            eventDir := 1
    if eGS
        overlap += 1
        if eventId == 0
            eventId := 1
            eventDir := -1
    if eBFB
        overlap += 1
        if eventId == 0
            eventId := 2
            eventDir := 1
    if eBFS
        overlap += 1
        if eventId == 0
            eventId := 2
            eventDir := -1
    if eBATB
        overlap += 1
        if eventId == 0
            eventId := 4
            eventDir := 1
    if eBATS
        overlap += 1
        if eventId == 0
            eventId := 4
            eventDir := -1
    if eDCB
        overlap += 1
        if eventId == 0
            eventId := 5
            eventDir := 1
    if eDCS
        overlap += 1
        if eventId == 0
            eventId := 5
            eventDir := -1
    if eCYB
        overlap += 1
        if eventId == 0
            eventId := 6
            eventDir := 1
    if eCYS
        overlap += 1
        if eventId == 0
            eventId := 6
            eventDir := -1
    if eALTB
        overlap += 1
        if eventId == 0
            eventId := 11
            eventDir := 1
    if eALTS
        overlap += 1
        if eventId == 0
            eventId := 11
            eventDir := -1
    if eNENB
        overlap += 1
        if eventId == 0
            eventId := 12
            eventDir := 1
    if eNENS
        overlap += 1
        if eventId == 0
            eventId := 12
            eventDir := -1
    if eASB
        overlap += 1
        if eventId == 0
            eventId := 7
            eventDir := 1
    if eASS
        overlap += 1
        if eventId == 0
            eventId := 7
            eventDir := -1
    if eSHB
        overlap += 1
        if eventId == 0
            eventId := 3
            eventDir := 1
    if eSHS
        overlap += 1
        if eventId == 0
            eventId := 3
            eventDir := -1
    if eDRB
        overlap += 1
        if eventId == 0
            eventId := 13
            eventDir := 1
    if eDRS
        overlap += 1
        if eventId == 0
            eventId := 13
            eventDir := -1
    if e50B
        overlap += 1
        if eventId == 0
            eventId := 8
            eventDir := 1
    if e50S
        overlap += 1
        if eventId == 0
            eventId := 8
            eventDir := -1
    if eAB
        overlap += 1
        if eventId == 0
            eventId := 9
            eventDir := 1
    if eAS
        overlap += 1
        if eventId == 0
            eventId := 9
            eventDir := -1
    if e3B
        overlap += 1
        if eventId == 0
            eventId := 10
            eventDir := 1
    if e3S
        overlap += 1
        if eventId == 0
            eventId := 10
            eventDir := -1
 
    newSignal = eventId != 0
    eventFamily = Family(eventId)
    float eventD = na
    int eventAnchor = na
    if newSignal
        if eventId == 13
            eventD := eventDir == 1 ? dBear : dBull
        else
            eventD := eventDir == 1 ? dBull : dBear
        eventAnchor := eventFamily == 1 ? xI : eventFamily == 3 ? aI : oI
        lastId := eventId
        lastDir := eventDir
        lastSignalTime := time_close
        lastD := eventD
        lastEntry := close
        lastConfirmed := false
        pendingConfirm := true
        lastOverlap := overlap
        lastAnchor := eventAnchor
 
    currentFamily = Family(lastId)
    currentAnchor = currentFamily == 1 ? xI : currentFamily == 3 ? aI : (currentFamily == 2 or currentFamily == 4) ? oI : na
    if pendingConfirm and not na(lastAnchor) and not na(currentAnchor) and currentAnchor != lastAnchor
        pendingConfirm := false
 
    candleRange = high - low
    bodyRatio = candleRange > 0 ? math.abs(close - open) / candleRange : 0.0
    if pendingConfirm
        confirmNow = lastDir == 1 ? close > high[1] and bodyRatio > 0.6 : close < low[1] and bodyRatio > 0.6
        if confirmNow
            lastConfirmed := true
            pendingConfirm := false
 
    float outOP = na
    int outOI = na
    float outXP = na
    int outXI = na
    float outAP = na
    int outAI = na
    float outBP = na
    int outBI = na
    float outCP = na
    int outCI = na
    float outDP = na
    int outDI = na
    if newSignal
        if eventFamily == 1
            outXP := xP
            outXI := xI
            outAP := aP
            outAI := aI
            outBP := bP
            outBI := bI
            outCP := cP
            outCI := cI
            outDP := eventD
            outDI := dI
        else if eventFamily == 2 or eventFamily == 4
            outOP := oP
            outOI := oI
            outXP := xP
            outXI := xI
            outAP := aP
            outAI := aI
            outBP := bP
            outBI := bI
            outCP := cP
            outCI := cI
            outDP := eventD
            outDI := dI
        else if eventFamily == 3
            outAP := aP
            outAI := aI
            outBP := bP
            outBI := bI
            outCP := cP
            outCI := cI
            outDP := eventD
            outDI := dI
 
    [newSignal, lastId, lastDir, lastSignalTime, lastD, lastEntry, lastConfirmed, lastOverlap, outOI, outOP, outXI, outXP, outAI, outAP, outBI, outBP, outCI, outCP, outDI, outDP]
 
Scan() =>
    [_, id, dir, sigTime, _, entryPrice, confirmed, overlap, _, _, _, _, _, _, _, _, _, _, _, _] = Engine()
    [id, dir, sigTime, entryPrice, confirmed, overlap]
 
[newChartSignal, chartId, chartDir, chartSignalTime, chartD, chartEntry, chartConfirmed, chartOverlap, chartOI, chartOP, chartXI, chartXP, chartAI, chartAP, chartBI, chartBP, chartCI, chartCP, chartDI, chartDP] = Engine()
 
var chartLines = array.new_line()
var chartLabels = array.new_label()
var chartPolys = array.new<polyline>()
 
AddMainLine(i1, p1, i2, p2, c) =>
    line ln = na
    if not na(i1) and not na(p1) and not na(i2) and not na(p2)
        ln := line.new(i1, p1, i2, p2, xloc = xloc.bar_index, color = c, width = LineWidth)
        array.push(chartLines, ln)
    ln
 
AddGuide(i1, p1, i2, p2, c) =>
    line ln = na
    if not na(i1) and not na(p1) and not na(i2) and not na(p2)
        ln := line.new(i1, p1, i2, p2, xloc = xloc.bar_index, color = color.new(c, 55), width = math.max(LineWidth - 1, 1), style = line.style_dotted)
        array.push(chartLines, ln)
    ln
 
AddBoxLabel(i, p, txt, c) =>
    if not na(i) and not na(p)
        lb = label.new(i, p, txt, xloc = xloc.bar_index, style = label.style_label_center, textcolor = color.white, color = c, size = LabelSize(LabelSizeInput))
        array.push(chartLabels, lb)
 
AddRatio(ln, i1, i2, ratioValue, c) =>
    if not na(ln) and not na(ratioValue)
        mid = int(math.round((i1 + i2) * 0.5))
        py = line.get_price(ln, mid)
        AddBoxLabel(mid, py, str.tostring(ratioValue, "#.###"), c)
 
AddPoly3(i1, p1, i2, p2, i3, p3, c) =>
    if not na(i1) and not na(p1) and not na(i2) and not na(p2) and not na(i3) and not na(p3)
        pl = polyline.new(array.from(chart.point.from_index(i1, p1), chart.point.from_index(i2, p2), chart.point.from_index(i3, p3)), curved = false, closed = true, xloc = xloc.bar_index, line_color = color.new(c, 100), fill_color = color.new(c, FillTransparency), line_style = line.style_solid, line_width = 1)
        array.push(chartPolys, pl)
 
AddPoly4(i1, p1, i2, p2, i3, p3, i4, p4, c) =>
    if not na(i1) and not na(p1) and not na(i2) and not na(p2) and not na(i3) and not na(p3) and not na(i4) and not na(p4)
        pl = polyline.new(array.from(chart.point.from_index(i1, p1), chart.point.from_index(i2, p2), chart.point.from_index(i3, p3), chart.point.from_index(i4, p4)), curved = false, closed = true, xloc = xloc.bar_index, line_color = color.new(c, 100), fill_color = color.new(c, FillTransparency), line_style = line.style_solid, line_width = 1)
        array.push(chartPolys, pl)
 
AddPoly5(i1, p1, i2, p2, i3, p3, i4, p4, i5, p5, c) =>
    if not na(i1) and not na(p1) and not na(i2) and not na(p2) and not na(i3) and not na(p3) and not na(i4) and not na(p4) and not na(i5) and not na(p5)
        pl = polyline.new(array.from(chart.point.from_index(i1, p1), chart.point.from_index(i2, p2), chart.point.from_index(i3, p3), chart.point.from_index(i4, p4), chart.point.from_index(i5, p5)), curved = false, closed = true, xloc = xloc.bar_index, line_color = color.new(c, 100), fill_color = color.new(c, FillTransparency), line_style = line.style_solid, line_width = 1)
        array.push(chartPolys, pl)
 
AddPoly6(i1, p1, i2, p2, i3, p3, i4, p4, i5, p5, i6, p6, c) =>
    if not na(i1) and not na(p1) and not na(i2) and not na(p2) and not na(i3) and not na(p3) and not na(i4) and not na(p4) and not na(i5) and not na(p5) and not na(i6) and not na(p6)
        pl = polyline.new(array.from(chart.point.from_index(i1, p1), chart.point.from_index(i2, p2), chart.point.from_index(i3, p3), chart.point.from_index(i4, p4), chart.point.from_index(i5, p5), chart.point.from_index(i6, p6)), curved = false, closed = true, xloc = xloc.bar_index, line_color = color.new(c, 100), fill_color = color.new(c, FillTransparency), line_style = line.style_solid, line_width = 1)
        array.push(chartPolys, pl)
 
if ShowChartPatterns and newChartSignal
    pc = chartDir == 1 ? BullColor : BearColor
    fam = Family(chartId)
    if fam == 1
        AddPoly3(chartXI, chartXP, chartAI, chartAP, chartBI, chartBP, pc)
        AddPoly3(chartBI, chartBP, chartCI, chartCP, chartDI, chartDP, pc)
        AddMainLine(chartXI, chartXP, chartAI, chartAP, pc)
        AddMainLine(chartAI, chartAP, chartBI, chartBP, pc)
        AddMainLine(chartBI, chartBP, chartCI, chartCP, pc)
        AddMainLine(chartCI, chartCP, chartDI, chartDP, pc)
        xb = AddGuide(chartXI, chartXP, chartBI, chartBP, pc)
        ac = AddGuide(chartAI, chartAP, chartCI, chartCP, pc)
        bd = AddGuide(chartBI, chartBP, chartDI, chartDP, pc)
        xd = AddGuide(chartXI, chartXP, chartDI, chartDP, pc)
        AddRatio(xb, chartXI, chartBI, Ratio(chartAP - chartBP, chartXP - chartAP), pc)
        AddRatio(ac, chartAI, chartCI, Ratio(chartBP - chartCP, chartAP - chartBP), pc)
        AddRatio(bd, chartBI, chartDI, Ratio(chartCP - chartDP, chartBP - chartCP), pc)
        AddRatio(xd, chartXI, chartDI, Ratio(chartAP - chartDP, chartXP - chartAP), pc)
        AddBoxLabel(chartXI, chartXP, "X", pc)
        AddBoxLabel(chartAI, chartAP, "A", pc)
        AddBoxLabel(chartBI, chartBP, "B", pc)
        AddBoxLabel(chartCI, chartCP, "C", pc)
        AddBoxLabel(chartDI, chartDP, "D", pc)
    else if fam == 2
        AddPoly3(chartOI, chartOP, chartXI, chartXP, chartAI, chartAP, pc)
        AddPoly3(chartBI, chartBP, chartCI, chartCP, chartDI, chartDP, pc)
        AddMainLine(chartOI, chartOP, chartXI, chartXP, pc)
        AddMainLine(chartXI, chartXP, chartAI, chartAP, pc)
        AddMainLine(chartAI, chartAP, chartBI, chartBP, pc)
        AddMainLine(chartBI, chartBP, chartCI, chartCP, pc)
        AddMainLine(chartCI, chartCP, chartDI, chartDP, pc)
        xb = AddGuide(chartXI, chartXP, chartBI, chartBP, pc)
        ac = AddGuide(chartAI, chartAP, chartCI, chartCP, pc)
        bd = AddGuide(chartBI, chartBP, chartDI, chartDP, pc)
        AddRatio(xb, chartXI, chartBI, Ratio(chartAP - chartBP, chartXP - chartAP), pc)
        AddRatio(ac, chartAI, chartCI, Ratio(chartBP - chartCP, chartAP - chartBP), pc)
        AddRatio(bd, chartBI, chartDI, Ratio(chartCP - chartDP, chartBP - chartCP), pc)
        AddBoxLabel(chartOI, chartOP, "0", pc)
        AddBoxLabel(chartXI, chartXP, "X", pc)
        AddBoxLabel(chartAI, chartAP, "A", pc)
        AddBoxLabel(chartBI, chartBP, "B", pc)
        AddBoxLabel(chartCI, chartCP, "C", pc)
        AddBoxLabel(chartDI, chartDP, "D", pc)
    else if fam == 3
        AddMainLine(chartAI, chartAP, chartBI, chartBP, pc)
        AddMainLine(chartBI, chartBP, chartCI, chartCP, pc)
        AddMainLine(chartCI, chartCP, chartDI, chartDP, pc)
        ac = AddGuide(chartAI, chartAP, chartCI, chartCP, pc)
        bd = AddGuide(chartBI, chartBP, chartDI, chartDP, pc)
        AddRatio(ac, chartAI, chartCI, Ratio(chartBP - chartCP, chartAP - chartBP), pc)
        AddRatio(bd, chartBI, chartDI, Ratio(chartCP - chartDP, chartBP - chartCP), pc)
        AddBoxLabel(chartAI, chartAP, "A", pc)
        AddBoxLabel(chartBI, chartBP, "B", pc)
        AddBoxLabel(chartCI, chartCP, "C", pc)
        AddBoxLabel(chartDI, chartDP, "D", pc)
    else if fam == 4
        AddPoly3(chartXI, chartXP, chartAI, chartAP, chartBI, chartBP, pc)
        AddPoly3(chartBI, chartBP, chartCI, chartCP, chartDI, chartDP, pc)
        AddMainLine(chartOI, chartOP, chartXI, chartXP, pc)
        AddMainLine(chartXI, chartXP, chartAI, chartAP, pc)
        AddMainLine(chartAI, chartAP, chartBI, chartBP, pc)
        AddMainLine(chartBI, chartBP, chartCI, chartCP, pc)
        AddMainLine(chartCI, chartCP, chartDI, chartDP, pc)
        xb = AddGuide(chartXI, chartXP, chartBI, chartBP, pc)
        bd = AddGuide(chartBI, chartBP, chartDI, chartDP, pc)
        AddRatio(xb, chartXI, chartBI, Ratio(chartAP - chartBP, chartXP - chartAP), pc)
        AddRatio(bd, chartBI, chartDI, Ratio(chartDP - chartCP, chartCP - chartBP), pc)
        AddBoxLabel(chartXI, chartXP, "1", pc)
        AddBoxLabel(chartBI, chartBP, "2", pc)
        AddBoxLabel(chartDI, chartDP, "3", pc)
    while array.size(chartPolys) > 200
        polyline.delete(array.shift(chartPolys))
    while array.size(chartLines) > 200 * 9
        line.delete(array.shift(chartLines))
    while array.size(chartLabels) > 200 * 14
        label.delete(array.shift(chartLabels))
 
confirmPulse = chartConfirmed and not chartConfirmed[1]
plotshape(confirmPulse and chartDir == 1, title = "Bullish Harmonic Confirmation", style = shape.triangleup, location = location.belowbar, color = BullColor, size = size.tiny, text = "H")
plotshape(confirmPulse and chartDir == -1, title = "Bearish Harmonic Confirmation", style = shape.triangledown, location = location.abovebar, color = BearColor, size = size.tiny, text = "H")
if ChartAlerts and barstate.isrealtime and newChartSignal
    alert("Harmonic | " + syminfo.tickerid + " | " + PatternName(chartId) + " | " + (chartDir == 1 ? "Bullish" : "Bearish") + " | TF " + timeframe.period, alert.freq_all)
 
invalidTF01 = timeframe.in_seconds(TF01) < timeframe.in_seconds(timeframe.period)
invalidTF02 = timeframe.in_seconds(TF02) < timeframe.in_seconds(timeframe.period)
invalidTF03 = timeframe.in_seconds(TF03) < timeframe.in_seconds(timeframe.period)
invalidTF04 = timeframe.in_seconds(TF04) < timeframe.in_seconds(timeframe.period)
invalidTF05 = timeframe.in_seconds(TF05) < timeframe.in_seconds(timeframe.period)
invalidTF06 = timeframe.in_seconds(TF06) < timeframe.in_seconds(timeframe.period)
invalidTF07 = timeframe.in_seconds(TF07) < timeframe.in_seconds(timeframe.period)
invalidTF08 = timeframe.in_seconds(TF08) < timeframe.in_seconds(timeframe.period)
invalidTF09 = timeframe.in_seconds(TF09) < timeframe.in_seconds(timeframe.period)
invalidTF10 = timeframe.in_seconds(TF10) < timeframe.in_seconds(timeframe.period)
invalidScreenerTF = invalidTF01 or invalidTF02 or invalidTF03 or invalidTF04 or invalidTF05 or invalidTF06 or invalidTF07 or invalidTF08 or invalidTF09 or invalidTF10
inVisibleRange = time >= chart.left_visible_bar_time and time <= chart.right_visible_bar_time
var float visibleHigh = na
var float visibleLow = na
if time == chart.left_visible_bar_time
    visibleHigh := high
    visibleLow := low
else if inVisibleRange
    visibleHigh := na(visibleHigh) ? high : math.max(visibleHigh, high)
    visibleLow := na(visibleLow) ? low : math.min(visibleLow, low)
var label tfWarningLabel = na
if barstate.islast
    if invalidScreenerTF
        warningX = int(math.round((chart.left_visible_bar_time + chart.right_visible_bar_time) * 0.5))
        warningY =  na(visibleHigh) or na(visibleLow) ? close : math.avg(visibleHigh, visibleLow)
        warningText = "Each screener timeframe must be equal to or higher than the chart timeframe."
        if na(tfWarningLabel)
            tfWarningLabel := label.new(warningX, warningY, warningText, xloc = xloc.bar_time, style = label.style_label_center, textcolor = color.white, color = #B33A3A, size = size.normal)
        else
            label.set_xy(tfWarningLabel, warningX, warningY)
            label.set_text(tfWarningLabel, warningText)
    else if not na(tfWarningLabel)
        label.delete(tfWarningLabel)
        tfWarningLabel := na
 
var symbols = array.new_string()
var scanTFs = array.new_string()
var scanTFValid = array.new_bool()
if barstate.isfirst
    array.push(symbols, S01)
    array.push(scanTFs, TF01)
    array.push(scanTFValid, not invalidTF01)
    array.push(symbols, S02)
    array.push(scanTFs, TF02)
    array.push(scanTFValid, not invalidTF02)
    array.push(symbols, S03)
    array.push(scanTFs, TF03)
    array.push(scanTFValid, not invalidTF03)
    array.push(symbols, S04)
    array.push(scanTFs, TF04)
    array.push(scanTFValid, not invalidTF04)
    array.push(symbols, S05)
    array.push(scanTFs, TF05)
    array.push(scanTFValid, not invalidTF05)
    array.push(symbols, S06)
    array.push(scanTFs, TF06)
    array.push(scanTFValid, not invalidTF06)
    array.push(symbols, S07)
    array.push(scanTFs, TF07)
    array.push(scanTFValid, not invalidTF07)
    array.push(symbols, S08)
    array.push(scanTFs, TF08)
    array.push(scanTFValid, not invalidTF08)
    array.push(symbols, S09)
    array.push(scanTFs, TF09)
    array.push(scanTFValid, not invalidTF09)
    array.push(symbols, S10)
    array.push(scanTFs, TF10)
    array.push(scanTFValid, not invalidTF10)
 
var scanIds = array.new_int()
var scanDirs = array.new_int()
var scanTimes = array.new_int()
var scanDPrices = array.new_float()
var scanConfirms = array.new_bool()
var scanOverlaps = array.new_int()
var scanDaily = array.new_float()
var prevTimes = array.new_int(10, na)
array.clear(scanIds)
array.clear(scanDirs)
array.clear(scanTimes)
array.clear(scanDPrices)
array.clear(scanConfirms)
array.clear(scanOverlaps)
array.clear(scanDaily)
 
symbolCount = math.min(array.size(symbols), 10)
if symbolCount > 0
    for i = 0 to symbolCount - 1
        sym = array.get(symbols, i)
        scanTF = array.get(scanTFs, i)
        tfValid = array.get(scanTFValid, i)
        sid = 0
        sdir = 0
        int stime = na
        float sd = na
        scon = false
        sover = 0
        if tfValid
            [reqId, reqDir, reqTime, reqEntry, reqCon, reqOver] = request.security(sym, scanTF, Scan(), gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_off, ignore_invalid_symbol = true, calc_bars_count = ScanHistory)
            sid := reqId
            sdir := reqDir
            stime := reqTime
            sd := reqEntry
            scon := reqCon
            sover := reqOver
        [dClose, dPrevClose] = request.security(sym, "D", [close, close[1]], gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_off, ignore_invalid_symbol = true, calc_bars_count = 10)
        dPct =  na(dClose) or na(dPrevClose) or dPrevClose == 0.0 ? na : ((dClose - dPrevClose) / dPrevClose) * 100.0
        array.push(scanIds, sid)
        array.push(scanDirs, sdir)
        array.push(scanTimes, stime)
        array.push(scanDPrices, sd)
        array.push(scanConfirms, scon)
        array.push(scanOverlaps, sover)
        array.push(scanDaily, dPct)
        oldTime = array.get(prevTimes, i)
        if tfValid and ScreenerAlerts and barstate.isrealtime and not na(oldTime) and not na(stime) and stime != oldTime
            msg = "Harmonic | " + sym + " | " + PatternName(sid) + " | " + (sdir == 1 ? "Bullish" : "Bearish") + " | TF " + scanTF
            alert(msg, alert.freq_all)
        array.set(prevTimes, i, stime)
 
var dash = table.new(TablePosition, 9, 12, border_width = 1, frame_color = color.new(#2A4177, 0), border_color = color.new(#2A4177, 0))
if barstate.isfirst
    table.merge_cells(dash, 0, 0, 8, 0)
if barstate.islast
    ageReferenceTime = barstate.isrealtime ? timenow : (na(time_close) ? time : time_close)
    titleBg = #081C4C
    headBg = #0C2560
    cellBg = #0A1F56
    cellBg2 = #0A225D
    titleColor = #51D9F6
    ageColor = #FF9D2E
    table.cell(dash, 0, 0, "♦ Harmonic Patterns All-in-One Scanner ♦", text_color = titleColor, bgcolor = titleBg, text_size = TableSize)
    h0 = "Num"
    h1 = "Symbol"
    h2 = "Exchange"
    h3 = "Daily(%)"
    h4 = "TimeFrame"
    h5 = "Signal"
    h6 = "Pattern"
    h7 = "Entry Price"
    h8 = "Signal Age"
    heads = array.from(h0, h1, h2, h3, h4, h5, h6, h7, h8)
    for c = 0 to 8
        table.cell(dash, c, 1, array.get(heads, c), text_color = color.white, bgcolor = headBg, text_size = TableSize)
    for row = 0 to 9
        rbg = row % 2 == 0 ? cellBg : cellBg2
        if row < symbolCount
            sym = array.get(symbols, row)
            sid = array.get(scanIds, row)
            sdir = array.get(scanDirs, row)
            stime = array.get(scanTimes, row)
            sd = array.get(scanDPrices, row)
            dPct = array.get(scanDaily, row)
            ptxt = sid == 0 ? "—" : PatternShort(sid) + (array.get(scanOverlaps, row) > 1 ? " +" + str.tostring(array.get(scanOverlaps, row) - 1) : "")
            sigTxt = sdir == 1 ? "▲" : sdir == -1 ? "▼" : "—"
            sigColor = sdir == 1 ? #00D26A : sdir == -1 ? #FF3131 : color.white
            pctColor =  na(dPct) ? color.white : dPct >= 0 ? #00C8A0 : #FF4D6D
            table.cell(dash, 0, row + 2, str.tostring(row + 1), text_color = color.white, bgcolor = rbg, text_size = TableSize)
            table.cell(dash, 1, row + 2, Ticker(sym), text_color = color.white, bgcolor = rbg, text_size = TableSize)
            table.cell(dash, 2, row + 2, Exchange(sym), text_color = color.white, bgcolor = rbg, text_size = TableSize)
            table.cell(dash, 3, row + 2, na(dPct) ? "—" : str.tostring(dPct, "#.##") + "%", text_color = pctColor, bgcolor = rbg, text_size = TableSize)
            rowTF = array.get(scanTFs, row)
            table.cell(dash, 4, row + 2, TFText(rowTF), text_color = color.white, bgcolor = rbg, text_size = TableSize)
            table.cell(dash, 5, row + 2, sigTxt, text_color = sigColor, bgcolor = rbg, text_size = TableSize)
            table.cell(dash, 6, row + 2, ptxt, text_color = color.white, bgcolor = rbg, text_size = TableSize)
            table.cell(dash, 7, row + 2, na(sd) ? "—" : str.tostring(sd, "#.#####"), text_color = color.white, bgcolor = rbg, text_size = TableSize)
            table.cell(dash, 8, row + 2, Elapsed(stime, ageReferenceTime), text_color = ageColor, bgcolor = rbg, text_size = TableSize)
        else
            for c = 0 to 8
                table.cell(dash, c, row + 2, "", bgcolor = rbg, text_size = TableSize)

← 返回列表