Ejemplo n.º 1
0
    def test_calculate_pnl_for_inverse2(self):
        # Arrange
        order = self.order_factory.market(
            ETHUSD_BITMEX.symbol,
            OrderSide.SELL,
            Quantity(100000),
        )

        fill = TestStubs.event_order_filled(
            order,
            instrument=ETHUSD_BITMEX,
            position_id=PositionId("P-123456"),
            strategy_id=StrategyId("S", "001"),
            fill_price=Price("375.95"),
        )

        position = Position(fill)

        # Act
        pnl = position.calculate_pnl(
            Price("375.95"),
            Price("365.50"),
            Quantity(100000),
            # xrate=Decimal("0.0294337")  # Currently not handling quanto settlement
        )

        # Assert
        self.assertEqual(Money(1582.66, USD), position.unrealized_pnl(Price("370.00")))
        self.assertEqual(Money(100000.00, USD), position.notional_value(Price("370.00")))
Ejemplo n.º 2
0
    def test_calculate_pnl_for_short_position_loss(self):
        # Arrange
        order = self.order_factory.market(
            BTCUSDT_BINANCE.symbol,
            OrderSide.SELL,
            Quantity("10"),
        )

        fill = TestStubs.event_order_filled(
            order,
            instrument=BTCUSDT_BINANCE,
            position_id=PositionId("P-123456"),
            strategy_id=StrategyId("S", "001"),
            fill_price=Price("10500.00"),
        )

        position = Position(fill)

        # Act
        pnl = position.calculate_pnl(
            Price("10500.00"),
            Price("10670.50"),
            Quantity("10.000000"),
        )

        # Assert
        self.assertEqual(Money("-1705.00000000", USDT), pnl)
        self.assertEqual(Money("-1705.00000000", USDT), position.unrealized_pnl(Price("10670.50")))
        self.assertEqual(Money("-105.00000000", USDT), position.realized_pnl)
        self.assertEqual([Money("105.00000000", USDT)], position.commissions())
        self.assertEqual(Money("105.00000000", USDT), position.commission)
        self.assertEqual(Money("106705.00000000", USDT), position.notional_value(Price("10670.50")))
Ejemplo n.º 3
0
    def test_calculate_pnl_for_inverse1(self):
        # Arrange
        order = self.order_factory.market(
            XBTUSD_BITMEX.symbol,
            OrderSide.SELL,
            Quantity(100000),
        )

        fill = TestStubs.event_order_filled(
            order,
            instrument=XBTUSD_BITMEX,
            position_id=PositionId("P-123456"),
            strategy_id=StrategyId("S", "001"),
            fill_price=Price("10000.00"),
        )

        position = Position(fill)

        # Act
        pnl = position.calculate_pnl(
            Price("10000.00"),
            Price("11000.00"),
            Quantity(100000),
        )

        # Assert
        self.assertEqual(Money(-10000.00, USD), pnl)
        self.assertEqual(Money(-10000.00, USD), position.unrealized_pnl(Price("11000.00")))
        self.assertEqual(Money(0.00, USD), position.realized_pnl)
        self.assertEqual(Money(0.00, USD), position.commission)
        self.assertEqual(Money(100000.00, USD), position.notional_value(Price("11000.00")))
Ejemplo n.º 4
0
    def test_calculate_pnl_for_inverse1(self):
        # Arrange
        order = self.order_factory.market(
            XBTUSD_BITMEX.id,
            OrderSide.SELL,
            Quantity.from_int(100000),
        )

        fill = TestStubs.event_order_filled(
            order,
            instrument=XBTUSD_BITMEX,
            position_id=PositionId("P-123456"),
            strategy_id=StrategyId("S-001"),
            last_px=Price.from_str("10000.00"),
        )

        position = Position(instrument=XBTUSD_BITMEX, fill=fill)

        # Act
        pnl = position.calculate_pnl(
            avg_px_open=Price.from_str("10000.00"),
            avg_px_close=Price.from_str("11000.00"),
            quantity=Quantity.from_int(100000),
        )

        # Assert
        self.assertEqual(Money(-0.90909091, BTC), pnl)
        self.assertEqual(Money(-0.90909091, BTC),
                         position.unrealized_pnl(Price.from_str("11000.00")))
        self.assertEqual(Money(-0.00750000, BTC), position.realized_pnl)
        self.assertEqual(Money(9.09090909, BTC),
                         position.notional_value(Price.from_str("11000.00")))
Ejemplo n.º 5
0
    def test_calculate_pnl_for_long_position_loss(self):
        # Arrange
        order = self.order_factory.market(
            BTCUSDT_BINANCE.symbol,
            OrderSide.BUY,
            Quantity(12),
        )

        fill = TestStubs.event_order_filled(
            order,
            instrument=BTCUSDT_BINANCE,
            position_id=PositionId("P-123456"),
            strategy_id=StrategyId("S", "001"),
            fill_price=Price("10500.00"),
        )

        position = Position(fill)

        # Act
        pnl = position.calculate_pnl(
            avg_open=Price("10500.00"),
            avg_close=Price("10480.50"),
            quantity=Quantity(10),
        )

        # Assert
        self.assertEqual(Money("-195.00000000", USDT), pnl)
        self.assertEqual(Money("-126.00000000", USDT), position.realized_pnl)
        self.assertEqual(Money("-234.00000000", USDT), position.unrealized_pnl(Price("10480.50")))
        self.assertEqual(Money("-360.00000000", USDT), position.total_pnl(Price("10480.50")))
        self.assertEqual([Money("126.00000000", USDT)], position.commissions())
        self.assertEqual(Money("126.00000000", USDT), position.commission)
Ejemplo n.º 6
0
    def test_calculate_pnl_for_long_position_win(self):
        # Arrange
        order = self.order_factory.market(
            BTCUSDT_BINANCE.id,
            OrderSide.BUY,
            Quantity.from_int(12),
        )

        fill = TestStubs.event_order_filled(
            order,
            instrument=BTCUSDT_BINANCE,
            position_id=PositionId("P-123456"),
            strategy_id=StrategyId("S-001"),
            last_px=Price.from_str("10500.00"),
        )

        position = Position(instrument=BTCUSDT_BINANCE, fill=fill)

        # Act
        pnl = position.calculate_pnl(
            avg_px_open=Price.from_str("10500.00"),
            avg_px_close=Price.from_str("10510.00"),
            quantity=Quantity.from_int(12),
        )

        # Assert
        self.assertEqual(Money(120.00000000, USDT), pnl)
        self.assertEqual(Money(-126.00000000, USDT), position.realized_pnl)
        self.assertEqual(
            Money(120.00000000, USDT),
            position.unrealized_pnl(Price.from_str("10510.00")),
        )
        self.assertEqual(Money(-6.00000000, USDT),
                         position.total_pnl(Price.from_str("10510.00")))
        self.assertEqual([Money(126.00000000, USDT)], position.commissions())
Ejemplo n.º 7
0
    def test_calculate_pnl_when_given_position_side_flat_returns_zero(self):
        # Arrange
        order = self.order_factory.market(
            BTCUSDT_BINANCE.id,
            OrderSide.BUY,
            Quantity.from_int(12),
        )

        fill = TestStubs.event_order_filled(
            order,
            instrument=BTCUSDT_BINANCE,
            position_id=PositionId("P-123456"),
            strategy_id=StrategyId("S-001"),
            last_px=Price.from_str("10500.00"),
        )

        position = Position(instrument=BTCUSDT_BINANCE, fill=fill)

        # Act
        result = position.calculate_pnl(
            Price.from_str("10500.00"),
            Price.from_str("10500.00"),
            Quantity.from_int(100000),
        )

        # Assert
        self.assertEqual(Money(0, USDT), result)
Ejemplo n.º 8
0
    def test_calculate_pnl_for_long_position_loss(self):
        # Arrange
        order = self.order_factory.market(
            BTCUSDT_BINANCE.id,
            OrderSide.BUY,
            Quantity.from_int(12),
        )

        fill = TestEventStubs.order_filled(
            order,
            instrument=BTCUSDT_BINANCE,
            position_id=PositionId("P-123456"),
            strategy_id=StrategyId("S-001"),
            last_px=Price.from_str("10500.00"),
        )

        position = Position(instrument=BTCUSDT_BINANCE, fill=fill)

        # Act
        pnl = position.calculate_pnl(
            avg_px_open=Price.from_str("10500.00"),
            avg_px_close=Price.from_str("10480.50"),
            quantity=Quantity.from_int(10),
        )

        # Assert
        assert pnl == Money(-195.00000000, USDT)
        assert position.realized_pnl == Money(-126.00000000, USDT)
        assert position.unrealized_pnl(Price.from_str("10480.50")) == Money(
            -234.00000000, USDT)
        assert position.total_pnl(Price.from_str("10480.50")) == Money(
            -360.00000000, USDT)
        assert position.commissions() == [Money(126.00000000, USDT)]
Ejemplo n.º 9
0
    def test_calculate_pnl_for_short_position_loss(self):
        # Arrange
        order = self.order_factory.market(
            BTCUSDT_BINANCE.id,
            OrderSide.SELL,
            Quantity.from_str("10"),
        )

        fill = TestEventStubs.order_filled(
            order,
            instrument=BTCUSDT_BINANCE,
            position_id=PositionId("P-123456"),
            strategy_id=StrategyId("S-001"),
            last_px=Price.from_str("10500.00"),
        )

        position = Position(instrument=BTCUSDT_BINANCE, fill=fill)

        # Act
        pnl = position.calculate_pnl(
            Price.from_str("10500.00"),
            Price.from_str("10670.50"),
            Quantity.from_str("10.000000"),
        )

        # Assert
        assert pnl == Money(-1705.00000000, USDT)
        assert position.unrealized_pnl(Price.from_str("10670.50")) == Money(
            -1705.00000000, USDT)
        assert position.realized_pnl == Money(-105.00000000, USDT)
        assert position.commissions() == [Money(105.00000000, USDT)]
        assert position.notional_value(Price.from_str("10670.50")) == Money(
            106705.00000000, USDT)
Ejemplo n.º 10
0
    def test_calculate_pnl_for_short_position_winning(self):
        # Arrange
        order = self.order_factory.market(
            BTCUSDT_BINANCE.id,
            OrderSide.SELL,
            Quantity.from_str("10.150000"),
        )

        fill = TestStubs.event_order_filled(
            order,
            instrument=BTCUSDT_BINANCE,
            position_id=PositionId("P-123456"),
            strategy_id=StrategyId("S-001"),
            last_px=Price.from_str("10500.00"),
        )

        position = Position(instrument=BTCUSDT_BINANCE, fill=fill)

        # Act
        pnl = position.calculate_pnl(
            Price.from_str("10500.00"),
            Price.from_str("10390.00"),
            Quantity.from_str("10.150000"),
        )

        # Assert
        self.assertEqual(Money(1116.50000000, USDT), pnl)
        self.assertEqual(
            Money(1116.50000000, USDT),
            position.unrealized_pnl(Price.from_str("10390.00")),
        )
        self.assertEqual(Money(-106.57500000, USDT), position.realized_pnl)
        self.assertEqual([Money(106.57500000, USDT)], position.commissions())
        self.assertEqual(
            Money(105458.50000000, USDT),
            position.notional_value(Price.from_str("10390.00")),
        )