def test_epic_market_open(self, market_open): fs = FrameSetFactory() epic = EpicFactory() epic.market_open = market_open fs.epic = epic assert fs.epic_market_open == market_open
def test_response(self, mocker): trade_provider_mock = mocker.Mock() epic = EpicFactory(trade_provider=trade_provider_mock) response = epic.open_trade(test="test") assert response == "my_new_trade"
def test_add_to_frame_sets(self, mocker): frame_set_mock = mocker.Mock() frame_set_mock.ref = "fs_ref" epic = EpicFactory() epic.add_frame_set(frame_set_mock) assert epic.frame_sets["fs_ref"] == frame_set_mock
def test_call_trade_provider(self, mocker): trade_provider_mock = mocker.Mock() epic = EpicFactory(trade_provider=trade_provider_mock) epic.open_trade(test="test") assert trade_provider_mock.open_trade.call_args_list == [call("my_new_trade")]
def test_nominal(self, mocker): mocker.patch( f"{CLASS_DEFINITION_PATH}.get_frame", return_value={"my_indicator": "indicator_value"}, ) epic = EpicFactory() assert epic.get_indicator_value("my_fs", "my_indicator") == "indicator_value"
def test_update_frameset_epic(self, mocker): frame_set_mock = mocker.Mock() frame_set_mock.ref = "fs_ref" epic = EpicFactory() epic.add_frame_set(frame_set_mock) assert frame_set_mock.epic == epic
def test_update_strategy(self, mocker): strategy_mock = mocker.Mock() strategy_mock.ref = "strategy_ref" strategy_mock.epics = {} epic = EpicFactory() epic.add_strategy(strategy_mock) assert strategy_mock.epics[epic.ref] == epic
def test_get_frame_call(self, mocker): get_frame_mock = mocker.patch( f"{CLASS_DEFINITION_PATH}.get_frame", return_value=None ) epic = EpicFactory() epic.get_indicator_value("my_fs", "my_indicator", 55) assert get_frame_mock.call_args_list == [call(frame_set_ref="my_fs", offset=55)]
def test_add_existing_ref(self, mocker): epic = EpicFactory() epic.frame_sets = {"MY_REF": "my_frame_set"} frame_set_mock = mocker.Mock() frame_set_mock.ref = "MY_REF" with pytest.raises(EpicException): epic.add_frame_set(frame_set_mock)
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_add_existing_ref(self, mocker): epic = EpicFactory() epic.strategies = {"MY_REF": "my_strategy"} strategy_mock = mocker.Mock() strategy_mock.ref = "MY_REF" with pytest.raises(EpicException): epic.add_strategy(strategy_mock)
def test_add_strategy(self, mocker): strategy_mock = mocker.Mock() strategy_mock.ref = "strategy_ref" strategy_mock.epics = {} epic = EpicFactory() epic.add_strategy(strategy_mock) assert epic.strategies["strategy_ref"] == strategy_mock
def test_call_open_trade(self, mocker, trade_open_from_epic_mock): trade_provider_mock = mocker.Mock() epic = EpicFactory(trade_provider=trade_provider_mock) epic.open_trade(test="test") assert trade_open_from_epic_mock.call_args_list == [ call(epic=epic, test="test") ]
def test_call_open_trade(self, mocker): trade_provider_mock = mocker.Mock() trade_mock = mocker.Mock() trade_mock.close_from_epic.return_value = "my_trade_close" epic = EpicFactory(trade_provider=trade_provider_mock) epic.close_trade(trade=trade_mock, test="test") assert trade_mock.close_from_epic.call_args_list == [call(test="test")]
def test_call_close_from_tick(self, mock_close_from_tick): epic = EpicFactory() trade = TradeFactory(epic=epic) last_tick = TickFactory() epic.last_tick = last_tick trade.close_from_epic() assert mock_close_from_tick.call_args_list == [call(last_tick)]
def test_response(self, mocker): trade_provider_mock = mocker.Mock() trade_mock = mocker.Mock() trade_mock.close_from_epic.return_value = "my_trade_close" epic = EpicFactory(trade_provider=trade_provider_mock) response = epic.close_trade(trade=trade_mock, test="test") assert response == "my_trade_close"
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_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_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_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_nominal(self): epic = EpicFactory(ref="MY_EPIC_REF") strategy = StrategyFactory(ref="MY_STRATEGY") trade = TradeFactory( ref="MY_TRADE", epic=epic, direction=TradeDirection.BUY, open_datetime=arrow.get("2020-01-01 12:34:56"), quantity=5, status=TransactionStatus.REFUSED, strategy=strategy, ) assert trade.asdict() == { "closed_quantities": 0, "direction": TradeDirection.BUY, "epic": "MY_EPIC_REF", "open_date": "2020-01-01 12:34:56", "open_quantity": 5, "open_value": 101, "ref": "MY_TRADE", "result": -10, "status": TransactionStatus.REFUSED, "strategy": "MY_STRATEGY", }
def test_return(self, mocker): mocker.patch( f"{CLASS_TRADE_DEFINITION_PATH}.close_from_tick", return_value="test_close", ) trade = TradeFactory(epic=EpicFactory()) result = trade.close_from_epic() assert result == "test_close"
def test_valid_epic(self, mock_trade_open_from_tick): epic = EpicFactory() default_trade_args, _ = TradeFactory.get_default_args() default_trade_args["epic"] = epic Trade.open_from_epic(**default_trade_args) assert mock_trade_open_from_tick.call_args_list == [ call(tick=epic.last_tick, **default_trade_args) ]
class TradeFactory(Factory): class Meta: model = Trade epic = EpicFactory() direction = TradeDirection.BUY quantity = 1 open_value = 101 current_close_value = 99 def open_datetime(): return arrow.utcnow()
def test_trade_provider__custom(self): class MyCustomTradeProvider(BaseTradeProvider): def open_trade_request(self, trade): pass def close_trade_request(self, trade_close): pass custom_trade_provider = MyCustomTradeProvider(ref="MY_PROVIDER") epic = EpicFactory(trade_provider=custom_trade_provider) assert isinstance(epic.trade_provider, MyCustomTradeProvider)
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_epic(self): epic = EpicFactory() tick_provider = TickProviderFactory(epics=[epic]) assert tick_provider.epics == {epic.ref: epic}
def test_nominal(self): epic1 = EpicFactory() epic2 = EpicFactory() tick_provider = TickProviderFactory(epics=[epic1, epic2]) assert tick_provider.get_epic_by_ref(epic2.ref) == epic2
class TickProviderFactory(Factory): class Meta: model = BaseTickProvider epics = [EpicFactory()]