John Ehlers - Non Linear Filters
John Ehlers - Non Linear Filters
Ehlers
TRADING TECHNIQUES
T moving averages —
either simple moving
averages (S MA ) or
exponential moving
averages (EMA). These
are linear filters. Linear filters are
best for smoothing stationary, slowly
varying signals that are corrupted
with high-frequency noise. In this
instance, “stationary” means that the
rules that dictate the underlying
process do not change and remain
stable; the underlying process that
generates prices doesn’t change.
Unfortunately, price data is not
stationary most of the time.
An example of a statistically
AMY VANSGARD
to restore signals that often are nonstationary and also corrupted the median value would be 8, as opposed to the average
by noise. When dealing with nonstationary signals with sharp value of 6.4.
transitions or when dealing with impulsive noise, linear filtering A nonlinear, median filter tends to smooth out short-term
techniques give poor results. variations that lead to whipsaw trades using linear filters. On
the other hand, the lag of a median filter in response to a sharp
NONLINEAR FILTERS and sustained price movement can be substantial; it is by
The filters I have devised are nonlinear finite impulse response necessity about half the filter window width. The response of
(FIR) filters. (See sidebar “Two types of filters.”) These filters a median filter with a 10-bar window width can be seen in
provide extraordinary smoothing in sideways markets and Figure 1. Note that the filter did not respond to small price
aggressively follow major price movements with minimal lag. movements in October/November, nor in January/February.
The development of my filters started with a general class of That eliminated several potential whipsaw trades that would
FIR filters called order statistic (OS) filters. In contrast to linear have been produced by linear filters.
filters, where sensitivity to time is necessary, OS filters are
based on the ranking of the samples within the filter window. COMPUTATION
The OS filter ranking is based on summary statistics, such as Finding the median value is a simple sorting problem. You only
mean or variance, rather than by position in time. need to list the data samples within the filter width in order of
Among OS filters, the median filter is the best known. their sizes and pick the value that has an equal number of values
Median filters are used in video circuits to sharpen the edges of above and below it. If there is no center value, you can compute
images and to remove impulsive noise. In a median filter, the the average of the two middle values. Median filters typically
output is the median value of all the values within the observation have high-frequency jiggles that can be smoothed by taking
window. As opposed to an averaging filter, which stores all the a subsequent exponential moving average of the median
data in its “output” or average, the median filter discards all data filter data.
except the median value. In this way, the median filter eliminates My filter has a formulation similar to that of a finite impulse
impulsive noise spikes and extreme price data rather than response (FIR) filter. If y is the filter’s output or result and xi is
include either in the average. the ith input across a filter window width n, then the equation is:
Time-sensitive characteristics of the median filter are lost
because the median value can fall at any time. For example, if y = c1x1 + c2x2 + c3x3 + c4x4 + . . . . . .+ cnxn
the data inputs to a five-bar FIR filter are, sequentially,
The c is the coefficient that contains the statistic in which you
[3 4 3 3 9] are interested — momentum, relative strength index (RSI),
stochastic, you name it. As an example, a classic front-weighted
the median value is 4. This median is neither the average value moving average might use:
(which is 4.4) nor the value at the center of the filter (which is
3). In this case, the big data spike value of 9 is ignored. As Y = (4 * Pricetoday + 3 * Priceyesterday + 2 * Pricetwo days ago + 1 * Pricethree days ago)/10
another example, if the data inputs were
Therefore, the complete formal description of the nonlinear example. In this latter case, there is only a small percentage
filter is the summation of the product of the filter coefficient difference between the coefficients and the filter would
and the price at each sample divided by the summation of the have performance virtually indistinguishable from a simple
coefficients as: moving average.
The EasyLanguage code and an Excel model for the nonlinear
n–1
filter are given in Traders’ Tips in this issue for the particular
Σ c i xi example of a five-bar momentum.
y = i n= –0 1
Figure 2 illustrates the fact that the momentum-derived
Σ ci
i=0 nonlinear filter — the red line — responds quickly to rapid
price movements while rejecting minor price movements to a
greater degree. (The blue line is a comparable moving average.)
The statistic used in the nonlinear filters should be detrended This kind of filter can be used to respond quickly to changes in
for maximum effectiveness. If you do not detrend the statistic, trend direction without producing the whipsaws that are so
each of the coefficients may have a large common term relative prevalent when linear filters are employed. This nonlinear,
to any differences between them. For example, if a five-bar momentum-based filter can be made to be very aggressive by
filter has a statistic with values such as squaring each coefficient.
[-1 -2 0 2 1]
OTHER IDEAS
the statistic has no common term, and there is a large percentage The flexibility of my nonlinear
change between positions within the filter. However, if the filters — you can insert just about
statistic has values such as any analytic construct into one —
opens up new avenues of technical
[99 98 100 102 101] analysis research. For example, the
statistic can be some tangible
the coefficients have a common term of 100, although the parameter of market activity such
difference between coefficients is the same as in the first as money flow or volume. On the
T HINKING ABOUT
THE FIR FILTER
Why derive the finite
impulse response filter
type at all? By so doing,
you may discover an
optimum solution for the
coefficient calculation.
We know market data is most often
nonstationary. We also know that you want
to follow the sharp and sustained
movements of price as closely as possible.
Just this desire led video engineers to devise
the median filter as an edge detector, to
clarify video images. But not all edges are
the same.
You can visualize the sharpness of edges FIGURE 3: INCORPORATING A DIFFERENCE. This version of the nonlinear filter uses the
difference between the current price and the previous value of the filter.
by imagining Figure 4 as a piece of paper
draped over the edge of a table. The edge at
the top of Figure 4 is very sharp, as if the
paper were creased. As you continue to FIGURE 4: VIDEO. Just as traders
must discern the difference between
look down at Figure 4, the light diffusion noise and trend inception, video
becomes more dispersed, giving the illusion engineers must discern the differ-
ence between gray and white in
video images.
KAMA = S*Price + (1 – S)*KAMA[1] The default value for N is 10. However, testing to find the
best length is advisable.
Where S = Smoothing factor
Chande’s variable index dynamic average (VIDYA)
This is the same equation that is used for the EMA, except the Tushar Chande’s variable index dynamic average (VIDYA)
variable S replaces the alpha constant of the EMA. uses a fixed pivotal smoothing constant. The suggested
The equation for the smoothing factor involves two bound- value of this constant is 0.2, corresponding to the alpha of a
aries and an efficiency ratio: nine-day EMA. The equation for VIDYA is:
“Fastest” means the alpha of the shortest period used. Again, this is the same equation as for an EMA, except the
“Slowest” means the alpha of the longest period used. The relative volatility term, k, has been included to introduce the
suggested period boundaries are two and 30 bars. In this nonlinearity. The volatility term is the ratio of the standard
case, the two alphas are: deviation of closes over the last n days to the standard
deviation of closes over the last m days, where m is greater
Fastest = 2/(2+1) = 0.6667 than n. Suggested values are n = 9 and m = 30.
[1 1 1 1 1 1 2 3 4 5]
The coefficients of a nonlinear filter, at the end of the last Like order statistic (OS) filters, nonlinear filters are robust.
period, would then be calculated as: Moreover, they also exploit both the rank-order and the time-
sensitive characteristics of the data. The generalized nonlinear
C1 = (5-4)2 + (5-3)2 + (5-2)2 + (5-1) 2 + (5-1) 2 filter can be oriented to any statistic you choose, making the
= 1 + 4 + 9 + 16 + 16 = 46 coefficients easy to calculate. The most obvious statistic to use
C2 = (4-3)2 + (4-2)2 + (4-2) 2 + (4-1) 2 + (4-1) 2 is price momentum, because this data enables the nonlinear
= 1 + 4 + 9 + 9 + 9 = 32
filter to rapidly follow price changes.
C3 = (3 - 2)2 + (3-1) 2 + (3-1) 2 + (3-1) 2 + (3-1) 2
= 1 + 4 + 4 + 4 + 4 = 17
The choice of statistic used is virtually limitless. For example,
C4 = (2 –1)2 +(2 –1)2 + (2 –1)2+ (2 –1)2+ (2 –1)2 the filter could be nonlinear with respect to acceleration (the
= 1 + 1 +1 + 1 + 1 = 5 rate change of momentum), signal-to-noise ratio, volume,
C5 = (1 – 1)2 +(1 – 1)2+(1 – 1)2+(1 – 1)2+(1 – 1)2 money flow (delta price times volume), and so on. Even other
=0+0+0+0+0=0 indicators such as stochastic or RSI can be used as a statistic.
The calculation of the distance-like coefficients is shown in John Ehlers is president of MESA Software and a frequent
the EasyLanguage code and Excel model for the filter in contributor to STOCKS & COMMODITIES. This article was
Traders’ Tips’ “EasyLanguage code for the distance coefficient adapted from Rocket Science For Traders, published by John
of a nonlinear filter.” If the difference of prices across the filter Wiley & Sons in 2001.
observation window are the same, then the coefficients of the
filter are all the same and you have the equivalent of a simple RELATED READING
moving average (SMA). On the other hand, if the prices shift Chande, Tushar [1997]. Beyond Technical Analysis: How To
rapidly, the distances from the increased price points increase, Develop And Implement A Winning Trading System, John
and higher weights are given to these filter coefficients. The Wiley & Sons.
performance of the distance coefficient nonlinear filter can be _____, and Stanley Kroll [1994]. The New Technical Trader,
seen in Figure 5. John Wiley & Sons.
The filter coefficients can be made to be even more nonlinear. Ehlers, John F. [2001]. Rocket Science For Traders, John
For example, the distance can be cubed or raised to the fourth Wiley & Sons.
power (by squaring the squared distance). A reciprocal Gaussian _____ [2000]. “Adaptive Trends And Oscillators,” Technical
response is an even more nonlinear function of distance than Analysis of STOCKS & COMMODITIES, Volume 18: May.
you can use to calculate the filter coefficients. These more _____ [2000]. “Phasor Displays,” Technical Analysis of STOCKS
nonlinear responses follow the edges in price movement more & COMMODITIES, Volume 18: December.
aggressively. However, the fact that they are so nonlinear _____ [2000]. “Squelch Those Whipsaws,” Technical Analysis
removes much of the gray area in the response. of STOCKS & COMMODITIES, Volume 18: September.
The most nonlinear calculations produce results that are not Kaufman, Perry J. [1998]. The New Commodity Trading Systems
discernible from median filters. The coefficients become black and Methods, 3d edition, John Wiley & Sons.
and white, so there is very little middle-ground gray area. †See Traders’ Glossary for definition