Watch how to download trading robots for free
Find us on Twitter!
Join our fan page
Interesting script?
So post a link to it -
let others appraise it
You liked the script? Try it in the MetaTrader 5 terminal
Experts

Code blocks for "Counters" like Count "X" time and pass - expert for MetaTrader 5

Views:
1056
Rating:
(2)
Published:
2024.04.14 10:15
Need a robot or indicator based on this code? Order it on Freelance Go to Freelance

01. Count "X" Times and then pass.

  • Step 01 - Created a variable for set the count limit. you can use it as an input parameter as well of optimization in your code. 
  • Step 02 - Create another variable to store the counted limit (How many times it has been counted).
  • Step 03 - Once the Counter and your count limit is equals, that means it's time to pass the code block specified by you.
  • Step 04 - Once the code passed, be sure to reset the counter as well. Otherwise, it will count for infinite (in this case).
you can set filtering conditions to the Counter block as well. like, >> "IF this is true, then count once."

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

int Counter; // counter variable

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

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

}

// Expert OnTick --------------------------
void OnTick()
{
 Counter ++; // add 1 to the counter on each tick.  
 Comment("Current Count -:", Counter);
 
 if(Counter == count)  // Count "X" times and pass   | This block Executed only once per each count.
 {
  
  // Your code goes here......

 Alert(count," Times counted"); 
 Counter = 0; // Reset the counter at the end of your code block. This is must. 
 } 

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


02. Pass "X" times and then Wait "X" times then pass.

this method can be use as like wait and pass, pass and wait. 

  • Step 01 - Created a variable for set the count limit and wait limit you can use them as an input parameters as well of optimization in your code. 
  • Step 02 - Create another variable to store the counted limit & wait limits (How many times it has been counted and waited).
  • Step 03 - Once the Counter and your count limit is equals, that means it's time to pass the code block specified by you.
  • Step 04 -   Once the waiter and your count wait limit is equals, that means it's time to wait for a little.
  • Step 04 - Once the waiting limit is reached, be sure to reset the counter and waiter as well. Otherwise, it will Seese to work (in this case).
you can set filtering conditions to the Counter block and waiter blocks as well. like, >> "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 Initializing --------------------
int OnInit()
  {
   return(INIT_SUCCEEDED);
  }

// Expert DeInitializing -------------------
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 -:

You can code the "Pass X times and stop" by modifying the above code by removing the waiting code block. Then it will count for specific number and stop works until the counter is rested. You can reset it anywhere in your code if you create these variables in global scale. (global variables) 


Basic GridManager Library Basic GridManager Library

This is a basic library to create and manage grids.

A Code block to detect A "New Candle/Bar" using bars history (very effective way) A Code block to detect A "New Candle/Bar" using bars history (very effective way)

If you only want to execute your code blocks "only once per bar" it's important to check if there is new bar arrived or not.

Simple Yet Effective Breakout Strategy Simple Yet Effective Breakout Strategy

A simple yet effective donchian channel breakout strategy. This strategy is timeless!

QuickTradeKeys123 QuickTradeKeys123

QuickTradeKeys 123 is a user-friendly Expert Advisor (EA) for MetaTrader 5, enabling traders to execute buy and sell operations quickly by simply pressing the numbers '1' and '2' on their keyboard. Pressing '3' closes all open positions. This EA is ideal for swift trading and testing purposes where manual intervention is required without using the mouse.