prime90
prime90

Reputation: 959

Pine script error when applying colors to volume bars

I'm trying to highlight volume bars that are 200% or greater than the previous bar but keep receiving the following error Error: Undeclared identifier volColor , however, it is declared so I don't understand the error.

study("200% Volume Highlight", shorttitle="Vol_200%")

v = volume
v0=volume[0]
v1=volume[1]

pct = (v0-v1)/v1*100

volColor = iff(pct>200, #DC143C)
plot(series=volume, style=histogram, color=volColor)

Upvotes: 0

Views: 459

Answers (2)

Bjorn Mistiaen
Bjorn Mistiaen

Reputation: 6865

The volColor error is not the problem.
That's the last error the compiler encountered, because a previous statement did not compile.
Your full error is this:

Error: Cannot call `iff` with arguments (series__bool, literal__color); available overloads: iff(bool, integer, integer) => integer; iff(bool, float, float) => float; iff(bool, series, series) => series; iff(bool, series__color, series__color) => series__color; iff(bool, bool, bool) => bool; iff(bool, string, string) => string; iff(series__bool, integer, integer) => series__integer; iff(series__bool, float, float) => series; iff(series__bool, series, series) => series; iff(series__bool, series__color, series__color) => series__color; iff(series__bool, bool, bool) => series__bool; iff(float, integer, integer) => integer; iff(float, float, float) => float; iff(float, series, series) => series; iff(float, series__color, series__color) => series__color; iff(float, bool, bool) => bool; iff(float, string, string) => string; iff(series, integer, integer) => series__integer; iff(series, float, float) => series; iff(series, series, series) => series; iff(series, series__color, series__color) => series__color; iff(series, bool, bool) => series__bool;
Error: Undeclared identifier `volColor`

The real problem is that iif takes 3 inputs instead of 2.

This will work.

//@version=4
study("200% Volume Highlight", shorttitle="Vol_200%")

signalThreshold = input(defval=200, title="Threshold (%)", type=input.float, minval=0)

v  = volume
v0 = volume[0] // volume[0] is the volume on the current bar. So it's the same as just using 'volume'. Or in your case, variable 'v'.
v1 = volume[1]

color volColor = na
color defaultColor = color.blue
color signalColor = #DC143C

pct = (v0-v1)/v1*100

volColor := iff(pct>signalThreshold, signalColor, defaultColor)

// This code will be faster. It does the same thing, but doesn't call a function.
//volColor := pct > signalThreshold ? signalColor : defaultColor

plot(series=volume, style=plot.style_histogram, color=volColor)

Note: You must always use the //@version=x as the first line of your scripts.

Upvotes: 0

PineCoders-LucF
PineCoders-LucF

Reputation: 8779

You did not have a color assigned for the case where pct>200 is false. na is used now but you can replace it with the color of your choice, of course.

Pls include the //@version= compiler directive with your snippets so we know which version of Pine you are using.

//@version=3
study("200% Volume Highlight", shorttitle="Vol_200%")

v = volume
v0=volume[0]
v1=volume[1]

pct = (v0-v1)/v1*100

volColor = iff(pct>200, #DC143C, na)
plot(series=volume, style=histogram, color=volColor)

enter image description here

Upvotes: 2

Related Questions