i-Sessions-02_v1
Download scriptThis script is designed for the MetaTrader 4 platform to provide a visual organization of trading sessions directly on your price charts. Instead of manually calculating when specific market periods begin and end, this tool automatically draws highlighted zones that represent different blocks of time throughout the trading day.
Core Functionality
The primary purpose of the script is to divide the 24-hour trading day into eight distinct chronological segments. By identifying these boundaries, traders can more easily visualize high-activity periods, session overlaps, or specific market behaviors associated with different times of the day (such as the transition between Asian, European, and American sessions).
How It Works
- Time Block Definition: The script defines eight separate time windows. Each window has a specific start time, an end time, and a customizable color.
- Visual Representation: For every session defined, the script draws a rectangular box on the chart. These boxes are set to appear in the background of your price action so they do not obscure the candles or other indicators.
- Historical Backfilling: The tool does not just show the current time; it is capable of projecting these session blocks backward over a user-defined number of days (defaulting to 50 days). This allows you to see historical patterns of how the market behaved during specific hours in previous weeks or months.
- Dynamic Price Scaling: To ensure the boxes look correct regardless of how volatile the market is, the script automatically calculates the highest and lowest price points reached during each specific time window. It then stretches the rectangle to cover that entire price range, creating a clean "shaded area" effect for that session.
User Customization
The script provides several adjustable parameters to tailor the experience to your specific trading needs:
- Lookback Period: You can choose how many days of historical data you want to display.
- Time Boundaries: You can manually edit the start and end times for all eight sessions to align with your preferred timezone or specific market openings.
- Color Coding: Each session can be assigned a unique color, making it easy to distinguish between different global markets at a glance.
Summary of Logic
Essentially, the script scans the historical data of your chart, identifies the start and end times for eight pre-set periods, finds the highest and lowest prices reached during those times, and draws shaded boxes over those areas. This transforms a cluttered price chart into a structured map of the trading day.
/*-----------------------------+
| |
| Shared by www.Aptrafx.com |
| |
+------------------------------*/
//+------------------------------------------------------------------+
//| 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