FileReadDouble
FileReadDouble
读取二进制文件当前位置上的双精度浮点数(double)。
double FileReadDouble(
int file_handle, // File handle
int size=DOUBLE_VALUE // Size
);参数
- file_handle
[in] 由FileOpen()返回的文件描述符。
- size=DOUBLE_VALUE
需要读取的字节数(最多包含8个),相应的常量如下:DOUBLE_VALUE = 8, FLOAT_VALUE = 4,因此该函数可以读取双精度或浮点类型的全部值。
返回值
双精度类型的值。
注意
关于错误的更多详细信息,请调用GetLastError()。
示例(此处使用了执行FileWriteDouble()函数后获得的文件)
//+------------------------------------------------------------------+
//| Demo_FileReadDouble.mq4 |
//| Copyright 2014, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
#property indicator_chart_window
#property indicator_buffers 1
//---- plot Label1
#property indicator_label1 "MA"
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrGreen
#property indicator_style1 STYLE_SOLID
#property indicator_width1 1
#property indicator_separate_window
//--- data reading parameters
input string InpFileName="MA.bin"; // File name
input string InpDirectoryName="Data"; // Folder name
//--- global variables
int ind=0;
int size=0;
double ma_buff[];
datetime time_buff[];
//--- indicator buffer
double buff[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- open the file
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));
//--- first, read the amount of data in the file
size=(int)FileReadDouble(file_handle,DOUBLE_VALUE);
//--- allocate memory for the arrays
ArrayResize(ma_buff,size);
ArrayResize(time_buff,size);
//--- read data from the file
for(int i=0;i<size;i++)
{
time_buff[i]=(datetime)FileReadDouble(file_handle,DOUBLE_VALUE);
ma_buff[i]=FileReadDouble(file_handle,DOUBLE_VALUE);
}
//--- close the file
FileClose(file_handle);
PrintFormat("Data is written, %s file is closed",InpFileName);
}
else
{
PrintFormat("Failed to open %s file, Error code = %d",InpFileName,GetLastError());
return(INIT_FAILED);
}
//--- bind the array to the indicator buffer with index 0
SetIndexBuffer(0,buff,INDICATOR_DATA);
//---- set the indicator values that will not be visible on the chart
SetIndexEmptyValue(0,0.0);
//--- set indexing as timeseries
ArraySetAsSeries(time_buff,true);
ArraySetAsSeries(ma_buff,true);
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
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,true);
//--- the loop for the bars that have not been handled yet
for(int i=prev_calculated;i<rates_total;i++)
{
//--- 0 by default
buff[i]=0;
for(int j=0;j<size;j++)
{
//--- if the dates coincide, the value from the file is used
if(time[i]==time_buff[j])
{
buff[i]=ma_buff[j];
}
}
}
//--- return value of prev_calculated for next call
return(rates_total);
}另请参阅
实数类型(double, float),StringToDouble(),DoubleToString(),FileWriteDouble()
最后更新于