如何通过脚本将MT4图表数据导出到CSV文件?

时间:2012-09-07 18:19:15

标签: metatrader4

mql4脚本在附加到图表时未导出到CSV - 文件。 的为什么吗

static datetime check;
//+------------------------------------------------------------------+
//|   scripts/Export.mq4                  pre  Build 562-            |
//|   MQL4/Scripts/Export.mq4             post Build 562+            |
//+------------------------------------------------------------------+
void start() {                // New-MQL4 post Build 562+: void OnStart(){}
   if ( check != Time[0] ) {
        check  = Time[0];
        WriteCSV(); 
      } 
}

//+------------------------------------------------------------------+
//| WriteCSV                                                         |
//+------------------------------------------------------------------+
void WriteCSV() {
   int handle = FileOpen( StringConcatenate( Symbol(), ".csv" ),
                          FILE_CSV|FILE_READ|FILE_WRITE, ','
                          );

   if ( handle > 0 ) {
        FileSeek( handle, 0, SEEK_END ); 

        FileWrite( handle,
                   TimeToStr( TimeCurrent(), TIME_DATE ),
                   iOpen ( Symbol(), Period(), 1 ),
                   iHigh ( Symbol(), Period(), 1 ),
                   iLow  ( Symbol(), Period(), 1 ),
                   iClose( Symbol(), Period(), 1 )
                   );

        FileClose( handle );
      }
}
//+------------------------------------------------------------------+

3 个答案:

答案 0 :(得分:2)

This looks like a old thread but I just wanted to throw this in for what it's worth. There are multiple things that concern me here.

  1. your first command in the script:

    int handle = FileOpen( StringConcatenate( Symbol(), ".csv" ),
                           FILE_CSV|FILE_READ|FILE_WRITE, ','
                           );
    

    should open the file for write, if it doesn't exist it will create it. So, if the file isn't located anywhere, then it's not being created. as stated by user3666197. Make sure your running MT4 as Administrator.

  2. you may want to include a loop when retrieving any data with the 'i' prefix. iClose, iOpen, iHigh, iLow - because all of the data your requesting may not be available. MT4 will need to pull from the server in which case the number of bars returned will be 0.

iClose() Returned value Close price value for the bar of specified symbol with timeframe and shift. If local history is empty (not loaded), function returns 0.

To check errors, one has to call the GetLastError() function. So if you've found the file and it is being created but no bars or just a few bars are being saved this would be why.

Just thought I'd throw that in there in case it became an issue for you down the road.

Good luck

答案 1 :(得分:0)

您尝试导出的目录很可能没有MT4的权限,因此不允许访问。

答案 2 :(得分:0)

文件名可能与O / S命名限制冲突:

首先测试一个恒定的O / S安全字符串

int handle = FileOpen( "FileOpenTest_FILENAME_FEASIBLE.CSV",
                        FILE_CSV | FILE_READ | FILE_WRITE
                        );

最后但并非最不重要的,请检查您的经纪人的工具命名是否与O / S发生冲突。已经看到这些已停止MQL4代码的实践,这些代码已处于生产状态多年。

符号(重新)同样命名经纪人方面的" EURUSD.." 可能会令人惊讶地阻止您的标准fileIO操作 - 第一手经验Windows Server 2008 DataCentre托管,一旦Broker公司营销部门为各种账户引入FOREX工具的新命名约定,O / S突然开始默默地拒绝打开具有类似Symbol()注入工件的文件名/产品类型。

fileIO操作可能会与用户有效权利冲突:

检查您的 user 有效地点的权利,其中MT4允许fileIO - s发生:

  1. ~<_TerminalDIR_>\MQL4\Files
  2. ~<_TerminalDIR_>\tester\files
  3. (自2014-10-01生效,Build 670)