#property indicator_chart_window
#property indicator_buffers 0
#property indicator_plots   0

// === INPUTS ===
input int BeijingHour = 2;     // Beijing open (GMT+0)
input int LondonHour  = 9;     // London open (GMT+0)
input int NYHour      = 14;    // New York open (GMT+0)

input color BeijingColor = clrRed;
input color LondonColor  = clrGreen;
input color NYColor      = clrYellow;

input ENUM_LINE_STYLE BeijingStyle = STYLE_SOLID;
input ENUM_LINE_STYLE LondonStyle  = STYLE_DASH;
input ENUM_LINE_STYLE NYStyle      = STYLE_DOT;

input int LineWidth = 1;
input int DaysBack = 20;       // How many past days to draw

// === INIT ===
int OnInit()
{
    DrawMarketLines();
    EventSetTimer(60); // Redraw every minute
    return(INIT_SUCCEEDED);
}

// === DEINIT ===
void OnDeinit(const int reason)
{
    EventKillTimer();

    // Remove market lines
    for(int i = 0; i < DaysBack; i++)
    {
        string dateTag = TimeToString(TimeCurrent() - i * 86400, TIME_DATE);
        ObjectDelete(0, "Beijing_" + dateTag);
        ObjectDelete(0, "London_"  + dateTag);
        ObjectDelete(0, "NY_"      + dateTag);
    }
}

// === TIMER EVENT ===
void OnTimer()
{
    DrawMarketLines();
}

// === INDICATOR CALCULATION ===
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
{
    return(rates_total);
}

// === DRAW MARKET LINES ===
void DrawMarketLines()
{
    datetime now = TimeCurrent();
    for (int i = 0; i < DaysBack; i++)
    {
        datetime day = DateToStruct(now - i * 86400).day_start;
        DrawLineForHour("Beijing", day, BeijingHour, BeijingColor, BeijingStyle);
        DrawLineForHour("London",  day, LondonHour,  LondonColor,  LondonStyle);
        DrawLineForHour("NY",      day, NYHour,      NYColor,      NYStyle);
    }
}

// === DRAW LINE FOR SPECIFIC HOUR ===
void DrawLineForHour(string tag, datetime dayStart, int hour, color clr, ENUM_LINE_STYLE style)
{
    datetime lineTime = dayStart + hour * 3600;
    string name = tag + "_" + TimeToString(lineTime, TIME_DATE);

    if (ObjectFind(0, name) < 0)
    {
        ObjectCreate(0, name, OBJ_VLINE, 0, lineTime, 0);
        ObjectSetInteger(0, name, OBJPROP_COLOR, clr);
        ObjectSetInteger(0, name, OBJPROP_WIDTH, LineWidth);
        ObjectSetInteger(0, name, OBJPROP_STYLE, style);
    }
}

// === HELPER STRUCT FOR START OF DAY ===
struct date_parts
{
    datetime day_start;
};

date_parts DateToStruct(datetime time)
{
    MqlDateTime dt;
    TimeToStruct(time, dt);
    dt.hour = 0;
    dt.min = 0;
    dt.sec = 0;
    datetime startOfDay = StructToTime(dt);

    date_parts result;
    result.day_start = startOfDay;
    return result;
}
