Coins: 14,016
Exchanges: 1,065
Market Cap: $2.464T 4.7%
24h Vol: $104.926B
Gas: 13 GWEI
Go Ad-free
API
TABLE OF CONTENTS

Building a DEX Tracker with Streamlit and Python

4.6 | by Automation Architech

In this tutorial, we'll explore how to connect to CoinGecko API to query on-chain, decentralized exchange (DEX) data to create a DEX tracker using Streamlit and Python.

Our on-chain endpoints provide access to real-time price and liquidity data for 2M+ tokens across 100+ blockchains – this includes cryptocurrencies not yet listed on CoinGecko. Fetch information such as new crypto pools, price changes and transaction volumes, which are crucial for tracking the performance of different cryptocurrency pairs on DEXes.

What is Streamlit?

Streamlit is an open-source Python library that allows you to build interactive, shareable web applications for your data projects with ease. It's designed for people with some Python programming experience. Used by many data scientists, machine learning engineers, and backend developers, Streamlit speeds up the process of creating beautiful data-driven applications without the need for extensive front-end development skills.

What is a DEX Tracker?

A DEX tracker is a tool or application that monitors and provides insights into the trading activities happening on decentralized exchanges. It can display various metrics, including liquidity, volume, price changes, and more, helping traders make informed trading decisions. CoinGecko's sister product, GeckoTerminal, is an example of a DEX tracker.

Prerequisites

Before we go into the code snippets, let's review the prerequisites for building a DEX tracker:

  • A basic understanding of Python – Python is a programming language known for its ease of use, powerful data manipulation libraries, and strong community support.
  • Streamlit – We’ll be using this App Starter Kit to bootstrap our dashboard.
  • CoinGecko API paid subscription – We’ll be calling the API's new /onchain endpoints to fetch live, on-chain data from DEXes, which is a paid exclusive endpoint. Ensure you have your Pro API key ready for authentication.

With that, let's dive in!


How to Create a DEX Tracker using DEX API data Streamlit and Python Guide with Code Examples | CoinGecko API

How to Build a DEX Tracker in 5 Steps

Step 1. Setup

To get data on all the newest pools across all supported blockchain networks, we’ll query the New Pools List endpoint. Such data can be particularly useful for crypto traders to identify new tokens and projects entering the market, evaluate the risk associated with each liquidity pool and spot potential arbitrage opportunities.

💡 Pro-tip: The equivalent page of this data on CoinGecko’s sister product, GeckoTerminal, is New Crypto Pools.

For this project, I would suggest using a virtual environment to install and isolate your dependencies.

Step 2. Boilerplate

We used this to get started: https://github.com/streamlit/app-starter-kit

If you want to just copy the code directly, you can download this repo on GitHub for the quickest setup. 

Step 3. Display + API Utilities

To fetch and parse the API, you’ll first need to get the associated data, followed by parsing out the raw JSON response into a format acceptable for streamlit. In this example, we created a separate utils.py file to handle these various functions.

fetch_data is a generic decorated function for APIs.

process_data takes our newly captured API JSON response and converts it into a data frame.

get_top_changes specifically gets the top3 pairs with the greatest change in a specific column. In this example, we use price_change_percentage_h1

parse_crypto_pools is used to map the response to a flat format. 

plot_price_change to plot the price changes in the last 24 hours

Here's how it all comes together:

import json

import streamlit as st

import requests

import pandas as pd

import matplotlib.pyplot as plt

import seaborn as sns 

 

# Function to fetch data from the API

@st.cache_data

def fetch_data(url, headers):

   response = requests.get(url, headers=headers)

   if response.status_code == 200:

       return response.json()

   else:

       st.error(f'Failed to retrieve data. Status code: {response.status_code}')

       return None

 

# Function to process the JSON data into a DataFrame

def process_data(data):

   return pd.DataFrame([pool['attributes'] for pool in data['data']])

 

# Function to get top 3 pairs with the greatest 1-hour volume change

def get_top_changes(df, column_name='price_change_percentage_h1'):

   # Ensure the column is of type float

   df[column_name] = df[column_name].astype(float)

   # Sort the DataFrame based on the volume change column

   top_changes = df.sort_values(by=column_name, ascending=False).head(3)

   # Include the 'transactions' column in the returned DataFrame

   return top_changes[['name', column_name, 'transactions_h1_sells', 'transactions_h1_buys']]

 

def parse_crypto_pools(json_payload):

   # Convert the JSON payload to a Python dictionary

   data = json_payload

 

   # List to hold parsed pool data

   parsed_pools = []

 

   # Iterate through each pool in the data

   for pool in data['data']:

       # Extract relevant information from each pool

       pool_info = {

           'address': pool['attributes']['address'],

           'name': pool['attributes']['name'],

           'pool_created_at': pool['attributes']['pool_created_at'],

           'fdv_usd': pool['attributes'].get('fdv_usd'),

           'market_cap_usd': pool['attributes'].get('market_cap_usd'),

           'volume_usd_h1': pool['attributes']['volume_usd']['h1'],

           'volume_usd_h24': pool['attributes']['volume_usd']['h24'],

           'price_change_percentage_h1': json.loads(pool['attributes']['price_change_percentage']['h1']),

           'price_change_percentage_h24': json.loads(pool['attributes']['price_change_percentage']['h24']),

           'transactions_h1_buys': pool['attributes']['transactions']['h1']['buys'],

           'transactions_h1_sells': pool['attributes']['transactions']['h1']['sells'],

       }

       parsed_pools.append(pool_info)

 

   return parsed_pools

 

def plot_price_change(df, log_scale=False):

   pool_names = df['name']

   # Directly use the float values from the series

   price_changes_24h = [float(change) if change else 0 for change in df['price_change_percentage_h1']]

   fig, ax = plt.subplots()

   sns.barplot(x='price_change_percentage_h1', y='name', data=df, ax=ax)

   ax.set(xlabel='Price Change Percentage (24h)', title='Price Change Percentage (24h) by Pool')

   return fig

 

4. Main Dashboard

For the main.py, we’ll reserve this for logic related to constants, UI configuration, and running the main application. 

import streamlit as st

import pandas as pd

import numpy as np

 

from utils import fetch_data, get_top_changes, parse_crypto_pools, plot_price_change

 

# Constants

BASE_URL = 'https://pro-api.coingecko.com/api/v3/onchain'

ENDPOINT = '/networks/new_pools?page=1'

API_URL = BASE_URL + ENDPOINT

HEADERS = {'accept': 'application/json', 'x-cg-pro-api-key':'INSERT_API_KEY_HERE'}

 

# Set the page to wide mode

st.set_page_config(layout="wide")

 

# Main app code

st.title('Cryptocurrency Pool Data Viewer')

 

# Fetch and process data

raw_data = fetch_data(API_URL, HEADERS)

if raw_data:

   df = pd.DataFrame(parse_crypto_pools(raw_data))

 

   top_volume_changes = get_top_changes(df, 'price_change_percentage_h1')

 

   # Display the top 3 price changes

   st.subheader('⭐️ Top 3 Pairs by 1-Hour Price Change')

   columns = st.columns(3)  # Create three columns for the top 3 changes

   for index, (col, row) in enumerate(zip(columns, top_volume_changes.iterrows())):

       with col:

           st.metric(label=row[1]['name'], value=f"{row[1]['price_change_percentage_h1']:.2f}%")

           st.text(f"Buys in last hour: {row[1]['transactions_h1_buys']}")

           st.text(f"Sells in last hour: {row[1]['transactions_h1_sells']}")

 

   # Create two columns for the data table and the bar chart

   col1, col2 = st.columns([2,3])

 

   with col1:

       # Display data table in the first column

       st.subheader('🏓 Data Overview')

       st.write("", df)

 

   with col2:

       # Visualization: Bar Chart for Price Change Percentage in the second column

       st.subheader("📊 Price Change Percentage (24h)")

       if 'price_change_percentage_h1' in df.columns:

           fig_price_change = plot_price_change(df)

           st.pyplot(fig_price_change, use_container_width=True)


The Final Result: Crypto Pools DEX Tracker on Streamlit

If you’ve setup your dependencies and code properly, you can now run streamlit run main.py in your terminal, after which the below dashboard should appear in your browser:

Crypto Pools DEX Tracker using a DEX API

By following these steps, you can easily build a DEX dashboard to visualize on-chain data from decentralized exchanges. This guide serves as a foundation, and you can expand it by adding more features and interactivity.


For more details on enhancing your dashboard, you may refer to this Streamlit documentation as well as leverage other popular on-chain DEX data endpoints on CoinGecko API, exclusively for paid subscribers:

  1. /onchain/networks/{network}/pools – Get top pools on a network
  2. /onchain/networks/{network}/trending_pools – Get trending pools on a network
  3. /onchain/networks/{network}/tokens/{token_address}/pools – Get top pools for a token, with a provided token contract address
  4. /onchain/search/pools – Search for pool address, token address or token symbols on a network
  5. /onchain/networks/{network} /pools/{pool_address}/ohlcv/{timeframe} OHLCV chart (Open, High, Low, Close, Volume) of a crypto pool based on a pool address on a network.

Access on-chain market data DEX API | CoinGecko API


Interested in more how-to guides leveraging CoinGecko API? Check out this walk-through on how to build an artificial intelligence crypto chatbot.

CoinGecko's Content Editorial Guidelines
CoinGecko’s content aims to demystify the crypto industry. While certain posts you see may be sponsored, we strive to uphold the highest standards of editorial quality and integrity, and do not publish any content that has not been vetted by our editors.
Learn more
Tell us how much you like this article!
Vote count: 11
Automation Architech
Automation Architech

Automation Architech boasts a seasoned team specializing in ML-driven solutions, operations engineering, and digital product development. They are dedicated to ethical AI, bridging tech gaps, and fostering creativity without sidelining humanity. Follow the author on Twitter @auto_architech

More Articles


Explore Polkadot's Ecosystem
Discover trending dApps, wallets, DeFi & more

What is Zeebu?
Learn more about the Web3 neobank


coingecko
Continue in app
Track prices in real-time
Open App
Select Currency
Suggested Currencies
USD
US Dollar
IDR
Indonesian Rupiah
TWD
New Taiwan Dollar
EUR
Euro
KRW
South Korean Won
JPY
Japanese Yen
RUB
Russian Ruble
CNY
Chinese Yuan
Fiat Currencies
AED
United Arab Emirates Dirham
ARS
Argentine Peso
AUD
Australian Dollar
BDT
Bangladeshi Taka
BHD
Bahraini Dinar
BMD
Bermudian Dollar
BRL
Brazil Real
CAD
Canadian Dollar
CHF
Swiss Franc
CLP
Chilean Peso
CZK
Czech Koruna
DKK
Danish Krone
GBP
British Pound Sterling
GEL
Georgian Lari
HKD
Hong Kong Dollar
HUF
Hungarian Forint
ILS
Israeli New Shekel
INR
Indian Rupee
KWD
Kuwaiti Dinar
LKR
Sri Lankan Rupee
MMK
Burmese Kyat
MXN
Mexican Peso
MYR
Malaysian Ringgit
NGN
Nigerian Naira
NOK
Norwegian Krone
NZD
New Zealand Dollar
PHP
Philippine Peso
PKR
Pakistani Rupee
PLN
Polish Zloty
SAR
Saudi Riyal
SEK
Swedish Krona
SGD
Singapore Dollar
THB
Thai Baht
TRY
Turkish Lira
UAH
Ukrainian hryvnia
VEF
Venezuelan bolívar fuerte
VND
Vietnamese đồng
ZAR
South African Rand
XDR
IMF Special Drawing Rights
Cryptocurrencies
BTC
Bitcoin
ETH
Ether
LTC
Litecoin
BCH
Bitcoin Cash
BNB
Binance Coin
EOS
EOS
XRP
XRP
XLM
Lumens
LINK
Chainlink
DOT
Polkadot
YFI
Yearn.finance
Bitcoin Units
BITS
Bits
SATS
Satoshi
Commodities
XAG
Silver - Troy Ounce
XAU
Gold - Troy Ounce
Select Language
Popular Languages
EN
English
RU
Русский
DE
Deutsch
PL
język polski
ES
Español
VI
Tiếng việt
FR
Français
PT
Português
All Languages
AR
العربية
BG
български
CS
čeština
DA
dansk
EL
Ελληνικά
FI
suomen kieli
HE
עִבְרִית
HI
हिंदी
HR
hrvatski
HU
Magyar nyelv
ID
Bahasa Indonesia
IT
Italiano
JA
日本語
KO
한국어
LT
lietuvių kalba
NL
Nederlands
NO
norsk
RO
Limba română
SK
slovenský jazyk
SL
slovenski jezik
SV
Svenska
TH
ภาษาไทย
TR
Türkçe
UK
украї́нська мо́ва
ZH
简体中文
ZH-TW
繁體中文
Login to track your favorite coin easily 🚀
By continuing, you agree to CoinGecko Terms of Service and acknowledge you’ve read our Privacy Policy
or
Forgot your password?
Didn't receive confirmation instructions?
Resend confirmation instructions
IT'S FREE! Track your favorite coin easily with CoinGecko 🚀
By continuing, you agree to CoinGecko Terms of Service and acknowledge you’ve read our Privacy Policy
or
Password must contain at least 8 characters including 1 uppercase letter, 1 lowercase letter, 1 number, and 1 special character
Didn't receive confirmation instructions?
Resend confirmation instructions
Forgot your password?
You will receive an email with instructions on how to reset your password in a few minutes.
Resend confirmation instructions
You will receive an email with instructions for how to confirm your email address in a few minutes.
Get the CoinGecko app.
Scan this QR code to download the app now App QR Code Or check it out in the app stores