Cosing all trades at pre-defined time of day

 

Hi champs,

This is probably a newbies question and please accept my appologies if it is, but I've been searching for a couple of days now and couldn't find any working code to close all my open orders at a certain time of the day.

Below is an extract of my EA in MT4 and I've tried several pieces of code to close orders, the latest one seemed straight forward but did not work:

  input int  TradeHour=21;
  input int  TradeMinutes=00;

void OnTick()
{
// Here I put my code to buy/sell on my strategy


  if ((TradeHour==Hour())&&(TradeMinutes==Minute()))
  bool close = OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),10,clrRed);

}

Any hint on what is wrong with my code?

Many thanks in advance.

Steve

 
Please edit your post and use the code button (Alt+S) when pasting code.

EDIT your original post, please do not just post the code correctly in a new post.

Topics concerning MT4 and MQL4 have their own section.
In future please post in the correct section.
I will move your topic to the MQL4 and Metatrader 4 section.

 if ((TradeHour==Hour())&&(TradeMinutes==Minute()))
  bool close = OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),10,clrRed);

You must select the order before you can use OrderTicket() etc.

 
  1. Please edit your (original) post and use the CODE button (or Alt+S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum (2019)
              Messages Editor

  2.   bool close = OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),10,clrRed);

    MT4: You can not use any Trade Functions until you first select an order.

  3.  if ((TradeHour==Hour())&&(TradeMinutes==Minute()))
    What if there is no tick that minute? There can be minutes between ticks during the Asian session, think M1 chart. Larger charts, think weekend, market holiday (country and broker specific), requires knowledge of when your broker stops and starts (not necessary the same as the market.)
              "Free-of-Holes" Charts - MQL4 Articles (2006)
              No candle if open = close ? - MQL4 programming forum (2010)
    if( tradeHour*3600+TradeMinutes*60 <= time() ) // if ((TradeHour==Hour())&&(TradeMinutes==Minute()))

              Find bar of the same time one day ago - MQL4 programming forum #1 & #6 (2017)

 
Keith Watford #:
Please edit your post and use the code button (Alt+S) when pasting code.

EDIT your original post, please do not just post the code correctly in a new post.

Topics concerning MT4 and MQL4 have their own section.
In future please post in the correct section.
I will move your topic to the MQL4 and Metatrader 4 section.

You must select the order before you can use OrderTicket() etc.

Many thanks Keith, will apply the rules from now on.
 
William Roeder #:
  1. Please edit your (original) post and use the CODE button (or Alt+S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum (2019)
              Messages Editor

  2. MT4: You can not use any Trade Functions until you first select an order.

  3. What if there is no tick that minute? There can be minutes between ticks during the Asian session, think M1 chart. Larger charts, think weekend, market holiday (country and broker specific), requires knowledge of when your broker stops and starts (not necessary the same as the market.)
              "Free-of-Holes" Charts - MQL4 Articles (2006)
              No candle if open = close ? - MQL4 programming forum (2010)

              Find bar of the same time one day ago - MQL4 programming forum #1 & #6 (2017)

Many thanks William,

Really appreciate your feedback!

 

And here is the final code, working fine apparently :)

Thanks guys !

 if ((TradeHour==Hour())&&(TradeMinutes>=Minute()))                                                              // Closing at preefined time
  for (int i=OrdersTotal()-1; i>=0; i--) {                                                                                      // Closing at preefined time  
  if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))                                                               // Closing at preefined time
  bool close = OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),10,clrYellow);}           // Closing at preefined time
 
Stefano Diamante #: And here is the final code, working fine apparently :)
 if ((TradeHour==Hour())&&(TradeMinutes>=Minute()))                                                              // Closing at preefined time

You've ignored #2.3. Set TradeMinutes to 59 and the hour to the Asian session, and watch it.

 
Stefano Diamante #:

And here is the final code, working fine apparently :)

Thanks guys !

Watch this:

https://www.youtube.com/watch?v=SrOBP4QYpSo&t=1160s

How to Automate Closing All MT4 Trades at a Scheduled Time | Boost Your Trading Efficiency
How to Automate Closing All MT4 Trades at a Scheduled Time | Boost Your Trading Efficiency
  • 2023.02.23
  • www.youtube.com
If you're an experienced trader who uses the popular MetaTrader platform, you may know that closing all trades manually can be a tedious and time-consuming t...