high low counter

 
 if(IsNewBar()) max_contada = false;
   if(IsNewBar()) min_contada = false;
   if(contar_max_min == 1){
      if(!max_contada && rates[0].close > rates[1].high){
         rompeu_max++;
         max_contada = true;
      }
      
      if(!min_contada && rates[0].close > rates[1].low){
         rompeu_min++;
         min_contada = true;
      }
   }

Hello!

I'm trying to do a counter of how many times the price cross de previous high or low after start a trade.

The variable contar_max_min initially is 0 and when the trade is open receive the value 1.

This is not working, rompeu_max and rompeu_min reach the value 1 and stop counting.

 

It is not working, because the IsNewBar() function that everyone insists on using, can only be called once to get the correct state. It uses a static variable to keep track of things, so it will fail on subsequent calls.

Instead study the following example, so that you can understand a better way to do it ...

Code Base

Detecting the start of a new bar or candle

Fernando Carreiro, 2022.04.24 00:38

Detecting the start of a new bar or candle, in the OnTick() event handler of an expert advisor.