Error modifying order!, error#130

To add comments, please log in or register
texcs81
47
texcs81  
double           IniTrailingStop =MarketInfo( Symbol(), MODE_STOPLEVEL );
void stopPercentuale(double InitialTrailingStop , double  trailing_stop_percentage)
 {
   double PointValue;
   for (int i = 0; i < OrdersTotal(); i++) 
      {
         OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
         //Calculate the point value in case there are extra digits in the quotes
         if (MarketInfo(OrderSymbol(), MODE_POINT) == 0.00001) PointValue = 0.0001;
         else if (MarketInfo(OrderSymbol(), MODE_POINT) == 0.001) PointValue = 0.01;
         else PointValue = MarketInfo(OrderSymbol(), MODE_POINT);
         //Normalize trailing stop value to the point value
         double InitialTrailingStop  = InitialTrailingStop * PointValue;
         if (OrderType() == OP_BUY)
         {
            if ((trailing_stop_percentage > 0) && (Bid - OrderOpenPrice() >= InitialTrailingStop ))
               { InitialTrailingStop  = NormalizeDouble((Bid - OrderOpenPrice()) * trailing_stop_percentage / 100, Digits);
                  { 
                  if (OrderStopLoss() < (Bid - InitialTrailingStop )){
                     bool res = OrderModify(OrderTicket(), OrderOpenPrice(), Bid - InitialTrailingStop , OrderTakeProfit(), Red); 
                     if ( res == false )
                        Print( "Error modifying order!, error#", GetLastError() );
               }  }
               }
         }
         else if ((OrderStopLoss() != Bid - StopLoss * PointValue) && (StopLoss != 0)){
           bool res = OrderModify(OrderTicket(), OrderOpenPrice(), Bid - StopLoss * PointValue, OrderTakeProfit(), Red);
           if ( res == false )
            Print( "Error modifying order!, error#", GetLastError() );
            }
      
         else if (OrderType() == OP_SELL)
            if ((trailing_stop_percentage > 0) && (OrderOpenPrice() - Ask >= InitialTrailingStop ))
            {  InitialTrailingStop  = NormalizeDouble((OrderOpenPrice() - Ask) * trailing_stop_percentage / 100, Digits);
              { if ((OrderStopLoss() > (Ask + InitialTrailingStop * PointValue)) || (OrderStopLoss() == 0))
                   OrderModify(OrderTicket(), OrderOpenPrice(), Ask + InitialTrailingStop , OrderTakeProfit(), Red);
              }
            }
          
         else if ((OrderStopLoss() != Ask + StopLoss * PointValue) && (StopLoss != 0))
            OrderModify(OrderTicket(), OrderOpenPrice(), Ask + StopLoss * PointValue, OrderTakeProfit(), Red);
            
            }
             
   }

Guys, I did this function but it gives me this error" EURUSD,H1: Error modifying order!, error#130" and I don't understand why since " double IniTrailingStop =MarketInfo( Symbol(), MODE_STOPLEVEL )"

William Roeder
19974
William Roeder  
  1. double           IniTrailingStop =MarketInfo( Symbol(), MODE_STOPLEVEL );
    
    Don't try to use any price or server related functions in OnInit (or on load,) as there may be no connection/chart yet:
    1. Terminal starts.
    2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
    3. OnInit is called.
    4. For indicators OnCalculate is called with any existing history.
    5. Human may have to enter password, connection to server begins.
    6. New history is received, OnCalculate called again.
    7. New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.

  2.    for (int i = 0; i < OrdersTotal(); i++){
             OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
    In the presence of multiple orders (one EA multiple charts, multiple EAs, manual trading,) while you are waiting for the current operation (closing, deleting, modifying) to complete, any number of other operations on other orders could have concurrently happened and changed the position indexing:
    1. For non-FIFO (US brokers,) (or the EA only opens one order per symbol,) you can simply count down in a position loop, and you won't miss orders. Get in the habit of always counting down.
                Loops and Closing or Deleting Orders - MQL4 and MetaTrader 4 - MQL4 programming forum
      For FIFO (US brokers,) and you (potentially) process multiple orders per symbol, you must count up and on a successful operation, reprocess all positions (set index to -1 before continuing.)
    2. and check OrderSelect in case earlier positions were deleted.
                What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
                Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
    3. and if you (potentially) process multiple orders, must call RefreshRates() after server calls if you want to use, on the next order / server call, the Predefined Variables (Bid/Ask) or (be direction independent and use) OrderClosePrice().

  3. if ((trailing_stop_percentage > 0) && (Bid - OrderOpenPrice() >= InitialTrailingStop ))
    
    1. Your loop is processing all orders, on all charts, from all sources. Bid is the current chart only.

    2. Using OrdersTotal directly and/or no Magic number filtering on your OrderSelect loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
                Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 and MetaTrader 4 - MQL4 programming forum
                MagicNumber: "Magic" Identifier of the Order - MQL4 Articles

  4. if (OrderType() == OP_BUY)
    {
    :
    }
    else if ((OrderStopLoss() != Bid - StopLoss * PointValue) && (StopLoss != 0)){
    :
       }
    
    else if (OrderType() == OP_SELL)
    
    If it's not a buy order it must be a sell. What is that first else if doing? The second else if will likely never run.

  5. Doubles are rarely equal. Understand the links in:
              The == operand. - MQL4 and MetaTrader 4 - MQL4 programming forum
To add comments, please log in or register