Type/to search
Built-in Functions
Global
Log
Market
exchange.GetTicker
exchange.GetTickers
exchange.GetDepth
exchange.GetTrades
exchange.GetRecords
exchange.GetMarkets
exchange.GetRawJSON
exchange.SetData
exchange.GetData
Trade
Account
Futures
Exchange
IO
Network
Storage
Threads
TA
Talib
OS
Structures
Built-in Variables and Constants
Conventions
Exchanges

Retrieves data loaded by the exchange.SetData() function, or data provided by an external link.

exchange.GetData(key)
exchange.GetData(key, timeout)
exchange.GetData(key, timeout, offset)

Examples

  • How to call the method for writing data directly.

    javascript
    /*backtest start: 2020-01-21 00:00:00 end: 2020-02-12 00:00:00 period: 1d basePeriod: 1d exchanges: [{"eid":"Bitfinex","currency":"BTC_USD"}] */ function main() { exchange.SetData("test", [[1579536000000, _D(1579536000000)], [1579622400000, _D(1579622400000)], [1579708800000, _D(1579708800000)]]) while(true) { Log(exchange.GetData("test")) Sleep(1000 * 60 * 60 * 24) } }
    python
    '''backtest start: 2020-01-21 00:00:00 end: 2020-02-12 00:00:00 period: 1d basePeriod: 1d exchanges: [{"eid":"Bitfinex","currency":"BTC_USD"}] ''' def main(): exchange.SetData("test", [[1579536000000, _D(1579536000000/1000)], [1579622400000, _D(1579622400000/1000)], [1579708800000, _D(1579708800000/1000)]]) while True: Log(exchange.GetData("test")) Sleep(1000 * 60 * 60 * 24)
    rust
    /*backtest start: 2020-01-21 00:00:00 end: 2020-02-12 00:00:00 period: 1d basePeriod: 1d exchanges: [{"eid":"Bitfinex","currency":"BTC_USD"}] */ fn main() { // In the Rust SDK, the data parameter of SetData is a JSON string; use format! to concatenate the data let data = format!(r#"[[1579536000000, "{}"], [1579622400000, "{}"], [1579708800000, "{}"]]"#, _D(1579536000000), _D(1579622400000), _D(1579708800000)); exchange.SetData("test", &data); loop { Log!(exchange.GetData("test")); Sleep(1000 * 60 * 60 * 24); } }
  • Data can be requested through external links. The data format returned by the request is as follows:

    json
    { "schema":["time","data"], "data":[ [1579536000000, "abc"], [1579622400000, 123], [1579708800000, {"price": 123}], [1579795200000, ["abc", 123, {"price": 123}]] ] }

    Here schema defines the data format of each record in the data body. This format is fixed as ["time","data"], corresponding one-to-one with the format of each piece of data in the data attribute. The data attribute is used to store the data body, where each piece of data consists of a millisecond-level timestamp and the data content (the data content can be any JSON-encodable data).

    The following is a test service program written in Go:

    mylang
    package main import ( "fmt" "net/http" "encoding/json" ) func Handle (w http.ResponseWriter, r *http.Request) { defer func() { fmt.Println("req:", *r) ret := map[string]interface{}{ "schema": []string{"time","data"}, "data": []interface{}{ []interface{}{1579536000000, "abc"}, []interface{}{1579622400000, 123}, []interface{}{1579708800000, map[string]interface{}{"price":123}}, []interface{}{1579795200000, []interface{}{"abc", 123, map[string]interface{}{"price":123}}}, }, } b, _ := json.Marshal(ret) w.Write(b) }() } func main () { fmt.Println("listen http://localhost:9090") http.HandleFunc("/data", Handle) http.ListenAndServe(":9090", nil) }

    The response data returned by the program after receiving the request:

    json
    { "schema":["time","data"], "data":[ [1579536000000, "abc"], [1579622400000, 123], [1579708800000, {"price": 123}], [1579795200000, ["abc", 123, {"price": 123}]] ] }

    The test strategy code is as follows:

    javascript
    /*backtest start: 2020-01-21 00:00:00 end: 2020-02-12 00:00:00 period: 1d basePeriod: 1d exchanges: [{"eid":"Bitfinex","currency":"BTC_USD"}] */ function main() { while(true) { Log(exchange.GetData("http://xxx.xx.x.xx:9090/data")) Sleep(1000) } }
    python
    '''backtest start: 2020-01-21 00:00:00 end: 2020-02-12 00:00:00 period: 1d basePeriod: 1d exchanges: [{"eid":"Bitfinex","currency":"BTC_USD"}] ''' def main(): while True: Log(exchange.GetData("http://xxx.xx.x.xx:9090/data")) Sleep(1000)
    rust
    /*backtest start: 2020-01-21 00:00:00 end: 2020-02-12 00:00:00 period: 1d basePeriod: 1d exchanges: [{"eid":"Bitfinex","currency":"BTC_USD"}] */ fn main() { loop { Log!(exchange.GetData("http://xxx.xx.x.xx:9090/data")); Sleep(1000); } }
  • How to call the method for fetching data from external links.

    javascript
    function main() { Log(exchange.GetData("http://xxx.xx.x.xx:9090/data")) Log(exchange.GetData("https://www.fmz.com/upload/asset/32bf73a69fc12d36e76.json")) }
    python
    def main(): Log(exchange.GetData("http://xxx.xx.x.xx:9090/data")) Log(exchange.GetData("https://www.fmz.com/upload/asset/32bf73a69fc12d36e76.json"))
    rust
    fn main() { Log!(exchange.GetData("http://xxx.xx.x.xx:9090/data")); Log!(exchange.GetData("https://www.fmz.com/upload/asset/32bf73a69fc12d36e76.json")); }
  • Request the query data created on the datadata platform. The response data format must meet the following requirements (the schema must describe the time and data fields):

    json
    { "data": [], "schema": ["time", "data"] }

    The "data" field contains the required data content, and the data in the "data" field must be consistent with the fields defined in the "schema". When calling the exchange.GetData() function, a JSON object is returned, for example: {"Time":1579795200000, "Data":"..."}.

    javascript
    function main() { Log(exchange.GetData("https://www.datadata.com/api/v1/query/xxx/data")) // The xxx part in the link is the code of the query data; xxx here is just an example }
    python
    def main(): Log(exchange.GetData("https://www.datadata.com/api/v1/query/xxx/data"))
    rust
    fn main() { Log!(exchange.GetData("https://www.datadata.com/api/v1/query/xxx/data")); // The xxx part in the link is the code of the query data; xxx here is just an example }

Returns

TypeDescription

object

Returns an object of the form {"Time": ..., "Data": ...}, where Time is the timestamp of the record and Data is its content. When no record is available (no data has been loaded, the request to the external link failed, or every record is later than offset), Data is a null value.

Arguments

NameTypeRequiredDescription

key

string

Yes

The name of the dataset, or the data request URL.

timeout

number

No

Used to set the cache timeout period for data requested from an external link, in milliseconds. In live trading, the default is 60000 (one minute) when this parameter is not passed, and the minimum is 5000. Data loaded with exchange.SetData() does not expire.

offset

number

No

A timestamp in milliseconds. The last record in the dataset whose time is not later than offset is returned; when this parameter is not passed it is the current time, i.e., only records that have already arrived are returned.

See Also

Remarks

In backtesting, the data is retrieved all at once; in live trading, the data is cached for one minute. In the backtesting system, when requesting data via an access interface, the backtesting system automatically adds parameters such as from (timestamp, in seconds), to (timestamp, in seconds), and period (the underlying K-line period, timestamp, in milliseconds) to the request, in order to determine the time range of the data to be retrieved.

In Rust, exchange.GetData(key) takes only the key argument and returns a Result (an error when the request to the external link fails or the dataset does not exist); use exchange.GetDataWithOptions(key, timeout, offset) to pass timeout and offset.