donchain and volume

PHOTO EMBED

Mon Nov 18 2024 17:38:48 GMT+0000 (Coordinated Universal Time)

Saved by @pk20

//@version=5
indicator("LMR with Flat Candles and Rising/Falling Band Logic", overlay=true)

// --- Input for Donchian Channel Period ---
donchianPeriod = input.int(20, title="Donchian Channel Period", minval=1)
flatCandleCount = input.int(3, title="Number of Flat Candles", minval=1)  // Number of candles to check for flatness

// --- Donchian Channel Calculations ---
donchianHigh = ta.highest(high, donchianPeriod)
donchianLow = ta.lowest(low, donchianPeriod)
donchianMid = (donchianHigh + donchianLow) / 2

// --- Plot Donchian Channel ---
plot(donchianHigh, color=color.new(color.blue, 0), title="Donchian High")
plot(donchianLow, color=color.new(color.red, 0), title="Donchian Low")
plot(donchianMid, color=color.new(color.gray, 0), title="Donchian Midline", linewidth=1)

// --- Flat Candle Detection (Checking multiple candles for flatness) ---
isFlatCandle(candleIndex) =>
    math.abs(close[candleIndex] - close[candleIndex-1]) < (high[candleIndex] - low[candleIndex]) * 0.2  // Price change is small compared to range

flatCandles = true
for i = 1 to flatCandleCount
    flatCandles := flatCandles and isFlatCandle(i)  // All previous candles must be flat

// --- Current Band Movement Detection ---
isUpperBandRising = donchianHigh > donchianHigh[1]  // Upper band is rising in the current candle
isUpperBandFlat = donchianHigh == donchianHigh[1]  // Upper band is flat in the current candle
isLowerBandFalling = donchianLow < donchianLow[1]  // Lower band is falling in the current candle

// --- Debugging: Plot the band changes to visualize them ---
plotshape(isUpperBandRising, title="Upper Band Rising", location=location.abovebar, color=color.green, style=shape.triangledown, size=size.small)
plotshape(isUpperBandFlat, title="Upper Band Flat", location=location.abovebar, color=color.blue, style=shape.triangledown, size=size.small)
plotshape(isLowerBandFalling, title="Lower Band Falling", location=location.belowbar, color=color.red, style=shape.triangleup, size=size.small)

// --- Long Condition (Flat candles + Rising Upper Band) ---
longCondition = flatCandles and isUpperBandRising  // Flat candles and upper band rising in current candle

// --- Short Condition (Flat candles + Falling Lower Band + Upper Band Flat) ---
shortCondition = flatCandles and isUpperBandFlat and isLowerBandFalling  // Flat candles and upper band flat + lower band falling

// --- Plot Background Colors for Signals ---
bgcolor(longCondition ? color.new(color.green, 90) : na, title="Long Condition Signal")
bgcolor(shortCondition ? color.new(color.red, 90) : na, title="Short Condition Signal")

// --- Plot Arrows for Signals ---
plotshape(longCondition, title="Long Signal Arrow", location=location.belowbar, color=color.green, style=shape.arrowup, size=size.small)
plotshape(shortCondition, title="Short Signal Arrow", location=location.abovebar, color=color.red, style=shape.arrowdown, size=size.small)

// --- Alerts ---
alertcondition(longCondition, title="Long Signal Alert", message="Flat candles detected, and upper band is rising.")
alertcondition(shortCondition, title="Short Signal Alert", message="Flat candles detected, and upper band is flat and lower band is falling.")

content_copyCOPY