Update app.py
Browse files
app.py
CHANGED
|
@@ -6,7 +6,7 @@ import matplotlib.pyplot as plt
|
|
| 6 |
import numpy as np
|
| 7 |
import os
|
| 8 |
|
| 9 |
-
from processes import
|
| 10 |
# Funci贸n para obtener los datos y mostrarlos en el DataFrame de la UI
|
| 11 |
|
| 12 |
def generar_tabla(option_type, ticker_input, expiration_date, size_of_the_window):
|
|
@@ -29,7 +29,68 @@ def generar_tabla(option_type, ticker_input, expiration_date, size_of_the_window
|
|
| 29 |
return df_final
|
| 30 |
else:
|
| 31 |
return pd.DataFrame()
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
def generar_todo(ticker_input, expiration_date, size_of_the_window):
|
| 34 |
ticker_input = ticker_input
|
| 35 |
puts = generar_tabla("put", ticker_input, expiration_date, size_of_the_window)
|
|
@@ -128,7 +189,17 @@ with gr.Blocks(title="An谩lisis de Opciones") as demo:
|
|
| 128 |
outputs=[output_file, output_msg]
|
| 129 |
)
|
| 130 |
|
| 131 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
|
| 133 |
|
| 134 |
|
|
|
|
| 6 |
import numpy as np
|
| 7 |
import os
|
| 8 |
|
| 9 |
+
from processes import *
|
| 10 |
# Funci贸n para obtener los datos y mostrarlos en el DataFrame de la UI
|
| 11 |
|
| 12 |
def generar_tabla(option_type, ticker_input, expiration_date, size_of_the_window):
|
|
|
|
| 29 |
return df_final
|
| 30 |
else:
|
| 31 |
return pd.DataFrame()
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def parse_opciones_table(df):
|
| 35 |
+
"""
|
| 36 |
+
Limpia y convierte un DataFrame extra铆do de una tabla decorada de opciones.
|
| 37 |
+
Filtra filas v谩lidas, convierte tipos y normaliza valores booleanos.
|
| 38 |
+
"""
|
| 39 |
+
# Filtrar filas que contienen s铆mbolos v谩lidos de opciones (ej. GOOGL251219P00165000)
|
| 40 |
+
df_clean = df[df['contractSymbol'].str.contains(r'^[A-Z]{4,6}\d{6}[PC]\d{8}$', regex=True)].copy()
|
| 41 |
+
|
| 42 |
+
# Convertir columnas relevantes a tipos num茅ricos
|
| 43 |
+
df_clean['strike'] = pd.to_numeric(df_clean['strike'], errors='coerce')
|
| 44 |
+
df_clean['volume'] = pd.to_numeric(df_clean['volume'], errors='coerce')
|
| 45 |
+
df_clean['lastPrice'] = pd.to_numeric(df_clean['lastPrice'], errors='coerce')
|
| 46 |
+
df_clean['impliedVolatility'] = df_clean['impliedVolatility'].astype(str).str.replace('%', '').astype(float) / 100
|
| 47 |
+
|
| 48 |
+
# Normalizar columna inTheMoney
|
| 49 |
+
#df_clean['inTheMoney'] = df_clean['inTheMoney'].astype(str).str.upper().map({'VERDADERO': True, 'FALSO': False})
|
| 50 |
+
|
| 51 |
+
# Eliminar filas con valores nulos en columnas clave
|
| 52 |
+
df_clean = df_clean.dropna(subset=['strike', 'volume', 'lastPrice', 'impliedVolatility'])
|
| 53 |
+
|
| 54 |
+
return df_clean
|
| 55 |
+
|
| 56 |
+
def plot_opciones(df_put, df_call, spot_price):
|
| 57 |
+
|
| 58 |
+
puts_itm = df_put[df_put['strike'] > spot_price]
|
| 59 |
+
puts_otm = df_put[df_put['strike'] <= spot_price]
|
| 60 |
+
calls_itm = df_call[df_call['strike'] < spot_price]
|
| 61 |
+
calls_otm = df_call[df_call['strike'] >= spot_price]
|
| 62 |
+
|
| 63 |
+
fig = go.Figure()
|
| 64 |
+
fig.add_trace(go.Bar(x=puts_itm['strike'], y=puts_itm['volume'], name='PUT ITM', marker_color='green', width=0.3))
|
| 65 |
+
fig.add_trace(go.Bar(x=puts_otm['strike'], y=puts_otm['volume'], name='PUT OTM', marker_color='red',width=0.3))
|
| 66 |
+
fig.add_trace(go.Bar(x=calls_itm['strike'], y=calls_itm['volume'], name='CALL ITM', marker_color='blue',width=0.3))
|
| 67 |
+
fig.add_trace(go.Bar(x=calls_otm['strike'], y=calls_otm['volume'], name='CALL OTM', marker_color='orange',width=0.3))
|
| 68 |
+
fig.add_trace(go.Scatter(x=[spot_price], y=[0], mode='markers+text',
|
| 69 |
+
marker=dict(color='black', size=10),
|
| 70 |
+
text=[f'Spot: {spot_price}'],
|
| 71 |
+
textposition='top center',
|
| 72 |
+
name='Spot Price'))
|
| 73 |
+
# Agrega la l铆nea vertical discontinua
|
| 74 |
+
fig.add_trace(go.Scatter(
|
| 75 |
+
x=[spot_price, spot_price],
|
| 76 |
+
y=[0, max(max(puts_itm['volume']), max(puts_otm['volume']), max(calls_itm['volume']), max(calls_otm['volume'])) * 1.1],
|
| 77 |
+
mode='lines',
|
| 78 |
+
name=f'Spot: {spot_price}',
|
| 79 |
+
marker=dict(color='black', size=10),
|
| 80 |
+
line=dict(dash='dash'),
|
| 81 |
+
showlegend=False
|
| 82 |
+
))
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
fig.update_layout(title='Distribuci贸n de Volumen de Opciones PUT y CALL',
|
| 86 |
+
xaxis_title='Strike',
|
| 87 |
+
yaxis_title='Volumen',
|
| 88 |
+
barmode='stack',
|
| 89 |
+
template='plotly_white')
|
| 90 |
+
fig.show()
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
|
| 94 |
def generar_todo(ticker_input, expiration_date, size_of_the_window):
|
| 95 |
ticker_input = ticker_input
|
| 96 |
puts = generar_tabla("put", ticker_input, expiration_date, size_of_the_window)
|
|
|
|
| 189 |
outputs=[output_file, output_msg]
|
| 190 |
)
|
| 191 |
|
| 192 |
+
with gr.Tab("Analizador"):
|
| 193 |
+
|
| 194 |
+
output_plot = gr.Plot(label="Distribuci贸n de Volumen de las opciones")
|
| 195 |
+
|
| 196 |
+
|
| 197 |
+
gr.on(
|
| 198 |
+
triggers=None, # Esto hace que se ejecute al cargar
|
| 199 |
+
fn=plot_opciones,
|
| 200 |
+
inputs=[parse_opciones_table(output_df_put,output_df_plot, price],
|
| 201 |
+
outputs=output_plot
|
| 202 |
+
)
|
| 203 |
|
| 204 |
|
| 205 |
|