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_close_value(self, mock_trade_close_init): trade = TradeFactory(quantity=2) trade.close(close_value=100, datetime=arrow.utcnow()) assert mock_trade_close_init.call_count == 1 assert mock_trade_close_init.call_args_list[0][1][ "close_value"] == 100
def test_datetime(self, mock_trade_close_init): trade = TradeFactory() trade.close(close_value=100, datetime="test") assert mock_trade_close_init.call_count == 1 assert mock_trade_close_init.call_args_list[0][1][ "datetime"] == "test"
def test_close_value(self, mock_trade_close): tick = TickFactory() trade = TradeFactory() trade.current_close_value = 87.9 trade.close_from_tick(tick) assert mock_trade_close.call_count == 1 assert mock_trade_close.call_args_list[0][1]["close_value"] == 87.9
def test_datetime(self, mock_trade_close): dt = arrow.get("2020-01-01 12:34:56") tick = TickFactory(datetime=dt) trade = TradeFactory() trade.close_from_tick(tick) assert mock_trade_close.call_count == 1 assert mock_trade_close.call_args_list[0][1]["datetime"] == dt
def test_kwargs(self, mock_trade_close): tick = TickFactory() trade = TradeFactory() trade.close_from_tick(tick, test_arg="test_value") assert mock_trade_close.call_count == 1 assert mock_trade_close.call_args_list[0][1][ "test_arg"] == "test_value"
def test_update_min_result(self, current_min_result, expected_min_result): trade = TradeFactory() trade.min_result = current_min_result trade._update_min_max() assert trade.min_result == expected_min_result
def test_return_close(self, mocker): mocker.patch(f"{CLASS_TRADE_CLOSE_DEFINITION_PATH}", return_value="new_close") trade = TradeFactory() response = trade.close(close_value=100, datetime="test") assert response == "new_close"
def test_append_to_closes(self, mocker): mocker.patch(f"{CLASS_TRADE_CLOSE_DEFINITION_PATH}", return_value="new_close") trade = TradeFactory() trade.close(close_value=100, datetime="test") assert trade.closes == ["new_close"]
def test_response(self, mocker): mocker.patch(f"{CLASS_TRADE_DEFINITION_PATH}.close", return_value="test_close") tick = TickFactory() trade = TradeFactory() response = trade.close_from_tick(tick) assert response == "test_close"
def test_some_closes(self, mocker): trade = TradeFactory() close_mock1 = mocker.Mock(spec=TradeClose) close_mock1.quantity = 3 close_mock2 = mocker.Mock(spec=TradeClose) close_mock2.quantity = 2 trade.closes = [close_mock2, close_mock1] assert trade.closed_quantities == 5
def test_kwargs(self, mock_trade_close_init): trade = TradeFactory(quantity=2) trade.close(close_value=100, datetime=arrow.utcnow(), random_arg="test") assert mock_trade_close_init.call_args_list[0][1][ "random_arg"] == "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_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_nominal(self, mocker): trade = TradeFactory() close_mock1 = mocker.Mock(spec=TradeClose) close_mock1.result = 65.89 close_mock2 = mocker.Mock(spec=TradeClose) close_mock2.result = -34.91 trade.closes = [close_mock2, close_mock1] assert trade.closed_result == (65.89 - 34.91)
def test_quantities(self, mock_trade_close_init, quantity, expected_quantity): trade = TradeFactory(quantity=5) trade.close(close_value=100, datetime=arrow.utcnow(), quantity=quantity) assert mock_trade_close_init.call_count == 1 assert (mock_trade_close_init.call_args_list[0][1]["quantity"] == expected_quantity)
def test_closed(self, mocker, mock_update_mix_max): # mock property closed of Trade closed_mock = PropertyMock(return_value=True) mocker.patch(f"{CLASS_TRADE_DEFINITION_PATH}.closed", new_callable=closed_mock) trade = TradeFactory() trade.update(34.5) assert mock_update_mix_max.not_called
def test_nominal(self, mocker): mocker.patch( f"{CLASS_TRADE_DEFINITION_PATH}.closed_quantities", new_callable=PropertyMock(return_value=3), ) trade = TradeFactory() close_mock1 = mocker.Mock(spec=TradeClose) close_mock1.result = 65.89 close_mock2 = mocker.Mock(spec=TradeClose) close_mock2.result = -34.91 trade.closes = [close_mock2, close_mock1] assert trade.closed_result_avg == round((65.89 - 34.91) / 3, 2)
def test_ref_init_called__manual(self, mocker): ref_init = mocker.patch.object(RefMixin, "__init__", wraps=RefMixin.__init__) trade = TradeFactory(ref="test") ref_init.assert_called_once_with(trade, "test")
def test_start_thread(self, mock_init_thread): mock_thread_start = mock_init_thread.return_value.start trade_provider = TradeProviderFactory() trade = TradeFactory() trade_provider.open_trade(trade) assert mock_thread_start.call_args_list == [call()]
def test_ref_init_called__default(self, mocker): ref_init = mocker.patch.object(RefMixin, "__init__", wraps=RefMixin.__init__) trade = TradeFactory() ref_init.assert_called_once_with(trade, None)
def test_add_to_strategy(self): trade_provider = TradeProviderFactory() strategy = StrategyFactory() trade = TradeFactory(strategy=strategy) trade_provider.open_trade(trade) assert strategy.trades == [trade]
def test_nominal(self, mock_update, direction, expected_current): trade: Trade = TradeFactory(direction=direction) tick = TickFactory(bid=99, ask=100) trade.update_from_tick(tick) mock_update.assert_called_once_with(expected_current)
def test_nominal(self, mocker): mocker.patch( f"{CLASS_TRADE_DEFINITION_PATH}.result", new_callable=PropertyMock(return_value=82.767), ) trade = TradeFactory(quantity=4) assert trade.result_avg == 20.69
def test_kwargs(self, mock_init_trade): default_trade_args, _ = TradeFactory.get_default_args() Trade.open_from_tick(tick=TickFactory(), ref="test", **default_trade_args) assert mock_init_trade.call_count == 1 assert mock_init_trade.call_args_list[0][1]["ref"] == "test"
def test_nominal(self, mocker, opened_quantities, expected_close): mocker.patch( f"{CLASS_TRADE_DEFINITION_PATH}.opened_quantities", new_callable=PropertyMock(return_value=opened_quantities), ) trade = TradeFactory() assert trade.closed == expected_close
def test_epic(self, mock_init_trade): tick = TickFactory() default_trade_args, _ = TradeFactory.get_default_args() Trade.open_from_tick(tick=tick, **default_trade_args) assert mock_init_trade.call_count == 1 assert (mock_init_trade.call_args_list[0][1]["epic"] == default_trade_args["epic"])
def test_closed(self, mocker): mocker.patch( f"{CLASS_TRADE_DEFINITION_PATH}.closed", new_callable=PropertyMock(return_value=True), ) trade = TradeFactory() assert trade.closed_result == 0
def test_open_datetime(self, mock_init_trade): tick_datetime = arrow.get("2020-01-01 12:34:56") tick = TickFactory(datetime=tick_datetime) default_trade_args, _ = TradeFactory.get_default_args() Trade.open_from_tick(tick=tick, **default_trade_args) assert mock_init_trade.call_count == 1 assert (mock_init_trade.call_args_list[0][1]["open_datetime"] == tick_datetime)
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) ]