//+------------------------------------------------------------------+
//| Demo_FileWriteInteger.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 datetime InpDateStart=D'2013.01.01 00:00'; // 复制起始日期的数据
//--- 编写文件数据的参数
input string InpFileName="Trend.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 the values of close prices. Error code = %d",GetLastError());
return;
}
//--- 复制每个柱形的时间
if(CopyTime(InpSymbolName,InpSymbolPeriod,InpDateStart,date_finish,time_buff)==-1)
{
PrintFormat("Failed to copy 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 available for writing",InpFileName);
PrintFormat("File path: %s\\Files\\",TerminalInfoString(TERMINAL_DATA_PATH));
//---
int up_down=0; // 趋势标识
int arr_size; // arr 数组大小
uchar arr[]; // uchar 类型数组
//--- 写下文件的时间值
for(int i=0;i<size-1;i++)
{
//--- 比较当前柱和下一柱的收盘价
if(close_buff[i]<=close_buff[i+1])
{
if(up_down!=1)
{
//--- 使用FileWriteInteger写下文件的日期值
StringToCharArray(TimeToString(time_buff[i]),arr);
arr_size=ArraySize(arr);
//--- 首先,写下数组中的交易品种数量
FileWriteInteger(file_handle,arr_size,INT_VALUE);
//--- 写下交易品种
for(int j=0;j<arr_size;j++)
FileWriteInteger(file_handle,arr[j],CHAR_VALUE);
//--- 改变趋势标识
up_down=1;
}
}
else
{
if(up_down!=-1)
{
//--- 使用FileWriteInteger写下文件的日期值
StringToCharArray(TimeToString(time_buff[i]),arr);
arr_size=ArraySize(arr);
//--- 首先,写下数组中的交易品种数量
FileWriteInteger(file_handle,arr_size,INT_VALUE);
//--- 写下交易品种
for(int j=0;j<arr_size;j++)
FileWriteInteger(file_handle,arr[j],CHAR_VALUE);
//--- 改变趋势标识
up_down=-1;
}
}
}
//--- 关闭文件
FileClose(file_handle);
PrintFormat("Data is written, %s file is closed",InpFileName);
}
else
PrintFormat("Failed to open %s file, Error code = %d",InpFileName,GetLastError());
}
|