//+------------------------------------------------------------------+
//| Demo_FileReadInteger.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 1
#property indicator_plots 1
//---- Label1 をプロットする
#property indicator_label1 "Trends"
#property indicator_type1 DRAW_SECTION
#property indicator_color1 clrRed
#property indicator_style1 STYLE_SOLID
#property indicator_width1 1
//--- データ読み込みのパラメータ
input string InpFileName="Trend.bin"; // ファイル名
input string InpDirectoryName="Data"; // ディレクトリ名
//--- グローバル変数
int ind=0;
int size=0;
datetime time_buff[];
//--- 指標バッファ
double buff[];
//+------------------------------------------------------------------+
//| カスタム指標を初期化する関数 |
//+------------------------------------------------------------------+
int OnInit()
{
int def_size=100;
//--- 配列へのメモリ追加
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));
//--- 追加の変数
int arr_size;
uchar arr[];
//--- ファイルからデータを読む
while(!FileIsEnding(file_handle))
{
//--- 時間を書くのに使用されるシンボルの数を見つける
arr_size=FileReadInteger(file_handle,INT_VALUE);
ArrayResize(arr,arr_size);
for(int i=0;i<arr_size;i++)
arr[i]=(char)FileReadInteger(file_handle,CHAR_VALUE);
//--- 時間値を格納する
time_buff[size]=StringToTime(CharArrayToString(arr));
size++;
//--- オーバーフローしている場合は、配列のサイズを増やす
if(size==def_size)
{
def_size+=100;
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);
//---- チャートでは表示されない指標値を設定する
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);
ArraySetAsSeries(close,false);
//--- まだ処理されてないバーのループ
for(int i=prev_calculated;i<rates_total;i++)
{
//--- デフォルトでは 0
buff[i]=0;
//--- まだデータがあるかをチェック
if(ind<size)
{
for(int j=ind;j<size;j++)
{
//--- 日付が同じならファイルの値を使用する
if(time[i]==time_buff[j])
{
//--- 価格を受け取る
buff[i]=close[i];
//--- カウンタを増加する
ind=j+1;
break;
}
}
}
}
//--- 次の呼び出しのために prev_calculated の値を返す
return(rates_total);
}
|