-1

I'm trying to improuve my python script on the Binance API. Now I have seen that if I get the price of a symbol with the API "get_ticker" and then try to buy or sell at market price, the prices are very different. The difference is about 1/2%.

Is there another API to get the market price?

Chris
  • 15,819
  • 3
  • 24
  • 37
Franz
  • 3
  • 1
  • 1
    Does this answer your question? [How to get market-price using binance api](https://stackoverflow.com/questions/67657123/how-to-get-market-price-using-binance-api) – Chris Dec 29 '21 at 15:07

1 Answers1

0

It's because get_ticker method gives you the 24 hour price change statistics.

Slow solution: You can use get_kline and take the last close price.

Fast solution: Use websocket-client package and subscribe to the aggregate trade stream which is real time

import websocket
import threading
import json

symbol = 'ETHUSDT'

def process_msg_stream(*args):
    msg = json.loads(args[1])
    print("Last price = ", str(msg['p']))

ws = websocket.WebSocketApp("".join(['wss://stream.binance.com:9443/ws/', symbol.lower(), '@aggTrade']), on_message=process_msg_stream)

threading.Thread(target=ws.run_forever, daemon=True).start()
Rom
  • 192
  • 1
  • 5
  • it works fine. is there a way to have more than one stream at the same time? – Franz Dec 30 '21 at 14:48
  • Yes you can combine the streams, take a look at the doc : https://github.com/binance/binance-spot-api-docs/blob/master/web-socket-streams.md – Rom Dec 30 '21 at 18:22