Libraries: Calendar - page 11

 
amrali #:

subtract directly from 12:30 (instead of -3 + 31*/2):

I did not do this on purpose, because I assume that in the future it is possible to change the input parameters of CalendarValueHistoryByEvent.

 
fxsaber #:

I didn't do this on purpose, because I assume that in the future it is possible to change the input parameters of CalendarValueHistoryByEvent.

you can use:

   return((int)(45000 - Value[0].time % 86400));
 
fxsaber #:

After updating the library, the Calendar should be synchronised with the history of quotes on any broker.

...

Did they remove sorting by time in the latest version?


 
Anatoli Kazharski #:

Did they remove time sorting in the latest version?

Yes. I was sure that the MQs were sorted. I was wrong.


ZY Added.

 

Unfortunately, you can't trust the current Calendar.


Here is the omission of an important CHF news in the list of events and the corresponding market reaction to it.


And this is another calendar, where the news is present.


 

In the Tutorial I found a treasure trove of information on working with the Calendar. In particular, in the Tester.

Теперь мы можем выбрать индикатор CalendarMonitorCached.mq5  в тестере и увидеть в динамике, на истории, как меняется таблица новостей.

News indicator with calendar cache in the Tester

The presence of the calendar cache allows you to test trading strategies on news. We will show it in the next section.

It uses the same API as the native MQL5 calendar.
Учебник по MQL5: Расширенные средства языка / Экономический календарь / Перенос базы календаря в тестер
Учебник по MQL5: Расширенные средства языка / Экономический календарь / Перенос базы календаря в тестер
  • www.mql5.com
Календарь доступен для MQL-программ только в режиме онлайн, в связи с чем тестирование новостных торговых стратегий представляет некоторую...
 

Same code, different results according to the chart symbol. I tested 2 brokers.

test_calendar.mq5

#include "Calendar.mqh"

void OnStart()
  {
   CALENDAR Calendar;

   if(Calendar.Set(NULL, CALENDAR_IMPORTANCE_NONE, D'2023.10.1', TimeCurrent()))
     {
      Calendar.FilterByEventID(840030016); // NFP

      Calendar.AutoDST();

      Print(Calendar.ToString());
     }
  }

FXOpen:

test_calendar (EURUSD,M1)       2023.10.06 15:30 USD 3 Nonfarm Payrolls (nonfarm-payrolls), United States (US) | 336 K | 1 K | 187 K | 227 K
test_calendar (EURUSD,M1)       2023.11.03 15:30 USD 3 Nonfarm Payrolls (nonfarm-payrolls), United States (US) | 150 K | -8 K | 336 K | 297 K
test_calendar (EURUSD,M1)       2023.12.08 15:30 USD 3 Nonfarm Payrolls (nonfarm-payrolls), United States (US) | 199 K | -3 K | 150 K | 
test_calendar (EURUSD,M1)       2024.01.05 15:30 USD 3 Nonfarm Payrolls (nonfarm-payrolls), United States (US) | 216 K | 1 K | 199 K | 173 K
test_calendar (EURUSD,M1)       2024.02.02 15:30 USD 3 Nonfarm Payrolls (nonfarm-payrolls), United States (US) | 353 K | 186 K | 216 K | 333 K
test_calendar (EURUSD,M1)       2024.03.08 15:30 USD 3 Nonfarm Payrolls (nonfarm-payrolls), United States (US) | 275 K | 220 K | 353 K | 229 K
test_calendar (EURUSD,M1)       2024.04.05 15:30 USD 3 Nonfarm Payrolls (nonfarm-payrolls), United States (US) | 303 K | 176 K | 275 K | 270 K
test_calendar (EURUSD,M1)       
test_calendar (XAUUSD,M1)       2023.10.06 15:30 USD 3 Nonfarm Payrolls (nonfarm-payrolls), United States (US) | 336 K | 1 K | 187 K | 227 K
test_calendar (XAUUSD,M1)       2023.11.03 14:30 USD 3 Nonfarm Payrolls (nonfarm-payrolls), United States (US) | 150 K | -8 K | 336 K | 297 K
test_calendar (XAUUSD,M1)       2023.12.08 15:30 USD 3 Nonfarm Payrolls (nonfarm-payrolls), United States (US) | 199 K | -3 K | 150 K | 
test_calendar (XAUUSD,M1)       2024.01.05 15:30 USD 3 Nonfarm Payrolls (nonfarm-payrolls), United States (US) | 216 K | 1 K | 199 K | 173 K
test_calendar (XAUUSD,M1)       2024.02.02 15:30 USD 3 Nonfarm Payrolls (nonfarm-payrolls), United States (US) | 353 K | 186 K | 216 K | 333 K
test_calendar (XAUUSD,M1)       2024.03.08 15:30 USD 3 Nonfarm Payrolls (nonfarm-payrolls), United States (US) | 275 K | 220 K | 353 K | 229 K
test_calendar (XAUUSD,M1)       2024.04.05 15:30 USD 3 Nonfarm Payrolls (nonfarm-payrolls), United States (US) | 303 K | 176 K | 275 K | 270 K
test_calendar (XAUUSD,M1)       
BrokerDaylightSchedule (XAUUSD,M1)      Server : FXOpen-MT5
BrokerDaylightSchedule (XAUUSD,M1)      Time   : 2024.04.13 03:05:59
BrokerDaylightSchedule (XAUUSD,M1)      Offset : GMT+3
BrokerDaylightSchedule (XAUUSD,M1)      DST_US : server dst begins on the second Sunday of March (+1) and ends on the first Sunday of November (-1)

Trading.com

test_calendar (EURUSD,H4)       2023.10.06 08:30 USD 3 Nonfarm Payrolls (nonfarm-payrolls), United States (US) | 336 K | 1 K | 187 K | 227 K
test_calendar (EURUSD,H4)       2023.11.03 07:30 USD 3 Nonfarm Payrolls (nonfarm-payrolls), United States (US) | 150 K | -8 K | 336 K | 297 K
test_calendar (EURUSD,H4)       2023.12.08 08:30 USD 3 Nonfarm Payrolls (nonfarm-payrolls), United States (US) | 199 K | -3 K | 150 K | 
test_calendar (EURUSD,H4)       2024.01.05 08:30 USD 3 Nonfarm Payrolls (nonfarm-payrolls), United States (US) | 216 K | 1 K | 199 K | 173 K
test_calendar (EURUSD,H4)       2024.02.02 08:30 USD 3 Nonfarm Payrolls (nonfarm-payrolls), United States (US) | 353 K | 186 K | 216 K | 333 K
test_calendar (EURUSD,H4)       2024.03.08 08:30 USD 3 Nonfarm Payrolls (nonfarm-payrolls), United States (US) | 275 K | 220 K | 353 K | 229 K
test_calendar (EURUSD,H4)       2024.04.05 08:30 USD 3 Nonfarm Payrolls (nonfarm-payrolls), United States (US) | 303 K | 176 K | 275 K | 270 K
test_calendar (EURUSD,H4)       
test_calendar (USDJPY,H1)       2023.10.06 08:30 USD 3 Nonfarm Payrolls (nonfarm-payrolls), United States (US) | 336 K | 1 K | 187 K | 227 K
test_calendar (USDJPY,H1)       2023.11.03 08:30 USD 3 Nonfarm Payrolls (nonfarm-payrolls), United States (US) | 150 K | -8 K | 336 K | 297 K
test_calendar (USDJPY,H1)       2023.12.08 08:30 USD 3 Nonfarm Payrolls (nonfarm-payrolls), United States (US) | 199 K | -3 K | 150 K | 
test_calendar (USDJPY,H1)       2024.01.05 08:30 USD 3 Nonfarm Payrolls (nonfarm-payrolls), United States (US) | 216 K | 1 K | 199 K | 173 K
test_calendar (USDJPY,H1)       2024.02.02 08:30 USD 3 Nonfarm Payrolls (nonfarm-payrolls), United States (US) | 353 K | 186 K | 216 K | 333 K
test_calendar (USDJPY,H1)       2024.03.08 08:30 USD 3 Nonfarm Payrolls (nonfarm-payrolls), United States (US) | 275 K | 220 K | 353 K | 229 K
test_calendar (USDJPY,H1)       2024.04.05 08:30 USD 3 Nonfarm Payrolls (nonfarm-payrolls), United States (US) | 303 K | 176 K | 275 K | 270 K
test_calendar (USDJPY,H1)       
BrokerDaylightSchedule (USDJPY,H1)      Server : Trading.comMarkets-MT5
BrokerDaylightSchedule (USDJPY,H1)      Time   : 2024.04.12 20:06:30
BrokerDaylightSchedule (USDJPY,H1)      Offset : GMT-4
BrokerDaylightSchedule (USDJPY,H1)      DST_US : server dst begins on the second Sunday of March (+1) and ends on the first Sunday of November (-1)

I have tracked the root of the error: DST::IsEurope() wrongly returns true for these DST_US brokers because the algo implemented in DST::RolloverTime() is not reliable (i.e., scanning spreads).

test_calendar_fixed.mq5

#include "Calendar.mqh"
#include "TimeZoneInfo.mqh"  // https://www.mql5.com/en/code/48419

void OnStart()
  {
   CALENDAR Calendar;

   if(Calendar.Set(NULL, CALENDAR_IMPORTANCE_NONE, D'2023.10.1', TimeCurrent()))
     {
      Calendar.FilterByEventID(840030016); // NFP

      //Calendar.AutoDST();

      //-----------------------------------------------------------------------
      EVENT eEvents[];
      Calendar.GetEvents(eEvents);  // Extract events.

      for(int i = ArraySize(eEvents) - 1; i >= 0; i--)
        {
         if(eEvents[i].TimeMode == CALENDAR_TIMEMODE_DATETIME ||
            eEvents[i].TimeMode == CALENDAR_TIMEMODE_TENTATIVE)
           {
            //--- Convert server times -> GMT
            eEvents[i].time += DST::TimeServerGMTOffset();

            //--- Convert GMT -> back to server times (using the correct offsets)
            eEvents[i].time = CTimeZoneInfo::ConvertTimeForPlace(eEvents[i].time, ZONE_ID_UTC, ZONE_ID_BROKER);
           }
        }

      Calendar = eEvents;  // Restore events
      //-----------------------------------------------------------------------

      Print(Calendar.ToString());
     }
  }

Now results are correct:

test_calendar_fixed (EURUSD,H4) 2023.10.06 08:30 USD 3 Nonfarm Payrolls (nonfarm-payrolls), United States (US) | 336 K | 1 K | 187 K | 227 K
test_calendar_fixed (EURUSD,H4) 2023.11.03 08:30 USD 3 Nonfarm Payrolls (nonfarm-payrolls), United States (US) | 150 K | -8 K | 336 K | 297 K
test_calendar_fixed (EURUSD,H4) 2023.12.08 08:30 USD 3 Nonfarm Payrolls (nonfarm-payrolls), United States (US) | 199 K | -3 K | 150 K | 
test_calendar_fixed (EURUSD,H4) 2024.01.05 08:30 USD 3 Nonfarm Payrolls (nonfarm-payrolls), United States (US) | 216 K | 1 K | 199 K | 173 K
test_calendar_fixed (EURUSD,H4) 2024.02.02 08:30 USD 3 Nonfarm Payrolls (nonfarm-payrolls), United States (US) | 353 K | 186 K | 216 K | 333 K
test_calendar_fixed (EURUSD,H4) 2024.03.08 08:30 USD 3 Nonfarm Payrolls (nonfarm-payrolls), United States (US) | 275 K | 220 K | 353 K | 229 K
test_calendar_fixed (EURUSD,H4) 2024.04.05 08:30 USD 3 Nonfarm Payrolls (nonfarm-payrolls), United States (US) | 303 K | 176 K | 275 K | 270 K
test_calendar_fixed (EURUSD,H4) 
test_calendar_fixed (USDJPY,H1) 2023.10.06 08:30 USD 3 Nonfarm Payrolls (nonfarm-payrolls), United States (US) | 336 K | 1 K | 187 K | 227 K
test_calendar_fixed (USDJPY,H1) 2023.11.03 08:30 USD 3 Nonfarm Payrolls (nonfarm-payrolls), United States (US) | 150 K | -8 K | 336 K | 297 K
test_calendar_fixed (USDJPY,H1) 2023.12.08 08:30 USD 3 Nonfarm Payrolls (nonfarm-payrolls), United States (US) | 199 K | -3 K | 150 K | 
test_calendar_fixed (USDJPY,H1) 2024.01.05 08:30 USD 3 Nonfarm Payrolls (nonfarm-payrolls), United States (US) | 216 K | 1 K | 199 K | 173 K
test_calendar_fixed (USDJPY,H1) 2024.02.02 08:30 USD 3 Nonfarm Payrolls (nonfarm-payrolls), United States (US) | 353 K | 186 K | 216 K | 333 K
test_calendar_fixed (USDJPY,H1) 2024.03.08 08:30 USD 3 Nonfarm Payrolls (nonfarm-payrolls), United States (US) | 275 K | 220 K | 353 K | 229 K
test_calendar_fixed (USDJPY,H1) 2024.04.05 08:30 USD 3 Nonfarm Payrolls (nonfarm-payrolls), United States (US) | 303 K | 176 K | 275 K | 270 K
test_calendar_fixed (USDJPY,H1) 
BrokerDaylightSchedule (USDJPY,H1)      Server : Trading.comMarkets-MT5
BrokerDaylightSchedule (USDJPY,H1)      Time   : 2024.04.12 20:11:17
BrokerDaylightSchedule (USDJPY,H1)      Offset : GMT-4
BrokerDaylightSchedule (USDJPY,H1)      DST_US : server dst begins on the second Sunday of March (+1) and ends on the first Sunday of November (-1)

Hope this helps you to fix the lib.

 
amrali #:

Same code, different results according to the chart symbol. I tested 2 brokers.

I have tracked the root of the error: DST::IsEurope() wrongly returns true for these DST_US brokers because the algo implemented in DST::RolloverTime() is not reliable (i.e., scanning spreads).


test_calendar_fixed.mq5

Now results are correct:

Hope this helps you to fix the lib.

Thank you very much for finding the bug, an example of how to reproduce it, and the proposed solution!


I have made changes to the file DST.mqh.

 

Not totally fixed on DST_US brokers.

test_calendar (USDJPY,H1)       2023.10.06 08:30 USD 3 Nonfarm Payrolls (nonfarm-payrolls), United States (US) | 336 K | 1 K | 187 K | 227 K
test_calendar (USDJPY,H1)       2023.11.03 08:30 USD 3 Nonfarm Payrolls (nonfarm-payrolls), United States (US) | 150 K | -8 K | 336 K | 297 K
test_calendar (USDJPY,H1)       2023.12.08 08:30 USD 3 Nonfarm Payrolls (nonfarm-payrolls), United States (US) | 199 K | -3 K | 150 K | 
test_calendar (USDJPY,H1)       2024.01.05 08:30 USD 3 Nonfarm Payrolls (nonfarm-payrolls), United States (US) | 216 K | 1 K | 199 K | 173 K
test_calendar (USDJPY,H1)       2024.02.02 08:30 USD 3 Nonfarm Payrolls (nonfarm-payrolls), United States (US) | 353 K | 186 K | 216 K | 333 K
test_calendar (USDJPY,H1)       2024.03.08 08:30 USD 3 Nonfarm Payrolls (nonfarm-payrolls), United States (US) | 275 K | 220 K | 353 K | 229 K
test_calendar (USDJPY,H1)       2024.04.05 08:30 USD 3 Nonfarm Payrolls (nonfarm-payrolls), United States (US) | 303 K | 176 K | 275 K | 270 K
test_calendar (USDJPY,H1)       
test_calendar (EURUSD,H4)       2023.10.06 08:30 USD 3 Nonfarm Payrolls (nonfarm-payrolls), United States (US) | 336 K | 1 K | 187 K | 227 K
test_calendar (EURUSD,H4)       2023.11.03 07:30 USD 3 Nonfarm Payrolls (nonfarm-payrolls), United States (US) | 150 K | -8 K | 336 K | 297 K
test_calendar (EURUSD,H4)       2023.12.08 08:30 USD 3 Nonfarm Payrolls (nonfarm-payrolls), United States (US) | 199 K | -3 K | 150 K | 
test_calendar (EURUSD,H4)       2024.01.05 08:30 USD 3 Nonfarm Payrolls (nonfarm-payrolls), United States (US) | 216 K | 1 K | 199 K | 173 K
test_calendar (EURUSD,H4)       2024.02.02 08:30 USD 3 Nonfarm Payrolls (nonfarm-payrolls), United States (US) | 353 K | 186 K | 216 K | 333 K
test_calendar (EURUSD,H4)       2024.03.08 08:30 USD 3 Nonfarm Payrolls (nonfarm-payrolls), United States (US) | 275 K | 220 K | 353 K | 229 K
test_calendar (EURUSD,H4)       2024.04.05 08:30 USD 3 Nonfarm Payrolls (nonfarm-payrolls), United States (US) | 303 K | 176 K | 275 K | 270 K
test_calendar (EURUSD,H4)       
BrokerDaylightSchedule (EURUSD,H4)      Server : Trading.comMarkets-MT5
BrokerDaylightSchedule (EURUSD,H4)      Time   : 2024.04.13 17:40:05
BrokerDaylightSchedule (EURUSD,H4)      Offset : GMT-4
BrokerDaylightSchedule (EURUSD,H4)      DST_US : server dst begins on the second Sunday of March (+1) and ends on the first Sunday of November (-1)

Beware there is also another problem with DST_NONE brokers:

test_calendar (EURUSDm,M5)      2023.10.06 12:30 USD 3 Nonfarm Payrolls (nonfarm-payrolls), United States (US) | 336 K | 1 K | 187 K | 227 K
test_calendar (EURUSDm,M5)      2023.11.03 12:30 USD 3 Nonfarm Payrolls (nonfarm-payrolls), United States (US) | 150 K | -8 K | 336 K | 297 K
test_calendar (EURUSDm,M5)      2023.12.08 12:30 USD 3 Nonfarm Payrolls (nonfarm-payrolls), United States (US) | 199 K | -3 K | 150 K | 
test_calendar (EURUSDm,M5)      2024.01.05 12:30 USD 3 Nonfarm Payrolls (nonfarm-payrolls), United States (US) | 216 K | 1 K | 199 K | 173 K
test_calendar (EURUSDm,M5)      2024.02.02 12:30 USD 3 Nonfarm Payrolls (nonfarm-payrolls), United States (US) | 353 K | 186 K | 216 K | 333 K
test_calendar (EURUSDm,M5)      2024.03.08 12:30 USD 3 Nonfarm Payrolls (nonfarm-payrolls), United States (US) | 275 K | 220 K | 353 K | 229 K
test_calendar (EURUSDm,M5)      2024.04.05 12:30 USD 3 Nonfarm Payrolls (nonfarm-payrolls), United States (US) | 303 K | 176 K | 275 K | 270 K
test_calendar (EURUSDm,M5)      
test_calendar_fixed (EURUSDm,M5)        2023.10.06 12:30 USD 3 Nonfarm Payrolls (nonfarm-payrolls), United States (US) | 336 K | 1 K | 187 K | 227 K
test_calendar_fixed (EURUSDm,M5)        2023.11.03 12:30 USD 3 Nonfarm Payrolls (nonfarm-payrolls), United States (US) | 150 K | -8 K | 336 K | 297 K
test_calendar_fixed (EURUSDm,M5)        2023.12.08 13:30 USD 3 Nonfarm Payrolls (nonfarm-payrolls), United States (US) | 199 K | -3 K | 150 K | 
test_calendar_fixed (EURUSDm,M5)        2024.01.05 13:30 USD 3 Nonfarm Payrolls (nonfarm-payrolls), United States (US) | 216 K | 1 K | 199 K | 173 K
test_calendar_fixed (EURUSDm,M5)        2024.02.02 13:30 USD 3 Nonfarm Payrolls (nonfarm-payrolls), United States (US) | 353 K | 186 K | 216 K | 333 K
test_calendar_fixed (EURUSDm,M5)        2024.03.08 13:30 USD 3 Nonfarm Payrolls (nonfarm-payrolls), United States (US) | 275 K | 220 K | 353 K | 229 K
test_calendar_fixed (EURUSDm,M5)        2024.04.05 12:30 USD 3 Nonfarm Payrolls (nonfarm-payrolls), United States (US) | 303 K | 176 K | 275 K | 270 K
test_calendar_fixed (EURUSDm,M5)        
BrokerDaylightSchedule (EURUSDm,M5)     Server : Exness-MT5Trial
BrokerDaylightSchedule (EURUSDm,M5)     Time   : 2024.04.13 21:46:15
BrokerDaylightSchedule (EURUSDm,M5)     Offset : GMT+0
BrokerDaylightSchedule (EURUSDm,M5)     DST_NONE
Because exness servers never change into DST. New York is GMT-5 in winter months, So that NFP at 08:30 am NYC time should match (08:30 + 5:00 offset) = 13:30 Exness server time.
 
fxsaber #:

Unfortunately, you can't trust the current Calendar.

Very unfortunately, the MQ-Calendar contains errors in the timing of events even of the current day.

The news was at 07:30 (on the alternative calendar above and on the symbol graph), and the MQ-Calendar says (including the Web-version) that the news will be an hour later.