### Triangle Pattern Detector

 Triangle Pattern Detector

```//www.aflcode.com
_SECTION_BEGIN("Traingle 2");
function calculatePosition(array,shift)
{
result = Null;
for( i = 0; i < BarCount; i++ )
{
if (array[ i ] == 1)
{
k = i - shift[ i ];
if( k >= 0 AND k < BarCount ) result[ k ] = 1;
}
}
return result;
}
function pattern_func(PLow,bi,t1,t2,t3,AA,AABar,BB,BBBar,CC,CCBar,nbar)
{
pattern =
(t3 == 1) AND (t2 == 3) AND (t1 == 2) AND PLow AND
BBBar - AABar > nbar AND CCBar - BBBar > nbar;

return pattern;
}

// E.M.Pottasch, 2/21/2011
// example code triangle fill using arrays

//SetBarsRequired(sbrAll,sbrAll);
bi = BarIndex();
nbar = Param("nbar",7,2,50,1);

// define fractals
PHigh = H > Ref(HHV(H,nbar),-1) AND Ref(HHV(H,nbar),nbar) <= H;
PHighPrice = ValueWhen(PHigh,H);
PLow = L < Ref(LLV(L,nbar),-1) AND Ref(LLV(L,nbar),nbar) >= L;
PLowPrice = ValueWhen(PLow,L);

ll = IIf( PLow AND PLowPrice < Ref(PLowPrice, -1), 1, 0 );
hl = IIf( PLow AND PLowPrice >= Ref(PLowPrice, -1), 2, 0 );
lh = IIf( PHigh AND PHighPrice < Ref(PHighPrice, -1), 3, 0 );
hh = IIf( PHigh AND PHighPrice >= Ref(PHighPrice, -1), 4, 0 );

combi = ll + hl + lh + hh;

t1 = ValueWhen(combi,combi,1);
t2 = ValueWhen(combi,combi,2);
t3 = ValueWhen(combi,combi,3);

AA = ValueWhen(PLow,L,2);
AABar = ValueWhen(PLow,bi,2);
BB = ValueWhen(PHigh,H,1);
BBBar = ValueWhen(PHigh,bi,1);
CC = ValueWhen(PLow,L,1);
CCBar = ValueWhen(PLow,bi,1);

// calculate the end point of the pattern => C
pattern = pattern_func(PLow,bi,t1,t2,t3,AA,AABar,BB,BBBar,CC,CCBar,nbar);

// calculate the starting point of the pattern => A
dBar = IIf(pattern,CCBar-AABar,0);
hhh1 = calculatePosition(pattern,dBar);
// calculate the middle point of the pattern => B
dBar = IIf(pattern,CCBar-BBBar,0);
hhh2 = calculatePosition(pattern,dBar);

// define x-axis range between A-C
rangeAC = Flip(hhh1,pattern);
// define x-axis range between A-B
rangeAB = Flip(hhh1,hhh2);
// define x-axis range between B-C
rangeBC = Flip(hhh2,pattern);

// extend AC
rangeAC = IIf(Ref(rangeAC,-1),Ref(rangeAC,-1),rangeAC);

// now define the legs of the triangle
// AC leg
startval1 = ValueWhen(PLow,L,1);
endval1 = ValueWhen(PLow,L,0);
startbar1 = ValueWhen(PLow,bi,1);
endbar1 = ValueWhen(PLow,bi,0);
aa1 = (endval1-startval1)/(endbar1-startbar1);
bb1 = startval1;
ACLeg = aa1 * (bi - startbar1) + bb1;
ACLeg = IIf(ACLeg AND rangeAC,ACLeg,Null);

// AB leg
startval1 = ValueWhen(PLow,L,1);
endval1 = ValueWhen(PHigh,H,0);
startbar1 = ValueWhen(PLow,bi,1);
endbar1 = ValueWhen(PHigh,bi,0);
aa1 = (endval1-startval1)/(endbar1-startbar1);
bb1 = startval1;
ABLeg = aa1 * (bi - startbar1) + bb1;
ABLeg = IIf(ABLeg AND rangeAB,ABLeg,Null);

// BC leg
startval1 = ValueWhen(PHigh,H,1);
endval1 = ValueWhen(PLow,L,0);
startbar1 = ValueWhen(PHigh,bi,1);
endbar1 = ValueWhen(PLow,bi,0);
aa1 = (endval1-startval1)/(endbar1-startbar1);
bb1 = startval1;
BCLeg = aa1 * (bi - startbar1) + bb1;
BCLeg = IIf(BCLeg AND rangeBC,BCLeg,Null);

// define top and bottom array for polygon fill
topLineArray = IIf(ABLeg,ABLeg,BCLeg);
bottomLineArray = ACLeg;
topLineArray = IIf(bottomLineArray AND IsEmpty(topLineArray),bottomLineArray,topLineArray);

// chart
GraphXSpace = 5;
SetChartOptions(0, chartShowDates);
Plot( C, "\nPrice",colorBrightGreen, styleCandle );
PlotShapes(shapeSmallCircle*PLow,colorGreen,0,L,-10);
PlotShapes(shapeSmallCircle*PHigh,colorRed,0,H,10);
PlotShapes(IIf(pattern,shapeUpTriangle,shapeNone),colorBrightGreen,0,L,-25);
PlotShapes(IIf(pattern,shapeHollowUpTriangle,shapeNone),colorWhite,0,L,-25);
//Plot(ACLeg,"",colorWhite,styleLine);
//Plot(ABLeg,"",colorWhite,styleLine);
//Plot(BCLeg,"",colorWhite,styleLine);
Plot(topLineArray,"",colorWhite,styleLine);
Plot(bottomLineArray,"",colorWhite,styleLine);

fillOnOff = ParamToggle("Fill Pattern","Fill Off|Fill On",1);
if (fillOnOff)
{
PlotOHLC(topLineArray,topLineArray,bottomLineArray,bottomLineArray,"",
colorAqua, styleCloud | styleNoLabel );
SetChartBkColor( ParamColor("background",colorBlack));
}
_SECTION_END();
```

Previous
Next Post »