//+------------------------------------------------------------------+
//| Demo_FileWiteStruct.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 script_show_inputs
//--- 터미널에서 데이터를 수신하기 위한 매개 변수
input string InpSymbolName="EURUSD"; // 통화
input ENUM_TIMEFRAMES InpSymbolPeriod=PERIOD_H1; // 타임프레임
input datetime InpDateStart=D'2013.01.01 00:00'; // 데이터 복사 시작 날짜
//--- 파일에 데이터를 쓰기 위한 매개 변수
input string InpFileName="EURUSD.txt"; // 파일명
input string InpDirectoryName="Data"; // 디렉토리명
//+------------------------------------------------------------------+
//| 캔들스틱 데이터 저장 구조 |
//+------------------------------------------------------------------+
struct candlesticks
{
double open; // 시가
double close; // 종가
double high; // 고가
double low; // 저가
datetime date; // 날짜
};
//+------------------------------------------------------------------+
//| 스크립트 프로그램 시작 함수 |
//+------------------------------------------------------------------+
void OnStart()
{
datetime date_finish=TimeCurrent();
int size;
datetime time_buff[];
double open_buff[];
double close_buff[];
double high_buff[];
double low_buff[];
candlesticks cand_buff[];
//--- 에러값 리셋
ResetLastError();
//--- 범위에서 막대의 도착 시간을 수령
if(CopyTime(InpSymbolName,InpSymbolPeriod,InpDateStart,date_finish,time_buff)==-1)
{
PrintFormat("시간 값 복제 실패. Error code = %d",GetLastError());
return;
}
//--- 범위에서 막대의 고가를 수령
if(CopyHigh(InpSymbolName,InpSymbolPeriod,InpDateStart,date_finish,high_buff)==-1)
{
PrintFormat("고가의 값 복제 실패. Error code = %d",GetLastError());
return;
}
//--- 범위로부터 막대의 저가를 수령
if(CopyLow(InpSymbolName,InpSymbolPeriod,InpDateStart,date_finish,low_buff)==-1)
{
PrintFormat("저가의 값 복제 실패. Error code = %d",GetLastError());
return;
}
//--- 범위로부터 막대 시가 수령
if(CopyOpen(InpSymbolName,InpSymbolPeriod,InpDateStart,date_finish,open_buff)==-1)
{
PrintFormat("시가 값 복제 실패. Error code = %d",GetLastError());
return;
}
//--- 범위로부터 막대의 종가 수령
if(CopyClose(InpSymbolName,InpSymbolPeriod,InpDateStart,date_finish,close_buff)==-1)
{
PrintFormat("종가의 값 복제 실패. Error code = %d",GetLastError());
return;
}
//--- 배열 차원 정의
size=ArraySize(time_buff);
//--- 구조 배열에 모든 데이터 저장
ArrayResize(cand_buff,size);
for(int i=0;i<size;i++)
{
cand_buff[i].open=open_buff[i];
cand_buff[i].close=close_buff[i];
cand_buff[i].high=high_buff[i];
cand_buff[i].low=low_buff[i];
cand_buff[i].date=time_buff[i];
}
//--- 구조 어레이를 파일에 쓰기 위해 파일을 엽니다(파일이 없는 경우 자동으로 생성됨)
ResetLastError();
int file_handle=FileOpen(InpDirectoryName+"//"+InpFileName,FILE_READ|FILE_WRITE|FILE_BIN|FILE_COMMON);
if(file_handle!=INVALID_HANDLE)
{
PrintFormat("%s 파일이 쓰기를 위해 열렸습니다",InpFileName);
PrintFormat("파일 경로: %s\\Files\\",TerminalInfoString(TERMINAL_COMMONDATA_PATH));
//--- 바이트 수 카운터를 준비
uint counter=0;
//--- 루프에 어레이 값 쓰기
for(int i=0;i<size;i++)
counter+=FileWriteStruct(file_handle,cand_buff[i]);
PrintFormat("%d 바이트의 정보가 %s 파일에 기록됩니다",InpFileName,counter);
PrintFormat("총 바이트 수: %d * %d * %d = %d, %s",size,5,8,size*5*8,size*5*8==counter ? "Correct" : "Error");
//--- 파일 닫기
FileClose(file_handle);
PrintFormat("데이터가 작성됐습니다, %s 파일이 닫힙니다",InpFileName);
}
else
PrintFormat("%s 파일 열기 실패, 에러 코드 = %d",InpFileName,GetLastError());
}
|