IND_TradingSessions

Author: Developer: Andrey Minaev
Price Data Components
Series array that contains open time of each bar
Miscellaneous
It issuies visual alerts to the screen
0 Views
0 Downloads
0 Favorites
IND_TradingSessions
ÿþ//+-----------------------------------------------------------------------------------------------+

//| Developer:   Andrey Minaev                                                                    |

//| Indicator:   IND_TradingSessions.mq4                                                          |

//| MQL5:        mql5.com/ru/users/id.scorpion                                                    |

//| Mail:        id.scorpion@mail.ru                                                              |

//| Skype:       id.scorpion                                                                      |

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

#property copyright "Developer: Andrey Minaev"

#property link      "http://www.mql5.com/ru/users/id.scorpion"

#property version   "1.00"

#property strict

#property indicator_chart_window



enum YESNO

{

   YES,   // 0

   NO,    // 5B

};



input int   diffInHours          = -2;             //  07=8F0 2> 2@5<5=8 GMT 8 A5@25@=>3> 2@5<5=8 1@>:5@0 (G0AK)

input YESNO showAsianSession     = YES;            // >:07K20BL 0780B:CN A5AA8N

input color colorAsianSession    = clrKhaki;       // &25B 0780BA:>9 A5AA88

input YESNO showEuropeanSession  = YES;            // >:07K20BL 52@>?59A:CN A5AA8N

input color colorEuropeanSession = clrLightBlue;   // &25B 52@>?59A:>9 A5AA88

input YESNO showAmericanSession  = YES;            // >:07K20BL 0<5@8:0=A:CN A5AA8N

input color colorAmericanSession = clrPink;        // &25B 0<5@8:0=A:>9 A5AA88

input YESNO showPacificSession   = YES;            // >:07K20BL B8E>>:50=A:CN A5AA8N

input color colorPacificSession  = clrLightGray;   // &25B B8E>>:50=A:>9 A5AA88



datetime newCandle;   // =>20O A25G0

long chartID;         // 845=B8D8:0B>@ 3@0D8:0

int  dayOfWeek;       // 45=L =545;8



//--- 0780BA:0O A5AA8O

bool     remTimeAsS;    // true - @07@5H5=> 70?><=8BL =>2K5 40BK A5AA88

datetime begTimeAsS;    // 2@5<O =0G0;0 A5AA88

datetime endTimeAsS;    // 2@5<O :>=F0 A5AA88

int      begHourAsS;    // G0A =0G0;0 A5AA88

int      endHourAsS;    // G0A :>=F0 A5AA88

double   maxPriceAsS;   // <0:A8<0;L=0O F5=0 70 A5AA8N

double   minPriceAsS;   // <8=8<0;L=0O F5=0 70 A5AA8N

bool     oneDayAsS;     // true -  A5AA8O =0E>48BAO 2 ?@545;0E >4=>3> 4=O



//--- 52@>?59A:0O A5AA8O

bool     remTimeEuS;    // true - @07@5H5=> 70?><=8BL =>2K5 40BK A5AA88

datetime begTimeEuS;    // 2@5<O =0G0;0 A5AA88

datetime endTimeEuS;    // 2@5<O :>=F0 A5AA88

int      begHourEuS;    // G0A =0G0;0 A5AA88

int      endHourEuS;    // G0A :>=F0 A5AA88

double   maxPriceEuS;   // <0:A8<0;L=0O F5=0 70 A5AA8N

double   minPriceEuS;   // <8=8<0;L=0O F5=0 70 A5AA8N

bool     oneDayEuS;     // true -  A5AA8O =0E>48BAO 2 ?@545;0E >4=>3> 4=O



//--- 0<5@8:0=A:0O A5AA8O

bool     remTimeAmS;    // true - @07@5H5=> 70?><=8BL =>2K5 40BK A5AA88

datetime begTimeAmS;    // 2@5<O =0G0;0 A5AA88

datetime endTimeAmS;    // 2@5<O :>=F0 A5AA88

int      begHourAmS;    // G0A =0G0;0 A5AA88

int      endHourAmS;    // G0A :>=F0 A5AA88

double   maxPriceAmS;   // <0:A8<0;L=0O F5=0 70 A5AA8N

double   minPriceAmS;   // <8=8<0;L=0O F5=0 70 A5AA8N

bool     oneDayAmS;     // true -  A5AA8O =0E>48BAO 2 ?@545;0E >4=>3> 4=O



//--- B8E>>:50=A:0O A5AA8O

bool     remTimePaS;    // true - @07@5H5=> 70?><=8BL =>2K5 40BK A5AA88

datetime begTimePaS;    // 2@5<O =0G0;0 A5AA88

datetime endTimePaS;    // 2@5<O :>=F0 A5AA88

int      begHourPaS;    // G0A =0G0;0 A5AA88

int      endHourPaS;    // G0A :>=F0 A5AA88

double   maxPricePaS;   // <0:A8<0;L=0O F5=0 70 A5AA8N

double   minPricePaS;   // <8=8<0;L=0O F5=0 70 A5AA8N

bool     oneDayPaS;     // true -  A5AA8O =0E>48BAO 2 ?@545;0E >4=>3> 4=O



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

int OnInit(void)

{

   //--- ?@>25@8< @07=8FC 2> 2@5<5=8

   bool within = false;

   for(int i = -12; i <= 12; i++) if(i == diffInHours) within = true;

   if(!within)

   {

      Alert(" 07=8F0 2> 2@5<5=8 GMT 8 A5@25@=>3> 2@5<5=8 1@>:5@0 =5 4>;6=0 1KBL 70 ?@545;0<8 -12 ... 12");

      return INIT_FAILED;

   }

   if(Period() > PERIOD_H1)

   {

      Alert(";O 287C0;L=>3> >B>1@065=8O B>@3>2KE A5AA89, B09<D@5< 4>;65= 1KBL =5 1>;LH5 1 G0A0");

      return INIT_FAILED;

   }       

   chartID = ChartID();

   

   remTimeAsS = remTimeEuS = remTimeAmS = remTimePaS = true;

   minPriceAsS = minPriceEuS = minPriceAmS = minPricePaS = 999999999;

   

   begHourAsS  = 23+diffInHours;

   endHourAsS  = 09+diffInHours;

   

   if(begHourAsS > 23) begHourAsS = begHourAsS-24;

   if(endHourAsS < 0)  endHourAsS = 24+endHourAsS;

   

   if(begHourAsS < endHourAsS) oneDayAsS = true;

   

   begHourEuS  = 06+diffInHours;

   endHourEuS  = 16+diffInHours;

   

   if(begHourEuS < 0)  begHourEuS = 24+begHourEuS;

   if(endHourEuS > 23) endHourEuS = endHourEuS-24;

   

   if(begHourEuS < endHourEuS) oneDayEuS = true;

   

   begHourAmS  = 13+diffInHours;

   endHourAmS  = 23+diffInHours;

   

   if(begHourAmS > 23) begHourAmS = begHourAmS-24;

   if(endHourAmS > 23) endHourAmS = endHourAmS-24;

   

   if(begHourAmS < endHourAmS) oneDayAmS = true;

   

   begHourPaS  = 20+diffInHours;

   endHourPaS  = 07+diffInHours;

   

   if(begHourPaS > 23) begHourPaS = begHourPaS-24;

   if(endHourPaS < 0)  endHourPaS = 24+endHourPaS;

   

   if(endHourPaS < endHourPaS) endHourPaS = true;

   

   return INIT_SUCCEEDED;

}

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

void OnDeinit(const int reason)

{

   ObjectsDeleteAll(chartID);

}

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

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[])

{

   if(prev_calculated == 0)

      for(int i = Bars-1; i > 0; i--)

      {

         string begDay = TimeToString(time[i], TIME_DATE);

         dayOfWeek = TimeDayOfWeek(StrToTime(begDay));

         

         if(showAsianSession    == YES) TradingSession("AsianSession ",    time[i], begDay, remTimeAsS, begTimeAsS, endTimeAsS, begHourAsS, endHourAsS, high[i], low[i], maxPriceAsS, minPriceAsS, oneDayAsS, colorAsianSession);

         if(showEuropeanSession == YES) TradingSession("EuropeanSession ", time[i], begDay, remTimeEuS, begTimeEuS, endTimeEuS, begHourEuS, endHourEuS, high[i], low[i], maxPriceEuS, minPriceEuS, oneDayEuS, colorEuropeanSession);

         if(showAmericanSession == YES) TradingSession("AmericanSession ", time[i], begDay, remTimeAmS, begTimeAmS, endTimeAmS, begHourAmS, endHourAmS, high[i], low[i], maxPriceAmS, minPriceAmS, oneDayAmS, colorAmericanSession);

         if(showPacificSession  == YES) TradingSession("PacificSession ",  time[i], begDay, remTimePaS, begTimePaS, endTimePaS, begHourPaS, endHourPaS, high[i], low[i], maxPricePaS, minPricePaS, oneDayPaS, colorPacificSession);

      }

   if(prev_calculated > 0)

   {

      //--- @01>B05< =0 =>2>9 A25G5

      if(newCandle == time[0]) return 0;

      newCandle = time[0];

      

      string begDay = TimeToString(TimeCurrent(), TIME_DATE);

      

         if(showAsianSession    == YES) TradingSession("AsianSession ",    TimeCurrent(), begDay, remTimeAsS, begTimeAsS, endTimeAsS, begHourAsS, endHourAsS, high[1], low[1], maxPriceAsS, minPriceAsS, oneDayAsS, colorAsianSession);

         if(showEuropeanSession == YES) TradingSession("EuropeanSession ", TimeCurrent(), begDay, remTimeEuS, begTimeEuS, endTimeEuS, begHourEuS, endHourEuS, high[1], low[1], maxPriceEuS, minPriceEuS, oneDayEuS, colorEuropeanSession);

         if(showAmericanSession == YES) TradingSession("AmericanSession ", TimeCurrent(), begDay, remTimeAmS, begTimeAmS, endTimeAmS, begHourAmS, endHourAmS, high[1], low[1], maxPriceAmS, minPriceAmS, oneDayAmS, colorAmericanSession);

         if(showPacificSession  == YES) TradingSession("PacificSession ",  TimeCurrent(), begDay, remTimePaS, begTimePaS, endTimePaS, begHourPaS, endHourPaS, high[1], low[1], maxPricePaS, minPricePaS, oneDayPaS, colorPacificSession);

   }

   return rates_total;

}

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

//| $C=:F8O @0AG5B0 B>@3>2>9 A5AA88                                                               |

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

void TradingSession(string name, datetime time, string begDay, bool &remTime, datetime &begTime, datetime &endTime, 

                     int begHour, int endHour, double high, double low, double &maxPrice, double &minPrice, bool oneDay, color clr)

{

   //--- B>@3>20O A5AA8O =0E>48BAO 2 ?@545;0E >4=>3> 4=O

   if(oneDay && remTime)

   {

      begTime = StringToTime(begDay+" "+(string)begHour+":00");

      endTime = StringToTime(begDay+" "+(string)endHour+":00");

      remTime = false;

   }

   //--- B>@3>20O A5AA8O =0E>48BAO 70 ?@545;0<8 >4=>3> 4=O

   if(!oneDay)

   {

      if(remTime && dayOfWeek != 5)

      {

         begTime = StringToTime(begDay+" "+(string)begHour+":00");

         endTime = StringToTime(begDay+" "+(string)endHour+":00")+86400;

         remTime = false;

      }

      if(remTime && dayOfWeek == 5)

      {

         begTime = StringToTime(begDay+" "+(string)begHour+":00");

         endTime = StringToTime(begDay+" "+(string)endHour+":00")+86400*3;

         remTime = false;

      }

   }

   //--- A>74048< D>= B>@3>2>9 A5AA88

   if(begTime <= time && endTime >= time)

   {

      if(maxPrice < high) maxPrice = high;

      if(minPrice > low)  minPrice = low;

      CreateBGSession(name+TimeToString(begTime), begTime, maxPrice, endTime, minPrice, clr);

   }

   //--- >1=C;8< 7=0G5=8O ?> >:>=G0=8N B>@3>2>9 A5AA88

   if(endTime < time)

   {

      remTime  = true;

      maxPrice = 0;

      minPrice = 999999999;

   }

}

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

//| $C=:F8O A>7405B D>= 4;O B>@3>2>9 c5AA88                                                       |

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

void CreateBGSession(string name, datetime time1, double price1, datetime time2, double price2, color clr)

{

   ObjectDelete(chartID, name);

   

   ObjectCreate    (chartID, name, OBJ_RECTANGLE, 0, time1, price1, time2, price2);

   ObjectSetInteger(chartID, name, OBJPROP_COLOR, clr);

   ObjectSetInteger(chartID, name, OBJPROP_BACK, true);

   ObjectSetInteger(chartID, name, OBJPROP_SELECTABLE, false);

   ObjectSetInteger(chartID, name, OBJPROP_SELECTED, false);

   ObjectSetInteger(chartID, name, OBJPROP_HIDDEN, true);

}

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

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---