Order failed to open. Error: 0 - page 2

 

It seems that the ea already has the function of retry to open an order "NumberOfTries", but still can't make it work properly.

I just want to make this ea opens the order at the moment it should, and if can't open then retry immediately, without having to wait until next iteration.


I've changed two "breaks" functions and add errors to continue the loop. Is this correct?.

From this:

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
                       {
                        if(err==4 || err==137 ||err==146 || err==136) //Busy errors
                          {
                           Sleep(5000);
                           continue;
                          }
                        else //normal error
                          {
                           break;
                          }
                       }
                    }
                  break;

To this:

case OP_BUY:
                  for(c=0;c < NumberOfTries;c++)
                    {
                     ticket=OrderSend(Symbol(),OP_BUY,pLots,Ask,sp,StopLong(Bid,sl),TakeLong(Ask,tp),pComment,pMagic,pExpiration,pColor);
                     //err=GetLastError();
                     if(err==0)
                       {
                        continue;
                       }
                     else
                       {
                       if(err==4 || err==137 ||err==146 || err==136 || err==0 || err==138 || err==4110) //Busy errors
                          {
                           continue;
                          }
                        else //normal error
                          {
                           continue;
                          }
                       }
                    }
                  break;
 
af1:

It seems that the ea already has the function of retry to open an order "NumberOfTries", but still can't make it work properly.

I just want to make this ea opens the order at the moment it should, and if can't open then retry immediately, without having to wait until next iteration.


I've changed two "breaks" functions and add errors to continue the loop. Is this correct?.

You will probably end up with many many orders that you didn't want . . .
 
  1. Did you adjust slippage for 4/5 digit brokers
  2. Did you call RefreshRates() after sleep() and between multiple server calls per start()?
 

aF1 did you consider making a separate function for error handling ? I think you should read the Book and study the example of an error handling function you will see the author deals with error 0. Which in fact is not a real error. GetLastError() will return zero when there was no error, in other words zero is GetLastError's way of saying everything is fine. You are having an issue with it because you are calling GetLastError() even if the OrderSend() works.

Also ask yourseslf do you really need to get the ticket number and put it in the variable ticket ? Or do you just want to do the OrderSend(), and then move on unless there was an error ?

if(OrderSend(Symbol(), OP_BUY,etc,etc,etc ) <= 0) //a valid ticket number was not returned
 {//do error stuff, call GetLastError() etc
 }else
 { // ordersend must have worked so continue with the rest of your code. 

or,

if(OrderSend(Symbol(), OP_BUY,etc,etc,etc ) >=0)   //a valid ticket number was returned
 {//everything is fine so continue with code 
 }else //ordersend didnt work.
 {do error stuff

So if a valid ticket is returned it will have a number > zero and therfore GetLastError() is not called when there was no error.

Also study the example of an open orders function and understand how the author passes errors from that function to the error handling function for processing, also look at how he puts the OrderSend() in a while loop so the order can retry after the error handling function attempts to remedy the problem.

 
RaptorUK:
You will probably end up with many many orders that you didn't want . . .


Yes, that's true.
 
WHRoeder:
  1. Did you adjust slippage for 4/5 digit brokers
  2. Did you call RefreshRates() after sleep() and between multiple server calls per start()?


I did call RefreshRates() after sleep(), and between server calls I guess not. Let me try.

 
SDC:

...do you really need to get the ticket number and put it in the variable ticket ? Or do you just want to do the OrderSend(), and then move on unless there was an error ?


Hi SDC, thanks for your time.

I just want to Open the order, but if for any reason (error) the order didn't open, then immediately try again, until opens, then break. That's it.

 
af1:


Hi SDC, thanks for your time.

I just want to Open the order, but if for any reason (error) the order didn't open, then immediately try again, until opens, then break. That's it.

If you really want to know the proper way to handle these errors . . . it tells you here: Trading errors for error 138 do this:

The requested price has become out of date or bid and ask prices have been mixed up. The data can be refreshed without any delay using the RefreshRates function and make a retry. If the error does not disappear, all attempts to trade must be stopped, the program logic must be changed.

 

af1:
Hi SDC, thanks for your time.

I just want to Open the order, but if for any reason (error) the order didn't open, then immediately try again, until opens, then break. That's it.

Just put it in a while loop. You can put a counter in there to limit the amount of retries.. prevents forever looping, and in your error handling do like Raptor just said, refresh rates for 138, do other things for other errors like it shows in the book.

int counter = 0;
while ( OrderSend( Symbol(), OP_BUY ,,,,,) <=0 )
{if(counter <5)
 {counter++;
  do error stuff;
 }else
 {break;
}}
 

Ok, at this moment I'm lost. I've been trying some changes in the code, but this is what I've seen so far:

  • Adding RefreshRates: helps but the ea only tries one time to open the order. Then retry again at the next iteration. (I would like to retry immediately).
  • No calling GetLastError: helps to solve Error: 0. But now other errors appears (138, 4110, 4111).
  • While loop: I think this is the answer to open an order and retry until opens. I just don't know how to add correctly to the code.
Someone could help me to add this "while" function. I really appreciate it.


Or in other words, let's forget all the errors. I just would like the ea open the order as fast as possible, if there is an error, then retry immediately at the next tick.

Reason: