// Downloaded From https://www.WiseStockTrader.com
_SECTION_BEGIN("Oz Trail Stop Long");
// E.M.Pottasch, Feb 2014
// based on http://www.tradernexus.com/advancedstop/advancedstop.html

_N(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);
entryPrice=Param("Entry Price Trade",0,0,150000,0.01); //entry price
trg1=ParamTrigger("Save Changes", "Click Here"); 

if(entryPrice==0)
{
	ttt=IIf(DateNum()>=selectDate,1,0);
	ttt=ttt-Ref(ttt,-1);
	entryPrice=LastValue(ValueWhen(ttt,C));
}
function conDate(nDate)
{
	result="";
	string=StrFormat("%0.9g",nDate);
	aa=StrLeft(string,3);
	mm=StrMid(string,3,2);
	dd=StrRight(string,2);
	aa1=StrToNum(aa)+1900; // ONLY CORRECT AFTER 2000
	result= dd + "/" + mm + "/" + NumToStr(aa1,1,False);
	return result;
}
function PersistentArraySet(VarName,infoArray)
{
	global PersistentPath;
	fh=fopen( PersistentPath+VarName+".pva","w" );
	if(fh)
	{
		cnt=0;
		while(!IsEmpty(infoArray[cnt]))
		{
			String=NumToStr(infoArray[cnt])+"\n";
			fputs( String,fh);
			cnt=cnt+1;
		}
		fclose(fh);
	}
}
function PersistentArrayGet(VarName)
{
	global PersistentPath;
	infoArray=0;
	fh = fopen( PersistentPath+VarName+".pva","r" );
	if(fh)
	{
		cnt=0;
		while(!feof(fh)) 
		{ 
			infoArray[cnt]=StrToNum(fgets( fh ));
			cnt=cnt+1;
		} 
		fclose(fh);
	}
	return infoArray;
}

infoArray=Null;
infoArray[0]=selectdate;
infoArray[1]=per1;
infoArray[2]=fac1;
infoArray[3]=tog1;
infoArray[4]=LastValue(entryPrice);

sdatep=PersistentArrayGet("ss"+Name());
lvsp=LastValue(Cum(sdatep));

if(lvsp==0)PersistentArraySet("ss"+Name(),infoArray);
if(trg1)PersistentArraySet("ss"+Name(),infoArray);

function vstopLong_func(trBull,tog1,per,idx,entryPrice)
{
	trailArray=0;
	if(tog1)
	{
		hh=H;
		ll=L;
	}
	else
	{
		hh=C;
		ll=C;	
	}

	trailArray[idx]=entryPrice-trBull[idx];

	// calculate trail
	if((idx+1)<BarCount AND idx>0)
	{
		cnt=0;	
		for(i=idx+1;i<BarCount;i++)
		{
			prev=trailArray[i-1];cnt=i;
 
			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 trigger
			{
				break;
			}
		}
		for(i=cnt;i<BarCount;i++)
		{
			trailArray[i]=trailArray[i-1];
		}		
	}
	return trailArray;	
}

sdatep=PersistentArrayGet("ss"+Name());

selectDateArr=sdatep[0];
per1Arr=sdatep[1];
fac1Arr=sdatep[2];
tog1Arr=sdatep[3];
entryPriceArr=sdatep[4];
bi=BarIndex();tt=IIf(DateNum()>=selectDateArr,1,0);
tt=tt-Ref(tt,-1);idx=LastValue(ValueWhen(tt,bi));
trBull=fac1Arr*ATR(per1Arr);
trailArray=vstopLong_func(trBull,tog1Arr,per1Arr,idx,entryPriceArr);trailArray=IIf(trailArray==0,Null,trailArray);
	
if(tog1Arr==0)
	trailRef="Close";
else
	trailRef="High";

Title = "Entry Date: " + conDate(selectDateArr) +"\n"+
"ATR Period: " + per1Arr +"\n"+
"ATR Multiple: " + fac1Arr +"\n"+
"Trail reference: " + trailRef +"\n"+
"Entry Price: " + entryPriceArr +"\n";

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(trailArray,"\ntrailLong",ColorRGB(0,255,0),styleStaircase);
_SECTION_END();