[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: returning value of previous calc?


  • To: gary@xxxxxxxxxxxx (Gary Funck)
  • Subject: Re: returning value of previous calc?
  • From: Bob Fulks <bfulks@xxxxxxxxxxxx>
  • Date: Thu, 26 Feb 1998 09:10:34 -0800 (PST)
  • In-reply-to: <eadamy@xxxxxxxxxx>

PureBytes Links

Trading Reference Links

At 9:14 PM -0800 2/25/98, Gary Funck wrote:

>Here's the example, that was described in the Sept/Oct 1996 issue
>of TS Express, p. 5 (copyright Inside Edge Systems).  (The article
>gave a thorough coverage of the use (and misuse) of simple/series
>functions).  I've cut the example down, to show the use of
>the previous value of a variable declared inside the function.  I
>actually haven't checked this method out, but assume that it works:
>
>(
>User function: Xavg
>Property: Simple
>}
>Inputs: Price(numericseries), Length(numericsimple);
>Var: Factor(0), Return(0);
>{ .... initialize Factor .... }
>Return = Factor*Price+(1-Factor)*Return;
>Xavg = Return;
>
>For this to work, Xavg has to be called on each bar, and I'd
>guess there's a problem if you call it with more than one
>numericseries in the same chart.
>

What this says is that:
  <New value of Return> becomes equal to
     Factor * Price + (1 - Factor) * <old value of return>

Obviously, you can only do this is you no longer need the old value of return.

You can also explicitely use extra variables to save past values to avoid
using the references to previous values.

For example, if your series function used Result, Result[1], and Result[2]
you could define additional variables to save these in simple functions.

  vars: Result1(0), Result2(0);

  Result2 = Result1;
  Result1 = Result;

  <new calculation of Result>

As you said, the code would have to be called on each bar to be updated
correctly.

Attached below are two versions of the T3 filter I posted in December, both
a simple and series version so you can see the differences.

Bob Fulks

----------------



I did some additional testing of the T3 average I posted last Sunday.

(The MetaStock code for this average was published in the January issue of
Technical Analysis of Stocks & Commodities in the article "Smoothing
Techniques for More Accurate Signals", by Tim Tillson.)

I recoded the function for better efficiency and created both a "series"
version and a "simple" version. The "simple" version is coded to allow
dynamically varying the "Periods" input on a bar-by-bar basis, should this
be needed. I have included a demo indicator that shows this input being
modulated by volatility as an example of a possible usage.

The frequency response of the average as a low-pass filter is very good.
The cutoff frequency is approximately:

    Cut off frequency (cycles/year) = 100 / "Periods"
       if "Periods" is given in daily bars.

The frequency response drops quickly and is down by a factor of 100 at
about 7 times the cutoff frequency which is a pretty steep cutoff. The
response is very smooth for the small lag created.

Included below are the two functions, an indicator using the series
function, and a demo indicator illustrating the modulation of the "Periods"
input.

Bob Fulks

{ *******************************************************************

  Function    : T3Average.series

  Last Edit   : 12/16/97

  Provided By : Bob Fulks

  Description : This function is an EasyLanguage version of the
     moving average described in the January. 1998 issue of TASC,
     p57, "Smoothing Techniques for More Accurate Signals", by Tim
     Tillson. It is translated from the MetaStock code presented
     in the article and recoded for efficiency.

     The variable, "Hot", is a damping coefficient which is set to
     the suggested default value of 0.7. The variable "b" is
     substituted for the variable, "a" used in the article since
     "a" is a reserved word. The variables e1 through e6 calculate
     the exponential moving averages in-line rather than calling
     other functions.

     The resulting indicator plotting this function appears to
     duplicate the results shown in Figure 4 of the article.

     The series version of this function uses previous values
     and, hence, cannot call variables.

     The "Periods" input can need not be an integer.

********************************************************************}

Inputs:     Price(NumericSeries), Periods(NumericSimple);

Variables:  b(0), b2(0), b3(0), e1(Price), e2(Price), e3(Price),
            e4(Price), e5(Price), e6(Price), c1(0), c2(0), c3(0),
            c4(0), f1(0), f2(0), Hot(0.7);

if Periods + 1 <> 0 then begin

  if CurrentBar <= 1 then begin

     b  = Hot;
     b2 = b * b;
     b3 = b * b * b;
     c1   = -b3;
     c2 = 3 * b2 + 3 * b3;
     c3 = -6 * b2 - 3 * b - 3 * b3;
     c4 = 1 + 3 * b + b3 + 3 * b2;
     f1 = 2 / (Periods + 1);
     f2 = 1 - f1;

  end else begin

     e1 = f1 * Price + f2 * e1[1];
     e2 = f1 * e1 + f2 * e2[1];
     e3 = f1 * e2 + f2 * e3[1];
     e4 = f1 * e3 + f2 * e4[1];
     e5 = f1 * e4 + f2 * e5[1];
     e6 = f1 * e5 + f2 * e6[1];

  end;

  T3Average.series = c1 * e6 + c2 * e5 + c3 * e4 + c4 * e3;

end;


{ *******************************************************************

  Function    : T3Average.simple

  Last Edit   : 12/16/97

  Provided By : Bob Fulks

  Description : This function is an EasyLanguage version of the
     moving average described in the January. 1998 issue of TASC,
     p57, "Smoothing Techniques for More Accurate Signals", by Tim
     Tillson. It is translated from the MetaStock code presented
     in the article and recoded for efficiency and to allow
     variable inputs.

     The variable, "Hot", is a damping coefficient which is set to
     the suggested default value of 0.7. The variable "b" is
     substituted for the variable, "a" used in the article since
     "a" is a reserved word. The variables e1 through e6 calculate
     the exponential moving averages in-line rather than calling
     other functions.

     The resulting indicator plotting this function appears to
     duplicate the results shown in Figure 4 of the article.

     The "simple" version of this function must be called on each
     bar. It should not be put inside a conditional statement where
     it is only evaluated on some bars.

     The inputs can be variables or arrays. The "Periods" input can
     be changed dynamically from bar to bar, if necessary, and need
     not be an integer.

********************************************************************}
Inputs:     Price(NumericSeries), Periods(NumericSimple);

Variables:  b(0), b2(0), b3(0), e1(Price), e2(Price), e3(Price),
            e4(Price), e5(Price), e6(Price), c1(0), c2(0), c3(0),
            c4(0), f1(0), f2(0), Hot(0.7);

if Periods + 1 <> 0 then begin

  if CurrentBar <= 1 then begin

     b  = Hot;
     b2 = b * b;
     b3 = b * b * b;
     c1   = -b3;
     c2 = 3 * b2 + 3 * b3;
     c3 = -6 * b2 - 3 * b - 3 * b3;
     c4 = 1 + 3 * b + b3 + 3 * b2;

  end else begin

     f1 = 2 / (Periods + 1);
     f2 = 1 - f1;

     e1 = f1 * Price + f2 * e1;
     e2 = f1 * e1 + f2 * e2;
     e3 = f1 * e2 + f2 * e3;
     e4 = f1 * e3 + f2 * e4;
     e5 = f1 * e4 + f2 * e5;
     e6 = f1 * e5 + f2 * e6;

  end;

  T3Average.simple = c1 * e6 + c2 * e5 + c3 * e4 + c4 * e3;

end;