I am writing a TradingView Indicator that I think is pretty simple. I am creating an array of price levels, than passing it to a function to print them called “draw_levels” that loops through the data array and uses line.new() with each price in the array.
That works fine.
I want to draw zone’s around those levels like +/- 1% then fill that space. So I added this code into the draw_levels() function with a conditional to only draw the zones if the input var is true.
The problem I am seeing is when I toggle the input to show/hide the zones. For some reason, when I turn the option on, some of the price levels are simply removed from the chart. Toggle it off and the levels come back.
Any idea why?
Here’s the code:
//@version=5
indicator("Levels", overlay = true)
zones_groupname = "Zone Options"
zone_band_size_l = input.float(1, "Level Zone +/- %", group = zones_groupname)
zone_band_size_a = input.float(0.09, "Zone for Alerts +/- %", group = zones_groupname)
visuals_groupname = "Visual Options"
supercycle_color = input.color(color.yellow, "", group = visuals_groupname, inline = "vis_1")
show_supercycles = input.bool(true, "Show Super Cycle Levels?", group = visuals_groupname, inline = "vis_1")
level_color = input.color(color.red, "", group = visuals_groupname, inline = "vis_2")
show_levels = input.bool(true, "Show Levels?", group = visuals_groupname, inline = "vis_2")
zone_band_color_l = input.color(color.blue, "", group = visuals_groupname, inline = "vis_4")
show_zone_bands_l = input.bool(false, "Show Zone around Levels?", group = visuals_groupname, inline = "vis_4")
// This function will draw the levels and super cycle levels on the chart.
draw_levels(levels, labels) =>
// Loop through the levels in the array and draw them
for i = 0 to array.size(levels) - 1
// get prices and labels
price = array.get(levels, i)
level_label = array.get(labels, i)
if level_label == "level"
line.new(bar_index, price, bar_index + 1, price, width=2, color=level_color, extend=extend.both)
if show_zone_bands_l and level_label == "level"
// calculate zone around level
upper_band_price = price + (price * (zone_band_size_l/100))
lower_band_price = price - (price * (zone_band_size_l/100))
// plot upper and lower band
upper_line = line.new(bar_index, upper_band_price, bar_index + 1, upper_band_price, width=2, color=zone_band_color_l, extend=extend.both)
lower_line = line.new(bar_index, lower_band_price, bar_index + 1, lower_band_price, width=2, color=zone_band_color_l, extend=extend.both)
// fill the zone between lines
linefill.new(upper_line, lower_line, color.new(zone_band_color_l, 75))
draw_sc_levels(levels, labels) =>
// Loop through the levels in the array and draw them
for i = 0 to array.size(levels) - 1
// get prices and labels
price = array.get(levels, i)
level_label = array.get(labels, i)
if level_label == "supercycle"
line.new(bar_index, price, bar_index + 1, price, width=2, color=supercycle_color, extend=extend.both)
if show_zone_bands_sc and level_label == "supercycle"
// calculate zone around level
upper_band_price = price + (price * (zone_band_size_l/100))
lower_band_price = price - (price * (zone_band_size_l/100))
// plot upper and lower band
upper_line = line.new(bar_index, upper_band_price, bar_index + 1, upper_band_price, width=2, color=zone_band_color_sc, extend=extend.both)
lower_line = line.new(bar_index, lower_band_price, bar_index + 1, lower_band_price, width=2, color=zone_band_color_sc, extend=extend.both)
// fill the zone between lines
linefill.new(upper_line, lower_line, color.new(zone_band_color_sc, 75))
// get the symbole the chart is on
symbol = syminfo.ticker
// declare arrays to hold level data
var float[] levels = na
var string[] labels = na
// symbol data
if symbol == "USDJPY"
levels := array.new_float(18)
array.set(levels , 0, 162.437)
array.set(levels , 1, 156.313)
array.set(levels , 2, 151.434)
array.set(levels , 3, 146.31)
array.set(levels , 4, 141.186)
array.set(levels , 5, 136.409)
array.set(levels , 6, 131.249)
array.set(levels , 7, 126.829)
array.set(levels , 8, 122.608)
array.set(levels , 9, 116.5)
array.set(levels , 10, 111.829)
array.set(levels , 11, 106.296)
array.set(levels , 12, 101.769)
array.set(levels , 13, 96.811)
array.set(levels , 14, 91.961)
array.set(levels , 15, 86.464)
array.set(levels , 16, 81.434)
array.set(levels , 17, 76.547)
labels := array.new_string(18)
array.set(labels, 0, 'level')
array.set(labels, 1, 'level')
array.set(labels, 2, 'supercycle')
array.set(labels, 3, 'level')
array.set(labels, 4, 'level')
array.set(labels, 5, 'level')
array.set(labels, 6, 'level')
array.set(labels, 7, 'supercycle')
array.set(labels, 8, 'level')
array.set(labels, 9, 'level')
array.set(labels, 10, 'level')
array.set(labels, 11, 'level')
array.set(labels, 12, 'supercycle')
array.set(labels, 13, 'level')
array.set(labels, 14, 'level')
array.set(labels, 15, 'level')
array.set(labels, 16, 'level')
array.set(labels, 17, 'supercycle')
// conditionally show the different types of levels
if show_levels
draw_levels(levels, labels)
if show_supercycles
draw_sc_levels(levels, labels)
I have tried changing the data, limiting the data, re-inputting the data, re-typing the data arrays to ensure no mistakes, changed around the logic for toggling the zones on/off.
Bryan Goldstein is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.