MQL4 OrderModify Error 130 After OrderSend

 

Hi all experts,

I am new to MQL programming and try on some EA programming but facing problem whenever call to OrderModify(). There is no compilation error but on running the EA in tester will give Error 130 on OrderModify() call. Attached below my create order function code for review.
Any advice and comments are welcome.
Thanks in advance.

void PlaceOrder(int op_type)
{
   int magicNo = 199999;
   double Lots = 0.1;
   double price = 0;
   double TProfit = 0;
   string ordColor = "Red";
   double TrailingStop= 30;
   double TakeProfit = 150;
   
   if (op_type == OP_SELL)
   {
      price = Bid;
      TProfit = price-TakeProfit*Point;
      ordColor = "Red";
      slPrice = NormalizeDouble(OrderOpenPrice()-(Point*TrailingStop),Digits);
   }
   else
   {    
      price = Ask;
      TProfit = price+TakeProfit*Point;
      ordColor = "Green";   
      slPrice = NormalizeDouble(OrderOpenPrice()+(Point*TrailingStop),Digits);                  
   }
    int totalOrders=OrdersTotal();
    int ticket=OrderSend(Symbol(),op_type,Lots,price,3,0,TProfit,"#Order "+totalOrders,magicNo,0,ordColor);
    if(ticket>0)
    {   
          if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) 
          {
            ticket = OrderTicket();          
            Print(op_type," order opened : ",price," Stop lost : ",slPrice, " | Selected ticket : #",ticket, " | " + totalOrders);            
            if (op_type == OP_SELL)
            {
               price = Bid;
               TProfit = price-TakeProfit*Point;
               ordColor = "Red";
               slPrice = price-(Point*TrailingStop);
            }
            else
            {    
               price = Ask;
               TProfit = price+TakeProfit*Point;
               ordColor = "Green";   
               slPrice = price+(Point*TrailingStop);                  
            }
            OpenOrdersChk();
            if (OrderModify(ticket,price,slPrice,TProfit,0,ordColor)==false)
              Print("Error OrderModify : #",ticket, " Price: ",price," SL: ",slPrice," TP: ",TProfit);
          } 

    } else Print("Error opening SELL order : ",GetLastError());      
} 
 
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.


I don't know what the point of this is

slPrice = NormalizeDouble(OrderOpenPrice()+(Point*TrailingStop),Digits);  

You have to select an order before you can use OrderOpenPrice().

   
    int ticket=OrderSend(Symbol(),op_type,Lots,price,3,0,TProfit,"#Order "+totalOrders,magicNo,0,ordColor);
    if(ticket>0)
    {   
          if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) 
          {
            ticket = OrderTicket();       
            if (op_type == OP_SELL)
            {
               price = Bid;
               TProfit = price-TakeProfit*Point;
               ordColor = "Red";
               slPrice = price-(Point*TrailingStop);
            }
            OpenOrdersChk();
            if (OrderModify(ticket,price,slPrice,TProfit,0,ordColor)==false)
              Print("Error OrderModify : #",ticket, " Price: ",price," SL: ",slPrice," TP: ",TProfit);

For a sell you are trying to modify the SL so that it will be below the current price.
It must be above the current price.