Download MetaTrader 5

Close orders after time

To add comments, please log in or register
Interested in earning money via MQL5 Cloud Network? Enter your profile!
skyblazer
45
skyblazer 2016.11.23 09:38 

Hey, I´ve been searching quite some time now but didnt find anything, so I hope anyone can help me here

 

I want to close an order after 24 Hours, but it should be at the end of the current bar(Dailyperiod), not at the start of the new one, in case of gaps.

 And I dont want to close all open trades, I want the ea to specifiy which trade´s open time is= 24hours 

Thanks in advance

honest_knave
Moderator
2220
honest_knave 2016.11.23 09:49  
skyblazer:

Hey, I´ve been searching quite some time now but didnt find anything, so I hope anyone can help me here

 

I want to close an order after 24 Hours, but it should be at the end of the current bar(Dailyperiod), not at the start of the new one, in case of gaps.

 And I dont want to close all open trades, I want the ea to specifiy which trade´s open time is= 24hours 

Thanks in advance

Working with the idea of the end of a bar is difficult, because you never know which tick will be the last one in for that bar. Workarounds include using timers and/or various time functions, but they are all just that - workarounds and prone to issues. You end up balancing how close to the line you want to push it (milliseconds? seconds? minutes?) versus the risk of missing the end of the bar.

I am a little confused - do you want the orders closed at the end of the D1 bar in which the order was placed, or after 24 hours of being opened?

Keith Watford
Moderator
9624
Keith Watford 2016.11.23 09:49  
You can't determine the end of the current bar, you only know that the bar is closed when a new bar opens.
Keith Watford
Moderator
9624
Keith Watford 2016.11.23 09:56  

If you wanted to close at 5 minutes before Midnight (assuming that there is a tick within the last 5 minutes)


   datetime NextMidnight=TimeCurrent() -(TimeCurrent()%(PERIOD_D1*60))+PERIOD_D1*60;
   datetime CloseTime=NextMidnight-300;
   if(TimeCurrent()>=CloseTime)
      {
      //code to close trades
      }
but it is not totally clear what you want
skyblazer
45
skyblazer 2016.11.23 10:05  
honest_knave:

Working with the idea of the end of a bar is difficult, because you never know which tick will be the last one in for that bar. Workarounds include using timers and/or various time functions, but they are all just that - workarounds and prone to issues. You end up balancing how close to the line you want to push it (milliseconds? seconds? minutes?) versus the risk of missing the end of the bar.

I am a little confused - do you want the orders closed at the end of the D1 bar in which the order was placed, or after 24 hours of being opened?

I want to close it at the end of the D1 bar. That may be then 23:59:59 if possible
whroeder1
14519
whroeder1 2016.11.23 15:03  
skyblazer: I want to close it at the end of the D1 bar. That may be then 23:59:59 if possible

What part of their posts about "end of a bar is difficult" was unclear? Closing at 23:59:59 is not possible.

There might be a tick in the last 5 minutes, there might not. There is a 99.99% probably that there will not be in the last second.

Even if it was, it takes time to close an order (live). It's likely that the order will be closed after midnight due to transmission delays, server delays, possible disconnects.

skyblazer
45
skyblazer 2016.11.23 19:17  

Well, 

 What if i want to close it, at the beginning of the new bar?

whroeder1
14519
whroeder1 2016.11.23 19:24  
skyblazer What if i want to close it, at the beginning of the new bar?
What about it?
To add comments, please log in or register