Add New Logic for Candlestick Pattern along side old logic, show Candlestick Pattern when RSI is above 65, make sure to keep all old logic as it: //@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")
// 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
Assistant
// 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!')
Certainly! Here's the updated code with the new logic added alongside the old logic. The candlestick patterns will now also be shown when the RSI is above 65, and all the old logic has been preserved as per your request.
pine
//@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
// Plot the existing indicators
plotshape(isHammer, title="Hammer", location=location.belowbar, style=shape.labeldown, color=color.red, size=size.small, text="H")
plotshape(isShootingStar, title="Shooting Star", location=location.abovebar, style=shape.labeldown, color=color.red, size=size.small, text="SS")
plotshape(isInvertedHammer, title="Inverted Hammer", location=location.belowbar, style=shape.labeldown, color=color.red, size=size.small, text="IH")
plotshape(isDoji, title="Doji", location=location.abovebar, style=shape.labeldown, color=color.red, size=size.small, text="D")
plotshape(isDragonflyDoji, title="Dragonfly Doji", location=location.belowbar, style=shape.labeldown, color=color.red, size=size.small, text="DD")
plotshape(isGravestoneDoji, title="Gravestone Doji", location=location.abovebar, style=shape.labeldown, color=color.red, size=size.small, text="GD")
plotshape(isHangingMan, title="Hanging Man", location=location.belowbar, style=shape.labeldown, color=color.red, size=size.small, text="HM")
plotshape(isSpinningTop, title="Spinning Top", location=location.abovebar, style=shape.labeldown, color=color.red, size=size.small, text="ST")
plotshape(isInsidePattern, title="Inside Pattern", location=location.abovebar, style=shape.labeldown, color=color.red, text="IP")
// Alerts for existing patterns with high volume
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!'
)
// -------------------------------------------------------------------
// 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 = showShootingStar and isShootingStar and rsiValue > rsiOverbought
isRSI_Hammer = showHammer and isHammer and rsiValue > rsiOverbought
isRSI_InvertedHammer = showInvertedHammer and isInvertedHammer and rsiValue > rsiOverbought
isRSI_Doji = showDoji and isDoji and rsiValue > rsiOverbought
isRSI_DragonflyDoji = showDragonflyDoji and isDragonflyDoji and rsiValue > rsiOverbought
isRSI_GravestoneDoji = showGravestoneDoji and isGravestoneDoji and rsiValue > rsiOverbought
isRSI_HangingMan = showHangingMan and isHangingMan and rsiValue > rsiOverbought
isRSI_SpinningTop = showSpinningTop and isSpinningTop and rsiValue > rsiOverbought
isRSI_InsidePattern = showInsidePattern and isInsidePattern and rsiValue > rsiOverbought
// Plot the new indicators when RSI is above 65
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 new 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!'
)
Explanation of the New Logic Added:
RSI Calculation:
Calculated the RSI using the built-in ta.rsi() function.
Introduced inputs to let you customize the RSI length, source, and overbought level.
Created new variables for each candlestick pattern that check if the RSI is above 65.
These variables use both the existing pattern conditions and the new RSI condition.
pine
isRSI_ShootingStar = showShootingStar and isShootingStar and rsiValue > rsiOverbought
// Similarly for other patterns...
Plotting New Patterns:
Used plotshape() to display the new patterns when RSI is above 65.
Differentiated the new plots by changing the color to color.green, increasing the size, and adding a "●" to the text label.
pine
plotshape(isRSI_ShootingStar, title="Shooting Star RSI>65", location=location.abovebar, style=shape.labeldown, color=color.green, size=size.normal, text="SS●")
// Similarly for other patterns...
Alerts for New Patterns:
Added new alertcondition() to notify when any of the new patterns occur with RSI above 65.
pine
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!'
)
Optional Customization:
You can adjust the RSI overbought level using the input: