본문 바로가기
트레이딩/메타트레이더 코딩

MQL5. MqlRates. CopyRates

by i.got.it 2020. 10. 11.

 

 

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

 

Documentation on MQL5: Constants, Enumerations and Structures / Data Structures / History Data Structure

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");   }

www.mql5.com

 

 

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

 

Documentation on MQL5: Timeseries and Indicators Access / CopyRates

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 = %

www.mql5.com

 

 

 

 

연관 

 

 

MQL5. CopyTime. Open.High.Low.Close.TickVolume.RealVolume.Spread.

개요. MQL5 에서 캔들 데이터의 요소(Time, Opon, High, Low, Close, TickVolume, RealVolume, Spread) 개별적으로 배열로 확보하는함수들. 비교. CopyRates 의 경우엔 구조체 MqlRates 의 배열로 확보하는 함수인..

igotit.tistory.com

 

 

MQL5. Access to Timeseries and Indicator Data

개요 - MQL5 에서의 timeseries 형식은 인덱스0이 최신의 것이고, 인덱스 증가할 수록 과거 정보가 기록되어있다. - 틱, 캔들, 인디케이터 타임시리즈 데이터를 배열로 복사하는 함수 제공.  캔들 구��

igotit.tistory.com

 

 

 

MQL5. MqlTick. CopyTicks

구조체 MqlTick struct MqlTick { datetime time; // Time of the last prices update double bid; // Current Bid price double ask; // Current Ask price double last; // Price of the last deal (Last) ulong..

igotit.tistory.com

 

 

 


첫등록 : 2019년 4월 21일

최종수정 : 2020.10.11

 

본 글 단축주소 : https://igotit.tistory.com/2123

 


 

댓글



 

비트코인




암호화폐       외환/나스닥/골드       암호화폐/외환/나스닥/골드 암호화폐/외환/나스닥/골드   암호화폐/외환/나스닥/골드
     
현물 |선물 인버스 |선물 USDT       전략매니저(카피트레이딩)     롤오버 이자 없는 스왑프리계좌
( 스왑프리 암호화폐도 거래 가능 )    
MT4, MT5 , cTrader 모두 지원     FTMO 계좌 매매운용. MT4,MT5