b6539802e0
- mv src/multi_swarm → src/multi_swarm_core/multi_swarm_core (member layout) - sed-replace globale degli import: from/import multi_swarm.* → multi_swarm_core.* - 115 occorrenze aggiornate in src/ scripts/ tests/ - multi_swarm_coevolutive (nome repo) preservato dal word boundary Pre-condizione per il setup uv workspace della Fase 3. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
39 lines
1.2 KiB
Python
39 lines
1.2 KiB
Python
from datetime import UTC, datetime
|
|
|
|
import pytest
|
|
|
|
from multi_swarm_core.backtest.orders import Order, Position, Side, Trade
|
|
|
|
|
|
def test_order_validates_side() -> None:
|
|
o = Order(ts=datetime(2024, 1, 1, tzinfo=UTC), side=Side.LONG, size=1.0)
|
|
assert o.side == Side.LONG
|
|
|
|
|
|
def test_position_pnl_long() -> None:
|
|
pos = Position(side=Side.LONG, entry_price=100.0, size=2.0)
|
|
assert pos.unrealized_pnl(110.0) == pytest.approx(20.0)
|
|
assert pos.unrealized_pnl(90.0) == pytest.approx(-20.0)
|
|
|
|
|
|
def test_position_pnl_short() -> None:
|
|
pos = Position(side=Side.SHORT, entry_price=100.0, size=2.0)
|
|
assert pos.unrealized_pnl(110.0) == pytest.approx(-20.0)
|
|
assert pos.unrealized_pnl(90.0) == pytest.approx(20.0)
|
|
|
|
|
|
def test_trade_realized_pnl_with_fees() -> None:
|
|
t = Trade(
|
|
entry_ts=datetime(2024, 1, 1, tzinfo=UTC),
|
|
exit_ts=datetime(2024, 1, 2, tzinfo=UTC),
|
|
side=Side.LONG,
|
|
size=1.0,
|
|
entry_price=100.0,
|
|
exit_price=110.0,
|
|
fees_bp=5.0,
|
|
)
|
|
# gross 10, fees = 5bp * (100+110) = 0.0005 * 210 = 0.105
|
|
assert t.gross_pnl == pytest.approx(10.0)
|
|
assert t.fees == pytest.approx(0.105)
|
|
assert t.net_pnl == pytest.approx(9.895)
|