I'm going crazy with OrderModify error 1

 

I guys! 

I have checked everything a tons of times and i just can't get it...

The Order some times get updated and some times get OrderModify error 1 

Please help me!

Thank you.

 

Just for information: 

PipsToTriggerTS = 0.01000 = 100 Pips

PipsToLockInTS  = 0.00500 = 50   Pips

void TrailingStop()
{
   for (int NumOrder = OrdersTotal()- 1; NumOrder >= 0; NumOrder--)
   {
      
      if (OrderSelect(NumOrder, SELECT_BY_POS, MODE_TRADES) && OrderMagicNumber() == MagicNumber && OrderSymbol() == Symbol())  
          
          if (OrderType() == OP_BUY)
          {                    
             if (Bid - OrderOpenPrice() > PipsToTriggerTS * Point)       
                if (OrderStopLoss() < Bid - (PipsToLockInTS * Point))       
                    
                        if (OrderModify(OrderTicket(), OrderOpenPrice(), Bid - (PipsToLockInTS * Point), OrderTakeProfit(), 0,clrNONE)) 
                            Alert("Buy Order updated TS");
                        else
                            Alert("Error nº: ", GetLastError());
          }
          
          if (OrderType() == OP_SELL)
          {                                                                                           
             if (OrderOpenPrice() - Ask > PipsToTriggerTS * Point)                               
                if (OrderStopLoss() > Ask + (PipsToLockInTS * Point) || OrderStopLoss() == 0)    
                                
                        if (OrderModify(OrderTicket(), OrderOpenPrice(), Ask + (PipsToLockInTS * Point),  OrderTakeProfit(), 0, clrNONE)) 
                            Alert("Sell Order updated TS");
                        else
                            Alert("Error nº: ", GetLastError()); 
          }
   }
} 
         
 
bro840:

I guys! 

I have checked everything a tons of times and i just can't get it...

The Order some times get updated and some times get OrderModify error 1 

Please help me!

Thank you.

 

Just for information: 

PipsToTriggerTS = 0.01000 = 100 Pips

PipsToLockInTS  = 0.00500 = 50   Pips

 

If those are the values, you shouldn't be multiplying them again by Point:

void TrailingStop()
{
   for (int NumOrder = OrdersTotal()- 1; NumOrder >= 0; NumOrder--)
   {
      
      if (OrderSelect(NumOrder, SELECT_BY_POS, MODE_TRADES) && OrderMagicNumber() == MagicNumber && OrderSymbol() == Symbol())  
          
          if (OrderType() == OP_BUY)
          {                    
             if (Bid - OrderOpenPrice() > PipsToTriggerTS * Point)       
                if (OrderStopLoss() < Bid - (PipsToLockInTS * Point))       
                    
                        if (OrderModify(OrderTicket(), OrderOpenPrice(), Bid - (PipsToLockInTS * Point), OrderTakeProfit(), 0,clrNONE)) 
                            Alert("Buy Order updated TS");
                        else
                            Alert("Error nº: ", GetLastError());
          }
          
          if (OrderType() == OP_SELL)
          {                                                                                           
             if (OrderOpenPrice() - Ask > PipsToTriggerTS * Point)                               
                if (OrderStopLoss() > Ask + (PipsToLockInTS * Point) || OrderStopLoss() == 0)    
                                
                        if (OrderModify(OrderTicket(), OrderOpenPrice(), Ask + (PipsToLockInTS * Point),  OrderTakeProfit(), 0, clrNONE)) 
                            Alert("Sell Order updated TS");
                        else
                            Alert("Error nº: ", GetLastError()); 
          }
   }
} 
 

these are the values after calculation. The problem it's not there because in stratagy tester the values are ok i have a function to calculate  those.

 

I have this litle function on OnInit() where the values are calculate.

And all theses variable are extern ones. For exemple:

extern TakeProfit is 100 by defalut. Then on EURUSD chart it gets updated to 100*10= 1000 then on my above function is 1000 * Point = 0.01000; = 100 pips

void AdjustPips()
{
   if (Point == 0.00001 || Point == 0.001)
   {
      StopLoss       = StopLoss * 10;
      TakeProfit     = TakeProfit * 10;
      PipsToTriggerBE  = PipsToTriggerBE * 10;
      PipsToLockInBE   = PipsToLockInBE * 10;
      PipsToTriggerTS  = PipsToTriggerTS * 10;
      PipsToLockInTS   = PipsToLockInTS * 10;
   }
}
 

I'd suggest you print all the values in your alert statement when the OrderModify fails. It will help to establish what is going on.

Order Modify Error 1 means you're not changing anything i.e. you're passing exactly the same values back in again.

 

void TrailingStop()
{
   for (int NumOrder = OrdersTotal()- 1; NumOrder >= 0; NumOrder--)
   {
      
      if (OrderSelect(NumOrder, SELECT_BY_POS, MODE_TRADES) && OrderMagicNumber() == MagicNumber && OrderSymbol() == Symbol())  
          
          if (OrderType() == OP_BUY)
          {                    
             if (Bid - OrderOpenPrice() > PipsToTriggerTS * Point)       
                if (OrderStopLoss() < Bid - (PipsToLockInTS * Point))       
                    
                        if (OrderModify(OrderTicket(), OrderOpenPrice(), Bid - (PipsToLockInTS * Point), OrderTakeProfit(), 0,clrNONE)) 
                            Alert("Buy Order updated TS");
                        else
                            Alert("Error nº: ", GetLastError());
          }
          
          if (OrderType() == OP_SELL)
          {                                                                                           
             if (OrderOpenPrice() - Ask > PipsToTriggerTS * Point)                               
                if (OrderStopLoss() > Ask + (PipsToLockInTS * Point) || OrderStopLoss() == 0)    
                                
                        if (OrderModify(OrderTicket(), OrderOpenPrice(), Ask + (PipsToLockInTS * Point),  OrderTakeProfit(), 0, clrNONE)) 
                            Alert("Sell Order updated TS");
                        else
                            Alert("Error nº: ", GetLastError()); 
          }
   }
} 
 
if (OrderStopLoss() < Bid - (PipsToLockInTS * Point))
This may be because of floating point round off. The == operand. - MQL4 forum
double newSL = Bid - PipsToLockInTS * Point; // Must move by at least one point
if (newSL - OrderStopLoss() >= _Point)       // Your code compared greater then zero
 

I was working on that

And i put a comment. And the problem seems that:

OrderStopLoss = 1.35000 Is Greater than (Ask + PipsToLockInTS * Point) = 1.35000

and it says Yes. And then the OrderModify Operator executes and it get error because the stoploss didn't change.

Why is this happen 1.35000>1.35000 true

Alert("Error nº: ", GetLastError());  

Comment("SL = ", OrderStopLoss(), "> ", (Ask + PipsToLockInTS * Point)); 

 
 
WHRoeder:
bro840: I have this litle function on OnInit() where the values are calculate.
Do not modify your externals. If you change timeframes you go through a deinit/init cycle and SL becomes 10X 100X 1000X...

I'm afraid that isn't actually the case.

When you change timeframes, the extern will revert to the value set in the Inputs window (so it forgets all the modifications made to it during the running of the code to date). 

So in the OP's code, it will always only be 10x 

 
WHRoeder:
bro840: I have this litle function on OnInit() where the values are calculate.
Do not modify your externals. If you change timeframes you go through a deinit/init cycle and SL becomes 10X 100X 1000X...

thank you for adevice :)
 
bro840:

Why is this happen 1.35000>1.35000 true

Run this script and you'll see:

void OnStart()
  {
   double val1=1.3+0.05;
   double val2=1.4-0.05;
   Print(DoubleToStr(val1));
   Print(DoubleToStr(val2));
   Print(DoubleToStr(val1,20));
   Print(DoubleToStr(val2,20));
  }

 

 

 

omfg!!!!

 

all that was needed was to use NormalizeDouble() function rsrsrrsrss

 if (OrderStopLoss() > NormalizeDouble((Ask + PipsToLockInTS * Point),5) || OrderStopLoss() == 0)    // If ins't already Trailled.