Example #1
0
    def __init__(self):
        super(BitstampWebsocketAPI2, self).__init__()

        # Pusher channels for live trades, order book, and live orders (order changes)
        self.trade_channel = None
        self.orderbook_channel = None
        self.orderchange_channel = None

        # todo consider moving back out to class variable...
        self._pusher = Pusher(BitstampWebsocketAPI2.APP_KEY)
Example #2
0
    def __init__(self):
        super(BitstampWebsocketAPI2, self).__init__()

        # Pusher channels for live trades, order book, and live orders (order changes)
        self.trade_channel = None
        self.orderbook_channel = None
        self.orderchange_channel = None

        # todo consider moving back out to class variable...
        self._pusher = Pusher(BitstampWebsocketAPI2.APP_KEY)
Example #3
0
class BitstampWebsocketAPI2(Observable):
    APP_KEY = "de504dc5763aeef9ff52"  # Bitstamp's Pusher API key

    def __init__(self):
        super(BitstampWebsocketAPI2, self).__init__()

        # Pusher channels for live trades, order book, and live orders (order changes)
        self.trade_channel = None
        self.orderbook_channel = None
        self.orderchange_channel = None

        # todo consider moving back out to class variable...
        self._pusher = Pusher(BitstampWebsocketAPI2.APP_KEY)

        # moving this back to being an instance variable, instead of class.
        # Initializing _pusher in class (where =None) instead of init makes it blow up spectacularly.
        #if not self._pusher:
        #    BitstampWebsocketAPI2._pusher = twistedpusher.Client(BitstampWebsocketAPI2.APP_KEY)
        #    pass

    # todo switch from time.time to utc
    @simpleschema.returns(schemas.Trade)
    def trade(self, event):
        data = json.loads(event.data, parse_float=Decimal)
        data['timestamp'] = time.time()
        return data

    @simpleschema.returns(schemas.OrderBook)
    def orderbook(self, event):
        book = dict()
        book['bids'] = [{
            'price': pr,
            'amount': amt
        } for pr, amt in event.data['bids']]
        book['asks'] = [{
            'price': pr,
            'amount': amt
        } for pr, amt in event.data['bids']]

        return book

    @simpleschema.returns(schemas.BitstampOrderChange)
    def orderchange(self, event):
        change = event.data
        if event.name == 'order_created':
            kind = 'created'
        elif event.name == 'order_deleted':
            kind = 'deleted'
        elif event.name == 'order_changed':
            kind = 'changed'
        else:
            return

        change['change'] = kind
        change['order_type'] = 'ask' if bool(int(
            change['order_type'])) else 'bid'

        return simpleschema.remap(change, {
            'datetime': 'timestamp',
            'order_type': 'direction'
        })

    def hook_listen(self, method_name, listener, is_first=False):
        methods = {'trade', 'orderbook', 'orderchange'}
        if is_first and method_name in methods:
            if method_name == 'trade':
                self.trade_channel = self._pusher.subscribe('live_trades')
                self.trade_channel.bind('trade', self.trade)
            elif method_name == 'orderbook':
                self.orderbook_channel = self._pusher.subscribe('order_book',
                                                                json_data=True)
                self.orderbook_channel.bind('data', self.orderbook)
            elif method_name == 'orderchange':
                self.orderchange_channel = self._pusher.subscribe(
                    'live_orders', json_data=True)
                self.orderchange_channel.bind_all(self.orderchange)
Example #4
0
class BitstampWebsocketAPI2(Observable):
    APP_KEY = "de504dc5763aeef9ff52"  # Bitstamp's Pusher API key

    def __init__(self):
        super(BitstampWebsocketAPI2, self).__init__()

        # Pusher channels for live trades, order book, and live orders (order changes)
        self.trade_channel = None
        self.orderbook_channel = None
        self.orderchange_channel = None

        # todo consider moving back out to class variable...
        self._pusher = Pusher(BitstampWebsocketAPI2.APP_KEY)

        # moving this back to being an instance variable, instead of class.
        # Initializing _pusher in class (where =None) instead of init makes it blow up spectacularly.
        #if not self._pusher:
        #    BitstampWebsocketAPI2._pusher = twistedpusher.Client(BitstampWebsocketAPI2.APP_KEY)
        #    pass

    # todo switch from time.time to utc
    @simpleschema.returns(schemas.Trade)
    def trade(self, event):
        data = json.loads(event.data, parse_float=Decimal)
        data['timestamp'] = time.time()
        return data

    @simpleschema.returns(schemas.OrderBook)
    def orderbook(self, event):
        book = dict()
        book['bids'] = [{'price': pr, 'amount': amt} for pr, amt in event.data['bids']]
        book['asks'] = [{'price': pr, 'amount': amt} for pr, amt in event.data['bids']]

        return book

    @simpleschema.returns(schemas.BitstampOrderChange)
    def orderchange(self, event):
        change = event.data
        if event.name == 'order_created':
            kind = 'created'
        elif event.name == 'order_deleted':
            kind = 'deleted'
        elif event.name == 'order_changed':
            kind = 'changed'
        else:
            return

        change['change'] = kind
        change['order_type'] = 'ask' if bool(int(change['order_type'])) else 'bid'

        return simpleschema.remap(change, {'datetime': 'timestamp', 'order_type': 'direction'})

    def hook_listen(self, method_name, listener, is_first=False):
        methods = {'trade', 'orderbook', 'orderchange'}
        if is_first and method_name in methods:
            if method_name == 'trade':
                self.trade_channel = self._pusher.subscribe('live_trades')
                self.trade_channel.bind('trade', self.trade)
            elif method_name == 'orderbook':
                self.orderbook_channel = self._pusher.subscribe('order_book', json_data=True)
                self.orderbook_channel.bind('data', self.orderbook)
            elif method_name == 'orderchange':
                self.orderchange_channel = self._pusher.subscribe('live_orders', json_data=True)
                self.orderchange_channel.bind_all(self.orderchange)
Example #5
0
#!/usr/bin/env python

from __future__ import print_function
from twisted.internet import reactor
from twistedpusher import Pusher

# Start the Pusher client. PusherService can also be used.
client = Pusher(key="de504dc5763aeef9ff52")

# Subscribe to a channel
channel = client.subscribe("live_trades")

# Listen to all events on the channel. If you want to receive
# Pusher events, use the flag ignore_pusher_events=False.
channel.bind_all(lambda event: print("Received an event named {}".format(event.name)))

# You can also receive connection state events.
client.connection.bind("connected", lambda _: print("Connected!"))
# state_change includes all connection state events.
client.connection.bind("state_change", lambda event: print("{} -> {}".format(event.previous, event.current)))

reactor.run()