Yearly OHLC

 
Hi! I saw it's possible to get the OHLC of the month using the enum time frame, is the same possible with a yearly time frame?

Thank you!
 
try Using PeriodSeconds for example
12*PeriodSeconds(PERIOD_MN1)
// Function to retrieve yearly OHLC data
void GetYearlyOHLC()
{
    int period_seconds = 12 * PeriodSeconds(PERIOD_MN1); // 12 months in seconds

    // Get the total number of bars in history
    int total_bars = Bars(Symbol(), PERIOD_MN1);

    // Loop over each year (loop through monthly bars)
    for(int i = total_bars - 1; i >= 0; i--)
    {
        // Calculate the year start and end time (each year has 12 months)
        datetime year_start = time[i] - (i % 12) * period_seconds;
        datetime year_end = year_start + period_seconds - 1;
        
        // Retrieve the OHLC data for the year
        double open = iOpen(Symbol(), PERIOD_MN1, i);
        double high = iHigh(Symbol(), PERIOD_MN1, i);
        double low = iLow(Symbol(), PERIOD_MN1, i);
        double close = iClose(Symbol(), PERIOD_MN1, i);
        
        // Print the yearly OHLC data
        Print("Year: ", TimeToString(year_start, TIME_DATE), " | Open: ", open, " | High: ", high, " | Low: ", low, " | Close: ", close);
    }
}
//code is just example. neither tested nor compiled
 
Rajesh Kumar Nait #:
try Using PeriodSeconds for example

Thank you!

 
Rajesh Kumar Nait #:
try Using PeriodSeconds for example
void GetYearlyOHLC()
{
    int total_bars = Bars(Symbol(), PERIOD_M1);

    if (total_bars == 0)
    {
        Print("No data available.");
        return;
    }

    int current_year = TimeYear(Time[total_bars - 1]);
    double open = Open[total_bars - 1];
    double high = High[total_bars - 1];
    double low = Low[total_bars - 1];
    double close = 0;

    for (int i = total_bars - 1; i >= 0; i--)
    {
        datetime t = Time[i];
        int year = TimeYear(t);

        if (year != current_year)
        {
            Print("Year: ", current_year,
                  " | Open: ", open,
                  " | High: ", high,
                  " | Low: ", low,
                  " | Close: ", close);

            current_year = year;
            open = Open[i];
            high = High[i];
            low = Low[i];
        }

        if (High[i] > high) high = High[i];
        if (Low[i] < low) low = Low[i];
        close = Close[i];
    }

    // Print the last year's data
    Print("Year: ", current_year,
          " | Open: ", open,
          " | High: ", high,
          " | Low: ", low,
          " | Close: ", close);
}

What about this in your opinion?

 
Colin Kimble #:

What about this in your opinion?

if its working, all good.