@orangecharts/wrun-chart-host 0.3.0 → 0.4.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.
Files changed (50) hide show
  1. package/README.md +11 -5
  2. package/dist/constants/scriptEditor/builtin-script-snippets.d.ts +133 -0
  3. package/dist/constants/scriptEditor/builtin-script-snippets.js +451 -0
  4. package/dist/host-env.d.ts +8 -6
  5. package/dist/host-ports.types.d.ts +7 -22
  6. package/dist/index.d.ts +10 -0
  7. package/dist/index.js +10 -0
  8. package/dist/utility/chart/date-labelled-interval.leaf.d.ts +15 -0
  9. package/dist/utility/chart/date-labelled-interval.leaf.js +19 -0
  10. package/dist/utility/chart/indicator-run-interval.util.d.ts +130 -0
  11. package/dist/utility/chart/indicator-run-interval.util.js +268 -0
  12. package/dist/utility/chart/window-geometry.leaf.d.ts +119 -0
  13. package/dist/utility/chart/window-geometry.leaf.js +391 -0
  14. package/dist/utility/chart/window-owner.leaf.d.ts +19 -0
  15. package/dist/utility/chart/window-owner.leaf.js +24 -0
  16. package/dist/utility/html-escape.leaf.d.ts +11 -0
  17. package/dist/utility/html-escape.leaf.js +20 -0
  18. package/dist/utility/wrun/sources/lanes/option_daily.d.ts +43 -15
  19. package/dist/utility/wrun/sources/lanes/option_daily.js +78 -20
  20. package/dist/utility/wrun/sources/lanes/option_stats_10m.d.ts +14 -10
  21. package/dist/utility/wrun/sources/lanes/option_stats_10m.js +5 -4
  22. package/dist/utility/wrun/sources/opra-option-lanes.util.d.ts +22 -3
  23. package/dist/utility/wrun/sources/opra-option-lanes.util.js +38 -7
  24. package/dist/utility/wrun/sources/registry.js +31 -1
  25. package/dist/utility/wrun/sources/row-lane-registry.js +5 -5
  26. package/dist/utility/wrun/sources/row-lanes/option_contract_state.d.ts +49 -0
  27. package/dist/utility/wrun/sources/row-lanes/option_contract_state.js +74 -18
  28. package/dist/utility/wrun/sources/row-lanes/option_strikes.d.ts +49 -0
  29. package/dist/utility/wrun/sources/row-lanes/option_strikes.js +57 -47
  30. package/dist/utility/wrun/sources/row-lanes/option_surface.d.ts +36 -4
  31. package/dist/utility/wrun/sources/row-lanes/option_surface.js +6 -12
  32. package/dist/utility/wrun/sources/wrun-poll-lane.types.d.ts +37 -3
  33. package/dist/utility/wrun/sources/wrun-poll-lanes.util.d.ts +128 -2
  34. package/dist/utility/wrun/sources/wrun-poll-lanes.util.js +233 -51
  35. package/dist/utility/wrun/sources/wrun-row-lane.types.d.ts +0 -8
  36. package/dist/utility/wrun/sources/wrun-row-lanes.util.d.ts +26 -24
  37. package/dist/utility/wrun/sources/wrun-row-lanes.util.js +85 -104
  38. package/dist/utility/wrun/wrun-opra-stats.leaf.d.ts +29 -10
  39. package/dist/utility/wrun/wrun-opra-stats.leaf.js +29 -10
  40. package/dist/utility/wrun/wrun-panel-mount.lazy.d.ts +10 -0
  41. package/dist/utility/wrun/wrun-panel-mount.lazy.js +68 -0
  42. package/dist/utility/wrun/wrun-panel-mount.util.d.ts +377 -0
  43. package/dist/utility/wrun/wrun-panel-mount.util.js +2251 -0
  44. package/dist/utility/wrun/wrun-pause-dim.util.d.ts +52 -0
  45. package/dist/utility/wrun/wrun-pause-dim.util.js +153 -0
  46. package/dist/utility/wrun/wrun-plot-gate.types.d.ts +23 -0
  47. package/dist/utility/wrun/wrun-plot-gate.types.js +7 -0
  48. package/dist/utility/wrun/wrun-run-interval-projection.types.d.ts +14 -0
  49. package/dist/utility/wrun/wrun-run-interval-projection.types.js +1 -0
  50. package/package.json +1 -1
@@ -0,0 +1,451 @@
1
+ /**
2
+ * kScript snippet helpers shared by every built-in template: the classic input set (source,
3
+ * timeframe, waitForClose), the candle and price series they read, the colour slot reader and the
4
+ * oscillator level / smoothing blocks. `technicalIndicatorScripts.constants.ts` (the classic
5
+ * built-ins) and `staples/*.constants.ts` (the TradingView staples) assemble their templates from
6
+ * these, so every built-in reads its inputs, candles and levels the same way. This module is
7
+ * i18n-free on purpose: the staple modules import it while the catalog module imports them, and a
8
+ * template module must never import the catalog.
9
+ */
10
+ /** Extra closed bars the oscillator smoothing (OSCILLATOR_SMOOTHING_INPUTS) adds to its oscillator's warm-up. */
11
+ export function oscillatorSmoothingWarmup(inputs) {
12
+ return inputs.str('smoothingType') === 'None' ? 0 : inputs.num('smoothingLength') - 1;
13
+ }
14
+ export const CLASSIC_TIMEFRAME_INPUTS = `var timeframe = input(name="timeframe", type="select", defaultValue="chart", options=["chart", "5m", "15m", "30m", "1h", "4h", "1D", "1W"]);
15
+ var waitForClose = input(name="waitForClose", type="boolean", defaultValue=true);`;
16
+ export const CLASSIC_PRICE_INPUTS = `var source = input(name="source", type="select", defaultValue="close", options=["close", "open", "high", "low"]);
17
+ ${CLASSIC_TIMEFRAME_INPUTS}`;
18
+ // field() auto-indexes a timeseries variable in 3.0.87. Passing the deduplicated
19
+ // source call directly retains its series value for the dynamic field selector.
20
+ export const CLASSIC_PRICE_SERIES = `timeseries data = ohlcv(symbol=currentSymbol, exchange=currentExchange);
21
+ timeseries price = timeframe == "chart" ? field(ohlcv(symbol=currentSymbol, exchange=currentExchange), source) : htf(field(ohlcv(symbol=currentSymbol, exchange=currentExchange), source), timeframe, {mode: waitForClose ? "confirmed" : "developing"});`;
22
+ // A conditional timeseries initializer stores one scalar unless given a row.
23
+ // Retain all candle fields and bypass HTF validation entirely in chart mode.
24
+ // Chart mode builds the row from the declared source's own fields: re-calling
25
+ // ohlcv() inside the initializer repeats a source lookup on every bar (quadratic,
26
+ // about 20 s at 10k bars) and candleValues(chartData) reads shifted rows.
27
+ export const CLASSIC_CANDLE_ROW = `func candleValues(bars) {
28
+ return [bars.open, bars.high, bars.low, bars.close, bars.volume];
29
+ }`;
30
+ export const CLASSIC_CANDLE_SERIES = `${CLASSIC_CANDLE_ROW}
31
+ timeseries chartData = ohlcv(symbol=currentSymbol, exchange=currentExchange);
32
+ timeseries data = timeframe == "chart" ? [chartData.open, chartData.high, chartData.low, chartData.close, chartData.volume] : candleValues(htf(chartData, timeframe, {mode: waitForClose ? "confirmed" : "developing"}));`;
33
+ // OHLCV-only builtins retain high/low/volume and use the selected price as close.
34
+ // The default close selection therefore preserves their existing formulas.
35
+ export const CLASSIC_SELECTED_CANDLES = `${CLASSIC_PRICE_SERIES}
36
+ ${CLASSIC_CANDLE_ROW}
37
+ timeseries candles = timeframe == "chart" ? [data.open, data.high, data.low, data.close, data.volume] : candleValues(htf(data, timeframe, {mode: waitForClose ? "confirmed" : "developing"}));
38
+ timeseries selectedCandles = [candles.open, candles.high, candles.low, price, candles.volume];`;
39
+ /**
40
+ * Reads slot `index` of a script's `colors` input as saved, alpha included. The chart completes
41
+ * every saved palette to the built-in's full slot list before a run (completeBuiltinPalette in
42
+ * builtin-script-theme-palette.util), so the first-slot fallback only covers a caller that hands a
43
+ * shorter one. Both branches read the input, so they share its colour type, which plot colour
44
+ * arrays require (a string-literal fallback needed an opacity() wrap, and opacity() forced full
45
+ * alpha and read most colour names as grey).
46
+ */
47
+ export function colorSlot(index) {
48
+ return `(colors.length > ${index} ? colors[${index}] : colors[0])`;
49
+ }
50
+ /** Level inputs shared by the bounded oscillators. */
51
+ export function oscillatorLevelInputs(overbought, oversold) {
52
+ return `var overbought = input(name="overbought", type="number", defaultValue=${overbought}, label="Overbought", group="Levels");
53
+ var oversold = input(name="oversold", type="number", defaultValue=${oversold}, label="Oversold", group="Levels");
54
+ var showZone = input(name="showZone", type="boolean", defaultValue=true, label="Zone", group="Levels");
55
+ var colorExtremes = input(name="colorExtremes", type="boolean", defaultValue=true, label="Color Extremes", group="Levels");`;
56
+ }
57
+ /** Colour slot of the oscillator line on a bar: 0 inside the levels, 1 overbought, 2 oversold. */
58
+ export function oscillatorExtremeIndex(value) {
59
+ return `colorExtremes ? (${value} > overbought ? 1 : ${value} < oversold ? 2 : 0) : 0`;
60
+ }
61
+ /**
62
+ * Dashed overbought / oversold levels, a dotted midline, and the zone between them shaded in the
63
+ * oscillator's own colour. The zone is a box drawn on the last bar from the first bar to well past
64
+ * the last one: drawings carry no alert output and no legend entry, and the last-bar box rolls back
65
+ * and redraws on live ticks.
66
+ */
67
+ export function oscillatorLevelsScript(zoneColor, midline) {
68
+ return `
69
+ // levels
70
+ var levelColor = opacity("#787B86", 70);
71
+ hline(value=overbought, color=levelColor, width=1, lineStyle="dashed");
72
+ hline(value=${midline}, color=levelColor, width=1, lineStyle="dotted");
73
+ hline(value=oversold, color=levelColor, width=1, lineStyle="dashed");
74
+ persist zoneStart = na;
75
+ if (isna(zoneStart)) {
76
+ zoneStart = data.time;
77
+ }
78
+ if (showZone && isLastBar && barIndex > 0) {
79
+ // The alpha rides in the fill colour: the chart's box renderer takes no separate fill opacity.
80
+ box.new(zoneStart, overbought, data.time[0] + 500 * (data.time[0] - data.time[1]), oversold, {fillColor: opacity(${zoneColor}, 6), borderWidth: 0});
81
+ }`;
82
+ }
83
+ export const OSCILLATOR_SMOOTHING_INPUTS = `var smoothingType = input(name="smoothingType", type="select", defaultValue="None", options=["None", "SMA", "SMA + Bollinger Bands", "EMA", "RMA", "WMA", "VWMA"], group="Smoothing");
84
+ var smoothingLength = input(name="smoothingLength", type="number", defaultValue=14, constraints={min: 1, step: 1}, group="Smoothing");
85
+ var bbStdDev = input(name="bbStdDev", type="number", defaultValue=2, constraints={min: 0, step: 0.1}, group="Smoothing");`;
86
+ export function oscillatorSmoothingScript(value, label, width, color) {
87
+ return `
88
+ // Retain oscillator history and the selected timeframe's volume for VWMA.
89
+ timeseries smoothingVolume = timeframe == "chart" ? data.volume : htf(data, timeframe, {mode: waitForClose ? "confirmed" : "developing"}).volume;
90
+ timeseries smoothingData = [${value}, ${value}, ${value}, ${value}, smoothingVolume];
91
+ if (smoothingType != "None") {
92
+ var smoothed = na;
93
+ if (smoothingType == "SMA" || smoothingType == "SMA + Bollinger Bands") {
94
+ smoothed = sma(source=smoothingData.close, period=smoothingLength);
95
+ } else if (smoothingType == "EMA") {
96
+ smoothed = ema(source=smoothingData.close, period=smoothingLength);
97
+ } else if (smoothingType == "RMA") {
98
+ smoothed = rma(source=smoothingData.close, period=smoothingLength);
99
+ } else if (smoothingType == "WMA") {
100
+ smoothed = wma(source=smoothingData.close, period=smoothingLength);
101
+ } else if (smoothingType == "VWMA") {
102
+ smoothed = vwma(source=smoothingData.close, period=smoothingLength);
103
+ }
104
+ plotLine(value=smoothed, width=${width}, colors=[${color}], smooth=smooth, label=["${label}"], desc=["${label}"]);
105
+ if (smoothingType == "SMA + Bollinger Bands") {
106
+ var deviation = bbStdDev * stdev(source=smoothingData.close, period=smoothingLength);
107
+ plotLine(value=smoothed + deviation, width=${width}, colors=[opacity(${color}, 45)], smooth=smooth, label=["Smoothing Upper Band"], desc=["Smoothing Upper Band"]);
108
+ plotLine(value=smoothed - deviation, width=${width}, colors=[opacity(${color}, 45)], smooth=smooth, label=["Smoothing Lower Band"], desc=["Smoothing Lower Band"]);
109
+ }
110
+ }`;
111
+ }
112
+ /*
113
+ * Selected-timeframe math (2026-10).
114
+ *
115
+ * `htf()` returns one row per CHART bar (each selected-timeframe value repeated), and every TA
116
+ * builtin windows over chart rows, so `sma(source=htf(...), period=9)` at 15m on a 1m chart
117
+ * averaged nine chart bars, mostly the same repeated 15m close. The blocks below keep a history of
118
+ * CLOSED selected-timeframe bars in persist state and run the math on it, plus the forming bar when
119
+ * waitForClose is off. Chart mode keeps the builtins untouched: a converted template behaves
120
+ * exactly as before on "chart". Everything the blocks declare starts with `htf`; `data`, `price`,
121
+ * `chartData` and `selectedCandles` keep their chart-mode meaning.
122
+ *
123
+ * A window indicator converts with one branch per output:
124
+ *
125
+ * ${CLASSIC_PRICE_INPUTS}
126
+ * ${HTF_PRICE_SERIES}
127
+ * ${htfHistory('htfPrices', 'htfClosedPrice', 'period')}
128
+ * var output = timeframe == "chart" ? sma(source=price, period=period) : htfAverage(${htfWindow('htfPrices', 'htfFormingPrice')}, period);
129
+ *
130
+ * A recursive one (EMA, RMA, Wilder) keeps its state with htfSmoothedState and reads the name:
131
+ *
132
+ * ${htfSmoothedState('htfEma', 'ema', 'htfClosedPrice', 'htfFormingPrice', 'period')}
133
+ * var output = timeframe == "chart" ? ema(source=price, period=period) : htfEma;
134
+ *
135
+ * Candle templates use HTF_CANDLE_SERIES and read htfClosedCandle.high[0] and friends (see the RSI
136
+ * and ATR templates in tests/constants/scriptEditor/htf-parity.spec.ts for a Wilder chain and a
137
+ * true range over closed candles). Per-bar work is linear: one push per closed bar, one pass over
138
+ * the window per read, and no htf() call outside init.
139
+ *
140
+ * Limits: htf() only sees loaded chart bars, so `period` closed selected-timeframe bars must sit
141
+ * inside the loaded history before a value appears (RSI(14) at 1h on a 1m chart needs 900+ bars).
142
+ * 1D / 1W use "the closed candle changed" as the boundary (their buckets follow the venue session on
143
+ * CME), so two identical consecutive daily candles would merge.
144
+ */
145
+ /**
146
+ * Window and step functions over a history array (oldest first). The window functions read the
147
+ * LAST `p` entries and return na unless all of them are finite, like the strict builtins (sma, wma,
148
+ * stdev, highest, lowest); htfSum skips na and accepts a short window, like sum(). The step
149
+ * functions mirror the engine's seeding (the plain average of the first `p` finite values) and its
150
+ * exact update arithmetic, so a converted template matches the builtin bit for bit.
151
+ */
152
+ export const HTF_WINDOW_FUNCS = `func htfAverage(values, p) {
153
+ if (values.length < p) {
154
+ return na;
155
+ }
156
+ var total = 0;
157
+ for (var k = values.length - p; k < values.length; k = k + 1) {
158
+ if (isna(values[k])) {
159
+ return na;
160
+ }
161
+ total = total + values[k];
162
+ }
163
+ return total / p;
164
+ }
165
+ func htfWeightedAverage(values, p) {
166
+ if (values.length < p) {
167
+ return na;
168
+ }
169
+ var total = 0;
170
+ var start = values.length - p;
171
+ for (var k = 0; k < p; k = k + 1) {
172
+ if (isna(values[start + k])) {
173
+ return na;
174
+ }
175
+ total = total + values[start + k] * (k + 1);
176
+ }
177
+ return total / (p * (p + 1) / 2);
178
+ }
179
+ func htfVolumeWeightedAverage(values, volumes, p) {
180
+ if (values.length < p || volumes.length < p) {
181
+ return na;
182
+ }
183
+ var numerator = 0;
184
+ var denominator = 0;
185
+ for (var k = 0; k < p; k = k + 1) {
186
+ var value = values[values.length - p + k];
187
+ var volume = volumes[volumes.length - p + k];
188
+ if (isna(value) || isna(volume)) {
189
+ return na;
190
+ }
191
+ numerator = numerator + value * volume;
192
+ denominator = denominator + volume;
193
+ }
194
+ return denominator == 0 ? na : numerator / denominator;
195
+ }
196
+ func htfStdev(values, p) {
197
+ var mean = htfAverage(values, p);
198
+ if (isna(mean)) {
199
+ return na;
200
+ }
201
+ var squares = 0;
202
+ for (var k = values.length - p; k < values.length; k = k + 1) {
203
+ var diff = values[k] - mean;
204
+ squares = squares + diff * diff;
205
+ }
206
+ return sqrt(squares / p);
207
+ }
208
+ func htfHighest(values, p) {
209
+ if (values.length < p) {
210
+ return na;
211
+ }
212
+ var best = na;
213
+ for (var k = values.length - p; k < values.length; k = k + 1) {
214
+ if (isna(values[k])) {
215
+ return na;
216
+ }
217
+ if (isna(best) || values[k] > best) {
218
+ best = values[k];
219
+ }
220
+ }
221
+ return best;
222
+ }
223
+ func htfLowest(values, p) {
224
+ if (values.length < p) {
225
+ return na;
226
+ }
227
+ var best = na;
228
+ for (var k = values.length - p; k < values.length; k = k + 1) {
229
+ if (isna(values[k])) {
230
+ return na;
231
+ }
232
+ if (isna(best) || values[k] < best) {
233
+ best = values[k];
234
+ }
235
+ }
236
+ return best;
237
+ }
238
+ func htfHighestOffset(values, p) {
239
+ if (values.length < p) {
240
+ return na;
241
+ }
242
+ var best = na;
243
+ var at = 0;
244
+ for (var k = values.length - p; k < values.length; k = k + 1) {
245
+ if (isna(values[k])) {
246
+ return na;
247
+ }
248
+ if (isna(best) || values[k] >= best) {
249
+ best = values[k];
250
+ at = k;
251
+ }
252
+ }
253
+ return at - (values.length - 1);
254
+ }
255
+ func htfLowestOffset(values, p) {
256
+ if (values.length < p) {
257
+ return na;
258
+ }
259
+ var best = na;
260
+ var at = 0;
261
+ for (var k = values.length - p; k < values.length; k = k + 1) {
262
+ if (isna(values[k])) {
263
+ return na;
264
+ }
265
+ if (isna(best) || values[k] <= best) {
266
+ best = values[k];
267
+ at = k;
268
+ }
269
+ }
270
+ return at - (values.length - 1);
271
+ }
272
+ func htfSum(values, p) {
273
+ var total = 0;
274
+ for (var k = max(0, values.length - p); k < values.length; k = k + 1) {
275
+ if (!isna(values[k])) {
276
+ total = total + values[k];
277
+ }
278
+ }
279
+ return total;
280
+ }
281
+ func htfValueBack(values, n) {
282
+ return values.length > n ? values[values.length - 1 - n] : na;
283
+ }
284
+ func htfEmaNext(previous, count, seedSum, value, p) {
285
+ if (isna(value) || count + 1 < p) {
286
+ return na;
287
+ }
288
+ if (count + 1 == p) {
289
+ return (seedSum + value) / p;
290
+ }
291
+ var alpha = 2 / (p + 1);
292
+ return value * alpha + previous * (1 - alpha);
293
+ }
294
+ func htfRmaNext(previous, count, seedSum, value, p) {
295
+ if (isna(value) || count + 1 < p) {
296
+ return na;
297
+ }
298
+ if (count + 1 == p) {
299
+ return (seedSum + value) / p;
300
+ }
301
+ return (previous * (p - 1) + value) / p;
302
+ }
303
+ func htfRsiOf(gain, loss) {
304
+ if (isna(gain) || isna(loss)) {
305
+ return na;
306
+ }
307
+ return loss == 0 ? 100 : 100 - 100 / (1 + gain / loss);
308
+ }`;
309
+ /**
310
+ * The selected timeframe's closed and forming candles, materialized ONCE at init (a direct htf()
311
+ * of the source runs in init; the "1Y" placeholder only keeps the calls valid in chart mode, which
312
+ * never reads them), and `htfClosed`: true on the chart bar right after a selected-timeframe bar
313
+ * closed. Rolling tokens compare the engine's own epoch-floor bucket of consecutive chart bars;
314
+ * calendar tokens (1D / 1W) fall back to "the closed candle changed". Requires the `timeframe`
315
+ * input (CLASSIC_TIMEFRAME_INPUTS).
316
+ */
317
+ export const HTF_SELECTED_TIMEFRAME = `${HTF_WINDOW_FUNCS}
318
+ timeseries htfBars = ohlcv(symbol=currentSymbol, exchange=currentExchange);
319
+ timeseries htfClosedCandle = htf(htfBars, timeframe == "chart" ? "1Y" : timeframe);
320
+ timeseries htfFormingCandle = htf(htfBars, timeframe == "chart" ? "1Y" : timeframe, {mode: "developing"});
321
+ var htfMs = timeframe == "1m" ? 60000 : timeframe == "3m" ? 180000 : timeframe == "5m" ? 300000 : timeframe == "15m" ? 900000 : timeframe == "30m" ? 1800000 : timeframe == "1h" ? 3600000 : timeframe == "2h" ? 7200000 : timeframe == "4h" ? 14400000 : timeframe == "6h" ? 21600000 : timeframe == "8h" ? 28800000 : timeframe == "12h" ? 43200000 : 0;
322
+ var htfBucketChanged = htfMs > 0 && barIndex > 0 && floor(htfBars.time[0] / htfMs) != floor(htfBars.time[1] / htfMs);
323
+ var htfCandleChanged = isna(htfClosedCandle.close[1]) || htfClosedCandle.close[0] != htfClosedCandle.close[1] || htfClosedCandle.open[0] != htfClosedCandle.open[1] || htfClosedCandle.high[0] != htfClosedCandle.high[1] || htfClosedCandle.low[0] != htfClosedCandle.low[1] || htfClosedCandle.volume[0] != htfClosedCandle.volume[1];
324
+ var htfClosed = timeframe != "chart" && !isna(htfClosedCandle.close[0]) && (htfMs > 0 ? htfBucketChanged : htfCandleChanged);`;
325
+ /**
326
+ * Candle templates: `data` is the chart's own candles in every mode (chart mode reads it exactly as
327
+ * CLASSIC_CANDLE_SERIES did), and the selected timeframe is read from htfClosedCandle /
328
+ * htfFormingCandle (`.high[0]`, `.volume[0]`, ...). Replaces CLASSIC_CANDLE_SERIES.
329
+ */
330
+ export const HTF_CANDLE_SERIES = `timeseries chartData = ohlcv(symbol=currentSymbol, exchange=currentExchange);
331
+ timeseries data = [chartData.open, chartData.high, chartData.low, chartData.close, chartData.volume];
332
+ ${HTF_SELECTED_TIMEFRAME}`;
333
+ /**
334
+ * Price templates: `data` and `price` as CLASSIC_PRICE_SERIES declares them in chart mode, plus
335
+ * the selected price read from the SELECTED candle (open / high / low / close of the closed and of
336
+ * the forming selected-timeframe bar). Requires CLASSIC_PRICE_INPUTS. Replaces CLASSIC_PRICE_SERIES.
337
+ */
338
+ export const HTF_PRICE_SERIES = `timeseries data = ohlcv(symbol=currentSymbol, exchange=currentExchange);
339
+ timeseries price = field(ohlcv(symbol=currentSymbol, exchange=currentExchange), source);
340
+ ${HTF_SELECTED_TIMEFRAME}
341
+ var htfClosedPrice = source == "open" ? htfClosedCandle.open[0] : source == "high" ? htfClosedCandle.high[0] : source == "low" ? htfClosedCandle.low[0] : htfClosedCandle.close[0];
342
+ var htfFormingPrice = source == "open" ? htfFormingCandle.open[0] : source == "high" ? htfFormingCandle.high[0] : source == "low" ? htfFormingCandle.low[0] : htfFormingCandle.close[0];`;
343
+ /**
344
+ * OHLCV templates that use the selected price as close: `selectedCandles` as CLASSIC_SELECTED_CANDLES
345
+ * declares it in chart mode; the selected timeframe reads htfClosedCandle / htfFormingCandle with
346
+ * htfClosedPrice / htfFormingPrice as the close. Replaces CLASSIC_SELECTED_CANDLES.
347
+ */
348
+ export const HTF_SELECTED_CANDLES = `${HTF_PRICE_SERIES}
349
+ timeseries selectedCandles = [data.open, data.high, data.low, price, data.volume];`;
350
+ /**
351
+ * A persist history of `closedValue`, appended once per closed selected-timeframe bar (na values
352
+ * are skipped, so a derived value's warm-up leaves no hole) and capped at `cap` entries, oldest
353
+ * first. `cap` is a kScript expression (`period`, `period + 1`, `slowPeriod`). An offset history
354
+ * passes `keepNa`: one entry per closed bar, na included, so `htfValueBack(history, offset)` counts
355
+ * selected-timeframe bars like `series[offset]` and an undefined value stays undefined instead of
356
+ * repeating the bar before it.
357
+ */
358
+ export function htfHistory(name, closedValue, cap, keepNa = false) {
359
+ return `persist ${name} = [];
360
+ if (htfClosed${keepNa ? '' : ` && !isna(${closedValue})`}) {
361
+ ${name}.push(${closedValue});
362
+ if (${name}.length > ${cap}) {
363
+ ${name}.shift();
364
+ }
365
+ }`;
366
+ }
367
+ /**
368
+ * The values a window reads: the closed history alone when waitForClose is on, the history plus
369
+ * `formingValue` (the forming selected-timeframe bar) when it is off.
370
+ */
371
+ export function htfWindow(history, formingValue) {
372
+ return `(waitForClose ? ${history} : ${history}.concat([${formingValue}]))`;
373
+ }
374
+ /**
375
+ * Recursive smoothing (EMA or RMA / Wilder) over closed selected-timeframe values. Declares the
376
+ * persist state (`<name>Closed`, `<name>Count`, `<name>Seed`), steps it once per closed bar with
377
+ * `closedValue`, and declares `var <name>`: the committed value when waitForClose is on, otherwise
378
+ * a provisional step with `formingValue` that is never committed. `closedValue` must be finite once
379
+ * it starts (leading na only), like the builtins' own seeding. Chain them by feeding one state's
380
+ * `<name>Closed` / `<name>` into the next (MACD's signal line, ATR from a true range).
381
+ */
382
+ export function htfSmoothedState(name, kind, closedValue, formingValue, period) {
383
+ const next = kind === 'ema' ? 'htfEmaNext' : 'htfRmaNext';
384
+ return `persist ${name}Closed = na;
385
+ persist ${name}Count = 0;
386
+ persist ${name}Seed = 0;
387
+ if (htfClosed && !isna(${closedValue})) {
388
+ ${name}Closed = ${next}(${name}Closed, ${name}Count, ${name}Seed, ${closedValue}, ${period});
389
+ ${name}Seed = ${name}Seed + ${closedValue};
390
+ ${name}Count = ${name}Count + 1;
391
+ }
392
+ var ${name} = waitForClose ? ${name}Closed : ${next}(${name}Closed, ${name}Count, ${name}Seed, ${formingValue}, ${period});`;
393
+ }
394
+ /**
395
+ * oscillatorSmoothingScript for a converted oscillator: chart mode smooths `value` with the
396
+ * builtins exactly as before; the selected timeframe smooths the oscillator's own closed values
397
+ * (`closedValue`, read on the bar a selected-timeframe bar closed) plus `formingValue` when
398
+ * waitForClose is off, weighted by the selected timeframe's volume for VWMA. Requires
399
+ * OSCILLATOR_SMOOTHING_INPUTS and one of the HTF_*_SERIES blocks.
400
+ */
401
+ export function htfOscillatorSmoothingScript(value, closedValue, formingValue, label, width, color) {
402
+ return `
403
+ // Retain oscillator history; the selected timeframe keeps its own closed values and volumes.
404
+ timeseries smoothingData = [${value}, ${value}, ${value}, ${value}, data.volume];
405
+ persist htfSmoothValues = [];
406
+ persist htfSmoothVolumes = [];
407
+ if (htfClosed && !isna(${closedValue})) {
408
+ htfSmoothValues.push(${closedValue});
409
+ htfSmoothVolumes.push(htfClosedCandle.volume[0]);
410
+ if (htfSmoothValues.length > smoothingLength) {
411
+ htfSmoothValues.shift();
412
+ htfSmoothVolumes.shift();
413
+ }
414
+ }
415
+ var htfSmoothWindow = ${htfWindow('htfSmoothValues', formingValue)};
416
+ var htfSmoothVolumeWindow = ${htfWindow('htfSmoothVolumes', 'htfFormingCandle.volume[0]')};
417
+ ${htfSmoothedState('htfSmoothEma', 'ema', closedValue, formingValue, 'smoothingLength')}
418
+ ${htfSmoothedState('htfSmoothRma', 'rma', closedValue, formingValue, 'smoothingLength')}
419
+ if (smoothingType != "None") {
420
+ var smoothed = na;
421
+ if (timeframe != "chart") {
422
+ if (smoothingType == "SMA" || smoothingType == "SMA + Bollinger Bands") {
423
+ smoothed = htfAverage(htfSmoothWindow, smoothingLength);
424
+ } else if (smoothingType == "EMA") {
425
+ smoothed = htfSmoothEma;
426
+ } else if (smoothingType == "RMA") {
427
+ smoothed = htfSmoothRma;
428
+ } else if (smoothingType == "WMA") {
429
+ smoothed = htfWeightedAverage(htfSmoothWindow, smoothingLength);
430
+ } else if (smoothingType == "VWMA") {
431
+ smoothed = htfVolumeWeightedAverage(htfSmoothWindow, htfSmoothVolumeWindow, smoothingLength);
432
+ }
433
+ } else if (smoothingType == "SMA" || smoothingType == "SMA + Bollinger Bands") {
434
+ smoothed = sma(source=smoothingData.close, period=smoothingLength);
435
+ } else if (smoothingType == "EMA") {
436
+ smoothed = ema(source=smoothingData.close, period=smoothingLength);
437
+ } else if (smoothingType == "RMA") {
438
+ smoothed = rma(source=smoothingData.close, period=smoothingLength);
439
+ } else if (smoothingType == "WMA") {
440
+ smoothed = wma(source=smoothingData.close, period=smoothingLength);
441
+ } else if (smoothingType == "VWMA") {
442
+ smoothed = vwma(source=smoothingData.close, period=smoothingLength);
443
+ }
444
+ plotLine(value=smoothed, width=${width}, colors=[${color}], smooth=smooth, label=["${label}"], desc=["${label}"]);
445
+ if (smoothingType == "SMA + Bollinger Bands") {
446
+ var deviation = bbStdDev * (timeframe == "chart" ? stdev(source=smoothingData.close, period=smoothingLength) : htfStdev(htfSmoothWindow, smoothingLength));
447
+ plotLine(value=smoothed + deviation, width=${width}, colors=[opacity(${color}, 45)], smooth=smooth, label=["Smoothing Upper Band"], desc=["Smoothing Upper Band"]);
448
+ plotLine(value=smoothed - deviation, width=${width}, colors=[opacity(${color}, 45)], smooth=smooth, label=["Smoothing Lower Band"], desc=["Smoothing Lower Band"]);
449
+ }
450
+ }`;
451
+ }
@@ -187,20 +187,22 @@ export interface WrunHostMarket {
187
187
  * tokenized-markets store); the one site reads only `.kind` (wrun-source-rows.util.ts:2378). */
188
188
  gapPolicyKind(exchange: string | undefined, symbol?: string): string;
189
189
  /** 0.3.0, boot loader. web: import('@/indicators/controls/optionsChartSource') viewed as { resolveOptionsChartSource }
190
- * (async; sources/opra-option-lanes.util.ts:39-40, wrun-opra-underlying.util.ts:27). */
190
+ * (async; sources/opra-option-lanes.util.ts `resolveWrunChartChain`, the one chart chain resolution every chain
191
+ * class reads). */
191
192
  loadOptionsChartSource(): Promise<WrunOptionsChartSourceModule>;
192
193
  /** 0.3.0, boot loader. web: import('@/utility/options/opra-options-gate.util') viewed as { governedOptionPlane,
193
- * opraOptionPlaneStop } (sync, called after the loads: sources/opra-option-lanes.util.ts:85; the stop's type is
194
- * `OpraOptionPlaneStop`). */
194
+ * opraOptionPlaneStop } (sync, called after the loads: sources/opra-option-lanes.util.ts `resolveWrunChartChain`;
195
+ * the stop's type is `OpraOptionPlaneStop`). */
195
196
  loadOptionsGate(): Promise<WrunOptionsGateModule>;
196
197
  /** 0.3.0, boot loader. web: import('@/utility/wrun/wrun-options-chain-venues.util') viewed as
197
- * { resolveWrunOptionsChainCryptoVenues } (sources/opra-option-lanes.util.ts:86-87). */
198
+ * { resolveWrunOptionsChainCryptoVenues } (sources/opra-option-lanes.util.ts `resolveWrunLaneCryptoVenues`). */
198
199
  loadOptionsChainVenues(): Promise<WrunOptionsChainVenuesModule>;
199
200
  /** 0.3.0, boot loader. web: import('@/utility/indicators/deribit-options.util') viewed as { hasDeribitOptions } (sync;
200
- * sources/row-lanes/option_strikes.ts:105). */
201
+ * sources/opra-option-lanes.util.ts `resolveWrunLaneChainChart`, every chain lane's chart resolution). */
201
202
  loadDeribitOptions(): Promise<WrunDeribitOptionsModule>;
202
203
  /** 0.3.0, boot loader. web: import('@/services/option-chain.service') viewed as { optionChainServiceForExchange,
203
- * default: { resolveChainContract } } (sources/opra-option-lanes.util.ts:338, :397). */
204
+ * default: { resolveChainContract } } (sources/opra-option-lanes.util.ts `readWrunLaneChainSeries`, the `auto` book's
205
+ * directory read, and `resolveWrunLaneChainChart`, a continuous CME chart's lead contract). */
204
206
  loadOptionChainService(): Promise<WrunOptionChainServiceModule>;
205
207
  }
206
208
  /** Rows over the wire: the relay, the cloud socket, the kit's main entry, the error envelope. */
@@ -13,6 +13,7 @@
13
13
  import type { WrunKitMainModule } from './host-env.js';
14
14
  import type { RuntimeDiagnostic } from './interfaces/kscript-diagnostics.interface.js';
15
15
  import type { FeedCombineRule as WrunFeedCombineRule, FeedHead as WrunFeedHead, FeedValueKey as WrunFeedValueKey, FeedWireBar as WrunFeedWireBar } from './interfaces/feed.interface.js';
16
+ import type { OverlayRunInterval as WrunOverlayRunInterval } from './utility/chart/indicator-run-interval.util.js';
16
17
  import type { OptionSource as WrunOptionSource } from './utility/options/option-venue.util.js';
17
18
  import type { WrunPollLaneLiveAsk, WrunPollLaneLiveSink, WrunPollLaneLiveSubscription } from './utility/wrun/sources/wrun-poll-lane.types.js';
18
19
  import type { WrunOptionsChainCryptoVenues as WrunOptionsChainVenues, WrunOptionsChainCryptoVenueWord } from './utility/wrun/wrun-options-chain.util.js';
@@ -281,13 +282,9 @@ export interface WrunIntegrationTarget extends WrunDrawingsChart {
281
282
  }
282
283
  /** How a run result integrates (web: `KScriptTitanChartsIntegrationMode`). */
283
284
  export type WrunIntegrationMode = 'metadata' | 'init-data' | 'lazy-data' | 'auto-data';
284
- /** The interval an overlay runs at (web: utility/chart/indicator-run-interval.util.ts `OverlayRunInterval`). */
285
- export interface WrunOverlayRunInterval {
286
- interval: string;
287
- intervalMs: number;
288
- waitForClose: boolean;
289
- sessionLattice?: boolean;
290
- }
285
+ /** The interval an overlay runs at (utility/chart/indicator-run-interval.util.ts `OverlayRunInterval`, moved in
286
+ * phase 1b). */
287
+ export type { WrunOverlayRunInterval };
291
288
  /** The replayed window of a partial replay (web: `KScriptPartialReplayWindow`). */
292
289
  export interface WrunPartialReplayWindow {
293
290
  fromMs: number;
@@ -429,18 +426,6 @@ export interface WrunPackEnsureResult {
429
426
  loaded: string[];
430
427
  failed: string[];
431
428
  }
432
- /** The chart a plot gate notice belongs to. */
433
- export interface WrunPlotGateChart {
434
- metadata: ReadonlyArray<{
435
- id: string;
436
- }>;
437
- }
438
- /** Why a plot kind cannot draw. */
439
- export type WrunPlotGateCause = 'pack-failed' | 'old-engine';
440
- /** One plot kind the gate refused. */
441
- export interface WrunPlotGateRefusal {
442
- kind: string;
443
- name: string;
444
- cause: WrunPlotGateCause;
445
- reason?: string;
446
- }
429
+ /** The chart a plot gate notice belongs to, why a plot kind cannot draw, and one plot kind the gate refused
430
+ * (utility/wrun/wrun-plot-gate.types.ts, moved in phase 1b). */
431
+ export type { WrunPlotGateChart, WrunPlotGateCause, WrunPlotGateRefusal } from './utility/wrun/wrun-plot-gate.types.js';
package/dist/index.d.ts CHANGED
@@ -12,6 +12,7 @@ export * from './constants/marketCapabilities.constants.js';
12
12
  export * from './constants/option-venues.constants.js';
13
13
  export * from './constants/overlay-data.constants.js';
14
14
  export * from './constants/pane-grid.constants.js';
15
+ export * from './constants/scriptEditor/builtin-script-snippets.js';
15
16
  export * from './constants/scriptEditor/technicalIndicatorScriptKeys.constants.js';
16
17
  export * from './constants/tpo-color-schemes.constants.js';
17
18
  export * from './constants/venue-presets.constants.js';
@@ -47,6 +48,7 @@ export * from './utility/chart/cme-realtime.util.js';
47
48
  export * from './utility/chart/cme-session-bucket.util.js';
48
49
  export * from './utility/chart/cme-trade-calendar.util.js';
49
50
  export * from './utility/chart/continuous-contracts.util.js';
51
+ export * from './utility/chart/date-labelled-interval.leaf.js';
50
52
  export * from './utility/chart/feed-interval.util.js';
51
53
  export * from './utility/chart/fetch-dedupe-policy.util.js';
52
54
  export * from './utility/chart/footprint-band.util.js';
@@ -58,6 +60,7 @@ export * from './utility/chart/indicator-coverage-planner.util.js';
58
60
  export * from './utility/chart/indicator-coverage-state.util.js';
59
61
  export * from './utility/chart/indicator-gap-detector.util.js';
60
62
  export * from './utility/chart/indicator-inflight-registry.util.js';
63
+ export * from './utility/chart/indicator-run-interval.util.js';
61
64
  export * from './utility/chart/interval-ladder.util.js';
62
65
  export * from './utility/chart/interval-registry.util.js';
63
66
  export * from './utility/chart/kscript-drawing-prep.util.js';
@@ -89,6 +92,8 @@ export * from './utility/chart/technical-script-legend-value.util.js';
89
92
  export * from './utility/chart/titan-constants.pin.js';
90
93
  export * from './utility/chart/trading-day.util.js';
91
94
  export * from './utility/chart/us-equities-session.util.js';
95
+ export * from './utility/chart/window-geometry.leaf.js';
96
+ export * from './utility/chart/window-owner.leaf.js';
92
97
  export * from './utility/chart/wrun-bar-session-facts.util.js';
93
98
  export * from './utility/chart/wrun-legend-name.util.js';
94
99
  export * from './utility/chart/wrun-run-result.util.js';
@@ -99,6 +104,7 @@ export * from './utility/common/time.util.js';
99
104
  export * from './utility/common/transformations.util.js';
100
105
  export * from './utility/common/visible-timeout.util.js';
101
106
  export * from './utility/exchange-normalization.util.js';
107
+ export * from './utility/html-escape.leaf.js';
102
108
  export * from './utility/indicators/heatmap-sensitivity.util.js';
103
109
  export * from './utility/intl-locale.util.js';
104
110
  export * from './utility/number-format.util.js';
@@ -183,13 +189,16 @@ export * from './utility/wrun/wrun-output-style.util.js';
183
189
  export * from './utility/wrun/wrun-overlay-identity.util.js';
184
190
  export * from './utility/wrun/wrun-overlay-result.util.js';
185
191
  export * from './utility/wrun/wrun-pack.util.js';
192
+ export * from './utility/wrun/wrun-panel-mount.lazy.js';
186
193
  export * from './utility/wrun/wrun-panel-style.lazy.js';
187
194
  export * from './utility/wrun/wrun-panel-style.util.js';
188
195
  export * from './utility/wrun/wrun-param-map.util.js';
189
196
  export * from './utility/wrun/wrun-param-values.util.js';
197
+ export * from './utility/wrun/wrun-pause-dim.util.js';
190
198
  export * from './utility/wrun/wrun-pin-spelling.util.js';
191
199
  export * from './utility/wrun/wrun-pin-venues.util.js';
192
200
  export * from './utility/wrun/wrun-pinned-interval.util.js';
201
+ export * from './utility/wrun/wrun-plot-gate.types.js';
193
202
  export * from './utility/wrun/wrun-present-bits.util.js';
194
203
  export * from './utility/wrun/wrun-protocol-pending.util.js';
195
204
  export * from './utility/wrun/wrun-refusal.util.js';
@@ -198,6 +207,7 @@ export * from './utility/wrun/wrun-render-select.util.js';
198
207
  export * from './utility/wrun/wrun-row-market.util.js';
199
208
  export * from './utility/wrun/wrun-row-source.js';
200
209
  export * from './utility/wrun/wrun-run-health.util.js';
210
+ export * from './utility/wrun/wrun-run-interval-projection.types.js';
201
211
  export * from './utility/wrun/wrun-script-inputs.util.js';
202
212
  export * from './utility/wrun/wrun-setting-words.lazy.js';
203
213
  export * from './utility/wrun/wrun-setting-words.util.js';
package/dist/index.js CHANGED
@@ -15,6 +15,7 @@ export * from './constants/marketCapabilities.constants.js';
15
15
  export * from './constants/option-venues.constants.js';
16
16
  export * from './constants/overlay-data.constants.js';
17
17
  export * from './constants/pane-grid.constants.js';
18
+ export * from './constants/scriptEditor/builtin-script-snippets.js';
18
19
  export * from './constants/scriptEditor/technicalIndicatorScriptKeys.constants.js';
19
20
  export * from './constants/tpo-color-schemes.constants.js';
20
21
  export * from './constants/venue-presets.constants.js';
@@ -50,6 +51,7 @@ export * from './utility/chart/cme-realtime.util.js';
50
51
  export * from './utility/chart/cme-session-bucket.util.js';
51
52
  export * from './utility/chart/cme-trade-calendar.util.js';
52
53
  export * from './utility/chart/continuous-contracts.util.js';
54
+ export * from './utility/chart/date-labelled-interval.leaf.js';
53
55
  export * from './utility/chart/feed-interval.util.js';
54
56
  export * from './utility/chart/fetch-dedupe-policy.util.js';
55
57
  export * from './utility/chart/footprint-band.util.js';
@@ -61,6 +63,7 @@ export * from './utility/chart/indicator-coverage-planner.util.js';
61
63
  export * from './utility/chart/indicator-coverage-state.util.js';
62
64
  export * from './utility/chart/indicator-gap-detector.util.js';
63
65
  export * from './utility/chart/indicator-inflight-registry.util.js';
66
+ export * from './utility/chart/indicator-run-interval.util.js';
64
67
  export * from './utility/chart/interval-ladder.util.js';
65
68
  export * from './utility/chart/interval-registry.util.js';
66
69
  export * from './utility/chart/kscript-drawing-prep.util.js';
@@ -92,6 +95,8 @@ export * from './utility/chart/technical-script-legend-value.util.js';
92
95
  export * from './utility/chart/titan-constants.pin.js';
93
96
  export * from './utility/chart/trading-day.util.js';
94
97
  export * from './utility/chart/us-equities-session.util.js';
98
+ export * from './utility/chart/window-geometry.leaf.js';
99
+ export * from './utility/chart/window-owner.leaf.js';
95
100
  export * from './utility/chart/wrun-bar-session-facts.util.js';
96
101
  export * from './utility/chart/wrun-legend-name.util.js';
97
102
  export * from './utility/chart/wrun-run-result.util.js';
@@ -102,6 +107,7 @@ export * from './utility/common/time.util.js';
102
107
  export * from './utility/common/transformations.util.js';
103
108
  export * from './utility/common/visible-timeout.util.js';
104
109
  export * from './utility/exchange-normalization.util.js';
110
+ export * from './utility/html-escape.leaf.js';
105
111
  export * from './utility/indicators/heatmap-sensitivity.util.js';
106
112
  export * from './utility/intl-locale.util.js';
107
113
  export * from './utility/number-format.util.js';
@@ -186,13 +192,16 @@ export * from './utility/wrun/wrun-output-style.util.js';
186
192
  export * from './utility/wrun/wrun-overlay-identity.util.js';
187
193
  export * from './utility/wrun/wrun-overlay-result.util.js';
188
194
  export * from './utility/wrun/wrun-pack.util.js';
195
+ export * from './utility/wrun/wrun-panel-mount.lazy.js';
189
196
  export * from './utility/wrun/wrun-panel-style.lazy.js';
190
197
  export * from './utility/wrun/wrun-panel-style.util.js';
191
198
  export * from './utility/wrun/wrun-param-map.util.js';
192
199
  export * from './utility/wrun/wrun-param-values.util.js';
200
+ export * from './utility/wrun/wrun-pause-dim.util.js';
193
201
  export * from './utility/wrun/wrun-pin-spelling.util.js';
194
202
  export * from './utility/wrun/wrun-pin-venues.util.js';
195
203
  export * from './utility/wrun/wrun-pinned-interval.util.js';
204
+ export * from './utility/wrun/wrun-plot-gate.types.js';
196
205
  export * from './utility/wrun/wrun-present-bits.util.js';
197
206
  export * from './utility/wrun/wrun-protocol-pending.util.js';
198
207
  export * from './utility/wrun/wrun-refusal.util.js';
@@ -201,6 +210,7 @@ export * from './utility/wrun/wrun-render-select.util.js';
201
210
  export * from './utility/wrun/wrun-row-market.util.js';
202
211
  export * from './utility/wrun/wrun-row-source.js';
203
212
  export * from './utility/wrun/wrun-run-health.util.js';
213
+ export * from './utility/wrun/wrun-run-interval-projection.types.js';
204
214
  export * from './utility/wrun/wrun-script-inputs.util.js';
205
215
  export * from './utility/wrun/wrun-setting-words.lazy.js';
206
216
  export * from './utility/wrun/wrun-setting-words.util.js';