Skip to Content
While25SDK docs
ExamplesMonitors

Fill journal for the whole account

Writes every execution on the account, from any strategy or by hand, to a CSV file in the workspace, one row per fill, and logs each order that ends refused or cancelled. A journal you can open in a spreadsheet at the end of the day.

Writes every execution on the account, from any strategy or by hand, to a CSV file in the workspace, one row per fill, and logs each order that ends refused or cancelled. A journal you can open in a spreadsheet at the end of the day.

Shows: Monitor, @on.fill and @on.order with no tag (account-wide), the event's exact Decimal figures, None on non-fill events, a CSV in data/, run as a module with no market data.

View on GitHub

monitors/fill_journal.py
"""Fill journal for the whole account

Writes every execution on the account, from any strategy or by hand, to a
CSV file in the workspace, one row per fill, and logs each order that ends
refused or cancelled. A journal you can open in a spreadsheet at the end of
the day.

Shows: Monitor, @on.fill and @on.order with no tag (account-wide), the event's exact Decimal figures, None on non-fill events, a CSV in data/, run as a module with no market data.
"""
import csv
from datetime import datetime, timezone
from pathlib import Path

import while25
from while25 import Monitor, on

JOURNAL = Path("data/fill_journal.csv")
COLUMNS = ["time_utc", "order_id", "symbol", "side", "order_type", "quantity",
           "price", "order_filled", "order_average", "tag", "held_by", "combo_id"]


def journal_row(ev):
    """One CSV row for a FILL event: its own execution figures, as text."""
    t = datetime.fromtimestamp(ev["timestamp_ms"] / 1000, timezone.utc) if ev.get("timestamp_ms") else None
    return {"time_utc": t.isoformat() if t else "",
            "order_id": ev["order_id"], "symbol": ev["symbol"], "side": ev["side"],
            "order_type": ev["order_type"],
            "quantity": str(ev["last_fill_quantity"]), "price": str(ev["last_fill_price"]),
            "order_filled": str(ev["filled_quantity"]),
            "order_average": "" if ev["average_price"] is None else str(ev["average_price"]),
            "tag": ev["tag"] or "", "held_by": ev["held_by"] or "",
            "combo_id": ev["combo_id"] or ""}


def append(row, path=JOURNAL):
    path.parent.mkdir(parents=True, exist_ok=True)
    new = not path.exists()
    with path.open("a", newline="") as f:
        w = csv.DictWriter(f, fieldnames=COLUMNS)
        if new:
            w.writeheader()
        w.writerow(row)


class FillJournal(Monitor):
    id = "fill_journal"
    label = "Fill journal"

    @on.fill()
    def on_fill(self, ctx, ev):
        append(journal_row(ev))

    @on.order()
    def on_order(self, ctx, ev):
        if ev["event"] in ("REJECT", "CANCEL"):
            while25.log(f"{ev['event']} {ev['symbol']} {ev['side']} {ev['quantity']}: "
                        f"{ev['reason'] or 'no reason given'}")