FileWrite

函数预定给CSV文件录入数据,除非等于0,定界符自动插入,在编辑文件中,添加最后字节线 "\r\n" 。

uint  FileWrite(

   int  file_handle,   // 文件句柄

   ...                 // 记录参量的列表

   );

参量

file_handle

[in]  通过 FileOpen()返回文件说明符。

...

[in] 参量列表由逗号分开,编辑入文件中,编辑参量数量最高为63。

返回值

编辑字节数。

注释

数量转换成文本输出(参看Print()函数)。双精度数据以小数点后16位精确度输出,数据以传统或科学模式显示-依据该模式是最紧凑的。浮点型数据以小数点后5位显示,为了以不同精确度输出真实数字或以清晰的指定模式,使用 DoubleToString()

布尔类型数量以true" 或者 "false"字符串显示,日期时间类型数量以"YYYY.MM.DD HH:MI:SS"显示。

示例 :

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

//|                                               Demo_FileWrite.mq5 |

//|                        Copyright 2013, MetaQuotes Software Corp. |

//|                                             https://www.mql5.com |

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

#property copyright "Copyright 2013, MetaQuotes Software Corp."

#property link      "https://www.mql5.com"

#property version   "1.00"

//--- 启动脚本时显示输入参数的窗口

#property script_show_inputs

//--- 接收程序端数据的参数

input string             InpSymbolName="EURUSD";           // 货币组

input ENUM_TIMEFRAMES    InpSymbolPeriod=PERIOD_H1;        // 时间帧

input int                InpFastEMAPeriod=12;              // 快速 EMA 周期

input int                InpSlowEMAPeriod=26;              // 慢速 EMA 周期

input int                InpSignalPeriod=9;                // 不同平均周期

input ENUM_APPLIED_PRICE InpAppliedPrice=PRICE_CLOSE;      // 价格类型

input datetime           InpDateStart=D'2012.01.01 00:00'; // 复制起始日期的数据

//--- 编写文件数据的参数

input string             InpFileName="MACD.csv";  // 文件名称

input string             InpDirectoryName="Data"; // 目录名称

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

//| 脚本程序启动函数                                                   |

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

void OnStart()

  {

   datetime date_finish; // 复制结束日期的数据

   bool     sign_buff[]; // 信号数组 (true - 买入,false - 卖出)

   datetime time_buff[]; // 信号到达时间数组

   int      sign_size=0; // 信号数组大小

   double   macd_buff[]; // 指标值数组

   datetime date_buff[]; // 指标日期数组

   int      macd_size=0; // 指标数组大小

//--- 结束时间是当前时间

   date_finish=TimeCurrent();

//--- 接收 MACD 指标句柄

   ResetLastError();

   int macd_handle=iMACD(InpSymbolName,InpSymbolPeriod,InpFastEMAPeriod,InpSlowEMAPeriod,InpSignalPeriod,InpAppliedPrice);

   if(macd_handle==INVALID_HANDLE)

     {

      //--- 接收指标句柄失败

      PrintFormat("Error when receiving indicator handle. Error code = %d",GetLastError());

      return;

     }

//--- 循环直至指标计算所有值

   while(BarsCalculated(macd_handle)==-1)

      Sleep(10); // 暂停允许指标计算所有值

//--- 复制某段时间周期的指标值

   ResetLastError();

   if(CopyBuffer(macd_handle,0,InpDateStart,date_finish,macd_buff)==-1)

     {

      PrintFormat("Failed to copy indicator values. Error code = %d",GetLastError());

      return;

     }

//--- 复制相应的指标值时间

   ResetLastError();

   if(CopyTime(InpSymbolName,InpSymbolPeriod,InpDateStart,date_finish,date_buff)==-1)

     {

      PrintFormat("Failed to copy time values. Error code = %d",GetLastError());

      return;

     }

//--- 释放指标占据的内存

   IndicatorRelease(macd_handle);

//--- 接收缓冲区大小

   macd_size=ArraySize(macd_buff);

//--- 分析数据,保存数组的指标信号

   ArrayResize(sign_buff,macd_size-1);

   ArrayResize(time_buff,macd_size-1);

   for(int i=1;i<macd_size;i++)

     {

      //--- 买入信号

      if(macd_buff[i-1]<0 && macd_buff[i]>=0)

        {

         sign_buff[sign_size]=true;

         time_buff[sign_size]=date_buff[i];

         sign_size++;

        }

      //--- 卖出信号

      if(macd_buff[i-1]>0 && macd_buff[i]<=0)

        {

         sign_buff[sign_size]=false;

         time_buff[sign_size]=date_buff[i];

         sign_size++;

        }

     }

//--- 打开编写指标值的文件(如果文件不在,则自动创建)

   ResetLastError();

   int file_handle=FileOpen(InpDirectoryName+"//"+InpFileName,FILE_READ|FILE_WRITE|FILE_CSV);

   if(file_handle!=INVALID_HANDLE)

     {

      PrintFormat("%s file is available for writing",InpFileName);

      PrintFormat("File path: %s\\Files\\",TerminalInfoString(TERMINAL_DATA_PATH));

      //--- 首先,写下信号的数量

      FileWrite(file_handle,sign_size);

      //--- 写下文件的信号时间和信号值

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

         FileWrite(file_handle,time_buff[i],sign_buff[i]);

      //--- 关闭文件

      FileClose(file_handle);

      PrintFormat("Data is written, %s file is closed",InpFileName);

     }

   else

      PrintFormat("Failed to open %s file, Error code = %d",InpFileName,GetLastError());

  }

另见

注释打印字符串格式化