Getting started coding tutorial#

Preface#

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.

Quickstart#

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.

[14]:
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.

[15]:
# 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 {datetime.date.today()}")
This Jupyter notebook was run and the results captured at 2022-01-10

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

[16]:
from typing import List
from IPython.display import display
import pandas as pd
import numpy as np
from tradingstrategy.chain import ChainId
from tradingstrategy.exchange 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]:
    exchanges.append(xchg.name)
    # Format volume with the  thousand separator
    volumes.append("{:,.2f}".format(xchg.vol_30d))
    # Pull blockchain name for the chain id number from
    # chain data database
    chain_id: ChainId = xchg.chain_id
    chains.append(chain_id.get_name())

# 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
display(df)
Exchange Blockchain USD Volume 30d
1 PancakeSwap v2 Binance Smart Chain 22,570,802,493.35
2 Uniswap v2 Ethereum 10,187,449,749.76
3 Sushiswap Ethereum 8,535,474,344.84
4 Biswap Binance Smart Chain 5,534,392,623.57
5 BabySwap Binance Smart Chain 3,730,382,286.98
6 Quickswap Polygon 1,628,781,813.87
7 GIBX Swap Binance Smart Chain 848,028,627.98
8 ApeSwap Binance Smart Chain 680,577,965.38
9 ErosSwap Binance Smart Chain 658,118,532.04
10 SushiSwap Polygon 629,315,682.02

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