Mack Drake
Mack Drake

Reputation: 1

Compare indicator value with historical value X candles ago

My goal is to compare the current bars CCI level against the last 10 candles and if it is lower than any/all of them, mark the current bar with barcolor. This would be done by running a CCI inside of the indicator like this:

MyCCI = cci(close, 10)

Pine does not allow barcolor inside of "if" statements and there seems to be no logical way to compare two values. Am I missing a more obvious way to do this?

Upvotes: 0

Views: 1556

Answers (1)

riot
riot

Reputation: 327

I would try using the function lowest(). This function takes two arguments: the series and the length in bars (integer).

The if statement isn't needed because the true/false logic can be done directly inside the barcolor() function. MyCCI == lowest_cci compares the CCI for current bar to the lowest for the last 10 bars.

//@version=3
study("Lowest CCI", overlay=true)
MyCCI = cci(close, 10)
lowest_cci = lowest(MyCCI, 10)
// if current CCI is lowest, change bar to white, else change to blue
barcolor(MyCCI == lowest_cci ? white : blue)

Here's how the script affects the chart. All the white bars match your condition. Hope this helps! changing bar color with pine script

Upvotes: 0

Related Questions