bitstamp-realtime-order-book

Gives you low latency access to Bitstamp Realtime Order Book.

MIT License

Stars
6
Committers
3

Bitstamp Realtime Client

Client for the Bitstamp websocket written in Python.

API Documentation: https://www.bitstamp.net/websocket/

What you can do with this client:

  • Live ticker (tested)
  • Live Order Book (tested)
  • Live Full Order Book (untested)
  • Liver Orders (untested)

Usage

Installation

git clone [email protected]:philipperemy/bitstamp-realtime-order-book.git
cd bitstamp-realtime-order-book
pip3 install -r requirements.txt
python3 live_order_book.py # will start streaming the order book of BTC/USD.

Firstly, instantiate a new Bitstamp object.

import bitstamp
client = bitstamp.wsclient.BitstampWebsocketClient()

Secondly, subscribe to data sources.

client.subscribe("live_trades", "btc", "eur")
#this will update client.lastprice["btc"]["eur"]
client.subscribe("order_book", "btc", "usd") #choose either this one, for accuracy
client.subscribe("diff_order_book", "btc", "usd") #or this one, for speed
#both will keep client.orderbook["btc"]["usd"] up to date
client.subscribe("liveorders", "eur", "usd")
#this will keep self.open_orders["eur"]["usd"] up to date, and stores open orders
#by id and by price

To get the data from the client, access its attributes. There are no functions to return the data from the instantiated object. The attributes that could be accessed are:

lastprice

{'eur': {'usd': {'price': {}, 'id': {}}},
 'btc': {'usd': '607.38',
         'eur': {'price': {}, 'id': {}}}}

orderbook

{'eur': {'usd': {'price': {}, 'id': {}}},
 'btc': {'usd': {'price': {}, 'id': {}},
         'eur': {'bids': [['542.57000000', '0.09375660'], ['542.56000000', '0.64376270'], ['542.55000000', '1.04019586'], ['542.43000000', '0.01429072'], ['542.40000000', '9.90000000'], ['542.00000000', '0.59001845'], ['541.92000000', '0.54879067'], ['541.85000000', '0.54010071'], ['541.31000000', '20.00000000'], ['541.28000000', '1.51017572'], ['541.26000000', '0.01432070'], ['541.06000000', '0.27646712'], ['540.99000000', '0.05999999'], ['540.68000000', '0.01433069'], ['540.54000000', '3.01230000'], ['540.09000000', '0.01435068'], ['540.00000000', '1.89310414'], ['539.96000000', '3.05143258'], ['539.91000000', '0.20000000'], ['539.84000000', '0.33000000']],
                'asks': [['545.41000000', '1.22027127'], ['545.42000000', '7.45199753'], ['545.50000000', '5.00000000'], ['545.96000000', '1.77000000'], ['545.97000000', '2.13058700'], ['545.98000000', '0.40000000'], ['545.99000000', '11.68681858'], ['546.05000000', '0.00940000'], ['546.10000000', '2.00000000'], ['546.63000000', '0.01427073'], ['546.67000000', '20.00000000'], ['546.70000000', '1.75590000'], ['547.04000000', '0.12500000'], ['547.21000000', '0.01426074'], ['547.44000000', '0.55088459'], ['547.45000000', '1.96054000'], ['547.80000000', '0.01424075'], ['548.00000000', '0.02000000'], ['548.08000000', '3.13810000'], ['548.15000000', '0.11107291']]}}}

openorders

#The data below was shortened for readability, so price and id do not match,
#because they are not complete
{'eur': {'usd': {'price': {}, 'id': {}}},
 'btc': {'eur': {'price': {}, 'id': {}},
         'usd': {'price': {'608.16': [],
                           '608.2': [],
                           '607.96': [{'price': '607.96', 'order_type': 1, 'id': 151771464, 'datetime': '1474285223', 'amount': 0.54},
                                      {'price': '607.96', 'order_type': 1, 'id': 151771465, 'datetime': '1474285224', 'amount': 0.84}],
                           '607.87': [],
                           '611.29': [{'price': '611.29', 'order_type': 1, 'id': 151771472, 'datetime': '1474285225', 'amount': 46.94}],
                           '608.75': [{'price': '608.75', 'order_type': 1, 'id': 151771467, 'datetime': '1474285223', 'amount': 22.23}],
                           '606.03': [{'price': '606.03', 'order_type': 0, 'id': 151771454, 'datetime': '1474285221', 'amount': 2.49}]},
                  'id': {151771456: {'price': '605.6', 'order_type': 0, 'id':
                                     151771456, 'datetime': '1474285221', 'amount': 2.01},
                         151771457: {'price': '607.84', 'order_type': 1, 'id':
                                     151771457, 'datetime': '1474285221', 'amount': 0.90248695},
                         151771458: {'price': '606.02', 'order_type': 0, 'id':
                                     151771458, 'datetime': '1474285221', 'amount': 0.99},
                         151771460: {'price': '606.24', 'order_type': 0, 'id':
                                     151771460, 'datetime': '1474285221', 'amount': 0.2764},
                         151771461: {'price': '608.19', 'order_type': 1, 'id':
                                     151771461, 'datetime': '1474285222', 'amount': 19.43},
                         151771462: {'price': '605.87', 'order_type': 0, 'id':
                                     151771462, 'datetime': '1474285222', 'amount': 2.09}}}}}

Note that all data points are strings, so users can convert them to their own favourite data type.

Third party source code included

  • The folder pusherclient was copied from Erik Kulyk (ekulyk), and is under the
    MIT license. Changes to pusherclient made here will be or have been proposed to
    the original repository, if they
    could be useful outside this project.