John Ehlers’ and Ric Way’s article in this issue, “Fractal Dimension As A Market Mode Sensor,” provides a method of calculating the fractal dimension of any price series. The authors suggest using the calculations to distinguish trending from cycling price movement
// Parameters
// N = 30 ; N must be an even number
// Threshold = 1.4
Price = MedianPrice
once HalfN = 0.5 * N
once NMinus1 = N - 1
once HalfNMinus1 = HalfN - 1
once Log2 = Log(2)
if barindex > N then
Smooth = (Price + 2 * Price[1] + 2 * Price[2] + Price[3]) / 6
N3 = ( highest[N](Smooth) - lowest[N](Smooth) ) / N
HH = Smooth
LL = Smooth
for Count = 0 to HalfNMinus1 do
if Smooth[Count] > HH then
HH = Smooth[Count]
endif
if Smooth[Count] < LL then
LL = Smooth[Count]
endif
next
N1 = ( HH - LL ) / HalfN
HH = Smooth[HalfN]
LL = Smooth[HalfN]
for Count = HalfN to NMinus1 do
if Smooth[Count] > HH then
HH = Smooth[Count]
endif
if Smooth[Count] < LL then
LL = Smooth[Count]
endif
next
N2 = ( HH - LL ) / HalfN
if N1 > 0 and N2 > 0 and N3 > 0 then
Ratio = 0.5 * ( ( Log( N1 + N2 ) - Log( N3 ) ) / Log2 + Dimen[1] )
endif
Dimen = Average[20](Ratio)
endif
return Dimen as "Dimen", 1.6 as "1.6", Threshold as "Trigger"