//+------------------------------------------------------------------+
//| 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
//---- plot 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"; // 파일명
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 파일을 읽을 수 있습니다",InpFileName);
PrintFormat("파일 경로: %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("데이터를 읽습니다, %s 파일이 닫힙니다",InpFileName);
}
else
{
PrintFormat("%s 파일 열기 실패, 에러 코드 = %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);
}
|