Skip to content

feat: implement Self-Trade Prevention (STP) - #596

Merged
fasenderos merged 2 commits into
mainfrom
feature/self-trade-prevention
Jun 27, 2026
Merged

feat: implement Self-Trade Prevention (STP)#596
fasenderos merged 2 commits into
mainfrom
feature/self-trade-prevention

Conversation

@fasenderos

@fasenderos fasenderos commented Jun 27, 2026

Copy link
Copy Markdown
Owner

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Other... Please describe:

What is the current behavior?

User can trade against themself

Related Issues: #537, #226 and #149

What is the new behavior?

Implements Self-Trade Prevention (STP) as described in Binance's STP documentation. Orders from the same account (accountId) can now be prevented from matching each other based on the taker's stpMode.

What's included

New API (SelfTradePreventionMode enum):

  • NONE — no prevention (default, backward compatible)
  • EXPIRE_MAKER — resting maker order(s) expire, taker continues
  • EXPIRE_TAKER — taker expires immediately, maker stays on book
  • EXPIRE_BOTH — both maker and taker expire

New fields on all order types:

  • accountId?: string — identifies the account/trade group for STP matching
  • stpMode?: SelfTradePreventionMode — STP mode for the order

New response field (IProcessOrder):

  • stpExpired?: IOrder[] — orders removed from the book due to STP

New error:

  • ERROR.STP_TRIGGERED (code 1202) — returned when EXPIRE_TAKER or EXPIRE_BOTH reject the taker

Scenarios covered (16 test cases):

  • EXPIRE_MAKER with limit and market orders
  • EXPIRE_TAKER with limit and market orders
  • EXPIRE_BOTH with limit and market orders
  • Taker mode wins over maker mode (per Binance spec)
  • Mixed accounts at same price level (STP only applies to matching account)
  • Multi-price-level STP expiration
  • NONE mode (same account matching allowed)
  • IOC orders with STP
  • STP carried through triggered stop orders
  • Backward compatible when no accountId is set

Fixes

Closes #537

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

@codecov

codecov Bot commented Jun 27, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (5e241ea) to head (8b59b53).

Additional details and impacted files
@@            Coverage Diff             @@
##              main      #596    +/-   ##
==========================================
  Coverage   100.00%   100.00%            
==========================================
  Files           10        10            
  Lines         2369      2538   +169     
  Branches       303       350    +47     
==========================================
+ Hits          2369      2538   +169     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@fasenderos
fasenderos merged commit 3bd83be into main Jun 27, 2026
9 checks passed
@fasenderos
fasenderos deleted the feature/self-trade-prevention branch June 27, 2026 14:45
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.

Add an optional [participantId] to avoid self-dealing

1 participant