how to modify trade wit ctrade ?

 

Hi guys I’m trying to modify a trade with ctrade, but I get this error:

2025.09.19 09:15:49.991  PARTIALTAKEandSTP (EURUSD,M5)  Requested SET SL 1.18000 for ticket 21821394 (file symbol: EURUSD)
2025.09.19 09:15:49.991  PARTIALTAKEandSTP (EURUSD,M5)  SetPositionStopLoss: PositionModify failed for EURUSD SL=1.18000 TP=0.00000 - Error: 0
2025.09.19 09:15:49.991  PARTIALTAKEandSTP (EURUSD,M5)  Error setting StopLoss.

I’m calling the function like this – do you think it’s correct?

double tpToSet = (currentTP > 0) ? currentTP : EMPTY_VALUE; 
bool res = trade.PositionModify(sym, slPrice, tpToSet);
if(!res)
  {
   int err = GetLastError();
   PrintFormat("SetPositionStopLoss: PositionModify failed for %s SL=%.5f TP=%.5f - Error: %d", sym, slPrice, currentTP, err);
   // some brokers require a minimum distance (stop level) or the price is not accepted: log the error
   return false;
  }

Trade Operations in MQL5 - It's Easy
Trade Operations in MQL5 - It's Easy
  • 2012.08.21
  • www.mql5.com
Almost all traders come to market to make money but some traders also enjoy the process itself. However, it is not only manual trading that can provide you with an exciting experience. Automated trading systems development can also be quite absorbing. Creating a trading robot can be as interesting as reading a good mystery novel.
 
Stefano Cerbioni:

Hi guys I’m trying to modify a trade with ctrade, but I get this error:

I’m calling the function like this – do you think it’s correct?

Are you selecting the position first with PositionSelectByTicket() ? Your function should start something like this: 

void TrailingStop(){
   for (int i = PositionsTotal()-1; i >= 0; i--){                                                                           // Loop through all open positions
      ulong posTicket = PositionGetTicket(i);                                                                               // Get the current position's ticket in the loop
      if(PositionSelectByTicket(posTicket)){                                                                                // Select position ticket
        // Now you can use CTrade and modify stuff in here
      } 
   }
}