mt4服务器数据导出到php网站,MT4 DLL开发:通过DLL传递数据到外部程序

本文介绍了如何使用VisualC++创建一个MFC DLL,包含了三个核心功能函数:获取收盘价、最高价和SMA移动平均线值。DLL的接口设计与MT4的数据结构兼容,利用ArrayCopyRates函数进行数据转换。在MT4中调用DLL时,通过操作系统自动完成数据映射。详细展示了DLL的C++实现及MT4中的调用示例代码。
摘要由CSDN通过智能技术生成

在Visual C++开发工具中创建一个工程,选择MFC(DLL)类型,假设工程名为demo。创建好工程后,最核心的两个文件为demo.cpp和demo.def。

假设希望开发的dll文件中包含三个功能函数:

? double GetCloseValue( RateInfo* rates,int totalRecords, int shift )  返回收盘价位

? double GetHighValue( RateInfo* rates,int totalRecords, int shift )  返回最高价位

? void GetSMAArray( RateInfo* rates, int totalRecords, int period, double result[] ) 返回SMA移动平均线值

其中RateInfo被定义为结构类型

struct RateInfo

{

unsigned int time;   //时间

double open;      //开盘价格

double low;       //最低价格

double high;      //最高价格

double close;     //收盘价格

double volume;   //成交量

};

比较精妙的是MT4提供了ArrayCopyRates函数用于复制一段走势图上的数据到一个二维数组,并返回复制柱子的总数。其第二维为固定的6个项目,从0到5分别为“时间、开盘价格、最低价格、最高价格、收盘价格、成交量”。

int ArrayCopyRates( void dest_array[], void symbol, void timeframe)

因此这里的RateInfo结构定义正好对应上面二维数组的第二维,MT4程序也是默认通过这种方式来提供二维数组到结构指针(即RateInfo结构数组)的映射的。

在demo.def中定义DLL的输出函数(如下),经过编译后将在指定目录生成DLL文件。

LIBRARY      "demo"

EXPORTS

GetCloseValue

GetHighValue

GetSMAArray

将生成的DLL文件拷贝到MT4程序的”experts/libraries目录下。在MT4程序中调用引用DLL的代码为:

#import "demo.dll"

double GetCloseValue( double rates[][6], int totalRecords, int shift );

double  GetHighValue( double rates[][6], int totalRecords, int shift );

void   GetSMAArray( double rates[][6], int totalRecords, int period, double& results[]);

#import

这里引用DLL函数的一个重要的区别在于RateInfo*被映射为二维数组double rates[][6],也就是说MT4调用DLL的时候由操作系统根据内存指针完成了数据的访问,且结构定义中的unsigned int是从double类型转换后得到的。在MT4程序中调用DLL中函数的代码为:

int start()

{

double rates[][6];

int totalRecords = ArrayCopyRates( rates, Symbol(), 0 );

for( int i = totalRecords; i >= 0; i-- )

{ `

results= EMPTY;

}

GetSMAArray( rates, totalRecords, period, results );

return(0);

}

例代码(DLL对应cpp文件中的函数定义和代码):

//+------------------------------------------------------------------+

//|                        MT4调用DLL示例程序 |

//|             Copyright @2009-2011, |

//|                       http://vod.fxeahoster.com |

//+------------------------------------------------------------------+

#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers

#define MT4_EXPFUNC __declspec(dllexport)

//+-----------------------------------------------------------------------------------------------------------------------------+

//| MT4数据结构                                |

//+-----------------------------------------------------------------------------------------------------------------------------+

#pragma pack(push,1)

struct RateInfo

{

unsigned int time;

double open;

double low;

double high;

double close;

double volume;

};

struct MqlStr

{

int len;

char* string;

};

#pragma pack(pop)

//+-----------------------------------------------------------------------------------------------------------------------------+

//|          DLL函数定义                             |

//+-----------------------------------------------------------------------------------------------------------------------------+

MT4_EXPFUNC double _stdcall GetCloseValue( RateInfo* rates,int totalRecords, int shift )

{

return( rates[totalRecords-shift-1].close );

}

MT4_EXPFUNC double _stdcall GetHighValue( RateInfo* rates,int totalRecords, int shift )

{

return( rates[totalRecords-shift-1].high );

}

MT4_EXPFUNC void _stdcall GetSMAArray( RateInfo* rates, int totalRecords, int period, double result[] )

{

for( int i = 0; i < totalRecords; i++)

{

double sum = 0.0;

for( int k = 0; k < period ; k++ )

{

sum += rates[totalRecords-i-1-k].close;

}

result[totalRecords-i-1] = sum / period ;

}

}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值