I'm trying to write the code that would count the times something happens. It doesn't matter what. You don't need any existing code. I'm just learning and trying to understand scope and the order that things are processed. It's all still pretty new. I just want to write, for example:
if(a == b)
{
do something.
counter++
}
So, every time "do something" happened, it would add 1 to the count. It seems straight forward enough but when I attempt it in any context, it only adds 1 to the original value. If I create "counter = 0" and if something happens then counter++, then I will just get a bunch of counter = 1 over and over every time something happens. Does the code have to be more complex then simply creating a variable with a value of 0 then just writing it with "++" inside the IF brackets? Or am I missing something else?
use static variables.
![Documentation on MQL5: Language Basics / Variables / Static Variables Documentation on MQL5: Language Basics / Variables / Static Variables](https://c.mql5.com/36/48/documentation-on-mql5-language__3.png)
- www.mql5.com
I'm trying to write the code that would count the times something happens. It doesn't matter what. You don't need any existing code. I'm just learning and trying to understand scope and the order that things are processed. It's all still pretty new. I just want to write, for example:
if(a == b) { // do something. counter++; }So, every time "do something" happened, it would add 1 to the count. It seems straight forward enough but when I attempt it in any context, it only adds 1 to the original value. If I create "counter = 0" and if something happens then counter++, then I will just get a bunch of counter = 1 over and over every time something happens. Does the code have to be more complex then simply creating a variable with a value of 0 then just writing it with "++" inside the IF brackets? Or am I missing something else?
- When posting code, please use the “</>“ icon from the toolbar or Alt-S. Don’t just paste code like normal text.
- Always provide code that is as correct as possible, for example using proper code comments and “;” at the end of the counter line. This helps prevent misunderstandings about possible unrelated errors not connected to your original request for help.
- The code you provided as an example is too simplistic. There are more issues that need to be considered, so provide an example that is more encompassing or closer to your actual requirement. One also needs to consider the scope of the counter. Is it only supposed to live within a loop, or only within a function, or does it need to a global variable within the life-time of the EA or Indicator?
- Show where in your code the counter is declared and initialised, as well as where its value is checked and used. All of those could be probable reasons for the logic to fail.
- Code is like maths and language; a single comma can make a vast difference to the result. Everything needs to be very precise. So provide as much detail as you can about what you want to achieve.
- There might be many answers for your question and many of them will be valid. There can be many solutions, each one tailored for a specific point of view of who provides it.
bool a = false; bool b = false; int counter = 0; void OnTick() { if(a == b) { counter = counter + 1; Print(IntegerToString(counter)); a = true; Sleep(1000); a = false; } if(counter == 10) { counter = 0; }
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
I'm trying to write the code that would count the times something happens. It doesn't matter what. You don't need any existing code. I'm just learning and trying to understand scope and the order that things are processed. It's all still pretty new. I just want to write, for example:
if(a == b)
{
do something.
counter++
}
So, every time "do something" happened, it would add 1 to the count. It seems straight forward enough but when I attempt it in any context, it only adds 1 to the original value. If I create "counter = 0" and if something happens then counter++, then I will just get a bunch of counter = 1 over and over every time something happens. Does the code have to be more complex then simply creating a variable with a value of 0 then just writing it with "++" inside the IF brackets? Or am I missing something else?