2017-08-07 56 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()