feat(backtest): Order/Position/Trade dataclasses with fees
Side StrEnum (long/short/flat), frozen dataclasses con calcolo unrealized_pnl per Position e gross/fees/net_pnl per Trade (fees in basis point, default 5bp). 4 test TDD passing, ruff + mypy strict clean. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
from datetime import UTC, datetime
|
||||
|
||||
import pytest
|
||||
|
||||
from multi_swarm.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)
|
||||
Reference in New Issue
Block a user