Stop Move EA - Troubleshooting

 

Hello, 

I have tried to create an EA which does the following:


If open position moves in profit by 140 points, stop-loss moves in favour by 75 points,
then, if open position moves in profit by 265 points, stop loss moves in favour again by 175 points from previous position. 

Here is an example of the code:


// Define input parameters
input double firstTrigger = 140.0;
input double firstMove = 75.0;
input double secondTrigger = 265.0;
input double secondMove = 175.0;

// Define variables
double initialStopLoss;
double currentStopLoss;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTick()
{
   // Loop through open positions
   for (int i = 0; i < PositionsTotal(); i++)
   {
      if (PositionSelect(i))
      {
         // Check if the position is in profit
         if (PositionGetDouble(POSITION_PROFIT) > 0)
         {
            // Calculate the current profit in points
            double profitInPoints = PositionGetDouble(POSITION_PROFIT) / SymbolInfoDouble(_Symbol, SYMBOL_POINT);

            // Check for the second trigger first
            if (profitInPoints >= secondTrigger)
            {
               currentStopLoss = PositionGetDouble(POSITION_SL) + secondMove * SymbolInfoDouble(_Symbol, SYMBOL_POINT);
            }
            // Then check for the first trigger
            else if (profitInPoints >= firstTrigger)
            {
               currentStopLoss = PositionGetDouble(POSITION_SL) + firstMove * SymbolInfoDouble(_Symbol, SYMBOL_POINT);
            }
            // If neither trigger is reached, maintain the initial stop loss
            else
            {
               currentStopLoss = initialStopLoss;
            }

            // Update the stop loss if it has changed
            if (currentStopLoss != PositionGetDouble(POSITION_SL))
            {
               bool result = PositionModify(PositionGetInteger(POSITION_TICKET), currentStopLoss, PositionGetDouble(POSITION_TP));
               if (result)
               {
                  Print("Stop loss updated for Position #", IntegerToString(PositionGetInteger(POSITION_TICKET)));
               }
               else
               {
                  Print("Failed to update stop loss for Position #", IntegerToString(PositionGetInteger(POSITION_TICKET)));
               }
            }
         }
      }
   }
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTradeTransaction(const MqlTradeTransaction &trans, const MqlTradeRequest &request, const MqlTradeResult &result)
{
   // Check for a new position
   if (trans.type == TRADE_TRANSACTION_DEAL_ADD && request.action == TRADE_ACTION_DEAL)
   {
      // Reset the trailing stop data for the new position
      if (PositionSelect(Symbol(), request.volume))
      {
         initialStopLoss = PositionGetDouble(POSITION_SL);
         currentStopLoss = initialStopLoss;
      }
   }
}



Here are the errors I have received: 

implicit conversion from 'number' to 'string'									TSSL.mq5	19	26
'PositionModify' - undeclared identifier									TSSL.mq5	46	30
',' - unexpected token												TSSL.mq5	46	80
'PositionGetInteger' - some operator expected									TSSL.mq5	46	45
'currentStopLoss' - semicolon expected										TSSL.mq5	46	82
',' - unexpected token												TSSL.mq5	46	97
expression has no effect											TSSL.mq5	46	82
')' - unexpected token												TSSL.mq5	46	129
'PositionSelect' - wrong parameters count									TSSL.mq5	70	11
   built-in: bool PositionSelect(const string)									TSSL.mq5	70	11


Any advice on this would be greatly appreciated, thanks.

Basic Principles - Trading Operations - MetaTrader 5 Help
  • www.metatrader5.com
Before you proceed to study the trade functions of the platform, you must have a clear understanding of the basic terms: order, deal and position...