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

Oz trail for Amibroker (AFL)

Copy & Paste Friendly
_SECTION_BEGIN("Oz trail");
// E.M.Pottasch, Jan 2014

PersistentPath="C:\\Program Files\\AmiBroker64\\PersistentVariables\\";  // **** You need to create a directory to store the persistent variable ****

selectDate=ParamDate("Start date","08/01/2011",0);
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);
trg1=ParamTrigger("Remove All Persistent Variables", "Click Here"); 

//persistant variables by Herman van Bergen
function PersistentVarSet( VarName, Number )
{
VarName=VarName+Name()+GetChartID();
global PersistentPath;
String = NumToStr(Number);
fh = fopen( PersistentPath+VarName+".pva","w" );
if( fh )
{
fputs( String, fh );
fclose( fh );
}
return fh;
}
function PersistentVarGet( VarName )
{
VarName=VarName+Name()+GetChartID();
global PersistentPath;
fh = fopen( PersistentPath+VarName+".pva","r" );
if( fh )
{
String = fgets( fh );
fclose( fh );
Number = StrToNum(String);
}
else Number = Null;
return Number;
}
function PersistentVarRemove( VarName )
{
VarName=VarName+Name()+GetChartID();
global PersistentPath;
Fn=PersistentPath + VarName + ".pva";
fh=fdelete( Fn ) ;
return fh;
}
function PersistentVarRemoveAll( VarName )
{
global PersistentPath;
Fn=PersistentPath + VarName + "*.*";
fh=fdelete( Fn ) ;
return fh;
}
if(trg1)
{
	PersistentVarRemoveAll( "ss" );
	//PersistentVarRemove( "ss" );
}

sdate=StaticVarGet("sdate");
sdatep=PersistentVarGet("ss");
"selectDate: " + WriteVal(selectdate);
"sdatep: " + WriteVal(sdatep); 
"sdate: " + WriteVal(sdate);

if(IsEmpty(sdatep) AND IsEmpty(sdate))
{
	StaticVarSet("sdate",selectdate);
	PersistentVarSet("ss",selectDate);
}
else if(!IsEmpty(sdatep) AND IsEmpty(sdate))
{
	StaticVarSet("sdate",selectdate);
}
else if(IsEmpty(sdatep) AND !IsEmpty(sdate))
{
	PersistentVarSet("ss",selectDate);
}
else if(sdate!=selectDate AND !IsEmpty(sdatep) AND !IsEmpty(sdate))
{
	StaticVarSet("sdate",selectdate);
	PersistentVarSet("ss",selectDate);
}

///////////// 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);
}

tt=IIf(DateNum()>=sdatep,1,0);
trailArray=IIf(tt,trailArray,Null);

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);
_SECTION_END();
Back