MQL4 Time and Time Related Issues Guide
Timing is everything in currency trading. - Kathy Lien
A nice World Time Map: http://www.weltzeit.de/zeitzonenkarte.php
We recommend you make a shortcut to this web link on your desktop.
Terms:
a.m. : ante meridiem, meaning before midday, or before the middle of the daylight hours.
Also, before noon is used to reduce confusion. For clarity, a person should say 12 Midnight
for 12:00 a.m. Ante Meridiem is usually referenced in coding comments as:
// a.m. is 00:00:00 to 11:59:59 (hours: minutes: seconds)
p.m. : post meridiem, meaning after midday. The time considered to be after noon. For
clarity one should say 12 Noon for 12:00 p.m. Post Meridiem is usually referenced in coding
comments as:
// p.m. is 12:00:00 to 23:59:59.
Meridian: A circle going around the earth passing through both the North and South poles.
Noon: This word originally referred to the ninth hour of the day (counting from sunrise,
some time around 6 am). It was nona "ninth" in Latin, from nona hora "ninth hour", and
the Anglo-Saxons recorded it as non. A related word is nones, the name of the daily church
service held at approximately 3pm in the Catholic church during the middle ages. By the
12th century, noon was used to refer to the "midday meal", and by the 13th century it
meant only "midday". It is apparent that the 3pm meal had shifted to 12 noon by the 12th
century, and it took the word noon with it.1
Midnight: The middle of the night hours.
Time Zones: One of the 24 established divisions or sectors into which the earth is divided
for convenience in reckoning time: each sector represents 15 degrees of longitude, or a
time interval of one hour. The zones start on the zero longitude in Greenwich, England. Each
time zone keeps time slightly differently so that at 12:00 noon the sun will be high in the
sky. http://wwp.greenwichmeantime.com/wise/dictionary/
In the United States the U.S. Department of Transportation is responsible for setting and
maintaining the six time zones of the United Sates.
GMT: Greenwich Mean Time 2. Pronounced Gren-itch. GMT is sometimes called
Greenwich Meridian Time because it is measured from the Greenwich Meridian Line
(Longitude 0) at the Royal Observatory in Greenwich, England. It is the place from where
all time zones are measured, starting in 1884. It is also known as the Universal Time.
Check out the GMT timestamp to see how accurate your computer time currently is!
Universal Time: Universal Time is equivalent to GMT. By international agreement, the term
UTC is recommended for all general timekeeping applications, and use of the term GMT is
discouraged.
1
Take Our Word For It, http://www.takeourword.com/Issue017.html
Greenwich Mean Time, http://wwp.greenwichmeantime.com/
It is often referred to as either Universal Time (UT) or Universal Time, Coordinated (UTC).
This time is considered to be the most accurate.
It is almost always referenced by a 24 hour clock: 0000 to 2359. Sometimes a Z is
appended to the time to indicate a Zero Hour offset because we are referencing the time at
the zero longitudinal meridian, as in 1030Z. Pronounced One Zero Three Zero Zulu.
UTC is considered to be more accurate for weather, and astronomical applications. UTC can
also be obtained readily from the Global Positioning System (GPS) satellites for navigation
purposes. 3
International Date Line: The International Date Line can
be anywhere on the globe. But it is most convenient to be
180 away from the defining meridian that goes through
Greenwich, England. It also is fortunate that this area is
covered, mainly, by empty ocean. However, there have
always been zigs and zags in it to allow for local
circumstances.4
Daylight Saving Time (DST):
http://webexhibits.org/daylightsaving/.
About 70 Countries use some form of Daylight Saving time.
Because the earth is tilted 23 degrees, we have changing
seasons, and shifting sunrise, and sunset times [as the
earth rotates around the Sun].5
The intent is to move 1 hour of daylight from the morning
to the evening to make better use of planet Earths available
daylight. In reality there is no saving of daylight. This
term could better be depicted as Daylight SHIFTING time.
The reasons to participate in DST are:
There is more sun light in the evening, so people turn their lights on less in the
Summer which creates an improved use of electricity in homes; 1% daily electricity
savings overall.
About 68% of people like having more light in the evening.
Violent crime is reduced 10 to 13%.
It generally decreases traffic accidents by about 1%.
The idea of daylight saving was first conceived by Benjamin Franklin during his sojourn as
an American delegate in Paris in 1784. It was conceptualized in his essay, "An Economical
Project."
EST: Eastern Standard Time (New York, United States). Add (-5) hours from Greenwich
Mean Time. This implies NO Daylight Saving time offset. Although if Daylight Saving time is
in effect, most people still refer to the time in New York, USA as EST.
EDT: Eastern Daylight Time. If Daylight Saving time is in effect, add (- 4) hours to
Greenwich Mean Time.
3
UTC is equivalent to the civil time for Iceland, Liberia, Morocco, Senegal, Ghana, Mali, Mauritania, and several
other countries. During the winter months, UTC is also the civil time scale for the United Kingdom and Ireland.
http://aa.usno.navy.mil/faq/docs/UT.html
4
Source: http://aa.usno.navy.mil/faq/docs/international_date.html
http://imagine.gsfc.nasa.gov/docs/ask_astro/answers/980225a.html
Worthless Trader information, but fun: There is never truly a Sunrise or Sunset, because
it is the Earths rotation which brings the Sun into view! It would be more accurate to say,
What a beautiful Earth rotational view this evening.
The circumference of the Earth is 40,008 kM, and is rotating at 1667 kM/Hr or, 27.78
kM/Min. In the 12 seconds it took you to read this paragraph, the earth rotated about 5.56
kM!
The Right Hand Rule: Hold your Thumb towards the North Pole and point your index finger
to the East. The earth rotates in the direction of your finger from West to East. As you move
East around the world, time increases. Paradoxically, if you travel far enough East, you will
arrive at Yesterday, when you cross the International Date Line.
Daylight Saving
Time by Country
Daylight Saving Time (DST)
Spring Move the Clock Forward +1 Hour so an
extra hour of daylight is shifted to the evening
during the Summer.
Standard Time:
Fall Move the
Clock Back (-1)
Hour
United States
before 2007. 6
The Uniform Time
Act of 1966, and
1986
2:00 a.m. Local time on the first Sunday of April.
At 1:59 a.m. add +1 Hour
2:00 a.m. Local time
on the last Sunday
of October.
At 1:59 a.m. add (
-1) Hour
United States,
2007 +
Energy Policy Act of 2005. This Act changed the
time change dates for Daylight Saving Time in
the U.S. Beginning in 2007, DST will begin on the
second Sunday of March .
The first Sunday of
November.
European Union
All Time zone
change at the
same time.
1:00 a.m. GMT. It begins on the last Sunday in
March.
1:00 a.m. GMT the
last Sunday in
October.
Russia (accuracy
of information
unknown)
During the summer, Russia's clocks are two hours
ahead of standard time. For example, Moscow
standard time (UTC+3) is about a half-hour
ahead of local mean time (UTC+2:30);
China
China has had a single time zone since May 1,
1980, observing summer Daylight Saving Time
from 1986 through 1991; they do not observe
DST now.
Japan
None
Equatorial and tropical countries (lower latitudes)
generally do not observe Daylight Saving Time. Since
the daylight hours are similar during every season, there
is no advantage to moving clocks forward during the
For the U.S. and its territories, Daylight Saving Time is NOT observed in Hawaii, American Samoa, Guam, Puerto
Rico, the Virgin Islands, Arizona, and most of the Eastern Time Zone portion of Indiana. The Navajo Nation
participates in the Daylight Saving Time policy, even in Arizona, due to its large size and location in three states.
summer.
Australia
http://www.statoids.com/tau.html
All Other
Countries
http://webexhibits.org/daylightsaving/g.html
Seasons:
The Northern Hemisphere is the upper half of the planet, and the Southern Hemisphere is
the lower half. The Seasons are opposite from each other in each half of the planet.
For example:
Winter
Spring
Summer
Autumn
USA
Dec. to Feb.
March to May
June to Aug.
Sept. to Nov.
Australia
June to Aug.
Sept. to Nov.
Dec. Feb.
March to May
http://www.wealth-lab.com/cgi-bin/WealthLab.DLL/topic?id=4894
World FOREX Markets
Missing Bars, adjustments for Daylight Saving time.
Time Stamping of Bars in MetaTrader 4.
Bars are time stamped at the END of the time interval.
One of the big problems associated with data is when the bar is time stamped. Some data
providers time stamp on the Open of the bar, others on the Close. Because of this it is
possible to get historical time data that is off by 1 time interval with another source of data.
History Center and Daylight saving time stamping.
Broker Time
Getting Everything in Sync
Suggestion: Set Your Computer Clock to GMT time with Auto Update. Why tolerate
the confusion with keeping track of Daylight Saving Time, Local Time Offsets, and keeping
your computer time accurate?
Here is a procedure to set your Trading Computer to GMT Time and automatically updating
it to NIST Atomic Clocks.
1. Open the Clock Update Menu by left
clicking on the time in the lower task bar:
2.
Choose the Adjust Date/Time option.
3. Choose the Time Zone
Tab. Choose GMT from
the pull down menu.
Deselect Automatically
adjust clock for
daylight saving
changes.
4. Set your computer to
automatically update weekly to
the NIST Atomic Clock.
Zolero: http://www.metatrader.org/cgi-bin/yabb2/YaBB.pl?action=search2
I had some problems to define how to make a program buy or sell only at first tick of new bar. I tried
volume and time methods and somehow nothing worked.
if(volume[0]>1) return(0); -- it is a good code if market is slowly moving but there is always a risk
that just a moment before new bar is created a bigger move starts and your code just doen't react.
so I made a little thinking and came up with a code that works very accurately... at least my tests
show that
at the beginning where you normally define extern int
datetime x;
x=iTime(NULL, 0, 0);
and then to the control line inside of start()
if(x==iTime(NULL, 0, 0)) return(0);
x=iTime(NULL, 0, 0);
that's it! just wanted to save some time for others who are trying to get their code act at this
moment.
Checkforelapsedtimefromlastentrytostopmultipleentriesonsame
bar
Five why:
MT doesn't use your local time, it uses whatever time the server is set to. You just happen to live in
the same time zone as your server.
you can call it a bug in MT4, I do, but the regardless you can't change the time and you can't make
the H4 bars span different hours than it already does. If you think it will help, goto MQ's website and
complain. But I doubt it will do any good.
string TimeToStr(datetime value, int mode=TIME_DATE|TIME_MINUTES)
Returns time as string in the format "yyyy.mm.dd hh:mi".
Parameters
value
Positive number of seconds from 00:00 January 1, 1970.
mod
e
Optional data output mode can be one or combination of:
TIME_DATE get result in form "yyyy.mm.dd",
TIME_MINUTES get result in form "hh:mi",
TIME_SECONDS get result in form "hh:mi:ss".
Sample
strign var1=TimeToStr(CurTime(),TIME_DATE|TIME_SECONDS);
int
ArrayBsearch(
double array[], double value, int count=WHOLE_ARRAY,
int start=0, int direction=MODE_ASCEND)
Returns the index of the first occurrence of a value in the first dimension of array if possible, or the
nearest
one,
if
the
occurrence
is
not
found.
The
function
cannot
be
used
with
string
arrays
and
serial
numeric
arrays.
Note: Binary search processes only sorted arrays. To sort numeric arrays use ArraySort() functions.
Parameters
array[]
The numeric array to search for.
value
The value to search for.
count
Count of elements to search for. By default, it searches in the whole array.
start
Starting index to search for. By default, the search starts on the first element.
directio
n
Search direction. It can be any of the following values:
MODE_ASCEND searching in forward direction,
MODE_DESCEND searching in the backward direction.
Sample
datetime daytimes[];
int
shift=10,dayshift;
// All the Time[] timeseries are sorted in descendant mode
ArrayCopySeries(daytimes,MODE_TIME,Symbol(),PERIOD_D1);
if(Time[shift]>=daytimes[0]) dayshift=0;
else
{
dayshift=ArrayBsearch(daytimes,Time[shift],WHOLE_ARRAY,0,MODE_DESCEND);
if(Period()<PERIOD_D1) dayshift++;
}
Print(TimeToStr(Time[shift])," corresponds to ",dayshift," day bar opened
at ",
TimeToStr(daytimes[dayshift]));
Time frame enumeration
Time frame on the chart. It can be any of the following values:
Constant
Value
Description
PERIOD_M1
1 minute.
PERIOD_M5
5 minutes.
PERIOD_M15
15
15 minutes.
PERIOD_M30
30
30 minutes.
PERIOD_H1
60
1 hour.
PERIOD_H4
240
4 hour.
PERIOD_D1
1440
Daily.
PERIOD_W1
10080
Weekly.
PERIOD_MN1
43200
Monthly.
0 (zero)
Time frame used on the
chart.
datetime CurTime()
Returns the last known server time, number of seconds elapsed from 00:00 January 1, 1970.
Sample
if(CurTime()-OrderOpenTime()<360) return(0);
int Day()
Returns the current day of the month.
Sample
if(Day()<5) return(0);
)
int DayOfWeek(
Returns the current zero based day of the week (0-Sunday,1,2,3,4,5,6).
Sample
// do not work on holidays.
if(DayOfWeek()==0 || DayOfWeek()==6) return(0);
int DayOfYear()
Returns the current day of the year (1-1 january,..,365(6) - 31 december).
Sample
if(DayOfYear()==245)
return(true);
int Hour()
Returns current hour (0,1,2,..23)
Sample
bool is_siesta=false;
if(Hour()>=12 || Hour()<17)
is_siesta=true;
datetime LocalTime()
Returns local computer time, number of seconds elapsed from 00:00 January 1, 1970.
Sample
if(LocalTime()-OrderOpenTime()<360) return(0);
int Minute()
Returns current minute (0,1,2,..59).
Sample
if(Minute()<=15)
return("first quarter");
int Month()
Returns current month as number (1-January,2,3,4,5,6,7,8,9,10,11,12).
Sample
if(Month()<=5)
return("first half of year");
int Seconds()
Returns current second (0,1,2,..59).
Sample
if(Seconds()<=15)
return(0);
int TimeDay(datetime date)
Returns day of month (1 - 31) for specified date.
Parameters
dat
e
Datetime is the number of seconds elapsed since midnight (00:00:00), January 1, 1970.
Sample
int day=TimeDay(D'2003.12.31');
// day is 31
int TimeDayOfWeek(datetime date)
Returns zero based day of week (0-Sunday,1,2,3,4,5,6) for specified date.
Parameters
dat
e
Datetime is the number of seconds elapsed since midnight (00:00:00), January 1, 1970.
Sample
int weekday=TimeDayOfWeek(D'2004.11.2');
// day is 2 - tuesday
int TimeDayOfYear(datetime date)
Returns day (1-1 january,..,365(6) - 31 december) of year for specified date.
Parameters
dat
e
Datetime is the number of seconds elapsed since midnight (00:00:00), January 1, 1970.
Sample
int day=TimeDayOfYear(CurTime());
int Year()
Returns current year.
Sample
// return if date before 1 May 2002
if(Year()==2002 && Month()<5)
return(0);
int TimeYear(datetime time)
Returns year for specified date. Return values can be in range 1970-2037.
Parameters
time
Datetime is the number of seconds elapsed since midnight (00:00:00), January 1, 1970.
Sample
int y=TimeYear(CurTime());
int TimeMonth(datetime time)
Returns month for specified time.
Parameters
time
Datetime is the number of seconds elapsed since midnight (00:00:00), January 1, 1970.
Sample
int m=TimeMonth(CurTime());
Problems with the Sunday Bar.
Triggers, delays, and Time Outs.
Using Complete Bars
Understand that when you back test, you have to access to complete bars, which means
that for the High, Low, and Closing prices to be valid, the interval must be complete.
Interestingly enough, the Opening price is valid prior to the close of the bar and therefore
you could use its value prior to a bar's completion. As stated in the request, this can be done
mechanically, i.e., in real-world trading.