def _lookup_most_recent_symbols(self, sids): symbol_cols = self.equity_symbol_mappings.c symbols = { row.sid: {c: row[c] for c in symbol_columns} for row in concat( self.engine.execute( sa.select( (symbol_cols.sid,) + tuple(map(op.getitem(symbol_cols), symbol_columns)), ).where( symbol_cols.sid.in_(map(int, sid_group)), ).order_by( symbol_cols.end_date.desc(), ).group_by( symbol_cols.sid, ) ).fetchall() for sid_group in partition_all( SQLITE_MAX_VARIABLE_NUMBER, sids ), ) } if len(symbols) != len(sids): raise EquitiesNotFound( sids=set(sids) - set(symbols), plural=True, ) return symbols
def _retrieve_assets(self, sids, asset_tbl, asset_type): """ Internal function for loading assets from a table. This should be the only method of `AssetFinder` that writes Assets into self._asset_cache. Parameters --------- sids : iterable of int Asset ids to look up. asset_tbl : sqlalchemy.Table Table from which to query assets. asset_type : type Type of asset to be constructed. Returns ------- assets : dict[int -> Asset] Dict mapping requested sids to the retrieved assets. """ # Fastpath for empty request. if not sids: return {} cache = self._asset_cache hits = {} querying_equities = issubclass(asset_type, Equity) filter_kwargs = ( _filter_equity_kwargs if querying_equities else _filter_future_kwargs ) rows = self._retrieve_asset_dicts(sids, asset_tbl, querying_equities) for row in rows: sid = row['sid'] # pd.DataFrame(row,index=[0]).to_csv("yuusha note") asset = asset_type(**filter_kwargs(row)) setattr(asset, 'strike', row['strike']) setattr(asset, 'callput', row['callput']) setattr(asset, 'style', row['style']) setattr(asset, 'expiration', row['expiration']) setattr(asset, 'underlying', row['underlying']) hits[sid] = cache[sid] = asset # If we get here, it means something in our code thought that a # particular sid was an equity/future and called this function with a # concrete type, but we couldn't actually resolve the asset. This is # an error in our code, not a user-input error. misses = tuple(set(sids) - viewkeys(hits)) if misses: if querying_equities: raise EquitiesNotFound(sids=misses) else: raise FutureContractsNotFound(sids=misses) return hits
def _retrieve_assets(self, sids, asset_tbl, asset_type): """ Internal function for loading assets from a table. This should be the only method of `AssetFinder` that writes Assets into self._asset_cache. Parameters --------- sids : iterable of int Asset ids to look up. asset_tbl : sqlalchemy.Table Table from which to query assets. asset_type : type Type of asset to be constructed. Returns ------- assets : dict[int -> Asset] Dict mapping requested sids to the retrieved assets. """ # Fastpath for empty request. if not sids: return {} cache = self._asset_cache hits = {} for assets in group_into_chunks(sids): # Load misses from the db. query = self._select_assets_by_sid(asset_tbl, assets) for row in imap(dict, query.execute().fetchall()): asset = asset_type(**_convert_asset_timestamp_fields(row)) sid = asset.sid hits[sid] = cache[sid] = asset # If we get here, it means something in our code thought that a # particular sid was an equity/future and called this function with a # concrete type, but we couldn't actually resolve the asset. This is # an error in our code, not a user-input error. misses = tuple(set(sids) - viewkeys(hits)) if misses: if asset_type == Equity: raise EquitiesNotFound(sids=misses) else: raise FutureContractsNotFound(sids=misses) return hits
def _lookup_most_recent_symbols(self, sids): symbols = { row.sid: {c: row[c] for c in symbol_columns} for row in concat( self.engine.execute( self._select_most_recent_symbols_chunk(sid_group), ).fetchall() for sid_group in partition_all( SQLITE_MAX_VARIABLE_NUMBER, sids ), ) } if len(symbols) != len(sids): raise EquitiesNotFound( sids=set(sids) - set(symbols), plural=True, ) return symbols