https://colab.research.google.com/assets/colab-badge.svg

Getting started

Preface

To get started you need

If you are new to these topics please see the Learn section for useful links to start learning more.

API keys

When you run the notebook for the first time, you are asked to register an API key for Trading Strategy dataset download.

The interaction happens over Python input, within the Jupyter Notebook. At the moment the API key registration over web is unsupported.

Quickstart

Google offers free cloud service to run your notebooks

  • Click Open in Colab button above

  • You need a Google account

  • Clicking the Open in Colab button on any notebook in this documentation will automatically create you a personal runnable copy on Google Colaboratory service

  • After the notebook is open on Colab, you need to press Connect to allocate a server for yourself and then execute the notebook by choosing Runtime > Run all from the menu

  • 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

  • The first execution will take around 5 minutes, because

Install

This step assumes you have managed to get your Jupyter Notebook environment set up and an empty notebook open.

This notebook starts by installing tradingstrategy Python package on your notebook environment if it is not installed yet.

[13]:
try:
    import tradingstrategy
except ImportError:
    # Install on-demand on Google Colab
    !pip install tradingstrategy
    import site
    site.main()

Usage

This is the 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.