exchange.GetRecords
Get the Record structure array (i.e. K-line data) of the spot or contract corresponding to the currently set trading pair or contract code.
exchange.GetRecords()
exchange.GetRecords(symbol)
exchange.GetRecords(symbol, period)
exchange.GetRecords(symbol, period, limit)
exchange.GetRecords(period)
exchange.GetRecords(period, limit)Examples
-
Get K-line data for a custom period.
javascriptfunction main() { // Print K-line data with a K-line period of 120 seconds (2 minutes) Log(exchange.GetRecords(60 * 2)) // Print K-line data with a K-line period of 5 minutes Log(exchange.GetRecords(PERIOD_M5)) }pythondef main(): Log(exchange.GetRecords(60 * 2)) Log(exchange.GetRecords(PERIOD_M5))rustfn main() { // Print K-line data with a K-line period of 120 seconds (2 minutes) Log!(exchange.GetRecords(None, 60 * 2, None)); // Print K-line data with a K-line period of 5 minutes Log!(exchange.GetRecords(None, PERIOD_M5, None)); }c++void main() { Log(exchange.GetRecords(60 * 2)[0]); Log(exchange.GetRecords(PERIOD_M5)[0]); } -
Output K-line bar data:
javascriptfunction main() { var records = exchange.GetRecords(PERIOD_H1) /* Due to network reasons, it may not be possible to access the exchange interface (even if the device running the docker program can open the exchange website, the API interface may still be inaccessible) In this case, records is null, and accessing records[0].Time will cause an error. Therefore, when testing this code, please make sure you can access the exchange interface normally */ Log("First K-line data: Time:", records[0].Time, "Open:", records[0].Open, "High:", records[0].High) Log("Second K-line data: Time:", records[1].Time ,"Close:", records[1].Close) Log("Current K-line (latest)", records[records.length-1], "Previous K-line", records[records.length-2]) }pythondef main(): records = exchange.GetRecords(PERIOD_H1) Log("First K-line data: Time:", records[0]["Time"], "Open:", records[0]["Open"], "High:", records[0]["High"]) Log("Second K-line data: Time:", records[1]["Time"], "Close:", records[1]["Close"]) Log("Current K-line (latest)", records[-1], "Previous K-line", records[-2])rustfn main() { let records = exchange.GetRecords(None, PERIOD_H1, None).unwrap(); Log!("First K-line data: Time:", records[0].Time, "Open:", records[0].Open, "High:", records[0].High); Log!("Second K-line data: Time:", records[1].Time, "Close:", records[1].Close); Log!("Current K-line (latest)", records[records.len() - 1], "Previous K-line", records[records.len() - 2]); }c++void main() { auto records = exchange.GetRecords(PERIOD_H1); Log("First K-line data: Time:", records[0].Time, "Open:", records[0].Open, "High:", records[0].High); Log("Second K-line data: Time:", records[1].Time, "Close:", records[1].Close); Log("Current K-line (latest)", records[records.size() - 1], "Previous K-line", records[records.size() - 2]); } -
When the configured
exchangeobject is a futures exchange object, you can use thesymbol,period, andlimitparameters to request K-line data for a specified instrument (futures instrument).javascriptfunction main() { var records = exchange.GetRecords("BTC_USDT.swap", 60, 100) Log(records) }pythondef main(): records = exchange.GetRecords("BTC_USDT.swap", 60, 100) Log(records)rustfn main() { let records = exchange.GetRecords("BTC_USDT.swap", 60, 100).unwrap(); Log!(records); }c++void main() { auto records = exchange.GetRecords("BTC_USDT.swap", 60, 100); Log(records); }
Returns
| Type | Description |
| The |
Arguments
| Name | Type | Required | Description |
symbol | string | No | The When calling the When calling the When calling the |
period | number | No | The |
limit | number | No | The |
See Also
Remarks
The default K-line period can be set on the backtesting and live trading pages. When calling the exchange.GetRecords() function, if a parameter is specified, it retrieves the K-line data for the period specified by that parameter; if no parameter is specified, it returns the K-line data for the period set in the backtesting or live trading parameters.
The return value is a Record structure array. The returned K-line data accumulates continuously over time, and the upper limit of the accumulated number of K-line bars is affected by the setting of the exchange.SetMaxBarLen() function. When not set, the default upper limit is 5000 K-line bars. Once the K-line data reaches the accumulation limit, each time a new K-line bar is added, the earliest K-line bar is deleted (similar to the first-in-first-out behavior of a queue). Some exchanges do not provide a K-line interface, in which case the docker collects market trade record data (a Trade structure array) in real time to synthesize K-lines.
If the exchange's K-line interface supports paginated queries, when calling the exchange.SetMaxBarLen() function to set a large K-line length, the system will initiate multiple API requests.
When the exchange.GetRecords() function is called for the first time, the number of K-line bars obtained differs between the backtesting and live trading environments:
-
The backtesting system pre-fetches a certain number of K-line bars prior to the start time of the backtesting time range (5000 by default; the relevant settings and data volume of the backtesting system will affect the final returned number) as the initial K-line data.
-
In live trading, the actual number of K-line bars obtained depends on the maximum amount of data that the exchange's K-line interface can provide.
Setting the period parameter to 5 means requesting K-line data with a period of 5 seconds. If the period parameter is not divisible by 60 (i.e., the represented period cannot be expressed in units of minutes), the underlying system will use the relevant interface of exchange.GetTrades() to obtain trade record data in order to synthesize the required K-line data; if the period parameter is divisible by 60, then 1-minute K-line data is used at minimum (using as large a period as possible) to synthesize the required K-line data.
In the simulation-level backtesting of the backtesting system, because the underlying K-line period must be set (during simulation-level backtesting, the system uses the corresponding K-line data to generate Tick data based on the configured underlying K-line period), the following must be noted: the K-line data period obtained in the strategy cannot be smaller than the underlying K-line period. This is because in simulation-level backtesting, the K-line data of each period is synthesized from the K-line data corresponding to the underlying K-line period.
In the C++ language, if you need to construct K-line data yourself, you can refer to the following code example:
c++
#include <sstream>
void main() {
Records r;
r.Valid = true;
for (auto i = 0; i < 10; i++) {
Record ele;
ele.Time = i * 100000;
ele.High = i * 10000;
ele.Low = i * 1000;
ele.Close = i * 100;
ele.Open = i * 10;
ele.Volume = i * 1;
r.push_back(ele);
}
// Output displays: Records[10]
Log(r);
auto ma = TA.MA(r,10);
// Output displays: [nan,nan,nan,nan,nan,nan,nan,nan,nan,450]
Log(ma);
}
Exchanges that do not support the exchange.GetRecords() function:
| Function Name | Unsupported Spot Exchanges | Unsupported Futures Exchanges |
|---|---|---|
| GetRecords | Zaif / Coincheck / BitFlyer | Futures_Aevo |