Coinbase Pro Live and Historical Market Data [Example Python Scripts]
Coinbase Pro has become one of the most prominent spots for US traders to engage with the crypto market. This hot spot has attracted countless developers, institutions, and day traders. Unfortunately, many people are still wondering how they can access the live and historical data from Coinbase Pro in an efficient and cost-effective way.
Throughout this tutorial, we will guide you through the process of setting up Python scripts that will provide you access to one of the most complete sets of data available in the market to date.
Don’t forget to join our Developer Telegram Group that is dedicated to builders who are developing applications on top of exchanges. We are always ready to answer your questions and help you get started!
Getting Started
Before we begin collecting exchange data, there are a few things we will need. The following section will provide a quick guide to get you set up and ready to begin implementing your own Coinbase Pro Python scripts.
Installing Shrimpy Python
We will begin by installing the Shrimpy Python Library.
You can do this using Pip. Open your “Command Prompt” and type the following.
pip install shrimpy-python
Shrimpy API Keys
Now that we have the Shrimpy Python Library installed, we can create our Shrimpy API Master Keys that will be used with the library.
Sign up for the Shrimpy Developer APIs, then follow the guide here to access your Master Keys.
In this tutorial, you will only need to enable “Data” permissions on your Master API Keys. All other permissions can remain disabled.
Throughout the following examples, you will notice places where the keys are assigned to variables. These instances will look something like the following.
shrimpy_public_key = '...'
shrimpy_secret_key = '...'
Simply substitute the ‘…’ with your actual Shrimpy Public and Secret Master API Keys.
Exchange Live Data
We will cover two different ways for collecting live data. The first way to collect data is through the REST APIs. In Shrimpy, all of these market endpoints are free to use. There are no fees for accessing these live data resources.
The second way we will cover for collecting live data is through the Shrimpy Websocket API. Shrimpy requires either a “Personal” or “Startup” payment plan to access the websocket API.
Note: We won’t be using any trading endpoints in these tutorials, so you don’t need a “User” / “Trading” plan.
REST API Market Data Endpoints
The following market data scripts will use the REST API endpoints to collect data from the Coinbase Pro exchange. All of the scripts in this section can be used without having a Shrimpy subscription.
Available Assets
Using the get_exchange_assets endpoint, we can retrieve the assets that are available on Coinbase Pro.
import shrimpy
# Assign public and secret Shrimpy Master API Keys
shrimpy_public_key = '...'
shrimpy_secret_key = '...'
# Create the Shrimpy REST API client
client = shrimpy.ShrimpyApiClient(shrimpy_public_key, shrimpy_secret_key)
exchange_assets = client.get_exchange_assets('coinbasepro')
Available Trading Pairs
Using the get_trading_pairs endpoint, we can get access to the available trading pairs on Coinbase Pro.
import shrimpy
# Assign public and secret Shrimpy Master API Keys
shrimpy_public_key = '...'
shrimpy_secret_key = '...'
# Create the Shrimpy REST API client
client = shrimpy.ShrimpyApiClient(shrimpy_public_key, shrimpy_secret_key)
trading_pairs = client.get_trading_pairs('coinbasepro')
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 Coinbase Pro.
The ticker endpoint updates on a 1-minute interval.
import shrimpy
# Assign public and secret Shrimpy Master API Keys
shrimpy_public_key = '...'
shrimpy_secret_key = '...'
# Create the Shrimpy REST API client
client = shrimpy.ShrimpyApiClient(shrimpy_public_key, shrimpy_secret_key)
# Request the ticker for all assets on Coinbase Pro
ticker = client.get_ticker('coinbasepro')
Simple Live Order Book Snapshot
In this example, we will access the live state of the Coinbase Pro order book by calling the REST API endpoint for get_order_books.
import shrimpy
shrimpy_public_key = '...'
shrimpy_secret_key = '...'
# Create the API client for REST calls
client = shrimpy.ShrimpyApiClient(shrimpy_public_key, shrimpy_secret_key)
# Retrieve the live order book
orderbooks = client.get_orderbooks(
'coinbasepro', # 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 more data, you must use the “historical” candlestick endpoint.
import shrimpy
import plotly.graph_objects as go
# sign up for the Shrimpy Developer APIs for your free API keys
shrimpy_public_key = '...'
shrimpy_secret_key = '...'
# collect the historical candlestick data
client = shrimpy.ShrimpyApiClient(shrimpy_public_key, shrimpy_secret_key)
candles = client.get_candles(
'coinbasepro', # 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 library
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'])
# plot the candlesticks
fig = 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 Coinbase Pro 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 example will use real-time trade websockets.
import shrimpy
shrimpy_public_key = '...'
shrimpy_secret_key = '...'
# This is a sample handler, it simply prints the incoming message to the console
def error_handler(err):
print(err)
# This is a sample handler, it simply prints the incoming message to the console
def handler(msg):
print(msg['content'][0]['price'])
# Create the websocket client using the raw token retrieved by the REST API
api_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 websocket
subscribe_data = {
"type": "subscribe",
"exchange": "coinbasepro",
"pair": "eth-btc",
"channel": "trade"
}
# Start processing the Shrimpy websocket stream!
client.connect()
client.subscribe(subscribe_data, handler)
# Once complete, stop the client
client.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 shrimpy
shrimpy_public_key = '...'
shrimpy_secret_key = '...'
# This is a sample handler, it simply prints the incoming message to the console
def error_handler(err):
print(err)
# This is a sample handler, it simply prints the incoming message to the console
def 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": "coinbasepro",
"pair": "eth-btc",
"channel": "orderbook"
}
# Start processing the Shrimpy websocket stream!
ws_client.connect()
ws_client.subscribe(subscribe_data, handler)
# Once complete, stop the client
ws_client.disconnect()
Exchange Historical Market Data
Now that we have an understanding of live data endpoints, we can move forward with examples on how to collect historical market data from Coinbase Pro.
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 keys
shrimpy_public_key = '...'
shrimpy_secret_key = '...'
# collect the historical candlestick data
client = shrimpy.ShrimpyApiClient(shrimpy_public_key, shrimpy_secret_key)
instruments = client.get_historical_instruments('coinbasepro')
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.
import shrimpy
import plotly.graph_objects as go
# sign up for the Shrimpy Developer APIs for your free API keys
public_key = '...'
secret_key = '...'
client = shrimpy.ShrimpyApiClient(public_key, secret_key)
candles = client.get_historical_candles(
'coinbasepro', # 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 Coinbase Pro. This script uses the get_historical_trades endpoint.
import shrimpy
# sign up for the Shrimpy Developer APIs for your free API keys
public_key = '...'
secret_key = '...'
client = shrimpy.ShrimpyApiClient(public_key, secret_key)
trades = client.get_historical_trades(
'coinbasepro',
'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 keys
public_key = '...'
secret_key = '...'
client = shrimpy.ShrimpyApiClient(public_key, secret_key)
orderbooks = client.get_historical_orderbooks(
'coinbasepro',
'ETH',
'BTC',
'2019-05-19T00:00:00.000Z',
'2019-05-20T00:00:00.000Z',
100
)
About Shrimpy
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:
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