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

No response from Futures User Socket (testnet) #1248

Open kasahh opened 2 years ago

kasahh commented 2 years ago

Describe the bug I want to get the account updates for things like position open/close, order open/close and balance stream. After running the code below, I get no response even after I create orders and open position on my testnet account.

To Reproduce Code snippet to reproduce the behavior:

import asyncio
from binance import AsyncClient, BinanceSocketManager
async def main():
    client = await AsyncClient.create(api_key, api_secret,testnet=True)
    bm = BinanceSocketManager(client)
    ts = bm.futures_user_socket()
    try:
        async with ts as tscm:
            while True:
                res = await tscm.recv()
                print(res)
    except KeyboardInterrupt:
        await client.close_connection()
    except Exception as e:
        print(e)
        await client.close_connection()
if __name__ == "__main__":
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())

Expected behavior I expected to get some sort of data stream but i get none.

Environment (please complete the following information):

kasahh commented 2 years ago

It works okay when using a live futures market. So I guess it hasn't been implemented for testnet yet.

trpt-nt commented 2 years ago

The issue appears to be the futures_user_socket function.

On line 1067 in streams.py it is passing self.FSTREAM_URL to the _get_account_socket internal function. _get_account_socket in turn passes the stream_url to _get_stream_url which does not process the client's Testnet flag if a stream_url parameter is passed.

I also note that the class variable FSTREAM_TESTNET_URL appears incorrect

[all references to streams.py] 297:

class BinanceSocketManager:
    STREAM_URL = 'wss://stream.binance.{}:9443/'
    STREAM_TESTNET_URL = 'wss://testnet.binance.vision/'
    FSTREAM_URL = 'wss://fstream.binance.{}/'
    **FSTREAM_TESTNET_URL = 'wss://stream.binancefuture.com/'**
    DSTREAM_URL = 'wss://dstream.binance.{}/'
    DSTREAM_TESTNET_URL = 'wss://dstream.binancefuture.com/'
    VSTREAM_URL = 'wss://vstream.binance.{}/'
    VSTREAM_TESTNET_URL = 'wss://testnetws.binanceops.{}/'

I altered lines 301 and 1067 as follows:

301 - Before

FSTREAM_TESTNET_URL = 'wss://stream.binancefuture.com/'

301 - After

FSTREAM_TESTNET_URL = 'wss://testnet.binancefuture.com/'

1067 - Before

return self._get_account_socket('futures', stream_url=self.FSTREAM_URL)

1067 - After

return self._get_account_socket('futures', self.FSTREAM_TESTNET_URL if self.testnet else self.FSTREAM_URL)

These changes have sorted out futures_users_socket on testnet for me and do not appear to have caused regression on mainnet

kasahh commented 2 years ago

The issue appears to be the futures_user_socket function.

On line 1067 in streams.py it is passing self.FSTREAM_URL to the _get_account_socket internal function. _get_account_socket in turn passes the stream_url to _get_stream_url which does not process the client's Testnet flag if a stream_url parameter is passed.

I also note that the class variable FSTREAM_TESTNET_URL appears incorrect

[all references to streams.py] 297:

class BinanceSocketManager:
    STREAM_URL = 'wss://stream.binance.{}:9443/'
    STREAM_TESTNET_URL = 'wss://testnet.binance.vision/'
    FSTREAM_URL = 'wss://fstream.binance.{}/'
    **FSTREAM_TESTNET_URL = 'wss://stream.binancefuture.com/'**
    DSTREAM_URL = 'wss://dstream.binance.{}/'
    DSTREAM_TESTNET_URL = 'wss://dstream.binancefuture.com/'
    VSTREAM_URL = 'wss://vstream.binance.{}/'
    VSTREAM_TESTNET_URL = 'wss://testnetws.binanceops.{}/'

I altered lines 301 and 1067 as follows:

301 - Before

FSTREAM_TESTNET_URL = 'wss://stream.binancefuture.com/'

301 - After

FSTREAM_TESTNET_URL = 'wss://testnet.binancefuture.com/'

1067 - Before

return self._get_account_socket('futures', stream_url=self.FSTREAM_URL)

1067 - After

return self._get_account_socket('futures', self.FSTREAM_TESTNET_URL if self.testnet else self.FSTREAM_URL)

These changes have sorted out futures_users_socket on testnet for me and do not appear to have caused regression on mainnet

Were you able to test it out? It didn't work for me. I also came accross this on binance-docs.github.io

Screenshot 2022-10-05 at 15 19 24
avfawkes commented 1 year ago

These changes have sorted out futures_users_socket on testnet for me and do not appear to have caused regression on mainnet

Thanks @trpt-nt, it works