options / processes.py
Quant0's picture
Create processes.py
639606c verified
Raw History Blame Contribute Delete
1.83 kB
# @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