Accurate Candlestick Pattern Detector


Accurate Candlestick Pattern Detector
Accurate Candlestick Pattern Detector

//www.aflcode.com
//*****************************************

//*****************************************

//PRICE

//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//TRENDING RIBBON
// Paste the code below to your price chart somewhere and green ribbon means both
// both MACD and ADX trending up so if the red ribbon shows up the MACD and the ADX 
// are both trending down.
_SECTION_BEGIN("trending ribbon");
uptrend=PDI()>MDI() AND MACD()>Signal();
downtrend=MDI()>PDI() AND Signal()>MACD();
Plot( 2, /* defines the height of the ribbon in percent of pane width */"",
    IIf( uptrend AND EMA(C,50)>=Ref(EMA(C,50),-1), colorLime, IIf( downtrend OR EMA(C,50)<Ref(EMA(C,50),-1),
     colorRed, colorTan)) , /* choose color */styleOwnScale|styleArea|styleNoLabel, -0.5, 100 );
_SECTION_END();
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

//TREND ADVISER

pointer[0] = 0;

/* Phase filter */

Cond1 = Close > MA(Close, 50)AND NOT(Close > MA(Close, 200))AND NOT(MA(Close, 50) > MA(Close, 200));
Cond2 = Close > MA(Close, 50)AND Close > MA(Close, 200)AND NOT(MA(Close, 50) > MA(Close, 200));
Cond3 = Close > MA(Close, 50)AND Close > MA(Close, 200)AND MA(Close, 50) > MA(Close, 200);
Cond4 = NOT(Close > MA(Close, 50))AND Close > MA(Close, 200)AND MA(Close, 50) > MA(Close, 200);
Cond5 = NOT(Close > MA(Close, 50))AND NOT(Close > MA(Close, 200))AND MA(Close, 50) > MA(Close, 200);
Cond6 = NOT(Close > MA(Close, 50))AND NOT(Close > MA(Close, 200))AND NOT(MA(Close, 50) > MA(Close, 200));


for (i = 1; i < BarCount; i++)
{

  if (Cond1[i])
    pointer[i] = 1;
  if (Cond2[i])
    pointer[i] = 2;
  if (Cond3[i])
    pointer[i] = 3;
  if (Cond4[i])
    pointer[i] = 4;
  if (Cond5[i])
    pointer[i] = 5;
  if (Cond6[i])
    pointer[i] = 6;

}

/* Plot Graphic */
//GraphXSpace= 15 ;
dynamic_color = IIf(pointer < 4, colorGreen, colorRed);
//Plot(pointer, "TrendAdv2", dynamic_color, styleHistogram | styleThick, Null, Null, 0);
//SetChartBkGradientFill(ParamColor("BgTop", colorWhite), ParamColor("BgBottom", colorLightYellow));
Cond= pointer < 4 ;
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

//KPL

/* my entry is very simple(daily data for trading)

kpl system for entry only & exit as follow:

1 st exit at x % from entry price only 1/3 quantity.(ie 1st profit target)
2 nd exit when exit Signal comes from kpl sys remaining 1/3 quantity.
3. scale-in to initial quantity if new kpl Buy Signal comes.
re-do above scaling-out & scaling-in till filal exit.
4. final exit all quantity when Close below 21 Day EMA.

kpl system code bellow :
*/
//AFL by Kamalesh Langote. Email:kpl@...
noR =Param( "SwingR", 5, 1, 55 ) ;
noS =Param( "SwingS", 2, 1, 55 ) ;
res=HHV(H,noR);
sup=LLV(L,noS);
avd=IIf(C>Ref(res,-1),1,IIf(C<Ref(sup,-1),-1,0));
avn=ValueWhen(avd!=0,avd,1);
tsl=IIf(avn==1,sup,res);
//tsl_col=ParamColor( "Color", colorCycle );
tsl_col= IIf(avn==1,colorBlue,colorRed );
Plot(tsl, "KPL", tsl_col, styleStaircase | styleThick);
//shape=Buy*shapeUpArrow + Sell*shapeDownArrow;
//PlotShapes(shape,IIf(Buy,colorBlue,colorRed),0,IIf(Buy,Low,High));
SetPositionSize(300,spsShares);
ApplyStop(0,1,10,1);
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

_SECTION_BEGIN("Background");
SetChartBkGradientFill(ParamColor("Top", colorTeal), ParamColor("Bottom", colorLightGrey), ParamColor("Title", colorTeal));
SetChartBkColor(ParamColor("Chart Background", colorWhite));
_SECTION_END();

_SECTION_BEGIN("trends trail SVE");
function trends_func(resistance)
{
 trends = (H+L)/2; // initialize
 support = (H+L)/2; // initialize
 
 for( i = 4; i < BarCount; i++ )
 {
  // support
  prev = support[ i - 1 ];
 
  if (L[ i ] >= L[ i - 2 ] 
   AND L[ i - 1 ] >= L[ i - 2 ] 
   AND L[ i - 3 ] >= L[ i - 2 ] 
   AND L[ i - 4 ] >= L[ i - 2 ])
  {
   support[ i ] = L[ i - 2 ];
  }
  else if (L[ i ] > H[ i - 1]*1.0013)
  {
   support[ i ] = H[ i - 1 ]*0.9945;
  }
  else if (L[ i ] > prev*1.1)
  {
   support[ i ] = prev*1.05;
  }
  else
  {
   support[ i ] = prev;
  } 
  // trends
  prev = trends[ i - 1 ];
 
  if (H[ i ] > prev AND H[ i - 1 ] > prev)
  {
   trends[ i ] = Max(prev,support[ i ]);
  }
  else if (H[ i ] < prev AND H[ i - 1 ] < prev)
  {
   trends[ i ] = Min(prev,resistance[ i ]);
  }
  else if (H[ i ] > prev)
  {
   trends[ i ] = support[ i ];
  }
  else
  {
   trends[ i ] = resistance[ i ];
  }
 }
 return trends;
}

// AFL code by E.M.Pottasch, 12/28/2010, 
// idea from: http://stocata.org/metastock/stop_trail_trends.html
atrfact = Param("atrfact",2, 1, 10, 0.1);
period = Param("period",10, 1, 100, 1);
HiLo = IIf(H-L<1.5*MA(H-L,period),H-L,1.5*MA(H-L,period));
Href = IIf(L<=Ref(H,-1),H-Ref(C,-1),(H-Ref(C,-1))-(L-Ref(H,-1))/2);
Lref = IIf(H>=Ref(L,-1),Ref(C,-1)-L,(Ref(C,-1)-L)-(Ref(L,-1)-H)/2);
diff1 = Max(HiLo,Href);
diff2 = Max(diff1,Lref);
ATRmod = Wilders(diff2,period);
loss = atrfact*ATRmod;
resistance = C + loss;
// calculate trends
trends = trends_func(resistance);

SetChartBkColor( ParamColor("ColorBG", ColorRGB( 0, 0, 0 ) ) );
GraphXSpace = 5;
SetChartOptions(0, chartShowDates);
Plot(IIf(trends > C,trends,Null),"\ntrailShort",ParamColor("ColorTrailShort",ColorRGB(255,0,0)),styleStaircase);
Plot(IIf(trends < C,trends,Null),"\ntrailLong",ParamColor("ColorTrailLong",ColorRGB(0,255,0)),styleStaircase);
_SECTION_END();

_SECTION_BEGIN("Candlesick Patterns");
Plot(C,"",colorLightGrey,styleCandle);

r=CdDoji( threshold = 0.05 );
s=CdHammer( rangefactor= 1.1 );
t=CdBearishEngulfing( bodyfactor = 0.4, rangefactor = 0.5);
u=CdBullishEngulfing( bodyfactor = 0.4, rangefactor = 0.5);
PlotShapes(r*shapeSmallCircle,colorRed,Layer=0,yposition=H,Offset=12); 
PlotShapes(s*shapeCircle,colorYellow,Layer=0,yposition=H,Offset=12); 
PlotShapes(t*shapeHollowSmallCircle,colorLime,Layer=0,yposition=H,Offset=12); 
PlotShapes(u*shapeHollowCircle,colorBlue,Layer=0,yposition=H,Offset=12); 

for(i=0;i<BarCount-1;i++) 
{
 if(r[i]==True)PlotText("Doji", i, H[i], colorRed, bkcolor = colorDefault); 
 if(s[i]==True)PlotText("Hammer", i, H[i], colorYellow, bkcolor = colorDefault); 
 if(t[i]==True)PlotText("BearishEngulf", i, H[i], colorLime, bkcolor = colorDefault); 
 if(u[i]==True)PlotText("BullishEngulf", i, H[i], colorBlue, bkcolor = colorDefault); 
}
_SECTION_END();

_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorBlack ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 
_SECTION_END();

_SECTION_BEGIN("Show Values at H&L");

n=Param("Values back",20,1,200,1);
p=Param("zig %",5,1,100,1);
dist = 0.8*ATR(15);

for( i = 1; i < n; i++ )
{ 
 PlotText(""+LastValue(Peak(H,p,i),True),BarCount-3-LastValue(PeakBars(H,p,i)),LastValue(dist,True)+LastValue(Peak(H,p,i),False),colorBlack,ColorRGB(225,225,225));
 PlotText(""+LastValue(Trough(L,p,i),True),BarCount-3-LastValue(TroughBars(L,p,i)),LastValue(Trough(L,p,i),False)-LastValue(dist,True),colorBlack,ColorRGB(225,225,225));
}

_SECTION_END();

_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorBlack ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 
_SECTION_END();

_SECTION_BEGIN("Average Side Way Mkt");
price = ParamField("field");
n = Param("periods",35);
SA=MA(Price,n);
v1=(StDev(Price,n))^2;

Ca=Null;
Ca[n]=sa[n];

for(i=n+1; i<BarCount;i++){
v2[i]=(CA[i-1]-SA[i])^2;
k[i]=IIf(V2[i]<V1[i],0,1-V1[i]/V2[i]);
CA[i]=CA[i-1]+K[i]*(SA[i-1]-CA[i-1]);
}
Plot(Ca,"OMA("+WriteVal(n,1.0)+")",colorYellow,styleThick);
Col = IIf(BarsSince(Ref(Ca,-1)>Ca)>BarsSince(Ref(Ca,-1)<Ca),colorBrightGreen,colorRed);
Plot(C,"",Col,styleBar);
_SECTION_END();

_SECTION_BEGIN("Bar Count Back V. Good");
//* Stephane Carrasset's Countback Line (CBL) popularized by Daryl Guppy

//Refer to amibroker posting 30th December 2004 */

nR=2;

Cbl[nR]=Null;

bCBL=False;

for( i=nR; i < BarCount; i++)

{

if( (Low[i-2]<Low[i-1]) && (Low[i-1]<Low[i]) )

{

Cbl[i] = Low[i-2];

bCBL = True;

}

else if (bCBL)

{

if (Low[i] < Cbl[i-1])

{

Cbl[i] = Cbl[i-1];

bCBL = False;

}

else

{

n = nR;

minval[i] = Low[i];

breakloop= False;

for (j = 1; NOT(breakloop) && j <= i; j++)

{

if (Low[i-j] < minval[i])

{

if (n>1)

{

minval[i] = Low[i-j];

n--;

}

else

{

Cbl[i] = Low[i-j];

breakloop=True;

}

}

}

if (Cbl[i] < Cbl[i-1])

Cbl[i] = Cbl[i-1];

}

}

else

{

Cbl[i] = Cbl[i-1];

}

if (Cbl[i]==0)

Cbl[i] = Cbl[i-1];

}

Plot(Cbl,"",colorGreen,1);

Plot(C,"",-1,64);

_SECTION_END();
_SECTION_END();



_SECTION_BEGIN("Background");
SetChartBkGradientFill(ParamColor("Top", colorTeal), ParamColor("Bottom", colorLightGrey), ParamColor("Title", colorTeal));
SetChartBkColor(ParamColor("Chart Background", colorWhite));
_SECTION_END();

Previous
Next Post »