MT4程序在MT5上运行:Time[]、Open[]、High[]、Low[]、Close[]、Volume[]转化

MT4系统变量在MT5上实现原理与代码如下

  • MT4上有Time[]、Open[]、High[]、Low[]、Close[]、Volume[]系统数组,MT5上没有。
  • 我们可以采用MQL5语言的类的数组运算符重载机制来实现。
  • 先定义一个基类CBaseRates,内部保存产品符号和产品周期两个变量。
  • 然后创建CTime、COpen、CHigh、CLow、CLose、CVolume这6个类,它们均继承CBaseRates类。
  • 以上每个子类 均加入operator[](int index)这个函数,并都各自实现自己的功能,该operator[](int index) 函数可重载数组运算符。
  • 然后创建对象如下:
    CTime Time; // 替代Time[]
    COpen Open; // 替代Open[]
    CHigh High; // 替代High[]
    CLow Low; // 替代Low[]
    CClose Close; // 替代Close[]
    CVolume Volume; // 替代Volume[]
  • 以上对象传入数组的索引,会自动调用数组运算符的重载函数即可实现数组的功能。
  • 详细的实现代码如下。
//+------------------------------------------------------------------+
//| Access to quotes of the required instrument and timeframe.       |
//+------------------------------------------------------------------+
class CBaseRates
{
   
    protected:
        string            m_symbol;
        ENUM_TIMEFRAMES   m_timeframe;
        CBaseRates();
    public:
        string            Symbol(void)    const {
    return m_symbol;    }
        ENUM_TIMEFRAMES   Timeframe(void) const {
    return m_timeframe; }
        void              Init(string symbol, ENUM_TIMEFRAMES tf);
        int               BarsTotal(void);
};
//+------------------------------------------------------------------+
//| Default constructor.                                             |
//+------------------------------------------------------------------+
CBaseRates::CBaseRates()
{
   
    m_symbol = _Symbol;
    m_timeframe = PERIOD_CURRENT;
}
//+------------------------------------------------------------------+
//| Sets symbols of the series.                                      |
//| Sets the timeframe of the symbol.                                |
//+------------------------------------------------------------------+
void CBaseRates::Init(string symbol, ENUM_TIMEFRAMES tf)
{
   
    m_symbol = symbol;
    m_timeframe = tf;
}
//+------------------------------------------------------------------+
//| Returns the available number of bars.                            |
//+------------------------------------------------------------------+
int CBaseRates::BarsTotal(void)
{
   
    return Bars(m_symbol, m_timeframe);
}
//+------------------------------------------------------------------+
//| Access to Open prices of the required instrument and timeframe.  |
//+------------------------------------------------------------------+
class COpen : public CBaseRates
{
   
    public:
        double operator[](int index)
        {
   
            double value[];
            if(CopyOpen(m_symbol, m_timeframe, index, 1, value) == 0)return 0.0;
            return value[0];
        }
};
//+------------------------------------------------------------------+
//| Access to High prices of the instrument bar.                     |
//+------------------------------------------------------------------+
class CHigh : public CBaseRates
{
   
    public:
        double operator[](int index)
        {
   
            double value[];
            if(CopyHigh(m_symbol, m_timeframe, index, 1, value) == 0)return 0.0;
            return value[0];
        }
};
//+------------------------------------------------------------------+
//| Access to Low prices of the instrument bar.                      |
//+------------------------------------------------------------------+
class CLow : public CBaseRates
{
   
    public:
        double operator[](int index)
        {
   
            double value[];
            if(CopyLow(m_symbol, m_timeframe, index, 1, value) == 0)return 0.0;
            return value[0];
        }
};
//+------------------------------------------------------------------+
//| Access to Close prices of the symbol bar.                        |
//+------------------------------------------------------------------+
class CClose : public CBaseRates
{
   
    public:
        double operator[](int index)
        {
   
            double value[];
            if(CopyClose(m_symbol, m_timeframe, index, 1, value) == 0)return 0.0;
            return value[0];
        }
};
//+------------------------------------------------------------------+
//| Access to real volumes of a symbol bar.                          |
//+------------------------------------------------------------------+
class CVolume : public CBaseRates
{
   
    public:
        long operator[](int index)
        {
   
            long value[];
            if(CopyRealVolume(m_symbol, m_timeframe, index, 1, value) == 0)return 0;
            return value[0];
        }
};
//+------------------------------------------------------------------+
//| Access to tick volumes of a symbol bar.                          |
//+------------------------------------------------------------------+
class CTickVolume : public CBaseRates
{
   
    public:
        long operator[](int index)
        {
   
            long value[];
            if(CopyTickVolume(m_symbol, m_timeframe, index, 1, value) == 0)return 0;
            return value[0];
        }
};
//+------------------------------------------------------------------+
//| Access to the bar opening time.                                  |
//+------------------------------------------------------------------+
class CTime : public CBaseRates
{
   
    public:
        datetime operator[](int index)
        {
   
            datetime value[];
            if(CopyTime(m_symbol, m_timeframe, index, 1, value) == 0)return 0;
            return value[0];
        }
};
//
CTime   Time;   // 替代 Time[]
COpen   Open;   // 替代 Open[]
CHigh   High;   // 替代 High[]
CLow    Low;    // 替代 Low[]
CClose  Close;  // 替代 Close[]
CVolume Volume; // 替代 Volume[]

将以上代码拷贝下来,保存到头文件 Rates4to5.mqh中,并将这个头文件用#include “Rates4to5.mqh” 包含到你自己的Ea文件中,即可实现该功能。

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-18 20:26:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-18 20:26:02       100 阅读
  3. 在Django里面运行非项目文件

    2024-01-18 20:26:02       82 阅读
  4. Python语言-面向对象

    2024-01-18 20:26:02       91 阅读

热门阅读

  1. 【webpack5】高级优化

    2024-01-18 20:26:02       46 阅读
  2. leetcode 141 判断链表中是否存在环

    2024-01-18 20:26:02       54 阅读
  3. C语言 switch case 语句优化方案其一

    2024-01-18 20:26:02       60 阅读
  4. FFmpeg之PostProc

    2024-01-18 20:26:02       51 阅读
  5. leetcode2744. 最大字符串配对数目

    2024-01-18 20:26:02       54 阅读
  6. virt-install支持VF

    2024-01-18 20:26:02       58 阅读