def test_statsDF(self): from pyEX import stockStatsDF with patch('requests.get') as mock: mock.return_value = MagicMock() mock.return_value.status_code = 200 mock.return_value.json = MagicMock(return_value=[{'financials': [{'reportDate': 1}], 'symbol': 'aapl'}]) stockStatsDF('test')
def test_statsDF(self): from pyEX import stockStatsDF with patch('requests.get') as mock: mock.return_value = MagicMock() mock.return_value.status_code = 200 mock.return_value.json = MagicMock(return_value=[]) stockStatsDF('test')
def refetch(field, symbol): if field == 'TICK': return p.chartDF(symbol, '1d') if field == 'FINANCIALS': return p.financialsDF(symbol) elif field == 'DAILY': return p.chartDF(symbol, '5y') elif field == 'COMPANY': return p.companyDF(symbol) elif field == 'EARNINGS': return p.earningsDF(symbol) elif field == 'DIVIDENDS': return p.dividendsDF(symbol) elif field == 'NEWS': return p.newsDF(symbol) elif field == 'STATS': return p.stockStatsDF(symbol) elif field == 'COMPOSITION': return _fetchComposition(symbol) elif field == 'PEERS': return p.peersDF(symbol) raise NotImplementedError('%s - %s' % (field, symbol))
def test_sstatsDF(self): from pyEX import stockStatsDF stockStatsDF(C)
def fetchDF(self, key, field, _ret=True): # tickers always caps key = key.upper() # fields always lower field = field.lower() if not (self._tickers['symbol'] == key).any(): # FIXME return pd.DataFrame() if key not in self._cache: # initialize cache self._cache[key] = {} self._cache[key]['timestamp'] = {} if field in ('financials', 'all'): if 'financials' not in self._cache[key] or self._check_timestamp( key, 'financials'): try: self._cache[key]['financials'] = p.financialsDF(key) except KeyError: self._cache[key]['financials'] = pd.DataFrame() self._cache[key]['timestamp']['financials'] = datetime.now() if field in ('chart', 'all'): if 'chart' not in self._cache[key] or self._check_timestamp( key, 'chart'): try: self._cache[key]['chart'] = p.chartDF(key, '1y') except KeyError: self._cache[key]['chart'] = pd.DataFrame() self._cache[key]['timestamp']['chart'] = datetime.now() if field in ('company', 'all'): if 'company' not in self._cache[key] or self._check_timestamp( key, 'company'): self._cache[key]['company'] = p.companyDF(key) self._cache[key]['timestamp']['company'] = datetime.now() if field in ('quote', 'all'): # always update self._cache[key]['quote'] = p.quoteDF(key) if field in ('dividends', 'all'): if 'dividends' not in self._cache[key] or self._check_timestamp( key, 'dividends'): try: self._cache[key]['dividends'] = p.dividendsDF(key) except KeyError: self._cache[key]['dividends'] = pd.DataFrame() self._cache[key]['timestamp']['dividends'] = datetime.now() if field in ('earnings', 'all'): if 'earnings' not in self._cache[key] or self._check_timestamp( key, 'earnings'): try: self._cache[key]['earnings'] = p.earningsDF(key) except KeyError: self._cache[key]['earnings'] = pd.DataFrame() self._cache[key]['timestamp']['earnings'] = datetime.now() if field in ('news', 'all'): if 'news' not in self._cache[key] or self._check_timestamp( key, 'news'): try: self._cache[key]['news'] = p.newsDF(key) except KeyError: self._cache[key]['news'] = pd.DataFrame() self._cache[key]['timestamp']['news'] = datetime.now() if field in ('peers', 'all'): if 'peers' not in self._cache[key] or self._check_timestamp( key, 'peers'): try: peers = p.peersDF(key) except KeyError: peers = pd.DataFrame() if peers is not None and not peers.empty: peers = peers.replace({np.nan: None}) infos = pd.concat( [p.companyDF(item) for item in peers['symbol'].values]) self._cache[key]['peers'] = infos else: self._cache[key]['peers'] = pd.DataFrame() self._cache[key]['timestamp']['peers'] = datetime.now() if field in ('stats', 'all'): if 'stats' not in self._cache[key] or self._check_timestamp( key, 'stats'): try: self._cache[key]['stats'] = p.stockStatsDF(key) except KeyError: self._cache[key]['stats'] = pd.DataFrame() self._cache[key]['timestamp']['stats'] = datetime.now() if field in ('composition', 'all'): if 'company' not in self._cache[key]: self.fetchDF(key, 'company', _ret=False) try: self._cache[key]['composition'] = pd.read_html( ETF_URL % key, attrs={'id': 'etfs-that-own'})[0] self._cache[key]['composition']['% of Total'] = self._cache[ key]['composition']['% of Total'].str.rstrip('%').astype( float) / 100.0 self._cache[key]['composition'].columns = [ 'Symbol', 'Name', 'Percent' ] self._cache[key]['composition'] = self._cache[key][ 'composition'][['Symbol', 'Percent', 'Name']] except (IndexError, requests.HTTPError, ValueError, HTTPError): self._cache[key]['composition'] = pd.DataFrame() self._cache[key]['timestamp']['composition'] = datetime.now() if _ret: # pull data if field == 'all': ret = copy.deepcopy(self._cache[key]) del ret['timestamp'] ret = pd.concat(ret) elif field in self._cache[key]: # or i have that field ret = pd.concat({field: self._cache[key][field]}) else: raise Exception('No ticker provided!') return ret