Skip to content

Deliver execution feedback with the next OnBars (#698) - #713

Merged
rustyeddy merged 1 commit into
mainfrom
feat/698-execution-feedback
Nov 21, 2026
Merged

rustyeddy merged 1 commit into
mainfrom
feat/698-execution-feedback

Conversation

@rustyeddy

@rustyeddy rustyeddy commented Nov 21, 2026 •

Copy link
Copy Markdown
Owner

Closes #698. Part of #690. Implements the plan posted on #698 (defaults unchanged: optional feedbacker interface, Order.Ref echoed on events, kinds accepted / filled / rejected / cancelled).

What

A worker is told what Trader did with its orders, in the same request as the next group of bars. There is no second channel and no new state machine; Trader still owns risk, sizing, execution and OrderState.

Timing (no look-ahead, no leak)

trader.onFrame now collects events after broker.onFrame(f) and before onBars. A request therefore carries:

  • what the broker did while processing this frame's bars (the bar is complete, as in-process strategies already see), and
  • the outcome of the previous call's orders.

It never carries the outcome of the orders returned by this same call, which are handled after onBars returns and reported with the next group. Each history entry is delivered once (a per-order cursor), in time order; on a tie the strategy's own orders come before the broker's exits.

Event mapping

Events come from the existing order.history, trader.rejected and broker.completed; nothing new records them. feedback.go (root):

  • the submitted, filled, rejected and cancelled steps of the strategy's orders (requested and sized are the trader's own steps and are not reported);
  • the broker's own exits (protective stop, take profit, margin closeout) when they fill. Their other steps are housekeeping and are not reported (e.g. the sibling stop cancelled by a close).

Delivery uses a small optional interface next to warmer, feedbacker { feedback([]feedbackEvent) }, called just before onBars only when the strategy implements it. In-process strategies and EMAX are untouched (md5 195e457a… unchanged); the remote adapter keeps the events and sends them with the next group.

Public type (strategy.Event, read-only, no domain types)

field meaning
time when it happened (the bar's opening time)
kind accepted, filled, rejected, cancelled
id host-assigned id; empty for a refusal before it was queued (those never get one)
ref the worker's Order.Ref, echoed; empty for the broker's own orders
instrument, action, direction what the order was
units the size Trader chose
price fill price, for filled
cause strategy, stop loss, take profit, margin closeout: a filled with cause stop loss is a protective-stop exit
reason why, for rejected / cancelled

Rejected orders have no id (ids are issued at submit; giving refusals ids would shift later ids), so the worker labels an order with the new optional Order.Ref and gets it back on that order's events. The adapter keeps the label in a map[*order]string; the domain order gets no new field.

Wire and API

The request is {"bars":[...],"events":[...]}, with the same framing, deadline, size cap and strict decoding. events is required and may be [] (missing or null is an error, as with the orders list). Client.OnBars(ctx, bars, events); the Serve / ListenAndServe callback is func(bars, events) ([]Order, error).

examples/remote-strategy: same rule as before, but it closes on the 6th group only if it saw its entry fill and the stop has not already taken it out, and it prints each event to stderr.

Tests

  • Collector (feedback_test.go): nothing before the first order; accepted and filled told with the next group, not the asking one; delivered once; time order regardless of watch order; protective-stop and take-profit exits with their causes and none of their housekeeping; refusal at sizing carries the reason and no id; a waiting order that expires is reported cancelled; a strategy that does not ask is not watched; the wire mapping per state.
  • Adapter: refs echoed and forgotten, events sent once with the next group.
  • strategy: Event wire form; the client sends [] rather than null; the server hands events to the callback and refuses a request without the list; ref round trip.
  • Parity over TCP on a-reversal-exit: the same feedback-driven rule in-process and remote for (1) fill then a close it asked for, (2) a protective-stop exit after which the worker skips its close, (3) a sizing refusal. The worker is told exactly what the in-process strategy is told, and the runs end with identical orders, fills, rejections, balance and trades. The real example binary run as a separate process matches the in-process rule and prints the events.
  • make check passes; EMAX md5 unchanged.

Known limitation (by design)

Feedback rides on the next group, so events caused by the last callback's orders, or by the broker after the last delivered group, have no following callback to arrive in. A worker must not assume it receives a complete event set before the connection closes. The durable run journal (#702) has to preserve those events; run finalization is out of this increment.

Deferred

Position and account snapshots, push or async events, amend/trail events, richer per-fill fields (margin, P/L), registration/RunID, a Trader-side listener, journal (#702), REST (#700), auth/TLS, binary transport. units on an accepted close is the position size once known (a close is not sized at accept time).

🤖 Generated with Claude Code

A worker that asks for it (strategy.Event) is told what the trader did with its
orders: accepted, filled, rejected (with the reason) or cancelled, and the broker's
own exits (protective stop, take profit, closeout) when they fill. The events are
collected after the broker has processed the frame and before onBars, so a
request carries the broker's work on its own bars and the outcome of the previous
call's orders, never the outcome of the orders it is about to return. Each is
delivered once, in time order. Order gains an optional ref echoed on its events.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@rustyeddy rustyeddy linked an issue Nov 21, 2026 that may be closed by this pull request
Copilot AI balanced review requested due to automatic review settings November 21, 2026 19:36

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

Copy link
Copy Markdown
Owner Author

Reviewed PR #713 (fc9b173) against #698 and the agreed synchronous feedback design.

What looks right:

  • Feedback collection occurs after broker.onFrame(f) and before strategy onBars, so current callback orders cannot appear in their own feedback.
  • feedbackLog is a small set of delivery cursors over authoritative order.history and broker.completed; no independent order lifecycle. Only strategies implementing feedbacker incur tracking.
  • Worker request remains one {"bars":...,"events":...} / {"orders":...} exchange. The public Event is read-only, and Order.Ref correlates pre-submit rejections without inventing IDs.
  • Tests cover accepted/filled/rejected/cancelled, stop and take-profit exits, event order/once-only delivery, protocol validation, and in-process/remote parity.

One non-blocking limitation to document explicitly: Events created by the final OnBars callback (or after the last delivered group) have no following callback in which to arrive. That's consistent with the intentionally synchronous protocol; the future durable run journal (#702) must preserve them, and the worker should not assume it receives a final complete event set on EOF. Please note this in the package docs or PR description.

No blocking findings from this code review. Merge recommendation: yes, provided CI / make check remain green. Keep position snapshots, async pushes and run finalization out of this increment.

@rustyeddy
rustyeddy merged commit b453b9d into main Nov 21, 2026
2 checks passed
@rustyeddy
rustyeddy deleted the feat/698-execution-feedback branch November 21, 2026 19:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Strategy Protocol: deliver execution feedback with the next OnBars

2 participants