problem with my ea. ontick or timer?

 

hello there,

 I'm building my ea with a mql programmer. I'm a programmer also, but I know vanilla javascript, not mql. Things are not going the way i expected, the ea was made but it works in a certain way, that according with the mql programmer, is an issue with the mql4 language itself, how the API works. I will describe what i wanted the ea to do, the problem that is happening and  the full code i have from the EA, and if you folks could share some light i would really appreciate that

ea specs: 

opening(the first order) and closing orders are manual

-ea should initialise and listen/wait for/idle (not sure the best term) an order to get closed.

-once an order get closed, ea should open a new BUY or SELL with the same lot size. if a sell was closed, ea should place a buy and the other way around too.

 

The problem that is happening, is that EA is taking too long to open a new order once 1 gets closed, sometimes even 3 or 4 seconds! It should open a new order immediately after one gets closed. The developer says the EA is working on a tick basis and the code is the best he could do. What you say?

#property copyright ""
#property link      ""



extern bool EA_work=true;


//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+



datetime starttime;
int init()
  {
    //set start time
    starttime=TimeCurrent();
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {

   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+


int start()
  {
    //checking status EA
    if(EA_work)  CheckOpenNewTrade();
    else return(0);
   return(0);
  }
//+------------------------------------------------------------------+
//function looking for open new trade 
void CheckOpenNewTrade()
{
  //get number ticket of last closed order
  int last=LastClosed();
  if(last!=0)
  {
    if(!Opened(last))//checking if no order open for last closed order
    {
      if(OrderSelect(last,SELECT_BY_TICKET))
      {
        int ticket=0;
        double lot=OrderLots();//get lot size
        switch(OrderType())//depending order type
        {
          case 0 :
          {
            ticket=OrderSend(Symbol(),OP_SELL,lot,Bid,20,0,0,DoubleToStr(last,0),0,0,Red);//send order open sell
            break;
          }
          case 1 :
          {
            ticket=OrderSend(Symbol(),OP_BUY,lot,Ask,20,0,0,DoubleToStr(last,0),0,0,Blue);//send open order buy
            break;          
          }
        }       
      }
    }
  }
  return;
}
//function checking for no open order by ticket, return true if order was opened false if not
bool Opened(int ticket)
{
  bool make=false;
  string ttt=DoubleToStr(ticket,0);
  for(int i=0;i<OrdersTotal();i++)
  if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
  if(OrderSymbol()==Symbol()&&OrderType()<=OP_SELL)
  {
    if(OrderComment()==ttt)make=true;
  }
  for(i=0;i<OrdersHistoryTotal();i++)
  if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))
  if(OrderSymbol()==Symbol()&&OrderType()<=OP_SELL)
  {
    if(OrderComment()==ttt)make=true;
  }
  return(make);
}
//function find last closed order, return number of ticket last closed order
int LastClosed()
{
  int tic=0;
  datetime ctt=0;
  for(int i=0;i<OrdersHistoryTotal();i++)
  if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))
  if(OrderSymbol()==Symbol()&&OrderType()<=OP_SELL)
  {
    if(OrderCloseTime()>ctt&&OrderCloseTime()>starttime)
    {
      ctt=OrderCloseTime();
      tic=OrderTicket();
    }
  }
  return(tic);
}
 

This code is not optimal. How many orders do you have in your history ?

I test it with 70 orders on history data, it took 328 ms.

2014.07.28 19:45:06.422    '9538975': order was opened : #1369570736 sell 0.10 EURUSD at 1.34389 sl: 0.00000 tp: 0.00000
2014.07.28 19:45:06.203    '9538975': order sell market 0.10 EURUSD sl: 0.00000 tp: 0.00000
2014.07.28 19:45:06.094    '9538975': order #1369570620 buy 0.10 EURUSD at 1.34413 closed due stop-loss at price 1.34392

 

Limit your trade history to a shorter period.

When the open time delay is still 3 to 4 seconds instead of 0.3 measured by Angevoyegeur, then there is high probability the broker is interfering. Your coder did not make any major mistake that would involve such delay.

 
DeepThought:

Limit your trade history to a shorter period.

When the open time delay is still 3 to 4 seconds instead of 0.3 measured by Angevoyegeur, then there is high probability the broker is interfering. Your coder did not make any major mistake that would involve such delay.


It's not necessarily the broker. From the posted code, you can see it uses a loop (several in facts) on all history (on each tick), so with my 70 orders it's not a problem. But I asked how many orders fartfx has in his history, as with 7000, 70000, etc...it can become a serious problem for performance.
 

I haven't examined the code closely, but if looping through the history is a problem, it may be an idea to consider storing the open ticket number(s) in a global scope variable or array(if more than one).

That way, you can select by ticket and check if an order has closed or not 

Reason: