Binance API [Python] How do I get positions in Binance using Binance Connector API?

27 views Asked by At

I already read the examples and can't find anything. I know I have open positions in Binance, but for some reason it doesn't output in the terminal when I try to run it.

Current code :

#!/usr/bin/env python
import logging
from binance.um_futures import UMFutures
from binance.lib.utils import config_logging
from binance.error import ClientError

# config_logging(logging, logging.DEBUG)

um_futures_client = UMFutures()

# get server time
print(um_futures_client.time())

um_futures_client = UMFutures(key='xxx', secret='yyy')

# Get account information
account_info = um_futures_client.account()

# Iterate through the assets
for asset in account_info['assets']:
    if float(asset['walletBalance']) > 0:
        print(f"Asset: {asset['asset']}, Wallet Balance: {asset['walletBalance']}")
        asset_usdt = float(asset['walletBalance'])  # Convert to float

This is what ChatGPT assist me so far, but output was nothing.

try:
    response = um_futures_client.account(recvWindow=6000)
    for asset in response.get('assets', []):
        if 'symbol' in asset and 'positionSide' in asset and 'unrealizedProfit' in asset:
            if float(asset.get('unrealizedProfit', 0)) != 0:
                print(f"Symbol: {asset['symbol']}, Position Side: {asset['positionSide']}, Unrealized Profit: {asset.get('unrealizedProfit', 0)}")


except ClientError as error:
    logging.error(
        "Found error. status: {}, error code: {}, error message: {}".format(
            error.status_code, error.error_code, error.error_message
        )
    )
0

There are 0 answers