FastMM documentation
FastMM C++ API 0.6.0
Public headers, tier 1 and tier 2
Loading...
Searching...
No Matches
order_events.hpp
Go to the documentation of this file.
1#pragma once
2// Helpers that build the normalised order/connection events a connector emits from its
3// control logic (WebSocket API responses, REST replies, local rejects). Each constructs the
4// message on the stack and copies it into the order EventSink (fixed-size messages only, so
5// hdr.len == sizeof(M)).
10
11#include <x86intrin.h>
12
13#include <cstdint>
14#include <string_view>
15
16namespace fastmm::venues {
17
18inline void emit_order_reject(EventSink& sink,
19 VenueId venue,
22 RejectReason reason,
23 int code,
24 std::string_view text) noexcept {
26 init_header(m, EventType::OrderReject, inst, venue);
27 m.cl_ord_id = id;
28 m.reason = reason;
29 m.venue_code = code;
30 m.text.assign(text); // truncates to capacity
31 m.hdr.recv_ts = wall_now();
32 m.hdr.t0_cycles = rdtscp();
33 static_cast<void>(sink.push(m.hdr));
34}
35
36inline void emit_cancel_reject(EventSink& sink,
37 VenueId venue,
40 RejectReason reason,
41 int code,
42 std::string_view text) noexcept {
45 m.cl_ord_id = id;
46 m.reason = reason;
47 m.venue_code = code;
48 m.text.assign(text); // truncates to capacity
49 m.hdr.recv_ts = wall_now();
50 m.hdr.t0_cycles = rdtscp();
51 static_cast<void>(sink.push(m.hdr));
52}
53
54// `flags`: OrderAckMsg::kAmendedInPlace when the venue amended the resting order rather than
55// creating a new one. `venue_ts`: a venue time the response carries, stamped as hdr.exch_ts
56// (invalid: unstamped). The balance estimate tells from it whether a report that came first
57// already had the order (core/balance_book.hpp), so it must not be earlier than the order's
58// effect on the balance: a later one at worst leaves the order held twice until the next report,
59// an earlier one could give back a hold the report did not have.
60inline void emit_order_ack(EventSink& sink,
61 VenueId venue,
64 std::string_view venue_order_id,
65 std::uint8_t flags = 0,
66 Timestamp venue_ts = Timestamp{}) noexcept {
67 OrderAckMsg m{};
68 init_header(m, EventType::OrderAck, inst, venue);
69 m.cl_ord_id = id;
70 m.venue_order_id.assign(venue_order_id);
71 m.flags = flags;
72 m.hdr.exch_ts = venue_ts;
73 m.hdr.recv_ts = wall_now();
74 m.hdr.t0_cycles = rdtscp();
75 static_cast<void>(sink.push(m.hdr));
76}
77
78// `venue_ts` as for emit_order_ack.
79inline void emit_cancel_ack(EventSink& sink,
80 VenueId venue,
83 std::string_view venue_order_id,
84 Qty cum_qty,
85 Timestamp venue_ts = Timestamp{}) noexcept {
88 m.cl_ord_id = id;
89 m.venue_order_id.assign(venue_order_id);
90 m.cum_qty = cum_qty;
91 m.hdr.exch_ts = venue_ts;
92 m.hdr.recv_ts = wall_now();
93 m.hdr.t0_cycles = rdtscp();
94 static_cast<void>(sink.push(m.hdr));
95}
96
97// An execution from the venue's trade history (Venue::request_executions), not from the private
98// stream. It carries the venue's execution id, which is what lets the OMS drop the ones it already
99// booked, and no cumulative quantity: the venue's trade history does not report one, so the OMS
100// works out how much of the execution is new. `cl_ord_id` may be empty when the connector cannot
101// map the venue's order id back to one of its own (a restarted session): the fill still reaches the
102// position, as an unknown fill. `trade_time_ms` is the venue's time of the trade (Unix ms), stamped
103// as hdr.exch_ts: fastmm-gateway tells from it whether an execution naming no live order is older
104// than what a strategy's store holds.
105inline void emit_replayed_fill(EventSink& sink,
106 VenueId venue,
108 ClientOrderId id,
109 std::string_view venue_order_id,
110 std::string_view exec_id,
111 Side side,
112 Price price,
113 Qty qty,
114 Notional fee,
115 FeeAsset fee_asset,
116 Liquidity liquidity,
117 std::int64_t trade_time_ms,
118 std::uint8_t extra_flags = 0) noexcept {
119 OrderFillMsg m{};
120 init_header(m, EventType::OrderFill, inst, venue);
121 m.hdr.exch_ts = Timestamp{trade_time_ms * 1'000'000}; // the venue's time of the trade
122 m.cl_ord_id = id;
123 m.venue_order_id.assign(venue_order_id);
124 m.exec_id.assign(exec_id);
125 m.price = price;
126 m.qty = qty;
127 m.fee = fee;
128 m.side = side;
129 m.liquidity = liquidity;
130 m.fee_asset = fee_asset;
131 m.flags = static_cast<std::uint8_t>(OrderFillMsg::kReplayed | extra_flags);
132 m.hdr.recv_ts = wall_now();
133 m.hdr.t0_cycles = rdtscp();
134 static_cast<void>(sink.push(m.hdr));
135}
136
137// A funding payment (FundingMsg): `amount` signed in `asset`, negative paid. `replayed` when it
138// comes from the venue's history rather than its private stream.
139inline void emit_funding(EventSink& sink,
140 VenueId venue,
142 std::string_view funding_id,
143 Notional amount,
144 std::string_view asset,
145 std::int64_t time_ms,
146 bool replayed) noexcept {
147 FundingMsg m{};
148 init_header(m, EventType::Funding, inst, venue);
149 m.hdr.exch_ts = Timestamp{time_ms * 1'000'000};
150 m.amount = amount;
151 m.funding_id.assign(funding_id);
152 m.asset.assign(asset);
153 m.flags = replayed ? FundingMsg::kReplayed : std::uint8_t{0};
154 m.hdr.recv_ts = wall_now();
155 m.hdr.t0_cycles = rdtscp();
156 static_cast<void>(sink.push(m.hdr));
157}
158
159// The connector cannot trade on this venue any more (error map HardStop / Fatal, failed
160// authentication): asks the engine to trip this venue's kill switch only
161// (ControlCommand::TripVenueKill, the reason in `arg`). Travels with the order events, so it is
162// journaled and replayed like them. Connectors send it once, on the first such error.
163inline void emit_venue_kill(EventSink& sink, VenueId venue, KillReason reason) noexcept {
164 ControlMsg m{};
167 m.arg = static_cast<std::uint64_t>(reason);
168 m.hdr.recv_ts = wall_now();
169 m.hdr.t0_cycles = rdtscp();
170 static_cast<void>(sink.push(m.hdr));
171}
172
173// channel: 0 = market data, 1 = order/user stream (ConnectionStateMsg contract).
174inline void emit_connection_state(EventSink& sink,
175 VenueId venue,
176 std::uint8_t channel,
177 ConnState state,
178 std::int32_t reason = 0) noexcept {
181 m.state = state;
182 m.channel = channel;
183 m.reason_code = reason;
184 m.hdr.recv_ts = wall_now();
185 static_cast<void>(sink.push(m.hdr));
186}
187
188} // namespace fastmm::venues
Exact decimal helpers for venue payloads (6.2).
EventSink (5.15): the net -> engine seam.
Event bus message structs (5.5).
Definition venue.hpp:38
void emit_replayed_fill(EventSink &sink, VenueId venue, InstrumentId inst, ClientOrderId id, std::string_view venue_order_id, std::string_view exec_id, Side side, Price price, Qty qty, Notional fee, FeeAsset fee_asset, Liquidity liquidity, std::int64_t trade_time_ms, std::uint8_t extra_flags=0) noexcept
An execution from the venue's trade history (Venue::request_executions), not from the private stream.
Definition order_events.hpp:107
void emit_order_reject(EventSink &sink, VenueId venue, InstrumentId inst, ClientOrderId id, RejectReason reason, int code, std::string_view text) noexcept
Definition order_events.hpp:20
void emit_funding(EventSink &sink, VenueId venue, InstrumentId inst, std::string_view funding_id, Notional amount, std::string_view asset, std::int64_t time_ms, bool replayed) noexcept
A funding payment (FundingMsg): amount signed in asset, negative paid.
Definition order_events.hpp:141
void emit_connection_state(EventSink &sink, VenueId venue, std::uint8_t channel, ConnState state, std::int32_t reason=0) noexcept
channel: 0 = market data, 1 = order/user stream (ConnectionStateMsg contract).
Definition order_events.hpp:176
void emit_order_ack(EventSink &sink, VenueId venue, InstrumentId inst, ClientOrderId id, std::string_view venue_order_id, std::uint8_t flags=0, Timestamp venue_ts=Timestamp{}) noexcept
flags: OrderAckMsg::kAmendedInPlace when the venue amended the resting order rather than creating a n...
Definition order_events.hpp:62
void emit_cancel_reject(EventSink &sink, VenueId venue, InstrumentId inst, ClientOrderId id, RejectReason reason, int code, std::string_view text) noexcept
Definition order_events.hpp:38
void emit_cancel_ack(EventSink &sink, VenueId venue, InstrumentId inst, ClientOrderId id, std::string_view venue_order_id, Qty cum_qty, Timestamp venue_ts=Timestamp{}) noexcept
venue_ts as for emit_order_ack.
Definition order_events.hpp:81
void emit_venue_kill(EventSink &sink, VenueId venue, KillReason reason) noexcept
The connector cannot trade on this venue any more (error map HardStop / Fatal, failed authentication)...
Definition order_events.hpp:165
ConnState
Definition enums.hpp:404
KillReason
Why a kill switch (global or one venue's) was tripped.
Definition enums.hpp:523
@ Funding
a perpetual funding payment booked by the venue (FundingMsg)
RejectReason
Ordered exactly like RiskEngine::check_new evaluates them (5.7), followed by OMS / transport / venue ...
Definition enums.hpp:123
Timestamp wall_now() noexcept
CLOCK_REALTIME via vDSO (~20 ns). Implemented in src/core/time.cpp.
constexpr void init_header(M &m, EventType type, InstrumentId inst={}, VenueId venue={}, std::uint32_t len=static_cast< std::uint32_t >(sizeof(M))) noexcept
For fixed-size messages len defaults to sizeof(M); BookDeltaMsg passes size_for().
Definition messages.hpp:75
Side
Definition enums.hpp:12
FeeAsset
The asset a fill's commission is charged in.
Definition enums.hpp:432
FASTMM_FORCE_INLINE Cycles rdtscp() noexcept
Serialising read (rdtscp) is what latency hops want: it waits for prior loads to retire.
Definition time.hpp:66
Liquidity
Definition enums.hpp:434
@ TripVenueKill
A venue connector gave up on one venue (arg: KillReason, hdr.venue: the venue): the engine trips that...
StrongId< struct VenueTag, std::uint8_t > VenueId
Definition strong_id.hpp:39
Definition messages.hpp:452
Definition messages.hpp:383
A perpetual funding payment the venue booked on the account's position in hdr.instrument (EventType::...
Definition messages.hpp:286
static constexpr std::uint8_t kReplayed
From the venue's history (Venue::request_executions), not the private stream.
Definition messages.hpp:288
Definition messages.hpp:188
Definition messages.hpp:213
Definition messages.hpp:222
Definition messages.hpp:233
static constexpr std::uint8_t kReplayed
The execution comes from the venue's trade history (Venue::request_executions), not from the private ...
Definition messages.hpp:237
Definition messages.hpp:202
static constexpr StrongId invalid() noexcept
Definition strong_id.hpp:30
Definition time.hpp:45
Time primitives.