Historical OHLCV from REST: Build Candlestick Charts
Introduction
This tutorial demonstrates how to fetch historical OHLCV (Open, High, Low, Close, Volume) data from the CoinAPI REST API and create professional candlestick charts. You'll learn how to retrieve historical price data for cryptocurrency pairs and visualize them using matplotlib and mplfinance.
What You Will Learn
- How to authenticate with the CoinAPI REST API
- How to fetch historical OHLCV data with specific parameters
- How to process and clean the API response data
- How to create professional candlestick charts
- How to analyze price trends and patterns
Prerequisites
- Python 3.7+
- Required packages: requests, pandas, numpy, matplotlib, mplfinance
- CoinAPI API key (free tier available)
Setup Instructions
- Install required packages:
pip install requests pandas numpy matplotlib mplfinance - Get your free API key from CoinAPI
- Replace API_KEY placeholder in the code below
1. Environment Setup
Set up your environment with necessary imports and configuration for working with the CoinAPI REST API.
2. Fetch Historical OHLCV Data
Now we'll fetch historical OHLCV data from the CoinAPI REST API using the specified parameters. We'll make a request to the /ohlcv/:symbol_id/history endpoint with our query parameters.
3. Data Processing and Analysis
Now we'll process the raw API response data into a pandas DataFrame and perform some basic analysis. We'll convert timestamps, calculate additional metrics, and prepare the data for visualization.
4. Creating Professional Candlestick Charts
Now we'll create professional candlestick charts using mplfinance, which is specifically designed for financial data visualization. We'll create multiple chart types to analyze different aspects of the price data.
Creating candlestick chart...
OHLCV DataFrame shape: (168, 5)
OHLCV DataFrame columns: ['Open', 'High', 'Low', 'Close', 'Volume']
First few rows of OHLCV data:
Open High Low Close \
time_period_start
2025-07-07 00:00:00+00:00 109203.85 109288.02 108800.01 108823.07
2025-07-07 01:00:00+00:00 108823.07 109089.00 108679.75 109019.12
2025-07-07 02:00:00+00:00 109019.12 109499.99 109019.12 109364.52
2025-07-07 03:00:00+00:00 109364.53 109700.00 109364.52 109389.47
2025-07-07 04:00:00+00:00 109389.46 109447.54 109128.72 109128.73
Volume
time_period_start
2025-07-07 00:00:00+00:00 253.53776
2025-07-07 01:00:00+00:00 299.50777
2025-07-07 02:00:00+00:00 433.77607
2025-07-07 03:00:00+00:00 326.47933
2025-07-07 04:00:00+00:00 184.06878
Candlestick chart created and saved as 'candlestick_chart.png'
Creating price analysis charts...
5. Conclusion and Next Steps
Congratulations! You've successfully fetched historical OHLCV data from the CoinAPI REST API and built professional candlestick charts.
What We Accomplished
- Successfully connected to the CoinAPI REST API using proper authentication
- Fetched historical OHLCV data for BTC/USDT with specific parameters
- Processed and analyzed the raw API response data
- Created professional candlestick charts using mplfinance
- Generated additional analysis charts for deeper insights
- Saved all visualizations as high-quality image files
Key Takeaways
- The CoinAPI REST API provides clean, structured OHLCV data
- Proper data processing is essential for financial analysis
- Volume analysis provides valuable insights into market activity
- Historical data analysis helps identify patterns and trends
API Endpoint Reference
- Base URL:
https://rest.coinapi.io/v1 - Endpoint:
/ohlcv/:symbol_id/history - Documentation: CoinAPI OHLCV Historical Data
Happy analyzing!