请观看如何免费下载自动交易
请在Twitter上找到我们!
加入我们粉丝页
有趣的脚本?
因此发布一个链接 -
让其他人评价
喜欢这个脚本? 在MetaTrader 5客户端尝试它
程序库

MQL4到MQL5兼容库 - MetaTrader 5程序库

显示:
2835
等级:
(49)
已发布:
2014.02.11 07:13
已更新:
2016.11.22 07:33
mql4compat.mqh (54.62 KB) 预览
需要基于此代码的EA交易或指标吗?请在自由职业者服务中订购 进入自由职业者服务

本库允许在Metatrader 5运行绝大多数Metatrader4脚本和指标。绝大多数函数都已经覆盖到了,只有EA的交易逻辑没有添加,因为交易机制需要重写。

本程序库覆盖了除了EA的交易机制外的绝大多数函数。对于在MT5上会报错的函数,有同名的以MQL4结尾的替代函数。他们中的大多数是用于在MQL5中存在冲突的同名函数。原文章仅仅提供了一个框架库,这个版本的库对MQL4函数进行了填充。请用“MQL4”模式搜索特定的函数,查看库中的源代码。

TODO:  EA交易机制中的订单发送和控制。一些函数有通用的提示,并会在日志中给出错误消息记录。这种情况的返回码一直为-1

安装:

将mql4compat.mqh放到Include文件夹下(它在你的Windows用户漫游配置文件中)。

在你的MQL4 EA/指标/库/脚本中, 请在#property 定义后面增加:


 

#include <mql4compat.mqh>

 

改变MQL4函数:

int init() 改为 int OnInit()
int deinit() 改为 void OnDeinit(const int reason);
int start() 在脚本中改为 void OnStart();
int start() 在指标中改为:
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {

最后一行return()函数应改为如下形式:

   return(rates_total);

 由于函数参数的变化,需要改变MQL4和MQL5之间这些名称冲突的函数,方法是在它们的函数名后增加MQL4。

   int ArrayInitializeMQL4(double &array[], double value)
   int ArrayMaximumMQL4(double &array[], int count=WHOLE_ARRAY, int start=0)
   int ArrayMinimumMQL4(double &array[], int count=WHOLE_ARRAY, int start=0)
   int ArraySortMQL4(double &array[], int count=WHOLE_ARRAY, int start=0, int sort_dir=MODE_ASCEND)
   double CopyBufferMQL4(int handle,int index,int shift)
   double EnvelopesMQL4(string symbol, int tf, int ma_period, int method, int ma_shift, int price, double deviation, int mode, int shift)
   double FileReadDoubleMQL4(int handle, int size=DOUBLE_VALUE)
   bool FileSeekMQL4(long handle, int offset, ENUM_FILE_POSITION origin)
   int IndicatorCountedMQL4(int _prev_calculated=0)
   bool ObjectCreateMQL4(string name, ENUM_OBJECT type, int window, datetime time1, double price1, datetime time2=0, double price2=0, datetime time3=0, double price3=0)
   bool ObjectDeleteMQL4(string name)
   string ObjectDescriptionMQL4(string name)
   int ObjectFindMQL4(string name)
   bool ObjectMoveMQL4(string name, int point, datetime time1, double price1)
   string ObjectNameMQL4(int index)
   int ObjectsDeleteAllMQL4(int window=EMPTY, int type=EMPTY)
   int ObjectsTotalMQL4(int type=EMPTY, int window=-1)
   bool SetIndexBufferMQL4(int index, double &array[])
   string StringConcatenateMQL4(string _str1,string _str2, ... string _str64="")
   int StringGetCharMQL4(string text, int pos)
   string StringSetCharMQL4(string text, int pos, int value)
   double iACMQL4(string symbol, int tf, int shift)
   double iADMQL4(string symbol, int tf, int shift)
   double iADXMQL4(string symbol, int tf, int period, int price, int mode, int shift)
   double iAOMQL4(string symbol, int tf, int shift)
   double iATRMQL4(string symbol, int tf, int period, int shift)
   double iAlligatorMQL4(string symbol, int tf, int jaw_period, int jaw_shift, int teeth_period, int teeth_shift, int lips_period, int lips_shift, int method, int price, int mode, int shift)
   double iBWMFIMQL4(string symbol, int tf, int shift)
   double iBandsMQL4(string symbol, int tf, int period, double deviation, int bands_shift, int method, int mode, int shift)
   double iBearsPowerMQL4(string symbol, int tf, int period, int price, int shift)
   double iBullsPowerMQL4(string symbol, int tf, int period, int price, int shift)
   double iCCIMQL4(string symbol, int tf, int period, int price, int shift)
   double iDeMarkerMQL4(string symbol, int tf, int period, int shift)
   double iForceMQL4(string symbol, int tf, int period, int method, int price, int shift)
   double iFractalsMQL4(string symbol, int tf, int mode, int shift)
   double iGatorMQL4(string symbol, int tf, int jaw_period, int jaw_shift, int teeth_period, int teeth_shift, int lips_period, int lips_shift, int method, int price, int mode, int shift)
   double iIchimokuMQL4(string symbol, int tf, int tenkan_sen, int kijun_sen, int senkou_span_b, int mode, int shift)
   double iMACDMQL4(string symbol, int tf, int fast_ema_period, int slow_ema_period, int signal_period, int price, int mode, int shift)
   double iMAMQL4(string symbol, int tf, int period, int ma_shift, int method, int price, int shift)
   double iMFIMQL4(string symbol, int tf, int period, int shift)
   double iMomentumMQL4(string symbol, int tf, int period, int price, int shift)
   double iOBVMQL4(string symbol, int tf, int price, int shift)
   double iOsMAMQL4(string symbol, int tf, int fast_ema_period, int slow_ema_period, int signal_period, int price, int shift)
   double iRSIMQL4(string symbol, int tf, int period, int price, int shift)
   double iRVIMQL4(string symbol, int tf, int period, int mode, int shift)
   double iSARMQL4(string symbol, int tf, double step, double maximum, int shift)
   double iStdDevMQL4(string symbol, int tf, int ma_period, int ma_shift, int method, int price, int shift)
   double iStochasticMQL4(string symbol, int tf, int Kperiod, int Dperiod, int slowing, int method, int field, int mode, int shift)
   double iWPRMQL4(string symbol, int tf, int period, int shift)

在指标中,OnInit()函数和#property头需要手动调整。IndicatorCounted()函数也需要通过去除函数定义,或改为MQL5 预定义变量,而变为同名变量:

#define IndicatorCounted IndicatorCountedMQL4(prev_calculated)

在自定义指标中请查看#property 头在MQL5中是怎样的,然后将输入参数的定义从extern变为input。在MQL5中它们现在都是constants类型,任何企图改变其值的行为都将导致报错。要向改变,需要创建临时变量并将输入参数的值赋给它们。

在你主代码的首行,请添加MQL4保留变量初始化函数(如果在OnCalculate() 函数中使用,自定义指标将对 OHLC/Time/Volume这些保留变量报警,忽略即可): 

 

// -- 初始化 MQL4 兼容环境
   InitMQL4Env();

由MetaQuotes Ltd译自英文
原代码: https://www.mql5.com/en/code/1787

相对动量指数(RMI) 相对动量指数(RMI)

RMI(相对动量指数)指标是RSI指标的改进版,计算时考虑了动量。

趋势评分 趋势评分

基于计算多头和空头柱形数量的简单趋势指标。

可视化分析测试结果 可视化分析测试结果

由策略测试期创建的简单可视化分析图形对象。

三屏Elder箭头 三屏Elder箭头

一个箭头形式的指标,基于Elder交易系统。