//@version=5
indicator("Group with Gann Fan", overlay = true)

// { ==== BB ====
length_BB = input.int(20, minval=1, title="BB Length")
src_BB = input(close, title="Source")
mult_BB = input.float(2.0, minval=0.001, maxval=50, title="StdDev Multiplier")
basis = ta.ema(src_BB, length_BB)
dev = mult_BB * ta.stdev(src_BB, length_BB)
upper = basis + dev
lower = basis - dev
plot(basis, "Basis", color=color.blue)
p1 = plot(upper, "Upper", color=color.red)
p2 = plot(lower, "Lower", color=color.green)

// Long Condition BB
long_con1_bb = (low <= lower) and (open > lower and open < basis) and (close > lower and close < basis) and (high < basis)
number_candle_long_con1_bb = ta.barssince(long_con1_bb)
long_con2_bb = number_candle_long_con1_bb > 0 and number_candle_long_con1_bb <= 6 and (close > lower and open > lower) and close > high[number_candle_long_con1_bb]

// Short Condition BB
short_con1_bb = (high >= upper) and (open < upper and open > basis) and (close < upper and close > basis) and (low > basis)
number_candle_short_con1_bb = ta.barssince(short_con1_bb)
short_con2_bb = number_candle_short_con1_bb > 0 and number_candle_short_con1_bb <= 6 and (close < upper and open < upper) and close < low[number_candle_short_con1_bb]
// }

// { === Candle Pattern Identification ===
pinBarBodyRatio = 0.5
pinBarTailRatio = 1.3
bodyLength = math.abs(open - close)
upperShadow = high - math.max(open, close)
lowerShadow = math.min(open, close) - low
isBullishPinBar = (lowerShadow > bodyLength * pinBarTailRatio) and (bodyLength < (high - low) * pinBarBodyRatio) and (upperShadow < bodyLength)
con_long_pinbar_bb = isBullishPinBar and long_con1_bb
isBearishPinBar = (upperShadow > bodyLength * pinBarTailRatio) and (bodyLength < (high - low) * pinBarBodyRatio) and (lowerShadow < bodyLength)
con_short_pinbar_bb = isBearishPinBar and short_con1_bb

isBullishEngulfing = (close[1] < open[1]) and (close > open) and (close >= open[1]) and (open <= close[1])
plotshape(series=isBullishEngulfing, location=location.belowbar, color=color.green, style=shape.triangleup, text="Bull Engulf", size=size.small)

isBearishEngulfing = (close[1] > open[1]) and (close < open) and (open >= close[1]) and (close <= open[1])
plotshape(series=isBearishEngulfing, location=location.abovebar, color=color.red, style=shape.triangledown, text="Bear Engulf", size=size.small)
// }

// { ==== Gann Fan ====
pivotHigh = ta.pivothigh(high, 5, 5)
pivotLow = ta.pivotlow(low, 5, 5)

// Function to draw Gann Fan lines
drawGannFan(pivot, color) =>
    fan_lines = array.new_line(0)
    angles = array.new_float(7)
    array.set(angles, 0, 1) // Offset multiplier for lines, can be adjusted according to scale
    array.set(angles, 1, 2)
    array.set(angles, 2, 3)
    array.set(angles, 3, 4)
    array.set(angles, 4, 8)
    array.set(angles, 5, 1/4)
    array.set(angles, 6, 1/8)
    
    for i = 0 to array.size(angles) - 1
        offset = array.get(angles, i)
        line.new(pivot.time, pivot.value, bar_index + 100, pivot.value + (offset * (100)), color=color, width=1)

if (not na(pivotHigh))
    drawGannFan(pivotHigh, color=color.green)
    
if (not na(pivotLow))
    drawGannFan(pivotLow, color=color.red)
// }این کد رو برام تصحیح کن