Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

count the number of green candles after each red candle untill we reach to the next green candle?

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading