Stock Portfolio Organizer

The ultimate porfolio management solution.

Shares, Margin, CFD's, Futures and Forex
EOD and Realtime
Dividends and Trust Distributions
And Much More ....
For Portfolio Manager Click Here

WiseTrader Toolbox

#1 Selling Amibroker Plugin featuring:

Advanced Adaptive Indicators
Advanced Pattern Exploration
Neural Networks
And Much More ....
Find Out More Here

trail compare for Amibroker (AFL)

Copy & Paste Friendly
// E.M.Pottasch, Jan 2014
per1=Param("Length ATR",20,1,150,1); // ATR length
fac1=Param("Chandelier Factor",2,1,10,0.1); // chandelier factor
tog1=ParamToggle("Trail value","Close|High&Low",1);
tog2=ParamToggle("Trail method","Chandelier|VSTOP",0);

///////////// Chandelier code by E.M.Pottasch, adapted from Chuck Lebeau's code
function vstop_func(trBull,trBear,hl,per)
{
	trailArray=0;
	if(hl)
	{
		hh=H;
		ll=L;
	}
	else
	{
		hh=C;
		ll=C;	
	}
	for(i=per+1;i<BarCount;i++)
	{
		prev=trailArray[i-1];
 
		if (C[i]>=prev AND C[i-1]>=prev)//long continuation
		{
			trailArray[i]=Max(prev,hh[i]-trBull[i]);
		}
		else if (C[i]<=prev AND C[i-1]<=prev)//short continuation
		{
			trailArray[i]=Min(prev,ll[i]+trBear[i]);
		} 
		else if (C[i]>prev AND C[i-1]<=prev)//long trigger 
		{
			trailArray[i]=hh[i]-trBull[i];
		}
		else if (C[i]<prev AND C[i-1]>=prev)//short trigger
		{
			trailArray[i]=ll[i]+trBear[i];	
		}
	}
	return trailArray;
}
///////////// end Chandelier code by E.M.Pottasch, adapted from Chuck Lebeau's code
///////////// Chandelier code by Geoff Mulhall
function aChandelierCl(AtrARRAY, AtrMult) {
// Skip empty values
i = 0;
do {result[i] = Null;
	i++;
	} 
while( i < BarCount AND (IsNull(O[i]) OR IsNull(H[i]) OR IsNull(L[i]) OR
IsNull(C[i]) ) ); 
First = i;

if (i < BarCount - 1) {
	HHC[First]    = C[First];
	LLC[First]    = C[First];

	if (C[First + 1] > HHC[First]) {
		HHC[First + 1] = C[First + 1];
		LLC[First + 1] = LLC[First];
		result[First] = C[First] - AtrMult * AtrARRAY[First];
		iTrade = "LT";
	}
	else {
		if (C[First + 1] < LLC[First]) {
			HHC[First = 1] = HHC[First];
			LLC[First + 1] = LLC[First + 1];
			result[First] = C[First] + AtrMult * AtrARRAY[First];
			iTrade = "ST";
		}
		else {
			HHC[First + 1] = C[First + 1];
			LLC[First + 1] = C[First + 1];
			result[First] = C[First] - AtrMult * AtrARRAY[First];
			iTrade = "LT";
		}
	}

	for( i = First; i < BarCount; i++ ) {
		if (iTrade == "LT") {
			if (C[i] >= result[i-1]) {        // Long Trade is continuing
				if (C[i] > C[i-1]) {
					HHC[i] = C[i];
				}
				else { 
					HHC[i] = HHC[i-1];
				}
				result[i] = HHC[i] - AtrMult * AtrARRAY[i];
				if (result[i] < result[i-1]) {
					result[i] = result[i-1];
				}
			}
			else {                            // Long trade Exit triggered
				iTrade = "ST";
				LLC[i] = C[i];
				result[i] = C[i] + AtrMult * AtrARRAY[i];
			}
		}
		else {                               // Short trade
			if (C[i] <= result[i-1]) {
				if (C[i] <= C[i-1]) {        // Short Trade is continuing
					LLC[i] = C[i];
				}
				else {
					LLC[i] = LLC[i-1];
				}
				result[i] = LLC[i] + AtrMult * AtrARRAY[i];
				if (result[i] > result[i-1]) {
					result[i] = result[i-1];
				}
			}
			else {                           //Short Trade Exit is triggered
				iTrade = "LT";
				HHC[i]  = C[i];
				result[i] = C[i] - AtrMult * AtrARRAY[i];
			}
		}
	}
}
return result;
}
function aChandelierHL(AtrARRAY, AtrMult) {
// Skip empty values
i = 0;
do {result[i] = Null;
	i++;
	} 
while( i < BarCount AND (IsNull(O[i]) OR IsNull(H[i]) OR IsNull(L[i]) OR
IsNull(C[i]) ) ); 
First = i;

if (i < BarCount - 1) {
	HHC[First]    = H[First];
	LLC[First]    = L[First];

	if (H[First + 1] > HHC[First]) {
		HHC[First + 1] = H[First + 1];
		LLC[First + 1] = LLC[First];
		result[First] = H[First] - AtrMult * AtrARRAY[First];
		iTrade = "LT";
	}
	else {
		if (L[First + 1] < LLC[First]) {
			HHC[First = 1] = HHC[First];
			LLC[First + 1] = LLC[First + 1];
			result[First] = L[First] + AtrMult * AtrARRAY[First];
			iTrade = "ST";
		}
		else {
			HHC[First + 1] = C[First + 1];
			LLC[First + 1] = C[First + 1];
			result[First] = H[First] - AtrMult * AtrARRAY[First];
			iTrade = "LT";
		}
	}

	for( i = First; i < BarCount; i++ ) {
		if (iTrade == "LT") {
			if (C[i] >= result[i-1]) {        // Long Trade is continuing
				if (H[i] > H[i-1]) {
					HHC[i] = H[i];
				}
				else { 
					HHC[i] = HHC[i-1];
				}
				result[i] = HHC[i] - AtrMult * AtrARRAY[i];
				if (result[i] < result[i-1]) {
					result[i] = result[i-1];
				}
			}
			else {                            // Long trade Exit triggered
				iTrade = "ST";
				LLC[i] = L[i];
				result[i] = L[i] + AtrMult * AtrARRAY[i];
			}
		}
		else {                               // Short trade
			if (C[i] <= result[i-1]) {
				if (L[i] <= L[i-1]) {        // Short Trade is continuing
					LLC[i] = L[i];
				}
				else {
					LLC[i] = LLC[i-1];
				}
				result[i] = LLC[i] + AtrMult * AtrARRAY[i];
				if (result[i] > result[i-1]) {
					result[i] = result[i-1];
				}
			}
			else {                           //Short Trade Exit is triggered
				iTrade = "LT";
				HHC[i]  = H[i];
				result[i] = H[i] - AtrMult * AtrARRAY[i];
			}
		}
	}
}
return result;
}
///////////// end Chandelier code by Geoff Mulhall

if(!tog2)
{
	if(tog1) trailArray=aChandelierHL(ATR(per1),fac1);
	else trailArray=aChandelierCl(ATR(per1),fac1);
}
else
{
	trBull=fac1*ATR(per1);
	trBear=fac1*ATR(per1);
	trailArray=vstop_func(trBull,trBear,tog1,per1);trailarray=IIf(trailarray==0,Null,trailarray);
}

GraphXSpace=5;SetChartBkColor(colorBlack);SetChartOptions(0,chartShowDates);
SetBarFillColor(IIf(C>O,ColorRGB(0,75,0),IIf(C<=O,ColorRGB(75,0,0),colorLightGrey)));
Plot(C,"\nPrice",IIf(C>O,ColorRGB(0,255,0),IIf(C<=O,ColorRGB(255,0,0),colorLightGrey)),64);
Plot(IIf(trailArray>C,trailArray,Null),"\ntrailShort",ParamColor("ColorTrailShort",ColorRGB(255,0,0)),styleStaircase);
Plot(IIf(trailArray<C,trailArray,Null),"\ntrailLong",ParamColor("ColorTrailLong",ColorRGB(0,255,0)),styleStaircase);
Back