Order Send error 138 ?

 
void OrderEntry(int direction) 

  {
   if(direction==1)
      if(OrderSend(Symbol(),OP_BUY,GetLongContracts(),Ask,SLIPPAGE,GetStopLossLongPrice(),0,NULL,MagicNumber,0,Green) < 0)
        {
         Print("Order Send failed, error # ", GetLastError());
	 RefreshRates();
        }

   if(direction==0)
      if(OrderSend(Symbol(),OP_SELL,(GetShortContracts()),Bid,SLIPPAGE,GetStopLossShortPrice(),0,NULL,MagicNumber,0,Green) < 0)
        {
         Print("Order Send failed, error # ", GetLastError());
	 RefreshRates();
        }
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+

datetime now;
int start()
{
   if( now != Time[0] )
   {
      now = Time[0];
         Alert("New Bar");
         
         if(CountLONGPositions() > 0 && (GetAroonup() < GetAroondown()))
          {
          Alert("Order Closed");
          CloseOrders(Symbol(),MagicNumber,OP_BUY);
          }

         if(CountSHORTPositions() > 0 && (GetAroondown() < GetAroonup()))
          {
          Alert("Order Closed");
          CloseOrders(Symbol(),MagicNumber,OP_SELL);
          }
          
        if((CountPositions() > 0))
           {
            ApplyTrailingStop(Symbol(), MagicNumber, ATRTrailingStop() * TrailingStopMultiplier );
           }
        else
           {
            CheckForLONGTrade();
            CheckForSHORTTrade();
           }
   }

return (0) ;

}

I am getting order send error 138, I have done some research and i need to add the refresh rates functions, I have done this above but I still get errors ? 

Did I put it in the wrong place ?

And when i want to use ask and bid prices should I do this 

double GetAskPrice()
  {
   double AskPrice;
   return AskPrice = Ask;
  }

double GetStopLossShortPrice()
  {
   double StopLossSHORTPrice;
   return StopLossSHORTPrice = GetAskPrice()+GetStopLossShortSize();
  }

or this 

double GetStopLossShortPrice()
  {
   double StopLossSHORTPrice;
   return StopLossSHORTPrice = Ask+GetStopLossShortSize();
  }

Thanks for any help 

 

Refresh rates just before you send orders to make sure your order has the latest prices.

At present you are refreshing only after you have sent the order and it has failed.

As for the second part just use Ask and Bid

 
Paul Anscombe #:

Refresh rates just before you send orders to make sure your order has the latest prices.

At present you are refreshing only after you have sent the order and it has failed.

As for the second part just use Ask and Bid

Ok great thanks 

if(direction==1)
    RefreshRates()
      if(OrderSend(Symbol(),OP_BUY,GetLongContracts(),Ask,SLIPPAGE,GetStopLossLongPrice(),0,NULL,MagicNumber,0,Green) < 0)
        {
         Print("Order Send failed, error # ", GetLastError());
        }

should it look like this ?

 
George Johnson #:

Ok great thanks 

should it look like this ?

Better placement of Refreshrates but you need to use the OrderSend function correctly it returns an integer not a bool  

https://docs.mql4.com/trading/ordersend

OrderSend - Trade Functions - MQL4 Reference
OrderSend - Trade Functions - MQL4 Reference
  • docs.mql4.com
OrderSend - Trade Functions - MQL4 Reference
 
Paul Anscombe #:

Better placement of Refreshrates but you need to use the OrderSend function correctly it returns an integer not a bool  

https://docs.mql4.com/trading/ordersend

I Moved RefreshRates to where I said above and now it enters long and short ?

void OrderEntry(int direction) 

  {
   if(direction==1)
     RefreshRates();
      if(OrderSend(Symbol(),OP_BUY,GetLongContracts(),Ask,SLIPPAGE,GetStopLossLongPrice(),0,NULL,MagicNumber,0,Green) < 0)
        {
         Print("Order Send failed, error # ", GetLastError());
        }

   if(direction==0)
     RefreshRates();
      if(OrderSend(Symbol(),OP_SELL,(GetShortContracts()),Bid,SLIPPAGE,GetStopLossShortPrice(),0,NULL,MagicNumber,0,Green) < 0)
        {
         Print("Order Send failed, error # ", GetLastError());
        }
  }
 
George Johnson #:

I Moved RefreshRates to where I said above and now it enters long and short ?

you need to add parentheses { } for the if statements.. or place RefreshRates() just once before the if statements

void OrderEntry(int direction) 
  {
   if(direction==1)
   {

     RefreshRates();
     if(OrderSend(Symbol(),OP_BUY,GetLongContracts(),Ask,SLIPPAGE,GetStopLossLongPrice(),0,NULL,MagicNumber,0,Green) < 0)
        {
         Print("Order Send failed, error # ", GetLastError());
        }
   }

   if(direction==0)
   {
      RefreshRates();
      if(OrderSend(Symbol(),OP_SELL,(GetShortContracts()),Bid,SLIPPAGE,GetStopLossShortPrice(),0,NULL,MagicNumber,0,Green) < 0)
        {
         Print("Order Send failed, error # ", GetLastError());
        }
   }
  }
 
Paul Anscombe #:

you need to add parentheses { } for the if statements.. or place RefreshRates() just once before the if statements

Hi this has helped a lot, I now get a lot less error 138s, although on live forward testing I still get a few of these errors along with error 133. I have done some research and it could be a drop in internet connection, although I wouldn't have thought that would have been the problem. what else could be causing these errors ?

Thanks 

 
George Johnson: Hi I seem to be getting order send error 138 and I believe its because I have a slippage that is too small, this is the code I use to calculate slippage an advice on why I keep getting order send error 138? 
  1. You are double posting. You already have a topic open about this error. Why did you start a new thread about the same thing?
  2. There are many, many threads on the forum about error 138, Did you read them and learn from them?
  3. Did you ever bother to read up on what error 138 means? It means there is a "Requote" when using "Instant Execution" method for example.
  4. How do you solve it?
    1. The quickest and simplest method is to use an Account or Broker, that uses "Market Execution" instead of "Instant Execution" and your orders will always be filled at the current market price irrespective of the slippage.
    2. The more difficult method is to use the most recent market prices and submit the market order as quickly as possible with a very wide deviation/slippage parameter (research this in the OrderSend documentation), and when that gives a requote error, you quickly resubmit a new order with the latest refreshed market price. And you repeat this process until it is accepted or you stop when the market prices have drifted to far away from what you originally wanted.
<Moderators note - the other topic has been deleted and this reply moved here>
 
George Johnson #:

Hi this has helped a lot, I now get a lot less error 138s, although on live forward testing I still get a few of these errors along with error 133. I have done some research and it could be a drop in internet connection, although I wouldn't have thought that would have been the problem. what else could be causing these errors ?

Thanks 

Or maybe you can add option to retry OrderSend() ... add this error to list and then allow to retry couple of times

 
Daniel Cioca #: Or maybe you can add option to retry OrderSend() ... add this error to list and then allow to retry couple of times

Retrying the OrderSend() with the same parameters is a waste of time and effort for a Requote. Obviously if it is a requote, then the market prices have changed and drifted too far away from the maximum deviation specified by the user. The parameters for the order are no longer valid and have to be recalculated and the conditions reevaluated to see if they still make sense according to the strategy rules or not, and only then resubmit the order under the new parameters and conditions.

 
Fernando Carreiro #:

Retrying the OrderSend() with the same parameters is a waste of time and effort for a Requote. Obviously if it is a requote, then the market prices have changed and drifted too far away from the maximum deviation specified by the user. The parameters for the order are no longer valid and have to be recalculated and the conditions reevaluated to see if they still make sense according to the strategy rules or not, and only then resubmit the order under the new parameters and conditions.

You are obviously right.

What I was saying is to retry OrderSend with the latest prices. Also maybe include IsTradeContextBusy() maybe..it also help to speedup filling requested order.

Because, as he is saying, he is only getting this error sometimes, not every time.

Reason: