1 //+------------------------------------------------------------------+
\r
2 //| AdaptiveCyberCycle.mq4 |
\r
4 //| Adaptive Cyber Cycle |
\r
6 //| Algorithm taken from book |
\r
7 //| "Cybernetics Analysis for Stock and Futures" |
\r
8 //| by John F. Ehlers |
\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
27 extern double Alpha = 0.07;
\r
33 initBuffer(Cycle, "Cycle", DRAW_LINE);
\r
34 initBuffer(Trigger, "Trigger", DRAW_LINE);
\r
36 IndicatorBuffers(buffers);
\r
37 IndicatorShortName("Adaptive Cyber Cycle [" + DoubleToStr(Alpha, 2) + "]");
\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
54 Cycle[s] = (P(s) - 2.0 * P(s + 1) + P(s + 2)) / 4.0;
\r
56 Trigger[s] = Cycle[s + 1];
\r
61 double P(int index) {
\r
62 return ((High[index] + Low[index]) / 2.0);
\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