Back to Ccxt

Binance Futures Watch Balance

wiki/examples/py/binance-futures-watch-balance.md

4.5.651.2 KB
Original Source
python
import ccxt
import ccxt.pro
import asyncio
from importlib import import_module
from importlib.util import find_spec

run = import_module(next(filter(find_spec, ('uvloop', 'winloop', 'asyncio')))).run

from pprint import pprint


async def print_balance(exchange, market_type):
    while True:
        try:
            balance = await exchange.watch_balance({'type': market_type})
            pprint(balance)
            print('balance of ' + market_type, balance)
            print(exchange.options[market_type])
        except ccxt.BaseError as e:
            print(type(e), e)
        except Exception as e:
            print(type(e), e)


async def main():
    exchange = ccxt.pro.binance({
        "apiKey": "",
        "secret": "",
        'enableRateLimit': True,
        'newUpdates': True,
    })
    # you must make an order a transfer first to the websocket to send updates
    try:
        await asyncio.gather(
            print_balance(exchange, 'future'),
            print_balance(exchange, 'delivery'),  # inverse futures settled in BTC
            print_balance(exchange, 'spot'),
        )
    finally:
        await exchange.close()


if __name__ == '__main__':
    run(main())