Path: blob/master/ invest-robot-contest_tinkoff-contest-python-main/src/traders/base.py
5935 views
import abc1import importlib.util2import inspect3import sys4from typing import List, Type56from src.containers.config import TraderConfig7from src.containers.market import MarketState, TraderDecision8910class BaseTrader(metaclass=abc.ABCMeta):11_trader_name: str = None1213@classmethod14@property15def name(cls):16return cls._trader_name1718def __init__(self, trader_config: TraderConfig):19self.trader_config = trader_config2021def initial_message(self):22return f"Are you sure you want to start the {self.name} trader? (yes/no)"2324@abc.abstractmethod25async def make_decisions(self, state: MarketState) -> List[TraderDecision]:26raise NotImplementedError272829def load_trader_class(file_name) -> Type[BaseTrader]:30# find and load the file as a python module31module_name = f"traders.{file_name}"32spec = importlib.util.spec_from_file_location(module_name, f"src/traders/{file_name}.py")33module = importlib.util.module_from_spec(spec)34sys.modules[module_name] = module35spec.loader.exec_module(module)3637# find the trader class in the module38module_classes = inspect.getmembers(module, inspect.isclass)39trader_cls = next(40class_41for _, class_ in module_classes42if hasattr(class_, "_trader_name") and class_ is not BaseTrader and issubclass(class_, BaseTrader)43)44return trader_cls454647