//+------------------------------------------------------------------+ //| Demo_FileReadStruct.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 4 #property indicator_plots 1 //---- Label1 をプロットする #property indicator_label1 "Candles" #property indicator_type1 DRAW_CANDLES #property indicator_color1 clrOrange #property indicator_style1 STYLE_SOLID #property indicator_width1 1 #property indicator_separate_window //--- データ受信のパラメータ input string InpFileName="EURUSD.txt"; // ファイル名 input string InpDirectoryName="Data"; // ディレクトリ名 //+------------------------------------------------------------------+ //| ローソク足データを格納する構造体 | //+------------------------------------------------------------------+ struct candlesticks { double open; // 始値 double close; // 終値 double high; // 高値 double low; // 安値 datetime date; // 日付 }; //--- 指標バッファ double open_buff[]; double close_buff[]; double high_buff[]; double low_buff[]; //--- グローバル変数 candlesticks cand_buff[]; int size=0; int ind=0; //+------------------------------------------------------------------+ //| カスタム指標を初期化する関数 | //+------------------------------------------------------------------+ int OnInit() { int default_size=100; ArrayResize(cand_buff,default_size); //--- ファイルを開く ResetLastError(); int file_handle=FileOpen(InpDirectoryName+"//"+InpFileName,FILE_READ|FILE_BIN|FILE_COMMON); if(file_handle!=INVALID_HANDLE) { PrintFormat("%s file is available for reading",InpFileName); PrintFormat("File path: %s\\Files\\",TerminalInfoString(TERMINAL_COMMONDATA_PATH)); //--- ファイルからデータを読む while(!FileIsEnding(file_handle)) { //--- データを配列に書く FileReadStruct(file_handle,cand_buff[size]); size++; //--- 配列がオーバーフローしているかチェックする if(size==default_size) { //--- 配列サイズを増やす default_size+=100; ArrayResize(cand_buff,default_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,open_buff,INDICATOR_DATA); SetIndexBuffer(1,high_buff,INDICATOR_DATA); SetIndexBuffer(2,low_buff,INDICATOR_DATA); SetIndexBuffer(3,close_buff,INDICATOR_DATA); //--- 空の値 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 open_buff[i]=0; close_buff[i]=0; high_buff[i]=0; low_buff[i]=0; //--- まだデータがあるかをチェック if(ind<size) { for(int j=ind;j<size;j++) { //--- 日付が同じならファイルの値を使用する if(time[i]==cand_buff[j].date) { open_buff[i]=cand_buff[j].open; close_buff[i]=cand_buff[j].close; high_buff[i]=cand_buff[j].high; low_buff[i]=cand_buff[j].low; //--- カウンタを増加する ind=j+1; break; } } } } //--- 次の呼び出しのために prev_calculated の値を返す return(rates_total); } |