# Bar Index Help

507

Hello Members,

Need little bit help on coding logic. Here is the details:

if Bar creates higher higher, higher low

1st Bar from where it created higher higher, I want to store it index number. Then finally when market no more creates higher higher, I use iLowest() with that index & current index to find the lowest point. I mean as long as market creates higher higher higher low, the lowest point will be fixed until it reverses.

That's the work need to be done. I tried coding with following but its not working.

```for(int i=20;  i>= 0; i--)
{
if((iHigh(Symbol(),0,i-1) >= iHigh(Symbol(),0,i) && iLow(Symbol(),0,i-1) >= iLow(Symbol(),0,i)))
{

if(C1 == 0) C1 = i;

UB = iLow(Symbol(),0,iLowest(Symbol(),0,MODE_LOW,C1,i));

buffer[i] = UB;
}

C1 = 0;

if((iHigh(Symbol(),0,i-1) <= iHigh(Symbol(),0,i) && iLow(Symbol(),0,i-1) <= iLow(Symbol(),0,i)))
{

if(C2 == 0) C2 = i;

LB = iHigh(Symbol(),0,iHighest(Symbol(),0,MODE_HIGH,C2,i));
buffer[i] = LB;

}
C2 = 0;

}```

1498

cashcube:

1st Bar from where it created higher higher, I want to store it index number. Then finally when market no more creates higher higher, I use iLowest() with that index & current index to find the lowest point. I mean as long as market creates higher higher higher low, the lowest point will be fixed until it reverses.

It's not 100% clear what you want and are trying to do, but I suspect your problem is the fourth parameter for iLowest:

```if(C1 == 0) C1 = i;
UB = iLow(Symbol(), 0, iLowest(Symbol(), 0, MODE_LOW, C1, i));```

That parameter is a count; a number of bars. Your code is looking back C1 bars from i. For example, if C1 is 20 and i is 10, then the code will examine bars 10 to 29.

I suspect that what you want to do is MODE_LOW, C1 - i, i or MODE_LOW, C1 - i + 1, i  (depending on whether you want the low since C1, or since-and-including C1).

15303

 cashcube: , I want to store it index number. No you don't. As soon as a new bar forms your index is useless. Save the date of the bar and find the shift.
507

jjc:

It's not 100% clear what you want and are trying to do, but I suspect your problem is the fourth parameter for iLowest:

That parameter is a count; a number of bars. Your code is looking back C1 bars from i. For example, if C1 is 20 and i is 10, then the code will examine bars 10 to 29.

I suspect that what you want to do is MODE_LOW, C1 - i, i or MODE_LOW, C1 - i + 1, i  (depending on whether you want the low since C1, or since-and-including C1).

Hello thank you for your post,

You spotted right, 4th parameter is the problem.

With MODE_LOW,C1-i+1 I get the followings: But I want to do something which are draw in black thick line. With C1-i+1 that is not happening. I need to keep C1 fixed. Any idea?

Regards

507

WHRoeder:
 cashcube: , I want to store it index number. No you don't. As soon as a new bar forms your index is useless. Save the date of the bar and find the shift.

Tried it but didn't worked. Result is same.

` if(C1 == 0) C1 = iBarShift(Symbol(),0,iTime(Symbol(),0,i));`
15303

 What do you expect that to return? It will always return i! `iBarShift(Symbol(),0,iTime(Symbol(),0,i));`
507

WHRoeder:
 What do you expect that to return? It will always return i!

Then how can I do it, If I Store i's Time & Then find iBarshift, it will keep changing as long as the loop moves forward. Look at my picture.
2202

cashcube:
Then how can I do it, If I Store i's Time & Then find iBarshift, it will keep changing as long as the loop moves forward. Look at my picture.

It is an Indicator, so use your buffers. Keep track of running values in extra support buffers (that don't display). In other words, instead of storing the Bar Index, keep track of the Bar shift in relative terms to the current bar (1 bar back, 2 bars back, 3 bars back, etc.).

You also don't need to keep looping back or using the iLowest() or iHighest() in every new bar. Just keep that information also as running values in extra support buffers.

507

FMIC:

It is an Indicator, so use your buffers. Keep track of running values in extra support buffers (that don't display). In other words, instead of storing the Bar Index, keep track of the Bar shift in relative terms to the current bar (1 bar back, 2 bars back, 3 bars back, etc.).

You also don't need to keep looping back or using the iLowest() or iHighest() in every new bar. Just keep that information also as running values in extra support buffers.

Inside the loop of i, it will keep changing, then how can I use ibarshift here? Do I need to run another loop inside?

I got following coding but it produce same results as before:

```for(int i=limit;  i>= 0; i--)
{
if((iHigh(Symbol(),0,i-1) >= iHigh(Symbol(),0,i) && iLow(Symbol(),0,i-1) >= iLow(Symbol(),0,i)))
{

Supportbuffer[i] = iLow(Symbol(),0,i);

if(Supportbuffer[i] < Supportbuffer[i-1])

buffer[i] = Supportbuffer[i];
}

}```
507

any correction on my coding or any coding example?

Thanks.

507

```for(int i=limit;  i>= 0; i--)
{
if((iHigh(Symbol(),0,i-1) <= iHigh(Symbol(),0,i) && iLow(Symbol(),0,i-1) <= iLow(Symbol(),0,i)))
{

Supportbuffer[i] = iHigh(Symbol(),0,i);

if(Supportbuffer[i] < Supportbuffer[i-1])
{
Supportbuffer2[i] = Supportbuffer2[i] + MathAbs(i-(i-1));

buffer[i] = iHigh(Symbol(),0,Supportbuffer2[i]);
}

}

}```

I updated the code more, declaring 3 buffer at initialization. Supportbuffer2 is integer type. But its showing, array out of range error.

Any idea?

123