//+------------------------------------------------------------------+
//| Demo_FileWriteDouble.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="EURJPY"; // 통화
input ENUM_TIMEFRAMES InpSymbolPeriod=PERIOD_M15; // 타임프레임
input int InpMAPeriod=10; // smoothing period
input int InpMAShift=0; // 지표 시프트
input ENUM_MA_METHOD InpMAMethod=MODE_SMA; // smoothing type
input ENUM_APPLIED_PRICE InpAppliedPrice=PRICE_CLOSE; // 가격 유형
input datetime InpDateStart=D'2013.01.01 00:00'; // 데이터 복사 시작 날짜
//--- 파일에 데이터를 쓰기 위한 매개 변수
input string InpFileName="MA.csv"; // 파일명
input string InpDirectoryName="Data"; // 디렉토리명
//+------------------------------------------------------------------+
//| 스크립트 프로그램 시작 함수 |
//+------------------------------------------------------------------+
void OnStart()
{
datetime date_finish=TimeCurrent();
double ma_buff[];
datetime time_buff[];
int size;
//--- MA 지표 핸들 수신
ResetLastError();
int ma_handle=iMA(InpSymbolName,InpSymbolPeriod,InpMAPeriod,InpMAShift,InpMAMethod,InpAppliedPrice);
if(ma_handle==INVALID_HANDLE)
{
//--- 지표 핸들을 수신하지 못했습니다
PrintFormat("지표 핸들 수신 시 오류 발생. Error code = %d",GetLastError());
return;
}
//--- 지표가 모든 값을 계산할 때까지 루프에 있음
while(BarsCalculated(ma_handle)==-1)
Sleep(20); // 지표가 모든 값을 계산할 수 있도록 잠시 정지
PrintFormat("%s에서 시작하는 지표 값이 파일에 기록됩니다",TimeToString(InpDateStart));
//--- 지표 값 복제
ResetLastError();
if(CopyBuffer(ma_handle,0,InpDateStart,date_finish,ma_buff)==-1)
{
PrintFormat("지표 값을 복사하지 못했습니다. Error code = %d",GetLastError());
return;
}
//--- 적절한 막대 도착 시간을 복제
ResetLastError();
if(CopyTime(InpSymbolName,InpSymbolPeriod,InpDateStart,date_finish,time_buff)==-1)
{
PrintFormat("시간 값 복제 실패. Error code = %d",GetLastError());
return;
}
//--- 버퍼 사이즈 수령
size=ArraySize(ma_buff);
//--- 지표의 메모리를 비움
IndicatorRelease(ma_handle);
//--- 지표 값을 쓰기 위해 파일을 엽니다(파일이 없을 경우 자동으로 생성됨)
ResetLastError();
int file_handle=FileOpen(InpDirectoryName+"//"+InpFileName,FILE_READ|FILE_WRITE|FILE_BIN);
if(file_handle!=INVALID_HANDLE)
{
PrintFormat("%s 파일을 쓸 수 있습니다",InpFileName);
PrintFormat("파일 경로: %s\\Files\\",TerminalInfoString(TERMINAL_DATA_PATH));
//--- 첫째, 데이터 샘플의 크기를 씁니다
FileWriteDouble(file_handle,(double)size);
//--- 파일에 시간과 값을 기록
for(int i=0;i<size;i++)
{
FileWriteDouble(file_handle,(double)time_buff[i]);
FileWriteDouble(file_handle,ma_buff[i]);
}
//--- 파일 닫기
FileClose(file_handle);
PrintFormat("데이터가 작성됐습니다, %s 파일이 닫힙니다",InpFileName);
}
else
PrintFormat("%s 파일 열기 실패, 에러 코드 = %d",InpFileName,GetLastError());
}
|