PureBytes Links
Trading Reference Links
|
Mark, thanks for the working implementation. I have a few suggestions,
and some EL questions to go with it.
On Feb 16, 11:54am, MarkBrown@xxxxxxxxxxxxx wrote:
> Subject: Re: Volatility & Predicting Future Range
> Date: 95-10-02 13:49:53 EDT
> From: JRehler
>
>
> (You must name the user function MyVolatility or change the code to a new name)}
I think it is VolatilityX, in your example below.
>
> Inputs : Price(NumericSeries),Length(NumericSimple);
Q: When is it necessary to define Price as NumericSeries, versus
NumericSimple?
> Vars : SumSqr(0),Counter(0),R(0),vari(0),yr_var(0),
> N(1);
>
> If Length <> 0 then Begin
> SumSqr = 0;
> R = 0;
> vari = 0;
> yr_var = 0;
> N = 1;
>
> For counter = 1 to Length begin
> R = (Price[counter] - Price[counter + 1]) / Price[counter + 1];
Above, since you're using Price[counter] and Price[Counter+1], it
looks to me like today's value of VolatilityX will actually be computing
the volatility at _yesterday's_ price. I think it is more consistent
if we calculate volatility in a way that includes today's data sample.
> r = Log(1 + R);
> SumSqr = SumSqr + (r - Log(1 + Average(R,Length) ) ) * (r - Log(1 + Average(R,Length) ) );
> End;
>
> vari = SumSqr / (Length - 1);
> If DataCompression = 2 then N = 252
> Else If DataCompression = 3 then N = 52
> Else If DataCompression = 4 then N = 12;
> yr_var = vari * N;
>
> VolatilityX = SquareRoot(yr_var);
> End
> Else
> VolatilityX = 0;
The calculation for r above can be simplified as:
r = log(Price[counter]/Price[counter+1]);
also, the use of both "r" and "R", I think is confusing.
Above, I think there should be a final multiplcation by 100.0
to convert the value of VolatilityX into an annual percentage, which
is the conventional representation.
I think the "Average" calculation can be factored out of
the std. dev. calculation, above, and simplify the resulting function.
How about the following?
{ Function VolatilityX - statistical volatility }
Inputs : Price(NumericSeries), Length(NumericSimple);
Vars: sum(0), sumsq(0), ix(0), t(1);
if currentbar = 1 then begin
If DataCompression = 2 then t = SquareRoot(252)
Else If DataCompression = 3 then t = SquareRoot(52)
Else If DataCompression = 4 then t = SquareRoot(12);
{ convert fraction to percentage }
t = t * 100;
end;
sum = 0;
sumsq = 0;
For ix = 0 to (Length-1) begin
r = Log(Price[ix] / Price[ix + 1]);
sum = sum + r;
sum_sq += Square(r);
}
VolatilityX = SquareRoot((sumsq - Square(sum) / Length) / (Length - 1)) * t;
I think my original statvol function does the same thing as above,
for datacompression = 2. The only problem it had is that its initial
value doesn't show up until currentbar > Length. Here's a
slight rewrite to fix that, below:
{ Function VolatilityX - statistical volatility }
Inputs : Price(NumericSeries), Length(NumericSimple);
Vars: sum(0), sumsq(0), ix(0), t(1);
if currentbar = 1 then begin
If DataCompression = 2 then t = SquareRoot(252)
Else If DataCompression = 3 then t = SquareRoot(52)
Else If DataCompression = 4 then t = SquareRoot(12);
{ convert fraction to percentage }
t = t * 100;
{
calculate the value of 'r', before currentbar; maxbars back must
be greater than (length+1).
}
For ix = 1 to Length-1 begin
r[ix] = Log(Price[ix] / Price[ix + 1]);
}
end;
r = Log(Price / Price[1]);
VolatilityX = StdDev(r, length) * t;
--
--
| Gary Funck, Intrepid Technology, gary@xxxxxxxxxxxx, (650) 964-8135
|