Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Binance support #1

Open
banteg opened this issue Nov 22, 2017 · 0 comments
Open

Binance support #1

banteg opened this issue Nov 22, 2017 · 0 comments

Comments

@banteg
Copy link

banteg commented Nov 22, 2017

I made a rough prototype that works with websockets, let's try to integrate it.

import websockets
import asyncio
import json
import requests
import sys


def get_listen_key(api_key):
    print('getting listen key')
    url = 'https://api.binance.com/api/v1/userDataStream'
    resp = requests.post(url, headers={'X-MBX-APIKEY': api_key})
    return resp.json()['listenKey']


async def binance_notify(listen_key):
    async with websockets.connect(f'wss://stream.binance.com:9443/ws/{listen_key}') as ws:
        print('connected to websocket')
        while True:
            msg = await ws.recv()
            data = json.loads(msg)
            if data['e'] == 'executionReport':
                pair = data['s']
                kind = data['S'].lower()
                price = data['p']
                amount = data['q']
                state = data['X'].lower()
                print(kind, amount, pair, price, state)


if __name__ == '__main__':
    listen_key = get_listen_key(sys.argv[1])
    loop = asyncio.get_event_loop()
    loop.run_until_complete(binance_notify(listen_key))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant