OrderSend return the ErrorCode "0" and Order didn't created.

[Deleted]  

in the Demo account it works, but in my live account, most times show me error. i don't know what's problem for this EA

Welcome any advice for this EA

Thanks

Error:

USDCAD,M1: Order Buy faild!ErrorCode:0 Close:1.0227 Stoploss:15 Open:1.0239 Stop:1.0224 Profit:1.0269

Source Code


while (....)
{
if(buyticket <=0 && IsTradeAllowed()){
      RefreshRates();
      double buyclose = Close[0];
      if (IsTradeAllowed()){
         buyticket = OrderSend(Symbol(),OP_BUYSTOP,Lots,(buyclose+Level*point),slippage,(buyclose+Level*point) -StopLoss*point,(buyclose+Level*point) +TakeProfit*point,"comment1" ,0,0,Green);
         if (buyticket <=0){
            if ( GetLastError() == 146){
               buyticket = OrderSend(Symbol(),OP_BUYSTOP,Lots,(buyclose+Level*point),slippage,(buyclose+Level*point) -StopLoss*point,(buyclose+Level*point) +TakeProfit*point,"comment1" ,0,0,Green);
            }
            Print(TimeToStr(CurTime(),TIME_SECONDS)," Order Buy failed! ErrorCode:" ,GetLastError()," Close:",buyclose," Stoploss:",StopLoss," Open:",(buyclose+Level*point)," Stop:",(buyclose+Level*point) -StopLoss*point," Profit:",(buyclose+Level*point) + TakeProfit*point);
         }
      }
   }

   //....exit
}


bernd
990
bernd  

Although GetLastError() has this innocent looking name starting with "Get" it has side effects!


It will reset the error number, the next call will return 0. You can only call it once. do it like:


err = GetLastError();
if (err == 146){
   ...
}
Print(err);
[Deleted]  

Thanks, i got it . i understand the Print(.... ) have bug. :( .

bernd
990
bernd  
pclovec:

Thanks, i got it . i understand the Print(.... ) have bug. :( .

why :( ?

it is not the Print(), it is you calling GetLastError() a second time (inside the print), so it will always give 0 as error number and it won't show the real error that happened. Do it like in my example and you will see the correct error number.

[Deleted]  

Sorry for my replay, i didn't expect it, i just want to say this line have problem,(first time call GetLastError() function after the system reset the error code to "0")

Thanks

William Roeder
25313
William Roeder  
  1. As for the why it fails, you buy at the Ask, Open[] close[] are the Bid.
  2. You also must adjust TP, SL, and slippage for 5 digit brokers. Demo and live may be different. Also never any slippage on demo.
    //++++ These are adjusted for 5 digit brokers.
    double  pips2points,    // slippage  3 pips    3=points    30=points
            pips2dbl;       // Stoploss 15 pips    0.0015      0.00150
    int     Digits.pips;    // DoubleToStr(dbl/pips2dbl, Digits.pips)
    int init(){
        if (Digits == 5 || Digits == 3){    // Adjust for five (5) digit brokers.
                    pips2dbl    = Point*10; pips2points = 10;   Digits.pips = 1;
        } else {    pips2dbl    = Point;    pips2points =  1;   Digits.pips = 0; }
        // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl