PureBytes Links
Trading Reference Links
|
Generally, lag is defined as the number of bars the response lags
when applied to a constantly increasing ramp function. You can
measure it with the System code below.
(This is written as a trading system to allow you to use the
optimization feature of TradeStation to cycle through various values
of Length.)
The ELA is attached.
{ *******************************************************************
Study : LagMeasure
Last Edit : 8/21/2001
Provided By : Bob Fulks
Description : Measures the Lag of a moving average by applying a
ramp function and measuring the lag after 1000 bars. Apply to
any chart with at least 1000 bars.
You can run an optimization on Length from 5 to 50, for example,
and it will print the Length and Lag for each iteration.
********************************************************************}
Input: Length(25), Slope(1);
Vars: Price(0), Ave(0), Lag(0), Measure(TRUE);
Price = Slope * CurrentBar;
Ave = XAverage(Slope * CurrentBar, Length); {use average here}
if Price >= 1000 and Measure then begin
Lag = (Price - Ave) / Slope;
Print("Length = ", Length:5:2, " Lag = ", Lag:5:2);
Measure = FALSE;
end;
-----------------------------------------------------
This works for all "linear" moving averages. For adaptive moving
averages, the measurement is more complicated and depends upon the
price waveform.
For many moving averages (which are really "Finite Impulse Response"
[FIR] Digital Filters) with symmetrical coefficients such as the
simple moving average, the lag is given by:
Lag = (Length - 1) / 2
The lag of most moving averages is easy to determine by applying a
ramp function to the equations of the average.
By convention the "Length" input of an Exponential Moving Average is
determined so that the lag of that average is also:
Lag = (Length - 1) / 2
It is easy to derive this from applying the equations for the
Exponential Moving Average to a ramp function as follows. (Remember
your algebra?)
The exponential MA is defined as:
F0 = a * P0 + (1 - a) * F1
where
F0 is the filter output for the current bar
F1 is the filter output for the previous bar, etc.
P0 is the price of this bar
P1 is the price of the previous bar, etc.
a is the filter factor
Assume a ramp function such that
P0 = P1 + S (where S is the slope of the ramp)
We know the output lags so
F0 = F1 + S
and the lag is the difference between the price and the filter
output, divided by the slope:
Lag = (P0 - F0) / S
Substituting this in the above equations you can derive the lag as:
Lag = (1 - a) / a
The lag of a simple MA is
Lag = (Length - 1) / 2
so if we want the lag of the Exponential Moving Average to be the
same for the same length, then the following must be true:
Length = (2 - a) / a
or, solving for "a":
a = 2 / (Length + 1)
which is the equation used inside the Exponential Moving Average function.
Bob Fulks
At 11:42 AM -0600 8/21/01, sptrader wrote:
>I'm doing some research on cycles and wanted to compare "LAGS" of
>different types of averages. Is it possible (or does anyone have
>ela,els code to measure it?) see attached -the yellow line is a 20
>per sma, the cyan is a 5 per sma. The 20 per lags by about 9 bars -
>is it possible to count the number of bars of lag bar by bar using
>ela or els ?
>
>(I think you would use the lagging average as a reference price and
>count back on the fast average(5per) until it meets or exceeds the
>lagging price. I really don't care how it's measured as long as it's
>relatively accurate bar by bar.)
Attachment:
Description: "LAGMEAS.ELA"
Attachment:
Description: "%LAGMEAS.ELA"
|