How can I compare the closing time of the last closed order and the current serverl time??

[Deleted]  

I am still a beginner in MQL4 and I want to only send one order every 15 minutes. To do this I was thinking of comparing the last closed order time with the current server time.

Here is my present code, which sadly does not fullfill my plan:


bool timeSpace;

for(int i=OrdersHistoryTotal();i>=0;i--)                                           
 {
   OrderSelect(i, SELECT_BY_POS,MODE_HISTORY); 
   if ( (OrderSymbol()== Symbol() )
   && (OrderCloseTime() <= (TimeCurrent() - 900) ) )
   {
      timeSpace == true;
   }
   else if (OrdersHistoryTotal() == 0)                                               //in case there are no closed orders yet
   {
      timeSpace == true;
   }
}

if (timeSpace == true)
{
  OrderSend(...
 

You probably should first search the newest order in the history pool, and then check on that..


Currently you are setting timeSpace to true if ANY of the trades is closed 15 min before

 
for(int i=OrdersHistoryTotal();i>=0;i--)                                           
 {
   OrderSelect(i, SELECT_BY_POS,MODE_HISTORY); 
   if ( (OrderSymbol()== Symbol() )
   && (OrderCloseTime() <= (TimeCurrent() - 900) ) )
   {
      timeSpace == true;
   }
   else if (OrdersHistoryTotal() == 0) //in case there are no closed orders yet
   {
      timeSpace == true;
   }
}
  1. The FOR is wrong. If there is one order history it's position is zero, but you try select position 1 which is wrong.
  2. Test your return codes What are Function return values ? How do I use them ? - MQL4 forum
  3. If there is no history orders The IF History == 0 should never execute because the loop should do nothing.
  4. You set timeSpace to true if there are ANY old orders, no ALL old orders.
  5. Use self documenting variable names. timeSpace is meaningless.
bool isOKtoOpen = true; // Assume all old orders or none.
for(int iPos=OrdersHistoryTotal()-1; iPos >= 0; iPos--) 
if(OrderSelect(iPos, SELECT_BY_POS, MODE_HISTORY)
&& OrderSymbol()== Symbol()
// What about magic number?
&& OrderType() <= OP_SELL // Avoid cr/bal forum.mql4.com/32363#325360
                          // https://forum.mql4.com/30708
                          // Never select canceled orders.
   ){
   int secondsSinceClose = TimeCurrent() - OrderCloseTime();
   if (secondsSinceClose <= 900){ isOKtoOpen = false; break; }
   }
}