MQL5: How can I write a function that gets a moving average value of any symbol regardless of what chart the indicator is placed on

 
 

Hi,

May I ask a question about how to write a function that gets a moving average value of any symbol regardless of what chart the indicator is placed on
I have attached the test files and see below attempt I made, but it returns a value of zero. Thanks!

double GetEMAValue(string symbol)

{

    double value = 0;

    double ema_value[];

    

    int ema_handle = iMA(symbol, _Period, 13, 0, MODE_EMA, PRICE_CLOSE);

    if (ema_handle != INVALID_HANDLE)

    {

         ArraySetAsSeries(ema_value,true);

        if(CopyBuffer(ema_handle,0,0,500,ema_value)<=0) return 0; 

        

        value = ema_value[2];

        

        if (value == 0)

            Print("Error getting EMA value: ", GetLastError());

    }

    else

        Print("Error getting EMA handle: ", GetLastError());

    

    return value;

}

Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Other Constants
Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Other Constants
  • www.mql5.com
Other Constants - Named Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
Files:
Test.mq5  4 kb
 
Chima Nwokoro: but it returns a value of zero. Thanks!
 int ema_handle = iMA(symbol, _Period, 13, 0, MODE_EMA, PRICE_CLOSE);

Perhaps you should read the manual, especially the examples.
   How To Ask Questions The Smart Way. (2004)
      How To Interpret Answers.
         RTFM and STFW: How To Tell You've Seriously Screwed Up.

They all (including iCustom) return a handle (an int). You get that in OnInit. In OnTick/OnCalculate (after the indicator has updated its buffers), you use the handle, shift and count to get the data.
          Technical Indicators - Reference on algorithmic/automated trading language for MetaTrader 5
          Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
          How to start with MQL5 - General - MQL5 programming forum - Page 3 #22 (2020)
          How to start with MQL5 - MetaTrader 5 - General - MQL5 programming forum - Page 7 #61 (2020)
          MQL5 for Newbies: Guide to Using Technical Indicators in Expert Advisors - MQL5 Articles (2010)
          How to call indicators in MQL5 - MQL5 Articles (2010)

 
William Roeder #:

Perhaps you should read the manual, especially the examples.
   How To Ask Questions The Smart Way. (2004)
      How To Interpret Answers.
         RTFM and STFW: How To Tell You've Seriously Screwed Up.

They all (including iCustom) return a handle (an int). You get that in OnInit. In OnTick/OnCalculate (after the indicator has updated its buffers), you use the handle, shift and count to get the data.
          Technical Indicators - Reference on algorithmic/automated trading language for MetaTrader 5
          Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
          How to start with MQL5 - General - MQL5 programming forum - Page 3 #22 (2020)
          How to start with MQL5 - MetaTrader 5 - General - MQL5 programming forum - Page 7 #61 (2020)
          MQL5 for Newbies: Guide to Using Technical Indicators in Expert Advisors - MQL5 Articles (2010)
          How to call indicators in MQL5 - MQL5 Articles (2010)

Thank you @William Roeder. 
First, I was hoping someone like you responds. I've followed you in the forums for years now. You won't believe how much your responses have helped me along the way.
Now, back to the topic...
Yes I agree that they return a handle (int) from which data (e.g. EMA) can be gotten. It is not a problem to get data for the Symbol of the chart on which the indicator is placed. I have been able to do that.
Where I am having issues is to able to do the same regardless of what Symbol I choose.
One will think you write a function that can send variable Symbles like I did with GetEMAValue(string symbol) function, but it doesn't work

GetEMAValue(string symbol)

I will go through the information on the links you sent, but I will appreciate your direct thought.
Thanks again

 
Issue resolved. Function works ok.
Error due to market being closed