工作中需要将 MT4 的数据读取并且存储到 Redis 数据库中去,同时 MT4 读取 Redis 当中的数据用于下单的切换账户。 MT4 支持使用 MQL 进行开发,通过调用标准的系统 DLL 实现系统调用,因此技术实现并不是太难,只需要按 MQL 的接口要求编写相应的 CPP 代码 编译成 DLL 即可实现所需要的功能。
在 Windows 平台上,连接 Redis 的客户端选用 Hiredis 作为连接客户端 lib。
MT4 连接 Redis 头文件定义, MT4RedisPlugin.h
#define MT4_REDIS_ERR -1 /* 错误 */
#define MT4_REDIS_OK 0 /* 成功 */
#define MT4_REDIS_ERR_IO 1 /* 读写错误 */
#define MT4_REDIS_ERR_EOF 3 /* 文件结束 */
#define MT4_REDIS_ERR_PROTOCOL 4 /* 协议错误 */
#define MT4_REDIS_ERR_OTHER 2 /* 其他错误 */
#define MT4_REDIS_CMD_SUCCESSED 0 /* 成功 */
#define MT4_REDIS_CMD_FAILED -1 /* 失败 */
//---
#define MT4_EXPFUNC __declspec(dllexport)
//+------------------------------------------------------------------+
//| 测试 Redis 是否正常工作 |
//+------------------------------------------------------------------+
MT4_EXPFUNC int __stdcall RedisTest(const char* server, const int port);
//+------------------------------------------------------------------+
//| 执行 Redis 命令 "GET key" |
//+------------------------------------------------------------------+
MT4_EXPFUNC wchar_t* __stdcall RedisGet(const wchar_t* key);
//+------------------------------------------------------------------+
//| 执行 Redis 命令 "GET key" |
//+------------------------------------------------------------------+
MT4_EXPFUNC wchar_t* __stdcall RedisGetWithTimeout(const wchar_t* key,int timeout);
//+------------------------------------------------------------------+
//| 执行 Redis 命令 "SET key value" |
//+------------------------------------------------------------------+
MT4_EXPFUNC int __stdcall RedisSet(const wchar_t* key, const wchar_t* value);
//+------------------------------------------------------------------+
//| 执行 Redis 命令 "SET key value" |
//+------------------------------------------------------------------+
MT4_EXPFUNC int __stdcall RedisSetWithTimeout(const wchar_t* key, const wchar_t* value,int timeout);
//+------------------------------------------------------------------+
//| 执行 Redis 命令 |
//+------------------------------------------------------------------+
MT4_EXPFUNC int __stdcall RedisCommand(const wchar_t* command);
//+------------------------------------------------------------------+
//| 执行 Redis 命令 |
//+------------------------------------------------------------------+
MT4_EXPFUNC int __stdcall RedisCommandWithTimeout(const wchar_t* command,int timeout);
//+------------------------------------------------------------------+
//| char 转 WCHAR 、wchar_t、LPWSTR 等 |
//+------------------------------------------------------------------+
static wchar_t * CStr2WStr(const char *cStr);
//+------------------------------------------------------------------+
//| WCHAR 、wchar_t、LPWSTR 转 char |
//+------------------------------------------------------------------+
static char* WStr2CStr(const wchar_t *wchar);
//+------------------------------------------------------------------+
//| 显示消息框 |
//+------------------------------------------------------------------+
static void MT4RedisMsgBox(const wchar_t* msg);
使用 Hiredis 读取 Redis 数据库
//+------------------------------------------------------------------+
//| 执行 Redis 命令 "GET key" |
//+------------------------------------------------------------------+
MT4_EXPFUNC wchar_t* __stdcall RedisGetWithTimeout(const wchar_t* key,int timeout)
{
// 构造 windows socket 对象
//WSADATA wsaData;
//WSAStartup(MAKEWORD(2,1), &wsaData);
// 设置超时
struct timeval tv;
tv.tv\_sec = timeout/1000;
tv.tv\_usec = timeout \*1000;
redisContext\* context = redisConnectWithTimeout(REDIS\_SERVER,REDIS\_PORT,tv);
//判断连接是否有错误
if (context->err) {
printf("连接Redis服务器失败\[%s:%d\]\\n",REDIS\_SERVER,REDIS\_PORT);
redisFree(context);
return NULL;
}
//构造Redis命令
//获取size
size\_t len = wcslen(key) + 20;
//命令长度
if (len > COMMAND\_BUFFER) {
printf("命令过长,命令字符串必须短于 %d",COMMAND\_BUFFER);
return NULL;
}
// 构建命令
wchar\_t command\[COMMAND\_BUFFER\];
//构造Redis命令
wsprintf(command,L"GET %s",key);
//字符串转换
const char\* command1 = WStr2CStr(command);
//执行Redis命令
printf("准备执行命令\[%s\]\\n", command1);
// 执行命令
redisReply\* reply = (redisReply\*)redisCommand(context, command1);
//没有Redis响应
if( NULL == reply)
{
printf("执行命令失败\[%s\]\\n",command1);
redisFree(context);
return NULL;
}
//响应状态
if ( reply->type != REDIS\_REPLY\_STRING)
{
printf("执行命令失败\[%s\]\\n",command1);
freeReplyObject(reply);
redisFree(context);
return NULL;
}
printf("键\\"%s\\"的值是\\"%s\\"\\n", key, reply->str);
//字符串转换
wchar\_t \* result = CStr2WStr(reply->str);
//释放Reply对象
freeReplyObject(reply);
//释放Redis连接
redisFree(context);
printf("成功执行命令\[%s\]\\n", command1);
delete command1;
command1 = NULL;
//返回结果字符串
return result;
}
使用 Hiredis 写入 Redis 数据库
//+------------------------------------------------------------------+
//| 执行 Redis 命令 "SET key value" |
//+------------------------------------------------------------------+
MT4_EXPFUNC int __stdcall RedisSetWithTimeout(const wchar_t* key, const wchar_t* value, int timeout)
{
// 构造 Redis 命令
// 获取 size
size_t len = wcslen(key)+wcslen(value) + 20;
// 命令长度
if (len > COMMAND_BUFFER) {
printf ("命令过长,命令字符串必须短于 % d",COMMAND_BUFFER);
return MT4_REDIS_CMD_FAILED;
}
// 构建命令
wchar_t command[COMMAND_BUFFER];
// 构造 Redis 命令
wsprintf(command,L"SET %s %s",key,value);
return RedisCommandWithTimeout(command,timeout);
}
使用 Hiredis 执行 Redis 命令
//+------------------------------------------------------------------+
//| 执行 Redis 命令 |
//+------------------------------------------------------------------+
MT4_EXPFUNC int __stdcall RedisCommandWithTimeout(const wchar_t* command,int timeout)
{
// 构造 windows socket 对象
//WSADATA wsaData;
//WSAStartup(MAKEWORD(2,1), &wsaData);
// 设置超时
struct timeval tv;
tv.tv\_sec = timeout/1000;
tv.tv\_usec = timeout \*1000;
redisContext\* context = redisConnectWithTimeout(REDIS\_SERVER,REDIS\_PORT,tv);
//判断连接是否有错误
if (context->err) {
printf("连接Redis服务器失败\[%s:%d\]\\n",REDIS\_SERVER,REDIS\_PORT);
redisFree(context);
return MT4\_REDIS\_CMD\_FAILED;
}
// 命令长度
size\_t len = wcslen(command) + 1;
if (len > COMMAND\_BUFFER) {
printf("命令过长,命令字符串必须短于 %d",COMMAND\_BUFFER);
redisFree(context);
return MT4\_REDIS\_CMD\_FAILED;
}
//字符串转换
const char\* command1 = WStr2CStr(command);
//执行Redis命令
printf("准备执行命令\[%s\]\\n", command1);
// 执行命令
redisReply\* reply = (redisReply\*)redisCommand(context, command1);
//没有Redis响应
if( NULL == reply)
{
printf("执行命令失败\[%s\]\\n",command1);
redisFree(context);
return MT4\_REDIS\_CMD\_FAILED;
}
//响应状态
if( !(reply->type == REDIS\_REPLY\_STATUS && \_stricmp(reply->str,"OK")==0))
{
printf("执行命令失败\[%s\]\\n",command1);
freeReplyObject(reply);
redisFree(context);
return MT4\_REDIS\_CMD\_FAILED;
}
//释放Reply对象
freeReplyObject(reply);
//释放Redis连接
redisFree(context);
printf("成功执行命令\[%s\]\\n", command1);
delete command1;
command1 = NULL;
//返回成功状态
return MT4\_REDIS\_CMD\_SUCCESSED;
}
测试 MQL 代码
//+------------------------------------------------------------------+
//| DLLSampleTester.mq4 |
//| Copyright ゥ 2005-2014, MetaQuotes Software Corp. |
//| http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "版权 ゥ 2005-2014, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net/"
#import "MT4RedisPlugin.dll"
string HelloWorld(string);
string RedisGet(string);
string RedisGetWithTimeout(string,int);
int RedisSet(string,string);
int RedisSetWithTimeout(string, string, int);
int RedisCommand(string);
int RedisCommandWithTimeout(string,int);
int RedisTest(string,int);
#import
#define TIME_INDEX 0
#define OPEN_INDEX 1
#define LOW_INDEX 2
#define HIGH_INDEX 3
#define CLOSE_INDEX 4
#define VOLUME_INDEX 5
//+------------------------------------------------------------------+
//| 专家初始化函数 |
//+------------------------------------------------------------------+
int init()
{
Print ("开始执行");
string hello;
hello = HelloWorld("1234abc");
Print ("结果是", hello);
string test;
test = RedisTest("127.0.0.1",6379);
Print ("测试结果是", test);
int cmd1;
cmd1 = RedisCommand("set KEYabc VALUE123");
Print ("RedisCommand 测试 =", cmd1);
int cmd2;
cmd2 = RedisCommandWithTimeout("set KEYabc VALUE123",10000);
Print ("RedisCommandWithTimeout 测试 =", cmd2);
string key1 = "key1";
string value1 = "value1";
int set1 = RedisSet(key1,value1);
Print ("RedisSet 测试 =", set1);
string key2 = "key2";
string value2 = "value2";
int timeout = 10000;
int set2 = RedisSetWithTimeout(key2,value2,timeout);
Print ("RedisSetWithTimeout 测试 =", set2);
string get1 = RedisGet(key1);
Print ("RedisGet 测试 =", get1);
string get2 = RedisGetWithTimeout(key2,timeout);
Print ("RedisGetWithTimeout 测试 =", get2);
string key3 = "key3";
string get3 = RedisGet(key3);
Print ("RedisGet 测试 =", get3);
string key4 = "key4";
string get4 = RedisGet(key4);
Print ("RedisGet 测试 =", get4);
//---
return(0);
}
//+------------------------------------------------------------------+
//| 数组函数调用 |
//+------------------------------------------------------------------+
int start()
{
return(0);
}
//+------------------------------------------------------------------+