Hi everyone,
I found a MT4 expert indicator that shows the mayor markets sessions. The expert didn’t show the sessions according to my server time zone so I do a little work on it.
Working with time zones always have been a headache to me in any scripting language, this time MQL appears not to be the exception.
The problem is that this simple code:
StrToTime( TimeToStr( TimeCurrent() ))
i.e: converting the current time from [I]timestamp[/I] to string and to [I]timestamp[/I] again appears to have a 4:30:00 (four and a half hours) of difference.
So I used this workaround and the session zones appear to be shown correctly:
twilightZone = TimeCurrent() - StrToTime(TimeToStr(TimeCurrent()));
In this case I simple add the [I]twilightZone[/I] timestamp difference before the objects are printed out to the chart.
This is the code:
File: Sessions.mq4
#property indicator_chart_window
//------- Âíåøíèå ïàðàìåòðû èíäèêàòîðà -------------------------------
extern int NumberOfDays = 100; // Êîëè÷åñòâî äíåé
extern string AsiaBegin = “00:00”; // Îòêðûòèå àçèàòñêîé ñåññèè
extern string AsiaEnd = “08:00”; // Çàêðûòèå àçèàòñêîé ñåññèè
extern color AsiaColor = 0xddffdd; // Öâåò àçèàòñêîé ñåññèè
extern string EurBegin = “07:00”; // Îòêðûòèå åâðîïåéñêîé ñåññèè
extern string EurEnd = “17:00”; // Çàêðûòèå åâðîïåéñêîé ñåññèè
extern color EurColor = 0xffeeff; // Öâåò åâðîïåéñêîé ñåññèè
extern string USABegin = “13:00”; // Îòêðûòèå àìåðèêàíñêîé ñåññèè
extern string USAEnd = “22:00”; // Çàêðûòèå àìåðèêàíñêîé ñåññèè
extern color USAColor = 0xf6f6e6; // Öâåò àìåðèêàíñêîé ñåññèè
extern bool ShowPrice = True; // Ïîêàçûâàòü öåíîâûå óðîâíè
extern color clFont = Silver; // Öâåò øðèôòà
extern int SizeFont = 8; // Ðàçìåð øðèôòà
extern int OffSet = 15; // Ñìåùåíèå
extern string ServerTimeZone = “+2:00”;
int SeverTZSecs;
int twilightZone;
//±-----------------------------------------------------------------+
//| Custom indicator initialization function |
//±-----------------------------------------------------------------+
void init() {
if (StringFind(ServerTimeZone,"-") >= 0){
SeverTZSecs = StrToTime(“0:00”) - StrToTime(ServerTimeZone);
}else{
SeverTZSecs = StrToTime(ServerTimeZone) - StrToTime(“0:00”);
}
twilightZone = TimeCurrent() - StrToTime(TimeToStr(TimeCurrent()));
DeleteObjects();
for (int i=0; i<NumberOfDays; i++) {
CreateObjects(“AS”+i, AsiaColor);
CreateObjects(“EU”+i, EurColor);
CreateObjects(“US”+i, USAColor);
}
Comment("");
}
//±-----------------------------------------------------------------+
//| Custor indicator deinitialization function |
//±-----------------------------------------------------------------+
void deinit() {
DeleteObjects();
Comment("");
}
//±-----------------------------------------------------------------+
//| Ñîçäàíèå îáúåêòîâ èíäèêàòîðà |
//| Ïàðàìåòðû: |
//| no - íàèìåíîâàíèå îáúåêòà |
//| cl - öâåò îáúåêòà |
//±-----------------------------------------------------------------+
void CreateObjects(string no, color cl) {
ObjectCreate(no, OBJ_RECTANGLE, 0, 0,0, 0,0);
ObjectSet(no, OBJPROP_STYLE, STYLE_SOLID);
ObjectSet(no, OBJPROP_COLOR, cl);
ObjectSet(no, OBJPROP_BACK, True);
}
//±-----------------------------------------------------------------+
//| Óäàëåíèå îáúåêòîâ èíäèêàòîðà |
//±-----------------------------------------------------------------+
void DeleteObjects() {
for (int i=0; i<NumberOfDays; i++) {
ObjectDelete(“AS”+i);
ObjectDelete(“EU”+i);
ObjectDelete(“US”+i);
}
ObjectDelete(“ASup”);
ObjectDelete(“ASdn”);
ObjectDelete(“EUup”);
ObjectDelete(“EUdn”);
ObjectDelete(“USup”);
ObjectDelete(“USdn”);
}
//±-----------------------------------------------------------------+
//| Custom indicator iteration function |
//±-----------------------------------------------------------------+
void start() {
datetime dt = TimeCurrent();
for (int i=0; i<NumberOfDays; i++) {
while (TimeDayOfWeek(dt) < 1 || TimeDayOfWeek(dt) > 5) dt=decDateTradeDay(dt);
if (ShowPrice && i==0) {
DrawPrices(dt, “AS”, AsiaBegin, AsiaEnd);
DrawPrices(dt, “EU”, EurBegin, EurEnd);
DrawPrices(dt, “US”, USABegin, USAEnd);
}
DrawObjects(dt, “AS”+i, AsiaBegin, AsiaEnd);
DrawObjects(dt, “EU”+i, EurBegin, EurEnd);
DrawObjects(dt, “US”+i, USABegin, USAEnd);
dt=decDateTradeDay(dt);
}
}
//±-----------------------------------------------------------------+
//| Ïðîðèñîâêà îáúåêòîâ íà ãðàôèêå |
//| Ïàðàìåòðû: |
//| dt - äàòà òîðãîâîãî äíÿ
//| no - íàèìåíîâàíèå îáúåêòà |
//| tb - âðåìÿ
//| te - âðåìÿ
//±-----------------------------------------------------------------+
void DrawObjects(datetime dt, string no, string tb, string te) {
datetime t1, t2;
double p1, p2;
int b1, b2;
t1=StrToTime(TimeToStr(dt, TIME_DATE)+" “+tb) + twilightZone + SeverTZSecs;
t2=StrToTime(TimeToStr(dt, TIME_DATE)+” "+te) + twilightZone + SeverTZSecs;
b1=iBarShift(NULL, 0, t1);
b2=iBarShift(NULL, 0, t2);
p1=High[Highest(NULL, 0, MODE_HIGH, b1-b2, b2)];
p2=Low [Lowest (NULL, 0, MODE_LOW , b1-b2, b2)];
ObjectSet(no, OBJPROP_TIME1 , t1);
ObjectSet(no, OBJPROP_PRICE1, p1);
ObjectSet(no, OBJPROP_TIME2 , t2);
ObjectSet(no, OBJPROP_PRICE2, p2);
}
//±-----------------------------------------------------------------+
//| Ïðîðèñîâêà öåíîâûõ ìåòîê íà ãðàôèêå |
//| Ïàðàìåòðû: |
//| dt - äàòà òîðãîâîãî äíÿ
//| no - íàèìåíîâàíèå îáúåêòà |
//| tb - âðåìÿ
//| te - âðåìÿ
//±-----------------------------------------------------------------+
void DrawPrices(datetime dt, string no, string tb, string te) {
datetime t1, t2;
double p1, p2;
int b1, b2;
t1=StrToTime(TimeToStr(dt, TIME_DATE)+" “+tb) + twilightZone + SeverTZSecs;
t2=StrToTime(TimeToStr(dt, TIME_DATE)+” "+te) + twilightZone + SeverTZSecs;
b1=iBarShift(NULL, 0, t1);
b2=iBarShift(NULL, 0, t2);
p1=High[Highest(NULL, 0, MODE_HIGH, b1-b2, b2)];
p2=Low [Lowest (NULL, 0, MODE_LOW , b1-b2, b2)];
if (ObjectFind(no+“up”)<0) ObjectCreate(no+“up”, OBJ_TEXT, 0, 0,0);
ObjectSet(no+“up”, OBJPROP_TIME1 , t2);
ObjectSet(no+“up”, OBJPROP_PRICE1 , p1+OffSet*Point);
ObjectSet(no+“up”, OBJPROP_COLOR , clFont);
ObjectSet(no+“up”, OBJPROP_FONTSIZE, SizeFont);
ObjectSetText(no+“up”, DoubleToStr(p1+Ask-Bid, Digits));
if (ObjectFind(no+“dn”)<0) ObjectCreate(no+“dn”, OBJ_TEXT, 0, 0,0);
ObjectSet(no+“dn”, OBJPROP_TIME1 , t2);
ObjectSet(no+“dn”, OBJPROP_PRICE1 , p2);
ObjectSet(no+“dn”, OBJPROP_COLOR , clFont);
ObjectSet(no+“dn”, OBJPROP_FONTSIZE, SizeFont);
ObjectSetText(no+“dn”, DoubleToStr(p2, Digits));
}
//±-----------------------------------------------------------------+
//| Óìåíüøåíèå äàòû íà îäèí òîðãîâûé äåíü |
//| Ïàðàìåòðû: |
//| dt - äàòà òîðãîâîãî äíÿ
//±-----------------------------------------------------------------+
datetime decDateTradeDay (datetime dt) {
int ty=TimeYear(dt);
int tm=TimeMonth(dt);
int td=TimeDay(dt);
int th=TimeHour(dt);
int ti=TimeMinute(dt);
td–;
if (td==0) {
tm–;
if (tm==0) {
ty–;
tm=12;
}
if (tm==1 || tm==3 || tm==5 || tm==7 || tm==8 || tm==10 || tm==12) td=31;
else if (tm==2){if (MathMod(ty, 4)==0) td=29; else td=28;}
else td=30;
}
return(StrToTime(ty+"."+tm+"."+td+" “+th+”:"+ti) + twilightZone);
}
//±-----------------------------------------------------------------+