@reyaxyz/strategy-format 0.0.0-stage → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +1 -0
- package/README.md +50 -2
- package/dist/en.json +212 -0
- package/dist/hash.d.ts +8 -0
- package/dist/hash.js +30 -0
- package/dist/index.d.ts +1814 -0
- package/dist/index.js +55 -0
- package/dist/lines.d.ts +138 -0
- package/dist/lines.js +516 -0
- package/dist/lines.json +287 -0
- package/dist/read.d.ts +21 -0
- package/dist/read.js +330 -0
- package/dist/schema/source.json +9 -0
- package/dist/schema/workflow-v3.accepted-kinds.json +226 -0
- package/dist/schema/workflow-v3.schema.json +1758 -0
- package/dist/sentences.d.ts +23 -0
- package/dist/sentences.js +553 -0
- package/dist/types.d.ts +242 -0
- package/dist/types.js +5 -0
- package/package.json +43 -4
package/dist/lines.js
ADDED
|
@@ -0,0 +1,516 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.whatIfLine = exports.WHAT_IF_RESPONSES = exports.gradedFigure = exports.READ_OUTS = exports.PAPER_LINES = exports.endedLine = exports.END_REASONS = exports.activityLine = exports.ACTIVITY_CODES = exports.ACTIVITY_NOT_LISTED = exports.ENTRY_DROPPED = exports.needsYouLine = exports.NEEDS_YOU_CODES = exports.FOLLOW_ONS = exports.line = exports.lineId = exports.LINES = void 0;
|
|
7
|
+
const lines_json_1 = __importDefault(require("./lines.json"));
|
|
8
|
+
exports.LINES = lines_json_1.default;
|
|
9
|
+
/** The line id of a key: what comes before its form. */
|
|
10
|
+
const lineId = (key) => key.split('.')[0];
|
|
11
|
+
exports.lineId = lineId;
|
|
12
|
+
/**
|
|
13
|
+
* The line for `key`, filled in one pass, so a `{...}` inside a value (a market's question) stays as
|
|
14
|
+
* text. A placeholder with no value is left as written.
|
|
15
|
+
*/
|
|
16
|
+
function line(key, vars = {}, t = exports.LINES) {
|
|
17
|
+
const text = t[key].replace(/\{(\w+)\}/g, (all, name) => name in vars ? String(vars[name]) : all);
|
|
18
|
+
return { id: (0, exports.lineId)(key), text };
|
|
19
|
+
}
|
|
20
|
+
exports.line = line;
|
|
21
|
+
/** Own keys only: a code such as `constructor` is not a line. */
|
|
22
|
+
const own = (table, key) => key !== undefined && Object.prototype.hasOwnProperty.call(table, key)
|
|
23
|
+
? table[key]
|
|
24
|
+
: undefined;
|
|
25
|
+
/**
|
|
26
|
+
* The follow-on code adds after the service's message, by the issue's code or, for a refused check,
|
|
27
|
+
* its `details.reason`. A code with none is relayed alone.
|
|
28
|
+
*/
|
|
29
|
+
exports.FOLLOW_ONS = {
|
|
30
|
+
no_max_loss: 'warn-no-max-loss',
|
|
31
|
+
max_loss_beyond_liquidation: 'warn-max-loss-liquidation',
|
|
32
|
+
positions_held_at_start: 'warn-held',
|
|
33
|
+
only_exit_reads_outside_series: 'warn-outside-exit',
|
|
34
|
+
stop_on_wrong_side: 'refuse-stop-side',
|
|
35
|
+
take_profit_on_wrong_side: 'refuse-stop-side',
|
|
36
|
+
stop_wrong_side_of_entry_by_construction: 'refuse-stop-entry',
|
|
37
|
+
account_has_live_strategy: 'refuse-second-live',
|
|
38
|
+
odds_source_off: 'refuse-odds-off',
|
|
39
|
+
paper_slippage_too_wide: 'refuse-paper-slippage',
|
|
40
|
+
};
|
|
41
|
+
/** On a Paper run every Needs-you and activity line reads "Paper: …". */
|
|
42
|
+
const forMode = (l, { t, mode }) => mode === 'paper'
|
|
43
|
+
? { id: l.id, text: line('paper-prefix', { line: l.text }, t).text }
|
|
44
|
+
: l;
|
|
45
|
+
const NEEDS_YOU = {
|
|
46
|
+
permission_gone: 'needs-permission',
|
|
47
|
+
deleveraged: 'needs-deleveraged',
|
|
48
|
+
order_unanswered_operator: 'needs-no-answer-team',
|
|
49
|
+
probe_unsigned: 'needs-no-answer-team',
|
|
50
|
+
consecutive_drops: 'needs-dropped',
|
|
51
|
+
expiry_approaching: 'needs-expiry',
|
|
52
|
+
execution_busted: 'needs-busted',
|
|
53
|
+
unsent_at_restart: 'needs-unsent',
|
|
54
|
+
rule_unreadable: 'needs-rule-unreadable',
|
|
55
|
+
refusal_handler_cycle: 'needs-engine-limit',
|
|
56
|
+
structural_transition_limit: 'needs-engine-limit',
|
|
57
|
+
account_suspended: 'needs-account-suspended',
|
|
58
|
+
restart_gap: 'needs-stops-paused',
|
|
59
|
+
stops_paused_service: 'needs-stops-paused',
|
|
60
|
+
position_unprotected: 'needs-unprotected',
|
|
61
|
+
sender_rest_fallback: 'needs-stop-slower',
|
|
62
|
+
entry_refused_kill_switch: 'needs-orders-paused',
|
|
63
|
+
market_catalogue_stale: 'needs-market-data',
|
|
64
|
+
cancel_left_in_place: 'needs-cancel-left',
|
|
65
|
+
round_result_unknown: 'needs-round-unknown',
|
|
66
|
+
feature_gated_at_restart: 'needs-feature-off',
|
|
67
|
+
position_unreadable: 'needs-position-unreadable',
|
|
68
|
+
cancels_pending: 'needs-cancels-pending',
|
|
69
|
+
close_cancel_refused: 'needs-cancel-refused',
|
|
70
|
+
orders_row_unreadable: 'needs-orders-unreadable',
|
|
71
|
+
stop_stale: 'why-stale',
|
|
72
|
+
odds_unreadable_exit: 'why-odds',
|
|
73
|
+
source_ended: 'needs-odds-ended',
|
|
74
|
+
};
|
|
75
|
+
const MAX_LOSS_PAUSED = {
|
|
76
|
+
max_loss_paused_account: 'needs-max-loss-paused.account',
|
|
77
|
+
max_loss_paused_funding: 'needs-max-loss-paused.funding',
|
|
78
|
+
max_loss_paused_error: 'needs-max-loss-paused.error',
|
|
79
|
+
};
|
|
80
|
+
const isCloseReason = (code) => !!code && own(exports.LINES, `close-reason.${code}`) !== undefined;
|
|
81
|
+
/**
|
|
82
|
+
* A max-loss pause's cause: the engine puts its sentence in the item's `detail`
|
|
83
|
+
* (`record_max_loss_readability`), not a code.
|
|
84
|
+
*/
|
|
85
|
+
const MAX_LOSS_DETAIL = [
|
|
86
|
+
[
|
|
87
|
+
/^Max loss is paused: the price for (.+) is stale$/,
|
|
88
|
+
'needs-max-loss-paused.price',
|
|
89
|
+
],
|
|
90
|
+
[
|
|
91
|
+
/^Max loss is paused: a market's price is stale$/,
|
|
92
|
+
'needs-max-loss-paused.price-any',
|
|
93
|
+
],
|
|
94
|
+
[
|
|
95
|
+
/^Max loss is paused: Reya's account data is behind$/,
|
|
96
|
+
'needs-max-loss-paused.account',
|
|
97
|
+
],
|
|
98
|
+
[
|
|
99
|
+
/^Max loss is paused: funding data is behind$/,
|
|
100
|
+
'needs-max-loss-paused.funding',
|
|
101
|
+
],
|
|
102
|
+
[
|
|
103
|
+
/^Max loss is paused: its figures can't be read$/,
|
|
104
|
+
'needs-max-loss-paused.error',
|
|
105
|
+
],
|
|
106
|
+
];
|
|
107
|
+
/** What a stopped close's `detail` adds after the engine's own sentence: the position left open. */
|
|
108
|
+
const STILL_OPEN = /^(?:The close stopped\. )?Still open with no stop from Reya(?:: )?/;
|
|
109
|
+
/** Every Needs-you code reya-chain sends that has a line of its own (the rest show `needs-other`). */
|
|
110
|
+
exports.NEEDS_YOU_CODES = [
|
|
111
|
+
...Object.keys(NEEDS_YOU),
|
|
112
|
+
'outside_order',
|
|
113
|
+
'liquidated',
|
|
114
|
+
'account_history_incomplete',
|
|
115
|
+
'margin_stall',
|
|
116
|
+
'order_unanswered',
|
|
117
|
+
'order_refused',
|
|
118
|
+
'close_stopped_open',
|
|
119
|
+
'max_loss_unreadable',
|
|
120
|
+
'max_loss_paused_price',
|
|
121
|
+
...Object.keys(MAX_LOSS_PAUSED),
|
|
122
|
+
'close_open_2min',
|
|
123
|
+
...Object.keys(exports.LINES)
|
|
124
|
+
.filter((k) => k.startsWith('close-reason.'))
|
|
125
|
+
.map((k) => k.slice('close-reason.'.length)),
|
|
126
|
+
];
|
|
127
|
+
/** The outside causes with a line of their own; `new_order` and any other read as the base line. */
|
|
128
|
+
const OUTSIDE_CAUSES = new Set(['modified', 'cancelled', 'found_at_restart']);
|
|
129
|
+
function needsYouKey(item, status) {
|
|
130
|
+
const { market, rule, detail } = item;
|
|
131
|
+
// An overdue item reads as the order manager's cause where that has a line of its own.
|
|
132
|
+
const cause = item.sender_code;
|
|
133
|
+
const code = cause && (own(NEEDS_YOU, cause) || isCloseReason(cause))
|
|
134
|
+
? cause
|
|
135
|
+
: item.code;
|
|
136
|
+
const fields = {
|
|
137
|
+
...(market && { market }),
|
|
138
|
+
...(rule && { rule }),
|
|
139
|
+
code,
|
|
140
|
+
};
|
|
141
|
+
if (code === 'max_loss_unreadable') {
|
|
142
|
+
for (const [pattern, key] of MAX_LOSS_DETAIL) {
|
|
143
|
+
const hit = detail?.match(pattern);
|
|
144
|
+
if (hit)
|
|
145
|
+
return [key, hit[1] ? { ...fields, market: hit[1] } : fields];
|
|
146
|
+
}
|
|
147
|
+
return ['needs-max-loss-paused', fields];
|
|
148
|
+
}
|
|
149
|
+
// These only explain the max-loss item on main; read alone, each is its cause's line.
|
|
150
|
+
if (code === 'max_loss_paused_price')
|
|
151
|
+
return [
|
|
152
|
+
market
|
|
153
|
+
? 'needs-max-loss-paused.price'
|
|
154
|
+
: 'needs-max-loss-paused.price-any',
|
|
155
|
+
fields,
|
|
156
|
+
];
|
|
157
|
+
const paused = own(MAX_LOSS_PAUSED, code);
|
|
158
|
+
if (paused)
|
|
159
|
+
return [paused, fields];
|
|
160
|
+
if (isCloseReason(code))
|
|
161
|
+
return [`close-reason.${code}`, fields];
|
|
162
|
+
switch (code) {
|
|
163
|
+
// With no cause of its own, a close open for 2 minutes reads as "nothing has filled".
|
|
164
|
+
case 'close_open_2min':
|
|
165
|
+
return ['close-reason.close_overdue', fields];
|
|
166
|
+
case 'margin_stall':
|
|
167
|
+
return item.escalated
|
|
168
|
+
? ['needs-margin.in-a-row', { ...fields, n: 3 }]
|
|
169
|
+
: ['needs-margin', fields];
|
|
170
|
+
case 'order_unanswered':
|
|
171
|
+
return [market ? 'needs-no-answer' : 'needs-no-answer.no-market', fields];
|
|
172
|
+
// With an effect the exchange refused the order; without one the strategy could not build it.
|
|
173
|
+
case 'order_refused':
|
|
174
|
+
if (item.effect !== undefined && item.effect !== null)
|
|
175
|
+
return [
|
|
176
|
+
market ? 'needs-order-refused' : 'needs-order-refused.no-market',
|
|
177
|
+
fields,
|
|
178
|
+
];
|
|
179
|
+
return [
|
|
180
|
+
market
|
|
181
|
+
? 'needs-order-refused.strategy'
|
|
182
|
+
: 'needs-order-refused.strategy-no-market',
|
|
183
|
+
fields,
|
|
184
|
+
];
|
|
185
|
+
case 'outside_order':
|
|
186
|
+
return [
|
|
187
|
+
item.cause && OUTSIDE_CAUSES.has(item.cause)
|
|
188
|
+
? `needs-outside-order.${item.cause}`
|
|
189
|
+
: 'needs-outside-order',
|
|
190
|
+
fields,
|
|
191
|
+
];
|
|
192
|
+
// Raised on carry_on (the run goes on) and when the close starts, before anything is closed.
|
|
193
|
+
case 'liquidated':
|
|
194
|
+
return [
|
|
195
|
+
status === 'ended'
|
|
196
|
+
? 'needs-liquidation.ended'
|
|
197
|
+
: status === 'closing'
|
|
198
|
+
? 'needs-liquidation.closing'
|
|
199
|
+
: status === 'running' || status === 'holding'
|
|
200
|
+
? 'needs-liquidation.running'
|
|
201
|
+
: 'needs-liquidation',
|
|
202
|
+
fields,
|
|
203
|
+
];
|
|
204
|
+
// It stays after the run ends, when nothing waits any more.
|
|
205
|
+
case 'account_history_incomplete':
|
|
206
|
+
return [
|
|
207
|
+
status === 'ended'
|
|
208
|
+
? 'needs-history-incomplete.ended'
|
|
209
|
+
: 'needs-history-incomplete',
|
|
210
|
+
fields,
|
|
211
|
+
];
|
|
212
|
+
case 'deleveraged':
|
|
213
|
+
return [
|
|
214
|
+
market ? 'needs-deleveraged' : 'needs-deleveraged.no-market',
|
|
215
|
+
fields,
|
|
216
|
+
];
|
|
217
|
+
case 'stop_stale':
|
|
218
|
+
return [market ? 'why-stale' : 'why-stale.no-market', fields];
|
|
219
|
+
case 'execution_busted':
|
|
220
|
+
return [market ? 'needs-busted' : 'needs-busted.no-market', fields];
|
|
221
|
+
case 'unsent_at_restart':
|
|
222
|
+
return [market ? 'needs-unsent' : 'needs-unsent.no-market', fields];
|
|
223
|
+
case 'close_stopped_open': {
|
|
224
|
+
const left = detail?.replace(STILL_OPEN, '').trim();
|
|
225
|
+
return left
|
|
226
|
+
? ['needs-close-stopped.detail', { ...fields, detail: left }]
|
|
227
|
+
: ['needs-close-stopped', fields];
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
return [own(NEEDS_YOU, code) ?? 'needs-other', fields];
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* The line for a Needs-you item, picked by its code. `vars` fills what the item does not carry (an
|
|
234
|
+
* expiry's `date`, an ended market's `question`, a rule's name as `rule`). Pass the run's `status`
|
|
235
|
+
* in `options`: a liquidation's line depends on it. A code this client has no line for, and
|
|
236
|
+
* `legacy`, show `needs-other` with the code as its reference.
|
|
237
|
+
*/
|
|
238
|
+
function needsYouLine(item, vars = {}, options = {}) {
|
|
239
|
+
const [key, fields] = needsYouKey(item, options.status);
|
|
240
|
+
return forMode(line(key, { ...fields, ...vars }, options.t), options);
|
|
241
|
+
}
|
|
242
|
+
exports.needsYouLine = needsYouLine;
|
|
243
|
+
/** The engine's fixed reasons for a dropped entry (`runtime/step.rs`), matched exactly. */
|
|
244
|
+
exports.ENTRY_DROPPED = {
|
|
245
|
+
'late entry': 'act-entry-skipped.late',
|
|
246
|
+
"missed: margin couldn't be checked": 'act-entry-skipped.margin',
|
|
247
|
+
'missed: the price moved more than 25 bps since the reading': 'act-entry-skipped.price',
|
|
248
|
+
};
|
|
249
|
+
const ACTIVITY = {
|
|
250
|
+
start: 'act-start',
|
|
251
|
+
claimed: 'act-rule',
|
|
252
|
+
order_sent: 'act-order-sent',
|
|
253
|
+
settled: 'act-fill',
|
|
254
|
+
account_fill: 'act-account-fill',
|
|
255
|
+
busted: 'act-busted',
|
|
256
|
+
execution_unresolved: 'act-entry-held',
|
|
257
|
+
condition_unreadable: 'act-condition-unreadable',
|
|
258
|
+
already_flat: 'act-already-flat',
|
|
259
|
+
reduction_below_lot: 'act-reduce-below-lot',
|
|
260
|
+
rounds_exhausted: 'act-rounds-used',
|
|
261
|
+
max_loss_reached: 'act-max-loss',
|
|
262
|
+
expiry: 'act-expiry',
|
|
263
|
+
ended: 'act-ended',
|
|
264
|
+
odds_entry_waiting: 'odds-waiting',
|
|
265
|
+
deleveraged: 'act-deleveraged',
|
|
266
|
+
liquidation: 'act-liquidation',
|
|
267
|
+
position_flipped: 'act-position-flipped',
|
|
268
|
+
};
|
|
269
|
+
const EVENTS = {
|
|
270
|
+
stop_close: 'act-stop-close',
|
|
271
|
+
stop_keep: 'act-stop-keep',
|
|
272
|
+
external_flat: 'act-external-flat',
|
|
273
|
+
};
|
|
274
|
+
const NOTICES = {
|
|
275
|
+
orders_restart: 'act-orders-restart',
|
|
276
|
+
witness_check_order: 'act-check-order',
|
|
277
|
+
close_band_busted: 'act-close-band-busted',
|
|
278
|
+
book_refused: 'act-book-refused',
|
|
279
|
+
};
|
|
280
|
+
/**
|
|
281
|
+
* Activity codes with nothing to show the user: the engine's bookkeeping, a fault written beside
|
|
282
|
+
* the rule's own event, a fill another order owns, and moving the user's own stop into a strategy
|
|
283
|
+
* (not in this release).
|
|
284
|
+
*/
|
|
285
|
+
exports.ACTIVITY_NOT_LISTED = [
|
|
286
|
+
'clock',
|
|
287
|
+
'observation',
|
|
288
|
+
'protection',
|
|
289
|
+
'execution',
|
|
290
|
+
'account',
|
|
291
|
+
'handover',
|
|
292
|
+
'reconciled',
|
|
293
|
+
'reconciliation_pending',
|
|
294
|
+
'stale_protective_arm',
|
|
295
|
+
'evaluator_fault',
|
|
296
|
+
'fill_owner_conflict',
|
|
297
|
+
// Written beside the external_flat event, which carries the line.
|
|
298
|
+
'market_left_flat',
|
|
299
|
+
'stop_transferred',
|
|
300
|
+
'stop_transfer_pending',
|
|
301
|
+
];
|
|
302
|
+
/** Every activity code with a line. */
|
|
303
|
+
exports.ACTIVITY_CODES = [
|
|
304
|
+
...Object.keys(ACTIVITY),
|
|
305
|
+
'order_refused',
|
|
306
|
+
'entry_dropped',
|
|
307
|
+
'event',
|
|
308
|
+
'order_notice',
|
|
309
|
+
'needs_you_raised',
|
|
310
|
+
'needs_you_updated',
|
|
311
|
+
'needs_you_cleared',
|
|
312
|
+
];
|
|
313
|
+
/**
|
|
314
|
+
* The line for an activity entry, picked by its code, or `null` for one with nothing to show (an
|
|
315
|
+
* event kind whose Needs-you item or effect carries the line, and `ACTIVITY_NOT_LISTED`). A code
|
|
316
|
+
* this client has no line for, and `legacy`, show `activity-other`.
|
|
317
|
+
*/
|
|
318
|
+
function activityLine(entry, vars = {}, options = {}) {
|
|
319
|
+
const { code, reason, rule, market } = entry;
|
|
320
|
+
const fields = {
|
|
321
|
+
...(rule && { rule }),
|
|
322
|
+
...(market && { market }),
|
|
323
|
+
code,
|
|
324
|
+
...vars,
|
|
325
|
+
};
|
|
326
|
+
const pick = (key, extra = {}) => forMode(line(key, { ...fields, ...extra }, options.t), options);
|
|
327
|
+
if (exports.ACTIVITY_NOT_LISTED.includes(code))
|
|
328
|
+
return null;
|
|
329
|
+
const listed = own(ACTIVITY, code);
|
|
330
|
+
if (listed)
|
|
331
|
+
return pick(listed);
|
|
332
|
+
switch (code) {
|
|
333
|
+
// With an effect the exchange refused it; without one the strategy could not build it. The
|
|
334
|
+
// engine's reason is diagnostic text and is not shown.
|
|
335
|
+
case 'order_refused':
|
|
336
|
+
return pick(entry.effect !== undefined && entry.effect !== null
|
|
337
|
+
? 'act-order-refused'
|
|
338
|
+
: 'act-order-refused.strategy');
|
|
339
|
+
case 'entry_dropped':
|
|
340
|
+
return pick(own(exports.ENTRY_DROPPED, reason) ?? 'act-entry-skipped');
|
|
341
|
+
case 'event': {
|
|
342
|
+
// The engine serializes the kind as written in Rust (`StopClose`); accept both spellings.
|
|
343
|
+
const kind = entry.event
|
|
344
|
+
?.replace(/(?<=.)[A-Z]/g, (c) => `_${c}`)
|
|
345
|
+
.toLowerCase();
|
|
346
|
+
const key = own(EVENTS, kind);
|
|
347
|
+
return key ? pick(key) : null;
|
|
348
|
+
}
|
|
349
|
+
case 'order_notice': {
|
|
350
|
+
const notice = entry.incident?.code;
|
|
351
|
+
return pick(own(NOTICES, notice) ?? 'activity-other', {
|
|
352
|
+
code: notice ?? code,
|
|
353
|
+
});
|
|
354
|
+
}
|
|
355
|
+
case 'needs_you_raised':
|
|
356
|
+
case 'needs_you_updated':
|
|
357
|
+
case 'needs_you_cleared': {
|
|
358
|
+
const item = entry.incident ?? { code: 'legacy' };
|
|
359
|
+
const itemLine = needsYouLine(item, vars, { t: options.t }).text;
|
|
360
|
+
return pick(code === 'needs_you_cleared'
|
|
361
|
+
? 'act-needs-you-cleared'
|
|
362
|
+
: 'act-needs-you', {
|
|
363
|
+
line: itemLine,
|
|
364
|
+
});
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
return pick('activity-other');
|
|
368
|
+
}
|
|
369
|
+
exports.activityLine = activityLine;
|
|
370
|
+
const ENDED = {
|
|
371
|
+
own_rule: 'ended-rules',
|
|
372
|
+
stop_close: 'ended-stop-close',
|
|
373
|
+
stop_keep: 'ended-stop-keep',
|
|
374
|
+
max_loss: 'ended-max-loss',
|
|
375
|
+
expiry: 'ended-expiry',
|
|
376
|
+
operator: 'ended-operator',
|
|
377
|
+
position_flipped: 'ended-position-flipped',
|
|
378
|
+
liquidated: 'needs-liquidation.ended',
|
|
379
|
+
outside_order: 'needs-outside-order',
|
|
380
|
+
permission_gone: 'needs-permission',
|
|
381
|
+
};
|
|
382
|
+
/** Every end reason reya-chain sends. */
|
|
383
|
+
exports.END_REASONS = Object.keys(ENDED);
|
|
384
|
+
/**
|
|
385
|
+
* Why a strategy ended, by the engine's reason: for an Ended run only. The engine sets the reason
|
|
386
|
+
* when the run starts to end, while it is still Closing; show `status-closing` until it is Ended.
|
|
387
|
+
* A liquidation, an order from outside and permission gone use their Needs-you line, so one reason
|
|
388
|
+
* has one set of words. With no reason, or one this client does not know, `status-ended`.
|
|
389
|
+
*/
|
|
390
|
+
const endedLine = (reason, t) => line(own(ENDED, reason) ?? 'status-ended', {}, t);
|
|
391
|
+
exports.endedLine = endedLine;
|
|
392
|
+
/**
|
|
393
|
+
* A Paper run's lasting lines, by code (reya-chain's `PaperLine`): shown for good once they happen,
|
|
394
|
+
* never as Needs-you items. `paper-below-zero` takes the `date`; `paper-not-closed` goes beside each
|
|
395
|
+
* position left open.
|
|
396
|
+
*/
|
|
397
|
+
exports.PAPER_LINES = {
|
|
398
|
+
paper_balance_below_zero: 'paper-below-zero',
|
|
399
|
+
not_closed: 'paper-not-closed',
|
|
400
|
+
};
|
|
401
|
+
/** Each read-out's name, by its field in the check result (`result.readouts`). */
|
|
402
|
+
exports.READ_OUTS = {
|
|
403
|
+
'margin_needed.total': 'read-margin',
|
|
404
|
+
'margin_needed.margin': 'read-margin-engine',
|
|
405
|
+
'margin_needed.fees': 'read-fees',
|
|
406
|
+
'margin_needed.funding': 'read-funding',
|
|
407
|
+
'margin_needed.funding_excluded': 'read-funding-excluded',
|
|
408
|
+
'margin_needed.post_expiry_holding_cost': 'read-after-end',
|
|
409
|
+
additional_deposit_needed: 'read-deposit',
|
|
410
|
+
'loops.fit_now': 'read-rounds',
|
|
411
|
+
/** A loop with no round count (`rounds` absent). */
|
|
412
|
+
'loops.fit_now.uncounted': 'read-rounds.uncounted',
|
|
413
|
+
'loops.cost_per_round': 'read-round-cost',
|
|
414
|
+
balance_needed: 'read-balance-all',
|
|
415
|
+
'loss_at_stop.pnl': 'read-loss-at-stop',
|
|
416
|
+
other_markets_closed: 'read-also-closed',
|
|
417
|
+
worst_deployed: 'read-deployed',
|
|
418
|
+
'liquidation.per_market.liq_price': 'read-liq-price',
|
|
419
|
+
'liquidation.per_market.no_price.none': 'read-liq-none',
|
|
420
|
+
'liquidation.per_market.no_price.underwater': 'read-liq-underwater',
|
|
421
|
+
'liquidation.account_move_pct': 'read-liq-move',
|
|
422
|
+
orders: 'read-order',
|
|
423
|
+
};
|
|
424
|
+
const GRADES = {
|
|
425
|
+
exact: 'read-exact',
|
|
426
|
+
at_most: 'read-at-most',
|
|
427
|
+
at_least: 'read-at-least',
|
|
428
|
+
unknown: 'read-unknown',
|
|
429
|
+
};
|
|
430
|
+
/**
|
|
431
|
+
* A figure with its grade, which is part of what is said: "at most 106.43 USDC", "not known yet".
|
|
432
|
+
* `figure` is the value as the client formats it, with its unit.
|
|
433
|
+
*/
|
|
434
|
+
const gradedFigure = (grade, figure, t) => line(own(GRADES, grade) ?? 'read-unknown', { figure }, t).text;
|
|
435
|
+
exports.gradedFigure = gradedFigure;
|
|
436
|
+
const WHAT_IF = {
|
|
437
|
+
close_and_end: 'set-liquidation',
|
|
438
|
+
carry_on: 'whatif-carry-on',
|
|
439
|
+
keep_running: 'set-margin',
|
|
440
|
+
stop_on_skip: 'whatif-stop-on-skip',
|
|
441
|
+
max_loss_close_all: 'whatif-max-loss',
|
|
442
|
+
expiry_hold: 'whatif-expiry',
|
|
443
|
+
order_refused: 'whatif-order-refused',
|
|
444
|
+
cancelled_at_fill: 'whatif-cancelled',
|
|
445
|
+
inherited_cancelled_at_fill: 'whatif-own-cancelled',
|
|
446
|
+
every_buy_fills: 'whatif-all-fill',
|
|
447
|
+
inherited_fill_moves_basis: 'whatif-own-fill-basis',
|
|
448
|
+
inherited_fill_outside_strategy: 'whatif-own-fill-other',
|
|
449
|
+
not_derived: 'whatif-not-derived',
|
|
450
|
+
};
|
|
451
|
+
const STOP_KIND = {
|
|
452
|
+
exchange_stop_loss: 'stop-loss',
|
|
453
|
+
exchange_take_profit: 'take-profit',
|
|
454
|
+
};
|
|
455
|
+
/** Every `response` reya-chain sends, `authored` included (it reads as its rule's sentence). */
|
|
456
|
+
exports.WHAT_IF_RESPONSES = [
|
|
457
|
+
...Object.keys(WHAT_IF),
|
|
458
|
+
'authored',
|
|
459
|
+
'no_position_change',
|
|
460
|
+
'inherited_fill_reduces',
|
|
461
|
+
'exchange_stop_ends_market_part',
|
|
462
|
+
'exchange_stop_outside_strategy',
|
|
463
|
+
'unknown',
|
|
464
|
+
];
|
|
465
|
+
function whatIfKey(row) {
|
|
466
|
+
const { response, event } = row;
|
|
467
|
+
if (response === 'authored')
|
|
468
|
+
return null;
|
|
469
|
+
const listed = own(WHAT_IF, response);
|
|
470
|
+
if (listed)
|
|
471
|
+
return listed;
|
|
472
|
+
switch (response) {
|
|
473
|
+
case 'no_position_change':
|
|
474
|
+
return event.startsWith('source_ended')
|
|
475
|
+
? 'whatif-odds-ended'
|
|
476
|
+
: 'whatif-odds-unreadable';
|
|
477
|
+
case 'inherited_fill_reduces':
|
|
478
|
+
return row.basis_after
|
|
479
|
+
? 'whatif-own-fill-reduces.basis'
|
|
480
|
+
: 'whatif-own-fill-reduces';
|
|
481
|
+
case 'exchange_stop_ends_market_part':
|
|
482
|
+
case 'exchange_stop_outside_strategy': {
|
|
483
|
+
const kind = own(STOP_KIND, event) ?? 'stop-loss';
|
|
484
|
+
const base = response === 'exchange_stop_ends_market_part'
|
|
485
|
+
? 'whatif-own-stop'
|
|
486
|
+
: 'whatif-own-stop-other';
|
|
487
|
+
return `${base}.${kind}`;
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
return 'whatif-unknown.plain';
|
|
491
|
+
}
|
|
492
|
+
/**
|
|
493
|
+
* The line for a "what happens if" row, picked by its `response`, or `null` for `authored`: that
|
|
494
|
+
* row reads as its rule's own sentence (`sentences(doc)[row.rule]`). `vars` holds the figures as the
|
|
495
|
+
* client formats them (`needed`, `have`, `basis`) and an odds row's `question`. An unknown row reads
|
|
496
|
+
* "{its event's 'If …' words}: not known yet." On a Paper run the liquidation row reads
|
|
497
|
+
* `paper-no-liquidation`.
|
|
498
|
+
*/
|
|
499
|
+
function whatIfLine(row, vars = {}, { t, mode } = {}) {
|
|
500
|
+
const fields = {
|
|
501
|
+
...(row.market && { market: row.market }),
|
|
502
|
+
...vars,
|
|
503
|
+
};
|
|
504
|
+
if (row.response === 'unknown') {
|
|
505
|
+
const ifKey = `whatif-if.${row.event}`;
|
|
506
|
+
if (own(exports.LINES, ifKey) === undefined)
|
|
507
|
+
return line('whatif-unknown.plain', fields, t);
|
|
508
|
+
return line('whatif-unknown', { if: line(ifKey, fields, t).text }, t);
|
|
509
|
+
}
|
|
510
|
+
// A Paper run has no liquidation, whatever the setting says.
|
|
511
|
+
if (mode === 'paper' && row.event === 'liquidation')
|
|
512
|
+
return line('paper-no-liquidation', fields, t);
|
|
513
|
+
const key = whatIfKey(row);
|
|
514
|
+
return key && line(key, fields, t);
|
|
515
|
+
}
|
|
516
|
+
exports.whatIfLine = whatIfLine;
|