//+------------------------------------------------------------------+
//| Demo_FileReadFloat.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 indicator_separate_window
#property indicator_buffers 2
#property indicator_plots 1
//---- 图 Label1
#property indicator_label1 "CloseLine"
#property indicator_type1 DRAW_COLOR_LINE
#property indicator_color1 clrRed,clrBlue
#property indicator_style1 STYLE_SOLID
#property indicator_width1 2
//--- 读取数据的参数
input string InpFileName="Close.bin"; // 文件名称
input string InpDirectoryName="Data"; // 目录名称
//--- 全局变量
int ind=0;
int size=0;
double close_buff[];
datetime time_buff[];
//--- 指标缓冲区
double buff[];
double color_buff[];
//+------------------------------------------------------------------+
//| 自定义指标初始化函数 |
//+------------------------------------------------------------------+
int OnInit()
{
int def_size=100;
//--- 为数组分配内存
ArrayResize(close_buff,def_size);
ArrayResize(time_buff,def_size);
//--- 打开文件
ResetLastError();
int file_handle=FileOpen(InpDirectoryName+"//"+InpFileName,FILE_READ|FILE_BIN);
if(file_handle!=INVALID_HANDLE)
{
PrintFormat("%s file is available for reading",InpFileName);
PrintFormat("File path: %s\\Files\\",TerminalInfoString(TERMINAL_DATA_PATH));
//--- 读取文件数据
while(!FileIsEnding(file_handle))
{
//--- 阅读时间和价格的值
time_buff[size]=(datetime)FileReadDouble(file_handle);
close_buff[size]=(double)FileReadFloat(file_handle);
size++;
//--- 如果超过增加数组大小
if(size==def_size)
{
def_size+=100;
ArrayResize(close_buff,def_size);
ArrayResize(time_buff,def_size);
}
}
//--- 关闭文件
FileClose(file_handle);
PrintFormat("Data is read, %s file is closed",InpFileName);
}
else
{
PrintFormat("Failed to open %s file, Error code = %d",InpFileName,GetLastError());
return(INIT_FAILED);
}
//--- 绑定数组和指标缓冲区
SetIndexBuffer(0,buff,INDICATOR_DATA);
SetIndexBuffer(1,color_buff,INDICATOR_COLOR_INDEX);
//---- 设置图表上看不到的指标值
PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0);
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| 自定义指标迭代函数 |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
const int prev_calculated,
const datetime &time[],
const double &open[],
const double &high[],
const double &low[],
const double &close[],
const long &tick_volume[],
const long &volume[],
const int &spread[])
{
ArraySetAsSeries(time,false);
//--- 还未处理的柱的循环
for(int i=prev_calculated;i<rates_total;i++)
{
//--- 默认为0
buff[i]=0;
color_buff[i]=0; // 默认为红色
//--- 检查任何日期是否仍然存在
if(ind<size)
{
for(int j=ind;j<size;j++)
{
//--- 如果日期一致,使用文件的值
if(time[i]==time_buff[j])
{
//--- 接收价格
buff[i]=close_buff[j];
//--- 如果当前价格超过之前的价格,为蓝色
if(buff[i-1]>buff[i])
color_buff[i]=1;
//--- 增加计数器
ind=j+1;
break;
}
}
}
}
//--- 返回prev_calculated 值,以便下次调用
return(rates_total);
}
|