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