Skip to content

Commit

Permalink
Improve order fills report (#1290)
Browse files Browse the repository at this point in the history
  • Loading branch information
r3k4mn14r authored Oct 20, 2023
1 parent 0082d06 commit bf30479
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
3 changes: 1 addition & 2 deletions nautilus_trader/analysis/reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

from nautilus_trader.accounting.accounts.base import Account
from nautilus_trader.core.datetime import unix_nanos_to_dt
from nautilus_trader.model.enums import OrderStatus
from nautilus_trader.model.events import AccountState
from nautilus_trader.model.orders import Order
from nautilus_trader.model.position import Position
Expand Down Expand Up @@ -71,7 +70,7 @@ def generate_order_fills_report(orders: list[Order]) -> pd.DataFrame:
if not orders:
return pd.DataFrame()

filled_orders = [o.to_dict() for o in orders if o.status == OrderStatus.FILLED]
filled_orders = [o.to_dict() for o in orders if o.filled_qty > 0]
if not filled_orders:
return pd.DataFrame()

Expand Down
32 changes: 30 additions & 2 deletions tests/unit_tests/analysis/test_reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,16 @@ def test_generate_order_fills_report(self):
order2.apply(TestEventStubs.order_submitted(order2))
order2.apply(TestEventStubs.order_accepted(order2))

order3 = self.order_factory.limit(
AUDUSD_SIM.id,
OrderSide.SELL,
Quantity.from_int(1_500_000),
Price.from_str("0.80000"),
)

order3.apply(TestEventStubs.order_submitted(order3))
order3.apply(TestEventStubs.order_accepted(order3))

filled = TestEventStubs.order_filled(
order1,
instrument=AUDUSD_SIM,
Expand All @@ -185,13 +195,24 @@ def test_generate_order_fills_report(self):

order1.apply(filled)

orders = [order1, order2]
partially_filled = TestEventStubs.order_filled(
order3,
instrument=AUDUSD_SIM,
position_id=PositionId("P-1"),
strategy_id=StrategyId("S-1"),
last_px=Price.from_str("0.80011"),
last_qty=Quantity.from_int(500_000),
)

order3.apply(partially_filled)

orders = [order1, order2, order3]

# Act
report = ReportProvider.generate_order_fills_report(orders)

# Assert
assert len(report) == 1
assert len(report) == 2
assert report.index.name == "client_order_id"
assert report.index[0] == order1.client_order_id.value
assert report.iloc[0]["instrument_id"] == "AUD/USD.SIM"
Expand All @@ -200,6 +221,13 @@ def test_generate_order_fills_report(self):
assert report.iloc[0]["quantity"] == "1500000"
assert report.iloc[0]["avg_px"] == "0.80011"
assert report.iloc[0]["slippage"] == "9.99999999995449e-06"
assert report.index[1] == order3.client_order_id.value
assert report.iloc[1]["instrument_id"] == "AUD/USD.SIM"
assert report.iloc[1]["side"] == "SELL"
assert report.iloc[1]["type"] == "LIMIT"
assert report.iloc[1]["quantity"] == "1500000"
assert report.iloc[1]["filled_qty"] == "500000"
assert report.iloc[1]["avg_px"] == "0.80011"

def test_generate_positions_report(self):
# Arrange
Expand Down

0 comments on commit bf30479

Please sign in to comment.