@reyaxyz/strategy-format 0.4.0 → 0.5.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/dist/en.json CHANGED
@@ -24,9 +24,23 @@
24
24
  "rule.do": "{when}{if}: {do}, then {go}.",
25
25
  "rule.go": "{when}{if}: {go}.",
26
26
  "rule.go.to": "go to {state}",
27
- "rule.go.round": "go back to {state} for its next round: {reentry}",
28
- "rule.go.round.then": "go back to {state} for its next round (after the last, go to {then}): {reentry}",
29
- "rule.go.to.loop": "go to {state} (after its last round, {then} instead)",
27
+ "rule.go.end.done": "close what the strategy still holds and end",
28
+ "rule.go.end.hold.one": "keep the position, with no more orders",
29
+ "rule.go.end.hold.other": "keep the positions, with no more orders",
30
+ "rule.go.end.hold.guarded.one": "keep the position and open nothing new",
31
+ "rule.go.end.hold.guarded.other": "keep the positions and open nothing new",
32
+ "rule.go.end.done.member": "close that member's position and end that member",
33
+ "rule.go.end.hold.member": "keep that member's position",
34
+ "rule.go.end.named": "{end} ({state})",
35
+ "rule.go.round": "repeat{loop}, with no round limit{reentry}",
36
+ "rule.go.round.then": "repeat{loop}, up to {rounds} rounds in all; after the last, {then}{reentry}",
37
+ "rule.go.to.loop": "go to {state} (up to {rounds} rounds; after the last, {then})",
38
+ "rule.round.loop": " {state}",
39
+ "rule.round.reentry": ". Each repeat starts afresh: {parts}",
40
+ "rule.round.reentry.clocks": "\"once the state has lasted\" clocks restart",
41
+ "rule.round.reentry.crossings": "crossings that have not fired start over",
42
+ "rule.round.reentry.remembered": "values remembered on entry are taken again",
43
+ "rule.round.reentry.orders": "the last round's resting orders that are not kept are cancelled",
30
44
  "rule.go.to.self": "go to {state} (from {state} itself, re-enter it: {reentry})",
31
45
  "rule.go.again": "re-enter {state}: {reentry}",
32
46
  "rule.reentry": "its \"once the state has lasted\" clocks restart, crossings that have not fired start over, values remembered on entry are taken again, and the previous visit's resting orders that are not kept are cancelled",
package/dist/sentences.js CHANGED
@@ -135,6 +135,118 @@ class Builder {
135
135
  return 'close';
136
136
  return s.kind === 'loop_head' ? 'loop' : 'wait';
137
137
  }
138
+ // ---- where a rule goes ----
139
+ /**
140
+ * Where a rule goes, as what happens there (staging, 23 Nov: a card's one line ended "then go to
141
+ * state 3", and nothing said what state 3 did). An end says what it does to the position, a loop
142
+ * head how many rounds it has and what comes after the last; any other state is named.
143
+ */
144
+ goTo(id) {
145
+ const s = this.doc.states[id];
146
+ if (s?.kind === 'final')
147
+ return this.end(id, s);
148
+ // Any entry into a used-up loop head goes to its `then` instead.
149
+ if (s?.kind === 'loop_head' && s.rounds !== undefined && s.then)
150
+ return this.f('rule.go.to.loop', {
151
+ state: this.stateRef(id),
152
+ rounds: s.rounds,
153
+ then: this.thenGo(s.then),
154
+ });
155
+ return this.f('rule.go.to', { state: this.stateRef(id) });
156
+ }
157
+ /** Where a loop goes after its last round: an end as what it does, any other state by name. */
158
+ thenGo(id) {
159
+ const s = this.doc.states[id];
160
+ return s?.kind === 'final'
161
+ ? this.end(id, s)
162
+ : this.f('rule.go.to', { state: this.stateRef(id) });
163
+ }
164
+ /**
165
+ * An end as what it does: a done end closes what the strategy still holds and finishes the run
166
+ * (after a stop or a close, only what is left); a held end keeps the positions and opens nothing
167
+ * new (the run holds until its end date or a Stop). A held end with stops of its own still closes
168
+ * on them, so it never says "no more orders". A named end keeps its name after it.
169
+ */
170
+ end(id, s) {
171
+ const end = s.in_list
172
+ ? this.f(`rule.go.end.${s.outcome}.member`)
173
+ : s.outcome === 'done'
174
+ ? this.f('rule.go.end.done')
175
+ : this.plural(s.on?.length ? 'rule.go.end.hold.guarded' : 'rule.go.end.hold', this.positions(), {});
176
+ return s.name === undefined
177
+ ? end
178
+ : this.f('rule.go.end.named', { end, state: this.stateRef(id) });
179
+ }
180
+ /**
181
+ * How many positions a held end keeps: one per market the actions trade (an action on the row's
182
+ * market counts once). A strategy with a list, or one that trades nothing, has positions.
183
+ */
184
+ positions() {
185
+ if (this.doc.lists?.length)
186
+ return 2;
187
+ const markets = new Set();
188
+ for (const a of Object.values(this.doc.actions ?? {})) {
189
+ if (a.kind === 'order' || a.kind === 'close' || a.kind === 'reduce')
190
+ markets.add(a.market);
191
+ if (a.kind === 'limit_orders')
192
+ for (const o of a.orders)
193
+ markets.add(o.market);
194
+ }
195
+ return markets.size || 2;
196
+ }
197
+ /**
198
+ * A return to the loop head a rule is on: its next round, how many rounds it has, what comes
199
+ * after the last, and what a new round starts afresh. The loop is named only when there are two.
200
+ */
201
+ round(id, head) {
202
+ const loops = Object.values(this.doc.states).filter((s) => s.kind === 'loop_head').length;
203
+ const vars = {
204
+ loop: loops > 1
205
+ ? this.f('rule.round.loop', { state: this.stateRef(id) })
206
+ : '',
207
+ reentry: this.roundReentry(id),
208
+ };
209
+ return head.rounds !== undefined && head.then
210
+ ? this.f('rule.go.round.then', {
211
+ ...vars,
212
+ rounds: head.rounds,
213
+ then: this.thenGo(head.then),
214
+ })
215
+ : this.f('rule.go.round', vars);
216
+ }
217
+ /**
218
+ * What a new round of a loop head starts afresh, only what its rules use (the engine's state
219
+ * entry, reya-chain `runtime/step.rs`): the clocks of its "once the state has lasted" rules, its
220
+ * crossings, values it remembers other than at Start, and the resting orders placed into it that
221
+ * are not kept. Nothing when none of these is used: a basket that buys every minute just repeats.
222
+ */
223
+ roundReentry(id) {
224
+ const rules = (this.doc.states[id]?.on ?? []).flatMap((r) => {
225
+ const rule = this.doc.rules?.[r];
226
+ return rule ? [rule] : [];
227
+ });
228
+ const actions = rules.flatMap((r) => (r.do ?? []).flatMap((a) => {
229
+ const action = this.doc.actions?.[a];
230
+ return action ? [action] : [];
231
+ }));
232
+ const kinds = new Set(rules.map((r) => r.when.kind));
233
+ const resting = Object.values(this.doc.rules ?? {}).some((r) => r.go === id &&
234
+ (r.do ?? []).some((a) => {
235
+ const action = this.doc.actions?.[a];
236
+ return action?.kind === 'limit_orders' && !action.keep;
237
+ }));
238
+ const parts = [
239
+ kinds.has('elapsed') && 'clocks',
240
+ kinds.has('cross') && 'crossings',
241
+ remembersOnEntry([rules, actions]) && 'remembered',
242
+ resting && 'orders',
243
+ ].filter((p) => Boolean(p));
244
+ return parts.length
245
+ ? this.f('rule.round.reentry', {
246
+ parts: this.join(parts.map((p) => this.f(`rule.round.reentry.${p}`))),
247
+ })
248
+ : '';
249
+ }
138
250
  market(market) {
139
251
  return market === undefined ? this.f('market.row') : (0, exports.marketName)(market);
140
252
  }
@@ -177,7 +289,7 @@ class Builder {
177
289
  : this.f('state.loop_head', {
178
290
  ...vars,
179
291
  rounds: s.rounds,
180
- then: s.then ? this.stateRef(s.then) : '',
292
+ then: s.then ? this.thenGo(s.then) : '',
181
293
  });
182
294
  case 'final':
183
295
  // A list member's end closes or holds that member only.
@@ -197,27 +309,19 @@ class Builder {
197
309
  const goVars = {
198
310
  state: this.stateRef(r.go),
199
311
  reentry: this.f('rule.reentry'),
200
- then: target?.kind === 'loop_head' && target.then
201
- ? this.stateRef(target.then)
202
- : '',
203
312
  };
204
313
  const vars = {
205
314
  when: this.trigger(r.when),
206
315
  if: r.if
207
316
  ? this.f(held ? 'rule.if.held' : 'rule.if', { cond: this.expr(r.if) })
208
317
  : '',
209
- go: this.f(fromItself
210
- ? 'rule.go.to.self'
318
+ go: fromItself
319
+ ? this.f('rule.go.to.self', goVars)
211
320
  : !again
212
- ? // Any entry into a used-up loop head goes to its `then` instead.
213
- goVars.then
214
- ? 'rule.go.to.loop'
215
- : 'rule.go.to'
216
- : target?.kind !== 'loop_head'
217
- ? 'rule.go.again'
218
- : goVars.then
219
- ? 'rule.go.round.then'
220
- : 'rule.go.round', goVars),
321
+ ? this.goTo(r.go)
322
+ : target?.kind === 'loop_head'
323
+ ? this.round(r.go, target)
324
+ : this.f('rule.go.again', goVars),
221
325
  };
222
326
  if (!r.do)
223
327
  return this.f('rule.go', vars);
@@ -594,6 +698,19 @@ exports.marketName = marketName;
594
698
  /** User text shown inside quotes: a quote in it cannot end the quote early. */
595
699
  const quoted = (text) => text.replace(/\\/g, '\\\\').replace(/"/g, '\\"');
596
700
  const COMPARISONS = new Set(['gt', 'ge', 'lt', 'le', 'eq', 'ne']);
701
+ /** Whether a value remembered other than at Start is read anywhere in `v`: entering again takes it again. */
702
+ const remembersOnEntry = (v) => {
703
+ if (Array.isArray(v))
704
+ return v.some(remembersOnEntry);
705
+ if (!v || typeof v !== 'object')
706
+ return false;
707
+ const o = v;
708
+ if (o.kind === 'input' &&
709
+ o.name === 'remembered' &&
710
+ o.remember_anchor !== 'start')
711
+ return true;
712
+ return Object.values(v).some(remembersOnEntry);
713
+ };
597
714
  const isOdds = (e) => e.kind === 'outside' ||
598
715
  e.kind === 'row' ||
599
716
  (e.kind === 'input' && e.name === 'remembered' && !!e.of && isOdds(e.of));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reyaxyz/strategy-format",
3
- "version": "0.4.0",
3
+ "version": "0.5.0",
4
4
  "description": "The v3 strategy format for TypeScript: reya-chain's schema and accepted kinds, the reader, the version hash, the block sentences and the line table",
5
5
  "publishConfig": {
6
6
  "access": "public",
@@ -41,5 +41,5 @@
41
41
  "devDependencies": {
42
42
  "typescript": "5.3.3"
43
43
  },
44
- "gitHead": "074c2ae1d2a37acf60104771b7c22904a959e37b"
44
+ "gitHead": "0c1f29cf631f02f9a6eb022aa73bd8b9989624aa"
45
45
  }