def test_quoteDF(self): from pyEX import quoteDF with patch('requests.get') as mock: mock.return_value = MagicMock() mock.return_value.status_code = 200 mock.return_value.json = MagicMock(return_value={'test': [4], 'symbol': ['test']}) quoteDF('test')
def test_quoteDF(self): from pyEX import quoteDF with patch("requests.get") as mock, patch("pickle.dump"): mock.return_value = MagicMock() mock.return_value.status_code = 200 mock.return_value.json = MagicMock( return_value={"test": [4], "symbol": ["test"]} ) quoteDF("test")
def read(self, symbol, field, fetch=True, fill=False): field = field.upper() symbol = symbol.upper() if field in ('QUOTE'): # dont cache, instantaneous return p.quoteDF(symbol) elif field in ('COMPOSITION'): return refetch(field, symbol) if field not in self.libraries and not fetch: return pd.DataFrame() l = _getLib(self.db, field) if not l.has_symbol(symbol): if not fetch: return pd.DataFrame() df = pd.DataFrame() else: df = l.read(symbol).data metadata = l.read_metadata(symbol).metadata if fetch: if df.empty or not metadata or not metadata.get('timestamp') or \ metadata.get('timestamp', never()) <= never() or \ metadata.get('timestamp', never()) < _updateTime(field): df = refetch(field, symbol) if fill: l.write(symbol, df, metadata={'timestamp': datetime.now()}) return df
def pyex_get_co_info(sym): #co_info = pyex.companyDF(sym) #key_stats = pyex.stockStatsDF(sym) stock_quote = pyex.quoteDF(sym) # the pyex.price() function returns a delayed price, must use pyex.last for real time # but pyex.last() returns empty when market is closed. last_price_df = pd.DataFrame({ 'symbol': [sym], 'lastprice': [pyex.price(sym)] }) last_price_df.set_index('symbol', inplace=True) #co_info = pd.concat([co_info, key_stats, stock_quote, last_price_df], axis = 1) co_info = pd.concat([stock_quote, last_price_df], axis=1) return co_info
def test_quoteDF(self): from pyEX import quoteDF quoteDF(C)
def test_quoteDF(self): from pyEX import quoteDF with patch('requests.get') as mock: mock.return_value = MagicMock() mock.return_value.status_code = 200 quoteDF('test')
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