sammchardy / python-binance

Binance Exchange API python implementation for automated trading
https://python-binance.readthedocs.io/en/latest/
MIT License
6.01k stars 2.2k forks source link

Binance Testnet user_socket() problem. #1388

Open ANWB2020 opened 9 months ago

ANWB2020 commented 9 months ago

Binance Python: 1.0.10 Python: 3.10

Hello,

I have published this issue also on Binance Developer., but didn't get answer.

My issue:

I don't get response from user_socket and do not know if my code is right.

I use Python Binance and asyncIO for parallel processing.

I’m expecting from my program:

What's wrong with my code? Do you have 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())