# Help needed:

530

Hi all,

3 parameters:

Total_Candles = 40

Above_Candles = 5

1 moving average as reference.

Condition 1:

If At least 5 candles in a row closes above Moving average over the last 40 candles, then Condition_1 is valid.

Question:

What is the proper function to use to check Condition_1?

A little example would be appreciated : )

Thank you

Moderator
37608

What have you tried ?
530

I have no idea what is the best logical function to use to do it.
18404

There is no function, you have to code it. For loop over last forty bars. Get the iMA, test your condition, count it.
530

I knew this will be hard.

I'm not familiar with loop.

I could probably check if candle has been closed above iMA over the last 40 bars... but Condition 1 would be validated only if 5 candles is a row are above. => It's getting far away from my skills

6486

I knew this will be hard.

I'm not familiar with loop.

I could probably check if candle has been closed above iMA over the last 40 bars... but Condition 1 would be validated only if 5 candles is a row are above. => It's getting far away from my skills

what would you code if condition was only 1 candle is above ...
530

It would be very ugly I know, but this is the way I can do it:

```// GET Candle closed price:
double Candle_1 = iClose(Symbol(),PERIOD_H1,1);
double Candle_2 = iClose(Symbol(),PERIOD_H1,2);
double Candle_3 = iClose(Symbol(),PERIOD_H1,3);
double Candle_4 = iClose(Symbol(),PERIOD_H1,4);
// ....
double Candle_40 = iClose(Symbol(),PERIOD_H1,40);

// GET iMA closed price:
double MA_1 = iMA(Symbol(),60,21,0,MODE_SMMA,PRICE_CLOSE,1);
double MA_2 = iMA(Symbol(),60,21,0,MODE_SMMA,PRICE_CLOSE,2);
double MA_3 = iMA(Symbol(),60,21,0,MODE_SMMA,PRICE_CLOSE,3);
double MA_4 = iMA(Symbol(),60,21,0,MODE_SMMA,PRICE_CLOSE,4);
// ...
double MA_40 = iMA(Symbol(),60,21,0,MODE_SMMA,PRICE_CLOSE,40);

// CHECK Condition_1: If at least 1 candle over the last 40 bars is above iMA
if( Candle_1 > MA_1 || Candle_2 > MA_2 || Candle_3 > MA_3 ||
//...
Candle_40 > MA_40 ) {double Condition_1 = 1; }
```

I guess there is more dynamic way... but how?

Thank you for helping

6486

It would be very ugly I know, but this is the way I can do it:

I guess there is more dynamic way... but how?

Thank you for helping

 iMA Moving Average

You wanted that if closes above Moving average over the last 40 candles, then Condition_1 is valid......

You have coded this line ......

`double MA_4 = iMA(Symbol(),60,21,0,MODE_SMMA,PRICE_CLOSE,4);`

What is it ???? Is it a moving average value over 40 candles ?? If not then what is it, you coded this line .? Give your explanation.

Place your moving average on a chart then view Data Window Ctrl+D and move your pc_mouse over the bars and you can check what value your moving average has on the bar your mouse is moving

530

But I really need some help please. o_O

Moderator
37608

But I really need some help please. o_O

It seems to me what you need is someone who code it for you.
Moderator
12503

It would be very ugly I know, but this is the way I can do it:

```// GET Candle closed price:
double Candle_1 = iClose(Symbol(),PERIOD_H1,1);
double Candle_2 = iClose(Symbol(),PERIOD_H1,2);
double Candle_3 = iClose(Symbol(),PERIOD_H1,3);
double Candle_4 = iClose(Symbol(),PERIOD_H1,4);
// ....
double Candle_40 = iClose(Symbol(),PERIOD_H1,40);

// GET iMA closed price:
double MA_1 = iMA(Symbol(),60,21,0,MODE_SMMA,PRICE_CLOSE,1);
double MA_2 = iMA(Symbol(),60,21,0,MODE_SMMA,PRICE_CLOSE,2);
double MA_3 = iMA(Symbol(),60,21,0,MODE_SMMA,PRICE_CLOSE,3);
double MA_4 = iMA(Symbol(),60,21,0,MODE_SMMA,PRICE_CLOSE,4);
// ...
double MA_40 = iMA(Symbol(),60,21,0,MODE_SMMA,PRICE_CLOSE,40);

// CHECK Condition_1: If at least 1 candle over the last 40 bars is above iMA
if( Candle_1 > MA_1 || Candle_2 > MA_2 || Candle_3 > MA_3 ||
//...
Candle_40 > MA_40 ) {double Condition_1 = 1; }```

I guess there is more dynamic way... but how?

Thank you for helping

Usually anywhere that you are using the same number to access different arrays and incrementing that number, you can use a loop. Cuts out a lot of typing, or most likely copy and paste. The problem with copy and paste is that you have to be very careful to change the values correctly

```   int counter=0;              //count number of candles that satify condition
for(int x=1;x<=40;x++)
{
// GET Candle closed price:
double Candle=iClose(Symbol(),PERIOD_H1,x);
// GET iMA closed price:
double MA=iMA(Symbol(),60,21,0,MODE_SMMA,PRICE_CLOSE,x);
// CHECK Condition
if(Candle>MA)
counter++;
}
Print("There were ",counter," candles that closed above the MA");
```