Hans_Indicator_x4

Author: Copyright © 2014, Shimodax
0 Views
0 Downloads
0 Favorites
Hans_Indicator_x4
ÿþ//+------------------------------------------------------------------+ 

//|                                            Hans_Indicator_x4.mq5 | 

//|                                       Copyright © 2014, Shimodax | 

//|   http://www.strategybuilderfx.com/forums/showthread.php?t=15439 | 

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

/* Introduction:



   Draw ranges for "Simple Combined Breakout System for EUR/USD and GBP/USD" thread

   (see http://www.strategybuilderfx.com/forums/showthread.php?t=15439)



   LocalTimeZone: TimeZone for which MT5 shows your local time, 

                  e.g. 1 or 2 for Europe (GMT+1 or GMT+2 (daylight 

                  savings time).  Use zero for no adjustment.

                  

                  The MetaQuotes demo server uses GMT +2.   

   Enjoy  :-)

   

   Markus



*/

#property copyright "Copyright © 2014, Shimodax"

#property link "http://www.strategybuilderfx.com/forums/showthread.php?t=15439"

#property description "=48:0B>@ @0AH8@ONI8EAO :>@84>@>2 2@5<5==KE 7>= A 42C<O :>@84>@0<8. !D>@<8@>20==K9 :>@84>@ @025= G5BK@Q< G0A0<, @0AH8@5=8O :>@84>@0 - H5AB=04F0BL G0A>2."

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

#property version   "1.02"

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

#property indicator_chart_window  

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

#property indicator_buffers 6 

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

#property indicator_plots   6

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

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

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

#define RESET  0 // >=AB0=B0 4;O 2>72@0B0 B5@<8=0;C :><0=4K =0 ?5@5AGQB 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 ;8=88

#property indicator_type1   DRAW_LINE

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

#property indicator_color1 clrBlue

//---- ;8=8O 8=48:0B>@0 - A?;>H=0O

#property indicator_style1  STYLE_SOLID

//---- B>;I8=0 ;8=88 8=48:0B>@0 @02=0 1

#property indicator_width1  1

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

#property indicator_label1  "Upper Hans_Indicator 1"

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

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

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

//---- >B@8A>2:0 8=48:0B>@0 2 2845 ;8=88

#property indicator_type2   DRAW_LINE

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

#property indicator_color2 clrMagenta

//---- ;8=8O 8=48:0B>@0 - A?;>H=0O

#property indicator_style2  STYLE_SOLID

//---- B>;I8=0 ;8=88 8=48:0B>@0 @02=0 1

#property indicator_width2  1

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

#property indicator_label2  "Lower Hans_Indicator 1"

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

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

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

//---- >B@8A>2:0 8=48:0B>@0 2 2845 ;8=88

#property indicator_type3   DRAW_LINE

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

#property indicator_color3 clrLime

//---- ;8=8O 8=48:0B>@0 - A?;>H=0O

#property indicator_style3  STYLE_SOLID

//---- B>;I8=0 ;8=88 8=48:0B>@0 @02=0 3

#property indicator_width3 3

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

#property indicator_label3  "Upper Hans_Indicator 2"

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

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

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

//---- >B@8A>2:0 8=48:0B>@0 2 2845 ;8=88

#property indicator_type4   DRAW_LINE

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

#property indicator_color4 clrRed

//---- ;8=8O 8=48:0B>@0 - A?;>H=0O

#property indicator_style4  STYLE_SOLID

//---- B>;I8=0 ;8=88 8=48:0B>@0 @02=0 3

#property indicator_width4  3

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

#property indicator_label4  "Lower Hans_Indicator 2"

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

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

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

//---- >B@8A>2:0 8=48:0B>@0 2 2845 ;8=88

#property indicator_type5   DRAW_LINE

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

#property indicator_color5 clrGreen

//---- ;8=8O 8=48:0B>@0 - A?;>H=0O

#property indicator_style5  STYLE_SOLID

//---- B>;I8=0 ;8=88 8=48:0B>@0 @02=0 3

#property indicator_width5 3

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

#property indicator_label5  "Upper Hans_Indicator 3"

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

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

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

//---- >B@8A>2:0 8=48:0B>@0 2 2845 ;8=88

#property indicator_type6   DRAW_LINE

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

#property indicator_color6 clrIndigo

//---- ;8=8O 8=48:0B>@0 - A?;>H=0O

#property indicator_style6  STYLE_SOLID

//---- B>;I8=0 ;8=88 8=48:0B>@0 @02=0 3

#property indicator_width6  3

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

#property indicator_label6  "Lower Hans_Indicator 3"

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

//|  %+  " + "            |

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

input uint LocalTimeZone=0;         // G0A =0G0;0 >BAGQB0 8AE>4=>3> :>@84>@0

input uint DestTimeZone=4;          // A4283 :>@84>@0 2;52> 2 10@0E

input uint PipsForEntry1=100;       // @0AH8@5=85 3@0=8F ?5@2>3> AD>@<8@>20==>3> :>@84>@0 2 ?C=:B0E

input uint PipsForEntry2=200;       // @0AH8@5=85 3@0=8F 2B>@>3> AD>@<8@>20==>3> :>@84>@0 2 ?C=:B0E

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

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



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

int  min_rates_total;

//---- >1JO2;5=85 48=0<8G5A:8E <0AA82>2, :>B>@K5 1C4CB 2 40;L=59H5< 8A?>;L7>20=K 2 :0G5AB25 8=48:0B>@=KE 1CD5@>2

double Zone1Upper[],Zone2Upper[],Zone3Upper[];

double Zone1Lower[],Zone2Lower[],Zone3Lower[];

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

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

   min_rates_total=100;



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

   SetIndexBuffer(0,Zone1Upper,INDICATOR_DATA);

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

   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,min_rates_total);

//---- CAB0=>2:0 7=0G5=89 8=48:0B>@0, :>B>@K5 =5 1C4CB 2848<K =0 3@0D8:5

   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,NULL);

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

   PlotIndexSetInteger(0,PLOT_SHIFT,Shift);

//---- 70?@5B =0 >B>1@065=85 7=0G5=89 8=48:0B>@0 2 ;52>< 25@E=5< C3;C >:=0 8=48:0B>@0

   PlotIndexSetInteger(0,PLOT_SHOW_DATA,false);

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

   ArraySetAsSeries(Zone1Upper,true);



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

   SetIndexBuffer(1,Zone1Lower,INDICATOR_DATA);

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

   PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,min_rates_total);

//---- CAB0=>2:0 7=0G5=89 8=48:0B>@0, :>B>@K5 =5 1C4CB 2848<K =0 3@0D8:5

   PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,NULL);

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

   PlotIndexSetInteger(1,PLOT_SHIFT,Shift);

//---- 70?@5B =0 >B>1@065=85 7=0G5=89 8=48:0B>@0 2 ;52>< 25@E=5< C3;C >:=0 8=48:0B>@0

   PlotIndexSetInteger(1,PLOT_SHOW_DATA,false);

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

   ArraySetAsSeries(Zone1Lower,true);



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

   SetIndexBuffer(2,Zone2Upper,INDICATOR_DATA);

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

   PlotIndexSetInteger(2,PLOT_DRAW_BEGIN,min_rates_total);

//---- CAB0=>2:0 7=0G5=89 8=48:0B>@0, :>B>@K5 =5 1C4CB 2848<K =0 3@0D8:5

   PlotIndexSetDouble(2,PLOT_EMPTY_VALUE,NULL);

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

   PlotIndexSetInteger(2,PLOT_SHIFT,Shift);

//---- 70?@5B =0 >B>1@065=85 7=0G5=89 8=48:0B>@0 2 ;52>< 25@E=5< C3;C >:=0 8=48:0B>@0

   PlotIndexSetInteger(2,PLOT_SHOW_DATA,false);

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

   ArraySetAsSeries(Zone2Upper,true);



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

   SetIndexBuffer(3,Zone2Lower,INDICATOR_DATA);

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

   PlotIndexSetInteger(3,PLOT_DRAW_BEGIN,min_rates_total);

//---- CAB0=>2:0 7=0G5=89 8=48:0B>@0, :>B>@K5 =5 1C4CB 2848<K =0 3@0D8:5

   PlotIndexSetDouble(3,PLOT_EMPTY_VALUE,NULL);

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

   PlotIndexSetInteger(3,PLOT_SHIFT,Shift);

//---- 70?@5B =0 >B>1@065=85 7=0G5=89 8=48:0B>@0 2 ;52>< 25@E=5< C3;C >:=0 8=48:0B>@0

   PlotIndexSetInteger(3,PLOT_SHOW_DATA,false);

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

   ArraySetAsSeries(Zone2Lower,true);



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

   SetIndexBuffer(4,Zone3Upper,INDICATOR_DATA);

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

   PlotIndexSetInteger(4,PLOT_DRAW_BEGIN,min_rates_total);

//---- CAB0=>2:0 7=0G5=89 8=48:0B>@0, :>B>@K5 =5 1C4CB 2848<K =0 3@0D8:5

   PlotIndexSetDouble(4,PLOT_EMPTY_VALUE,NULL);

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

   PlotIndexSetInteger(4,PLOT_SHIFT,Shift);

//---- 70?@5B =0 >B>1@065=85 7=0G5=89 8=48:0B>@0 2 ;52>< 25@E=5< C3;C >:=0 8=48:0B>@0

   PlotIndexSetInteger(4,PLOT_SHOW_DATA,false);

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

   ArraySetAsSeries(Zone3Upper,true);



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

   SetIndexBuffer(5,Zone3Lower,INDICATOR_DATA);

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

   PlotIndexSetInteger(5,PLOT_DRAW_BEGIN,min_rates_total);

//---- CAB0=>2:0 7=0G5=89 8=48:0B>@0, :>B>@K5 =5 1C4CB 2848<K =0 3@0D8:5

   PlotIndexSetDouble(5,PLOT_EMPTY_VALUE,NULL);

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

   PlotIndexSetInteger(5,PLOT_SHIFT,Shift);

//---- 70?@5B =0 >B>1@065=85 7=0G5=89 8=48:0B>@0 2 ;52>< 25@E=5< C3;C >:=0 8=48:0B>@0

   PlotIndexSetInteger(5,PLOT_SHOW_DATA,false);

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

   ArraySetAsSeries(Zone3Lower,true);



//--- 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,"Hans_Indicator_x4("+string(LocalTimeZone)+","+

                      string(DestTimeZone)+","+string(PipsForEntry1)+","+string(PipsForEntry2)+","+string(Shift)+")");

//--- >?@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[],

                const double &Low[],

                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;



//---- @0AG5B AB0@B>2>3> =><5@0 limit 4;O F8:;0 ?5@5AG5B0 10@>2

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

     {

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

     }

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

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

   ArraySetAsSeries(Time,true);

   ArraySetAsSeries(High,true);

   ArraySetAsSeries(Low,true);



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

   BreakoutRanges(0,limit,LocalTimeZone,DestTimeZone,rates_total,Time,High,Low);

//----    

   return(rates_total);

  }

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

//| Compute index of first/last bar of yesterday and today           |

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

int BreakoutRanges(int offset,int &lastbar,int tzlocal,int tzdest,

                   const int rates_total_,const datetime &Time_[],const double &High_[],const double &Low_[])

  {

//----

   int i,j,k,

   tzdiff=tzlocal-tzdest,

   tzdiffsec=tzdiff*3600,

   tidxstart[2]={ 0,0},

   tidxend[2]={ 0,0 };

   double thigh[2]={ 0.0,0.0 },

   tlow[2]= { DBL_MAX };

   string tfrom[3]={ "04:00","08:00",/*rest of day: */ "12:00"},

   tto[3]={ "08:00","12:00",/*rest of day: */ "24:00" },

   tday;

   bool inperiod=-1;

   datetime timet;



//

// search back for the beginning of the day

//

   tday=TimeToString(Time_[lastbar]-tzdiffsec,TIME_DATE);

   for(; lastbar<rates_total_-1; lastbar++)

     {

      if(TimeToString(Time_[lastbar]-tzdiffsec,TIME_DATE)!=tday)

        {

         lastbar--;

         break;

        }

     }



//

// find the high/low for the two periods and carry them forward through the day

//

   tday="XXX";

   for(i=lastbar; i>=offset; i--)

     {



      timet=Time_[i]-tzdiffsec;   // time of this bar



      string timestr=TimeToString(timet,TIME_MINUTES),// current time HH:MM

      thisday=TimeToString(timet,TIME_DATE);       // current date



                                                   //

      // for all three periods (first period, second period, rest of day)

      //

      for(j=0; j<2; j++)

        {

         if(tfrom[j]<=timestr && timestr<tto[j])

           {   // Bar[i] in this period

            if(inperiod!=j)

              { // entered new period, so last one is completed



               if(j>0)

                 {      // now draw high/low back over the recently completed period

                  for(k=tidxstart[j-1]; k>=tidxend[j-1]; k--)

                    {

                     if(j-1==0)

                       {

                        Zone1Upper[k]= thigh[j-1];

                        Zone1Lower[k]= tlow[j-1];

                       }



                     if(j-1==1)

                       {

                        Zone2Upper[k]= thigh[j-1];

                        Zone2Lower[k]= tlow[j-1];

                       }

                    }

                 }



               inperiod=j;   // remember current period

              }



            if(inperiod==2) // inperiod==2 (end of day) is just to check completion of zone 2

               break;



            // for the current period find idxstart, idxend and compute high/low

            if(tidxstart[j]==0)

              {

               tidxstart[j]=i;

               tday=thisday;

              }



            tidxend[j]=i;



            thigh[j]=MathMax(thigh[j],High_[i]);

            tlow[j]=MathMin(tlow[j],Low_[i]);

           }

        }



      // 

      // carry forward the periods for which we have definite high/lows

      //

      if(inperiod>=1 && tday==thisday)

        { // first time period completed

         Zone1Upper[i]= thigh[0]+PipsForEntry1*_Point;

         Zone1Lower[i]= tlow[0]-PipsForEntry1*_Point;

         Zone2Upper[i]= thigh[0]+PipsForEntry1*_Point;

         Zone2Lower[i]= tlow[0]-PipsForEntry1*_Point;

         Zone3Upper[i]= thigh[0]+PipsForEntry2*_Point;

         Zone3Lower[i]= tlow[0]-PipsForEntry2*_Point;

        }

      else

        {   // none yet to carry forward (zero to clear old values, e.g. from switching timeframe)

         Zone1Upper[i]=Zone1Lower[i]=Zone2Upper[i]=Zone2Lower[i]=Zone3Upper[i]=Zone3Lower[i]=NULL;

        }



      //

      // at the beginning of a new day reset everything

      //

      if(tday!="XXX" && tday!=thisday)

        {

         //Print("#",i,"new day ",thisday,"/",tday);



         tday="XXX";



         inperiod=-1;



         for(j=0; j<2; j++)

           {

            tidxstart[j]=0;

            tidxend[j]=0;



            thigh[j]=0;

            tlow[j]=99999;

           }

        }

     }

//----

   return (0);

  }

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

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