FileReadDatetime

CSV 形式のファイルから、「YYYY.MM.DD HH:MM:SS」、「YYYY.MM.DD」または「HH:MM:SS」フォーマットの文字列を読み込みdatetime 型の値に変換します。

datetime  FileReadDatetime(
  int  file_handle    // ファイルハンドル
  );

パラメータ

file_handle

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

戻り値

datetime 型の値

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

//+------------------------------------------------------------------+
//|                                        Demo_FileReadDateTime.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_chart_window
#property indicator_buffers 2
#property indicator_plots   2
//---- Label1 をプロットする
#property indicator_label1 "UpSignal"
#property indicator_type1   DRAW_ARROW
#property indicator_color1 clrRed
#property indicator_style1 STYLE_SOLID
#property indicator_width1  4
//---- Label2 をプロットする
#property indicator_label2 "DownSignal"
#property indicator_type2   DRAW_ARROW
#property indicator_color2 clrRed
#property indicator_style2 STYLE_SOLID
#property indicator_width2  4
//--- データ読み込みのパラメータ
input string InpFileName="MACD.csv"; // ファイル名
input string InpDirectoryName="Data"; // ディレクトリ名
//--- グローバル変数
int      ind=0;       // インデックス
double   upbuff[];   // 上向き矢印の指標バッファ
double   downbuff[]; // 下向き矢印の指標バッファ
bool     sign_buff[]; // シグナル配列(true - 買、false - 売)
datetime time_buff[]; // シグナル到着時の配列
int      size=0;     // シグナル配列のサイズ
//+------------------------------------------------------------------+
//| カスタム指標を初期化する関数                                            |
//+------------------------------------------------------------------+
int OnInit()
 {
//--- ファイルを開く
  ResetLastError();
  int file_handle=FileOpen(InpDirectoryName+"//"+InpFileName,FILE_READ|FILE_CSV);
  if(file_handle!=INVALID_HANDLE)
    {
    PrintFormat("%s file is open for reading",InpFileName);
    //--- 初めにシグナルの数を読む
     size=(int)FileReadNumber(file_handle);
    //--- 配列へメモリを割り当てる
    ArrayResize(sign_buff,size);
    ArrayResize(time_buff,size);
    //--- ファイルからデータを読む
    for(int i=0;i<size;i++)
       {
        //--- 時間シグナル
        time_buff[i]=FileReadDatetime(file_handle);
        //--- 値シグナル
        sign_buff[i]=FileReadBool(file_handle);
       }
    //--- ファイルを閉じる
    FileClose(file_handle);
    }
  else
    {
    PrintFormat("Failed to open %s file, Error code = %d",InpFileName,GetLastError());
    return(INIT_FAILED);
    }
//--- 配列の結合
  SetIndexBuffer(0,upbuff,INDICATOR_DATA);
  SetIndexBuffer(1,downbuff,INDICATOR_DATA);
//--- PLOT_ARROW での描画のためにシンボルコードを設定
  PlotIndexSetInteger(0,PLOT_ARROW,241);
  PlotIndexSetInteger(1,PLOT_ARROW,242);
//---- チャートでは表示されない指標値を設定
  PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0);
  PlotIndexSetDouble(1,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);
  ArraySetAsSeries(low,false);
  ArraySetAsSeries(high,false);
//--- まだ処理されてないバーのループ
  for(int i=prev_calculated;i<rates_total;i++)
    {
    //--- デフォルトでは 0
     upbuff[i]=0;
     downbuff[i]=0;
    //--- まだデータがあるかをチェック
    if(ind<size)
       {
        for(int j=ind;j<size;j++)
          {
          //--- 日付が同じならファイルの値を使用する
          if(time[i]==time_buff[j])
             {
              //--- シグナルに合わせて矢印を描画する
              if(sign_buff[j])
                 upbuff[i]=high[i];
              else
                 downbuff[i]=low[i];
              //--- カウンタを増加する
              ind=j+1;
              break;
             }
          }
       }
    }
//--- 次の呼び出しのために prev_calculated の値を返す
  return(rates_total);
 }

参照

日付時刻型StringToTimeTimeToStringFileWrite