Find bar of the same time one day ago
- You probably do not want yesterday (weekends and market holidays.) You want trading days.
- Save the time, find yesterday date, find the correct time.
#define HR2400 (PERIOD_D1 * 60) // 86400 = 24 * 3600 = 1440 * 60 #define INDEX uint #define SECONDS uint 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) ); } datetime tomorrow( datetime when=0){ return date(when) + HR2400; } datetime yesterday(datetime when=0){ datetime BOD = date(when); INDEX iBod = iBarShift(_Symbol, _Period, BOD); return date(Time[iBod-1]); } : datetime when = TimeCurrent(); SECONDS TodayTOD = time(when); datetime yesterdayBod = yesterday(when); INDEX yesterdayTOD = iBarShift(_Symbol, _Period, yesterday + TOD);
- The other approach is to use the D1 chart, but you must must handle
4066/4073 errors.
Download history in MQL4 EA - MQL4 and MetaTrader 4 - MQL4 programming forum
Thank you whroeder1, I think it will work just fine (I'll test it tomorrow). There's no way working around that complexity, is there ;)
Thank you whroeder1, I think it will work just fine (I'll test it tomorrow). There's no way working around that complexity, is there ;)
So I tested it doesn't quite work, but I can't understand why. Something is fishy with unix time...
I have a Log function which logs Date of Day and Time of Day when OnCalculate is called.
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[]) { Print("Date of Day time[rates_total-1-0) = ", TimeToStr(DateOfDay(time[rates_total-1-0])), " time into day = ", TimeToStr(TimeOfDay(time[rates_total-1-0]), TIME_SECONDS)); ... }
Now I would've expected it to log the date of the day of the first bar from the back of the chart and the time in seconds that we've already progressed into that day at this point. So what I expected was:
Date of Day time[rates_total-1-0) = 2014.06.12 00:00 time into day = 14:30:00
What actually prints though is:
Date of Day time[rates_total-1-0) = 2014.06.12 08:30 time into day = 06:00:00
The day doesn't start at midnight, and TimeOfDay() is also off therefore. It seems like the linearity of unix time is broken somewhere and the seconds do not add up to all full days since 1970.1.1 ... How can this even be?
-
-
FX opens 5pm ET Sunday and ends 5pm ET 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 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 NYs. 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.])
-
FX opens 5pm ET Sunday and ends 5pm ET Friday. Some brokers start after (6pm is common/end before (up
to 15 minutes) due to low volatility.
- You probably do not want yesterday (weekends and market holidays.) You want trading days.
- Save the time, find yesterday date, find the correct time.
- The other approach is to use the D1 chart, but you must must handle
4066/4073 errors.
Download history in MQL4 EA - MQL4 and MetaTrader 4 - MQL4 programming forum
- You probably do not want yesterday (weekends and market holidays.) You want trading days.
- Save the time, find yesterday date, find the correct time.
- The other approach is to use the D1 chart, but you must must handle 4066/4073 errors.
Download history in MQL4 EA - MQL4 and MetaTrader 4 - MQL4 programming forum

- Free trading apps
- Free Forex VPS for 24 hours
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi guys,
As described in the title, I want to find the bar of one day ago at exactly the same time. So e.g. the time now is 20:23 on the 06.10.2017 which is covered by the 20:20 M5-bar. I want to find the M5 that covers yesterday's (05.10.2017) period of 20:20. How would you do it?
Thanks :)