Subscription & data-feed pricing Class schedule

Margin rates Stock & option commissions

Attention: Discussion forums are read-only for extended maintenance until further notice.

 Cascades of Moving Averages Topic Rating:     Previous Topic · Next Topic Watch this topic · Print this topic ·
bustermu
 Posted : Saturday, May 07, 2005 5:58:06 PM
Registered User
Joined: 1/1/2005
Posts: 2,645
The following was posted elsewhere but should be of interest here.

We will show that a cascade of an EMA and an SMA can
be "uncascaded" in much the same way that two EMA's can. The
relevant portion of a previous message is repeated here.

Exponential Moving Averages (EMA) are frequently cascaded. For
example, this is done in the MACD Indicator.

If an EMA of period P is applied to an EMA of period Q and P<>Q,
then the result is a linear combination of the EMA of period P and
the EMA of period Q both on the original data. We will state this
as a theorem, but first we need some terminology.

We use the notation EVG(X,P) to designate the EMA of period P
applied to the data X. Thus, EVG(C,13) is an EMA of period 13
applied to closing prices and EVG(EVG(C,26),9)is an EMA of
period 9 applied to an EMA of period 26 applied to closing prices.

Theorem 1: If P<>Q and X is any data sequence, then

EVG(EVG(X,P),Q) = ((P-1)/(P-Q))*EVG(X,P)+((Q-1)/(Q-P))*EVG(X,Q)

Of course, this result can be extended to any number of cascaded
EMA's provided no two periods are the same.

Now, consider the cascade of an EMA of period P with an SMA of
period Q. Define XQ to be tha data sequence X delayed Q bars.

Theorem 2: If X is any data sequence, then

AVG(EVG(X,P),Q) = AVG(X,Q)-((P-1)/(2*Q))*EVG((X-XQ),P)

Thanks,
Jim Murphy
bustermu
 Posted : Monday, September 05, 2005 9:32:18 AM
Registered User
Joined: 1/1/2005
Posts: 2,645
In Theorem 1, we essentially "uncascaded" the cascade of two EMA's of different periods. Here, we will consider the cascade of n EMA's of the same period. The impulse response will be given and the nested-parentheses form for PCF's will be shown.

Let EVG^n(X,P) be the cascade of n EMA's of period P applied to the data sequence X so that

EVG^n(X,P) = EVG(EVG^(n-1)(X,P),P)

and

EVG^1(X,P) = EVG(X,P)

We will change the data sequence X to C in order to take advantage of the already known notation. We define a to be

a = (P-1)/(P+1).

Then, we have the more familiar expression

1-a = 2/(P+1).

The EMA of period P applied to C is, in terms of its impulse response h( k ), given by:

EVG(C,P) = h( 0 )*C0+h( 1 )*C1+...+h( k )*Ck+...

where

h( k ) = (1-a)*a^k

The binomial coefficients C(p,q) are defined by:

C(p,q) = p!/(q!(p-q)!)
= p(p-1)...1/((q(q-1)...1)((p-q)(p-q-1)...1))
= p(p-1)...(p-q+1)/(q(q-1)...1)
= p(p-1)...(q+1)/((p-q)(p-q-1)...1)
= C(p,p-q)

Theorem 3: The n cascades of EMA's of period P applied to C, EVG^n(C,P), is given by:

h( 0 )*C0+h( 1 )*C1+...+h( k )*Ck+...

where

h( k ) = (1-a)^n*C(k+n-1,k)*a^k
= (1-a)^n*C(k+n-1,n-1)*a^k

For example,

h( k ) = (1-a)*a^k, n = 1,

h( k ) = (1-a)^2*((k+1)/1)*a^k, n = 2,

h( k ) = (1-a)^3*((k+1)*(k+2)/2)*a^k, n = 3,

h( k ) = (1-a)^4*((k+1)*(k+2)*(k+3)/(2*3))*a^k, n = 4.

If we associate C(k+n-1,n-1) with the Ck, we have the nested-parentheses form:

Corollary 3.1: The n cascades of EMA's of period P applied to C, EVG^n(C,P), is given by:

(1-a)^n
*(C(n-1,n-1)*C0+a
*(C(1+n-1,n-1)*C1+a
*(C(2+n-1,n-1)*C2+a
*...
*(C(k+n-1,n-1)*Ck+a
*...)))...

For example,

for n = 1, EVG(C,P) is:

(1-a)
*(C0+a
*(C1+a
*(C1+a
*(C2+a
*...
*(Ck+a
*...)))...

for n = 2, EVG^2(C,P) is:

(1-a)^2
*(1*C0+a
*(2*C1+a
*(3*C2+a
*...
*((k+1)*Ck+a
*...)))...

for n = 3, EVG^3(C,P) is:

(1/2)*(1-a)^3
*(1*2*C0+a
*(2*3*C1+a
*(3*4*C2+a
*...
*((k+1)*(k+2)*Ck+a
*...)))...

for n = 4, EVG^4(C,P) is:

(1/(2*3))*(1-a)^4
*(1*2*3*C0+a
*(2*3*4*C1+a
*(3*4*5*C2+a
*...
*((k+1)*(k+2)*(k+3)*Ck+a
*...)))...

Notice that the (n-1)! in the denominator of C(k+n-1,n-1) has been factored out.

Any adjustments needed for truncation and statements concerning the error due to truncation are absent because I have not done the necessary derivations. Of course, if you use enough terms the approximation will be adequate, but that is insufficient information.

Thanks,
Jim Murphy
bustermu
 Posted : Monday, October 02, 2006 8:21:13 AM
Registered User
Joined: 1/1/2005
Posts: 2,645
We give three Lemmas on EMAs which should have been presented in the first post. These are followed by a remarkable Theorem which completely characterizes EMAs.

We will interchangably use closing prices C and X as filter input sequences with Ck and Xk representing the input k bars ago, k = 0,1,2,... The output of a filter will be represented by Y or whatever notation is used by TeleChart. We will be interested in constructable, stable, realizable, linear time-invariant (CSR LTI) filters. That is all a fancy way of saying that the output is given by:

Y0 = A0*X0+A1*X1+A2*X2+...+Ak*Xk+...

The fixed sequence A, which for stability must satisfy:

ABS(A0)+ABS(A1)+ABS(A2)+...+ABS(Ak)+,,, < infinity

is the impulse response of the filter.

For example, if:

Ak = 1/P, k = 0,1,2,...,(P-1)
Ak = 0, k = P,P+1,P+2,...

the filter is a Simple Moving Average (SMA) of period P.

If a satisfies 0 <= a < 1 and if:

Ak = (1-a)*a^k, k = 0,1,2,...

the filter is an Exponential Moving Average (EMA) of period P where P = (1+a)/(1-a). The only requirement on P is that it satisfy 1 <= P < infinity. There is no requirement that P be an integer.

Examples of built-in CSR LTI filters are: SMA, EMA, LRMA, PROC, and ROC. All of their cascades and all finite linear combinations of such cascades also satisify the requirements. It should be noted that all CSR LTI filters commute.

The EMA can be defined, equivalently, by its impulse response, its Z-transform, or its difference equation. For those aware of such things, the Z-transform of the EMA is:

(1-a)/(1-a*z^(-1))

The difference equation is given in:

Lemma 1: If the filter is an EMA, then:

Y0 = a*Y1 + (1-a)*X0

or,

XAVGCP.0 = a*XAVGCP.1 + (1-a)*C0

A couple of applications will lead to a proof of Theorem 1.

Lemma2: If the filter is an EMA, then:

X0 = (Y0 - a*Y1)/(1-a)

or,

C0 = ((P+1)*XAVGCP.0 - (P-1)*XAVGCP.1)/2

This shows that an EMA is invertable, i.e., the input is recoverable from the output. An SMA is not invertable. An application of an SMA to this form will yield Theorem 2 after simplification.

Lemma 3: If the filter is an EMA, then:

Y0 - Y1 = (1-a)*(X0-Y1)

or,

XAVGCP.0-XAVGCP.1 = (1-a)*(X0-XAVGCP.1)

This is an interesting result. Since 0 < 1-a <= 1, it shows that the output always moves strictly toward the present input and never beyond it. Any filter satisfying that condition is said to follow its input. We can construct filters which are not EMAs that follow their input, but you will find that they are not CSR LTI filters.

Theorem 4: A constructable, stable, realizable, LTI filter output follows its input if and only if it is an EMA.

Suppose we observe an EMA and an SMA on closing prices. The Theorem gives us an easy way to identify the EMA. It is the one which follows its input.

Filters have many uses and one is to smooth and track its input. For tracking applications. the ability of a filter to follow its input is very desirable. The Theorem tells us that the EMA is the only CSR LTI filter with this property. For example, AVGC5 and XAVG(XAVGC5,5) do not follow their input C. In our search for good tracking filters which follow their input, we have to use an EMA or turn to nonlinear filters and that is exactly what is done with many of the adaptive tracking filters.

As far as I know, Theorem 4 is not a published result.

Thanks,
Jim Murphy
 Posted : Friday, March 13, 2015 4:37:05 PM Gold Customer

Joined: 5/9/2011
Posts: 80

Bruce,

Much work for the weekend... Can you help me with an equation for RSI... need the equation for RSI both in Value and Linear Regression for a period of 9 and 18 periods. Plus, do you have the equation to calcuation the XAVG too again on a 9 period? It would be great if you could just post the equations so I have all weekend to work on fine tunning for correlating with imagery of the charts... the previous help you supplied for PPO and the offset feature is working well. Thank you much appreciate for that! Jim

Bruce_L
 Posted : Friday, March 13, 2015 5:01:28 PM  Worden Trainer

Joined: 10/7/2004
Posts: 65,138

A plain RSI9 without Wilder's smoothing is just the following.

RSI9

But an RSI - Wilder's indicator with Wilder's smoothing is a lot longer.

50 * (C - XAVGC17.1) / 9 / (.11112122 * (ABS(C - C1) + 8 / 9 * (ABS(C1 - C2) + 8 / 9 * (ABS(C2 - C3) + 8 / 9 * (ABS(C3 - C4) + 8 / 9 * (ABS(C4 - C5) + 8 / 9 * (ABS(C5 - C6) + 8 / 9 * (ABS(C6 - C7) + 8 / 9 * (ABS(C7 - C8) + 8 / 9 * (ABS(C8 - C9) + 8 / 9 * (ABS(C9 - C10) + 8 / 9 * (ABS(C10 - C11) + 8 / 9 * (ABS(C11 - C12) + 8 / 9 * (ABS(C12 - C13) + 8 / 9 * (ABS(C13 - C14) + 8 / 9 * (ABS(C14 - C15) + 8 / 9 * (ABS(C15 - C16) + 8 / 9 * (ABS(C16 - C17) + 8 / 9 * (ABS(C17 - C18) + 8 / 9 * (ABS(C18 - C19) + 8 / 9 * (ABS(C19 - C20) + 8 / 9 * (ABS(C20 - C21) + 8 / 9 * (ABS(C21 - C22) + 8 / 9 * (ABS(C22 - C23) + 8 / 9 * (ABS(C23 - C24) + 8 / 9 * (ABS(C24 - C25) + 8 / 9 * (ABS(C25 - C26) + 8 / 9 * (ABS(C26 - C27) + 8 / 9 * (ABS(C27 - C28) + 8 / 9 * (ABS(C28 - C29) + 8 / 9 * (ABS(C29 - C30) + 8 / 9 * (ABS(C30 - C31) + 8 / 9 * (ABS(C31 - C32) + 8 / 9 * (ABS(C32 - C33) + 8 / 9 * (ABS(C33 - C34) + 8 / 9 * (ABS(C34 - C35) + 8 / 9 * (ABS(C35 - C36) + 8 / 9 * (ABS(C36 - C37) + 8 / 9 * (ABS(C37 - C38) + 8 / 9 * (ABS(C38 - C39) + 8 / 9 * (ABS(C39 - C40) + 8 / 9 * (ABS(C40 - C41) + 8 / 9 * (ABS(C41 - C42) + 8 / 9 * (ABS(C42 - C43) + 8 / 9 * (ABS(C43 - C44) + 8 / 9 * (ABS(C44 - C45) + 8 / 9 * (ABS(C45 - C46) + 8 / 9 * (ABS(C46 - C47) + 8 / 9 * (ABS(C47 - C48) + 8 / 9 * (ABS(C48 - C49) + 8 / 9 * (ABS(C49 - C50) + 8 / 9 * (ABS(C50 - C51) + 8 / 9 * (ABS(C51 - C52) + 8 / 9 * (ABS(C52 - C53) + 8 / 9 * (ABS(C53 - C54) + 8 / 9 * (ABS(C54 - C55) + 8 / 9 * (ABS(C55 - C56) + 8 / 9 * (ABS(C56 - C57) + 8 / 9 * (ABS(C57 - C58) + 8 / 9 * (ABS(C58 - C59) + 8 / 9 * (ABS(C59 - C60) + 8 / 9 * (ABS(C60 - C61) + 8 / 9 * (ABS(C61 - C62) + 8 / 9 * (ABS(C62 - C63) + 8 / 9 * (ABS(C63 - C64) + 8 / 9 * (ABS(C64 - C65) + 8 / 9 * (ABS(C65 - C66) + 8 / 9 * (ABS(C66 - C67) + 8 / 9 * (ABS(C67 - C68) + 8 / 9 * (ABS(C68 - C69) + 8 / 9 * (ABS(C69 - C70) + 8 / 9 * (ABS(C70 - C71) + 8 / 9 * (ABS(C71 - C72) + 8 / 9 * (ABS(C72 - C73) + 8 / 9 * (ABS(C73 - C74) + 8 / 9 * (ABS(C74 - C75) + 8 / 9 * (ABS(C75 - C76) + 8 / 9 * (ABS(C76 - C77) + 8 / 9 * (ABS(C77 - C78) + 8 / 9 * (ABS(C78 - C79))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) + 50

So we can calculate an 18 period linear regression slope of a the plain RSI9.

(8.5 * RSI9.1. + 7.5 * RSI9.1.1 + 6.5 * RSI9.1.2 + 5.5 * RSI9.1.3 + 4.5 * RSI9.1.4 + 3.5 * RSI9.1.5 + 2.5 * RSI9.1.6 + 1.5 * RSI9.1.7 + .5 * RSI9.1.8 - .5 * RSI9.1.9 - 1.5 * RSI9.1.10 - 2.5 * RSI9.1.11 - 3.5 * RSI9.1.12 - 4.5 * RSI9.1.13 - 5.5 * RSI9.1.14 - 6.5 * RSI9.1.15 - 7.5 * RSI9.1.16 - 8.5 * RSI9.1.17) / 484.5

And an 18 period moving linear regression of the plain RSI9.

RSI9.18 + 8.5 * (8.5 * RSI9.1. + 7.5 * RSI9.1.1 + 6.5 * RSI9.1.2 + 5.5 * RSI9.1.3 + 4.5 * RSI9.1.4 + 3.5 * RSI9.1.5 + 2.5 * RSI9.1.6 + 1.5 * RSI9.1.7 + 0.5 * RSI9.1.8 - 0.5 * RSI9.1.9 - 1.5 * RSI9.1.10 - 2.5 * RSI9.1.11 - 3.5 * RSI9.1.12 - 4.5 * RSI9.1.13 - 5.5 * RSI9.1.14 - 6.5 * RSI9.1.15 - 7.5 * RSI9.1.16 - 8.5 * RSI9.1.17) / 484.5

But we can't create similar linear regression based Indicator Formulas based on the Wilder's smoothed version of RSI.

A 9-period exponential moving average of a plain RSI9 is just the following.

XAVG(RSI9,9)

We can't do anything similar for the Wilder's smoothed version.

If you want an example of the construction of manually expanded 9-period exponential moving average, it could be done as follows for price (this can also be written as just XAVGC9 on its own without expansion).

.20019827 * (C + .8 * (C1 + .8 * (C2 + .8 * (C3 + .8 * (C4 + .8 * (C5 + .8 * (C6 + .8 * (C7 + .8 * (C8 + .8 * (C9 + .8 * (C10 + .8 * (C11 + .8 * (C12 + .8 * (C13 + .8 * (C14 + .8 * (C15 + .8 * (C16 + .8 * (C17 + .8 * (C18 + .8 * (C19 + .8 * (C20 + .8 * (C21 + .8 * (C22 + .8 * (C23 + .8 * (C24 + .8 * (C25 + .8 * (C26 + .8 * (C27 + .8 * (C28 + .8 * (C29 + .8 * (C30)))))))))))))))))))))))))))))))

You basically need to replace C with the properly formatted bars ago version of whatever you trying to average if you need to use an expansion.

The Monday, September 05, 2005 9:32:18 AM ET post by bustermu explores why the works in a more generalized way.

You don't need to do manual expansions if there is a single argument (such as just RSI9 on its own) to be averaged. In such cases, you can use the XAVG() function.

-Bruce
Personal Criteria Formulas
TC2000 Support Articles
 Posted : Friday, March 13, 2015 5:34:27 PM Gold Customer

Joined: 5/9/2011
Posts: 80

Thanks Bruce...very much appreciate all that you do- Jim

 Posted : Friday, September 18, 2015 12:10:03 AM
Registered User
Joined: 1/3/2014
Posts: 36