02.//---- indicator settings
03.
04.#property indicator_separate_window
05.
06.#property indicator_buffers 5
07.
08.#property indicator_color1 DodgerBlue
09.
10.#property indicator_color2 Magenta
11.
12.#property indicator_color3 Yellow
13.
14.#property indicator_color4 DeepSkyBlue
15.
16.#property indicator_color5 Blue
17.
18.
19.
20.#property indicator_minimum 0
21.
22.#property indicator_level1 20
23.
24.#property indicator_level2 25
25.
26.#property indicator_maximum 60
27.
28.
29.
30.//---- indicator parameters
31.
32.extern int Smooth = 14;
33.
34.extern bool Hide_DI_Plus = False;
35.
36.extern bool Hide_DI_Minus = False;
37.
38.extern bool Hide_DX = true;
39.
40.extern bool Hide_ADX = true;
41.
42.extern bool Hide_ADXR = true;
43.
44.
45.
46.//---- indicator buffers
47.
48.double b_di_p[]; // buffer +DI
49.
50.double b_di_m[]; // buffer -DI
51.
52.double b_dmi[]; // buffer DMI
53.
54.double b_adx[]; // buffer Average Directional indeX
55.
56.double b_adxr[]; // buffer Average Directional indeX Rate
57.
58.
59.
60.//---- non-indicator buffers
61.
62.//---- NOTICE that these arrays must be included in ArraySetAsSeries()
63.
64.double dm_p_avg[];
65.
66.double dm_m_avg[];
67.
68.double tr_avg[];
69.
70.
71.
72.
73.
74.//+------------------------------------------------------------------+
75.
76.//| Custom indicator initialization function |
77.
78.//+------------------------------------------------------------------+
79.
80.int init() {
81.
82.//---- indicator buffers mapping
83.
84.IndicatorBuffers(8);
85.
86.if ( !SetIndexBuffer(0,b_di_p)
87.
88.&& !SetIndexBuffer(1,b_di_m)
89.
90.&& !SetIndexBuffer(2,b_dmi)
91.
92.&& !SetIndexBuffer(3,b_adx)
93.
94.&& !SetIndexBuffer(4,b_adxr)
95.
96.&& !SetIndexBuffer(5,dm_p_avg)
97.
98.&& !SetIndexBuffer(6,dm_m_avg)
99.
100.&& !SetIndexBuffer(7,tr_avg)
101.
102.) { Print("cannot set indicator buffers!"); return(-1); }
103.
104.//---- name for DataWindow and indicator subwindow label
105.
106.IndicatorShortName("DMI("+Smooth+")");
107.
108.SetIndexLabel(0,"+DI");
109.
110.SetIndexLabel(1,"-DI");
111.
112.SetIndexLabel(2,"DX");
113.
114.SetIndexLabel(3,"ADX");
115.
116.SetIndexLabel(4,"ADXR");
117.
118.//----
119.
120.SetIndexStyle(0, DRAW_LINE+Hide_DI_Plus*DRAW_NONE, STYLE_SOLID, 1,
121.indicator_color1);
122.
123.SetIndexStyle(1, DRAW_LINE+Hide_DI_Minus*DRAW_NONE, STYLE_SOLID, 1,
124.indicator_color2);
125.
126.SetIndexStyle(2, DRAW_LINE+Hide_DX*DRAW_NONE, STYLE_SOLID, 1,
127.indicator_color3);
128.
129.SetIndexStyle(3, DRAW_LINE+Hide_ADX*DRAW_NONE, STYLE_SOLID, 1,
130.indicator_color4);
131.
132.SetIndexStyle(4, DRAW_LINE+Hide_ADXR*DRAW_NONE, STYLE_SOLID, 1,
133.indicator_color5);
134.
135.
136.
137.//----
138.
139.SetIndexDrawBegin(0,Smooth);
140.
141.SetIndexDrawBegin(1,Smooth);
142.
143.SetIndexDrawBegin(2,Smooth);
144.
145.SetIndexDrawBegin(3,2*Smooth);
146.
147.SetIndexDrawBegin(4,3*Smooth);
148.
149.
150.
151.return(0);
152.
153.}
154.
155.//+------------------------------------------------------------------+
156.
157.//| Custor indicator deinitialization function |
158.
159.//+------------------------------------------------------------------+
160.
161.int deinit() {
162.
163.return(0);
164.
165.}
166.
167.
168.
169.//+------------------------------------------------------------------+
170.
171.//| Custom indicator iteration function |
172.
173.//+------------------------------------------------------------------+
174.
175.int start() {
176.
177.int counted_bars=IndicatorCounted();
178.
179.if (counted_bars<0) return(-1);
180.
181.if (Bars < 2) return(-1);
182.
183.if (Smooth < 2) return(-1);
184.
185.int limit=Bars-1-counted_bars;
186.
187.
188.
189.double smooth = 1.0 / (Smooth * 1.0);
190.
191.
192.
193.int firstDMI = Bars-1-Smooth;
194.
195.int firstADX = Bars-1-(2*Smooth);
196.
197.int firstADXR = Bars-1-(3*Smooth);
198.
199.
200.
201.for (int i=limit; i>=0; i--) {
202.
203.double high;
204.
205.double low;
206.
207.double dm_p;
208.
209.double dm_m;
210.
211.
212.
213.if (i<firstDMI) {
214.
215.high = High[i] - High[i+1];
216.
217.low = Low[i+1] - Low[i];
218.
219.dm_p = 0.0;
220.
221.dm_m = 0.0;
222.
223.if ((high > low) && (high > 0.0))
224.
225.dm_p = high;
226.
227.else if ((low > high) && (low > 0.0))
228.
229.dm_m = low;
230.
231.// calculate averages (cumulative formula)
232.
233.dm_p_avg[i] = dm_p_avg[i+1] - ( smooth * dm_p_avg[i+1] ) + dm_p;
234.
235.dm_m_avg[i] = dm_m_avg[i+1] - ( smooth * dm_m_avg[i+1] ) + dm_m;
236.
237.tr_avg[i] = tr_avg[i+1] - ( smooth * tr_avg[i+1] ) +
238.calcTR(i);
239.
240.} else if (i==firstDMI) {
241.
242.double sum_dm_p = 0.0;
243.
244.double sum_dm_m = 0.0;
245.
246.double sum_tr = 0.0;
247.
248.for (int j=i; j<i+Smooth; j++) {
249.
250.high = High[j] - High[j+1];
251.
252.low = Low[j+1] - Low[j];
253.
254.dm_p = 0.0 ;
255.
256.dm_m = 0.0 ;
257.
258.if ((high > low) && (high > 0.0))
259.
260.dm_p = high;
261.
262.else if ((low > high) && (low > 0.0))
263.
264.dm_m = low;
265.
266.sum_dm_p = sum_dm_p + dm_p;
267.
268.sum_dm_m = sum_dm_m + dm_m;
269.
270.sum_tr = sum_tr + calcTR(j);
271.
272.}
273.
274.// define current values
275.
276.dm_p_avg[i] = sum_dm_p * smooth;
277.
278.dm_m_avg[i] = sum_dm_m * smooth;
279.
280.tr_avg[i] = sum_tr * smooth;
281.
282.} else {
283.
284.tr_avg[i] = 0.0;
285.
286.}
287.
288.
289.
290.// Calculate +DI and -DI
291.
292.if (tr_avg[i] > 0.0) {
293.
294.b_di_p[i] = 100.0 * dm_p_avg[i] / tr_avg[i];
295.
296.b_di_m[i] = 100.0 * dm_m_avg[i] / tr_avg[i];
297.
298.} else {
299.
300.b_di_p[i] = 0.0;
301.
302.b_di_m[i] = 0.0;
303.
304.}
305.
306.
307.
308.// calcule DMI
309.
310.double sum = b_di_p[i] + b_di_m[i];
311.
312.double diff = MathAbs( b_di_p[i] - b_di_m[i] );
313.
314.if (sum > 0.0) {
315.
316.b_dmi[i] = 100.0 * diff / sum;
317.
318.} else {
319.
320.b_dmi[i] = 0.0 ;
321.
322.}
323.
324.
325.
326.// Calculate ADX
327.
328.if (i<firstADX) {
329.
330.b_adx[i] = smooth*(b_adx[i+1]*(Smooth-1) + b_dmi[i]);
331.
332.} else if (i==firstADX) {
333.
334.double sum_dmi = 0.0;
335.
336.for (int k=i+Smooth+1; k>i; k--) {
337.
338.sum_dmi = sum_dmi + b_dmi[k];
339.
340.}
341.
342.b_adx[i] = smooth * sum_dmi;
343.
344.} else {
345.
346.b_adx[i] = 0.0;
347.
348.}
349.
350.
351.
352.// Calculate ADXR
353.
354.if (i<=firstADXR) {
355.
356.b_adxr[i] = (b_adx[i] + b_adx[i+Smooth]) / 2;
357.
358.} else {
359.
360.b_adxr[i] = 0.0;
361.
362.}
363.
364.
365.
366.} // for
367.
368.
369.
370.return(0);
371.
372.}
373.
374.
375.
376.double calcTR(int bar) {
377.
378.double a = High[bar] - Low[bar]; // A = Today's High -Today's Low
379.
380.double b = MathAbs(Close[bar+1] - High[bar]); // B = Yesterday's Close - Today's High
381.
382.double c = MathAbs(Close[bar+1] - Low[bar]); // C = Yesterday's Close - Today's Low
383.
384.return(MathMax(MathMax(a,b),c));
385.
386.}
|