// Downloaded From https://www.WiseStockTrader.com
// Supertrend - Translated from Kolier MQ4
// see: http://kolier.li/indicator/kolier-supertrend-indi
// translation in Amibroker AFL code by E.M.Pottasch, 2011
SetTradeDelays(0,0,0,0);
SetOption("CommissionMode",3);
SetOption("CommissionAmount",2.32);
SetOption("FuturesMode",True);
NumContracts=1;
PositionSize=NumContracts*MarginDeposit;
SetOption("MaxOpenPositions",4);

ATR_Multiplier=Param("ATR_Multiplier",2,0.5,10,0.1);
ATR_Period=Param( "ATR_Period",5,2,20,1);
TrendMode=ParamToggle("TrendMode","Off|On",1);

procedure calcTrend_proc(ATR_Period,tr,ATR_Multiplier,TrendMode,CalcPrice)
{
global buffer_line_down;
global buffer_line_up;
buffer_line_down = Null;
buffer_line_up = Null;

PHASE_NONE = 0;
PHASE_BUY = 1;
PHASE_SELL = -1;

phase=PHASE_NONE;
band_upper = 0;band_lower = 0;
  
for(i = ATR_Period + 1; i < BarCount; i++)
{
	band_upper = CalcPrice[i] + ATR_Multiplier * tr[i];
	band_lower = CalcPrice[i] - ATR_Multiplier * tr[i];
	
	if(phase==PHASE_NONE)
	{
		buffer_line_up[i] = CalcPrice[i];
		buffer_line_down[i] = CalcPrice[i];
	}
	if(phase!=PHASE_BUY && Close[i]>buffer_line_down[i-1] && !IsEmpty(buffer_line_down[i-1])) 
	{
		phase = PHASE_BUY;
		buffer_line_up[i] = band_lower;
		buffer_line_up[i-1] = buffer_line_down[i-1];
	}
	if(phase!=PHASE_SELL && Close[i]<buffer_line_up[i-1] && !IsEmpty(buffer_line_up[i-1]))
	{
		phase = PHASE_SELL;
		buffer_line_down[i] = band_upper;
		buffer_line_down[i-1] = buffer_line_up[i-1];
	}	
	if(phase==PHASE_BUY && ((TrendMode==0 && !IsEmpty(buffer_line_up[i-2])) || TrendMode==1) )
	{
		if(band_lower>buffer_line_up[i-1]) 
		{
			buffer_line_up[i] = band_lower;
		}
		else 
		{
			buffer_line_up[i] = buffer_line_up[i-1];
		}
	}
	if(phase==PHASE_SELL && ((TrendMode==0 && !IsEmpty(buffer_line_down[i-2])) || TrendMode==1) )
	{
		if(band_upper<buffer_line_down[i-1])
		{
			buffer_line_down[i] = band_upper;
		}
		else
		{
			buffer_line_down[i] = buffer_line_down[i-1];
		}
	}
}
}
 
tr = ATR(ATR_Period);
CalcPrice=(H+L)/2;
calcTrend_proc(ATR_Period,tr,ATR_Multiplier,TrendMode,CalcPrice);
TrendUp=buffer_line_up;TrendDown=buffer_line_down;
totalTrend=IIf(TrendUp,TrendUp,TrendDown);
dtotalTrend=totalTrend-Ref(totalTrend,-1);
vtotalTrend=ValueWhen(dtotalTrend,dtotalTrend);

if(TrendMode==1)
{
	Buy=vtotalTrend>0 AND Ref(vtotalTrend,-1)<0;BuyPrice=C;
	Sell=vtotalTrend<0 AND Ref(vtotalTrend,-1)>0;SellPrice=C;
	Buy=ExRem(Buy,Sell);Sell=ExRem(Sell,Buy);
	Cover=Buy;CoverPrice=BuyPrice;
	Short=Sell;ShortPrice=SellPrice;	
	nw_bull=Flip(Buy,Sell);
	nw_bear=Flip(Short,Cover);
}
else if(TrendMode==0)
{
	Buy=!IsEmpty(TrendUp) AND !IsEmpty(Ref(TrendUp,-1)) AND !IsEmpty(Ref(TrendDown,-1));BuyPrice=C;
	Sell=!IsEmpty(TrendDown) AND !IsEmpty(Ref(TrendUp,-1)) AND !IsEmpty(Ref(TrendDown,-1));SellPrice=C;
	Buy=ExRem(Buy,Sell);Sell=ExRem(Sell,Buy);
	Cover=Buy;CoverPrice=BuyPrice;
	Short=Sell;ShortPrice=SellPrice;
	nw_bull=Flip(Buy,Sell);
	nw_bear=Flip(Short,Cover);
}

SetChartOptions(0, chartShowDates);
Title="Symbol: "+ Name();
Plot(C,"Close",colorLightGrey,styleCandle);
Plot(IIf(nw_bull,1,Null),"",ColorRGB(0,20,0),styleArea|styleOwnScale,0,1,0,-1);
Plot(IIf(nw_bear,1,Null),"",ColorRGB(20,0,0),styleArea|styleOwnScale,0,1,0,-1);
Plot(buffer_line_up,"\ntu",colorBlue,styleThick);
Plot(buffer_line_down,"\ntd",colorRed,styleThick);

PlotShapes(IIf(Buy,shapeUpArrow,shapeNone),colorDarkGreen,0,L,-15);
PlotShapes(IIf(Buy,shapeSmallCircle,shapeNone),colorWhite,0,BuyPrice,0);
PlotShapes(IIf(Sell,shapeDownArrow,shapeNone),colorRed,0,H,-15);
PlotShapes(IIf(Sell,shapeSmallCircle,shapeNone),colorWhite,0,SellPrice,0);
PlotShapes(IIf(Short,shapeSmallDownTriangle,shapeNone),colorRed,0,H,IIf(Short AND Sell,-30,-15));
PlotShapes(IIf(Short,shapeSmallCircle,shapeNone),colorWhite,0,ShortPrice,0);
PlotShapes(IIf(Cover,shapeSmallUpTriangle,shapeNone),colorDarkGreen,0,L,IIf(Cover AND Buy,-30,-15));
PlotShapes(IIf(Cover,shapeSmallCircle,shapeNone),colorWhite,0,CoverPrice,0);