2017-08-07 106 views
15

的API文檔不鼓勵在/ticker端點投票,並推薦使用的WebSocket流偵聽匹配消息如何獲得實時買價/賣價從GDAX /價格的WebSocket飼料

但比賽響應只提供一個priceside(出售/購買)

我該如何從websocket feed重新創建股票數據(價格,詢價和出價)?

{ 
    「price」: 「333.99」, 
    「size」: 「0.193」, 
    「bid」: 「333.98」, 
    「ask」: 「333.99」, 
    「volume」: 「5957.11914015」, 
    「time」: 「2015-11-14T20:46:03.511254Z」 
} 

ticker端點和WebSocket的飼料都返回一個「價格」,但我想這是不一樣的。隨着時間的推移,ticker端點的price是否會達到某種平均值?

我該如何計算Bid的值,Ask的值?

+1

我最近在探索gdax API,並且有同樣的問題。不知道他們如何計算股票「價格」。我仍然因爲這個原因結束了投票(每5秒一次)。 – Aknosis

回答

10

如果我使用的這些參數訂閱消息:

params = { 
    "type": "subscribe", 
    "channels": [{"name": "ticker", "product_ids": ["BTC-EUR"]}] 
} 

每一個新的貿易執行(和可見http://www.gdax.com)的時候,我得到這樣的消息從網絡插口:

{ 
u'best_ask': u'3040.01', 
u'best_bid': u'3040', 
u'last_size': u'0.10000000', 
u'price': u'3040.00000000', 
u'product_id': u'BTC-EUR', 
u'sequence': 2520531767, 
u'side': u'sell', 
u'time': u'2017-09-16T16:16:30.089000Z', 
u'trade_id': 4138962, 
u'type': u'ticker' 
} 

就在這個特別的消息後,我做了https://api.gdax.com/products/BTC-EUR/ticker得到,我得到這個:

{ 
    "trade_id": 4138962, 
    "price": "3040.00000000", 
    "size": "0.10000000", 
    "bid": "3040", 
    "ask": "3040.01", 
    "volume": "4121.15959844", 
    "time": "2017-09-16T16:16:30.089000Z" 
} 

目前的數據是從網絡套接字相同得到請求。

請在下面找到一個完整的測試腳本,用這個代碼實現一個web套接字。

#!/usr/bin/env python 
# -*- coding: utf-8 -*- 

"""Test for websockets.""" 

from websocket import WebSocketApp 
from json import dumps, loads 
from pprint import pprint 

URL = "wss://ws-feed.gdax.com" 


def on_message(_, message): 
    """Callback executed when a message comes. 

    Positional argument: 
    message -- The message itself (string) 
    """ 
    pprint(loads(message)) 
    print 


def on_open(socket): 
    """Callback executed at socket opening. 

    Keyword argument: 
    socket -- The websocket itself 
    """ 

    params = { 
     "type": "subscribe", 
     "channels": [{"name": "ticker", "product_ids": ["BTC-EUR"]}] 
    } 
    socket.send(dumps(params)) 


def main(): 
    """Main function.""" 
    ws = WebSocketApp(URL, on_open=on_open, on_message=on_message) 
    ws.run_forever() 


if __name__ == '__main__': 
    main()