Here is an AFL for the implementation of the standard Camarilla system with whipsaws minimised. The AFL also demonstrated ways to manage/modify signals in a flexible manner. This system is designed for trading with the Nifty and has a stop profit/trailing stop loss incorporated with re-entry signals when a trend continues after a stop loss is hit. Useful information regarding the active Camarilla level, recent highs and lows are displayed in the title bar.
In case of copy paste difficulties, the indicator can also be picked from my post on wisetrader.
_SECTION_BEGIN("Draw Camarilla Levels");
SetChartOptions(0,chartShowArrows|chartShowDates);
showcamarella=ParamList("Show Camarella Lines","YES|NO");
showcandlechart=ParamList("Show Candle chart","YES|NO");
showwhat=IIf(ParamList("Todays or Tomorrows Levels","Today|Tomorrow")=="Tomorrow",1,-1);
if (showcandlechart=="YES")
{
Plot( C, "Close", ParamColor("Color", colorWhite ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
}
EMA10=EMA(C,10);
EMA3=EMA(C,3);
Plot(EMA10,"EMA10", colorWhite,styleLine|styleNoLabel);
Plot(EMA3,"EMA3", colorGreen,styleLine|styleNoLabel);
XH = TimeFrameGetPrice( "H", inDaily,showwhat);
XL = TimeFrameGetPrice( "L", inDaily,showwhat);
XC = TimeFrameGetPrice( "C", inDaily, showwhat );
r2=(((xh-xl)*1.1)/2)+xc;
r1=(((xh-xl)*1.1)/4)+xc;
s1=(xc-((xh-xl)*1.1)/4);
s2=(xc-((xh-xl)*1.1)/2);
n3=1;
tgt1=(1+.005*n3)*r2;
tgt2=(1+.01*n3)*r2;
tgt3=s2/(1+.005*n3);
tgt4=s2/(1+.01*n3);
if (showcamarella=="YES")
{
Plot(r1,"R1",colorGreen,styleLine|styleNoTitle);
Plot(r2,"R2",colorGreen,styleLine|styleNoTitle);
Plot(s1,"S1",colorRed,styleLine|styleNoTitle);
Plot(s2,"S2",colorRed,styleLine|styleNoTitle);
Plot(tgt1,"tgt1",colorYellow,styleLine|styleNoTitle);
Plot(tgt2,"tgt2",colorYellow,styleLine|styleNoTitle);
Plot(tgt3,"tgt3",colorYellow,styleLine|styleNoTitle);
Plot(tgt4,"tgt4",colorYellow,styleLine|styleNoTitle);
Plot(s1+(0.85*(r1-s1)),"int long target 2",colorBlue,styleLine|styleNoTitle);
Plot(s1+(0.65*(r1-s1)),"int long target 1",colorBlue,styleLine|styleNoTitle);
Plot(s1+(0.35*(r1-s1)),"int short target 1",colorPink,styleLine|styleNoTitle);
Plot(s1+(0.15*(r1-s1)),"int short target 2",colorPink,styleLine|styleNoTitle);
Buy=Cross(C,s1) OR Cross(C,r1) OR Cross(C,r2);
Sell=Cross(r1,C) OR Cross(s1,C) OR Cross(s2,C);
//parameters
gapavg=Param("Gap between Averages",8,0,20,.1);
gapcam=Param("Gap between Cam lines and fast avg",3.8,0,20,.1);
SP=Param("Stop Profit",23,0,25,.1);
Camlevel=IIf(abs(EMA3-r1)<abs(EMA3-r2),r1,r2);
Camlevel=IIf(abs(EMA3-s1)<abs(EMA3-r1),s1,Camlevel);
Camlevel=IIf(abs(EMA3-s2)<abs(EMA3-s1),s2,Camlevel);
Cvp=(O+C)/2;
Datex=DateNum();
//whipsaw removal
Buysignal=Sellsignal=lastbuy=lastsell=priceatbuy=priceatsell=slow=shigh=exitbuy=exitsell=curri=priceatexitbuy=priceatexitsell=0;
Lastexitsell=Lastexitbuy=profit=lastprofit=0;
for (i=1;i<BarCount-1;i++)
{
//change of day initialise variables
if(Datex!=Datex[i-1])
{
if (lastbuy==1)Lastprofit=profit[i-1]+Cvp[i-1]-priceatbuy;
else if (Lastsell==1)Lastprofit=profit[i-1]+priceatsell-Cvp[i-1];
else Lastprofit=profit[i-1];
PlotText("Profit\n"+WriteVal(Lastprofit,1.0),i,H+15,colorWhite);
Buysignal=Sellsignal=lastbuy=lastsell=priceatbuy=priceatsell=curri=lastexitsell=lastexitbuy=0;
profit=0;
}
if(i==BarCount-2)
{
if (lastbuy==1)Lastprofit=profit+Cvp-priceatbuy;
else if (Lastsell==1)Lastprofit=profit+priceatsell-Cvp;
else Lastprofit=profit[i-1];
PlotText("Profit\n"+WriteVal(Lastprofit,1.0),i,H+15,colorWhite);
}
if (Datex==Datex[i-1])
{
shigh=shigh[i-1];
slow=slow[i-1];
profit=profit[i-1];
}
if (H>shigh)shigh=H;
if (L<slow)slow=L;
if (Buy)
{
Buy=0;
Buysignal=1;
Sellsignal=0;
}
if (Sell)
{
Sell=0;
Sellsignal=1;
Buysignal=0;
}
// PlotText(WriteVal(profit,1.0),i,H+5,colorWhite);
if (Buysignal==1 AND ((EMA3-EMA10>gapavg) OR (EMA3>EMA10)) AND EMA3-Camlevel>gapcam AND O<C )
{
if (lastbuy==1)profit=profit+Cvp-priceatbuy;
if (Lastsell==1)profit=profit+priceatsell-Cvp;
Lastbuy=Lastsell=lastexitbuy=lastexitsell=0;
Buysignal=0;
Buy=1;
Lastbuy=1;
priceatbuy=Cvp;
slow=L;
shigh=H;
Curri=i;
}
if (SellSignal==1 AND ((EMA10-EMA3>gapavg) OR (EMA10>EMA3)) AND Camlevel-EMA3>gapcam AND O>C)
{
if (lastbuy==1)profit=profit+Cvp-priceatbuy;
if (Lastsell==1)profit=profit+priceatsell-Cvp;
Lastbuy=Lastsell=lastexitbuy=lastexitsell=0;
Sellsignal=0;
Sell=1;
Lastsell=1;
priceatsell=Cvp;
slow=L;
shigh=H;
Curri=i;
}
if ((Lastbuy==1 OR (lastexitbuy==1 AND cvp-priceatexitbuy>4)) AND shigh-Cvp>sp AND !Sell AND i>Curri)
{
profit=profit+Cvp-priceatbuy;
exitbuy=1;
Lastexitbuy=1;
Lastbuy=0;
slow=L;
shigh=H;
priceatexitbuy=Cvp;
Curri=i;
}
if ((Lastsell==1 OR (Lastexitsell==1 AND priceatexitsell-Cvp>4)) AND Cvp-slow>sp AND !Buy AND i>Curri)
{
profit=profit+priceatsell-Cvp;
exitsell=1;
Lastexitsell=1;
Lastsell=0;
slow=L;
shigh=H;
priceatexitsell=Cvp;
Curri=i;
}
if (i>Curri AND Lastexitsell==1 AND priceatexitsell-Cvp>4)
{
Sellsignal=1;
Lastexitsell=0;
}
if (i>Curri AND Lastexitbuy==1 AND Cvp-priceatexitbuy>4)
{
buySignal=1;
Lastexitbuy=0;
}
if (Buy OR Sell OR exitbuy OR exitsell)
{
PlotText(WriteVal(Profit,1.0),i,H+15,colorGreen);
}
}
shape = Buy * shapeUpArrow + Sell * shapeDownArrow;
PlotShapes( shape, IIf( Buy, colorLime, colorRed ), 0, IIf( Buy, Low, High));
shape2 = exitsell *shapeHollowUpArrow + exitbuy *shapeHollowDownArrow;
PlotShapes( shape2, IIf( exitsell, colorWhite, colorWhite ), 0, IIf( exitsell, Low, High));
}
function GetSecondNum()
{
Time = Now( 4 );
Seconds = int( Time % 100 );
Minutes = int( Time / 100 % 100 );
Hours = int( Time / 10000 % 100 );
SecondNum = int( Hours * 60 * 60 + Minutes * 60 + Seconds );
return SecondNum;
}
RequestTimedRefresh( 1 );
TimeFrame = Interval();
SecNumber = GetSecondNum();
Newperiod = SecNumber % TimeFrame == 0;
SecsLeft = SecNumber - int( SecNumber / TimeFrame ) * TimeFrame;
SecsToGo = TimeFrame - SecsLeft;
Title = Date()+" "+ Interval(format=2)+" "+Name()+" O "+WriteVal(O,1.2)+" H "+WriteVal(H,1.2)+" L "+WriteVal(L,1.2)+" C "+WriteVal(C,1.2)+" Cam level " + WriteVal(Camlevel,1.2)+" Fast avg "+WriteVal(EMA3,1.2)+" CurrHigh "+WriteVal(shigh,1.2)+" Currlow "+WriteVal(slow,1.2)+" Avg Price "+WriteVal(Cvp,1.2)+" "+ EncodeColor(colorYellow)+ " Bar secs remaining "+WriteVal(secstogo,1.0);
In case of copy paste difficulties, the indicator can also be picked from my post on wisetrader.
_SECTION_BEGIN("Draw Camarilla Levels");
SetChartOptions(0,chartShowArrows|chartShowDates);
showcamarella=ParamList("Show Camarella Lines","YES|NO");
showcandlechart=ParamList("Show Candle chart","YES|NO");
showwhat=IIf(ParamList("Todays or Tomorrows Levels","Today|Tomorrow")=="Tomorrow",1,-1);
if (showcandlechart=="YES")
{
Plot( C, "Close", ParamColor("Color", colorWhite ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
}
EMA10=EMA(C,10);
EMA3=EMA(C,3);
Plot(EMA10,"EMA10", colorWhite,styleLine|styleNoLabel);
Plot(EMA3,"EMA3", colorGreen,styleLine|styleNoLabel);
XH = TimeFrameGetPrice( "H", inDaily,showwhat);
XL = TimeFrameGetPrice( "L", inDaily,showwhat);
XC = TimeFrameGetPrice( "C", inDaily, showwhat );
r2=(((xh-xl)*1.1)/2)+xc;
r1=(((xh-xl)*1.1)/4)+xc;
s1=(xc-((xh-xl)*1.1)/4);
s2=(xc-((xh-xl)*1.1)/2);
n3=1;
tgt1=(1+.005*n3)*r2;
tgt2=(1+.01*n3)*r2;
tgt3=s2/(1+.005*n3);
tgt4=s2/(1+.01*n3);
if (showcamarella=="YES")
{
Plot(r1,"R1",colorGreen,styleLine|styleNoTitle);
Plot(r2,"R2",colorGreen,styleLine|styleNoTitle);
Plot(s1,"S1",colorRed,styleLine|styleNoTitle);
Plot(s2,"S2",colorRed,styleLine|styleNoTitle);
Plot(tgt1,"tgt1",colorYellow,styleLine|styleNoTitle);
Plot(tgt2,"tgt2",colorYellow,styleLine|styleNoTitle);
Plot(tgt3,"tgt3",colorYellow,styleLine|styleNoTitle);
Plot(tgt4,"tgt4",colorYellow,styleLine|styleNoTitle);
Plot(s1+(0.85*(r1-s1)),"int long target 2",colorBlue,styleLine|styleNoTitle);
Plot(s1+(0.65*(r1-s1)),"int long target 1",colorBlue,styleLine|styleNoTitle);
Plot(s1+(0.35*(r1-s1)),"int short target 1",colorPink,styleLine|styleNoTitle);
Plot(s1+(0.15*(r1-s1)),"int short target 2",colorPink,styleLine|styleNoTitle);
Buy=Cross(C,s1) OR Cross(C,r1) OR Cross(C,r2);
Sell=Cross(r1,C) OR Cross(s1,C) OR Cross(s2,C);
//parameters
gapavg=Param("Gap between Averages",8,0,20,.1);
gapcam=Param("Gap between Cam lines and fast avg",3.8,0,20,.1);
SP=Param("Stop Profit",23,0,25,.1);
Camlevel=IIf(abs(EMA3-r1)<abs(EMA3-r2),r1,r2);
Camlevel=IIf(abs(EMA3-s1)<abs(EMA3-r1),s1,Camlevel);
Camlevel=IIf(abs(EMA3-s2)<abs(EMA3-s1),s2,Camlevel);
Cvp=(O+C)/2;
Datex=DateNum();
//whipsaw removal
Buysignal=Sellsignal=lastbuy=lastsell=priceatbuy=priceatsell=slow=shigh=exitbuy=exitsell=curri=priceatexitbuy=priceatexitsell=0;
Lastexitsell=Lastexitbuy=profit=lastprofit=0;
for (i=1;i<BarCount-1;i++)
{
//change of day initialise variables
if(Datex!=Datex[i-1])
{
if (lastbuy==1)Lastprofit=profit[i-1]+Cvp[i-1]-priceatbuy;
else if (Lastsell==1)Lastprofit=profit[i-1]+priceatsell-Cvp[i-1];
else Lastprofit=profit[i-1];
PlotText("Profit\n"+WriteVal(Lastprofit,1.0),i,H+15,colorWhite);
Buysignal=Sellsignal=lastbuy=lastsell=priceatbuy=priceatsell=curri=lastexitsell=lastexitbuy=0;
profit=0;
}
if(i==BarCount-2)
{
if (lastbuy==1)Lastprofit=profit+Cvp-priceatbuy;
else if (Lastsell==1)Lastprofit=profit+priceatsell-Cvp;
else Lastprofit=profit[i-1];
PlotText("Profit\n"+WriteVal(Lastprofit,1.0),i,H+15,colorWhite);
}
if (Datex==Datex[i-1])
{
shigh=shigh[i-1];
slow=slow[i-1];
profit=profit[i-1];
}
if (H>shigh)shigh=H;
if (L<slow)slow=L;
if (Buy)
{
Buy=0;
Buysignal=1;
Sellsignal=0;
}
if (Sell)
{
Sell=0;
Sellsignal=1;
Buysignal=0;
}
// PlotText(WriteVal(profit,1.0),i,H+5,colorWhite);
if (Buysignal==1 AND ((EMA3-EMA10>gapavg) OR (EMA3>EMA10)) AND EMA3-Camlevel>gapcam AND O<C )
{
if (lastbuy==1)profit=profit+Cvp-priceatbuy;
if (Lastsell==1)profit=profit+priceatsell-Cvp;
Lastbuy=Lastsell=lastexitbuy=lastexitsell=0;
Buysignal=0;
Buy=1;
Lastbuy=1;
priceatbuy=Cvp;
slow=L;
shigh=H;
Curri=i;
}
if (SellSignal==1 AND ((EMA10-EMA3>gapavg) OR (EMA10>EMA3)) AND Camlevel-EMA3>gapcam AND O>C)
{
if (lastbuy==1)profit=profit+Cvp-priceatbuy;
if (Lastsell==1)profit=profit+priceatsell-Cvp;
Lastbuy=Lastsell=lastexitbuy=lastexitsell=0;
Sellsignal=0;
Sell=1;
Lastsell=1;
priceatsell=Cvp;
slow=L;
shigh=H;
Curri=i;
}
if ((Lastbuy==1 OR (lastexitbuy==1 AND cvp-priceatexitbuy>4)) AND shigh-Cvp>sp AND !Sell AND i>Curri)
{
profit=profit+Cvp-priceatbuy;
exitbuy=1;
Lastexitbuy=1;
Lastbuy=0;
slow=L;
shigh=H;
priceatexitbuy=Cvp;
Curri=i;
}
if ((Lastsell==1 OR (Lastexitsell==1 AND priceatexitsell-Cvp>4)) AND Cvp-slow>sp AND !Buy AND i>Curri)
{
profit=profit+priceatsell-Cvp;
exitsell=1;
Lastexitsell=1;
Lastsell=0;
slow=L;
shigh=H;
priceatexitsell=Cvp;
Curri=i;
}
if (i>Curri AND Lastexitsell==1 AND priceatexitsell-Cvp>4)
{
Sellsignal=1;
Lastexitsell=0;
}
if (i>Curri AND Lastexitbuy==1 AND Cvp-priceatexitbuy>4)
{
buySignal=1;
Lastexitbuy=0;
}
if (Buy OR Sell OR exitbuy OR exitsell)
{
PlotText(WriteVal(Profit,1.0),i,H+15,colorGreen);
}
}
shape = Buy * shapeUpArrow + Sell * shapeDownArrow;
PlotShapes( shape, IIf( Buy, colorLime, colorRed ), 0, IIf( Buy, Low, High));
shape2 = exitsell *shapeHollowUpArrow + exitbuy *shapeHollowDownArrow;
PlotShapes( shape2, IIf( exitsell, colorWhite, colorWhite ), 0, IIf( exitsell, Low, High));
}
function GetSecondNum()
{
Time = Now( 4 );
Seconds = int( Time % 100 );
Minutes = int( Time / 100 % 100 );
Hours = int( Time / 10000 % 100 );
SecondNum = int( Hours * 60 * 60 + Minutes * 60 + Seconds );
return SecondNum;
}
RequestTimedRefresh( 1 );
TimeFrame = Interval();
SecNumber = GetSecondNum();
Newperiod = SecNumber % TimeFrame == 0;
SecsLeft = SecNumber - int( SecNumber / TimeFrame ) * TimeFrame;
SecsToGo = TimeFrame - SecsLeft;
Title = Date()+" "+ Interval(format=2)+" "+Name()+" O "+WriteVal(O,1.2)+" H "+WriteVal(H,1.2)+" L "+WriteVal(L,1.2)+" C "+WriteVal(C,1.2)+" Cam level " + WriteVal(Camlevel,1.2)+" Fast avg "+WriteVal(EMA3,1.2)+" CurrHigh "+WriteVal(shigh,1.2)+" Currlow "+WriteVal(slow,1.2)+" Avg Price "+WriteVal(Cvp,1.2)+" "+ EncodeColor(colorYellow)+ " Bar secs remaining "+WriteVal(secstogo,1.0);