Weiß jemand, wo der Fehler liegt? "country_code" undeclared identifier

 

Servus zusammen,

ich habe in MQL5 ein Skript bzw. einen Expert Advisor programmiert, doch leider tritt ständig ein Fehler auf. Ich habe bereits versucht, den Code zu debuggen, konnte aber die genaue Ursache des Problems nicht identifizieren.

Hat jemand Erfahrung mit einem ähnlichen Problem oder kann mir dabei helfen, den Fehler zu finden und zu beheben? Ich wäre für jede Unterstützung dankbar!

Vielen Dank im Voraus! 😊


'country_code' - undeclared identifier NewsHandler.mqh 190 45

'country_code' - undeclared identifier NewsHandler.mqh 223 45

'country_code' - undeclared identifier NewsHandler.mqh 252 45

'country_code' - undeclared identifier NewsHandler.mqh 284 58

implicit conversion from 'unknown' to 'string' NewsHandler.mqh 284 58


//+------------------------------------------------------------------+
//|                                                 NewsHandler.mqh |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Copyright 2024"
#property link      ""
#property version   "1.00"

// Include necessary files
#include "../definition/Constants/Defines.mqh"
#include "../definition/Constants/NewsSettings.mqh"
#include "../include/Utils/Logger.mqh"

//+------------------------------------------------------------------+
//| Class to handle economic news events                               |
//+------------------------------------------------------------------+
class CNewsHandler
{
private:
    CLogger*          m_logger;           // Logger instance
    string            m_symbol;           // Current symbol
    string            m_symbol_currency;  // Base currency of the symbol
    string            m_quote_currency;   // Quote currency of the symbol
    MqlCalendarValue  m_values[];        // Array to store calendar values
    MqlCalendarEvent  m_events[];        // Array to store calendar events
    
    bool              InitializeNewsFilter();
    string            GetSymbolCurrency();
    bool              IsHighImpactNews(const MqlCalendarEvent &event);
    bool              IsCurrencyRelevant(const string currency);
    bool              IsRelevantNews(const MqlCalendarEvent &event);
    
public:
                     CNewsHandler(string symbol, CLogger* logger);
                    ~CNewsHandler();
    
    bool             Init();              // Initialize news handler
    void             Update();            // Update news data
    bool             HasUpcomingNews(int minutes_ahead = 60);
    bool             IsNewsTime();
    void             GetCurrentNews(string &news_description);
};

//+------------------------------------------------------------------+
//| Constructor                                                        |
//+------------------------------------------------------------------+
CNewsHandler::CNewsHandler(string symbol, CLogger* logger)
{
    m_symbol = symbol;
    m_logger = logger;
    m_symbol_currency = "";
    m_quote_currency = "";
}

//+------------------------------------------------------------------+
//| Destructor                                                         |
//+------------------------------------------------------------------+
CNewsHandler::~CNewsHandler()
{
    // Cleanup is handled automatically for arrays
}

//+------------------------------------------------------------------+
//| Initialize the news handler                                         |
//+------------------------------------------------------------------+
bool CNewsHandler::Init()
{
    if(m_symbol == "" || m_logger == NULL)
    {
        if(m_logger != NULL)
            m_logger.Error("Invalid initialization parameters for NewsHandler");
        return false;
    }
    
    m_symbol_currency = GetSymbolCurrency();
    if(m_symbol_currency == "")
    {
        m_logger.Error("Failed to get symbol currency");
        return false;
    }
    
    if(!InitializeNewsFilter())
    {
        m_logger.Error("Failed to initialize news filter");
        return false;
    }
    
    m_logger.Info("News handler initialized successfully");
    return true;
}

//+------------------------------------------------------------------+
//| Get the base currency of the symbol                                |
//+------------------------------------------------------------------+
string CNewsHandler::GetSymbolCurrency()
{
    // Check if the symbol has the required length
    if(StringLen(m_symbol) < 6)
    {
        m_logger.Error("Symbol format incorrect: " + m_symbol);
        return "";
    }
    
    m_symbol_currency = StringSubstr(m_symbol, 0, 3);
    m_quote_currency = StringSubstr(m_symbol, 3, 3);
    
    m_logger.Info("Symbol currencies: " + m_symbol_currency + "/" + m_quote_currency);
    return m_symbol_currency;
}

//+------------------------------------------------------------------+
//| Initialize the news filter                                         |
//+------------------------------------------------------------------+
bool CNewsHandler::InitializeNewsFilter()
{
    MqlCalendarCountry countries[];
    if(CalendarCountries(countries) == 0)
    {
        m_logger.Error("Failed to get calendar countries");
        return false;
    }
    
    // Additional initialization logic if needed
    
    return true;
}

//+------------------------------------------------------------------+
//| Check if the event is high impact                                  |
//+------------------------------------------------------------------+
bool CNewsHandler::IsHighImpactNews(const MqlCalendarEvent &event)
{
    return event.importance == CALENDAR_IMPORTANCE_HIGH;
}

//+------------------------------------------------------------------+
//| Check if the currency is relevant for the current symbol            |
//+------------------------------------------------------------------+
bool CNewsHandler::IsCurrencyRelevant(const string currency)
{
    if(::FilterAllCurrencies)
        return true;
    else if(::FilterCurrencyPairOnly) // Added 'else if' to fix logical issue
        return (currency == m_symbol_currency || currency == m_quote_currency);
    else
        return false; // Added explicit else case
}

//+------------------------------------------------------------------+
//| Check if the news event is relevant based on impact settings       |
//+------------------------------------------------------------------+
bool CNewsHandler::IsRelevantNews(const MqlCalendarEvent &event)
{
    if(::HighImpactNewsOnly && event.importance == CALENDAR_IMPORTANCE_HIGH)
        return true;
        
    if(::MediumImpactNewsFilter && event.importance == CALENDAR_IMPORTANCE_MEDIUM)
        return true;
        
    if(::LowImpactNewsFilter && event.importance == CALENDAR_IMPORTANCE_LOW)
        return true;
        
    return false;
}

//+------------------------------------------------------------------+
//| Update news data                                                   |
//+------------------------------------------------------------------+
void CNewsHandler::Update()
{
    datetime start_time = TimeCurrent();
    datetime end_time = start_time + PeriodSeconds(PERIOD_D1); // Look ahead 24 hours
    
    // Clear existing events arrays to prevent duplicates
    ArrayResize(m_events, 0);
    
    // Get calendar events for the next 24 hours
    int count = CalendarValueHistory(m_values, start_time, end_time);
    if(count > 0)
    {
        m_logger.Info("Retrieved " + IntegerToString(count) + " calendar values");
        
        for(int i = 0; i < ArraySize(m_values); i++)
        {
            MqlCalendarEvent event;
            if(CalendarEventById(m_values[i].event_id, event))
            {
                // Fixed: Proper country code checking
                // MQL5 stores country code in event.country_code, not event.country_id
                if(IsCurrencyRelevant(event.country_code) && IsRelevantNews(event))
                {
                    int size = ArraySize(m_events);
                    ArrayResize(m_events, size + 1);
                    m_events[size] = event;
                }
            }
        }
        
        m_logger.Info("Filtered to " + IntegerToString(ArraySize(m_events)) + " relevant events");
    }
    else
    {
        m_logger.Warning("No calendar events found for the next 24 hours");
    }
}

//+------------------------------------------------------------------+
//| Check for upcoming news in the specified time window               |
//+------------------------------------------------------------------+
bool CNewsHandler::HasUpcomingNews(int minutes_ahead = 60)
{
    datetime current_time = TimeCurrent();
    datetime future_time = current_time + minutes_ahead * 60;
    
    for(int i = 0; i < ArraySize(m_values); i++)
    {
        if(m_values[i].time >= current_time && m_values[i].time <= future_time)
        {
            MqlCalendarEvent event;
            if(CalendarEventById(m_values[i].event_id, event))
            {
                // Fixed: Proper country code checking
                if(IsCurrencyRelevant(event.country_code) && IsHighImpactNews(event))
                {
                    m_logger.Info("Upcoming high impact news: " + event.name + " at " + 
                                TimeToString(m_values[i].time, TIME_DATE|TIME_MINUTES));
                    return true;
                }
            }
        }
    }
    return false;
}

//+------------------------------------------------------------------+
//| Check if current time is during a news release                     |
//+------------------------------------------------------------------+
bool CNewsHandler::IsNewsTime()
{
    datetime current_time = TimeCurrent();
    
    for(int i = 0; i < ArraySize(m_values); i++)
    {
        // Consider 5 minutes before and after the news release
        if(current_time >= m_values[i].time - 300 && 
           current_time <= m_values[i].time + 300)
        {
            MqlCalendarEvent event;
            if(CalendarEventById(m_values[i].event_id, event))
            {
                // Fixed: Proper country code checking
                if(IsCurrencyRelevant(event.country_code) && IsHighImpactNews(event))
                {
                    m_logger.Info("Currently in news time: " + event.name);
                    return true;
                }
            }
        }
    }
    return false;
}

//+------------------------------------------------------------------+
//| Get description of current news events                             |
//+------------------------------------------------------------------+
void CNewsHandler::GetCurrentNews(string &news_description)
{
    datetime current_time = TimeCurrent();
    news_description = "";
    
    // Instead of using non-existent CalendarValueById, we use already stored data
    for(int i = 0; i < ArraySize(m_events); i++)
    {
        // Find the matching value for this event
        for(int j = 0; j < ArraySize(m_values); j++)
        {
            if(m_values[j].event_id == m_events[i].id)
            {
                if(current_time >= m_values[j].time - 300 && 
                   current_time <= m_values[j].time + 300)
                {
                    // Fixed: Proper country code usage
                    news_description += m_events[i].name + 
                                      " (" + m_events[i].country_code + ")\n";
                    break;
                }
            }
        }

    }
    
    if(news_description == "")
        m_logger.Info("No current news events");
    else
        m_logger.Info("Current news events: " + news_description);
}
 
  1. Es wäre besser, das Script auch anzuhängen statt nur den Code ohne Zeilennummern, auf die Du Dich beziehst, zu posten.
  2. Dein Fehler liegt wahrscheinlich woanders, Deine Fehler sind nur die Konsequenz dessen.
  3. Warum ein Script, das kann man nicht debuggen, statt eines EAs oder Indikators?
  4. Warum kopierst Du nicht was es schon gibt?
    Es gibt fast nichts, was nicht schon programmiert wurde!
    Oben ist das Suchfester, "News" ergibt: https://www.mql5.com/de/search#!keyword=News
    Es gibt eine Artikelreihe, hier der erste: https://www.mql5.com/de/articles/14324
    oder Google mal nach: "site:mql5.com Newstrader"
Building an Automatic News Trader
Building an Automatic News Trader
  • www.mql5.com
This is the continuation of Another MQL5 OOP class article which showed you how to build a simple OO EA from scratch and gave you some tips on object-oriented programming. Today I am showing you the technical basics needed to develop an EA able to trade the news. My goal is to keep on giving you ideas about OOP and also cover a new topic in this series of articles, working with the file system.
 
Carl Schreiber #:
  1. Es wäre besser, das Script auch anzuhängen statt nur den Code ohne Zeilennummern, auf die Du Dich beziehst, zu posten.
  2. Dein Fehler liegt wahrscheinlich woanders, Deine Fehler sind nur die Konsequenz dessen.
  3. Warum ein Script, das kann man nicht debuggen, statt eines EAs oder Indikators?
  4. Warum kopierst Du nicht was es schon gibt?
    Es gibt fast nichts, was nicht schon programmiert wurde!
    Oben ist das Suchfester, "News" ergibt: https://www.mql5.com/de/search#!keyword=News
    Es gibt eine Artikelreihe, hier der erste: https://www.mql5.com/de/articles/14324
    oder Google mal nach: "site:mql5.com Newstrader"
Danke dir für die hilfe und ja stimmt, das ist kein Script hab mich vertippt sondern ein indicator für mein EA 
 

Hallo Allerseits,

in einem MqlCalendarEvent gibt es keinen country_code

, sondern country_id!

Als Hilfe 

MqlCalendarEvent

mit F1 ausführen und in der Dokumentation nachlesen.

Gruß Igor