lightweight-charts-indicators 0.20.2 → 0.21.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 CHANGED
@@ -10,7 +10,7 @@
10
10
  npm install lightweight-charts-indicators oakscriptjs lightweight-charts
11
11
  ```
12
12
 
13
- `oakscriptjs` is a peer dependency: version 0.16.0 or later, below 0.17. `lightweight-charts` 5 is only needed for
13
+ `oakscriptjs` is a peer dependency: version 0.16.0 or later, below 0.18. `lightweight-charts` 5 is only needed for
14
14
  the renderer.
15
15
 
16
16
  ## Quick Start
package/dist/helpers.d.ts CHANGED
@@ -11,4 +11,5 @@ export * from './lower-tf-volume';
11
11
  export * from './bar-time';
12
12
  export * from './alert-states';
13
13
  export * from './live-run';
14
+ export * from './packed-series';
14
15
  //# sourceMappingURL=helpers.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../src/helpers.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,cAAc,iBAAiB,CAAC;AAChC,cAAc,iBAAiB,CAAC;AAChC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,mBAAmB,CAAC;AAClC,cAAc,YAAY,CAAC;AAC3B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,YAAY,CAAC"}
1
+ {"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../src/helpers.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,cAAc,iBAAiB,CAAC;AAChC,cAAc,iBAAiB,CAAC;AAChC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,mBAAmB,CAAC;AAClC,cAAc,YAAY,CAAC;AAC3B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,YAAY,CAAC;AAC3B,cAAc,iBAAiB,CAAC"}
package/dist/helpers.mjs CHANGED
@@ -619,6 +619,286 @@ import {
619
619
  saveDrawingCheckpoint,
620
620
  setDrawingBar
621
621
  } from "oakscriptjs";
622
+
623
+ // src/packed-series.ts
624
+ var PACKED = Symbol.for("lightweight-charts-indicators/packed-series");
625
+ function isPoint(p) {
626
+ if (p === null || typeof p !== "object" || Array.isArray(p) || Object.getPrototypeOf(p) !== Object.prototype)
627
+ return false;
628
+ let k = 0;
629
+ for (const f in p) {
630
+ if (k === 0 && f !== "time")
631
+ return false;
632
+ if (k === 1)
633
+ return f === "value" && typeof p.time === "number" && typeof p.value === "number";
634
+ k++;
635
+ }
636
+ return false;
637
+ }
638
+ var Columns = class _Columns {
639
+ /** What the list holds: decided by its first value; `any` keeps them in `items`. */
640
+ kind = "none";
641
+ n = 0;
642
+ time;
643
+ value;
644
+ /** 1 where an entry is set (an index past the others leaves holes). */
645
+ has;
646
+ extra = {};
647
+ items = [];
648
+ constructor(capacity) {
649
+ const cap = Math.max(16, capacity);
650
+ this.time = new Float64Array(cap);
651
+ this.value = new Float64Array(cap);
652
+ this.has = new Uint8Array(cap);
653
+ }
654
+ grow(size) {
655
+ if (size <= this.value.length)
656
+ return;
657
+ const cap = Math.max(size, this.value.length + (this.value.length >> 1));
658
+ const t = new Float64Array(cap);
659
+ t.set(this.time.subarray(0, this.n));
660
+ const v = new Float64Array(cap);
661
+ v.set(this.value.subarray(0, this.n));
662
+ const h = new Uint8Array(cap);
663
+ h.set(this.has.subarray(0, this.n));
664
+ this.time = t;
665
+ this.value = v;
666
+ this.has = h;
667
+ }
668
+ get(i) {
669
+ if (this.kind === "any")
670
+ return this.items[i];
671
+ if (i >= this.n || !this.has[i])
672
+ return void 0;
673
+ if (this.kind === "numbers")
674
+ return this.value[i];
675
+ const p = { time: this.time[i], value: this.value[i] };
676
+ for (const k in this.extra) {
677
+ const x = this.extra[k][i];
678
+ if (x !== void 0)
679
+ p[k] = x;
680
+ }
681
+ return p;
682
+ }
683
+ /** Every entry as it was written, in a plain list (the kinds no longer hold the next value). */
684
+ toAny() {
685
+ const items = new Array(this.n);
686
+ for (let i = 0; i < this.n; i++)
687
+ if (this.has[i])
688
+ items[i] = this.get(i);
689
+ this.kind = "any";
690
+ this.items = items;
691
+ this.time = new Float64Array(0);
692
+ this.value = new Float64Array(0);
693
+ this.has = new Uint8Array(0);
694
+ this.extra = {};
695
+ }
696
+ set(i, p) {
697
+ if (this.kind === "none" && p !== void 0)
698
+ this.kind = isPoint(p) ? "points" : typeof p === "number" ? "numbers" : "any";
699
+ const fits = this.kind === "any" || p === void 0 || (this.kind === "points" ? isPoint(p) : typeof p === "number");
700
+ if (!fits)
701
+ this.toAny();
702
+ if (this.kind === "any") {
703
+ this.items[i] = p;
704
+ if (i >= this.n)
705
+ this.n = i + 1;
706
+ return;
707
+ }
708
+ if (i >= this.n) {
709
+ this.grow(i + 1);
710
+ this.has.fill(0, this.n, i);
711
+ this.n = i + 1;
712
+ }
713
+ this.has[i] = p === void 0 ? 0 : 1;
714
+ for (const k in this.extra) {
715
+ const list = this.extra[k];
716
+ if (i < list.length)
717
+ list[i] = void 0;
718
+ }
719
+ if (p === void 0)
720
+ return;
721
+ if (this.kind === "numbers") {
722
+ this.value[i] = p;
723
+ return;
724
+ }
725
+ const q = p;
726
+ this.time[i] = q.time;
727
+ this.value[i] = q.value;
728
+ for (const k in q) {
729
+ if (k === "time" || k === "value")
730
+ continue;
731
+ const x = q[k];
732
+ if (x !== void 0)
733
+ (this.extra[k] ??= [])[i] = x;
734
+ }
735
+ }
736
+ setLength(len) {
737
+ if (this.kind === "any") {
738
+ this.items.length = len;
739
+ this.n = len;
740
+ return;
741
+ }
742
+ if (len < this.n) {
743
+ for (const k in this.extra) {
744
+ const list = this.extra[k];
745
+ if (list.length > len)
746
+ list.length = len;
747
+ }
748
+ } else if (len > this.n) {
749
+ this.grow(len);
750
+ this.has.fill(0, this.n, len);
751
+ }
752
+ this.n = len;
753
+ }
754
+ /** Entries [a, b) in new columns. */
755
+ copy(a, b) {
756
+ const out = new _Columns(Math.max(0, b - a));
757
+ out.kind = this.kind;
758
+ out.n = Math.max(0, b - a);
759
+ if (this.kind === "any") {
760
+ out.items = this.items.slice(a, b);
761
+ return out;
762
+ }
763
+ out.time.set(this.time.subarray(a, b));
764
+ out.value.set(this.value.subarray(a, b));
765
+ out.has.set(this.has.subarray(a, b));
766
+ for (const k in this.extra)
767
+ out.extra[k] = this.extra[k].slice(a, b);
768
+ return out;
769
+ }
770
+ };
771
+ var isIndex = (p) => typeof p === "string" && p !== "" && String(+p >>> 0) === p;
772
+ function bound(x, n, fallback) {
773
+ if (x === void 0)
774
+ return fallback;
775
+ const v = Math.trunc(Number(x)) || 0;
776
+ return v < 0 ? Math.max(0, n + v) : Math.min(v, n);
777
+ }
778
+ var read = (cols, i) => cols.get(i);
779
+ function view(cols) {
780
+ const slice = (start, end) => {
781
+ const a = bound(start, cols.n, 0);
782
+ return view(cols.copy(a, Math.max(a, bound(end, cols.n, cols.n))));
783
+ };
784
+ return new Proxy([], {
785
+ get(target, prop, receiver) {
786
+ if (prop === "length")
787
+ return cols.n;
788
+ if (prop === PACKED)
789
+ return cols;
790
+ if (prop === "slice")
791
+ return slice;
792
+ if (isIndex(prop))
793
+ return read(cols, +prop);
794
+ return Reflect.get(target, prop, receiver);
795
+ },
796
+ set(target, prop, value) {
797
+ if (prop === "length") {
798
+ cols.setLength(value);
799
+ return true;
800
+ }
801
+ if (isIndex(prop)) {
802
+ cols.set(+prop, value);
803
+ return true;
804
+ }
805
+ return Reflect.set(target, prop, value);
806
+ },
807
+ has(target, prop) {
808
+ if (isIndex(prop))
809
+ return +prop < cols.n && (cols.kind === "any" ? +prop in cols.items : cols.has[+prop] === 1);
810
+ return Reflect.has(target, prop);
811
+ },
812
+ getOwnPropertyDescriptor(target, prop) {
813
+ if (prop === "length")
814
+ return { value: cols.n, writable: true, enumerable: false, configurable: false };
815
+ if (isIndex(prop)) {
816
+ const i = +prop;
817
+ const present = i < cols.n && (cols.kind === "any" ? i in cols.items : cols.has[i] === 1);
818
+ return present ? { value: read(cols, i), writable: true, enumerable: true, configurable: true } : void 0;
819
+ }
820
+ return Reflect.getOwnPropertyDescriptor(target, prop);
821
+ },
822
+ ownKeys(target) {
823
+ const keys = [];
824
+ for (let i = 0; i < cols.n; i++)
825
+ if (cols.kind === "any" ? i in cols.items : cols.has[i] === 1)
826
+ keys.push(String(i));
827
+ for (const k of Reflect.ownKeys(target))
828
+ if (!isIndex(k))
829
+ keys.push(k);
830
+ return keys;
831
+ },
832
+ deleteProperty(target, prop) {
833
+ if (isIndex(prop)) {
834
+ const i = +prop;
835
+ if (cols.kind === "any")
836
+ delete cols.items[i];
837
+ else if (i < cols.n)
838
+ cols.set(i, void 0);
839
+ return true;
840
+ }
841
+ return Reflect.deleteProperty(target, prop);
842
+ }
843
+ });
844
+ }
845
+ var packed = false;
846
+ function setPackedSeries(on) {
847
+ packed = on;
848
+ }
849
+ function packedSeriesMode() {
850
+ return packed;
851
+ }
852
+ function packedSeries(list = []) {
853
+ const cols = new Columns(list.length);
854
+ for (let i = 0; i < list.length; i++)
855
+ if (i in list)
856
+ cols.set(i, list[i]);
857
+ cols.setLength(list.length);
858
+ return view(cols);
859
+ }
860
+ function packedColumns(v) {
861
+ if (v === null || typeof v !== "object" || !Array.isArray(v))
862
+ return null;
863
+ const cols = v[PACKED];
864
+ return cols && cols.kind === "points" && cols.has.subarray(0, cols.n).every((x) => x === 1) ? cols : null;
865
+ }
866
+ var isPackedSeries = (v) => v !== null && typeof v === "object" && Array.isArray(v) && v[PACKED] !== void 0;
867
+ function packedFromColumns(c) {
868
+ const cols = new Columns(0);
869
+ cols.kind = "points";
870
+ cols.n = c.n;
871
+ cols.time = c.time;
872
+ cols.value = c.value;
873
+ cols.has = new Uint8Array(c.n).fill(1);
874
+ cols.extra = c.extra;
875
+ return view(cols);
876
+ }
877
+ function packRunState(s) {
878
+ const own = s;
879
+ const fields = /* @__PURE__ */ new Map();
880
+ for (const k of Object.keys(own)) {
881
+ if (k === "series" || !Array.isArray(own[k]))
882
+ continue;
883
+ const names = fields.get(own[k]);
884
+ if (names)
885
+ names.push(k);
886
+ else
887
+ fields.set(own[k], [k]);
888
+ }
889
+ s.series = s.series.map((list) => {
890
+ const names = fields.get(list);
891
+ if (!names || isPackedSeries(list))
892
+ return list;
893
+ const p = packedSeries(list);
894
+ for (const k of names)
895
+ own[k] = p;
896
+ return p;
897
+ });
898
+ return s;
899
+ }
900
+
901
+ // src/live-run.ts
622
902
  var LAST_BAR_FIELDS = /* @__PURE__ */ new Set(["lastBarConfirmed", "lastBarNew", "realtime", "now"]);
623
903
  function sameChart(a, b, functions = true) {
624
904
  const keys = /* @__PURE__ */ new Set([...Object.keys(a), ...Object.keys(b)]);
@@ -675,6 +955,7 @@ function createBarRun(study, chart = {}) {
675
955
  let copy = new Float64Array(0);
676
956
  let saved = null;
677
957
  const copyVars = study.copyVars ?? ((v) => ({ ...v }));
958
+ const packed2 = packedSeriesMode();
678
959
  const limits = study.drawings;
679
960
  let lastKey;
680
961
  let lastData;
@@ -713,6 +994,8 @@ function createBarRun(study, chart = {}) {
713
994
  if (limits)
714
995
  resetDrawings(limits);
715
996
  state = study.init(bars, next);
997
+ if (packed2)
998
+ packRunState(state);
716
999
  confirmed = 0;
717
1000
  }
718
1001
  const s = state;
@@ -1213,12 +1496,18 @@ export {
1213
1496
  hasCalendar,
1214
1497
  higherTimeframeBars,
1215
1498
  isBarSource,
1499
+ isPackedSeries,
1216
1500
  lastBarConfirmedOf,
1217
1501
  liveAlertStates,
1218
1502
  lowerTimeframe,
1219
1503
  mintickOf,
1220
1504
  nowOf,
1221
1505
  otherSymbolChart,
1506
+ packRunState,
1507
+ packedColumns,
1508
+ packedFromColumns,
1509
+ packedSeries,
1510
+ packedSeriesMode,
1222
1511
  periodChanges,
1223
1512
  periodStarts,
1224
1513
  rootOf,
@@ -1230,6 +1519,7 @@ export {
1230
1519
  securityStarts,
1231
1520
  sessionFlags,
1232
1521
  sessionHoursOf,
1522
+ setPackedSeries,
1233
1523
  spreadBars,
1234
1524
  spreadOptions,
1235
1525
  spreadSymbols,