Last active
September 26, 2021 17:06
-
-
Save Vido/978a34be9b1d97bdf9cd03958fab4e84 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import yfinance | |
import dryscrape | |
from bs4 import BeautifulSoup | |
import pandas as pd | |
def get_b3_html(): | |
url = 'https://sistemaswebb3-listados.b3.com.br/indexPage/day/IBOV?language=pt-br' | |
session = dryscrape.Session() | |
session.visit(url) | |
time.sleep(10) # make sure it loads properly | |
session.render('b3_1.png') # show what is loaded 1 | |
select = session.at_xpath('//*[@id="selectPage"]') | |
select.set("120") | |
select.select_option() | |
time.sleep(10) # make sure it loads properly | |
session.render('b3_2.png') # show what is loaded 2 | |
response = session.body() | |
with open('b3.html', 'w') as fp: | |
# Cache | |
fp.write(response) | |
return response | |
def get_df(html): | |
soup = BeautifulSoup(html, 'html.parser') | |
table = soup.find('table') | |
df = pd.read_html( | |
str(table), | |
decimal=',', | |
thousands='.', | |
)[0] | |
mask = df['Código'].isin(['Quantidade Teórica Total', 'Redutor']) | |
comp, redutor = df[~mask], df[mask] | |
# for yfinance | |
comp['Código'] = comp['Código'] + '.SA' | |
comp.set_index("Código", inplace=True) | |
redutor.set_index("Código", inplace=True) | |
# Cache | |
comp.to_pickle('composition.df') | |
redutor.to_pickle('redutor.df') | |
return comp, redutor | |
def get_market_data(): | |
market_data = yfinance.download( | |
tickers = comp.index.tolist(), | |
start="2021-04-14", | |
end="2021-04-16", | |
interval = '1d', | |
treads = False | |
) | |
return market_data | |
html = get_b3_html() | |
comp, redutor = get_df(html) | |
market_data = get_market_data() | |
ibov_index = comp['Qtde. Teórica'] * market_data['Close'].loc['2021-04-15'] | |
ibov = ibov_index.sum() / redutor['Qtde. Teórica'].loc['Redutor'] | |
print('%.2f' % ibov) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment