Esempio n. 1
0
    def setUp(self):
        tx1 = Trade(
            uniqueid="",
            datetime=datetime(2016, 1, 1),
            fiaccount=None,
            security=1,
            units=Decimal("100"),
            cash=Decimal("-1000"),
            currency="USD",
        )
        self.lot1 = Lot(
            opentransaction=tx1,
            createtransaction=tx1,
            units=tx1.units,
            price=abs(tx1.cash / tx1.units),
            currency=tx1.currency,
        )

        tx2 = Trade(
            uniqueid="",
            datetime=datetime(2016, 1, 1),
            fiaccount=None,
            security=2,
            units=Decimal("-300"),
            cash=Decimal("3600"),
            currency="USD",
        )
        self.lot2 = Lot(
            opentransaction=tx2,
            createtransaction=tx2,
            units=tx2.units,
            price=abs(tx2.cash / tx2.units),
            currency=tx2.currency,
        )

        self.portfolio = Portfolio({
            (None, 1): [self.lot1],
            (None, 2): [self.lot2]
        })
Esempio n. 2
0
    def setUp(self):
        tx0 = Trade(
            datetime=datetime(2016, 1, 1),
            uniqueid="",
            fiaccount=None,
            security=None,
            units=Decimal("100"),
            cash=Decimal("-1000"),
            currency="USD",
        )
        self.lot0 = Lot(
            opentransaction=tx0,
            createtransaction=tx0,
            units=tx0.units,
            price=abs(tx0.cash / tx0.units),
            currency=tx0.currency,
        )

        tx1 = Trade(
            datetime=datetime(2016, 1, 3),
            uniqueid="",
            fiaccount=None,
            security=None,
            units=Decimal("300"),
            cash=Decimal("-3600"),
            currency="USD",
        )
        self.lot1 = Lot(
            opentransaction=tx1,
            createtransaction=tx1,
            units=tx1.units,
            price=abs(tx1.cash / tx1.units),
            currency=tx1.currency,
        )

        self.portfolio = Portfolio({(None, None): [self.lot0, self.lot1]})
Esempio n. 3
0
    def setUp(self):
        tx1 = Trade(
            uniqueid="",
            datetime=datetime(2016, 1, 1),
            fiaccount="",
            security="",
            units=Decimal("100"),
            cash=Decimal("1000"),
            currency="USD",
        )
        self.lot1 = Lot(
            opentransaction=tx1,
            createtransaction=tx1,
            units=tx1.units,
            price=abs(tx1.cash / tx1.units),
            currency=tx1.currency,
        )

        tx2 = Trade(
            uniqueid="",
            datetime=datetime(2016, 1, 2),
            fiaccount="",
            security="",
            units=Decimal("200"),
            cash=Decimal("2200"),
            currency="USD",
        )
        self.lot2 = Lot(
            opentransaction=tx2,
            createtransaction=tx2,
            units=tx2.units,
            price=abs(tx2.cash / tx2.units),
            currency=tx2.currency,
        )

        tx3 = Trade(
            uniqueid="",
            datetime=datetime(2016, 1, 1),
            fiaccount="",
            security="",
            units=Decimal("300"),
            cash=Decimal("3600"),
            currency="USD",
        )
        tx3c = Trade(
            uniqueid="",
            datetime=datetime(2016, 1, 3),
            fiaccount="",
            security="",
            cash=None,
            currency=None,
            units=None,
        )
        self.lot3 = Lot(
            opentransaction=tx3,
            createtransaction=tx3c,
            units=tx3.units,
            price=abs(tx3.cash / tx3.units),
            currency=tx3.currency,
        )

        self.portfolio = Portfolio({
            (None, None): [self.lot1, self.lot2, self.lot3]
        })
Esempio n. 4
0
    def _testTradeSort(self, sort, matchedTrades, partialClose):
        """
        Args:
            sort: FIFO/LIFO/MAXGAIN/MINGAIN/None/list of Trade.ids
            matchedTrades: tuple of (open index, close index, units)
            partialClose: tuple of (index, units)
        """

        # Predict the Gains that will be generated by booking the Transactions
        def matchTrades(indexopen, indexclose, units):
            opentx = self.trades[indexopen]
            closetx = self.trades[indexclose]
            lot = Lot(
                opentransaction=opentx,
                createtransaction=opentx,
                units=units,
                price=abs(opentx.cash / opentx.units),
                currency=opentx.currency,
            )
            return Gain(lot=lot,
                        transaction=closetx,
                        price=abs(closetx.cash / closetx.units))

        testGains = [
            matchTrades(*matchedTrade) for matchedTrade in matchedTrades
        ]

        # Book the trades and collect the Gains
        portfolio = Portfolio()
        gains = []
        for t in self.trades:
            g = portfolio.book(t, sort=sort)
            gains.extend(g)

        testGains.sort(key=lambda x: str(x.lot.opentransaction.uniqueid))
        gains.sort(key=lambda x: str(x.lot.opentransaction.uniqueid) + str(
            x.transaction.uniqueid))

        # Generated Gains should match prediction
        self.assertEqual(len(gains), len(testGains))

        for i, gain in enumerate(gains):
            testGain = testGains[i]
            self.assertEqual(gain.lot.opentransaction,
                             testGain.lot.opentransaction)
            self.assertEqual(gain.lot.createtransaction,
                             testGain.lot.createtransaction)
            self.assertEqual(gain.lot.units, testGain.lot.units)
            self.assertEqual(gain.lot.price, testGain.lot.price)
            self.assertEqual(gain.lot.currency, testGain.lot.currency)
            self.assertEqual(gain.transaction, testGain.transaction)

        # The rest of the trades up to the covering buys remain open
        testLots = []
        for i in range(0, 22):
            t = self.trades[i]
            testLots.append(
                Lot(
                    opentransaction=t,
                    createtransaction=t,
                    units=t.units,
                    price=abs(t.cash / t.units),
                    currency=t.currency,
                ))
        indices = list({matchedTrade[0] for matchedTrade in matchedTrades})
        indices.sort(reverse=True)
        for i in indices:
            del testLots[i]

        partialindex, partialunits = partialClose
        partial = self.trades[partialindex]
        testLots.append(
            Lot(
                opentransaction=partial,
                createtransaction=partial,
                units=partialunits,
                price=abs(partial.cash / partial.units),
                currency=partial.currency,
            ))
        testLots.sort(**FIFO)
        position = portfolio[(None, None)]
        position.sort(**FIFO)

        self.assertEqual(len(position), len(testLots))
        for i, lot in enumerate(position):
            testLot = testLots[i]
            self.assertEqual(lot, testLot)