You're reading...
EA development, MQL5

MQL5: Getting indicator values – default indicator function versus iCustom()

Posting this as a repository for myself as I can’t remember what I wrote yesterday much less a few months ago.

This script illustrates 2 things:
1. Getting indicator values in MQL5 is more laborious than in MQL4 if you only need 1 value
2. Calling a default indicator function versus iCustom()

#property script_show_inputs
input bool using_iCustom = true;

void OnStart(){

   // Create dynamic array to store a range of indicator values
   double MACD_Main[];

   // Set this array as time series (where index 0 is referring to the most recent bar/period)
   ArraySetAsSeries(MACD_Main, true);

   // Generate an object with indicator_handle as its ID
   int indicator_handle = 0;
   if(using_iCustom)indicator_handle = iCustom(_Symbol, _Period, "Examples\\MACD", 12, 26, 9, PRICE_CLOSE);
   else             indicator_handle = iMACD  (_Symbol, _Period,                   12, 26, 9, PRICE_CLOSE);
   // Define indicator data to be polled and store in array
   int numberOfMACDMain = CopyBuffer(indicator_handle, /* indicator handle */
                                     0,                /* indicator buffer number (here 0 = MACD Main) */
                                     0,                /* start position (here start copying from the most recent bar) */
                                     20,               /* amount to copy (here copy 20 elements) */
                                     MACD_Main);       /* target array to copy to (here MACD_Main array) */

   // Use the information
   if(using_iCustom)Print("The value of iCustom MACD(12,26,9) at shift 5 of " + _Symbol + " is " + DoubleToString(MACD_Main[5], 6)  + ".");
   else             Print("The value of iMACD(12,26,9) at shift 5 of "        + _Symbol + " is " + DoubleToString(MACD_Main[5], 6)  + ".");


Know that iMACD() will return a successful handle even if you delete the MACD indicator from the MT5 terminal, but naturally iCustom() will not.

You can also get the indicator handle using indicatorCreate(), but it requires populating the last parameter of MqlParam type with the necessary type and value of the input parameter. For sanity, I wouldn’t recommend it.



No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s



Flag Counter
%d bloggers like this: