lightweight-charts-indicators 0.20.3 → 0.22.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -3577,14 +3577,19 @@ __export(src_exports, {
3577
3577
  currencyOf: () => currencyOf,
3578
3578
  foregroundColorOf: () => foregroundColorOf,
3579
3579
  indicatorRegistry: () => indicatorRegistry,
3580
+ isPackedSeries: () => isPackedSeries,
3580
3581
  lastBarConfirmedOf: () => lastBarConfirmedOf,
3581
3582
  mintickOf: () => mintickOf,
3582
3583
  nowOf: () => nowOf,
3583
3584
  otherSymbolChart: () => otherSymbolChart,
3585
+ packedColumns: () => packedColumns,
3586
+ packedFromColumns: () => packedFromColumns,
3587
+ packedSeries: () => packedSeries,
3584
3588
  rootOf: () => rootOf,
3585
3589
  securityLowerTf: () => import_oakscriptjs310.securityLowerTf,
3586
3590
  securityOther: () => import_oakscriptjs310.securityOther,
3587
3591
  securityOtherValues: () => import_oakscriptjs310.securityOtherValues,
3592
+ setPackedSeries: () => setPackedSeries,
3588
3593
  spreadBars: () => spreadBars,
3589
3594
  spreadOptions: () => spreadOptions,
3590
3595
  spreadSymbols: () => spreadSymbols,
@@ -3598,6 +3603,320 @@ __export(src_exports, {
3598
3603
  });
3599
3604
  module.exports = __toCommonJS(src_exports);
3600
3605
 
3606
+ // src/packed-series.ts
3607
+ var PACKED = Symbol.for("lightweight-charts-indicators/packed-series");
3608
+ function isPoint(p) {
3609
+ if (p === null || typeof p !== "object" || Array.isArray(p) || Object.getPrototypeOf(p) !== Object.prototype)
3610
+ return false;
3611
+ let k = 0;
3612
+ for (const f in p) {
3613
+ if (k === 0 && f !== "time")
3614
+ return false;
3615
+ if (k === 1)
3616
+ return f === "value" && typeof p.time === "number" && typeof p.value === "number";
3617
+ k++;
3618
+ }
3619
+ return false;
3620
+ }
3621
+ var Columns = class _Columns {
3622
+ /** What the list holds: decided by its first value; `any` keeps them in `items`. */
3623
+ kind = "none";
3624
+ n = 0;
3625
+ time;
3626
+ value;
3627
+ /** 1 where an entry is set (an index past the others leaves holes). */
3628
+ has;
3629
+ extra = {};
3630
+ items = [];
3631
+ constructor(capacity) {
3632
+ const cap = Math.max(16, capacity);
3633
+ this.time = new Float64Array(cap);
3634
+ this.value = new Float64Array(cap);
3635
+ this.has = new Uint8Array(cap);
3636
+ }
3637
+ grow(size2) {
3638
+ if (size2 <= this.value.length)
3639
+ return;
3640
+ const cap = Math.max(size2, this.value.length + (this.value.length >> 1));
3641
+ const t = new Float64Array(cap);
3642
+ t.set(this.time.subarray(0, this.n));
3643
+ const v = new Float64Array(cap);
3644
+ v.set(this.value.subarray(0, this.n));
3645
+ const h = new Uint8Array(cap);
3646
+ h.set(this.has.subarray(0, this.n));
3647
+ this.time = t;
3648
+ this.value = v;
3649
+ this.has = h;
3650
+ }
3651
+ get(i) {
3652
+ if (this.kind === "any")
3653
+ return this.items[i];
3654
+ if (i >= this.n || !this.has[i])
3655
+ return void 0;
3656
+ if (this.kind === "numbers")
3657
+ return this.value[i];
3658
+ const p = { time: this.time[i], value: this.value[i] };
3659
+ for (const k in this.extra) {
3660
+ const x = this.extra[k][i];
3661
+ if (x !== void 0)
3662
+ p[k] = x;
3663
+ }
3664
+ return p;
3665
+ }
3666
+ /** Every entry as it was written, in a plain list (the kinds no longer hold the next value). */
3667
+ toAny() {
3668
+ const items = new Array(this.n);
3669
+ for (let i = 0; i < this.n; i++)
3670
+ if (this.has[i])
3671
+ items[i] = this.get(i);
3672
+ this.kind = "any";
3673
+ this.items = items;
3674
+ this.time = new Float64Array(0);
3675
+ this.value = new Float64Array(0);
3676
+ this.has = new Uint8Array(0);
3677
+ this.extra = {};
3678
+ }
3679
+ set(i, p) {
3680
+ if (this.kind === "none" && p !== void 0)
3681
+ this.kind = isPoint(p) ? "points" : typeof p === "number" ? "numbers" : "any";
3682
+ const fits = this.kind === "any" || p === void 0 || (this.kind === "points" ? isPoint(p) : typeof p === "number");
3683
+ if (!fits)
3684
+ this.toAny();
3685
+ if (this.kind === "any") {
3686
+ this.items[i] = p;
3687
+ if (i >= this.n)
3688
+ this.n = i + 1;
3689
+ return;
3690
+ }
3691
+ if (i >= this.n) {
3692
+ this.grow(i + 1);
3693
+ this.has.fill(0, this.n, i);
3694
+ this.n = i + 1;
3695
+ }
3696
+ this.has[i] = p === void 0 ? 0 : 1;
3697
+ for (const k in this.extra) {
3698
+ const list = this.extra[k];
3699
+ if (i < list.length)
3700
+ list[i] = void 0;
3701
+ }
3702
+ if (p === void 0)
3703
+ return;
3704
+ if (this.kind === "numbers") {
3705
+ this.value[i] = p;
3706
+ return;
3707
+ }
3708
+ const q = p;
3709
+ this.time[i] = q.time;
3710
+ this.value[i] = q.value;
3711
+ for (const k in q) {
3712
+ if (k === "time" || k === "value")
3713
+ continue;
3714
+ const x = q[k];
3715
+ if (x !== void 0)
3716
+ (this.extra[k] ??= [])[i] = x;
3717
+ }
3718
+ }
3719
+ setLength(len) {
3720
+ if (this.kind === "any") {
3721
+ this.items.length = len;
3722
+ this.n = len;
3723
+ return;
3724
+ }
3725
+ if (len < this.n) {
3726
+ for (const k in this.extra) {
3727
+ const list = this.extra[k];
3728
+ if (list.length > len)
3729
+ list.length = len;
3730
+ }
3731
+ } else if (len > this.n) {
3732
+ this.grow(len);
3733
+ this.has.fill(0, this.n, len);
3734
+ }
3735
+ this.n = len;
3736
+ }
3737
+ /** Entries [a, b) in new columns. */
3738
+ copy(a, b) {
3739
+ const out = new _Columns(Math.max(0, b - a));
3740
+ out.kind = this.kind;
3741
+ out.n = Math.max(0, b - a);
3742
+ if (this.kind === "any") {
3743
+ out.items = this.items.slice(a, b);
3744
+ return out;
3745
+ }
3746
+ out.time.set(this.time.subarray(a, b));
3747
+ out.value.set(this.value.subarray(a, b));
3748
+ out.has.set(this.has.subarray(a, b));
3749
+ for (const k in this.extra)
3750
+ out.extra[k] = this.extra[k].slice(a, b);
3751
+ return out;
3752
+ }
3753
+ };
3754
+ var isIndex = (p) => typeof p === "string" && p !== "" && String(+p >>> 0) === p;
3755
+ function bound(x, n, fallback) {
3756
+ if (x === void 0)
3757
+ return fallback;
3758
+ const v = Math.trunc(Number(x)) || 0;
3759
+ return v < 0 ? Math.max(0, n + v) : Math.min(v, n);
3760
+ }
3761
+ var read = (cols, i) => cols.get(i);
3762
+ function view(cols) {
3763
+ const slice = (start, end) => {
3764
+ const a = bound(start, cols.n, 0);
3765
+ return view(cols.copy(a, Math.max(a, bound(end, cols.n, cols.n))));
3766
+ };
3767
+ return new Proxy([], {
3768
+ get(target, prop, receiver) {
3769
+ if (prop === "length")
3770
+ return cols.n;
3771
+ if (prop === PACKED)
3772
+ return cols;
3773
+ if (prop === "slice")
3774
+ return slice;
3775
+ if (isIndex(prop))
3776
+ return read(cols, +prop);
3777
+ return Reflect.get(target, prop, receiver);
3778
+ },
3779
+ set(target, prop, value) {
3780
+ if (prop === "length") {
3781
+ cols.setLength(value);
3782
+ return true;
3783
+ }
3784
+ if (isIndex(prop)) {
3785
+ cols.set(+prop, value);
3786
+ return true;
3787
+ }
3788
+ return Reflect.set(target, prop, value);
3789
+ },
3790
+ has(target, prop) {
3791
+ if (isIndex(prop))
3792
+ return +prop < cols.n && (cols.kind === "any" ? +prop in cols.items : cols.has[+prop] === 1);
3793
+ return Reflect.has(target, prop);
3794
+ },
3795
+ getOwnPropertyDescriptor(target, prop) {
3796
+ if (prop === "length")
3797
+ return { value: cols.n, writable: true, enumerable: false, configurable: false };
3798
+ if (isIndex(prop)) {
3799
+ const i = +prop;
3800
+ const present = i < cols.n && (cols.kind === "any" ? i in cols.items : cols.has[i] === 1);
3801
+ return present ? { value: read(cols, i), writable: true, enumerable: true, configurable: true } : void 0;
3802
+ }
3803
+ return Reflect.getOwnPropertyDescriptor(target, prop);
3804
+ },
3805
+ ownKeys(target) {
3806
+ const keys = [];
3807
+ for (let i = 0; i < cols.n; i++)
3808
+ if (cols.kind === "any" ? i in cols.items : cols.has[i] === 1)
3809
+ keys.push(String(i));
3810
+ for (const k of Reflect.ownKeys(target))
3811
+ if (!isIndex(k))
3812
+ keys.push(k);
3813
+ return keys;
3814
+ },
3815
+ deleteProperty(target, prop) {
3816
+ if (isIndex(prop)) {
3817
+ const i = +prop;
3818
+ if (cols.kind === "any")
3819
+ delete cols.items[i];
3820
+ else if (i < cols.n)
3821
+ cols.set(i, void 0);
3822
+ return true;
3823
+ }
3824
+ return Reflect.deleteProperty(target, prop);
3825
+ }
3826
+ });
3827
+ }
3828
+ var packed = false;
3829
+ function setPackedSeries(on2) {
3830
+ packed = on2;
3831
+ }
3832
+ function packedSeriesMode() {
3833
+ return packed;
3834
+ }
3835
+ function packedSeries(list = []) {
3836
+ const cols = new Columns(list.length);
3837
+ for (let i = 0; i < list.length; i++)
3838
+ if (i in list)
3839
+ cols.set(i, list[i]);
3840
+ cols.setLength(list.length);
3841
+ return view(cols);
3842
+ }
3843
+ function packedColumns(v) {
3844
+ if (v === null || typeof v !== "object" || !Array.isArray(v))
3845
+ return null;
3846
+ const cols = v[PACKED];
3847
+ return cols && cols.kind === "points" && cols.has.subarray(0, cols.n).every((x) => x === 1) ? cols : null;
3848
+ }
3849
+ var isPackedSeries = (v) => v !== null && typeof v === "object" && Array.isArray(v) && v[PACKED] !== void 0;
3850
+ function packedFromColumns(c4) {
3851
+ const cols = new Columns(0);
3852
+ cols.kind = "points";
3853
+ cols.n = c4.n;
3854
+ cols.time = c4.time;
3855
+ cols.value = c4.value;
3856
+ cols.has = new Uint8Array(c4.n).fill(1);
3857
+ cols.extra = c4.extra;
3858
+ return view(cols);
3859
+ }
3860
+ var RUN_FIELDS = /* @__PURE__ */ new Set(["series", "lists"]);
3861
+ function packLists(s, lists, pick2) {
3862
+ const own = s;
3863
+ const places = /* @__PURE__ */ new Map();
3864
+ const reached = /* @__PURE__ */ new Set();
3865
+ const at6 = (list, holder, key2) => {
3866
+ const p = places.get(list);
3867
+ if (p)
3868
+ p.push([holder, key2]);
3869
+ else
3870
+ places.set(list, [[holder, key2]]);
3871
+ };
3872
+ const holders = /* @__PURE__ */ new Set();
3873
+ for (const k of Object.keys(own)) {
3874
+ const v = own[k];
3875
+ if (!Array.isArray(v))
3876
+ continue;
3877
+ const study1286 = !RUN_FIELDS.has(k);
3878
+ at6(v, own, k);
3879
+ if (study1286)
3880
+ reached.add(v);
3881
+ if (isPackedSeries(v))
3882
+ continue;
3883
+ if (study1286) {
3884
+ for (const item of v)
3885
+ if (Array.isArray(item))
3886
+ reached.add(item);
3887
+ }
3888
+ if (holders.has(v))
3889
+ continue;
3890
+ holders.add(v);
3891
+ for (let j = 0; j < v.length; j++)
3892
+ if (Array.isArray(v[j]))
3893
+ at6(v[j], v, j);
3894
+ }
3895
+ for (let j = 0; j < lists.length; j++) {
3896
+ const list = lists[j];
3897
+ if (!Array.isArray(list) || !reached.has(list) || isPackedSeries(list) || !pick2(list))
3898
+ continue;
3899
+ const p = packedSeries(list);
3900
+ for (const [holder, key2] of places.get(list) ?? [])
3901
+ holder[key2] = p;
3902
+ if (lists[j] !== p)
3903
+ lists[j] = p;
3904
+ }
3905
+ }
3906
+ function packRunState(s) {
3907
+ packLists(s, s.series, () => true);
3908
+ return s;
3909
+ }
3910
+ function packPointLists(s) {
3911
+ packLists(s, s.lists, (list) => {
3912
+ for (const x of list)
3913
+ if (x !== void 0)
3914
+ return isPoint(x);
3915
+ return false;
3916
+ });
3917
+ return s;
3918
+ }
3919
+
3601
3920
  // src/standard/sma.ts
3602
3921
  var import_oakscriptjs2 = require("oakscriptjs");
3603
3922
 
@@ -3659,6 +3978,7 @@ function createBarRun(study1286, chart = {}) {
3659
3978
  let copy = new Float64Array(0);
3660
3979
  let saved = null;
3661
3980
  const copyVars = study1286.copyVars ?? ((v) => ({ ...v }));
3981
+ const packed2 = packedSeriesMode();
3662
3982
  const limits = study1286.drawings;
3663
3983
  let lastKey;
3664
3984
  let lastData;
@@ -3720,6 +4040,10 @@ function createBarRun(study1286, chart = {}) {
3720
4040
  }
3721
4041
  for (const out of s.series)
3722
4042
  out.length = n;
4043
+ if (packed2 && from === 0) {
4044
+ packRunState(s);
4045
+ packPointLists(s);
4046
+ }
3723
4047
  copy = copyBars(bars, confirmed, copy, Math.min(from, confirmed));
3724
4048
  return Object.assign(study1286.result(s, bars, next), { from });
3725
4049
  } catch (e) {
@@ -238376,8 +238700,8 @@ function calculate1316(bars, inputs = {}) {
238376
238700
  const getX = lx2(getEW.l[4]);
238377
238701
  const getY = ly2(getEW.l[4]);
238378
238702
  const isSame2 = x1_ === lx2(getEW.l[2]) && x2_ === lx2(getEW.l[3]) && x3 === lx2(getEW.l[4]);
238379
- const bound = s * getY + diff * cfg.i854;
238380
- const isValid = x3 === getX && lt582(s * y6, bound) && lt582(s * y4, bound) && gt746(s * y5, s * getY);
238703
+ const bound2 = s * getY + diff * cfg.i854;
238704
+ const isValid = x3 === getX && lt582(s * y6, bound2) && lt582(s * y4, bound2) && gt746(s * y5, s * getY);
238381
238705
  const current = isSame2 && bX(getEW.bA) > x3;
238382
238706
  if (isValid) {
238383
238707
  const width = x6 - x2_;
@@ -239063,7 +239387,7 @@ function calculate1318(bars, inputs = {}, chart) {
239063
239387
  const ms = n > 0 && bars[0].time >= 1e12;
239064
239388
  const tMs = bars.map((b) => ms ? b.time : b.time * 1e3);
239065
239389
  const start = Math.max(0, n - CALC_BARS);
239066
- const view = bars.slice(start);
239390
+ const view2 = bars.slice(start);
239067
239391
  const timenow = nowOf(chart);
239068
239392
  const MINTICK = mintickOf(chart);
239069
239393
  const CHART_FG = foregroundColorOf(chart);
@@ -239088,8 +239412,8 @@ function calculate1318(bars, inputs = {}, chart) {
239088
239412
  const T20 = (i) => i >= start ? tMs[i] : NaN;
239089
239413
  const MX = (i) => i < start ? NaN : obbUseBody ? Math.max(bars[i].close, bars[i].open) : bars[i].high;
239090
239414
  const MN = (i) => i < start ? NaN : obbUseBody ? Math.min(bars[i].close, bars[i].open) : bars[i].low;
239091
- const atr = import_oakscriptjs1198.ta.atr(view, 10).toArray().map((v) => v ?? NaN);
239092
- const atr200Arr = import_oakscriptjs1198.ta.atr(view, 200).toArray().map((v) => v ?? NaN);
239415
+ const atr = import_oakscriptjs1198.ta.atr(view2, 10).toArray().map((v) => v ?? NaN);
239416
+ const atr200Arr = import_oakscriptjs1198.ta.atr(view2, 200).toArray().map((v) => v ?? NaN);
239093
239417
  const ictms = () => ({
239094
239418
  lastPrice: NaN,
239095
239419
  midPrice: NaN,
@@ -406016,10 +406340,10 @@ function calculate1744(bars, inputs = {}, chart) {
406016
406340
  const timePoint = (start, units) => start + units / mult * msByPeriodUnit;
406017
406341
  const roundTick = (price) => import_oakscriptjs1621.math.round(price / mintick) * mintick;
406018
406342
  const deltaTick = (p1, p2) => p1 / mintick - p2 / mintick;
406019
- const isIndex = symbolTypeOf(chart) === "index";
406343
+ const isIndex2 = symbolTypeOf(chart) === "index";
406020
406344
  const isPeriodOK = tfInfo.isminutes && mult <= 720 && cfg.period % 2 === 0 && cfg.period % mult === 0;
406021
- const customHelpHeader = isIndex ? "Ticker not allowed! It is an index, select a ticker with volume data." : !isPeriodOK ? "Parameters not allowed! Select a new period size and/or chart timeframe." : "HELP INFO";
406022
- const isGoodToGo = !isIndex && !cfg.showHelp && isPeriodOK;
406345
+ const customHelpHeader = isIndex2 ? "Ticker not allowed! It is an index, select a ticker with volume data." : !isPeriodOK ? "Parameters not allowed! Select a new period size and/or chart timeframe." : "HELP INFO";
406346
+ const isGoodToGo = !isIndex2 && !cfg.showHelp && isPeriodOK;
406023
406347
  const lastConfirmed = lastBarConfirmedOf(chart);
406024
406348
  const now = nowOf(chart);
406025
406349
  (0, import_oakscriptjs1621.resetDrawings)({ line: 500, label: 500 });
@@ -462546,4 +462870,4 @@ var registryPart12 = [
462546
462870
  }
462547
462871
  ];
462548
462872
  var indicatorRegistry = [...registryPart1, ...registryPart11, ...registryPart2, ...registryPart3, ...registryPart4, ...registryPart5, ...registryPart6, ...registryPart7, ...registryPart8, ...registryPart9, ...registryPart10, ...registryPart12].map(withReferenceNames).map(withValueFormat).map(withStyleOutputs).map(withAlertConditions);
462549
- var version = "0.20.3";
462873
+ var version = "0.22.0";
package/dist/index.d.ts CHANGED
@@ -12,6 +12,7 @@ import type { StudyDrawingKind } from './style-outputs';
12
12
  import type { ChartContext, SymbolRequest } from './chart-context';
13
13
  import type { IndicatorRun } from './live-run';
14
14
  export type { IndicatorRun } from './live-run';
15
+ export { setPackedSeries, isPackedSeries, packedColumns, packedFromColumns, packedSeries, type PackedColumns } from './packed-series';
15
16
  export type { InputConfig, PlotConfig, HLineConfig, FillConfig, ShapeConfig, BarColorConfig, CandleConfig } from 'oakscriptjs';
16
17
  export { SMA, calculate as calculateSMA } from './standard/sma';
17
18
  export type { SMAInputs } from './standard/sma';
@@ -3724,5 +3725,5 @@ export type { ChartContext, VisibleRange, SymbolRequest, SymbolInfo } from './ch
3724
3725
  export { MissingSymbolDataError, symbolBarsOf } from './chart-context';
3725
3726
  export { mintickOf, timezoneOf, tickeridOf, tickerOf, symbolTypeOf, currencyOf, basecurrencyOf, rootOf, foregroundColorOf, backgroundColorOf, lastBarConfirmedOf, nowOf, visibleRangeOf, } from './chart-context';
3726
3727
  export { securityOther, securityOtherValues, securityLowerTf, otherSymbolChart, spreadSymbols, spreadBars, spreadOptions, type OtherSymbolOptions, type OtherSymbolChart, type SymbolCalendar, } from './other-symbol';
3727
- export declare const version = "0.20.3";
3728
+ export declare const version = "0.22.0";
3728
3729
  //# sourceMappingURL=index.d.ts.map