# Problem with IF funcion in Fibonacci AFL

Hi dear friends

Your amateur self-educated AFL prgrammer is back. Sorry to bother you.

I have been trying to use an automatic fibonnact AFL as a base for an exploration. I dont want it to draw lines but rather give me data about 1-where the price has closed in relation to the fibo levels that have originated from the HHV and LLV during n bars ago (say 120) and tell me the direction of the levels (ascending or descending). I want these to be shown in exploration columns.

Below is the original code I took from the net:

``````_SECTION_BEGIN("Automatic Fib Levels");
/*---------------------------------------------------
Automatic Fib Levels
Aron Pipa, December, 11, 2005
modified by Kook 30 December 2005
--------------------------------------------------------*/

GraphXSpace=2;
Plot(C,"", colorWhite,styleCandle);

// Get values for fib levels

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

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

for( i = startbar; i < finishbar; i++ )
{
if(EndValue(C)<SelectedValue(C))
{
Line0 = EndValue(Lo);
Line100 = EndValue(Hi);
Line1 = Line0 + abs(Line100-Line0)*0.382;
Line2 = Line0 + abs(Line100-Line0)*0.5;
Line3 = Line0 + abs(Line100-Line0)*0.618;
Line4 = Line0 + abs(Line100-Line0)*0.786;
Line5 = Line0 + abs(Line100-Line0)*1.618;
Line6 = Line0 + abs(Line100-Line0)*2.618;

}
else
{
Line100 = EndValue(Lo);
Line0 = EndValue(Hi);
Line1 =Line0 - abs(Line100-Line0)*0.382;
Line2 = Line0 - abs(Line100-Line0)*0.5;
Line3 = Line0 - abs(Line100-Line0)*0.618;
Line4 = Line0 - abs(Line100-Line0)*0.786;
Line5 = Line0 - abs(Line100-Line0)*1.618;
Line6 = Line0 - abs(Line100-Line0)*2.618;

}
}

// external fib lines begining fom selecetdbarindex()
fib0= LineArray(startbar, Line0, finishbar, Line0, 0, 1);
fib100 = 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);
fib5= LineArray((finishbar-n), Line5, finishbar, Line5, 0, 1);
fib6= LineArray((finishbar-n), Line6, finishbar, Line6, 0, 1);

Plot(fib0,"", colorWhite);
Plot(fib100,"", colorRed);
Plot(fib1,"38.6%=", colorOrange);
Plot(fib2,"50%=", colorPink);
Plot(fib3,"61.8%=", colorPaleGreen);
Plot(fib4,"78.6%=", colorYellow);
Plot(fib5,"161.8%=", colorSkyblue);
//Plot(fib6,"", colorPink);

Title = Name() + " - Auto FIB LEVELS "
+"\n"+EncodeColor(colorOrange)+"38.6% = "+line1
+"\n"+EncodeColor(colorPink)+"50% = "+line2
+"\n"+EncodeColor(colorPaleGreen)+"61.8% = "+line3
+"\n"+EncodeColor(colorYellow)+"78.6% = "+line4
+"\n"+EncodeColor(colorSkyblue)+"161.8%= "+line5
+"\n "
;
_SECTION_END();
``````

And this is the code I have wrote based on this framework:

`````` /*---------------------------------------------------
Automatic Fib Levels
Aron Pipa, December, 11, 2005
modified by Kook 30 December 2005
--------------------------------------------------------*/

// Get values for fib levels

period = 120;

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

BL=LLVBars(L,period);
BH=HHVBars(H,period);
THL=BH-BL;

Fibcolor=IIf(THL>0,colorBrightGreen,colorRed);

for (i=period;i<BarCount;i++)
{
if (THL>0)
{
Line0 = Lo;
Line100 = Hi;
Line1 = Line0 + abs(Line100-Line0)*0.382;
Line2 = Line0 + abs(Line100-Line0)*0.5;
Line3 = Line0 + abs(Line100-Line0)*0.618;
Line4 = Line0 + abs(Line100-Line0)*0.786;
Line5 = Line0 + abs(Line100-Line0)*1.618;
Line6 = Line0 + abs(Line100-Line0)*2.618;
C38=Close>Line1 AND Close<Line2;
C50=Close>Line2 AND Close<Line3;
C62=Close>Line3 AND Close<Line4;
C78=Close>Line4;

}
else
{
Line100 = Lo;
Line0 = Hi;
Line1 =Line0 - abs(Line100-Line0)*0.382;
Line2 = Line0 - abs(Line100-Line0)*0.5;
Line3 = Line0 - abs(Line100-Line0)*0.618;
Line4 = Line0 - abs(Line100-Line0)*0.786;
Line5 = Line0 - abs(Line100-Line0)*1.618;
Line6 = Line0 - abs(Line100-Line0)*2.618;
C38=Close<Line1 AND Close>Line2;
C50=Close<Line2 AND Close>Line3;
C62=Close<Line3 AND Close>Line4;
C78=Close<Line4;
}
}

Cstat=WriteIf(C38,"38",WriteIf(C50,"50",WriteIf(C62,"62",WriteIf(C78,"78","NA"))));

Filter=1;

``````

As you can see it is simple enough but I cant get the IF function to work, It gives the Boolean condition error and I have not been able to fix it no matter how much I tried. I have read articles but cant wrap my head around the problem.

Your help will be very much appreciated.

Regards
Mehdi

Ok. I think I solved the problem. Here is the code:

``````
// Get values for fib levels

period = 120;

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

BL= LLVBars(L,period);
BH= HHVBars(H,period);
THL=BH-BL;

Fibcolor=IIf(THL<0,colorBrightGreen,colorRed);

for (i=period;i<BarCount;i++)
{
if (BL[i]<BH[i])
{
Line0 = Lo;
Line100 = Hi;
Line1 = Line0 + abs(Line100-Line0)*0.382;
Line2 = Line0 + abs(Line100-Line0)*0.5;
Line3 = Line0 + abs(Line100-Line0)*0.618;
Line4 = Line0 + abs(Line100-Line0)*0.786;
Line5 = Line0 + abs(Line100-Line0)*0.886;
Line6 = Line0 + abs(Line100-Line0)*1.0;
C38=Close>Line1 AND Close<Line2;
C50=Close>Line2 AND Close<Line3;
C62=Close>Line3 AND Close<Line4;
C78=Close>Line4 AND Close<Line5;
C88=Close>Line5 AND Close<Line6;

}
else
{
Line100 = Lo;
Line0 = Hi;
Line1 =Line0 - abs(Line100-Line0)*0.382;
Line2 = Line0 - abs(Line100-Line0)*0.5;
Line3 = Line0 - abs(Line100-Line0)*0.618;
Line4 = Line0 - abs(Line100-Line0)*0.786;
Line5 = Line0 - abs(Line100-Line0)*0.886;
Line6 = Line0 - abs(Line100-Line0)*1.0;
C38=Close<Line1 AND Close>Line2;
C50=Close<Line2 AND Close>Line3;
C62=Close<Line3 AND Close>Line4;
C78=Close<Line4 AND Close>Line5;
C88=Close<Line5 AND Close>Line6;

}
}

Cstat=WriteIf(C38,"38",WriteIf(C50,"50",WriteIf(C62,"62",WriteIf(C78,"78",WriteIf(C88,"88","NA")))));

Filter=1;

``````

But I have met another issue…

However its so UNBELIEVABLY SLOW in execution. Can someone please tell me why?!

It is slow because you are doing lots of unnecesary work (repeating same work many times inside loop). You should remove for() loop. Read this: http://www.amibroker.com/guide/h_understandafl.html (you may need to re-read it a couple of times until you get “aha” moment).

Use debugger and see how many times your loop is doing same thing How do I debug my formula?

And really this has been covered on this forum a couple of times already: IIf function versus if statement and Whats the difference between these iif and if? and Duplicate topic ! Was: Why Ref does not seem to work in IIf and IIF (Buy, ...) then Shellexecute but its triggering every bar

Thank you so much. How should I change the formula if I remove the For loop?

I love the IIF function but I thought it would be easier to use IF here. Can you show me an example of how to use IFF here?

Thanks again. Much appreciated