File size: 1,833 Bytes
639606c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | # @title función de datos:
import yfinance as yf
import pandas as pd
import numpy as np
from datetime import datetime, date
def data_option(ticker, date, option_type,size_of_the_window=4):
'''
El ticker es un string
la fecha es un string en formato 'yyyy-mm-dd'
el tipo de opción es un string 'call' o 'put' por defecto se mostrará el de call
'''
## la función está bien, sólo queda comprobar que el formato sea correcto, por ejemplo fecha es sting
price = yf.Ticker(ticker).info['regularMarketPrice']
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]]
data['impliedVolatility'] = data['impliedVolatility'].apply(lambda x: f"{x*100:.2f}%")
## now, we wanto to keep the 20 closest to spot options in the list
data = data.sort_values(by='strike', key=lambda x: abs(x - price))
price_row = pd.DataFrame([['░░░░░░░ Spot Price ░░░░░░░', price,'░░░░░░░░░░░░░░','░░░░░░░░░░','░░░░░░░░░░░░░░░░░░','░░░░░░░░░░░','░░░░░░░░░░']], columns= data.columns)
date_row = pd.DataFrame([[f"▓▓▓▓▓▓▓ {date} ▓▓▓▓▓▓▓",'▓▓▓▓▓▓▓','▓▓▓▓▓▓▓▓▓▓▓▓▓▓','▓▓▓▓▓▓▓▓▓▓', '▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓', '▓▓▓▓▓▓▓▓▓▓▓','▓▓▓▓▓▓▓▓▓▓']],columns = data.columns)
data = pd.concat( [data.head(size_of_the_window), price_row]).sort_values(by='strike')
data = pd.concat([date_row, data])
return pd.DataFrame(data)
## we wanto to add a row indicating the price of the underlying
|