OrderClose error 4051

 

Good morning, this ea when I try to do the backtest returns the error 4051 in the Diary. Thanks.

extern double percentage = 2.0;
extern int bars = 2, stop = 100;
double average_swing,entry;
int ticket,bar;

void OnTick()
  {
  if(!newbar()) return;
  if(ticket!=0)
    {
    OrderSelect(ticket,SELECT_BY_TICKET);
    if(OrderType()==OP_BUYSTOP)
      {
      OrderDelete(ticket);
      ticket = 0;
      }
    }
  average_swing = ((High[4]-Open[4])+(High[3]-Open[3])+(High[2]-Open[2])+(High[1]-Open[1]))/4;
  if(Close[0]<Close[3])
    {
    if(ticket!=0)
      {
      OrderSelect(ticket,SELECT_BY_TICKET);
      if(OrderType()==OP_BUY && OrderCloseTime()==0) return;
      }
    entry = Open[0]+average_swing*percentage;
    ticket = OrderSend(Symbol(),OP_BUYSTOP,1,entry,2,entry-stop*10*Point,0);
    bar = Bars(Symbol(),PERIOD_D1);
    }
  if(Bars(Symbol(),PERIOD_D1)>=bar+bars)
    {
    if(ticket!=0)
      {
      OrderSelect(ticket,SELECT_BY_TICKET);
      if(OrderType()==OP_BUY && OrderCloseTime()==0) OrderClose(ticket,OrderLots(),Bid,2);
      }
    }
  }

bool newbar()
  {
  static datetime lastbar;
  datetime currentbar = Time[0];
  if(lastbar != currentbar)
    {
    lastbar = currentbar;
    return true;
    }
  else return false;
  }
 
  1. GiorgioLauria: when I try to do the backtest returns the error 4051 in the Diary. 

    Do you really expect an answer? There are no mind readers here and our crystal balls are cracked.

    Use the debugger or print out your variables, including _LastError and prices and find out why.

  2.     OrderSelect(ticket,SELECT_BY_TICKET);
        if(OrderType()==OP_BUYSTOP)
          {
          OrderDelete(ticket);
    You don't test if the ticket is in history, maybe it has already been deleted previously.

  3. EAs must be coded to recover. If the power fails, OS crashes, terminal or chart is accidentally closed, on the next tick, any static/global ticket variables will have been lost. You will have an open order but don't know it, so the EA will never try to close it, trail SL, etc. How are you going to recover? Use a OrderSelect loop to recover, or persistent storage (GV+flush or files) of ticket numbers required.

 

You need to check the return value of OrderSend for errors. If ticket < 0 means an error occurred, and on the next OnTick you test if ticket != 0, what if it's -1 now?

Further you need to normalize the order price with NormalizeDouble(entry,_Digits) same goes for SL.

 
lippmaje:

You need to check the return value of OrderSend for errors. If ticket < 0 means an error occurred, and on the next OnTick you test if ticket != 0, what if it's -1 now?

Further you need to normalize the order price with NormalizeDouble(entry,_Digits) same goes for SL.

Thank you very much, I solved the problems and strangely the strategy works quite well.
 
GiorgioLauria:
Thank you very much, I solved the problems and strangely the strategy works quite well.

It's interesting that you got this error on OrderClose while OrderSend succeeded. Maybe it was just this invalid ticket -1 that you tried to close.

Always check the return of OrderSend:

ticket = OrderSend(... arguments);
if(ticket<0)
  {
   Print("OrderSend failed, error: ",GetLastError());
   ... some recovery what ever
   return;
  }
 
lippmaje:

It's interesting that you got this error on OrderClose while OrderSend succeeded. Maybe it was just this invalid ticket -1 that you tried to close.

Always check the return of OrderSend:

Thanks again, I'm not expert in this field, because I began not even a month ago, I studied the mql4 book and now I'm starting to create my first ea.
Reason: