Hello everyone,
I'm stuck with a (simple) problem concerning the calculation between 2 dates.
For example :
Today is the 30th of November. I would like to count the number of working days (not Saturday and Sunday) between September 17th and today with MQL4
Any idea ?
Thank you !
How would you do it with a pen and paper?
Write the code to do exactly the same.
- Why did you post your MT4 question in the Root / MT5 General
section instead of the MQL4 section, (bottom of the Root page?)
General rules and best pratices of the Forum. - General - MQL5 programming forum
Next time post in the correct place. The moderators will likely move this thread there soon. - 42137: I would like to count the number of working days (not Saturday and Sunday)So just count them. What's the problem?Not Compiled, not tested.
#define SECONDS uint #define HR2400 86400 // = 24 * 3600 = 1440 * 60 = PeriodSeconds(PERIOD_D1) SECONDS time(datetime when=0){ if(when == 0) when = TimeCurrent(); return SECONDS(when % HR2400); } datetime date(datetime when=0){ if(when == 0) when = TimeCurrent(); return datetime(when - time(when) ); } uint count_weekdays(datetime begin, datetime last=0){ // inclusive. if(last == 0) last = TimeCurrent(); begin = date(begin); last = date(last); uint count = 0; while(begin <= last){ ENUM_DAY_OF_WEEK dow = (ENUM_DAY_OF_WEEK)TimeDayOfWeek(last); if(dow > SUNDAY && dow < SATURDAY) ++count; last -= HR2400; } return count; }
Not Compiled, not tested.
You could just count the number of bars on D1:
datetime dfrom=D'17.9.2019'; datetime dtill=TimeCurrent(); int days=iBarShift(NULL,PERIOD_D1,dfrom) - iBarShift(NULL,PERIOD_D1,dtill);
This only counts true workdays.
- www.mql5.com
also
int nbDays = Bars(_Symbol,PERIOD_D1,D'2019.09.17',D'2019.11.30');
-
On MT4: Unless the current chart is that
specific symbols/TFs referenced, you
must handle 4066/4073
errors before accessing candle/indicator values.
Download history in MQL4 EA - Forex Calendar - MQL4 programming forum - Page 3 #26 № 4 - Not what OP asked for. Fails if your broker's timezone has short
Sunday or Saturday trading.
Chart times are broker times.
- FX opens 5pm Eastern Time (NY) Sunday and ends 5pm NY Friday. Some brokers start after (6pm is common/end before (up to 15
minutes) due to low volatility.
Checking for Market Closed - Expert Advisors and MQL5 programming forum Trading - MQL5 programming forum
Swap is computed 5pm ET. No swap if no open orders at that time. -
Brokers use a variety of timezones. Their local time (with or without DST,) GMT/UTC, GMT+2, NY+7.
Only with NY+7 does the broker's 00:00 equals 5pm ET and the start of a daily bar (and H4) is the start of a new FX day.
GMT brokers, means there is a 1 or 2 hour D1/H4 bar on Sunday (depending on NY DST,) and a short Friday bar.
GMT+2 is close but doesn't adjust for NY DST.
EET is closer except when their DST doesn't match NY's. Last Sunday of March and 1:00 on the last Sunday of October vs second Sunday in March and return at 2:00 a.m. EDT to 1:00 a.m. EST on the first Sunday in November.
-
Non-NY+7, means the chart daily bar overlaps the start, and converting broker time to NY time requires broker to GMT to NY timezone conversions.
- If you search the web you will find differing answers. Those are all wrong (half the year) because they do not take DST into
account (or that it changed in 2007 [important when testing history.])
- Then there are H4 candles that start on odd hours.
Why My XAUUSD 4H candles start with 1 hour shift? - Currency Pairs - General - MQL5 programming forum
- FX opens 5pm Eastern Time (NY) Sunday and ends 5pm NY Friday. Some brokers start after (6pm is common/end before (up to 15
minutes) due to low volatility.
- On MT4: Unless the current chart is that specific symbols/TFs referenced, you must handle 4066/4073
errors before accessing candle/indicator values.
Download history in MQL4 EA - Forex Calendar - MQL4 programming forum - Page 3 #26 № 4 - Not what OP asked for. Fails if your broker's timezone has short Sunday or Saturday trading.
1. Of course, not to be called in OnInit, but a script would do fine.
2. Yes, to avoid the Sunday bars you need to choose a symbol that's not traded on that day. Major index such as Dow should do it. The neat thing
about this approach is that you don't need to count public holidays. I guess that's what OP was looking for.
1. yup
2. The topic is about workdays, not weekdays. So until OP comes back and clarifies, we could also say that your code is not what OP was asking
for.
1. yup
2. The topic is about workdays, not weekdays. So until OP
comes back and clarifies, we could also say that your code is not what OP was asking for.
Hello everyone,
I'm stuck with a (simple) problem concerning the calculation between 2 dates.
For example :
Today is the 30th of November. I would like to count the number of working days (not Saturday and Sunday) between September 17th and today with MQL4
Any idea ?
Thank you !
I think that the OP was clear enough.
Hello Everyone,
Thanks for all your answers.
My question was about worksday (So Just Monday, Tuesday, Wednesday,Thursday and Friday only).
Counting the number of bars doesn't work well because of the sunday opening, as William noticed.
I'm working on Forex so maybe by counting the number of bars with an other asset class as suggested lippmaje... I'll check
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hello everyone,
I'm stuck with a (simple) problem concerning the calculation between 2 dates.
For example :
Today is the 30th of November. I would like to count the number of working days (not Saturday and Sunday) between September 17th and today with MQL4
Any idea ?
Thank you !