def test_anterior_tick(self): epic = EpicFactory() last_tick = TickFactory(datetime=arrow.get("2020-01-01 12:34:56")) epic.last_tick = last_tick new_tick = TickFactory(datetime=arrow.get("2020-01-01 12:34:55")) with pytest.raises(EpicException): epic.on_new_tick(new_tick)
def test_update_frame_sets(self, mocker): epic = EpicFactory() frame_set_mock = mocker.Mock() epic.frame_sets["test"] = frame_set_mock new_tick = TickFactory() epic.on_new_tick(new_tick) assert frame_set_mock.on_new_tick.call_args_list == [call(new_tick)]
def test_update_trades(self, mocker): epic = EpicFactory() trade = TradeFactory(direction=TradeDirection.BUY, quantity=4, epic=epic) epic.trade_provider.open_trade(trade) trade_update_mock = mocker.patch.object(trade, "update_from_tick") new_tick = TickFactory() epic.on_new_tick(new_tick) assert trade_update_mock.call_args_list == [call(new_tick)]
def test_convert_tick_datetime(self): epic = EpicFactory(timezone="Asia/Tokyo") tz_NY = pytz.timezone("America/New_York") datetime_NY = datetime.now(tz_NY) tick = TickFactory(datetime=datetime_NY) epic.on_new_tick(tick) # datetime is converted to epic timezone assert epic.last_tick.datetime.tzinfo == tz.gettz(epic.timezone)
def test_update_strategies_call_on_every_tick__inactive_strategy(self, mocker): epic = EpicFactory() strategy_mock = mocker.Mock(spec=BaseStrategy) strategy_mock.is_active.return_value = False epic.strategies["test"] = strategy_mock new_tick = TickFactory() epic.on_new_tick(new_tick) assert strategy_mock.on_every_tick.call_args_list == []
def test_update_strategies_call_on_market_open__nominal(self, mocker): mocker.patch(f"{CLASS_DEFINITION_PATH}.is_market_open", return_value=True) epic = EpicFactory() epic.market_open = False strategy_mock = mocker.Mock(spec=BaseStrategy) strategy_mock.is_active.return_value = True epic.strategies["test"] = strategy_mock new_tick = TickFactory() epic.on_new_tick(new_tick) assert strategy_mock.on_market_open.call_args_list == [call(epic)]
def test_update_strategies_call_on_market_open__no_call_expected( self, mocker, market_open_before_tick, market_open_after_tick, strategy_is_active, ): mocker.patch( f"{CLASS_DEFINITION_PATH}.is_market_open", return_value=market_open_after_tick, ) epic = EpicFactory() epic.market_open = market_open_before_tick strategy_mock = mocker.Mock(spec=BaseStrategy) strategy_mock.is_active.return_value = strategy_is_active epic.strategies["test"] = strategy_mock new_tick = TickFactory() epic.on_new_tick(new_tick) assert strategy_mock.on_market_open.call_args_list == []
def test_update_last_tick(self): epic = EpicFactory() tick = TickFactory() epic.on_new_tick(tick) assert epic.last_tick == tick
def test_in_holidays(self): tick = TickFactory(datetime=arrow.get("2020-01-01 12:00:00")) epic = EpicFactory(holidays=[date(2020, 1, 1)]) epic.on_new_tick(tick) assert epic.market_open is False
def test_out_of_trade_days(self): tick = TickFactory(datetime=arrow.get("2020-01-01 12:00:00")) epic = EpicFactory(trade_days=[0, 1, 3, 4, 5]) epic.on_new_tick(tick) assert epic.market_open is False
def test_after_close(self): tick = TickFactory(datetime=arrow.get("2020-01-01 18:01:00")) epic = EpicFactory(close_time=time(18, 0)) epic.on_new_tick(tick) assert epic.market_open is False
def test_before_open(self): tick = TickFactory(datetime=arrow.get("2020-01-01 08:59:59")) epic = EpicFactory(open_time=time(9, 0)) epic.on_new_tick(tick) assert epic.market_open is False
def test_nominal(self): tick = TickFactory(datetime=arrow.get("2020-01-01 12:00:00")) epic = EpicFactory() epic.on_new_tick(tick) assert epic.market_open is True