Exponential moving average versus moving exponential average
EEXPONENTIAL MOVING AVERAGE VERSUSMOVING EXPONENTIAL AVERAGE
FRANK KLINKER
Abstract.
In this note we discuss the mathematical tools to definetrend indicators which are used to describe market trends. We explainthe relation between averages and moving averages on the one hand andthe so called exponential moving average (EMA) on the other hand. Wepresent a lot of examples and give the definition of the most frequentlyused trend indicator, the MACD, and discuss its properties.
Keywords.
Average · Trend indicator · Chart analysis · MACD Introduction
When trends of charts of indices or stocks are investigated then it is com-mon to use so called trend indicators to decide whether to buy, hold orsell. But how are these indicators calculated? The first idea is, to smooththe curve of points given by the closes and extract informations from therelation between the original and the smoothed curve. One assumption onthe smoothing, is that local maxima and minima should not be seen by thesmoothed curve when they are only short living or of low amplitude. An-other assumption is, that the information is achieved from the data aloneand not from its development. Therefore, usual simple methods like inter-polation by polynomials or Bezier curves are not appropriate, if we do notwant to remove single data from our calculations. Another way is to useregression methods. This is not applicable to our problem, too, because thechoice of an adequate regression curve needs the knowledge of the shape ofthe original curve, i.e. the development of the data. For the properties ofinterpolation and regression see for example [10] and [3]. A good way ofsmoothing turns out to consider averages of the original sequence of closes.But what average should we take? In the literature moving averages as wellas exponential averages are taken to be good candidates, see for example[2], [4], or [9]. (cid:66) : Fakult¨at f¨ur Mathematik, TU Dortmund, 44221 Dortmund, Germany. (cid:107) : [email protected]. Semesterber. (2011), no.1, 97-107. DOI 10.1007/s00591-010-0080-8. a r X i v : . [ s t a t . O T ] J a n FRANK KLINKER
In this note we discuss some frequently used objects such as the so calledexponential moving average (EMA), which is not a moving average in amathematically rigorous sense, and compare it to the rigorous moving ex-ponential average (MEA), see Section 4. Before we do so, we give a precisenotion of average and moving average for a real sequence in Sections 2 and 3and discuss examples. In Section 5 we apply the discussion to trend analy-sis by introducing the EMA and the moving average convergence/divergence(MACD). We discuss these objects at a basic example, too.The motivation for this note came from the request to explain special tech-nical trend indicators like the MACD. This request was annexed a lot oftexts from the world wide web. In all of these texts the terms we explainhere in a rigorous way were mixed up or were not used properly. Also in theprofessional literature which explains the handling of trend indicators anddiscusses markets the authors often do not mention the precise mathematicaldefinitions and structures. Of course, this is caused by the author’s intentionto describe the market analysis and the system of trading intelligibly to allreaders, see e.g. [8].With this note we give the interested user the chance to learn about themathematical tools needed to construct trend indicators.2.
Averages
We consider a sequence of real numbers x = ( x k ) k =1 , ,... and a further se-quence α = ( α k ) k =1 , ,... where the latter takes its values in the interval [0 , x with respect to the weights α . Definition 1.
Let x = ( x , x , . . . ) be a real sequence and α = ( α , α , . . . ) asequence taking its values in the interval [0 , α - average of x is definedas the sequence δ = ( δ , δ , . . . ) defined recursively by δ n +1 := (1 − α n ) δ n + α n x n +1 , n ≥ , δ := x . (2.1)If the limit ¯ δ := lim n →∞ δ n exists, then this is called the limit average . Fora finite sequence of length n we identify ¯ δ with δ n . Remark 2.
This recursively defined element can be expanded to δ n = n − (cid:89) s = (cid:96) (1 − α s ) δ (cid:96) + n (cid:88) r = (cid:96) +1 (cid:32) n − (cid:89) s = r (1 − α s ) (cid:33) α r − x r . (2.2)Using this with (cid:96) = 1 and inserting the initial condition δ = x yields δ n = n − (cid:89) s =1 (1 − α s ) x + n (cid:88) r =2 (cid:32) n − (cid:89) s = r (1 − α s ) (cid:33) α r − x r . (2.3) MA VS. MEA 3
Definition 3.
The coefficients ˆ α ( n ) s in δ n = (cid:80) nr =1 ˆ α ( n ) r x r are called the weights of the α -average:ˆ α ( n )1 = n − (cid:89) s =1 (1 − α s ) , ˆ α ( n ) r = α r − n − (cid:89) s = r (1 − α s ) , ≤ r ≤ n . (2.4)In particular, (cid:80) ns =1 ˆ α ( n ) s = 1. Example 4.
Special choices for α yield well known examples:(1) The sequence α s = µ − ν +1 s + µ +1 with 0 ≤ ν ≤ µ yields weightsˆ α ( n )1 = (cid:0) n + ν − n − (cid:1)(cid:0) n + µn − (cid:1) , ˆ α ( n ) r = µ − ν + 1 r + µ (cid:0) n + ν − n − r (cid:1)(cid:0) n + µn − r (cid:1) , ≤ r ≤ n . (a) µ = ν = 0 defines the arithmetic mean with α s = s +1 and ˆ α ( n ) r = n as well as δ n = n n (cid:80) s =1 x s .(b) µ = 1 , ν = 0 defines the weighted arithmetic mean with α s = s +2 and ˆ α ( n ) r = rn ( n +1) as well as δ n = n ( n +1) n (cid:80) s =1 sx s .(2) The sequence α s = α < δ n = (1 − α ) n − x + α n − (cid:88) s =0 (1 − α ) s x n − s , (2.5)the exponential average (EA) of weight α . Its weights are given byˆ α ( n )1 = (1 − α ) n − and ˆ α ( n ) r = α (1 − α ) n − r for r ≥ Remark 5.
There are further classical and important averages which arenot covered by Definition 1. As an example we would like to mention thegeometric mean which is defined by γ n := n √ x x · . . . · x n . The recurrenceformula for the geometric mean is given by γ n = n (cid:112) ( γ n − ) n − · x n . Thisaverage is important in applications, for instance the calculation of ellipticintegrals uses the geometric mean (see for example [6]). Remark 6.
In the weighted arithmetic as well as in the exponential averagethe values of x with higher indices have a bigger influence to the average,due to their higher weights. In particular the bigger α the less the influenceof the lower contributions to the exponential average.Moreover, because the entries in the weight sequence are constant, the ex-ponential average has the advantage that if we go from δ n to δ n +1 we do notneed to know how far we have gone in the sequence x to calculate the newaverage. It is calculated only from the next entry of x and the old average. FRANK KLINKER Moving averages
We modify the discussion from the last section in such a way that we donot consider the whole sequence x up to n to evaluate the average δ n . Aswe saw in Example 4 (1b) and (2) the “older” the value of x the smallerthe weight with which it enters into the average. Therefore we forgetabout the old ones and for fixed N we let only enter the “youngest” ones( x n − N , x n − N +1 , . . . , x n ) to our new average. This property is sometimescalled limited memory. Definition 7.
Let x = ( x , x , . . . ) be an infinite real sequence and N ∈ N a fixed number. Furthermore let α = ( α , . . . , α N ) be a finite sequencetaking its values in the interval [0 , x ( n ) =( x n − N +1 , . . . , x n ) of x of length N we define the associated α -average δ ( n ) ,N =( δ ( n ) ,N , . . . , δ ( n ) ,NN ) as in Definition 1: δ ( n ) ,Nk := (1 − α k − ) δ ( n ) ,Nk − + α k − x n − N + k , ≤ k ≤ N,δ ( n ) ,N := x n − N +1 . (3.1)The N - moving α - average of x is defined by the sequence ∆ N = (∆ N , ∆ N , . . . )with ∆ Nn := δ ( n ) ,NN . (3.2)If it exists, we denote the limit by¯∆ N := lim n →∞ ∆ Nn and call it the limit moving average . Example 8.
We consider the special choices similar to Example 4.(1) In case of the arithmetic average, i.e. α s = s +1 , we get δ ( n ) ,Nk = 1 k (cid:0) x n − N +1 + . . . + x n − N + k (cid:1) , and the N -moving arithmetic average is given by∆ Nn = 1 N (cid:0) x n − N +1 + . . . + x n (cid:1) . (3.3)(2) In case of the weighted arithmetic mean, i.e. α s = s +2 , we get δ ( n ) ,Nk = 2 k ( k + 1) (cid:0) x n − N +1 + 2 x n − N +2 + . . . + kx n − N + k (cid:1) , and the N -moving arithmetic mean is given by∆ Nn = 2 N ( N + 1) (cid:0) x n − N +1 + 2 x n − N +2 + . . . + N x n (cid:1) . (3.4) MA VS. MEA 5 (3) In case of the exponential average of weight α we get δ ( n ) ,Nk = (1 − α ) k − x n − N +1 + α k − (cid:88) s =0 (1 − α ) s x n − N + k − s , and the N -moving exponential average (MEA) is given by∆ Nn = (1 − α ) N − x n − N +1 + α N − (cid:88) s =0 (1 − α ) s x n − s . (3.5) Example 9. (1) In our first example we consider the sequence x with x s = s . Then δ ( n ) ,Nk as well as the N -moving averages ∆ Nn are given interms of δ n by δ ( n ) ,Nk = ( n − N ) + δ k , ∆ Nn = ( n − N ) + δ N with • arithmetic mean δ n = n + 12 • weighted arithmetic mean δ n = 2 n + 13 • exponential average δ n = n − − αα (cid:0) − (1 − α ) n − (cid:1) (2) The second example deals with the sequence defined by x s = s (1 − β ) s .This yields the following averages : • arithmetic mean δ n ≈ n (cid:16) ln 1 β − n + 1 (cid:17) • weighted arithmetic mean δ n = 2(1 − β )(1 − β n +1 ) n ( n + 1) β • exponential average δ n ≈ (1 − α ) n (cid:16) − β − αn + 1 + α ln 1 − αβ − α (cid:17) Here we used the expansionln 11 − x = ∞ (cid:88) k =1 x k k ≈ n (cid:88) k =1 x k k + 1 n + 1 . This example has been corrected compared with the published version.
FRANK KLINKER Comparing MEA and EA
To compare the exponential average (2.5) and the moving exponential av-erage (3.5) and their respective limits, we turn from the finite sequence x ( n ) = ( x , x , . . . , x n ) to the finite sequence y ( n ) = ( y ( n )0 , . . . , y ( n ) n − ) =( x n , x n − , . . . , x ), i.e. y ( n ) k := x n − k . This numbering ensures, that the“youngest” element of the sequence belongs to the lowest index. Then (2.5)and (3.5) are rewritten as δ n = (1 − α ) n − y ( n ) n − + α n − (cid:88) s =0 (1 − α ) s y ( n ) s (2.5’)and ∆ Nn = (1 − α ) N − y ( n ) N − + α N − (cid:88) s =0 (1 − α ) s y ( n ) s (3.5’)for any finite subsequence and look quite similar.Now let x = ( x , x , x , . . . ) be an infinite sequence with finite subsequences x ( n ) and associated inversed sequences y ( n ) . Then the limit exponentialaverage as well as the limit moving exponential average of the sequence x is ¯ δ = α ∞ (cid:88) s =0 (1 − α ) s y s , ¯∆ N = (1 − α ) N − y N − + α N − (cid:88) s =0 (1 − α ) s y s . (4.1)To compare EA and MEA, we calculate the difference¯ δ − ¯∆ N = α ∞ (cid:88) s = N − (1 − α ) s y s − (1 − α ) N − y N − = (1 − α ) N (cid:32) α ∞ (cid:88) s =0 (1 − α ) s y s + N − y N − (cid:33) (4.2) Definition 10.
A sequence x is called admissible, if it does not spreadwidely. More precisely, for N ∈ N we assume α ∞ (cid:88) s =0 (1 − α ) s y N + s ≈ ¯ δ and | ¯ δ − y N | ¯ δ < These expressions seem to be a little formal, because we deal with terms like y s := x ∞− s , i.e. we need to know the whole starting sequence in reverse order. In practicalapplications this is not a problem, because the sequences are given in the way “the firstdata is the youngest one”. MA VS. MEA 7
Proposition 11.
If the initial sequence is admissible then | ¯ δ − ¯∆ N | ¯ δ < (1 − α ) N . (4.3)In particular, formula (4.3) tells us, that for a given sequence – under theassumptions of admissibility – the relative error by replacing the limit EAby the limit MEA only depends on the moving length N . Remark 12.
Sometimes the value which we get by canceling the sum of ¯ δ in (4.1) is taken as an average and the error is estimated with regard to thisfinite sum. The problem is, that this value is not an average in the sense ofDefinition 1, because the weights do not sum up to 1. Nevertheless, underthe assumption of admissibility, the error will be the same.The inversion of the sequence x is also needed, when we want to add newvalues to a given sequence and compare their respective limit averages. Inthis situation, which will be important in the application in the next section,we start with an infinite (inversed) sequence y [ k ] = ( y [ k ]0 , y [ k ]1 , . . . ) at a “day” k . The next day k +1 we add an element to get a new sequence expanding theold one. The new element should be the initial value of our new sequence,and so we call it y [ k +1]0 . The whole sequence y [ k +1] = ( y [ k +1]0 , y [ k +1]1 , . . . ) isthen supplemented by y [ k +1] i := y [ k ] i − for i ≥ y ( n ) and y [ n ] coincide, when we define y [ n +1]0 := x n +1 and y [1] := ( x ). The inversed sequence y of the infinite sequence x can thenbe viewed as the limit sequence of y [ k ] .5. Application to index charts
When we want to discuss the chart of a stock or an index it is necessaryto smooth the curve, given by the closes. This may be done by taking anappropriate average instead of the closes itself. One condition which can betaken as natural is that the “younger” closes are more important than the“older” ones. Therefore, the weighted arithmetic and the exponential aver-age seem to be proper candidates. One obvious advantage of the exponentialaverage is that the sequence α is constant (see Remark 6). More restrictivelywe could demand that the market do not see very old closes at all but onlycloses of a specific amount of periods. This leads us to the moving variantsof the averages, where we always consider a fixed amount of closes. But inthe context of moving averages we lose the nice recurrence formula (2.1), be-cause the initial condition changes every day, see (3.1). Nevertheless, theseaverages have their application in trend analysis, too, see [9].As we saw in (4.3) the relative error we make when we pass from the movingexponential average to its non moving variant is given by (1 − α ) N . This FRANK KLINKER is true, when we make some mild assumptions on the starting sequence x (see Definition 10). A usual way to model the moving length is the choice α = ρN +1 with suitable ρ ∈ R . For instance ρ = 2 yields a relative error lessthan 13 . ρ ≥ .
7. This can beseen by using (1 − ρN +1 ) N ≤ e − ρ . The moving exponential average with thisspecial choice of weight is called N -day EMA in the literature, see e.g. [1],[2]. Definition 13.
The values of an index (or an stock) at the closes of themarket define a sequence of non-negative real numbers c [ k ] = ( c [ k ]0 , c [ k ]1 , . . . )and is called the sequence of closes at day k . The value c [ k ]0 is called theclose at day k . The sequence of closes obey c [ k ] i = c [ k − i − for i ≥
1, such that c [ k ]0 is always the youngest contribution. Definition 14.
The N -day exponential moving average (EMA) at close c [ k ] is defined by the exponential average of weight N +1 of the series c [ k ] andwill be denoted by E Nk ( c ), i.e. E Nk ( c ) := ¯ δ EA (cid:12)(cid:12)(cid:12) α = 2 N +1 ,y = c [ k ] . (5.1)Within the investigation of market trends we use a further EMA, which isbased on a sequence defined by two EMAs of the sequence of closes. Theso constructed EMA is used to produce a trend indicator for the initialsequence of closes. Definition 15.
The
MACD (Moving Average Convergence/Divergence) atthe close c [ k ] associated to the moving lengths N and N is defined as thedifference M N ,N k ( c ) := E N k ( c ) − E N k ( c ) . (5.2)The MACD defines a new sequence m [ k ] with m [ k ]0 := M N ,N k and m [ k ] i := m [ k − i − . We call the close c [ k ] (or the day k ) N -short or N -long if thedifference m [ k ]0 − E N k ( m ) is negative or positive, respectively. Remark 16.
The change from short to long and from long to short may betaken as indicator for “buy” and “sell”, respectively.
Example 17.
Figures 1 and 2 show an application of Definition 14 to thevalues of the MDAX between December 8, 2008 and April 3, 2009. We useda short term EMA ( N = 12) and a minor intermediate EMA ( N = 26) todefine a very short term EMA of the MACD ( N = 9). This is a commoncombination used in market analysis, see [4]. This combination yields twosell and two buy signals. The closes from Example 17 were kindly provided by
EasyTrend24 . MA VS. MEA 9 Concluding remarks
The EMA, as used in Section 5 as well as in the literature, is not a movingaverage in the mathematical sense of Section 3. In fact, it is a usual limitexponential average with a specific weight. For its calculation we do not usea finite subsequence of the closes c of constant length, but all of it. On theone hand, the name is motivated my the special choice of the weight and theerror discussion above, see (4.3). On the other hand a further motivation forthe term moving comes from the fact that c [ k ] is by construction an infinitesubsequence of c [ k +1] , see Definition 13.As noticed in Example 17 and the subsequent remark the MACD and itsEMA are used to define “buy” and “sell” signals. For small N local effectsin c play a major role, so that the incidents buy/sell will appear more often.In particular if the sequence of closes itself is range-bound, then maxima andminima within this region lead to unnecessary signals (compare Example 17with figures 3 and 4 in particular regions day 1 to day 13 and day 64 to day75). It is a question of strategy which moving lengths we take in Definition14 and 15, or if we – in particular for small N – replace the EMA by anothermoving average.Another way to handle the sometimes unnecessary signals in range-boundregions is the introduction of more subtle indicators such as the ADX (Av-erage Directional Movement Index) or the RSI (Relative Strength Index).For the calculation of the ADX – in contrast to the RSI – the data sequencewe used in Section 5 are not sufficient. In addition we need the highest andlowest value within one period. For more details on this see for example[11].The MACD is a trend follower, i.e. the signals always appear after the ex-trema – how far depends on the choice of the parameters. In this sensethe trend followers only describe the direction of the trend of the data butneither describes the strength of the trend nor does it make predictions onthe future development of the data. The first problem may be weakenedby introducing indicators like the ADX, as noticed before. One method togain more information about the future development is the introduction of atrend forecasters, see for example [7]. Usually this is made by using regres-sion curves to extrapolate the closes, e.g. linear, polynomial, logarithmic orexponential regression, see [5] or [3]. Which method is used depends highlyon the data structure. Therefore the calculation of such trend forcasters –in contrast to the trend follower – needs more than the pure data but a goodknowledge of its developing. Figure 1.
Series of closes, c , and its 12-day and 26-day EMA, E ( c ) and E ( c ), for the period 2008-12-08 to 2009-04-03. Figure 2.
MACD, m = M , ( c ), its 9-day EMA, E ( m ), andthe associated signal for the period 2008-12-08 to 2009-04-03. MA VS. MEA 11
Figure 3.
Series of closes, c , and its 7-day and 14-day EMA, E ( c ) and E ( c ), for the period 2008-12-08 to 2009-04-03. Figure 4.
MACD, m = M , ( c ), its 4-day EMA, E ( m ), andthe associated signal for the period 2008-12-08 to 2009-04-03. References [1] Alexander, C.:
Timing Techniques for Commodity Future Markets.
McGraw-Hill,2003.[2] Appel, G.:
Technical Analysis: Power Tools for Active Investors.
FT press, Pren-tice Hall, 2005.[3] H¨ammerlin, G., Hoffmann, K.-H.:
Numerische Mathematik. (4. ed.) Springer-Verlag, Berlin, 1994.[4] Kaufman, P. J.:
A Short Course in Technical Trading.
John Wiley & Sons, Inc,2008.[5] Kaufman, P. J.:
Trading Systems and Methods (3. ed.) John Wiley & Sons, Inc,1998.[6] K¨onigsberger, K.:
Analysis (6. ed.). Springer-Verlag, Berlin, 2004.[7] Mendelsohn, L. B.:
Trend Forecasting with Intermarket Analysis (4. ed.). Market-place Books, 2008.[8] Pohlschr¨oder, S.:
B¨orse vs. Trader.
Colebergh, 2010.[9] Pring, M. J.:
Technical Analysis Explained (4. ed.). McGraw-Hill, 2002.[10] Quarteronio, A., Sacco, R., Saleri, F.:
Numerical Mathematics (4. ed.). Texts inApplied Mathematics 37, Springer Verlag, Berlin, 2007.[11] Wilder, J. W.: