Conditional execution on last bar (LastValue), was: Array Problem

Again and again I get the same problem with

if versus iif conditions. While the if statement allows to align larger segments

in a whole process, one needs to repeat the iif statement for every line…

In this case I would like to translate the ProRealCode for the

Fibonacci Bands indicator which one can find here

https://www.prorealcode.com/prorealtime-indicators/fibonacci-bands-indicator/

One notices that from the line “if islastbarupdate then” until “endif”

there is a large segment which seems to be very laborious

to enclose in “iif(…, NULL); “ statements for every line.

So how is it possible to translate this block ?

Many thanks for your assistance !

The code you referenced does not show how isLastBarUpdate is defined. Based on the name, one might guess that it's a scalar, and as such, would work fine with if rather than iif. If isLastBarUpdate is an array, then you would need to post complete code so that others could help you find a reasonable approach, for example perhaps you could use something like this:

if (LastValue(isLastBarUpdate))

Most importantly, please post your AFL code where you've attempted to solve the problem yourself, and then provide context on what's not working as expected.

2 Likes

@Achalendra, if you translated the rest of the code correctly (I used Chat-GPT and only had to make a few corrections/additions) you can use something like this example to draw the "extensions" (to replace the part inside the "if" you are referring to)

function drawExt(array, slope, color,label)
{
	x1 = BarCount - 1;
	x2 = x1 + extend;
	y1 = array[x1];
	y2 = y1 + slope[x1] * extend;
	GfxSetCoordsMode(1);
	GfxSelectPen(color, 2, 2);  // or (color, 1, 1) to get a solid 1 pixel line
	GfxMoveTo(x1, y1);
	GfxLineTo(x2, y2);    
	GfxSetCoordsMode(0);
	PlotText(label + StrFormat(" ( %.2f )", y2), x2+1, y2, color);
}

drawExt(smma, slope, colorBlack, "0%");
drawExt(fibTop1, slope, colorRed, "38.2%");
drawExt(fibTop2, slope, colorRed, "61.8%");
drawExt(fibTop3, slope, colorRed, "100.0%");
drawExt(fibBot1, slope, colorGreen, "38.2%");
drawExt(fibBot2, slope, colorGreen, "61.8%");
drawExt(fibBot3, slope, colorGreen, "100.0%");

Simply add this block of code at the end of your translated code (not inside a conditional).
Obviously in your chart you must have reserved a certain number of "blank bars" (the extend variable is defined at the top of formula, maybe as a user defined parameter)

4 Likes

Thank you for your answer. I translated the code until I reached this large segment and my question concerned in fact the way one could approach such a segment with AFL rather than the detailed code in itself. And the preceding code would not have contributed to elucidate this, that's why I didn't add my code at all...

Thank you very much for your answer. This is indeed the solution. I didn't know that chatgtp is able to do such translations...

What @beppe's code is doing is essentially just LastValue( array ) (i.e. array[ BarCount - 1 ]). And as @mradtke noticed you can use if( LastValue( array ) ) if you need conditional execution. But in this particular case, you don't need that if, because you just drawing a couple of lines and text on last bar values.

Other platforms need those strange if( lastbarupdate) but AmiBroker doesn't need that at all, because it runs the formula ONCE for ALL BARS AT ONCE, not separately over and over for every bar.

2 Likes

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.