summaryrefslogtreecommitdiff
path: root/services/portfolio-manager/src/portfolio_manager/pnl.py
blob: 96f0da81a19aad761c889030fd97e06fa443fd5b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
"""PnL calculation functions for the portfolio manager."""
from decimal import Decimal


def calculate_unrealized_pnl(
    quantity: Decimal,
    avg_entry_price: Decimal,
    current_price: Decimal,
) -> Decimal:
    """Calculate unrealized PnL for an open position."""
    return quantity * (current_price - avg_entry_price)


def calculate_realized_pnl(
    buy_price: Decimal,
    sell_price: Decimal,
    quantity: Decimal,
    fee: Decimal = Decimal("0"),
) -> Decimal:
    """Calculate realized PnL for a completed trade."""
    return quantity * (sell_price - buy_price) - fee