Alert not getting after Implementation of code, Friends any one can guide me to get a solution.

 

Code compiled, with out error. but  alert not generating. Is there is any method to verify and correct the problem


// Global variables to store spread data

double openSpread = 0;
double highSpread = 0;
double lowSpread = 0;
datetime openTime;
datetime highTime;
datetime lowTime;
double lastSpread = 0;
double spreadThreshold = 1;  // Alert threshold set to 1 USD

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
   // Set timer to check every minute (adjust based on needs)
   EventSetTimer(60);  
   
   // Record initial spread and time
   openSpread = GetSpread();
   openTime = TimeCurrent();
   highSpread = openSpread;
   lowSpread = openSpread;
   
   // Print initial spread data to logs
   Print("Open Spread: ", openSpread, " at Time: ", openTime);
   
   return INIT_SUCCEEDED;
}

//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
   // Cancel the timer on deinit
   EventKillTimer();
}

//+------------------------------------------------------------------+
//| Expert tick function (Main logic)                                |
//+------------------------------------------------------------------+
void OnTick()
{
   // Get current spread
   double currentSpread = GetSpread();
   
   // If spread changes by 1 USD or more, trigger alert
   if (MathAbs(currentSpread - lastSpread) >= spreadThreshold)
   {
      string message = "Live Spread: " + DoubleToString(currentSpread, 2) + 
                       ", Time: " + TimeToString(TimeCurrent(), TIME_DATE | TIME_MINUTES);
      
      // Send push notification with sound
      SendNotification(message);
      PlaySound("alert.wav");  // Play sound notification
      
      // Log the alert
      LogSpreadData(currentSpread);
      
      // Update last spread value
      lastSpread = currentSpread;
   }
   
   // Update high and low spread tracking
   if (currentSpread > highSpread)
   {
      highSpread = currentSpread;
      highTime = TimeCurrent();
      Print("High Spread: ", highSpread, " at Time: ", highTime);
   }
   
   if (currentSpread < lowSpread)
   {
      lowSpread = currentSpread;
      lowTime = TimeCurrent();
      Print("Low Spread: ", lowSpread, " at Time: ", lowTime);
   }

   // Log spread data for future reference
   Print("Open Spread: ", openSpread, " at Time: ", openTime);
   Print("High Spread: ", highSpread, " at Time: ", highTime);
   Print("Low Spread: ", lowSpread, " at Time: ", lowTime);
}

//+------------------------------------------------------------------+
//| Function to calculate spread between XAUUSD and GOLDAPR25        |
//+------------------------------------------------------------------+
double GetSpread()
{
   // Get XAUUSD Bid and GOLDAPR25 Ask prices using SymbolInfoDouble
   double xauUsdBid = SymbolInfoDouble("XAUUSD", SYMBOL_BID);  // XAUUSD Bid
   double goldApr25Ask = SymbolInfoDouble("GOLDAPR25", SYMBOL_ASK);  // GOLDAPR25 Ask
   
   // Check for errors in retrieving the prices
   if (xauUsdBid == 0 || goldApr25Ask == 0)
   {
      Print("Error: Failed to get bid price for one or both symbols.");
      return 0;  // Return 0 if there's an error
   }
   
   // Calculate the spread as the absolute difference
   return MathAbs(xauUsdBid - goldApr25Ask);
}

//+------------------------------------------------------------------+
//| Function to log spread data in the required format                |
//+------------------------------------------------------------------+
void LogSpreadData(double currentSpread)
{
   string action = "ALERT";
   string change = "CHANGE " + (currentSpread > lastSpread ? "+" : "-") + "1$";  // Example action based on spread change
   string time = TimeToString(TimeCurrent(), TIME_DATE | TIME_MINUTES);
   
   // Log data in the required format
   Print("XAUUSD Bid: ", SymbolInfoDouble("XAUUSD", SYMBOL_BID), " | GOLDAPR25 Ask: ", SymbolInfoDouble("GOLDAPR25", SYMBOL_ASK), 
         " | Spread: ", currentSpread, " | Time: ", time, " | Action: ", action, " | Change: ", change);
   
   // Optionally log to file or database for historical analysis
   // Example for CSV logging
   // FileAppend("spread_data.csv", SymbolInfoDouble("XAUUSD", SYMBOL_BID) + "," + SymbolInfoDouble("GOLDAPR25", SYMBOL_ASK) + "," + DoubleToString(currentSpread, 2) + "," + time + "," + action + "," + change);
}

 
Your topic has been moved to the section: Expert Advisors and Automated Trading
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
 
  • Did you debug the code?
  • Did you place "Print()" at various levels to see if its logic is executing the way you want?
  • Did you check the log for any warnings or error messages?
  • Why do you set an event timer when you don't have a timer event handler?
  • Don't call trading or symbol data functions from the OnInit() handler. Wait for the first tick to make sure the connection has been established and that data is available.
  • Have your code make sure the symbols for which you need data, are enabled and their data synchronised before requesting their data.