{_FractalDim
Calculate the approximate Fractal Dimension of a waveform
Copyright (c) 2003 by Alex Matulich, Unicorn Research Corporation
January 2006: changed to calculate on n intervals rather than n-1.
Note: the Hurst exponent H = 2 - D, where D is the fractal dimension.
See the following article (particularly equation 6):
http://www.complexity.org.au/ci/vol05/sevcik/sevcik.html
Cevcik, Carlos, "A procedure to Estimate the Fractal Dimension of
Waveforms," International Complexity, Vol 5, 1998.
Because the lookback length n goes from 0 to n, we also have n
intervals, and not n-1 intervals as described in the paper.
}
Inputs:
y(NumericSeries), {price data, e.g. Close of data1}
n(NumericSimple); {lookback length}
Vars: j(0), lngth(0), ymax(0), ymin(0), yscl(0), dx2(0), dy(0);
ymin = Lowest(y, n); ymax = Highest(y, n); yscl = ymax - ymin;
if n < 2 Or ymax = ymin then
lngth = 1
else begin
{ calculate length of curve }
lngth = 0;
dx2 = Square(1/n);
for j = 1 To n begin
dy = (y[j] - y[j-1]) / yscl;
lngth = lngth + SquareRoot(dx2 + dy*dy);
end;
end;
_FractalDim = 1 + (Log(lngth) + Log(2)) / Log(2*n);
{Note: for large n, the result above converges to:
_FractalDim = 1 + Log(lngth) / Log(2*n);
However, because we typically using small values of n (like n<50)
for market analysis, we use the result with the Log(2) term in it.
See the technical article referenced in the opening comments.}