bleh
[mqlkit.git] / indicators / JurikBands_v1.mq4
bloba9c5301670eb7c7d7808c9be470f400e021383c3
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
7 // List of Prices:\r
8 // Price    = 0 - Close  \r
9 // Price    = 1 - Open  \r
10 // Price    = 2 - High  \r
11 // Price    = 3 - Low  \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
19  \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
30 //---- \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
34 //---- \r
35 double Up[],Dn[];\r
36 //----\r
37 double haClose[], haOpen[], haHigh[], haLow[];\r
38 int    draw_begin, pBars, mcnt_bars; \r
39 string short_name;\r
40 double len, len1, len2, pow1, bet, R;\r
41 int    AvgLen = 65;\r
42 //+------------------------------------------------------------------+\r
43 //| Custom indicator initialization function                         |\r
44 //+------------------------------------------------------------------+\r
45 int init()\r
46 {\r
47 //---- \r
48    SetIndexStyle(0,DRAW_LINE);\r
49    SetIndexStyle(1,DRAW_LINE);\r
50   \r
51    if(TimeFrame == 0 || TimeFrame < Period()) TimeFrame = Period();\r
52      \r
53    draw_begin=AvgLen*TimeFrame/Period();\r
54 //---- \r
55    switch(TimeFrame)\r
56    {\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
67    } \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
72    \r
73    SetIndexDrawBegin(0,draw_begin);\r
74    SetIndexDrawBegin(1,draw_begin);\r
75 //---- \r
76    SetIndexBuffer(0,Up);\r
77    SetIndexBuffer(1,Dn);   \r
78 //----\r
79    len = 0.5*(Length - 1);\r
80    \r
81    len1 = MathLog(MathSqrt(len))/MathLog(2.0) + 2;\r
82         if (len1 < 0) len1 = 0;\r
83         \r
84         pow1 = len1 - 2;\r
85         if (pow1 < 0.5) pow1 = 0.5;\r
86     \r
87         len2 = MathSqrt(len)*len1;\r
88     \r
89         bet = len2/(len2 + 1);   \r
90 //---- \r
91    return(0);\r
92 }\r
93 //+------------------------------------------------------------------+\r
94 //| JurikBands_v1                                                    |\r
95 //+------------------------------------------------------------------+\r
96 int start()\r
97 {\r
98    int limit, y, i, shift, cnt_bars=IndicatorCounted(); \r
99    double price, bsmax[], bsmin[], Volty[], AvgVolty[], vSum[];\r
100       \r
101    if(TimeFrame!=Period()) int mBars = iBars(NULL,TimeFrame); else mBars = Bars; \r
102    if(mBars != pBars)\r
103    {\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
110       {\r
111       ArrayResize(haClose,mBars);\r
112       ArrayResize(haOpen,mBars);\r
113       ArrayResize(haHigh,mBars);\r
114       ArrayResize(haLow,mBars);\r
115       }\r
116   \r
117    pBars = mBars;\r
118    }  \r
119    \r
120    if(cnt_bars<1)\r
121    {\r
122       for(i=Bars-1;i>0;i--) \r
123       {\r
124       Up[i]=EMPTY_VALUE; \r
125       Dn[i]=EMPTY_VALUE;\r
126       }\r
127    mcnt_bars = 0;\r
128    }\r
129 //---- \r
130    if(mcnt_bars > 0) mcnt_bars--;\r
131    \r
132    for(y=mcnt_bars;y<mBars;y++)\r
133    {\r
134       if(Price <= 6) price = iMA(NULL,TimeFrame,1,0,0,Price,mBars-y-1);   \r
135       else\r
136       if(Price > 6 && Price <= 10) price = HeikenAshi(TimeFrame,Price-7,mBars-y-1);\r
137    \r
138       if(y == 1) {bsmax[y] = price; bsmin[y] = price;}\r
139       else\r
140       {  \r
141                    double del1 = price - bsmax[y-1];\r
142          double del2 = price - bsmin[y-1];\r
143                 \r
144          if(MathAbs(del1) > MathAbs(del2)) Volty[y] = MathAbs(del1); \r
145          else \r
146               if(MathAbs(del1) < MathAbs(del2)) Volty[y] = MathAbs(del2);\r
147               else\r
148               if(MathAbs(del1) == MathAbs(del2)) Volty[y] = 0;\r
149                 \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
152                    else\r
153                    AvgVolty[y] = SMA(vSum,AvgLen,y);\r
154                  \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
158            \r
159               double pow2 = MathPow(dVolty, pow1);\r
160          double Kv = MathPow(bet, MathSqrt(pow2));              \r
161         \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
164            \r
165          if(TimeFrame == Period()) \r
166          {\r
167          Up[mBars-y-1] = bsmax[y];\r
168          Dn[mBars-y-1] = bsmin[y];\r
169          }\r
170       }\r
171       mcnt_bars = mBars-1;\r
172    }\r
173    \r
174    if(TimeFrame > Period())\r
175    { \r
176       if(cnt_bars>0) cnt_bars--;\r
177       limit = Bars-cnt_bars+TimeFrame/Period()-1;\r
178       \r
179       for(shift=0,y=0;shift<limit;shift++)\r
180       {\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
184       }\r
185    }\r
186    return(0);\r
189 double SMA(double array[],int per,int bar)\r
191    double Sum = 0;\r
192    for(int i = 0;i < per;i++) Sum += array[bar-i];\r
193    \r
194    return(Sum/per);\r
195 }                \r
197 double HeikenAshi(int tf,int price,int bar)\r
198 \r
199    if(bar == iBars(NULL,TimeFrame)- 1) \r
200    {\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
205    }\r
206    else\r
207    {\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
212    }\r
213    \r
214    switch(price)\r
215    {\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
220    }\r
221 }     \r