//+------------------------------------------------------------------+
//| Demo_FileWriteArray.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"
//--- 입력 매개 변수
input string InpFileName="data.bin";
input string InpDirectoryName="SomeFolder";
//+------------------------------------------------------------------+
//| 가격 데이터 저장 구조 |
//+------------------------------------------------------------------+
struct prices
{
datetime date; // 날짜
double bid; // 입찰가
double ask; // 요청가
};
//--- 글로벌 변수
int count=0;
int size=20;
string path=InpDirectoryName+"//"+InpFileName;
prices arr[];
//+------------------------------------------------------------------+
//| Expert 초기화 함수 |
//+------------------------------------------------------------------+
int OnInit()
{
//--- 배열용 메모리 할당
ArrayResize(arr,size);
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert 초기화 해제 함수 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//--- 카운트가 n보다 작을 경우 남은 카운트를 씁니다
WriteData(count);
}
//+------------------------------------------------------------------+
//| Expert 틱 함수 |
//+------------------------------------------------------------------+
void OnTick()
{
//--- 배열에 데이터 저장
arr[count].date=TimeCurrent();
arr[count].bid=SymbolInfoDouble(Symbol(),SYMBOL_BID);
arr[count].ask=SymbolInfoDouble(Symbol(),SYMBOL_ASK);
//--- 현재 데이터를 보여줌
Print("Date = ",arr[count].date," Bid = ",arr[count].bid," Ask = ",arr[count].ask);
//--- 카운터 증가
count++;
//--- 배열이 가득 찬 경우 파일에 데이터를 기록한 후 0으로 설정합니다
if(count==size)
{
WriteData(size);
count=0;
}
}
//+------------------------------------------------------------------+
//| 파일에 배열의 요소 기록 |
//+------------------------------------------------------------------+
void WriteData(const int n)
{
//--- 파일 열기
ResetLastError();
int handle=FileOpen(path,FILE_READ|FILE_WRITE|FILE_BIN);
if(handle!=INVALID_HANDLE)
{
//--- 파일 끝에 어레이 데이터를 씁니다
FileSeek(handle,0,SEEK_END);
FileWriteArray(handle,arr,0,n);
//--- 파일 닫기
FileClose(handle);
}
else
Print("파일 오픈 실패, 오류 ",GetLastError());
}
|