# Find Last Red Candle

247
2016.02.18 21:31

Hi,

Any one can help me with this situation as i'm new to mql4 programing.

I would line to find the last red candle as it is showing in the attached photo. I know only one way to do that. But the problem is (What if the Red candle it is not in fifth bar back??) for Ex: if it in 4 or 3 or even in 7?

This is the way I do.

`if(Close[1] > Open[1] && Close[2] > Open[2] && Close[3] > Open[3] && Close[4] > Open[4] && Close[5] < Open[5])`

Moderator
9640
2016.02.18 23:02
use an increasing while loop to check back through the candles. Stop when the 1st read candle is found.
247
2016.02.19 09:11

hi GUMRai,

Thank you for your response, As I told i'm new to mql4 programing and I read some article and see some video about loop BUT i'm not able to do that .

I found this code which is show the numbers of bar down or up. But i can't figure how to find the last down candle?

Any help will be appreciated.

```int n, upbar, downbar;
double candleclose, candleopen, isitupordown;
for(n=50; n!=0; n--)
{
candleclose = iClose(Symbol(), 0, n);
candleopen = iOpen(Symbol(), 0, n);
isitupordown = candleclose - candleopen;
if(isitupordown>0) upbar= upbar+1;
if(isitupordown<0) downbar= downbar+1;
}```
Moderator
9640
2016.02.19 09:42
```  int last_red_candle_index=1;
while(true)
{
if(Open[last_red_candle_index]>Close[last_red_candle_index])
break;
last_red_candle_index++;
}
Comment("Last Red Candle Index Is "+(string)last_red_candle_index);```

Study and I hope that you will understand

77
2016.02.19 09:44
247
2016.02.19 10:14

Thank you very much GUMRai,

I really appreciate your help, I read a lot but i'm not able to ...

Any way thank you again
247
2016.02.20 11:07

Hi GUMRai,

I'm trying to use the code you post with (iOpen and iClose) But the application stop respond .

```     int last_red_candle_index=1;
while(true)
{

if(iOpen(Symbol(),0,last_red_candle_index) > iClose(Symbol(),0,last_red_candle_index));

last_red_candle_index++;
}```

Moderator
9640
2016.02.20 11:13

Hi GUMRai,

I'm trying to use the code you post with (iOpen and iClose) But the application stop respond .

Why have you removed the break? Without it it is an endless loop
247
2016.02.20 11:20

Thank you for response

you mean this is the right one

```
if(iOpen(Symbol(),0,[last_red_candle_index]) > iClose(Symbol(),0,[last_red_candle_index]));```
247
2016.02.20 11:23
Sorry break;
247
2016.03.04 18:21

Hi GUMRai

```  int last_Red_candle_index=1;

while(true)
{
if(iOpen(Symbol(),0,last_Red_candle_index) > iClose(Symbol(),0,last_Red_candle_index)) break;
last_Red_candle_index++;
}

double Close_Above_Red_Candle_High  = iClose(Symbol(),0,1) > iHigh(Symbol(),0,last_Red_candle_index);

if( last_Red_candle_index < 3
&& Close_Above_Red_Candle_High
&& Volume[0]==1)
Alert(" Close Above Red Candle High  "+Symbol(),"    ",TimeToStr(CurTime(),TIME_DATE|TIME_MINUTES),"   M",Period(),"  ");

```

I have one problem with the above code, it keeping alert me with every candle that close above red candle until new red candle format, I try many way but i couldn't find the solution as i'm new to MQL4.

finally i limit it with this code,

`last_Red_candle_index < 3`

but this not what i need