How to get the order book list with Binance API?
Asked Answered
O

2

9

I would like to get the same information (opened orders) as displayed in order book on Binance site here:

enter image description here

I tried:

/api/v3/allOrders -- this apparently shows all MY orders

api/v3/openOrders -- this apparently shows opened MY orders

and

/api/v3/trades -- this apparently shows closed orders

How to see OPENED EVERYBODY'S orders?

Overijssel answered 1/11, 2021 at 18:7 Comment(1)
How you are getting the Total? And are you showing asks or bids?Gan
B
14

You're looking for the Order Book endpoint.

Docs: https://binance-docs.github.io/apidocs/spot/en/#order-book

Example: https://api.binance.com/api/v3/depth?limit=10&symbol=BTCUSDT

Build answered 1/11, 2021 at 19:23 Comment(6)
How can we get the whole order book without limiting it to a number?Glaucous
@OmidEbrahimi Based on the linked docs page, the maximal limit is 5000, and there's no offset. There currently doesn't seem to be a way to retrieve records past the 5000.Build
how can I get the historical data of the orderbook? like from 2024-01-01 to 2024-03-01? thanksInnings
@XuShan No, the API doesn't seem to support historical orderbook data.Build
Thanks for your reply @PetrHejda, then how can I get a historical orderbook data? ideally from one time point to another. Thanks.Innings
@XuShan Possibly some 3rd party that has collected them in the past. Unfortunately I don't know of any source that would provide them.Build
I
0

You can use a local depth cache to access a current Binance order book in Python at high frequency. https://github.com/LUCIT-Systems-and-Development/unicorn-binance-local-depth-cache

from unicorn_binance_local_depth_cache import BinanceLocalDepthCacheManager, DepthCacheOutOfSync
import asyncio
import logging
import os

exchange: str = "binance.com"
markets: list = ['BTCUSDT', 'ETHUSDT', 'SOLUSDT']
limit_count: int = 4

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


async def main():
    ubra = ubldc.get_ubra_manager()

    print(f"Starting {exchange} DepthCaches for {len(markets)} markets: {markets}")
    ubldc.create_depth_cache(markets=markets)

    while ubldc.is_stop_request() is False:
        add_string = (f"binance_api_status={ubra.get_used_weight(cached=True)}\r\n "
                      f"---------------------------------------------------------------------------------------------")
        for market in markets:
            try:
                top_asks = ubldc.get_asks(market=market, limit_count=limit_count)
                top_bids = ubldc.get_bids(market=market, limit_count=limit_count)
            except DepthCacheOutOfSync:
                top_asks = "Out of sync!"
                top_bids = "Out of sync!"
            depth = (f"depth_cache '{market}' is in sync: {ubldc.is_depth_cache_synchronized(market=market)}\r\n " 
                     f" - top {limit_count} asks: {top_asks}\r\n "
                     f" - top {limit_count} bids: {top_bids}")
            add_string = f"{add_string}\r\n {depth}"

        ubldc.print_summary(add_string=add_string)
        await asyncio.sleep(1)


with BinanceLocalDepthCacheManager(exchange=exchange) as ubldc:
    try:
        asyncio.run(main())
    except KeyboardInterrupt:
        print("\r\nGracefully stopping ...")
    except Exception as e:
        print(f"\r\nERROR: {e}")
        print("Gracefully stopping ...")
Isopropanol answered 18/5 at 9:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.