I am trying to find the number of consecutive green candles that happen after each red candle, then show that number on the top of the corresponding red candle.
For example, if I have RGGRRGGGGRG
for red (R) and green (G) candles, I am hoping to show 2
on the first red candles, 0
on the 2nd red candle, 4
on the 3rd red candle and 1
on the last red candle.
I appreciate it if someone can help me how I can do this.
All you need is two var
variables. One to keep track of the number of consecutive green bars, and one to keep track of the bar index of the red bar so you can place your label there.
//@version=5
indicator("My script", overlay=true,max_labels_count=500)
var green_cnt = 0
var red_candle_idx = bar_index
is_green_candle = (close >= open)
is_red_candle = not is_green_candle
if (is_red_candle)
label.new(red_candle_idx, high, str.tostring(green_cnt), yloc=yloc.abovebar, color=na, textcolor=color.white)
green_cnt := 0
red_candle_idx := bar_index
else
green_cnt := green_cnt + 1