Auto close after duration script

 

Hi, I am just new to MQL. I learn well by modifying professionally

written code. Right now I want to know how to modify an EA to

automatically close orders that have run a certain (but Variable)

amount of time, such as 4 hours or 2 days. It will, of course,

be the first thing executed in the EA. I am just having trouble

accounting for the time/duration calculation, expecially over

the weekend 2-day gap.

Thanks

Oyster55

==================================================

 
Oyster55:

[...] Right now I want to know how to modify an EA to automatically close orders that have run a certain (but Variable) amount of time, such as 4 hours or 2 days. [...]

Elapsed time is easy. A timestamp in MT4 is simply a number of seconds, and subtracting something like an order's OrderOpenTime() from TimeCurrent() will therefore give you the number of seconds for which an order has been open.

Trading time is very much harder. You need to know your broker's opening hours, which MT4 cannot tell you. So, that information has to be entered manually, e.g. as parameters for the EA, and you then have to do quite a complex adjustment of the open-time calculation to subtract time for each weekend. (If you were trading things like equity CFDs, then you would also have to allow for the market being closed overnight.)
 
jjc:

Trading time is very much harder. You need to know your broker's opening hours, which MT4 cannot tell you. So, that information has to be entered manually, e.g. as parameters for the EA, and you then have to do quite a complex adjustment of the open-time calculation to subtract time for each weekend. (If you were trading things like equity CFDs, then you would also have to allow for the market being closed overnight.)
Surely not. Suppose you are on an H1 chart. The order was opened on bar 6. How long ago was that in trading time? 6 hours (±1, I never get the exact number!) MT4 charts are gapless so you don't have to worry about broker hours or anything. Just count bars on your chart.
 
dabbler:
Surely not. Suppose you are on an H1 chart. The order was opened on bar 6. How long ago was that in trading time? 6 hours (±1, I never get the exact number!) MT4 charts are gapless so you don't have to worry about broker hours or anything. Just count bars on your chart.
For short term calculations, I'd agree with you. But if you start up MT4 after a break of e.g. days, then an EA will potentially start running before the history since your last start has been downloaded. That probably won't matter in this case, because the error will be in the acceptable direction and will shortly be rectified, but it's fundamentally not a reliable way of counting trading time.
 
jjc:
For short term calculations, I'd agree with you. But if you start up MT4 after a break of e.g. days, then an EA will potentially start running before the history since your last start has been downloaded. That probably won't matter in this case, because the error will be in the acceptable direction and will shortly be rectified, but it's fundamentally not a reliable way of counting trading time.
...In addition, if you are doing a calculation such as "4 hours", and you care about the difference between 3:59 and 4:00, then you will potentially be misled by looking at M1 bar history, and the fact that there can be missing M1 bars because no tick occurred during a particular minute. Strictly speaking - and assuming that the history has been fully downloaded - then the bar history tells you "traded" time rather than "trading" time.
 
jjc:
For short term calculations, I'd agree with you. But if you start up MT4 after a break of e.g. days, then an EA will potentially start running before the history since your last start has been downloaded. That probably won't matter in this case, because the error will be in the acceptable direction and will shortly be rectified, but it's fundamentally not a reliable way of counting trading time.

Good point, then life just got ugly :-(

... although one could put an ArrayCopyRates in the EA and wait a few seconds for the charts to update.

 
jjc:
For short term calculations, I'd agree with you. But if you start up MT4 after a break of e.g. days, then an EA will potentially start running before the history since your last start has been downloaded. That probably won't matter in this case, because the error will be in the acceptable direction and will shortly be rectified, but it's fundamentally not a reliable way of counting trading time.

hang on, hang on, hang on ...

Why are we closing this EA down for a couple of days when it is supposed to be managing trades? It obviously can't close a trade if it is not running! Missing M1 bars are fair enough. Perhaps we could count H1 bars, then get closer with M1 bars, but make sure there are enough of them and so forth. I think that would be workable and simpler than trying to figure out broker hours and holidays and such like.

 
dabbler:

Why are we closing this EA down for a couple of days when it is supposed to be managing trades?

...Unforeseen events.

I was thinking of something like the following. This isn't pretty - and may well be buggy - but it's more reliable in some regards and executes faster than querying the bar history. Doesn't handle public holidays or other unusual closures.

   // **********************************************
   // Start by counting the the elapsed time in seconds.
   // Requires start time to be before end time.
   // This figure will potentially include weekend periods 
   // when the broker is closed. (It also includes 
   // public holidays, but we don't account for them.)
   // **********************************************
   int lElapsedTime = dtEndTime - dtStartTime;
   

   // **********************************************
   // Work out the number of weekends between the start time and end time 
   // **********************************************

   // Calculate the number of completed weeks (seconds divided by 604800 in a week)
   int Weekends = (lElapsedTime / 604800);
   int lDoWStart = TimeDayOfWeek(dtStartTime);
   int lDoWEnd = TimeDayOfWeek(dtEndTime);
   
   // Also need to look at day-of-week
   if (lDoWStart > lDoWEnd) {
      // Start time is later day of week than end time (e.g. Thursday through Tuesday)
      // Count of weekends will therefore be one short 
      Weekends++;
      
   } else if (lDoWStart < lDoWEnd) {
      // Start time is earlier day of week than end time (e.g. Wednesday through Friday)
      // Count of weekends will therefore be correct and complete 

   } else {
      // Start and end times are same day of week 
      // The count of weekends will be one short if start time is later in the day than the end time 
      if ((dtStartTime % 86400) > (dtEndTime % 86400)) Weekends++;
   }


   // **********************************************
   // And finally...
   // **********************************************

   // The elapsed time can now be adjusted by the number of seconds 
   // for which the broker is closed at the weekend. For example,
   // 8.30pm on Friday through to 11pm on Sunday would be 181,800

   
   int lTradingTime = lElapsedTime - (Weekends * BrokerWeekendClosureSeconds)
 
jjc:

...Unforeseen events.

I was thinking of something like the following. This isn't pretty - and may well be buggy - but it's more reliable in some regards and executes faster than querying the bar history. Doesn't handle public holidays or other unusual closures.


Thanks for all your efforts, folks. I can see you'all are pretty hardcore and careful about details.

So, I appreciate that. I like the idea of just counting minute-bars (my EA only uses minutes) -

seems simple for me, but I am now concerned if too many are missed. Does anyone have any

idea how many minute-ticks are missed in say, first opening hours of the week? (slowest activity

in my experience)

Oyster55

 
Oyster55:


Thanks for all your efforts, folks. I can see you'all are pretty hardcore and careful about details.

So, I appreciate that. I like the idea of just counting minute-bars (my EA only uses minutes) -

seems simple for me, but I am now concerned if too many are missed. Does anyone have any

idea how many minute-ticks are missed in say, first opening hours of the week? (slowest activity

in my experience)

Oyster55


Another thing. I don't plan on closing the running EA as it's tested on a VPS that's on 24/7

Oy

=======

 
Oyster55:


Another thing. I don't plan on closing the running EA as it's tested on a VPS that's on 24/7

Oy

=======


Except when the people who run the VPS run maintenance and then they sometimes close you down.
Reason: