OrderSend Error 1 when trying to modify order

 

Hi,


I am struggeling with my code. I want to set Stoploss to break even as a start of a trailingstop. 

For BUY orders I only get error 1 all the time. (Not for SELL, there it works like a charm)


I cant figure out what Im doing wrong here.

Can I please have some assistant?


Thanks!

void SetTrailing()
{
    
      for (int i = 0; i < OrdersTotal(); i++) 
    {
        if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) 
        {
           if(OrderType() == OP_BUY)
           {   
               if (Bid-(ActivateTs*Point)>OrderOpenPrice() && OrderStopLoss()<OrderOpenPrice())
               {
                  if (OrderStopLoss()!=OrderOpenPrice())
                  {
                     Print(OrderStopLoss(),"  ",OrderOpenPrice());
                     if(!OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,clrNONE))
                     {
                        Print("Could not start Trailing Stop Buy",GetLastError());
                     }
                  }
               }
               if (OrderStopLoss()>=OrderOpenPrice())
               {
                  if (Bid-FloatingTs*Point>OrderStopLoss())
                  {
                     if (!OrderModify(OrderTicket(),OrderOpenPrice(),Bid-FloatingTs*Point,OrderTakeProfit(),0,clrNONE))
                     {
                        Print("Could not set floating trailing stop Buy", GetLastError());
                     }
                  }
               }
           }
           
           if (OrderType() == OP_SELL)
           {   
               if (Ask+(ActivateTs*Point)<OrderOpenPrice() && OrderStopLoss()>OrderOpenPrice())
               {  
                  if(!OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,clrNONE))
                  {
                     Print("Could not start Trailing Stop Sell", GetLastError());
                  }
               }
               if(OrderStopLoss()<=OrderOpenPrice())
               {  
                  if (Ask+FloatingTs*Point<OrderStopLoss())
                  {
                     if (!OrderModify(OrderTicket(),OrderOpenPrice(),Ask+FloatingTs*Point,OrderTakeProfit(),0,clrNONE))
                     {
                        Print("Could not set floating trailing stop Sell", GetLastError());
                     }
                  }
               }
            }
         }
     }
}
 
  1.                   if (OrderStopLoss()!=OrderOpenPrice())
    
    1. Doubles are rarely equal. Understand the links in:
                The == operand. - MQL4 programming forum #2 2013.06.07

    2. Make sure you have moving the SL at least a point.

      double newSL = Bid-ActivateTs*Point;
      double curSL = MathMax(OrderOpenPrice(), OrderStopLoss() );
      if (newSL - curSL > _Point)

  2.       for (int i = 0; i < OrdersTotal(); i++) {
            if (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 (non-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 programming forum
    2. For In First Out (FIFO rules — US brokers), and you (potentially) process multiple orders per symbol, you must find the earliest order (count up), close it, and on a successful operation, reprocess all positions.
                CloseOrders by FIFO Rules - Strategy Tester - MQL4 programming forum - Page 2 #16
                MetaTrader 5 platform beta build 2155: MQL5 scope, global Strategy Tester and built-in Virtual Hosting updates - Best Expert Advisors - General - MQL5 programming forum #1.11 ACCOUNT_FIFO_CLOSE

    3. and check OrderSelect in case later positions were deleted.
                What are Function return values ? How do I use them ? - MQL4 programming forum
                Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
    4. 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 instead, be direction independent and just use OrderClosePrice().

  3. Magic number only allows an EA to identify its trades from all others. Using OrdersTotal/OrdersHistoryTotal (MT4) or PositionsTotal (MT5), directly and/or no Magic number/symbol filtering on your OrderSelect / Position select 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 programming forum 2013.02.15
              PositionClose is not working - MQL5 programming forum 2020.02.21
              MagicNumber: "Magic" Identifier of the Order - MQL4 Articles 24 July 2006
              Orders, Positions and Deals in MetaTrader 5 - MQL5 Articles Small>1 February 2011

 
William Roeder:
    1. Doubles are rarely equal. Understand the links in:
                The == operand. - MQL4 programming forum #2 2013.06.07

    2. Make sure you have moving the SL at least a point.

  1. 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 (non-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 programming forum
    2. For In First Out (FIFO rules — US brokers), and you (potentially) process multiple orders per symbol, you must find the earliest order (count up), close it, and on a successful operation, reprocess all positions.
                CloseOrders by FIFO Rules - Strategy Tester - MQL4 programming forum - Page 2 #16
                MetaTrader 5 platform beta build 2155: MQL5 scope, global Strategy Tester and built-in Virtual Hosting updates - Best Expert Advisors - General - MQL5 programming forum #1.11 ACCOUNT_FIFO_CLOSE

    3. and check OrderSelect in case later positions were deleted.
                What are Function return values ? How do I use them ? - MQL4 programming forum
                Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
    4. 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 instead, be direction independent and just use OrderClosePrice().

  2. Magic number only allows an EA to identify its trades from all others. Using OrdersTotal/OrdersHistoryTotal (MT4) or PositionsTotal (MT5), directly and/or no Magic number/symbol filtering on your OrderSelect / Position select 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 programming forum 2013.02.15
              PositionClose is not working - MQL5 programming forum 2020.02.21
              MagicNumber: "Magic" Identifier of the Order - MQL4 Articles 24 July 2006
              Orders, Positions and Deals in MetaTrader 5 - MQL5 Articles Small>1 February 2011

Thank you for the input. When I converted OrderStoploss() and OrderOpenPrice() to int variables before not equal check, then it worked without errors!


About the rest, I know about it. Just trying to get the EA to work correct without any errors. Magic Number will be put in before I go live with it and will ofcourse be included in any loop for finding orders.


Thanks again for good feedback!

Reason: