{
Function: CPDensity ("Consolidation Pattern Density")
Calculates the "density" of a consolidation/trading range pattern
over the past NBars bars. The density is defined as the area of bars in
the pattern divided by the maximum possible area. Area is defined as
the sum of the height of the bars, where height is given by the
true range. The max possible area is the area of the rectangle
that bounds the bars; i.e., the highest high minus the lowest low
times the number of bars.
Michael R. Bryant
Breakout Futures
www.BreakoutFutures.com
Copyright 2003 Breakout Futures
August 2003
}
Input: NBars (NumericSimple); { number of bars in pattern }
Var: CPWidth (0), { highest high minus lowest low over last NBars bars }
NB (0), { equal to NBars, provided NBars > 0 }
SumTR (0), { sum of true ranges over NB bars }
ii (0); { loop counter }
{ Make sure the input value of NBars is greater than zero }
NB = MaxList(NBars, 1);
{ Calculate width of rectangle bounding consolidatin pattern }
CPWidth = Highest(H, NB) - Lowest(L, NB);
{ Calculate sum of true ranges }
SumTR = 0;
For ii = 0 to NB - 1 Begin
SumTR = SumTR + TrueRange[ii];
End;
{ Return sum of true ranges in pattern divided by max area of pattern }
CPDensity = SumTR/(CPWidth * NB);
{
Function: CPLocate
Determine if current bar is part of a consolidation/trading range
pattern. Return true if it is; false otherwise.
The pattern is identified as follows:
1. Patterns of length NBMin to NBMax are scanned using the
function CPDensity, which calculates the "density" of
the consolidation pattern. All patterns start at the
current bar.
2. The length of the pattern with the highest density is
recorded.
3. If the density of the highest density pattern matches or
exceeds a threshold level, the pattern is considered to be
a consolidation pattern, and TRUE is returned.
The function also returns the density, length (in bars),
and the upper and lower boundaries of the pattern with the highest
density.
Michael R. Bryant
Breakout Futures
www.BreakoutFutures.com
Copyright 2003 Breakout Futures
August 2003
}
Input: DensTH (NumericSimple), { Density threshold }
NBMin (NumericSimple), { Min # of bars in pattern }
NBMax (NumericSimple), { Max # of bars in pattern }
PatDens (NumericRef), { Density of pattern }
NBars (NumericRef), { Number of bars in pattern }
HBound (NumericRef), { Upper/high boundary of pattern }
LBound (NumericRef); { Lower boundary of pattern }
Var: MaxDens (0), { max density found }
NMaxDens (0), { length of pattern of max density }
ibars (0); { loop counter }
{ Search for pattern with highest density }
MaxDens = 0.;
For ibars = NBMin to NBMax Begin
PatDens = CPDensity(ibars);
If PatDens > MaxDens then Begin
MaxDens = PatDens;
NMaxDens = ibars;
End;
End;
{ Record results for densest pattern for return }
PatDens = MaxDens;
NBars = NMaxDens;
HBound = Highest(H, NBars);
LBound = Lowest(L, NBars);
{ Check if densest pattern exceeds threshold }
If MaxDens >= DensTH then
CPLocate = True
Else
CPLocate = False;
{
Indicator: CPIndicate
Plot the upper and lower bands of consolidation patterns identified
by function CPLocate.
Michael R. Bryant
Breakout Futures
www.BreakoutFutures.com
Copyright 2003 Breakout Futures
August 2003
}
Input: DensTH (0.55); { Density threshold }
Var: NBMin (4), { Min # of bars in pattern }
NBMax (30), { Max # of bars in pattern }
PatDens (0), { Density of pattern }
NBars (5), { Number of bars in pattern }
HBound (0), { Upper/high boundary of pattern }
LBound (0), { Lower boundary of pattern }
LocTF (false); { output of CPLocate function }
LocTF = CPLocate(DensTH, NBMin, NBMax, PatDens, NBars, HBound, LBound);
MessageLog(" Date: ", Date:6:0, " Time: ", time:6:0, " Density: ", PatDens:6:3, " # Bars: ", NBars:3:0);
If LocTF then Begin
Plot1(HBound, "Upper");
Plot2(LBound, "Lower");
End;
{Plot3(PatDens, "Density");
Plot4(NBars, "NBars"); }