Download MetaTrader 5

Order failed to open. Error: 0

To add comments, please log in or register
af1
283
af1  

Hi. I'm testing this ea on demo, but I'm frequently getting the "Error: 0". And when this error shows up, the orders failed to open. Then, the ea retry to open again, but at the next iteration (5m, 15m or whichever the timeframe is). Sometimes opens, sometimes retries, and retries until finally open the order.

I've been searching for solutions, but still can't figure out.

Any suggestion?
Thanks

Files:
ilan1.4.mq4 30 kb
Simon Gniadkowski
Moderator
18024
Simon Gniadkowski  
af1:

Hi. I'm testing this ea on demo, but I'm frequently getting the "Error: 0". And when this error shows up, the orders failed to open. Then, the ea retry to open again, but at the next iteration (5m, 15m or whichever the timeframe is). Sometimes opens, sometimes retries, and retries until finally open the order.

I've been searching for solutions, but still can't figure out.

Any suggestion?
Thanks

Add more meaningful error messages so you can track down where in th code you are getting the error, there re several instances of "Error:" in the code. Instead use something like "Opening OP_BUYSTOP Error:" so you know specifically where the error came from.
af1
283
af1  
RaptorUK:
Add more meaningful error messages so you can track down where in th code you are getting the error, there re several instances of "Error:" in the code. Instead use something like "Opening OP_BUYSTOP Error:" so you know specifically where the error came from.


Hi Raptor. I've changed from "Error:" to "Error1:", "Error2", "Error3:" and "Error4" to identify. So when the ea shows the error, sometimes is "Error3" and sometimes is "Error4", but in both cases the order failed to open.

Here is the code line where appears "Error3" and "Error4".

iLots=fGetLots(OP_SELL);
              if(iLots>0)
              {//#
               ticket=OpenPendingOrder(OP_SELL,iLots,SellLimit,slip,SellLimit,0,0,EAName+"-"+NumOfTrades,MagicNumber,0,HotPink);
               if(ticket<0){Print(iLots,"Error3: ",GetLastError()); return(0);
               }
               LastBuyPrice=FindLastBuyPrice();
               NewOrdersPlaced=true;
              }//#
           }
           else
           {
            iLots=fGetLots(OP_BUY);
              if(iLots>0)
              {//#      
               ticket=OpenPendingOrder(OP_BUY,iLots,BuyLimit,slip,BuyLimit,0,0,EAName+"-"+NumOfTrades,MagicNumber,0,Lime);
               if(ticket<0){Print(iLots,"Error4: ",GetLastError()); return(0);}
               LastSellPrice=FindLastSellPrice();
               NewOrdersPlaced=true;
              }//#
           }
        }
      if(ticket>0) expiration=CurTime()+MaxTradeOpenHours*60*60;
      TradeNow=false;
     }
Simon Gniadkowski
Moderator
18024
Simon Gniadkowski  
af1:


Hi Raptor. I've changed from "Error:" to "Error1:", "Error2", "Error3:" and "Error4" to identify. So when the ea shows the error, sometimes is "Error3" and sometimes is "Error4", but in both cases the order failed to open.

Here is the code line where appears "Error3" and "Error4".

Unfortunately the code is badly written and prevents the GetLastError() returning the real error because GetLastError() has already been called during the OpenPendingOrder() function . . .

If you want to know why the pending order is failing add these two lines in the OpenPendingOrder() function . . .

              case OP_BUY:
                  for(c=0;c < NumberOfTries;c++)
                    {
                     RefreshRates();
                     ticket=OrderSend(Symbol(),OP_BUY,pLots,Ask,sp,StopLong(Bid,sl),TakeLong(Ask,tp),pComment,pMagic,pExpiration,pColor);
                     err=GetLastError();
                     if(err==0)
                       {
                        break;
                       }
                     else
                       {
                       Print("Opening not-pending order failed, error# ", err, " size: ", DoubleToStr(pLots, 3), " Ask: ", DoubleToStr(Ask, Digits), " SL: ",  //  add this line
                        DoubleToStr(StopLong(Bid,sl), Digits), " TP: ", DoubleToStr(TakeLong(Ask,tp), Digits) );                                               //  add this line

                        if(err==4 || err==137 ||err==146 || err==136) //Busy errors
                          {
                           Sleep(5000);
                           continue;
                          }
                        else //normal error

af1
283
af1  
RaptorUK:

Unfortunately the code is badly written and prevents the GetLastError() returning the real error because GetLastError() has already been called during the OpenPendingOrder() function . . .

If you want to know why the pending order is failing add these two lines in the OpenPendingOrder() function . . .


I get these messages in the Log:

21:07:45 | Ilan1.4: 0.1Error4: 0
21:07:45 | Ilan1.4: Opening not-pending order failed, error# 138 size 0.100

Simon Gniadkowski
Moderator
18024
Simon Gniadkowski  
af1:


I get these messages in the Log:

21:07:45 | Ilan1.4: 0.1Error4: 0
21:07:45 | Ilan1.4: Opening not-pending order failed, error# 138 size 0.100

Looks like you need to add a RefreshRates(); at the start of that function, the Ask price is out of date . . .
af1
283
af1  
RaptorUK:
Looks like you need to add a RefreshRates(); at the start of that function, the Ask price is out of date . . .

I add RefreshRates(); to these code lines. But I'm still getting the error3 and error4.


if(iLots>0)
              {//#
               RefreshRates();
               ticket=OpenPendingOrder(OP_SELL,iLots,Bid,slip,Ask,0,0,EAName+"-"+NumOfTrades,MagicNumber,0,HotPink);
               if(ticket<0){Print("Error1: ",GetLastError()); return(0);}
               LastSellPrice=FindLastSellPrice();
               TradeNow=false;
               NewOrdersPlaced=true;
              }//#
if(iLots>0)
                 {//#
                 RefreshRates();
                  ticket=OpenPendingOrder(OP_BUY,iLots,Ask,slip,Bid,0,0,EAName+"-"+NumOfTrades,MagicNumber,0,Lime);
                  if(ticket<0)
                  {Print("Error2: ",GetLastError()); return(0);}
                  LastBuyPrice=FindLastBuyPrice();
                  TradeNow=false;
                  NewOrdersPlaced=true;
                 }//#
case OP_SELL:
                  for(c=0;c < NumberOfTries;c++)
                    {
                    RefreshRates();
                     ticket=OrderSend(Symbol(),OP_SELL,pLots,Bid,sp,StopShort(Ask,sl),TakeShort(Bid,tp),pComment,pMagic,pExpiration,pColor);
                     err=GetLastError();
                     if(err==0)
                       {
                        break;
                       }
                     else
                       {
                        if(err==4 || err==137 ||err==146 || err==136) //Busy errors
                          {
                           Sleep(5000);
                           continue;
                          }
                        else //normal error
                          {
                           break;
                          }
                       }
                    }
                  break;
              }

            return(ticket);
           }
whroeder1
15586
whroeder1  
 ticket=OrderSend(Symbol(),OP_SELL,pLots,Bid,sp,StopShort(Ask,sl),TakeShort(...
 err=GetLastError();
 if(err==0)
This is wrong. Do NOT call GetLastError() unless ticket is negative. A positive ticket means it worked and GetLastError is meaningless.
Simon Gniadkowski
Moderator
18024
Simon Gniadkowski  
af1:

I add RefreshRates(); to these code lines. But I'm still getting the error3 and error4.

Add it to the correct place and it may well help . . . why add it when the function does an OP_SELL when you are interested in an OP_BUY ?
af1
283
af1  
RaptorUK:
Add it to the correct place and it may well help . . . why add it when the function does an OP_SELL when you are interested in an OP_BUY ?


I'm using this ea in two charts, one for Only Buy and one for Only Sell orders. This help me to manage in a better way both sides. (lots, pipstep, etc.)

af1
283
af1  
WHRoeder:
This is wrong. Do NOT call GetLastError() unless ticket is negative. A positive ticket means it worked and GetLastError is meaningless.

Hi WHRoeder. Thanks again for your time and sharing your knowledge.

I put //GetLastError() to don't call this function and it seems that the error: 0 is gone. Now I'm getting the error 138 (not always, but frequently). This means reqoute. How can avoid requotes?. I'm thinking that a loop (retrying until open the order) could solve this, but I really don't know how to add a loop. Could you give me some tips in this matter?.

123
To add comments, please log in or register