Breakeven my order. Please help!

 

Hi all, 

I use this code to set Breakeven for my EA, but i got message :

2025.02.05 23:14:28.843 2025.01.20 05:51:32   failed modify #2 buy 1 XAUUSD sl: 0.00, tp: 0.00 -> sl: 0.00, tp: 0.00 [Invalid stops]

2025.02.05 23:14:28.843 2025.01.20 05:51:32   OrderSend error 4756

2025.02.05 23:14:28.843 2025.01.20 05:51:35   retcode=10016  deal=0  order=0

I tried many times to correct it but not work. Please help me to correct it. Thank you so much

void Breakeven()
{
//--- declare and initialize the trade request and result of trade request
   MqlTradeRequest request;
   MqlTradeResult  result;   
   
   int total=PositionsTotal(); // number of open positions   
//--- iterate over all open positions
   for(int i=0; i<total; i++)
     {
      //--- parameters of the order
      ulong  position_ticket=PositionGetTicket(i);// ticket of the position
      string position_symbol=PositionGetString(POSITION_SYMBOL); // symbol 
      int    digits=(int)SymbolInfoInteger(position_symbol,SYMBOL_DIGITS); // number of decimal places
      ulong  magic=PositionGetInteger(POSITION_MAGIC); // MagicNumber of the position
      double volume=PositionGetDouble(POSITION_VOLUME);    // volume of the position
      double sl=PositionGetDouble(POSITION_SL);  // Stop Loss of the position      
      ENUM_POSITION_TYPE type=(ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);  // type of the position
      
      if(magic==Expert_Magic && sl == 0)
        { 
         //--- calculate the current price levels
         double price=PositionGetDouble(POSITION_PRICE_OPEN);
         double bid=SymbolInfoDouble(position_symbol,SYMBOL_BID);
         double ask=SymbolInfoDouble(position_symbol,SYMBOL_ASK);
                  
         //--- calculation and rounding of the Stop Loss and Take Profit values         
         if(type==POSITION_TYPE_BUY)
         {
            if(ask - price  >= 200*_Point) 
            {
               sl = price + 200*_Point; 
            } 
         }
         if (type==POSITION_TYPE_SELL)
         {
            if (price - bid >=  200*_Point)
            {
               sl = price - 200*_Point;
            }
         }   
         //--- zeroing the request and result values
         ZeroMemory(request);
         ZeroMemory(result);
         //--- setting the operation parameters
         request.action    =  TRADE_ACTION_SLTP; // type of trade operation
         request.position  =  position_ticket;   // ticket of the position
         request.symbol    =  position_symbol;     // symbol 
         request.sl        =  sl;                // Stop Loss of the position         
         request.magic     =  Expert_Magic;         // MagicNumber of the position
         //--- output information about the modification
         PrintFormat("Modify #%I64d %s %s",position_ticket,position_symbol,EnumToString(type));
         //--- send the request
         if(!OrderSend(request,result))
            PrintFormat("OrderSend error %d",GetLastError());  // if unable to send the request, output the error code
         //--- information about the operation   
         PrintFormat("retcode=%u  deal=%I64u  order=%I64u",result.retcode,result.deal,result.order);
        }
     }

}



 
Tomq:
2025.02.05 23:14:28.843 2025.01.20 05:51:32   failed modify #2 buy 1 XAUUSD sl: 0.00, tp: 0.00 -> sl: 0.00, tp: 0.00 [Invalid stops]

If you're trading a 2 or 3 digit symbol, you might not be respecting your broker-dealer's minimum stop distance. This will cause an invalid stops error.

See SYMBOL_TRADE_STOPS_LEVEL:

Symbol Properties - Environment State - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
Documentation on MQL5: Constants, Enumerations and Structures / Environment State / Symbol Properties
Documentation on MQL5: Constants, Enumerations and Structures / Environment State / Symbol Properties
  • www.mql5.com
To obtain the current market information there are several functions: SymbolInfoInteger() , SymbolInfoDouble() and SymbolInfoString() . The first...
 
#include <Trade\Trade.mqh>
CTrade trade;

int be_points = 200;

void BreakEven()
{
   CPositionInfo pos;
   double bid = SymbolInfoDouble(pos.Symbol(), SYMBOL_BID);
   double ask = SymbolInfoDouble(pos.Symbol(), SYMBOL_ASK);
   for (int i=0; i<PositionsTotal(); i++)
   {
      if (pos.SelectByIndex(i) && pos.Symbol()==_Symbol && pos.Magic()==Expert_Magic)
      {
         if (pos.PositionType() == POSITION_TYPE_BUY)
         {
            double be_target = (pos.PriceOpen() + be_points*_Point));

            if (pos.StopLoss()<=pos.PriceOpen() && bid>=be_target)
            {
               trade.PositionModify(pos.Ticket(), pos.PriceOpen(), pos.TakeProfit());
            }
         }
         else if (pos.PositionType() == POSITION_TYPE_SELL)
         {
            double be_target = (pos.PriceOpen() -  be_points*_Point));

            if (pos.StopLoss()>=pos.PriceOpen() && bid<=be_target)
            {
               trade.PositionModify(pos.Ticket(), pos.PriceOpen(), pos.TakeProfit());
            }
         }
      }
   }
}

I use a different approach as yours, this code snippet is easier to read.


Your code's conditional: 

sl == 0

Is now this:

pos.StopLoss()>=pos.PriceOpen()

So in the future if you want to add a Stoploss to your strategy it can adapt accordingly.