Editing a Tradingview pine script that boxes area of resistance/consolidation

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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật