FileReadNumber

이 함수는 CSV 파일에서 현재 위치에서 구분 기호(또는 텍스트 문자열 끝까지)까지 문자열을 읽고 읽기 문자열을 double 유형의 값으로 변환합니다.

double  FileReadNumber(
   int  file_handle    // 파일 핸들
   );

Parameters

file_handle

[in] FileOpen()에서 반환된 파일 설명자.

반환 값

double 타입의 값.

(FileWriteString 함수에 대한 예제를 실행하는 동안 얻은 파일이 여기에 사용됨)

//+------------------------------------------------------------------+
//|                                          Demo_FileReadNumber.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
//---- plot Label1
#property indicator_label1  "Overbought & Oversold"
#property indicator_type1   DRAW_COLOR_BARS
#property indicator_color1  clrRedclrBlue
#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 파일을 읽을 수 있습니다",InpFileName);
      PrintFormat("파일 경로: %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("데이터가 작성됐습니다, %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);
   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))
     {
      //--- 번호를 읽은 후 carriage 시프트
      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);
  }

더 보기

FileWriteString