pythonbinancecryptocurrencyccxt

How do I print a single symbol with python fetch_balance() CCXT?


I am able to pull a balance of all symbols but how do I print just a single symbol balance? Here is the code I already have working just not sure how to get say USDT to print a balance.

import ccxt
import config

binanceus = ccxt.binanceus({
'apiKey': config.API_KEY,
'secret': config.API_SECRET

})

def trade_crypto(request):
    balance = binanceus.fetch_balance()
    
    print(balance)

    return {
        "balance": list(balance.keys())
    }

I am trying to print USDT just not sure of the syntax needed. Thanks!


Solution

  • Basically, the code in the question works or at least i could get it to work with the binance website. (i dont have binanceus but the cctx library seems to offer more than just one vendor).

    Also, note that you don't need curly braces on the return statement in python.

    So i did this to verify:

    import ccxt
    
    import sys
    sys.path.append('G:\\path\\to\\creds\\python\\binance')
    import creds
    
    binance = ccxt.binance({
    'apiKey': creds.API_Key,
    'secret': creds.Secret_Key
    
    })
    
    def trade_crypto():
        balance = binance.fetch_balance()
        
        # print(balance)
    
        return balance
    
    x = trade_crypto()
    print(x)
    

    The above returns a python dictionary.

    From this, you can get the values that you seek.

    It looks like you used this stack question for the answer: How can I get list of values from dict?

    But the dictionary has nested dictionaries and lists, so you need to understand the structure to achieve what you want.

    Whilst i dont know the exact structure, the answer will generically look like this:

    nested_dict = {
        'key1': {
            'key2': 'value'
        }
    }
    
    # Get the value from the nested dictionary
    value = nested_dict['key1']['key2']
    print(value)  # Output: "value"
    

    You need to post the dict for someone to clearly help if you are stuck there...

    here is a useful python book for a newb: https://www.amazon.co.uk/dp/B0BHL2XKCR