OrderModify gives error 1 even checking the TP values before

 

I have created this function to update the TP/SL of market orders. In tester it sometimes shows error 1 whereas the TP and new price are the same values. if TakeProfit() and _precio are the same, I expect the function to skip OrderModify().

Most probably it is a stupid error from my side, but after a whole day working on this I am unable to find the error in this code.

I would appreciate any help. Thanks

void Ajustar_TPSL(double _precio, int _tipoOrden)
  {
   if(_precio < 0) return;

   for(int pos = 0; pos < OrdersTotal(); pos++)
     {
      if(OrderSelect(pos, SELECT_BY_POS) && OrderType() == _tipoOrden && OrderSymbol() == _Symbol && OrderMagicNumber() == MagicNumber)
        {
         if(DEBUG) printf("### %s ##:: Type= %d :: Order= %d :: NewPrice=%.7f ::  TP=%.7f", __FUNCTION__, OrderType(), OrderTicket(), _precio, OrderTakeProfit());

         switch(OrderType())
           {
            case OP_BUY:
               if(_precio > Bid && _precio != OrderTakeProfit())    // For TakeProfit
                 {
                  if(DEBUG) printf(">>>> Ajustando TP de BUY #%d: TP=%.7f  :: NewPrice=%.7f", OrderTicket(), OrderTakeProfit(), _precio);
                  OrderModify( OrderTicket(), OrderOpenPrice(), OrderStopLoss(), _precio, OrderExpiration() );
                  continue;
                 }

               if(_precio < Bid && _precio != OrderStopLoss())    // For StopLoss
                 {
                  if(DEBUG) printf(">>>> Ajustando SL de BUY #%d: SL=%.7f  :: NewPrice=%.7f", OrderTicket(), OrderStopLoss(), _precio);
                  OrderModify( OrderTicket(), OrderOpenPrice(), _precio, OrderTakeProfit(), OrderExpiration() );
                  continue;
                 }

               break;

            case OP_SELL:
               if(_precio < Bid && _precio != OrderTakeProfit())    // For TakeProfit
                 {
                  if(DEBUG) printf(">>>> Ajustando TP de SELL #%d: TP=%.7f  :: NewPrice=%.7f  :: Div=%.7f", OrderTicket(), OrderTakeProfit(), _precio, OrderTakeProfit()/_precio);
                  OrderModify( OrderTicket(), OrderOpenPrice(), OrderStopLoss(), _precio, OrderExpiration() );
                  continue;
                 }

               if(_precio > Bid && _precio != OrderStopLoss())    // For StopLoss
                 {
                  if(DEBUG) printf(">>>> Ajustando SL de SELL #%d: SL=%f  :: NewPrice=%f", OrderTicket(), OrderStopLoss(), _precio);
                  OrderModify( OrderTicket(), OrderOpenPrice(), _precio, OrderTakeProfit(), OrderExpiration() );
                 }
           }
        }//end OrderSelect
     }//end For
  }


Here is the output 

 

 
Fernando Morales:

I have created this function to update the TP/SL of market orders. In tester it sometimes shows error 1 whereas the TP and new price are the same values. if TakeProfit() and _precio are the same, I expect the function to skip OrderModify().

Most probably it is a stupid error from my side, but after a whole day working on this I am unable to find the error in this code.

I would appreciate any help. Thanks


Here is the output 

 

Hello,

separate this line...

if(OrderSelect(pos, SELECT_BY_POS) && OrderType() == _tipoOrden && OrderSymbol() == _Symbol && OrderMagicNumber() == MagicNumber)

make 2 lines like example...

if(OrderSelect(pos, SELECT_BY_POS))
{
 if(OrderType() == _tipoOrden && OrderSymbol() == _Symbol && OrderMagicNumber() == MagicNumber)
   {

 
Fernando Morales:

I have created this function to update the TP/SL of market orders. In tester it sometimes shows error 1 whereas the TP and new price are the same values. if TakeProfit() and _precio are the same, I expect the function to skip OrderModify().

Most probably it is a stupid error from my side, but after a whole day working on this I am unable to find the error in this code.

I would appreciate any help. Thanks


Here is the output 

 

When you want to compare given price for TP or SL, with OrderTakeProfit() or OrderStopLoss(), you need to round that price to TickSize.
Here is a function for this purpose:

double RoundToTickSize(double price, string symbol) {
   if(price <= 0) {
      return(0);
   }

   double tickSize = SymbolInfoDouble(symbol, SYMBOL_TRADE_TICK_SIZE);

   if(tickSize != 0) {
      price = MathRound(price / tickSize) * tickSize;
   }

   return(price);
}

And here is how to use it:

if(_precio > Bid && RoundToTickSize(_precio, OrderSymbol())!= OrderTakeProfit())    // For TakeProfit
{
   OrderModify( OrderTicket(), OrderOpenPrice(), OrderStopLoss(), RoundToTickSize(_precio, OrderSymbol()), OrderExpiration() );
}
 
Nikolaos Pantzos: separate this line... make 2 lines like example...

Those two are identical. There is no difference.

 
Fernando Morales:

OrderModify gives error 1 even checking the TP values before

ERR_NO_RESULT
You Server
Change the SL to X It is at X!
Change the SL to X It is at X!
Change the SL to X You are insane
Insanity: doing the same thing over and over again and expecting different results.
          Unknown
Compute the new value, then check that you are moving the existing value at least a tick. PIP, Point, or Tick are all different in general.
          What is a TICK? - MQL4 programming forum
 
Mohammad Hossein Sadeghi:

When you want to compare given price for TP or SL, with OrderTakeProfit() or OrderStopLoss(), you need to round that price to TickSize.
Here is a function for this purpose:

And here is how to use it:

The price passed to the function comes normalized 

NormalizeDouble ( price, _Digits );


Is this different from your rounding function?

 
William Roeder:
ERR_NO_RESULT
You Server
Change the SL to X It is at X!
Change the SL to X It is at X!
Change the SL to X You are insane
Compute the new value, then check that you are moving the existing value at least a tick. PIP, Point, or Tick are all different in general.
          What is a TICK? - MQL4 programming forum

The price value calculated as new TP is normalized and then passed to the function.

I print the price and TP values up to 7th decimal and divide them to confirm they are same values. What I am missing here?


if(DEBUG) printf(">>>> Ajustando TP de SELL #%d: TP=%.7f  :: NewPrice=%.7f  :: Div=%.7f", OrderTicket(), OrderTakeProfit(), _precio, OrderTakeProfit()/_precio);
 
Fernando Morales: to confirm they are same values. What I am missing here?

They are the same values. Therefor you get "no result." What part of "check that you are moving the existing value at least a tick" was unclear?

 
William Roeder:

They are the same values. Therefor you get "no result." What part of "check that you are moving the existing value at least a tick" was unclear?

This is the check that results TRUE when should not

if(_precio < Bid && _precio != OrderTakeProfit())    // For TakeProfit
{
  if(DEBUG) printf(">>>> Ajustando TP de SELL #%d: TP=%.7f  :: NewPrice=%.7f  :: Div=%.7f", OrderTicket(), OrderTakeProfit(), _precio, OrderTakeProfit()/_precio);
  OrderModify( OrderTicket(), OrderOpenPrice(), OrderStopLoss(), _precio, OrderExpiration() );
  continue;
}
 
_precio != OrderTakeProfit()

Doubles are rarely equal. Understand the links in:
          The == operand. - MQL4 programming forum

And for the third time: What part of "check that you are moving the existing value at least a tick" was unclear?

 
Fernando Morales:

The price passed to the function comes normalized 


Is this different from your rounding function?

Yes, different.