Getting started coding tutorial#


This is a tutorial how to programmatically access DeFi market data using Trading Strategy library.

To get started you need understand

  • Basic algorithmic trade, finance and quantitative research understanding

  • Basic Python

  • Basic Jupyter Notebook

You also need to

API keys#

When you run the notebook for the first time, you are asked to register an API key for Trading Strategy dataset download. You can also sign up for the API key on the website.


Trading Strategy offers running its notebooks for free on Binder, a cloud Jupyter notebook hosting service.

  • Clicking the Launch binder button on any notebook in this documentation will automatically direct you to a runnable server on Binder service

  • This document is displaying the results of the last notebook run. Running the notebook yourself will execute all the cells and update the result of calculations

About this Getting started example#

This notebook you are reading is “Hello World” where we make our first dynamic market analysis.

The code belows creates a Trading Strategy API client instance that is used to communicate with the dataset server.

If you do not have an API key saved on the Notebook server yet, this will prompt you to create an API key and then saves it for you. Please answer the interactive questions when the notebook is running.

from tradingstrategy.client import Client

client = Client.create_jupyter_client()
Started Trading Strategy in Jupyter notebook environment, configuration is stored in /Users/moo/.tradingstrategy

Now when the API key is created and we are connected, we can do some analysis. Let’s get the top 10 supported exchanges by their 30 days trading volume for supported tokens.

# Let's log the date when the this notebook was run,
# as the notebook and its output will be on the website documentation
import datetime
print(f"This Jupyter notebook was run and the results captured at {}")
This Jupyter notebook was run and the results captured at 2022-11-27

Then let’s extract some montly statistics from the exchange dataset.

from typing import List
from IPython.display import display
import pandas as pd
import numpy as np
from tradingstrategy.chain import ChainId
from import Exchange

universe = client.fetch_exchange_universe()

# Have nice type decoration to make the tutorial
# more readable
exchanges: List[Exchange] = []
volumes: List[float] = []
chains: List[str] = []

for xchg in universe.get_top_exchanges_by_30d_volume()[0:10]:
    # Format volume with the  thousand separator
    # Pull blockchain name for the chain id number from
    # chain data database
    chain_id: ChainId = xchg.chain_id

# Convert output to Pandas DataFrame object so that
# notebook can render it as a nice table
df = pd.DataFrame({"Exchange": exchanges, "Blockchain": chains, "USD Volume 30d": volumes})

# Index rows starting with one instead of zero
df.index = np.arange(1, len(df)+1)

# Show dataframe as HTML table
Exchange Blockchain USD Volume 30d
1 Uniswap v3 Ethereum 24,411,982,299.52
2 PancakeSwap v2 Binance Smart Chain 6,917,489,085.00
3 Uniswap v2 Ethereum 3,570,216,679.30
4 Sushi Ethereum 1,225,083,503.20
5 Biswap Binance Smart Chain 846,066,186.16
6 Quickswap Polygon 467,719,126.60
7 Shiba Swap Ethereum 372,847,330.83
8 Trader Joe Avalanche C-chain 266,942,336.02
9 GIBX Swap Binance Smart Chain 227,116,880.05
10 Nomiswap Stable Binance Smart Chain 126,110,856.78

Ta-da - all done! Now you can proceed to view more complex examples.