@reyaxyz/strategy-format 0.3.1 → 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/index.d.ts CHANGED
@@ -1808,7 +1808,7 @@ export declare const schemaSource: {
1808
1808
  "schema/workflow-v3.accepted-kinds.json": string;
1809
1809
  };
1810
1810
  };
1811
- export { sentences } from './sentences';
1811
+ export { marketName, sentences } from './sentences';
1812
1812
  export type { Mode, SentenceKey, Translations } from './sentences';
1813
1813
  export { line, lineId, LINES, FOLLOW_ONS, needsYouLine, NEEDS_YOU_CODES, activityLine, ACTIVITY_CODES, ACTIVITY_NOT_LISTED, ENTRY_DROPPED, endedLine, PAPER_LINES, END_REASONS, READ_OUTS, gradedFigure, whatIfLine, WHAT_IF_RESPONSES, } from './lines';
1814
1814
  export type { LineKey, LineTranslations, LineVars, SayLine, RunStatus, NeedsYouItem, ActivityEntry, Grade, WhatIfRow, } from './lines';
package/dist/index.js CHANGED
@@ -17,7 +17,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
17
17
  return (mod && mod.__esModule) ? mod : { "default": mod };
18
18
  };
19
19
  Object.defineProperty(exports, "__esModule", { value: true });
20
- exports.WHAT_IF_RESPONSES = exports.whatIfLine = exports.gradedFigure = exports.READ_OUTS = exports.END_REASONS = exports.PAPER_LINES = exports.endedLine = exports.ENTRY_DROPPED = exports.ACTIVITY_NOT_LISTED = exports.ACTIVITY_CODES = exports.activityLine = exports.NEEDS_YOU_CODES = exports.needsYouLine = exports.FOLLOW_ONS = exports.LINES = exports.lineId = exports.line = exports.sentences = exports.schemaSource = exports.acceptedKinds = exports.workflowV3Schema = exports.versionHash = exports.canonical = exports.MAX_DOCUMENT_BYTES = exports.readStrategy = void 0;
20
+ exports.WHAT_IF_RESPONSES = exports.whatIfLine = exports.gradedFigure = exports.READ_OUTS = exports.END_REASONS = exports.PAPER_LINES = exports.endedLine = exports.ENTRY_DROPPED = exports.ACTIVITY_NOT_LISTED = exports.ACTIVITY_CODES = exports.activityLine = exports.NEEDS_YOU_CODES = exports.needsYouLine = exports.FOLLOW_ONS = exports.LINES = exports.lineId = exports.line = exports.sentences = exports.marketName = exports.schemaSource = exports.acceptedKinds = exports.workflowV3Schema = exports.versionHash = exports.canonical = exports.MAX_DOCUMENT_BYTES = exports.readStrategy = void 0;
21
21
  const workflow_v3_schema_json_1 = __importDefault(require("./schema/workflow-v3.schema.json"));
22
22
  const workflow_v3_accepted_kinds_json_1 = __importDefault(require("./schema/workflow-v3.accepted-kinds.json"));
23
23
  const source_json_1 = __importDefault(require("./schema/source.json"));
@@ -34,6 +34,7 @@ exports.workflowV3Schema = workflow_v3_schema_json_1.default;
34
34
  exports.acceptedKinds = workflow_v3_accepted_kinds_json_1.default;
35
35
  exports.schemaSource = source_json_1.default;
36
36
  var sentences_1 = require("./sentences");
37
+ Object.defineProperty(exports, "marketName", { enumerable: true, get: function () { return sentences_1.marketName; } });
37
38
  Object.defineProperty(exports, "sentences", { enumerable: true, get: function () { return sentences_1.sentences; } });
38
39
  var lines_1 = require("./lines");
39
40
  Object.defineProperty(exports, "line", { enumerable: true, get: function () { return lines_1.line; } });
package/dist/lines.json CHANGED
@@ -42,7 +42,8 @@
42
42
  "why-nothing": "The strategy's record has no event that explains it. The stop is at {level}; the mark is {mark} as of {time}.",
43
43
  "size-from-share": "{pct}% of your balance is {amount} USDC today, so each order is {amount} USDC.",
44
44
  "feature-unavailable": "{asked} is not available in strategies.",
45
- "pin-question": "Which prediction market should this read?",
45
+ "pin-question": "I found {count} questions on Polymarket. Pick the one you mean.",
46
+ "pin-question.one": "I found the question on Polymarket. Check it is the one you mean.",
46
47
  "pin-source": "Polymarket, as of {time} UTC",
47
48
  "pin-option": "{outcome} · {pct}% now · listed end {date}",
48
49
  "pin-thin": " · thin market: ${volume} traded in 24h, ${depth} near the price",
@@ -21,3 +21,5 @@ export type Translations = Record<SentenceKey, string> & Record<string, string>;
21
21
  */
22
22
  export declare function sentences(doc: StrategyV3, t?: Translations, locale?: string, mode?: Mode): Record<string, string>;
23
23
  export type Mode = 'live' | 'paper';
24
+ /** A perp market as the app names it: `BTCRUSDPERP` is `BTC-PERP`; any other code as written. */
25
+ export declare const marketName: (market: string) => string;
package/dist/sentences.js CHANGED
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.sentences = void 0;
6
+ exports.marketName = exports.sentences = void 0;
7
7
  const en_json_1 = __importDefault(require("./en.json"));
8
8
  /**
9
9
  * One line per block, keyed by block id: every state, rule, action, pin and list, and every block
@@ -135,8 +135,120 @@ 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
- return market ?? this.f('market.row');
251
+ return market === undefined ? this.f('market.row') : (0, exports.marketName)(market);
140
252
  }
141
253
  pinById(id) {
142
254
  const pin = this.doc.series?.[id];
@@ -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);
@@ -402,7 +506,7 @@ class Builder {
402
506
  if (l.kind === 'fixed') {
403
507
  const rows = l.members.map((m) => {
404
508
  const line = this.f('row', {
405
- market: m.market,
509
+ market: (0, exports.marketName)(m.market),
406
510
  side: m.side ? this.f('row.side', { side: this.side(m.side) }) : '',
407
511
  size: m.size ? this.f('row.size', { size: this.size(m.size) }) : '',
408
512
  reads: m.reads
@@ -419,7 +523,9 @@ class Builder {
419
523
  rows: this.join(rows, 'rows.join'),
420
524
  });
421
525
  }
422
- const from = l.from ? this.join(l.from) : this.f('list.from.all');
526
+ const from = l.from
527
+ ? this.join(l.from.map(exports.marketName))
528
+ : this.f('list.from.all');
423
529
  return l.kind === 'filter'
424
530
  ? this.f('list.filter', {
425
531
  ...base,
@@ -586,9 +692,25 @@ const WINDOWED = new Set([
586
692
  ]); // prettier-ignore
587
693
  /** The window functions that take an `anchor` (the engine's `WindowFn`); others ignore it. */
588
694
  const ANCHORED = new Set(['sma', 'ema', 'high', 'low', 'stdev', 'pct_change']);
695
+ /** A perp market as the app names it: `BTCRUSDPERP` is `BTC-PERP`; any other code as written. */
696
+ const marketName = (market) => market.replace(/^([A-Z0-9]+)RUSDPERP$/, '$1-PERP');
697
+ exports.marketName = marketName;
589
698
  /** User text shown inside quotes: a quote in it cannot end the quote early. */
590
699
  const quoted = (text) => text.replace(/\\/g, '\\\\').replace(/"/g, '\\"');
591
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
+ };
592
714
  const isOdds = (e) => e.kind === 'outside' ||
593
715
  e.kind === 'row' ||
594
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.3.1",
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": "004e1bb86cf7d4258a6750e139c7f35bc8fd3b1b"
44
+ "gitHead": "0c1f29cf631f02f9a6eb022aa73bd8b9989624aa"
45
45
  }