0

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.

G.Lebret
  • 2,826
  • 2
  • 16
  • 27
Lavy
  • 1

1 Answers1

0

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)
Moebius
  • 329
  • 1
  • 7
  • Thank you very much for the time you invested in writing this code, it works fantastic for me. – Lavy Apr 13 '23 at 11:22
  • Glad to hear. If you are happy with the answer - please accept it. Otherwise feel free to ask. – Moebius Apr 13 '23 at 13:54
  • If I don't ask too much, can you also please write a Pine Code script that it highlights in any way (color candle in black for example) a list of already predefined dates on the chart? Dates example: 01 Mar 2023, 02 March 2023, 03 Mar 2023. – Lavy Apr 13 '23 at 14:32
  • This looks like a separate question. Please accept the answer to this question if you are happy with that and I will help you with the other one. How do you wish to supply the list of the dates, using input fields or as a list in the script itself? – Moebius Apr 14 '23 at 13:21
  • Hi, thanks so much. The answer is: as a list in the script itself. – Lavy Apr 15 '23 at 07:22
  • The script is ready. Please accept the answer to your previous question if it is satisfactory and ask a new question and I will paste the script in reply to your question. – Moebius Apr 15 '23 at 19:56