如何使用WebSocket python-binance获取未结订单?

gj3fmq9x  于 2023-04-21  发布在  Python
关注(0)|答案(2)|浏览(174)

我想立即获得我的订单状态。我查看了下面的文档,但我不知道如何使用WebSocket获取我的未结订单。
https://python-binance.readthedocs.io/en/latest/websockets.html
https://binance-docs.github.io/apidocs/spot/en/#websocket-market-streams
我可以在这个链接上看到我的开放订单,但我想用WebSocket来做。
orders = client.get_open_orders(symbol='BNBBTC')
https://python-binance.readthedocs.io/en/latest/account.html#id6

jucafojl

jucafojl1#

它不是WebSocket的替代品,但我修复了setInterval的问题。
后端app.py

@app.route('/allOrders')
@cross_origin(supports_credentials=True)
def allOrders():
    openOrders = getAllOrders("LITBUSD")
    return jsonify(openOrders) 

def getAllOrders(symbol):
    try:
        openOrders =  client.get_all_orders(symbol=symbol)
    except requests.exceptions.ConnectTimeout:
        print("timeout")
        pass 
    return openOrders

js side chart.js

setInterval(function(){ 
    fetch('http://localhost:5000/allOrders')
    .then((r) => r.json())
    .then((response) => {
        console.log(response)       
    })
 }, 3000);

我像这样在index.html中添加引用。

<script src="{{url_for('static', filename='chart.js')}}"></script>
8zzbczxx

8zzbczxx2#

安装Unicorn Binance WebSocket API:

pip install unicorn-binance-websocket-api

如何通过Binance WebSocket API查询所有未结订单的完整示例:

from unicorn_binance_websocket_api.manager import BinanceWebSocketApiManager
import asyncio
import logging
import os

api_key = ""
api_secret = ""

async def binance_api_stream(ubwa):
    def handle_socket_message(data):
        print(f"received data:\r\n{data}\r\n")

    ubwa.create_stream(api=True, api_key=api_key, api_secret=api_secret,
                       process_stream_data=handle_socket_message)
    ubwa.api.get_open_orders(symbol="BNBBTC")
    ubwa.stop_manager_with_all_streams()

if __name__ == "__main__":
    logging.getLogger("unicorn_binance_websocket_api")
    logging.basicConfig(level=logging.DEBUG,
                        filename=os.path.basename(__file__) + '.log',
                        format="{asctime} [{levelname:8}] {process} {thread} {module}: {message}",
                        style="{")

    ubwa = BinanceWebSocketApiManager(exchange='binance.com')

    try:
        asyncio.run(binance_api_stream(ubwa))
    except KeyboardInterrupt:
        print("\r\nGracefully stopping the websocket manager...")
        ubwa.stop_manager_with_all_streams()

更多信息:https://medium.lucit.tech/create-and-cancel-orders-via-websocket-on-binance-7f828831404

相关问题