Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# @title Web interactiva
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import yfinance as yf
|
| 5 |
+
import matplotlib.pyplot as plt
|
| 6 |
+
import numpy as np
|
| 7 |
+
import os
|
| 8 |
+
|
| 9 |
+
# Funci贸n para obtener los datos y mostrarlos en el DataFrame de la UI
|
| 10 |
+
|
| 11 |
+
def generar_tabla(option_type, ticker_input, expiration_date, size_of_the_window):
|
| 12 |
+
all_tables = []
|
| 13 |
+
ticker_input = ticker_input.upper()
|
| 14 |
+
|
| 15 |
+
if yf.Ticker(ticker_input).options:
|
| 16 |
+
if expiration_date is None or expiration_date == "":
|
| 17 |
+
for date in yf.Ticker(ticker_input).options:
|
| 18 |
+
table = data_option(ticker_input, date, option_type, size_of_the_window)
|
| 19 |
+
if not table.empty:
|
| 20 |
+
table['dates'] = date
|
| 21 |
+
all_tables.append(table)
|
| 22 |
+
df_final = pd.concat(all_tables, ignore_index=True)
|
| 23 |
+
else:
|
| 24 |
+
fechas = pd.Series(yf.Ticker(ticker_input).options)
|
| 25 |
+
timedeltas = fechas.apply(lambda x: pd.to_datetime(x) - pd.to_datetime(expiration_date))
|
| 26 |
+
expiration_date1 = fechas[timedeltas.abs().idxmin()]
|
| 27 |
+
df_final = data_option(ticker_input, expiration_date1, option_type, size_of_the_window)
|
| 28 |
+
return df_final
|
| 29 |
+
else:
|
| 30 |
+
return pd.DataFrame()
|
| 31 |
+
|
| 32 |
+
# Funci贸n para generar y devolver el archivo Excel
|
| 33 |
+
def exportar_a_excel(option_type, ticker_input, expiration_date, size_of_the_window):
|
| 34 |
+
df_final = generar_tabla(option_type, ticker_input, expiration_date, size_of_the_window)
|
| 35 |
+
if not df_final.empty:
|
| 36 |
+
nombre_archivo = f"{ticker_input.upper()}_opciones_{option_type}.xlsx"
|
| 37 |
+
df_final.to_excel(nombre_archivo, index=False)
|
| 38 |
+
return nombre_archivo
|
| 39 |
+
return None
|
| 40 |
+
|
| 41 |
+
# Configuraci贸n de Gradio
|
| 42 |
+
# --- Valores por defecto para inicializar la tabla ---
|
| 43 |
+
initial_df = generar_tabla("put", "MSTR", None, 3)
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
with gr.Blocks(title="An谩lisis de Opciones") as demo:
|
| 47 |
+
# A帽ade esta l铆nea con la URL de tu imagen
|
| 48 |
+
gr.Image("https://cdn.prod.website-files.com/67174dcb1d43b06a0eae317e/672bbbc7a2b88045e5661994_logo-afi.svg", show_label=False)
|
| 49 |
+
|
| 50 |
+
gr.Markdown("# Analizador de Opciones")
|
| 51 |
+
gr.Markdown("#### Herramienta para visualizar y exportar datos de opciones.")
|
| 52 |
+
gr.Markdown("Introduzca el tipo de opci贸n que desea consultar, indicando el Ticker y la Fecha de vencimiento. Si no se especifica una fecha de vencimiento, se mostrar谩n todas las disponibles.")
|
| 53 |
+
with gr.Row():
|
| 54 |
+
option_type = gr.Radio(choices=["put", "call"], label="Tipo de opci贸n", value="put")
|
| 55 |
+
ticker_input = gr.Textbox(label="Ticker (ej. GOOGL)", value="MSTR")
|
| 56 |
+
expiration_date = gr.Textbox(label="Fecha de vencimiento (formato: aaaa-mm-dd)", value = "" )
|
| 57 |
+
size_of_the_window = gr.Slider(minimum=1, maximum=20, value=8, step=1, label="Tama帽o de la ventana")
|
| 58 |
+
|
| 59 |
+
with gr.Row():
|
| 60 |
+
btn_mostrar = gr.Button("Mostrar datos", )
|
| 61 |
+
btn_descargar = gr.Button("Descargar en Excel")
|
| 62 |
+
|
| 63 |
+
output_df = gr.DataFrame(label="Datos de Opciones", value=initial_df)
|
| 64 |
+
output_file = gr.File(label="Archivo Excel")
|
| 65 |
+
|
| 66 |
+
btn_mostrar.click(
|
| 67 |
+
fn=generar_tabla,
|
| 68 |
+
inputs=[option_type, ticker_input, expiration_date, size_of_the_window],
|
| 69 |
+
outputs=output_df
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
btn_descargar.click(
|
| 73 |
+
fn=exportar_a_excel,
|
| 74 |
+
inputs=[option_type, ticker_input, expiration_date, size_of_the_window],
|
| 75 |
+
outputs=output_file
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
# Inicia la aplicaci贸n
|
| 79 |
+
demo.launch(share=True)
|