//+------------------------------------------------------------------+
//| Demo_FileWriteFloat.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_M15; // 时间帧
input datetime InpDateStart=D'2013.01.01 00:00'; // 复制起始日期的数据
//--- 编写文件数据的参数
input string InpFileName="Close.bin"; // 文件名称
input string InpDirectoryName="Data"; // 目录名称
//+------------------------------------------------------------------+
//| 脚本程序启动函数 |
//+------------------------------------------------------------------+
void OnStart()
{
datetime date_finish=TimeCurrent();
double close_buff[];
datetime time_buff[];
int size;
//--- 重置错误值
ResetLastError();
//--- 复制每个柱的收盘价
if(CopyClose(InpSymbolName,InpSymbolPeriod,InpDateStart,date_finish,close_buff)==-1)
{
PrintFormat("Failed to copy close price values. Error code = %d",GetLastError());
return;
}
//--- 复制每个柱形的时间
if(CopyTime(InpSymbolName,InpSymbolPeriod,InpDateStart,date_finish,time_buff)==-1)
{
PrintFormat("Failed to copy the time values. Error code = %d",GetLastError());
return;
}
//--- 接收缓冲区大小
size=ArraySize(close_buff);
//--- 打开编写指标值的文件(如果文件不在,则自动创建)
ResetLastError();
int file_handle=FileOpen(InpDirectoryName+"//"+InpFileName,FILE_READ|FILE_WRITE|FILE_BIN);
if(file_handle!=INVALID_HANDLE)
{
PrintFormat("%s file is open for writing",InpFileName);
PrintFormat("File path: %s\\Files\\",TerminalInfoString(TERMINAL_DATA_PATH));
//--- 写下文件的收盘价时间和值
for(int i=0;i<size;i++)
{
FileWriteDouble(file_handle,(double)time_buff[i]);
FileWriteFloat(file_handle,(float)close_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());
}
|