Skip to content

Engine: amend a position's stop, target and trail (#695) - #696

Merged
rustyeddy merged 2 commits into
mainfrom
amend-stop-trail-695
Nov 20, 2026
Merged

rustyeddy merged 2 commits into
mainfrom
amend-stop-trail-695

Conversation

@rustyeddy

Copy link
Copy Markdown
Owner

Closes #695

Adds the order action amend, which changes the protective stop and take profit the broker holds for an open position. Needed by the external strategy protocol (#690, ADR-094) and usable in process.

Rules (owner decisions on #690):

  • A stop only tightens (a long's rises, a short's falls) and stays on the losing side of the latest price; it may pass the entry to lock in profit.
  • A fixed stop can become a trailing one (it keeps its level and trails from the next bar); a trail can only be shortened.
  • A target may be added or moved either way on the winning side of the latest price; it cannot be removed.
  • All or nothing: a refused amend changes nothing.
  • Statically invalid amends fail the run through validate(); stateful problems (no position, loosening, wrong side) are expected rejections kept in trader.rejected, and the run continues.
  • An amend is not an execution: it is not in broker.completed and is not counted as a fill. It is kept in broker.amended, and each exit it changed records the change in its history.

Verification: make check clean; EMAX md5 unchanged (195e457a21def1223e7181c86cb824a4); 25 new tests in broker_test.go, order_test.go and trader_test.go; six rule mutations (each loosening or side check, applying the change, the position check) each fail a test.

🤖 Generated with Claude Code

https://claude.ai/code/session_01DzeiWMoBKxFP8WVjepv2fP

A new order action amend changes the protective stop and take profit a
broker holds for an open position. It never loosens the stop or the trail
(the account sized the position from them), turns a fixed stop into a
trailing one, and may move or add a target on the winning side of the
latest price. Static problems fail the run through validate(); stateful
ones (no position, loosening, wrong side) are expected rejections.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01DzeiWMoBKxFP8WVjepv2fP
@rustyeddy rustyeddy linked an issue Nov 20, 2026 that may be closed by this pull request
Copilot AI balanced review requested due to automatic review settings November 20, 2026 02:45

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.

@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.

This has valuable coverage for move-to-breakeven/trailing stops, but it gets ahead of the agreed implementation sequence: Rusty explicitly kept #695 open for a focused later increment, with engine and external protocol changes together, after the initial working TCP/JSON vertical slice. Please do not merge as part of the first protocol slice without a fresh owner decision.

Two behavioral points to resolve/test when we pick this up:

  1. The amend request is recorded as stateFilled while explicitly not being an execution/fill (broker.completed excludes it). Please clarify the domain semantics: is stateFilled intended to mean 'amend accepted/applied', or should we represent amendment completion distinctly without confusing trade-fill accounting/results? ADR-093's single order lifecycle should remain authoritative.
  2. A changed protective stop and target are mutated in place and stored in exit history. Verify that the future durable journal can reconstruct pre-amend and post-amend state and the reason for expected stateful rejections—not just read br.amended in memory. This is particularly important for Investor's postmortem.

I have not independently run make check here; the PR reports it passed. Recommendation: keep the PR available for later focused review, but first finish and test the base transport slice.

Copy link
Copy Markdown
Owner Author

Correction to my review: I misunderstood Rusty's use of v1. He means a complete milestone that can perform realistic external strategy backtests, achieved through incremental features; not the very first vertical slice. Thus it is entirely reasonable to develop amend as one of the increments toward v1, and the existence of PR #696 is not itself a scope violation. Please continue evaluating the engine change on technical correctness and tests; my questions about stateFilled semantics and durable reconstruction remain open. Build → test → evaluate → decide, one issue at a time.

@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.

Re-review at 3b4b2d5: the earlier conceptual comments are addressed in this revision. The code now explicitly defines stateFilled for amend as applied, not an execution, keeps it out of broker.completed, and records the amendment ID plus before/after values on modified exits. The tests cover tightening, stateful rejection, target changes, trailing ratchet, timing, and account isolation. I see no remaining blocker in the diff for this focused engine increment. Merge recommendation: yes, subject to the normal required checks/owner approval. I could not independently execute make check, and GitHub reported no commit statuses for this SHA; the PR itself reports a clean run. Durable on-disk journal implementation remains tracked separately under #702.

@rustyeddy
rustyeddy merged commit 427547e into main Nov 20, 2026
1 check passed
@rustyeddy
rustyeddy deleted the amend-stop-trail-695 branch November 20, 2026 04:07
rustyeddy added a commit that referenced this pull request Nov 20, 2026
Reverts PRs #697, #706, #707 and #708 while preserving PR #696 engine stop amendments and ADR-093. Document historical external strategy ADR scope.
rustyeddy added a commit that referenced this pull request Nov 20, 2026
…709)

Reverts PRs #697, #706, #707 and #708 while preserving PR #696 engine stop amendments and ADR-093. Document historical external strategy ADR scope.
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.

Engine: amend a position's stop and trail (action amend)

2 participants