//+------------------------------------------------------------------+
//| Demo_FileWriteDouble.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="EURJPY"; // 货币组
input ENUM_TIMEFRAMES InpSymbolPeriod=PERIOD_M15; // 时间帧
input int InpMAPeriod=10; // 平滑期
input int InpMAShift=0; // 指标变化
input ENUM_MA_METHOD InpMAMethod=MODE_SMA; // 平滑类型
input ENUM_APPLIED_PRICE InpAppliedPrice=PRICE_CLOSE; // 价格类型
input datetime InpDateStart=D'2013.01.01 00:00'; // 复制起始日期的数据
//--- 编写文件数据的参数
input string InpFileName="MA.csv"; // 文件名称
input string InpDirectoryName="Data"; // 目录名称
//+------------------------------------------------------------------+
//| 脚本程序启动函数 |
//+------------------------------------------------------------------+
void OnStart()
{
datetime date_finish=TimeCurrent();
double ma_buff[];
datetime time_buff[];
int size;
//--- 接收 MA 指标句柄
ResetLastError();
int ma_handle=iMA(InpSymbolName,InpSymbolPeriod,InpMAPeriod,InpMAShift,InpMAMethod,InpAppliedPrice);
if(ma_handle==INVALID_HANDLE)
{
//--- 接收指标句柄失败
PrintFormat("Error when receiving indicator handle. Error code = %d",GetLastError());
return;
}
//--- 循环直至指标计算所有值
while(BarsCalculated(ma_handle)==-1)
Sleep(20); // 暂停允许指标计算所有值
PrintFormat("Indicator values starting from %s will be written to the file",TimeToString(InpDateStart));
//--- 复制指标值
ResetLastError();
if(CopyBuffer(ma_handle,0,InpDateStart,date_finish,ma_buff)==-1)
{
PrintFormat("Failed to copy the indicator values. Error code = %d",GetLastError());
return;
}
//--- 复制相应的柱形到达时间
ResetLastError();
if(CopyTime(InpSymbolName,InpSymbolPeriod,InpDateStart,date_finish,time_buff)==-1)
{
PrintFormat("Failed to copy time values. Error code = %d",GetLastError());
return;
}
//--- 接收缓冲区大小
size=ArraySize(ma_buff);
//--- 释放指标占据的内存
IndicatorRelease(ma_handle);
//--- 打开编写指标值的文件(如果文件不在,则自动创建)
ResetLastError();
int file_handle=FileOpen(InpDirectoryName+"//"+InpFileName,FILE_READ|FILE_WRITE|FILE_BIN);
if(file_handle!=INVALID_HANDLE)
{
PrintFormat("%s file is available for writing",InpFileName);
PrintFormat("File path: %s\\Files\\",TerminalInfoString(TERMINAL_DATA_PATH));
//--- 首先,写下数据样本的大小
FileWriteDouble(file_handle,(double)size);
//--- 写下文件的指标时间和值
for(int i=0;i<size;i++)
{
FileWriteDouble(file_handle,(double)time_buff[i]);
FileWriteDouble(file_handle,ma_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());
}
|