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