Beispiel #1
0
    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)
Beispiel #2
0
        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)
Beispiel #3
0
    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()]
Beispiel #4
0
    def test_add_to_strategy(self):
        trade_provider = TradeProviderFactory()
        strategy = StrategyFactory()
        trade = TradeFactory(strategy=strategy)
        trade_provider.open_trade(trade)

        assert strategy.trades == [trade]
Beispiel #5
0
    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")
Beispiel #6
0
    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",
        }
Beispiel #7
0
        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
Beispiel #8
0
        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
Beispiel #9
0
        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"
Beispiel #10
0
    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
Beispiel #11
0
    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
Beispiel #12
0
        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
Beispiel #13
0
        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"
Beispiel #14
0
        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
Beispiel #15
0
    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
Beispiel #16
0
        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"
Beispiel #17
0
        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"
Beispiel #18
0
        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"
Beispiel #19
0
        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"]
Beispiel #20
0
    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
Beispiel #21
0
        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"
Beispiel #22
0
        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)]
Beispiel #23
0
        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)
Beispiel #24
0
    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)]
Beispiel #25
0
        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
Beispiel #26
0
    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)
Beispiel #27
0
    def test_open_thread(self, mock_init_thread):
        trade_provider = TradeProviderFactory()
        trade = TradeFactory()
        trade_provider.open_trade(trade)

        assert mock_init_thread.call_args_list == [
            call(
                target=trade_provider.open_trade_request,
                args=(),
                kwargs={"trade": trade},
            )
        ]
Beispiel #28
0
    def test_nominal(self, mocker):
        mocker.patch(
            f"{CLASS_TRADE_DEFINITION_PATH}.opened_result",
            new_callable=PropertyMock(return_value=12.56),
        )
        mocker.patch(
            f"{CLASS_TRADE_DEFINITION_PATH}.closed_result",
            new_callable=PropertyMock(return_value=-45.82),
        )

        trade = TradeFactory()
        assert trade.result == 12.56 - 45.82
Beispiel #29
0
    def test_nominal(self, mocker):
        mocker.patch(
            f"{CLASS_TRADE_DEFINITION_PATH}.opened_result_avg",
            new_callable=PropertyMock(return_value=87.36),
        )
        mocker.patch(
            f"{CLASS_TRADE_DEFINITION_PATH}.opened_quantities",
            new_callable=PropertyMock(return_value=6),
        )

        trade = TradeFactory()

        assert trade.opened_result == 524.16
Beispiel #30
0
    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)