资源加载中... loading...

exchange.GetData

exchange.GetData()函数用于获取exchange.SetData()函数加载的数据或外部链接提供的数据。

数据集合中的记录。 object

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

数据集合名称。 key true string 用于设置缓存超时,单位为毫秒。实盘时默认为一分钟缓存超时。 timeout false number

/*backtest
start: 2020-01-21 00:00:00
end: 2020-02-12 00:00:00
period: 1d
basePeriod: 1d
exchanges: [{"eid":"Futures_CTP","currency":"FUTURES"}]
*/

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)
    }
}
'''backtest
start: 2020-01-21 00:00:00
end: 2020-02-12 00:00:00
period: 1d
basePeriod: 1d
exchanges: [{"eid":"Futures_CTP","currency":"FUTURES"}]
'''  
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)
/*backtest
start: 2020-01-21 00:00:00
end: 2020-02-12 00:00:00
period: 1d
basePeriod: 1d
exchanges: [{"eid":"Futures_CTP","currency":"FUTURES"}]
*/    
void main() {
    json arr = R"([[1579536000000, ""], [1579622400000, ""], [1579708800000, ""]])"_json;
    arr[0][1] = _D(1579536000000);
    arr[1][1] = _D(1579622400000);
    arr[2][1] = _D(1579708800000);
    exchange.SetData("test", arr);
    while(true) {
        Log(exchange.GetData("test"));
        Sleep(1000 * 60 * 60 * 24);
    }
}

获取直接写入的数据的调用方式。

/*backtest
start: 2020-01-21 00:00:00
end: 2020-02-12 00:00:00
period: 1d
basePeriod: 1d
exchanges: [{"eid":"Futures_CTP","currency":"FUTURES"}]
*/
function main() {
    while(true) {
        Log(exchange.GetData("http://xxx.xx.x.xx:9090/data"))
        Sleep(1000)
    }
}
'''backtest
start: 2020-01-21 00:00:00
end: 2020-02-12 00:00:00
period: 1d
basePeriod: 1d
exchanges: [{"eid":"Futures_CTP","currency":"FUTURES"}]
'''  

def main():
    while True:
        Log(exchange.GetData("http://xxx.xx.x.xx:9090/data"))
        Sleep(1000)
/*backtest
start: 2020-01-21 00:00:00
end: 2020-02-12 00:00:00
period: 1d
basePeriod: 1d
exchanges: [{"eid":"Futures_CTP","currency":"FUTURES"}]
*/  

void main() {
    while(true) {
        Log(exchange.GetData("http://xxx.xx.x.xx:9090/data"));
        Sleep(1000);
    }
}

支持通过外部链接请求数据,请求到的数据格式:

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

其中schema为加载数据的主体中的每一条记录的数据格式,该格式固定为["time","data"]对应data属性中的逐条数据的格式。 data属性中储存的为数据主体,每条数据由毫秒级别时间戳和数据内容构成(数据内容可以是任何可JSON编码的数据)。 测试用的服务程序,使用Go语言编写:

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)
}

程序在接收到请求后的应答数据:

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

测试策略代码:

function main() {
    Log(exchange.GetData("http://xxx.xx.x.xx:9090/data"))
}
def main():
    Log(exchange.GetData("http://xxx.xx.x.xx:9090/data"))
void main() {
    Log(exchange.GetData("http://xxx.xx.x.xx:9090/data"));
}

获取外部链接的数据的调用方式。

function main() {
    // 链接中xxx部分为查询的数据的编码,此处xxx为举例。具体查询链接可以登录datadata.cn平台创建,也可以在优宽量化平台「数据探索」页面创建
    Log(exchange.GetData("https://www.datadata.cn/api/v1/query/xxx/data"))   
}
def main():
    Log(exchange.GetData("https://www.datadata.cn/api/v1/query/xxx/data"))
void main() {
    Log(exchange.GetData("https://www.datadata.cn/api/v1/query/xxx/data"));
}

请求datadata平台上创建的查询数据,要求应答的数据格式为(必须有time、data字段在schema里描述):

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

其中"data"字段为需要的数据内容,"data"字段中的数据需要与"schema"中约定的一致。当调用exchange.GetData()函数时,返回一个JSON对象,例如:{"Time":1579795200000, "Data":"..."}

回测时一次性获取数据,实盘时缓存一分钟的数据。 回测系统中,当使用访问接口请求数据的方式时,回测系统会自动给请求增加from(时间戳秒),to(时间戳秒), period(底层K线周期,时间戳毫秒)等参数,用于确定要获取数据的时间范围。

{@fun/Market/exchange.SetData exchange.SetData}

exchange.SetData exchange.GetMarkets