Referencing prior value on Ehler's Cyclic Component

Hi all,

There were so many questions regarding using previous value and I whittled through all of them and I am not sure what I am missing I am still not able to get the formula to work.

Essentially I am trying to create Ehler's Cyclic Component formula into AFL.

which is as below

Period - calculation period;
Applied price - price used for calculations.
Cyclic Component [i] = (HP[i] + 2HP[i-1] + 2HP[i-2] + HP[i-3])/6,
where
HP[i] = (Price[i] - Price[i-1])(1 + Alpha)/2 + 2AlphaHP[i-1],
Alpha = (1 - sin(2
Pi/Period))/cos(2*Pi/Period)

In the calculation of HP I see the previous value of HP is referenced there so I used For loop to do this but the result is not as expected. I tried various corrections to the formula and even tried using some of the formulas which were used in other threads to recreate it but I am still struggling. Much appreciate if you could take a look for me.


Period = 20;
pi = 3.141592653589793238;
Price = C;
Alpha = (1 - sin(2*pi/Period))/cos(2*pi/Period);
HP = 0;
for( i = 3; i < BarCount; i++ )
{
  HP[i] = (Price[i] - Price[i-1])*(1 + Alpha)/2 + 2*Alpha*HP[ i - 1 ];
  CyclicComponent [i] = (HP[i] + 2*HP[i-1] + 2*HP[i-2] + HP[i-3])/6;
  }
Plot(CyclicComponent,"CC",colorWhite,styleLine);

Using filters I figured out that the issue lies in

2*Alpha*HP[ i - 1 ]

But don't know how to solved it.
Appreciate if you could help

Doing that is like power of two.

Check yourself:

mult = 2;

HP = 1;
for( i = 1; i < BarCount; i++ )
{
    HP[i] = mult *  HP[ i - 1 ];    
}

gives similar results as this one

mult = 2;
HP = mult ^ BarIndex();

So you will reach infinity pretty quickly!
So use 1*alpha*HP[i-1] but not 2*alpha*HP[i-1].


That one can be moved outside of loop

CyclicComponent = (HP + 2 * Ref(HP,-1) + 2 * Ref(HP,-2) + Ref(HP,-3) ) / 6;

The other part of loop is like AMA/AMA2.
So this one

Period = 20;
pi = 3.141592;
pi_period = 2 * pi / Period;
Price = C;
Alpha = (1 - sin(pi_period))/ cos(pi_period);
price_diff = (Price - Ref(Price,-1)) * ( 1 + Alpha ) * 0.5;

mult = 1;
HP = 0;
for( i = 1; i < BarCount; i++ )
{
    HP[i] = price_diff[i] + mult * Alpha * HP[ i - 1 ];    
}

CyclicComponent = (HP + 2 * Ref(HP,-1) + 2 * Ref(HP,-2) + Ref(HP,-3) ) / 6;
Plot( CyclicComponent, "CC", colorWhite, styleLine );

Can be simplified by removing looping to get this one (by using AMA2 instead).

Period = 20;
pi = 3.141592;
pi_period = 2 * pi / Period;
Price = C;
Alpha = (1 - sin(pi_period))/ cos(pi_period);
price_diff = (Price - Ref(Price,-1)) * ( 1 + Alpha ) * 0.5;

mult = 1;
HP = AMA2(price_diff, 1, mult * Alpha);

CyclicComponent = (HP + 2 * Ref(HP,-1) + 2 * Ref(HP,-2) + Ref(HP,-3) ) / 6;
Plot( CyclicComponent, "CC", colorWhite, styleLine );
3 Likes

Thank you @fxshrat learned a lot just within this post. I see your point on 2^barindex() but if we are to calculate the same value in excel this will not go into infinity right? as prior value of the calculation can be taken into consideration. So I am failing to understand this logically.

Power of two applies everywhere!

Got nothing to do with AFL, Excel,... whatever.
It is just Math.

If you get different results in Excel then your calculations are different.

Long story short... simply accept facts.

BTW, here is Excel result. You pretty quickly do reach large numbers there too, don't you?
24

As for "infinity":
AmiBroker definition of infinity is reached at 2 ^ 128.

25

Look here how large result of it is:

2128 = 340,282,366,920,938,463,463,374,607,431,768,211,456
This would be something like the number of cells in 77,371,252,455,336,267,181,195,264 human bodies.

Do you ever reach such large number in your calculation related to stock market?


As aside other programs have limit too

Matlab, R, Excel limit is at 2 ^ 1024

MatLab
24

R
26

Excel
23

Other programs do not have either of upper limits.
So it is software development decision.

1 Like

Wrong. It is NOT "AmiBroker" definition of infinity. It is IEEE 754 International Standard for floating point representation in binary computers used IN ALL PROGRAMS because it is implemented in CPU/FPU HARDWARE.
And as long as you use hardware floating point (and pretty much all programs that want speed do), you are forced to use implementation available in hardware.

Wikipedia has lots of information about it, it is strongly recommended read for everyone:

1 Like

Yes, I know it is not AmiBroker definition and invention. Wrong wording on my end.
I was just comparing maxima between programs at which inf is shown as result.
29
Source: Wikipedia

That's why I wrote that it is software development decision.
That was my point trying to make if people think there may be "bug".

2 Likes

Thank you both for the detailed explanation.

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.