Add prelim fqsn support into our `Symbol` type

fqsns
Tyler Goodlet 2022-03-18 10:59:57 -04:00
parent 434c340cb8
commit b16167b8f3
1 changed files with 63 additions and 6 deletions

View File

@ -92,6 +92,28 @@ def ohlc_zeros(length: int) -> np.ndarray:
return np.zeros(length, dtype=base_ohlc_dtype) return np.zeros(length, dtype=base_ohlc_dtype)
def uncons_fqsn(fqsn: str) -> tuple[str, str, str]:
'''
Unpack a fully-qualified-symbol-name to ``tuple``.
'''
# TODO: probably reverse the order of all this XD
tokens = fqsn.split('.')
if len(tokens) > 3:
symbol, venue, suffix, broker = tokens
else:
symbol, venue, broker = tokens
suffix = ''
# head, _, broker = fqsn.rpartition('.')
# symbol, _, suffix = head.rpartition('.')
return (
broker,
'.'.join([symbol, venue]),
suffix,
)
class Symbol(BaseModel): class Symbol(BaseModel):
"""I guess this is some kinda container thing for dealing with """I guess this is some kinda container thing for dealing with
all the different meta-data formats from brokers? all the different meta-data formats from brokers?
@ -103,6 +125,7 @@ class Symbol(BaseModel):
lot_tick_size: float = 0.0 # "volume" precision as min step value lot_tick_size: float = 0.0 # "volume" precision as min step value
tick_size_digits: int = 2 tick_size_digits: int = 2
lot_size_digits: int = 0 lot_size_digits: int = 0
suffix: str = ''
broker_info: dict[str, dict[str, Any]] = {} broker_info: dict[str, dict[str, Any]] = {}
# specifies a "class" of financial instrument # specifies a "class" of financial instrument
@ -115,6 +138,7 @@ class Symbol(BaseModel):
broker: str, broker: str,
symbol: str, symbol: str,
info: dict[str, Any], info: dict[str, Any],
suffix: str = '',
# XXX: like wtf.. # XXX: like wtf..
# ) -> 'Symbol': # ) -> 'Symbol':
@ -129,9 +153,27 @@ class Symbol(BaseModel):
lot_tick_size=lot_tick_size, lot_tick_size=lot_tick_size,
tick_size_digits=float_digits(tick_size), tick_size_digits=float_digits(tick_size),
lot_size_digits=float_digits(lot_tick_size), lot_size_digits=float_digits(lot_tick_size),
suffix=suffix,
broker_info={broker: info}, broker_info={broker: info},
) )
@classmethod
def from_fqsn(
cls,
fqsn: str,
info: dict[str, Any],
# XXX: like wtf..
# ) -> 'Symbol':
) -> None:
broker, key, suffix = uncons_fqsn(fqsn)
return cls.from_broker_info(
broker,
key,
info=info,
suffix=suffix,
)
@property @property
def type_key(self) -> str: def type_key(self) -> str:
return list(self.broker_info.values())[0]['asset_type'] return list(self.broker_info.values())[0]['asset_type']
@ -141,9 +183,10 @@ class Symbol(BaseModel):
return list(self.broker_info.keys()) return list(self.broker_info.keys())
def nearest_tick(self, value: float) -> float: def nearest_tick(self, value: float) -> float:
"""Return the nearest tick value based on mininum increment. '''
Return the nearest tick value based on mininum increment.
""" '''
mult = 1 / self.tick_size mult = 1 / self.tick_size
return round(value * mult) / mult return round(value * mult) / mult
@ -159,11 +202,25 @@ class Symbol(BaseModel):
self.key, self.key,
) )
def front_fqsn(self) -> str:
broker, key = self.front_feed()
if self.suffix:
tokens = (key, self.suffix, broker)
else:
tokens = (key, broker)
fqsn = '.'.join(tokens)
return fqsn
def iterfqsns(self) -> list[str]: def iterfqsns(self) -> list[str]:
return [ keys = []
mk_fqsn(self.key, broker) for broker in self.broker_info.keys():
for broker in self.broker_info.keys() fqsn = mk_fqsn(self.key, broker)
] if self.suffix:
fqsn += f'.{self.suffix}'
keys.append(fqsn)
return keys
def from_df( def from_df(