Indicators: Two iMA Formula

 

Two iMA Formula:

Study of the dependencies of two iMA (Moving Average, MA)

Two iMA Formula

Author: Vladimir Karputov

 

An example of how to get the indicator value from an EA:

//+------------------------------------------------------------------+
//|                                    Two iMA Formula get value.mq5 |
//|                              Copyright © 2022, Vladimir Karputov |
//|                      https://www.mql5.com/en/users/barabashkakvn |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2022, Vladimir Karputov"
#property link      "https://www.mql5.com/en/users/barabashkakvn"
#property version   "1.000"
//+------------------------------------------------------------------+
//| Enum Formula                                                     |
//+------------------------------------------------------------------+
enum ENUM_FORMULA
  {
   f_0=0,   // Fast-Slow
   f_1=1,   // Fast/Slow
   f_2=2,   // (Fast-Slow)/Slow
  };
//--- input parameters
input group             "Fast MA"
input int                  Inp_MA_Fast_ma_period      = 9;              // MA Fast: averaging period
input int                  Inp_MA_Fast_ma_shift       = 0;              // MA Fast: horizontal shift
input ENUM_MA_METHOD       Inp_MA_Fast_ma_method      = MODE_SMA;       // MA Fast: smoothing type
input ENUM_APPLIED_PRICE   Inp_MA_Fast_applied_price  = PRICE_CLOSE;    // MA Fast: type of price
input group             "Slow MA"
input int                  Inp_MA_Slow_ma_period      = 26;             // MA Slow: averaging period
input int                  Inp_MA_Slow_ma_shift       = 0;              // MA Slow: horizontal shift
input ENUM_MA_METHOD       Inp_MA_Slow_ma_method      = MODE_SMA;       // MA Slow: smoothing type
input ENUM_APPLIED_PRICE   Inp_MA_Slow_applied_price  = PRICE_CLOSE;    // MA Slow: type of price
input group             "Formula"
input ENUM_FORMULA         InpFormula                 = f_0;            // Formula:
input group             "Additional features"
input bool                 InpPrintLog                = false;          // Print log
//---
int      handle_iCustom;                        // variable for storing the handle of the iCustom indicator
bool     m_init_error               = false;    // error on InInit
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- create handle of the indicator iCustom
   handle_iCustom=iCustom(Symbol(),Period(),"Two iMA Formula",
                          "Fast MA",
                          Inp_MA_Fast_ma_period,
                          Inp_MA_Fast_ma_shift,
                          Inp_MA_Fast_ma_method,
                          Inp_MA_Fast_applied_price,
                          "Slow MA",
                          Inp_MA_Slow_ma_period,
                          Inp_MA_Slow_ma_shift,
                          Inp_MA_Slow_ma_method,
                          Inp_MA_Slow_applied_price,
                          "Formula",
                          InpFormula);
//--- if the handle is not created
   if(handle_iCustom==INVALID_HANDLE)
     {
      //--- tell about the failure and output the error code
      PrintFormat("Failed to create handle of the iCustom indicator for the symbol %s/%s, error code %d",
                  Symbol(),
                  EnumToString(Period()),
                  GetLastError());
      //--- the indicator is stopped early
      m_init_error=true;
      return(INIT_SUCCEEDED);
     }
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   if(handle_iCustom!=INVALID_HANDLE)
      IndicatorRelease(handle_iCustom);
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   if(m_init_error)
      return;
//---
   double two_ima_buffer[];
   ArraySetAsSeries(two_ima_buffer,true);
   int start_pos=0,count=3;
   if(!iGetArray(handle_iCustom,0,start_pos,count,two_ima_buffer))
      return;
//---
   string text="";
   int limit=(count>3)?3:count;
   for(int i=0; i<limit; i++)
     {
      text=text+
           " bar #"+IntegerToString(i)+": "+
           " "+DoubleToString(two_ima_buffer[i],Digits()+1)+"\n";
     }
   Comment(text);
  }
//+------------------------------------------------------------------+
//| Get value of buffers                                             |
//+------------------------------------------------------------------+
bool iGetArray(const int handle,const int buffer,const int start_pos,
               const int count,double &arr_buffer[])
  {
   bool result=true;
   if(!ArrayIsDynamic(arr_buffer))
     {
      if(InpPrintLog)
         PrintFormat("ERROR! EA: %s, FUNCTION: %s, this a no dynamic array!",__FILE__,__FUNCTION__);
      return(false);
     }
   ArrayFree(arr_buffer);
//--- reset error code
   ResetLastError();
//--- fill a part of the iBands array with values from the indicator buffer
   int copied=CopyBuffer(handle,buffer,start_pos,count,arr_buffer);
   if(copied!=count)
     {
      //--- if the copying fails, tell the error code
      if(InpPrintLog)
         PrintFormat("ERROR! EA: %s, FUNCTION: %s, amount to copy: %d, copied: %d, error code %d",
                     __FILE__,__FUNCTION__,count,copied,GetLastError());
      //--- quit with zero result - it means that the indicator is considered as not calculated
      return(false);
     }
   return(result);
  }
//+------------------------------------------------------------------+

Result:

Two iMA Formula get value

Reason: