get last index for last fractal formed

 

Hello guys. I wanted to ask you if there is any function or somebody has any idea to determine the index of last formed fractal in a bar chart.

If I refer to ifractal() I can determine the price but not the index.

Thanks in advance.

 

The index is relative to the current bar. Presumably you are getting the price data by calling iFractals() in the current bar as you go along, and then recording that info to be used later? In this case the index will obviously always be "0" when you are calling the price.

EDIT: Now that I've thought about it, I think that indicator only draws the signals two bars later, doesn't it? So you are probably not using that method to get the price ....

The best way to get the index I can think of is to cycle backwards through earlier bars of the relevant iFractal() indicator buffer until you find the bar you want and then read off its index.

You can do this with a simple cycle operation using for or while. https://book.mql4.com/operators/while https://book.mql4.com/operators/for

If you already know how to do this, then I think that is your best bet. If you don't know how to do this then here is a quick example (I'm no coding expert). I guess you don't already know this, otherwise you wouldn't be asking your question.

I use while here because I find it less confusing than the for layout, although it does practically the same thing. This cycles backwards through the previous bars (starting at the current bar) and checks the iFractals() indicator for whatever you want. When it finds it, it outputs the index to the variable I've called "answer", and then ends the cycle.

int counter = 0;
int MaximumBarsToCheck = 100;
int answer = -1;   // not started at zero, because the thing you are checking for could sometimes be in the current bar (though not with fractals obviously).
while ( counter <= MaximumBarsToCheck )
{
if ( iFractals("bla, bla, bla", "relavantbuffer" , counter) == "required result") ){ answer = counter; break; } // here you replace the index (normally "i") with counter.
counter++;
}
Reason: