FileReadFloat

バイナリファイルの現在位置から単精度浮動小数点数(float)を読み込みます。

float  FileReadFloat(
  int  file_handle    // ファイルハンドル
  );

パラメータ

file_handle

[in] FileOpen() から戻されたファイル記述子

戻り値

float 型の値

注意事項

エラーの詳細については GetLastError()を呼びます。

FileWriteFloat 関数の例の実行によって取得されたファイルが使用されます)

//+------------------------------------------------------------------+
//|                                           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"; // file name
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);
 }

参照

浮動小数点数型(ダブル、フロート)FileReadDoubleFileWriteFloat