Search code examples
colorslabelpine-scriptshapespine-script-v5

Pine script box/label too thick/vibrant. Possibly duplicate stacks?


The boxes and labels generated by this code:

TKO = #e91e63
TKObg = color.new(#e91e63, 90)
LDN = #2157f3
LDNbg = color.new(#2157f3, 90)
NYC = #ff5d00
NYCbg = color.new(#ff5d00, 90)
box.new(1699578000000, 96.379, 1699599600000, 96.154, bgcolor=TKObg, border_color=TKO, xloc=xloc.bar_time, border_style=line.style_dotted)
label.new(1699588800000, 96.379, 'Tokyo', textcolor=TKO, color=color.new(color.white,100), xloc=xloc.bar_time, size=size.tiny, style=label.style_label_down)
box.new(1699599600000, 96.358, 1699621200000, 96.161, bgcolor=LDNbg, border_color=LDN, xloc=xloc.bar_time, border_style=line.style_dotted)
label.new(1699610400000, 96.358, 'London', textcolor=LDN, color=color.new(color.white,100), xloc=xloc.bar_time, size=size.tiny, style=label.style_label_down)
box.new(1699621200000, 96.327, 1699635600000, 96.037, bgcolor=NYCbg, border_color=NYC, xloc=xloc.bar_time, border_style=line.style_dotted)
label.new(1699628400000, 96.327, 'New York', textcolor=NYC, color=color.new(color.white,100), xloc=xloc.bar_time, size=size.tiny, style=label.style_label_down)
box.new(1699635600000, 96.418, 1699653300000, 96.2, bgcolor=NYCbg, border_color=NYC, xloc=xloc.bar_time, border_style=line.style_dotted)
label.new(1699644450000, 96.418, 'New York', textcolor=NYC, color=color.new(color.white,100), xloc=xloc.bar_time, size=size.tiny, style=label.style_label_down)

look like this: enter image description hereThe labels are unreadable and the boxes are very opaque while supposedly having 90% transparency.

Here is an example of boxes that use the same colors and same transparency (not generated by my code): enter image description here

I tried going through the code that creates proper boxes and it seems that they consider timeframes when generating shapes. My guess is that my code creates a stack of duplicates, one for each timeframe, and displays all of them at once, causing this over-saturation of color/text.

I have not found a proper way to adapt their code to my purpose, as my pine script is generated by a python script automatically, and adds shapes over time, line by line, with pre-generated timestamp values. I haven't found any solution for this in the documentation or on forums. I can't seem to find a way to make my shapes timeframe-specific or otherwise not over-saturated.

I want my shapes to not be over-saturated.


Solution

  • The Pine Script engine will execute your code (at least) once on every new bar drawn. As it's currently written, it will generate three labels and three boxes at the same location as many times as there is a candle drawn on the chart. Hence the stacking of labels/boxes caused the effect you observed.

    If you wanted those boxes/labels to be drawn only once, wrap your code in an

    if barstate.islast
        ...
    

    condition. This will ensure that your boxes/labels are only drawn once when the script executes on the last bar.