-
Notifications
You must be signed in to change notification settings - Fork 0
/
code4_GetAllPrices.py
64 lines (53 loc) · 1.82 KB
/
code4_GetAllPrices.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
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Dec 15 10:32:13 2019
@author: itamar
"""
import pickle
import datetime as dt
import pandas as pd
import os
import pandas_datareader as web
import requests
import bs4 as bs
#First of all you need to find a site with the table list of the b3 companies
def save_B3_tickers():
tickers = []
for i in range (24):
site = 'https://br.advfn.com/bolsa-de-valores/bovespa/'
site = site + chr(ord('A')+i)
resp = requests.get(site)
soup = bs.BeautifulSoup(resp.text,'lxml')
table = soup.find('table',{'class':'atoz-link-bov'})
for row in table.findAll('tr')[1:]:
ticker = row.findAll('td')[1].text
if not ticker.endswith('L') and not ticker.endswith('B') and not len(ticker) > 6:
print(ticker)
tickers.append(ticker)
with open ("bovtickers.pickle","wb") as f:
pickle.dump(tickers,f)
print(tickers)
return tickers
def get_data_from_yahoo(reload_b3 = False):
if reload_b3:
save_B3_tickers()
else:
with open("bovtickers.pickle", "rb") as f:
tickers = pickle.load(f)
if not os.path.exists('stock_dfs'):
os.makedirs('stock_dfs')
start = dt.datetime(2000,1,1)
end = dt.datetime(2020,4,30)
for ticker in tickers:
print(ticker)
df = pd.DataFrame()
if not os.path.exists('stock_dfs/{}.csv'.format(ticker)):
try:
df = web.DataReader(ticker + '.SA','yahoo',start,end)
except:
print('No data for {}'.format(ticker))
df.to_csv('stock_dfs/{}.csv'.format(ticker))
else:
print('Already have {}'.format(ticker))
get_data_from_yahoo()