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

//|                                                   ShadeOpenX.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 0

#property indicator_buffers 0

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

#property indicator_plots   0

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

//|  >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 "ShadeOpenX"                    // >=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

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

//|  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 string SirName=INDICATOR_NAME; //8<O 3@0D8G5A:8E >1J5:B>2]

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 color Upbody=clrLimeGreen;     //F25B B5;0 A25G8  >AB

input color Dnbody=clrDarkOrange;    //F25B B5;0 A25G8 045=85

input color Uphigh=clrPaleGreen;     //F25B 25@E=59 B5=8  >AB

input color Dnhigh=clrYellow;        //F25B 25@E=59 B5=8 045=85

input color Uplow=clrPaleGreen;     //F25B =86=59 B5=8  >AB

input color Dnlow=clrYellow;        //F25B =86=59 B5=8 045=85

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

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

int min_rates_total;

//--- >1JO2;5=85 AB@8=3>2KE 8<Q= 3@0D8G5A:8E >1J5:B>2

string candlebody,candlehigh,candlelow;

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

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

   candlebody=SirName+"_candlebody";

   candlehigh=SirName+"_candlehigh";

   candlelow=SirName+"_candlelow";

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

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

void OnDeinit(const int reason)

  {

//---- 

   ObjectDelete(0,candlebody);

   ObjectDelete(0,candlehigh);

   ObjectDelete(0,candlelow);

//----

   ChartRedraw(NULL);

  }

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

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



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



//---- ?>8A: =0G0;0 4=O

   datetime iTime[1];

   if(CopyTime(Symbol(),PERIOD_D1,time[0],1,iTime)<=0) return(RESET);

   limit=0;

   for(int bar=0; bar<rates_total-1 && !IsStopped(); bar++)

     {

      if(time[bar]<iTime[0]) break;

      limit++;

     }



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

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

     {

      if(!ShadeOpenCheck(time[bar]))

        {



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

           {

            SetRectangle(0,candlebody,0,Start,iOpen[0],End,iClose[0],Upbody,0,0,candlebody);

            SetRectangle(0,candlehigh,0,Start,iClose[0],End,ihigh,Uphigh,0,0,candlehigh);

            SetRectangle(0,candlelow,0,Start,iOpen[0],End,ilow,Uplow,0,0,candlelow);

           }

         else

           {

            SetRectangle(0,candlebody,0,Start,iOpen[0],End,iClose[0],Dnbody,0,0,candlebody);

            SetRectangle(0,candlehigh,0,Start,iOpen[0],End,ihigh,Dnhigh,0,0,candlehigh);

            SetRectangle(0,candlelow,0,Start,iClose[0],End,ilow,Dnlow,0,0,candlelow);

           }

        }

     }

//----  

   ChartRedraw(NULL);

   return(rates_total);

  }

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

//|  !>740=85 ?@O<>C3>;L=8:0                                         |

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

void CreateRectangle

(

 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 1 F5=>2>3> C@>2=O

 double   price1,        // 1 F5=>2>9 C@>25=L

 datetime time2,         // 2@5<O 2 F5=>2>3> C@>2=O

 double   price2,        // 2 F5=>2>9 C@>25=L

 color    Color,         // F25B ;8=88

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

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

 string   text           // B5:AB

 )

//---- 

  {

//----

   ObjectCreate(chart_id,name,OBJ_RECTANGLE,nwin,time1,price1,time2,price2);

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

   ObjectSetInteger(chart_id,name,OBJPROP_FILL,true);

//----

  }

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

//|  5@5CAB0=>2:0 ?@O<>C3>;L=8:0                                    |

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

void SetRectangle

(

 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 1 F5=>2>3> C@>2=O

 double   price1,        // 1 F5=>2>9 C@>25=L

 datetime time2,         // 2@5<O 2 F5=>2>3> C@>2=O

 double   price2,        // 2 F5=>2>9 C@>25=L

 color    Color,         // F25B ;8=88

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

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

 string   text           // B5:AB

 )

//---- 

  {

//----

   if(ObjectFind(chart_id,name)==-1) CreateRectangle(chart_id,name,nwin,time1,price1,time2,price2,Color,style,width,text);

   else

     {

      ObjectSetString(chart_id,name,OBJPROP_TEXT,text);

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

      ObjectMove(chart_id,name,1,time2,price2);

      ObjectSetInteger(chart_id,name,OBJPROP_COLOR,Color);

     }

//----

  }

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

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