Create Sell Signal When Pattern made after RSI Touch or Cross 65: //@version=5
indicator("Flexible Candlestick Patterns V5 with Shortcodes", overlay=true)

// Calculate body size
bodySize = math.abs(close - open)

// Calculate wick sizes using max function (available from v4 onwards)
upperWick = if open > close
    high - open
else
    high - close

lowerWick = if open > close
    close - low
else
    open - low

// Variables
var int numberOfCandles = input(5, "Number of Candles") // Input for the number of last candles to consider
var float maxVolume = na

// Function to find maximum volume within the specified number of last candles
if bar_index >= numberOfCandles - 1
    maxVolume := volume
    for i = 1 to numberOfCandles - 1
        maxVolume := math.max(maxVolume, volume[i])

// Input parameters for flexibility
shootingStarBodyRatio = input.float(0.35, title="Shooting Star Body Ratio")
shootingStarWickMultiplier = input.float(3, title="Shooting Star Wick Multiplier")
hammerUpperWickRatio = input.float(0.25, title="Hammer Upper Wick Ratio")
hammerBodyRatio = input.float(0.35, title="Hammer Body Ratio")
dojiBodyRatio = input.float(0.05, title="Doji Body Ratio")
dragonflyDojiWickRatio = input.float(0.05, title="Dragonfly Doji Wick Ratio")
gravestoneDojiWickRatio = input.float(0.05, title="Gravestone Doji Wick Ratio")
hangingManBodyRatio = input.float(0.35, title="Hanging Man Body Ratio")
spinningTopBodyRatio = input.float(0.3, title="Spinning Top Body Ratio")
insidePatternBodyRatio = input.float(0.4, title="Inside Pattern Body Ratio")

// Checkboxes to enable/disable each pattern
showShootingStar = input(true, title="Show Shooting Star")
showHammer = input(true, title="Show Hammer")
showInvertedHammer = input(true, title="Show Inverted Hammer")
showDoji = input(true, title="Show Doji")
showDragonflyDoji = input(true, title="Show Dragonfly Doji")
showGravestoneDoji = input(true, title="Show Gravestone Doji")
showHangingMan = input(true, title="Show Hanging Man")
showSpinningTop = input(true, title="Show Spinning Top")
showInsidePattern = input(true, title="Show Inside Pattern")

// Define Candlestick Patterns with refined conditions
isShootingStar = showShootingStar and bodySize < (high - low) * shootingStarBodyRatio and upperWick > lowerWick * shootingStarWickMultiplier and volume == maxVolume
isHammer = showHammer and close > open and (high - close) < (open - low) * hammerUpperWickRatio and bodySize < (high - low) * hammerBodyRatio and volume == maxVolume
isInvertedHammer = showInvertedHammer and open > close and (high - open) < (close - low) * hammerUpperWickRatio and bodySize < (high - low) * hammerBodyRatio and volume == maxVolume
isDoji = showDoji and bodySize <= (high - low) * dojiBodyRatio and math.abs(open - close) <= 0.001 and volume == maxVolume
isDragonflyDoji = showDragonflyDoji and isDoji and close > open and (high - close) < (open - low) * dragonflyDojiWickRatio and volume == maxVolume
isGravestoneDoji = showGravestoneDoji and isDoji and open > close and (high - open) < (close - low) * gravestoneDojiWickRatio and volume == maxVolume
isHangingMan = showHangingMan and isHammer and close < open and bodySize < (high - low) * hangingManBodyRatio and volume == maxVolume
isSpinningTop = showSpinningTop and bodySize < (high - low) * spinningTopBodyRatio and upperWick < (high - low) * spinningTopBodyRatio and lowerWick < (high - low) * spinningTopBodyRatio and volume == maxVolume
isInsidePattern = showInsidePattern and high <= high[1] and low >= low[1] and bodySize < (high - low) * insidePatternBodyRatio

// Plot the indicator only if it's one of the specified patterns with higher volume
plotshape(isHammer, location=location.belowbar, style=shape.labeldown, color=color.red, size=size.small, text="H")
plotshape(isShootingStar, location=location.abovebar, style=shape.labeldown, color=color.red, size=size.small, text="SS")
plotshape(isInvertedHammer, location=location.belowbar, style=shape.labeldown, color=color.red, size=size.small, text="IH")
plotshape(isDoji, location=location.abovebar, style=shape.labeldown, color=color.red, size=size.small, text="D")
plotshape(isDragonflyDoji, location=location.belowbar, style=shape.labeldown, color=color.red, size=size.small, text="DD")
plotshape(isGravestoneDoji, location=location.abovebar, style=shape.labeldown, color=color.red, size=size.small, text="GD")
plotshape(isHangingMan, location=location.belowbar, style=shape.labeldown, color=color.red, size=size.small, text="HM")
plotshape(isSpinningTop, location=location.abovebar, style=shape.labeldown, color=color.red, size=size.small, text="ST")
plotshape(isInsidePattern, location=location.abovebar, style=shape.labeldown, color=color.red, text="")

// Alerts
alertcondition((isInsidePattern or isHammer or isShootingStar or isInvertedHammer or isDoji or isDragonflyDoji or isGravestoneDoji or isHangingMan or isSpinningTop) and volume > maxVolume, title='Candlestick Pattern Detected with High Volume', message='A candlestick pattern with high volume has been detected!')