Hello, I have an issue with my user_socket and trying to find solution since three days…
I use Python Binance and asyncIO for parallel processing.
I’m expecting from my program:
- sending an order via order_market_buy() (ok, tested via get_my_trades())
- response from user_socket() after change in balance. (not ok, receiving nothing)
Any suggestions for my following code?
import asyncio
from binance import AsyncClient, BinanceSocketManager
import os
import datetime
api_key = os.environ.get('binance_api_key_test')
api_secret = os.environ.get('binance_api_secret_test')
async def user_socket(client):
bm = BinanceSocketManager(client)
ts = bm.user_socket()
async with ts as tscm:
while True:
res = await tscm.recv()
print("user socket: ", res)
async def set_order_market_buy(symbol, quantity, client):
await asyncio.sleep(5) # waiting user_socket
await client.order_market_buy(
symbol=symbol,
quantity=quantity)
result = await client.get_my_trades(symbol=symbol, limit=1)
print("last order: ", datetime.datetime.fromtimestamp(result[0]['time']/1000)) # converts trade time in normal date
await client.close_connection()
async def aux():
client = await AsyncClient.create(api_key, api_secret)
client.API_URL = 'https://testnet.binance.vision/api'
user_socket2 = asyncio.create_task(user_socket(client=client))
task_order = asyncio.create_task(set_order_market_buy(symbol='BTCUSDT', quantity=0.001, client=client))
await user_socket2
await task_order
if __name__ == "__main__":
asyncio.run(aux())
Peace and thanks for reading