// Downloaded From https://www.WiseStockTrader.com
procedure exit_proc(Buy,BuyPrice,TrailLevel,StopLevel)
{
global BuyHelpArray;
global BuyPriceHelpArray;
global SellHelpArray;
global SellPriceHelpArray;
global LongStopTrailArray;
global FlagStorageArray;
global exitTrigger;

BuyHelpArray=0;
BuyPriceHelpArray=0;
SellHelpArray=0;
SellPriceHelpArray=0;
LongStopTrailArray=Null;
FlagStorageArray=0;
exitTrigger=0;

delay=1;
slip=TickSize*0;

for(i=1;i<BarCount;i++) 
{
	if(Buy[i]) 
	{
		BuyHelpArray[i]=1;
		BuyPriceHelpArray[i]=BuyPrice[i]+slip;	
		LongStopTrailArray[i]=BuyPriceHelpArray[i]-BuyPriceHelpArray[i]/100*StopLevel;
		LongStopTrailArray[i-1]=LongStopTrailArray[i];
		FlagStorageArray[i]=1;
		
		for(j=i+delay;j<BarCount;j++) 
		{	
			LongStopTrailArray[j]=Max(H[j]-H[j]/100*TrailLevel[j],LongStopTrailArray[j-1]);
			if(LongStopTrailArray[j]<=LongStopTrailArray[i]) FlagStorageArray[j]=1;
			else FlagStorageArray[j]=2;
			
			if(FlagStorageArray[j]==1 AND L[j]<LongStopTrailArray[j])
			{
				SellHelpArray[j]=1;
				SellPriceHelpArray[j]=Min(O[i],LongStopTrailArray[j])-slip;
				i=j;
				break;	
			}
			else if(FlagStorageArray[j]==2 AND C[j-1]<LongStopTrailArray[j-1])
			{
				SellHelpArray[j]=1;
				SellPriceHelpArray[j]=O[j]-slip;
				i=j;
				break;
			}
			else if(FlagStorageArray[j]==2 AND C[j]<LongStopTrailArray[j])
			{
				exitTrigger[j]=1;
			}			
			else if(j==BarCount-1) 
			{
				i=BarCount;
				break;				
			}
		}
	}
}
}

//SetBarsRequired(sbrAll,0);
//TrailLevel=Param("Trail stop %",20,5,20,1);
TrailLevel = IIf(RSI(15)>70,7,10);
StopLevel=Param("Stop loss %",5,5,20,1);

BuyTrigger=Cross(EMA(Close,10),EMA(Close,25));
Buy=Ref(BuyTrigger,-1);BuyPrice=Open;

exit_proc(Buy,BuyPrice,TrailLevel,StopLevel);
Buy=BuyHelpArray;
BuyPrice=BuyPriceHelpArray;
Sell=SellHelpArray;
SellPrice=SellPriceHelpArray;

BuyTrigger=ExRem(BuyTrigger,Sell);

GraphXSpace=5;SetChartBkColor(ColorRGB(0,0,0));SetChartOptions(0,chartShowDates);
SetBarFillColor(IIf(C>O,colorGreen,IIf(C<=O,colorRed,colorLightGrey)));
Plot(C,"Price",IIf(C>O,colorDarkGreen,IIf(C<=O,colorDarkRed,colorLightGrey)),64,0,0,0,0);
PlotShapes(IIf(BuyTrigger,shapeSmallCircle,shapeNone),colorBrightGreen,0,L,-15);
PlotShapes(IIf(ExitTrigger,shapeSmallCircle,shapeNone),colorOrange,0,H,15);
PlotShapes(IIf(Buy,shapeUpArrow,shapeNone),colorDarkGreen,0,L,-15);
PlotShapes(IIf(Buy,shapeSmallCircle,shapeNone),colorLightBlue,0,BuyPrice,0);
PlotShapes(IIf(Sell,shapeDownArrow,shapeNone),colorRed,0,H,-15);
PlotShapes(IIf(Sell,shapeSmallCircle,shapeNone),colorYellow,0,SellPrice,0);
Plot(LongStopTrailArray,"",IIf(FlagStorageArray==1,colorRed,colorBlue),1);