Reputation: 13
There seems to be a bug in the compiler of Pine Script, in this below example the use of a variable called transp
sets a user definable input value for the transparency of a given colour.
Yet when using plotshape
and bgcolor
it has inconsistent results. bgcolour
works as expected, plotshape
however behaves very oddly. It sets the colour correctly for the plotted shape using style=shape.cross
, but it fails to understand the colour instruction for the text = ‘hi’
or text = ‘lo’
. In this case it uses the default colour of blue.
If you change transp
to a set integer like transp = 80
, it then works correctly and displays both the shape and text in the given colour. This is incredibly bogus, if it merely didn’t accept variables assigned to user inputs for transparency then it would affect both shape and text. You could also just enter the color.new
expression straight into plotshape
, and this works in the same way, use a variable for transparency that has a user input associated to it and it will not work correctly, use a hardcoded integer assignment and it works fine.
//@version=5
indicator(title='RSI and test colour variable', shorttitle='Colour test', overlay=false)
transp = input.int(60, minval=0, maxval=100, title='Transparency:')
blue = color.new(color.blue, 0)
green = color.new(color.green, transp)
red = color.new(color.red, transp)
white = color.new(color.white, 0)
lower = 30
higher = 60
Len = input(title='RSI Length:', defval=10)
Src = input(title='RSI Source:', defval=close)
rsi = ta.rsi(Src, Len)
plot(rsi, color=white)
plot(lower, color=blue)
plot(higher, color=blue)
plotshape (rsi > higher, location=location.top, color=green, style=shape.cross, text='Hi', size=size.tiny)
bgcolor (rsi > higher ? green : na)
plotshape (rsi < lower, location=location.top, color=red, style=shape.cross, text='Lo', size=size.tiny)
bgcolor (rsi < lower ? red : na)
I have looked over this and tried every conceivable permutation to the code to get around this, and it always responds in the same incoherent manner. It took sometime to actually realise what the issue was, this is clearly a bug, not overly fatal, but it’s not an ideal presentation, and I can’t move on with this bug looking at me, still bugging me. ;o\
Any work-arounds?
Upvotes: 1
Views: 360
Reputation: 21179
Although it might not be the reason, it does not necessarily need to be a user defined input. The value needs to be known at compile time.
If you try transp=close > 2 ? 40 : 80
, you will get the same behavior.
As a workaround, use the textcolor
parameter.
plotshape (rsi > higher, location=location.top, color=green, textcolor=green, style=shape.cross, text='Hi', size=size.tiny)
plotshape (rsi < lower, location=location.top, color=red, textcolor=red, style=shape.cross, text='Lo', size=size.tiny)
Upvotes: 1