Moving Average value is shown as 10 although it is plotted correctly on chart - Help please

To add comments, please log in or register
Bhoopalan
33
Bhoopalan  

Hi, 

I'm developing an EA. Part of the EA is to check if the price is closed above a particular EMA. 

I declared a 'double' global variable as 'EMA34'. Everytime a new candlestick is generated, I reset the value of the variable as follows: 

  EMA34 = iMA(NULL,0,34,0,MODE_EMA,PRICE_CLOSE);

When I test the EA in Strategy tester, I do see the 34 EMA at the correct place. However, the code to check if the price closed above the EMA34 doesn't work. This is my code for that: 

if (iClose(NULL,NULL,1) > EMA34)

            {

               trade.Buy(1,NULL,NULL);

            }

So I added a Print statement to find the EMA34 value. Everytime a new candlestick is generated, the EMA34 shows 10.0 as value. My print statement: 

            Print ("34 EMA is at: " + EMA34);

How come the EMA is plotted correctly in the 'strategy tester visualization' chart but the value always comes as 10.0? What am I missing? 

Since I reset the EMA at the beginning of every candlestick, I thought I don't need it as an array. 


Appreciate your help. 

Side note: I'm using iMA, iClose, iOpen and so on because the simpler 'Close[0]' and such things don't work for me. I don't understand why. First time coding in mql5

Keith Watford
Moderator
20327
Keith Watford  
Bhoopalan:

Side note: I'm using iMA, iClose, iOpen and so on because the simpler 'Close[0]' and such things don't work for me. I don't understand why. First time coding in mql5

Is this MQL4 or MQL5 ?

Alain Verleyen
38657
Alain Verleyen  
Keith Watford:

Is this MQL5 or MQL5 ?

MQL5 :-D
Alain Verleyen
38657
Alain Verleyen  
Bhoopalan:
...

Appreciate your help. 

Side note: I'm using iMA, iClose, iOpen and so on because the simpler 'Close[0]' and such things don't work for me. I don't understand why. First time coding in mql5

Because this is mql5, there is no Close[0] AND iMA returns an handle, not a MA value.

Read the documentation please.

Keith Watford
Moderator
20327
Keith Watford  

Alain Verleyen:

Keith Watford:

Is this MQL5 or MQL5 ?

MQL5 :-D

 Oops!

I've edited my post now.

Bhoopalan
33
Bhoopalan  
Alain Verleyen:

Because this is mql5, there is no Close[0] AND iMA returns an handle, not a MA value.

Read the documentation please.


Thank you, Alain. I've read it but having difficulty grasping it. I'm not sure how to get the value instead of the handler. I think it has something to do with CopyBuffer but its parameters are something that I don't understand as well. :( 

I have modified the code and introduced an int variable for the handler and double variable for the value as follows: 

   EMA34Handler = iMA(NULL,0,34,0,MODE_EMA,PRICE_CLOSE);

   EMA34Value = CopyBuffer(EMA34Handler, 0,0);   

It throws an error: "CopyBuffer: no one of the overloads can be applied to the function call". Please help. 

Bhoopalan
33
Bhoopalan  

Tracking down the value of an indicator shouldn't be as difficult as this. I just don't understand how to use copyBuffer... let alone if that is the function I must use. 

All I want is this. When a new candlestick is created, EA should check if the price is above a particular EMA. 

If this is about SMA, I wouldn't have even had to call the indicator in my code. I could have directly calculated the value. But since it is an EMA, the code is getting really complex. I tried a script to calculate the value of the EMA without using the iMA function at all. But it doesn't return the accurate EMA value. 

To add comments, please log in or register