1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
"""Tests for PortfolioTracker."""
from decimal import Decimal
from shared.models import Order, OrderSide, OrderStatus, OrderType
from portfolio_manager.portfolio import PortfolioTracker
def make_order(side: OrderSide, price: str, quantity: str) -> Order:
"""Helper to create a filled Order."""
return Order(
signal_id="test-signal",
symbol="BTC/USDT",
side=side,
type=OrderType.MARKET,
price=Decimal(price),
quantity=Decimal(quantity),
status=OrderStatus.FILLED,
)
def test_portfolio_add_buy_order() -> None:
tracker = PortfolioTracker()
order = make_order(OrderSide.BUY, "50000", "0.1")
tracker.apply_order(order)
position = tracker.get_position("BTC/USDT")
assert position is not None
assert position.quantity == Decimal("0.1")
assert position.avg_entry_price == Decimal("50000")
def test_portfolio_add_multiple_buys() -> None:
tracker = PortfolioTracker()
tracker.apply_order(make_order(OrderSide.BUY, "50000", "0.1"))
tracker.apply_order(make_order(OrderSide.BUY, "52000", "0.1"))
position = tracker.get_position("BTC/USDT")
assert position is not None
assert position.quantity == Decimal("0.2")
assert position.avg_entry_price == Decimal("51000")
def test_portfolio_sell_reduces_position() -> None:
tracker = PortfolioTracker()
tracker.apply_order(make_order(OrderSide.BUY, "50000", "0.2"))
tracker.apply_order(make_order(OrderSide.SELL, "55000", "0.1"))
position = tracker.get_position("BTC/USDT")
assert position is not None
assert position.quantity == Decimal("0.1")
assert position.avg_entry_price == Decimal("50000")
def test_portfolio_no_position_returns_none() -> None:
tracker = PortfolioTracker()
position = tracker.get_position("ETH/USDT")
assert position is None
def test_realized_pnl_on_sell() -> None:
"""Selling should track realized PnL."""
tracker = PortfolioTracker()
# Buy at 50000
tracker.apply_order(Order(
signal_id="s1", symbol="BTCUSDT", side=OrderSide.BUY,
type=OrderType.MARKET, price=Decimal("50000"),
quantity=Decimal("0.1"), status=OrderStatus.FILLED,
))
assert tracker.realized_pnl == Decimal("0")
# Sell at 55000 — profit of 500
tracker.apply_order(Order(
signal_id="s2", symbol="BTCUSDT", side=OrderSide.SELL,
type=OrderType.MARKET, price=Decimal("55000"),
quantity=Decimal("0.1"), status=OrderStatus.FILLED,
))
assert tracker.realized_pnl == Decimal("500")
def test_realized_pnl_on_loss() -> None:
"""Selling at a loss should track negative realized PnL."""
tracker = PortfolioTracker()
tracker.apply_order(Order(
signal_id="s1", symbol="BTCUSDT", side=OrderSide.BUY,
type=OrderType.MARKET, price=Decimal("50000"),
quantity=Decimal("0.1"), status=OrderStatus.FILLED,
))
tracker.apply_order(Order(
signal_id="s2", symbol="BTCUSDT", side=OrderSide.SELL,
type=OrderType.MARKET, price=Decimal("45000"),
quantity=Decimal("0.1"), status=OrderStatus.FILLED,
))
assert tracker.realized_pnl == Decimal("-500")
def test_realized_pnl_accumulates() -> None:
"""Multiple sells accumulate realized PnL."""
tracker = PortfolioTracker()
# Buy 0.2 at 50000
tracker.apply_order(Order(
signal_id="s1", symbol="BTCUSDT", side=OrderSide.BUY,
type=OrderType.MARKET, price=Decimal("50000"),
quantity=Decimal("0.2"), status=OrderStatus.FILLED,
))
# Sell 0.1 at 55000 -> +500
tracker.apply_order(Order(
signal_id="s2", symbol="BTCUSDT", side=OrderSide.SELL,
type=OrderType.MARKET, price=Decimal("55000"),
quantity=Decimal("0.1"), status=OrderStatus.FILLED,
))
# Sell 0.1 at 60000 -> +1000
tracker.apply_order(Order(
signal_id="s3", symbol="BTCUSDT", side=OrderSide.SELL,
type=OrderType.MARKET, price=Decimal("60000"),
quantity=Decimal("0.1"), status=OrderStatus.FILLED,
))
assert tracker.realized_pnl == Decimal("1500")
|