// Downloaded From https://www.WiseStockTrader.com
/*//////////////////
// Amibroker AFL Code by E.M.Pottasch, Jan 2014
// Using Range Markers to display Fibonacci Levels
// 1) Set start range Marker using F12 => go with mouse/cursor to starting peak/trough, click on chart, then press F12 
// 2) Set end range market using SHIFT-F12 => go with mouse/cursor to ending peak/trough, click on chart, then press SHIFT-F12 
// 3) Clear, remove range markers and remove Fibs using CTRL-F12
*///////////////////



//////////////////// BEGIN PARAMETER DEFINITIONS /////////////////////
nlev=Param("Number of Fib Levels to display",10,0,12,1);
tog1=ParamToggle("Show Primary Fib Levels","Off|On",1);
tog2=ParamToggle("Show Secondary Fib Levels","Off|On",1);
//////////////////// END PARAMETER DEFINITIONS /////////////////////



//////////////////// BEGIN COLOR DEFINITIONS /////////////////////
fibLevelUpRetraceColor=ColorRGB(200,0,0);
fibLevelDnRetraceColor=ColorRGB(0,200,0);
primaryValueLabelColor=ColorRGB(160,160,160);
secondaryValueLabelColor=ColorRGB(80,80,80);
chartBackgroundColor=ColorRGB(0,0,0);
//////////////////// END COLOR DEFINITIONS /////////////////////



//////////////////// BEGIN CHART DISPLAY /////////////////////
SetChartOptions(0, chartShowDates);SetChartBkColor(chartBackgroundColor);
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,Null,Null,0,0,0);
//////////////////// END CHART DISPLAY /////////////////////



//////////////////// BEGIN FIBONACCI LEVEL DISPLAY /////////////////////
bi=BarIndex();bvi=BeginValue(bi);evi=EndValue(bi);
rng=Flip(bvi==bi,evi==bi);
//"Primary numbers: 0.618, 0.786, 1.27 AND 1.618" =>levType=1
//"Secondary numbers: 0.382, 0.50, 1.00, 2.00, 2.24, 2.618 AND 3.14" =>levType=2
fibLevel=0;levType=0;
fibLevel[0]=0;levType[0]=3;
fibLevel[1]=0.382;levType[1]=2;
fibLevel[2]=0.5;levType[2]=2;
fibLevel[3]=0.618;levType[3]=1;
fibLevel[4]=0.786;levType[4]=1;
fibLevel[5]=1;levType[5]=2;
fibLevel[6]=1.272;levType[6]=1;
fibLevel[7]=1.618;levType[7]=1;
fibLevel[8]=2;levType[8]=2;
fibLevel[9]=2.236;levType[9]=2;
fibLevel[10]=2.618;levType[10]=2;
fibLevel[11]=3.14;levType[11]=2;

bvl=BeginValue(L);bvh=BeginValue(H);
evl=EndValue(L);evh=EndValue(H);rr=EndValue(bi);

idx=LastValue(rr);
rridx=IIf(bi>=idx,1,0);

if(bvl>evl)
{
	sit=1;//peak first
	rr=ValueWhen(bi==BeginValue(bi),H);
	ss=ValueWhen(bi==EndValue(bi),L);
	rr1=IIf(ss,rr,Null);ss1=ss;
}
else
{
	sit=2;//trough first
	ss=ValueWhen(bi==BeginValue(bi),L);
	rr=ValueWhen(bi==EndValue(bi),H);
	ss1=IIf(rr,ss,Null);rr1=rr;
}

if(bvi!=0 AND evi!=(BarCount-1))
{
rr2=IIf(rng,LastValue(rr),Null);
ss2=IIf(rng,LastValue(ss),Null);
Plot(rr2,"",colorRed,1);
Plot(ss2,"",colorGreen,1);
if(sit==1) Plot(LineArray(bvi,H[bvi],evi,L[evi],0),"",colorBlue,styleDashed);
if(sit==2) Plot(LineArray(bvi,L[bvi],evi,H[evi],0),"",colorBlue,styleDashed);
delta=abs(rr-ss);
fvb=Status("firstvisiblebar");
for(i=0;i<nlev;i++)
{
	if(sit==1)
	{
		if(tog1 AND levType[i]==1)//primary level
		{
			ll=ss1+delta*fibLevel[i];
			Plot(IIf(ss1,ll,Null),"",fibLevelUpRetraceColor,1);
			str=NumToStr(fibLevel[i]*100);str=StrMid(str,0,StrFind(str,".")+1);
			PlotText(""+str+"%",BarCount,LastValue(ll),primaryValueLabelColor);
		}
		if(tog2 AND levType[i]==2)//secondary level
		{
			ll=ss1+delta*fibLevel[i];
			Plot(IIf(ss1,ll,Null),"",fibLevelUpRetraceColor,styleDashed);
			str=NumToStr(fibLevel[i]*100);str=StrMid(str,0,StrFind(str,".")+1);
			PlotText(""+str+"%",BarCount,LastValue(ll),secondaryValueLabelColor);
		}	
		if(levType[i]==3)//basic level
		{
			ll=ss1+delta*fibLevel[i];
			Plot(IIf(ss1,ll,Null),"",fibLevelUpRetraceColor,styleThick);
			str=NumToStr(fibLevel[i]*100);str=StrMid(str,0,StrFind(str,".")+1);
			PlotText(""+str+"%",BarCount,LastValue(ll),primaryValueLabelColor);
		}		
	}
	else if(sit==2)
	{
		if(tog1 AND levType[i]==1)//primary
		{	
			ll=rr1-delta*fibLevel[i];
			Plot(IIf(rr1,ll,Null),"",fibLevelDnRetraceColor,1);
			str=NumToStr(fibLevel[i]*100);str=StrMid(str,0,StrFind(str,".")+1);
			PlotText(""+str+"%",BarCount,LastValue(ll),primaryValueLabelColor);
		}
		if(tog2 AND levType[i]==2)//secondary
		{	
			ll=rr1-delta*fibLevel[i];
			Plot(IIf(rr1,ll,Null),"",fibLevelDnRetraceColor,styleDashed);
			str=NumToStr(fibLevel[i]*100);str=StrMid(str,0,StrFind(str,".")+1);
			PlotText(""+str+"%",BarCount,LastValue(ll),secondaryValueLabelColor);
		}	
		if(levType[i]==3)//basic
		{	
			ll=rr1-delta*fibLevel[i];
			Plot(IIf(rr1,ll,Null),"",fibLevelDnRetraceColor,styleThick);
			str=NumToStr(fibLevel[i]*100);str=StrMid(str,0,StrFind(str,".")+1);
			PlotText(""+str+"%",BarCount,LastValue(ll),primaryValueLabelColor);
		}		
	}
}
}
//////////////////// END FIBONACCI LEVEL DISPLAY /////////////////////