Can't save correct bar dates

 

Hello there,

I'm trying to save some values to file for analysis.

First, I get the open and close times for each week, and then I use them as the range to find the day of the week with the highest and lowest price.

That works fine but for a few iterations. For this sample, I was collecting 52 weeks' worth of data. I only get 26 weeks of data right and then after that I get the 1970.01.01 dates. What could be the issue?

Here is my simple code:

      WeeklyData thisWeek;
      datetime openTime = iTime(_Symbol,PERIOD_W1,i);
      datetime closeTime = openTime + PeriodSeconds(PERIOD_W1);
      int strt= iBarShift(_Symbol,PERIOD_D1,openTime,false);
      int end= iBarShift(_Symbol,PERIOD_D1,closeTime,false);
      string highestDay = DayOfWeek(iTime(_Symbol,PERIOD_D1,iHighest(_Symbol,PERIOD_D1,MODE_HIGH,strt-end+1,end)));
      string lowestDay = DayOfWeek(iTime(_Symbol,PERIOD_D1,iLowest(_Symbol,PERIOD_D1,MODE_LOW,strt-end+1,end)));

      //Fill our current week struct
      thisWeek.open_date = TimeToString(openTime, TIME_DATE);
      thisWeek.close_date = TimeToString(closeTime, TIME_DATE);
      thisWeek.high_of_week = highestDay;
      thisWeek.low_of_week = lowestDay;

      //Add the current week struct to array
      ArrayResize(weeks,(ArraySize(weeks)+1));
      weeks[ArraySize(weeks)-1]=thisWeek;

And this is the sample output:

Open Date       Close Date      Week High       Week Low
2024.11.10      2024.11.17      MONDAY  THURSDAY
2024.11.03      2024.11.10      TUESDAY WEDNESDAY
2024.10.27      2024.11.03      FRIDAY  TUESDAY
2024.10.20      2024.10.27      SUNDAY  WEDNESDAY
2024.10.13      2024.10.20      MONDAY  THURSDAY
2024.10.06      2024.10.13      TUESDAY THURSDAY
2024.09.29      2024.10.06      MONDAY  FRIDAY
2024.09.22      2024.09.29      WEDNESDAY       MONDAY
2024.09.15      2024.09.22      WEDNESDAY       THURSDAY
2024.09.08      2024.09.15      FRIDAY  WEDNESDAY
2024.09.01      2024.09.08      FRIDAY  TUESDAY
2024.08.25      2024.09.01      MONDAY  FRIDAY
2024.08.18      2024.08.25      FRIDAY  SUNDAY
2024.08.11      2024.08.18      WEDNESDAY       SUNDAY
2024.08.04      2024.08.11      MONDAY  THURSDAY
2024.07.28      2024.08.04      FRIDAY  THURSDAY
2024.07.21      2024.07.28      SUNDAY  WEDNESDAY
2024.07.14      2024.07.21      WEDNESDAY       TUESDAY
2024.07.07      2024.07.14      FRIDAY  TUESDAY
2024.06.30      2024.07.07      FRIDAY  TUESDAY
2024.06.23      2024.06.30      MONDAY  WEDNESDAY
2024.06.16      2024.06.23      TUESDAY FRIDAY
2024.06.09      2024.06.16      WEDNESDAY       FRIDAY
2024.06.02      2024.06.09      TUESDAY SUNDAY
2024.05.26      2024.06.02      TUESDAY THURSDAY
1970.01.01      1970.01.08      SUNDAY  SUNDAY
1970.01.01      1970.01.08      SUNDAY  SUNDAY
1970.01.01      1970.01.08      SUNDAY  SUNDAY
1970.01.01      1970.01.08      SUNDAY  SUNDAY
1970.01.01      1970.01.08      SUNDAY  SUNDAY
1970.01.01      1970.01.08      SUNDAY  SUNDAY
1970.01.01      1970.01.08      SUNDAY  SUNDAY
1970.01.01      1970.01.08      SUNDAY  SUNDAY
1970.01.01      1970.01.08      SUNDAY  SUNDAY
1970.01.01      1970.01.08      SUNDAY  SUNDAY
1970.01.01      1970.01.08      SUNDAY  SUNDAY
1970.01.01      1970.01.08      SUNDAY  SUNDAY
 
Kevin Onsongo:

Hello there,

I'm trying to save some values to file for analysis.

First, I get the open and close times for each week, and then I use them as the range to find the day of the week with the highest and lowest price.

That works fine but for a few iterations. For this sample, I was collecting 52 weeks' worth of data. I only get 26 weeks of data right and then after that I get the 1970.01.01 dates. What could be the issue?

Here is my simple code:

And this is the sample output:

it would of helped if you had showed your loop code....

also check you have the data downloaded in the terminal, easily done by opening the chart with the relevant timeframe(s)

 
Kevin Onsongo: What could be the issue?
      datetime openTime = iTime(_Symbol,PERIOD_W1,i);
  1. Why did you post your coding question in the MT5 General section (a miscellaneous catch-all category) instead of the MT5 EA section (non-indicator coding)?
              General rules and best pratices of the Forum. - General - MQL5 programming forum? (2017)
    Next time, post in the correct place. I have moved this thread.

  2. You don't verify that all W1 bars have been created before accessing the data.

    On MT5: Unless the current chart is that specific pair/TF, you must synchronize the terminal Data from the Server before accessing candle/indicator values.
              Error 4806 while using CopyBuffer() - Expert Advisors and Automated Trading - MQL5 programming forum #10 (2020)
              Is it mystical?! It is! - Withdraw - Technical Indicators - MQL5 programming forum (2019)
              Timeseries and Indicators Access / Data Access - Reference on algorithmic/automated trading language for MetaTrader 5
              Synchronize Server Data with Terminal Data - Symbols - General - MQL5 programming forum #2 (2018)
              SymbolInfoInteger doesn't work - Symbols - General - MQL5 programming forum (2019)

  3. You don't show your loop. We can't read your mind nor see your machine; only what you give us on this forum.
         How To Ask Questions The Smart Way. (2004)
              When asking about code
              Be precise and informative about your problem.

 
Paul Anscombe #:

it would of helped if you had showed your loop code....

also check you have the data downloaded in the terminal, easily done by opening the chart with the relevant timeframe(s)

Thank you. I tried opening the terminal chart window.

Indeed, the data doesn't go further past  May 2024 in all the timeframes I checked.

Monthly, weekly, and daily timeframes. I will switch my broker and see if the data changes.

 
William Roeder #:
  1. Why did you post your coding question in the MT5 General section (a miscellaneous catch-all category) instead of the MT5 EA section (non-indicator coding)?
              General rules and best pratices of the Forum. - General - MQL5 programming forum? (2017)
    Next time, post in the correct place. I have moved this thread.

  2. You don't verify that all W1 bars have been created before accessing the data.

    On MT5: Unless the current chart is that specific pair/TF, you must synchronize the terminal Data from the Server before accessing candle/indicator values.
              Error 4806 while using CopyBuffer() - Expert Advisors and Automated Trading - MQL5 programming forum #10 (2020)
              Is it mystical?! It is! - Withdraw - Technical Indicators - MQL5 programming forum (2019)
              Timeseries and Indicators Access / Data Access - Reference on algorithmic/automated trading language for MetaTrader 5
              Synchronize Server Data with Terminal Data - Symbols - General - MQL5 programming forum #2 (2018)
              SymbolInfoInteger doesn't work - Symbols - General - MQL5 programming forum (2019)

  3. You don't show your loop. We can't read your mind nor see your machine; only what you give us on this forum.
         How To Ask Questions The Smart Way. (2004)
              When asking about code
              Be precise and informative about your problem.


   WeeklyData weeks[];
   ArraySetAsSeries(weeks, true);

   for(int i = 1; i < WeekCount + 1; i++)
     {
      WeeklyData thisWeek;
      datetime openTime = iTime(_Symbol,PERIOD_W1,i);
      datetime closeTime = openTime + PeriodSeconds(PERIOD_W1);
      int strt= iBarShift(_Symbol,PERIOD_D1,openTime,false);
      int end= iBarShift(_Symbol,PERIOD_D1,closeTime,false);
      string highestDay = DayOfWeek(iTime(_Symbol,PERIOD_D1,iHighest(_Symbol,PERIOD_D1,MODE_HIGH,strt-end+1,end)));
      string lowestDay = DayOfWeek(iTime(_Symbol,PERIOD_D1,iLowest(_Symbol,PERIOD_D1,MODE_LOW,strt-end+1,end)));

      //Fill our current week struct
      thisWeek.open_date = TimeToString(openTime, TIME_DATE);
      thisWeek.close_date = TimeToString(closeTime, TIME_DATE);
      thisWeek.high_of_week = highestDay;
      thisWeek.low_of_week = lowestDay;

      //Add the current week struct to array
      ArrayResize(weeks,(ArraySize(weeks)+1));
      weeks[ArraySize(weeks)-1]=thisWeek;

     }
Here is the full loop. And thanks for the pointers. I should make sure the bars are created, and the terminal is synchronized to the server for the other periods.