Skip to content

Instantly share code, notes, and snippets.

@Vido
Created August 17, 2020 22:57
Show Gist options
  • Save Vido/17608ccfa1da31b2fcc6380658cb3f7a to your computer and use it in GitHub Desktop.
Save Vido/17608ccfa1da31b2fcc6380658cb3f7a to your computer and use it in GitHub Desktop.
import numpy as np
import pandas as pd
import yfinance
import matplotlib.pyplot as mplt
from bs4 import BeautifulSoup
import dryscrape
iframe_url = 'http://www2.bmf.com.br/pages/portal/bmfbovespa/lumis/lum-taxas-referenciais-bmf-ptBR.asp'
session = dryscrape.Session()
session.visit(iframe_url)
response = session.body()
soup = BeautifulSoup(response, 'html.parser')
# Curva de Taxa de Juros
df_ettj = pd.read_html(
str(soup.table),
header=1,
decimal=',',
thousands='.',
)[0]
df_ettj.columns = ['Dias', '252', '360']
# Volatilidade
data = yfinance.download(
tickers = 'VALE3.SA',
start="2018-08-08",
end="2020-08-08",
interval = '1d',
treads = False
)
data['LogReturn'] = np.log(data['Close']
/ data['Close'].shift(1))
data['Volatility'] = data['LogReturn'].rolling(21).std() * np.sqrt(252)
# Exemplo: VALEI603 60,30 21/setembro
# Data do Cálculo: 08/agosto/2020
S0 = 60.45 # initial index level
K = 60.30 # strike price
T = 29/252 # time-to-maturity
r = df_ettj.loc[df_ettj['Dias'] == 29]['252'].values[0] / 100 # riskless short rate
sigma = data['Volatility'][-1]
I = 10000000 # number of simulations
# Valuation Algorithm
z = np.random.standard_normal(I) # pseudorandom numbers
ST = S0 * np.exp((r - 0.5 * sigma ** 2) * T + sigma * np.sqrt(T) * z)
# index values at maturity
hT = np.maximum(ST - K, 0) # inner values at maturity
C0 = np.exp(-r * T) * np.sum(hT) / I # Monte Carlo estimator
# Result Output
print("Valor teórico da Opção Européia %5.3f" % C0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment