Spaces:
Runtime error
Runtime error
diaznicolasandres1
commited on
Commit
•
e1ca29e
1
Parent(s):
21fba72
Add application file
Browse files
app.py
ADDED
@@ -0,0 +1,185 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from distutils.command.build_scripts import first_line_re
|
2 |
+
import streamlit as st
|
3 |
+
import pandas as pd
|
4 |
+
import altair as alt
|
5 |
+
import datetime as dt
|
6 |
+
from datetime import timedelta
|
7 |
+
from datetime import datetime
|
8 |
+
import pytz
|
9 |
+
from pymongo import MongoClient
|
10 |
+
import base64
|
11 |
+
|
12 |
+
|
13 |
+
def download_link(object_to_download, download_filename, download_link_text):
|
14 |
+
|
15 |
+
if isinstance(object_to_download,pd.DataFrame):
|
16 |
+
object_to_download = object_to_download.to_csv(index=True)
|
17 |
+
b64 = base64.b64encode(object_to_download.encode()).decode()
|
18 |
+
return f'<a href="data:file/txt;base64,{b64}" download="{download_filename}">{download_link_text}</a>'
|
19 |
+
|
20 |
+
|
21 |
+
|
22 |
+
def main():
|
23 |
+
st.set_page_config( # Alternate names: setup_page, page, layout
|
24 |
+
layout="wide", # Can be "centered" or "wide". In the future also "dashboard", etc.
|
25 |
+
initial_sidebar_state="auto", # Can be "auto", "expanded", "collapsed"
|
26 |
+
page_title="Ratio de Bonos v2", # String or None. Strings get appended with "• Streamlit".
|
27 |
+
page_icon=None, # String, anything supported by st.image, or None.
|
28 |
+
)
|
29 |
+
|
30 |
+
st.title("Ratio de bonos v2")
|
31 |
+
|
32 |
+
today = datetime.today().astimezone(pytz.timezone('America/Argentina/Buenos_Aires')).replace(hour=0,minute=0,second= 0)
|
33 |
+
|
34 |
+
# conexion a bd
|
35 |
+
broker_bd = client = MongoClient("mongodb+srv://diaznicolasandres:diaznicolasandres941231@cluster0.azraf.mongodb.net/broker?retryWrites=true&w=majority")
|
36 |
+
db = client.get_database('broker')
|
37 |
+
bonds_table = db.bond
|
38 |
+
|
39 |
+
|
40 |
+
lista_nombre_bonos_1 = ["AL30", "AL29", "GD30", "GD29"]
|
41 |
+
|
42 |
+
lista_nombre_bonos_2 = ["GD30","AL30","AL29", "GD29"]
|
43 |
+
|
44 |
+
|
45 |
+
add_selectbox_bono1 = st.sidebar.selectbox(
|
46 |
+
'Seleccionar bono 1 ', lista_nombre_bonos_1
|
47 |
+
|
48 |
+
)
|
49 |
+
|
50 |
+
add_selectbox_bono2 = st.sidebar.selectbox(
|
51 |
+
'Seleccionar bono 2',
|
52 |
+
lista_nombre_bonos_2
|
53 |
+
)
|
54 |
+
|
55 |
+
|
56 |
+
cantidad_dias = st.sidebar.selectbox("Cant dias", [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15])
|
57 |
+
|
58 |
+
|
59 |
+
if(add_selectbox_bono1 is not None and add_selectbox_bono2 is not None):
|
60 |
+
especie1 = '_'+add_selectbox_bono1
|
61 |
+
especie2 = '_'+add_selectbox_bono2
|
62 |
+
|
63 |
+
if(especie1 == especie2):
|
64 |
+
especie2 = especie2+"2"
|
65 |
+
|
66 |
+
|
67 |
+
|
68 |
+
bonos1_found = bonds_table.find({'ticker': add_selectbox_bono1, "date": {"$gt": today - timedelta(days= cantidad_dias-1)}})
|
69 |
+
|
70 |
+
|
71 |
+
|
72 |
+
bonos2_found = bonds_table.find({'ticker': add_selectbox_bono2, "date": {"$gt": today - timedelta(days= cantidad_dias-1)}})
|
73 |
+
|
74 |
+
|
75 |
+
dataframe1 = pd.DataFrame(bonos1_found , columns = ['lastPrice', 'buyPrice','sellPrice', 'date'])
|
76 |
+
dataframe2 = pd.DataFrame(bonos2_found , columns = ['lastPrice', 'buyPrice','sellPrice', 'date'])
|
77 |
+
|
78 |
+
|
79 |
+
|
80 |
+
|
81 |
+
dataframe1 = dataframe1.rename(columns = {'date': 'index'}).set_index('index')
|
82 |
+
dataframe1.drop_duplicates(keep='first')
|
83 |
+
|
84 |
+
dataframe2 = dataframe2.rename(columns = {'date': 'index'}).set_index('index')
|
85 |
+
dataframe2.drop_duplicates(keep='first')
|
86 |
+
dataframe1 = dataframe1.loc[(dataframe1['lastPrice']!= '-' ) & (dataframe1['buyPrice']!= '-' ) & (dataframe1['sellPrice']!= '-' ) ]
|
87 |
+
dataframe2 = dataframe2.loc[(dataframe2['lastPrice']!= '-' ) & (dataframe2['buyPrice']!= '-' ) & (dataframe2['sellPrice']!= '-' ) ]
|
88 |
+
|
89 |
+
dataframe1 = dataframe1.rename(columns={'lastPrice': 'UltimoPrecio'+especie1, 'buyPrice': 'PrecioCompra'+especie1, 'sellPrice': 'PrecioVenta'+especie1})
|
90 |
+
dataframe2 = dataframe2.rename(columns={'lastPrice': 'UltimoPrecio'+especie2, 'buyPrice': 'PrecioCompra'+especie2, 'sellPrice': 'PrecioVenta'+especie2})
|
91 |
+
|
92 |
+
df_combinado = dataframe1.join(dataframe2, on='index')
|
93 |
+
|
94 |
+
|
95 |
+
|
96 |
+
df_combinado['Ratio'] = df_combinado['UltimoPrecio'+especie1].astype(float) / df_combinado['UltimoPrecio'+especie2].astype(float)
|
97 |
+
|
98 |
+
argentina_timezone = 'America/Argentina/Buenos_Aires'
|
99 |
+
|
100 |
+
df_combinado.index = df_combinado.index.tz_localize(pytz.utc).tz_convert(argentina_timezone)
|
101 |
+
df_combinado = df_combinado[~df_combinado.index.duplicated(keep='first')]
|
102 |
+
|
103 |
+
st.dataframe(df_combinado.style.highlight_max(color='green', axis=0))
|
104 |
+
# Graficar data
|
105 |
+
if st.button("Graficar"):
|
106 |
+
cust_data = df_combinado["Ratio"]
|
107 |
+
line = alt.Chart(cust_data.reset_index()).mark_line(
|
108 |
+
color='purple',
|
109 |
+
size=3
|
110 |
+
).transform_window(
|
111 |
+
rolling_mean='mean(Ratio)'
|
112 |
+
).encode(
|
113 |
+
x=alt.X('index'),
|
114 |
+
y=alt.Y('rolling_mean:Q', scale=alt.Scale(zero=False))
|
115 |
+
).properties(title=add_selectbox_bono1 + '/' + add_selectbox_bono2).interactive()
|
116 |
+
|
117 |
+
points = alt.Chart(cust_data.reset_index()).mark_line().encode(
|
118 |
+
x=alt.X('index', axis=alt.Axis(title='Fecha')),
|
119 |
+
y=alt.Y('Ratio:Q',
|
120 |
+
axis=alt.Axis(title='Ratio - Media'), scale=alt.Scale(zero=False))
|
121 |
+
).interactive()
|
122 |
+
|
123 |
+
chart = line + points
|
124 |
+
|
125 |
+
chart = line + points
|
126 |
+
|
127 |
+
st.altair_chart(chart, use_container_width=True)
|
128 |
+
|
129 |
+
base1 = alt.Chart(df_combinado.reset_index()).encode(alt.X('index', axis = alt.Axis(title='Fecha')))
|
130 |
+
|
131 |
+
line_1 = base1.mark_line(
|
132 |
+
color='purple',
|
133 |
+
size=2
|
134 |
+
).encode(
|
135 |
+
y=alt.Y("UltimoPrecio" + especie1 + ':Q', scale=alt.Scale(zero=False),
|
136 |
+
axis=alt.Axis(title='Ultimo precio', titleColor='black'))
|
137 |
+
).properties(title=add_selectbox_bono1).interactive()
|
138 |
+
|
139 |
+
|
140 |
+
layer1 = alt.layer(line_1)
|
141 |
+
st.altair_chart(layer1, use_container_width=True)
|
142 |
+
|
143 |
+
|
144 |
+
|
145 |
+
|
146 |
+
line_2 = base1.mark_line(
|
147 |
+
color='purple',
|
148 |
+
size=2
|
149 |
+
).encode(
|
150 |
+
y=alt.Y("UltimoPrecio" + especie2 + ':Q', scale=alt.Scale(zero=False),
|
151 |
+
axis=alt.Axis(title='Ultimo precio', titleColor='black'))
|
152 |
+
).properties(title=add_selectbox_bono2).interactive()
|
153 |
+
|
154 |
+
layer2 = alt.layer(line_2)
|
155 |
+
st.altair_chart(layer2, use_container_width=True)
|
156 |
+
|
157 |
+
|
158 |
+
if st.button("Descargar"):
|
159 |
+
# open('bonos.csv', 'w').write(df_ratio.to_csv())
|
160 |
+
tmp_download_link = download_link(df_combinado, 'bonos_' + especie1 + '_' + especie2 + '.csv',
|
161 |
+
'Presione para descargar el archivo')
|
162 |
+
st.markdown(tmp_download_link, unsafe_allow_html=True)
|
163 |
+
|
164 |
+
|
165 |
+
|
166 |
+
|
167 |
+
|
168 |
+
|
169 |
+
|
170 |
+
|
171 |
+
|
172 |
+
|
173 |
+
|
174 |
+
|
175 |
+
|
176 |
+
|
177 |
+
|
178 |
+
|
179 |
+
|
180 |
+
|
181 |
+
|
182 |
+
|
183 |
+
|
184 |
+
if __name__ == '__main__':
|
185 |
+
main()
|