Drawing DEX prices using OHLCV candle charts#

Getting started#

First, let’s create Trading Strategy oracle market data client. If you do not have an API key yet, you will be asked to create one.

[8]:
from tradingstrategy.client import Client

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

Resolve trading pair metadata#

We look at BNB / BUSD trading pair on PancakeSwap DEX (version 2).

  • To download the data we first need to download exchange and trading pair metadata, so that we can resolve symbolic exchange name and ticket information to the trading pair id

  • We may have duplicate trading pair with the same ticket and token symbol information. This is often the case for popular trading pairs as there are a lot of scam pairs around with the same symbol. We go around this by using the option pick_by_highest_vol which gives us the correct trading pair.

  • Note that the blockchain native token of EVM chain is presented by its wrapped version on DEXes. E.g. BNB on BNB Chain becomes WBNB.

[9]:
from pyarrow import Table
from tradingstrategy.chain import ChainId
from tradingstrategy.exchange import ExchangeUniverse
from tradingstrategy.pair import PandasPairUniverse, DEXPair

# Fetch all exchange names, slugs and addresses
exchange_universe: ExchangeUniverse = client.fetch_exchange_universe()

# Fetch all trading pairs across all exchanges
pair_table: Table = client.fetch_pair_universe()
pair_universe = PandasPairUniverse(pair_table.to_pandas())

pancake_v2 = exchange_universe.get_by_chain_and_slug(ChainId.bsc, "pancakeswap-v2")

pair: DEXPair = pair_universe.get_one_pair_from_pandas_universe(
    pancake_v2.exchange_id,
    "WBNB",
    "BUSD",
    pick_by_highest_vol=True)

print(f"PancakeSwap v2 is {pancake_v2}")
print(f"We have trading pair {pair}")
PancakeSwap v2 is <Exchange PancakeSwap v2 at 0xca143ce32fe78f1f7019d7d551a6402fc5350c73 on Binance Smart Chain>
We have trading pair <Pair #1364760 WBNB - BUSD (0x58f876857a02d6762e0101bb5c46a8c1ed44dc16) at exchange #1187 on binance>

Fetch price data feed#

  • We then downlaod OHLCV data using Trading Strategy oracle’s real-time API endpoint

  • The data is internally delivered as JSONL, but this is abstracted way by trading-strategy Python client library

  • Any price of non-dollar pairs, e.g. AAVE/ETH, is pre-converted to US dollars by Trading Strategy exchange rate rules

[10]:
import pandas as pd
from tradingstrategy.timebucket import TimeBucket

candles: pd.DataFrame = client.fetch_candles_by_pair_ids(
    {pair.pair_id},
    TimeBucket.d1,
    progress_bar_description=f"Download data for {pair.get_ticker()}"
)

Inspect OHCLV data#

  • You can manually inspect OHCLV trading dataframes.

  • Easiest way to do this is to use IPython display() function to render dataframes as table.

[11]:
# Display latest 14 days of WBNB-BUSD data
display(candles.iloc[-14:])
pair_id timestamp open high low close buy_volume sell_volume start_block end_block exchange_rate buys sells avg volume
timestamp
2022-11-23 1364760 2022-11-23 266.261026 299.841244 264.315010 297.825673 2.729343e+07 2.228333e+07 23288796 23317233 1.0 21300.0 24550.0 NaN 4.957676e+07
2022-11-24 1364760 2022-11-24 297.822293 302.814980 292.549276 300.091218 9.670815e+06 9.369412e+06 23317237 23345600 1.0 16895.0 16145.0 NaN 1.904023e+07
2022-11-25 1364760 2022-11-25 300.087669 304.630323 293.529879 300.225753 9.101830e+06 9.063683e+06 23345606 23373804 1.0 16136.0 14800.0 NaN 1.816551e+07
2022-11-26 1364760 2022-11-26 300.225295 316.787367 300.139182 311.051139 1.072095e+07 9.386321e+06 23373806 23402018 1.0 15987.0 15941.0 NaN 2.010727e+07
2022-11-27 1364760 2022-11-27 311.050841 316.365007 306.025563 306.773434 9.036277e+06 9.531502e+06 23402024 23430502 1.0 15283.0 14602.0 NaN 1.856778e+07
2022-11-28 1364760 2022-11-28 306.773488 308.490684 289.239530 293.141829 1.015980e+07 1.179148e+07 23430504 23458977 1.0 17488.0 14359.0 NaN 2.195128e+07
2022-11-29 1364760 2022-11-29 293.141677 306.658445 289.679437 295.772926 6.853360e+06 6.512749e+06 23458979 23487565 1.0 16409.0 15357.0 NaN 1.336611e+07
2022-11-30 1364760 2022-11-30 295.772956 305.356409 293.931451 300.922936 9.490703e+06 8.836190e+06 23487568 23516178 1.0 19505.0 17381.0 NaN 1.832689e+07
2022-12-01 1364760 2022-12-01 300.924183 301.409985 290.616758 291.678394 9.108918e+06 1.030238e+07 23516180 23544574 1.0 20378.0 17129.0 NaN 1.941129e+07
2022-12-02 1364760 2022-12-02 291.681450 293.328217 286.174944 292.979351 1.483278e+07 1.458857e+07 23544576 23572636 1.0 22339.0 18104.0 NaN 2.942134e+07
2022-12-03 1364760 2022-12-03 292.979907 293.566933 289.695106 290.307121 5.278060e+06 5.676123e+06 23572640 23600811 1.0 16711.0 14991.0 NaN 1.095418e+07
2022-12-04 1364760 2022-12-04 290.306878 294.650247 288.330343 292.470853 4.900642e+06 4.552601e+06 23600814 23628935 1.0 17466.0 13406.0 NaN 9.453243e+06
2022-12-05 1364760 2022-12-05 292.470940 297.145499 286.615769 287.910945 5.476807e+06 6.183128e+06 23628937 23657267 1.0 17560.0 13305.0 NaN 1.165994e+07
2022-12-06 1364760 2022-12-06 287.909748 292.217899 287.497567 289.707345 2.083389e+06 1.794518e+06 23657271 23667239 1.0 4735.0 3841.0 NaN 3.877908e+06

Draw candle chart#

  • Display the price chart using OHLCV candles

  • We have a shortcut function for this, but you can as well construct Plotly chart by hand

[12]:
from tradingstrategy.charting.candle_chart import visualise_ohlcv

figure = visualise_ohlcv(
    candles,
    chart_name=f"{pair.base_token_symbol} - {pair.quote_token_symbol} price chart",
    y_axis_name=f"$ {pair.base_token_symbol} price",
)

display(figure)

Dark theme for the price chart#

[13]:
figure.update_layout(template="plotly_dark")

display(figure)

Unlabeled chart#

We can create a chart without any labels as well.

[14]:
figure = visualise_ohlcv(
    candles,
    chart_name=None,
    y_axis_name=None,
    volume_axis_name=None,
)

display(figure)

Further reading#

For more advanced charting, trade-executor Python library provides charts that plot out a trading strategy performance (as opposite to vanilla price chart).