Skip to content

fix(futures-paper): net triggered order margin against opposite-side positions - #56

Closed
theghostmac wants to merge 1 commit into
krakenfx:mainfrom
theghostmac:fix/futures-paper-triggered-order-margin
Closed

theghostmac wants to merge 1 commit into
krakenfx:mainfrom
theghostmac:fix/futures-paper-triggered-order-margin

Conversation

@theghostmac

Copy link
Copy Markdown
Contributor

Summary

Aligns initial margin calculation in FuturesPaperState::place_triggered_order (stop, take-profit, trailing-stop) with place_limit_order and edit_order by delegating to compute_margin_delta.

Root Cause

  • place_limit_order (src/futures_paper.rs:424) and edit_order (src/futures_paper.rs:963) both compute required margin via compute_margin_delta / compute_margin_delta_for_size, which nets order size against any existing opposite-side position ((size - opposite_pos_size).max(0.0)).
  • However, place_triggered_order (src/futures_paper.rs:671) computed notional / leverage directly without calling compute_margin_delta.
  • Consequently:
    1. Placing a non-reduce_only stop, take-profit, or trailing-stop order against an existing opposite-side position double-reserved margin (+850.00 on a 0.1 BTC stop buy at $85,000 10x while holding a 0.1 BTC short).
    2. Subsequent edits to that same order via edit_order recalculated reserved_margin using compute_margin_delta_for_size, causing reserved_margin to abruptly drop from 850.00 to 0.00.

Fix

  • In FuturesPaperState::place_triggered_order, compute eff_price = params.price.unwrap_or(stop_price) and delegate margin calculation to self.compute_margin_delta(params, eff_price, leverage).
  • Add unit test triggered_order_nets_margin_against_opposite_position.

Verification & CLI Proof

With an open 0.1 BTC Short position on PF_XBTUSD at $82,799.00 (10x leverage, Used Margin = 827.99):

kraken futures paper buy PF_XBTUSD 0.1 --type stop --stop-price 85000 --leverage 10 -o json
kraken futures paper balance

Before Fix

Used Margin jumped by +850.00 from 827.99 to 1677.99:

┌──────────────────┬───────────────────────────────────┐
│ Field            ┆ Value                             │
╞══════════════════╪═══════════════════════════════════╡
│ Mode             ┆ [FUTURES PAPER] Simulated Trading │
│ Collateral       ┆ 9995.86 USD                       │
│ Unrealized PnL   ┆ -11.91                            │
│ Used Margin      ┆ 1677.99                           │
│ Available Margin ┆ 8305.96                           │
│ Open Orders      ┆ 2                                 │
│ Positions        ┆ 1                                 │
└──────────────────┴───────────────────────────────────┘

After Fix

Used Margin stays at 827.99 (net new exposure is 0.0), consistent with place_limit_order and edit_order:

┌──────────────────┬───────────────────────────────────┐
│ Field            ┆ Value                             │
╞══════════════════╪═══════════════════════════════════╡
│ Mode             ┆ [FUTURES PAPER] Simulated Trading │
│ Collateral       ┆ 9995.86 USD                       │
│ Unrealized PnL   ┆ +6.54                             │
│ Used Margin      ┆ 827.99                            │
│ Available Margin ┆ 9174.41                           │
│ Open Orders      ┆ 1                                 │
│ Positions        ┆ 1                                 │
└──────────────────┴───────────────────────────────────┘

Automated Tests & Lints

  • cargo +nightly fmt --check
  • cargo clippy --all-targets -- -D warnings
  • cargo test --lib triggered_order_nets_margin_against_opposite_position

@MaxOxide

Copy link
Copy Markdown
Contributor

Thanks @theghostmac! This is fixed in v0.5.0. Stop, take-profit and trailing-stop orders now net their margin against an opposite position. We ported your fix to the new crates/ layout 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