@orangecharts/wrun-chart-host 0.1.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/README.md +72 -0
- package/dist/constants/governed-exchanges.constants.d.ts +11 -0
- package/dist/constants/governed-exchanges.constants.js +11 -0
- package/dist/constants/indicator-sets.constants.d.ts +8 -0
- package/dist/constants/indicator-sets.constants.js +8 -0
- package/dist/constants/interval.constants.d.ts +34 -0
- package/dist/constants/interval.constants.js +121 -0
- package/dist/constants/scriptEditor/technicalIndicatorScriptKeys.constants.d.ts +28 -0
- package/dist/constants/scriptEditor/technicalIndicatorScriptKeys.constants.js +65 -0
- package/dist/constants/tpo-color-schemes.constants.d.ts +30 -0
- package/dist/constants/tpo-color-schemes.constants.js +32 -0
- package/dist/host-env.d.ts +102 -0
- package/dist/host-env.js +26 -0
- package/dist/index.d.ts +93 -0
- package/dist/index.js +96 -0
- package/dist/indicators/controls/tpo-letter.util.d.ts +2 -0
- package/dist/indicators/controls/tpo-letter.util.js +13 -0
- package/dist/indicators/controls/tpo-session.util.d.ts +99 -0
- package/dist/indicators/controls/tpo-session.util.js +389 -0
- package/dist/interfaces/kscript-diagnostics.interface.d.ts +64 -0
- package/dist/interfaces/kscript-diagnostics.interface.js +1 -0
- package/dist/interfaces/om-panel-data.interface.d.ts +103 -0
- package/dist/interfaces/om-panel-data.interface.js +7 -0
- package/dist/interfaces/wrun-runtime.interface.d.ts +2659 -0
- package/dist/interfaces/wrun-runtime.interface.js +55 -0
- package/dist/utility/chart/calendar-interval.util.d.ts +24 -0
- package/dist/utility/chart/calendar-interval.util.js +81 -0
- package/dist/utility/chart/candle-bar-end.util.d.ts +15 -0
- package/dist/utility/chart/candle-bar-end.util.js +23 -0
- package/dist/utility/chart/cme-realtime.util.d.ts +24 -0
- package/dist/utility/chart/cme-realtime.util.js +53 -0
- package/dist/utility/chart/interval-ladder.util.d.ts +27 -0
- package/dist/utility/chart/interval-ladder.util.js +92 -0
- package/dist/utility/chart/kscript-drawing-prep.util.d.ts +40 -0
- package/dist/utility/chart/kscript-drawing-prep.util.js +184 -0
- package/dist/utility/chart/kscript-drawing-safe-area.util.d.ts +95 -0
- package/dist/utility/chart/kscript-drawing-safe-area.util.js +805 -0
- package/dist/utility/chart/kscript-legend-entries.util.d.ts +166 -0
- package/dist/utility/chart/kscript-legend-entries.util.js +538 -0
- package/dist/utility/chart/kscript-output-role.util.d.ts +32 -0
- package/dist/utility/chart/kscript-output-role.util.js +215 -0
- package/dist/utility/chart/kscript-provenance-keys.util.d.ts +22 -0
- package/dist/utility/chart/kscript-provenance-keys.util.js +56 -0
- package/dist/utility/chart/kscript-run-result-normalize.util.d.ts +53 -0
- package/dist/utility/chart/kscript-run-result-normalize.util.js +476 -0
- package/dist/utility/chart/kscript-run-result-shape.util.d.ts +14 -0
- package/dist/utility/chart/kscript-run-result-shape.util.js +66 -0
- package/dist/utility/chart/kscript-titancharts-container.util.d.ts +29 -0
- package/dist/utility/chart/kscript-titancharts-container.util.js +151 -0
- package/dist/utility/chart/kscript-viewport-overlays.util.d.ts +83 -0
- package/dist/utility/chart/kscript-viewport-overlays.util.js +443 -0
- package/dist/utility/chart/om-panel-push-style.util.d.ts +78 -0
- package/dist/utility/chart/om-panel-push-style.util.js +301 -0
- package/dist/utility/chart/om-panel.util.d.ts +166 -0
- package/dist/utility/chart/om-panel.util.js +815 -0
- package/dist/utility/chart/om-series.util.d.ts +42 -0
- package/dist/utility/chart/om-series.util.js +330 -0
- package/dist/utility/chart/om-target.util.d.ts +11 -0
- package/dist/utility/chart/om-target.util.js +24 -0
- package/dist/utility/chart/om-widget.util.d.ts +148 -0
- package/dist/utility/chart/om-widget.util.js +413 -0
- package/dist/utility/chart/overlay-output-budget.util.d.ts +16 -0
- package/dist/utility/chart/overlay-output-budget.util.js +109 -0
- package/dist/utility/chart/overlay-paint-flags.util.d.ts +24 -0
- package/dist/utility/chart/overlay-paint-flags.util.js +55 -0
- package/dist/utility/chart/profile-row-height.util.d.ts +21 -0
- package/dist/utility/chart/profile-row-height.util.js +110 -0
- package/dist/utility/chart/session-interval.util.d.ts +1 -0
- package/dist/utility/chart/session-interval.util.js +35 -0
- package/dist/utility/chart/technical-script-legend-value.util.d.ts +18 -0
- package/dist/utility/chart/technical-script-legend-value.util.js +53 -0
- package/dist/utility/chart/trading-day.util.d.ts +70 -0
- package/dist/utility/chart/trading-day.util.js +238 -0
- package/dist/utility/chart/wrun-legend-name.util.d.ts +25 -0
- package/dist/utility/chart/wrun-legend-name.util.js +87 -0
- package/dist/utility/chart/wrun-run-result.util.d.ts +977 -0
- package/dist/utility/chart/wrun-run-result.util.js +5179 -0
- package/dist/utility/chart/wrun-visibility-chart.util.d.ts +25 -0
- package/dist/utility/chart/wrun-visibility-chart.util.js +22 -0
- package/dist/utility/indicators/heatmap-sensitivity.util.d.ts +62 -0
- package/dist/utility/indicators/heatmap-sensitivity.util.js +82 -0
- package/dist/utility/number-format.util.d.ts +68 -0
- package/dist/utility/number-format.util.js +188 -0
- package/dist/utility/v2/opra-governed.util.d.ts +1 -0
- package/dist/utility/v2/opra-governed.util.js +13 -0
- package/dist/utility/wrun/wrun-auto-row.util.d.ts +54 -0
- package/dist/utility/wrun/wrun-auto-row.util.js +96 -0
- package/dist/utility/wrun/wrun-batched-marks.util.d.ts +341 -0
- package/dist/utility/wrun/wrun-batched-marks.util.js +938 -0
- package/dist/utility/wrun/wrun-batched-readout.util.d.ts +75 -0
- package/dist/utility/wrun/wrun-batched-readout.util.js +171 -0
- package/dist/utility/wrun/wrun-box-segment.util.d.ts +185 -0
- package/dist/utility/wrun/wrun-box-segment.util.js +570 -0
- package/dist/utility/wrun/wrun-build-memo.util.d.ts +133 -0
- package/dist/utility/wrun/wrun-build-memo.util.js +301 -0
- package/dist/utility/wrun/wrun-builtin-twin-ports.util.d.ts +25 -0
- package/dist/utility/wrun/wrun-builtin-twin-ports.util.js +75 -0
- package/dist/utility/wrun/wrun-builtin-twin.util.d.ts +9 -0
- package/dist/utility/wrun/wrun-builtin-twin.util.js +40 -0
- package/dist/utility/wrun/wrun-canvas-cache.util.d.ts +28 -0
- package/dist/utility/wrun/wrun-canvas-cache.util.js +56 -0
- package/dist/utility/wrun/wrun-canvas-readout.util.d.ts +69 -0
- package/dist/utility/wrun/wrun-canvas-readout.util.js +53 -0
- package/dist/utility/wrun/wrun-canvases.util.d.ts +295 -0
- package/dist/utility/wrun/wrun-canvases.util.js +1432 -0
- package/dist/utility/wrun/wrun-cap-notice.util.d.ts +77 -0
- package/dist/utility/wrun/wrun-cap-notice.util.js +296 -0
- package/dist/utility/wrun/wrun-card-style.util.d.ts +154 -0
- package/dist/utility/wrun/wrun-card-style.util.js +539 -0
- package/dist/utility/wrun/wrun-chart-context.util.d.ts +89 -0
- package/dist/utility/wrun/wrun-chart-context.util.js +213 -0
- package/dist/utility/wrun/wrun-chart-names.util.d.ts +23 -0
- package/dist/utility/wrun/wrun-chart-names.util.js +33 -0
- package/dist/utility/wrun/wrun-chart-time.util.d.ts +12 -0
- package/dist/utility/wrun/wrun-chart-time.util.js +23 -0
- package/dist/utility/wrun/wrun-child-overlay-id.util.d.ts +13 -0
- package/dist/utility/wrun/wrun-child-overlay-id.util.js +44 -0
- package/dist/utility/wrun/wrun-color-facts.util.d.ts +40 -0
- package/dist/utility/wrun/wrun-color-facts.util.js +88 -0
- package/dist/utility/wrun/wrun-color-settings.lazy.d.ts +26 -0
- package/dist/utility/wrun/wrun-color-settings.lazy.js +9 -0
- package/dist/utility/wrun/wrun-color-settings.util.d.ts +351 -0
- package/dist/utility/wrun/wrun-color-settings.util.js +1363 -0
- package/dist/utility/wrun/wrun-footprint-modes.util.d.ts +23 -0
- package/dist/utility/wrun/wrun-footprint-modes.util.js +24 -0
- package/dist/utility/wrun/wrun-frames.d.ts +217 -0
- package/dist/utility/wrun/wrun-frames.js +1021 -0
- package/dist/utility/wrun/wrun-handles.util.d.ts +792 -0
- package/dist/utility/wrun/wrun-handles.util.js +2050 -0
- package/dist/utility/wrun/wrun-heatmap-cell-budget.util.d.ts +156 -0
- package/dist/utility/wrun/wrun-heatmap-cell-budget.util.js +342 -0
- package/dist/utility/wrun/wrun-hosted-wire-caps.d.ts +218 -0
- package/dist/utility/wrun/wrun-hosted-wire-caps.js +667 -0
- package/dist/utility/wrun/wrun-hosted-wire.util.d.ts +40 -0
- package/dist/utility/wrun/wrun-hosted-wire.util.js +27 -0
- package/dist/utility/wrun/wrun-hover.util.d.ts +182 -0
- package/dist/utility/wrun/wrun-hover.util.js +534 -0
- package/dist/utility/wrun/wrun-hud-looks.util.d.ts +246 -0
- package/dist/utility/wrun/wrun-hud-looks.util.js +587 -0
- package/dist/utility/wrun/wrun-integration-budget.util.d.ts +47 -0
- package/dist/utility/wrun/wrun-integration-budget.util.js +122 -0
- package/dist/utility/wrun/wrun-latest-snapshot.d.ts +19 -0
- package/dist/utility/wrun/wrun-latest-snapshot.js +7 -0
- package/dist/utility/wrun/wrun-levels-child.util.d.ts +131 -0
- package/dist/utility/wrun/wrun-levels-child.util.js +514 -0
- package/dist/utility/wrun/wrun-lifecycle.contract.d.ts +75 -0
- package/dist/utility/wrun/wrun-lifecycle.contract.js +9 -0
- package/dist/utility/wrun/wrun-market-facts.util.d.ts +116 -0
- package/dist/utility/wrun/wrun-market-facts.util.js +238 -0
- package/dist/utility/wrun/wrun-output-style.util.d.ts +344 -0
- package/dist/utility/wrun/wrun-output-style.util.js +1239 -0
- package/dist/utility/wrun/wrun-overlay-identity.util.d.ts +84 -0
- package/dist/utility/wrun/wrun-overlay-identity.util.js +142 -0
- package/dist/utility/wrun/wrun-overlay-result.util.d.ts +120 -0
- package/dist/utility/wrun/wrun-overlay-result.util.js +219 -0
- package/dist/utility/wrun/wrun-pack.util.d.ts +70 -0
- package/dist/utility/wrun/wrun-pack.util.js +301 -0
- package/dist/utility/wrun/wrun-pane-format.util.d.ts +180 -0
- package/dist/utility/wrun/wrun-pane-format.util.js +299 -0
- package/dist/utility/wrun/wrun-panel-style.lazy.d.ts +25 -0
- package/dist/utility/wrun/wrun-panel-style.lazy.js +15 -0
- package/dist/utility/wrun/wrun-panel-style.util.d.ts +174 -0
- package/dist/utility/wrun/wrun-panel-style.util.js +726 -0
- package/dist/utility/wrun/wrun-param-values.util.d.ts +69 -0
- package/dist/utility/wrun/wrun-param-values.util.js +230 -0
- package/dist/utility/wrun/wrun-refusal.util.d.ts +263 -0
- package/dist/utility/wrun/wrun-refusal.util.js +1259 -0
- package/dist/utility/wrun/wrun-registry-errors.util.d.ts +199 -0
- package/dist/utility/wrun/wrun-registry-errors.util.js +257 -0
- package/dist/utility/wrun/wrun-render-select.util.d.ts +336 -0
- package/dist/utility/wrun/wrun-render-select.util.js +1157 -0
- package/dist/utility/wrun/wrun-script-inputs.util.d.ts +434 -0
- package/dist/utility/wrun/wrun-script-inputs.util.js +1971 -0
- package/dist/utility/wrun/wrun-setting-words.lazy.d.ts +20 -0
- package/dist/utility/wrun/wrun-setting-words.lazy.js +9 -0
- package/dist/utility/wrun/wrun-setting-words.util.d.ts +79 -0
- package/dist/utility/wrun/wrun-setting-words.util.js +176 -0
- package/dist/utility/wrun/wrun-socket-row-store.d.ts +98 -0
- package/dist/utility/wrun/wrun-socket-row-store.js +417 -0
- package/dist/utility/wrun/wrun-style-flags.util.d.ts +18 -0
- package/dist/utility/wrun/wrun-style-flags.util.js +21 -0
- package/dist/utility/wrun/wrun-style-words.util.d.ts +148 -0
- package/dist/utility/wrun/wrun-style-words.util.js +439 -0
- package/dist/utility/wrun/wrun-table-look.util.d.ts +125 -0
- package/dist/utility/wrun/wrun-table-look.util.js +225 -0
- package/dist/utility/wrun/wrun-text-look.util.d.ts +51 -0
- package/dist/utility/wrun/wrun-text-look.util.js +227 -0
- package/dist/utility/wrun/wrun-text-params.util.d.ts +106 -0
- package/dist/utility/wrun/wrun-text-params.util.js +171 -0
- package/dist/utility/wrun/wrun-tpo-rows.util.d.ts +71 -0
- package/dist/utility/wrun/wrun-tpo-rows.util.js +438 -0
- package/dist/utility/wrun/wrun-twin-compute-inputs.util.d.ts +14 -0
- package/dist/utility/wrun/wrun-twin-compute-inputs.util.js +58 -0
- package/dist/utility/wrun/wrun-visibility-table.d.ts +437 -0
- package/dist/utility/wrun/wrun-visibility-table.js +237 -0
- package/dist/utility/wrun/wrun-volume-profile.util.d.ts +37 -0
- package/dist/utility/wrun/wrun-volume-profile.util.js +278 -0
- package/package.json +38 -0
|
@@ -0,0 +1,570 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Boxes and segments: per-bar geometry an Indicator package declares on its
|
|
3
|
+
* sheet (`wrun/metadata.json` `boxes` / `segments`) over outputs that already
|
|
4
|
+
* stream, evaluated by the host from the output rows it holds. No wire
|
|
5
|
+
* record carries the geometry: every anchor is a declared numeric output or
|
|
6
|
+
* a literal bar offset, so the sheet additions are legal under wrun-1 and
|
|
7
|
+
* wrun-2 alike and the wasm ABI is untouched.
|
|
8
|
+
*
|
|
9
|
+
* Sheet contract (frozen across hosts):
|
|
10
|
+
* - `boxes[]`: `{ name, top, bottom, x_from, x_to, when, panel, color,
|
|
11
|
+
* border_color, opacity (0..1, default 0.2), border_width (0..10,
|
|
12
|
+
* default 1) }`, at most 16. PT-box-ladder (WRUN-STYLE g2) adds the
|
|
13
|
+
* per-bar colour ladders: `color_by` + `colors` (1..64) or
|
|
14
|
+
* `color_packed_by` for the fill, `border_color_by` + `border_colors`
|
|
15
|
+
* (1..64) or `border_color_packed_by` for the border; a `_by` and its
|
|
16
|
+
* palette come together and never beside the packed form, every name a
|
|
17
|
+
* declared output (data-only ones included).
|
|
18
|
+
* - Every colour key also takes a theme token (`theme.up` ... `theme.accent`,
|
|
19
|
+
* TH-theme-tokens) beside its own grammar; the token rides to the paint
|
|
20
|
+
* pass unresolved.
|
|
21
|
+
* - `segments[]`: `{ name, y_from, y_to, x_from, x_to, when, panel, color,
|
|
22
|
+
* width (0.5..20, default 1), line_style (solid|dashed|dotted, default
|
|
23
|
+
* solid) }`, at most 16.
|
|
24
|
+
* - `x_from` / `x_to` are bar OFFSETS relative to the evaluated bar
|
|
25
|
+
* (negative = past), either a literal in [-500, 500], whole or
|
|
26
|
+
* fractional, or the name of an output whose per-bar value is truncated
|
|
27
|
+
* to an integer; both default 0. `when` optionally gates the bar (drawn
|
|
28
|
+
* only when finite and nonzero). `panel` defaults to the panel of the
|
|
29
|
+
* first y output.
|
|
30
|
+
*
|
|
31
|
+
* Per-bar semantics (`evaluateWrunSpans`): on the run's row grid, bar i
|
|
32
|
+
* draws nothing when the row is not ready, when any referenced output
|
|
33
|
+
* (y anchors, output-valued offsets, `when`) is non-finite there, or when
|
|
34
|
+
* `when` is 0; otherwise the span runs from row i + x_from to row i + x_to
|
|
35
|
+
* (a fractional literal anchors on the row of its whole bars) with anchors
|
|
36
|
+
* clamped to the loaded range's edges, carrying both y values in declared
|
|
37
|
+
* order. An edge's time is om's one placement rule (`wrunSpanEdgeTs`, the
|
|
38
|
+
* vendored wrunShapeEdgeTs): the whole bars on the row grid, a fraction
|
|
39
|
+
* as that share of the run's bar interval, where the kScript engine draws
|
|
40
|
+
* `bars.time[0] - currentInterval * 0.5`. The chart adapter shapes the
|
|
41
|
+
* spans into the kScript engine's own per-bar records (a box = a plotRange
|
|
42
|
+
* record, a segment = a per-bar line drawing), so the forming bar's
|
|
43
|
+
* re-evaluation on every tick REPLACES its record instead of stacking one.
|
|
44
|
+
*
|
|
45
|
+
* The vendored indicator kit types the raw sheet arrays as `WrunBox` /
|
|
46
|
+
* `WrunSegment` (every style field optional). This util reads
|
|
47
|
+
* them as untyped parsed JSON on purpose (a malformed sheet must refuse by
|
|
48
|
+
* name, not typecheck its way past) and hands back the defaults-applied
|
|
49
|
+
* `WrunBoxSpec` / `WrunSegmentSpec` forms the chart adapter consumes.
|
|
50
|
+
*
|
|
51
|
+
* The sheet's `strategy` section (wrun-3, OM-WRUN3-STRATEGIES-CONTRACT
|
|
52
|
+
* section 3) is read here too, in the same style: `readWrunStrategySheet`
|
|
53
|
+
* validates the section with the om validator's wording (every field
|
|
54
|
+
* optional, the engine's own value rules, `{ "param": "<name>" }` links on
|
|
55
|
+
* numeric fields, the four cross-field refusals by name) and hands the
|
|
56
|
+
* validated section to the shared host's resolver
|
|
57
|
+
* (`resolveWrunStrategySheet`, through the seam), which turns it into the
|
|
58
|
+
* engine's declared config at run time. The rows side table at the
|
|
59
|
+
* bottom (`attachWrunStrategy` / `wrunStrategyOf`) carries a run's
|
|
60
|
+
* `StrategyOutput` from the host to the run-result adapter the way the
|
|
61
|
+
* handle set travels.
|
|
62
|
+
*/
|
|
63
|
+
import { wrunChartHostEnv } from '../../host-env.js';
|
|
64
|
+
import { wrunAbiVersion } from './wrun-pack.util.js';
|
|
65
|
+
import { parseWrunThemeToken } from './wrun-style-words.util.js';
|
|
66
|
+
import { abiAtLeast } from '../../interfaces/wrun-runtime.interface.js';
|
|
67
|
+
export const WRUN_BOX_CAP = 16;
|
|
68
|
+
export const WRUN_SEGMENT_CAP = 16;
|
|
69
|
+
/** A box colour ladder (`colors`, `border_colors`) lists 1..64 colours. */
|
|
70
|
+
export const WRUN_BOX_LADDER_CAP = 64;
|
|
71
|
+
/** The color forms the chart adapter's opacity rewrite understands (3/4/6/8
|
|
72
|
+
* digit hex, rgb()/rgba(), hsl()/hsla()). */
|
|
73
|
+
const WRUN_ALPHA_COLOR_RE = /^(?:#(?:[0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})|rgba?\(\s*\d+(?:\.\d+)?\s*,\s*\d+(?:\.\d+)?\s*,\s*\d+(?:\.\d+)?\s*(?:,[^)]*)?\)|hsla?\(\s*-?\d+(?:\.\d+)?(?:deg)?\s*,\s*\d+(?:\.\d+)?%\s*,\s*\d+(?:\.\d+)?%\s*(?:,[^)]*)?\))$/i;
|
|
74
|
+
export function isWrunAlphaColor(color) {
|
|
75
|
+
return WRUN_ALPHA_COLOR_RE.test(color);
|
|
76
|
+
}
|
|
77
|
+
/** A theme token word (`theme.<t>`), legal on every colour key
|
|
78
|
+
* (TH-theme-tokens): it resolves at paint time, alpha included. */
|
|
79
|
+
function isWrunThemeTokenWord(color) {
|
|
80
|
+
return parseWrunThemeToken(color) !== null;
|
|
81
|
+
}
|
|
82
|
+
/** A box fill colour: one the opacity rewrite can re-alpha, or a token. */
|
|
83
|
+
function isWrunBoxFillColor(color) {
|
|
84
|
+
return isWrunAlphaColor(color) || isWrunThemeTokenWord(color);
|
|
85
|
+
}
|
|
86
|
+
/** A border ladder colour: any colour string of at most 64 characters, or a
|
|
87
|
+
* token (the border never takes the opacity). */
|
|
88
|
+
function isWrunBorderLadderColor(color) {
|
|
89
|
+
return (color.length >= 1 && color.length <= 64) || isWrunThemeTokenWord(color);
|
|
90
|
+
}
|
|
91
|
+
const WRUN_VISUAL_PANELS = new Set(['overlay', 'lower']);
|
|
92
|
+
const WRUN_SEGMENT_LINE_STYLES = new Set(['solid', 'dashed', 'dotted']);
|
|
93
|
+
function isRecord(value) {
|
|
94
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
95
|
+
}
|
|
96
|
+
function sheetArray(metadata, field) {
|
|
97
|
+
const value = metadata[field];
|
|
98
|
+
if (value === undefined)
|
|
99
|
+
return [];
|
|
100
|
+
if (!Array.isArray(value))
|
|
101
|
+
throw new Error(`WRUN ${metadata.id}: '${field}' must be an array`);
|
|
102
|
+
return value;
|
|
103
|
+
}
|
|
104
|
+
/** Field readers shared by both declaration kinds; every refusal names the
|
|
105
|
+
* sheet, the declaration and the field (the om validator's spelling). */
|
|
106
|
+
class WrunVisualFields {
|
|
107
|
+
sheetId;
|
|
108
|
+
context;
|
|
109
|
+
entry;
|
|
110
|
+
outputNames;
|
|
111
|
+
constructor(sheetId, context, entry, outputNames) {
|
|
112
|
+
this.sheetId = sheetId;
|
|
113
|
+
this.context = context;
|
|
114
|
+
this.entry = entry;
|
|
115
|
+
this.outputNames = outputNames;
|
|
116
|
+
}
|
|
117
|
+
refuse(field, requirement) {
|
|
118
|
+
throw new Error(`WRUN ${this.sheetId}: ${this.context} '${field}' ${requirement}`);
|
|
119
|
+
}
|
|
120
|
+
outputRef(field) {
|
|
121
|
+
const value = this.entry[field];
|
|
122
|
+
if (typeof value !== 'string' || !this.outputNames.has(value)) {
|
|
123
|
+
throw new Error(`WRUN ${this.sheetId}: ${this.context} '${field}' names an undeclared output`);
|
|
124
|
+
}
|
|
125
|
+
return value;
|
|
126
|
+
}
|
|
127
|
+
optionalOutputRef(field) {
|
|
128
|
+
return this.entry[field] === undefined ? undefined : this.outputRef(field);
|
|
129
|
+
}
|
|
130
|
+
/** A literal bar offset may carry a fraction (the edge then sits inside
|
|
131
|
+
* its bar, wrunSpanEdgeTs); an output name resolves per bar. */
|
|
132
|
+
barOffset(field) {
|
|
133
|
+
const value = this.entry[field];
|
|
134
|
+
if (value === undefined)
|
|
135
|
+
return 0;
|
|
136
|
+
if (typeof value === 'string')
|
|
137
|
+
return this.outputRef(field);
|
|
138
|
+
const { WRUN_MAX_SHAPE_OFFSET_BARS: maxBars } = wrunChartHostEnv().sharedHost();
|
|
139
|
+
if (typeof value !== 'number' || !Number.isFinite(value) || Math.abs(value) > maxBars) {
|
|
140
|
+
this.refuse(field, `must be a bar offset in -${maxBars}..${maxBars} or a declared output name`);
|
|
141
|
+
}
|
|
142
|
+
return value;
|
|
143
|
+
}
|
|
144
|
+
optionalColor(field) {
|
|
145
|
+
const value = this.entry[field];
|
|
146
|
+
if (value === undefined)
|
|
147
|
+
return undefined;
|
|
148
|
+
if (typeof value !== 'string' || value.length === 0)
|
|
149
|
+
this.refuse(field, 'must be a color string');
|
|
150
|
+
return value;
|
|
151
|
+
}
|
|
152
|
+
/** A color the adapter can re-alpha (the box fill rides `opacity`), or a
|
|
153
|
+
* theme token (its alpha rides the word): a named color would draw the
|
|
154
|
+
* fill opaque and drop the opacity silently. */
|
|
155
|
+
optionalAlphaColor(field) {
|
|
156
|
+
const value = this.optionalColor(field);
|
|
157
|
+
if (value !== undefined && !isWrunBoxFillColor(value)) {
|
|
158
|
+
this.refuse(field, 'must be a hex, rgb() or hsl() color or a theme token (the fill takes the opacity)');
|
|
159
|
+
}
|
|
160
|
+
return value;
|
|
161
|
+
}
|
|
162
|
+
/** A per-bar colour ladder (PT-box-ladder): `by` names the output whose
|
|
163
|
+
* floor picks a colour of the 1..64 `table`, or `packed` names an output
|
|
164
|
+
* of packed rgba. The pair comes together and never beside the packed
|
|
165
|
+
* form; every name must be a declared output (data-only ones included). */
|
|
166
|
+
colorLadder(by, table, packed, admits, grammar) {
|
|
167
|
+
const byRef = this.optionalOutputRef(by);
|
|
168
|
+
const packedRef = this.optionalOutputRef(packed);
|
|
169
|
+
if (byRef !== undefined && packedRef !== undefined) {
|
|
170
|
+
this.refuse(packed, `and '${by}' on the same declaration (the packed colour replaces the ladder)`);
|
|
171
|
+
}
|
|
172
|
+
const colors = this.entry[table];
|
|
173
|
+
if (colors === undefined) {
|
|
174
|
+
if (byRef !== undefined)
|
|
175
|
+
this.refuse(by, `needs '${table}' (1..${WRUN_BOX_LADDER_CAP} colours)`);
|
|
176
|
+
return packedRef === undefined ? {} : { packed: packedRef };
|
|
177
|
+
}
|
|
178
|
+
if (byRef === undefined)
|
|
179
|
+
this.refuse(table, `needs '${by}'`);
|
|
180
|
+
if (!Array.isArray(colors) ||
|
|
181
|
+
colors.length < 1 ||
|
|
182
|
+
colors.length > WRUN_BOX_LADDER_CAP ||
|
|
183
|
+
!colors.every((color) => typeof color === 'string' && admits(color))) {
|
|
184
|
+
this.refuse(table, `must list 1..${WRUN_BOX_LADDER_CAP} colours (${grammar})`);
|
|
185
|
+
}
|
|
186
|
+
return { by: byRef, table: [...colors] };
|
|
187
|
+
}
|
|
188
|
+
optionalPanel() {
|
|
189
|
+
const value = this.entry.panel;
|
|
190
|
+
if (value === undefined)
|
|
191
|
+
return undefined;
|
|
192
|
+
if (typeof value !== 'string' || !WRUN_VISUAL_PANELS.has(value))
|
|
193
|
+
this.refuse('panel', "must be 'overlay' or 'lower'");
|
|
194
|
+
return value;
|
|
195
|
+
}
|
|
196
|
+
number(field, fallback, min, max) {
|
|
197
|
+
const value = this.entry[field];
|
|
198
|
+
if (value === undefined)
|
|
199
|
+
return fallback;
|
|
200
|
+
if (typeof value !== 'number' || !Number.isFinite(value) || value < min || value > max) {
|
|
201
|
+
this.refuse(field, `must be a number in ${min}..${max}`);
|
|
202
|
+
}
|
|
203
|
+
return value;
|
|
204
|
+
}
|
|
205
|
+
lineStyle() {
|
|
206
|
+
const value = this.entry.line_style;
|
|
207
|
+
if (value === undefined)
|
|
208
|
+
return 'solid';
|
|
209
|
+
if (typeof value !== 'string' || !WRUN_SEGMENT_LINE_STYLES.has(value)) {
|
|
210
|
+
this.refuse('line_style', "must be 'solid', 'dashed' or 'dotted'");
|
|
211
|
+
}
|
|
212
|
+
return value;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
function readVisualEntries(metadata, field, cap, read) {
|
|
216
|
+
const entries = sheetArray(metadata, field);
|
|
217
|
+
const kind = field === 'boxes' ? 'box' : 'segment';
|
|
218
|
+
if (entries.length > cap) {
|
|
219
|
+
throw new Error(`WRUN ${metadata.id}: declares ${entries.length} ${field}; the cap is ${cap}`);
|
|
220
|
+
}
|
|
221
|
+
const outputNames = new Set(metadata.outputs.map((output) => output.name));
|
|
222
|
+
const seen = new Set();
|
|
223
|
+
return entries.map((entry, at) => {
|
|
224
|
+
if (!isRecord(entry))
|
|
225
|
+
throw new Error(`WRUN ${metadata.id}: ${field}[${at}] must be an object`);
|
|
226
|
+
const name = entry.name;
|
|
227
|
+
if (typeof name !== 'string' || name.length === 0) {
|
|
228
|
+
throw new Error(`WRUN ${metadata.id}: ${field}[${at}] 'name' must be a non-empty string`);
|
|
229
|
+
}
|
|
230
|
+
if (seen.has(name))
|
|
231
|
+
throw new Error(`WRUN ${metadata.id}: ${kind} '${name}' is declared twice`);
|
|
232
|
+
seen.add(name);
|
|
233
|
+
return read(new WrunVisualFields(metadata.id, `${kind} '${name}'`, entry, outputNames), name);
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
/** The sheet's `boxes`, defaults applied, refused by name on a bad
|
|
237
|
+
* declaration (cap, cross-reference, offset, style range). Absent = []. */
|
|
238
|
+
export function readWrunBoxSpecs(metadata) {
|
|
239
|
+
return readVisualEntries(metadata, 'boxes', WRUN_BOX_CAP, (fields, name) => {
|
|
240
|
+
const when = fields.optionalOutputRef('when');
|
|
241
|
+
const panel = fields.optionalPanel();
|
|
242
|
+
const color = fields.optionalAlphaColor('color');
|
|
243
|
+
const borderColor = fields.optionalColor('border_color');
|
|
244
|
+
const fill = fields.colorLadder('color_by', 'colors', 'color_packed_by', isWrunBoxFillColor, 'hex, rgb(), hsl() or a theme token');
|
|
245
|
+
const border = fields.colorLadder('border_color_by', 'border_colors', 'border_color_packed_by', isWrunBorderLadderColor, 'at most 64 characters each, or a theme token');
|
|
246
|
+
return {
|
|
247
|
+
name,
|
|
248
|
+
top: fields.outputRef('top'),
|
|
249
|
+
bottom: fields.outputRef('bottom'),
|
|
250
|
+
x_from: fields.barOffset('x_from'),
|
|
251
|
+
x_to: fields.barOffset('x_to'),
|
|
252
|
+
...(when !== undefined ? { when } : {}),
|
|
253
|
+
...(panel !== undefined ? { panel } : {}),
|
|
254
|
+
...(color !== undefined ? { color } : {}),
|
|
255
|
+
...(borderColor !== undefined ? { border_color: borderColor } : {}),
|
|
256
|
+
opacity: fields.number('opacity', 0.2, 0, 1),
|
|
257
|
+
border_width: fields.number('border_width', 1, 0, 10),
|
|
258
|
+
...(fill.by !== undefined ? { color_by: fill.by, colors: fill.table } : {}),
|
|
259
|
+
...(fill.packed !== undefined ? { color_packed_by: fill.packed } : {}),
|
|
260
|
+
...(border.by !== undefined ? { border_color_by: border.by, border_colors: border.table } : {}),
|
|
261
|
+
...(border.packed !== undefined ? { border_color_packed_by: border.packed } : {}),
|
|
262
|
+
};
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
/** The sheet's `segments`, defaults applied, refused by name on a bad
|
|
266
|
+
* declaration. Absent = []. */
|
|
267
|
+
export function readWrunSegmentSpecs(metadata) {
|
|
268
|
+
return readVisualEntries(metadata, 'segments', WRUN_SEGMENT_CAP, (fields, name) => {
|
|
269
|
+
const when = fields.optionalOutputRef('when');
|
|
270
|
+
const panel = fields.optionalPanel();
|
|
271
|
+
const color = fields.optionalColor('color');
|
|
272
|
+
return {
|
|
273
|
+
name,
|
|
274
|
+
y_from: fields.outputRef('y_from'),
|
|
275
|
+
y_to: fields.outputRef('y_to'),
|
|
276
|
+
x_from: fields.barOffset('x_from'),
|
|
277
|
+
x_to: fields.barOffset('x_to'),
|
|
278
|
+
...(when !== undefined ? { when } : {}),
|
|
279
|
+
...(panel !== undefined ? { panel } : {}),
|
|
280
|
+
...(color !== undefined ? { color } : {}),
|
|
281
|
+
width: fields.number('width', 1, 0.5, 20),
|
|
282
|
+
line_style: fields.lineStyle(),
|
|
283
|
+
};
|
|
284
|
+
});
|
|
285
|
+
}
|
|
286
|
+
/** Evaluate one declaration's spans over the row grid: one entry per row,
|
|
287
|
+
* `null` where the bar draws nothing. `outputIndexByName` maps declared
|
|
288
|
+
* output names to their column; every reference in `refs` must resolve
|
|
289
|
+
* (the readers above guarantee it for sheet declarations). */
|
|
290
|
+
export function evaluateWrunSpans(rows, outputIndexByName, refs) {
|
|
291
|
+
const column = (name) => {
|
|
292
|
+
const index = outputIndexByName.get(name);
|
|
293
|
+
if (index === undefined)
|
|
294
|
+
throw new Error(`WRUN span references '${name}', which does not name a declared output`);
|
|
295
|
+
return index;
|
|
296
|
+
};
|
|
297
|
+
const aIndex = column(refs.a);
|
|
298
|
+
const bIndex = column(refs.b);
|
|
299
|
+
const whenIndex = refs.when === undefined ? undefined : column(refs.when);
|
|
300
|
+
const offsetSource = (ref) => typeof ref === 'string' ? { column: column(ref) } : { literal: ref };
|
|
301
|
+
const fromSource = offsetSource(refs.x_from);
|
|
302
|
+
const toSource = offsetSource(refs.x_to);
|
|
303
|
+
const lastRow = rows.length - 1;
|
|
304
|
+
const clampRow = (rowAt) => Math.min(lastRow, Math.max(0, rowAt));
|
|
305
|
+
return rows.map((row, rowAt) => {
|
|
306
|
+
const outputs = row.outputs;
|
|
307
|
+
if (outputs === null)
|
|
308
|
+
return null;
|
|
309
|
+
const cell = (index) => outputs[index] ?? Number.NaN;
|
|
310
|
+
if (whenIndex !== undefined) {
|
|
311
|
+
const gate = cell(whenIndex);
|
|
312
|
+
if (!Number.isFinite(gate) || gate === 0)
|
|
313
|
+
return null;
|
|
314
|
+
}
|
|
315
|
+
const a = cell(aIndex);
|
|
316
|
+
const b = cell(bIndex);
|
|
317
|
+
if (!Number.isFinite(a) || !Number.isFinite(b))
|
|
318
|
+
return null;
|
|
319
|
+
// A literal offset rides as-is; an output-valued offset is truncated to
|
|
320
|
+
// an integer, and a non-finite value draws nothing for the bar.
|
|
321
|
+
const resolveOffset = (source) => {
|
|
322
|
+
if ('literal' in source)
|
|
323
|
+
return source.literal;
|
|
324
|
+
const value = cell(source.column);
|
|
325
|
+
return Number.isFinite(value) ? Math.trunc(value) : null;
|
|
326
|
+
};
|
|
327
|
+
const fromOffset = resolveOffset(fromSource);
|
|
328
|
+
if (fromOffset === null)
|
|
329
|
+
return null;
|
|
330
|
+
const toOffset = resolveOffset(toSource);
|
|
331
|
+
if (toOffset === null)
|
|
332
|
+
return null;
|
|
333
|
+
return {
|
|
334
|
+
fromRow: clampRow(rowAt + Math.trunc(fromOffset)),
|
|
335
|
+
toRow: clampRow(rowAt + Math.trunc(toOffset)),
|
|
336
|
+
a,
|
|
337
|
+
b,
|
|
338
|
+
};
|
|
339
|
+
});
|
|
340
|
+
}
|
|
341
|
+
/** The time a box or segment edge sits at: `offset` (a literal as written,
|
|
342
|
+
* an output-valued one already truncated) from the bar at `rowAt`, its
|
|
343
|
+
* whole bars through `rowTs` (the run's row grid, with the adapter's reach
|
|
344
|
+
* past the loaded rows) and its fraction as that share of `intervalMs`
|
|
345
|
+
* (the run's bar interval, kScript's currentInterval). om's one placement
|
|
346
|
+
* rule, vendored verbatim: a whole offset is exactly its row's time. */
|
|
347
|
+
export function wrunSpanEdgeTs(rowTs, rowAt, offset, intervalMs) {
|
|
348
|
+
return wrunChartHostEnv().sharedHost().wrunShapeEdgeTs(rowTs, rowAt, offset, intervalMs);
|
|
349
|
+
}
|
|
350
|
+
const STRATEGY_WAY_IN = 'needs abi_version "wrun-3" (wrun-1 and wrun-2 are frozen and have no strategy channel)';
|
|
351
|
+
/** The engine's `strategy()` parameters in sheet spelling: the shared host resolver's
|
|
352
|
+
* WRUN_STRATEGY_SHEET_FIELDS (the sheet is read during a run, after the host loaded). */
|
|
353
|
+
function strategyFieldNames() {
|
|
354
|
+
return Object.keys(wrunChartHostEnv().sharedHost().WRUN_STRATEGY_SHEET_FIELDS);
|
|
355
|
+
}
|
|
356
|
+
function strategyFieldOfEngineParam(param) {
|
|
357
|
+
const entry = Object.entries(wrunChartHostEnv().sharedHost().WRUN_STRATEGY_SHEET_FIELDS).find(([, engine]) => engine === param);
|
|
358
|
+
return entry?.[0];
|
|
359
|
+
}
|
|
360
|
+
/** The strategy refusals the reader names (the shared host's codes). */
|
|
361
|
+
const STRATEGY_SLIPPAGE_MODEL_UNSUPPORTED = 'wrun_strategy_slippage_model_unsupported';
|
|
362
|
+
const STRATEGY_FEED_NOT_OHLCV = 'wrun_strategy_feed_not_ohlcv';
|
|
363
|
+
const STRATEGY_RESERVED_OUTPUT = 'wrun_strategy_reserved_output';
|
|
364
|
+
/** The outputs the host publishes beside a strategy module's own (never in its output count). */
|
|
365
|
+
const STRATEGY_RESERVED_OUTPUT_NAMES = ['strategy.position', 'strategy.equity'];
|
|
366
|
+
/** Every numeric field's validity rule, as the contract table spells it
|
|
367
|
+
* (the engine's `isValidStrategyParamValue`, verbatim in effect). */
|
|
368
|
+
const STRATEGY_NUMERIC_RULES = {
|
|
369
|
+
initial_capital: { expected: 'a number > 0', valid: (value) => Number.isFinite(value) && value > 0 },
|
|
370
|
+
commission_percent: { expected: 'a number >= 0', valid: (value) => Number.isFinite(value) && value >= 0 },
|
|
371
|
+
slippage_bps: { expected: 'a number >= 0', valid: (value) => Number.isFinite(value) && value >= 0 },
|
|
372
|
+
qty_value: { expected: 'a number > 0', valid: (value) => Number.isFinite(value) && value > 0 },
|
|
373
|
+
pyramiding: { expected: 'an integer >= 1', valid: (value) => Number.isInteger(value) && value >= 1 },
|
|
374
|
+
leverage: { expected: 'a number > 0', valid: (value) => Number.isFinite(value) && value > 0 },
|
|
375
|
+
maintenance_margin_percent: {
|
|
376
|
+
expected: 'a number >= 0 and < 100',
|
|
377
|
+
valid: (value) => Number.isFinite(value) && value >= 0 && value < 100,
|
|
378
|
+
},
|
|
379
|
+
maker_fee_percent: { expected: 'a number >= 0', valid: (value) => Number.isFinite(value) && value >= 0 },
|
|
380
|
+
taker_fee_percent: { expected: 'a number >= 0', valid: (value) => Number.isFinite(value) && value >= 0 },
|
|
381
|
+
};
|
|
382
|
+
/** The enum fields and their engine literals, in the engine's order (each list typed by the
|
|
383
|
+
* engine config the shared host declares; wrun-strategy-sheet.spec.ts holds the words to the
|
|
384
|
+
* host's value rule and its expected words). */
|
|
385
|
+
const STRATEGY_ENUM_LITERALS = {
|
|
386
|
+
slippage_model: ['fixed', 'bookEstimate'],
|
|
387
|
+
qty_type: ['fixed', 'percentOfEquity', 'cash'],
|
|
388
|
+
fill_model: ['pessimistic', 'pathHeuristic'],
|
|
389
|
+
instrument: ['spot', 'perps'],
|
|
390
|
+
funding: ['data', 'off'],
|
|
391
|
+
on_liquidation: ['continue', 'halt'],
|
|
392
|
+
};
|
|
393
|
+
const STRATEGY_ENUM_VALUES = STRATEGY_ENUM_LITERALS;
|
|
394
|
+
const STRATEGY_PARAM_LINK_HINT = 'a param link is exactly { "param": "<declared param name>" }';
|
|
395
|
+
/** True when the sheet declares the strategy section (an Indicator that
|
|
396
|
+
* runs the broker), whatever the section holds. */
|
|
397
|
+
export function isWrunStrategySheet(metadata) {
|
|
398
|
+
return metadata.strategy !== undefined;
|
|
399
|
+
}
|
|
400
|
+
/** An unknown `strategy` key is refused by name, never stripped, and the
|
|
401
|
+
* message names the fix for the three likely slips: the engine's camelCase
|
|
402
|
+
* spelling, `title` (the package display name), and a tick setting the
|
|
403
|
+
* engine does not have. */
|
|
404
|
+
function unknownStrategyFieldsMessage(keys) {
|
|
405
|
+
const hints = [];
|
|
406
|
+
for (const key of keys) {
|
|
407
|
+
if (key === 'title') {
|
|
408
|
+
hints.push("'title' is the package display_name, not a sheet field");
|
|
409
|
+
continue;
|
|
410
|
+
}
|
|
411
|
+
const field = strategyFieldOfEngineParam(key);
|
|
412
|
+
if (field !== undefined && field !== key) {
|
|
413
|
+
hints.push(`'${key}' is spelled ${field} in the sheet (snake_case)`);
|
|
414
|
+
continue;
|
|
415
|
+
}
|
|
416
|
+
if (key === 'calc_on_every_tick' || key === 'calcOnEveryTick') {
|
|
417
|
+
hints.push(`'${key}' does not exist (the engine has no such setting; live behavior is fixed)`);
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
return (`unknown strategy field ${keys.map((key) => `'${key}'`).join(', ')}; the strategy fields are ` +
|
|
421
|
+
`${strategyFieldNames().join(', ')}${hints.length > 0 ? ` (${hints.join('; ')})` : ''}`);
|
|
422
|
+
}
|
|
423
|
+
/**
|
|
424
|
+
* The sheet's `strategy` section, validated, or undefined when the sheet
|
|
425
|
+
* declares none. Every refusal names the sheet and the field in the om
|
|
426
|
+
* validator's words; the section itself comes back verbatim (literals and
|
|
427
|
+
* `{ "param": ... }` links, no default injected), the form the shared
|
|
428
|
+
* host's `resolveWrunStrategySheet` resolves against the instance's
|
|
429
|
+
* params at run time. Read during a run only (the host loaded first). Cross-field rules, each refusing by name:
|
|
430
|
+
* - a param link names a declared param (`wrun_strategy_*` links resolve at
|
|
431
|
+
* init() from the instance's params);
|
|
432
|
+
* - `slippage_model: "bookEstimate"` needs an order book provider no host
|
|
433
|
+
* supplies in this rung (`wrun_strategy_slippage_model_unsupported`);
|
|
434
|
+
* - the primary input (index 0) is the broker feed, so it reads source
|
|
435
|
+
* class `ohlcv` on the instance's own market (`wrun_strategy_feed_not_ohlcv`);
|
|
436
|
+
* - `strategy.position` and `strategy.equity` are host-published and
|
|
437
|
+
* reserved across the chart-name namespace (`wrun_strategy_reserved_output`).
|
|
438
|
+
*/
|
|
439
|
+
export function readWrunStrategySheet(metadata) {
|
|
440
|
+
const raw = metadata.strategy;
|
|
441
|
+
if (raw === undefined)
|
|
442
|
+
return undefined;
|
|
443
|
+
const refuse = (detail) => {
|
|
444
|
+
throw new Error(`Indicator ${metadata.id}: ${detail}`);
|
|
445
|
+
};
|
|
446
|
+
if (!abiAtLeast(wrunAbiVersion(metadata), 'wrun-3'))
|
|
447
|
+
refuse(`strategy ${STRATEGY_WAY_IN}`);
|
|
448
|
+
if (!isRecord(raw)) {
|
|
449
|
+
refuse("strategy must be an object (the engine's strategy() fields in snake_case); {} enables the channel with every default");
|
|
450
|
+
}
|
|
451
|
+
const fieldNames = strategyFieldNames();
|
|
452
|
+
const unknown = Object.keys(raw).filter((key) => !fieldNames.includes(key));
|
|
453
|
+
if (unknown.length > 0)
|
|
454
|
+
refuse(unknownStrategyFieldsMessage(unknown));
|
|
455
|
+
const paramNames = new Set(metadata.params.map((param) => param.name));
|
|
456
|
+
const sheet = {};
|
|
457
|
+
for (const field of fieldNames) {
|
|
458
|
+
const value = raw[field];
|
|
459
|
+
if (value === undefined)
|
|
460
|
+
continue;
|
|
461
|
+
const numeric = STRATEGY_NUMERIC_RULES[field];
|
|
462
|
+
if (numeric !== undefined) {
|
|
463
|
+
if (typeof value === 'number') {
|
|
464
|
+
if (!numeric.valid(value))
|
|
465
|
+
refuse(`${field} must be ${numeric.expected} or a { "param": "<declared param name>" } link`);
|
|
466
|
+
sheet[field] = value;
|
|
467
|
+
continue;
|
|
468
|
+
}
|
|
469
|
+
if (!isRecord(value) || typeof value.param !== 'string') {
|
|
470
|
+
refuse(`${field} must be ${numeric.expected} or a { "param": "<declared param name>" } link`);
|
|
471
|
+
}
|
|
472
|
+
const extra = Object.keys(value).filter((key) => key !== 'param');
|
|
473
|
+
if (extra.length > 0) {
|
|
474
|
+
refuse(`unknown key ${extra.map((key) => `'${key}'`).join(', ')} in a param link; ${STRATEGY_PARAM_LINK_HINT}`);
|
|
475
|
+
}
|
|
476
|
+
if (!paramNames.has(value.param)) {
|
|
477
|
+
refuse(`${field} links param '${value.param}', which is not a declared param; a link resolves at init() from ` +
|
|
478
|
+
`the instance's params, so declare it in params (with a numeric default that is ${numeric.expected})`);
|
|
479
|
+
}
|
|
480
|
+
sheet[field] = { param: value.param };
|
|
481
|
+
continue;
|
|
482
|
+
}
|
|
483
|
+
if (field === 'currency') {
|
|
484
|
+
if (typeof value !== 'string')
|
|
485
|
+
refuse('currency must be a string');
|
|
486
|
+
sheet[field] = value;
|
|
487
|
+
continue;
|
|
488
|
+
}
|
|
489
|
+
const literals = STRATEGY_ENUM_VALUES[field];
|
|
490
|
+
if (typeof value !== 'string' || !literals.includes(value)) {
|
|
491
|
+
refuse(`${field} must be one of ${literals.map((literal) => `'${literal}'`).join(', ')}`);
|
|
492
|
+
}
|
|
493
|
+
if (field === 'slippage_model' && value === 'bookEstimate') {
|
|
494
|
+
refuse(`${STRATEGY_SLIPPAGE_MODEL_UNSUPPORTED}: slippage_model "bookEstimate" walks order-book ` +
|
|
495
|
+
'snapshots, which no host supplies in this rung; declare "fixed" (the flat slippage_bps) so the run never ' +
|
|
496
|
+
'silently degrades');
|
|
497
|
+
}
|
|
498
|
+
sheet[field] = value;
|
|
499
|
+
}
|
|
500
|
+
// The broker feed: the primary lane's own candles (the engine's
|
|
501
|
+
// sourceMatchesChartMarket rule; an interval pin is the grid itself and
|
|
502
|
+
// stays legal).
|
|
503
|
+
const primaryInput = [...metadata.inputs].sort((a, b) => a.index - b.index)[0];
|
|
504
|
+
const primarySource = primaryInput ? metadata.inputSources[primaryInput.name] : undefined;
|
|
505
|
+
if (primaryInput && primarySource) {
|
|
506
|
+
const pinned = primarySource.symbol !== undefined || primarySource.exchange !== undefined;
|
|
507
|
+
if (primarySource.source !== 'ohlcv' || pinned) {
|
|
508
|
+
const reads = primarySource.source === 'metric' ? 'a metric composition source' : `source class '${primarySource.source}'`;
|
|
509
|
+
const pin = primarySource.symbol !== undefined && primarySource.exchange !== undefined
|
|
510
|
+
? ` pinned to ${primarySource.symbol}:${primarySource.exchange}`
|
|
511
|
+
: pinned
|
|
512
|
+
? ' pinned to a fixed market'
|
|
513
|
+
: '';
|
|
514
|
+
refuse(`${STRATEGY_FEED_NOT_OHLCV}: a strategy package's primary input (index 0) must read source ` +
|
|
515
|
+
"class 'ohlcv' on the instance's own market (no symbol or exchange pin): the broker fills against the " +
|
|
516
|
+
`primary lane's own candles; input '${primaryInput.name}' reads ${reads}${pin}`);
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
const reserved = new Set(STRATEGY_RESERVED_OUTPUT_NAMES);
|
|
520
|
+
const reservedIn = (what, entries) => {
|
|
521
|
+
for (const entry of entries ?? []) {
|
|
522
|
+
if (!reserved.has(entry.name))
|
|
523
|
+
continue;
|
|
524
|
+
refuse(`${STRATEGY_RESERVED_OUTPUT}: ${what} '${entry.name}' takes a reserved strategy output name; ` +
|
|
525
|
+
'the host publishes strategy.position and strategy.equity itself on every confirmed bar (never in the ' +
|
|
526
|
+
"module's output count), so rename it");
|
|
527
|
+
}
|
|
528
|
+
};
|
|
529
|
+
reservedIn('output', metadata.outputs);
|
|
530
|
+
reservedIn('box', asNamedEntries(metadata.boxes));
|
|
531
|
+
reservedIn('segment', asNamedEntries(metadata.segments));
|
|
532
|
+
reservedIn('renderer', metadata.renderers);
|
|
533
|
+
reservedIn('drawing', metadata.drawings);
|
|
534
|
+
return sheet;
|
|
535
|
+
}
|
|
536
|
+
/** The `name` entries of a raw sheet array (a malformed array is the box
|
|
537
|
+
* and segment readers' concern; this pass only reads names). */
|
|
538
|
+
function asNamedEntries(value) {
|
|
539
|
+
if (!Array.isArray(value))
|
|
540
|
+
return [];
|
|
541
|
+
return value.filter((entry) => isRecord(entry) && typeof entry.name === 'string');
|
|
542
|
+
}
|
|
543
|
+
/** The instance's params by declared name from init()'s positional
|
|
544
|
+
* argument block (`buildWrunInitArgs` order), the values a
|
|
545
|
+
* `{ "param": ... }` link resolves from. */
|
|
546
|
+
export function wrunStrategyParamsOf(metadata, initArgs) {
|
|
547
|
+
const params = {};
|
|
548
|
+
metadata.params.forEach((param, at) => {
|
|
549
|
+
const value = initArgs[at];
|
|
550
|
+
if (value !== undefined)
|
|
551
|
+
params[param.name] = value;
|
|
552
|
+
});
|
|
553
|
+
return params;
|
|
554
|
+
}
|
|
555
|
+
// ---------------------------------------------------------------------------
|
|
556
|
+
// The run's strategy output beside its rows (contract section 4). The host
|
|
557
|
+
// answers the engine's StrategyOutput with every run and every live tick
|
|
558
|
+
// (replace semantics); it rides out hung on the rows array the host
|
|
559
|
+
// returns, keyed by identity, so every integration route that hands the
|
|
560
|
+
// host's rows to the run-result adapter carries it under the run-level
|
|
561
|
+
// `strategy` key without a second argument. Same shape as the handle set's
|
|
562
|
+
// side table in wrun-handles.util.ts.
|
|
563
|
+
// ---------------------------------------------------------------------------
|
|
564
|
+
const strategyByRows = new WeakMap();
|
|
565
|
+
export function attachWrunStrategy(rows, strategy) {
|
|
566
|
+
strategyByRows.set(rows, strategy);
|
|
567
|
+
}
|
|
568
|
+
export function wrunStrategyOf(rows) {
|
|
569
|
+
return strategyByRows.get(rows);
|
|
570
|
+
}
|