-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_fmp_api.py
More file actions
75 lines (67 loc) · 2.75 KB
/
test_fmp_api.py
File metadata and controls
75 lines (67 loc) · 2.75 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
#!/usr/bin/env python3
"""Test FMP API for earnings data."""
import os
import requests
from datetime import datetime
from pathlib import Path
# Load .env file
env_path = Path("backtesting/.env")
if env_path.exists():
with open(env_path) as f:
for line in f:
if '=' in line and not line.startswith('#'):
key, value = line.strip().split('=', 1)
os.environ[key] = value
api_key = os.getenv("FMP_API_KEY")
print(f"FMP API Key: {api_key[:5]}...{api_key[-5:] if api_key else 'Not found'}")
# Test earnings calendar API
symbols = ["AAPL", "MSFT", "AMZN", "NVDA", "GOOGL"]
for symbol in symbols:
print(f"\n{symbol}:")
# Try earnings surprises endpoint
url = f"https://financialmodelingprep.com/api/v3/earnings-surprises/{symbol}?apikey={api_key}"
try:
response = requests.get(url, timeout=10)
print(f" Status: {response.status_code}")
if response.status_code == 200:
data = response.json()
if isinstance(data, list) and len(data) > 0:
print(f" Found {len(data)} earnings records")
for item in data[:3]:
print(f" Date: {item.get('date')}, Surprise: {item.get('actualEarningResult')} vs {item.get('estimatedEarning')}")
else:
print(f" No data or unexpected format: {type(data)}")
else:
print(f" Error: {response.text[:200]}")
except Exception as e:
print(f" Exception: {e}")
# Try earnings calendar endpoint
url2 = f"https://financialmodelingprep.com/api/v3/earnings_calendar/{symbol}?apikey={api_key}"
try:
response = requests.get(url2, timeout=10)
if response.status_code == 200:
data = response.json()
print(f" Calendar endpoint: {len(data) if isinstance(data, list) else 'N/A'} records")
except Exception as e:
pass
# Try general earnings calendar
print("\n\nGeneral Earnings Calendar (2022):")
url = f"https://financialmodelingprep.com/api/v3/earnings_calendar?from=2022-01-01&to=2022-06-30&apikey={api_key}"
try:
response = requests.get(url, timeout=10)
print(f"Status: {response.status_code}")
if response.status_code == 200:
data = response.json()
if isinstance(data, list):
print(f"Total earnings events: {len(data)}")
# Count by symbol
symbol_counts = {}
for item in data[:100]: # First 100
sym = item.get('symbol')
if sym in symbols:
symbol_counts[sym] = symbol_counts.get(sym, 0) + 1
print(f"Events for target symbols: {symbol_counts}")
else:
print(f"Unexpected format: {type(data)}")
except Exception as e:
print(f"Exception: {e}")