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 1-hour K-lines and read the oldest bar and the two latest bars.
javascriptfunction main() { var records = exchange.GetRecords(PERIOD_H1) if (!records || records.length < 2) return // null on failure // records[0] is the oldest bar; the last element is the latest bar Log("Oldest Time:", records[0].Time, "Open:", records[0].Open, "High:", records[0].High) Log("Latest", records[records.length - 1], "Previous", records[records.length - 2]) // Output: Latest {"Time":...,"Open":...,"High":...,"Low":...,"Close":...,"Volume":...,...} // Previous {...} }pythondef main(): records = exchange.GetRecords(PERIOD_H1) if not records or len(records) < 2: return Log("Oldest Time:", records[0]["Time"], "Open:", records[0]["Open"], "High:", records[0]["High"]) Log("Latest", records[-1], "Previous", records[-2]) # Output: Latest {"Time":...,"Open":...,"High":...,"Low":...,"Close":...,"Volume":...,...} # Previous {...}rustfn main() { let Ok(records) = exchange.GetRecords(None, PERIOD_H1, None) else { return }; let n = records.len(); if n < 2 { return; } Log!("Oldest Time:", records[0].Time, "Open:", records[0].Open, "High:", records[0].High); Log!("Latest", records[n - 1], "Previous", records[n - 2]); } -
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)); } -
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); }
Returns
| Type | Description |
|
|
Arguments
| Name | Type | Required | Description |
symbol | string | No | Instrument code, for example |
period | number | No | Period of the requested K-line data, for example: |
limit | number | No | Length of the requested K-line data. If this parameter is not passed, the default requested length is the K-line length set by 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 200 K-line bars (if the limit passed in the current call is larger, limit is used as the upper limit). 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 first call fetches
limitK-line bars (the length set byexchange.SetMaxBarLen()whenlimitis not passed, 200 by default); if that exceeds what the exchange's K-line interface returns in one request, the data is fetched page by page.
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.
Exchanges that do not support this function: Pendle, Web3. The call fails with an error and returns null; for all exchanges see Capability Matrix.
Exchanges that serve this function through a fallback: the exchange's API does not fully provide this function, and the docker fills the gap.
The exchange has no such endpoint; the docker implements the function:
BitFlyer,Coincheck,Zaif,Futures_Polymarket: The exchange has no K-line endpoint; bars are synthesized by the docker from trades. The first call covers only the few bars spanned by recent trades, and more accumulate with later calls.Futures_Aevo: The exchange has no K-line endpoint; bars are synthesized by the docker from trades. The first call covers only the few bars spanned by recent trades, and more accumulate with later calls. The result is empty while the contract has no trades.
Some periods are synthesized by the docker:
Backpack,Futures_Backpack,Futures_Binance,Futures_ApolloX,Futures_Aster: Native periods are 1m, 3m, 5m, 15m, 30m, 1h, 2h, 4h, 6h, 8h, 12h, 1d, 3d, 1w, 1M; other periods are synthesized by the docker.BigONE: Native periods are 1m, 5m, 15m, 30m, 1h, 3h, 4h, 6h, 12h, 1d, 1w, 1M; other periods are synthesized by the docker.Binance,Aster: Native periods are 1s, 1m, 3m, 5m, 15m, 30m, 1h, 2h, 4h, 6h, 8h, 12h, 1d, 3d, 1w, 1M; other periods are synthesized by the docker.Bitfinex,Futures_Bitfinex: Native periods are 1m, 5m, 15m, 30m, 1h, 3h, 6h, 12h, 1d, 1w, 14d, 1M; other periods are synthesized by the docker.Bitget: Native periods are 1m, 3m, 5m, 15m, 30m, 1h, 4h, 6h, 12h, 1d, 3d, 1w, 1M; other periods are synthesized by the docker. 3m is available only with the unified account (v3).Futures_Bitget: Native periods are 1m, 3m, 5m, 15m, 30m, 1h, 4h, 6h, 12h, 1d, 3d, 1w, 1M; other periods are synthesized by the docker. Coin-margined delivery contracts on a unified account (v3) have no 3d period.Bithumb: Native periods are 1m, 3m, 5m, 10m, 15m, 30m, 1h, 4h, 1d, 1w, 1M; other periods are synthesized by the docker.Bitstamp: Native periods are 1m, 3m, 5m, 15m, 30m, 1h, 2h, 4h, 6h, 12h, 1d, 3d; other periods are synthesized by the docker.Bybit: Native periods are 1m, 3m, 5m, 15m, 30m, 1h, 2h, 4h, 6h, 12h, 1d, 1w, 1M; other periods are synthesized by the docker.Futures_Bybit: Native periods are 1m, 3m, 5m, 15m, 30m, 1h, 2h, 4h, 6h, 12h, 1d, 1w, 1M; other periods are synthesized by the docker. Options have no K-line endpoint; their bars are synthesized by the docker from trades.Coinone: Native periods are 1m, 3m, 5m, 10m, 15m, 30m, 1h, 2h, 4h, 6h, 1d, 1w, 1M; other periods are synthesized by the docker.Coinw: Native periods are 1m, 3m, 5m, 15m, 30m, 1h, 2h, 4h, 1d, 1w; other periods are synthesized by the docker.Futures_Coinw: Native periods are 1m, 3m, 5m, 15m, 30m, 1h, 4h, 1d, 1w, 1M; other periods are synthesized by the docker. At most 300 bars per call.Crypto,Futures_Crypto: Native periods are 1m, 5m, 15m, 30m, 1h, 2h, 4h, 6h, 12h, 1d, 1w, 14d, 1M; other periods are synthesized by the docker.DeepCoin,Futures_DeepCoin: Native periods are 1m, 3m, 5m, 15m, 30m, 1h, 2h, 4h, 6h, 12h, 1d, 1w, 1M, 1y; other periods are synthesized by the docker.Futures_Deribit: Native periods are 1m, 3m, 5m, 10m, 15m, 30m, 1h, 2h, 3h, 6h, 12h, 1d; other periods are synthesized by the docker.DigiFinex,Futures_DigiFinex,Futures_Kraken,Futures_WEEX,Futures_Lighter: Native periods are 1m, 5m, 15m, 30m, 1h, 4h, 12h, 1d, 1w; other periods are synthesized by the docker.GateIO: Native periods are 10s, 1m, 5m, 15m, 30m, 1h, 4h, 8h, 1d, 1w, 1M; other periods are synthesized by the docker.Futures_GateIO: Native periods are 10s, 30s, 1m, 5m, 15m, 30m, 1h, 2h, 4h, 6h, 8h, 12h, 1d, 1w, 1M; other periods are synthesized by the docker.Gemini: Native periods are 1m, 5m, 15m, 30m, 1h, 6h, 1d; other periods are synthesized by the docker.HashKey: Native periods are 1m, 3m, 5m, 15m, 30m, 1h, 2h, 4h, 6h, 8h, 12h, 1d, 1w, 1M; other periods are synthesized by the docker.HitBTC: Native periods are 1m, 3m, 5m, 15m, 30m, 1h, 4h, 1d, 1w, 1M; other periods are synthesized by the docker.Huobi: Native periods are 1m, 5m, 15m, 30m, 1h, 4h, 1d, 1w, 1M, 1y; other periods are synthesized by the docker.Futures_HuobiDM,MEXC: Native periods are 1m, 5m, 15m, 30m, 1h, 4h, 1d, 1w, 1M; other periods are synthesized by the docker.Korbit: Native periods are 1m, 5m, 15m, 30m, 1h, 4h, 1d, 1w; other periods are synthesized by the docker.Kraken: Native periods are 1m, 5m, 15m, 30m, 1h, 4h, 1d, 1w, 15d; other periods are synthesized by the docker.KuCoin: Native periods are 1m, 3m, 5m, 15m, 30m, 1h, 2h, 4h, 6h, 8h, 12h, 1d, 1w; other periods are synthesized by the docker.Futures_KuCoin: Native periods are 1m, 5m, 15m, 30m, 1h, 2h, 4h, 8h, 12h, 1d, 1w; other periods are synthesized by the docker.LBank: Native periods are 1m, 5m, 15m, 30m, 1h, 4h, 8h, 12h, 1d, 1w, 1M; other periods are synthesized by the docker.Futures_MEXC: Native periods are 1m, 5m, 15m, 30m, 1h, 4h, 8h, 1d, 1w, 1M; other periods are synthesized by the docker.OKEX,Futures_OKCoin,OKX_DEX: Native periods are 1m, 3m, 5m, 15m, 30m, 1h, 2h, 4h, 6h, 12h, 1d, 3d, 1w, 1M, 3M; other periods are synthesized by the docker.Poloniex: Native periods are 1m, 5m, 10m, 15m, 30m, 1h, 2h, 4h, 6h, 12h, 1d, 3d, 1w, 1M; other periods are synthesized by the docker.Upbit: Native periods are 1s, 1m, 3m, 5m, 10m, 15m, 30m, 1h, 4h, 1d, 1w, 1M, 1y; other periods are synthesized by the docker.WEEX: Native periods are 1m, 5m, 15m, 30m, 1h, 2h, 4h, 6h, 8h, 12h, 1d, 1w; other periods are synthesized by the docker.WOO: Native periods are 1m, 5m, 15m, 30m, 1h, 4h, 12h, 1d, 1w, 1M; other periods are synthesized by the docker.Futures_WOO: Native periods are 1m, 5m, 15m, 30m, 1h, 4h, 12h, 1d, 1w, 1M, 1y; other periods are synthesized by the docker.Hyperliquid: Native periods are 1m, 3m, 5m, 15m, 30m, 1h, 2h, 4h, 8h, 12h, 1d, 3d, 1w, 1M; other periods are synthesized by the docker.Futures_Hyperliquid: Native periods are 1m, 3m, 5m, 15m, 30m, 1h, 2h, 4h, 8h, 12h, 1d, 3d, 1w; other periods are synthesized by the docker. Outcome instruments have no bars.Futures_EdgeX: Native periods are 1m, 5m, 15m, 30m, 1h, 2h, 4h, 6h, 8h, 12h, 1d, 1w, 1M; other periods that are a multiple of 60 seconds are synthesized by the docker from native bars, and the rest (such as second-level periods) are not available (the exchange has no trades endpoint).Futures_dYdX: Native periods are 1m, 5m, 15m, 30m, 1h, 4h and 1d; other periods are synthesized by the docker.Futures_WOOFi: Native periods are 1m, 5m, 15m, 30m, 1h, 4h, 12h, 1d, 1w, 1M, 1y; other periods are synthesized by the docker. At most 1000 bars per call.Uniswap,PancakeSwap,Aerodrome,Velodrome: Native periods are 1m, 5m, 15m, 1h, 4h, 12h, 1d (GeckoTerminal's timeframes); other periods are synthesized by the docker: periods of a minute or longer are merged from native periods, and second-level periods are accumulated from the on-chain trades ofexchange.GetTrades()(the first call has only the bars covered by the trades of roughly the last 10 minutes, and later calls keep accumulating; with no trade seen yet the call fails saying so, and when fetching trades fails the error carries that cause).KyberSwap,OneInch,Jupiter: Native periods are 1m, 5m, 15m, 1h, 4h, 12h, 1d (GeckoTerminal's timeframes); other periods of a minute or longer are synthesized by the docker, while second-level periods are unavailable (there is no trade feed to build them from).Futures_Futu: Native periods are 1m, 3m, 5m, 15m, 30m, 1h, 1d, 1w, 1M; other periods are synthesized by the docker.Futures_IB: Native periods are 1m, 3m, 5m, 15m, 30m, 1h, 2h, 4h, 6h, 1d, 1w; other periods that are a multiple of 60 seconds are synthesized by the docker from native bars, and the rest (such as second-level periods) are not available (this exchange object does not supportexchange.GetTrades()).Exchange: The gateway'srecordstakes only the 1m, 3m, 5m, 15m, 30m, 1h and 1d periods; other periods are synthesized by the docker, and a start time cannot be specified.