def test_context_load_statistic_error() -> None: settings = Setting() settings.STATISTIC = MagicMock # type:ignore context = Context(settings) with pytest.raises(SettingError): context.load_statistic()
def test_context_load_strategy_error() -> None: settings = Setting() settings.STRATEGY = MagicMock() # type:ignore context = Context(settings) with pytest.raises(SettingError): context.load_strategy()
def test_context_load_default() -> None: with patch("monkq.exchange.bitmex.exchange.BitmexDataloader"): settings = Setting() context = Context(settings) context.setup_context() assert isinstance(context.strategy, BaseStrategy) assert isinstance(context.trade_counter, TradeCounter) assert isinstance(context.stat, Statistic) assert isinstance(context.accounts['bitmex_account'], FutureAccount) assert isinstance(context.exchanges['bitmex'], BitmexSimulateExchange)
def __init__(self, settings: Setting) -> None: self.setting = settings self.context = Context(settings) self.context.setup_context() self.start_datetime = settings.START_TIME # type: ignore self.end_datetime = settings.END_TIME # type: ignore self.ticker = FrequencyTicker(self.start_datetime, self.end_datetime, '1m') self.stat = self.context.stat
def test_context_custom_setting() -> None: with patch("monkq.exchange.bitmex.exchange.BitmexDataloader"): settings = Setting() settings.STRATEGY = TestStrategy # type:ignore settings.TRADE_COUNTER = TestTradeCounter # type:ignore settings.STATISTIC = TestStatistic # type:ignore settings.ACCOUNTS[0]['ACCOUNT_MODEL'] = TestAccount # type:ignore settings.EXCHANGES['bitmex']['ENGINE'] = TestExchange # type:ignore context = Context(settings) context.setup_context() assert isinstance(context.strategy, TestStrategy) assert isinstance(context.trade_counter, TestTradeCounter) assert isinstance(context.stat, TestStatistic) assert isinstance(context.accounts, dict) assert isinstance(context.exchanges, dict)
class Runner(): def __init__(self, settings: Setting) -> None: self.setting = settings self.context = Context(settings) self.context.setup_context() self.start_datetime = settings.START_TIME # type: ignore self.end_datetime = settings.END_TIME # type: ignore self.ticker = FrequencyTicker(self.start_datetime, self.end_datetime, '1m') self.stat = self.context.stat async def _run(self) -> None: await self.context.strategy.setup() self.stat.freq_collect_account() for current_time in self.ticker.timer(): self.context.now = current_time logger.debug("Handler time {}".format(current_time)) await self.context.strategy.handle_bar() logger.debug("Finish handle bar to {}".format(current_time)) for key, exchange in self.context.exchanges.items(): exchange.match_open_orders() # type:ignore self.stat.freq_collect_account() self.stat.collect_account_info() self.lastly() def lastly(self) -> None: self.stat.report() def run(self) -> None: loop = get_event_loop() loop.run_until_complete(self._run())
def test_context_load_trade_counter_error() -> None: settings = Setting() settings.TRADE_COUNTER = MagicMock() # type:ignore context = Context(settings) context.load_statistic() with pytest.raises(SettingError): context.load_trade_counter()
def exchange(settings: Setting) -> Generator[BaseSimExchange, None, None]: context = Context(settings) exchange = BaseSimExchange(context, 'exchange_test', {}) yield exchange