oakscriptjs 0.11.6 → 0.12.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 (63) hide show
  1. package/dist/cjs/drawing/registry.d.ts +2 -0
  2. package/dist/cjs/drawing/registry.d.ts.map +1 -1
  3. package/dist/cjs/drawing/registry.js +7 -0
  4. package/dist/cjs/drawing/registry.js.map +2 -2
  5. package/dist/cjs/index.d.ts +4 -3
  6. package/dist/cjs/index.d.ts.map +1 -1
  7. package/dist/cjs/index.js +6 -1
  8. package/dist/cjs/index.js.map +2 -2
  9. package/dist/cjs/runtime/inputs.d.ts.map +1 -1
  10. package/dist/cjs/runtime/inputs.js +6 -2
  11. package/dist/cjs/runtime/inputs.js.map +2 -2
  12. package/dist/cjs/runtime/types.d.ts +69 -0
  13. package/dist/cjs/runtime/types.d.ts.map +1 -1
  14. package/dist/cjs/runtime/types.js.map +1 -1
  15. package/dist/cjs/script/index.d.ts +24 -2
  16. package/dist/cjs/script/index.d.ts.map +1 -1
  17. package/dist/cjs/script/index.js +58 -21
  18. package/dist/cjs/script/index.js.map +2 -2
  19. package/dist/cjs/types/metadata.d.ts +8 -0
  20. package/dist/cjs/types/metadata.d.ts.map +1 -1
  21. package/dist/cjs/types/metadata.js.map +1 -1
  22. package/dist/cjs/utils/index.d.ts.map +1 -1
  23. package/dist/cjs/utils/index.js +13 -2
  24. package/dist/cjs/utils/index.js.map +2 -2
  25. package/dist/cjs/utils/source-resolver.d.ts +17 -0
  26. package/dist/cjs/utils/source-resolver.d.ts.map +1 -0
  27. package/dist/cjs/utils/source-resolver.js +45 -0
  28. package/dist/cjs/utils/source-resolver.js.map +7 -0
  29. package/dist/esm/drawing/registry.d.ts +2 -0
  30. package/dist/esm/drawing/registry.d.ts.map +1 -1
  31. package/dist/esm/drawing/registry.js +8 -0
  32. package/dist/esm/drawing/registry.js.map +1 -1
  33. package/dist/esm/index.d.ts +4 -3
  34. package/dist/esm/index.d.ts.map +1 -1
  35. package/dist/esm/index.js +3 -2
  36. package/dist/esm/index.js.map +1 -1
  37. package/dist/esm/runtime/inputs.d.ts.map +1 -1
  38. package/dist/esm/runtime/inputs.js +8 -2
  39. package/dist/esm/runtime/inputs.js.map +1 -1
  40. package/dist/esm/runtime/types.d.ts +69 -0
  41. package/dist/esm/runtime/types.d.ts.map +1 -1
  42. package/dist/esm/script/index.d.ts +24 -2
  43. package/dist/esm/script/index.d.ts.map +1 -1
  44. package/dist/esm/script/index.js +57 -15
  45. package/dist/esm/script/index.js.map +1 -1
  46. package/dist/esm/types/metadata.d.ts +8 -0
  47. package/dist/esm/types/metadata.d.ts.map +1 -1
  48. package/dist/esm/utils/index.d.ts.map +1 -1
  49. package/dist/esm/utils/index.js +18 -1
  50. package/dist/esm/utils/index.js.map +1 -1
  51. package/dist/esm/utils/source-resolver.d.ts +17 -0
  52. package/dist/esm/utils/source-resolver.d.ts.map +1 -0
  53. package/dist/esm/utils/source-resolver.js +24 -0
  54. package/dist/esm/utils/source-resolver.js.map +1 -0
  55. package/package.json +1 -1
  56. package/src/drawing/registry.ts +10 -0
  57. package/src/index.ts +6 -1
  58. package/src/runtime/inputs.ts +7 -2
  59. package/src/runtime/types.ts +65 -0
  60. package/src/script/index.ts +80 -21
  61. package/src/types/metadata.ts +8 -0
  62. package/src/utils/index.ts +15 -1
  63. package/src/utils/source-resolver.ts +29 -0
@@ -184,6 +184,8 @@ export interface PlotData {
184
184
  * Horizontal line data
185
185
  */
186
186
  export interface HLineData {
187
+ /** Id of the declaration that produced it (its config entry) */
188
+ id?: string;
187
189
  /** Y-axis value */
188
190
  value: number;
189
191
  /** Line options */
@@ -194,6 +196,8 @@ export interface HLineData {
194
196
  * Fill data (area between two plots)
195
197
  */
196
198
  export interface FillData {
199
+ /** Id of the declaration that produced it (its config entry) */
200
+ id?: string;
197
201
  /** First plot index or name */
198
202
  plot1: number | string;
199
203
  /** Second plot index or name */
@@ -345,6 +349,8 @@ export interface ArrowData {
345
349
  * Per-bar candle color (barcolor() output)
346
350
  */
347
351
  export interface BarColorData {
352
+ /** Id of the declaration that produced it (its config entry) */
353
+ id?: string;
348
354
  /** Time of the colored bar (offset already applied) */
349
355
  time: number;
350
356
  /** Color */
@@ -355,6 +361,8 @@ export interface BarColorData {
355
361
  * Per-bar background color (bgcolor() output)
356
362
  */
357
363
  export interface BgColorData {
364
+ /** Id of the declaration that produced it (its config entry) */
365
+ id?: string;
358
366
  /** Time of the colored bar (offset already applied) */
359
367
  time: number;
360
368
  /** Color (with its transparency) */
@@ -8,6 +8,7 @@ import { sma, ema, wma, rma } from '../ta/index.js';
8
8
  import { Series } from '../runtime/series.js';
9
9
  import type { SourceType } from '../input.js';
10
10
  import { isDeleted } from '../drawing/registry.js';
11
+ import { externalSource } from './source-resolver.js';
11
12
 
12
13
  /**
13
14
  * Validates that a series has sufficient data for a given length
@@ -197,6 +198,9 @@ export function getSource(
197
198
  case 'hlcc4': return ohlc.high.map((h, i) => (h + ohlc.low[i]! + ohlc.close[i]! + ohlc.close[i]!) / 4);
198
199
  case 'volume': return ohlc.volume ?? ohlc.close.map(() => NaN);
199
200
  }
201
+ const ext = externalSource(source);
202
+ if (!ext) return ohlc.close;
203
+ return ohlc.close.map((_, i) => (ext as ArrayLike<number>)[i] ?? NaN);
200
204
  } else {
201
205
  const bars = data as Bar[];
202
206
  switch (source) {
@@ -210,6 +214,10 @@ export function getSource(
210
214
  case 'hlcc4': return bars.map(b => (b.high + b.low + b.close + b.close) / 4);
211
215
  case 'volume': return bars.map(b => b.volume ?? NaN);
212
216
  }
217
+ // Another source (study on study), else close.
218
+ const ext = externalSource(source);
219
+ if (!ext) return bars.map(b => b.close);
220
+ return bars.map((_, i) => (ext as ArrayLike<number>)[i] ?? NaN);
213
221
  }
214
222
  }
215
223
 
@@ -248,7 +256,13 @@ export function getSourceSeries(
248
256
  case 'ohlc4': return open.add(high).add(low).add(close).div(4);
249
257
  case 'hlcc4': return high.add(low).add(close).add(close).div(4);
250
258
  case 'volume': return Series.fromBars(bars, 'volume');
251
- default: return close;
259
+ default: {
260
+ // Another source (study on study), else close.
261
+ const ext = externalSource(source);
262
+ if (!ext) return close;
263
+ const values = Array.from({ length: bars.length }, (_, i) => ext[i] ?? NaN);
264
+ return new Series(bars, (_bar, i) => values[i] ?? NaN);
265
+ }
252
266
  }
253
267
  }
254
268
 
@@ -0,0 +1,29 @@
1
+ /**
2
+ * Sources other than the bar fields: a study calculated on another study's output (PineScript hosts call it "study
3
+ * on study"). A source input then holds a host name such as `"<sourceId>$<plotIndex>"`; the host resolves it to
4
+ * values aligned with the bars it passes to the calculation (`NaN` where there is no value).
5
+ *
6
+ * The host sets the resolver around a calculation and clears it after; a name it does not know falls back to
7
+ * `close`, as before.
8
+ */
9
+
10
+ /** Values of a source by name, aligned with the bars of the calculation; null/undefined = unknown name. */
11
+ export type SourceResolver = (name: string) => ArrayLike<number> | null | undefined;
12
+
13
+ let resolver: SourceResolver | null = null;
14
+
15
+ /** Built-in sources (bar fields and their averages). */
16
+ export const BUILTIN_SOURCES: ReadonlySet<string> = new Set([
17
+ 'open', 'high', 'low', 'close', 'hl2', 'hlc3', 'ohlc4', 'hlcc4', 'volume',
18
+ ]);
19
+
20
+ /** Set (or clear with null) how source names other than the built-ins are read. */
21
+ export function setSourceResolver(fn: SourceResolver | null): void {
22
+ resolver = fn;
23
+ }
24
+
25
+ /** Values of a non built-in source name, or null when no resolver knows it. */
26
+ export function externalSource(name: string): ArrayLike<number> | null {
27
+ if (!resolver || BUILTIN_SOURCES.has(name)) return null;
28
+ return resolver(name) ?? null;
29
+ }