Last active
May 8, 2019 10:44
-
-
Save mpersano/0542eb60b5aa19bc7efa9aaf2321fe34 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 datetime | |
| # figure out how many different companies were traded on the Brazilian stock exchange per month | |
| # data: http://www.bmfbovespa.com.br/en_us/services/market-data/historical-data/equities/historical-data/ | |
| # file format: http://www.bmfbovespa.com.br/en_us/services/market-data/historical-data/equities/historical-data-1/ | |
| class QuoteCache: | |
| def __init__(self): | |
| self.stocks = {} | |
| def read_quote_file(self, year): | |
| base_filename = 'COTAHIST_A%04d.TXT' % year | |
| with open(base_filename, 'r') as f: | |
| for line in f: | |
| self._parse_quote_line(line) | |
| def _to_datetime(self, s): | |
| y, m, d = int(s[:4]), int(s[4:6]), int(s[6:]) | |
| return datetime.date(y, m, d) | |
| def _parse_quote_line(self, line): | |
| if line[:2] != '01': | |
| return | |
| # ignore fundos de investimento ou BDRs | |
| spec = line[39:48].strip() | |
| spec_parts = spec.split() | |
| if len(spec_parts) > 0: | |
| share_type = spec_parts[0] | |
| if share_type in ('CI', 'DRN'): | |
| return | |
| # so' mercado 'a vista | |
| market_type = line[24:27] | |
| if market_type != '010': | |
| return | |
| ticker = line[12:24].strip() | |
| date = self._to_datetime(line[2:10]) | |
| year_month = (date.year, date.month) | |
| self.stocks.setdefault(year_month, set()).add(ticker[:4]) | |
| cache = QuoteCache() | |
| for year in range(1986, 2019): | |
| cache.read_quote_file(year) | |
| for year_month in sorted(cache.stocks.keys()): | |
| print "%04d-%02d\t%d" % (year_month[0], year_month[1], len(cache.stocks[year_month])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment