Get the {@struct/Record Record} structure array of the spot or contract corresponding to the currently set trading pair, contract code, i.e. K-line data.
The exchange.GetRecords()
function returns an array of {@struct/Record Record} structures when the request for data succeeds, and it returns null values when the request for data fails.
{@struct/Record Record}arrays, null values
exchange.GetRecords() exchange.GetRecords(symbol) exchange.GetRecords(symbol, period) exchange.GetRecords(symbol, period, limit) exchange.GetRecords(period) exchange.GetRecords(period, limit)
The parameter symbol
is used to specify the specific trading pair and contract code corresponding to the requested {@struct/Record Record} array data. If this parameter is not passed, the K-line data of the currently set trading pair and contract code will be requested by default. When calling the exchange.GetRecords(symbol)
function, exchange
is the spot exchange object. If you need to request to obtain the data with the denominated currency as USDT and the transaction currency as BTC, the parameter symbol
is: "BTC_USDT"
, and the format is the trading pair format defined by the FMZ platform. When calling the exchange.GetRecords(symbol)
function, exchange
is the futures exchange object. If you need to request the order book data of BTC’s U-standard perpetual contract, the parameter symbol
is: "BTC_USDT.swap"
, and the format is a combination of the trading pair and contract code defined by the FMZ platform, separated by the character “.”. When calling the exchange.GetRecords(symbol)
function, exchange
is the futures exchange object. If you need to request the order book data of BTC’s U-standard option contract, the parameter symbol
is: "BTC_USDT.BTC-240108-40000-C"
(taking Binance Option BTC-240108-40000-C as an example), the format is the combination of the trading pair defined by the FMZ platform and the specific option contract code defined by the exchange, separated by the character “.”.
symbol
false
string
The parameter period
specifies the period of the requested K-line data, for example: {@var/PERIOD/PERIOD_M1 PERIOD_M1}, {@var/PERIOD/PERIOD_M5 PERIOD_M5}, {@var/PERIOD/PERIOD_M15 PERIOD_M15}, etc. The value of parameter period
can be passed not only the defined standard period, but also integer values in seconds. If this parameter is not passed, the period of the K-line data requested by default is the default K-line period of the current strategy real-time/backtest configuration.
period
false
number
The parameter limit
is used to specify the length of the requested K-line data. If this parameter is not passed, the default request length is the maximum number of K-line bars requested at a time of the exchange K-line interface. This parameter may cause paging to query the exchange K-line data, and the time consumption of the function call will increase during paging query.
limit
false
number
function 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))
}
def main():
Log(exchange.GetRecords(60 * 2))
Log(exchange.GetRecords(PERIOD_M5))
void main() {
Log(exchange.GetRecords(60 * 2)[0]);
Log(exchange.GetRecords(PERIOD_M5)[0]);
}
Get K-line data for a custom period.
function main() {
var records = exchange.GetRecords(PERIOD_H1)
/*
The exchange interface may not be accessible due to network reasons (even if the docker program's device can open the exchange website, the API interface may not be accessible).
At this point, records is null. When accessing records[0].Time, it will cause an error. Therefore, when testing this code, ensure that you can access the exchange interface.
*/
Log("The first k-line data is Time:", records[0].Time, "Open:", records[0].Open, "High:", records[0].High)
Log("The second k-line data is Time:", records[1].Time ,"Close:", records[1].Close)
Log("Current K-line (latest)", records[records.length-1], "Previous K-line", records[records.length-2])
}
def main():
records = exchange.GetRecords(PERIOD_H1)
Log("The first k-line data is Time:", records[0]["Time"], "Open:", records[0]["Open"], "High:", records[0]["High"])
Log("The second k-line data Time:", records[1]["Time"], "Close:", records[1]["Close"])
Log("Current K-line (latest)", records[-1], "Previous K-line", records[-2])
void main() {
auto records = exchange.GetRecords(PERIOD_H1);
Log("The first k-line data is Time:", records[0].Time, "Open:", records[0].Open, "High:", records[0].High);
Log("The 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]);
}
Output K-line bar data:
function main() {
var records = exchange.GetRecords("BTC_USDT.swap", 60, 100)
Log(records)
}
def main():
records = exchange.GetRecords("BTC_USDT.swap", 60, 100)
Log(records)
void main() {
auto records = exchange.GetRecords("BTC_USDT.swap", 60, 100);
Log(records);
}
When the configured exchange
object is a futures exchange object, use the symbol
, period
, and limit
parameters to request the K-line data of a specific product (futures product).
The default K-line period can be set in the backtest and real trading pages. If you specify a parameter when calling the exchange.GetRecords()
function, the K-line data corresponding to that parameter period will be obtained. If no parameter is specified when the function is called, the corresponding K-line data will be returned according to the K-line period set in the backtest and real market parameters.
The return value is an array of Record
structures, the returned K-line data will be accumulated over time, the upper limit of the accumulated K-line bars is affected by the exchange.SetMaxBarLen()
function setting. The default limit is 5000 bars when it’s not set. When the K-line data reaches the K-line bar accumulation limit, it will be updated by adding a K-line bar and deleting the earliest K-line bar (e.g. queue in/out). Some exchanges do not provide a K-line interface, so the docker collects market transaction record data (Trade
structured array) in real time to generate K-lines.
If the exchange’s K-line interface supports paging queries, multiple API requests will be made when calling the exchange.SetMaxBarLen()
function to set a larger K-line length.
When the exchange.GetRecords()
function is called initially, the number of K-line bars obtained differs between backtesting and real trading: - The backtesting system will obtain a certain number of K-line bars before the start of the backtesting time range in advance (the default is 5000, the settings of the backtesting system and the amount of data will affect the final number returned), as the initial K-line data. - The number of K-line bars obtained during the actual trading is based on the maximum amount of data that can be obtained from the K-line interface of the exchange.
The period
parameter is set to 5, which is a request to get K-line data with a period of 5 seconds. If the period
parameter is not divisible by 60 (i.e., the period represented is not divisible by minutes). The underlying system uses the relevant interface of exchange.GetTrades()
to obtain the transaction record data and synthesize the required K-line data. If the period
parameter is divisible by 60, then the required K-line data is synthesized using a minimum of 1-minute K-line data (if possible, the required K-line data is synthesized using a larger period).
The simulated level backtesting in the backtesting system requires the setting of the underlying K-line period (when the backtesting system simulates level backtesting, the corresponding K-line data is used to generate Tick data according to the set underlying K-line period). It should be noted that the period of the K-line data obtained in the strategy should not be smaller than the underlying K-line period. Because in the simulation level backtesting, the K-line data of each period in the backtesting system is synthesized from the K-line data of the underlying K-line period.
The C++
language has the following code example if you need to construct your own K-line data:
#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 |
{@fun/Market/exchange.GetTicker exchange.GetTicker}, {@fun/Market/exchange.GetDepth exchange.GetDepth}, {@fun/Market/exchange.GetTrades exchange.GetTrades}, {@fun/Market/exchange.SetMaxBarLen exchange.SetMaxBarLen}
exchange.GetTrades exchange.GetPeriod