all gm
all gm

Reputation: 1

How to get asset from dictionary on "Bingx" in Python

I wrote "def" that it fetch contracts like below:

instruments =send_request(.......)
contractdict = dict()        
if instruments is not None:
   for s in instruments['data']:          
       contractdict [s['symbol']] = Contract(s, "bingx")

And it works correctly.

Now I write another "def" like this, but I want to fetch 'asset', I put it below:

margindata =send_request(.......)
balancedic = dict()
if margindata is not None:            
   for b in margindata ['balance']:
       balancedic [b['asset']] = balance(b, "bingx")

But, it has type error!

TypeError: string indices must be integers, not 'str'

Send_request in both the above functions will return the correct answer.

I also put the reference link below:

text

I also test to change 'key' name such as 'asset','data','balance', but they are wrong too. Please tell me how to fetch every fields of margindata dictionary like instruments. ***I not used any library like ccxt,... .

Upvotes: 0

Views: 338

Answers (2)

Amirhossein Zare
Amirhossein Zare

Reputation: 23

So what's happening here is that "b" that is being given to the "Balance" class is a string and it is not a json object. So your class cannot initialize correctly.

Here is how you can fix this using py-bingx:

from bingx.api import BingxAPI

bingx = BingxAPI(API_KEY, SECRET_KEY, timestamp="local")

class Balance(dict):
    """
    Returns a python dict object
    """
    def __init__(self, data, exchange):
        if exchange == "bingx":
            balance_data = data['data']['balance']
            my_dict = {
                'balance': float(balance_data['balance']),
                'equity': float(balance_data['equity']),
                'unrealizedProfit': float(balance_data['unrealizedProfit']),
                'realizedProfit': float(balance_data['realisedProfit']),
                'availableMargin': float(balance_data['availableMargin']),
                'usedMargin': float(balance_data['usedMargin']),
                'freezedMargin': float(balance_data['freezedMargin'])
            }
            super().__init__(my_dict)


data = bingx.get_perpetual_balance()
if data is not None:
    balancedic = Balance(data, "bingx")
    print(balancedic)
    print(balancedic['balance'])

Output:

{'balance': '0.0000', 'equity': '0.0000', 'unrealizedProfit': '0.0000', 'realizedProfit': '0.0000', 'availableMargin': '0.0000', 'usedMargin': '0.0000', 'freezedMargin': '0.0000'}
0.0000

Hope it helps!

Upvotes: 0

Amirhossein Zare
Amirhossein Zare

Reputation: 23

You are sending a request to /openApi/swap/v2/user/balance endpoint. Which returns a json response that looks like this:

response = {"code":0,"msg":"","data":{"balance":{"userId":"12345","asset":"USDT","balance":"0.0000","equity":"0.0000","unrealizedProfit":"0.0000","realisedProfit":"0.0000","availableMargin":"0.0000","usedMargin":"0.0000","freezedMargin":"0.0000"}}}

Please note that if you are trading perpetual futures, then opening a position would not add that symbol to your perpetual balance.

I suppose this would solve your issue:

balancedic = dict()
if response is not None:            
    for b in response["data"]["balance"]:
        balancedic[b["asset"]] = balance(b, "bingx")

Upvotes: 0

Related Questions