Andi
2025-01-16
Category: None

//@version=5
indicator("Advanced Harmonic Patterns with Filters", overlay=true)

// Inputs
sensitivity = input.float(2, "Sensitivity", minval=0.5, maxval=10, step=0.1)
atrPeriod = input.int(10, "ATR Period", minval=1)
keltnerLength = input.int(10, "Keltner Length", minval=1)
factor = input.float(3.5, "Keltner Factor", minval=1)
showHarmonics = input.bool(true, "Show Harmonic Patterns")
showSupportResistance = input.bool(true, "Show Support/Resistance")
showSignals = input.bool(true, "Show Buy/Sell Signals")
useRSIFilter = input.bool(true, "Use RSI Filter")
rsiLength = input.int(14, "RSI Length", minval=1)
rsiOverbought = input.int(70, "RSI Overbought Level", minval=50, maxval=100)
rsiOversold = input.int(30, "RSI Oversold Level", minval=0, maxval=50)
useVolumeFilter = input.bool(true, "Use Volume Filter")
volumeThreshold = input.float(1.5, "Volume Threshold", minval=1, step=0.1)

// Supertrend Calculation
  = ta.supertrend(factor, atrPeriod)

// Plot Supertrend
plot(supertrend, color=direction == 1 ? color.green : color.red, linewidth=2)

// ZigZag Highs and Lows
var float  zigzagHighs = array.new_float(0)
var float  zigzagLows = array.new_float(0)
var int  zigzagHighBars = array.new_int(0)
var int  zigzagLowBars = array.new_int(0)

if ta.pivothigh(high, 2, 2)
    array.push(zigzagHighs, high )
    array.push(zigzagHighBars, bar_index )

if ta.pivotlow(low, 2, 2)
    array.push(zigzagLows, low )
    array.push(zigzagLowBars, bar_index )

// Plot ZigZag Highs and Lows
if showHarmonics
    plotshape(array.size(zigzagHighs) > 0 ? array.get(zigzagHighs, array.size(zigzagHighs) - 1) : na, style=shape.labeldown, location=location.abovebar, color=color.red, text="High")
    plotshape(array.size(zigzagLows) > 0 ? array.get(zigzagLows, array.size(zigzagLows) - 1) : na, style=shape.labelup, location=location.belowbar, color=color.green, text="Low")

// Support and Resistance Levels
var float  supportLevels = array.new_float(0)
var float  resistanceLevels = array.new_float(0)

if array.size(zigzagLows) >= 2
    supportLevel := array.get(zigzagLows, array.size(zigzagLows) - 1)
    array.push(supportLevels, supportLevel)

if array.size(zigzagHighs) >= 2
    resistanceLevel := array.get(zigzagHighs, array.size(zigzagHighs) - 1)
    array.push(resistanceLevels, resistanceLevel)

// Plot Support and Resistance Levels
if showSupportResistance
    for i = 0 to array.size(supportLevels) - 1
        line.new(bar_index - 10, array.get(supportLevels, i), bar_index + 10, array.get(supportLevels, i), color=color.green, width=1, style=line.style_dashed)

    for i = 0 to array.size(resistanceLevels) - 1
        line.new(bar_index - 10, array.get(resistanceLevels, i), bar_index + 10, array.get(resistanceLevels, i), color=color.red, width=1, style=line.style_dashed)

// RSI Filter
rsi = ta.rsi(close, rsiLength)
rsiFilter = not useRSIFilter or (rsi < rsiOverbought and rsi > rsiOversold)

// Volume Filter
volumeFilter = not useVolumeFilter or (volume > volumeThreshold * ta.sma(volume, 20))

// Buy/Sell Signals based on Supertrend and Filters
buySignal = ta.crossover(close, supertrend) and direction == 1 and rsiFilter and volumeFilter
sellSignal = ta.crossunder(close, supertrend) and direction == -1 and rsiFilter and volumeFilter

if showSignals
    plotshape(buySignal, style=shape.labelup, location=location.belowbar, color=color.green, text="BUY")
    plotshape(sellSignal, style=shape.labeldown, location=location.abovebar, color=color.red, text="SELL")

// Alerts
alertcondition(buySignal, title="Buy Signal", message="Buy Signal Detected")
alertcondition(sellSignal, title="Sell Signal", message="Sell Signal Detected")

// Advanced Harmonic Patterns (Gartley, Butterfly, Crab, Bat)
var bool gartleyPattern = false
var bool butterflyPattern = false
var bool crabPattern = false
var bool batPattern = false

if array.size(zigzagHighs) >= 4 and array.size(zigzagLows) >= 4
    X = array.get(zigzagHighs, 3)
    A = array.get(zigzagLows, 2)
    B = array.get(zigzagHighs, 1)
    C = array.get(zigzagLows, 0)
    D = close

    AB = math.abs(B - A)
    BC = math.abs(C - B)
    XA = math.abs(A - X)
    AD = math.abs(D - A)

    // Gartley Pattern
    gartleyPattern := AB / XA >= 0.618 and AB / XA <= 0.786 and BC / AB >= 0.382 and BC / AB <= 0.886 and AD / XA >= 0.786 and AD / XA <= 0.886

    // Butterfly Pattern
    butterflyPattern := AB / XA >= 0.786 and AB / XA <= 0.886 and BC / AB >= 0.382 and BC / AB <= 0.886 and AD / XA >= 1.272 and AD / XA <= 1.618

    // Crab Pattern
    crabPattern := AB / XA >= 0.382 and AB / XA <= 0.618 and BC / AB >= 0.382 and BC / AB <= 0.886 and AD / XA >= 1.618 and AD / XA <= 2.24

    // Bat Pattern
    batPattern := AB / XA >= 0.382 and AB / XA <= 0.5 and BC / AB >= 0.382 and BC / AB <= 0.886 and AD / XA >= 0.886 and AD / XA <= 1.13

// Plot Harmonic Patterns
if showHarmonics
    plotshape(gartleyPattern, style=shape.labeldown, location=location.abovebar, color=color.blue, text="Gartley")
    plotshape(butterflyPattern, style=shape.labeldown, location=location.abovebar, color=color.orange, text="Butterfly")
    plotshape(crabPattern, style=shape.labeldown, location=location.abovebar, color=color.purple, text="Crab")
    plotshape(batPattern, style=shape.labeldown, location=location.abovebar, color=color.teal, text="Bat")