A simple counter?

 

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?

 
dasilvja:

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
  • www.mql5.com
Static Variables - Variables - Language Basics - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
dasilvja:

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?
  1. When posting code, please use the “</>“ icon from the toolbar or Alt-S. Don’t just paste code like normal text.
  2. 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.
  3. 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?
  4. 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.
  5. 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.
  6. 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.
 
I think dasilvja had in mind something simple like this:
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;
   }