Quant0/options
0
1# @title función de datos:2import yfinance as yf3import pandas as pd4import numpy as np5from datetime import datetime, date6def data_option(ticker, date, option_type,size_of_the_window=4):7 8 9 '''10 El ticker es un string11 la fecha es un string en formato 'yyyy-mm-dd'12 el tipo de opción es un string 'call' o 'put' por defecto se mostrará el de call13 '''14 15## la función está bien, sólo queda comprobar que el formato sea correcto, por ejemplo fecha es sting16 price = yf.Ticker(ticker).info['regularMarketPrice']17 18 data = yf.Ticker(ticker).option_chain(date).puts.iloc[:, [0, 2, 3, 8, 10, 11, 13]] if option_type == 'put' else yf.Ticker(ticker).option_chain(date).calls.iloc[:, [0, 2, 3, 8, 10, 11, 13]]19 data['impliedVolatility'] = data['impliedVolatility'].apply(lambda x: f"{x*100:.2f}%")20 ## now, we wanto to keep the 20 closest to spot options in the list21 22 data = data.sort_values(by='strike', key=lambda x: abs(x - price))23 24 price_row = pd.DataFrame([['░░░░░░░ Spot Price ░░░░░░░', price,'░░░░░░░░░░░░░░','░░░░░░░░░░','░░░░░░░░░░░░░░░░░░','░░░░░░░░░░░','░░░░░░░░░░']], columns= data.columns)25 date_row = pd.DataFrame([[f"▓▓▓▓▓▓▓ {date} ▓▓▓▓▓▓▓",'▓▓▓▓▓▓▓','▓▓▓▓▓▓▓▓▓▓▓▓▓▓','▓▓▓▓▓▓▓▓▓▓', '▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓', '▓▓▓▓▓▓▓▓▓▓▓','▓▓▓▓▓▓▓▓▓▓']],columns = data.columns)26 data = pd.concat( [data.head(size_of_the_window), price_row]).sort_values(by='strike')27 data = pd.concat([date_row, data])28 29 return pd.DataFrame(data)30 ## we wanto to add a row indicating the price of the underlying31 32 