Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,7 +4,6 @@ import numpy as np
|
|
4 |
import pandas as pd
|
5 |
from scipy.optimize import minimize
|
6 |
|
7 |
-
# Define stock tickers (25 from S&P 500)
|
8 |
TICKERS = [
|
9 |
'AAPL', 'MSFT', 'NVDA', 'AVGO', 'ADBE',
|
10 |
'AMZN', 'TSLA', 'HD',
|
@@ -21,8 +20,8 @@ TICKERS = [
|
|
21 |
def optimize_portfolio(years, target_return):
|
22 |
try:
|
23 |
data = yf.download(TICKERS, period=f"{years}y", interval="1mo")
|
24 |
-
if "Adj Close" not in data:
|
25 |
-
return pd.DataFrame(), "Error: 'Adj Close' column missing.", "", ""
|
26 |
|
27 |
prices = data['Adj Close']
|
28 |
returns = prices.pct_change().dropna()
|
@@ -51,7 +50,7 @@ def optimize_portfolio(years, target_return):
|
|
51 |
)
|
52 |
|
53 |
if not result.success:
|
54 |
-
return pd.DataFrame(), "Optimization failed. Try adjusting inputs.", "", ""
|
55 |
|
56 |
weights = result.x
|
57 |
port_return = weights @ mean_returns
|
@@ -71,7 +70,7 @@ def optimize_portfolio(years, target_return):
|
|
71 |
|
72 |
with gr.Blocks() as demo:
|
73 |
gr.Markdown("# π Modern Portfolio Optimizer (MPT)")
|
74 |
-
gr.Markdown("Optimize a portfolio of 25 S&P 500 stocks for **minimum risk**
|
75 |
|
76 |
with gr.Row():
|
77 |
years_slider = gr.Slider(1, 10, value=5, step=1, label="Years of Historical Data")
|
|
|
4 |
import pandas as pd
|
5 |
from scipy.optimize import minimize
|
6 |
|
|
|
7 |
TICKERS = [
|
8 |
'AAPL', 'MSFT', 'NVDA', 'AVGO', 'ADBE',
|
9 |
'AMZN', 'TSLA', 'HD',
|
|
|
20 |
def optimize_portfolio(years, target_return):
|
21 |
try:
|
22 |
data = yf.download(TICKERS, period=f"{years}y", interval="1mo")
|
23 |
+
if "Adj Close" not in data.columns:
|
24 |
+
return pd.DataFrame(), "Error: 'Adj Close' column missing.", "", "", ""
|
25 |
|
26 |
prices = data['Adj Close']
|
27 |
returns = prices.pct_change().dropna()
|
|
|
50 |
)
|
51 |
|
52 |
if not result.success:
|
53 |
+
return pd.DataFrame(), "Optimization failed. Try adjusting inputs.", "", "", ""
|
54 |
|
55 |
weights = result.x
|
56 |
port_return = weights @ mean_returns
|
|
|
70 |
|
71 |
with gr.Blocks() as demo:
|
72 |
gr.Markdown("# π Modern Portfolio Optimizer (MPT)")
|
73 |
+
gr.Markdown("Optimize a portfolio of 25 S&P 500 stocks for **minimum risk** with a target return.")
|
74 |
|
75 |
with gr.Row():
|
76 |
years_slider = gr.Slider(1, 10, value=5, step=1, label="Years of Historical Data")
|