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[]