Please help with MT5 EA code error

 
//+------------------------------------------------------------------+
//|                Antfruition.mq5                                   |
//|                Expert Advisor for MT5                            |
//+------------------------------------------------------------------+
#property strict
#include <Trade/Trade.mqh>

CTrade trade;

// Input parameters
input int ATR_Period = 14;
input int BaseTakeProfitPips = 20;
input double ATR_Multiplier = 1.5;
input int StopLossBufferPips = 15;
input int RetestBufferPips = 2;
input bool UseCandleConfirmation = true;
input double LotSize = 0.1;
input int MagicNumber = 12345;
input int Slippage = 3;

// Global variables for session tracking
bool sessionLevelsCaptured = false;
double sessionHigh = 0;
double sessionLow = 0;

//+------------------------------------------------------------------+
//| Count orders by Magic Number                                     |
//+------------------------------------------------------------------+
int CountOrdersByMagic(int magic)
{
    int count = 0;
    for (int i = PositionsTotal() - 1; i >= 0; i--)
    {
        if (PositionSelect(i))
        {
            if (PositionGetInteger(POSITION_MAGIC) == magic && PositionGetString(POSITION_SYMBOL) == _Symbol)
                count++;
        }
    }
    return count;
}

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
    return (INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Main tick function                                               |
//+------------------------------------------------------------------+
void OnTick()
{
    // Capture session levels only once per day
    if (!sessionLevelsCaptured)
    {
        sessionHigh = iHigh(_Symbol, PERIOD_M5, 1);
        sessionLow = iLow(_Symbol, PERIOD_M5, 1);
        sessionLevelsCaptured = true;

        Print("Session levels captured: High = " + DoubleToString(sessionHigh, 5) +
              ", Low = " + DoubleToString(sessionLow, 5));
    }

    // Exit if an order is already active
    if (CountOrdersByMagic(MagicNumber) > 0)
        return;

    // ATR-based Dynamic Take Profit Calculation
    double atrValue = iATR(_Symbol, PERIOD_M5, ATR_Period);
    double dynamicTP = (BaseTakeProfitPips * _Point) + (ATR_Multiplier * atrValue);

    // Determine recent highs and lows from the last three completed M5 candles
    double recentLow = MathMin(iLow(_Symbol, PERIOD_M5, 1), MathMin(iLow(_Symbol, PERIOD_M5, 2), iLow(_Symbol, PERIOD_M5, 3)));
    double recentHigh = MathMax(iHigh(_Symbol, PERIOD_M5, 1), MathMax(iHigh(_Symbol, PERIOD_M5, 2), iHigh(_Symbol, PERIOD_M5, 3)));

    double stopLossLong = recentLow - (StopLossBufferPips * _Point) - (ATR_Multiplier * atrValue);
    double stopLossShort = recentHigh + (StopLossBufferPips * _Point) + (ATR_Multiplier * atrValue);

    // Spread Check to Avoid High-Spread Trades
    double spread = 0.0;
    string currentSymbol = _Symbol; //Explicit string variable.
    if (!SymbolInfoDouble(currentSymbol, SYMBOL_SPREAD, spread))
    {
        Print("Error retrieving spread");
        return;
    }
    spread *= _Point;

    if (spread > 3 * _Point)
        return;

    // Get current market prices
    double askPrice = 0.0;
    if (!SymbolInfoDouble(currentSymbol, SYMBOL_ASK, askPrice))
    {
        Print("Error retrieving ask price");
        return;
    }

    double bidPrice = 0.0;
    if (!SymbolInfoDouble(currentSymbol, SYMBOL_BID, bidPrice))
    {
        Print("Error retrieving bid price");
        return;
    }

    // Long Trade Condition (Execution on M1)
    if (iClose(_Symbol, PERIOD_M5, 1) > sessionHigh && iClose(_Symbol, PERIOD_M1, 1) <= (sessionHigh + (RetestBufferPips * _Point)))
    {
        if (UseCandleConfirmation && iClose(_Symbol, PERIOD_M5, 2) < iOpen(_Symbol, PERIOD_M5, 2))
            return;

        trade.Buy(LotSize, _Symbol, askPrice, Slippage, stopLossLong, sessionHigh + dynamicTP);
        Print("Long trade placed successfully at: " + DoubleToString(askPrice, 5));
    }

    // Short Trade Condition (Execution on M1)
    if (iClose(_Symbol, PERIOD_M5, 1) < sessionLow && iClose(_Symbol, PERIOD_M1, 1) >= (sessionLow - (RetestBufferPips * _Point)))
    {
        if (UseCandleConfirmation && iClose(_Symbol, PERIOD_M5, 2) > iOpen(_Symbol, PERIOD_M5, 2))
            return;

        trade.Sell(LotSize, _Symbol, bidPrice, Slippage, stopLossShort, sessionLow - dynamicTP);
        Print("Short trade placed successfully at: " + DoubleToString(bidPrice, 5));
    }

    // Reset session levels at the start of a new day
    static int previousDay = 0;
    MqlDateTime dt;
    TimeToStruct(TimeCurrent(), dt);
    int currentDay = dt.day;
    if (currentDay != previousDay)
    {
        sessionLevelsCaptured = false;
        previousDay = currentDay;
    }
}


I am getting error 

'SymbolInfoDouble' - no one of the overloads can be applied to the function call Antfruition.mq5 85 10
could be one of 2 function(s) Antfruition.mq5 85 10
   built-in: double SymbolInfoDouble(const string,ENUM_SYMBOL_INFO_DOUBLE) Antfruition.mq5 85 10
   built-in: bool SymbolInfoDouble(const string,ENUM_SYMBOL_INFO_DOUBLE,double&) Antfruition.mq5 85 10
implicit conversion from 'number' to 'string' Antfruition.mq5 116 83
implicit conversion from 'number' to 'string' Antfruition.mq5 126 84
implicit conversion from 'number' to 'string' Antfruition.mq5 34 28
1 errors, 3 warnings 2 4


i've been using chatgpt and i found a few people had similar error but i dont know anything about coding. If someone can help I'd really appreciate this 
 
This originally worked for MT4 but when i used chatgpt to convert it to MT5 i'm getting this line error 
 
Anthony Nguyen #:
This originally worked for MT4 but when i used chatgpt to convert it to MT5 i'm getting this line error 

search this website for the errors and the function SymbolInfoDouble.

 
Anthony Nguyen #:
This originally worked for MT4 but when i used chatgpt to convert it to MT5 i'm getting this line error 

Fixed Code attached. Try it and Let me know. 

//+------------------------------------------------------------------+
//|                Antfruition.mq5                                   |
//|                Expert Advisor for MT5                            |
//+------------------------------------------------------------------+
#property strict
#include <Trade\Trade.mqh>

CTrade trade;

// Input parameters
input int    ATR_Period             = 14;
input int    BaseTakeProfitPips     = 20;
input double ATR_Multiplier         = 1.5;
input int    StopLossBufferPips     = 15;
input int    RetestBufferPips       = 2;
input bool   UseCandleConfirmation  = true;
input double LotSize                = 0.1;
input int    MagicNumber            = 12345;
input int    Slippage               = 3;

// Global variables for session tracking
bool   sessionLevelsCaptured = false;
double sessionHigh           = 0;
double sessionLow            = 0;

// Global variable for ATR indicator handle
int atrHandle = INVALID_HANDLE;

//+------------------------------------------------------------------+
//| Count open positions for the given Magic Number                  |
//+------------------------------------------------------------------+
int CountPositionsByMagic(int magic)
{
   int count = 0;
   int total = PositionsTotal();
   for(int i = 0; i < total; i++)
   {
      ulong ticket = PositionGetTicket(i);
      if(PositionSelectByTicket(ticket))
      {
         if(PositionGetInteger(POSITION_MAGIC) == magic &&
            PositionGetString(POSITION_SYMBOL) == _Symbol)
            count++;
      }
   }
   return count;
}

//+------------------------------------------------------------------+
//| Retrieve latest ATR value using the indicator handle             |
//+------------------------------------------------------------------+
double GetATRValue()
{
   double atrValue = 0.0;
   if(atrHandle == INVALID_HANDLE)
   {
      Print("Invalid ATR handle");
      return 0.0;
   }
   // Retrieve data from the last closed bar (shift = 1)
   double atrBuffer[1];
   int copied = CopyBuffer(atrHandle, 0, 1, 1, atrBuffer);
   if(copied < 1)
   {
      Print("Failed to copy ATR data. Error: ", GetLastError());
      return 0.0;
   }
   atrValue = atrBuffer[0];
   return atrValue;
}

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
   // Set magic number for all trade orders
   trade.SetExpertMagicNumber(MagicNumber);

   // Create ATR indicator handle on M5 timeframe
   atrHandle = iATR(_Symbol, PERIOD_M5, ATR_Period);
   if(atrHandle == INVALID_HANDLE)
   {
      Print("Failed to create ATR handle. Error: ", GetLastError());
      return INIT_FAILED;
   }
   return INIT_SUCCEEDED;
}

//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
   if(atrHandle != INVALID_HANDLE)
   {
      IndicatorRelease(atrHandle);
      atrHandle = INVALID_HANDLE;
   }
}

//+------------------------------------------------------------------+
//| Main tick function                                               |
//+------------------------------------------------------------------+
void OnTick()
{
   // Retrieve the number of digits for price normalization
   int digits = (int)SymbolInfoInteger(_Symbol, SYMBOL_DIGITS);
   
   // Reset session levels at the start of a new day
   static int previousDay = 0;
   MqlDateTime dt;
   TimeToStruct(TimeCurrent(), dt);
   int currentDay = dt.day;
   if(currentDay != previousDay)
   {
      sessionLevelsCaptured = false;
      previousDay = currentDay;
   }
   
   // Capture session levels (once per day) using the last completed M5 candle (shift = 1)
   if(!sessionLevelsCaptured)
   {
      sessionHigh = NormalizeDouble(iHigh(_Symbol, PERIOD_M5, 1), digits);
      sessionLow  = NormalizeDouble(iLow(_Symbol, PERIOD_M5, 1), digits);
      sessionLevelsCaptured = true;
      Print("Session levels captured: High = ", DoubleToString(sessionHigh, digits),
            ", Low = ", DoubleToString(sessionLow, digits));
   }
   
   // Exit if a position with the specified MagicNumber is already open
   if(CountPositionsByMagic(MagicNumber) > 0)
      return;
   
   // Retrieve the latest ATR value
   double atrValue = GetATRValue();
   if(atrValue == 0.0)
      return;
   
   // ATR-based Dynamic Take Profit Calculation (in price terms)
   double dynamicTP = NormalizeDouble((BaseTakeProfitPips * _Point) + (ATR_Multiplier * atrValue), digits);
   
   // Determine recent highs and lows from the last three completed M5 candles
   double recentLow  = NormalizeDouble(MathMin(iLow(_Symbol, PERIOD_M5, 1),
                                  MathMin(iLow(_Symbol, PERIOD_M5, 2), iLow(_Symbol, PERIOD_M5, 3))), digits);
   double recentHigh = NormalizeDouble(MathMax(iHigh(_Symbol, PERIOD_M5, 1),
                                  MathMax(iHigh(_Symbol, PERIOD_M5, 2), iHigh(_Symbol, PERIOD_M5, 3))), digits);
   
   double stopLossLong  = NormalizeDouble(recentLow - (StopLossBufferPips * _Point) - (ATR_Multiplier * atrValue), digits);
   double stopLossShort = NormalizeDouble(recentHigh + (StopLossBufferPips * _Point) + (ATR_Multiplier * atrValue), digits);
   
   // Retrieve current market prices using the output parameter overload
   double askPrice = 0.0, bidPrice = 0.0;
   if(!SymbolInfoDouble(_Symbol, SYMBOL_ASK, askPrice))
   {
      Print("Error retrieving ask price");
      return;
   }
   if(!SymbolInfoDouble(_Symbol, SYMBOL_BID, bidPrice))
   {
      Print("Error retrieving bid price");
      return;
   }
   askPrice = NormalizeDouble(askPrice, digits);
   bidPrice = NormalizeDouble(bidPrice, digits);
   
   // Calculate spread as the difference between ask and bid
   double spreadPrice = NormalizeDouble(askPrice - bidPrice, digits);
   if(spreadPrice > 3 * _Point)
      return;
   
   //----- Long Trade Condition (Execution on M1) -----
   if(iClose(_Symbol, PERIOD_M5, 1) > sessionHigh &&
      iClose(_Symbol, PERIOD_M1, 1) <= NormalizeDouble(sessionHigh + (RetestBufferPips * _Point), digits))
   {
      if(UseCandleConfirmation && (iClose(_Symbol, PERIOD_M5, 2) < iOpen(_Symbol, PERIOD_M5, 2)))
         return;
      
      if(trade.Buy(LotSize, _Symbol, askPrice, Slippage, stopLossLong, ""))
      {
         Print("Long trade placed successfully at: ", DoubleToString(askPrice, digits));
         ulong ticket = trade.ResultOrder();
         if(ticket != 0)
         {
            if(trade.PositionModify(ticket, stopLossLong, NormalizeDouble(sessionHigh + dynamicTP, digits)))
               Print("Long trade modified with TP successfully");
            else
               Print("Failed to modify long trade TP. Error: ", GetLastError());
         }
      }
      else
         Print("Failed to place long trade. Error: ", GetLastError());
   }
   
   //----- Short Trade Condition (Execution on M1) -----
   if(iClose(_Symbol, PERIOD_M5, 1) < sessionLow &&
      iClose(_Symbol, PERIOD_M1, 1) >= NormalizeDouble(sessionLow - (RetestBufferPips * _Point), digits))
   {
      if(UseCandleConfirmation && (iClose(_Symbol, PERIOD_M5, 2) > iOpen(_Symbol, PERIOD_M5, 2)))
         return;
      
      if(trade.Sell(LotSize, _Symbol, bidPrice, Slippage, stopLossShort, ""))
      {
         Print("Short trade placed successfully at: ", DoubleToString(bidPrice, digits));
         ulong ticket = trade.ResultOrder();
         if(ticket != 0)
         {
            if(trade.PositionModify(ticket, stopLossShort, NormalizeDouble(sessionLow - dynamicTP, digits)))
               Print("Short trade modified with TP successfully");
            else
               Print("Failed to modify short trade TP. Error: ", GetLastError());
         }
      }
      else
         Print("Failed to place short trade. Error: ", GetLastError());
   }
}
 
OMG it worked. Thank you so much !! 
 
I ran a strategy tester and i'm getting this error in the journal 

2025.03.16 00:55:25.551 Core 1 2024.07.11 15:42:09   failed market buy 0.1 GBPJPY sl: 3.000 tp: 207.215 [Invalid stops]


any chance you know what could cause this or will it be different when it's in a live trading condition? 

 
Anthony Nguyen #:
I ran a strategy tester and i'm getting this error in the journal 

2025.03.16 00:55:25.551 Core 1 2024.07.11 15:42:09   failed market buy 0.1 GBPJPY sl: 3.000 tp: 207.215 [Invalid stops]


any chance you know what could cause this or will it be different when it's in a live trading condition? 

do you know how to use search features? on every page of this website there is a search bar.

However, after looking at your code in previous msg, i can see several issues. first will be SymbolInfoDouble is not used appropriately.

 
The “Invalid stops” error means that one or both of the stop loss (SL) and take profit (TP) levels you’re sending do not meet the broker’s requirements.
Recheck the SL/TP and Fix it.
 
Michael Charles Schefe #:

do you know how to use search features? on every page of this website there is a search bar.

However, after looking at your code in previous msg, i can see several issues. first will be SymbolInfoDouble is not used appropriately.


If I knew what I was looking at that yes It would help but i literally don't have anything about coding. I used chatgpt to write these codes. Thank you for your help. 

 
Sivakumar Paul Suyambu #:
The “Invalid stops” error means that one or both of the stop loss (SL) and take profit (TP) levels you’re sending do not meet the broker’s requirements.
Recheck the SL/TP and Fix it.
Thank you for your help