示例#1
0
    def __add__(self, other):
        """Adding positions should give you the net position"""
        assert isinstance(other, CashPosition)
        assert self.instrument == other.instrument

        # collect histories
        notional_history = _merge(self.__notional_history,
                                  other.__notional_history)

        ret = CashPosition(
            notional_history[-1][0],
            self.instrument,
            self.exchange  # FIXME
        )

        ret.__notional_history = notional_history
        return ret
示例#2
0
    def __add__(self, other: object) -> "Position":
        """Adding positions should give you the net position"""
        assert isinstance(other, Position)
        assert self.instrument == other.instrument

        # collect histories
        size_history = _merge(self.__size_history, other.__size_history)
        notional_history = _merge(self.__notional_history,
                                  other.__notional_history)
        price_history = _merge(self.__price_history, other.__price_history,
                               False)
        investment_history = _merge(self.__investment_history,
                                    other.__investment_history)
        instrumentPrice_history = _merge(self.__instrumentPrice_history,
                                         other.__instrumentPrice_history,
                                         False)
        pnl_history = _merge(self.__pnl_history, other.__pnl_history)
        unrealizedPnl_history = _merge(self.__unrealizedPnl_history,
                                       other.__unrealizedPnl_history)

        ret = Position(
            size_history[-1][0],
            price_history[-1][0],
            size_history[-1][1],
            self.instrument,
            self.exchange,  # FIXME
            self.trades + other.trades,
        )

        ret.__size_history = size_history
        ret.__notional_history = notional_history
        ret.__price_history = price_history
        ret.__investment_history = investment_history
        ret.__instrumentPrice_history = instrumentPrice_history
        ret.__pnl_history = pnl_history
        ret.__unrealizedPnl_history = unrealizedPnl_history

        return ret
示例#3
0
    def test_merge(self):
        from aat.common import _merge

        l1 = [
            (1, datetime(2020, 1, 1)),
            (1, datetime(2020, 1, 2)),
            (2, datetime(2020, 1, 3)),
            (2, datetime(2020, 1, 4)),
            (3, datetime(2020, 1, 5)),
            (3, datetime(2020, 1, 6)),
            (4, datetime(2020, 1, 7)),
            (4, datetime(2020, 1, 8)),
            (3, datetime(2020, 1, 9)),
            (3, datetime(2020, 1, 20)),
            (2, datetime(2020, 1, 21)),
            (2, datetime(2020, 1, 22)),
            (1, datetime(2020, 1, 23)),
            (1, datetime(2020, 1, 24)),
            (0, datetime(2020, 1, 25)),
            (0, datetime(2020, 1, 26)),
            (-1, datetime(2020, 1, 27)),
            (-1, datetime(2020, 1, 28)),
            (0, datetime(2020, 1, 29)),
            (0, datetime(2020, 1, 30)),
        ]

        l2 = [
            (1, datetime(2020, 1, 5)),
            (1, datetime(2020, 1, 6)),
            (1, datetime(2020, 1, 7)),
            (1, datetime(2020, 1, 8)),
            (-2, datetime(2020, 1, 9)),
            (-2, datetime(2020, 1, 10)),
            (-2, datetime(2020, 1, 11)),
            (-2, datetime(2020, 1, 12)),
            (-2, datetime(2020, 1, 13)),
            (-2, datetime(2020, 1, 14)),
            (2, datetime(2020, 1, 22)),
            (2, datetime(2020, 1, 23)),
            (2, datetime(2020, 1, 24)),
            (2, datetime(2020, 1, 25)),
            (2, datetime(2020, 1, 26)),
            (1, datetime(2020, 1, 29)),
            (1, datetime(2020, 1, 30)),
        ]

        combined = [
            (1, datetime(2020, 1, 1)),
            (1, datetime(2020, 1, 2)),
            (2, datetime(2020, 1, 3)),
            (2, datetime(2020, 1, 4)),
            (4, datetime(2020, 1, 5)),
            (4, datetime(2020, 1, 6)),
            (5, datetime(2020, 1, 7)),
            (5, datetime(2020, 1, 8)),
            (1, datetime(2020, 1, 9)),
            (1, datetime(2020, 1, 10)),
            (1, datetime(2020, 1, 11)),
            (1, datetime(2020, 1, 12)),
            (1, datetime(2020, 1, 13)),
            (1, datetime(2020, 1, 14)),
            (1, datetime(2020, 1, 20)),
            (0, datetime(2020, 1, 21)),
            (4, datetime(2020, 1, 22)),
            (3, datetime(2020, 1, 23)),
            (3, datetime(2020, 1, 24)),
            (2, datetime(2020, 1, 25)),
            (2, datetime(2020, 1, 26)),
            (1, datetime(2020, 1, 27)),
            (1, datetime(2020, 1, 28)),
            (1, datetime(2020, 1, 29)),
            (1, datetime(2020, 1, 30)),
        ]

        ret = _merge(l1, l2)

        assert ret == combined