-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathberkshire_portfolio.py
More file actions
57 lines (44 loc) · 1.9 KB
/
berkshire_portfolio.py
File metadata and controls
57 lines (44 loc) · 1.9 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
"""
Example: Berkshire Hathaway 13F Portfolio
Prints Berkshire Hathaway's full institutional portfolio sorted by position
size, showing the total portfolio value and each holding with its weight.
Get your free API key:
https://rapidapi.com/dapdev-dapdev-default/api/sec-edgar-financial-data-api
"""
import os
from edgar_client import EdgarAPI
API_KEY = os.environ.get("RAPIDAPI_KEY", "YOUR_API_KEY_HERE")
def main():
with EdgarAPI(api_key=API_KEY) as api:
print("Fetching Berkshire Hathaway 13F holdings...\n")
portfolio = api.holdings(EdgarAPI.BERKSHIRE, limit=100)
sep = "=" * 60
print(sep)
print(" " + portfolio.company_name)
print(" Report Date : " + portfolio.report_date)
print(" Total Value : $" + f"{portfolio.total_value_billions:.2f}B")
print(" # Holdings : " + str(portfolio.total_holdings))
print(sep + "\n")
col_rank = "Rank"
col_company = "Company"
col_value = "Value ($M)"
col_weight = "Weight"
col_shares = "Shares"
print(f"{col_rank:<5} {col_company:<35} {col_value:<14} {col_weight:<8} {col_shares:>12}")
print("-" * 80)
sorted_holdings = sorted(
portfolio.holdings, key=lambda h: h.value_usd, reverse=True
)
total = portfolio.total_value_usd or 1
for rank, h in enumerate(sorted_holdings, start=1):
weight = h.value_usd / total * 100
shares_str = f"{h.shares:,}"
val_str = "$" + f"{h.value_millions:,.1f}"
weight_str = f"{weight:.2f}%"
name = h.name_of_issuer[:34]
print(f"{rank:<5} {name:<35} {val_str:<14} {weight_str:<8} {shares_str:>12}")
top5_val = sum(h.value_usd for h in portfolio.top(5))
pct = top5_val / total * 100
print(f"\nTop 5 positions account for {pct:.1f}% of the portfolio.")
if __name__ == "__main__":
main()