ordermodify error 131 - take profit price error

 

Hello,

I am going to use the highest price as take profit price,

ans set stop loss price = 0 always.

Sometimes the code runs well, sometimes it show error 130,

would you please give me some hints to fix this error ?

Thanks.



   for(i=0;i<=OrdersTotal();i++)   {
      buy_tp=High[iHighest(Symbol(),0,MODE_HIGH,in_period,1)];  
     
      OrderSelect(i,SELECT_BY_POS,MODE_TRADES);

      //
      if(OrderSymbol()==Symbol() && (OrderType()==OP_BUY || OrderType()==OP_BUYLIMIT) && OrderMagicNumber()==magicno)    {           
         //if current price < tp
         if(OrderOpenPrice()<buy_tp && OrderTakeProfit()!=buy_tp ) {
            OrderModify(OrderTicket(),OrderOpenPrice(),0,buy_tp,0,White);
         }
        
         //if current price > tp
         if(OrderOpenPrice()>buy_tp && OrderTakeProfit()!=buy_tp ) {
            OrderModify(OrderTicket(),OrderOpenPrice(),buy_tp,0,0,White);
         }        
      }
                      
   }   
 

Chihming Tsao:

would you please give me some hints to fix this error ?


This code section:

         //if current price > tp
         if(OrderOpenPrice()>buy_tp && OrderTakeProfit()!=buy_tp ) {
            OrderModify(OrderTicket(),OrderOpenPrice(),buy_tp,0,0,White);
         }

Error 130 is invalid stop.

You are setting the STOPLOSS to the buy_tp value and setting the TP value to 0.

If this is a buy order, then you are setting the Stoploss at a greater value than the current price.

This means the price is below your stoploss value on a buy ticket.

In regard to Error 131.

Error 131 is invalid lot size. Make sure you are using a lot size that is allowed for your account.


Error codes page: https://docs.mql4.com/constants/errorswarnings/errorcodes

And this one too: https://docs.mql4.com/constants/errorswarnings/enum_trade_return_codes

Runtime Errors - Codes of Errors and Warnings - Standard Constants, Enumerations and Structures - MQL4 Reference
Runtime Errors - Codes of Errors and Warnings - Standard Constants, Enumerations and Structures - MQL4 Reference
  • docs.mql4.com
The GetLastError() function return last error code, stored in _LastError predefined variable. This value can be reset using the ResetLastError() function. Error code constants defined at stderror.mqh file. To print text messages use ErrorDescription() function defined at stdlib.mqh file.
Reason: