nunodsousa
nunodsousa

Reputation: 2785

bitstamp orderbook using websocket

The following code written in python uses websocket to get the price of a cryptocoin (ether in USD) in real time, of the bitstamp exchange. It prints in the screen the downloaded information.

import pusherclient
import logging

def connect_handler(data):
    trades_channel_ethusd = pusher.subscribe("live_trades_ethusd")
    trades_channel_ethusd.bind('trade', trade_callback_ethusd)

def trade_callback_ethusd(data):
    print(data)

pusher = pusherclient.Pusher("de504dc5763aeef9ff52")
pusher.connection.logger.setLevel(logging.WARNING) 

pusher.connection.bind('pusher:connection_established', connect_handler)
pusher.connect()

If I want to do the same with the orderbook, we should change the "live_trades_ethusd" to "diff_order_book_ethusd". (https://www.bitstamp.net/websocket/)

However, when I replace the string it returns nothing. Is it a websocket failure?

EDIT:

The right code is:

import pusherclient
import logging

def connect_handler(data):
        trades_channel_ethusd = pusher.subscribe("live_trades_ethusd")
        trades_channel_ethusd.bind('data', trade_callback_ethusd)

def trade_callback_ethusd(data):
    print(data)

pusher = pusherclient.Pusher("de504dc5763aeef9ff52")
pusher.connection.logger.setLevel(logging.WARNING) 

pusher.connection.bind('pusher:connection_established',     connect_handler)
pusher.connect()

Upvotes: 0

Views: 1672

Answers (1)

Gregory Nikitas
Gregory Nikitas

Reputation: 491

Short Answer

Change your bind line to the following

trades_channel_ethusd.bind('data', trade_callback_ethusd)

Full Answer

When subscribing to a WebSocket, be sure to bind to the correct Event name. The "previous" call (namely live_trades_ethusd) binds to the "trade" event where as the diff_order_book_ethusd call binds to the "data" event

Upvotes: 2

Related Questions