Building a cryptocurrency exchange comparison tool using Market Data API
If you want to develop a tool for comparing cryptocurrency prices across different exchanges using our Market Data API, follow these simple steps.
-
API Integration
Integrate our Market Data API into your application to access real-time data from diverse cryptocurrency exchanges simply by configuring the API KEY.
-
Fetch Data
Our API provides data such as the current price, trading volume, and historical prices of different cryptocurrencies across multiple exchanges.
Within this how-to guide, we'll use the current quotes endpoint to obtain the present asset price across diverse exchanges.
Quote refers to the current price at which a particular asset is bought or sold on a specific exchange.
- Data Comparison
After acquiring the data, you can compare prices for a specific asset across various exchanges and showcase them. This could prove beneficial for analyzing trends.
Cryptocurrency exchange comparison tool in Python
Let’s put the theory into practice.
Here’s an example of comparing real-time prices based on current quotes
with our Market Data API in Python.
- Import the necessary libraries and set up your API key.
import requests
import json
api_key = 'YOUR_API_KEY'
- Define the
PriceInformation
class to store price data.
class PriceInformation:
def __init__(self, symbol, price, size, taker_side):
self.symbol = symbol
self.price = price
self.size = size
self.taker_side = taker_side
- Define the function to fetch price information for a given coinapi symbol via the current quotes endpoint.
def get_price(coinapiSymbol):
url = f'https://rest.coinapi.io/v1/quotes/{coinapiSymbol}/current?apikey={api_key}'
response = requests.get(url)
data = json.loads(response.text)
last_trade = data.get('last_trade', {})
if last_trade:
price = last_trade.get('price', '')
size = last_trade.get('size', '')
taker_side = last_trade.get('taker_side', '')
return PriceInformation(coinapiSymbol, price, size, taker_side)
return None
Refer to our metadata documentation for the complete list of coinapi symbols available on the specified exchange.
- Define the function to fetch price information for two symbols.
def fetch_prices_for_symbols(symbol1, symbol2):
priceSymbol1 = get_price(symbol1)
priceSymbol2 = get_price(symbol2)
return priceSymbol1, priceSymbol2
- Call
fetch_prices_for_symbols
to retrieve priceBTC/USDT
ANDETH/USDT
onKRAKEN
andBINANCE
.
# Example usage 1
priceInfo1, priceInfo2 = fetch_prices_for_symbols('KRAKEN_SPOT_BTC_USDT', 'BINANCE_SPOT_BTC_USDT')
print(f'Price on [{priceInfo1.symbol}] is [{priceInfo1.price}], based on the last [{priceInfo1.taker_side}] with a size of [{priceInfo1.size}]')
print(f'Price on [{priceInfo2.symbol}] is [{priceInfo2.price}], based on the last [{priceInfo2.taker_side}] with a size of [{priceInfo2.size}]')
# Example usage 2
priceInfo3, priceInfo4 = fetch_prices_for_symbols('KRAKEN_SPOT_ETH_USDT', 'BINANCE_SPOT_ETH_USDT')
print(f'Price on [{priceInfo3.symbol}] is [{priceInfo3.price}], based on the last [{priceInfo3.taker_side}] with a size of [{priceInfo3.size}]')
print(f'Price on [{priceInfo4.symbol}] is [{priceInfo4.price}], based on the last [{priceInfo4.taker_side}] with a size of [{priceInfo4.size}]')
- Output of the script.
> Price on [KRAKEN_SPOT_BTC_USDT] is [67739.9], based on the last [BUY] with a size of [0.00469343]
> Price on [BINANCE_SPOT_BTC_USDT] is [67791.98], based on the last [SELL] with a size of [0.00168]
> Price on [KRAKEN_SPOT_ETH_USDT] is [3531.67], based on the last [BUY] with a size of [0.00290074]
> Price on [BINANCE_SPOT_ETH_USDT] is [3532.58], based on the last [BUY] with a size of [0.0002]
- Here's a complete Python script encompassing all the preceding steps.
import requests
import json
# Set up your API key
api_key = 'YOUR_API_KEY'
# Define the PriceInformation class to store price data
class PriceInformation:
def __init__(self, symbol, price, size, taker_side):
self.symbol = symbol
self.price = price
self.size = size
self.taker_side = taker_side
# Function to fetch price information for a given symbol
def get_price(coinapiSymbol):
url = f'https://rest.coinapi.io/v1/quotes/{coinapiSymbol}/current?apikey={api_key}'
response = requests.get(url)
data = json.loads(response.text)
last_trade = data.get('last_trade', {})
if last_trade:
price = last_trade.get('price', '')
size = last_trade.get('size', '')
taker_side = last_trade.get('taker_side', '')
return PriceInformation(coinapiSymbol, price, size, taker_side)
return None
# Function to fetch prices for two symbols
def fetch_prices_for_symbols(symbol1, symbol2):
priceSymbol1 = get_price(symbol1)
priceSymbol2 = get_price(symbol2)
return priceSymbol1, priceSymbol2
# Example usage 1
priceInfo1, priceInfo2 = fetch_prices_for_symbols('KRAKEN_SPOT_BTC_USDT', 'BINANCE_SPOT_BTC_USDT')
print(f'Price on [{priceInfo1.symbol}] is [{priceInfo1.price}], based on the last [{priceInfo1.taker_side}] with a size of [{priceInfo1.size}]')
print(f'Price on [{priceInfo2.symbol}] is [{priceInfo2.price}], based on the last [{priceInfo2.taker_side}] with a size of [{priceInfo2.size}]')
# Example usage 2
priceInfo3, priceInfo4 = fetch_prices_for_symbols('KRAKEN_SPOT_ETH_USDT', 'BINANCE_SPOT_ETH_USDT')
print(f'Price on [{priceInfo3.symbol}] is [{priceInfo3.price}], based on the last [{priceInfo3.taker_side}] with a size of [{priceInfo3.size}]')
print(f'Price on [{priceInfo4.symbol}] is [{priceInfo4.price}], based on the last [{priceInfo4.taker_side}] with a size of [{priceInfo4.size}]')
Here's a simplified way to fetch the current price for a given asset using the quotes endpoint
and present it for comparison.
To access the last 100 quotes
instead of just the current one, you can utilize our quotes/latest endpoint.
In a real-world scenario, additional functionalities such as error handling, data formatting, and data storage for analysis would be necessary.