Binance Websocket, Order Book, and Candlestick Data (Python Scripts) (2024)

Binance is widely known as the largest exchange in the crypto market. In terms of trading volume, there are no other exchanges that come close to the massive volume that is executed every day on Binance.

The impressive liquidity on Binance provides the perfect platform for traders, institutions, and investors to aggregate. Paired with their low trading fees, there are few, if any, exchanges that are better for day trading.

Unfortunately, many people are still having difficulties when it comes to collecting the necessary data for analyzing trading strategies, backtesting, and portfolio analysis. This article will provide an efficient and cost-effective way for anyone to access comprehensive live and historical market data from Binance.

Using Python, we will be able to collect and store any data we want. Each script will break down a different piece of data that may be interesting for you.

Don’t forget to join the Developer Telegram Group that is dedicated to builders who are creating exciting applications that use data from exchanges. We would love to hear what you’re building and help you get started!

This section will set up the libraries and APIs that we will need in order to begin collecting Binance exchange data.

Installing Shrimpy Python

Start by installing the Shrimpy Python Library.

pip install shrimpy-python

Shrimpy API Keys

Once the Shrimpy Python Library is installed, we will need to create a set of Shrimpy API Master Keys. These keys will be used to access the data.

Sign up for the Shrimpy Developer APIs, then follow the guide here to access your Master Keys.

Note: We will need to enable “Data” permissions on the API Master Keys before we can run these scripts. No other permissions are required for these scripts.

In each example script, you will see two lines that look like the following. This is where you will assign your own public and secret API keys that you generated in your Shrimpy developer account.

shrimpy_public_key = '...'shrimpy_secret_key = '...'

With Shrimpy, there are two ways to collect live data. The options are either through the REST APIs or the websockets. Both of these methods will use slightly different scripts to access the data. They will also change the way you collect and process the data.

Note: The Live REST APIs are free to access without a premium subscription. However, the websockets require a “Personal” or “Startup” subscription plan to access these endpoints. Since we won’t be using trading endpoints in these examples, we won’t be needing a “User” / “Trading” plan.

REST API Market Data Endpoints

As discussed, the scripts in this section do not require a paid premium subscription. You can copy any of these scripts and start using them immediately.

Available Assets

Using the get_exchange_assets endpoint, we can retrieve the assets that are available on Binance.

import shrimpy# Assign public and secret Shrimpy Master API Keysshrimpy_public_key = '...'shrimpy_secret_key = '...'# Create the Shrimpy REST API clientclient = shrimpy.ShrimpyApiClient(shrimpy_public_key, shrimpy_secret_key)exchange_assets = client.get_exchange_assets('binance')

Available Trading Pairs

Using the get_trading_pairs endpoint, we can get access to the available trading pairs on Binance.

import shrimpy# Assign public and secret Shrimpy Master API Keysshrimpy_public_key = '...'shrimpy_secret_key = '...'# Create the Shrimpy REST API clientclient = shrimpy.ShrimpyApiClient(shrimpy_public_key, shrimpy_secret_key)trading_pairs = client.get_trading_pairs('binance')

Simple Price Ticker

The following example will provide a simple price ticker. The ticker will use the REST API call get_ticker to collect the asset-specific pricing data on Binance.

The ticker endpoint updates on a 1-minute interval.

import shrimpy# Assign public and secret Shrimpy Master API Keysshrimpy_public_key = '...'shrimpy_secret_key = '...'# Create the Shrimpy REST API clientclient = shrimpy.ShrimpyApiClient(shrimpy_public_key, shrimpy_secret_key)# Request the ticker for all assets on Binanceticker = client.get_ticker('binance')

Simple Live Order Book Snapshot

In this example, we will access the live state of the Binance order book by calling the REST API endpoint for get_order_books.

import shrimpyshrimpy_public_key = '...'shrimpy_secret_key = '...'# Create the API client for REST callsclient = shrimpy.ShrimpyApiClient(shrimpy_public_key, shrimpy_secret_key)# Retrieve the live order bookorderbooks = client.get_orderbooks( 'binance', # exchange 'ETH', # base_symbol 'BTC', # quote_symbol 10 # limit)

Simple Live Candlesticks

The next market data endpoint provides access to trade candlesticks through the get_candles endpoint. The candlesticks are updated in real-time as new trades are executed on the exchange. The following script will only return up to the last 1,000 candlesticks. To retrieve additional candlestick data, you must use the “historical” candlestick endpoint.

import shrimpyimport plotly.graph_objects as go# sign up for the Shrimpy Developer APIs for your free API keysshrimpy_public_key = '...'shrimpy_secret_key = '...'# collect the historical candlestick dataclient = shrimpy.ShrimpyApiClient(shrimpy_public_key, shrimpy_secret_key)candles = client.get_candles( 'binance', # exchange 'ETH', # base_trading_symbol 'BTC', # quote_trading_symbol '1d' # interval)dates = []open_data = []high_data = []low_data = []close_data = []# format the data to match the plotting libraryfor candle in candles: dates.append(candle['time']) open_data.append(candle['open']) high_data.append(candle['high']) low_data.append(candle['low']) close_data.append(candle['close'])# plot the candlesticksfig = go.Figure(data=[go.Candlestick(x=dates, open=open_data, high=high_data, low=low_data, close=close_data)])fig.show()

Websocket Market Data Endpoints

The next set of example scripts will use the websocket APIs to collect real-time market data from the Binance exchange.

These scripts will require you to subscribe to the “Personal” or “Startup” plans. If you are only planning on using websockets and no historical data, the “Personal” plan is recommended.

Websocket Price Ticker

The following example is a more complex way to implement a price ticker. If real-time data is required for your service, this option would be ideal to ensure the most up-to-date data is used. This option can be a replacement for the “simple ticker” that was previously discussed.

This example uses real-time trade websockets.

import shrimpyshrimpy_public_key = '...'shrimpy_secret_key = '...'# This is a sample handler, it simply prints the incoming message to the consoledef error_handler(err): print(err)# This is a sample handler, it simply prints the incoming message to the consoledef handler(msg): print(msg['content'][0]['price'])# Create the websocket client using the raw token retrieved by the REST APIapi_client = shrimpy.ShrimpyApiClient(shrimpy_public_key, shrimpy_secret_key)raw_token = api_client.get_token()client = shrimpy.ShrimpyWsClient(error_handler, raw_token['token'])# The subscription data for the websocketsubscribe_data = { "type": "subscribe", "exchange": "binance", "pair": "eth-btc", "channel": "trade"}# Start processing the Shrimpy websocket stream!client.connect()client.subscribe(subscribe_data, handler)# Once complete, stop the clientclient.disconnect()

Websocket Live Order Book

Similar to the websocket ticker example, the following example will use websockets to collect the live order book data.

import shrimpyshrimpy_public_key = '...'shrimpy_secret_key = '...'# This is a sample handler, it simply prints the incoming message to the consoledef error_handler(err): print(err)# This is a sample handler, it simply prints the incoming message to the consoledef handler(msg): print(msg)# Create the websocket client by getting the raw token.api_client = shrimpy.ShrimpyApiClient(shrimpy_public_key, shrimpy_secret_key)raw_token = api_client.get_token()ws_client = shrimpy.ShrimpyWsClient(error_handler, raw_token['token'])subscribe_data = { "type": "subscribe", "exchange": "binance", "pair": "eth-btc", "channel": "orderbook"}# Start processing the Shrimpy websocket stream!ws_client.connect()ws_client.subscribe(subscribe_data, handler)# Once complete, stop the clientws_client.disconnect()

This section will highlight examples for how to collect historical market data from Binance. Each example will target a specific type of data that fits your needs.

Calls to the historical data endpoints will require a “Startup” subscription. Since we will only be collecting data, we do not need any user / trading credits. That means you can move the “User Plan” slider to “0 Active Users”. The “Data Plan” slider should be adjusted to the level of historical data you would like to access.

Available Historical Data

The following script will retrieve the currently available list of historical data. That way you can browse the Shrimpy data catalog and determine what data you would like to collect. This will use the get_historical_instruments endpoint.

import shrimpy# sign up for the Shrimpy Developer APIs for your free API keysshrimpy_public_key = '...'shrimpy_secret_key = '...'# collect the historical candlestick dataclient = shrimpy.ShrimpyApiClient(shrimpy_public_key, shrimpy_secret_key)instruments = client.get_historical_instruments('binance')

Historical Candlesticks

Similar in some ways to live candlesticks, the historical candlestick endpoint allows you to collect historical candlestick data. The major difference between this script and the previous candlestick script is the use of the date range, which can contain any time range that is available. We will use the get_historical_candles endpoint for this script.

With this script we are not restricted to only collecting the last 1,000 candles. We can collect 1-minute candlesticks from 2012 or 5-minute candlesticks from 2015 (note: Binance data does not go back that far, but other exchanges do have data that goes back all the way to 2011). Any data you want is available.

import shrimpyimport plotly.graph_objects as go# sign up for the Shrimpy Developer APIs for your free API keyspublic_key = '...'secret_key = '...'client = shrimpy.ShrimpyApiClient(public_key, secret_key)candles = client.get_historical_candles( 'binance', # exchange 'ETH', # base_trading_symbol 'BTC', # quote_trading_symbol '2017-02-11T00:00:00.000Z', # start_time '2019-10-20T00:00:00.000Z', # end_time 1000, # num_candles '1d' # interval)dates = []open_data = []high_data = []low_data = []close_data = []for candle in candles: dates.append(candle['time']) open_data.append(candle['open']) high_data.append(candle['high']) low_data.append(candle['low']) close_data.append(candle['close'])fig = go.Figure(data=[go.Candlestick(x=dates, open=open_data, high=high_data, low=low_data, close=close_data)])fig.show()

Historical Trades

The following script will demonstrate how to collect tick-by-tick trade data for any available trading pair on Binance. This script uses the get_historical_trades endpoint.

import shrimpy# sign up for the Shrimpy Developer APIs for your free API keyspublic_key = '...'secret_key = '...'client = shrimpy.ShrimpyApiClient(public_key, secret_key)trades = client.get_historical_trades( 'binance', 'ETH', 'BTC', '2019-05-19T00:00:00.000Z', '2019-05-20T00:00:00.000Z', 100)

Historical Order Book Snapshots

Our final script will use the get_historical_orderbooks endpoint to collect 1-minute order book snapshots.

import shrimpy# sign up for the Shrimpy Developer APIs for your free API keyspublic_key = '...'secret_key = '...'client = shrimpy.ShrimpyApiClient(public_key, secret_key)orderbooks = client.get_historical_orderbooks( 'binance', 'ETH', 'BTC', '2019-05-19T00:00:00.000Z', '2019-05-20T00:00:00.000Z', 100)

Shrimpy is an account aggregating platform for cryptocurrency. It is designed for both professional and novice traders to come and learn about the growing crypto industry. Trade with ease, track your performance, and analyze the market. Shrimpy is the trusted platform for trading over $13B in digital assets.

Shrimpy’s Universal Crypto Exchange APIs are designed for developers. Integrating with our unified APIs gives you instant access to uniform endpoints for trading, data collection, user management, and more across every major cryptocurrency exchange.

To access the complete Python and Node libraries, follow these links:

Node

Python

Follow us on Twitter and Facebook for updates, and ask any questions to our amazing, active communities on Telegram & Discord.

Thanks for stopping by!

The Shrimpy Team

Additional Good Reads

Arbitrage Scripts for Crypto Trading Bots

Python Scripts for Crypto Trading Bots

Script for Bitcoin Price Live Ticker (Using Websockets)

Python Scripts for Cryptocurrency Price Charts

Binance Websocket, Order Book, and Candlestick Data (Python Scripts) (2024)
Top Articles
Latest Posts
Article information

Author: Ouida Strosin DO

Last Updated:

Views: 5678

Rating: 4.6 / 5 (76 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Ouida Strosin DO

Birthday: 1995-04-27

Address: Suite 927 930 Kilback Radial, Candidaville, TN 87795

Phone: +8561498978366

Job: Legacy Manufacturing Specialist

Hobby: Singing, Mountain biking, Water sports, Water sports, Taxidermy, Polo, Pet

Introduction: My name is Ouida Strosin DO, I am a precious, combative, spotless, modern, spotless, beautiful, precious person who loves writing and wants to share my knowledge and understanding with you.