Download MetaTrader 5

Help needed:

To add comments, please log in or register
Let your idle PCs do something useful. Join MQL5 Cloud Network!
Frenchytrader
359
Frenchytrader 2014.04.19 16:42 

Hi all,

Please help and advise how to do this:

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?


Here


A little example would be appreciated : )


Thank you

Alain Verleyen
Moderator
30743
Alain Verleyen 2014.04.20 21:05  
What have you tried ?
Frenchytrader
359
Frenchytrader 2014.04.21 18:37  
I have no idea what is the best logical function to use to do it.
whroeder1
14519
whroeder1 2014.04.21 18:52  
There is no function, you have to code it. For loop over last forty bars. Get the iMA, test your condition, count it.
Frenchytrader
359
Frenchytrader 2014.04.21 19:04  

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

Tjipke de Vries
6315
Tjipke de Vries 2014.04.21 19:11  
FrenchyTrader:

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 ...
Frenchytrader
359
Frenchytrader 2014.04.21 20:37  

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

Tjipke de Vries
6315
Tjipke de Vries 2014.04.21 21:20  
FrenchyTrader:

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

Read.....

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

Frenchytrader
359
Frenchytrader 2014.04.21 21:44  

Thank you for your help.


But I really need some help please. o_O

Alain Verleyen
Moderator
30743
Alain Verleyen 2014.04.21 22:04  
FrenchyTrader:

Thank you for your help.


But I really need some help please. o_O

It seems to me what you need is someone who code it for you.
Keith Watford
Moderator
9623
Keith Watford 2014.04.21 22:06  
FrenchyTrader:

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");
123
To add comments, please log in or register