Brain too small to figure out what causes this error 3

 

Hello world :)


EDIT: I added the complete loop (should have done that right away, apologies)

I have a few MT4-coding miles on my counter but I bumped into an error that I simply cannot seem to correct.


I have an EA that opens orders with a loop

bool SendSingleTrade(string symbol, int type, string comment, double lotsize, double price, double stop, double take)
{
   
   if (type == OP_BUYSTOP && price <= MarketInfo(symbol, MODE_ASK)) //Would cause an invalid stop error
      return(False);
      
   
   double slippage = MaxSlippagePips * MathPow(10, Digits) / (factor);
   int ticket = -1;
   
   color col = Red;
   if (type == OP_BUY || type == OP_BUYSTOP || type == OP_BUYLIMIT) col = Green;
   
   datetime expiry = 0;
   if (OrderType() > 1)
      expiry = TimeCurrent() + (PendingExpiryMinutes * 60);

   //RetryCount is declared as 10 in the Trading variables section at the top of this file
   for (int cc = 0; cc < RetryCount; cc++)
   {
      //for (int d = 0; (d < RetryCount) && IsTradeContextBusy(); d++) Sleep(100);

      RefreshRates();
      if (type == OP_BUY) price = MarketInfo(symbol, MODE_ASK);
      if (type == OP_SELL) price = MarketInfo(symbol, MODE_BID);
      
      while(IsTradeContextBusy()) Sleep(100);//Put here so that excess slippage will cancel the trade if the ea has to wait for some time.
      
      ticket = OrderSend(symbol,type, lotsize, price, (int)slippage, stop, take, comment, MagicNumber, expiry, col);
     
      if (ticket > -1) break;//Exit the trade send loop
      if (cc == RetryCount - 1) return(false);

      //Error trapping for both
      if (ticket < 0)
      {
         string stype;
         if (type == OP_BUY) stype = "OP_BUY";
         if (type == OP_SELL) stype = "OP_SELL";
         if (type == OP_BUYLIMIT) stype = "OP_BUYLIMIT";
         if (type == OP_SELLLIMIT) stype = "OP_SELLLIMIT";
         if (type == OP_BUYSTOP) stype = "OP_BUYSTOP";
         if (type == OP_SELLSTOP) stype = "OP_SELLSTOP";
         
         int err=GetLastError();
    
         Alert(symbol, " ", stype," order send failed with error(",err,"): ",ErrorDescription(err), 
               " Bid = ", DoubleToStr(Bid, Digits), " Ask = ", DoubleToStr(Ask, Digits)," Price = ", DoubleToStr(price, Digits), " slippage= ", slippage,
               " comment ", comment, " stop= ", stop, " take= ", take);
         Print(symbol, " ", stype," order send failed with error(",err,"): ",ErrorDescription(err),
               " Bid = ", DoubleToStr(Bid, Digits), " Ask = ", DoubleToStr(Ask, Digits)," Price = ", DoubleToStr(price, Digits), " slippage= ", slippage,
               " comment ", comment, " stop= ", stop, " take= ", take);
         return(false);
      }//if (ticket < 0)  
   }//for (int cc = 0; cc < RetryCount; cc++);
   
   
   
   return(true);
   
}//End bool SendSingleTrade(int type, string comment, double lotsize, double price, double stop, double take)

It obviously gets all the inputs it needs and then needs to open the trade.


I do however run into this error 3 that pops up every few seconds. (see screenshot)

I have no idea what causes it. For debugging purposes I print all the values a BUYSTOP takes. The price at which the EA wants to  open a position is clearly above the Ask, so I don't see what's wrong.

Does anyone have an idea why it errors out ??


All help welcome. Thanks in advance !!

Files:
alerts2.png  1312 kb
 
Spinn: I have no idea what causes it.
  1. There are no mind readers here. We can't see your broken code.
  2. Print out your variables, and find out why.
 
whroeder1:
  1. There are no mind readers here. We can't see your broken code.
  2. Print out your variables, and find out why.
I realize now that I should have added the complete loop. Sorry. Updated the original post.
 
Spinn:
I realize now that I should have added the complete loop. Sorry. Updated the original post.
Are you joking ? There is not even an OrderSend() in your code.
 

You were absolutely right, Mr. Alain.


Somehow part of my code did not get copied over. Brainfart, I guess. :s


I corrected it (again, sigh)

 

Fixed it !

Had a problem with the expiry time

 
Spinn: I realize now that I should have added the complete loop. Sorry. Updated the original post.
  1.    if (OrderType() > 1)
          expiry = TimeCurrent() + (PendingExpiryMinutes * 60);
    
    You can not use any Trade Functions until you select an order.
  2.       RefreshRates();
          if (type == OP_BUY) price = MarketInfo(symbol, MODE_ASK);
          if (type == OP_SELL) price = MarketInfo(symbol, MODE_BID);
          
          while(IsTradeContextBusy()) Sleep(100);//Put here so that excess slippage will cancel the trade if the ea has to wait for some time.
    
    If you sleep price may not be valid. Sleep, RefreshRates, then get price.
  3.       if (ticket < 0){
             :
                   " Bid = ", DoubleToStr(Bid, Digits), ...
    
    This prints the values you had before the OrderSend. Server calls take time, up to minutes during news releases. You must RefreshRates after server calls.
  4. What is the purpose of the loop, when you break on success and return on the first failure?


Reason: