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

 

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

 
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 ?

 
Keith Watford:

Is this MQL5 or MQL5 ?

MQL5 :-D
 
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.

 

Alain Verleyen:

Keith Watford:

Is this MQL5 or MQL5 ?

MQL5 :-D

 Oops!

I've edited my post now.

 
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. 

 

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. 

 

simply try below Code .I Think this is what you need. even I have issues using mql5. This is the way what i found. Good luck!

 
void OnTick()
  {

 

double MAvalue=iMAMQL4(NULL,0,21,0,MODE_SMMA,PRICE_CLOSE,1)
printf(MAvalue); // 

}

/////// NOTE : this Part goes after the OnInt();

double iMAMQL4(string symbol,
               int tf,
               int period,
               int ma_shift,
               int method,
               int price,
               int shift)
  {
   ENUM_TIMEFRAMES timeframe=TFMigrate(tf);
   ENUM_MA_METHOD ma_method=MethodMigrate(method);
   ENUM_APPLIED_PRICE applied_price=PriceMigrate(price);
   int handle=iMA(symbol,timeframe,period,ma_shift,
                  ma_method,applied_price);
   if(handle<0)
     {
      return(-1);
     }
   else
      return(CopyBufferMQL4(handle,0,shift));
  }
 
Prasad Niroshan:

simply use 

***

Please insert the code correctly: when editing a message, press the button      Codeand paste your code into the pop-up window. (The first time I corrected your message)
 
Bhoopalan :


You can attach ( Attach file) full code of the advisor? I am very interested in seeing OnInit.

 
Vladimir Karputov:
Please insert the code correctly: when editing a message, press the button      and paste your code into the pop-up window. (The first time I corrected your message)
Sorry cuz in first time i reply to an isssue and Thanks for making me correct <3 happy coding!
Reason: