I used this code: import time from binance.client import Client from binance.enums…
- User
I used this code: import time from binance.client import Client from binance.enums import * from binance.exceptions import BinanceAPIException from binance.helpers import round_step_size import pandas as pd import json import numpy as np import pytz import datetime as dt import ccxt from decimal import Decimal import requests import hmac import hashlib import ntplib import os import ta API_KEY = '' API_SECRET = ''
client = Client(API_KEY, API_SECRET)
Set the endpoint and parameters for the request
url = "https://fapi.binance.com/fapi/v2/account" recv_window = 10000 # update the recv_window value
params = { "timestamp": int(time.time() * 1000), "recvWindow": recv_window }
Get the Binance server time and calculate the time difference
async def get_server_time(): client = await Client.create("","") server_time = client.time()['serverTime'] local_time = int(time.time() * 1000) time_difference = server_time - local_time return time_difference time_difference = get_server_time()
Adjust the timestamp to synchronize with the Binance server time
params = { "timestamp": int(time.time() * 1000) + time_difference, "recvWindow": recv_window }
Sign the message using the Client’s secret key
message = "&".join([f"{k}={v}" for k, v in params.items()]) signature = hmac.new(API_SECRET.encode(), message.encode(), hashlib.sha256).hexdigest() params['signature'] = signature leverage = 100
Send the request using the requests library
response = requests.get(url, params=params, headers={'X-MBX-APIKEY': API_KEY}) account_info = response.json()
Get the USDT balance and calculate the max trade size based on the leverage
try: usdt_balance = next((item for item in account_info['accountBalance'] if item["asset"] == "USDT"), {"free": 0})['free'] except KeyError: usdt_balance = 0 print("Error: Could not retrieve USDT balance from API response.")
max_trade_size = float(usdt_balance) * leverage
Get the current time and timestamp
now = dt.datetime.now() date = now.strftime("%m/%d/%Y %H:%M:%S") print(date)
STOP_LOSS_PERCENTAGE = -50 TAKE_PROFIT_PERCENTAGE = 100 MAX_TRADE_QUANTITY_PERCENTAGE = 100 POSITION_SIDE_SHORT = 'SELL' POSITION_SIDE_LONG = 'BUY' quantity = 1 symbol = 'BTC/USDT' order_type = 'market' leverage = 100 max_trade_quantity_percentage = 1 binance_futures = ccxt.binance({ 'apiKey': API_KEY, 'secret': API_SECRET, 'enableRateLimit': True, # enable rate limitation 'options': { 'defaultType': 'future', 'adjustForTimeDifference': True }, 'future': { 'sideEffectType': 'MARGIN_BUY', # MARGIN_BUY, AUTO_REPAY, etc… } })
Load the market symbols
params = { "timestamp": int(time.time() * 1000) + time_difference, "recvWindow": recv_window } try: markets = binance_futures.load_markets() except ccxt.BaseError as e: print(f'Error fetching markets: {e}') markets = []
- Assistant
The error is occurring because
get_server_time()returns a coroutine, but the result is not being awaited before adding it to an integer value. To fix the issue, you need to add anawaitkeyword before callingget_server_time()on line 44:time_difference = await get_server_time()This will ensure that the result of the coroutine is awaited and ready to be added to the integer value.
- User
But it will be ERROR
- Assistant