bleh
[mqlkit.git] / indicators / AdaptiveCyberCycle.mq4
blobfcdf59c5aa335cee8b5fa3512d137d5a59505e24
1 //+------------------------------------------------------------------+\r
2 //|                                           AdaptiveCyberCycle.mq4 |\r
3 //|                                                                  |\r
4 //| Adaptive Cyber Cycle                                             |\r
5 //|                                                                  |\r
6 //| Algorithm taken from book                                        |\r
7 //|     "Cybernetics Analysis for Stock and Futures"                 |\r
8 //| by John F. Ehlers                                                |\r
9 //|                                                                  |\r
10 //|                                              contact@mqlsoft.com |\r
11 //|                                          http://www.mqlsoft.com/ |\r
12 //+------------------------------------------------------------------+\r
13 #property copyright "Coded by Witold Wozniak"\r
14 #property link      "www.mqlsoft.com"\r
16 #property indicator_separate_window\r
17 #property indicator_buffers 2\r
18 #property indicator_color1 Red\r
19 #property indicator_color2 Blue\r
21 #property indicator_level1 0\r
23 double Cycle[];\r
24 double Trigger[];\r
25 double Smooth[];\r
27 extern double Alpha = 0.07;\r
28 int buffers = 0;\r
29 int drawBegin = 0;\r
31 int init() {\r
32     drawBegin = 8;      \r
33     initBuffer(Cycle, "Cycle", DRAW_LINE);\r
34     initBuffer(Trigger, "Trigger", DRAW_LINE);\r
35     initBuffer(Smooth);\r
36     IndicatorBuffers(buffers);\r
37     IndicatorShortName("Adaptive Cyber Cycle [" + DoubleToStr(Alpha, 2) + "]");\r
38     return (0);\r
39 }\r
40   \r
41 int start() {\r
42     if (Bars <= drawBegin) return (0);\r
43     int countedBars = IndicatorCounted();\r
44     if (countedBars < 0) return (-1);\r
45     if (countedBars > 0) countedBars--;\r
46     int s, limit = Bars - countedBars - 1;\r
47     for (s = limit; s >= 0; s--) {\r
48         Smooth[s] = (P(s) + 2.0 * P(s + 1) + 2.0 * P(s + 2) + P(s + 3)) / 6.0;\r
49         double period = iCustom(0, 0, "CyclePeriod", Alpha, 0, s);\r
50         double alpha1 = 2.0 / (period + 1.0);\r
51         Cycle[s] = (1.0 - 0.5 * alpha1) * (1.0 - 0.5 * alpha1) * (Smooth[s] - 2.0 * Smooth[s + 1] \r
52             + Smooth[s + 2]) + 2.0 * (1.0 - alpha1) * Cycle[s + 1] - (1.0 - alpha1) * Cycle[s + 2];           \r
53         if (s > Bars - 8) {\r
54             Cycle[s] = (P(s) - 2.0 * P(s + 1) + P(s + 2)) / 4.0;\r
55         }\r
56         Trigger[s] = Cycle[s + 1];\r
57     }\r
58     return (0);\r
59 }\r
61 double P(int index) {\r
62     return ((High[index] + Low[index]) / 2.0);\r
63 }\r
65 void initBuffer(double array[], string label = "", int type = DRAW_NONE, int arrow = 0, int style = EMPTY, int width = EMPTY, color clr = CLR_NONE) {\r
66     SetIndexBuffer(buffers, array);\r
67     SetIndexLabel(buffers, label);\r
68     SetIndexEmptyValue(buffers, EMPTY_VALUE);\r
69     SetIndexDrawBegin(buffers, drawBegin);\r
70     SetIndexShift(buffers, 0);\r
71     SetIndexStyle(buffers, type, style, width);\r
72     SetIndexArrow(buffers, arrow);\r
73     buffers++;\r