Fibonacci Retracement Based On previous 5 Day High Low

_SECTION_BEGIN(" Fib Retracements");
	fibs = ParamToggle("Plot Fibs","Off|On",1);
	pctH = Param ("Pivot Hi %", 0.647,0.001,2.0,0.002);
	HiLB = Param ("Hi LookBack",1,1,BarCount-1,1);
	pctL = Param ("Pivot Lo %", 0.647,0.001,2.0,0.002);
	LoLB = Param ("Lo LookBack",1,1,BarCount-1,1);
	Back = Param ("Extend Left = 2",1,1,500,1);
	Fwd  = Param("Plot Forward", 0, 0, 500, 1);
	text = ParamToggle("Plot Text","Off|On",1);
	hts  = Param ("Text Shift", -33.5,-50,50,0.10);
	style =ParamStyle("Line Style",styleLine,styleNoLabel);
x = BarIndex();
pRp  = PeakBars( H, pctH, 1) == 0;
yRp0 = SelectedValue(ValueWhen( pRp, H, HiLB));
xRp0 = SelectedValue(ValueWhen( pRp, x, HiLB));
pSp  = TroughBars( L, pctL, 1) == 0;
ySp0 = SelectedValue(ValueWhen( pSp, L, LoLB));
xSp0 = SelectedValue(ValueWhen( pSp, x, LoLB));
Delta = yRp0 - ySp0;

function fib(ret)
{
retval = (Delta * ret);
Fibval = IIf(ret < 1.0 
AND xSp0 < xRp0, yRp0 - retval, IIf(ret < 1.0 
AND xSp0 > xRp0, ySp0 + retval,IIf(ret > 1.0 
AND xSp0 < xRp0, yRp0 - retval, IIf(ret > 1.0 
AND xSp0 > xRp0, ySp0 + retval, Null)))); 
return FibVal;
}

x0 = Min(xSp0,xRp0)-Back;
x1 = (BarCount -1);
//////////////////////////////////////////////////////////////////
r236 = fib(0.236);	r236I = LastValue (r236,1);
r382 = fib(0.382);	r382I = LastValue (r382,1);
r050 = fib(0.50);		r050I = LastValue (r050,1);
r618 = fib(0.618);	r618I = LastValue (r618,1);
r786 = fib(0.786);	r786I = LastValue (r786,1);
e127 = fib(1.27);		e127I = LastValue (e127,1);
e162 = fib(1.62);		e162I = LastValue (e162,1);
e200 = fib(2.00);		e200I = LastValue (e200,1);
e262 = fib(2.62);		e262I = LastValue (e262,1);
e424 = fib(4.24);		e424I = LastValue (e424,1);
//////////////////////////////////////////////////////////////////
p00 = IIf(xSp0 > xRp0,ySp0,yRp0); 	p00I = LastValue (p00,1);
p100 = IIf(xSp0 < xRp0,ySp0,yRp0); 	p100I = LastValue (p100,1);
color00 =IIf(xSp0 > xRp0,colorLime,colorRed);
color100 =IIf(xSp0 < xRp0,colorLime,colorRed);
//////////////////////////////////////////////////////////////////
numbars = LastValue(Cum(Status("barvisible")));
fraction= IIf(StrRight(Name(),3) == "", 3.2, 3.2);
//////////////////////////////////////////////////////////////////
if(fibs==1)
{
Plot(LineArray(xRp0-Fwd,yRp0,x1,yRp0,Back),"PR",32,8|styleNoRescale,Null, Null,Fwd);
Plot(LineArray(xSp0-Fwd,ySp0,x1,ySp0,Back),"PS",27,8|styleNoRescale,Null, Null,Fwd);

if(text==1)
{
PlotText(" 0% = " + WriteVal(p00,fraction),     LastValue(BarIndex())-(numbars/hts), p00I  + 0.05, colorWhite);
//PlotText("23% = " + WriteVal(r236,fraction), LastValue(BarIndex())-(numbars/hts), r236I + 0.05,colorWhite);
PlotText("38% = " + WriteVal(r382,fraction), LastValue(BarIndex())-(numbars/hts), r382I + 0.05, colorWhite);
PlotText("50% = " + WriteVal(r050,fraction), LastValue(BarIndex())-(numbars/hts), r050I + 0.05, colorWhite);
PlotText("62% = " + WriteVal(r618,fraction), LastValue(BarIndex())-(numbars/hts), r618I + 0.05, colorWhite);
PlotText("78% = " + WriteVal(r786,fraction), LastValue(BarIndex())-(numbars/hts), r786I + 0.05, colorWhite);
PlotText("100% = " + WriteVal(p100,fraction), LastValue(BarIndex())-(numbars/hts),p100I + 0.05, colorWhite);
//PlotText("127% = " + WriteVal(e127,fraction), LastValue(BarIndex())-(numbars/hts),e127I + 0.05, colorWhite);
//PlotText("162% = " + WriteVal(e162,fraction), LastValue(BarIndex())-(numbars/hts),e162I + 0.05, colorWhite);
//PlotText("200% = " + WriteVal(e200,fraction), LastValue(BarIndex())-(numbars/hts),e200I + 0.05, colorWhite);
//PlotText("262% = " + WriteVal(e262,fraction), LastValue(BarIndex())-(numbars/hts),e262I + 0.05, colorWhite);
//PlotText("424% = " + WriteVal(e424,fraction), LastValue(BarIndex())-(numbars/hts),e424I + 0.05, colorWhite);
}

prev=AMA2(C,1,0);
d=IIf(C>Ref(Max(Max(H,Ref(H,-20)),Max(Ref(H,-10),Ref(H,-10))),-1),Min(Min(L,Ref(L,-20)),Min(Ref(L,-10),Ref(L,-15))),
  IIf(C<Ref(Min(Min(L,Ref(L,-20)),Min(Ref(L,-10),Ref(L,-15))),-1),Max(Max(H,Ref(H,-20)),Max(Ref(H,-10),Ref(H,-15))),PREV));
a=Cross(Close,d);
b=Cross(d,Close);
state=IIf(BarsSince(a)<BarsSince(b),1,0);
s=state>Ref(state,-1);
ss=state<Ref(state,-1);
sss=state==Ref(state,-1);
col=IIf(state == 1 ,51,IIf(state ==0,4,1));
Plot(C,"",Col,128);
//PlotShapes( shapeUpArrow * s ,6,0,L); 
//PlotShapes( shapeDownArrow *ss ,4,0,H);
Buy=S;
Sell=SS;
Filter =  s OR sss OR sss  ;
AddColumn(C,"close",1.2);
AddColumn( IIf( s, 66,1 ), "buy", formatChar, 1, bkcolor =IIf (s,colorYellow, colorPink ));
AddColumn( IIf( Ss, 83,1 ), "sell", formatChar, 1, bkcolor =IIf (Ss,colorPink, colorYellow ));
AddColumn( IIf( sss, 87,1 ), "wait", formatChar, 1, bkcolor =IIf (sss,colorYellow, colorRed ));
}
_SECTION_END();

This is the regular Fibonacci I need to convert Pcth & pctL should be previous 5 days high & low & start the line of resistant or support from that point in daily Time Frame.
If possible it should be extend for last 5 to 50 days.
Please help to code for the same.
Thanks in advance.

1 Like