Author: Yuriy Tokman (YTG)
0 Views
0 Downloads
0 Favorites
OneSession
ÿþ//+------------------------------------------------------------------+

//|                                                   OneSession.mq4 |

//|                                               Yuriy Tokman (YTG) |

//|                       https://www.mql5.com/ru/users/satop/seller |

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

#property copyright "Yuriy Tokman (YTG)"

#property link      "https://www.mql5.com/ru/users/satop/seller"

#property version   "1.00"

#property strict

#property indicator_chart_window



input int    NumberOfDays = 30;

input string TimeBegin    = "10:00";

input string TimeEnd      = "18:00";

input color  Color    = clrRed;

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- indicator buffers mapping

   DeleteObjects();

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

      CreateObjects("AS"+DoubleToString(i,0), Color);

   Comment("");

//---

   return(INIT_SUCCEEDED);

  }

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

//| Custor indicator deinitialization function                       |

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

void deinit()

  {

   DeleteObjects();

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

  {

//---

   datetime dt=CurTime();



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

     {

      DrawObjects(dt, "AS"+DoubleToString(i,0), TimeBegin, TimeEnd);

      dt=decDateTradeDay(dt);

      while(TimeDayOfWeek(dt)>5)

         dt=decDateTradeDay(dt);

     }

//--- return value of prev_calculated for next call

   return(rates_total);

  }

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

bool CreateObjects(

   const string          name="Rectangle",  // 8<O ?@O<>C3>;L=8:0

   const color           clr=clrRed,        // F25B ?@O<>C3>;L=8:0

   const long            chart_ID=0,        // ID 3@0D8:0

   const int             sub_window=0,      // =><5@ ?>4>:=0

   datetime              time1=0,           // 2@5<O ?5@2>9 B>G:8

   double                price1=0,          // F5=0 ?5@2>9 B>G:8

   datetime              time2=0,           // 2@5<O 2B>@>9 B>G:8

   double                price2=0,          // F5=0 2B>@>9 B>G:8

   const ENUM_LINE_STYLE style=STYLE_SOLID, // AB8;L ;8=89 ?@O<>C3>;L=8:0

   const int             width=2,           // B>;I8=0 ;8=89 ?@O<>C3>;L=8:0

   const bool            fill=false,        // 70;82:0 ?@O<>C3>;L=8:0 F25B><

   const bool            back=false,        // =0 704=5< ?;0=5

   const bool            selection=false,    // 2K45;8BL 4;O ?5@5<5I5=89

   const bool            hidden=true,       // A:@KB 2 A?8A:5 >1J5:B>2

   const long            z_order=0)         // ?@8>@8B5B =0 =060B85 <KHLN

  {

//--- A1@>A8< 7=0G5=85 >H81:8

   ResetLastError();

//--- A>74048< ?@O<>C3>;L=8: ?> 7040==K< :>>@48=0B0<

   if(!ObjectCreate(chart_ID,name,OBJ_RECTANGLE,sub_window,time1,price1,time2,price2))

     {

      Print(__FUNCTION__,

            ": =5 C40;>AL A>740BL ?@O<>C3>;L=8:! >4 >H81:8 = ",GetLastError());

      return(false);

     }

//--- CAB0=>28< F25B ?@O<>C3>;L=8:0

   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);

//--- CAB0=>28< AB8;L ;8=89 ?@O<>C3>;L=8:0

   ObjectSetInteger(chart_ID,name,OBJPROP_STYLE,style);

//--- CAB0=>28< B>;I8=C ;8=89 ?@O<>C3>;L=8:0

   ObjectSetInteger(chart_ID,name,OBJPROP_WIDTH,width);

//--- 2:;NG8< (true) 8;8 >B:;NG8< (false) @568< 70;82:8 ?@O<>C3>;L=8:0

   ObjectSetInteger(chart_ID,name,OBJPROP_FILL,fill);

//--- >B>1@078< =0 ?5@54=5< (false) 8;8 704=5< (true) ?;0=5

   ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);

//--- 2:;NG8< (true) 8;8 >B:;NG8< (false) @568< 2K45;5=8O ?@O<>C3>;L=8:0 4;O ?5@5<5I5=89

//--- ?@8 A>740=88 3@0D8G5A:>3> >1J5:B0 DC=:F859 ObjectCreate, ?> C<>;G0=8N >1J5:B

//--- =5;L7O 2K45;8BL 8 ?5@5<5I0BL. =CB@8 65 MB>3> <5B>40 ?0@0<5B@ selection

//--- ?> C<>;G0=8N @025= true, GB> ?>72>;O5B 2K45;OBL 8 ?5@5<5I0BL MB>B >1J5:B

   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection);

   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection);

//--- A:@>5< (true) 8;8 >B>1@078< (false) 8<O 3@0D8G5A:>3> >1J5:B0 2 A?8A:5 >1J5:B>2

   ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden);

//--- CAB0=>28< ?@8>@8B5B =0 ?>;CG5=85 A>1KB8O =060B8O <KH8 =0 3@0D8:5

   ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order);

//--- CA?5H=>5 2K?>;=5=85

   return(true);

  }

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

void DeleteObjects()

  {

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

      ObjectDelete("AS"+DoubleToString(i,0));

  }

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

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);



   if(!TimeDayOfWeek(t1))

      return;



   b1=iBarShift(NULL, 0, t1);

   b2=iBarShift(NULL, 0, t2);

   p1=Open[b1];//High[Highest(NULL, 0, MODE_HIGH, b1-b2, b2)];

   p2=Close[b2];//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);

  }

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

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(DoubleToString(ty,0)+"."+DoubleToString(tm,0)+"."+DoubleToString(td,0)+" "+DoubleToString(th,0)+":"+DoubleToString(ti,0)));

  }

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

  

Comments