-
Notifications
You must be signed in to change notification settings - Fork 0
/
brapi_wrapper.py
74 lines (62 loc) · 2.02 KB
/
brapi_wrapper.py
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
import requests
import pandas as pd
from datetime import datetime, timedelta
import os
from dotenv import load_dotenv
import logging
from typing import Union, List, Dict, Optional
from tools import (
get_quote_list,
get_quote,
get_balance_sheet_history,
get_income_statements,
get_financial_data,
get_default_key_statistics,
get_inflation,
get_prime_rate
)
logger = logging.getLogger(__name__)
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s'
)
def get_balance_sheet_data(ticker: str) -> Optional[Dict]:
"""Get balance sheet data for a Brazilian stock."""
try:
load_dotenv(override=True)
BASE_URL = "https://brapi.dev/api"
API_KEY = os.getenv("BRAPI_TOKEN")
if not API_KEY:
raise ValueError("BRAPI_TOKEN not found in environment variables")
url = f"{BASE_URL}/fundamental/balance_sheet"
params = {
'token': API_KEY,
'ticker': ticker,
'interval': 'yearly'
}
annual_data = None
quarterly_data = None
# Get annual data
response = requests.get(url, params=params)
if response.status_code == 200:
data = response.json()
if 'results' in data and data['results']:
annual_data = data['results']
# Get quarterly data
params['interval'] = 'quarterly'
response = requests.get(url, params=params)
if response.status_code == 200:
data = response.json()
if 'results' in data and data['results']:
quarterly_data = data['results']
if not annual_data and not quarterly_data:
logger.error("No balance sheet statements found in response")
return None
return {
'annual': annual_data,
'quarterly': quarterly_data
}
except Exception as e:
logger.error(f"Error getting balance sheet data: {str(e)}")
return None