개요
mql5 에서 캔들 데이터들 활용하기 위해서는 함수 CopyRates 호출하면서 마지막 인자에 구조체 MqlRates 배열 전달해주면 MqlRates 로 캔들 데이터 얻을 수 있다.
MqlRates
struct MqlRates
{
datetime time; // Period start time
double open; // Open price
double high; // The highest price of the period
double low; // The lowest price of the period
double close; // Close price
long tick_volume; // Tick volume
int spread; // Spread
long real_volume; // Trade volume
};
// Code Example
void OnStart()
{
MqlRates rates[];
int copied=CopyRates(NULL,0,0,100,rates);
if(copied<=0)
Print("Error copying price data ",GetLastError());
else Print("Copied ",ArraySize(rates)," bars");
}
/// 현재 캔들의 시각 구하는 함수 예.
datetime Get_TimeCrntCandle(string symbol, ENUM_TIMEFRAMES timeframe)
{
MqlRates rates[];
ArraySetAsSeries(rates,true);
int copied = CopyRates(symbol,timeframe,0,2,rates);
return rates[0].time;
}
구조체 : MqlRates
CopyRates
- 지정한 심볼, 주기, 수량/시구간 의 구조체 MqlRates 를 배열로 확보하는 함수.
- 복사해올 대상 History data array (아래그림) 는 인덱스 0이 최신, 인덱스 증가할 수록 과거. 인덱스0의 데이터는 현재시점의 바(=캔들, 봉)가 완성 상태가 아닌 것이며, 해당 바의 close 타임이 되면 종가가 확정되면서 인덱스 1에 배치되고 인덱스 0에는 현재 시점의 신규 바의 정보 고가, 저가, 거래량 정보가 실시간 갱신된다.
- 바의 과거 데이터 요청시 완성된 캔들만 받으려면 인덱스 0은 제외하고 start_pos 에 1 기록하여 요청한다.
- 현재시점의 미완성 캔들 1개의 정보만 필요한 경우엔 함수인자에 start_pos=0 , count=1로 하여 호출한다.
- 복사한 구조체 MqlRates 배열 인덱스 0 이 과거, 인덱스 증가 할수록 최신.
// Call by the first position and the number of required elements
int CopyRates(
string symbol_name, // symbol name
ENUM_TIMEFRAMES timeframe, // period
int start_pos, // History Array Data 의 start position. 0이 최신.
int count, // data count to copy
MqlRates rates_array[] // target array to copy
);
//Call by the start date and the number of required elements
int CopyRates(
string symbol_name, // symbol name
ENUM_TIMEFRAMES timeframe, // period
datetime start_time, // start date and time
int count, // data count to copy
MqlRates rates_array[] // target array to copy
);
// Call by the start and end dates of a required time interval
int CopyRates(
string symbol_name, // symbol name
ENUM_TIMEFRAMES timeframe, // period
datetime start_time, // start date and time
datetime stop_time, // end date and time
MqlRates rates_array[] // target array to copy
);
// example
void OnStart()
{
//---
MqlRates rates[];
ArraySetAsSeries(rates,true);
int copied=CopyRates(Symbol(),0,0,100,rates);
if(copied>0)
{
Print("Bars copied: "+copied);
string format="open = %G, high = %G, low = %G, close = %G, volume = %d";
string out;
int size=fmin(copied,10);
for(int i=0;i<size;i++)
{
out=i+":"+TimeToString(rates[i].time);
out=out+" "+StringFormat(format,
rates[i].open,
rates[i].high,
rates[i].low,
rates[i].close,
rates[i].tick_volume);
Print(out);
}
}
else Print("Failed to get history data for the symbol ",Symbol());
}
Parameters
symbol_name
[in] Symbol name.
timeframe
[in] Period.
start_time
[in] Bar time for the first element to copy.
start_pos
[in] The start position for the first element to copy. 앞의 그림에서 History data array 에서의 인덱스 . 0이 최신.
count
[in] Data count to copy.
stop_time
[in] Bar time, corresponding to the last element to copy.
rates_array[]
[out] Array of MqlRates type.
Return Value
Returns the number of copied elements or -1 in case of an error.
Note
If the whole interval of requested data is out of the available data on the server, the function returns -1. If data outside TERMINAL_MAXBARS (maximal number of bars on the chart) is requested, the function will also return -1.
When requesting data from the indicator, if requested timeseries are not yet built or they need to be downloaded from the server, the function will immediately return -1, but the process of downloading/building will be initiated.
When requesting data from an Expert Advisor or script, downloading from the server will be initiated, if the terminal does not have these data locally, or building of a required timeseries will start, if data can be built from the local history but they are not ready yet. The function will return the amount of data that will be ready by the moment of timeout expiration, but history downloading will continue, and at the next similar request the function will return more data.
When requesting data by the start date and the number of required elements, only data whose date is less than (earlier) or equal to the date specified will be returned. It means, the open time of any bar, for which value is returned (volume, spread, value on the indicator buffer, prices Open, High, Low, Close or open time Time) is always less or equal to the specified one.
When requesting data in a specified range of dates, only data from this interval will be returned. The interval is set and counted up to seconds. It means, the open time of any bar, for which value is returned (volume, spread, value on the indicator buffer, prices Open, High, Low, Close or open time Time) is always within the requested interval.
Thus, if the current day is Saturday, at the attempt to copy data on a week timeframe specifying start_time=Last_Tuesday and stop_time=Last_Friday the function will return 0, because the open time on a week timeframe is always Sunday, but one week bar does not fall into the specified interval.
If you need to return value corresponding to the current uncompleted bar, you can use the first form of call specifying start_pos=0 and count=1.
함수 : CopyRates
연관
첫등록 : 2019년 4월 21일
최종수정 : 2024.08.27
본 글 단축주소 : https://igotit.tistory.com/2123
'트레이딩 > 메타트레이더 코딩' 카테고리의 다른 글
MQL5. MqlTick. CopyTicks. 틱 데이터. (9) | 2020.10.15 |
---|---|
MQL5. CopyTime. Open.High.Low.Close.TickVolume.RealVolume.Spread. (0) | 2020.10.12 |
MQL5. Access to Timeseries and Indicator Data (0) | 2020.10.11 |
MQL5. AccountInfoString. 메타 연결된 서버 정보. (0) | 2020.10.10 |
MQL5. Order, Deal, Position (0) | 2019.06.23 |
댓글