Questions for using class

 

Dear all,

class CMovingAverage
{
private:
    string                 m_Symbol;      // Symbol
    ENUM_TIMEFRAMES        m_Timeframe;   // Timeframe
    int                    m_Period;      // Period for the moving average
    ENUM_MA_METHOD         m_Method;      // Type of moving average
    ENUM_APPLIED_PRICE     m_Price;       // Applied price type
    double                 m_MaValue;     // Calculated moving average value
    int                    m_Handle;      // Handle for the moving average indicator

public:
    // Constructor to initialize the moving average parameters
    CMovingAverage(string symbol, ENUM_TIMEFRAMES timeframe, int period, ENUM_MA_METHOD method, ENUM_APPLIED_PRICE price)
    {
        m_Symbol      = symbol;
        m_Timeframe   = timeframe;
        m_Period      = period;
        m_Method      = method;
        m_Price       = price;
        m_Handle      = INVALID_HANDLE; // Initialize handle
        m_MaValue     = 0.0;    
    }

    // Calculate moving average value
    double CalculateMaValue()
    {
        // Release previous handle if it exists
        if (m_Handle != INVALID_HANDLE)
        {
            IndicatorRelease(m_Handle);
        }
        
        // Create a new handle
        m_Handle = iMA(m_Symbol, m_Timeframe, m_Period, 0, m_Method, m_Price);
        
        // Check if handle is valid
        if (m_Handle == INVALID_HANDLE)
        {
            Print("Failed to create MA handle.");
            return 0.0;
        }
        
        // Buffer to store moving average values
        double ma[];
        
        // Copy the buffer
        if (CopyBuffer(m_Handle, 0, 0, 1, ma) < 0)
        {
            Print("Failed to copy buffer for MA. Error code: ", GetLastError());
            return 0.0;
        }
        
        // Return the calculated moving average value
        return m_MaValue = ma[0];
    }
    
    double GetMaValue(){
    
        return m_MaValue;
    
    }
    
    // Destructor
    ~CMovingAverage()
    {
        // Release the handle if it is valid
        if (m_Handle != INVALID_HANDLE)
        {
            IndicatorRelease(m_Handle);
        }
    }
};
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
    ENUM_TIMEFRAMES timeframe = PERIOD_H1; // Example timeframe
    int maPeriod = 14;                      // Example period for MA
    ENUM_MA_METHOD maMethod = MODE_SMA;    // Simple Moving Average
    ENUM_APPLIED_PRICE maPrice = PRICE_CLOSE; // Closing price

    // Create an instance of the MovingAverage class
    CMovingAverage ma(Symbol(), timeframe, maPeriod, maMethod, maPrice);
    
    // Get the calculated moving average value
    double maValue = ma.GetMaValue();
    
    // Print the moving average value
    Print("Moving Average (", maMethod, ") for ", Symbol(), " at period ", maPeriod, " is: ", maValue);
      
      
//---
   return(INIT_SUCCEEDED);
  }

I am new and try to learn MQL5.

I create class of moving average. However, maValue is always 0.0.

May I ask why.

BTW, any good tutorial websites or video teach MQL5?

 
Tyrone Chan:

Dear all,

I am new and try to learn MQL5.

I create class of moving average. However, maValue is always 0.0.

May I ask why.

BTW, any good tutorial websites or video teach MQL5?

Because you are creating the handle every time.

Do it once and keep it around. Return the handle when you destroy the object.