Skip to main content

Another Trail for Amibroker (AFL)

empottasch over 15 years ago Amibroker (AFL)

  • Rating:
    3 / 5 (Votes 4)
  • Tags:
    amibroker, trend

I adjusted code from a recent posting by Gopal titled: “NMA v 3.6 a optimiser” which is written by M.Karthik.

My adjustment code displays the trail using colors and allows the bull and bear parameter to be set separately. Further I use similar code and in my other trail postings. Also I removed the signals. This code simply shows the trend.

Screenshots

Indicator / Formula

Copy & Paste Friendly
function trail_func(jj,trBull,trBear)
{
	trailArray[ 0 ] = jj[ 0 ]; // initialize
	for( i = 1; i < BarCount; i++ )
	{
		prev = trailArray[ i - 1 ];
 
		if (jj[ i ] > prev AND jj[ i - 1 ] > prev)
		{
			trailArray[ i ] = Max(prev,jj[ i ] - trBull[ i ]);
		}
		else if (jj[ i ] < prev AND jj[ i - 1 ] < prev)
		{
			trailArray[ i ] = Min(prev,jj[ i ] + trBear[ i ]);
		}
		else if (jj[ i ] > prev)
		{
			trailArray[ i ] = jj[ i ] - trBull[ i ];
		}
		else
		{
			trailArray[ i ] = jj[ i ] + trBear[ i ];	
		}
	}
	return trailArray;
}

// code by E.M.Pottasch
// adjusted from M.Kartnik 
// following: http://wisestocktrader.com/indicators/1710-nma-v-3-6-a-optimiser

k_bull = Param("mult bull",1.25,1,4,0.05); 
k_bear = Param("mult bear",1.25,1,4,0.05); 
Per= Param("period",10,5,50,1); 
 
HaClose=(O+H+L+C)/4; 
HaOpen = AMA(Ref(HaClose,-1),0.5); 
HaHigh = Max(H,Max( HaClose,HaOpen)); 
HaLow = Min(L,Min(HaClose,HaOpen));

ms=ParamList("ST0P/REV",List="Reg|Smoothed",0);
if(ms=="Reg")
{
	nm =(H-L);
	j=(O+H+L+C)/4;
}
else
{
	nm=(HaHigh-HaLow);
	j=(HaOpen+HaHigh+HaLow+HaClose)/4;
}
 
rfsctor = WMA(nm,PER); 
revers_bull=k_bull*rfsctor; 
revers_bear=k_bear*rfsctor;

nw = trail_func(j,revers_bull,revers_bear);

GraphXSpace = 5;
SetChartOptions(0, chartShowDates);
Plot( Close, "\nPrice", colorWhite, styleCandle ); 
Plot(IIf(NW > j,NW,Null),"\ntrailShort",ParamColor("ColorTrailShort",ColorRGB(255,0,0)),styleStaircase);
Plot(IIf(NW < j,NW,Null),"\ntrailLong",ParamColor("ColorTrailLong",ColorRGB(0,255,0)),styleStaircase);

9 comments

over 15 years ago

see link in code for the original code. This code contains signals. It all depends how you want to trade, either pullbacks within the trend or at the point the trend changes.

over 15 years ago

Hi empottasch,
I have a system that gives entries and exits and I want to use this code just as trail and maybe as a trend filter as well. Can you please tell me how to do that?
Thanks,
Rafael

over 15 years ago

Hi empottasch,
I have a system that gives entries and exits and I want to use this code just as trail and maybe as a trend filter as well. Can you please tell me how to do that?
Thanks,
Rafael

over 15 years ago

for your filter question you can use (in case of Long trades) for example:

trendLong = IIf(NW < j,NW,Null);

so if you have your buy signals defined as:

Buy = your buy signal definition;

you add:

Buy = your buy signal definition AND !IsEmpty(trendLong);

to use it as a trail there are many ways to do this. Simple example is e.g. in the Long case:

Buy = your buy signal definition;
Sell = your sell signal definition OR Cross(NW,j);

over 15 years ago

I used this code also to define trend channels, see: http://www.youtube.com/watch?v=HWPSqwEkN64

about 15 years ago

hi empottash,
could you plz suggest me an afl formula with indicators .i actually need it with mano calculation

8. nicky
about 15 years ago

Sir this afl is not working in an hourly charts. Can u pls suggest if we can use this in hourly charts or not.

about 15 years ago

should work in any timeframe. It works for me with hourly charts with no problem.

Leave Comment

Please login here to leave a comment.