文章 "如何从 MQL5 (MQL4) 访问 MySQL 数据库"

 

新文章 如何从 MQL5 (MQL4) 访问 MySQL 数据库已发布:

本文描述开发一个在 MQL 与 MySQL 之间的接口。它讨论了现有的可行解决方案,并采用更便捷的途径来实现与数据库协同工作的链接库。本文包括功能的详尽描述,接口结构,例程,以及一些使用 MySQL 时的特性。作为软件解决方案,本文附件中包含了用于 MQL4 和 MQL5 语言的动态库,文档和脚本例程。

介绍

MQL 与数据库的交互问题并非新事物,但它们依然是相关的。利用数据库​​可以极大增强 MetaTrader 的可塑性: 存储并分析价格历史,从一个交易平台拷贝交易至另一个平台,提供实时报价/交易,在服务器端定期进行深度分析计算,使用 web 技术监视并远程控制账户。

总之,有许多种应用尝试从 MQL 和 MySQL 的组合之中获益,一些方案已经在代码库里出现。

例如 "MySQL 包装 - 用于 MetaTrader 4 的链接库" 就是这样的项目,许多程序员开始自己开发,在将来还可扩充。我认为,这种解决方案的缺点之一是分配特殊数组用来从数据库中读数据。

另一个项目 "MySQL 日志 1 - 用于 MetaTrader 4 的 EA" 更加专业,它不使用包装来访问标准链接库 libmysql.dll。因此,它不能在 MetaTrader4 编译版 600+ 上工作,由于 char 字符类型已经被 wchar_t 替代,且使用 int 类型替代了 TMYSQL 结构指针,导致在项目中产生内存泄漏 (内存分配不能控制/释放)。

另一个有趣的项目是 "EAX_Mysql - MySQL 链接库 - 用于 MetaTrader 5 的链接库"。它是十分出色的实现。不过作者列出了一些缺点,在使用时有强制限制。

任何人若需要在他们的项目中使用数据库,有两个选项: 要么开发自己的解决方案,并了解它的每一个部分,或者使用/改编任何第三方解决方案,了解如何使用它们并检测是否会阻碍他们的项目。

在我开发一个相当复杂的自动交易时,就要面对这样的必要性和两个选项。依照现有项目经过搜索,且研究了很多的解决方案后,我意识到,已发现的实施方案均无助于把我的自动交易提升到“专业水平”。

此外,也有些荒谬的方案,例如: 使用标准 libmysql.dll 执行 DML/DDL 操作 (插入/更新/删除数据, 在数据库中创建/废弃对象), 以及将数据检索 (SELECT) 的实现作为 HTTP 请求 (使用 inet.dll) 与 MySQL 服务器端的 web 服务器上的 PHP 脚本通信。而 SQL 查询被写在 PHP 脚本中。

换句话说,要运行该项目,一定需要保证下 述所有部件准备妥当,配置好并运行:MySQL 服务器,Apache/ IIS Web 服务器,在服务器端的 PHP/ASP 脚本... 大量技术的组合。当然,在某些情况下,这是可以接受的,但当唯一的任务就是从数据库中查询数据 - 那么这些全无意义。此外,支持如此累赘的方案也耗费时间。

大部分的方案在插入数据,创建对象等等操作时没有问题。问题在于数据查询,因为数据将会被返回调用环境。

我认为出于此目的而使用数组是不切实际的和不方便的,简单的原因就是在主程序的开发/调试/支持过程中,数据库查询是可以变化的,而您也要正确控制为数组分配的内存.. 那么,这些可以,而且必须要避免。

下文讨论的 MQL <-> MySql 的接口基于 Oracle PL/SQL, MS SQL T-SQL, AdoDB 等产品内使用的典型方式- 使用游标。这个接口的开发目标是易于编程和维护,再加上最少元部件。它作为 DLL 包装器实现,连接标准链接库 libmysql.dll,且接口函数集合作为一个 .mqh 文件。

1. MQL <-> MySQL 接口

在 MetaTrader 终端之间交互 (通过 MQL 程序) 可以在如下元部件的帮助下实现:

MQL 和 MySQL 交互规划

作者:Eugeniy Lugovoy

 
對於x64平台,使用下面的庫(附討論)
附加的文件:
 
elugovoy:
對於x64平台,使用下面的庫(附討論)
Access violation read to 0x72656472
 

Hello,

which version of MT5 terminal you are uses?

please try latest update of MQLMySQL libraries, attached here.

附加的文件:
MQL4_x32.zip  1073 kb
MQL5_x32.zip  1073 kb
MQL5_x64.zip  1076 kb
MQLMySQL_v2_2.zip  5072 kb
 
elugovoy:

Hello,

which version of MT5 terminal you are uses?

please try latest update of MQLMySQL libraries, attached here.

I have used the latest version of MT4, when only use one call, it's OK, But when use Sender send data and use receiver reccive data at the same MT4 at the same time, the MT4 print the error "Access violation read to 0x65D6954".
 
elugovoy:

Hello,

which version of MT5 terminal you are uses?

please try latest update of MQLMySQL libraries, attached here.

   I s  the mqlmysql.dll is 
singleton call, the same mt4 can't multicall this dll 
 

Maybe you have libmysql.dll in different locations in your operating system.

if you have libmysql.dll in %WINDOWS%\SYSTEM32 folder, you have to delete it from MT4\MQL4\Experts\Libraries

so the only one dll have to be used.

The one terminal attaches the library once, even if expert advisor used for different charts. so, the reason is different.

The MQLMySQL.dll uses mutexes for accessing internal shared data, so it's also can't be a reason.

If you can, please provide me with the MQL code you are uses to raise such situation and the build number of MT4, I will try to test it and deliver the solution.

 

Thanks. 

 
elugovoy:

Maybe you have libmysql.dll in different locations in your operating system.

if you have libmysql.dll in %WINDOWS%\SYSTEM32 folder, you have to delete it from MT4\MQL4\Experts\Libraries

so the only one dll have to be used.

The one terminal attaches the library once, even if expert advisor used for different charts. so, the reason is different.

The MQLMySQL.dll uses mutexes for accessing internal shared data, so it's also can't be a reason.

If you can, please provide me with the MQL code you are uses to raise such situation and the build number of MT4, I will try to test it and deliver the solution.

 

Thanks. 

MQL CODE is collect mt4 data to the mysql table,  when use in one mt4 in only one, it is ok, when use four or more in two mt4, it print Access violation read to 0x00000002 in ..\MQLMySQL.dll'

 


附加的文件:
sendData.mq4  11 kb
 
elugovoy:

Maybe you have libmysql.dll in different locations in your operating system.

if you have libmysql.dll in %WINDOWS%\SYSTEM32 folder, you have to delete it from MT4\MQL4\Experts\Libraries

so the only one dll have to be used.

The one terminal attaches the library once, even if expert advisor used for different charts. so, the reason is different.

The MQLMySQL.dll uses mutexes for accessing internal shared data, so it's also can't be a reason.

If you can, please provide me with the MQL code you are uses to raise such situation and the build number of MT4, I will try to test it and deliver the solution.

 

Thanks. 

 I used mysql memory table, is it the problem? ENGINE=MEMORY
 
yukaixie:
 I used mysql memory table, is it the problem? ENGINE=MEMORY
I changed the table type engine from memory to Innodb, the problem is still exist. 2015.04.08 15:31:59.296 Access violation read to 0x00000004 in '..MQLMySQL.dll'

 
yukaixie:

MQL CODE is collect mt4 data to the mysql table,  when use in one mt4 in only one, it is ok, when use four or more in two mt4, it print Access violation read to 0x00000002 in ..\MQLMySQL.dll'

 


MySqlDisconnect(DB);

the database disconnection needed, but it's still not solve the problem. 

原因: