分享

(8)[转载]MT4 DLL开发

 quanshan 2019-05-11
  • 在Visual C++开发工具中创建一个工程,选择MFC(DLL)类型,假设工程名为demo。创建好工程后,最核心的两个文件为demo.cpp和demo.def。
    假设希望开发的dll文件中包含三个功能函数:
    复制代码
  1.  double GetCloseValue( RateInfo* rates,int totalRecords, int shift )返回收盘价位
  2.  double GetHighValue( RateInfo* rates,int totalRecords, int shift )返回最高价位
  3.  void GetSMAArray( RateInfo* rates, int totalRecords, int period, double result[] ) 返回SMA移动平均线值

其中RateInfo被定义为结构类型:
复制代码
  1. struct RateInfo
  2. {
  3. unsigned int time; //时间
  4. double open;//开盘价格
  5. double low; //最低价格
  6. double high;//最高价格
  7. double close; //收盘价格
  8. double volume; //成交量
  9. };

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

因此这里的RateInfo结构定义正好对应上面二维数组的第二维,MT4程序也是默认通过这种方式来提供二维数组到结构指针(即RateInfo结构数组)的映射的。
在demo.def中定义DLL的输出函数(如下),经过编译后将在指定目录生成DLL文件。
复制代码
  1. LIBRARY"demo"
  2. EXPORTS
  3. GetCloseValue
  4. GetHighValue
  5. GetSMAArray

将生成的DLL文件拷贝到MT4程序的”experts/libraries目录下。在MT4程序中调用引用DLL的代码为:
复制代码
  1. #import "demo.dll"
  2. double GetCloseValue( double rates[][6], int totalRecords, int shift );
  3. doubleGetHighValue( double rates[][6], int totalRecords, int shift );
  4. void GetSMAArray( double rates[][6], int totalRecords, int period, double& results[]);
  5. #import

这里引用DLL函数的一个重要的区别在于RateInfo*被映射为二维数组double rates[][6],也就是说MT4调用DLL的时候由操作系统根据内存指针完成了数据的访问,且结构定义中的unsigned int是从double类型转换后得到的。在MT4程序中调用DLL中函数的代码为:
复制代码
  1. int start()
  2. {
  3. double rates[][6];
  4. int totalRecords = ArrayCopyRates( rates, Symbol(), 0 );
  5. for( int i = totalRecords; i >= 0; i-- )
  6. { `
  7. results[i] = EMPTY;
  8. }
  9. GetSMAArray( rates, totalRecords, period, results );
  10. return(0);
  11. }

示例代码(DLL对应cpp文件中的函数定义和代码):
复制代码
  1. //+------------------------------------------------------------------+
  2. //|MT4调用DLL示例程序 |
  3. //| Copyright @2009-2010, 笨蛋学经济 |
  4. //| [url]http://macy01.[/url] |
  5. //+------------------------------------------------------------------+
  6. #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
  7. #define MT4_EXPFUNC __declspec(dllexport)
  8. //+-----------------------------------------------------------------------------------------------------------------------------+
  9. //| MT4数据结构|
  10. //+-----------------------------------------------------------------------------------------------------------------------------+
  11. #pragma pack(push,1)
  12. struct RateInfo
  13. {
  14. unsigned int time;
  15. double open;
  16. double low;
  17. double high;
  18. double close;
  19. double volume;
  20. };
  21. struct MqlStr
  22. {
  23. int len;
  24. char* string;
  25. };
  26. #pragma pack(pop)
  27. //+-----------------------------------------------------------------------------------------------------------------------------+
  28. //|DLL函数定义 |
  29. //+-----------------------------------------------------------------------------------------------------------------------------+
  30. MT4_EXPFUNC double _stdcall GetCloseValue( RateInfo* rates,int totalRecords, int shift )
  31. {
  32. return( rates[totalRecords-shift-1].close );
  33. }
  34. MT4_EXPFUNC double _stdcall GetHighValue( RateInfo* rates,int totalRecords, int shift )
  35. {
  36. return( rates[totalRecords-shift-1].high );
  37. }
  38. MT4_EXPFUNC void _stdcall GetSMAArray( RateInfo* rates, int totalRecords, int period, double result[] )
  39. {
  40. for( int i = 0; i < totalRecords; i++)
  41. {
  42. double sum = 0.0;
  43. for( int k = 0; k < period ; k++ )
  44. {
  45. sum += rates[totalRecords-i-1-k].close;
  46. }
  47. result[totalRecords-i-1] = sum / period ;
  48. }
  49. }

http://www./2012/05/17/1091.shtml

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多