i-Sessions-02

Author: ��� ����� �. aka KimIV
i-Sessions-02

This script is designed to visually organize a trading chart by highlighting specific time blocks, commonly referred to as "trading sessions." Instead of forcing you to manually track the clock to know when different market behaviors occur, the tool automatically draws shaded regions on your chart to represent these periods.

Core Functionality

The primary purpose of this script is to divide the trading day into eight distinct segments. Each segment has its own designated start and end time, which can be customized by the user. Once these times are set, the script identifies the high and low price points reached during those specific windows and draws a rectangular box over them.

How it Works

  • Dynamic Time Mapping: The logic automatically calculates the correct dates for the past 50 trading days. It is smart enough to skip weekends (Saturdays and Sundays), ensuring that only active market days are populated with data.
  • Price Range Detection: For every designated session, the script looks back at the historical price data. It identifies the highest point reached and the lowest point reached during that specific timeframe.
  • Visual Overlay: It then places a rectangle on your chart that spans from the start time to the end time of the session, covering the vertical space between those high and low prices. This allows you to see the "volatility range" of each period at a glance.
  • Customization: You can define exactly when these sessions begin and end (for example, setting a 3-hour window for a specific market's opening). You can also choose different colors for each session to easily distinguish between different global markets (like London, New York, or Tokyo sessions).

Key Benefits for the User

  • Visual Clarity: It provides an immediate "map" of the day’s price action, making it easier to see which times of day are the most active and which are the most volatile.
  • Automated History: Because it looks back over a set number of days (defaulting to 50), you can see historical patterns of how certain sessions behave without having to scroll through months of data manually.
  • Clean Layout: The script includes a "cleanup" logic that ensures the chart remains organized by removing and regenerating the correct objects whenever the indicator is loaded or updated, preventing clutter on your screen.
20 Views
0 Downloads
0 Favorites
i-Sessions-02
//+------------------------------------------------------------------+
//|                                                   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 S1Begin    = "00:55";
extern string S1End      = "03:55";
extern color  S1Color    = DarkSlateGray;

extern string S2Begin    = "03:55";
extern string S2End      = "06:55";
extern color  S2Color    = DarkSlateGray;

extern string S3Begin    = "06:55";
extern string S3End      = "09:55";
extern color  S3Color    = DarkSlateGray;

extern string S4Begin    = "09:55";
extern string S4End      = "12:55";
extern color  S4Color    = DarkSlateGray;

extern string S5Begin    = "12:55";
extern string S5End      = "15:55";
extern color  S5Color    = DarkSlateGray;

extern string S6Begin    = "15:55";
extern string S6End      = "18:55";
extern color  S6Color    = DarkSlateGray;

extern string S7Begin    = "18:55";
extern string S7End      = "21:55";
extern color  S7Color    = DarkSlateGray;

extern string S8Begin    = "21:55";
extern string S8End      = "23:55";
extern color  S8Color    = DarkSlateGray;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
void init() {
  DeleteObjects();
  for (int i=0; i<NumberOfDays; i++) {
    CreateObjects("S1"+i, S1Color);
    CreateObjects("S2"+i, S2Color);
    CreateObjects("S3"+i, S3Color);
    CreateObjects("S4"+i, S4Color);
    CreateObjects("S5"+i, S5Color);
    CreateObjects("S6"+i, S6Color);
    CreateObjects("S7"+i, S7Color);
    CreateObjects("S8"+i, S8Color);
 }
  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, 2);
  ObjectSet(no, OBJPROP_COLOR, cl);
  ObjectSet(no, OBJPROP_BACK, True);
}

//+------------------------------------------------------------------+
//| Óäàëåíèå îáúåêòîâ èíäèêàòîðà                                     |
//+------------------------------------------------------------------+
void DeleteObjects() {
  for (int i=0; i<NumberOfDays; i++) {
    ObjectDelete("S1"+i);
    ObjectDelete("S2"+i);
    ObjectDelete("S3"+i);
    ObjectDelete("S4"+i);
    ObjectDelete("S5"+i);
    ObjectDelete("S6"+i);
    ObjectDelete("S7"+i);
    ObjectDelete("S8"+i);
  }
}

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
void start() {
  datetime dt=CurTime();

  for (int i=0; i<NumberOfDays; i++) {
    DrawObjects(dt, "S1"+i, S1Begin, S1End);
    DrawObjects(dt, "S2"+i, S2Begin, S2End);
    DrawObjects(dt, "S3"+i, S3Begin, S3End);
    DrawObjects(dt, "S4"+i, S4Begin, S4End);
    DrawObjects(dt, "S5"+i, S5Begin, S5End);
    DrawObjects(dt, "S6"+i, S6Begin, S6End);
    DrawObjects(dt, "S7"+i, S7Begin, S7End);
    DrawObjects(dt, "S8"+i, S8Begin, S8End);
      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;

  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 - äàòà òîðãîâîãî äíÿ                                        |
//+------------------------------------------------------------------+
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));
}
//+------------------------------------------------------------------+

Comments