prasanth.thangavel commited on
Commit
b382e22
·
1 Parent(s): 4f24509

Made improvements

Browse files
Files changed (1) hide show
  1. app.py +9 -24
app.py CHANGED
@@ -4,11 +4,6 @@ import yfinance as yf
4
  import plotly.graph_objects as go
5
  from datetime import datetime, timedelta
6
  import numpy as np
7
- import base64
8
- from io import BytesIO
9
- import matplotlib.pyplot as plt
10
- import matplotlib
11
- matplotlib.use('Agg')
12
 
13
  # Import utility functions
14
  from utils.yfinance_utils import fetch_yfinance_daily
@@ -29,7 +24,7 @@ st.set_page_config(page_title="Asset Class Comparison", layout="wide")
29
  # Title and description
30
  st.title("Asset Class Performance Comparison")
31
  st.write("Compare the performance of different asset classes over time")
32
- st.write("Note: Cryptocurrencies (BTC, ETH, SOL, DOGE) are highly volatile and should be considered high-risk investments")
33
 
34
  # Sidebar for user inputs
35
  st.sidebar.header("Investment Parameters")
@@ -37,9 +32,15 @@ currency = st.sidebar.selectbox("Display Currency", ["USD", "SGD"], index=0)
37
  initial_investment = st.sidebar.number_input(f"Initial Investment Amount ({currency})", min_value=1000, value=10000, step=1000)
38
  start_date = st.sidebar.date_input("Start Date", value=datetime.now() - timedelta(days=365*25))
39
  user_end_date = st.sidebar.date_input("End Date", value=datetime.now())
 
40
  fd_rate = st.sidebar.number_input("Fixed Deposit Rate (%)", min_value=0.0, value=2.9, step=0.1) / 100
41
  use_log_scale = st.sidebar.checkbox("Use Log Scale", value=True)
42
 
 
 
 
 
 
43
  # Asset selection
44
  selected_assets = st.sidebar.multiselect(
45
  "Select Assets to Compare",
@@ -207,33 +208,18 @@ for asset in selected_assets:
207
  annualized_return = ((final_value / initial_investment) ** (1/years) - 1) * 100
208
 
209
  # Calculate yearly return statistics
210
- yearly_data = valid_series.resample('Y').first()
211
  yearly_returns = yearly_data.pct_change().dropna()
212
  positive_years = (yearly_returns > 0).sum()
213
  total_years = len(yearly_returns)
214
  positive_percentage = (positive_years / total_years) * 100
215
 
216
- # Create sparkline using matplotlib
217
- plt.figure(figsize=(2, 0.5))
218
- plt.plot(valid_series.index, valid_series, linewidth=1)
219
- plt.axis('off')
220
- plt.margins(0)
221
- plt.tight_layout(pad=0)
222
-
223
- # Convert plot to base64 string
224
- buf = BytesIO()
225
- plt.savefig(buf, format='png', bbox_inches='tight', pad_inches=0, transparent=True)
226
- buf.seek(0)
227
- sparkline = base64.b64encode(buf.read()).decode('utf-8')
228
- plt.close()
229
-
230
  summary_data.append({
231
  "Asset": asset,
232
  f"Final Value ({currency_symbol})": final_value,
233
  "Annualized Return (%)": annualized_return,
234
  "Positive Years": f"{positive_years}/{total_years}",
235
  "Positive Years %": positive_percentage,
236
- "Performance": f'<img src="data:image/png;base64,{sparkline}" width="100" height="30">'
237
  })
238
  else:
239
  summary_data.append({
@@ -242,7 +228,6 @@ for asset in selected_assets:
242
  "Annualized Return (%)": None,
243
  "Positive Years": "N/A",
244
  "Positive Years %": None,
245
- "Performance": "N/A"
246
  })
247
 
248
  # Convert to DataFrame
@@ -306,7 +291,7 @@ for asset in selected_assets:
306
  valid_series = returns_data[asset].dropna()
307
  if len(valid_series) > 1:
308
  # Resample to yearly data
309
- yearly_data = valid_series.resample('Y').first()
310
 
311
  # Calculate yearly returns
312
  yearly_returns = yearly_data.pct_change().dropna()
 
4
  import plotly.graph_objects as go
5
  from datetime import datetime, timedelta
6
  import numpy as np
 
 
 
 
 
7
 
8
  # Import utility functions
9
  from utils.yfinance_utils import fetch_yfinance_daily
 
24
  # Title and description
25
  st.title("Asset Class Performance Comparison")
26
  st.write("Compare the performance of different asset classes over time")
27
+ # st.write("Note: Cryptocurrencies (BTC, ETH, SOL, DOGE) are highly volatile and should be considered high-risk investments")
28
 
29
  # Sidebar for user inputs
30
  st.sidebar.header("Investment Parameters")
 
32
  initial_investment = st.sidebar.number_input(f"Initial Investment Amount ({currency})", min_value=1000, value=10000, step=1000)
33
  start_date = st.sidebar.date_input("Start Date", value=datetime.now() - timedelta(days=365*25))
34
  user_end_date = st.sidebar.date_input("End Date", value=datetime.now())
35
+
36
  fd_rate = st.sidebar.number_input("Fixed Deposit Rate (%)", min_value=0.0, value=2.9, step=0.1) / 100
37
  use_log_scale = st.sidebar.checkbox("Use Log Scale", value=True)
38
 
39
+ # Calculate and display investment period
40
+ investment_days = (user_end_date - start_date).days
41
+ investment_years = investment_days / 365
42
+ st.write(f"Investment Period: {investment_days} days ({investment_years:.1f} years)")
43
+
44
  # Asset selection
45
  selected_assets = st.sidebar.multiselect(
46
  "Select Assets to Compare",
 
208
  annualized_return = ((final_value / initial_investment) ** (1/years) - 1) * 100
209
 
210
  # Calculate yearly return statistics
211
+ yearly_data = valid_series.resample('YE').first()
212
  yearly_returns = yearly_data.pct_change().dropna()
213
  positive_years = (yearly_returns > 0).sum()
214
  total_years = len(yearly_returns)
215
  positive_percentage = (positive_years / total_years) * 100
216
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
217
  summary_data.append({
218
  "Asset": asset,
219
  f"Final Value ({currency_symbol})": final_value,
220
  "Annualized Return (%)": annualized_return,
221
  "Positive Years": f"{positive_years}/{total_years}",
222
  "Positive Years %": positive_percentage,
 
223
  })
224
  else:
225
  summary_data.append({
 
228
  "Annualized Return (%)": None,
229
  "Positive Years": "N/A",
230
  "Positive Years %": None,
 
231
  })
232
 
233
  # Convert to DataFrame
 
291
  valid_series = returns_data[asset].dropna()
292
  if len(valid_series) > 1:
293
  # Resample to yearly data
294
+ yearly_data = valid_series.resample('YE').first()
295
 
296
  # Calculate yearly returns
297
  yearly_returns = yearly_data.pct_change().dropna()