How to start Indicator calculation based on first bar of current day?

 

Dear MQL5 Experts, good afternoon!

Kindly ask for your help on this.

Please let me know if any additional information is required.

Thanks in advance!

 

Context: 

  I`ve built an EA on which I`m using EMA and MACD indicators inside the Expert Signal class. I`ve inherited the standard Expert Signal class and created my own. I`m using indicator classes CiMA and CiMyMACD (my custom implementation of CiMACD).

Issue: 

Due to huge gaps between last day closing and current day opening prices, the indicators are not behaving as expected on the first N bars (screenshot attached).

Question:

How to begin indicators calculation from the first bar of the day, instead of the first bar on history?

Expected behaviour:

  1. Considering an EMA on the closing prices, this would mean to set the value of EMA on the first bar as the closing price of this bar and then calculating further values (from second bar on) using standard EMA calculation 
  2. Considering MACD, the idea would be to have the same behaviour described above for the 3 used EMAs: fast, slow and signal

Code:

Indicator objects declaration: 

class CMyExpertSignal : public CExpertSignal
{
    protected: 
    
        CiMA               m_iMA;
        CiMyMACD           m_iMyMACD;
.
.
.

 

Indicators init:

bool CMyExpertSignal::InitIndicators(CIndicators *indicators)
{
    //--- check of pointer is performed in the method of the parent class
    //---
    //--- initialization of indicators and timeseries of additional filters
    if(!CExpertSignal::InitIndicators(indicators))
        return(false);
    
    //--- create and initialize MA
    if(!InitMA(indicators))
        return(false);   
 
    //--- create and initialize MACD
    if(!InitMACD(indicators))
        return(false);
            
    //--- ok
    return(true);
}

 

MA init (I`m using a 26 days period and applying EMA to closing prices):

bool CMyExpertSignal::InitMA(CIndicators *indicators)
{
    //--- add object to collection
    if(!indicators.Add(GetPointer(m_iMA)))
    {
        printf(__FUNCTION__+": error adding MA object");
        return(false);
    }
    
    //--- initialize object
    if(!m_iMA.Create(m_symbol.Name(),m_period,m_MA_period,0,MODE_EMA,m_MA_applied))
    {
        printf(__FUNCTION__+": error initializing MA object");
        return(false);
    }
        
    //--- ok
    return(true);
}

 

MACD init (I`m using classical MACD period values 26 /12 /9 and applying it to the close prices):

MACD init:

bool CMyExpertSignal::InitMACD(CIndicators *indicators)
{
    //--- add object to collection
    if(!indicators.Add(GetPointer(m_iMyMACD)))
    {
        printf(__FUNCTION__+": error adding MACD object");
        return(false);
    }
    
    //--- initialize object
    if(!m_iMyMACD.Create(m_symbol.Name(),m_period,m_MACD_fast_period,m_MACD_slow_period,m_MACD_signal_period,m_MACD_applied))
    {
        printf(__FUNCTION__+": error initializing MACD object");
        return(false);
    }
    
    //--- ok
    return(true);
}

 

Methods used for retrieving data from indicators:

double MA        (int ind)   { return(m_iMA.Main(ind));       }
double Main      (int ind)   { return(m_iMyMACD.Main  (ind)); }
double Signal    (int ind)   { return(m_iMyMACD.Signal(ind)); }
double Histo     (int ind)   { return(m_iMyMACD.Histo (ind)); }