//+------------------------------------------------------------------+
//| Demo_FileIsLineEnding.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 5
#property indicator_plots 1
//---- Label1 をプロットする
#property indicator_label1 "Overbought & Oversold"
#property indicator_type1 DRAW_COLOR_BARS
#property indicator_color1 clrRed, clrBlue
#property indicator_style1 STYLE_SOLID
#property indicator_width1 2
//--- データ読み込みのパラメータ
input string InpFileName="RSI.csv"; // ファイル名
input string InpDirectoryName="Data"; // ディレクトリ名
//--- 指標バッファ
double open_buff[];
double high_buff[];
double low_buff[];
double close_buff[];
double color_buff[];
//--- 買いすぎ変数
int ovb_ind=0;
int ovb_size=0;
datetime ovb_time[];
//--- 売りすぎ変数
int ovs_ind=0;
int ovs_size=0;
datetime ovs_time[];
//+------------------------------------------------------------------+
//| カスタム指標を初期化する関数 |
//+------------------------------------------------------------------+
int OnInit()
{
//--- デフォルトの配列サイズの変数
int ovb_def_size=100;
int ovs_def_size=100;
//--- 変数にメモリを割り当てる
ArrayResize(ovb_time,ovb_def_size);
ArrayResize(ovs_time,ovs_def_size);
//--- ファイルを開く
ResetLastError();
int file_handle=FileOpen(InpDirectoryName+"//"+InpFileName,FILE_READ|FILE_CSV|FILE_ANSI);
if(file_handle!=INVALID_HANDLE)
{
PrintFormat("%s file is available for reading",InpFileName);
PrintFormat("File path: %s\\Files\\",TerminalInfoString(TERMINAL_DATA_PATH));
double value;
//--- ファイルからデータを読む
while(!FileIsEnding(file_handle))
{
//--- 文字列の初めの値を読む
value=FileReadNumber(file_handle);
//--- 関数の結果に応じて異なる配列を読む
if(value>=70)
ReadData(file_handle,ovb_time,ovb_size,ovb_def_size);
else
ReadData(file_handle,ovs_time,ovs_size,ovs_def_size);
}
//--- ファイルを閉じる
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);
}
//--- 配列の結合
SetIndexBuffer(0,open_buff,INDICATOR_DATA);
SetIndexBuffer(1,high_buff,INDICATOR_DATA);
SetIndexBuffer(2,low_buff,INDICATOR_DATA);
SetIndexBuffer(3,close_buff,INDICATOR_DATA);
SetIndexBuffer(4,color_buff,INDICATOR_COLOR_INDEX);
//---- チャートでは表示されない指標値を設定する
PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0);
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| ファイルの文字列データを読む |
//+------------------------------------------------------------------+
void ReadData(const int file_handle,datetime &arr[],int &size,int &def_size)
{
bool flag=false;
//--- 文字列の終わりかファイルの終わりまで読む
while(!FileIsLineEnding(file_handle) && !FileIsEnding(file_handle))
{
//--- 数字を読んで位置をシフトする
if(flag)
FileReadNumber(file_handle);
//--- 現在の日付を格納する
arr[size]=FileReadDatetime(file_handle);
size++;
//--- 必要な場合配列サイズを増やす
if(size==def_size)
{
def_size+=100;
ArrayResize(arr,def_size);
}
//--- 最初の反復をすり抜ける
flag=true;
}
}
//+------------------------------------------------------------------+
//| カスタム指標の反復関数 |
//+------------------------------------------------------------------+
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(open,false);
ArraySetAsSeries(high,false);
ArraySetAsSeries(low,false);
ArraySetAsSeries(close,false);
//--- まだ処理されてないバーのループ
for(int i=prev_calculated;i<rates_total;i++)
{
//--- デフォルトでは 0
open_buff[i]=0;
high_buff[i]=0;
low_buff[i]=0;
close_buff[i]=0;
color_buff[i]=0;
//--- まだデータがあるかをチェック
if(ovb_ind<ovb_size)
for(int j=ovb_ind;j<ovb_size;j++)
{
//--- 日付が一致した場合、バーが買われ過ぎ領域にある
if(time[i]==ovb_time[j])
{
open_buff[i]=open[i];
high_buff[i]=high[i];
low_buff[i]=low[i];
close_buff[i]=close[i];
//--- 0 - 赤
color_buff[i]=0;
//--- カウンタを増加する
ovb_ind=j+1;
break;
}
}
//--- データがまだあるかをチェック
if(ovs_ind<ovs_size)
for(int j=ovs_ind;j<ovs_size;j++)
{
//--- 日付が一致した場合、バーが売られ過ぎ領域にある
if(time[i]==ovs_time[j])
{
open_buff[i]=open[i];
high_buff[i]=high[i];
low_buff[i]=low[i];
close_buff[i]=close[i];
//--- 1 - 青
color_buff[i]=1;
//--- カウンタを増加する
ovs_ind=j+1;
break;
}
}
}
//--- 次の呼び出しのために prev_calculated の値を返す
return(rates_total);
}
//+------------------------------------------------------------------+
//| ChartEvent イベントハンドラ |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
const long &lparam,
const double &dparam,
const string &sparam
)
{
//--- スケールに合わせて指標の幅を変更する
if(ChartGetInteger(0,CHART_SCALE)>3)
PlotIndexSetInteger(0,PLOT_LINE_WIDTH,2);
else
PlotIndexSetInteger(0,PLOT_LINE_WIDTH,1);
}
|