1 //+------------------------------------------------------------------+
\r
2 //| JurikBands_v1.mq4 |
\r
3 //| Copyright © 2008-09, TrendLaboratory |
\r
4 //| http://finance.groups.yahoo.com/group/TrendLaboratory |
\r
5 //| E-mail: igorad2003@yahoo.co.uk |
\r
6 //+------------------------------------------------------------------+
\r
8 // Price = 0 - Close
\r
10 // Price = 2 - High
\r
12 // Price = 4 - Median Price = (High+Low)/2
\r
13 // Price = 5 - Typical Price = (High+Low+Close)/3
\r
14 // Price = 6 - Weighted Close = (High+Low+Close*2)/4
\r
15 // Price = 7 - Heiken Ashi Close
\r
16 // Price = 8 - Heiken Ashi Open
\r
17 // Price = 9 - Heiken Ashi High
\r
18 // Price =10 - Heiken Ashi Low
\r
20 #property copyright "Copyright © 2008-09, TrendLaboratory"
\r
21 #property link "http://finance.groups.yahoo.com/group/TrendLaboratory"
\r
23 #property indicator_chart_window
\r
24 #property indicator_buffers 2
\r
25 #property indicator_color1 Blue
\r
26 #property indicator_width1 1
\r
27 #property indicator_color2 Red
\r
28 #property indicator_width2 1
\r
31 extern int TimeFrame = 0; //Time Frame in min
\r
32 extern int Price = 0; //Price Mode (0...10)
\r
33 extern int Length = 14; //Period of smoothing
\r
37 double haClose[], haOpen[], haHigh[], haLow[];
\r
38 int draw_begin, pBars, mcnt_bars;
\r
40 double len, len1, len2, pow1, bet, R;
\r
42 //+------------------------------------------------------------------+
\r
43 //| Custom indicator initialization function |
\r
44 //+------------------------------------------------------------------+
\r
48 SetIndexStyle(0,DRAW_LINE);
\r
49 SetIndexStyle(1,DRAW_LINE);
\r
51 if(TimeFrame == 0 || TimeFrame < Period()) TimeFrame = Period();
\r
53 draw_begin=AvgLen*TimeFrame/Period();
\r
57 case 1 : string TF = "M1"; break;
\r
58 case 5 : TF = "M5"; break;
\r
59 case 15 : TF = "M15"; break;
\r
60 case 30 : TF = "M30"; break;
\r
61 case 60 : TF = "H1"; break;
\r
62 case 240 : TF ="H4"; break;
\r
63 case 1440 : TF="D1"; break;
\r
64 case 10080 : TF="W1"; break;
\r
65 case 43200 : TF="MN1"; break;
\r
66 default : TF="Current";
\r
68 short_name = "JurikBands_v1";
\r
69 IndicatorShortName(short_name+"("+Length+")"+" "+TF);
\r
70 SetIndexLabel(0,short_name+"("+Length+")"+" "+TF);
\r
71 SetIndexLabel(1,short_name+"_lower");
\r
73 SetIndexDrawBegin(0,draw_begin);
\r
74 SetIndexDrawBegin(1,draw_begin);
\r
76 SetIndexBuffer(0,Up);
\r
77 SetIndexBuffer(1,Dn);
\r
79 len = 0.5*(Length - 1);
\r
81 len1 = MathLog(MathSqrt(len))/MathLog(2.0) + 2;
\r
82 if (len1 < 0) len1 = 0;
\r
85 if (pow1 < 0.5) pow1 = 0.5;
\r
87 len2 = MathSqrt(len)*len1;
\r
89 bet = len2/(len2 + 1);
\r
93 //+------------------------------------------------------------------+
\r
95 //+------------------------------------------------------------------+
\r
98 int limit, y, i, shift, cnt_bars=IndicatorCounted();
\r
99 double price, bsmax[], bsmin[], Volty[], AvgVolty[], vSum[];
\r
101 if(TimeFrame!=Period()) int mBars = iBars(NULL,TimeFrame); else mBars = Bars;
\r
104 ArrayResize(bsmin,mBars);
\r
105 ArrayResize(bsmax,mBars);
\r
106 ArrayResize(AvgVolty,mBars);
\r
107 ArrayResize(Volty,mBars);
\r
108 ArrayResize(vSum,mBars);
\r
109 if(Price > 6 && Price <= 10)
\r
111 ArrayResize(haClose,mBars);
\r
112 ArrayResize(haOpen,mBars);
\r
113 ArrayResize(haHigh,mBars);
\r
114 ArrayResize(haLow,mBars);
\r
122 for(i=Bars-1;i>0;i--)
\r
124 Up[i]=EMPTY_VALUE;
\r
130 if(mcnt_bars > 0) mcnt_bars--;
\r
132 for(y=mcnt_bars;y<mBars;y++)
\r
134 if(Price <= 6) price = iMA(NULL,TimeFrame,1,0,0,Price,mBars-y-1);
\r
136 if(Price > 6 && Price <= 10) price = HeikenAshi(TimeFrame,Price-7,mBars-y-1);
\r
138 if(y == 1) {bsmax[y] = price; bsmin[y] = price;}
\r
141 double del1 = price - bsmax[y-1];
\r
142 double del2 = price - bsmin[y-1];
\r
144 if(MathAbs(del1) > MathAbs(del2)) Volty[y] = MathAbs(del1);
\r
146 if(MathAbs(del1) < MathAbs(del2)) Volty[y] = MathAbs(del2);
\r
148 if(MathAbs(del1) == MathAbs(del2)) Volty[y] = 0;
\r
150 vSum[y] = vSum[y-1] + 0.1*(Volty[y] - Volty[y-10]);
\r
151 if(y <= AvgLen+1) AvgVolty[y] = AvgVolty[y-1] + 2.0*(vSum[y] - AvgVolty[y-1])/(AvgLen+1);
\r
153 AvgVolty[y] = SMA(vSum,AvgLen,y);
\r
155 if (AvgVolty[y] > 0) double dVolty = Volty[y]/AvgVolty[y];
\r
156 if (dVolty > MathPow(len1,1.0/pow1)) dVolty = MathPow(len1,1.0/pow1);
\r
157 if (dVolty < 1) dVolty = 1.0;
\r
159 double pow2 = MathPow(dVolty, pow1);
\r
160 double Kv = MathPow(bet, MathSqrt(pow2));
\r
162 if (del1 > 0) bsmax[y] = price; else bsmax[y] = price - Kv*del1;
\r
163 if (del2 < 0) bsmin[y] = price; else bsmin[y] = price - Kv*del2;
\r
165 if(TimeFrame == Period())
\r
167 Up[mBars-y-1] = bsmax[y];
\r
168 Dn[mBars-y-1] = bsmin[y];
\r
171 mcnt_bars = mBars-1;
\r
174 if(TimeFrame > Period())
\r
176 if(cnt_bars>0) cnt_bars--;
\r
177 limit = Bars-cnt_bars+TimeFrame/Period()-1;
\r
179 for(shift=0,y=0;shift<limit;shift++)
\r
181 if (Time[shift] < iTime(NULL,TimeFrame,y)) y++;
\r
182 Up[shift]=bsmax[mBars-y-1];
\r
183 Dn[shift]=bsmin[mBars-y-1];
\r
189 double SMA(double array[],int per,int bar)
\r
192 for(int i = 0;i < per;i++) Sum += array[bar-i];
\r
197 double HeikenAshi(int tf,int price,int bar)
\r
199 if(bar == iBars(NULL,TimeFrame)- 1)
\r
201 haClose[bar] = iClose(NULL,tf,bar);
\r
202 haOpen[bar] = iOpen(NULL,tf,bar);
\r
203 haHigh[bar] = iHigh(NULL,tf,bar);
\r
204 haLow[bar] = iLow(NULL,tf,bar);
\r
208 haClose[bar] = (iOpen(NULL,tf,bar)+iHigh(NULL,tf,bar)+iLow(NULL,tf,bar)+iClose(NULL,tf,bar))/4;
\r
209 haOpen[bar] = (haOpen[bar+1]+haClose[bar+1])/2;
\r
210 haHigh[bar] = MathMax(iHigh(NULL,tf,bar),MathMax(haOpen[bar], haClose[bar]));
\r
211 haLow[bar] = MathMin(iLow(NULL,tf,bar),MathMin(haOpen[bar], haClose[bar]));
\r
216 case 0: return(haClose[bar]);break;
\r
217 case 1: return(haOpen[bar]);break;
\r
218 case 2: return(haHigh[bar]);break;
\r
219 case 3: return(haLow[bar]);break;
\r