Interactive charts#

In this notebook, we will show you create interactive technical analysis charts with Capitalgram. Interactive means you can easily pan and zoom over a larger time horizon, have tooltips and hovers available to show additional details.

We will

About Plotly#

Plotry and other interactive charts work in environments that can load the supporting JavaScript libraries on the page the charts are displayed. For example, currently Plotly does not work on Google Colab Jupyter Notebooks.

Getting started#

First, let’s create Trading Strategy dataset client.

[ ]:
from tradingstrategy.client import Client

client = Client.create_jupyter_client()

Fetch data#

Let’s download 1 day (24h) candles to all trading pairs. This dataset is several dozens of megabytes. You should see a progress bar during the download.

[2]:
from tradingstrategy.timebucket import TimeBucket

# Download all 24h candles as Parquet columnar data
all_candles = client.fetch_all_candles(TimeBucket.d1)

Let’s pick one pair, ETH-USDC on Uniswap v2, from the dataset to analyse.

[3]:
import pandas as pd

# Convert PyArrow table to Pandas format to continue working on it
all_candles_dataframe = all_candles.to_pandas()

# To shorten this notebook, we know by hearth that USDC-ETH Uniswap v2 is the pair id numero uno,
# because that what Hayden used to test the production code when he deployed Uniswap v2
pair_id = 1

eth_usdc_pair: pd.DataFrame = all_candles_dataframe.loc[all_candles_dataframe['pair_id'] == pair_id]

print(f"Uniswap v2 ETH-USDC has {len(eth_usdc_pair)} daily candles")
Uniswap v2 ETH-USDC has 497 daily candles

Plotting an interactive chart with Plotly#

Next, let’s try interactive charts. You can pan and zoom into interactive charts, making them easier to explore.

Plotly uses its JavaScript library to make the chart interactive on any HTML page.

[4]:
import plotly.graph_objects as go
from plotly.offline import iplot

fig = go.Figure(data=[go.Candlestick(x=eth_usdc_pair['timestamp'],
                open=eth_usdc_pair['open'],
                high=eth_usdc_pair['high'],
                low=eth_usdc_pair['low'],
                close=eth_usdc_pair['close'])])
iplot(fig)