Python Cryptocurrency Price Wrapper: Using the CryptoCompare API

ยท

A comprehensive guide to querying cryptocurrency prices and market data using the cryptocompare Python3 wrapper for the CryptoCompare API.


Installation

To install the cryptocompare package, run:

pip3 install cryptocompare

Basic Usage

Start by importing the library:

import cryptocompare

Setting Up Your API Key

If you have a CryptoCompare API key:

  1. Environment Variable: Set it as CRYPTOCOMPARE_API_KEY.
  2. Manual Configuration:

    cryptocompare.cryptocompare._set_api_key_parameter("YOUR_API_KEY_HERE")

Core Functions

1. Fetching Coin List

Retrieve a list of all supported cryptocurrencies:

coin_list = cryptocompare.get_coin_list(format=False)
# Returns detailed metadata for each coin (e.g., Bitcoin's algorithm, supply, etc.)

formatted_list = cryptocompare.get_coin_list(format=True)
# Returns a simplified list of coin abbreviations (e.g., ['BTC', 'ETH'])

2. Real-Time Price Queries

Get current prices for one or multiple cryptocurrencies:

# Single coin, default currency (USD)
cryptocompare.get_price('BTC')

# Single coin with specific currency and full data
cryptocompare.get_price('BTC', currency='USD', full=True)

# Multiple coins and currencies
cryptocompare.get_price(['BTC', 'ETH'], ['EUR', 'GBP'])
# Output: {'BTC': {'EUR': 3709.04, 'GBP': 3354.78}, 'ETH': {'EUR': 258.1, 'GBP': 241.25}}

3. Historical Price Data

Daily Data

cryptocompare.get_historical_price_day('BTC', currency='EUR', limit=30)

Hourly Data

cryptocompare.get_historical_price_hour('BTC', 'EUR', limit=24, toTs=datetime.datetime(2023, 6, 6, 12))

Minute-by-Minute Data

cryptocompare.get_historical_price_minute('BTC', currency='EUR', limit=1440)

4. Market Averages

Calculate weighted averages across exchanges:

avg_price = cryptocompare.get_avg('BTC', currency='EUR', exchange='Kraken')
# Returns metrics like 24-hour volume, price change, and last trade details.

5. Exchange and Pair Data

List all exchanges or trading pairs:

exchanges = cryptocompare.get_exchanges()
pairs = cryptocompare.get_pairs(exchange='Kraken')

Development Guide

Running Tests

Ensure you have an API key to avoid rate limits:

pip3 install -r requirements.txt
python3 -m pytest

FAQ

Q1: Why am I getting rate-limited?

A: Free-tier API keys have usage limits. Upgrade to a paid plan or cache responses to reduce calls.

Q2: How do I fetch data for a custom timestamp?

A: Use toTs parameter with a datetime object or Unix timestamp:

cryptocompare.get_historical_price_hour('BTC', 'USD', toTs=datetime.datetime(2023, 1, 1))

Q3: Can I get OHLCV (Open-High-Low-Close-Volume) data?

A: Yes! Use get_historical_price_* functions with exchange='CCCAGG' for consolidated metrics.


Credit & Disclaimer

๐Ÿ‘‰ Explore advanced trading tools for real-time market analysis.

๐Ÿ‘‰ Learn how to secure your API keys with best practices.