Skip to content

Instantly share code, notes, and snippets.

@Vido
Created March 20, 2021 20:27
Show Gist options
  • Save Vido/91bd23490da61c57cd17162e8edb4ac8 to your computer and use it in GitHub Desktop.
Save Vido/91bd23490da61c57cd17162e8edb4ac8 to your computer and use it in GitHub Desktop.
import time
import locale
locale.setlocale(locale.LC_ALL, 'pt_BR.UTF-8')
import requests
import pandas as pd
from bs4 import BeautifulSoup
def parse_df(content):
soup = BeautifulSoup(content, 'html.parser')
table = soup.find('table', {'class': 'histo-results'})
df = pd.read_html(
str(table),
decimal=',',
thousands='.',
)[0]
df['Data'] = pd.to_datetime(df['Data'], format='%d %b %Y')
df.set_index('Data')
return df
def get_advfn_df(contratos):
_DFS = {}
for contrato in contratos:
time.sleep(2)
advfn_url = f'https://br.advfn.com/bolsa-de-valores/bmf/{contrato}/historico/mais-dados-historicos'
response = requests.get(advfn_url)
df = parse_df(response.content)
_DFS[contrato] = df
data = pd.concat(_DFS.values(), axis=1, keys=_DFS.keys())
data.columns = data.columns.swaplevel(0, 1)
data.sort_index(level=0, axis=1, inplace=True)
return data
if __name__ == '__main__':
contratos = [
'BGIK21',
'ICFK21',
'CCMK21',
'SJCK21',
'ETHK21',
]
data = get_advfn_df(contratos)
corr = data['Fechamento'].corr()
import seaborn as sns
import matplotlib.pyplot as plt
ax = sns.heatmap(
corr,
vmin=-1, vmax=1, center=0,
square=True,
annot=True,
linewidths=.8
)
# Fix - https://github.com/matplotlib/matplotlib/issues/14751
ax.set_ylim(len(corr), 0)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment