MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal

Source code library - Expert Advisors, Indicators and Scripts

MetaTrader Market: we have 650 Expert Advisors for algorithmic trading!
To post a new code, please log in or register

Interesting script?
So post a link to it -
let others appraise it

You liked the script? Try it in the MetaTrader 5 terminal

2008.07.08 08:31
Indicator of Trading Sessions

Indicator of Trading Sessions - indicator for MetaTrader 4

| English Spanish Portuguese Russian Chinese

Views:
12385
Rating:
votes: 5
Files:
i_sessions.gif (11.49 KB)

The indicator shows the beginnings and the ends of three trading sessions: Asian, European, and American. The sessions are displayed with the background rectangles. The length of the rectangle corresponds with the duration of a trading session, and its height is the trade range.



Translated from Russian by MetaQuotes Software Corp.
Original code: http://www.mql5.com/ru/code/7753

Last comments | Go to discussion (5)
MQL4 Comments
mql4_comments | 25 Sep 2008 at 10:48

You can alter the start/stop times for the individual session in the start window for the indicator.  I was able to move the start time for the asian session up to the 5pm EST dateline on metatrader.  However, when I tried to delay the US session one hour so that it would end at this date line instead of 4pm EST, the coding refused to extend this rectangle the extra hour to the dateline!  The end of the US session is defaulted to "23:00" and I changed it to "24:00" and nothing changed on the chart.  Then I tried entering "00:00" which is the changed start time for the asian session (which worked for the asian session), but this caused the US session rectangle to completely move elsewhere and expand to overlap the asian and most of the euro session!

Obviously, the coding for this indicator is flawed. 

I like the indicator and request the author rework the code to fix it so the user can alter start and stop times without the indicator failing.

Also, a change I would like to see is to make the height of the rectangles for the upcoming session(s), or part(s) thereof, be the same as the height of the current  session (height of session at current bar) instead of filling the whole screen from top to bottom, which I find distracting.  Or maybe have them displayed in a narrow band.

Any fixes and improvements will be appreciated.

MQL4 Comments
mql4_comments | 24 Nov 2008 at 19:32
frederick:

You can alter the start/stop times for the individual session in the start window for the indicator. I was able to move the start time for the asian session up to the 5pm EST dateline on metatrader. However, when I tried to delay the US session one hour so that it would end at this date line instead of 4pm EST, the coding refused to extend this rectangle the extra hour to the dateline! The end of the US session is defaulted to "23:00" and I changed it to "24:00" and nothing changed on the chart. Then I tried entering "00:00" which is the changed start time for the asian session (which worked for the asian session), but this caused the US session rectangle to completely move elsewhere and expand to overlap the asian and most of the euro session!

Obviously, the coding for this indicator is flawed.

I like the indicator and request the author rework the code to fix it so the user can alter start and stop times without the indicator failing.

Also, a change I would like to see is to make the height of the rectangles for the upcoming session(s), or part(s) thereof, be the same as the height of the current session (height of session at current bar) instead of filling the whole screen from top to bottom, which I find distracting. Or maybe have them displayed in a narrow band.

Any fixes and improvements will be appreciated.

Try to change the US ending time to 23.55

aaron lee
pulsation | 23 Jul 2010 at 22:30

Two comments.

1. Because this indicator paints over the bar representing the time you supplied as an argument, you want your session closing times to be xx:59 instead of xx:00. In frederick's case, his NY closing time should be 23:59.

2. This indicator doesn't handle the session opening bar correctly if it happens to be the highest or lowest of the session. You can even see it in the sample picture above where both Asian session rectangles don't cover the session highs. To correct this problem, modify two lines inside DrawObjects() as follows:

void DrawObjects(datetime dt, string no, string tb, string te)

{ // ............

p1=High[iHighest(NULL, 0, MODE_HIGH, b1-b2+1, b2)];

p2=Low [iLowest (NULL, 0, MODE_LOW, b1-b2+1, b2)];

// ............ }

MQL4 Comments
mql4_comments | 9 Jun 2011 at 06:52

If a session starts before midnight and ends after midnight it will be drawn incorrectly. I have this problem with the Asian session (22:00 -> 09:00) and anyone who is using a UTC broker will have this problem.

below is the updated code to correctly show the asian session ONLY...

//+------------------------------------------------------------------+
//|                                                   i-Sessions.mq4 |
//|                                           Êèì Èãîðü Â. aka KimIV |
//|                                              http://www.kimiv.ru |
//|                                                                  |
//|  16.11.2005  Èíäèêàòîð òîðãîâûõ ñåññèé                           |
//+------------------------------------------------------------------+
#property copyright "Êèì Èãîðü Â. aka KimIV"
#property link      "http://www.kimiv.ru"
#property indicator_chart_window
//------- Âíåøíèå ïàðàìåòðû èíäèêàòîðà -------------------------------
extern int    NumberOfDays = 50;             // Êîëè÷åñòâî äíåé
extern string AsiaBegin    = "22:00";        // Îòêðûòèå àçèàòñêîé ñåññèè
extern string AsiaEnd      = "09:00";        // Çàêðûòèå àçèàòñêîé ñåññèè
extern color  AsiaColor    = C'0,32,0';      // Öâåò àçèàòñêîé ñåññèè
extern string EurBegin     = "08:00";        // Îòêðûòèå åâðîïåéñêîé ñåññèè
extern string EurEnd       = "17:00";        // Çàêðûòèå åâðîïåéñêîé ñåññèè
extern color  EurColor     = C'48,0,0';      // Öâåò åâðîïåéñêîé ñåññèè
extern string USABegin     = "13:00";        // Îòêðûòèå àìåðèêàíñêîé ñåññèè
extern string USAEnd       = "21:00";        // Çàêðûòèå àìåðèêàíñêîé ñåññèè
extern color  USAColor     = C'0,0,56';      // Öâåò àìåðèêàíñêîé ñåññèè
extern bool   ShowPrice    = False;          // Ïîêàçûâàòü öåíîâûå óðîâíè
extern color  clFont       = Blue;           // Öâåò øðèôòà
extern int    SizeFont     = 8;              // Ðàçìåð øðèôòà
extern int    OffSet       = 10;             // Ñìåùåíèå
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
void init() {
  DeleteObjects();
  //for (int i=0; i<NumberOfDays; i++) {
  for (int i=NumberOfDays; i>=0; 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++) {
  for (int i=NumberOfDays; i>=0; 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=CurTime();
  
  //for (int i=0; i<NumberOfDays; i++) {
  for (int i=NumberOfDays; i>=0; i--) {
    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);
    while (TimeDayOfWeek(dt)>5) 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;
  
  if (StringFind(no,"AS",0)==0) {
    t1=AsianDate(dt, tb);
  } else {
    t1=StrToTime(TimeToStr(dt, TIME_DATE)+" "+tb);
  }
  t2=StrToTime(TimeToStr(dt, TIME_DATE)+" "+te);
  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;
  if (StringFind(no,"AS",0)==0) {
    t1=AsianDate(dt, tb);
  } else {
    t1=StrToTime(TimeToStr(dt, TIME_DATE)+" "+tb);
  }
  t2=StrToTime(TimeToStr(dt, TIME_DATE)+" "+te);
  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;
    if (tm==2) if (MathMod(ty, 4)==0) td=29; else td=28;
    if (tm==4 || tm==6 || tm==9 || tm==11) td=30;
  }
  return(StrToTime(ty+"."+tm+"."+td+" "+th+":"+ti));
}
datetime AsianDate (datetime dt, string tb) {
  int ty=TimeYear(dt);
  int tm=TimeMonth(dt);
  int td=TimeDay(dt);
  int th=TimeHour(dt);
  int ti=TimeMinute(dt);
  td--;
  return(StrToTime(ty+"."+tm+"."+td+" "+tb));
}
//+------------------------------------------------------------------+
sara01
sara01 | 26 Aug 2013 at 14:24

Hello KimIV!

I need that my EA close all EURUSD trades when the day session end or at 23:30 hrs. server time and work again when the next session begin.

Do you know how to do it?

I don´t know how to stop it.

Thank you!

Sara