modify order failed [Invalid S/L or T/P]

 

Hi,


After successful backtesting I launch my EA to live trading. If a long or short condition is TRUE, it will go long or short first and then set the ST and TP by using the OrderModify(). My code look as follow:

...
extern double SL_prozent = 0.5;
extern double TP_prozent = 5;

...
   if(bEntryLong){
      if(iTotal>0)
         closeAllOpenedShortOrders();
      else{         
         int iLongTicket=OrderSend(Symbol(),OP_BUY,dLots,MarketInfo(Symbol(),MODE_ASK),iSlippage,0,0,"T05_MURI",MAGIC_NUMBER,0,clrGreen);
         if(iLongTicket>0)
           {
            Print("Current Time: ", TimeCurrent()," Ask: ",Ask," Bid: ",Bid," Spread: ",(Ask-Bid));           
            if(OrderSelect(iLongTicket,SELECT_BY_TICKET,MODE_TRADES)){
               Print("LONG order opened : ",OrderOpenPrice());
               if(bStopLossAndTakeProfit){
                  if(OrderCloseTime() == 0 && OrderStopLoss() == 0)
                  {
                     double StoppLoss = NormalizeDouble(OrderOpenPrice() / ( 1 + SL_prozent/100), Digits);
                     bool OrderAngepasst = OrderModify(OrderTicket(),OrderOpenPrice(),StoppLoss,OrderTakeProfit(),0,Yellow);
                     if(!OrderAngepasst)
                        Print("Error by OrderModify : ",GetLastError());
                     else
                        Print("Order modified successfully.");            
                  }
                  
                  if(OrderCloseTime() == 0 && OrderTakeProfit() == 0)
                  {
                     double TakeProfit = NormalizeDouble(OrderOpenPrice() * ( 1 + (TP_prozent /100)), Digits);
                     bool OrderAngepasst = OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),TakeProfit,0,Orange);
                     if(!OrderAngepasst)
                        Print("Error by OrderModify : ",GetLastError());
                     else
                        Print("Order modified successfully.");            
                  }
                  
               }
            }   
            else
               Print("Error by OrderSelect : ",GetLastError());
           }
         else
            Print("Error opening LONG order : ",GetLastError());
...

the same by Short


But I got following errors when a Buy or Sell order is filled:

2018.05.02 21:00:00.958 '1000004894': modification of order #3525203 sell 0.20 DAX. at 12774.05 sl: 0.00 tp: 0.00 -> sl: 0.00 tp: 12165.76 failed [Invalid S/L or T/P]
2018.05.02 21:00:00.818 '1000004894': modify order #3525203 sell 0.20 DAX. at 12774.05 sl: 0.00 tp: 0.00 -> sl: 0.00 tp: 12165.76
2018.05.02 21:00:00.802 '1000004894': modification of order #3525203 sell 0.20 DAX. at 12774.05 sl: 0.00 tp: 0.00 -> sl: 12837.92 tp: 0.00 failed [Invalid S/L or T/P]
2018.05.02 21:00:00.646 '1000004894': modify order #3525203 sell 0.20 DAX. at 12774.05 sl: 0.00 tp: 0.00 -> sl: 12837.92 tp: 0.00
2018.05.02 21:00:00.488 '1000004894': order was opened : #3525203 sell 0.20 DAX. at 12774.05 sl: 0.00 tp: 0.00
2018.05.02 21:00:00.160 '1000004894': order sell market 0.20 DAX. sl: 0.00 tp: 0.00
2018.05.02 11:09:51.503 '1000004894': ping to current access point Main is n/a

Here one can see a sell-order is filled. But by modifying the ST and TP it failed.


Besides, accoding my code, if an order is opend, it should print out "Current Time:...". But I can't see this.

 
thomas2004:

Hi,


After successful backtesting I launch my EA to live trading. If a long or short condition is TRUE, it will go long or short first and then set the ST and TP by using the OrderModify(). My code look as follow:


But I got following errors when a Buy or Sell order is filled:

Here one can see a sell-order is filled. But by modifying the ST and TP it failed.


Besides, accoding my code, if an order is opend, it should print out "Current Time:...". But I can't see this.

Try this code to Normalize Prices instead of NormalizeDouble(), then it might work :)

double NormalizePrice(double price)
  {
        double tickSize = MarketInfo(Symbol(), MODE_TICKSIZE)
        return( MathRound(price/tickSize) * tickSize );

  }     
 
Lakshan Perera:

Try this code to Normalize Prices instead of NormalizeDouble(), then it might work :)

Hi thanks. I will try. 


But how about the following? My reason: different instrument may has different ticksize such as EUR/USD and German DAX. Right?

double NormalizePrice(double price)
  {
        double tickSize = calculatePipSize();
        return( MathRound(price/tickSize) * tickSize );

  }     

double calculatePipSize()
{
   double pips;
   double ticksize = MarketInfo(Symbol(),MODE_TICKSIZE);
   if (ticksize ==0.00001|| ticksize ==0.001)
      pips = ticksize*10;
   else
      pips =ticksize;
   return pips;
}


Besides, in your code you haven't dealed with the TP and ST, right?


I wonder, why my code works by backtesting but not by live trading?


And as I wrote in my first post: "accoding my code, if an order is opend, it should print out "Current Time:...". But I can't see this...  This should be happend before the NormalizeDouble().

 
thomas2004:

And as I wrote in my first post: "accoding my code, if an order is opend, it should print out "Current Time:...". But I can't see this...  This should be happend before the NormalizeDouble().

The example you have given is for a sell open right. but the code is for a buy order, please check and confirm that, in the sell order open code, the Print() statement is written correctly

 
Lakshan Perera:

The example you have given is for a sell open right. but the code is for a buy order, please check and confirm that, in the sell order the Print() statement is written correctly

Here are the codes for Sell/Short, they are the similar or analog to Buy/Long:

   // Handle Short Signal
   if(bEntryShort){
      if(iTotal>0)
         closeAllOpenedLongOrders();
      else{         
         int iShortTicket=OrderSend(Symbol(),OP_SELL,dLots,MarketInfo(Symbol(),MODE_BID),iSlippage,0,0,"T05_MURI",MAGIC_NUMBER,0,clrYellow);
         if(iShortTicket>0)
           {
            Print("Current Time: ", TimeCurrent()," Ask: ",Ask," Bid: ",Bid," Spread: ",(Ask-Bid));           
            if(OrderSelect(iShortTicket,SELECT_BY_TICKET,MODE_TRADES)){
               Print("SHORT order opened : ",OrderOpenPrice());
               if(bStopLossAndTakeProfit){
                  if(OrderCloseTime() == 0 && OrderStopLoss() == 0)
                  {
                     double StoppLoss = NormalizeDouble(OrderOpenPrice() * ( 1 + SL_prozent/100), Digits);
                     bool OrderAngepasst = OrderModify(OrderTicket(),OrderOpenPrice(),StoppLoss,OrderTakeProfit(),0,Yellow);
                     if(!OrderAngepasst)
                        Print("Error by OrderModify : ",GetLastError());
                     else
                        Print("Order modified successfully.");            
                  }
                  
                  if(OrderCloseTime() == 0 && OrderTakeProfit() == 0)
                  {
                     double TakeProfit = NormalizeDouble(OrderOpenPrice() / ( 1 + (TP_prozent /100)), Digits);
                     bool OrderAngepasst = OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),TakeProfit,0,Orange);
                     if(!OrderAngepasst)
                        Print("Error by OrderModify : ",GetLastError());
                     else
                        Print("Order modified successfully.");            
                  }
                  
               }
            }               
            else
               Print("Error by OrderSelect : ",GetLastError());
           }
         else
            Print("Error opening SHORT order : ",GetLastError());
      }         
 
thomas2004:

Here are the codes for Sell/Short, they are the similar or analog to Buy/Long:

The Print statements are printed in the experts tab of the terminal

 
Lakshan Perera:

The Print statements are printed in the experts tab of the terminal

Thanks, you are right!


Now I look at the Experts tab and find the following messages:

2018.05.02 21:00:00.958 EA_T05_MURI_Long_Short DAX.,M5: Error by OrderModify : 130
2018.05.02 21:00:00.818 EA_T05_MURI_Long_Short DAX.,M5: Error by OrderModify : 130
2018.05.02 21:00:00.646 EA_T05_MURI_Long_Short DAX.,M5: SHORT order opened : 12774.05
2018.05.02 21:00:00.646 EA_T05_MURI_Long_Short DAX.,M5: Current Time: 2018.05.02 22:00:00 Ask: 12776.05 Bid: 12774.05 Spread: 2.0


This could be because of the NormalizeDouble()? But your code hasn't dealed with the ST and TP. How can I do that?


And as I wrote before, why there is any problem by backtesting?

 
thomas2004:

Thanks, you are right!


Now I look at the Experts tab and find the following messages:

Great!
Error 130 is for invalid stops, now use the Function I wrote above to Normalize your SL and TPs

  // Handle Short Signal
   if(bEntryShort){
      if(iTotal>0)
         closeAllOpenedLongOrders();
      else{         
         int iShortTicket=OrderSend(Symbol(),OP_SELL,dLots,MarketInfo(Symbol(),MODE_BID),iSlippage,0,0,"T05_MURI",MAGIC_NUMBER,0,clrYellow);
         if(iShortTicket>0)
           {
            Print("Current Time: ", TimeCurrent()," Ask: ",Ask," Bid: ",Bid," Spread: ",(Ask-Bid));           
            if(OrderSelect(iShortTicket,SELECT_BY_TICKET,MODE_TRADES)){
               Print("SHORT order opened : ",OrderOpenPrice());
               if(bStopLossAndTakeProfit){
                  if(OrderCloseTime() == 0 && OrderStopLoss() == 0)
                  {
                     double StoppLoss = NormalizePrice(OrderOpenPrice() * ( 1 + SL_prozent/100));
                     bool OrderAngepasst = OrderModify(OrderTicket(),OrderOpenPrice(),StoppLoss,OrderTakeProfit(),0,Yellow);
                     if(!OrderAngepasst)
                        Print("Error by OrderModify : ",GetLastError());
                     else
                        Print("Order modified successfully.");            
                  }
                  
                  if(OrderCloseTime() == 0 && OrderTakeProfit() == 0)
                  {
                     double TakeProfit = NormalizePrice(OrderOpenPrice() / ( 1 + (TP_prozent /100)));
                     bool OrderAngepasst = OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),TakeProfit,0,Orange);
                     if(!OrderAngepasst)
                        Print("Error by OrderModify : ",GetLastError());
                     else
                        Print("Order modified successfully.");            
                  }
                  
               }
            }               
            else
               Print("Error by OrderSelect : ",GetLastError());
           }
         else
            Print("Error opening SHORT order : ",GetLastError());
      } 


// Somewhere in the global scope, place this function

double NormalizePrice(double price)
  {
        double tickSize = MarketInfo(Symbol(), MODE_TICKSIZE)
        return( MathRound(price/tickSize) * tickSize );
  } 
 
Lakshan Perera:

Great!
Error 130 is for invalid stops, now use the Function I wrote above to Normalize your SL and TPs

Many thanks! I will have a try.

 
thomas2004:

Hi,


After successful backtesting I launch my EA to live trading. If a long or short condition is TRUE, it will go long or short first and then set the ST and TP by using the OrderModify(). My code look as follow:


But I got following errors when a Buy or Sell order is filled:

Here one can see a sell-order is filled. But by modifying the ST and TP it failed.


Besides, accoding my code, if an order is opend, it should print out "Current Time:...". But I can't see this.

My first guess after seeing your code is your SL or TP is below broker's allowed StopLevel. Try to make your EA check it first by using MarketInfo(Symbol(),MODE_STOPLEVEL)

and I'm wondering, why you didn't directly use Stoploss and TakeProfit in OrderSend() function? That will reduce your line of code, isn't it?

Goodluck.

 
Lakshan Perera:

Great!
Error 130 is for invalid stops, now use the Function I wrote above to Normalize your SL and TPs

Hi Lakshan,

I've used your code in my EA. In fact it's much better. I got no error messages any more:

2018.05.07 12:25:06.284 EA_T05_MURI_Long_Short DAX.,M5: Order modified successfully.
2018.05.07 12:25:06.284 EA_T05_MURI_Long_Short DAX.,M5: modify #3549430 buy 0.20 DAX. at 12867.05 sl: 0.00 tp: 13510.40 ok
2018.05.07 12:25:06.128 EA_T05_MURI_Long_Short DAX.,M5: Order modified successfully.
2018.05.07 12:25:06.128 EA_T05_MURI_Long_Short DAX.,M5: modify #3549430 buy 0.20 DAX. at 12867.05 sl: 12803.05 tp: 0.00 ok
2018.05.07 12:25:05.972 EA_T05_MURI_Long_Short DAX.,M5: LONG order opened : 12867.05
2018.05.07 12:25:05.972 EA_T05_MURI_Long_Short DAX.,M5: Current Time: 2018.05.07 13:25:05 Ask: 12867.55 Bid: 12865.55 Spread: 2.0
2018.05.07 12:25:05.972 EA_T05_MURI_Long_Short DAX.,M5: open #3549430 buy 0.20 DAX. at 12867.05 ok

But one can see a buy-order is opened at 12867.05. The TP order at 13510.40 is modified but the ST at 12803.05 is not set. 

The output of The STOPLEVEL by DAX is as follow:

2018.05.07 13:35:00.246 PrintMarketInfo DAX.,H1: Stop level in points=0.0
Reason: