Basic Examples

Example I

Warning

This example is written for python-bittrex-websocket-aio. If you’re using python-bittrex-websocket, you need to remove async from the message channels.

Sample script showing how to receive real-time updates of the state of a single market by invoking SubscribeToSummaryDeltas.

The script creates a ticker container dict, connects to Bittrex, records data to the container and closes when it has received data for all tickers.

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

# /examples/ticker_updates.py
# Stanislav Lazarov

# Sample script showing how subscribe_to_exchange_deltas() works.

# Overview:
# ---------
# 1) Creates a custom ticker_updates_container dict.
# 2) Subscribes to N tickers to get their general information.
# 3) When information is received, checks if the ticker is
#    in ticker_updates_container and adds it if not.
# 4) Disconnects when it has the information for each ticker.

from bittrex_websocket.websocket_client import BittrexSocket
from time import sleep

def main():
    class MySocket(BittrexSocket):

        async def on_public(self, msg):
            name = msg['M']
            if name not in ticker_updates_container:
                ticker_updates_container[name] = msg
                print('Just received market update for {}.'.format(name))

    ticker_updates_container = {}

    # 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 ticker information
    for ticker in tickers:
        sleep(0.01)
        ws.subscribe_to_exchange_deltas([ticker])

    # Users can also subscribe without introducing delays during invoking but
    # it is the recommended way when you are subscribing to a large list of tickers.
    # ws.subscribe_to_exchange_deltas(tickers)

    while len(ticker_updates_container) < len(tickers):
        sleep(1)
    else:
        print('We have received updates for all tickers. Closing...')
        ws.disconnect()
        sleep(10)

if __name__ == "__main__":
    main()

Example II

Warning

This documentation is written for python-bittrex-websocket. The async version doesn’t support order book sync currently.

This script shows how to enable and use order book syncing.

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

# /examples/order_book.py
# Stanislav Lazarov


from __future__ import print_function
from time import sleep
from bittrex_websocket import OrderBook

def main():
    class MySocket(OrderBook):
        def on_ping(self, msg):
            print('Received order book update for {}'.format(msg))

    # Create the socket instance
    ws = MySocket()
    # Enable logging
    ws.enable_log()
    # Define tickers
    tickers = ['BTC-ETH', 'BTC-ZEC']
    # Subscribe to order book updates
    ws.subscribe_to_orderbook(tickers)

    while True:
        sleep(10)
        book = ws.get_order_book('BTC-ETH')
        print(book[u'S'][0])
    else:
        pass


if __name__ == "__main__":
    main()