Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,44 +3,25 @@ import pandas as pd
|
|
3 |
import numpy as np
|
4 |
import optuna
|
5 |
import plotly.express as px
|
6 |
-
import requests
|
7 |
-
import io
|
8 |
|
9 |
st.title("Portfolio weights calculator")
|
10 |
|
11 |
help_string = "NOTA: Para su información los datos utilizados se extrajeron de GOOGLEFINANCE."
|
12 |
|
13 |
-
|
14 |
-
symbols = ['AMZN', 'MELI', 'ETSY']
|
15 |
|
16 |
-
|
17 |
|
18 |
-
|
19 |
-
|
20 |
-
if option == "API Alpha Vantage":
|
21 |
-
for symbol in symbols:
|
22 |
-
st.subheader(symbol)
|
23 |
-
response = requests.get(f"{api_url}&symbol={symbol}")
|
24 |
-
if response.status_code == 200:
|
25 |
-
data = pd.read_csv(io.BytesIO(response.content))
|
26 |
-
st.write(f"Datos de la acción {symbol}:")
|
27 |
-
st.write(data.head())
|
28 |
-
else:
|
29 |
-
st.write(f"Error al obtener los datos de la acción {symbol}. Código de estado:", response.status_code)
|
30 |
-
elif option == "Subir archivo propio":
|
31 |
-
uploaded_file = st.file_uploader("Sube aquí tu archivo de excel", type=[".xls", ".xlsx", ".csv"], help=help_string)
|
32 |
-
if uploaded_file is not None:
|
33 |
-
# Cargar y procesar el archivo subido
|
34 |
-
if uploaded_file.name[-3:] == "csv":
|
35 |
-
df = pd.read_csv(uploaded_file)
|
36 |
-
else:
|
37 |
-
df = pd.read_excel(uploaded_file)
|
38 |
-
# Resto del procesamiento del archivo ...
|
39 |
-
elif option == "Usar archivo predeterminado":
|
40 |
uploaded_file = "STOCKS - Hoja 1.csv"
|
|
|
|
|
|
|
|
|
41 |
|
42 |
if uploaded_file is not None:
|
43 |
-
|
|
|
44 |
df = pd.read_csv(uploaded_file)
|
45 |
else:
|
46 |
df = pd.read_excel(uploaded_file)
|
@@ -48,6 +29,7 @@ if uploaded_file is not None:
|
|
48 |
df = df.drop(0, axis=0)
|
49 |
df = df.drop("Unnamed: 2", axis=1).drop("Unnamed: 4", axis=1).rename({"Unnamed: 0": "Date"}, axis=1)
|
50 |
|
|
|
51 |
df['Date'] = pd.to_datetime(df['Date'], format="%d/%m/%Y %H:%M:%S")
|
52 |
|
53 |
stocks = list(df.columns)[-3:]
|
@@ -60,9 +42,11 @@ if uploaded_file is not None:
|
|
60 |
|
61 |
st.write(df[["Date"] + stocks_rets])
|
62 |
|
|
|
63 |
fig = px.line(df, x=df.Date, y=stocks, labels={'value': 'Value', 'variable': 'Series'}, title='Time Series Plot')
|
64 |
fig.update_layout(xaxis_title='Date', yaxis_title='Value')
|
65 |
|
|
|
66 |
st.plotly_chart(fig)
|
67 |
|
68 |
ret_list = df[stocks_rets].mean().to_numpy().reshape(-1, 1)
|
|
|
3 |
import numpy as np
|
4 |
import optuna
|
5 |
import plotly.express as px
|
|
|
|
|
6 |
|
7 |
st.title("Portfolio weights calculator")
|
8 |
|
9 |
help_string = "NOTA: Para su información los datos utilizados se extrajeron de GOOGLEFINANCE."
|
10 |
|
11 |
+
"STOCKS - Hoja 1.csv"
|
|
|
12 |
|
13 |
+
check_box = st.checkbox("¿Quieres usar el archivo que tenemos para ti?")
|
14 |
|
15 |
+
if check_box:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
uploaded_file = "STOCKS - Hoja 1.csv"
|
17 |
+
file_name = uploaded_file
|
18 |
+
else:
|
19 |
+
uploaded_file = st.file_uploader("Sube aquí tu archivo de excel", type=[".xls", ".xlsx", ".csv"], help=help_string)
|
20 |
+
file_name = uploaded_file.name if uploaded_file is not None else None
|
21 |
|
22 |
if uploaded_file is not None:
|
23 |
+
# Can be used wherever a "file-like" object is accepted:
|
24 |
+
if file_name[-3:] == "csv":
|
25 |
df = pd.read_csv(uploaded_file)
|
26 |
else:
|
27 |
df = pd.read_excel(uploaded_file)
|
|
|
29 |
df = df.drop(0, axis=0)
|
30 |
df = df.drop("Unnamed: 2", axis=1).drop("Unnamed: 4", axis=1).rename({"Unnamed: 0": "Date"}, axis=1)
|
31 |
|
32 |
+
|
33 |
df['Date'] = pd.to_datetime(df['Date'], format="%d/%m/%Y %H:%M:%S")
|
34 |
|
35 |
stocks = list(df.columns)[-3:]
|
|
|
42 |
|
43 |
st.write(df[["Date"] + stocks_rets])
|
44 |
|
45 |
+
# Plotting with Plotly
|
46 |
fig = px.line(df, x=df.Date, y=stocks, labels={'value': 'Value', 'variable': 'Series'}, title='Time Series Plot')
|
47 |
fig.update_layout(xaxis_title='Date', yaxis_title='Value')
|
48 |
|
49 |
+
# Use Streamlit to render the plot
|
50 |
st.plotly_chart(fig)
|
51 |
|
52 |
ret_list = df[stocks_rets].mean().to_numpy().reshape(-1, 1)
|