Indicators: Indicator of Trading Sessions - page 2

 
star
 

To the traders who are asking for MQL5 version, here is the code that you can copy paste into your MQL5 MetaEditor. note I just modified the code to work on MQL5 only colors were changed and timing windows were changed, EVERY THING belongs to the original author (KimIV). NOTE: the session timings in right now are based on FTMO America servers, you'll have to convert it to your own timings. but I only use New York time. Asia- 8pm - 11pm | London 11pm - 2am | NewYork 8am - 11am. GOOD LUCK.

//+------------------------------------------------------------------+

//|                                                i-Sessions_mq5.mq5|

//|                                       Converted from MQL4 version|

//|                                         Original by KimIV, Russia|

//|                                              https://www.kimiv.ru|

//|                                                                  |

//|                            Indicator of trading sessions (visual)|

//+------------------------------------------------------------------+

#property copyright "Copyright 2005-2025, KimIV, converted to MQL5"

#property link      "https://www.kimiv.ru"

#property version   "1.00"

#property indicator_chart_window



//--- Input parameters 

input int      NumberOfDays = 10;        // Number of days to display

input string   AsiaBegin    = "03:00";   // Asia session start time -- it is in FTMO broker time NY 8pm

input string   AsiaEnd      = "06:00";   // Asia session end time -- NY 11pm

input color    AsiaColor    = clrCyan; // Asia session rectangle color

input string   EurBegin     = "09:00";   // European session start time --NY 2am

input string   EurEnd       = "12:00";   // European session end time -- NY 5am

input color    EurColor     = clrPink;       // European session rectangle color

input string   USABegin     = "15:00";   // US session start time --NY 8am

input string   USAEnd       = "18:00";   // US session end time -- NY 11am

input color    USAColor     = clrYellow; // US session rectangle color



//+------------------------------------------------------------------+

//| Custom indicator initialization function                         |

//+------------------------------------------------------------------+

int OnInit()

{

   return(INIT_SUCCEEDED);

}



//+------------------------------------------------------------------+

//| Custom indicator deinitialization function                       |

//+------------------------------------------------------------------+

void OnDeinit(const int reason)

{

   ObjectsDeleteAll(0, "SS_");

   Comment("");

}



//+------------------------------------------------------------------+

//| Custom indicator iteration function                              |

//+------------------------------------------------------------------+

int OnCalculate(const int rates_total,

                const int prev_calculated,

                const datetime &time[],

                const double &open[],

                const double &high[],

                const double &low[],

                const double &close[],

                const long &tick_volume[],

                const long &volume[],

                const int &spread[])

{

   RefreshSessions();

   return(rates_total);

}



//+------------------------------------------------------------------+

//| Refresh all session rectangles                                   |

//+------------------------------------------------------------------+

void RefreshSessions()

{

   datetime dt = TimeCurrent();

   

   for(int i = 0; i < NumberOfDays; i++)

   {

      DrawSession(dt, "SS_AS_" + IntegerToString(i), AsiaBegin, AsiaEnd, AsiaColor);

      DrawSession(dt, "SS_EU_" + IntegerToString(i), EurBegin,  EurEnd,  EurColor);

      DrawSession(dt, "SS_US_" + IntegerToString(i), USABegin, USAEnd,  USAColor);

      

      dt = PrevTradingDay(dt);

   }

}



//+------------------------------------------------------------------+

//| Draw a single session rectangle                                  |

//+------------------------------------------------------------------+

void DrawSession(datetime dt, string objName, string startTimeStr, string endTimeStr, color clr)

{

   string datePart = TimeToString(dt, TIME_DATE);

   datetime t1 = StringToTime(datePart + " " + startTimeStr);

   datetime t2 = StringToTime(datePart + " " + endTimeStr);

   

   // Handle overnight sessions (e.g., 22:00 to 06:00 next day)

   if(t2 <= t1)

      t2 += 86400;

   

   double highPrice = 0, lowPrice = 0;

   if(!GetSessionHighLow(t1, t2, highPrice, lowPrice))

      return;

   

   UpdateRectangle(objName, t1, t2, highPrice, lowPrice, clr);

}



//+------------------------------------------------------------------+

//| Retrieve highest high and lowest low between two times          |

//+------------------------------------------------------------------+

bool GetSessionHighLow(datetime t1, datetime t2, double &highPrice, double &lowPrice)

{

   double highArray[];

   int highCount = CopyHigh(_Symbol, PERIOD_CURRENT, t1, t2, highArray);

   if(highCount <= 0)

      return false;

   

   double lowArray[];

   int lowCount = CopyLow(_Symbol, PERIOD_CURRENT, t1, t2, lowArray);

   if(lowCount <= 0)

      return false;

   

   highPrice = highArray[ArrayMaximum(highArray)];

   lowPrice  = lowArray[ArrayMinimum(lowArray)];

   return true;

}



//+------------------------------------------------------------------+

//| Create or update a rectangle object                              |

//+------------------------------------------------------------------+

void UpdateRectangle(string name, datetime t1, datetime t2, double highPrice, double lowPrice, color clr)

{

   if(ObjectFind(0, name) < 0)

   {

      ObjectCreate(0, name, OBJ_RECTANGLE, 0, t1, highPrice, t2, lowPrice);

      ObjectSetInteger(0, name, OBJPROP_STYLE, STYLE_SOLID);

      ObjectSetInteger(0, name, OBJPROP_COLOR, clr);

      ObjectSetInteger(0, name, OBJPROP_BACK, true);

      ObjectSetInteger(0, name, OBJPROP_FILL, true);

      ObjectSetInteger(0, name, OBJPROP_WIDTH, 1);

   }

   else

   {

      ObjectSetInteger(0, name, OBJPROP_TIME, 0, t1);

      ObjectSetDouble(0, name, OBJPROP_PRICE, 0, highPrice);

      ObjectSetInteger(0, name, OBJPROP_TIME, 1, t2);

      ObjectSetDouble(0, name, OBJPROP_PRICE, 1, lowPrice);

      ObjectSetInteger(0, name, OBJPROP_COLOR, clr);

   }

}



//+------------------------------------------------------------------+

//| Returns the previous trading day (Monday to Friday)              |

//+------------------------------------------------------------------+

datetime PrevTradingDay(datetime dt)

{

   MqlDateTime dtStruct;

   dt -= 86400;

   TimeToStruct(dt, dtStruct);

   // day_of_week: 0=Sunday, 1=Monday, ..., 6=Saturday

   while(dtStruct.day_of_week == 0 || dtStruct.day_of_week == 6)

   {

      dt -= 86400;

      TimeToStruct(dt, dtStruct);

   }

   return dt;

}

//+------------------------------------------------------------------+