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

//|                                                    ShadeOpen.mq5 | 

//|                               Copyright © 2017, Nikolay Kositsin | 

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

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

#property copyright "Copyright © 2017, Nikolay Kositsin"

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

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

#property version   "1.60"

#property description "=48:0B>@ =>G=KE AA5A89 2 2845 A25G5:."

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

#property indicator_chart_window

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

#property indicator_buffers 6

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

#property indicator_plots   3

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

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

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

#define RESET 0                                        // >=AB0=B0 4;O 2>72@0B0 B5@<8=0;C :><0=4K =0 ?5@5AG5B 8=48:0B>@0

#define INDICATOR_NAME "ShadeOpen"                     // >=AB0=B0 4;O 8<5=8 8=48:0B>@0

#define EMPTYVALUE 0                                   // >=AB0=B0 4;O =5>B>1@0605<KE 7=0G5=89 8=48:0B>@0

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

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

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

//--- >B@8A>2:0 8=48:0B>@0 2 2845 F25B=>3> >1;0:0

#property indicator_type1   DRAW_FILLING

//--- 2 :0G5AB25 F25B0 8=48:0B>@0 8A?>;L7>20=K

#property indicator_color1  clrPaleGreen,clrYellow

//--- ;8=8O 8=48:0B>@0 - =5?@5@K2=0O :@820O

#property indicator_style1  STYLE_SOLID

//--- >B>1@065=85 <5B:8 8=48:0B>@0

#property indicator_label1  "Upper Shade"

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

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

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

//--- >B@8A>2:0 8=48:0B>@0 2 2845 F25B=>3> >1;0:0

#property indicator_type2   DRAW_FILLING

//--- 2 :0G5AB25 F25B0 8=48:0B>@0 8A?>;L7>20=K

#property indicator_color2  clrLimeGreen,clrDarkOrange

//--- ;8=8O 8=48:0B>@0 - =5?@5@K2=0O :@820O

#property indicator_style2  STYLE_SOLID

//--- >B>1@065=85 <5B:8 8=48:0B>@0

#property indicator_label2  "Body"

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

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

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

//--- >B@8A>2:0 8=48:0B>@0 2 2845 F25B=>3> >1;0:0

#property indicator_type3   DRAW_FILLING

//--- 2 :0G5AB25 F25B0 8=48:0B>@0 8A?>;L7>20=

#property indicator_color3  clrPaleGreen,clrYellow

//--- ;8=8O 8=48:0B>@0 - =5?@5@K2=0O :@820O

#property indicator_style3  STYLE_SOLID

//--- >B>1@065=85 <5B:8 8=48:0B>@0

#property indicator_label3  "Lower Shade"

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

//|  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             

  };

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

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

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

input HOURS  StartH=ENUM_HOUR_6;   //!B0@B B>@3>2;8 ('0AK)

input MINUTS StartM=ENUM_MINUT_30; //!B0@B B>@3>2;8 (8=CBK)

input HOURS  EndH=ENUM_HOUR_13;    //:>=G0=85 B>@3>2;8 ('0AK)

input MINUTS EndM=ENUM_MINUT_5;    //:>=G0=85 B>@3>2;8 (8=CBK) 

input int    Shift=0;              //!4283 8=48:0B>@0 ?> 3>@87>=B0;8 2 10@0E

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

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

//--- 1C4CB 8A?>;L7>20=K 2 :0G5AB25 8=48:0B>@=KE 1CD5@>2

double ExtA1Buffer[];

double ExtB1Buffer[];

double ExtA2Buffer[];

double ExtB2Buffer[];

double ExtA3Buffer[];

double ExtB3Buffer[];

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

int min_rates_total;

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

//| Custom indicator initialization function                         | 

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

int OnInit()

  {

//--- ?@>25@:0 2E>4=KE ?5@5<5==KE 

   if(StartH>EndH || (StartH==EndH && StartM>=EndM))

     {

      Print("525@=K5 7=0G5=8O 2E>4=KE ?5@5<5==KE AB0@B0 8 >:>=G0=8O B>@3>2;8!");

      return(INIT_PARAMETERS_INCORRECT);

     }



//--- 8=8F80;870F8O ?5@5<5==KE 

   min_rates_total=2;

//--- ?@52@0I5=85 48=0<8G5A:8E <0AA82>2 2 8=48:0B>@=K5 1CD5@K

   ArrayInit(0,ExtA1Buffer);

   ArrayInit(1,ExtB1Buffer);

   ArrayInit(2,ExtA2Buffer);

   ArrayInit(3,ExtB2Buffer);

   ArrayInit(4,ExtA3Buffer);

   ArrayInit(5,ExtB3Buffer);

//--- 8=8F80;870F8O 8=48:0B>@>2

   PlotInit(0,2,Shift);

   PlotInit(1,2,Shift);

   PlotInit(2,2,Shift);

//--- A>740=85 8<5=8 4;O >B>1@065=8O 2 >B45;L=>< ?>4>:=5 8 2> 2A?;K20NI59 ?>4A:07:5

   IndicatorSetString(INDICATOR_SHORTNAME,INDICATOR_NAME);

//--- >?@545;5=85 B>G=>AB8 >B>1@065=8O 7=0G5=89 8=48:0B>@0

   IndicatorSetInteger(INDICATOR_DIGITS,_Digits);

//--- 7025@H5=85 8=8F80;870F88

   return(INIT_SUCCEEDED);

  }

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

//| Custom indicator iteration function                              |

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

int OnCalculate(

                const int rates_total,    // :>;8G5AB2> 8AB>@88 2 10@0E =0 B5:CI5< B8:5

                const int prev_calculated,// :>;8G5AB2> 8AB>@88 2 10@0E =0 ?@54K4CI5< B8:5

                const datetime &time[],

                const double &open[],

                const double& high[],     // F5=>2>9 <0AA82 <0:A8<C<>2 F5=K 4;O @0AGQB0 8=48:0B>@0

                const double& low[],      // F5=>2>9 <0AA82 <8=8<C<>2 F5=K  4;O @0AGQB0 8=48:0B>@0

                const double &close[],

                const long &tick_volume[],

                const long &volume[],

                const int &spread[]

                )

  {

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

   if(rates_total<min_rates_total) return(RESET);



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

   int limit;

   static int lshift=NULL;

   double;



//---- @0AGQB AB0@B>2>3> =><5@0 first 4;O F8:;0 ?5@5AGQB0 10@>2

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

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

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



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

   ArraySetAsSeries(open,true);

   ArraySetAsSeries(high,true);

   ArraySetAsSeries(low,true);

   ArraySetAsSeries(close,true);

   ArraySetAsSeries(time,true);



//---- >A=>2=>9 F8:; @0AGQB0 8=48:0B>@0

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

     {

      if(!ShadeOpenCheck(time[bar]))

        {

         ExtA1Buffer[bar]=EMPTYVALUE;

         ExtB1Buffer[bar]=EMPTYVALUE;

         ExtA2Buffer[bar]=EMPTYVALUE;

         ExtB2Buffer[bar]=EMPTYVALUE;

         ExtA3Buffer[bar]=EMPTYVALUE;

         ExtB3Buffer[bar]=EMPTYVALUE;

         lshift=NULL;

         continue;

        }

      else

        {

         datetime Start=GetShadeOpenStart(time[bar]);

         datetime End=MathMin(GetShadeOpenEnd(time[bar]),time[0]);

         double iOpen[1],iClose[1],iHigh[],iLow[];

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

         ArraySetAsSeries(iHigh,true);

         ArraySetAsSeries(iLow,true);

         //----

         if(CopyOpen(Symbol(),PERIOD_CURRENT,Start,1,iOpen)<=0) return(RESET);

         if(CopyHigh(Symbol(),PERIOD_CURRENT,Start,End,iHigh)<=0) return(RESET);

         if(CopyLow(Symbol(),PERIOD_CURRENT,Start,End,iLow)<=0) return(RESET);

         if(CopyClose(Symbol(),PERIOD_CURRENT,End,1,iClose)<=0) return(RESET);

         //----

         double ihigh=iHigh[ArrayMaximum(iHigh,0,WHOLE_ARRAY)];

         double ilow=iLow[ArrayMinimum(iLow,0,WHOLE_ARRAY)];

         //----

         if(iClose[0]>=iOpen[0])

           {

            ExtA1Buffer[bar]=ihigh;

            ExtB1Buffer[bar]=iClose[0];

            ExtA2Buffer[bar]=iClose[0];

            ExtB2Buffer[bar]=iOpen[0];

            ExtA3Buffer[bar]=iOpen[0];

            ExtB3Buffer[bar]=ilow;

           }

         else

           {

               ExtB1Buffer[bar]=ihigh;

               ExtA1Buffer[bar]=iClose[0];

               ExtA2Buffer[bar]=iClose[0];

               ExtB2Buffer[bar]=iOpen[0];

               ExtB3Buffer[bar]=iOpen[0];

               ExtA3Buffer[bar]=ilow;

           }

         lshift=bar;

        }

     }

//----     

   return(rates_total);

  }

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

//| ?@545;5=85 A83=0;>2 4;O B>@3>2;8 2 ?5@8>45                      |

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

bool ShadeOpenCheck(datetime ChkTime)

  {

//---- 1JO2;5=85 ?5@5<5==>9 4;O @07@5H5=8O B>@3>2;8

   bool Trade=false;

   MqlDateTime tm;

   TimeToStruct(ChkTime,tm);



   if(StartH<EndH)

     {

      if(tm.hour==StartH && tm.min>=StartM) Trade=true;

      if(tm.hour>StartH && tm.hour<EndH) Trade=true;

      if(tm.hour>StartH && tm.hour==EndH && tm.min<EndM) Trade=true;

     }

   if(StartH==EndH)

     {

      if(tm.hour==StartH && tm.min>=StartM && tm.min<EndM) Trade=true;

     }

//---- 

   return(Trade);

  }

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

//| ?@545;5=85 =0G0;0 4;O B>@3>2;8 2 ?5@8>45                        |

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

datetime GetShadeOpenStart(datetime ChkTime)

  {

//---- 

   datetime Start,iTime[1];

   if(CopyTime(Symbol(),PERIOD_D1,ChkTime,1,iTime)<=0) return(RESET);

   Start=iTime[0]+StartH*60*60+StartM*60;

//---- 

   return(Start);

  }

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

//| ?@545;5=85 :>=F0 4;O B>@3>2;8 2 ?5@8>45                         |

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

datetime GetShadeOpenEnd(datetime ChkTime)

  {

//---- 1JO2;5=85

   datetime End,iTime[1];

   if(CopyTime(Symbol(),PERIOD_D1,ChkTime,1,iTime)<=0) return(RESET);

   End=iTime[0]+EndH*60*60+EndM*60;

//---- 

   return(End);

  }

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

//| PlotInit()                                                       |

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

void PlotInit(uint PlotNumber,

              int DrawBegin,

              int PlotShift)

  {

//--- >ACI5AB2;5=85 A42830 =0G0;0 >BAG5B0 >B@8A>2:8 8=48:0B>@0

   PlotIndexSetInteger(PlotNumber,PLOT_DRAW_BEGIN,DrawBegin);

//--- >ACI5AB2;5=85 A42830 8=48:0B>@0 1 ?> 3>@87>=B0;8 =0 Shift

   PlotIndexSetInteger(PlotNumber,PLOT_SHIFT,PlotShift);

//---

  }

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

//| ArrayInit()                                                      |

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

void ArrayInit(uint ArrNumber,

               double &Array[])

  {

//--- ?@52@0I5=85 48=0<8G5A:>3> <0AA820 2 8=48:0B>@=K9 1CD5@

   SetIndexBuffer(ArrNumber,Array,INDICATOR_DATA);

//--- 8=45:A0F8O M;5<5=B>2 2 1CD5@5 :0: 2 B09<A5@88

   ArraySetAsSeries(Array,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 ---