Order Modify Error 130

 

Hi,

I have written the code below for trailing stoploss. it does work but on the other end it seems to keep spamming lots of error in the log too. which makes me confuse.

input int                     TrailingStart           = 150; //Trailing Start
input int                     TraillingStop           = 30; //Trailing Stoploss

void TrailStops()
{
   
    
    for (int i = 0; i < OrdersTotal(); i++) {
        if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
            continue;
        }
  
        if (OrderSymbol() != Symbol()) {
            continue;
        }
        
        if (OrderMagicNumber() != MagicNumber) {
            continue;
        } 
        
        if (OrderComment() != Comment) {
            continue;
        }
       if((OrderType()==OP_BUY) && (CheckTotalNumberOfOrders_Buy() == 1))
       {
           if(((Bid - OrderOpenPrice()) > (Point*TrailingStart)) && (OrderStopLoss() < Bid - Point*TraillingStop ))
           {
               

                  
                  int ticket=OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Bid-Point*TraillingStop,Digits),OrderTakeProfit(),0,Blue);
                  
                  if(ticket <= 0) return;
                  
              
           } 

        } 
        if((OrderType()==OP_SELL) && (CheckTotalNumberOfOrders_Sell() == 1))
        {
          if(((OrderOpenPrice() - Ask) > (Point*TrailingStart)) && ((OrderStopLoss() > Ask + Point*TraillingStop) || (OrderStopLoss() == 0)))
            {
                   
                   int ticket=OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Ask+Point*TraillingStop,Digits),OrderTakeProfit(),0,Red);
                   
                   if(ticket <= 0) return;

            }       
        }
    }
   
}

void OnTick()
  { 
   if (( (CheckTotalNumberOfOrders_Buy() == 1)||(CheckTotalNumberOfOrders_Sell() == 1)))
   {
      TrailStops();
   }
}
 
           if(((Bid - OrderOpenPrice()) > (Point*TrailingStart)) && (OrderStopLoss() < Bid - Point*TraillingStop ))
           {           
                  int ticket=OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Bid-Point*TraillingStop,Digits),OrderTakeProfit(),0,Blue);
                  
                  if(ticket <= 0) return;
           } 

You normalize the SL when you set it, but you don't normalize when you check it.

OrderModify returns a bool, not an int.


Topics concerning MT4 and MQL4 have their own section.

In future please post in the correct section.

I have moved your topic to the MQL4 and Metatrader 4 section.

 
Keith Watford:

You normalize the SL when you set it, but you don't normalize when you check it.

OrderModify returns a bool, not an int.


Topics concerning MT4 and MQL4 have their own section.

In future please post in the correct section.

I have moved your topic to the MQL4 and Metatrader 4 section.

Hi Thanks,

I just changed it to 

       if((OrderType()==OP_BUY) && (CheckTotalNumberOfOrders_Buy() == 1))
       {
           if(((Bid - OrderOpenPrice()) > (Point*TrailingStart)) && (OrderStopLoss() < NormalizeDouble(Bid-Point*TraillingStop,Digits)))
           {
               

                  //Trailing_SL_Point = Bid-Point*TraillingStop;
                  int ticket=OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Bid-Point*TraillingStop,Digits),OrderTakeProfit(),0,Blue);
                  //Print("modify BUY :", DoubleToString( NormalizeDouble(Bid-Point*TraillingStop,Digits)));
                  if(ticket <= 0) return;
                  
              
           } 

        } 
        if((OrderType()==OP_SELL) && (CheckTotalNumberOfOrders_Sell() == 1))
        {
          if(((OrderOpenPrice() - Ask) > (Point*TrailingStart)) && ((OrderStopLoss() > NormalizeDouble(Ask+Point*TraillingStop,Digits)) || (OrderStopLoss() == 0)))
            {
                   //Trailing_SL_Point = Ask+Point*TraillingStop;
                   int ticket=OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Ask+Point*TraillingStop,Digits),OrderTakeProfit(),0,Red);
                   //Print("modify SELL :", DoubleToString( NormalizeDouble(Ask+Point*TraillingStop,Digits)));
                   if(ticket <= 0) return;

            }       
        }

but it still same issue.

I can see the order being modified. however in the log it shows OrderModify Error 130. even though it manages to modify the order itself (Backtest).

 
Budiman Lays:

Hi Thanks,

I just changed it to 

but it still same issue.

I can see the order being modified. however in the log it shows OrderModify Error 130. even though it manages to modify the order itself (Backtest).

Please Change this code

if((OrderType()==OP_BUY) && (CheckTotalNumberOfOrders_Buy() == 1))
       {
          // if(((Bid - OrderOpenPrice()) > (Point*TrailingStart)) && (OrderStopLoss() < NormalizeDouble(Bid-Point*TraillingStop,Digits)))
           if(((Bid - OrderOpenPrice()) > (Point*TrailingStart)) && (OrderStopLoss() < NormalizeDouble(OrderOpenPrice()-Point*TraillingStop,Digits)))
          
           {
               

                  //Trailing_SL_Point = Bid-Point*TraillingStop;
                 // int ticket=OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Bid-Point*TraillingStop,Digits),OrderTakeProfit(),0,Blue);
                  int ticket=OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(OrderOpenPrice()-Point*TraillingStop,Digits),OrderTakeProfit(),0,Blue);
                 
                  //Print("modify BUY :", DoubleToString( NormalizeDouble(Bid-Point*TraillingStop,Digits)));
                  if(ticket <= 0) return;
                  
              
           } 

        } 
        if((OrderType()==OP_SELL) && (CheckTotalNumberOfOrders_Sell() == 1))
        {
          //if(((OrderOpenPrice() - Ask) > (Point*TrailingStart)) && ((OrderStopLoss() > NormalizeDouble(Ask+Point*TraillingStop,Digits)) || (OrderStopLoss() == 0)))
          
          if(((OrderOpenPrice() - Ask) > (Point*TrailingStart)) && ((OrderStopLoss() > NormalizeDouble(OrderOpenPrice()+Point*TraillingStop,Digits)) || (OrderStopLoss() == 0)))
            
            {
                   //Trailing_SL_Point = Ask+Point*TraillingStop;
                   //int ticket=OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Ask+Point*TraillingStop,Digits),OrderTakeProfit(),0,Red);
                   int ticket=OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(OrderOpenPrice()+Point*TraillingStop,Digits),OrderTakeProfit(),0,Red);
                   
                   
                   //Print("modify SELL :", DoubleToString( NormalizeDouble(Ask+Point*TraillingStop,Digits)));
                   if(ticket <= 0) return;

            }       
        }
 
I think I found the issue. it seems sometime it has issue in modifying the order on the wrong tickets. hmm but sometime not. weird. not sure if this is just issue with my Backtest MT4 client.
 

I have just made some adjustment , but still seems the trailing sometime try to modify wrong OrderTicket. any idea everyone?


void TrailStops()
{
   
    
    for (int i = 0; i < OrdersTotal(); i++) {
    
        if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
            continue;
        }
  
        if (OrderSymbol() != Symbol()) {
            continue;
        }
        
        if (OrderMagicNumber() != MagicNumber) {
            continue;
        } 
        
        if (OrderComment() != Comment) {
            continue;
        }
       if((OrderType()==OP_BUY) && (CheckTotalNumberOfOrders_Buy() == 1))
       {
           if(((Bid - OrderOpenPrice()) > (Point*TrailingStart)) && (OrderStopLoss() < NormalizeDouble(Bid-Point*TraillingStop,Digits)))
           {
               
               if (OrderStopLoss() == 0)
               {
                  
                  bool ticket=OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Bid-Point*TraillingStop,Digits),OrderTakeProfit(),0,Blue);
                  Print("modify BUY :", OrderTicket());
                  if(ticket) Print("Order Stop Loss Has Been Modified Successfully.");
                  
               } else {
                  if ((OrderStopLoss() + TrailingStep*Point) < NormalizeDouble(Bid-Point*TraillingStop,Digits))
                  {
                     bool ticket=OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Bid-Point*TraillingStop,Digits),OrderTakeProfit(),0,Blue);
                     Print("modify BUY :", OrderTicket());
                     if(ticket) Print("Order Stop Loss Has Been Modified Successfully.");
                  }
               
               }
                  
              
           } 

        } 
        if((OrderType()==OP_SELL) && (CheckTotalNumberOfOrders_Sell() == 1))
        {
          if(((OrderOpenPrice() - Ask) > (Point*TrailingStart)) && ((OrderStopLoss() > NormalizeDouble(Ask+Point*TraillingStop,Digits)) || (OrderStopLoss() == 0)))
            {
               if (OrderStopLoss() == 0)
               {
                   
                   bool ticket=OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Ask+Point*TraillingStop,Digits),OrderTakeProfit(),0,Red);
                   Print("modify SELL :", OrderTicket());
                   if(ticket) Print("Order Stop Loss Has Been Modified Successfully.");
               } else {
                  if ((OrderStopLoss()-TrailingStep*Point) > NormalizeDouble(Ask+Point*TraillingStop,Digits))
                  {
                      bool ticket=OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Ask+Point*TraillingStop,Digits),OrderTakeProfit(),0,Red);
                      Print("modify SELL :", OrderTicket());
                      if(ticket) Print("Order Stop Loss Has Been Modified Successfully.");
                  }
                  
               }

            }       
        }
    }
   
}
 
Managed to resolve it myself it is due to my slow PC I am running on the MT4 clients.
Reason: