Delete app.py
Browse files
app.py
DELETED
|
@@ -1,172 +0,0 @@
|
|
| 1 |
-
# -*- coding: utf-8 -*-
|
| 2 |
-
# Interfaz Gradio y visualización
|
| 3 |
-
|
| 4 |
-
import gradio as gr
|
| 5 |
-
import plotly.graph_objects as go
|
| 6 |
-
from procesos import evolucion_correlacion, exportar_correlacion, descargar_resumen_holding_excel
|
| 7 |
-
from utils import find_company_online
|
| 8 |
-
from data import generar_tickers
|
| 9 |
-
from config import DEFAULT_TEMPLATE_URL
|
| 10 |
-
|
| 11 |
-
# Diccionario de IDs para iframes
|
| 12 |
-
tickers_to_ids = {
|
| 13 |
-
"MSTR": 1, "MARA": 4, "CEP": 436, "BSTR": 674, "RIOT": 7, "DJT": 671, "3350.T": 176, "GLXY": 10, "CLSK": 28, "TSLA": 2,
|
| 14 |
-
"HUT": 6, "COIN": 8, "XYZ": 5, "NXTT": 473, "CCCM": 595, "SMLR": 194, "GME": 517, "CANG": 337, "ADE.DE": 11, "0434.HK": 238,
|
| 15 |
-
"HOLO": 508, "SQNS": 644, "HIVE": 9, "EXOD": 168, "ALTBG.PA": 295, "3659.T": 14, "FUFU": 325, "BTDR": 324, "SWC.AQ": 440, "FLD": 256,
|
| 16 |
-
"CAN": 111, "BITF": 12, "CIFR": 82, "3825.T": 301, "KULR": 341, "NA": 331, "CORZ": 3, "MSW": 356, "SOS": 426, "BTCT.V": 608,
|
| 17 |
-
"AKER.OL": 15, "GS9": 491, "CASH3.SA": 395, "MELI": 182, "SRAG.DU": 279, "JAS.BK": 307, "DCC.AX": 24, "ARLP": 425, "377030.KQ": 401, "BTBT": 18,
|
| 18 |
-
"NDA.V": 32, "DDC": 500, "NTHOL.IS": 40, "DMGI.V": 23, "NCTY": 442, "VLCN": 679, "PNIX.AQ": 582, "ABT.DU": 21, "LQWD.V": 29, "CS.ST": 288,
|
| 19 |
-
"VIRT": 472, "112040.KQ": 403, "RUM": 402, "DEFI.NE": 254, "GNS": 294, "PRE": 573, "3189.T": 445, "BTCB.SE": 636, "LMFA": 91, "BMNR": 550,
|
| 20 |
-
"SIX.CN": 597, "BNXAF": 30, "PHX.AD": 449, "HKHC": 480, "042420.KQ": 406, "BTC.BK": 31, "K33.ST": 538, "COIN.AQ": 483, "BIGG.V": 17, "BTM": 372,
|
| 21 |
-
"KWM": 643, "BTC.L": 292, "8005.HK": 342, "MATA.V": 357, "VANA.MC": 535, "V3TC.AQ": 586, "SBC": 380, "FRMO": 35, "ATAI": 479, "CBIT.V": 27,
|
| 22 |
-
"MLGO": 511, "MOGO": 36, "8267.HK": 239, "WNDR.TO": 242, "SATO.V": 240, "9238.T": 434, "1723.HK": 384, "PXI.CN": 634, "SATS.L": 666, "ALBDM.PA": 580,
|
| 23 |
-
"IREN": 12, "WULF": 15, "GREE": 21, "EBON": 14, "DGHI": 25, "SDIG": 17, "MIGI": 16, "SLNH": 24, "ANY": 23, "APLD": 20,
|
| 24 |
-
"BTCS": 19, "RVYL": 30, "ICG": 29, "HOOD": 35, "PYPL": 2, "NVDA": 22, "CME": 26, "DEFI": 31, "IBIT": 119, "BTF": 33,
|
| 25 |
-
"SQ": 34, "BKKT": 37, "BTCM": 38, "MA": 39, "IBM": 40, "GBTC": 50, "BITO": 41, "BITB": 117, "BTC-USD": 0, "MTPLF": 176
|
| 26 |
-
}
|
| 27 |
-
|
| 28 |
-
def get_iframe_stock_vs_btc(ticker):
|
| 29 |
-
try:
|
| 30 |
-
entity_id = tickers_to_ids[ticker]
|
| 31 |
-
return f"""https://bitcointreasuries.net/embed?component=StockVsBitcoinChart&entityId={entity_id}"""
|
| 32 |
-
except KeyError:
|
| 33 |
-
return f"No hay datos de holding de Bitcoin para {ticker}."
|
| 34 |
-
|
| 35 |
-
def get_iframe_holdings(ticker):
|
| 36 |
-
try:
|
| 37 |
-
entity_id = tickers_to_ids[ticker]
|
| 38 |
-
return f"""https://bitcointreasuries.net/embed?component=BitcoinHoldingsChart&entityId={entity_id}"""
|
| 39 |
-
except KeyError:
|
| 40 |
-
return f"No hay datos de holding de Bitcoin para {ticker}."
|
| 41 |
-
|
| 42 |
-
def plot_evolucion_correlacion_gradio(ticker_company, step, window_months):
|
| 43 |
-
serie = evolucion_correlacion(ticker_company, step, window_months)
|
| 44 |
-
if serie is None or serie.empty:
|
| 45 |
-
return None, f"No hay datos de correlación disponibles para {ticker_company}."
|
| 46 |
-
|
| 47 |
-
fig = go.Figure()
|
| 48 |
-
fig.add_trace(go.Scatter(
|
| 49 |
-
x=serie.index,
|
| 50 |
-
y=serie.values,
|
| 51 |
-
mode='lines',
|
| 52 |
-
name='Correlación',
|
| 53 |
-
line=dict(color='#2263eb', width=2),
|
| 54 |
-
fill='tozeroy',
|
| 55 |
-
fillcolor='rgba(34,99,235,0.1)',
|
| 56 |
-
hovertemplate='Fecha: %{x|%Y-%m-%d}<br>Correlación: %{y:.4f}<extra></extra>'
|
| 57 |
-
))
|
| 58 |
-
|
| 59 |
-
fig.update_layout(
|
| 60 |
-
title_text=f'<b>Evolución de la correlación entre {ticker_company} y BTC-USD<b>',
|
| 61 |
-
title_x=0,
|
| 62 |
-
height=500,
|
| 63 |
-
template="plotly_white",
|
| 64 |
-
margin=dict(l=20, r=20, t=60, b=20),
|
| 65 |
-
showlegend=False,
|
| 66 |
-
xaxis=dict(
|
| 67 |
-
rangeslider=dict(
|
| 68 |
-
visible=True,
|
| 69 |
-
thickness=0.08,
|
| 70 |
-
bgcolor="rgba(50, 0, 128, 0.1)",
|
| 71 |
-
bordercolor="rgba(50, 0, 128, 0.2)",
|
| 72 |
-
borderwidth=1,
|
| 73 |
-
range=[serie.index.min(), serie.index.max()]
|
| 74 |
-
),
|
| 75 |
-
rangeselector=dict(
|
| 76 |
-
buttons=[
|
| 77 |
-
dict(count=1, label="1m", step="month", stepmode="backward"),
|
| 78 |
-
dict(count=3, label="3m", step="month", stepmode="backward"),
|
| 79 |
-
dict(count=6, label="6m", step="month", stepmode="backward"),
|
| 80 |
-
dict(step="year", count=1, label="YTD", stepmode="todate"),
|
| 81 |
-
dict(count=1, label="1y", step="year", stepmode="backward"),
|
| 82 |
-
dict(step="all", label="All")
|
| 83 |
-
],
|
| 84 |
-
bgcolor="rgba(247,247,247,255)",
|
| 85 |
-
bordercolor = "rgba(0,0,0,0)",
|
| 86 |
-
activecolor = "rgba(230,233,255,255)",
|
| 87 |
-
font=dict(color="#000000", size=10),
|
| 88 |
-
x=0.9, y=1.1, xanchor='right', yanchor='top'
|
| 89 |
-
),
|
| 90 |
-
type="date",
|
| 91 |
-
showgrid=False,
|
| 92 |
-
gridcolor='#e0e0e0',
|
| 93 |
-
zeroline=False
|
| 94 |
-
),
|
| 95 |
-
yaxis=dict(
|
| 96 |
-
title_text='Correlación',
|
| 97 |
-
side='left',
|
| 98 |
-
showgrid=True,
|
| 99 |
-
gridcolor='#e0e0e0',
|
| 100 |
-
zeroline=False,
|
| 101 |
-
tickformat=".2f",
|
| 102 |
-
range=[-1.0, 1.0]
|
| 103 |
-
),
|
| 104 |
-
hovermode="x unified",
|
| 105 |
-
paper_bgcolor='rgba(255,255,255,1)',
|
| 106 |
-
plot_bgcolor='rgba(255,255,255,1)'
|
| 107 |
-
)
|
| 108 |
-
|
| 109 |
-
max_value = serie.max()
|
| 110 |
-
max_index = serie.idxmax()
|
| 111 |
-
fig.add_annotation(
|
| 112 |
-
x=max_index,
|
| 113 |
-
y=max_value,
|
| 114 |
-
text=f'Corr_max: {max_value:.4f}<br>Fecha: {max_index.date()}',
|
| 115 |
-
showarrow=True,
|
| 116 |
-
arrowhead=2,
|
| 117 |
-
ax=0,
|
| 118 |
-
ay=-40,
|
| 119 |
-
bgcolor="rgba(255, 255, 255, 0.8)",
|
| 120 |
-
bordercolor="#c7c7c7",
|
| 121 |
-
borderwidth=1,
|
| 122 |
-
borderpad=4,
|
| 123 |
-
font=dict(size=10, color="#333")
|
| 124 |
-
)
|
| 125 |
-
|
| 126 |
-
return fig, "Gráfico de correlación generado."
|
| 127 |
-
|
| 128 |
-
# Interfaz Gradio
|
| 129 |
-
tickers = generar_tickers()
|
| 130 |
-
|
| 131 |
-
with gr.Blocks(title="Análisis de Correlación") as demo:
|
| 132 |
-
gr.Markdown("# Analizador de Correlación de Bitcoin")
|
| 133 |
-
gr.Markdown("Explora la correlación de diferentes activos con Bitcoin y sus holdings.")
|
| 134 |
-
|
| 135 |
-
with gr.Row():
|
| 136 |
-
input_company = gr.Dropdown(label="Buscar por Nombre", choices=tickers, value="MicroStrategy", allow_custom_value=True)
|
| 137 |
-
texto_info1 = gr.Textbox(label="Nombre Completo:", interactive=False)
|
| 138 |
-
texto_info2 = gr.Textbox(label="Ticker:", interactive=False)
|
| 139 |
-
|
| 140 |
-
input_company.change(fn=lambda texto: find_company_online(texto[:6]), inputs=input_company, outputs=[texto_info1, texto_info2])
|
| 141 |
-
demo.load(fn=lambda texto: find_company_online(texto[:6]), inputs=input_company, outputs=[texto_info1, texto_info2])
|
| 142 |
-
|
| 143 |
-
with gr.Tab("Evolución de Correlación"):
|
| 144 |
-
input_step = gr.Slider(1, 30, value=1, step=1, label="Cada cuántos días calcular")
|
| 145 |
-
input_window = gr.Slider(1, 60, value=12, step=1, label="Ventana rolling (meses)")
|
| 146 |
-
btn_plot = gr.Button("Mostrar Gráfico")
|
| 147 |
-
output_plot = gr.Plot()
|
| 148 |
-
btn_excel = gr.Button("Descargar en Excel")
|
| 149 |
-
output_file = gr.File()
|
| 150 |
-
output_msg = gr.Label()
|
| 151 |
-
|
| 152 |
-
btn_plot.click(fn=plot_evolucion_correlacion_gradio, inputs=[texto_info2, input_step, input_window], outputs=[output_plot, output_msg])
|
| 153 |
-
btn_excel.click(fn=exportar_correlacion, inputs=[texto_info2, input_step, input_window], outputs=[output_file, output_msg])
|
| 154 |
-
|
| 155 |
-
with gr.Tab("Stock vs BTC"):
|
| 156 |
-
btn_stock_btc = gr.Button("Cargar Gráfico")
|
| 157 |
-
output_iframe_stock = gr.HTML()
|
| 158 |
-
btn_stock_btc.click(fn=get_iframe_stock_vs_btc, inputs=[texto_info2], outputs=[output_iframe_stock])
|
| 159 |
-
|
| 160 |
-
with gr.Tab("Holding de Bitcoin"):
|
| 161 |
-
btn_holdings = gr.Button("Cargar Gráfico")
|
| 162 |
-
output_iframe_holdings = gr.HTML()
|
| 163 |
-
btn_holdings.click(fn=get_iframe_holdings, inputs=[texto_info2], outputs=[output_iframe_holdings])
|
| 164 |
-
|
| 165 |
-
with gr.Tab("Exportar Resumen Holding"):
|
| 166 |
-
input_template_url = gr.Textbox(label="URL de la plantilla de Excel (Google Drive)", value=DEFAULT_TEMPLATE_URL)
|
| 167 |
-
btn_descargar_resumen = gr.Button("Descargar Resumen a Excel")
|
| 168 |
-
output_resumen_file = gr.File()
|
| 169 |
-
output_resumen_msg = gr.Label()
|
| 170 |
-
btn_descargar_resumen.click(fn=descargar_resumen_holding_excel, inputs=[input_template_url], outputs=[output_resumen_file, output_resumen_msg])
|
| 171 |
-
|
| 172 |
-
demo.launch(share=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|