Help with Automatic Fibonacci Exploration Code Tweak

I am making small teaks to a common Automatic Fibonacci script to enable it too much much more effective with exploration. It works well for the most part but has one flaw that needs to be fixed when the higher pivot and low pivot to do coincide with the start of the range been above or below.

In order to place the internal retracement lines the correct way, the code says if(StartBar < FinishBar), then do it one way else the other way.

However, It is not entirely about whether the start and end periods are above or below, but where the HHV and LLV are within that range.

CORRECT
correct

INCORRECT
incorrect

So the code needs to factor in within the "range" StartBar to FinishBar, whether the HHV before or after the LLV, and then set the internal retracement accordingly.

_SECTION_BEGIN("Fib_Levels");

SetChartOptions(0,chartShowArrows|chartShowDates);

Plot(C,"", IIf(C>O,colorGreen,colorRed) , styleCandle);
// Get values for fib levels

//StartBar=SelectedValue(BarIndex());
FinishBar = EndValue( BarIndex() );
StartBar  = FinishBar-80;
i = startbar;
period = FinishBar - StartBar;

Lo = LLV(L,period);
Hi = HHV(H,period);
Line0 = 0;
Line1 = 0;
Line2 = 0;
Line3 = 0;
Line4= 0;
Line100 = 0;

for( i = startbar; i < finishbar; i++ )
{
if(StartBar < FinishBar)
{
Line0  = EndValue(Lo);
Line100 = EndValue(Hi);
Line1 = Line0 + abs(Line100-Line0)*0.236;
Line2 = Line0 + abs(Line100-Line0)*0.382;
Line3 = Line0 + abs(Line100-Line0)*0.5;
Line4 = Line0 + abs(Line100-Line0)*0.618;

}
else
{
Line0  = EndValue(Hi);
Line100 = EndValue(Lo);
Line1 =Line0 - abs(Line100-Line0)*0.236;
Line2 = Line0 - abs(Line100-Line0)*0.382;
Line3 = Line0 - abs(Line100-Line0)*0.5;
Line4 = Line0 - abs(Line100-Line0)*0.618;

}
}

// external fib lines begining fom selecetdbarindex()
fib100= LineArray(startbar, Line0, finishbar, Line0, 0, 1);
fib0 = LineArray(startbar, Line100, finishbar, Line100, 0, 1);

// depth of middle lines
n= round((finishbar-startbar)/2);

// middle lines
fib1= LineArray((finishbar-n), Line1, finishbar, Line1, 0, 1);
fib2= LineArray((finishbar-n), Line2, finishbar, Line2, 0, 1);
fib3= LineArray((finishbar-n), Line3, finishbar, Line3, 0, 1);
fib4= LineArray((finishbar-n), Line4, finishbar, Line4, 0, 1);

Plot(fib0,"Base", colorGrey50);
Plot(fib100,"Top", colorGrey50);
Plot(fib1,"23.6%", colorPink);
Plot(fib2,"38.2%", colorLightBlue);
Plot(fib3,"50%", colorRed);
Plot(fib4,"61.8%", colorGold);

PlotText(" 0.00% ",FinishBar+2, Line0,colorWhite);
PlotText(" 23.6% ",FinishBar+2, Line1,colorGreen);
PlotText(" 38.6% ",FinishBar+2, Line2,colorBlue);
PlotText(" 50% ",FinishBar+2, Line3,colorGold);
PlotText(" 61.8% ",FinishBar+2, Line4,colorAqua);
PlotText(" 100% ",FinishBar+2, Line100,colorRed);
1 Like