-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathoptionchain.py
54 lines (40 loc) · 1.73 KB
/
optionchain.py
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# Obtendo dados de opções negociadas na B3 - Derivativos em Py #01
# Opções nos EUA
import yfinance as yf
# Defina o ticker
symbol = 'NVDA'
# Crie o objeto
stock = yf.Ticker(symbol)
# Colete dados das opções - datas de vencimento
options = stock.options
# E, finalmente, a option chain
for option_symbol in options:
option_chain = stock.option_chain(option_symbol)
# Call options
call_options = option_chain.calls
print(f"Call Options para {option_symbol}:")
print(call_options)
# Put options
put_options = option_chain.puts
print(f"Put Options para {option_symbol}:")
print(put_options)
# Opções na B3
import pandas as pd
import requests
subjacente = 'PETR4' # não vem do Yfinance! esqueça o ".SA" ao final do ticker!
# Um vencimento
vencimento = '2024-12-20' # YYYY-MM-DD
def optionchaindate(subjacente, vencimento):
url = f'https://opcoes.net.br/listaopcoes/completa?idAcao={subjacente}&listarVencimentos=false&cotacoes=true&vencimentos={vencimento}'
r = requests.get(url).json()
x = ([subjacente, vencimento, i[0].split('_')[0], i[2], i[3], i[5], i[8], i[9], i[10]] for i in r['data']['cotacoesOpcoes'])
return pd.DataFrame(x, columns=['subjacente', 'vencimento', 'ativo', 'tipo', 'modelo', 'strike', 'preco', 'negocios', 'volume'])
optionchaindate(subjacente, vencimento)
# Todos os vencimentos
def optionchain(subjacente):
url2 = f'https://opcoes.net.br/listaopcoes/completa?idLista=ML&idAcao={subjacente}&listarVencimentos=true&cotacoes=true'
r = requests.get(url2).json()
vencimentos = [i['value'] for i in r['data']['vencimentos']]
df = pd.concat([optionchaindate(subjacente, vencimento) for vencimento in vencimentos])
return df
optionchain(subjacente)