Repository navigation
Expand file tree
/
Copy pathtest_crypto_apis.py
More file actions
386 lines (333 loc) · 14.3 KB
/
Copy pathtest_crypto_apis.py
File metadata and controls
386 lines (333 loc) · 14.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
#!/usr/bin/env python3
"""
Test module to compare CoinGecko and CoinMarketCap API responses
for cryptocurrency price data.
"""
import os
import sys
import time
from datetime import datetime
from pprint import pprint
import pandas as pd
import requests
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
# Configure logging
import logging
logging.basicConfig(
level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
logger = logging.getLogger(__name__)
# Add project to path
project_root_path = os.path.abspath(os.path.dirname(__file__))
sys.path.append(project_root_path)
# CoinMarketCap API Configuration
CMC_API_KEY = "6e1f5ef3-47fc-4288-9b3e-f02a7493be6a" # API key provided
CMC_BASE_URL_V1 = "https://pro-api.coinmarketcap.com/v1" # For quotes
CMC_BASE_URL_V2 = "https://pro-api.coinmarketcap.com/v2" # For historical
# CoinGecko API Configuration
COINGECKO_BASE_URL = "https://api.coingecko.com/api/v3"
def fetch_coinmarketcap_latest_quote(symbol="BTC"):
"""Fetch latest cryptocurrency quote data from CoinMarketCap API (v1)."""
logger.info(f"Fetching latest {symbol} quote from CoinMarketCap...")
url = f"{CMC_BASE_URL_V1}/cryptocurrency/quotes/latest"
headers = {"X-CMC_PRO_API_KEY": CMC_API_KEY, "Accept": "application/json"}
params = {"symbol": symbol, "convert": "USD"}
try:
response = requests.get(url, headers=headers, params=params)
response.raise_for_status()
data = response.json()
if "data" in data and symbol in data["data"]:
coin_data = data["data"][symbol]
quote = coin_data["quote"]["USD"]
result = {
"source": "coinmarketcap",
"timestamp": datetime.now().isoformat(),
"symbol": symbol,
"name": coin_data["name"],
"price_usd": quote["price"],
"market_cap_usd": quote["market_cap"],
"volume_24h_usd": quote["volume_24h"],
"price_change_24h_pct": quote["percent_change_24h"],
"additional_data": {
"circulating_supply": coin_data["circulating_supply"],
"total_supply": coin_data["total_supply"],
"max_supply": coin_data["max_supply"],
"price_change_1h": quote["percent_change_1h"],
"price_change_7d": quote["percent_change_7d"],
"price_change_30d": quote["percent_change_30d"],
},
}
return result
else:
logger.error(
f"Unexpected response structure from CoinMarketCap (latest quote): {data}"
)
return None
except requests.exceptions.RequestException as e:
logger.error(f"Error fetching latest quote from CoinMarketCap: {e}")
if hasattr(e, "response") and e.response is not None:
logger.error(
f"Response status: {e.response.status_code}, Response text: {e.response.text}"
)
return None
def fetch_coinmarketcap_historical_data(symbol="BTC", count=30, interval="daily"):
"""Fetch historical OHLCV data from CoinMarketCap API (v2)."""
logger.info(
f"Fetching historical (last {count} {interval}) {symbol} OHLCV data from CoinMarketCap..."
)
url = f"{CMC_BASE_URL_V2}/cryptocurrency/ohlcv/historical"
headers = {"X-CMC_PRO_API_KEY": CMC_API_KEY, "Accept": "application/json"}
params = {
"symbol": symbol,
"count": count,
"interval": interval, # e.g., "daily", "weekly", "hourly", "5m", "15m", "30m", "1h", "2h", "4h", "6h", "12h", "1d"
"convert": "USD",
}
try:
response = requests.get(url, headers=headers, params=params)
response.raise_for_status() # Raise HTTPError for bad responses (4xx or 5xx)
data = response.json()
if "data" in data and symbol in data["data"]:
coin_data = data["data"][symbol]
if "quotes" in coin_data:
logger.info(
f"Successfully fetched {len(coin_data['quotes'])} historical data points from CMC."
)
# Structure the data slightly for easier comparison/use
ohlcv_data = []
for quote_entry in coin_data["quotes"]:
quote_usd = quote_entry.get("quote", {}).get("USD", {})
ohlcv_data.append(
{
"timestamp": quote_entry.get("time_close"),
"open": quote_usd.get("open"),
"high": quote_usd.get("high"),
"low": quote_usd.get("low"),
"close": quote_usd.get("close"),
"volume": quote_usd.get("volume"),
"market_cap": quote_usd.get(
"market_cap"
), # Historical market cap might be useful
}
)
return {
"source": "coinmarketcap_historical",
"symbol": symbol,
"interval": interval,
"count": len(ohlcv_data),
"ohlcv": ohlcv_data, # List of dicts
}
else:
logger.error(
f"No 'quotes' found in CoinMarketCap historical response for {symbol}: {coin_data}"
)
return None
else:
logger.error(
f"Unexpected response structure from CoinMarketCap (historical): {data}"
)
return None
except requests.exceptions.RequestException as e:
logger.error(f"Error fetching historical data from CoinMarketCap: {e}")
if hasattr(e, "response") and e.response is not None:
logger.error(
f"Response status: {e.response.status_code}, Response text: {e.response.text}"
)
return None
def fetch_coingecko_latest_data(coin_id="bitcoin"):
"""Fetch latest cryptocurrency data from CoinGecko API."""
logger.info(f"Fetching {coin_id} data from CoinGecko...")
url = f"{COINGECKO_BASE_URL}/coins/{coin_id}"
params = {
"localization": "false",
"tickers": "false",
"market_data": "true",
"community_data": "false",
"developer_data": "false",
}
try:
response = requests.get(url, params=params)
response.raise_for_status()
data = response.json()
if "market_data" in data:
market_data = data["market_data"]
result = {
"source": "coingecko",
"timestamp": datetime.now().isoformat(),
"symbol": data["symbol"].upper(),
"name": data["name"],
"price_usd": market_data["current_price"]["usd"],
"market_cap_usd": market_data["market_cap"]["usd"],
"volume_24h_usd": market_data["total_volume"]["usd"],
"price_change_24h_pct": market_data["price_change_percentage_24h"],
"additional_data": {
"circulating_supply": market_data.get("circulating_supply"),
"total_supply": market_data.get("total_supply"),
"max_supply": market_data.get("max_supply"),
"price_change_1h": market_data.get(
"price_change_percentage_1h_in_currency", {}
).get("usd"),
"price_change_7d": market_data.get("price_change_percentage_7d"),
"price_change_30d": market_data.get("price_change_percentage_30d"),
"ath": market_data.get("ath", {}).get("usd"),
"low_24h": market_data.get("low_24h", {}).get("usd"),
"high_24h": market_data.get("high_24h", {}).get("usd"),
},
}
return result
else:
logger.error(f"Unexpected response structure from CoinGecko: {data}")
return None
except requests.exceptions.RequestException as e:
logger.error(f"Error fetching data from CoinGecko: {e}")
if hasattr(e, "response") and hasattr(e.response, "text"):
logger.error(f"Response: {e.response.text}")
return None
def compare_latest_api_data(crypto_symbol="BTC", coingecko_id="bitcoin"):
"""Compare LATEST data from both APIs side by side."""
logger.info(f"\n=== Comparing LATEST data for {crypto_symbol} ({coingecko_id}) ===")
# Fetch data from both APIs
cmc_data = fetch_coinmarketcap_latest_quote(crypto_symbol)
# Add a small delay to avoid rate limits
time.sleep(1)
cg_data = fetch_coingecko_latest_data(coingecko_id)
if not cmc_data or not cg_data:
logger.error("Could not fetch data from one or both APIs")
return
# Create comparison table
comparison = {
"Source": ["CoinMarketCap", "CoinGecko"],
"Price (USD)": [cmc_data["price_usd"], cg_data["price_usd"]],
"Market Cap (USD)": [cmc_data["market_cap_usd"], cg_data["market_cap_usd"]],
"24h Volume (USD)": [cmc_data["volume_24h_usd"], cg_data["volume_24h_usd"]],
"24h Change (%)": [
cmc_data["price_change_24h_pct"],
cg_data["price_change_24h_pct"],
],
"7d Change (%)": [
cmc_data["additional_data"]["price_change_7d"],
cg_data["additional_data"]["price_change_7d"],
],
"Circulating Supply": [
cmc_data["additional_data"]["circulating_supply"],
cg_data["additional_data"]["circulating_supply"],
],
}
# Calculate differences
diff = {
"Price Diff (%)": calculate_percentage_diff(
cmc_data["price_usd"], cg_data["price_usd"]
),
"Market Cap Diff (%)": calculate_percentage_diff(
cmc_data["market_cap_usd"], cg_data["market_cap_usd"]
),
"Volume Diff (%)": calculate_percentage_diff(
cmc_data["volume_24h_usd"], cg_data["volume_24h_usd"]
),
}
# Convert to pandas DataFrame for pretty printing
df = pd.DataFrame(comparison)
# Print raw data for inspection
print(f"\n--- Raw LATEST {crypto_symbol} Data ---")
print("\nCoinMarketCap Latest Quote:")
pprint(cmc_data)
print("\nCoinGecko Latest Data:")
pprint(cg_data)
# Print comparison
print(f"\n--- LATEST {crypto_symbol} Comparison ---")
print(df.to_string(index=False))
# Print differences
print("\nDifferences:")
for key, value in diff.items():
print(f"{key}: {value:.2f}%")
return {
"coinmarketcap": cmc_data,
"coingecko": cg_data,
"comparison": df,
"differences": diff,
}
def calculate_percentage_diff(a, b):
"""Calculate percentage difference between two numbers, handling None."""
if a is None or b is None:
return None # Cannot compare if one value is missing
if a == 0 and b == 0:
return 0.0
if a == 0:
# Avoid division by zero, return large number or specific indicator?
# Returning None might be safer if interpretation is unclear.
return None
try:
# Ensure they are floats for calculation
a_f = float(a)
b_f = float(b)
return ((b_f - a_f) / abs(a_f)) * 100.0
except (ValueError, TypeError):
return None # Return None if conversion fails
def test_historical_data(crypto_symbol="BTC", count=30):
"""Test fetching and display historical data from CMC."""
logger.info(
f"\n=== Testing HISTORICAL data fetch for {crypto_symbol} (last {count} days) ==="
)
cmc_hist_data = fetch_coinmarketcap_historical_data(
symbol=crypto_symbol, count=count, interval="daily"
)
if cmc_hist_data and cmc_hist_data.get("ohlcv"):
print(
f"\n--- Historical Data Structure (First 2 points) for {crypto_symbol} from CoinMarketCap ---"
)
pprint(cmc_hist_data["ohlcv"][:2])
print(f"\nTotal historical points fetched: {cmc_hist_data['count']}")
# Check if essential keys are present in the first record
if cmc_hist_data["ohlcv"]:
first_record = cmc_hist_data["ohlcv"][0]
required_keys = ["timestamp", "open", "high", "low", "close", "volume"]
missing_keys = [
key
for key in required_keys
if key not in first_record or first_record[key] is None
]
if not missing_keys:
print(
"\nEssential OHLCV keys (timestamp, open, high, low, close, volume) seem present in the first record."
)
else:
print(
f"\nWARNING: Missing essential OHLCV keys in first record: {missing_keys}"
)
else:
print("\nWARNING: No historical OHLCV data points returned.")
else:
print(
f"\nFailed to fetch or parse historical data for {crypto_symbol} from CoinMarketCap."
)
def main():
"""Run comparison for major cryptocurrencies and test historical fetch."""
crypto_pairs = [
{"symbol": "BTC", "coingecko_id": "bitcoin"},
{"symbol": "ETH", "coingecko_id": "ethereum"},
{"symbol": "SOL", "coingecko_id": "solana"},
{"symbol": "BNB", "coingecko_id": "binancecoin"},
]
# --- Compare Latest Data ---
logger.info("\n\n*** Comparing LATEST Data from APIs ***")
for pair in crypto_pairs:
try:
compare_latest_api_data(pair["symbol"], pair["coingecko_id"])
print("\n" + "=" * 50 + "\n") # Separator
time.sleep(2) # Delay
except Exception as e:
logger.error(f"Error comparing latest data for {pair['symbol']}: {e}")
# --- Test Historical Data Fetch (CoinMarketCap) ---
logger.info("\n\n*** Testing HISTORICAL Data Fetch from CoinMarketCap ***")
for pair in crypto_pairs:
try:
# Test fetching last 30 days of daily data
test_historical_data(crypto_symbol=pair["symbol"], count=30)
print("\n" + "=" * 50 + "\n") # Separator
time.sleep(2) # Delay
except Exception as e:
logger.error(f"Error testing historical fetch for {pair['symbol']}: {e}")
if __name__ == "__main__":
main()