Spaces:
Sleeping
Sleeping
prasanth.thangavel
commited on
Commit
·
4f24509
1
Parent(s):
e22872a
Made improvements
Browse files
app.py
CHANGED
@@ -4,6 +4,11 @@ import yfinance as yf
|
|
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
|
@@ -164,6 +169,12 @@ if not selected_assets:
|
|
164 |
# Create the plot
|
165 |
fig = go.Figure()
|
166 |
|
|
|
|
|
|
|
|
|
|
|
|
|
167 |
for asset in selected_assets:
|
168 |
fig.add_trace(go.Scatter(
|
169 |
x=returns_data.index,
|
@@ -202,12 +213,27 @@ for asset in selected_assets:
|
|
202 |
total_years = len(yearly_returns)
|
203 |
positive_percentage = (positive_years / total_years) * 100
|
204 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
205 |
summary_data.append({
|
206 |
"Asset": asset,
|
207 |
f"Final Value ({currency_symbol})": final_value,
|
208 |
"Annualized Return (%)": annualized_return,
|
209 |
"Positive Years": f"{positive_years}/{total_years}",
|
210 |
-
"Positive Years %": positive_percentage
|
|
|
211 |
})
|
212 |
else:
|
213 |
summary_data.append({
|
@@ -215,7 +241,8 @@ for asset in selected_assets:
|
|
215 |
f"Final Value ({currency_symbol})": None,
|
216 |
"Annualized Return (%)": None,
|
217 |
"Positive Years": "N/A",
|
218 |
-
"Positive Years %": None
|
|
|
219 |
})
|
220 |
|
221 |
# Convert to DataFrame
|
@@ -239,6 +266,9 @@ st.dataframe(
|
|
239 |
),
|
240 |
"Positive Years %": st.column_config.NumberColumn(
|
241 |
format="%.1f"
|
|
|
|
|
|
|
242 |
)
|
243 |
}
|
244 |
)
|
|
|
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
|
|
|
169 |
# Create the plot
|
170 |
fig = go.Figure()
|
171 |
|
172 |
+
# Add vertical lines for every 5 years
|
173 |
+
start_year = returns_data.index[0].year
|
174 |
+
end_year = returns_data.index[-1].year
|
175 |
+
for year in range(start_year, end_year + 1, 5):
|
176 |
+
fig.add_vline(x=datetime(year, 1, 1), line_dash="dash", line_color="gray", opacity=0.3)
|
177 |
+
|
178 |
for asset in selected_assets:
|
179 |
fig.add_trace(go.Scatter(
|
180 |
x=returns_data.index,
|
|
|
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({
|
|
|
241 |
f"Final Value ({currency_symbol})": None,
|
242 |
"Annualized Return (%)": None,
|
243 |
"Positive Years": "N/A",
|
244 |
+
"Positive Years %": None,
|
245 |
+
"Performance": "N/A"
|
246 |
})
|
247 |
|
248 |
# Convert to DataFrame
|
|
|
266 |
),
|
267 |
"Positive Years %": st.column_config.NumberColumn(
|
268 |
format="%.1f"
|
269 |
+
),
|
270 |
+
"Performance": st.column_config.ImageColumn(
|
271 |
+
"Performance"
|
272 |
)
|
273 |
}
|
274 |
)
|