Mastering Counters in MetaTrader 5: Count, Wait, and Execute

Mike 2024.04.14 19:15 29 0 0
Attachments

01. Count "X" Times and Then Execute.

  • Step 01 - Create a variable to set the count limit. You can also use this as an input parameter for optimization in your code.
  • Step 02 - Set another variable to track how many times the count has occurred.
  • Step 03 - When the Counter reaches your set count limit, it’s time to execute the specified code block.
  • Step 04 - After executing the code, remember to reset the counter. Failing to do so will cause it to count indefinitely.
You can also implement filtering conditions for the Counter block, like: >> "IF this is true, then count once."
input int count = 50; // Set the counting limit as an input

int Counter; // Counter variable

// Expert Initialization --------------------
int OnInit()
{
 return(INIT_SUCCEEDED);
}

// Expert Deinitialization -------------------
void OnDeinit(const int reason)
{

}

// Expert OnTick --------------------------
void OnTick()
{
 Counter ++; // Increment the counter on each tick.
 Comment("Current Count -:", Counter);
 
 if(Counter == count) // Count "X" times and execute
 {
  
  // Your code goes here......
 Alert(count," Times counted");
 Counter = 0; // Reset the counter at the end of your code block.
 }

} // OnTick End  <<----------------------


02. Pass "X" Times and Then Wait "X" Times Before Executing.

This method can be used for various execution patterns like wait and pass, or pass and wait.

  • Step 01 - Create variables to set both the count limit and the wait limit. These can also serve as input parameters for optimization.
  • Step 02 - Set up another variable to store both the counted limit and the wait limit.
  • Step 03 - When the Counter reaches the count limit, it’s time to execute the specified code block.
  • Step 04 - Once the Waiter reaches the wait limit, it’s time to pause for a bit.
  • Step 05 - After the waiting limit is met, make sure to reset both the counter and the waiter. Otherwise, it will stop functioning.
You can set filtering conditions for both the Counter and Waiter blocks, such as: >> "IF this is true, wait a bit."


input int count = 50; // Set the counting limit as an input
input int wait = 50; // Set the waiting limit as an input

int Counter; // Counter variable, default value is "0"
int Waiter; // Waiting variable, default value is "0"

// Expert Initialization --------------------
int OnInit()
  {
   return(INIT_SUCCEEDED);
  }

// Expert Deinitialization -------------------
void OnDeinit(const int reason)
  {

  }

// Expert OnTick --------------------------
void OnTick()
  {
   Comment("Counted Ticks -: ", Counter, "\n", "Waited Ticks -: ", Waiter);

   if(Counter < count) // Pass "X" times
     {
      Counter++; // Update the counter

      // Your code goes here.

     }
   else
      if(Waiter < wait) // Wait for "X" times
        {
         Waiter++; // Update the waiter

         // Your code goes here.

        }

   if(Waiter == wait) // Waiting limit is reached
     {
      Counter = 0; // Reset counter
      Waiter = 0; // Reset waiter
     }



  } // OnTick End  <<----------------------
//+------------------------------------------------------------------+

Special Note:

You can modify the above code to create a "Pass X times and stop" function by simply removing the waiting code block. This will allow it to count a specific number of times and then stop until the counter is reset. You can reset it anywhere in your code if you declare these variables on a global scale.


List
Comments 0