-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
130 lines (104 loc) · 4.26 KB
/
main.py
File metadata and controls
130 lines (104 loc) · 4.26 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
# main.py
"""
Command-line orchestrator for the portfolio system.
This file:
- Pulls static positions from Static_Portfolio (transactions.csv)
- Computes live metrics via Market_Data (yfinance)
- Prints a human-readable summary to the console
- Optionally generates interactive matplotlib charts via visuals
Only the main() function is intended as the public entrypoint.
"""
from __future__ import annotations
from Static_Portfolio import (
today, # returns today's date and updates Last_Updated
owned_stocks, # builds current portfolio positions from files
Portfolio_Buy_Total, # computes total cost basis of the portfolio
Stock_Holding, # computes holding duration (in days) for each position
)
from Market_Data import (
unrealized_pl, # per-ticker unrealized P/L using live prices
portfolio_performance_summary, # aggregate market value & P/L summary
Sector_Allocation, # sector allocation by cost/market
compare_to_sp500, # compares portfolio return vs S&P 500
)
import visuals # plotting functions (matplotlib)
def main() -> None:
"""
Run a full sanity check of the portfolio system and show key charts."""
print("========== PORTFOLIO TEST RUN ==========\n")
# 1) Date / last updated
current_date, last_updated = today()
print(f"Today: {current_date}")
print(f"Last_Updated: {last_updated}\n")
# 2) Static portfolio (from transactions.csv)
df_positions, tickers = owned_stocks()
if df_positions.empty or not tickers:
print("No positions found. Check your data/transactions.csv file.")
print("========== END OF TEST RUN ==========\n")
return
print("---- Current Positions (Static from transactions.csv) ----")
print(df_positions.to_string(index=False))
print()
# 3) Total cost basis
total_cost = Portfolio_Buy_Total()
print("---- Total Cost Basis ----")
print(f"Total invested (cost basis): ${total_cost:,.2f}\n")
# 4) Holding durations
print("---- Holding Durations (Days) ----")
holding_df = Stock_Holding()
if not holding_df.empty:
print(holding_df.to_string(index=False))
else:
print("No holding duration data available.")
print()
# 5) Live unrealized P/L
print("---- Unrealized P/L (Live Prices via yfinance) ----")
pl_df = unrealized_pl()
if not pl_df.empty:
print(pl_df.to_string(index=False))
else:
print("No live P/L data available (perhaps no prices could be fetched).")
print()
# 6) Portfolio performance summary
print("---- Portfolio Performance Summary ----")
summary = portfolio_performance_summary()
for k, v in summary.items():
if isinstance(v, (float, int)):
print(f"{k}: {v:,.2f}")
else:
print(f"{k}: {v}")
print()
# 7) Sector allocation
print("---- Sector Allocation by Cost ----")
sector_df = Sector_Allocation(by="cost") # default: by cost basis
if not sector_df.empty:
print(sector_df.to_string(index=False))
else:
print("No sector allocation data available.")
print()
# 8) Benchmark comparison
print("---- Benchmark vs S&P 500 ----")
bench = compare_to_sp500(period="1y")
for k, v in bench.items():
if isinstance(v, (float, int)):
print(f"{k}: {v:,.2f}")
else:
print(f"{k}: {v}")
print()
# 9) Visualizations (interactive matplotlib windows)
print("Generating charts... (close each window to continue)\n")
# Sector allocation bar chart
visuals.plot_sector_allocation(by="cost")
# Unrealized P/L by ticker
visuals.plot_unrealized_pl_bar()
# Portfolio value over time (1 year)
visuals.plot_portfolio_value_over_time(period="1y")
# Portfolio vs S&P 500 comparison
visuals.plot_benchmark_comparison(period="1y")
# Optional extras (useful for deeper inspection)
visuals.plot_allocation_by_ticker(by="market")
visuals.plot_realized_vs_unrealized()
visuals.plot_correlation_heatmap(period="1y")
print("========== END OF TEST RUN ==========\n")
if __name__ == "__main__":
main()