Deribit websocket API 鏈接範例,使用JavaScript語言,策略運行在FMZ發明者量化平臺。javascript
源碼地址:https://www.fmz.com/strategy/147765java
var client = null var deribitAcc = {} // 能夠設置個全局對象保存 token function WS_GetAccount() { // 獲取帳戶 某個幣種 的資產信息 var msg = { "jsonrpc": "2.0", "id": 2515, "method": "private/get_account_summary", "params": { "currency": "ETH", "extended": true } } client.write(JSON.stringify(msg)) var ret = client.read() Log(ret, "#FF0000") } function WS_GetToken() { // 認證 ,而且獲取 token var msg = { "jsonrpc": "2.0", "id": 9929, "method": "public/auth", "params": { "grant_type": "client_credentials", "client_id": "XXXXXXX", // 申請 API KEY 時獲取 "client_secret": "XXXXXXXXXXXXXXXXXXXXXXXXXX" // 申請 API KEY 時獲取 } } while (1) { client.write(JSON.stringify(msg)) var ret = client.read() try { var jsonObj = JSON.parse(ret) if (jsonObj) { deribitAcc.accessToken = jsonObj.result.access_token deribitAcc.refToken = jsonObj.result.refresh_token break } } catch (e) { Log("error:", e) } } Log("更新 deribitAcc accessToken , refToken:", deribitAcc) } function WS_Depth() { // 訪問 get_order_book 公共 頻道,獲取 訂單薄深度數據 var msg = { "jsonrpc": "2.0", "id": 8772, "method": "public/get_order_book", "params": { "instrument_name": "BTC-PERPETUAL", // 指定,獲取 BTC 永續合約的深度數據 "depth": 5 } } client.write(JSON.stringify(msg)) var ret = client.read() Log("depth : ", ret) } function main() { client = Dial("wss://www.deribit.com/ws/api/v2") WS_GetToken() WS_GetAccount() WS_Depth() } function onexit() { Log("關閉 ws 鏈接") client.close() }