Sofi1606 commited on
Commit
6777368
1 Parent(s): dae423b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -12
app.py CHANGED
@@ -3,25 +3,43 @@ import pandas as pd
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,7 +47,6 @@ if uploaded_file is not None:
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,11 +59,9 @@ if uploaded_file is not None:
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)
 
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
+ api_url = "https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&apikey=QVQGE7YPO68S403J&datatype=csv"
14
+ symbols = ['AMZN', 'MELI', 'ETSY']
15
 
16
+ option = st.selectbox("Selecciona la fuente de datos:", ("Usar archivo predeterminado", "API Alpha Vantage", "Subir archivo propio"))
17
 
18
+ if option == "Usar archivo predeterminado":
19
+ uploaded_file = "STOCKS - Hoja 1.csv"
20
+
21
+ elif option == "API Alpha Vantage":
22
+ for symbol in symbols:
23
+ st.subheader(symbol)
24
+ response = requests.get(f"{api_url}&symbol={symbol}")
25
+ if response.status_code == 200:
26
+ data = pd.read_csv(io.BytesIO(response.content))
27
+ st.write(f"Datos de la acción {symbol}:")
28
+ st.write(data.head())
29
+ else:
30
+ st.write(f"Error al obtener los datos de la acción {symbol}. Código de estado:", response.status_code)
31
+ elif option == "Subir archivo propio":
32
  uploaded_file = st.file_uploader("Sube aquí tu archivo de excel", type=[".xls", ".xlsx", ".csv"], help=help_string)
33
+ if uploaded_file is not None:
34
+ # Cargar y procesar el archivo subido
35
+ if uploaded_file.name[-3:] == "csv":
36
+ df = pd.read_csv(uploaded_file)
37
+ else:
38
+ df = pd.read_excel(uploaded_file)
39
+ # Resto del procesamiento del archivo ...
40
 
41
  if uploaded_file is not None:
42
+ if uploaded_file.name[-3:] == "csv":
 
43
  df = pd.read_csv(uploaded_file)
44
  else:
45
  df = pd.read_excel(uploaded_file)
 
47
  df = df.drop(0, axis=0)
48
  df = df.drop("Unnamed: 2", axis=1).drop("Unnamed: 4", axis=1).rename({"Unnamed: 0": "Date"}, axis=1)
49
 
 
50
  df['Date'] = pd.to_datetime(df['Date'], format="%d/%m/%Y %H:%M:%S")
51
 
52
  stocks = list(df.columns)[-3:]
 
59
 
60
  st.write(df[["Date"] + stocks_rets])
61
 
 
62
  fig = px.line(df, x=df.Date, y=stocks, labels={'value': 'Value', 'variable': 'Series'}, title='Time Series Plot')
63
  fig.update_layout(xaxis_title='Date', yaxis_title='Value')
64
 
 
65
  st.plotly_chart(fig)
66
 
67
  ret_list = df[stocks_rets].mean().to_numpy().reshape(-1, 1)