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 ...
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.

You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
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.