Python Bittrex WebSocket¶
Python Bittrex WebSocket (PBW) is the first unofficial Python wrapper for the Bittrex Websocket API. It provides users with a simple and easy to use interface to the Bittrex Exchange.
Users can use it to access real-time public data (e.g exchange status, summary ticks and order fills) and account-level data such as order and balance status. The goal of the package is to serve as a foundation block which users can use to build upon their applications. Examples usages can include maintaining live order books, recording trade history, analysing order flow and many more.
Versions¶
PBW comes in two flavours which share the same methods and level of maintenance:
bittrex-websocket-aio¶
The version is built upon asyncio
which is Python’s standard asynchronous I/O framework.
Source code: https://github.com/slazarov/python-bittrex-websocket-aio
bittrex-websocket¶
Choose this version if you decide not to use asyncio or you are using Python = 2.7.
Source code: https://github.com/slazarov/python-bittrex-websocket
Quick Start¶
pip install bittrex-websocket
#!/usr/bin/python
# -*- coding: utf-8 -*-
# /examples/record_trades.py
# Stanislav Lazarov
# Sample script to show how subscribe_to_exchange_deltas() works.
# Overview:
# ---------
# 1) Creates custom trade_history dict.
# 2) When an order is executed, the fill is recorded in trade_history.
# 3) When each ticker has received an order, the script disconnects.
from __future__ import print_function
from time import sleep
from bittrex_websocket import BittrexSocket, BittrexMethods
def main():
class MySocket(BittrexSocket):
def on_public(self, msg):
# Create entry for the ticker in the trade_history dict
if msg['invoke_type'] == BittrexMethods.SUBSCRIBE_TO_EXCHANGE_DELTAS:
if msg['M'] not in trade_history:
trade_history[msg['M']] = []
# Add history nounce
trade_history[msg['M']].append(msg)
# Ping
print('[Trades]: {}'.format(msg['M']))
# Create container
trade_history = {}
# Create the socket instance
ws = MySocket()
# Enable logging
ws.enable_log()
# Define tickers
tickers = ['BTC-ETH', 'BTC-NEO', 'BTC-ZEC', 'ETH-NEO', 'ETH-ZEC']
# Subscribe to trade fills
ws.subscribe_to_exchange_deltas(tickers)
while len(set(tickers) - set(trade_history)) > 0:
sleep(1)
else:
for ticker in trade_history.keys():
print('Printing {} trade history.'.format(ticker))
for trade in trade_history[ticker]:
print(trade)
ws.disconnect()
sleep(10)
if __name__ == "__main__":
main()
Disclaimer¶
I am not associated with Bittrex. Use the library at your own risk, I don’t bear any responsibility if you end up losing your money.