rustrade_execution/trade.rs
1use crate::order::id::{ClientOrderId, OrderId, StrategyId};
2use chrono::{DateTime, Utc};
3use derive_more::{Constructor, From};
4use rust_decimal::Decimal;
5use rustrade_instrument::{
6 Side,
7 asset::QuoteAsset,
8 instrument::{Instrument, kind::InstrumentKind, round_toward_zero_to_step},
9};
10use serde::{Deserialize, Serialize};
11use smol_str::SmolStr;
12use std::fmt::{Display, Formatter};
13
14/// A venue's own identifier for one trade, carried through opaquely.
15///
16/// # Uniqueness is the venue's to define, and is often narrower than global
17///
18/// This library does not mint these — it passes through whatever the venue said, so what an id
19/// distinguishes is whatever that venue distinguishes. Binance's are **per symbol**, which is why
20/// this crate's own deduplication keys on the instrument alongside the id rather than on the id
21/// alone; Hyperliquid reports a transaction hash that can cover several matches at once. A
22/// consumer that needs a key should use `(exchange, instrument, TradeId)` and not assume any part
23/// of it is redundant.
24///
25/// [`SimulatedVenue`](crate::exchange::mock::SimulatedVenue) mints ids unique within one venue
26/// instance for its lifetime — and, because each venue counts from zero, **not** across the
27/// several a multi-exchange backtest holds. That is the same scoping a real venue gives, for the
28/// same reason.
29///
30/// # `Ord` is byte order, not time order
31///
32/// The derived comparison is lexicographic over the underlying string, so `"10" < "9"`. It exists
33/// so this type can key a map or a set; it carries no chronological meaning and is not a stand-in
34/// for one. Order trades by [`Trade::time_exchange`].
35///
36/// # Finding the order behind a trade
37///
38/// Use [`Trade::order_id`], which is typed. Do **not** parse this id: one order can print more
39/// than once, several venues embed structure of their own here (IBKR's `execution_id` is
40/// dot-separated, Alpaca's can be a UUID), and no format is common to them.
41#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Deserialize, Serialize, From)]
42pub struct TradeId<T = SmolStr>(pub T);
43
44impl<T> Display for TradeId<T>
45where
46 T: Display,
47{
48 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
49 Display::fmt(&self.0, f)
50 }
51}
52
53impl TradeId {
54 pub fn new<S: AsRef<str>>(id: S) -> Self {
55 Self(SmolStr::new(id))
56 }
57}
58
59/// One execution: a fill the venue reported, or one the engine settled itself.
60///
61/// `#[non_exhaustive]`, so a field can be added without breaking a caller. Outside this crate,
62/// build one with [`Trade::new`] and the `with_*` methods, and change a copy by assigning its
63/// public fields.
64#[non_exhaustive]
65#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Deserialize, Serialize)]
66pub struct Trade<AssetKey, InstrumentKey> {
67 pub id: TradeId,
68 pub order_id: OrderId,
69 pub instrument: InstrumentKey,
70 pub strategy: StrategyId,
71 pub time_exchange: DateTime<Utc>,
72 pub side: Side,
73 pub price: Decimal,
74 /// The size of *this* execution.
75 pub quantity: Decimal,
76 /// The order's cumulative filled quantity as of this execution, as the venue reported it --
77 /// **not** the size of this execution, which is [`Trade::quantity`].
78 ///
79 /// A fill carries two facts: that an execution happened, and what it left the order at. A
80 /// consumer that tracks order state needs both, and they arrive together or not at all. Where
81 /// the venue supplies the second, carrying it here lets the order be advanced from the fill
82 /// itself rather than from a separate message that may never come.
83 ///
84 /// Advancing from a cumulative is idempotent: re-applying the same fill, or applying two out
85 /// of order, cannot double-count, because the receiver takes the greater of what it holds and
86 /// what is reported rather than adding to a running total.
87 ///
88 /// `None` means the venue's fill payload does not carry it, and the order's state must be
89 /// learned some other way -- an order snapshot, or a reconciliation fetch. It is not a claim
90 /// that nothing is filled. Known producers, as of writing:
91 ///
92 /// | Source | Reports it |
93 /// |---|---|
94 /// | Binance Spot / Margin WebSocket `executionReport` | yes (`z`) |
95 /// | Binance Spot / Margin fill recovery after a disconnect | yes, rebuilt from the order's executions; `None` where that lookup failed or ran out of time |
96 /// | Binance Spot / Margin `fetch_trades` (`myTrades` REST) | no |
97 /// | Alpaca WebSocket `trade_updates` | yes (`order.filled_qty`) |
98 /// | Alpaca account-activities REST | yes (`cum_qty`) |
99 /// | Interactive Brokers `ExecutionData` | yes (`cumulative_quantity`) |
100 /// | Hyperliquid `userFills` | no |
101 /// | `SimulatedVenue` | yes |
102 ///
103 /// `#[serde(default)]` so trades serialised before this field existed still deserialise, as
104 /// `None`.
105 #[serde(default)]
106 pub order_filled_quantity: Option<Decimal>,
107 pub fees: AssetFees<AssetKey>,
108 /// Why the fill happened, as far as its producer can tell. See [`TradeOrigin`] for what each
109 /// venue reports.
110 ///
111 /// `#[serde(default)]` so trades serialised before this field existed still deserialise, as
112 /// [`TradeOrigin::Order`].
113 #[serde(default)]
114 pub origin: TradeOrigin,
115 /// The side of the position the fill applies to, when the venue names one: [`Side::Buy`] for
116 /// a long, [`Side::Sell`] for a short.
117 ///
118 /// A venue in hedge mode, holding a long and a short in one instrument at once, names the
119 /// position each fill belongs to (Binance USD-M futures' `LONG` and `SHORT`). The engine in
120 /// `OmsMode::Hedging` checks a forced fill, which matches none of its orders, against it: a
121 /// forced fill must reduce the position the venue names. It only ever confirms the side a
122 /// reducing fill already implies, the side opposite its own, and does not choose among
123 /// several of the engine's positions on that side.
124 ///
125 /// It takes part in the derived `Eq`, `Hash` and `Ord`, as every field does: deduplicate fills
126 /// by instrument and [`id`](Self::id), not by the whole `Trade`.
127 /// `None` when the venue names none, as every client in this crate does as of writing: none
128 /// trades a venue in hedge mode.
129 ///
130 /// `#[serde(default)]` so trades serialised before this field existed still deserialise, as
131 /// `None`.
132 #[serde(default)]
133 pub position_side: Option<Side>,
134 /// The client order id of the order this fill belongs to, as the venue reported it with the
135 /// fill.
136 ///
137 /// The id is chosen before the order is sent, so a consumer can attribute a fill by it at
138 /// once. That includes a fill of an order placed outside the engine on the same account,
139 /// which no order the engine knows matches: [`order_id`](Self::order_id) is the venue's, and
140 /// the fill can arrive before the venue's response to the order names it, or long after, in a
141 /// recovery read. A consumer that trades an account outside the engine can mark those orders'
142 /// client order ids, by a prefix say, and keep their fills from the engine.
143 ///
144 /// `None` means the fill's payload did not carry one. It is not a claim that the order has
145 /// none. Known producers, as of writing:
146 ///
147 /// | Source | Reports it |
148 /// |---|---|
149 /// | Binance Spot / Margin WebSocket `executionReport` | yes (`c`) |
150 /// | Binance Spot / Margin fill recovery after a disconnect | yes, read from each recovered fill's order; `None` where that lookup failed or ran out of time |
151 /// | Binance Spot / Margin `fetch_trades` (`myTrades` REST) | no |
152 /// | Alpaca WebSocket `trade_updates` | yes (`order.client_order_id`) |
153 /// | Alpaca account-activities REST (fill recovery, `fetch_trades`), `fetch_option_events` | no |
154 /// | Interactive Brokers executions | yes, the order reference, which this crate's client sets to the client order id; `None` for an order without one |
155 /// | Hyperliquid `userFills` | yes, the client order id the order's cloid maps to; `None` for an order placed without a cloid |
156 /// | `SimulatedVenue` | yes |
157 /// | The `rustrade` engine's expiry settlement | no |
158 ///
159 /// `#[serde(default)]` so trades serialised before this field existed still deserialise, as
160 /// `None`.
161 #[serde(default)]
162 pub client_order_id: Option<ClientOrderId>,
163}
164
165impl<AssetKey, InstrumentKey> Trade<AssetKey, InstrumentKey> {
166 /// A trade of [`TradeOrigin::Order`]. Set another origin with
167 /// [`with_origin`](Self::with_origin).
168 #[allow(clippy::too_many_arguments)] // One argument per field a fill always has.
169 pub fn new(
170 id: TradeId,
171 order_id: OrderId,
172 instrument: InstrumentKey,
173 strategy: StrategyId,
174 time_exchange: DateTime<Utc>,
175 side: Side,
176 price: Decimal,
177 quantity: Decimal,
178 order_filled_quantity: Option<Decimal>,
179 fees: AssetFees<AssetKey>,
180 ) -> Self {
181 Self {
182 id,
183 order_id,
184 instrument,
185 strategy,
186 time_exchange,
187 side,
188 price,
189 quantity,
190 order_filled_quantity,
191 fees,
192 origin: TradeOrigin::Order,
193 position_side: None,
194 client_order_id: None,
195 }
196 }
197
198 /// This trade with its [`origin`](Self::origin) set.
199 #[must_use]
200 pub fn with_origin(self, origin: TradeOrigin) -> Self {
201 Self { origin, ..self }
202 }
203
204 /// This trade with the [`position_side`](Self::position_side) the venue named.
205 #[must_use]
206 pub fn with_position_side(self, position_side: Side) -> Self {
207 Self {
208 position_side: Some(position_side),
209 ..self
210 }
211 }
212
213 /// This trade with the [`client_order_id`](Self::client_order_id) the venue reported.
214 #[must_use]
215 pub fn with_client_order_id(self, client_order_id: ClientOrderId) -> Self {
216 Self {
217 client_order_id: Some(client_order_id),
218 ..self
219 }
220 }
221
222 pub fn value_quote(&self) -> Decimal {
223 self.price * self.quantity.abs()
224 }
225
226 /// How this fill moves the position it applies to, when that is not by its own
227 /// [`side`](Self::side) and [`quantity`](Self::quantity): a spot fill whose fee the venue
228 /// charged in `instrument`'s base asset, or a `reduce_only` fill that would grow or flip the
229 /// position. `None` otherwise: the fill then moves the position by its own side and quantity.
230 ///
231 /// The `rustrade` engine applies every fill with this function. A component that mirrors
232 /// positions outside the engine, such as a ledger of the fills an execution client forwarded,
233 /// agrees with the engine by calling it too.
234 ///
235 /// `held` is the position the fill applies to, as it stands before the fill, signed: positive
236 /// for a long, negative for a short, zero for none. In an account holding a long and a short
237 /// at once, it is the one position the fill is routed to. `reduce_only` is whether the fill's
238 /// order was sent to reduce a position only, as
239 /// [`RequestOpen::reduce_only`](crate::order::request::RequestOpen::reduce_only) says.
240 ///
241 /// # The rule
242 ///
243 /// In this order:
244 ///
245 /// - **The fee is netted.** It leaves the account in base on either side, so the holding moves
246 /// by the fill's signed quantity less the fee: a buy of `q` with a base fee `f` holds
247 /// `q − f`, and a sell delivers `q + f`. A negative fee, a rebate, adds to it. Only on a
248 /// spot instrument, spot margin included. A derivative's position is counted in contracts,
249 /// and an inverse contract pays its fees in the settlement coin, so a fee there never
250 /// changes the quantity. A fee in quote or a third asset, such as BNB, and a fee of zero
251 /// are not netted.
252 /// - **A reduce-only move never grows or flips the position.** With `reduce_only`, a netted
253 /// move larger than the opposite position `held` is cut to land it flat, and one that does
254 /// not reduce it at all, because `held` is zero or on the move's side, moves nothing. What
255 /// is cut off is [`HoldingMove::excess`]. A cut move is not rounded below: flat is on any
256 /// step, and a position the fill left as it was is not the fill's to round. This applies to
257 /// any instrument kind.
258 /// - **The position is rounded toward zero to the step.** Only when the fee was netted and
259 /// the move not cut. When [`Instrument::quantity_step`] gives a step, the position the fill
260 /// leaves, `held` plus the netted move, is rounded toward zero to a whole multiple of it, and
261 /// the move is what lands the position there. A venue sells only whole steps, so a position
262 /// off the step could never be closed to flat. It is the position that is rounded, not the
263 /// move: a sell of 1 with a fee of 0.001 from a long of 2, in steps of 0.01, leaves 0.999, so
264 /// the position is 0.99 and the move a sell of 1.01. What is rounded off is
265 /// [`HoldingMove::dust`]. A `held` already off the step is rounded too, so the dust can
266 /// include a remainder this fill did not create. Without a step, the dust is zero.
267 /// - **The move's side is its sign.** A fee larger than a buy's quantity sells the difference.
268 /// A move of zero keeps the netted move's side, which is the fill's own unless the fee
269 /// reversed it, with a quantity of zero.
270 ///
271 /// # What it leaves to the caller
272 ///
273 /// - **The price and the fee.** The netted quantity keeps the fill's price, so PnL is charged
274 /// the fee's quote equivalent, its `fees_quote`: buying `q − f` at `p` and paying `f × p`
275 /// costs `q × p`, what the fill paid. The engine also adds the value of a long's dust, at
276 /// the fill's price, to the fill's fee, since the account holds that base but cannot sell
277 /// it. A short's dust is base still owed on the loan, which the position books as bought
278 /// back at the fill's price, so nothing is added.
279 /// - **Only the fee the venue reported** is netted. A fee the caller computes itself, as the
280 /// engine's fee model does, is in quote, and reaches PnL alone.
281 /// - **The excess and its share of the fee.** No position takes the excess: the account
282 /// holds it, or the venue spent it, as on a margin repayment sized to cover interest. The
283 /// engine charges the position the share of the fill's fees that the move is of the netted
284 /// move, and the rest to its tear sheet as fees no position carries. It reports the excess
285 /// without charging its value to PnL.
286 /// - **A settlement fill larger than its position, in a hedged account.** Where an account
287 /// can hold a long and a short at once (the engine's `OmsMode::Hedging`), the engine does not
288 /// call this for a settlement fill, such as a liquidation, that matches none of its orders
289 /// and is larger than the position it reduces: it cuts that fill to the position, unnetted,
290 /// so the position lands flat, and reports the excess. A mirror that matches the engine does
291 /// the same rather than calling this. Such a fill is of no order of the engine's, so it is
292 /// never reduce-only. With one position per instrument (`OmsMode::Netting`), the engine
293 /// never cuts a settlement fill, so this applies to every fill.
294 pub fn holding_move<ExchangeKey>(
295 &self,
296 instrument: &Instrument<ExchangeKey, AssetKey>,
297 held: Decimal,
298 reduce_only: bool,
299 ) -> Option<HoldingMove>
300 where
301 AssetKey: PartialEq,
302 {
303 let base_fee = matches!(instrument.kind, InstrumentKind::Spot)
304 && self.fees.asset == instrument.underlying.base
305 && !self.fees.fees.is_zero();
306 let netted = signed(self.side, self.quantity.abs())
307 - if base_fee {
308 self.fees.fees
309 } else {
310 Decimal::ZERO
311 };
312 let (netted_side, _) = unsigned(netted, self.side);
313
314 if reduce_only && let Some(moved) = reduced_by(held, netted) {
315 let (side, quantity) = unsigned(moved, netted_side);
316 return Some(HoldingMove {
317 side,
318 quantity,
319 dust: Decimal::ZERO,
320 excess: netted - moved,
321 });
322 }
323 if !base_fee {
324 return None;
325 }
326
327 let holds = held + netted;
328 let rounded = instrument
329 .quantity_step()
330 .map_or(holds, |step| round_toward_zero_to_step(holds, step));
331 let (side, quantity) = unsigned(rounded - held, netted_side);
332 Some(HoldingMove {
333 side,
334 quantity,
335 dust: holds - rounded,
336 excess: Decimal::ZERO,
337 })
338 }
339}
340
341/// How a fill moves the position it applies to, when that is not by its own side and quantity:
342/// see [`Trade::holding_move`].
343#[non_exhaustive]
344#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, Deserialize, Serialize)]
345pub struct HoldingMove {
346 /// [`Side::Buy`] if the move adds to the position, [`Side::Sell`] if it takes from it.
347 pub side: Side,
348 /// The size of the move, never negative.
349 pub quantity: Decimal,
350 /// The part of the netted holding rounded off the position to land it on the quantity step,
351 /// signed as a position is, and less than one step in size. Positive is base the account holds
352 /// that a long does not count; negative is base still owed on a short's loan that the short
353 /// does not count. Zero without a step, or when the position lands on it.
354 ///
355 /// It stays in the account's balance. Each fill's is rounded off on its own, so the balance can
356 /// gather more than one step of it over many fills, which no position holds.
357 pub dust: Decimal,
358 /// The part of a reduce-only fill's netted move cut off so it does not grow or flip the
359 /// position, signed as a move is: positive for a buy. Zero unless the move was cut. No
360 /// position takes it.
361 pub excess: Decimal,
362}
363
364/// `quantity` with the sign of `side`: positive for a buy, or for a long position.
365fn signed(side: Side, quantity: Decimal) -> Decimal {
366 match side {
367 Side::Buy => quantity,
368 Side::Sell => -quantity,
369 }
370}
371
372/// The side and size of a signed move: a buy if positive, a sell if negative, and `at_zero`
373/// with a size of zero if it moves nothing.
374fn unsigned(moved: Decimal, at_zero: Side) -> (Side, Decimal) {
375 let side = match moved.cmp(&Decimal::ZERO) {
376 std::cmp::Ordering::Greater => Side::Buy,
377 std::cmp::Ordering::Less => Side::Sell,
378 std::cmp::Ordering::Equal => at_zero,
379 };
380 (side, moved.abs())
381}
382
383/// The part of a reduce-only signed move `moved` that the signed position `held` takes, when that is
384/// less than all of it: nothing unless the move reduces `held`, and no more than lands `held`
385/// flat. `None` when `held` takes all of it.
386fn reduced_by(held: Decimal, moved: Decimal) -> Option<Decimal> {
387 // Opposite signs reduce. A flat `held` has no sign to oppose, and a zero move takes nothing.
388 if held.cmp(&Decimal::ZERO) != moved.cmp(&Decimal::ZERO).reverse() {
389 return (!moved.is_zero()).then_some(Decimal::ZERO);
390 }
391 (moved.abs() > held.abs()).then_some(-held)
392}
393
394/// What one [`ExecutionClient::fetch_trades`](crate::client::ExecutionClient::fetch_trades) call
395/// read of the span it was asked for.
396///
397/// A venue reads a span in bounded calls, so one call may stop before the span's end. `resume`
398/// says whether this one did:
399/// - `None`: every fill in the span the venue still holds is in `trades`.
400/// - `Some(t)`: the call stopped at its bound, and fills from `t` on may be missing from
401/// `trades`. Read on from `start = t` with the same end. That read can return again fills this
402/// one returned at or after `t`, so match them by instrument and [`TradeId`].
403///
404/// `trades` can be empty while `resume` is `Some`: a venue that reads every instrument's fills
405/// and filters them afterwards can spend a whole call on other instruments'. A caller that takes
406/// `trades` without reading on from `resume` can miss fills.
407///
408/// A venue also keeps fills for a limited time or count. `held_since` says when that limit cuts
409/// into the span, so a caller can tell fills the venue no longer has from fills that never
410/// happened.
411#[non_exhaustive]
412#[must_use = "`resume` says whether the span was read to its end"]
413#[derive(Debug, Clone, Eq, PartialEq, Hash, Deserialize, Serialize)]
414pub struct TradesRead<AssetKey, InstrumentKey> {
415 /// The fills read, in the span and for the requested instruments.
416 pub trades: Vec<Trade<AssetKey, InstrumentKey>>,
417 /// Where to read on from, or `None` when the read reached the span's end.
418 pub resume: Option<DateTime<Utc>>,
419 /// The earliest moment the venue still holds fills from, when that is inside the span; fills
420 /// before it may exist but cannot be read. `None` when the venue's limit is not known to cut
421 /// into the span, which is no claim that the venue holds fills without limit.
422 ///
423 /// A caller that needs every fill in the span treats the part before `held_since` as a gap:
424 /// `resume: None` covers what the venue holds, not what it held. The limit cuts into the span
425 /// from its start, so only the first call of a span can report it; a caller reading on from
426 /// `resume` has nothing to learn from a later call's.
427 pub held_since: Option<DateTime<Utc>>,
428}
429
430impl<AssetKey, InstrumentKey> TradesRead<AssetKey, InstrumentKey> {
431 /// A read that stopped at `resume` (`None`: it reached the span's end), with no venue limit
432 /// cutting into the span.
433 pub fn new(trades: Vec<Trade<AssetKey, InstrumentKey>>, resume: Option<DateTime<Utc>>) -> Self {
434 Self {
435 trades,
436 resume,
437 held_since: None,
438 }
439 }
440
441 /// A read that reached the span's end.
442 pub fn complete(trades: Vec<Trade<AssetKey, InstrumentKey>>) -> Self {
443 Self::new(trades, None)
444 }
445
446 /// Sets [`held_since`](Self::held_since).
447 pub fn with_held_since(mut self, held_since: Option<DateTime<Utc>>) -> Self {
448 self.held_since = held_since;
449 self
450 }
451}
452
453impl<AssetKey, InstrumentKey> Display for Trade<AssetKey, InstrumentKey>
454where
455 AssetKey: Display,
456 InstrumentKey: Display,
457{
458 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
459 write!(
460 f,
461 "{{ instrument: {}, side: {}, price: {}, quantity: {}, time: {}",
462 self.instrument, self.side, self.price, self.quantity, self.time_exchange
463 )?;
464 // Shown only when it is not the default, so a forced fill stands out in a log line.
465 if self.origin != TradeOrigin::Order {
466 write!(f, ", origin: {}", self.origin)?;
467 }
468 write!(f, " }}")
469 }
470}
471
472/// Why a [`Trade`] happened, as far as its producer can tell.
473///
474/// # `Order` is the default, not a guarantee
475///
476/// [`Order`](Self::Order) means the producer reported no other origin. Most venues mark some
477/// origins and not others, so a fill can be `Order` and still be forced, or still be for an order
478/// this client did not place. What each producer reports, as of writing:
479///
480/// | Source | Reports |
481/// |---|---|
482/// | Interactive Brokers | [`Liquidation`](Self::Liquidation), [`External`](Self::External) (opt-in), `Order` |
483/// | Binance Spot / Margin | `Order` only |
484/// | Hyperliquid | `Order` only |
485/// | Alpaca | `Order` on fills; [`Expiry`](Self::Expiry), [`Exercise`](Self::Exercise), [`Assignment`](Self::Assignment) and [`External`](Self::External) from `AlpacaClient::fetch_option_events` |
486/// | [`SimulatedVenue`](crate::exchange::mock::SimulatedVenue) | `Order` only |
487/// | The `rustrade` engine's expiry settlement | [`Expiry`](Self::Expiry) |
488///
489/// - **Interactive Brokers** flags a fill of an IB-initiated liquidation. Another API client's
490/// fill is reported, as `External`, only when `IbkrConfig::other_clients_fills` opts in.
491/// - **Binance** marks no forced liquidation in its Spot or Margin order stream: its liquidation
492/// markers are documented for futures only. Every fill on the account arrives, whoever placed
493/// its order. `BinanceMargin::fetch_forced_liquidations` reads the margin orders Binance placed
494/// to liquidate the account, for a caller to match against its fills.
495/// - **Hyperliquid** fills carry a liquidation marker, but the SDK this crate uses drops it.
496/// - **Alpaca** marks no liquidation. Option assignment, exercise and expiry are account
497/// activities, which this crate does not read.
498/// - The **simulated venue** models no liquidation.
499///
500/// A forced variant takes precedence over [`External`](Self::External): a liquidation is
501/// reported as a liquidation whoever placed the order it closed.
502///
503/// # Serialised form and ordering
504///
505/// Serialised as the variant's name (`"Liquidation"`), as [`Side`] is, while `Display` writes it
506/// in lower case (`liquidation`). The names are a stored format: they are not renamed, and new
507/// variants are added after the existing ones. A trade with an origin a build does not know does
508/// not deserialise in that build.
509///
510/// `Ord` follows declaration order. It exists so [`Trade`] can derive `Ord`, and carries no
511/// meaning beyond that.
512#[non_exhaustive]
513#[derive(
514 Debug, Copy, Clone, Default, Eq, PartialEq, Ord, PartialOrd, Hash, Deserialize, Serialize,
515)]
516pub enum TradeOrigin {
517 /// A fill of an order, with no other origin reported. See the type's docs for why this is not
518 /// a guarantee that this client placed the order.
519 #[default]
520 Order,
521 /// The venue or broker closed the position because the account fell short of margin.
522 Liquidation,
523 /// The venue reduced the position against a counterparty it was liquidating
524 /// (auto-deleveraging).
525 Adl,
526 /// An option this account was short was assigned.
527 Assignment,
528 /// An option this account held was exercised, by the holder or automatically.
529 Exercise,
530 /// The instrument expired and the position was settled.
531 Expiry,
532 /// A fill of an order that another client, such as another API connection or the venue's own
533 /// trading screen, placed on the same account.
534 External,
535}
536
537impl TradeOrigin {
538 /// Whether the position changed without the account placing an order for it: a
539 /// [`Liquidation`](Self::Liquidation), an [`Adl`](Self::Adl) or an
540 /// [`Assignment`](Self::Assignment).
541 ///
542 /// An [`Exercise`](Self::Exercise) is the holder's right, and an [`Expiry`](Self::Expiry) is
543 /// the instrument's end, so neither counts.
544 pub fn is_forced(self) -> bool {
545 matches!(self, Self::Liquidation | Self::Adl | Self::Assignment)
546 }
547}
548
549impl Display for TradeOrigin {
550 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
551 f.write_str(match self {
552 Self::Order => "order",
553 Self::Liquidation => "liquidation",
554 Self::Adl => "adl",
555 Self::Assignment => "assignment",
556 Self::Exercise => "exercise",
557 Self::Expiry => "expiry",
558 Self::External => "external",
559 })
560 }
561}
562
563/// A venue's notice that a trade it reported earlier was busted or corrected.
564///
565/// Sent as [`AccountEventKind::TradeAmended`](crate::AccountEventKind::TradeAmended). The
566/// [`Trade`] it amends was delivered as reported and is not withdrawn: positions, PnL and fees
567/// built on it stay wrong until the consumer applies this. Find it by instrument and
568/// [`original`](Self::original), reverse it, and apply the [`kind`](Self::kind)'s replacement, if
569/// any. The library does not act on it beyond reporting it, and the engine only logs it.
570///
571/// A replacement can itself be amended later. That amendment names the replacement's id as its
572/// `original`, so follow amendments by id, one at a time.
573///
574/// # Known limitations
575///
576/// - An order's cumulative filled quantity is not lowered by a bust:
577/// [`Open::is_superseded_by`](crate::order::state::Open::is_superseded_by) refuses a lower
578/// cumulative, so the order stays on what it reported before.
579/// - Alpaca's fill recovery after a reconnect reads fills only, so an Alpaca amendment sent while
580/// the stream was disconnected is not reported. IBKR's recovery reads corrections too.
581///
582/// # Delivery
583///
584/// Apply an amendment idempotently: reverse a given `original` once, and apply a replacement
585/// once by its [`TradeId`]. Whether a venue can send one amendment twice, such as around a
586/// reconnect, is not known for every producer. The library delivers each IBKR correction once
587/// per account stream, but does not deduplicate Alpaca's amendments.
588///
589/// Known producers, as of writing:
590/// - Alpaca's `trade_updates` `trade_bust` and `trade_correct`;
591/// - IBKR's corrected executions, as [`Corrected`](TradeAmendmentKind::Corrected) only. IB
592/// documents no busts.
593#[non_exhaustive]
594#[derive(Debug, Clone, Eq, PartialEq, Hash, Deserialize, Serialize, Constructor)]
595pub struct TradeAmendment<AssetKey, InstrumentKey> {
596 /// The instrument of the trade amended.
597 pub instrument: InstrumentKey,
598 /// The order whose trade was amended.
599 pub order_id: OrderId,
600 /// When the venue amended the trade, or when it was received where the venue does not say.
601 pub time_exchange: DateTime<Utc>,
602 /// The [`TradeId`] of the trade amended, as the venue named it. `None` when the venue's notice
603 /// did not name it: match it to a trade of [`order_id`](Self::order_id) yourself, or
604 /// reconcile with
605 /// [`ExecutionClient::fetch_trades`](crate::client::ExecutionClient::fetch_trades).
606 pub original: Option<TradeId>,
607 /// What the venue did to the trade.
608 pub kind: TradeAmendmentKind<AssetKey, InstrumentKey>,
609}
610
611/// What a [`TradeAmendment`] did to the trade.
612#[non_exhaustive]
613#[derive(Debug, Clone, Eq, PartialEq, Hash, Deserialize, Serialize)]
614pub enum TradeAmendmentKind<AssetKey, InstrumentKey> {
615 /// The trade was cancelled: it did not happen.
616 Busted {
617 /// The quantity busted, as a magnitude, when the venue said. A venue that reports it as
618 /// a negative reversal is read as its absolute value.
619 quantity: Option<Decimal>,
620 },
621 /// The trade was replaced, in full.
622 Corrected {
623 /// The trade as corrected: its price and quantity are the corrected values, not
624 /// differences from the original's. It has its own [`TradeId`], which a later amendment
625 /// of it names.
626 replacement: Trade<AssetKey, InstrumentKey>,
627 },
628 /// The trade was corrected, but the venue's notice lacked what a replacement [`Trade`] needs.
629 /// What it did carry is here. Reconcile the trade with
630 /// [`ExecutionClient::fetch_trades`](crate::client::ExecutionClient::fetch_trades).
631 CorrectedUnresolved {
632 /// The id of the replacement, which a later amendment of it names.
633 id: Option<TradeId>,
634 /// The corrected price, if the notice had one.
635 price: Option<Decimal>,
636 /// The corrected quantity, if the notice had one.
637 quantity: Option<Decimal>,
638 },
639}
640
641#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Deserialize, Serialize)]
642pub struct AssetFees<AssetKey> {
643 pub asset: AssetKey,
644 pub fees: Decimal,
645 /// Fee value in quote currency when computable.
646 /// - `Some(fees)` if fee asset == quote asset
647 /// - `Some(fees * price)` if fee asset == base asset (computed by indexer)
648 /// - `None` if fee asset is third-party (e.g., BNB), which needs a price the indexer does not
649 /// have. The `rustrade` engine fills it in when it prices that asset, before applying the
650 /// fill (see its `EngineState::update_from_account`). Also `None` when the indexer zeroed a
651 /// fee whose asset was not in its map (see
652 /// [`AccountEventIndexer::trade`](crate::indexer::AccountEventIndexer::trade)).
653 ///
654 /// The engine's position PnL counts only this quote equivalent. A fee with `None` is left
655 /// out of PnL, and its raw [`fees`](Self::fees) stays recorded on the position. On a
656 /// position's running fee totals, this is the sum of the quote equivalents its fees had, and
657 /// `None` only while none had one.
658 #[serde(default, skip_serializing_if = "Option::is_none")]
659 pub fees_quote: Option<Decimal>,
660}
661
662impl<AssetKey> AssetFees<AssetKey> {
663 pub fn new(asset: AssetKey, fees: Decimal, fees_quote: Option<Decimal>) -> Self {
664 Self {
665 asset,
666 fees,
667 fees_quote,
668 }
669 }
670}
671
672impl AssetFees<QuoteAsset> {
673 /// Construct fees already denominated in quote asset.
674 /// Sets `fees_quote = Some(fees)` since no conversion needed.
675 pub fn quote_fees(fees: Decimal) -> Self {
676 Self {
677 asset: QuoteAsset,
678 fees,
679 fees_quote: Some(fees),
680 }
681 }
682}
683
684impl Default for AssetFees<QuoteAsset> {
685 fn default() -> Self {
686 Self {
687 asset: QuoteAsset,
688 fees: Decimal::ZERO,
689 fees_quote: Some(Decimal::ZERO),
690 }
691 }
692}
693
694impl<AssetKey> Default for AssetFees<Option<AssetKey>> {
695 fn default() -> Self {
696 Self {
697 asset: None,
698 fees: Decimal::ZERO,
699 fees_quote: None,
700 }
701 }
702}
703
704#[cfg(test)]
705#[allow(clippy::unwrap_used)] // Test code: panics on bad input are acceptable
706mod tests {
707 use super::*;
708 use rust_decimal_macros::dec;
709 use rustrade_instrument::{
710 asset::{Asset, name::AssetNameExchange},
711 exchange::ExchangeId,
712 instrument::{
713 kind::perpetual::PerpetualContract,
714 name::InstrumentNameExchange,
715 spec::{
716 InstrumentSpec, InstrumentSpecNotional, InstrumentSpecPrice,
717 InstrumentSpecQuantity, OrderQuantityUnits,
718 },
719 },
720 test_utils,
721 };
722
723 fn trade() -> Trade<AssetNameExchange, InstrumentNameExchange> {
724 Trade::new(
725 TradeId::new("t-1"),
726 OrderId::new("o-1"),
727 InstrumentNameExchange::new("btc_usdt"),
728 StrategyId::unknown(),
729 DateTime::<Utc>::MIN_UTC,
730 Side::Sell,
731 Decimal::ONE,
732 Decimal::TWO,
733 None,
734 AssetFees::new(AssetNameExchange::from("usdt"), Decimal::ZERO, None),
735 )
736 }
737
738 #[test]
739 fn a_trade_is_of_an_order_unless_given_another_origin() {
740 assert_eq!(trade().origin, TradeOrigin::Order);
741 assert_eq!(
742 trade().with_origin(TradeOrigin::Adl).origin,
743 TradeOrigin::Adl
744 );
745 }
746
747 /// A trade serialised before `origin` existed still loads, as an order's.
748 #[test]
749 fn a_trade_without_an_origin_deserialises_as_an_orders() {
750 let mut json = serde_json::to_value(trade().with_origin(TradeOrigin::External)).unwrap();
751 assert_eq!(json["origin"], "External");
752 json.as_object_mut().unwrap().remove("origin");
753
754 let read: Trade<AssetNameExchange, InstrumentNameExchange> =
755 serde_json::from_value(json).unwrap();
756 assert_eq!(read, trade());
757 }
758
759 #[test]
760 fn display_names_the_origin_only_when_it_is_not_an_orders() {
761 assert!(!trade().to_string().contains("origin"));
762 assert!(
763 trade()
764 .with_origin(TradeOrigin::Liquidation)
765 .to_string()
766 .ends_with(", origin: liquidation }")
767 );
768 }
769
770 // --- holding_move ------------------------------------------------------------------------
771
772 /// A spot BTC/USDT instrument, with a quantity step in BTC when `step` is given.
773 fn btc_usdt(step: Option<Decimal>) -> Instrument<ExchangeId, Asset> {
774 let mut instrument = test_utils::instrument(ExchangeId::BinanceSpot, "btc", "usdt");
775 instrument.spec = step.map(|step| {
776 InstrumentSpec::new(
777 InstrumentSpecPrice::new(dec!(0.01), dec!(0.01)),
778 InstrumentSpecQuantity::new(
779 OrderQuantityUnits::Asset(test_utils::asset("btc")),
780 step,
781 step,
782 ),
783 InstrumentSpecNotional::new(Decimal::ZERO),
784 )
785 });
786 instrument
787 }
788
789 /// A fill of `quantity` on `side`, charged `fee` in `fee_asset`.
790 fn fill(side: Side, quantity: Decimal, fee_asset: &str, fee: Decimal) -> Trade<Asset, ()> {
791 Trade::new(
792 TradeId::new("t-1"),
793 OrderId::new("o-1"),
794 (),
795 StrategyId::unknown(),
796 DateTime::<Utc>::MIN_UTC,
797 side,
798 dec!(100),
799 quantity,
800 None,
801 AssetFees::new(test_utils::asset(fee_asset), fee, None),
802 )
803 }
804
805 fn moved(side: Side, quantity: Decimal, dust: Decimal) -> Option<HoldingMove> {
806 Some(HoldingMove {
807 side,
808 quantity,
809 dust,
810 excess: Decimal::ZERO,
811 })
812 }
813
814 fn cut(side: Side, quantity: Decimal, excess: Decimal) -> Option<HoldingMove> {
815 Some(HoldingMove {
816 side,
817 quantity,
818 dust: Decimal::ZERO,
819 excess,
820 })
821 }
822
823 #[test]
824 fn holding_move_is_none_unless_a_spot_fill_pays_a_base_fee() {
825 let mut perpetual = btc_usdt(None);
826 perpetual.kind = InstrumentKind::Perpetual(PerpetualContract {
827 contract_size: Decimal::ONE,
828 settlement_asset: test_utils::asset("btc"),
829 });
830 let cases = [
831 (
832 "quote fee",
833 btc_usdt(None),
834 fill(Side::Buy, dec!(1), "usdt", dec!(0.1)),
835 ),
836 (
837 "third-asset fee",
838 btc_usdt(None),
839 fill(Side::Buy, dec!(1), "bnb", dec!(0.001)),
840 ),
841 (
842 "zero base fee",
843 btc_usdt(None),
844 fill(Side::Buy, dec!(1), "btc", Decimal::ZERO),
845 ),
846 (
847 "derivative",
848 perpetual,
849 fill(Side::Buy, dec!(1), "btc", dec!(0.001)),
850 ),
851 ];
852
853 for (case, instrument, fill) in cases {
854 assert_eq!(
855 fill.holding_move(&instrument, Decimal::ZERO, false),
856 None,
857 "{case}"
858 );
859 }
860 }
861
862 #[test]
863 fn holding_move_nets_a_base_fee_on_either_side() {
864 let spot = btc_usdt(None);
865 let cases = [
866 (
867 "buy holds less",
868 fill(Side::Buy, dec!(1), "btc", dec!(0.001)),
869 moved(Side::Buy, dec!(0.999), Decimal::ZERO),
870 ),
871 (
872 "sell delivers more",
873 fill(Side::Sell, dec!(1), "btc", dec!(0.001)),
874 moved(Side::Sell, dec!(1.001), Decimal::ZERO),
875 ),
876 (
877 "rebate adds",
878 fill(Side::Buy, dec!(1), "btc", dec!(-0.001)),
879 moved(Side::Buy, dec!(1.001), Decimal::ZERO),
880 ),
881 (
882 "fee above a buy sells",
883 fill(Side::Buy, dec!(0.001), "btc", dec!(0.003)),
884 moved(Side::Sell, dec!(0.002), Decimal::ZERO),
885 ),
886 (
887 "fee equal to a buy moves nothing",
888 fill(Side::Buy, dec!(0.001), "btc", dec!(0.001)),
889 moved(Side::Buy, Decimal::ZERO, Decimal::ZERO),
890 ),
891 ];
892
893 for (case, fill, expected) in cases {
894 assert_eq!(fill.holding_move(&spot, dec!(2), false), expected, "{case}");
895 }
896 }
897
898 /// The position `held` plus the netted move is what is rounded, toward zero, and the move is
899 /// what lands it there.
900 #[test]
901 fn holding_move_rounds_the_position_toward_zero_to_the_step() {
902 let spot = btc_usdt(Some(dec!(0.01)));
903 let cases = [
904 (
905 "buy from flat",
906 Decimal::ZERO,
907 fill(Side::Buy, dec!(1), "btc", dec!(0.001)),
908 moved(Side::Buy, dec!(0.99), dec!(0.009)),
909 ),
910 // Rounding the move of 1.001 instead would leave a long of 1, more than the 0.999 held.
911 (
912 "sell from a long",
913 dec!(2),
914 fill(Side::Sell, dec!(1), "btc", dec!(0.001)),
915 moved(Side::Sell, dec!(1.01), dec!(0.009)),
916 ),
917 // The fee leaves a short of 0.001, rounded away: base still owed, which no short counts.
918 (
919 "buy back a short",
920 dec!(-1),
921 fill(Side::Buy, dec!(1), "btc", dec!(0.001)),
922 moved(Side::Buy, dec!(1), dec!(-0.001)),
923 ),
924 (
925 "sell opening a short",
926 Decimal::ZERO,
927 fill(Side::Sell, dec!(1), "btc", dec!(0.001)),
928 moved(Side::Sell, dec!(1), dec!(-0.001)),
929 ),
930 (
931 "held off the step",
932 dec!(0.005),
933 fill(Side::Buy, dec!(1), "btc", dec!(0.001)),
934 moved(Side::Buy, dec!(0.995), dec!(0.004)),
935 ),
936 (
937 "lands on the step",
938 dec!(0.001),
939 fill(Side::Buy, dec!(1), "btc", dec!(0.001)),
940 moved(Side::Buy, dec!(0.999), Decimal::ZERO),
941 ),
942 ];
943
944 for (case, held, fill, expected) in cases {
945 assert_eq!(fill.holding_move(&spot, held, false), expected, "{case}");
946 }
947 }
948
949 /// A rounded move of zero keeps the netted move's side: the fill's own, unless the fee
950 /// reversed it.
951 #[test]
952 fn holding_move_rounded_to_nothing_keeps_the_netted_side() {
953 let spot = btc_usdt(Some(dec!(1)));
954 let cases = [
955 (
956 "buy",
957 dec!(1),
958 fill(Side::Buy, dec!(0.5), "btc", dec!(0.001)),
959 moved(Side::Buy, Decimal::ZERO, dec!(0.499)),
960 ),
961 (
962 "fee reverses the buy",
963 dec!(-1),
964 fill(Side::Buy, dec!(0.001), "btc", dec!(0.003)),
965 moved(Side::Sell, Decimal::ZERO, dec!(-0.002)),
966 ),
967 ];
968
969 for (case, held, fill, expected) in cases {
970 assert_eq!(fill.holding_move(&spot, held, false), expected, "{case}");
971 }
972 }
973
974 /// A reduce-only move larger than the opposite position is cut to land it flat; one that
975 /// reduces nothing moves nothing. Either way the cut is the excess, and nothing is rounded.
976 #[test]
977 fn holding_move_cuts_a_reduce_only_move_at_the_position() {
978 let spot = btc_usdt(Some(dec!(0.01)));
979 let cases = [
980 // A repayment buy sized above the short: netted to 10.49, cut at 10.
981 (
982 "buy past a short",
983 dec!(-10),
984 fill(Side::Buy, dec!(10.5), "btc", dec!(0.01)),
985 cut(Side::Buy, dec!(10), dec!(0.49)),
986 ),
987 (
988 "sell past a long",
989 dec!(1),
990 fill(Side::Sell, dec!(1.5), "usdt", dec!(0.1)),
991 cut(Side::Sell, dec!(1), dec!(-0.5)),
992 ),
993 (
994 "buy from flat",
995 Decimal::ZERO,
996 fill(Side::Buy, dec!(1), "usdt", dec!(0.1)),
997 cut(Side::Buy, Decimal::ZERO, dec!(1)),
998 ),
999 (
1000 "buy onto a long",
1001 dec!(1),
1002 fill(Side::Buy, dec!(1), "btc", dec!(0.001)),
1003 cut(Side::Buy, Decimal::ZERO, dec!(0.999)),
1004 ),
1005 // The fee reverses the buy into a sell, which a short does not reduce.
1006 (
1007 "fee reverses a buy onto a short",
1008 dec!(-1),
1009 fill(Side::Buy, dec!(0.001), "btc", dec!(0.003)),
1010 cut(Side::Sell, Decimal::ZERO, dec!(-0.002)),
1011 ),
1012 ];
1013
1014 for (case, held, fill, expected) in cases {
1015 assert_eq!(fill.holding_move(&spot, held, true), expected, "{case}");
1016 }
1017 }
1018
1019 /// A reduce-only move the position takes whole is moved as any other fill: rounded when its
1020 /// base fee is netted, and `None` when it is not.
1021 #[test]
1022 fn holding_move_leaves_a_reduce_only_move_the_position_takes() {
1023 let spot = btc_usdt(Some(dec!(0.01)));
1024 let cases = [
1025 (
1026 "partial, base fee",
1027 dec!(-2),
1028 fill(Side::Buy, dec!(1), "btc", dec!(0.001)),
1029 moved(Side::Buy, dec!(1), dec!(-0.001)),
1030 ),
1031 (
1032 "to flat exactly, base fee",
1033 dec!(-0.999),
1034 fill(Side::Buy, dec!(1), "btc", dec!(0.001)),
1035 moved(Side::Buy, dec!(0.999), Decimal::ZERO),
1036 ),
1037 (
1038 "partial, rounded",
1039 dec!(2),
1040 fill(Side::Sell, dec!(1), "btc", dec!(0.001)),
1041 moved(Side::Sell, dec!(1.01), dec!(0.009)),
1042 ),
1043 (
1044 "to flat exactly, quote fee",
1045 dec!(1),
1046 fill(Side::Sell, dec!(1), "usdt", dec!(0.1)),
1047 None,
1048 ),
1049 (
1050 "fee equal to a buy moves nothing",
1051 dec!(-1),
1052 fill(Side::Buy, dec!(0.001), "btc", dec!(0.001)),
1053 moved(Side::Buy, Decimal::ZERO, Decimal::ZERO),
1054 ),
1055 // Nothing to cut, so a short already off the step is still rounded.
1056 (
1057 "fee equal to a buy, held off the step",
1058 dec!(-1.005),
1059 fill(Side::Buy, dec!(0.001), "btc", dec!(0.001)),
1060 moved(Side::Buy, dec!(0.005), dec!(-0.005)),
1061 ),
1062 ];
1063
1064 for (case, held, fill, expected) in cases {
1065 assert_eq!(fill.holding_move(&spot, held, true), expected, "{case}");
1066 }
1067 }
1068
1069 /// The cut applies to any instrument kind, where no fee is netted.
1070 #[test]
1071 fn holding_move_cuts_a_reduce_only_derivative_fill() {
1072 let mut perpetual = btc_usdt(None);
1073 perpetual.kind = InstrumentKind::Perpetual(PerpetualContract {
1074 contract_size: Decimal::ONE,
1075 settlement_asset: test_utils::asset("btc"),
1076 });
1077 let fill = fill(Side::Buy, dec!(3), "btc", dec!(0.001));
1078
1079 assert_eq!(
1080 fill.holding_move(&perpetual, dec!(-2), true),
1081 cut(Side::Buy, dec!(2), dec!(1))
1082 );
1083 assert_eq!(fill.holding_move(&perpetual, dec!(-3), true), None);
1084 assert_eq!(fill.holding_move(&perpetual, dec!(-2), false), None);
1085 }
1086
1087 #[test]
1088 fn only_liquidation_adl_and_assignment_are_forced() {
1089 let forced: Vec<_> = [
1090 TradeOrigin::Order,
1091 TradeOrigin::Liquidation,
1092 TradeOrigin::Adl,
1093 TradeOrigin::Assignment,
1094 TradeOrigin::Exercise,
1095 TradeOrigin::Expiry,
1096 TradeOrigin::External,
1097 ]
1098 .into_iter()
1099 .filter(|origin| origin.is_forced())
1100 .collect();
1101 assert_eq!(
1102 forced,
1103 [
1104 TradeOrigin::Liquidation,
1105 TradeOrigin::Adl,
1106 TradeOrigin::Assignment
1107 ]
1108 );
1109 }
1110}