Pine code v5, the rank and the abs functions not recognised

442 Views Asked by At

this pinescript v5 doesn't seem to work for me, do you see anything wrong, how to correct it?

//@version=5
indicator("Top 20 Candles with Highest Absolute Differences", overlay=true)

// calculate the absolute difference between open and close prices using math.abs() function
abs_diff = math.abs(open - close)

// get the top 20 candles with the highest absolute differences over the last 200 days
rank = rank(abs_diff, 200)
is_top_20 = rank <= 20

// color the top 20 candles black
bar.color(is_top_20 ? color.black : na)

My tradingview account doesn't recognise the 'Rank' and the 'Abs' functions in the way I wrote them.

1

There are 1 best solutions below

6
Moebius On

Please check the script below:

//@version=5 
indicator("Top 20 Candles with Highest Absolute Differences", overlay=true)

lookback = input.int(200, "Lookback window, bars")

// calculate the absolute difference between open and close prices using math.abs() function 
abs_diff = math.abs(open - close)

var diffs = array.new<float>()
diffs.unshift(abs_diff)
while diffs.size() > lookback // keep array size <= 200
    diffs.pop()



// get the top 20 candles with the highest absolute differences over the last 200 days 
// rank = rank(abs_diff, 200) 
// is_top_20 = rank <= 20
rank = diffs.percentrank(0)
is_top_20 = rank >= 80

lb = label.new(bar_index, high, str.format("{0,number, 0}", rank), style = label.style_none, size = size.normal, textcolor = color.yellow)
lb1 = label.new(bar_index, low, str.format("{0,number, 0.0}", abs_diff), style = label.style_label_up, size = size.normal, textcolor = color.yellow)


// color the top 20 candles black 
barcolor(is_top_20 ? color.black : na)
bgcolor(is_top_20 ? color.rgb(163, 234, 236, 51) : na)