0% found this document useful (0 votes)
270 views6 pages

John Ehlers - Non Linear Filters

Uploaded by

Robert Petkov
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
270 views6 pages

John Ehlers - Non Linear Filters

Uploaded by

Robert Petkov
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Stocks & Commodities V. 19:4 (25-34): Nonlinear Ehlers Filters by John F.

Ehlers
TRADING TECHNIQUES

What If Linear Filters Won’t Work For Your Tradable?

Nonlinear Ehlers Filters


Linear filters like moving averages
are great for slow, “stationary” data.
Unfortunately, prices aren’t slow or
stationary.

he most common filters


that traders use are

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

stationary process is the classic coin-


flip experiment; the nature of the
coin flip never changes. However, if
weighted coins were randomly
introduced into the experiment, the statistics of the described statistically. For example, human speech
experiment would then depend on which coin is used, has noise-like statistics. The speech process is
and therefore the results would become nonstationary. nonstationary because it changes from moment to
The signals you deal with every day often can be moment. Even though speech has noise-like
characteristics, it obviously carries information.
Price data resembles speech in statistical
characteristics; it is both noise-like and nonstationary.
by John F. Ehlers One of the main problems you encounter in trading
when using technical analysis is that you must attempt
Copyright (c) Technical Analysis Inc.
Stocks & Commodities V. 19:4 (25-34): Nonlinear Ehlers Filters by John F. Ehlers

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

[3 3 8 9 9] This weighted moving average has each


coefficient normalized to the sum of the
coefficients — that is, 4 + 3 + 2 + 1 = 10
and 10/10 = 1. This normalization keeps
the average’s scale the same as the price
on your charts. But how would you
normalize a momentum? If you were
interested in a five-bar momentum, each
coefficient, c, would be:

Pricethis bar – Price5 bars back

The answer is to normalize all of the


coefficients to their sum, just as you did
for the weighted moving average.

FIGURE 1: RESPONSE TIME. A median


filter does well in ranges but is slow to react
to dramatic price changes.

Copyright (c) Technical Analysis Inc.


Stocks & Commodities V. 19:4 (25-34): Nonlinear Ehlers Filters by John F. Ehlers

TWO TYPES OF FILTERS


Value EMA Computation
There are two basic kinds of filters: finite impulse response 1 None. Period 1
(FIR) filters and infinite impulse response (IIR) filters. An FIR 2 1.250 (0.25 * 2) + (0.75 * 1)
is referred to as finite because it only responds to prices within 3 1.687 (0.25 * 3) + (0.75 * 1.250)
the period (the “window”) of the filter (usually, an average of 4 2.266 (0.25 * 4) + (0.75 * 1.687)
some kind). An IIR, such as an exponential moving average, 5 2.949 (0.25 * 5) + (0.75 * 2.266)
retains data through its averaging process from all periods of 6 3.712 (0.25 * 6) + (0.75 * 2.949)
its calculation. Theoretically, at least, that could be an infinite 7 4.534 (0.25 * 7) + (0.75 * 3.712)
amount of data. 8 5.400 (0.25 * 8) + (0.75 * 4.534)
For example, say you have 9 sequential values: 1,2,3,. . .9. 9 6.300 (0.25 * 9) + (0.75 * 5.400)
On the last period, the seven-period simple average will be
(3+4+5+ 6+7+8+9)/7 = 6. The only values that went into the When a price is applied to an IIR filter such as an exponential
calculation were those in the filter’s window: the last seven moving average, a portion of the first output is fed back to the
numbers in the sequence. input and added to the next data input sample. Because this
An exponential moving average (EMA) of equivalent length calculation is iterative, the effects of the impulse are theoreti-
would be formulated as cally present in the output indefinitely — hence the name
“inifinite impulse response.”
EMA = 0.25*price + (1-0.25) *EMA When the impulse is applied to the input of an FIR filter — an
today today previous SMA, for example — the effects of the impulse will be present in
or, more generally, the filter output only over the length of the filter, and the output will
be zero otherwise — hence the name “finite impulse response.”
Perry Kaufman, Tushar Chande, as well as others (see
EMA = alpha*price + (1-alpha) *EMA
today today previous sidebar “Adaptive averages”) have designed nonlinear IIR
filters to better smooth market data. Their basic approach is to
Every time you calculate the EMA, you use its previous value, craft a volatility-adjusted value for the alpha parameter in an
thus, in effect, retaining some portion of the previous prices. EMA. These are certainly workable approaches, but their
Here’s what would happen: effectiveness is constrained by the opposing requirements of
providing adequate smoothing while vigorously attacking ma-
jor price movements. —J.F.E.

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

Copyright (c) Technical Analysis Inc.


Stocks & Commodities V. 19:4 (25-34): Nonlinear Ehlers Filters by John F. Ehlers

other hand, more arcane parameters such


as signal-to-noise ratio can be used as the
statistic. The coefficients where the signal-
to-noise ratio is the greatest would have the
largest weight, discounting the price data
values where the signal-to-noise ratio is
less. In addition, my nonlinear filters can
be adaptive. The length of the five-bar
momentum filter in our example could be
made adaptive to the length of the measured
cycle period. Such a filter would be both
adaptive and nonlinear.
For example, in Figure 3, the statistic
used is the difference between the current
price and the previously calculated value
of the filter. This has some aspects not of a
finite impulse response filter, but an infinite
one.
FIGURE 2: PERFORMANCE. A momentum plopped into a nonlinear filter responds well to sharp
movements without a great deal of dithering in ranges.

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.

The “length” for any data


sample is the square root of
the sum of the squares of the
price difference between
that price and each of the
prices back for the length of
the filter window.

Copyright (c) Technical Analysis Inc.


Stocks & Commodities V. 19:4 (25-34): Nonlinear Ehlers Filters by John F. Ehlers

ADAPTIVE AVERAGES Slowest = 2/(30+1) = 0.0645


Kaufman’s adaptive moving average (KAMA)
Perry J. Kaufman’s adaptive moving average (KAMA) is Simplifying the equation for the smoothing factor, we get:
based on the concept that a noisy market requires a slower
trend than one with less noise. The basic principle is that the S = (0.6022*E + 0.0645)2
trendline must lag further behind the price in a relatively noisy
market to avoid being penetrated by the price. The moving The efficiency ratio (E) is the absolute value of the differ-
average can speed up when the prices move consistently in ence of price across the calculation span divided by the sum
one direction. of the absolute value of the individual price differences across
According to Kaufman, who invented the system, KAMA is the calculation span. The equation for E is:
intended to use the fastest trend possible, based on the Σ Price –Price N
smallest calculation period for the existing market conditions. E= N
It does so by changing the alpha of the exponential moving Σ Price i –Price i + 1
average with each new sample. The equation for KAMA is: i=0

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:

S = (E*(fastest – slowest) + slowest)2 VIDYA = 0.2*k*Close + (1 - .2*k)*VIDYA[1]

“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.

that the edge becomes more rounded.


If you consider the gray shading levels
in Figure 5 as distances from the centerline,
you can compute filter coefficients in terms
of edge sharpness. White is the maximum
distance in one direction from the median
and black is the maximum distance in the
other direction. Thus, distance is a measure
of departure from the edge, taking into
account the edge sharpness.
Now, thinking of price charts, imagine
the difference in prices as a distance, but in
two dimensions. The “length” for any data
sample is the square root of the sum of the
squares of the price difference between
that price and each of the prices back for
the length of the filter window. The sum of
the distances squared at each data point are
the coefficients of the nonlinear filter. (I
use the sum of the squares rather than the FIGURE 5: HIGHLY RESPONSIVE. Coefficients are computed as the sum of the squares of price
square root of the sum of the squares to differences across the filter span.
heighten the filter response.)
Suppose the last 10 data values were

[1 1 1 1 1 1 2 3 4 5]

Copyright (c) Technical Analysis Inc.


Stocks & Commodities V. 19:4 (25-34): Nonlinear Ehlers Filters by John F. Ehlers

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

Copyright (c) Technical Analysis Inc.

You might also like