Rewrite Entire Code and Fix: Mismatched input 'end of line without line continuation' expecting ')': //@version=5
indicator("Flexible Candlestick Patterns V5 with Shortcodes", overlay=true)

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

// Calculate wick sizes
upperWick = open > close ? high - open : high - close
lowerWick = open > close ? close - low : 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

// -------------------------------------------------------------------
// New Logic: Show Candlestick Pattern when RSI is above 65
// -------------------------------------------------------------------

// Calculate RSI
rsiLength = input.int(14, minval=1, title="RSI Length")
rsiSource = input(close, title="RSI Source")
rsiOverbought = input.int(65, "RSI Overbought Level", minval=1, maxval=100)
rsiValue = ta.rsi(rsiSource, rsiLength)

// Define new variables for patterns when RSI is above 65
isRSI_ShootingStar = isShootingStar and rsiValue > rsiOverbought
isRSI_Hammer = isHammer and rsiValue > rsiOverbought
isRSI_InvertedHammer = isInvertedHammer and rsiValue > rsiOverbought
isRSI_Doji = isDoji and rsiValue > rsiOverbought
isRSI_DragonflyDoji = isDragonflyDoji and rsiValue > rsiOverbought
isRSI_GravestoneDoji = isGravestoneDoji and rsiValue > rsiOverbought
isRSI_HangingMan = isHangingMan and rsiValue > rsiOverbought
isRSI_SpinningTop = isSpinningTop and rsiValue > rsiOverbought
isRSI_InsidePattern = isInsidePattern and rsiValue > rsiOverbought

// Plot only the patterns that meet the RSI condition
plotshape(isRSI_Hammer, title="Hammer RSI>65", location=location.belowbar, style=shape.labeldown, color=color.green, size=size.normal, text="H")
plotshape(isRSI_ShootingStar, title="Shooting Star RSI>65", location=location.abovebar, style=shape.labeldown, color=color.green, size=size.normal, text="SS")
plotshape(isRSI_InvertedHammer, title="Inverted Hammer RSI>65", location=location.belowbar, style=shape.labeldown, color=color.green, size=size.normal, text="IH")
plotshape(isRSI_Doji, title="Doji RSI>65", location=location.abovebar, style=shape.labeldown, color=color.green, size=size.normal, text="D")
plotshape(isRSI_DragonflyDoji, title="Dragonfly Doji RSI>65", location=location.belowbar, style=shape.labeldown, color=color.green, size=size.normal, text="DD")
plotshape(isRSI_GravestoneDoji, title="Gravestone Doji RSI>65", location=location.abovebar, style=shape.labeldown, color=color.green, size=size.normal, text="GD")
plotshape(isRSI_HangingMan, title="Hanging Man RSI>65", location=location.belowbar, style=shape.labeldown, color=color.green, size=size.normal, text="HM")
plotshape(isRSI_SpinningTop, title="Spinning Top RSI>65", location=location.abovebar, style=shape.labeldown, color=color.green, size=size.normal, text="ST")
plotshape(isRSI_InsidePattern, title="Inside Pattern RSI>65", location=location.abovebar, style=shape.labeldown, color=color.green, text="IP")

// Alerts for patterns when RSI is above 65
alertcondition(
    isRSI_ShootingStar or isRSI_Hammer or isRSI_InvertedHammer or isRSI_Doji or isRSI_DragonflyDoji or isRSI_GravestoneDoji or isRSI_HangingMan or isRSI_SpinningTop or isRSI_InsidePattern,
    title='Candlestick Pattern Detected when RSI > 65',
    message='A candlestick pattern has been detected when RSI is above 65!'
)