NormalizedDouble() error - Trailing stop function - page 2

 

corrected code

      double newStopLoss = 0;
      if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
      {
        newStopLoss = normalizePrice(Bid - trailingStopPointsapply);
        if(newStopLoss > openPrice && (stopLoss < openPrice || newStopLoss > stopLoss) && newStopLoss != stopLoss && checkStopLossTakeProfitLevels(Bid, newStopLoss, positionTP))
        {
          if(!trade.PositionModify(ticket, newStopLoss, positionTP))
          {
            PrintFormat("Error: Failed to modify position. Error code: %d", GetLastError());
          }
        }
      }
      else if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL)
      {
        newStopLoss = normalizePrice(Ask + trailingStopPointsapply);
        if(newStopLoss < openPrice && (stopLoss > openPrice || newStopLoss < stopLoss) && newStopLoss != stopLoss  && checkStopLossTakeProfitLevels(Ask, newStopLoss, positionTP))
        {
          if(!trade.PositionModify(ticket, newStopLoss, positionTP))
          {
            PrintFormat("Error: Failed to modify position. Error code: %d", GetLastError());
          }
        }
      }
 

I wanted to provide a quick update on the progress of the project. After carefully considering all of your insights and doing some further research, I am pleased to announce that the issues have been resolved. The latest version of the code has passed the market automatic validation and I have successfully completed the required testing with no errors. I appreciate all of your help and support throughout this process.



Please find the updated trail stop function below.

//+------------------------------------------------------------------+
//| Trailing stop                                                    |
//+------------------------------------------------------------------+
void applyTrailingStop(void)
  {
//Print("The code has entered the trailing stop function");
   double pipValue= GetPipValue(_Symbol);
//Print("The Pip value is ", pipValue);
   MqlTick currentTick;
   if(SymbolInfoTick(_Symbol, currentTick))
     {
      double Bid = currentTick.bid;
      //Print("The Trailing stop Bid price is: ", Bid);
      double Ask = currentTick.ask;
      //Print("The Trailing stop Ask price is: ", Ask);
      int totalOrders = PositionsTotal();
      //Print("The total open orders are: ", totalOrders);
      double trailingStopPointsapply = trailingStopPoints *pipValue;
      //Print("The Trailing stop in pips: ", trailingStopPointsapply);

      for(int i = 0; i < totalOrders; i++)
        {
         ulong ticket =PositionGetInteger(POSITION_TICKET);
         //Print("The current ticket number is: ", ticket);
         const string symbol = PositionGetString(POSITION_SYMBOL);
         //Print("The current position symbol is: ", symbol);
         //Print("=======================================================Sym = Symbol==================================== ",symbol==_Symbol);
         if(symbol != _Symbol)
           {
            //Print("Position symbol does not match chart symbol: ", symbol, _Symbol);
            continue; // Skip positions of other symbols
           }
         else
           {
            // Print("Position symbol  matches chart symbol: ", symbol, _Symbol);
           }

         if(PositionGetInteger(POSITION_MAGIC) != magicNumber)
           {
            // Print("Position magic number does not match EA magic number: ",PositionGetInteger(POSITION_MAGIC), magicNumber);
            continue; // Skip positions with a different magic number
           }
         else
           {
            //Print("Position magic number matches EA magic number: ",PositionGetInteger(POSITION_MAGIC), magicNumber);
           }

         double openPrice = PositionGetDouble(POSITION_PRICE_OPEN);
         //Print("Position Open Price: ", openPrice);
         double stopLoss = PositionGetDouble(POSITION_SL);
         double positionTP = PositionGetDouble(POSITION_TP);
         //Print("Position Take Profit: ", positionTP);
         int    freeze_level=(int)SymbolInfoInteger(symbol,SYMBOL_TRADE_FREEZE_LEVEL);
         //Print("Position Freeze Level: ", freeze_level);
         int    stop_level=(int)SymbolInfoInteger(symbol,SYMBOL_TRADE_STOPS_LEVEL);
         double price_level;
         //--- if the minimum allowed offset distance in points from the current close price is not set
         if(stop_level<=0)
            stop_level=150; // set the offset distance of 150 points from the current close price
         else
            stop_level+=50; // set the offset distance to (SYMBOL_TRADE_STOPS_LEVEL + 50) points for reliability

         //--- calculation and rounding of the Stop Loss and Take Profit values
         price_level=stop_level*SymbolInfoDouble(symbol,SYMBOL_POINT);

         double newStopLoss = 0;
         double point=SymbolInfoDouble(symbol,SYMBOL_POINT);



         if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
           {
            //Print("Current POSITION_TYPE_BUY");
            newStopLoss = normalizePrice(Bid - price_level - trailingStopPointsapply);
            bool StopLossChanged=(MathAbs(stopLoss-newStopLoss)>point);
            /*Print("New Stop Loss: ", newStopLoss);
            Print("Position Stop loss: ", stopLoss);
            Print("The difference betewwen the current stop loss and the new stop loss is: ",MathAbs(stopLoss - newStopLoss));

            Print("Trailing stop loss conditions check");
            Print("1. New stop loss greater than open price: ", newStopLoss > openPrice);
            Print("2. Current Stop Loss less than open price: ", newStopLoss > openPrice);
            Print("2.5 New Stop Loss greater than current stop loss: ", newStopLoss > stopLoss);
            Print("3. The new stop loss is not the current stoploss: ", newStopLoss != stopLoss);
            Print("3. Stop loss canged: ", StopLossChanged);
            Print("4. The new stop loss is not 0: ", newStopLoss != 0);
            Print("5. TBid - new stoploss is gretare than frezze level: ", Bid-newStopLoss >freeze_level);
            if(newStopLoss > openPrice && (stopLoss < openPrice || newStopLoss > stopLoss) && newStopLoss != stopLoss && newStopLoss != 0 && Bid-newStopLoss >freeze_level && StopLossChanged)
              {

               Print("--------------------ALL TRAILING STOP CONDINTIONS ARE TRUE------------------ ");

              }
              */

            if(newStopLoss > openPrice && (stopLoss < openPrice || newStopLoss > stopLoss) && newStopLoss != stopLoss && newStopLoss != 0 && Bid-newStopLoss >freeze_level && StopLossChanged)
              {
               if(!trade.PositionModify(ticket, newStopLoss, positionTP))
                 {
                  PrintFormat("Error: Failed to modify position. Error code: %d", GetLastError());
                 }
              }
           }
         else
            if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL)
              {
               //Print("Current POSITION_TYPE_SELL");
               newStopLoss = normalizePrice(Ask + price_level+ trailingStopPointsapply);
               bool StopLossChanged=(MathAbs(stopLoss-newStopLoss)>point);
               //Print("New Stop Loss: ", newStopLoss);
               //Print("Position Stop loss: ", stopLoss);
               //Print("The difference betewwen the new current stop loss and the new stop loss is: ",MathAbs(stopLoss - newStopLoss));
               if(newStopLoss < openPrice && (stopLoss > openPrice || newStopLoss < stopLoss) && newStopLoss != stopLoss && newStopLoss != 0 && newStopLoss-Ask > freeze_level && StopLossChanged)
                 {
                  if(!trade.PositionModify(ticket, newStopLoss, positionTP))
                    {
                     PrintFormat("Error: Failed to modify position. Error code: %d", GetLastError());
                    }
                 }
              }
        }
     }
  }