John Ehlers used the following EasyLanguage code to calculate the Dominant Cycle in a small sample of data. If you are interested in cycles and noise reduction, definitely check out the books by John Ehlers – “Rocket Science for Traders” or “Cybernetic Analysis for Stocks and Futures.” I am doing some research in this area and wanted to share how I programmed the indicator/function in Python. I refer you to his books or online resources for an explanation of the code. I can tell you it involves an elegantly simplified approach using the Hilbert Transform.
In my Python based back tester an indicator of this type is best programmed by using a class. A class is really a simple construct, especially in Python, once you familiarize yourself with the syntax. This indicator requires you to refer to historical values to calculate the next value in the equation: Value1, inPhase, re, etc.,. In EasyLanguage these values are readily accessible as every variable is defined as a BarArray – the complete history of a variable is accessible by using indexing. In my PSB I used lists to store values for those variables most often used such as Open, High, Low, Close. When you need to store the values of let’s say the last five bars its best to just create a list on the fly or build them into a class structure. A Class stores data and data structures and includes the methods (functions) that the data will be pumped into. The follow code describes the class in two sections: 1) data declaration and instantiation and 2) the function to calculate the Dominant Cycle. First off I create the variables that will hold the constant values: imult and qmult. By using the word self I make these variables class members and can access them using “.” notation. I will show you later what this means. I also make the rest of the variables class members, but this time I make them lists and instantiate the first five values to zero. I use list comprehension to create the lists and zero out the first five elements – all in one line of code. This is really just a neat short cut, but can be used for much more powerful applications. Once you create a dominantCycleClass object the object is constructed and all of the data is connected to this particular object. You can create many dominantCycleClass objects and each one would maintain its own data. Remember a class is just a template that is used to create an object.
The second part of the class template contains the method or function for calculating the Dominant Cycle. Notice how I index into the lists to extract prior values. You will also see the word self. preceding the variable names used in the calculations. Initially I felt like this redundancy hurt the readability of the code and in this case it might. But by using self. I know I am dealing with a class member. This is an example of the ” . ” notation I referred to earlier. Basically this ties the variable to the class.
Okay we now have the class template to calculate the Dominant Cycle but how do we us it?
Here I assign domCycle the object created by calling the dominantCycleClass constructor. TempVal1 is assigned the Dominant Cycle when the function or method is called using the objects name (domCycle) and the now familiar ” . ” notation. See how you can also access the imult variable using the same notation.
Here is the code in its entirety. I put this in the indicator module of the PSB.