Back to Ccxt

https://github.com/ccxt/ccxt/blob/master/CONTRIBUTING.md#how-to-contribute-code

wiki/examples/py/binance-fetch-balance-snapshot-watch-balance-updates.md

4.5.561.8 KB
Original Source
python
import os
import sys

# PLEASE DO NOT EDIT THIS FILE, IT IS GENERATED AND WILL BE OVERWRITTEN:
# https://github.com/ccxt/ccxt/blob/master/CONTRIBUTING.md#how-to-contribute-code
import asyncio
import ccxt.pro as ccxt  # noqa: E402


# AUTO-TRANSPILE #
'use strict'


print('CCXT Version:', ccxt.version)  # eslint-disable-line import/no-named-as-default-member


# This example will run silent and will return your balance only when the balance is updated.
#
# 1. launch the example with your keys and keep it running
# 2. go to the trading section on the website
# 3. place a order on a spot market
# 4. see your balance updated in the example
#
# Warning! This example might produce a lot of output to your screen
async def watch_balance(exchange):
   balance = await exchange.fetch_balance()
   print('------------- Initial -------------')
   print(exchange.iso8601(exchange.milliseconds()))
   print(balance)
   while True:
       try:
           update = await exchange.watch_balance()
           balance = exchange.deep_extend(balance, update)
           # it will print the balance update when the balance changes
           # if the balance remains unchanged the exchange will not send it
           print('------------- Update -------------')
           print(exchange.iso8601(exchange.milliseconds()))
           print(balance)
       except Exception as e:
           print(e)
           break


async def main():
   exchange = ccxt.binance({
       'apiKey': 'YOUR_API_KEY',
       'secret': 'YOUR_SECRET',
   })
   await exchange.load_markets()
   # exchange.verbose = true # uncomment for debugging purposes if necessary
       await exchange.close()

asyncio.run(watch_balance(exchange))
   await exchange.close()


main()