fix(@typedtrader/exchange): report real crypto fees on Alpaca fills#1177
Open
bennycode wants to merge 2 commits into
Open
fix(@typedtrader/exchange): report real crypto fees on Alpaca fills#1177bennycode wants to merge 2 commits into
bennycode wants to merge 2 commits into
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates the Alpaca exchange integration to stop hardcoding zero fees on fills and instead compute Alpaca crypto commissions (keeping stocks/ETFs commission-free), while also making toFilledOrder reject filled orders that lack an average fill price.
Changes:
- Compute realized crypto fees in
AlpacaBrokerMapper.toFilledOrderusingbig.js, and plumb fee rates into the mapper. - Update Alpaca broker call sites to pass fee rates (pair-specific for REST
getFills, default schedule for account-wide streaming). - Expand mapper unit tests to cover crypto fee calculation cases and the new mapper signature.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| packages/exchange/src/broker/alpaca/AlpacaBrokerMapper.ts | Derives crypto fill fees and throws when filled_avg_price is missing; updates fill mapping accordingly. |
| packages/exchange/src/broker/alpaca/AlpacaBrokerMapper.test.ts | Adds coverage for crypto maker/taker fee computations and updates tests for new mapper signature. |
| packages/exchange/src/broker/alpaca/AlpacaBroker.ts | Passes fee rates into fill mapping for both REST fill fetching and the trading stream. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+129
to
+131
| if (order.asset_class === 'crypto') { | ||
| const feeRate = order.type === 'limit' ? feeRates.LIMIT : feeRates.MARKET; | ||
| const filledQty = new Big(order.filled_qty); |
| * The stream is account-wide (no single pair to query rates for), so the default fee | ||
| * schedule is used — the same values `getFeeRates` returns today. | ||
| */ | ||
| const fill = AlpacaBrokerMapper.toFilledOrder(message.order, pair, AlpacaBroker.DEFAULT_FEE_RATES); |
…cument the maker-rate approximation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
AlpacaBrokerMapper.toFilledOrderhardcodedfee: '0'on every fill, so realized P&L for crypto silently ignored Alpaca's crypto commission. Alpaca's order payload carries no fee field, so the mapper now derives it from the broker's fee rates forasset_class: 'crypto'orders.filled_qty x rate), a SELL pays in the credited counter/fiat (filled_qty x filled_avg_price x rate). Limit orders bill at the maker rate (0.0015); all other order types execute as takers (0.0025). All money math usesbig.js; wire types stay string-based and theFillshape is unchanged.'0'(commission-free).toFilledOrdernow takes theFeeRateas a parameter:getFillspassesawait this.getFeeRates(pair); the account-wide trading stream usesAlpacaBroker.DEFAULT_FEE_RATES(same values today) since there is no single pair to query.filled_avg_pricenow throws instead of mappingprice: 'null'.position: OrderPosition.LONGstays: Alpaca's order payload exposes no position side (a SELL closing a long is indistinguishable from one opening a short), and this integration only trades long — now documented at the assignment.Test plan
npm testinpackages/exchange(87 tests, 8 files) — includes new mapper cases: stock fill maps with fee'0'; crypto MARKET BUY charges2.5 BTC x 0.0025 = '0.00625'inBTC; crypto LIMIT SELL charges0.4 x 61234.5 x 0.0015 = '36.7407'inUSD(exact Big string assertions)npm run lintinpackages/exchangenpx lerna run dist --scope trading-strategies --include-dependencies(3 projects build cleanly)