Code blocks for "counters", e.g. count "X" time and pass - MetaTrader 5 Expert - MT4/MT5 Resources
01. Count "X" times and pass.
input integer count = 50 ; // Set count limit as input integer counter; // counter variable //Expert initialization-------------------- Integer initialization () { return ( initialization successful ); } // Expert de-initialization------------------ Blank solution initialization ( constant integer reason) { } //ExpertOnTick----------------------------------------- Blank check () { Counter++; // The counter is incremented by 1 for each quote. comment ( "Current count -:" , counter); if (counter == count) // Count "X" times and pass | This block is executed only once per count. { // Your code goes here... Alert (count, "a few times" ); Counter = 0 ; // Reset counter at end of code block. This is a must. } } // OnTick ends <<------------------------
02. Pass "X" times then wait "X" times before passing.
This method can be used like wait and pass, pass and wait.
input integer count = 50 ; // Set count limit as input input integer wait = 50 ; // Set wait limit to input integer counter; // The default value of the counter variable is "0" integer waiter; //wait variable default value is "0" //Expert initialization-------------------- Integer initialization () { Return ( initialization successful ); } // Expert de-initialization------------------ Blank solution initialization ( constant integer reason) { } //ExpertOnTick----------------------------------------- Blank check () { comment ( "Counting ticks-:" , counter, "\n" , "Waiting for ticks-:" , waiter); if (counter < count) // Pass "X" times { counter++; // Update counter // Your code goes here. } Else if (waiter<wait) //wait "X" times { waiter++; // Update waiter // Your code goes here. } if (waiter == etc.) // wait limit reached { counter = 0 ; //reset counter waiter = 0 ; //reset waiter } } // OnTick ends <<------------------------ //+------------------------------------------------------------------+
Special -:
You can code "go through X times and then stop" by modifying the code above to remove the wait code block. It then counts up to a specific number and stops working until the counter stops working. If you create these variables in the global scope, you can reset it anywhere in your code. (global variable)
Attachment download
📎 count_and_pass.mq5 (0.73 KB)
📎pass_wait_then_pass.mq5 (1.1 KB)
Source: MQL5 #49213
💡 Featured Recommendations
✍️ Latest by the author
- •
- •
- •
- •
- •
- •
📌 Popular topics
- •
- •
- •
- •
- •
- •
- •
- •
🔗 You May Be Interested In
- •
- •
- •
- •
- •
- •