from dataclasses import dataclass DEFAULT_SYMBOL = "/ES" @dataclass(frozen=True) class Instrument: id: str yahoo_symbol: str schwab_symbol: str tick: float decimals: int = 2 session: str = "globex_18_17" rth: str = "spy_rth" def snap(self, price: float) -> float: return round(round(price / self.tick) * self.tick, self.decimals) def payload(self) -> dict: return { "id": self.id, "yahoo_symbol": self.yahoo_symbol, "schwab_symbol": self.schwab_symbol, "tick": self.tick, "decimals": self.decimals, "session": self.session, "rth": self.rth, } INSTRUMENTS = { "es": Instrument("es", "ES=F", "/ES", 0.25, rth="spy_rth"), "nq": Instrument("nq", "NQ=F", "/NQ", 0.25, rth="spy_rth"), "gc": Instrument("gc", "GC=F", "/GC", 0.10, rth="none"), "cl": Instrument("cl", "CL=F", "/CL", 0.01, rth="nymex_day"), } def get_instrument(instrument_id: str) -> Instrument: try: return INSTRUMENTS[instrument_id] except KeyError: raise ValueError(f"Unknown instrument: {instrument_id}") from None def instrument_for_symbol(symbol: str) -> Instrument: for instrument in INSTRUMENTS.values(): if symbol in (instrument.id, instrument.schwab_symbol, instrument.yahoo_symbol): return instrument return INSTRUMENTS["es"]