MetaTrader5
Understanding the Cycle Period Indicator for MetaTrader 5
Author: Witold Wozniak
The Cycle Period indicator is a handy tool designed to measure the periodicity of price changes in financial assets. It keeps track of current market cycle values, which, as you might expect, are never really stable. This indicator works best when paired with oscillators, allowing them to adapt to the ever-changing market cycles.
This concept is inspired by John Ehlers' insightful article "Using The Fisher Transform", published in November 2002 in the "Technical Analysis Of Stock & Commodities" magazine.
To use the Cycle Period indicator, you'll need to declare its handle at a global level. This allows it to be utilized in other indicator codes, like the RVI oscillator:
//---- Declaration of integer variables for the indicator handles
int CP_Handle;
Next, you need to retrieve the Cycle Period indicator handle in the initialization block of the RVI indicator:
//---- Getting the Cycle Period indicator handle
CP_Handle=iCustom(NULL,0,"CyclePeriod", Alpha);
if(CP_Handle==INVALID_HANDLE)
{
Print("Failed to get the Cycle Period indicator handle");
return(1);
}
Now, you have the new Alpha variable, which serves as the input parameter for the indicator and the smoothing ratio. This needs to be transformed into the input variable of the developed indicator:
//+----------------------------------------------+
//| Indicator input parameters |
//+----------------------------------------------+
input double Alpha=0.07; // Smoothing ratio
You might want to remove the previous Length input variable from the input parameters list and change it to a local variable within the OnCalculate() function. This is because the size of the arrays used for smoothing is determined by the Length parameter:
//---- Memory allocation for the variable arrays
ArrayResize(Count, Length);
ArrayResize(Value1, Length);
ArrayResize(Value2, Length);
Since this parameter's value is changing, it’s wise to set the array sizes to a value that won’t be less than the maximum expected for this variable. Typically, we find that this value doesn’t exceed 100, so you can set the array sizes like this:
//---- Memory distribution for the variable arrays
ArrayResize(Count, MAXPERIOD);
ArrayResize(Value1, MAXPERIOD);
ArrayResize(Value2, MAXPERIOD);
Finally, in the OnCalculate() block, you'll need to retrieve the period values for the current bar from the Cycle Period custom indicator buffer. This allows you to use them instead of the former Length input parameter:
//---- Main indicator calculation loop
for(bar=first; bar<rates_total && !IsStopped(); bar++)
{
//---- Copy newly appeared data into the array
if(CopyBuffer(CP_Handle,0,rates_total-1-bar,4,period)<=0) return(RESET);
Length=int(MathFloor((4.0*period[0]+3.0*period[1]+2.0*period[2]+period[3])/20.0));
if(bar<Length) Length=bar; // Adjusting the smoothing to the actual number of bars
In this case, the last four values are taken from the Cycle Period indicator buffer, and their linearly weighted smoothing is applied. The resulting value is used as the Length smoothing period. Lastly, make sure to modify the ending line of the indicator code:
if(bar<rates_total-1) Recount_ArrayZeroPos(Count, MAXPERIOD);
And there you have it! You've successfully created an Adaptive RVI oscillator:
2011.11.23