VGridLine_Custom

Author: Copyright © 2018, Nikolay Kositsin
0 Views
0 Downloads
0 Favorites
VGridLine_Custom
ÿþ//+------------------------------------------------------------------+ 

//|                                             VGridLine_Custom.mq5 |

//|                               Copyright © 2018, Nikolay Kositsin | 

//|                              Khabarovsk,   farria@mail.redcom.ru | 

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

//---- 02B>@AB2> 8=48:0B>@0

#property copyright "Copyright © 2018, Nikolay Kositsin"

//---- AAK;:0 =0 A09B 02B>@0

#property link "farria@mail.redcom.ru" 

//---- =><5@ 25@A88 8=48:0B>@0

#property version   "1.10"

#property description "@5<5==0O :>>@48=0B=0O A5B:0. (03 A5B:8 - 1 45=L."

#property description "=48:0B>@ @01>B05B =0 2A5E B09<D@59<0E <5=LH5 4=52=>3>."

//---- :>;8G5AB2> 8=48:0B>@=KE 1CD5@>2 0

#property indicator_buffers 0 

//---- 8A?>;L7>20=> =>;L 3@0D8G5A:8E ?>AB@>5=89

#property indicator_plots   0

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

//|  0@0<5B@K >B@8A>2:8 8=48:0B>@0              |

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

//---- >B@8A>2:0 8=48:0B>@0 2 3;02=>< >:=5

#property indicator_chart_window 

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

//|  >1JO2;5=85 :>=AB0=B                         |

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

#define RESET 0 // :>=AB0=B0 4;O E@0=5=8O 7=0G5=8O, 2>72@0B :>B>@>3> OnCalculate() ?@8254QB : ?5@5AGQBC 8=48:0B>@0

#define DAILY_PERIOD_SECOND 86400 // >;8G5AB2> A5:C=4 2 =545;L=>< 4=52=>< ?5@8>45

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

//|  1JO2;5=85 ?5@5G8A;5=8O G0A>2 ACB>:         |

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

enum HOURS

  {

   ENUM_HOUR_0=0,   //0

   ENUM_HOUR_1,     //1

   ENUM_HOUR_2,     //2

   ENUM_HOUR_3,     //3

   ENUM_HOUR_4,     //4

   ENUM_HOUR_5,     //5

   ENUM_HOUR_6,     //6

   ENUM_HOUR_7,     //7

   ENUM_HOUR_8,     //8

   ENUM_HOUR_9,     //9

   ENUM_HOUR_10,     //10

   ENUM_HOUR_11,     //11   

   ENUM_HOUR_12,     //12

   ENUM_HOUR_13,     //13

   ENUM_HOUR_14,     //14

   ENUM_HOUR_15,     //15

   ENUM_HOUR_16,     //16

   ENUM_HOUR_17,     //17

   ENUM_HOUR_18,     //18

   ENUM_HOUR_19,     //19

   ENUM_HOUR_20,     //20

   ENUM_HOUR_21,     //21  

   ENUM_HOUR_22,     //22

   ENUM_HOUR_23      //23    

  };

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

//|  1JO2;5=85 ?5@5G8A;5=8O <8=CB G0A>2         |

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

enum MINUTS

  {

   ENUM_MINUT_0=0,   //0

   ENUM_MINUT_1,     //1

   ENUM_MINUT_2,     //2

   ENUM_MINUT_3,     //3

   ENUM_MINUT_4,     //4

   ENUM_MINUT_5,     //5

   ENUM_MINUT_6,     //6

   ENUM_MINUT_7,     //7

   ENUM_MINUT_8,     //8

   ENUM_MINUT_9,     //9

   ENUM_MINUT_10,     //10

   ENUM_MINUT_11,     //11   

   ENUM_MINUT_12,     //12

   ENUM_MINUT_13,     //13

   ENUM_MINUT_14,     //14

   ENUM_MINUT_15,     //15

   ENUM_MINUT_16,     //16

   ENUM_MINUT_17,     //17

   ENUM_MINUT_18,     //18

   ENUM_MINUT_19,     //19

   ENUM_MINUT_20,     //20

   ENUM_MINUT_21,     //21  

   ENUM_MINUT_22,     //22

   ENUM_MINUT_23,     //23

   ENUM_MINUT_24,     //24

   ENUM_MINUT_25,     //25

   ENUM_MINUT_26,     //26

   ENUM_MINUT_27,     //27

   ENUM_MINUT_28,     //28

   ENUM_MINUT_29,     //29

   ENUM_MINUT_30,     //30

   ENUM_MINUT_31,     //31  

   ENUM_MINUT_32,     //32

   ENUM_MINUT_33,     //33

   ENUM_MINUT_34,     //34

   ENUM_MINUT_35,     //35

   ENUM_MINUT_36,     //36

   ENUM_MINUT_37,     //37

   ENUM_MINUT_38,     //38

   ENUM_MINUT_39,     //39 

   ENUM_MINUT_40,     //40

   ENUM_MINUT_41,     //41  

   ENUM_MINUT_42,     //42

   ENUM_MINUT_43,     //43

   ENUM_MINUT_44,     //44

   ENUM_MINUT_45,     //45

   ENUM_MINUT_46,     //46

   ENUM_MINUT_47,     //47

   ENUM_MINUT_48,     //48

   ENUM_MINUT_49,     //49

   ENUM_MINUT_50,     //50

   ENUM_MINUT_51,     //51  

   ENUM_MINUT_52,     //52

   ENUM_MINUT_53,     //53

   ENUM_MINUT_54,     //54

   ENUM_MINUT_55,     //55

   ENUM_MINUT_56,     //56

   ENUM_MINUT_57,     //57

   ENUM_MINUT_58,     //58

   ENUM_MINUT_59      //59             

  };

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

//|  >1JO2;5=85 ?5@5G8A;5=89                     |

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

enum ENUM_WIDTH //"8? :>=AB0=BK

  {

   w_1 = 1,   //1

   w_2,       //2

   w_3,       //3

   w_4,       //4

   w_5        //5

  };

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

//| E>4=K5 ?0@0<5B@K 8=48:0B>@0                 |

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

input string LinesSirname="VLine_Grid_Custom_";     //=0720=85 ;8=88

input HOURS  LineHours=ENUM_HOUR_0;                 //@5<O CAB0=>2:8 ;8=88 ('0AK)

input MINUTS LineMinuts=ENUM_MINUT_0;               //@5<O CAB0=>2:8 ;8=88 (8=CBK)

input color Line_Color=clrMagenta;                  //F25B ;8=88

input ENUM_LINE_STYLE Line_Style=STYLE_DASHDOTDOT;  //AB8;L 87>1@065=8O ;8=88

input ENUM_WIDTH Line_Width=w_1;                    //B>;I8=0 ;8=88

input bool SetBackground=true;                      //D>=>2>5 >B>1@065=85 ;8=89

input uint LinesTotal=10;                           //:>;8G5AB2> ;8=89 =0 8AB>@88

input uint FutureTotal=1;                           //:>;8G5AB2> ;8=89 =0 ?CAB>9 8AB>@88 1C4CI53>

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

//---- >1JO2;5=85 48=0<8G5A:8E <0AA82>2, :>B>@K5 1C4CB 2 

// 40;L=59H5< 8A?>;L7>20=K 2 :0G5AB25 :>;LF52KE 1CD5@>2

uint Count[];

datetime VLineTime[];

//---- 1JO2;5=85 F5;KE ?5@5<5==KE =0G0;0 >BAGQB0 40==KE

uint LinesTotal_;

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

//|  ?@>25@:0 10@0 =0 <><5=B 2@5<5=8                                 |

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

bool CheckVLinePoint(datetime bartime1,datetime bartime0)

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

  {

//----

   MqlDateTime tm0,tm1;

   TimeToStruct(bartime0,tm0);

   TimeToStruct(bartime1,tm1);

//----

   if(tm0.hour==LineHours && tm0.min==LineMinuts) return(true);

//----

   if(!LineMinuts)

     {

      if(tm0.day_of_year==tm1.day_of_year) if(tm1.hour<LineHours && tm0.hour>LineHours) return(true);

      if(tm0.day_of_year!=tm1.day_of_year) if(tm0.hour>=LineHours) return(true);

     }

//----

   if(LineMinuts)

     {

      if(tm0.hour==LineHours && tm1.min<LineMinuts && tm0.min>=LineMinuts) return(true);

      if(tm0.day_of_year==tm1.day_of_year && tm1.hour<LineHours && tm0.hour>LineHours)  return(true);

      if(tm0.day_of_year!=tm1.day_of_year) if(tm0.hour>LineHours) return(true);

     }

//----

   return(false);

  }

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

//|  ?5@5AGQB ?>78F88 A0<>3> =>2>3> M;5<5=B0 2 <0AA825               |

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

void Recount_ArrayZeroPos

(

 int &CoArr[],// >72@0B ?> AAK;:5 =><5@0 B5:CI53> 7=0G5=8O F5=>2>3> @O40

 int Size // :>;8G5AB2> M;5<5=B>2 2 :>;LF52>< 1CD5@5

 )

// Recount_ArrayZeroPos(count, Length)

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

  {

//----

   int numb,Max1,Max2;

   static int count=1;



   Max2=Size;

   Max1=Max2-1;



   count--;

   if(count<0) count=Max1;



   for(int iii=0; iii<Max2; iii++)

     {

      numb=iii+count;

      if(numb>Max1) numb-=Max2;

      CoArr[iii]=numb;

     }

//----

  }

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

//|  5@58<5=>20=85 25@B8:0;L=>9 ;8=88                               |

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

bool RenameVline

(

 long     chart_id,      // 845=B8D8:0B>@ 3@0D8:0

 string   oldname,       // AB0@>5 8<O >1J5:B0

 string   newname        // =>2>5 8<O >1J5:B0

 )

//---- 

  {

//----

   if(ObjectFind(chart_id,oldname)>0)

     {

      ObjectSetString(chart_id,oldname,OBJPROP_NAME,newname);

      return(true);

     }

//----

   return(false);

  }

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

//|  !>740=85 25@B8:0;L=>9 ;8=88                                     |

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

void CreateVline

(

 long     chart_id,      // 845=B8D8:0B>@ 3@0D8:0

 string   name,          // 8<O >1J5:B0

 int      nwin,          // 8=45:A >:=0

 datetime time1,         // 2@5<O 25@B8:0;L=>3> C@>2=O

 color    Color,         // F25B ;8=88

 int      style,         // AB8;L ;8=88

 int      width,         // B>;I8=0 ;8=88

 bool     background,// D>=>2>5 >B>1@065=85 ;8=88

 string   text           // B5:AB

 )

//---- 

  {

//----

   ObjectCreate(chart_id,name,OBJ_VLINE,nwin,time1,999999999);

   ObjectSetInteger(chart_id,name,OBJPROP_COLOR,Color);

   ObjectSetInteger(chart_id,name,OBJPROP_STYLE,style);

   ObjectSetInteger(chart_id,name,OBJPROP_WIDTH,width);

   ObjectSetString(chart_id,name,OBJPROP_TEXT,text);

   ObjectSetInteger(chart_id,name,OBJPROP_BACK,background);

   ObjectSetInteger(chart_id,name,OBJPROP_RAY,true);

   ObjectSetString(chart_id,name,OBJPROP_TOOLTIP,"\n"); //70?@5B 2A?;K20NI59 ?>4A:07:8

   ObjectSetInteger(chart_id,name,OBJPROP_BACK,true); //>1J5:B =0 704=5< ?;0=5

//----

  }

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

//|  5@5CAB0=>2:0 25@B8:0;L=>9 ;8=88                                |

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

void SetVline

(

 long     chart_id,      // 845=B8D8:0B>@ 3@0D8:0

 string   name,          // 8<O >1J5:B0

 int      nwin,          // 8=45:A >:=0

 datetime time1,         // 2@5<O 25@B8:0;L=>3> C@>2=O

 color    Color,         // F25B ;8=88

 int      style,         // AB8;L ;8=88

 int      width,         // B>;I8=0 ;8=88

 bool     background,// D>=>2>5 >B>1@065=85 ;8=88

 string   text           // B5:AB

 )

//---- 

  {

//----

   if(ObjectFind(chart_id,name)==-1) CreateVline(chart_id,name,nwin,time1,Color,style,width,background,text);

   else

     {

      ObjectSetString(chart_id,name,OBJPROP_TEXT,text);

      ObjectMove(chart_id,name,0,time1,999999999);

     }

//----

  }

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

//| Custom indicator initialization function                         |

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

void OnInit()

  {

//---- ?@>25@:0 ?5@8>40 3@0D8:0 =0 4>AB0B>G=>ABL 4;O @0AGQB0

   if(Period()>PERIOD_H12) return;

   Deinit();

//---- =8F80;870F8O ?5@5<5==KE =0G0;0 >BAGQB0 40==KE

   LinesTotal_=LinesTotal+FutureTotal;



//----  0A?@545;5=85 ?0<OB8 ?>4 <0AA82K ?5@5<5==KE  

   ArrayResize(Count,LinesTotal_);

   ArrayResize(VLineTime,LinesTotal_);



//---- 8<O 4;O >:>= 40==KE 8 ;M910 4;O AC1J>:>= 

   string shortname;

   StringConcatenate(shortname,"VLine_Grid_Custom(",LinesSirname,", ",LinesTotal,")");

   IndicatorSetString(INDICATOR_SHORTNAME,shortname);

//----   

  }

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

//| Custom indicator deinitialization function                       |

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

void Deinit()

  {

//----

   ObjectsDeleteAll(0,LinesSirname,-1,OBJ_VLINE);

//----

   ChartRedraw(0);

  }

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

//| Custom indicator deinitialization function                       |

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

void OnDeinit(const int reason)

  {

//----

   Deinit();

//----

  }

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

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

  {

//---- ?@>25@:0 ?5@8>40 3@0D8:0 =0 4>AB0B>G=>ABL 4;O @0AGQB0

   if(Period()>PERIOD_H12) return(RESET);

//---- ?@>25@:0 :>;8G5AB20 10@>2 =0 4>AB0B>G=>ABL 4;O @0AGQB0

   if(rates_total<2) return(RESET);

   if(prev_calculated==rates_total) return(rates_total);



//---- >1JO2;5=8O ;>:0;L=KE ?5@5<5==KE 

   string Name;

   datetime Time;

   int limit,bar;

   static datetime LastTime=0;



//---- 8=45:A0F8O M;5<5=B>2 2 <0AA820E :0: 2 B09<A5@8OE 

   ArraySetAsSeries(time,true);



//---- @0AGQB AB0@B>2>3> =><5@0 limit 4;O F8:;0 ?5@5AGQB0 10@>2 8 AB0@B>20O 8=8F80;870F8O ?5@5<5==KE

   if(prev_calculated>rates_total || prev_calculated<=0)// ?@>25@:0 =0 ?5@2K9 AB0@B @0AGQB0 8=48:0B>@0

     {

      limit=rates_total-2; // AB0@B>2K9 =><5@ 4;O @0AGQB0 2A5E 10@>2

      uint VLineCount=0;

      ArrayInitialize(Count,NULL);

      for(bar=0; bar<=limit && !IsStopped(); bar++)

         if(CheckVLinePoint(time[bar+1],time[bar]))

           {

            VLineCount++;

            if(VLineCount>=LinesTotal) break;

           }



      if(bar<limit) limit=bar;

     }

   else limit=rates_total-prev_calculated; // AB0@B>2K9 =><5@ 4;O @0AGQB0 =>2KE 10@>2



//---- F8:; @0AGQB0 8=48:0B>@0 =0 B5:CI8E 10@0E

   for(bar=limit; bar>=0 && !IsStopped(); bar--)

     {

      if(CheckVLinePoint(time[bar+1],time[bar]))

        {

         Name=LinesSirname+TimeToString(time[bar]);

         RenameVline(0,LinesSirname+TimeToString(VLineTime[Count[FutureTotal]]),Name);

         SetVline(0,Name,0,time[bar],Line_Color,Line_Style,Line_Width,SetBackground,"4=52=>9 10@");

         VLineTime[Count[FutureTotal]]=time[bar];

         if(bar>0) Recount_ArrayZeroPos(Count,LinesTotal_);

         LastTime=time[bar];

        }

     }



//---- F8:; @0AGQB0 8=48:0B>@0 =0 1C4CI5< 

   for(int numb=int(FutureTotal)-1; numb>=0 && !IsStopped(); numb--)

     {

      Time=LastTime+DAILY_PERIOD_SECOND*(FutureTotal-numb);

      Name=LinesSirname+TimeToString(Time);

      RenameVline(0,LinesSirname+TimeToString(VLineTime[Count[numb]]),Name);

      SetVline(0,Name,0,Time,Line_Color,Line_Style,Line_Width,SetBackground,"4=52=>9 10@");

      VLineTime[Count[numb]]=Time;

     }

//---- 

   ChartRedraw(0);

   return(rates_total);

  }

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

Comments