Thread: MQL4 Guide
View Single Post
  #18 (permalink)  
Old 03-24-2008, 07:21 PM
RickD's Avatar
RickD RickD is offline
Member
 
Join Date: Jan 2006
Location: Eastern Europe
Posts: 52
RickD is on a distinguished road
Quote:
Originally Posted by wonderkey
Hi RickD,

many thanks for being so helpful here. You couldnīt be so kind to introduce the solution of the following problem to me?

My EA sometimes gets stopped out in a winning trade and a few seconds later it sets up the same trade once again, which is not only senseless but very often results in losses. How can I tell the EA to wait with a new order 1 hour, or any other time?

Thanks in advance!

-wonderkey
Hi,

Select the last order and don't trade some time.

Code:
int MinTimeSec = 60; //no trade 60 seconds after last close
int Magic = ...

void start()
{
  ...

  if (LastOrderSelect(MODE_HISTORY, OP_BUY, OP_SELL))
  {
    if (TimeCurrent() - OrderCloseTime() < MinTimeSec) return;
  }

  ...
}


bool LastOrderSelect(int pool, int type1, int type2 = -1)
{
  datetime tm = -1;
  int ticket = -1;

  if (pool == MODE_TRADES)
  {
    int cnt = OrdersTotal();
    for (int i=0; i < cnt; i++) 
    {
      if (!OrderSelect(i, SELECT_BY_POS, pool)) continue;
      if (OrderSymbol() != Symbol()) continue;
      if (OrderMagicNumber() != Magic) continue;
        
      int type = OrderType();
      if (type == type1 || type == type2)
      {
        if (OrderOpenTime() > tm)
        {
          tm = OrderOpenTime();
          ticket = OrderTicket();
        }
      }
    }
  
    return (OrderSelect(ticket, SELECT_BY_TICKET));
  }

  if (pool == MODE_HISTORY)
  {
    cnt = OrdersHistoryTotal();
    for (i=0; i < cnt; i++) 
    {
      if (!OrderSelect(i, SELECT_BY_POS, pool)) continue;
      if (OrderSymbol() != Symbol()) continue;
      if (OrderMagicNumber() != Magic) continue;
        
      type = OrderType();
      if (type == type1 || type == type2)
      {
        if (OrderCloseTime() > tm)
        {
          tm = OrderCloseTime();
          ticket = OrderTicket();
        }
      }
    }
  
    return (OrderSelect(ticket, SELECT_BY_TICKET));
  }
    
  return (false);
}
Reply With Quote