blob: 4c0fa5667dbe1db3b3a75a62eee25752fe1f3e36 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
"""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
|