def fetch_asset(self, sid): asset_data = self.query_engine.query_all_filtered("sid", sid, "equals") if asset_data.empty: raise SymbolNotFound(msg=f"could not find sid {sid}") return create_asset_from_dict(asset_data.iloc[0].to_dict())
def lookup_symbol(self, sym, as_of_date=None): sid = self.query_engine.query_cols_filtered("symbol", sym, "equals", "sid") if sid.empty: raise SymbolNotFound(msg=f"could not find symbol {sym}") return self.fetch_asset(sid.iloc[0])
def fetch_asset(self, sid): ''' Implementation of the interface. The plural version will remains same as the parent class. For an SID different than -1, it implies the asset is in our database and a search is done using the underlying asset finder. ''' if self._asset_finder is not None: return self._asset_finder.fetch_asset(sid) else: raise SymbolNotFound(msg=f"could not find sid {sid}")
def asset_to_id(self, asset): ''' Given an asset retrieve the instrument id. Instrument ID is required for placing trades or querying hisotrical data. ''' row = self._instruments_list[self._instruments_list.\ tradingsymbol==asset.symbol] if row.empty: raise SymbolNotFound(msg=f"no id found for {asset.symbol}") row = row.iloc[0].to_dict() return row['instrument_token']
def symbol_to_asset(self, tradingsymbol, as_of_date=None): ''' Asset finder that first looks at the provided asset finder. If no match found, it searches the current list of instruments and creates an asset. This asset can only be used for sending orders and fetching data from the broker directly, not from our database. If no match found even in instrument list, returns None. The symbol stored, if not matched in our databse, is always the tradeable symbol. ''' if self._asset_finder is not None: try: asset = self._asset_finder.lookup_symbol( tradingsymbol, as_of_date) # we got a match in our databse, return early return asset except SymbolNotFound: pass sym = tradingsymbol.split(":")[-1] bases = sym.split("-I") if bases[0] != sym: if len(bases) > 1 and bases[-1] in ['', 'I', 'II']: exp = bases[-1] + 'I' row = self._instruments_list.loc[ (self._instruments_list.underlying==bases[0]) &\ (self._instruments_list.exp == exp) &\ (self._instruments_list.instrument_type=='FUT'),] else: row = self._instruments_list.loc[ self._instruments_list.tradingsymbol == sym, ] else: row = self._instruments_list.loc[ self._instruments_list.tradingsymbol == sym, ] if row.empty: # no match found. Refuse to trade the symbol # default handling is to log raise SymbolNotFound(msg=tradingsymbol) row = row.iloc[0].to_dict() return self._asset_from_row(row)
def id_to_asset(self, instrument_id): ''' create an asset from the instrument id. First extract the matching row and search for the asset in our own database. If no match found, create an asset and return. ''' row = self._instruments_list[self._instruments_list.\ instrument_token==instrument_id] if row.empty: raise SymbolNotFound(msg=f"no asset found for {instrument_id}") row = row.iloc[0].to_dict() if self._asset_finder is not None: try: asset = self._asset_finder.lookup_symbol(row['tradingsymbol']) return asset except SymbolNotFound: pass return self._asset_from_row(row)
def _asset_from_sym(self, sym): ''' Create an asset from a matching entry in the instruments list. ''' # pylint: disable=no-self-use # TODO: replace this by create_asset_from_dict from _assets # all instrument types are Forex. sym = sym.split(":")[0] if sym not in self._instruments_list: raise SymbolNotFound(f"no instruments with symbol {sym}") base, quote = tuple(sym.split('/')) if not base or not quote: raise ValidationError(f"Invalid symbol {sym} for Forex.") # TODO: hardcoded assumptions here on ticksize, generalize this. # NOTE: actual tick size is the inverse of this number!! if quote == 'JPY': tick_size = 100 else: tick_size = 10000 asset = Forex( -1, symbol=sym, ccy_pair=base + '/' + quote, base_ccy=base, quote_ccy=quote, name=sym, mult=1000, # the micro-lot tick_size=tick_size, ccy=quote, exchange_name='FXCM') return asset
def lookup_symbol(self, sym, as_of_date=None): raise SymbolNotFound(msg=f"could not find symbol {sym}")
def fetch_asset(self, sid): raise SymbolNotFound(msg=f"could not find sid {sid}")