Getting started coding tutorial#


This is a Python and Jupyter Notebook 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

Note: Python 3.10+ required. If you get errors like ValueError: 'uniswap_v2' is not a valid ExchangeType make sure that you are running at least Python 3.10+ and the latest version of ``trading-strategy` library <>`__.

Setting up the development environment#

Before you can start running tutorials

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.

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()
<jemalloc>: MADV_DONTNEED does not work (memset will be used instead)
<jemalloc>: (This is the expected behaviour if you are running under QEMU)
No existing Trading Strategy configuration found in /root/.tradingstrategy/settings.json. Starting interactive setup.
Using Trading Strategy requires you to have an API key.
The API key is in format 'secret-token:tradingstrategy-...'
Testing out API key: secret-token:tradingstra
The server replied accepted our API key and sent the following greetings:
Server version: 0.1
Message of the day: Han shot first
The API key setup complete.

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 2023-02-01

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,796,171,631.11
2 PancakeSwap v2 Binance Smart Chain 3,296,881,933.19
3 Uniswap v2 Ethereum 2,365,628,460.17
4 Uniswap v3 Polygon 1,582,595,009.69
5 Sushi Ethereum 748,421,932.53
6 Shiba Swap Ethereum 271,597,428.20
7 Biswap Binance Smart Chain 270,492,281.23
8 Quickswap Polygon 181,327,202.93
9 Trader Joe Avalanche C-chain 120,264,325.57
10 Nomiswap Stable Binance Smart Chain 92,298,036.89

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