Invalid Stops OrderSend Error 4756 retcode=10016

 

Hello,

Can someone help me?

maybe i can't normalize my ST and TP properly.

The parameters for the calculation of SL and TP are


08.19.2022 13:44: 18.049 StopLossShort = 1.05

2022.08.19 13: 44: 18.049 TakeProfitShort = 0.995


When I run the EA it gives me the following error


08.19.2022 13:44: 25.819 23.06.2010 00:00:00 market sale 0.01 CORN (350.25 / 350.50 / 350.25)

19.08.2022 13:44: 25.819 23.06.2010 00:00:00 deal n. 2 sell 0.01 CORN at 350.25 done (based on order # 2)

08.19.2022 13:44: 25.819 23.06.2010 00:00:00 operation performed [# 2 sale 0.01 CORN at 350.25]

2022.08.19 13: 44: 25.819 2010.06.23 00:00:00 executed order sell 0.01 at 350.25 [# 2 sell 0.01 CORN at 350.25]

2022.08.19 13: 44: 25.822 2010.06.23 00:00:00 retcode = 10009 Deal = 2 Order = 2

2022.08.19 13:44: 25.822 2010.06.23 00:00:00 Position: # 2 -Symbol: CORN -TipoOperazione: POSITION_TYPE_SELL -Volume: 0.01 -Price: 350.25 -sl: 0.00 -tp: 0.00 -MagicNumber: [1]

08.19.2022 13: 44: 25.822 23.06.2010 00:00:00 23.06.2010 00:00:00: Bid = 350.25 Ask = 350.5 Volume = 0

08.19.2022 13:44: 25.822 23.06.2010 00:00:00 Change Position: # 2 -Symbol: CORN -TipoOperazione: POSITION_TYPE_SELL

19.08.2022 13:44: 25.822 23.06.2010 00:00:00 failed change n. 2 rooms 0.01 CORN sl: 0.00, tp: 0.00 -> sl: 368.03, tp: 348.75 [Invalid stops]

2022.08.19 13: 44: 25.822 2010.06.23 00:00:00 OrderSend error 4756

2022.08.19 13: 44: 25.822 2010.06.23 00:00:00 retcode = 10016 Deal = 0 Order = 0

350.5 * 0.995


I have tried several scripts to set up ST and TP and this is the last one.

It seems correct to me but if there are roundings it does not work

In this example the real values would be:

TP is 350.5 * 0.995 = 348.7475

SL is 350.5 * 1.05 = 368.025


void SLTPmodify()
  {
//--- dichiara ed inizializza la richiesta di trade ed il risultato della richiesta di trade
   MqlTradeRequest request;
   MqlTradeResult  result;
   int total=PositionsTotal(); // numero delle posizioni aperte
//--- itera su tutte le posizioni/t34>
   for(int i=0; i<total; i++)
     {
      //--- parametri dell'Order
      ulong  position_ticket=PositionGetTicket(i);// ticket della posizione
      string position_symbol=PositionGetString(POSITION_SYMBOL); // simbolo
      int    digits=(int)SymbolInfoInteger(position_symbol,SYMBOL_DIGITS); // numero di cifre decimali
      ulong  magic=PositionGetInteger(POSITION_MAGIC); // MagicNumber della posizione
      double volume=PositionGetDouble(POSITION_VOLUME);    // volume della posizione
      double sl=PositionGetDouble(POSITION_SL);  // Stop Loss della posizione
      double tp=PositionGetDouble(POSITION_TP);  // Take Profit della posizione
      double bid=SymbolInfoDouble(position_symbol,SYMBOL_BID);
      double ask=SymbolInfoDouble(position_symbol,SYMBOL_ASK);
      ENUM_POSITION_TYPE type=(ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);  // tipo della posizione
      //--- output informazioni riguardo la posizione
      PrintFormat("Posizione:#%I64u -Simbolo:%s -TipoOperazione:%s -Volume:%.2f -Prezzo:%s -sl:%s -tp:%s -MagicNumber:[%I64d]",
                  position_ticket,
                  position_symbol,
                  EnumToString(type),
                  volume,
                  DoubleToString(PositionGetDouble(POSITION_PRICE_OPEN),digits),
                  DoubleToString(sl,digits),
                  DoubleToString(tp,digits),
                  magic);
      //--- se il MagicNumber corrisponde, Stop Loss e Take Profit non sono stati definiti
      if(magic==MagicNumber && sl==0 && tp==0)
        {
         //--- calcola i livelli del prezzo corrente

         if(type==POSITION_TYPE_BUY)
           {
            sl=NormalizeDouble(bid*StopLossLong,digits);
            tp=NormalizeDouble(bid*TakeProfitLong,digits);
           }
         else
           {
            sl=NormalizeDouble(ask*StopLossShort,digits);
            tp=NormalizeDouble(ask*TakeProfitShort,digits);
           }
         //--- azzero la richiesta ed i risultanti valori
         ZeroMemory(request);
         ZeroMemory(result);
         lasttick();
         //--- setting the operation parameters
         request.action  =TRADE_ACTION_SLTP; // type of trade operation
         request.position=position_ticket;   // ticket of the position
         request.symbol=position_symbol;     // simbolo
         request.sl      =NormalizeDouble(sl,digits);                // Stop Loss della posizione
         request.tp      =NormalizeDouble(tp,digits);                // Take Profit della posizione
         request.magic=MagicNumber;         // MagicNumber della posizione
         //--- output informazioni riguardo la modifica
         PrintFormat("Modifica Posizione:#%I64d -Simbolo:%s -TipoOperazione:%s",position_ticket,position_symbol,EnumToString(type));
         //--- invia la richiesta
         if(!OrderSend(request,result))
            PrintFormat("OrderSend error %d",GetLastError());  // se impossibile inviare la richiesta, da in output il codice errore
         //--- informazioni riguardo l'operazione
         PrintFormat("retcode=%u  Deal=%I64u  Order=%I64u",result.retcode,result.deal,result.order);
         ////Print("Trade stop level=",SymbolInfoInteger(_Symbol,SYMBOL_TRADE_STOPS_LEVEL));//stampo il differenza tra ask e bid del simbolo
        }
     }
  }



Thank you



Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Position Properties
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Position Properties
  • www.mql5.com
Position Properties - Trade Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Michele Bertuzzo:

Hello,

Can someone help me?

maybe i can't normalize my ST and TP properly.

The parameters for the calculation of the SL and TP are


08.19.2022 13: 44: 18.049 StopLossShort = 1.05

2022.08.19 13: 44: 18.049 TakeProfitShort = 0.995


When I run The EA it gives me the following error


08.19.2022 13: 44: 25.819 23.06.2010 00:00:00 market sale 0.01 CORN (350.25 / 350.50 / 350.25)

2022.08.19 13: 44: 25.819 2010.06.23 00:00:00 deal no. 2 sell 0.01 CORN at 350.25 done (based on order # 2)

08.19.2022 13: 44: 25.819 23.06.2010 00:00:00 operation performed [# 2 sale 0.01 CORN at 350.25]

2022.08.19 13: 44: 25.819 2010.06.23 00:00:00 executed order sell 0.01 at 350.25 [# 2 sell 0.01 CORN at 350.25]

2022.08.19 13: 44: 25.822 2010.06.23 00:00:00 retcode = 10009 Deal = 2 Order = 2

2022.08.19 13: 44: 25.822 2010.06.23 00:00:00 Position: # 2 -Symbol: CORN -TipoOperazione: POSITION_TYPE_SELL -Volume: 0.01 -Price: 350.25 -sl: 0.00 -tp: 0.00 -MagicNumber: [1]

08.19.2022 13: 44: 25.822 23.06.2010 00:00:00 23.06.2010 00:00:00: Bid = 350.25 Ask = 350.5 Volume = 0

08.19.2022 13: 44: 25.822 23.06.2010 00:00:00 Change Position: # 2 -Symbol: CORN -TipoOperazione: POSITION_TYPE_SELL

2022.08.19 13: 44: 25.822 2010.06.23 00:00:00 failed change n. 2 sale 0.01 CORN sl: 0.00, tp: 0.00 -> sl: 368.03, tp: 348.75 [Invalid stops]

2022.08.19 13: 44: 25.822 2010.06.23 00:00:00 OrderSend error 4756

2022.08.19 13: 44: 25.822 2010.06.23 00:00:00 retcode = 10016 Deal = 0 Order = 0

350.5 * 0.995


I have tried several scripts to set up ST and TP and this is the last one.

It seems correct to me but if there are roundings it does not work

In this example the real values would be:

TP is 350.5 * 0.995 = 348.7475

SL is 350.5 * 1.05 = 368.025


The script is



Thanks



Try making 2 calls - one for SL and then for TP.

You can figure out if both are invalid or just one - your TP looks like it is too close to the position open price and that could be the reason why the call fails

 
         sl=NormalizeDouble(bid*StopLossLong,digits);
         tp=NormalizeDouble(bid*TakeProfitLong,digits);

A price times a multiplier is meaningless. TP/SL are prices. The TP must be above the Bid and SL below.

You can't move stops (or pending prices) closer to the market than the minimum: MODE_STOPLEVEL * _Point or SymbolInfoInteger(SYMBOL_TRADE_STOPS_LEVEL).
          Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial

On some ECN type brokers, the value might be zero (the broker doesn't know). Use a minimum of two (2) PIPs.

The checks a trading robot must pass before publication in the Market - MQL5 Articles (2016)

 
R4tna C #:

Prova a fare 2 chiamate: una per SL e poi per TP.

Puoi capire se entrambi non sono validi o solo uno: il tuo TP sembra essere troppo vicino al prezzo di apertura della posizione e questo potrebbe essere il motivo per cui la chiamata non riesce

thanks, I only tried with the stoploss but it doesn't work

2022.08.19 14:34:05.764 2010.06.23 00:00:00   failed modify #2 sell 0.01 CORN sl: 0.00, tp: 0.00 -> sl: 368.03, tp: 0.00 [Invalid stops]


 
William Roeder #:

Un prezzo moltiplicato per un moltiplicatore non ha significato. TP/SL sono prezzi. Il TP deve essere al di sopra del Bid e SL al di sotto.

Non puoi spostare gli stop (o i prezzi in sospeso) più vicini al mercato del minimo: MODE_STOPLEVEL  * _Point o SymbolInfoInteger(SYMBOL_TRADE_STOPS_LEVEL) .
           Requisiti e Limitazioni nell'Effettuare Operazioni - Appendici - Tutorial MQL4

Su alcuni broker di tipo ECN, il valore potrebbe essere zero (il broker non lo sa). Utilizzare un minimo di due (2) PIP .

I controlli che un robot di trading deve superare prima della pubblicazione nel Mercato - Articoli MQL5 (20 16 )

thanks I try to set only Sl in less than ASK but it doesn't work

2022.08.19 14:41:36.852 2010.06.23 00:00:00   failed modify #2 sell 0.01 CORN sl: 0.00, tp: 0.00 -> sl: 348.75, tp: 0.00 [Invalid stops]


 
Michele Bertuzzo #:

grazie provo a impostare solo Sl in meno di ASK ma non funziona

2022.08.19 14:41:36.852 2010.06.23 00:00:00 non riuscita modifica n. 2 vendita 0.01 CORN sl: 0.00, tp: 0.00 -> sl: 348.75, tp: 0.00 [Soste non valide]


the trade stop level is

2022.08.19 14:47:29.802 2010.06.23 00:00:00   Trade stop level=100


so how much should my minimum stoploss be?

 
Michele Bertuzzo #:

thanks, I only tried with the stoploss but it doesn't work

2022.08.19 14:34:05.764 2010.06.23 00:00:00   failed modify #2 sell 0.01 CORN sl: 0.00, tp: 0.00 -> sl: 368.03, tp: 0.00 [Invalid stops]


I see you are computing it as 1.05 * open price -  you need to figure out an approach which works. Run some tests with larger numbers to get an idea.

And look at William's suggestion above

 
R4tna C #:

I see you are computing it as 1.05 * open price -  you need to figure out an approach which works. Run some tests with larger numbers to get an idea.

And look at William's suggestion above


and I ran it as a test. Also in this case I get an invalid stops error

2022.08.19 22:55:41.834 MetaTester 5 started on 127.0.0.1:3000

2022.08.19 22:55:41.840 initialization finished

2022.08.19 22:55:42.038 login (build 3391)

2022.08.19 22:55:42.117 4412 bytes of account info loaded

2022.08.19 22:55:42.117 1478 bytes of tester parameters loaded

2022.08.19 22:55:42.117 3268 bytes of input parameters loaded

2022.08.19 22:55:42.118 6739 bytes of symbols list loaded (799 symbols)

2022.08.19 22:55:42.119 expert file added: Experts\positionpropertiesplus.ex5. 140062 bytes loaded

2022.08.19 22:55:42.131 8477 Mb available, 105 blocks set for ticks generating

2022.08.19 22:55:42.131 initial deposit 10000.00 EUR, leverage 1:1

2022.08.19 22:55:42.133 successfully initialized

2022.08.19 22:55:42.133 148 Kb of total initialization data received

2022.08.19 22:55:42.133 AMD Ryzen 5 2400G with Radeon Vega, 14243 MB

2022.08.19 22:55:42.163 connected

2022.08.19 22:55:42.164 US_500: symbol to be synchronized

2022.08.19 22:55:42.164 US_500: symbol synchronized, 4280 bytes of symbol info received

2022.08.19 22:55:42.164 US_500: history synchronization started

2022.08.19 22:55:42.169 US_500: load 27 bytes of history data to synchronize in 0:00:00.002

2022.08.19 22:55:42.169 US_500: history synchronized from 2011.01.17 to 2022.08.18

2022.08.19 22:55:42.169 US_500: start time changed to 2011.05.18 00:00 to provide data at beginning

2022.08.19 22:55:42.169 US_500,Daily: history cache allocated for 3037 bars and contains 101 bars from 2011.01.17 00:00 to 2011.05.17 00:00

2022.08.19 22:55:42.169 US_500,Daily: history begins from 2011.01.17 00:00

2022.08.19 22:55:42.169 US_500,Daily (Ava-Real 1-MT5): every tick generating

2022.08.19 22:55:42.169 testing with execution delay 20 milliseconds

2022.08.19 22:55:42.169 US_500,Daily: testing of Experts\positionpropertiesplus.ex5 from 2011.01.17 00:00 to 2022.08.18 00:00 started with inputs:

2022.08.19 22:55:42.169   NumberOfBars=2

2022.08.19 22:55:42.169   Lot=0.1

2022.08.19 22:55:42.169   StopLoss=50.0

2022.08.19 22:55:42.169   TakeProfit=100.0

2022.08.19 22:55:42.169   TrailingStop=10.0

2022.08.19 22:55:42.169   Reverse=true

2022.08.19 22:55:42.196 2011.05.18 00:00:00   CheckNewBar: Initialization [US_500][TF: D1][2011.05.17 00:00:00]

2022.08.19 22:55:43.566 2011.05.19 00:00:00   failed market buy 0.1 US_500 sl: 1338.74 tp: 1340.50 [Invalid stops]

2022.08.19 22:55:43.566 2011.05.19 00:00:00   CTrade::OrderSend: market buy 0.10 US_500 sl: 1338.74 tp: 1340.50 [invalid stops]

2022.08.19 22:55:43.566 2011.05.19 00:00:00   Error opening the position: 4756 - Failed to send the trade request

2022.08.19 22:55:43.584 2011.05.20 00:00:00   failed market buy 0.1 US_500 sl: 1340.74 tp: 1342.50 [Invalid stops]

2022.08.19 22:55:43.584 2011.05.20 00:00:00   CTrade::OrderSend: market buy 0.10 US_500 sl: 1340.74 tp: 1342.50 [invalid stops]


Could it be a broker-related problem?

I don't think anything is posted on the site that isn't working

 
Michele Bertuzzo #:


and I ran it as a test. Also in this case I get an invalid stops error

2022.08.19 22:55:41.834 MetaTester 5 started on 127.0.0.1:3000

2022.08.19 22:55:41.840 initialization finished

2022.08.19 22:55:42.038 login (build 3391)

2022.08.19 22:55:42.117 4412 bytes of account info loaded

2022.08.19 22:55:42.117 1478 bytes of tester parameters loaded

2022.08.19 22:55:42.117 3268 bytes of input parameters loaded

2022.08.19 22:55:42.118 6739 bytes of symbols list loaded (799 symbols)

2022.08.19 22:55:42.119 expert file added: Experts\positionpropertiesplus.ex5. 140062 bytes loaded

2022.08.19 22:55:42.131 8477 Mb available, 105 blocks set for ticks generating

2022.08.19 22:55:42.131 initial deposit 10000.00 EUR, leverage 1:1

2022.08.19 22:55:42.133 successfully initialized

2022.08.19 22:55:42.133 148 Kb of total initialization data received

2022.08.19 22:55:42.133 AMD Ryzen 5 2400G with Radeon Vega, 14243 MB

2022.08.19 22:55:42.163 connected

2022.08.19 22:55:42.164 US_500: symbol to be synchronized

2022.08.19 22:55:42.164 US_500: symbol synchronized, 4280 bytes of symbol info received

2022.08.19 22:55:42.164 US_500: history synchronization started

2022.08.19 22:55:42.169 US_500: load 27 bytes of history data to synchronize in 0:00:00.002

2022.08.19 22:55:42.169 US_500: history synchronized from 2011.01.17 to 2022.08.18

2022.08.19 22:55:42.169 US_500: start time changed to 2011.05.18 00:00 to provide data at beginning

2022.08.19 22:55:42.169 US_500,Daily: history cache allocated for 3037 bars and contains 101 bars from 2011.01.17 00:00 to 2011.05.17 00:00

2022.08.19 22:55:42.169 US_500,Daily: history begins from 2011.01.17 00:00

2022.08.19 22:55:42.169 US_500,Daily (Ava-Real 1-MT5): every tick generating

2022.08.19 22:55:42.169 testing with execution delay 20 milliseconds

2022.08.19 22:55:42.169 US_500,Daily: testing of Experts\positionpropertiesplus.ex5 from 2011.01.17 00:00 to 2022.08.18 00:00 started with inputs:

2022.08.19 22:55:42.169   NumberOfBars=2

2022.08.19 22:55:42.169   Lot=0.1

2022.08.19 22:55:42.169   StopLoss=50.0

2022.08.19 22:55:42.169   TakeProfit=100.0

2022.08.19 22:55:42.169   TrailingStop=10.0

2022.08.19 22:55:42.169   Reverse=true

2022.08.19 22:55:42.196 2011.05.18 00:00:00   CheckNewBar: Initialization [US_500][TF: D1][2011.05.17 00:00:00]

2022.08.19 22:55:43.566 2011.05.19 00:00:00   failed market buy 0.1 US_500 sl: 1338.74 tp: 1340.50 [Invalid stops]

2022.08.19 22:55:43.566 2011.05.19 00:00:00   CTrade::OrderSend: market buy 0.10 US_500 sl: 1338.74 tp: 1340.50 [invalid stops]

2022.08.19 22:55:43.566 2011.05.19 00:00:00   Error opening the position: 4756 - Failed to send the trade request

2022.08.19 22:55:43.584 2011.05.20 00:00:00   failed market buy 0.1 US_500 sl: 1340.74 tp: 1342.50 [Invalid stops]

2022.08.19 22:55:43.584 2011.05.20 00:00:00   CTrade::OrderSend: market buy 0.10 US_500 sl: 1340.74 tp: 1342.50 [invalid stops]


Could it be a broker-related problem?

I don't think anything is posted on the site that isn't working

Can't say.

I suggest changing your way of computing the SL/TP - add/subtract a number of points rather than using a percentage multiplier.

Write a test script and use a demo account  - keep the TP/SL setting as separate calls and create a loop with an increasing number of points. That will help you identify what the minimum must be for your scenario

 
You could also take a look at the symbol specifications and check the tick size value. (CTRL-U)

It is possible, that price may have another granularity, Like 0.05, then a price of 0.74 would be considered invalid, as it needs to be 0.75 in this case.


 

Thanks Everybody,

for now I've solved with MathRound Function

Reason: