I've created a pinescript to box candlesticks if their bodies close near each other around the same price. I'm new to coding and have been trying to piece things together but my boxes are forming at the highest high and lowest low.
<code>//@version=5
indicator("Box Box", overlay=true)
// Input parameters
dynamicLookBackMultiplier = input.float(0.5, title="Dynamic Look Back Multiplier", minval=0.1, maxval=1.0, step=0.1)
tolerance = input.float(0.01, title="Price Tolerance", minval=0.0, step=0.001)
minCloseCount = input.int(3, title="Minimum Close Count", minval=1)
// Customizable colors and line styles
resistanceBorderColor = input.color(color.yellow, title="Resistance Box Border Color")
supportBorderColor = input.color(color.yellow, title="Support Box Border Color")
resistanceBgColor = input.color(color.new(color.red, 90), title="Resistance Box Background Color")
supportBgColor = input.color(color.new(color.green, 90), title="Support Box Background Color")
centerLineColor = input.color(color.red, title="Center Line Color")
centerLineStyle = input.string("dashed", title="Center Line Style", options=["solid", "dashed", "dotted"])
centerLineWidth = input.int(1, title="Center Line Width", minval=1)
// Calculate dynamic look-back period based on visible bars
totalBars = bar_index + 1
visibleBars = input.int(100, title="Number of Visible Bars") // Adjust this if needed
dynamicLookBackPeriod = math.round(visibleBars * dynamicLookBackMultiplier)
// Function to detect close prices within tolerance
isCloseWithinTolerance(p1, p2, tolerance) =>
math.abs(p1 - p2) <= tolerance
// Initialize arrays to store candle close values
var float[] candleCloses = array.new_float(dynamicLookBackPeriod)
// Populate array with candle close values
for i = 0 to dynamicLookBackPeriod - 1
array.set(candleCloses, i, close[i])
// Initialize resistance and support levels
var float resistanceLevel = na
var float supportLevel = na
// Counters for close prices near levels
var int closeCount = 0
// Calculate potential resistance and support levels based on candle closes
for i = 0 to dynamicLookBackPeriod - 1
float levelToCheck = array.get(candleCloses, i)
if (isCloseWithinTolerance(close[i], levelToCheck, tolerance))
closeCount := closeCount + 1
// Set levels if the number of close counts meets the minimum requirement
if (closeCount >= minCloseCount)
resistanceLevel := array.max(candleCloses)
supportLevel := array.min(candleCloses)
// Calculate center line
var float centerY = na
if (not na(resistanceLevel) and not na(supportLevel))
centerY := (resistanceLevel + supportLevel) / 2
// Draw boxes and center lines
var box resistanceBox = na
var box supportBox = na
var line centerLine = na
if (not na(resistanceLevel) and closeCount >= minCloseCount)
if (na(resistanceBox))
resistanceBox := box.new(left=bar_index - dynamicLookBackPeriod, top=resistanceLevel, right=bar_index, bottom=supportLevel, border_color=resistanceBorderColor, border_width=2, bgcolor=resistanceBgColor)
box.set_left(resistanceBox, bar_index - dynamicLookBackPeriod)
box.set_right(resistanceBox, bar_index)
box.set_top(resistanceBox, resistanceLevel)
box.set_bottom(resistanceBox, supportLevel)
if (na(centerLine))
centerLine := line.new(x1=bar_index - dynamicLookBackPeriod, y1=centerY, x2=bar_index, y2=centerY, color=centerLineColor, width=centerLineWidth, style=centerLineStyle == "dashed" ? line.style_dashed : centerLineStyle == "dotted" ? line.style_dotted : line.style_solid)
line.set_xy1(centerLine, bar_index - dynamicLookBackPeriod, centerY)
line.set_xy2(centerLine, bar_index, centerY)
// Update box background color based on the most recent candle
if (not na(resistanceBox))
box.set_bgcolor(resistanceBox, close > centerY ? color.new(color.green, 90) : color.new(color.red, 90))
if (not na(supportBox))
box.set_bgcolor(supportBox, close > centerY ? color.new(color.green, 90) : color.new(color.red, 90))
</code>
<code>//@version=5
indicator("Box Box", overlay=true)
// Input parameters
dynamicLookBackMultiplier = input.float(0.5, title="Dynamic Look Back Multiplier", minval=0.1, maxval=1.0, step=0.1)
tolerance = input.float(0.01, title="Price Tolerance", minval=0.0, step=0.001)
minCloseCount = input.int(3, title="Minimum Close Count", minval=1)
// Customizable colors and line styles
resistanceBorderColor = input.color(color.yellow, title="Resistance Box Border Color")
supportBorderColor = input.color(color.yellow, title="Support Box Border Color")
resistanceBgColor = input.color(color.new(color.red, 90), title="Resistance Box Background Color")
supportBgColor = input.color(color.new(color.green, 90), title="Support Box Background Color")
centerLineColor = input.color(color.red, title="Center Line Color")
centerLineStyle = input.string("dashed", title="Center Line Style", options=["solid", "dashed", "dotted"])
centerLineWidth = input.int(1, title="Center Line Width", minval=1)
// Calculate dynamic look-back period based on visible bars
totalBars = bar_index + 1
visibleBars = input.int(100, title="Number of Visible Bars") // Adjust this if needed
dynamicLookBackPeriod = math.round(visibleBars * dynamicLookBackMultiplier)
// Function to detect close prices within tolerance
isCloseWithinTolerance(p1, p2, tolerance) =>
math.abs(p1 - p2) <= tolerance
// Initialize arrays to store candle close values
var float[] candleCloses = array.new_float(dynamicLookBackPeriod)
// Populate array with candle close values
for i = 0 to dynamicLookBackPeriod - 1
array.set(candleCloses, i, close[i])
// Initialize resistance and support levels
var float resistanceLevel = na
var float supportLevel = na
// Counters for close prices near levels
var int closeCount = 0
// Calculate potential resistance and support levels based on candle closes
for i = 0 to dynamicLookBackPeriod - 1
float levelToCheck = array.get(candleCloses, i)
if (isCloseWithinTolerance(close[i], levelToCheck, tolerance))
closeCount := closeCount + 1
// Set levels if the number of close counts meets the minimum requirement
if (closeCount >= minCloseCount)
resistanceLevel := array.max(candleCloses)
supportLevel := array.min(candleCloses)
// Calculate center line
var float centerY = na
if (not na(resistanceLevel) and not na(supportLevel))
centerY := (resistanceLevel + supportLevel) / 2
// Draw boxes and center lines
var box resistanceBox = na
var box supportBox = na
var line centerLine = na
if (not na(resistanceLevel) and closeCount >= minCloseCount)
if (na(resistanceBox))
resistanceBox := box.new(left=bar_index - dynamicLookBackPeriod, top=resistanceLevel, right=bar_index, bottom=supportLevel, border_color=resistanceBorderColor, border_width=2, bgcolor=resistanceBgColor)
box.set_left(resistanceBox, bar_index - dynamicLookBackPeriod)
box.set_right(resistanceBox, bar_index)
box.set_top(resistanceBox, resistanceLevel)
box.set_bottom(resistanceBox, supportLevel)
if (na(centerLine))
centerLine := line.new(x1=bar_index - dynamicLookBackPeriod, y1=centerY, x2=bar_index, y2=centerY, color=centerLineColor, width=centerLineWidth, style=centerLineStyle == "dashed" ? line.style_dashed : centerLineStyle == "dotted" ? line.style_dotted : line.style_solid)
line.set_xy1(centerLine, bar_index - dynamicLookBackPeriod, centerY)
line.set_xy2(centerLine, bar_index, centerY)
// Update box background color based on the most recent candle
if (not na(resistanceBox))
box.set_bgcolor(resistanceBox, close > centerY ? color.new(color.green, 90) : color.new(color.red, 90))
if (not na(supportBox))
box.set_bgcolor(supportBox, close > centerY ? color.new(color.green, 90) : color.new(color.red, 90))
</code>
//@version=5
indicator("Box Box", overlay=true)
// Input parameters
dynamicLookBackMultiplier = input.float(0.5, title="Dynamic Look Back Multiplier", minval=0.1, maxval=1.0, step=0.1)
tolerance = input.float(0.01, title="Price Tolerance", minval=0.0, step=0.001)
minCloseCount = input.int(3, title="Minimum Close Count", minval=1)
// Customizable colors and line styles
resistanceBorderColor = input.color(color.yellow, title="Resistance Box Border Color")
supportBorderColor = input.color(color.yellow, title="Support Box Border Color")
resistanceBgColor = input.color(color.new(color.red, 90), title="Resistance Box Background Color")
supportBgColor = input.color(color.new(color.green, 90), title="Support Box Background Color")
centerLineColor = input.color(color.red, title="Center Line Color")
centerLineStyle = input.string("dashed", title="Center Line Style", options=["solid", "dashed", "dotted"])
centerLineWidth = input.int(1, title="Center Line Width", minval=1)
// Calculate dynamic look-back period based on visible bars
totalBars = bar_index + 1
visibleBars = input.int(100, title="Number of Visible Bars") // Adjust this if needed
dynamicLookBackPeriod = math.round(visibleBars * dynamicLookBackMultiplier)
// Function to detect close prices within tolerance
isCloseWithinTolerance(p1, p2, tolerance) =>
math.abs(p1 - p2) <= tolerance
// Initialize arrays to store candle close values
var float[] candleCloses = array.new_float(dynamicLookBackPeriod)
// Populate array with candle close values
for i = 0 to dynamicLookBackPeriod - 1
array.set(candleCloses, i, close[i])
// Initialize resistance and support levels
var float resistanceLevel = na
var float supportLevel = na
// Counters for close prices near levels
var int closeCount = 0
// Calculate potential resistance and support levels based on candle closes
for i = 0 to dynamicLookBackPeriod - 1
float levelToCheck = array.get(candleCloses, i)
if (isCloseWithinTolerance(close[i], levelToCheck, tolerance))
closeCount := closeCount + 1
// Set levels if the number of close counts meets the minimum requirement
if (closeCount >= minCloseCount)
resistanceLevel := array.max(candleCloses)
supportLevel := array.min(candleCloses)
// Calculate center line
var float centerY = na
if (not na(resistanceLevel) and not na(supportLevel))
centerY := (resistanceLevel + supportLevel) / 2
// Draw boxes and center lines
var box resistanceBox = na
var box supportBox = na
var line centerLine = na
if (not na(resistanceLevel) and closeCount >= minCloseCount)
if (na(resistanceBox))
resistanceBox := box.new(left=bar_index - dynamicLookBackPeriod, top=resistanceLevel, right=bar_index, bottom=supportLevel, border_color=resistanceBorderColor, border_width=2, bgcolor=resistanceBgColor)
box.set_left(resistanceBox, bar_index - dynamicLookBackPeriod)
box.set_right(resistanceBox, bar_index)
box.set_top(resistanceBox, resistanceLevel)
box.set_bottom(resistanceBox, supportLevel)
if (na(centerLine))
centerLine := line.new(x1=bar_index - dynamicLookBackPeriod, y1=centerY, x2=bar_index, y2=centerY, color=centerLineColor, width=centerLineWidth, style=centerLineStyle == "dashed" ? line.style_dashed : centerLineStyle == "dotted" ? line.style_dotted : line.style_solid)
line.set_xy1(centerLine, bar_index - dynamicLookBackPeriod, centerY)
line.set_xy2(centerLine, bar_index, centerY)
// Update box background color based on the most recent candle
if (not na(resistanceBox))
box.set_bgcolor(resistanceBox, close > centerY ? color.new(color.green, 90) : color.new(color.red, 90))
if (not na(supportBox))
box.set_bgcolor(supportBox, close > centerY ? color.new(color.green, 90) : color.new(color.red, 90))
I would like multiple boxes to form instead of just one and for the box to stop generating when a candle falls out of the internal box zone. When a candle does fall out I would want the code to wait until another area of consolidation begins to form and then begin boxing around those new candles. Right now I can only get it to plot one box so its making them much larger than they should be. And with no prior boxes being left behind its not as useful when trying to see where the next trend might bounce to and find resistance. I haven’t found much online or with chatgpt. I’ve attached images with the yellow boxes I’ve plotted and would like the code to mimic and the boxes it’s currently plotting. Thanks for any help!
The boxes in yellow are the ones I’ve plotted and would like the code to mimic and chart as well. The red box is what it is actually plotting.`
New contributor
Ryan Shields is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.