danpaquin / coinbasepro-python

The unofficial Python client for the Coinbase Pro API
MIT License
1.82k stars 731 forks source link

Example of subscribing to the ticker channel using the websocket client? - update setup.py and publish 1.0.7? #177

Open skyl opened 6 years ago

skyl commented 6 years ago

First I was using 1.0.6 from PyPI. This version didn't have support for channels. So, I pip installed master. But, master is also at 1.0.6 in setup.py. So, it looks like pip will not install because I already have 1.0.6. After pip uninstall gdax and pip install git+https://github.com/danpaquin/gdax-python@master, I am able to subscribe to the ticker channel. It would be nice to publish out what is master now as 1.0.7 and put maybe 1.0.8 in the setup.py and maybe document how to subscribe to the ticker channel.

Thanks for the great library BTW!

skyl commented 6 years ago

FWIW my usage against master looks something like this now:

import json
from channels import Group

import gdax

from xex.gdax.exchange import Gdax

class GdaxTickerSender(gdax.WebsocketClient):
    def on_open(self):
        self.gdax = Gdax()
        self.url = "wss://ws-feed.gdax.com/"
        self.products = [
            m['id'] for m in self.gdax.api.get_products()
        ]
        self.channels = ['ticker']
        self.type = 'ticker'
        self.count = 0
        self.r = {}

    def on_message(self, msg):
        if msg['type'] == 'ticker':
            data = self.gdax.translate_summary(msg)
            market = data['market']
            self.r[market] = data
            self.count += 1
            if self.count > 100:
                # print('broadcast!', self.r)
                Group('ticker').send({
                    'text': json.dumps({'gdax': self.r})
                })
                self.count = 0
                self.r = {}

    def on_close(self):
        print("-- Goodbye! --")

This is not a minimal example and relies on some other code of mine. But, maybe it will help someone. I haven't experimented with the differences between self.type and self.channels. I would guess that setting self.type = 'ticker' is a no-op or something ...