@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.
@@ -0,0 +1,23 @@
1
+ import en from './en.json';
2
+ import type { StrategyV3 } from './types';
3
+ export type SentenceKey = keyof typeof en;
4
+ /**
5
+ * A translation of `en.json`: every key, with the same `{placeholders}`, in any order. A key ending
6
+ * in `.one` / `.other` is a plural: a translation adds the forms its language needs (`.few`,
7
+ * `.many`, ...), picked by `Intl.PluralRules` for `locale`.
8
+ */
9
+ export type Translations = Record<SentenceKey, string> & Record<string, string>;
10
+ /**
11
+ * One line per block, keyed by block id: every state, rule, action, pin and list, and every block
12
+ * inside one (stop levels, resting orders, list rows, weight rows), plus the three settings under
13
+ * their field names. Built from the document alone, so it describes exactly what is signed. A field
14
+ * the format leaves out has a meaning (a crossing with no band fires again only after going back past
15
+ * its level, a stop level with no size closes all of it): each such default is worded, never skipped.
16
+ * The card passes its translations; the chat passes English.
17
+ *
18
+ * `mode` is the run the card is for. A Paper run has no liquidation, and its max loss, position and
19
+ * account value are its own, so in `paper` a key with a `<key>.paper` form uses it. A draft with no
20
+ * mode chosen yet reads as Live.
21
+ */
22
+ export declare function sentences(doc: StrategyV3, t?: Translations, locale?: string, mode?: Mode): Record<string, string>;
23
+ export type Mode = 'live' | 'paper';
@@ -0,0 +1,553 @@
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.sentences = void 0;
7
+ const en_json_1 = __importDefault(require("./en.json"));
8
+ /**
9
+ * One line per block, keyed by block id: every state, rule, action, pin and list, and every block
10
+ * inside one (stop levels, resting orders, list rows, weight rows), plus the three settings under
11
+ * their field names. Built from the document alone, so it describes exactly what is signed. A field
12
+ * the format leaves out has a meaning (a crossing with no band fires again only after going back past
13
+ * its level, a stop level with no size closes all of it): each such default is worded, never skipped.
14
+ * The card passes its translations; the chat passes English.
15
+ *
16
+ * `mode` is the run the card is for. A Paper run has no liquidation, and its max loss, position and
17
+ * account value are its own, so in `paper` a key with a `<key>.paper` form uses it. A draft with no
18
+ * mode chosen yet reads as Live.
19
+ */
20
+ function sentences(doc, t = en_json_1.default, locale = 'en', mode = 'live') {
21
+ return new Builder(doc, t, new Intl.PluralRules(locale), mode).build();
22
+ }
23
+ exports.sentences = sentences;
24
+ const num = (id) => id.slice(id.indexOf('_') + 1);
25
+ class Builder {
26
+ constructor(doc, t, plurals, mode) {
27
+ this.doc = doc;
28
+ this.t = t;
29
+ this.plurals = plurals;
30
+ this.mode = mode;
31
+ this.lines = {};
32
+ /** Whether the block being worded runs per list member, where an absent market is the row's. */
33
+ this.inList = false;
34
+ this.ruleRef = (id) => this.f('rule.ref', { n: num(id) });
35
+ }
36
+ build() {
37
+ const { doc } = this;
38
+ const listRules = new Set(Object.values(doc.states).flatMap((s) => (s.in_list ? s.on ?? [] : [])));
39
+ const listActions = new Set([...listRules].flatMap((id) => doc.rules?.[id]?.do ?? []));
40
+ for (const [id, state] of Object.entries(doc.states))
41
+ this.lines[id] = this.state(id, state);
42
+ for (const [id, rule] of Object.entries(doc.rules ?? {})) {
43
+ this.inList = listRules.has(id);
44
+ this.lines[id] = this.rule(id, rule);
45
+ }
46
+ for (const [id, action] of Object.entries(doc.actions ?? {})) {
47
+ this.inList = listActions.has(id) || 'list' in action;
48
+ this.lines[id] = this.capital(this.action(action));
49
+ }
50
+ for (const [id, pin] of Object.entries(doc.series ?? {})) {
51
+ this.lines[id] = this.f('pin.line', {
52
+ // The question is in quotes, the outcome is not: only the question is escaped.
53
+ outcome: pin.label.outcome,
54
+ question: quoted(pin.label.question),
55
+ ends: this.time(pin.label.ends),
56
+ });
57
+ }
58
+ for (const list of doc.lists ?? []) {
59
+ this.inList = true;
60
+ this.lines[list.id] = this.list(list);
61
+ }
62
+ this.lines.max_loss_rusd =
63
+ doc.max_loss_rusd === undefined
64
+ ? this.f('setting.max_loss_rusd.none')
65
+ : this.f('setting.max_loss_rusd', { value: doc.max_loss_rusd });
66
+ this.lines.on_liquidation = this.f(`setting.on_liquidation.${doc.on_liquidation}`);
67
+ this.lines.on_margin_skip = this.f(`setting.on_margin_skip.${doc.on_margin_skip}`);
68
+ return this.lines;
69
+ }
70
+ /**
71
+ * Fills a template in one pass, so a `{...}` inside a value (a pin's question) stays as text. In
72
+ * Paper, `<key>.paper` when the translation has one.
73
+ */
74
+ f(key, vars = {}) {
75
+ const paper = `${key}.paper`;
76
+ const template = this.mode === 'paper' && paper in this.t ? this.t[paper] : this.t[key];
77
+ return template.replace(/\{(\w+)\}/g, (all, name) => name in vars ? String(vars[name]) : all);
78
+ }
79
+ /** A plural: `<base>.<category>` for `n`, else `<base>.other`. */
80
+ plural(base, n, vars) {
81
+ const key = `${base}.${this.plurals.select(n)}`;
82
+ return this.f((key in this.t ? key : `${base}.other`), vars);
83
+ }
84
+ capital(s) {
85
+ return s.charAt(0).toUpperCase() + s.slice(1);
86
+ }
87
+ join(items, key = 'list.join') {
88
+ return items.join(this.t[key]);
89
+ }
90
+ // ---- references ----
91
+ stateRef(id) {
92
+ const name = this.doc.states[id]?.name;
93
+ return name === undefined
94
+ ? this.f('state.ref', { n: num(id) })
95
+ : this.f('state.named', { name: quoted(name), n: num(id) });
96
+ }
97
+ market(market) {
98
+ return market ?? this.f('market.row');
99
+ }
100
+ pinById(id) {
101
+ const pin = this.doc.series?.[id];
102
+ return pin ? this.pin(pin) : id;
103
+ }
104
+ pin(pin) {
105
+ return this.f('pin', {
106
+ outcome: quoted(pin.label.outcome),
107
+ question: quoted(pin.label.question),
108
+ });
109
+ }
110
+ // ---- blocks ----
111
+ state(id, s) {
112
+ return this.capital(this.stateLine(id, s));
113
+ }
114
+ stateLine(id, s) {
115
+ const memberStart = (this.doc.lists ?? []).some((l) => l.start === id);
116
+ const vars = {
117
+ start: id === this.doc.start
118
+ ? this.f('state.start')
119
+ : memberStart
120
+ ? this.f('state.start.member')
121
+ : '',
122
+ ref: this.stateRef(id),
123
+ in_list: s.in_list
124
+ ? this.f('state.in_list', { list: num(s.in_list) })
125
+ : '',
126
+ checks: s.on
127
+ ? this.f('state.checks', { rules: this.join(s.on.map(this.ruleRef)) })
128
+ : '',
129
+ };
130
+ switch (s.kind) {
131
+ case 'normal':
132
+ return this.f('state.normal', vars);
133
+ case 'loop_head':
134
+ return s.rounds === undefined
135
+ ? this.f('state.loop_head.unbounded', vars)
136
+ : this.f('state.loop_head', {
137
+ ...vars,
138
+ rounds: s.rounds,
139
+ then: s.then ? this.stateRef(s.then) : '',
140
+ });
141
+ case 'final':
142
+ // A list member's end closes or holds that member only.
143
+ return this.f(s.in_list
144
+ ? `state.final.${s.outcome}.member`
145
+ : `state.final.${s.outcome}`, vars);
146
+ }
147
+ }
148
+ rule(id, r) {
149
+ // A false `if` holds an `elapsed` rule: it fires the first time the condition holds after that.
150
+ const held = r.when.kind === 'elapsed';
151
+ // A rule whose `go` is the only state it is on re-enters it.
152
+ const owners = Object.entries(this.doc.states).filter(([, s]) => s.on?.includes(id));
153
+ const again = owners.length > 0 && owners.every(([sid]) => sid === r.go);
154
+ const fromItself = !again && owners.some(([sid]) => sid === r.go);
155
+ const target = this.doc.states[r.go];
156
+ const goVars = {
157
+ state: this.stateRef(r.go),
158
+ reentry: this.f('rule.reentry'),
159
+ then: target?.kind === 'loop_head' && target.then
160
+ ? this.stateRef(target.then)
161
+ : '',
162
+ };
163
+ const vars = {
164
+ when: this.trigger(r.when),
165
+ if: r.if
166
+ ? this.f(held ? 'rule.if.held' : 'rule.if', { cond: this.expr(r.if) })
167
+ : '',
168
+ go: this.f(fromItself
169
+ ? 'rule.go.to.self'
170
+ : !again
171
+ ? // Any entry into a used-up loop head goes to its `then` instead.
172
+ goVars.then
173
+ ? 'rule.go.to.loop'
174
+ : 'rule.go.to'
175
+ : target?.kind !== 'loop_head'
176
+ ? 'rule.go.again'
177
+ : goVars.then
178
+ ? 'rule.go.round.then'
179
+ : 'rule.go.round', goVars),
180
+ };
181
+ if (!r.do)
182
+ return this.f('rule.go', vars);
183
+ // Resting orders a rule places belong to the state it goes to.
184
+ const actions = r.do.map((aid) => {
185
+ const action = this.doc.actions?.[aid];
186
+ return action ? this.action(action, this.stateRef(r.go)) : aid;
187
+ });
188
+ return this.f('rule.do', {
189
+ ...vars,
190
+ do: this.join(actions, 'rule.actions.join'),
191
+ });
192
+ }
193
+ trigger(w) {
194
+ switch (w.kind) {
195
+ case 'start':
196
+ case 'close_open':
197
+ return this.f(`trigger.${w.kind}`);
198
+ case 'cross': {
199
+ // Odds are percentage points (a pin, or a row's reading of one): 60 is 60%, a band 2 points.
200
+ const odds = isOdds(w.value) || isOdds(w.level);
201
+ const side = (e) => odds && e.kind === 'num'
202
+ ? this.f('odds.level', { value: e.value })
203
+ : this.expr(e);
204
+ const vars = { value: side(w.value), level: side(w.level) };
205
+ // Absent band = 0 (the format's default): it re-arms on any reading back past the level.
206
+ const band = w.band === undefined
207
+ ? this.f('trigger.cross.no_band', vars)
208
+ : odds
209
+ ? this.plural('trigger.cross.band.odds', Number(w.band), {
210
+ ...vars,
211
+ band: w.band,
212
+ })
213
+ : this.f('trigger.cross.band', { ...vars, band: w.band });
214
+ return this.f(`trigger.cross.${w.direction}`, { ...vars, band });
215
+ }
216
+ case 'every':
217
+ return w.anchor === undefined
218
+ ? this.f('trigger.every', { period: this.dur(w.period_secs) })
219
+ : this.f('trigger.every.anchor', {
220
+ period: this.dur(w.period_secs),
221
+ anchor: this.time(w.anchor),
222
+ });
223
+ case 'at':
224
+ return this.f(`trigger.at.${w.cadence}`, {
225
+ time: w.time,
226
+ day: w.day
227
+ ? this.f('trigger.at.day', { day: this.f(`day.${w.day}`) })
228
+ : '',
229
+ });
230
+ case 'elapsed':
231
+ return this.f('trigger.elapsed', { after: this.dur(w.after_secs) });
232
+ case 'filled':
233
+ return this.f('trigger.filled', { order: num(w.order) });
234
+ case 'stop_loss':
235
+ case 'take_profit':
236
+ return this.f(`trigger.${w.kind}`, {
237
+ market: this.market(w.market),
238
+ levels: this.join(w.levels.map((lv) => this.stopLevel(lv)), 'levels.join'),
239
+ });
240
+ case 'instances_ended':
241
+ return this.f('trigger.instances_ended', { list: num(w.list) });
242
+ case 'refused':
243
+ return w.action === undefined
244
+ ? this.f('trigger.refused.any')
245
+ : this.f('trigger.refused', { action: num(w.action) });
246
+ case 'source_unreadable':
247
+ case 'source_ended':
248
+ return this.f(`trigger.${w.kind}`, {
249
+ question: this.doc.series?.[w.series]?.label.question ?? w.series,
250
+ });
251
+ }
252
+ }
253
+ stopLevel(lv) {
254
+ // Absent size = the level closes all of it.
255
+ const closes = lv.size
256
+ ? this.f('level.closes.size', { size: this.size(lv.size) })
257
+ : this.f('level.closes.all');
258
+ const line = this.f('level.line', { at: this.level(lv.at), closes });
259
+ this.lines[lv.id] = this.capital(line);
260
+ return line;
261
+ }
262
+ level(at) {
263
+ switch (at.kind) {
264
+ case 'price':
265
+ return this.f('level.price', { price: at.price });
266
+ case 'from_entry':
267
+ return at.pct !== undefined
268
+ ? this.f('level.from_entry.pct', { pct: at.pct })
269
+ : this.f('level.from_entry.distance', {
270
+ distance: at.distance ?? '',
271
+ });
272
+ case 'trailing': {
273
+ const activation = at.activation === undefined
274
+ ? ''
275
+ : this.f('level.trailing.activation', {
276
+ activation: at.activation,
277
+ });
278
+ return at.pct !== undefined
279
+ ? this.f('level.trailing.pct', { pct: at.pct, activation })
280
+ : this.f('level.trailing.distance', {
281
+ distance: at.distance ?? '',
282
+ activation,
283
+ });
284
+ }
285
+ case 'expr':
286
+ return this.f('level.expr', { value: this.expr(at.value) });
287
+ }
288
+ }
289
+ action(a, home = this.f('action.limit_orders.home')) {
290
+ switch (a.kind) {
291
+ case 'order':
292
+ return this.f('action.order', {
293
+ side: this.side(a.side),
294
+ size: this.size(a.size),
295
+ market: this.market(a.market),
296
+ bps: a.max_slippage_bps,
297
+ });
298
+ case 'limit_orders': {
299
+ const orders = a.orders.map((o) => {
300
+ const line = this.f('order.resting', {
301
+ side: this.side(o.side),
302
+ size: this.size(o.size),
303
+ market: this.market(o.market),
304
+ price: this.price(o.price),
305
+ // Absent taker_ok = post-only.
306
+ taker: this.f(o.taker_ok ? 'order.taker_ok' : 'order.post_only'),
307
+ });
308
+ this.lines[o.id] = this.capital(line);
309
+ return line;
310
+ });
311
+ return this.plural('action.limit_orders', a.orders.length, {
312
+ count: a.orders.length,
313
+ orders: this.join(orders, 'orders.join'),
314
+ // Absent keep = the orders are cancelled when the state is left.
315
+ keep: this.f(a.keep ? 'action.limit_orders.keep' : 'action.limit_orders.no_keep', { home }),
316
+ });
317
+ }
318
+ case 'close':
319
+ return this.f('action.close', { market: this.market(a.market) });
320
+ case 'reduce':
321
+ return this.f('action.reduce', {
322
+ market: this.market(a.market),
323
+ by: this.size(a.by),
324
+ });
325
+ case 'close_set':
326
+ return this.f('action.close_set', { list: num(a.list) });
327
+ case 'rebalance': {
328
+ // The first matching row sets a member's weight. A row's condition is read per member (the
329
+ // crate's expression sites put it in the list's scope), whichever rule runs the action.
330
+ const outer = this.inList;
331
+ this.inList = true;
332
+ const weights = a.weights.map((w, i) => {
333
+ const line = this.f(i === 0 ? 'weight.row' : 'weight.row.otherwise', {
334
+ weight: w.weight,
335
+ members: w.if
336
+ ? this.f('weight.if', { cond: this.expr(w.if) })
337
+ : this.f('weight.all'),
338
+ });
339
+ this.lines[w.id] = this.capital(line);
340
+ return line;
341
+ });
342
+ this.inList = outer;
343
+ return this.f('action.rebalance', {
344
+ list: num(a.list),
345
+ total: a.total_rusd === undefined
346
+ ? this.f('action.rebalance.total.account')
347
+ : this.f('action.rebalance.total', { total: a.total_rusd }),
348
+ weights: this.join(weights, 'weights.join'),
349
+ });
350
+ }
351
+ case 'move_stop':
352
+ return this.f('action.move_stop', {
353
+ level: num(a.level),
354
+ rule: this.ruleRef(a.rule),
355
+ to: this.level(a.to),
356
+ });
357
+ }
358
+ }
359
+ list(l) {
360
+ const base = { n: num(l.id), start: this.stateRef(l.start) };
361
+ if (l.kind === 'fixed') {
362
+ const rows = l.members.map((m) => {
363
+ const line = this.f('row', {
364
+ market: m.market,
365
+ side: m.side ? this.f('row.side', { side: this.side(m.side) }) : '',
366
+ size: m.size ? this.f('row.size', { size: this.size(m.size) }) : '',
367
+ reads: m.reads
368
+ ? this.f('row.reads', {
369
+ pins: this.join(m.reads.map((id) => this.pinById(id))),
370
+ })
371
+ : '',
372
+ });
373
+ this.lines[m.id] = line;
374
+ return line;
375
+ });
376
+ return this.f('list.fixed', {
377
+ ...base,
378
+ rows: this.join(rows, 'rows.join'),
379
+ });
380
+ }
381
+ const from = l.from ? this.join(l.from) : this.f('list.from.all');
382
+ return l.kind === 'filter'
383
+ ? this.f('list.filter', {
384
+ ...base,
385
+ max: l.max_size,
386
+ from,
387
+ where: this.expr(l.where),
388
+ })
389
+ : this.f('list.rank', {
390
+ ...base,
391
+ top: l.top,
392
+ from,
393
+ order: this.f(`list.rank.${l.order}`),
394
+ by: this.expr(l.by),
395
+ });
396
+ }
397
+ // ---- values ----
398
+ side(s) {
399
+ return typeof s === 'string' ? this.f(`side.${s}`) : this.f('side.row');
400
+ }
401
+ size(s) {
402
+ if ('rusd' in s)
403
+ return this.f('size.rusd', { rusd: s.rusd });
404
+ if ('qty' in s)
405
+ return this.f('size.qty', { qty: s.qty });
406
+ if ('pct' in s)
407
+ return this.f('size.pct', { pct: s.pct });
408
+ if ('expr' in s)
409
+ return this.f('size.expr', { expr: this.expr(s.expr) });
410
+ return this.f('size.row');
411
+ }
412
+ price(p) {
413
+ if ('fixed' in p)
414
+ return this.f('price.fixed', { price: p.fixed });
415
+ if ('offset_bps' in p) {
416
+ // mark × (1 + bps / 10,000) when sent, whatever the side.
417
+ const bps = p.offset_bps;
418
+ if (bps === 0)
419
+ return this.f('price.offset_bps.zero');
420
+ return this.f(bps > 0 ? 'price.offset_bps.above' : 'price.offset_bps.below', {
421
+ bps: Math.abs(bps),
422
+ });
423
+ }
424
+ return this.f('price.expr', { expr: this.expr(p.expr) });
425
+ }
426
+ expr(e) {
427
+ switch (e.kind) {
428
+ case 'num':
429
+ return e.value;
430
+ case 'series':
431
+ return this.f(`series.${e.series}`, { market: this.market(e.market) });
432
+ case 'outside':
433
+ return this.pinById(e.series);
434
+ case 'row':
435
+ return this.f('row.reading', { n: e.index + 1 });
436
+ case 'input':
437
+ return this.input(e);
438
+ case 'op': {
439
+ // A number compared with odds is in percentage points, as on a crossing.
440
+ const odds = COMPARISONS.has(e.op) && e.args.some(isOdds);
441
+ const args = e.args.map((x) => x.kind === 'op'
442
+ ? this.f('group', { x: this.expr(x) })
443
+ : odds && x.kind === 'num'
444
+ ? this.f('odds.level', { value: x.value })
445
+ : this.expr(x));
446
+ // A wrong count is the service's to refuse; the line still shows every argument.
447
+ if (args.length !== (e.op === 'neg' || e.op === 'not' ? 1 : 2)) {
448
+ return this.f('op.raw', {
449
+ op: e.op,
450
+ args: this.join(args, 'fn.args.join'),
451
+ });
452
+ }
453
+ return this.f(`op.${e.op}`, { a: args[0], b: args[1] ?? '' });
454
+ }
455
+ case 'fn':
456
+ return this.fn(e);
457
+ }
458
+ }
459
+ fn(e) {
460
+ const args = e.args.map((x) => this.expr(x));
461
+ // A windowed function is read on bars: absent interval_secs = 60 (the minute grid), absent
462
+ // anchor = a plain rolling window. Either written on another function is still shown.
463
+ const windowed = WINDOWED.has(e.fn);
464
+ // Each qualifier is its own placeholder, so a translation can order them.
465
+ const extras = {
466
+ window: e.window_secs === undefined
467
+ ? ''
468
+ : this.f('fn.window', { dur: this.dur(e.window_secs) }),
469
+ period: e.period === undefined ? '' : this.f('fn.period', { period: e.period }),
470
+ ago: e.ago_secs === undefined
471
+ ? ''
472
+ : this.f('fn.ago', { dur: this.dur(e.ago_secs) }),
473
+ bars: windowed || e.interval_secs !== undefined
474
+ ? this.f(`fn.bars.${e.interval_secs ?? 60}`)
475
+ : '',
476
+ anchor: ANCHORED.has(e.fn) || e.anchor !== undefined
477
+ ? this.f(`fn.anchor.${e.anchor ?? 'rolling'}`)
478
+ : '',
479
+ };
480
+ if (e.fn === 'n_of_m') {
481
+ // args[0] is N: true when at least N of the other conditions hold.
482
+ const [n = '', ...conds] = args;
483
+ return this.f('fn.n_of_m', {
484
+ n,
485
+ conds: this.join(conds, 'fn.args.join'),
486
+ ...extras,
487
+ });
488
+ }
489
+ if (e.fn === 'if_else') {
490
+ const [cond = '', then = '', otherwise = ''] = args;
491
+ return this.f('fn.if_else', { cond, then, otherwise, ...extras });
492
+ }
493
+ return this.f(`fn.${e.fn}`, {
494
+ args: this.join(args, 'fn.args.join'),
495
+ ...extras,
496
+ });
497
+ }
498
+ input(e) {
499
+ switch (e.name) {
500
+ case 'pnl':
501
+ // Absent market on pnl = the row's market in a list member's rules, else the total.
502
+ return e.market === undefined && !this.inList
503
+ ? this.f('input.pnl.total')
504
+ : this.f('input.pnl', { market: this.market(e.market) });
505
+ case 'rounds':
506
+ case 'wins':
507
+ case 'losses':
508
+ // Absent head = the innermost loop.
509
+ return this.f(`input.${e.name}`, {
510
+ loop: e.head
511
+ ? this.f('input.loop', { state: this.stateRef(e.head) })
512
+ : this.f('input.loop.innermost'),
513
+ });
514
+ case 'remembered':
515
+ return this.f('input.remembered', {
516
+ of: e.of ? this.expr(e.of) : '',
517
+ anchor: this.f(`input.remembered.${e.remember_anchor ?? 'any'}`),
518
+ });
519
+ default:
520
+ return this.f(`input.${e.name}`, { market: this.market(e.market) });
521
+ }
522
+ }
523
+ /** A unix time in UTC, to the second (every second is signed); the card formats it in the reader's zone. */
524
+ time(secs) {
525
+ const d = new Date(secs * 1000);
526
+ if (Number.isNaN(d.getTime()))
527
+ return this.f('time.unix', { secs });
528
+ return d.toISOString().replace('.000Z', 'Z');
529
+ }
530
+ dur(secs) {
531
+ const units = [
532
+ ['week', 604800],
533
+ ['day', 86400],
534
+ ['hour', 3600],
535
+ ['minute', 60],
536
+ ['second', 1],
537
+ ];
538
+ const [unit, size] = units.find(([, s]) => secs % s === 0 && secs >= s) ?? units[4];
539
+ const n = secs / size;
540
+ return this.plural(`dur.${unit}`, n, { n });
541
+ }
542
+ }
543
+ const WINDOWED = new Set([
544
+ 'sma', 'ema', 'high', 'low', 'stdev', 'pct_change', 'corr', 'value_ago', 'rsi', 'atr',
545
+ ]); // prettier-ignore
546
+ /** The window functions that take an `anchor` (the engine's `WindowFn`); others ignore it. */
547
+ const ANCHORED = new Set(['sma', 'ema', 'high', 'low', 'stdev', 'pct_change']);
548
+ /** User text shown inside quotes: a quote in it cannot end the quote early. */
549
+ const quoted = (text) => text.replace(/\\/g, '\\\\').replace(/"/g, '\\"');
550
+ const COMPARISONS = new Set(['gt', 'ge', 'lt', 'le', 'eq', 'ne']);
551
+ const isOdds = (e) => e.kind === 'outside' ||
552
+ e.kind === 'row' ||
553
+ (e.kind === 'input' && e.name === 'remembered' && !!e.of && isOdds(e.of));