Tutorial overview
Stream and analyze pool OHLCV in InfluxDB with hourly/minute intervals and live Grafana dashboards.From real-time monitoring to historical analysis
While InfluxDB is a champion of real-time data, its power extends far beyond live dashboards. It provides a highly optimized engine for storing and querying massive time-series datasets, making it a perfect middle-ground between the local analytics of DuckDB and the enterprise scale of ClickHouse.Looking for other analytics solutions? Check out our full list of API Tutorials for more step-by-step guides.
- Ingest 1-hour OHLCV data for the top 500 Uniswap v3 pools over a 14-day period.
- Run complex time-series analysis using Python and the Flux language.
- Visualize the data in a live-updating Grafana dashboard.
Step 1: Docker Setup
Get InfluxDB and Grafana running in seconds with Docker.
Step 2: ETL Pipeline
Create a robust data pipeline for ingesting large time-series datasets.
Step 3: Programmatic Analysis
Analyze your time-series data with InfluxDB’s Python client.
Step 4: Live Dashboard
Build a real-time dashboard to monitor crypto pools.
Step 1: Setting up InfluxDB and Grafana
We’ll use Docker Compose to spin up both services. First, create a new directory namedINFLUXDB in your project root. Inside that directory, create a file named docker-compose.yml with the following content.
INFLUXDB/docker-compose.yml
- InfluxDB UI:
http://localhost:8087 - Grafana UI:
http://localhost:3000(login withadmin/admin)
my-super-secret-token to connect to InfluxDB.
We use port
8087 for InfluxDB to avoid potential conflicts with other services that might be using the default port 8086.Step 2: Build the Python ETL pipeline
Create a new file namedINFLUXDB/build_influxdb_db.py. This script is built to be robust and efficient, capable of ingesting large volumes of time-series data from the DexPaprika API into your InfluxDB instance. It uses two endpoints: pool search with a dex_name filter to discover pools, and the Pool OHLCV Data endpoint to fetch historical price data.
This script needs a free API key. Without one, OHLCV reaches back only 24 hours at
1h and up, and the script asks for six days of hourly candles. Export the key before running it; it goes in the Authorization header on its own. On Pro, point API_BASE_URL at https://api-pro.dexpaprika.com. Limits per plan: OHLCV limits by plan.INFLUXDB/build_influxdb_db.py
This script is built for performance and reliability, using several best practices common in data pipelines:
- Asynchronous operations: By using
asyncioandaiohttp, the script can make many API requests concurrently instead of one by one. - Dynamic windowing: The
fetch_pool_ohlcv_paginatedfunction calculates how much data to request per API call to ensure complete history is fetched efficiently. - Concurrency control & throttling: An
asyncio.Semaphore, combined with carefully tunedBATCH_SIZEandasyncio.sleep()calls, keeps the script under the free tier’s 15 requests a minute keyless, or 30 with a free key. Pro raises that to 500 a minute, which lets you shorten the sleeps and finish the backfill sooner. - Resiliency: The
fetch_with_retryfunction automatically retries failed requests with an exponential backoff delay. - Data Integrity: The script automatically clears old data from the bucket before each run to ensure a clean, consistent dataset.
Set up the Python environment
Before running the ETL script, it’s a critical best practice to create an isolated Python environment to manage dependencies.-
Create a virtual environment:
Open your terminal in the project root and run:
-
Activate the environment:
- On macOS and Linux:
- On Windows:
(venv), indicating that the virtual environment is active. - On macOS and Linux:
-
Install required libraries:
Now, install the necessary Python packages inside the activated environment:
Step 3: Programmatic analysis with Python
While the InfluxDB UI is great for exploration, the real power comes from programmatic access. Theinfluxdb-client library for Python allows you to run complex Flux queries and integrate the data into other tools or scripts.
We’ve created a query_influxdb.py script to demonstrate how to connect to your database and perform analysis on the hourly data.
INFLUXDB/query_influxdb.py
Step 4: Visualizing data in Grafana
-
Open Grafana at
http://localhost:3000. - Go to Connections > Add new connection > InfluxDB.
-
Configure the connection:
- Name: InfluxDB_Crypto
- Query Language: Flux
- URL:
http://influxdb:8086(use the Docker service name) - Under Auth, toggle Basic auth off.
- In the Custom HTTP Headers section, add a header:
- Header:
Authorization - Value:
Token my-super-secret-token
- Header:
- Enter your InfluxDB Organization (
my-org) and default Bucket (crypto-data).
- Click Save & Test. You should see a “Bucket found” confirmation.
- Now, let’s create a dashboard. In the left-hand menu, click the + icon and select Dashboard.
- Click on the Add new panel button.
-
In the new panel view, ensure your
InfluxDB_Cryptodata source is selected at the top. - Below the graph, you’ll see a query editor. You can switch to the Script editor by clicking the pencil icon on the right.
-
Paste the following query into the editor. This query will plot the raw closing price for the
AAVE/USDCtrading pair.Troubleshooting Tip: If you initially see “No data,” there are two common reasons:- Time Range: Ensure the time picker at the top right is set to “Last 7 days” or wider, not a shorter period like “Last 6 hours.”
- Trading Pair: The default
WETH/USDCpair used in the original tutorial may not have been in the top 500 pools fetched by the script. The query above usesAAVE/USDC, which is more likely to be present. You can find other available pairs by running thequery_influxdb.pyscript.
- At the top right of the dashboard page, set the time range to Last 7 days to ensure you see all the historical data you ingested.
- You should now see the data appear in the panel. You can give the panel a title (e.g., “AAVE/USDC Close Price”) in the settings on the right.
- Click Apply to save the panel to your dashboard. You can now add more panels for other queries.
What you’ve built
You now have a powerful, scalable analytics pipeline for time-series crypto data. You’ve combined a production-grade Python ETL script with the industry-standard tools for time-series data storage (InfluxDB) and visualization (Grafana). Key achievements:- Built a production-ready ETL pipeline: You have a reusable, high-performance Python script that can populate a time-series database from any supported DEX.
- Unlocked programmatic time-series analysis: You can now perform complex analytical queries on large time-series datasets using Python and Flux.
- Mastered a scalable analytics workflow: This pipeline provides a solid foundation for building live dashboards, conducting in-depth market research, and developing sophisticated monitoring or trading algorithms.
- Enabled live data visualization: You’ve connected your database to Grafana, the leading open-source tool for observability and data visualization.
FAQs
Why choose InfluxDB over ClickHouse for this use case?
Why choose InfluxDB over ClickHouse for this use case?
InfluxDB excels at time-series ingestion and dashboards; it’s ideal for hourly/minute data with Grafana visualization.
How much history should I store?
How much history should I store?
Keep recent weeks/months for dashboards and archive older data elsewhere; adjust by bucket retention.
What interval is best for the ETL?
What interval is best for the ETL?
1h is a sensible default; lower to 15m/5m for more granularity if needed.How do I keep the dashboard fresh?
How do I keep the dashboard fresh?
Schedule the ETL to append new windows, and set Grafana panels to auto-refresh.