// Downloaded From https://www.WiseStockTrader.com
// STAMP CODE NAME 
GfxSelectFont("Tahoma", 15, 700 ); 
GfxSetBkColor( colorGreen );
GfxSetTextColor(colorBlack);
GfxTextOut("High Low Trailing Stop",Status("pxwidth")/1.25,Status("pxheight")/50);
GraphXSpace=5;

//====== Original TT by X ========
//------ Setting -----------------
SetOption("InitialEquity",1000000);
SetTradeDelays( 0, 0, 0, 0 );
RoundLotSize = 100;
BuyPrice=Close;
SellPrice=Close;
SetOption("maxopenpositions",1);
PositionSize=-100;

PcntOffset = 0;
function Hstop_func()
{
	trailArray[ 0 ] = L[ 0 ]; // initialize
	for( i = 1; i < BarCount; i++ )
	{
		prev = trailArray[ i - 1 ];
 
		if (C[ i ] >= prev AND C[ i - 1 ] >= prev)// Long Side This Day and Previous Day > Trailing
		{

			if(H[i]>H[i-1])// Make New High
				trailArray[ i ] = (Max(prev,L[i-1]))*(1-PcntOffset/100);
			else // No New high
				trailArray[ i ] = prev;
		}
		else if (C[ i ] < prev AND C[ i - 1 ] < prev)// Short Side This Day and Previous Day < Trailing
		{
			if(L[i]<L[i-1])
				trailArray[ i ] = (Min(prev,H[i-1]))*(1+PcntOffset/100);
			else
				trailArray[ i ] = prev;
		}
		else if (C[ i ] < prev AND C[ i - 1 ] >= prev)// Short Point
		{
			trailArray[ i ] = (Max(H[i],H[i-1]))*(1+PcntOffset/100);//C[ i ] - trBull[ i ];			
			stoploss[i-1]=1;
		}
		else //Long Point
		{
			trailArray[ i ] = (L[i])*(1-PcntOffset/100);//C[ i ] + trBear[ i ];	
			
		}
	}
	return trailArray;
}


trailArray = Hstop_func();

Buy = Cross(C,trailarray);
Sell = Cross(trailarray,C);



//====== Ploting ========================================

// ----- Chart Optiion ----------------------------------
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
SetChartOptions(0,chartShowDates);


//------ Channel ----------------------------------------
Plot( Close, "Price", IIf( Close > Open, colorDarkGrey, colorDarkGrey ), styleCandle );
Plot( trailarray,"Stop Loss",IIf(C<trailarray,colorRed,colorBlack),styleStaircase+styleDots);

// ----- Buy and Sell -----------------------------------
Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);
                    
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorGreen, 0,L, Offset=-45);                      
PlotShapes(IIf(Sell, shapeDownArrow, shapeNone),colorRed, 0,H, Offset=-45);