exchange.GetRecords
Get the Record structure array corresponding to the currently set trading pair or contract code for spot or futures, i.e., K-line data.
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 with custom period.
javascriptfunction main() { // Print K-line data with period of 120 seconds (2 minutes) Log(exchange.GetRecords(60 * 2)) // Print K-line data with period of 5 minutes Log(exchange.GetRecords(PERIOD_M5)) }pythondef main(): Log(exchange.GetRecords(60 * 2)) Log(exchange.GetRecords(PERIOD_M5))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 issues, the exchange API may be inaccessible (even if the host device can open the exchange website, the API interface may still be unavailable) In this case, records will be null, and accessing records[0].Time will cause an error, so please ensure the exchange API is accessible when testing this code */ 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])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, use thesymbol,period, andlimitparameters to request K-line data for a specific 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)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 in the backtest and live trading pages. If a parameter is specified when calling the exchange.GetRecords() function, it will retrieve K-line data corresponding to that parameter's period. If no parameter is specified during the function call, it will return K-line data according to the K-line period set in the backtest or live trading parameters.
The return value is an array of Record structures. The returned K-line data accumulates over time, with the upper limit of accumulated K-line bars affected by the exchange.SetMaxBarLen() function setting. When not set, the default upper limit is 5000 K-line bars. When the K-line data reaches the accumulation limit, adding a new K-line bar will simultaneously delete the earliest K-line bar (like a queue operation). Some exchanges do not provide K-line interfaces, in which case the docker will collect real-time market trade data (Trade structure array) to generate K-lines.
If the exchange's K-line interface supports pagination queries, when calling the exchange.SetMaxBarLen() function to set a larger K-line length, multiple API requests will be made.
When initially calling the exchange.GetRecords() function, the number of K-line bars obtained differs between backtesting and live trading:
-
In the backtest system, a certain number of K-line bars before the start time of the backtest time range will be pre-fetched (default is 5000, the backtest system settings and data volume will affect the final returned quantity) as initial K-line data.
-
In live trading, the specific number of K-line bars obtained is based on the maximum data volume available from the exchange's K-line interface.
When the period parameter is set to 5, it requests 5-second period K-line data. If the period parameter is not divisible by 60 (meaning the period cannot be expressed in minutes), the underlying system will use the exchange.GetTrades() related interface to obtain trade record data and synthesize the required K-line data. If the period parameter is divisible by 60, it will use at minimum 1-minute K-line data (using larger periods as much as possible to synthesize the required K-line data) to synthesize the required K-line data.
In the backtest system, simulation-level backtesting requires setting the underlying K-line period (during simulation-level backtesting, the backtest system generates tick data based on the K-line data corresponding to the set underlying K-line period). Note that 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, K-line data of various periods in the backtest system are synthesized from the K-line data corresponding to the underlying K-line period.
In C++ language, if you need to construct K-line data yourself, here is a 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 display: Records[10]
Log(r);
auto ma = TA.MA(r,10);
// Output display: [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 |