/*
      ███████╗ ██████╗ ██████╗ ███████╗██╗  ██╗     ███████╗████████╗ █████╗ ████████╗██╗ ██████╗ ███╗   ██╗
      ██╔════╝██╔═══██╗██╔══██╗██╔════╝╚██╗██╔╝     ██╔════╝╚══██╔══╝██╔══██╗╚══██╔══╝██║██╔═══██╗████╗  ██║
      █████╗  ██║   ██║██████╔╝█████╗   ╚███╔╝█████╗███████╗   ██║   ███████║   ██║   ██║██║   ██║██╔██╗ ██║
      ██╔══╝  ██║   ██║██╔══██╗██╔══╝   ██╔██╗╚════╝╚════██║   ██║   ██╔══██║   ██║   ██║██║   ██║██║╚██╗██║
      ██║     ╚██████╔╝██║  ██║███████╗██╔╝ ██╗     ███████║   ██║   ██║  ██║   ██║   ██║╚██████╔╝██║ ╚████║
      ╚═╝      ╚═════╝ ╚═╝  ╚═╝╚══════╝╚═╝  ╚═╝     ╚══════╝   ╚═╝   ╚═╝  ╚═╝   ╚═╝   ╚═╝ ╚═════╝ ╚═╝  ╚═══╝
*/

//+-------------------------------------------------------!/--------------------------------------------------------+
//| [Command Center + Mission Status]                    (ò ó)                                   [XU NewsTicker.mq5] |
//+-------------------------------------------------o0o---(_)---o0o--------------------------------------------------+

#property copyright "Barry's Van AI"
#property version   "1.9"
#property strict
#property indicator_chart_window
#property indicator_plots 0

// --- INPUTS FOR POSITIONING ---
input ENUM_BASE_CORNER InpCorner = CORNER_LEFT_LOWER; // Ticker Corner Location
input int InpXDistance = 10;                          // X-Axis Distance (Left/Right)
input int InpYDistance = 130;                          // Y-Axis Distance (Up/Down)

// --- INIT ---
int OnInit() {
    EventSetTimer(60); // Check for news every minute
    CreateTickerLabel(); 
    UpdateNewsLine();          
    return(INIT_SUCCEEDED);
}

void OnDeinit(const int reason) {
    ObjectDelete(0, "BarryTicker");
    EventKillTimer();
}

int OnCalculate(const int rates_total, const int prev_calculated, const int begin, const double &price[]) { return(rates_total); }

void OnTimer() { UpdateNewsLine(); }

// --- THE ENGINE ---

void UpdateNewsLine() {
    MqlCalendarValue values[];
    datetime now = TimeCurrent();
    string newsTxt = " [ NO HIGH IMPACT NEWS IN NEXT 24H ] ";
    color newsCol = clrLime;

    // Pull next 24 hours of USD news
    int count = CalendarValueHistory(values, now, now + 86400, "US");
    
    if(count > 0) {
        for(int i=0; i<ArraySize(values); i++) {
            MqlCalendarEvent event;
            if(CalendarEventById(values[i].event_id, event)) {
                // Filter specifically for High Impact
                if(event.importance == CALENDAR_IMPORTANCE_HIGH) {
                    long diffSeconds = (long)values[i].time - (long)now;
                    
                    if(diffSeconds > -300) { // Keep showing for 5 mins after release
                        long mins = diffSeconds / 60;
                        
                        if(mins > 0) {
                            newsTxt = " [ NEXT RED NEWS: " + event.name + " | IN: " + (string)mins + " MINS ] ";
                            // Change color based on proximity
                            if(mins <= 30) newsCol = clrRed;
                            else if(mins <= 60) newsCol = clrOrangeRed;
                            else newsCol = clrDarkGray;
                        } else {
                            newsTxt = " [ !!! NEWS ACTIVE: " + event.name + " !!! ] ";
                            newsCol = clrRed;
                        }
                        break; // Exit loop - we only want the one closest line
                    }
                }
            }
        }
    }
    
    // Update the label
    if(ObjectFind(0, "BarryTicker") < 0) CreateTickerLabel();
    ObjectSetString(0, "BarryTicker", OBJPROP_TEXT, newsTxt);
    ObjectSetInteger(0, "BarryTicker", OBJPROP_COLOR, newsCol);
    ChartRedraw();
}

void CreateTickerLabel() {
    string name = "BarryTicker";
    if(ObjectFind(0, name) < 0) {
        ObjectCreate(0, name, OBJ_LABEL, 0, 0, 0);
        ObjectSetInteger(0, name, OBJPROP_CORNER, InpCorner);
        ObjectSetInteger(0, name, OBJPROP_XDISTANCE, InpXDistance);
        ObjectSetInteger(0, name, OBJPROP_YDISTANCE, InpYDistance); 
        ObjectSetInteger(0, name, OBJPROP_FONTSIZE, 11);
        ObjectSetString(0, name, OBJPROP_FONT, "Courier New");
    }
}
//+------------------------------------------------------------------+
//--- End of XU NewsTicker -- COMPLETE! 🎉