-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpersonal_finance.py
818 lines (687 loc) · 28.4 KB
/
personal_finance.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
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
import streamlit as st
import pandas as pd
import plotly.graph_objects as go
from datetime import datetime
from dateutil.relativedelta import relativedelta
import os
# Page configuration
st.set_page_config(layout="wide", page_title="Finance Dashboard", page_icon="💰")
# Enhanced CSS Styling with enforced dark theme
st.markdown("""
<style>
/* Force dark theme */
[data-testid="stAppViewContainer"],
[data-testid="stHeader"],
section[data-testid="stSidebar"],
[data-testid="stToolbar"] {
background-color: #0e1117 !important;
color: #ffffff !important;
}
/* Headers */
h1, h2, h3, h4, h5, h6, .stMarkdown h1, .stMarkdown h2, .stMarkdown h3 {
color: #ffffff !important;
font-weight: 600 !important;
}
/* Ensure dark theme for all text elements */
p, span, div, label, .stMarkdown p {
color: #ffffff !important;
}
/* Style metrics containers */
[data-testid="stMetric"] {
background-color: #1e1e1e !important;
padding: 1rem !important;
border-radius: 0.5rem !important;
border: 1px solid #2d2d2d !important;
}
/* Style metric values and labels */
[data-testid="stMetricValue"] {
color: #ffffff !important;
font-size: 1.2rem !important;
}
/* Enhanced Select/Dropdown styling */
select,
.stSelectbox > div > div > select,
div[data-baseweb="select"] > div,
div[data-baseweb="select"] input {
background-color: #262730 !important;
color: #ffffff !important;
border-color: #404040 !important;
}
/* Style all dropdown-related elements */
div[data-baseweb="popover"] {
background-color: #262730 !important;
}
div[data-baseweb="popover"] * {
background-color: #262730 !important;
color: #ffffff !important;
}
div[data-baseweb="select"] * {
background-color: #262730 !important;
color: #ffffff !important;
}
/* Style the dropdown options container */
div[role="listbox"] {
background-color: #262730 !important;
border: 1px solid #404040 !important;
}
/* Style individual dropdown items */
div[role="option"] {
background-color: #262730 !important;
color: #ffffff !important;
}
/* Style dropdown hover state */
div[role="option"]:hover {
background-color: #404040 !important;
}
/* Style selected option */
div[aria-selected="true"] {
background-color: #404040 !important;
color: #ffffff !important;
}
/* Fix for white background in dropdowns */
.stSelectbox > div,
.stSelectbox > div > div {
background-color: #262730 !important;
}
/* Style the select value container */
div[data-baseweb="select"] div[data-testid="stMarkdown"] {
background-color: #262730 !important;
color: #ffffff !important;
}
[data-testid="stMetricDelta"] {
color: #00ff88 !important;
}
[data-testid="stMetricLabel"] {
color: #cccccc !important;
}
/* Style buttons consistently */
button, [data-testid="baseButton-secondary"] {
background-color: #262730 !important;
color: #ffffff !important;
border: 1px solid #404040 !important;
}
button:hover {
border-color: #00ff88 !important;
}
/* Style expanders */
[data-testid="stExpander"] {
background-color: #1e1e1e !important;
border-radius: 0.5rem !important;
border: 1px solid #2d2d2d !important;
}
/* Form inputs */
input, .stTextInput > div > div > input,
.stNumberInput > div > div > input,
.stSelectbox > div > div > select {
background-color: #262730 !important;
color: #ffffff !important;
border-color: #404040 !important;
}
/* Selectbox */
.stSelectbox > div > div::before {
background-color: #262730 !important;
}
/* Dataframe styling */
.dataframe {
background-color: #1e1e1e !important;
color: #ffffff !important;
}
.dataframe th {
background-color: #262730 !important;
color: #ffffff !important;
}
.dataframe td {
background-color: #1e1e1e !important;
color: #ffffff !important;
}
/* Force white text for all markdown */
.stMarkdown {
color: #ffffff !important;
}
/* Style tooltips */
.tooltip {
background-color: #1e1e1e !important;
color: #ffffff !important;
}
/* Override any Streamlit defaults */
.stApp {
background-color: #0e1117 !important;
}
.streamlit-expanderHeader {
color: #ffffff !important;
}
/* Sidebar */
[data-testid="stSidebarNav"] {
background-color: #0e1117 !important;
}
[data-testid="stSidebarNav"] li {
background-color: #1e1e1e !important;
}
/* Mobile-specific adjustments */
@media (max-width: 768px) {
.stMetric {
padding: 0.75rem !important;
}
[data-testid="stMetricValue"] {
font-size: 1rem !important;
}
.main .block-container {
padding: 1rem !important;
}
}
</style>
""", unsafe_allow_html=True)
# Get current month for default selection
current_month = datetime.now().strftime("%B")
months = [
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
]
# File for saving data
file_path = "finance_data.csv"
# Initialize data storage
if "data" not in st.session_state:
if os.path.exists(file_path):
st.session_state.data = pd.read_csv(file_path)
else:
st.session_state.data = pd.DataFrame(columns=[
"Month", "Year", "Income Salary", "Income plus", "Expenses Day-to-day",
"Expenses rent", "Expenses loan", "Expenses market",
"Expenses taxes", "Expenses mortgage", "Monthly left"
])
if "yearly_return" not in st.session_state:
st.session_state.yearly_return = 7 # Default value
def update_yearly_return():
st.session_state.yearly_return = st.session_state.yearly_return_slider
def calculate_portfolio_value(df, yearly_return):
if df.empty:
return 0, 0, 0, 0, 0
monthly_rate = (1 + yearly_return / 100) ** (1 / 12) - 1
current_date = datetime.now()
total_invested = 0
current_portfolio_value = 0
df['Date'] = pd.to_datetime(df.apply(lambda x: f"{x['Month']} {x['Year']}", axis=1))
df_sorted = df.sort_values('Date')
for _, row in df_sorted.iterrows():
investment_date = row['Date']
investment_amount = row['Expenses market']
if investment_amount <= 0:
continue
months_passed = relativedelta(current_date, investment_date).months + \
12 * relativedelta(current_date, investment_date).years
current_value = investment_amount * (1 + monthly_rate) ** months_passed
total_invested += investment_amount
current_portfolio_value += current_value
portfolio_gains = current_portfolio_value - total_invested
tax_amount = 0.25 * portfolio_gains if portfolio_gains > 0 else 0
portfolio_after_tax = current_portfolio_value - tax_amount
return current_portfolio_value, total_invested, portfolio_gains, tax_amount, portfolio_after_tax
# Main layout
st.title("💰 Personal Finance Dashboard")
col_input, col_summary, col_projection = st.columns([1.2, 1, 1])
with col_input:
with st.expander("📝 Add New Month Data", expanded=True):
with st.form("add_month_form", clear_on_submit=True):
# Set current month as default
month = st.selectbox("Month",
options=months,
index=months.index(current_month))
current_year = st.number_input("Year", value=datetime.now().year, min_value=2000, max_value=2100)
c1, c2 = st.columns(2)
with c1:
st.subheader("Income")
income_salary = st.number_input("Salary (₪)", value=0, step=100)
income_plus = st.number_input("Additional Income (₪)", value=0, step=100)
with c2:
st.subheader("Expenses")
expenses_day_to_day = st.number_input("Day-to-Day (₪)", value=0, step=100)
expenses_rent = st.number_input("Rent (₪)", value=0, step=100)
expenses_loan = st.number_input("Loan (₪)", value=0, step=100)
expenses_market = st.number_input("Market Investment (₪)", value=0, step=100)
expenses_taxes = st.number_input("Taxes (₪)", value=0, step=100)
expenses_mortgage = st.number_input("Mortgage (₪)", value=0, step=100)
monthly_left = income_salary + income_plus - (
expenses_day_to_day + expenses_rent + expenses_loan +
expenses_market + expenses_taxes + expenses_mortgage
)
submitted = st.form_submit_button("Add Month")
if submitted:
if not st.session_state.data.empty and any((st.session_state.data["Month"] == month) &
(st.session_state.data["Year"] == current_year)):
st.error(f"Data for {month} {current_year} already exists!")
else:
new_row = {
"Month": month,
"Year": current_year,
"Income Salary": income_salary,
"Income plus": income_plus,
"Expenses Day-to-day": expenses_day_to_day,
"Expenses rent": expenses_rent,
"Expenses loan": expenses_loan,
"Expenses market": expenses_market,
"Expenses taxes": expenses_taxes,
"Expenses mortgage": expenses_mortgage,
"Monthly left": monthly_left,
}
st.session_state.data = pd.concat([st.session_state.data, pd.DataFrame([new_row])],
ignore_index=True)
st.session_state.data.to_csv(file_path, index=False)
st.success(f"Added data for {month} {current_year}")
if not st.session_state.data.empty:
st.subheader("📊 Monthly Records")
for index, row in st.session_state.data.iterrows():
col1, col2 = st.columns([0.95, 0.05])
with col1:
st.write(f"{row['Month']} {row['Year']}: ₪{row['Monthly left']:,.2f}")
with col2:
if st.button("🗑", key=f"delete_{index}"):
st.session_state.data = st.session_state.data.drop(index).reset_index(drop=True)
st.session_state.data.to_csv(file_path, index=False)
st.rerun()
with col_summary:
st.subheader("📈 Financial Summary")
if not st.session_state.data.empty:
current_bank_account = st.session_state.data["Monthly left"].sum()
yearly_return = st.slider(
"Yearly Stock Return (%)",
min_value=0,
max_value=20,
value=st.session_state.yearly_return,
key="yearly_return_slider",
on_change=update_yearly_return
)
current_portfolio, total_invested, portfolio_gains, tax_amount, portfolio_after_tax = \
calculate_portfolio_value(st.session_state.data, st.session_state.yearly_return)
overall_assets = current_bank_account + portfolio_after_tax
st.metric("💳 Bank Account", f"₪{current_bank_account:,.2f}")
st.metric("💰 Portfolio Investment", f"₪{total_invested:,.2f}")
st.metric("📈 Portfolio Gains", f"₪{portfolio_gains:,.2f}")
st.metric("💸 Tax Amount (25%)", f"₪{tax_amount:,.2f}")
st.metric("📊 Portfolio After Tax", f"₪{portfolio_after_tax:,.2f}")
st.metric("🏦 Total Assets", f"₪{overall_assets:,.2f}")
def calculate_future_portfolio(
current_portfolio,
yearly_return,
monthly_contribution,
years,
inflation_rate=2.0
):
"""
Calculate future portfolio value with more realistic assumptions.
Parameters:
current_portfolio (float): Current portfolio value
yearly_return (float): Expected yearly return percentage
monthly_contribution (float): Monthly investment amount
years (int): Number of years to project
inflation_rate (float): Expected yearly inflation rate percentage
Returns:
tuple: (nominal_values, real_values, total_invested)
"""
months = years * 12
monthly_rate = (1 + yearly_return / 100) ** (1 / 12) - 1
monthly_inflation = (1 + inflation_rate / 100) ** (1 / 12) - 1
nominal_values = []
real_values = []
total_invested = current_portfolio
portfolio_nominal = current_portfolio
portfolio_real = current_portfolio
for _ in range(months):
# Add monthly contribution
portfolio_nominal += monthly_contribution
portfolio_real += monthly_contribution
# Apply returns
portfolio_nominal *= (1 + monthly_rate)
portfolio_real *= (1 + monthly_rate)
# Apply inflation adjustment to real value
portfolio_real *= (1 - monthly_inflation)
total_invested += monthly_contribution
nominal_values.append(portfolio_nominal)
real_values.append(portfolio_real)
return nominal_values, real_values, total_invested
with col_projection:
st.subheader("🔮 Portfolio Projections")
if not st.session_state.data.empty:
years = st.slider("Projection Years", min_value=1, max_value=30, value=5)
# Tab creation
tab1, tab2 = st.tabs(["Simple Projection", "Detailed Projection"])
# Simple Projection (Original)
with tab1:
st.markdown("##### Current Portfolio Growth")
st.caption("Projects current portfolio value without additional contributions")
future_months = pd.date_range(start=datetime.now(), periods=years * 12, freq="M")
monthly_rate = (1 + st.session_state.yearly_return / 100) ** (1 / 12) - 1
future_portfolio = [current_portfolio]
for _ in range(len(future_months)):
future_portfolio.append(future_portfolio[-1] * (1 + monthly_rate))
fig_simple = go.Figure()
fig_simple.add_trace(go.Scatter(
x=future_months,
y=future_portfolio[1:],
mode='lines',
name='Portfolio Value',
line=dict(color="#00ff88", width=2)
))
fig_simple.update_layout(
margin=dict(l=20, r=20, t=30, b=20),
height=400,
template="plotly_dark",
paper_bgcolor='rgba(0,0,0,0)',
plot_bgcolor='rgba(0,0,0,0)',
font=dict(color='white'),
showlegend=False,
xaxis=dict(
showgrid=True,
gridcolor='rgba(128,128,128,0.2)',
title=None
),
yaxis=dict(
showgrid=True,
gridcolor='rgba(128,128,128,0.2)',
title="Portfolio Value (₪)"
)
)
st.plotly_chart(fig_simple, use_container_width=True)
final_portfolio_value = future_portfolio[-1]
final_gains = final_portfolio_value - total_invested
final_tax = 0.25 * final_gains if final_gains > 0 else 0
final_value_after_tax = final_portfolio_value - final_tax
c1, c2 = st.columns(2)
with c1:
st.metric("Projected Value", f"₪{final_portfolio_value:,.2f}")
st.metric("Projected Tax", f"₪{final_tax:,.2f}")
with c2:
st.metric("Projected Gains", f"₪{final_gains:,.2f}")
st.metric("After Tax Value", f"₪{final_value_after_tax:,.2f}")
# Detailed Projection (New)
with tab2:
st.markdown("##### Advanced Portfolio Projection")
st.caption("Projects future value including monthly contributions and inflation")
# Get the average monthly investment from historical data
avg_monthly_investment = st.session_state.data['Expenses market'].mean()
# Additional inputs for detailed projection
monthly_contribution = st.number_input(
"Monthly Investment (₪)",
value=float(avg_monthly_investment),
step=100.0,
help="Expected monthly contribution to your portfolio"
)
inflation_rate = st.slider(
"Expected Inflation Rate (%)",
min_value=0.0,
max_value=10.0,
value=2.0,
step=0.1,
help="Average annual inflation rate"
)
# Calculate detailed projections
nominal_values, real_values, total_future_invested = calculate_future_portfolio(
current_portfolio=current_portfolio,
yearly_return=st.session_state.yearly_return,
monthly_contribution=monthly_contribution,
years=years,
inflation_rate=inflation_rate
)
# Create the detailed projection chart
fig_detailed = go.Figure()
fig_detailed.add_trace(go.Scatter(
x=future_months,
y=nominal_values,
mode='lines',
name='Nominal Value',
line=dict(color="#00ff88", width=2)
))
fig_detailed.add_trace(go.Scatter(
x=future_months,
y=real_values,
mode='lines',
name='Real Value (Inflation Adjusted)',
line=dict(color="#88ffcc", width=2, dash='dash')
))
fig_detailed.update_layout(
margin=dict(l=20, r=20, t=30, b=20),
height=400,
template="plotly_dark",
paper_bgcolor='rgba(0,0,0,0)',
plot_bgcolor='rgba(0,0,0,0)',
font=dict(color='white'),
legend=dict(
yanchor="top",
y=0.99,
xanchor="left",
x=0.01
),
xaxis=dict(
showgrid=True,
gridcolor='rgba(128,128,128,0.2)',
title=None
),
yaxis=dict(
showgrid=True,
gridcolor='rgba(128,128,128,0.2)',
title="Portfolio Value (₪)"
)
)
st.plotly_chart(fig_detailed, use_container_width=True)
# Calculate and display detailed metrics
final_portfolio_nominal = nominal_values[-1]
final_portfolio_real = real_values[-1]
final_gains = final_portfolio_nominal - total_future_invested
final_tax = 0.25 * final_gains if final_gains > 0 else 0
final_value_after_tax = final_portfolio_nominal - final_tax
c1, c2 = st.columns(2)
with c1:
st.metric(
"Projected Nominal Value",
f"₪{final_portfolio_nominal:,.2f}",
help="Future value without accounting for inflation"
)
st.metric(
"Total Invested",
f"₪{total_future_invested:,.2f}",
help="Current portfolio plus all future contributions"
)
with c2:
st.metric(
"Projected Real Value",
f"₪{final_portfolio_real:,.2f}",
help="Future value adjusted for inflation"
)
st.metric(
"After Tax Value",
f"₪{final_value_after_tax:,.2f}",
help="Nominal value after 25% capital gains tax on profits"
)
if not st.session_state.data.empty:
with st.expander("📋 View Full Data Table"):
st.dataframe(st.session_state.data, use_container_width=True)
st.markdown("---")
st.header("📊 Financial Analytics")
if not st.session_state.data.empty:
# Prepare data
df = st.session_state.data.copy()
df['Date'] = pd.to_datetime(df.apply(lambda x: f"{x['Month']} {x['Year']}", axis=1))
df = df.sort_values('Date')
# Format dates for display
df['Display_Date'] = df['Date'].dt.strftime('%b %Y')
# 1. Income vs Expenses Breakdown
st.subheader("Income vs Expenses Over Time")
st.markdown(
'<p style="font-size: 0.9em; color: #888888;">Shows your total monthly income (green) versus expenses (red), helping you track your spending relative to earnings.</p>',
unsafe_allow_html=True)
total_expenses = df['Expenses Day-to-day'] + df['Expenses rent'] + df['Expenses loan'] + \
df['Expenses market'] + df['Expenses taxes'] + df['Expenses mortgage']
total_income = df['Income Salary'] + df['Income plus']
fig_income_expenses = go.Figure()
fig_income_expenses.add_trace(go.Scatter(
x=df['Display_Date'],
y=total_income,
name='Total Income',
line=dict(color='#00ff88', width=2),
fill='tonexty'
))
fig_income_expenses.add_trace(go.Scatter(
x=df['Display_Date'],
y=total_expenses,
name='Total Expenses',
line=dict(color='#ff4444', width=2),
fill='tonexty'
))
fig_income_expenses.update_layout(
template='plotly_dark',
paper_bgcolor='rgba(0,0,0,0)',
plot_bgcolor='rgba(0,0,0,0)',
margin=dict(l=20, r=20, t=30, b=20),
height=400,
yaxis_title="Amount (₪)",
hovermode='x unified',
xaxis_title=None
)
st.plotly_chart(fig_income_expenses, use_container_width=True)
# 2. Expense Categories Breakdown
col1, col2 = st.columns(2)
with col1:
st.subheader("Expense Distribution")
st.markdown(
'<p style="font-size: 0.9em; color: #888888;">Breakdown of your total expenses by category, showing where most of your money goes.</p>',
unsafe_allow_html=True)
expense_categories = {
'Day-to-day': df['Expenses Day-to-day'].sum(),
'Rent': df['Expenses rent'].sum(),
'Loan': df['Expenses loan'].sum(),
'Market Investment': df['Expenses market'].sum(),
'Taxes': df['Expenses taxes'].sum(),
'Mortgage': df['Expenses mortgage'].sum()
}
fig_pie = go.Figure(data=[go.Pie(
labels=list(expense_categories.keys()),
values=list(expense_categories.values()),
hole=0.4,
marker=dict(colors=['#00ff88', '#00cc88', '#008866', '#ff4444', '#cc4444', '#884444'])
)])
fig_pie.update_layout(
template='plotly_dark',
paper_bgcolor='rgba(0,0,0,0)',
plot_bgcolor='rgba(0,0,0,0)',
margin=dict(l=20, r=20, t=30, b=20),
height=400,
showlegend=True
)
st.plotly_chart(fig_pie, use_container_width=True)
with col2:
st.subheader("Monthly Savings Rate")
st.markdown(
'<p style="font-size: 0.9em; color: #888888;">Your monthly savings as a percentage of income, with dashed line showing the average rate.</p>',
unsafe_allow_html=True)
savings_rate = (df['Monthly left'] / total_income * 100).round(2)
fig_savings = go.Figure()
fig_savings.add_trace(go.Scatter(
x=df['Display_Date'],
y=savings_rate,
mode='lines+markers',
name='Savings Rate',
line=dict(color='#00ff88', width=2),
marker=dict(size=8)
))
fig_savings.add_hline(
y=savings_rate.mean(),
line_dash="dash",
line_color="white",
annotation_text=f"Average: {savings_rate.mean():.1f}%"
)
fig_savings.update_layout(
template='plotly_dark',
paper_bgcolor='rgba(0,0,0,0)',
plot_bgcolor='rgba(0,0,0,0)',
margin=dict(l=20, r=20, t=30, b=20),
height=400,
yaxis_title="Savings Rate (%)",
yaxis_range=[0, max(100, savings_rate.max() * 1.1)],
xaxis_title=None
)
st.plotly_chart(fig_savings, use_container_width=True)
# 3. Monthly Investment Growth
st.subheader("Investment Contributions Over Time")
st.markdown(
'<p style="font-size: 0.9em; color: #888888;">Monthly investment amounts (bars) and cumulative total invested (white line) over time.</p>',
unsafe_allow_html=True)
fig_investment = go.Figure()
fig_investment.add_trace(go.Bar(
x=df['Display_Date'],
y=df['Expenses market'],
name='Monthly Investment',
marker_color='#00ff88'
))
fig_investment.add_trace(go.Scatter(
x=df['Display_Date'],
y=df['Expenses market'].cumsum(),
name='Cumulative Investment',
line=dict(color='white', width=2),
yaxis='y2'
))
fig_investment.update_layout(
template='plotly_dark',
paper_bgcolor='rgba(0,0,0,0)',
plot_bgcolor='rgba(0,0,0,0)',
margin=dict(l=20, r=20, t=30, b=20),
height=400,
yaxis_title="Monthly Investment (₪)",
yaxis2=dict(
title="Cumulative Investment (₪)",
overlaying='y',
side='right'
),
showlegend=True,
hovermode='x unified',
xaxis_title=None
)
st.plotly_chart(fig_investment, use_container_width=True)
# 4. Key Metrics
st.subheader("Key Financial Metrics")
st.markdown('''
<p style="font-size: 0.9em; color: #888888;">
Summary of important financial indicators and their trends:
<br>• <b>Avg Monthly Savings</b>: Your average money left after all expenses each month. The delta shows how your latest month compares to this average.
<br>• <b>Avg Savings Rate</b>: The percentage of your income you typically save each month. The delta indicates if your latest month's savings rate was above or below average.
<br>• <b>Expense Efficiency</b>: The ratio of essential expenses (rent, mortgage, loan) to discretionary spending (day-to-day). Lower is better, indicating more controlled daily spending.
<br>• <b>Monthly Investment Ratio</b>: Your average monthly investment as a percentage of monthly income. Shows investment consistency relative to earnings.
</p>
''', unsafe_allow_html=True)
metrics_col1, metrics_col2, metrics_col3, metrics_col4 = st.columns(4)
with metrics_col1:
average_savings = df['Monthly left'].mean()
st.metric(
"Avg Monthly Savings",
f"₪{average_savings:,.2f}",
delta=f"₪{df['Monthly left'].iloc[-1] - average_savings:,.2f} vs avg"
)
with metrics_col2:
avg_savings_rate = savings_rate.mean()
st.metric(
"Avg Savings Rate",
f"{avg_savings_rate:.1f}%",
delta=f"{savings_rate.iloc[-1] - avg_savings_rate:.1f}% vs avg"
)
with metrics_col3:
# Calculate essential vs discretionary spending ratio
essential_expenses = df['Expenses rent'] + df['Expenses mortgage'] + df['Expenses loan']
discretionary_expenses = df['Expenses Day-to-day']
expense_ratio = (essential_expenses / discretionary_expenses).mean()
current_ratio = (essential_expenses.iloc[-1] / discretionary_expenses.iloc[-1])
st.metric(
"Expense Efficiency",
f"{expense_ratio:.2f}",
delta=f"{current_ratio - expense_ratio:.2f} vs avg",
delta_color="inverse" # Lower is better for this metric
)
with metrics_col4:
# Calculate average monthly investment ratio
monthly_investment_ratios = (df['Expenses market'] / (df['Income Salary'] + df['Income plus']) * 100)
avg_monthly_investment_ratio = monthly_investment_ratios.mean()
current_investment_ratio = monthly_investment_ratios.iloc[-1]
st.metric(
"Monthly Investment Ratio",
f"{avg_monthly_investment_ratio:.1f}%",
delta=f"{current_investment_ratio - avg_monthly_investment_ratio:.1f}% vs avg"
)
else:
st.info("Add some financial data to see the analytics!")