Skip to content

Return requested orders over TCP/JSON (#711) - #712

Merged
rustyeddy merged 2 commits into
mainfrom
feat/711-orders-over-tcp
Nov 21, 2026
Merged

rustyeddy merged 2 commits into
mainfrom
feat/711-orders-over-tcp

Conversation

@rustyeddy

Copy link
Copy Markdown
Owner

Closes #711. Part of #690. Follows the plan posted on #711 and the owner's clarification there (no instrument-membership check).

What

A worker can now answer each group of bars with zero or more requested orders. Trader maps them onto its own order and runs them through the existing handleOrder → order.validate → account sizing → broker path. There is no second order lifecycle.

Public type mapping (the narrow boundary)

strategy cannot import trader, and order is unexported, so there is one transport DTO in the public package: strategy.Order. It is a request, not a lifecycle model, and no domain types are exposed.

JSON field Maps to
instrument order.instrument
action (open, add, close, reduce) order.action (amend is refused)
direction (long, short; open only) order.direction
type (market by default, limit, stop) order.orderType
price, stop_loss, take_profit (decimal strings, num.Price) price, stopLoss, takeProfit
  • The worker never sets units, id, account, state, fill price, reason or decision time. Replies are decoded strictly (DisallowUnknownFields), so any such field, or any unknown one, fails the run with an explicit error.
  • The trader sets Time (the delivered bar of that instrument if the group has one, else the group's first bar), decidedAt, accountID, id, units and state, in the existing code.
  • Per the owner's decision there is no instrument-membership check: an order may name an instrument that is not in the current group.
  • strategy_remote.go validates every requested order (order.validate, the same rules as in-process) before returning any, so one bad order fails the callback and none of the group is processed. An open without a stop fails there.

Changes

  • strategy/strategy.go: Order; the answer holds []Order. strategy/frame.go: strict decoding (the 16 MiB cap stays). strategy/client.go: Client.OnBars returns ([]Order, error). strategy/server.go: Serve / ListenAndServe callbacks return orders; deadline, cancel and loopback behaviour are unchanged.
  • strategy_remote.go: toOrders / toOrder and parseAction / parseDirection / parseOrderType.
  • examples/remote-strategy: a tiny deterministic rule. It opens a long on the 3rd group with a stop 0.015 below the close and closes on the 6th.

Tests

  • strategy: wire form of Order; orders arrive in order with exact prices; zero orders is [], never null; a field the worker may not send (units, id, state, account, fill_price, unknown) is an error; malformed answers are errors; Serve carries orders; the existing deadline, cancel and loopback tests still pass.
  • Adapter: field mapping and what stays unset; every validation failure (missing stop or price, stop on the wrong side, no direction, amend, unknown action/direction/type, no time); times by instrument; an instrument outside the group is accepted; a bad second order returns none.
  • Integration on the a-reversal-exit fixture: the same rule in-process and over TCP gives identical orders, sizes, fills, lifecycle, balance and trades (open filled, close filled, the broker's stop cancelled). The real example binary, run as a separate process, matches too. Bad orders (no stop, amend, a bad order after a good one) fail the run, and nothing of that group reaches the broker, the account or rejected.
  • make check passes; the EMAX md5 is unchanged (195e457a…).

Deferred

trail / expires / amend, registration and RunID, a Trader-side listener, broker feedback (#698), journal (#702), REST (#700), auth/TLS, binary transport.

🤖 Generated with Claude Code

A worker answers each group with zero or more strategy.Order requests: an
instrument, action, direction, type and decimal-string prices. The root adapter
maps each onto the trader's own order and validates every one before any is
processed, so the existing handleOrder, account sizing and broker path decide
what happens. Replies decode strictly, so units, id, state and any other field
the worker may not set fail the run. The example worker opens then closes a long.

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:02

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 #712 (4337b15) against #711. Good direction overall: a single small public strategy.Order request DTO, strict unknown-field decoding, no extra order lifecycle, all returned requests validated before any are passed to the existing Account → Broker pipeline, and useful in-process/remote backtest parity tests. The owner's decision to omit instrument-membership checking is preserved.

I would address two correctness details before merging:

  1. Order timestamp for an instrument absent from the current bar group (strategy_remote.go:orderTime). The fallback uses bars[0].Time (the opening timestamp of an arbitrary other instrument's bar). The group may contain mixed intervals/opening times; this can assign the wrong effective time to the requested order. Since the owner explicitly allows ordering an instrument with no bar in this group, please use the current frame's actual availability/decision time as the fallback, or explain and test an equivalent invariant. Keep the existing bar-time semantics for matched instruments if required by the engine; avoid introducing an instrument-membership gate. If the adapter lacks the frame time, propose the smallest change needed rather than silently choosing the first bar's open.

  2. Require a well-formed response envelope. readMessage uses DisallowUnknownFields, which rejects forbidden order fields, but unmarshalling {} or {"orders":null} into ordersMessage succeeds and the client treats it like zero orders. Our agreed wire response is {"orders":[]} (or a nonempty array). Please reject a missing/null orders field and test both cases; keep [] valid.

Otherwise the scope is right. I haven't independently executed make check in this environment; the PR reports it passes. Please address these two items, rerun tests, and I'll re-review promptly.

@rustyeddy rustyeddy left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Looks OK.

…st (#711)

An order for an instrument with no bar in the group takes the time the group
became available instead of another instrument's opening time. An answer without
an orders list, or with null, is an error; [] stays valid.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@rustyeddy

Copy link
Copy Markdown
Owner Author

Addressed both items in eab2569 (not merged).

  1. Order time for an instrument outside the group. orderTime now falls back to when the group became available (the latest AvailableAt of its bars, which is the same for every bar of a frame) instead of bars[0].Time. A matched instrument keeps its own bar's opening time. No membership gate was added. The test uses bars of different opening times and checks the fallback is the availability time, not another instrument's open.
  2. Well-formed envelope. Client now rejects an answer with no orders field or "orders":null ("answer has no orders list"); [] is still valid. Tests cover {} and {"orders":null}, and the existing empty-list test still passes. The package doc says the list must be present.

make check passes; EMAX md5 unchanged (195e457a…).

Copy link
Copy Markdown
Owner Author

Re-reviewed updated commit eab2569 against my two findings on #712:

  1. Timestamp fallback fixed. For an instrument absent from the current OnBars group, orderTime now uses the group's latest AvailableAt rather than an unrelated bar's opening Time. The mixed-opening-time test covers this. No new instrument-membership gate was introduced.
  2. Response envelope fixed. Client.exchange now rejects missing/null orders lists, while accepting {"orders":[]}. Tests cover {}, null, malformed replies, forbidden fields and valid replies.

The overall mapping still uses one public request DTO and Trader's existing order validation, Account sizing and Broker lifecycle, with in-process/remote parity tests. No further blocking findings in this review; I recommend merging #712 provided CI/checks pass. Good focused increment—keep registration, feedback and journaling for later issues.

@rustyeddy
rustyeddy merged commit 9f68ac0 into main Nov 21, 2026
1 check passed
@rustyeddy
rustyeddy deleted the feat/711-orders-over-tcp branch November 21, 2026 19:23
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: return requested orders over TCP/JSON

2 participants