oakscriptjs 0.11.7 → 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 (40) 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 +3 -3
  6. package/dist/cjs/index.d.ts.map +1 -1
  7. package/dist/cjs/index.js +2 -1
  8. package/dist/cjs/index.js.map +2 -2
  9. package/dist/cjs/runtime/types.d.ts +69 -0
  10. package/dist/cjs/runtime/types.d.ts.map +1 -1
  11. package/dist/cjs/runtime/types.js.map +1 -1
  12. package/dist/cjs/script/index.d.ts +24 -2
  13. package/dist/cjs/script/index.d.ts.map +1 -1
  14. package/dist/cjs/script/index.js +51 -20
  15. package/dist/cjs/script/index.js.map +2 -2
  16. package/dist/cjs/types/metadata.d.ts +8 -0
  17. package/dist/cjs/types/metadata.d.ts.map +1 -1
  18. package/dist/cjs/types/metadata.js.map +1 -1
  19. package/dist/esm/drawing/registry.d.ts +2 -0
  20. package/dist/esm/drawing/registry.d.ts.map +1 -1
  21. package/dist/esm/drawing/registry.js +8 -0
  22. package/dist/esm/drawing/registry.js.map +1 -1
  23. package/dist/esm/index.d.ts +3 -3
  24. package/dist/esm/index.d.ts.map +1 -1
  25. package/dist/esm/index.js +2 -2
  26. package/dist/esm/index.js.map +1 -1
  27. package/dist/esm/runtime/types.d.ts +69 -0
  28. package/dist/esm/runtime/types.d.ts.map +1 -1
  29. package/dist/esm/script/index.d.ts +24 -2
  30. package/dist/esm/script/index.d.ts.map +1 -1
  31. package/dist/esm/script/index.js +47 -14
  32. package/dist/esm/script/index.js.map +1 -1
  33. package/dist/esm/types/metadata.d.ts +8 -0
  34. package/dist/esm/types/metadata.d.ts.map +1 -1
  35. package/package.json +1 -1
  36. package/src/drawing/registry.ts +10 -0
  37. package/src/index.ts +5 -1
  38. package/src/runtime/types.ts +65 -0
  39. package/src/script/index.ts +72 -20
  40. package/src/types/metadata.ts +8 -0
@@ -88,7 +88,7 @@ import * as tableCore from '../table/index.js';
88
88
  import * as chartPointCore from '../chartpoint/index.js';
89
89
  import * as textCore from '../text/index.js';
90
90
  import * as mathCore from '../math/index.js';
91
- import { resetDrawings, setDrawingBar, setDrawingLimits } from '../drawing/registry.js';
91
+ import { createdDrawingKinds, resetDrawings, setDrawingBar, setDrawingLimits, type DrawingKind } from '../drawing/registry.js';
92
92
  export { holdDrawing, releaseDrawing, varDrawing, type DrawingVariable } from '../drawing/registry.js';
93
93
  import { drawingOutputs } from '../drawing/output.js';
94
94
  import * as timeframeCore from '../timeframe/index.js';
@@ -137,6 +137,11 @@ export interface ScriptRunResult {
137
137
  barColorConfig: BarColorConfig[];
138
138
  /** plotcandle() / plotbar() declarations */
139
139
  candleConfig: CandleConfig[];
140
+ /**
141
+ * The kinds of drawing objects (label.new, line.new, box.new, linefill.new, polyline.new, table.new) the run
142
+ * created, even the ones deleted since: the drawing groups a user can show or hide.
143
+ */
144
+ drawingKinds: DrawingKind[];
140
145
  /**
141
146
  * The plot(), plotshape(), plotchar(), plotarrow(), plotcandle() and plotbar() declarations in the order the
142
147
  * script made them (the id of each in its own config). PineScript numbers a script's outputs in this order.
@@ -362,6 +367,7 @@ export function executeScript(
362
367
  arrowConfig: c.arrowConfig,
363
368
  barColorConfig: c.barColorConfig,
364
369
  candleConfig: c.candleConfig,
370
+ drawingKinds: createdDrawingKinds(),
365
371
  outputs: c.outputs,
366
372
  defaultInputs: c.defaultInputs,
367
373
  alertConfig: c.alertConfig,
@@ -698,6 +704,8 @@ export interface ScriptPlotOptions {
698
704
  histbase?: number;
699
705
  /** Draw on the main chart pane even when the script is not an overlay (PineScript `force_overlay`); default false. */
700
706
  force_overlay?: boolean;
707
+ /** False: not listed in the Style settings of the study (PineScript `editable`); default true. */
708
+ editable?: boolean;
701
709
  }
702
710
 
703
711
  /** PineScript `plot()` — declares the plot and supplies its data in one call. */
@@ -716,6 +724,7 @@ function plotImpl(series: Series, title?: string, options: ScriptPlotOptions = {
716
724
  display: options.display,
717
725
  histbase: options.histbase,
718
726
  forceOverlay: options.force_overlay,
727
+ ...(options.editable === false ? { editable: false } : {}),
719
728
  });
720
729
  c.outputs.push({ kind: 'plot', id, title: title ?? id });
721
730
  const values = series.toArray();
@@ -756,6 +765,8 @@ export interface ScriptHLineOptions {
756
765
  /** Line style; default dashed (PineScript). */
757
766
  linestyle?: HLineConfig['linestyle'];
758
767
  linewidth?: number;
768
+ /** False: not listed in the Style settings of the study (PineScript `editable`); default true. */
769
+ editable?: boolean;
759
770
  }
760
771
 
761
772
  /** PineScript `hline()` — a static horizontal level. Default line style: dashed (PineScript). */
@@ -769,6 +780,7 @@ function hlineImpl(price: number, title?: string, options: ScriptHLineOptions =
769
780
  color: options.color,
770
781
  linestyle: options.linestyle ?? 'dashed',
771
782
  linewidth: options.linewidth,
783
+ ...(options.editable === false ? { editable: false } : {}),
772
784
  });
773
785
  return { id, kind: 'hline' };
774
786
  }
@@ -784,6 +796,8 @@ export interface ScriptFillOptions {
784
796
  /** Static color, or a per-bar color array from color.when(). */
785
797
  color?: string | string[];
786
798
  title?: string;
799
+ /** False: not listed in the Style settings of the study (PineScript `editable`); default true. */
800
+ editable?: boolean;
787
801
  }
788
802
 
789
803
  /** A fill colour argument: a colour, na (null / undefined), or a per-bar array from color.when(). */
@@ -801,14 +815,20 @@ function perBarColors(c: ScriptFillColor, n: number): Array<string | null> {
801
815
  return new Array<string | null>(n).fill(c ?? null);
802
816
  }
803
817
 
818
+ /** The colour of every bar when it is the same on all of them (and not na). */
819
+ function sameColor(colors: Array<string | null>): string | undefined {
820
+ const first = colors[0];
821
+ return first && colors.every((x) => x === first) ? first : undefined;
822
+ }
823
+
804
824
  /**
805
825
  * PineScript `fill()`, with the three PineScript overloads (positional arguments), for two plots or two hlines:
806
826
  * - `fill(p1, p2, color?, title?, ...)`
807
827
  * - `fill(p1, p2, top_value, bottom_value, top_color, bottom_color, title?, ...)`: gradient fill; values and colours
808
828
  * can change per bar (a Series / a color.when() array)
809
829
  * - `fill(p1, p2, { color, title })`: options object (oakscriptjs form, kept for existing scripts)
810
- * Arguments that change only how the chart shows the fill (`editable`, `show_last`, `fillgaps`, `display`) are
811
- * accepted and not used.
830
+ * `editable = false` keeps the fill out of the Style settings; `show_last`, `fillgaps` and `display` are accepted
831
+ * and not used. Every fill has an id in `fillConfig` (the fills between plots also list it on their data).
812
832
  */
813
833
  export function fill(a: PlotHandle, b: PlotHandle, options?: ScriptFillOptions): void;
814
834
  export function fill(a: PlotHandle, b: PlotHandle, color: ScriptFillColor, title?: string, ...rest: unknown[]): void;
@@ -829,6 +849,7 @@ export function fill(a: PlotHandle, b: PlotHandle, ...args: unknown[]): void {
829
849
  let staticColor: string | undefined;
830
850
  let title: string | undefined;
831
851
  let gradient: FillGradient | undefined;
852
+ let editable: unknown;
832
853
  if (typeof third === 'number' || third instanceof Series) {
833
854
  const n = ctxBars.bars.length;
834
855
  gradient = {
@@ -838,30 +859,39 @@ export function fill(a: PlotHandle, b: PlotHandle, ...args: unknown[]): void {
838
859
  bottomColor: perBarColors(args[3] as ScriptFillColor, n),
839
860
  };
840
861
  title = args[4] as string | undefined;
862
+ editable = args[5];
841
863
  } else if (third !== null && typeof third === 'object' && !Array.isArray(third)) {
842
864
  const options = third as ScriptFillOptions;
843
865
  perBar = Array.isArray(options.color) ? options.color : undefined;
844
866
  staticColor = typeof options.color === 'string' ? options.color : undefined;
845
867
  title = options.title;
868
+ editable = options.editable;
846
869
  } else {
847
870
  const color = third as ScriptFillColor;
848
871
  perBar = Array.isArray(color) ? color.map((x) => x ?? '') : undefined;
849
872
  staticColor = typeof color === 'string' ? color : undefined;
850
873
  title = args[1] as string | undefined;
874
+ editable = args[2];
851
875
  }
852
- if (a.kind === 'hline' && b.kind === 'hline') {
853
- c.fillConfig.push({
854
- id: `fill${c.fillConfig.length}`,
855
- plot1: a.id,
856
- plot2: b.id,
857
- color: staticColor,
858
- colors: perBar,
859
- gradient,
860
- title,
861
- });
862
- return;
863
- }
876
+ const id = `fill${c.fillConfig.length}`;
877
+ const topColor = gradient && sameColor(gradient.topColor);
878
+ const bottomColor = gradient && sameColor(gradient.bottomColor);
879
+ const hlines = a.kind === 'hline' && b.kind === 'hline';
880
+ c.fillConfig.push({
881
+ id,
882
+ plot1: a.id,
883
+ plot2: b.id,
884
+ color: staticColor,
885
+ // the per-bar data of a fill between plots is in the result
886
+ ...(hlines ? { colors: perBar, gradient } : {}),
887
+ title,
888
+ ...(topColor ? { topColor } : {}),
889
+ ...(bottomColor ? { bottomColor } : {}),
890
+ ...(editable === false ? { editable: false } : {}),
891
+ });
892
+ if (hlines) return;
864
893
  c.fills.push({
894
+ id,
865
895
  plot1: a.id,
866
896
  plot2: b.id,
867
897
  options: { color: staticColor, title },
@@ -883,6 +913,8 @@ export interface PlotShapeOptions {
883
913
  tooltip?: string;
884
914
  /** Draw on the main chart pane even when the script is not an overlay (PineScript `force_overlay`); default false. */
885
915
  force_overlay?: boolean;
916
+ /** False: not listed in the Style settings of the study (PineScript `editable`); default true. */
917
+ editable?: boolean;
886
918
  }
887
919
 
888
920
  export interface PlotCharOptions extends Omit<PlotShapeOptions, 'style'> {
@@ -965,6 +997,7 @@ function emitMarkers(
965
997
  size: options.size,
966
998
  offset: options.offset,
967
999
  forceOverlay: options.force_overlay,
1000
+ ...(options.editable === false ? { editable: false } : {}),
968
1001
  });
969
1002
  c.outputs.push({ kind, id, title });
970
1003
  const shapeStyle = kind === 'shape' ? (style as ShapeStyle) : undefined;
@@ -1018,6 +1051,8 @@ export interface PlotArrowOptions {
1018
1051
  display?: ArrowConfig['display'];
1019
1052
  /** Draw on the main chart pane even when the script is not an overlay (PineScript `force_overlay`); default false. */
1020
1053
  force_overlay?: boolean;
1054
+ /** False: not listed in the Style settings of the study (PineScript `editable`); default true. */
1055
+ editable?: boolean;
1021
1056
  }
1022
1057
 
1023
1058
  /**
@@ -1042,6 +1077,7 @@ export function plotarrow(series: Series, title?: string, options: PlotArrowOpti
1042
1077
  offset: options.offset,
1043
1078
  display: options.display,
1044
1079
  forceOverlay: options.force_overlay,
1080
+ ...(options.editable === false ? { editable: false } : {}),
1045
1081
  });
1046
1082
  c.outputs.push({ kind: 'arrow', id, title });
1047
1083
  const values = series.toArray();
@@ -1065,11 +1101,21 @@ export function plotarrow(series: Series, title?: string, options: PlotArrowOpti
1065
1101
  function emitBarColors(
1066
1102
  kind: 'bgcolor' | 'barcolor',
1067
1103
  colors: string | Array<string | undefined>,
1068
- options: { offset?: number; title?: string; force_overlay?: boolean }
1104
+ options: { offset?: number; title?: string; force_overlay?: boolean; editable?: boolean }
1069
1105
  ): void {
1070
1106
  const c = collector();
1071
1107
  const id = `${kind}${c.barColorConfig.filter((x) => x.kind === kind).length}`;
1072
- c.barColorConfig.push({ id, kind, title: options.title, offset: options.offset, forceOverlay: options.force_overlay });
1108
+ c.barColorConfig.push({
1109
+ id,
1110
+ kind,
1111
+ title: options.title,
1112
+ offset: options.offset,
1113
+ forceOverlay: options.force_overlay,
1114
+ // a constant colour is a palette of one colour (PineScript)
1115
+ ...(typeof colors === 'string' ? { palette: [colors] } : {}),
1116
+ ...(options.editable === false ? { editable: false } : {}),
1117
+ outputIndex: c.outputs.length,
1118
+ });
1073
1119
  const b = bars();
1074
1120
  const off = options.offset ?? 0;
1075
1121
  const target: BgColorData[] = kind === 'bgcolor' ? c.bgColors : c.barColors;
@@ -1079,7 +1125,7 @@ function emitBarColors(
1079
1125
  if (!col) continue;
1080
1126
  const j = i + off;
1081
1127
  if (j < 0 || j >= b.length) continue;
1082
- target.push(forceOverlay ? { time: b[j]!.time, color: col, forceOverlay } : { time: b[j]!.time, color: col });
1128
+ target.push(forceOverlay ? { time: b[j]!.time, color: col, id, forceOverlay } : { time: b[j]!.time, color: col, id });
1083
1129
  }
1084
1130
  }
1085
1131
 
@@ -1089,7 +1135,7 @@ function emitBarColors(
1089
1135
  */
1090
1136
  export function bgcolor(
1091
1137
  colors: string | Array<string | undefined>,
1092
- options: { offset?: number; title?: string; force_overlay?: boolean } = {}
1138
+ options: { offset?: number; title?: string; force_overlay?: boolean; editable?: boolean } = {}
1093
1139
  ): void {
1094
1140
  emitBarColors('bgcolor', colors, options);
1095
1141
  }
@@ -1097,7 +1143,7 @@ export function bgcolor(
1097
1143
  /** PineScript `barcolor()` — per-bar candle color override. */
1098
1144
  export function barcolor(
1099
1145
  colors: string | Array<string | undefined>,
1100
- options: { title?: string } = {}
1146
+ options: { title?: string; editable?: boolean } = {}
1101
1147
  ): void {
1102
1148
  emitBarColors('barcolor', colors, options);
1103
1149
  }
@@ -1115,6 +1161,8 @@ export interface PlotCandleOptions {
1115
1161
  display?: CandleConfig['display'];
1116
1162
  /** Draw on the main chart pane even when the script is not an overlay (PineScript `force_overlay`); default false. */
1117
1163
  force_overlay?: boolean;
1164
+ /** False: not listed in the Style settings of the study (PineScript `editable`); default true. */
1165
+ editable?: boolean;
1118
1166
  }
1119
1167
 
1120
1168
  export interface PlotBarOptions {
@@ -1123,6 +1171,8 @@ export interface PlotBarOptions {
1123
1171
  display?: CandleConfig['display'];
1124
1172
  /** Draw on the main chart pane even when the script is not an overlay (PineScript `force_overlay`); default false. */
1125
1173
  force_overlay?: boolean;
1174
+ /** False: not listed in the Style settings of the study (PineScript `editable`); default true. */
1175
+ editable?: boolean;
1126
1176
  }
1127
1177
 
1128
1178
  /** Colour of bar i: undefined when the argument is not given, 'transparent' for an na entry. */
@@ -1178,6 +1228,7 @@ export function plotcandle(
1178
1228
  borderColor: staticColor(options.bordercolor),
1179
1229
  display: options.display,
1180
1230
  forceOverlay: options.force_overlay,
1231
+ ...(options.editable === false ? { editable: false } : {}),
1181
1232
  });
1182
1233
  c.outputs.push({ kind: 'candle', id, title });
1183
1234
  const b = bars();
@@ -1215,6 +1266,7 @@ export function plotbar(
1215
1266
  color: staticColor(options.color),
1216
1267
  display: options.display,
1217
1268
  forceOverlay: options.force_overlay,
1269
+ ...(options.editable === false ? { editable: false } : {}),
1218
1270
  });
1219
1271
  c.outputs.push({ kind: 'bar', id, title });
1220
1272
  const b = bars();
@@ -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) */