Unable to place order because of invalid stops

 

Hey,

 In an EA, I place orders like this:

 

MqlTradeRequest request;
MqlTradeResult result;
request.symbol      =Symbol();
request.volume      =0.1;
request.deviation   =1;
request.type_filling=ORDER_FILLING_FOK;
request.action = TRADE_ACTION_DEAL;
double askPrice   = SymbolInfoDouble(Symbol(),SYMBOL_ASK);
double bidPrice   = SymbolInfoDouble(Symbol(),SYMBOL_BID);
int stopLevels = SymbolInfoInteger(Symbol(), SYMBOL_TRADE_STOPS_LEVEL);
int stopDist = MathMax(stopLevels+1,50);
request.type = ORDER_TYPE_BUY;
request.sl   = NormalizeDouble(askPrice - stopDist * Point(),_Digits);
request.tp   = NormalizeDouble(askPrice + stopDist * Point(),_Digits);
request.price = askPrice;
OrderSend(request,result);

I always get as a result: "Invalid stops".

That is confusing, because my stops are at least "stopLevels+1" pips away from the price.

Any tips/Idea what is wrong?

Thanks!

Nathan

 Edit: Use SRC instead of CODE 

 

This may be due to your brokers spreads.exanples: If your broker spread is 3 and you set your stop to one, the ea won't open order till yur stop level is greater than 3.

Appart from that, your brokers may have predifine stop level: (some brokers don't allow their client to set stop loss level closer to the ask level. So, ask your prokers the numbers of pips you are to move away from the price level before setting stop loss.

 
RudolfKrugstein:

Hey,

 In an EA, I place orders like this:

 <CODE REMOVED>

I always get as a result: "Invalid stops".

That is confusing, because my stops are at least "stopLevels+1" pips away from the price.

Any tips/Idea what is wrong?

Thanks!

Nathan 

Please use the SRC button to post code: How to use the SRC button.

Do you know that you can send Stops with your Order ?  or do you need to send them afterwards ?  you need to check, click the following links for more info . . .

It depends on the symbol execution type  ( ENUM_SYMBOL_TRADE_EXECUTION ) if it's Market or Exchange execution then you don't send the open price,  if it's Request or Instant execution then you need to send the open price.

There is a little more info in this thread:  https://www.mql5.com/en/forum/11303  

 

Hey,

 Well, when I change the line

int stopDist = MathMax(stopLevels+1,50);

 to

int stopDist = MathMax(stopLevels+1,100);

 (so I the TP and SL will be set 100 pips away from the price), it works!

 So my TP and SL are to close to the price, and that is what is confusing me. I though that  stopDist = SymbolInfoInteger(Symbol(), SYMBOL_TRADE_STOPS_LEVEL); would return a number of pips, that is enough for setting TP and SL.

Am I wrong?

 

Thanks!

Nathan 

 
RudolfKrugstein:

Hey,

 Well, when I change the line

 to

 (so I the TP and SL will be set 100 pips away from the price), it works!

 So my TP and SL are to close to the price, and that is what is confusing me. I though that  stopDist = SymbolInfoInteger(Symbol(), SYMBOL_TRADE_STOPS_LEVEL); would return a number of pips, that is enough for setting TP and SL.

Am I wrong?

 

Thanks!

Nathan 

Sometimes SYMBOL_TRADE_STOPS_LEVEL  return 0, but your stop still have to be outside the spread.
 
angevoyageur:
Sometimes SYMBOL_TRADE_STOPS_LEVEL  return 0, but your stop still have to be outside the spread.
Mmmh, good Idea. But when I print the stop levels, it returns 80.
 
RudolfKrugstein:

I always get as a result: "Invalid stops".

That is confusing, because my stops are at least "stopLevels+1" pips away from the price.

Any tips/Idea what is wrong?

Thanks!

1. Use ZeroMemory before preparing MqlTradeRequest structure.

2. Calculate TP/SL levels relative to Bid/Ask levels.

void OnStart()
  {
//---
   MqlTradeRequest request;
   MqlTradeResult result;
   ZeroMemory(request);
   request.symbol      =Symbol();
   request.volume      =0.1;
   request.deviation   =1;
   request.type_filling=ORDER_FILLING_FOK;
   request.action=TRADE_ACTION_DEAL;
   double askPrice   = SymbolInfoDouble(Symbol(),SYMBOL_ASK);
   double bidPrice   = SymbolInfoDouble(Symbol(),SYMBOL_BID);
   long stopLevels=SymbolInfoInteger(Symbol(),SYMBOL_TRADE_STOPS_LEVEL);
   request.type = ORDER_TYPE_BUY;
   request.sl   = NormalizeDouble(bidPrice - stopLevels * Point(),_Digits);
   request.tp   = NormalizeDouble(askPrice + stopLevels * Point(),_Digits);
   request.price= askPrice;
   if (!OrderSend(request,result)) {Print("Error=", GetLastError());}
  }
 
Automated-Trading:

1. Use ZeroMemory before preparing MqlTradeRequest structure.

2. Calculate TP/SL levels relative to Bid/Ask levels.

Ah, I set the TP/SL both on bid, and not SL on Ask.

 Thanks! 

 

Hello everyone,

I meet the same problem : Invalid Stops. Error 04756. RetCode 10016 especially when I want to modify SL/TP of an existing open trade.

My code works well to open an order. For the modification, it fails. I checked the stop level, TP, SL level, etc, but always the same error return.

I read a lot of discussions about this topic in the forum and tested several suggestions, but nothing works.

It would be very nice if someone could tell me my mistake.

Thanks in advance.

See the full code bellow (simplified for the test), just copy it and past it, and let me know if it works with you.

(Used with EURUSD pair. 5 digits)


MqlTradeRequest request ;
MqlTradeResult result ;
         
void OnTick()
  {
     //if position = 0 then open a buy order
     if (PositionsTotal() == 0)
     { 
         ZeroMemory(request) ;
         ZeroMemory(result) ;
        
         request.action       = TRADE_ACTION_DEAL ;                         
         request.symbol       = _Symbol ;                                     
         request.volume       = 0.01 ;                                 
         request.type         = ORDER_TYPE_BUY ;                             
         request.type_filling = ORDER_FILLING_IOC ;
         request.price        = NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_ASK),5);
         request.sl           = NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_ASK)-0.00500,5);  //-50 pips
         request.tp           = NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_ASK)+0.00500,5);  //+50 pips                 
         request.magic        = 99999 ;
         
         if(!OrderSend(request, result))   
            {
            PrintFormat("Order has NOT been sent. Error : %d" + IntegerToString(GetLastError(),0)) ;
            PrintFormat("retcode=%u deal=%I64u order=%I64u", result.retcode, result.deal, result.order) ;
            }
         else
            {
            Print("Buy order has been sent successfully") ;
            PrintFormat("retcode=%u deal=%I64u order=%I64u", result.retcode, result.deal, result.order) ;
            }    
      }
      
      //if position = 1 then modify the buy position 
      else if (PositionsTotal() == 1) 
      { 
        //Check stop level :
        Print("Min stop level :" + IntegerToString(SymbolInfoInteger(_Symbol, SYMBOL_TRADE_STOPS_LEVEL),0)) ; 
        
        //Display bid price :
        Print("Current Bid price is " + DoubleToString(SymbolInfoDouble(_Symbol, SYMBOL_BID),5)) ;
        
        //calculate and display spread :
        double spread = (SymbolInfoDouble(_Symbol, SYMBOL_ASK)- SymbolInfoDouble(_Symbol, SYMBOL_BID))/_Point ;
        Print("Current spread " + DoubleToString(spread,0)) ;
        
        //get the position ticket
        
        ulong ticket = PositionGetTicket(0);
        
        Print("ticket is " + IntegerToString(ticket,0)) ;
                        
        ZeroMemory(request) ;
        ZeroMemory(result) ;
        
         request.action       = TRADE_ACTION_SLTP ;                        
         request.symbol       = _Symbol ;                                     
         request.volume       = 0.01 ;                                     
         request.position     =  ticket ;
         request.sl           = NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_ASK)-0.00400,5);  //-40 pips
         request.tp           = NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_ASK)+0.00400,5);  //+40 pips 
 
         if(!OrderSend(request, result))   
            {
            PrintFormat("Buy position has NOT been modified. Error : %d" + IntegerToString(GetLastError(),0)) ; //if enble to send the request, output the error code
            PrintFormat("retcode=%u deal=%I64u order=%I64u", result.retcode, result.deal, result.order) ;
            
            }
         else
            {
            Print("Buy position has been sent successfully") ;
            PrintFormat("retcode=%u deal=%I64u order=%I64u", result.retcode, result.deal, result.order) ;        
            }
      }    
   }
Files:
logs.png  197 kb
 
HelloTrade7 #: I meet the same problem : Invalid Stops. Error 04756. RetCode 10016 especially when I want to modify SL/TP of an existing open trade. My code works well to open an order. For the modification, it fails. I checked the stop level, TP, SL level, etc, but always the same error return. I read a lot of discussions about this topic in the forum and tested several suggestions, but nothing works. It would be very nice if someone could tell me my mistake.

Did you pay attention the log entry? Did you not notice that there is no change in the stops (s/l & t/p)?

Obviously if you try to modify a position to the same stops as before there will be an error.


So, before trying to apply a modification, make sure that the new stops are in in fact different to the current ones.

 
Fernando Carreiro #:

Did you pay attention the log entry? Did you not notice that there is no change in the stops (s/l & t/p)?

Obviously if you try to modify a position to the same stops as before there will be an error.


So, before trying to apply a modification, make sure that the new stops are in in fact different to the current ones.

Effectively, I didn't get it. Thank you Fernando
Reason: