Skip to content

fix(futures-paper): update trailing_anchor when editing stop_price on trailing-stop orders - #54

Closed
theghostmac wants to merge 1 commit into
krakenfx:mainfrom
theghostmac:fix/futures-paper-trailing-stop-edit
Closed

theghostmac wants to merge 1 commit into
krakenfx:mainfrom
theghostmac:fix/futures-paper-trailing-stop-edit

Conversation

@theghostmac

Copy link
Copy Markdown
Contributor

Summary

Fixes a bug in FuturesPaperState::edit_order where editing --stop-price on an open trailing-stop order updated stop_price but left trailing_anchor unchanged.

Root Cause

  1. When a TrailingStop order is placed in place_triggered_order (src/futures_paper.rs), trailing_anchor is initialized from stop_price (Some(stop_price)).
  2. During reconciliation, check_trailing_stop_trigger evaluates trigger deviation exclusively against order.trailing_anchor (never order.stop_price).
  3. However, FuturesPaperState::edit_order only updated self.open_orders[idx].stop_price = Some(sp) when new_stop_price was provided, leaving trailing_anchor stale at its previous value. As a result, editing --stop-price on a trailing-stop order had no effect on trigger evaluation.

Fix

  • In FuturesPaperState::edit_order, when new_stop_price is Some(sp) and order_type == FuturesOrderType::TrailingStop, update trailing_anchor = Some(sp) alongside stop_price.
  • Add unit test edit_order_updates_trailing_stop_anchor in src/futures_paper.rs.

Verification & CLI Proof

1. Place a trailing-stop order (--stop-price 90000) and edit --stop-price

kraken futures paper buy PF_XBTUSD 0.1 \
  --type trailing-stop \
  --stop-price 90000 \
  --trailing-stop-max-deviation 2 \
  --trailing-stop-deviation-unit percent \
  --leverage 10 -o json

kraken futures paper edit-order --order-id FP-00003 --stop-price 85000 -o json

Before Fix (futures_state.json)

stop_price updated to 85000.0, while trailing_anchor remained stuck at 90000.0:

{
  "id": "FP-00003",
  "symbol": "PF_XBTUSD",
  "side": "Long",
  "size": 0.1,
  "filled_size": 0.0,
  "order_type": "TrailingStop",
  "price": null,
  "stop_price": 85000.0,
  "trigger_signal": "Last",
  "client_order_id": null,
  "reduce_only": false,
  "leverage": 10.0,
  "reserved_margin": 0.0,
  "status": "Open",
  "trailing_stop_max_deviation": 2.0,
  "trailing_stop_deviation_unit": "percent",
  "trailing_anchor": 90000.0
}

After Fix (futures_state.json)

Editing --stop-price 86000 updates both stop_price and trailing_anchor in lockstep:

{
  "id": "FP-00003",
  "symbol": "PF_XBTUSD",
  "side": "Long",
  "size": 0.1,
  "filled_size": 0.0,
  "order_type": "TrailingStop",
  "price": null,
  "stop_price": 86000.0,
  "trigger_signal": "Last",
  "client_order_id": null,
  "reduce_only": false,
  "leverage": 10.0,
  "reserved_margin": 0.0,
  "status": "Open",
  "trailing_stop_max_deviation": 2.0,
  "trailing_stop_deviation_unit": "percent",
  "trailing_anchor": 86000.0
}

2. Automated Tests & Lints

running 1 test
test futures_paper::tests::edit_order_updates_trailing_stop_anchor ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 486 filtered out
  • cargo +nightly fmt --check
  • cargo clippy --all-targets -- -D warnings
  • cargo test --lib edit_order_updates_trailing_stop_anchor

@MaxOxide

Copy link
Copy Markdown
Contributor

Thanks @theghostmac! This is fixed in v0.5.0. Editing --stop-price on a trailing stop now moves its trigger. The code moved from src/ to crates/ in this release, so we ported your fix there and added you as co-author on the release commit. Closing this PR.

@MaxOxide MaxOxide closed this Oct 29, 2026
needaccforwhonix pushed a commit to needaccforwhonix/kraken-cli that referenced this pull request Nov 7, 2026
Sign in from the browser to create a restricted, expiring Spot API key.
The MCP server and the REPL ship as their own binaries, kraken-mcp and
kraken-shell, installed next to kraken. A paper strategy can go live in
a budget-fenced workspace, and a finished session packages as a recipe.
A timed-out order, cancel, withdrawal or transfer is never resent, and
an envelope that may hide an execution carries "verify_first": true.

Upgrading from 0.4.1 needs action from scripts, agents and MCP clients:
see CHANGELOG.md under 0.5.0.

Includes community fixes from krakenfx#50, krakenfx#51, krakenfx#54, krakenfx#55 and krakenfx#56.

Co-authored-by: theghostmac <83423188+theghostmac@users.noreply.github.com>
Co-authored-by: SashaMIT <33847880+SashaMIT@users.noreply.github.com>
Co-authored-by: sashaphmn <122053040+sashaphmn@users.noreply.github.com>
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.

2 participants