Help with retry for opening an order

 

Hello,

I'm just starting to look at MQL5 and now that I've successfully opened an order I would like to convert my MQL4 OpenOrder() function into MQL5.  If someone could give the equivalent MQL5 code for the MQL4 code below it would be greatly appreciated; hopefully someone has already coded something similar.  BTW - is this needed in MQL5?  I had to have retry logic in MQL4 with my broker.

Thanks!

//+------------------------------------------------------------------+
int OpenOrder(int type)
{  
   lastErr = 0;

   for(int c=0;c<Retries;c++)
   {
      RefreshRates();

      if(type==OP_BUY)           {double price=Ask; double sl=Ask-StopLoss*myPoint; double tp=Ask+TakeProfit*myPoint; datetime exp=0; int clr=Green;}
      else if(type==OP_SELL)     {price=Bid; sl=Bid+StopLoss*myPoint; tp=Bid-TakeProfit*myPoint; exp=0; clr=Red;}

      if(StopLoss==0)    sl = 0.0;
      if(TakeProfit==0)  tp = 0.0;

      string orderPrintString = "type = " + type +
              " Lot = " + DoubleToStr(InitLot,2) +
              " Bid = " + DoubleToStr(Bid,Digits) +
              " Ask = " + DoubleToStr(Ask,Digits) +
              " price = " + DoubleToStr(price,Digits) +
              " sl = " + DoubleToStr(sl,Digits) +
              " tp = " + DoubleToStr(tp,Digits) +
              " exp = " + TimeToStr(exp,TIME_SECONDS) +
              "";

      ticket=OrderSend(Symbol(),type,Lots,price,Slippage,sl,tp,ExpertComment,MagicNumber,exp,clr);
      if(ticket > 0) {break;}
      else   // error opening the order
      {
         int err = GetLastError();
         Print("Error Open Order: # " + err + ": Description = " + ErrorDescription(err) + "\n" + "OrderInfo: " + orderPrintString);
         if(err==0 || err==2 || err==4 ||err==6 || err==8 || err==9 || err==64 || err==128 || err==132 || err==133 || err==137 || err==139 || err==141 || err==146) //Sleep and retry errors
         {
            Print("Sleep And Retry Error Code");
            Sleep(100);  // wait .10 secs
            continue;
         }
         if(err==135 || err==136 || err== 138) // price change errors         
         {
            Print("Invalid Price; sleep and retry");
            if(c == 0) WindowScreenShot("S_REQUOTE_"+Month()+"_D"+Day()+"_H"+Hour()+"_M"+Minute()+"_S"+Seconds()+".gif",1440,900);
            Sleep(100);  // wait .10 secs
            continue;
         }
         else  // other errors -- exit, open order failed
         {
            if(err==134) lastErr = err;
            Print("Will not retry");
            break;
         }
      }
   }
   return(ticket);
}

 
Try to insert source code properly.
MQL5.community - User Memo
  • 2010.02.25
  • MetaQuotes Software Corp.
  • www.mql5.com
You have just registered and most likely you have questions such as, "How do I insert a picture to my a message?" "How do I format my MQL5 source code?" "Where are my personal messages kept?" You may have many other questions. In this article, we have prepared some hands-on tips that will help you get accustomed in MQL5.community and take full advantage of its available features.
 

Sorry about that.

int OpenOrder(int type)
{   
   lastErr = 0;

   for(int c=0;c<Retries;c++)
   {
      RefreshRates();

      if(type==OP_BUY)           {double price=Ask; double sl=Ask-StopLoss*myPoint; double tp=Ask+TakeProfit*myPoint; datetime exp=0; int clr=Green;}
      else if(type==OP_SELL)     {price=Bid; sl=Bid+StopLoss*myPoint; tp=Bid-TakeProfit*myPoint; exp=0; clr=Red;}

      if(StopLoss==0)    sl = 0.0;
      if(TakeProfit==0)  tp = 0.0;

      string orderPrintString = "type = " + type + 
              " Lot = " + DoubleToStr(InitLot,2) + 
              " Bid = " + DoubleToStr(Bid,Digits) +
              " Ask = " + DoubleToStr(Ask,Digits) + 
              " price = " + DoubleToStr(price,Digits) + 
              " sl = " + DoubleToStr(sl,Digits) + 
              " tp = " + DoubleToStr(tp,Digits) + 
              " exp = " + TimeToStr(exp,TIME_SECONDS) +
              "";

      ticket=OrderSend(Symbol(),type,Lots,price,Slippage,sl,tp,ExpertComment,MagicNumber,exp,clr);
      if(ticket > 0) {break;}
      else   // error opening the order 
      {
         int err = GetLastError();
         Print("Error Open Order: # " + err + ": Description = " + ErrorDescription(err) + "\n" + "OrderInfo: " + orderPrintString);
         if(err==0 || err==2 || err==4 ||err==6 || err==8 || err==9 || err==64 || err==128 || err==132 || err==133 || err==137 || err==139 || err==141 || err==146) //Sleep and retry errors 
         {
            Print("Sleep And Retry Error Code");
            Sleep(100);  // wait .10 secs
            continue;
         }
         if(err==135 || err==136 || err== 138) // price change errors          
         {
            Print("Invalid Price; sleep and retry");
            if(c == 0) WindowScreenShot("S_REQUOTE_"+Month()+"_D"+Day()+"_H"+Hour()+"_M"+Minute()+"_S"+Seconds()+".gif",1440,900);
            Sleep(100);  // wait .10 secs
            continue;
         }
         else  // other errors -- exit, open order failed
         {
            if(err==134) lastErr = err;
            Print("Will not retry");
            break; 
         } 
      }
   }
   return(ticket);
}

 

Reason: