# custom indicator - return bar index for last bar with value greater than X

29

Hi all,

I try to insert a variable into the custom indicator that returns to me the closest (lowest) BAR index (for example CCI) where value is greater than 100.

example:

For last 10 bars CCI have values [0, 50, 50, 20, 50, 99, 103, 102, 101, 0]

```extern int CCI_period = 14;
extern int range= 10;

//---- buffers
double CCI_buffer[];
double shift[];

int start() {
int counted_bars = IndicatorCounted();
if(counted_bars > 0) counted_bars--;
int limit = Bars - counted_bars;
for(int i = limit - 1; i >= 0; i--) {
CCI_buffer[i] = iCCI(NULL,0,CCI_period,PRICE_WEIGHTED,i);
}

for(int i = limit - 1; i >= 0; i--) {
if(CCI_buffer[i] > 100){
shift[i] = AND NOW WHAT???;
}
return(false);
}
return(INIT_SUCCEEDED);
}```

For this example shift = 6;

Any idea?

Moderator
20422

```int x=0;       //Make the value 1 if you only want to check closed bars
while (x<100)
{
if(CCI_buffer[x]>100)
break;
x++;
}
//x=the shift that you want```

.

29

ok, this code is particle works. but... I need to make the function for each column return its own value.

for example:

- last value bigger than 150 is on bar[10], next last is on bar[20]

and function return for

- bar[0] - 10

- bar[1] - 9

- bar[2] - 8

- ....

- bar[10] - 0

until this point the function works well

the problem occurs after bar[10]. the function returns for each additional bar 0 (bar[11] - 0, bar[12] - 0,...)

I need this:

- bar[11] - 9

- bar[12] - 8

- ....

- bar[20] - 0

I tried everything possible but without result. I do not know where I'm making a mistake. I need it for the test (to see the last value for each bar). In live trading it works well.

my code:

```   for(int i = limit-1; i >= 0; i--) {
int x = 0;
while (x <= 100) { //search in last 100 bars
if(CCI_buffer[x] > 150){ //if CCI is bigger than 150
last_bar[i] = 0;
break;
}
x++;
}
if(x-i <= 0){
last_long[i] = 0;
} else if(x == 100) {
last_long[i] = NULL;
} else {
last_long[i] = x-i;
}
}```
29

no idea anyone?