-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrading_ui_format.cpp
More file actions
212 lines (195 loc) · 8.56 KB
/
trading_ui_format.cpp
File metadata and controls
212 lines (195 loc) · 8.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include "trading_ui_format.h"
#include <cmath>
#include <iomanip>
#include <sstream>
namespace {
void appendLatencyLine(std::ostringstream& oss, const char* label, double valueMs) {
oss << label << ": ";
if (valueMs >= 0.0) {
if (valueMs >= 1000.0) {
oss << std::fixed << std::setprecision(3) << (valueMs / 1000.0) << " s";
} else {
oss << std::fixed << std::setprecision(1) << valueMs << " ms";
}
} else {
oss << "--";
}
oss << '\n';
}
std::string formatDurationFromNow(std::chrono::steady_clock::time_point start) {
if (start.time_since_epoch().count() == 0) {
return "--";
}
const double elapsedMs =
std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - start).count();
std::ostringstream oss;
if (elapsedMs >= 1000.0) {
oss << std::fixed << std::setprecision(1) << (elapsedMs / 1000.0) << " s";
} else {
oss << std::fixed << std::setprecision(0) << elapsedMs << " ms";
}
return oss.str();
}
} // namespace
std::string formatOrderLocalStateText(const OrderInfo& order) {
std::ostringstream oss;
oss << localOrderStateToString(order.localState);
const bool showAttempts =
order.watchdogs.reconciliationAttempts > 0 &&
(order.localState == LocalOrderState::NeedsReconciliation ||
order.localState == LocalOrderState::NeedsManualReview);
if ((order.localState == LocalOrderState::NeedsManualReview && order.manualReviewAcknowledged) || showAttempts) {
oss << " (";
bool wroteDetail = false;
if (order.localState == LocalOrderState::NeedsManualReview && order.manualReviewAcknowledged) {
oss << "ack";
wroteDetail = true;
}
if (showAttempts) {
if (wroteDetail) {
oss << ", ";
}
oss << order.watchdogs.reconciliationAttempts;
}
oss << ')';
}
return oss.str();
}
std::string formatOrderWatchdogText(const OrderInfo& order) {
std::ostringstream oss;
if (order.localState == LocalOrderState::AwaitingBrokerEcho && order.watchdogs.brokerEchoArmed) {
oss << "echo " << formatDurationFromNow(order.submitTime);
} else if (order.localState == LocalOrderState::AwaitingCancelAck && order.watchdogs.cancelAckArmed) {
const auto startedAt = order.watchdogs.lastBrokerCallback.time_since_epoch().count() != 0
? order.watchdogs.lastBrokerCallback
: order.submitTime;
oss << "cancel " << formatDurationFromNow(startedAt);
} else if (order.localState == LocalOrderState::PartiallyFilled && order.watchdogs.partialFillQuietArmed) {
oss << "quiet " << formatDurationFromNow(order.watchdogs.lastBrokerCallback);
} else if (order.localState == LocalOrderState::NeedsReconciliation) {
oss << "retry " << order.watchdogs.reconciliationAttempts;
} else if (order.localState == LocalOrderState::NeedsManualReview) {
oss << "manual review";
if (order.manualReviewAcknowledged) {
oss << " ack";
}
} else if (order.watchdogs.lastBrokerCallback.time_since_epoch().count() != 0 && !order.isTerminal()) {
oss << "callback " << formatDurationFromNow(order.watchdogs.lastBrokerCallback) << " ago";
} else {
return formatOrderTimingText(order);
}
if (!order.lastReconciliationReason.empty() &&
(order.localState == LocalOrderState::NeedsReconciliation ||
order.localState == LocalOrderState::NeedsManualReview)) {
oss << " · " << order.lastReconciliationReason;
}
return oss.str();
}
std::string formatOrderTimingText(const OrderInfo& order) {
std::ostringstream oss;
if (order.fillDurationMs >= 0.0) {
if (order.fillDurationMs >= 1000.0) {
oss << std::fixed << std::setprecision(2) << (order.fillDurationMs / 1000.0) << " s";
} else {
oss << std::fixed << std::setprecision(0) << order.fillDurationMs << " ms";
}
} else if (order.submitTime.time_since_epoch().count() > 0 && !order.isTerminal()) {
const auto elapsed = std::chrono::steady_clock::now() - order.submitTime;
const double elapsedMs = std::chrono::duration<double, std::milli>(elapsed).count();
if (elapsedMs >= 1000.0) {
oss << std::fixed << std::setprecision(1) << (elapsedMs / 1000.0) << " s...";
} else {
oss << std::fixed << std::setprecision(0) << elapsedMs << " ms...";
}
} else {
oss << "--";
}
return oss.str();
}
std::string formatTradeTraceDetailsText(const TradeTraceSnapshot& snapshot) {
if (!snapshot.found) {
return "No trade trace selected.";
}
const TradeTrace& trace = snapshot.trace;
std::ostringstream oss;
oss << "Trace " << trace.traceId;
if (trace.orderId > 0) {
oss << " | Order " << static_cast<long long>(trace.orderId);
} else {
oss << " | No order ID assigned";
}
if (trace.permId > 0) {
oss << " | PermId " << trace.permId;
}
oss << "\n\n";
oss << "Source: " << (trace.source.empty() ? "<unknown>" : trace.source) << '\n';
oss << "Symbol: " << (trace.symbol.empty() ? "<none>" : trace.symbol) << '\n';
oss << "Side: " << (trace.side.empty() ? "<none>" : trace.side) << '\n';
oss << "Requested: " << trace.requestedQty << " @ " << std::fixed << std::setprecision(2) << trace.limitPrice << '\n';
oss << "Close-only: " << (trace.closeOnly ? "yes" : "no") << '\n';
if (!trace.latestStatus.empty()) {
oss << "Latest status: " << trace.latestStatus << '\n';
}
if (!trace.terminalStatus.empty()) {
oss << "Terminal status: " << trace.terminalStatus << '\n';
}
if (!trace.latestError.empty()) {
oss << "Latest error: " << trace.latestError << '\n';
}
oss << '\n';
oss << "Decision snapshot\n";
oss << " bid=" << std::fixed << std::setprecision(2) << trace.decisionBid
<< " ask=" << trace.decisionAsk
<< " last=" << trace.decisionLast
<< " sweep=" << trace.sweepEstimate
<< " buffer=" << trace.priceBuffer << '\n';
if (!trace.bookSummary.empty()) {
oss << " book: " << trace.bookSummary << '\n';
}
if (!trace.notes.empty()) {
oss << " notes: " << trace.notes << '\n';
}
oss << '\n';
oss << "Latency breakdown\n";
appendLatencyLine(oss, "Validation", durationMs(trace.validationStartMono, trace.validationEndMono));
appendLatencyLine(oss, "Trigger -> placeOrder return", durationMs(trace.triggerMono, trace.placeCallEndMono));
appendLatencyLine(oss, "placeOrder return -> openOrder", durationMs(trace.placeCallEndMono, trace.firstOpenOrderMono));
appendLatencyLine(oss, "placeOrder return -> first orderStatus", durationMs(trace.placeCallEndMono, trace.firstStatusMono));
appendLatencyLine(oss, "placeOrder return -> first exec", durationMs(trace.placeCallEndMono, trace.firstExecMono));
appendLatencyLine(oss, "Trigger -> first exec", durationMs(trace.triggerMono, trace.firstExecMono));
appendLatencyLine(oss, "First exec -> full fill", durationMs(trace.firstExecMono, trace.fullFillMono));
appendLatencyLine(oss, "Trigger -> full fill", durationMs(trace.triggerMono, trace.fullFillMono));
oss << '\n';
if (!trace.fills.empty()) {
oss << "Fill slices\n";
for (const auto& fill : trace.fills) {
oss << " " << fill.execId
<< " shares=" << fill.shares
<< " price=" << std::fixed << std::setprecision(2) << fill.price
<< " cum=" << fill.cumQty
<< " exch=" << fill.exchange;
if (fill.commissionKnown) {
oss << " commission=" << std::fixed << std::setprecision(4)
<< fill.commission << ' ' << fill.commissionCurrency;
}
oss << '\n';
}
if (!trace.commissionCurrency.empty()) {
oss << " total commission=" << std::fixed << std::setprecision(4)
<< trace.totalCommission << ' ' << trace.commissionCurrency << '\n';
}
oss << '\n';
}
oss << "Timeline\n";
for (const auto& event : trace.events) {
oss << " " << formatWallTime(event.wallTs) << " ";
const double sinceTriggerMs = durationMs(trace.triggerMono, event.monoTs);
if (sinceTriggerMs >= 0.0) {
oss << std::fixed << std::setprecision(1) << sinceTriggerMs << " ms ";
} else {
oss << "-- ";
}
oss << event.stage << " " << event.details << '\n';
}
return oss.str();
}