Download MetaTrader 5

Code help needed with a simple MQ4 Trade Timer

To add comments, please log in or register
Download MetaTrader 5 mobile version and trade from anywhere in the world!
Sizzles
274
Sizzles 2016.08.30 06:06 

Hello PPL,

I was hoping one of you wise coders could help me out here.

I need a timer expert that will automatically close trades, across multiple currency pairs after each trade has been open for 4.5 Hours.

I found the script below over at https://www.mql5.com/en/forum/130018 which is nice but it only works for one pair.

Can any of you modify it to work on multiple pairs please?

///+---------------------------------------------------------------------+
//|                                                       TIMEKEEPER.mq4 |
//|                                                                      |
///+---------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
//----
   return(0);
  }
//====================================================================
//====================================================================
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
//----
   return(0);
  }
//====
//+------------------------------------------------------------------+
//| |EXPERT
//+------------------------------------------------------------------+
int start()
   {
int maxDuration = 15 * 60; 
for(int pos = OrdersTotal()-1; pos >= 0 ; pos--) if (
    OrderSelect(pos, SELECT_BY_POS)            // Only my orders w/
&&  OrderSymbol()== "EURUSD"){               // and period and symbol
    int duration = TimeCurrent() - OrderOpenTime();
    if (duration >= maxDuration)
         OrderClose( OrderTicket(), OrderLots(), OrderClosePrice(),
                     3*Point);
}      return(0);
      }

//+------------------------------------------------------------------+
///+---------------------------------------------------------------------+
//|                                                       TIMEKEEPER.mq4 |
//|                                            Thanks to the MQL Forum ! |
///+---------------------------------------------------------------------+
Closing opened order after a definite number of seconds or minutes (mat) - MQL4 forum
Closing opened order after a definite number of seconds or minutes (mat) - MQL4 forum
  • www.mql5.com
Closing opened order after a definite number of seconds or minutes (mat) - MQL4 forum
Ernst Van Der Merwe
4731
Ernst Van Der Merwe 2016.08.30 09:52  
string symbols[]={"EURUSD","GBPUSD","AUDUSD","USDJPY"};
//---
OrdersCloseByTimer(4.5,symbols);
//---
bool OrdersCloseByTimer(double hours,const string &symbol[])
  {
   int total=OrdersTotal();
   int size=ArraySize(symbol);
   int maxDuration=int(hours * 3600); 
//---
   for(int pos=total-1;pos>=0;pos--)
      for(int i=0;i<size;i++)
        {
         if(OrderSelect(pos, SELECT_BY_POS)
         && OrderType()<=OP_SELL             // Only my orders w/
         && OrderSymbol()==symbol[i])        // period and symbol
           {      
            int duration=int(TimeCurrent()-OrderOpenTime());
            if(duration>=maxDuration)
               if(!OrderClose( OrderTicket(), OrderLots(), OrderClosePrice(),30))
                  return(false);
           }
         }
//---
   return(true);
  }  
Sizzles
274
Sizzles 2016.08.30 13:29  

Thank you so much for helping me out Ernst.

I am seeing an error on the line below,

OrdersCloseByTimer(4.5,symbols);

'OrdersCloseByTimer' - declaration without type

Ernst Van Der Merwe
4731
Ernst Van Der Merwe 2016.08.30 15:17  
Sizzles:

Thank you so much for helping me out Ernst.

I am seeing an error on the line below,

'OrdersCloseByTimer' - declaration without type

It has to be placed in the start() function.

string symbols[]={"EURUSD","GBPUSD","AUDUSD","USDJPY"};
//---
int init()
  {
//----
//----
   return(0);
  }
//---
int start()
  {
   OrdersCloseByTimer(4.5,symbols);
//---
   return(0);
  }
To add comments, please log in or register