Skip to content

Instantly share code, notes, and snippets.

@Fabiocke
Last active August 25, 2022 02:06
Show Gist options
  • Select an option

  • Save Fabiocke/689183af3efa5045c0976c0a72b91495 to your computer and use it in GitHub Desktop.

Select an option

Save Fabiocke/689183af3efa5045c0976c0a72b91495 to your computer and use it in GitHub Desktop.
import requests
import pandas as pd
# Obtem os vencimentos
def vencimentos(ticker):
url = f'https://opcoes.net.br/listaopcoes/completa?au=False&uinhc=0&idLista=ML&idAcao={ticker}&listarVencimentos=true&cotacoes=true'
response = requests.get(url, verify=False).json()
vctos = [[i['value'], i['text']] for i in response['data']['vencimentos']]
return vctos
# Obtem as opções
def listar_opcoes(ticker):
# Busca os vencimentos
vctos = vencimentos(ticker)
opcs=[]
# Busca as opções para cada data de vencimento
for vcto in vctos:
url=f'https://opcoes.net.br/listaopcoes/completa?au=False&uinhc=0&idLista=ML&idAcao={ticker}&listarVencimentos=false&cotacoes=true&vencimentos={vcto[0]}'
response = requests.get(url).json()
# Busca apenas os campos necessários
opcs += ([[ticker]+[i[0][:i[0].find('_')]] + i[2:4] + [vcto[1]] + [i[5]] + [i[8]] for i in response['data']['cotacoesOpcoes']])
# Gera o dataframe
colunas = ['ATIVO_OBJ','ATIVO', 'TIPO', 'MOD', 'DT_VCTO', 'STRIKE', 'PRECO']
opcs = pd.DataFrame(opcs, columns=colunas)
# transforma o campo vencimento e datetime
opcs['DT_VCTO'] = pd.to_datetime(opcs['DT_VCTO'])
return opcs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment