LUCIT-Systems-and-Development / unicorn-binance-websocket-api

A Python SDK by LUCIT to use the Binance Websocket API`s (com+testnet, com-margin+testnet, com-isolated_margin+testnet, com-futures+testnet, com-coin_futures, us, tr, dex/chain+testnet) in a simple, fast, flexible, robust and fully-featured way.
https://unicorn-binance-websocket-api.docs.lucit.tech/
Other
683 stars 165 forks source link

Send requests to Binance API via websockets #319

Open oliver-zehentleitner opened 1 year ago

oliver-zehentleitner commented 1 year ago

Binance has added the ability to send requests to the Binance API directly via websocket: https://developers.binance.com/docs/binance-trading-api/websocket_api

I have started to add support for this in the UNICORN Binance WebSocket API.

I have added the following functions:

The implementation works, but is not yet complete. There are a lot of functions missing... will do that in the next time.

How To: https://medium.lucit.tech/create-and-cancel-orders-via-websocket-on-binance-7f828831404

Docs: https://unicorn-binance-websocket-api.docs.lucit.tech/unicorn_binance_websocket_api.html#unicorn-binance-websocket-api-ws-api-module

Todo:

oliver-zehentleitner commented 1 year ago

https://github.com/LUCIT-Systems-and-Development/unicorn-binance-websocket-api/discussions/320

derWahnsinn commented 1 year ago

Thank you very much for your code update!

When sending a new create_order with type "MARKET" , "timeInForce" and "price" need to be removed from posting to the endpoint, otherwise the order will not get submitted.

oliver-zehentleitner commented 1 year ago

Thank you very much for your code update!

When sending a new create_order with type "MARKET" , "timeInForce" and "price" need to be removed from posting to the endpoint, otherwise the order will not get submitted.

Right, thank you! I will fix that!

Beste Grüße ;)

Animouse31 commented 1 year ago

@oliver-zehentleitner

Could you give me the code example on how to use it? Like sending a new order, get account information, etc.

I wonder if using websocket API is better than rest API?

oliver-zehentleitner commented 1 year ago

@Animouse31 in the post above yours there is a link to medium with an how to... doesnt it help you?

https://github.com/LUCIT-Systems-and-Development/unicorn-binance-websocket-api#place-orders-cancel-orders-or-send-other-requests-via-websocket

oliver-zehentleitner commented 1 year ago

what do you think about the idea of being able to pass a callback function to each request that is executed as soon as the response is received from binance?

oliver-zehentleitner commented 1 year ago

I updatd the implementation, its released with 1.46.0. I think the code structure/layout is ok now. If tests are fine I will start adding more request types.

oliver-zehentleitner commented 1 year ago

Updated the how to: https://medium.lucit.tech/create-and-cancel-orders-via-websocket-on-binance-7f828831404

moloshniy commented 1 year ago

For LIMIT_MAKER timeInForce should be removed 'api.py line 648 ' side can only be SELL, BUY if side.upper() == "LIMIT": params['price'] = str(price) params['timeInForce'] = time_in_force

api.py line 1191
if symbol is None: params = {"symbol": symbol} if symbols is None: symbols = [symbol.upper() for symbol in symbols] params = {"symbols": symbols} if permissions is None: params = {"permissions": permissions}

seems like should be is not None

Beside, Great Job !!!

fabriken commented 1 year ago

order = ws.api.create_order(order_type='MARKET', quantity=qty, side='SELL', symbol=symbol) "code":-1106,"msg":"Parameter 'timeInForce' sent when not required." Gives the timeInForce error described earlier. Was excited to try this out today, but turns out this issue has not been addressed yet. Any progress on making this work, or perhaps a quick fix I can try myself? Cheers

oliver-zehentleitner commented 1 year ago

Yes, There will be a new release in mid of July!

We are restructuring to be able to deliver updates quickly again in the future.

Assemblage-Devel commented 11 months ago

I am getting the following error using the requests:

connection.py", line 138, in __aenter__ self._conn = connect(uri, raise exceptions.InvalidURI(uri, "scheme isn't ws or wss") websockets.exceptions.InvalidURI: isn't a valid URI: scheme isn't ws or wss

My code is the basic ping test:

` def soc_message(data): print(f"receive data: \r\n {data}\r\n")

bwam = BinanceWebSocketApiManager( exchange='binance.com-futures', process_stream_data=soc_message, output_default='UnicornFy' )

bwam.create_stream( api=True, api_key=API, api_secret=SECRET, stream_label='trader_1', stream_buffer_name='trader_1' )

while True: bwam.api.ping() time.sleep(10) `

Have tried this with numerous virtual envs is there a solution to this problem? Thanks in advance Oliver for the fantastic work your doing!

oliver-zehentleitner commented 11 months ago

I am getting the following error using the requests:

connection.py", line 138, in __aenter__ self._conn = connect(uri, raise exceptions.InvalidURI(uri, "scheme isn't ws or wss") websockets.exceptions.InvalidURI: isn't a valid URI: scheme isn't ws or wss

My code is the basic ping test:

` def soc_message(data): print(f"receive data: \r\n {data}\r\n")

bwam = BinanceWebSocketApiManager( exchange='binance.com-futures', process_stream_data=soc_message, output_default='UnicornFy' )

bwam.create_stream( api=True, api_key=API, api_secret=SECRET, stream_label='trader_1', stream_buffer_name='trader_1' )

while True: bwam.api.ping() time.sleep(10) `

Have tried this with numerous virtual envs is there a solution to this problem? Thanks in advance Oliver for the fantastic work your doing!

At the time I implemented websocket api, it was only available for binance.com spot+testnet. Futures was not available from Binance side at that time...

oliver-zehentleitner commented 6 months ago

Screenshot_20230808_194805_Telegram

oliver-zehentleitner commented 1 month ago

Make WS API available for Binance Futures: https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-api-general-info