NF Swing Trading using Fractal AFL

oldtrader

Well-Known Member
Thanks Ankurji for your efforts.Meanwhile if oldtraderji permits us can we tweak this code to avoid whipsawing trade to some extent?
Condition will be as follows.
1)Execute buy or sell signal on any candle in which price crosses 0.2% of high/low of candle which one is showing crossover arrow.
Hi hmp,

Pls add the following lines for 0.2% of high/low

t1= Flip(Buy,Sell);
t2= Flip(Sell,Buy);

BPrice=ValueWhen(t1 AND Ref(t1,-1)==0,C,1);
SPrice=ValueWhen(t2 AND Ref(t2,-1)==0,C,1);

buySLpercent=Bprice*0.002;
SellSLpercent=Sprice*0.002;

Buyplus=(Bprice+buySLpercent);

Plot(Buyplus,"R2",colorGreen,styleDashed|styleNoTitle);

Sellminus=(Sprice-sellSLpercent);

Plot(Sellminus,"R2",colorRed,styleDashed,styleNoLabel);
 

hmp

Well-Known Member
Thanks a lot Dear Oldtraderji for your prompt response.I will try it & let you know.Meanwhile you can back test it & compare its profit/loss with original one.
Thanks & regards
 

oldtrader

Well-Known Member
Thanks a lot Dear Oldtraderji for your prompt response.I will try it & let you know.Meanwhile you can back test it & compare its profit/loss with original one.
Thanks & regards
No brother, this condition is not possible to apply in backtest. As far as my limited knowledge of AFL is concerned. But visually I had observed that it is not making much difference.
 
Means you want to trigger the signal when prices crosses the high of signal candle (+ some filter say 5 points in nifty) and same for short.

In backtesting I don't know how to add these filter but in algo trading it can be done I suppose.

But there will be one drawback. suppose buy signal came around 3-3:30 PM and we wait for price to cross the high of signal candle but same day it not happened and next day it opened gap up then in this case we are cutting the profit.
But on the other way, we are cutting the loosing trade that would be more important I suppose. Let other members share their views.
No brother, this condition is not possible to apply in backtest. As far as my limited knowledge of AFL is concerned. But visually I had observed that it is not making much difference.
Hi Oldtrader,
Is this condition same as if we buy on signal candle high (not close) + filter......?
 

hmp

Well-Known Member
Hi all
Recently while googling i found this afl which is almost similar to our fractal.So i wish to share with you.Pl. follow below image where A is our fractal afl & B is the new afl.Pl. follow the given setting.With this i found that new afl is avoiding minor whipsaws.Only problem is, it does not having exploration facility which i will request all experts to try to include in it.Also if possible someone like Oldtraderji can back test it.Pl. note that so far i have not tried it in real market.
Thanks & regards.

Code:
_SECTION_BEGIN("Sound-script");
Length=Param("Length",25, 2); 
Deviation=Param("Deviation",2);
MoneyRisk=Param("Money Risk", 1);
LineStyle=ParamToggle("Display line mode", "No|Yes", 0); 
cUpTrendLine = ParamColor("UpTrendLine", colorGold);
cDnTrendLine = ParamColor("DownTrendLine", colorGold);

TurnedUp=Nz(StaticVarGet("TurnedUp"));
TurnedDown=Nz(StaticVarGet("TurnedDown"));
SoundON = ParamToggle("Sound","Off|On",1);

procedure CalcTrend_proc(bbtop,bbbot,Length,MoneyRisk,SoundON,TurnedUp,TurnedDown)
{
global UpTrendLine;
global DownTrendLine;
global smax;
global smin;

	UpTrendLine=Null;
	DownTrendLine=Null;
	smax=Null;
	smin=Null;
	trend=0;


	for (i=Length+1; i<BarCount; i++)	
  { 
		smax[i]=bbtop[i];
		smin[i]=bbbot[i];
		if (C[i]>smax[i-1]) trend=1;
		if (C[i]<smin[i-1]) trend=-1;
		if(trend>0 && smin[i]<smin[i-1]) smin[i]=smin[i-1];
		if(trend<0 && smax[i]>smax[i-1]) smax[i]=smax[i-1];
		bsmax[i]=smax[i]+0.5*(MoneyRisk-1)*(smax[i]-smin[i]);
		bsmin[i]=smin[i]-0.5*(MoneyRisk-1)*(smax[i]-smin[i]);
		if(trend>0 && bsmin[i]<bsmin[i-1]) bsmin[i]=bsmin[i-1];
		if(trend<0 && bsmax[i]>bsmax[i-1]) bsmax[i]=bsmax[i-1];
		if (trend>0) 
		{ 
			UpTrendLine[i]=bsmin[i];
			if (SoundON==True && !TurnedUp && i==BarCount-1 && IsEmpty(UpTrendLine[i-1])) 
			{ 
				Say("hi i am Kumaran Attention please, Market Going, UP");
				PopupWindow(Date()+ "\n  > " +  Name() + "\n  > Buy : " + H[ i ] + "\n  > Target : " +(L[i]*1.005) +"\n  > Stop Loss : " + (L[i]*0.9975),"Alert", 100, 640*mtRandom(), 480*mtRandom());				TurnedUp=StaticVarSet("TurnedUp",1);
				TurnedDown=StaticVarSet("TurnedDown",0);

			} 
		} 
	
   	if (trend<0) 
		{ 
			DownTrendLine[i]=bsmax[i];
			if (SoundON==True && !TurnedDown && i==BarCount-1 && IsEmpty(DownTrendLine[i-1])) 
			{
				Say("Attention please, Market Going, DOWN");
				PopupWindow( Date()+ "\n  > " + Name() + "\n  > Sell : " + L[i] + "\n  > Target : " + (H[i]*0.995)+"\n  > Stop Loss : " +(H[i]*1.0025),"Alert", 100, 640*mtRandom(), 480*mtRandom());
				TurnedUp=StaticVarSet("TurnedUp",0);
				TurnedDown=StaticVarSet("TurnedDown",1);
			} 
		} 
	} 
} 
	bbtop=BBandTop(C,Length,Deviation);
	bbbot=BBandBot(C,Length,Deviation);

	CalcTrend_proc(bbtop,bbbot,Length,MoneyRisk,SoundON,TurnedUp,TurnedDown);
	UpTrendSigNal=UpTrendLine AND IsEmpty(Ref(UpTrendLine,-1));
	DownTrendSigNal=DownTrendLine AND IsEmpty(Ref(DownTrendLine,-1));
	
mycolor = IIf( TurnedUp>TurnedDown , colorLime, colorRed);



dist = 2.8*ATR(10);
dist1 = 2*ATR(10);
for( i = 0; i < BarCount; i++ ) 
{ 
 if( UpTrendSignal[i] ) 
 {
  PlotText( "\nBuy:" + H[ i ] , i, H[ i ]-dist[i], colorGreen, colorWhite );
 }
 if( DownTrendSignal[i] )
 {
  PlotText( "Sell:" + L[ i ] , i, L[ i ]+dist1[i], colorRed, colorWhite ); 
 }
}


  DisplayStyle = styleNoLabel|styleLine|styleNoTitle;
  if(LineStyle == 0) DisplayStyle |= styleLine; 


	Plot(UpTrendLine,"Buy Stop Loss",cUpTrendLine,DisplayStyle);
	Plot(DownTrendLine,"Sell Stop Loss",cDnTrendLine,DisplayStyle) ;

	PlotShapes(IIf(UpTrendSignal,shapeSquare,shapeNone),colorLime,0,bbbot,Offset=-1);
	PlotShapes(IIf(UpTrendSignal,shapeUpArrow,shapeNone),colorWhite,0,bbbot,Offset=-1);


	PlotShapes(IIf(DownTrendSignal,shapeSquare,shapeNone),colorOrange,0,bbtop,Offset=-1);
	PlotShapes(IIf(DownTrendSignal,shapeDownArrow,shapeNone),colorWhite,0,bbtop,Offset=-1);










_SECTION_END();


Plot( C, "Close", mycolor, styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 
_SECTION_END();
_SECTION_BEGIN("Background text");
C13=Param("fonts",20,10,30,1 );
C14=Param("left-right",2.1,1.0,5.0,0.1 );
C15=Param("up-down",12,1,20,1 );
Miny = Status("axisminy");
Maxy = Status("axismaxy");
lvb = Status("lastvisiblebar");
fvb = Status("firstvisiblebar");
pxwidth = Status("pxwidth");
pxheight = Status("pxheight");
GfxSetBkMode(transparent=1);
GfxSetOverlayMode(1);
GfxSelectFont("Tahoma", Status("pxheight")/C13 );
GfxSetTextAlign( 6 );
GfxSetTextColor( ColorRGB (217,217,213));
GfxTextOut( Name(), Status("pxwidth")/C14, Status("pxheight")/C15 );
GfxSelectFont("Tahoma", Status("pxheight")/C13*0.5 );
GfxSetTextColor( ColorRGB (103,103,103));
GfxTextOut( "By", Status("pxwidth")/C14, Status("pxheight")/C15*2.5 );
GfxSelectFont("Tahoma", Status("pxheight")/C13*0.5 );
GfxSetTextColor( ColorRGB (103,103,103));
GfxTextOut( "WayToWin", Status("pxwidth")/C14, Status("pxheight")/C15*4 );
GfxSelectFont("MS Sans Serif", 10, 500, False, False, 0);
_SECTION_END();



_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 
_SECTION_END();
 

debdeeps

Active Member
Do below(red colored) path really exist in your local machine. If not then replace original path with the red colored.

Code:
AlertIf( Buy,"SOUND [B][COLOR="Red"]C:\\Windows\\Media\\Chord.wav[/COLOR][/B]", "Audio alert", 2 );
AlertIf( Sell,"SOUND [B][COLOR="red"]C:\\Windows\\Media\\Ding.wav[/COLOR][/B]", "Audio alert", 2 );
Excellent Chart. Would follow this for couple of days.
 

oldtrader

Well-Known Member
Hi all
Recently while googling i found this afl which is almost similar to our fractal.So i wish to share with you.Pl. follow below image where A is our fractal afl & B is the new afl.Pl. follow the given setting.With this i found that new afl is avoiding minor whipsaws.Only problem is, it does not having exploration facility which i will request all experts to try to include in it.Also if possible someone like Oldtraderji can back test it.Pl. note that so far i have not tried it in real market.
Thanks & regards.
Hi hmp,

The AFL looks good. For backtesting Buy and Sell variables need to be defined in the AFL. With my limited knowledge of AFL I am unable to write the Buy and Sell condition for the AFL. Need help from AFL experts for the same.
 

Similar threads