How to insert a minimum time interval for opening new orders and delay trades.

 

Hello,


Please let me explain my situation.


I’m trying many different expert advisors in a demo account. Each expert is attached to a different chart and/or currency.


The problem is that in some cases theses experts are opening many orders in a small amount of time.


My objective is to set a minimum time interval (about 30 second or more) between opening trades and delay all orders that are lunched too close the previous ones.


For example:


EA1-chart1: order 1 at 00:00:07

EA1-chart2: order 2 at 00:00:08

EA2-chart3: order 3 at 00:00:09

EA3-chart4: order 4 at 00:00:10


Become

EA1-chart1: order 1 at 00:00:07

EA1-chart2: order 2 at 00:00:38 or (00:00:37)

EA2-chart3: order 3 at 00:01:09 or (00:01:07)

EA3-chart4: order 4 at 00:01:40 or (00:01:37)


Did anyone have a similar situation in the past? If yes, is there a way to develop an expert that is able to do this task by putting it just in a random chart.


This might also be applied to orders placed manually.


Sorry for my English and thank you in advance.

 
MT4MQL4Developper:

I’m trying many different expert advisors in a demo account. Each expert is attached to a different chart and/or currency.

The problem is that in some cases theses experts are opening many orders in a small amount of time.

My objective is to set a minimum time interval (about 30 second or more) between opening trades and delay all orders that are lunched too close the previous ones.

Did anyone have a similar situation in the past? If yes, is there a way to develop an expert that is able to do this task by putting it just in a random chart.

This might also be applied to orders placed manually.

Not possible, unless you have the codes for all the EAs, and modify them accordingly.

To apply this rule to manually placed orders, you can open orders via the running EA (e.g. draw a HLine to indicate your intent of placing an order, and let the EA do all necessary time checks).

But since you're talking about demo account, why not install multiple MT4s and let each one run a different account and different EA?

 

Hello,

Thank you for the answer.

I’m already running 4 terminals in my VPS server; I’m consuming all resources and I do not want to buy a more performant VPS or another server.

I have already found these codes for doing the job, but I was not able to compile them.

https://www.mql5.com/en/articles/1355

Does anyone have a simpler code to insert in each EA for delaying trades?

Thank you in advance.

 

MT4MQL4Developper: I have already found these codes for doing the job, but I was not able to compile them.

Does anyone have a simpler code to insert in each EA for delaying trades?

  1. That article/code is over 13 years old. A lot has changed since Build 600 and Higher. 2014.02.03 Context busy was because of one thread for opening orders. Now the terminal supports 8 (IIRC.)

  2. datetime delay=0;
    void OnTick(){
       if(TimeCurrent() < delay) return;
       ⋮
       delay = TimeCurent() + 30*60; // 30 second delay.

 

Hello William,

Thank you for the answer.

I think that what you mean is something like :


datetime delay=0;
void OnTick(){
   if(TimeCurrent() < delay) sleep(30000);
   ⋮
   delay = TimeCurent() + 30*60; // 30 second delay.


Is this correct ?

 
MT4MQL4Developper:

Hello William,

Thank you for the answer.

I think that what you mean is something like :

Is this correct ?

"William" can answer you later on when his "shift" comes... for now, I'll just put down what I know :).

Be it "return" or "sleep" or anything else - it really depends on what you want your EA to do during the time that it cannot open new trades (e.g. does it still need to monitor the market for any opportunities to close trades on-the-fly?)

Another thing is your "delay" - a variable won't survive beyond a system shutdown, and definitely won't be accessibly by other EAs running on the same terminal, so you should consider either using a GlobalVariables ( https://book.mql4.com/variables/globals) instead, or perhaps even retrieving your order's open time to be absolutely sure of the time elapsed.

Documentation on MQL5: Global Variables of the Terminal
Documentation on MQL5: Global Variables of the Terminal
  • www.mql5.com
Global variables are kept in the client terminal for 4 weeks since the last access, then they will be deleted automatically. An access to a global variable is not only setting of a new value, but reading of the global variable value, as well.
 
  1. If you don't need to do anything during the delay, just Sleep and RefreshRates.
    After Sleep and between server calls the market will have changed. You must update your variables before using any Predefined Variables and series arrays you must call RefreshRates
  2. If you must do other things, set the variable, return, and do them on the next tick.

  3. Since the delay was 30 seconds, I discounted restarts. If that is not the case, then you would need persistent storage (files or GV+Flush.)


Good morning Seng
 
Thank you Seng and William for the answers.