wickchart 1.0.0 → 1.3.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/types/core.d.ts CHANGED
@@ -585,6 +585,28 @@ export declare function checkAlertCross(alert: {
585
585
  price: number;
586
586
  direction?: 'above' | 'below' | 'cross';
587
587
  }, prevPrice: number, price: number): boolean;
588
+ /**
589
+ * Boolean truth series for a WickScript predicate: any numeric expression
590
+ * where nonzero & finite counts as true (NaN / 0 / ±Infinity → false).
591
+ * Powers scripted alerts — `addAlert({ when: 'crossup(close, sma(close,50))' })`.
592
+ * @param {object|string} compiled compiled predicate (or raw source)
593
+ * @param {Bar[]} bars
594
+ * @returns {boolean[]}
595
+ */
596
+ export declare function predicateTrueSeries(compiled: object | string, bars: Bar[]): boolean[];
597
+ /**
598
+ * Edge-triggered step for a scripted alert. `armed` starts true; a rising
599
+ * edge (false → true) fires once and disarms; a true → false transition
600
+ * re-arms, so `once: false` alerts can fire again on the next edge while
601
+ * `once: true` alerts are removed after their first fire.
602
+ * @param {boolean} armed
603
+ * @param {boolean} curTrue
604
+ * @returns {{ fire: boolean, armed: boolean }}
605
+ */
606
+ export declare function scriptAlertStep(armed: boolean, curTrue: boolean): {
607
+ fire: boolean;
608
+ armed: boolean;
609
+ };
588
610
  /**
589
611
  * Statistics over a visible slice of bars.
590
612
  * @param {Bar[]} bars
@@ -648,6 +670,239 @@ export declare function parseVolShading(val: string | null | undefined): {
648
670
  p2: number;
649
671
  period: number;
650
672
  };
673
+ /**
674
+ * Index of the last bar whose time is <= `t` (binary search). Clamps to
675
+ * [0, n-1]: a time before the first bar → 0, past the last bar → n-1.
676
+ * Empty bars or a non-numeric time → null.
677
+ * @param {object[]} bars normalized bar objects
678
+ * @param {number} t timestamp in ms or s
679
+ * @returns {number|null}
680
+ */
681
+ export declare function barIndexForTime(bars: object[], t: number): number | null;
682
+ /**
683
+ * Validate & normalize server-side overlay definitions. Overlays are data
684
+ * from an API, so invalid entries are silently dropped — never thrown.
685
+ *
686
+ * zone: { type:'zone', from?: time|null, to?: time|null, priceFrom, priceTo,
687
+ * color?, alpha?, border?, label?, id? } — a time×price rectangle.
688
+ * `from`/`to` omitted (or null) anchor to the left/right chart edge;
689
+ * a zone with no `to` extends into future space past the last bar.
690
+ * level: { type:'level', price, from?, to?, color?, width?, dash?, label?, id? }
691
+ * — a horizontal price line, full width by default.
692
+ *
693
+ * Colors go through safeColor(); `alpha` clamps to [0.02, 0.8] (default 0.22).
694
+ * @param {any} list
695
+ * @returns {object[]} normalized overlays (possibly empty)
696
+ */
697
+ export declare function normalizeOverlays(list: any): object[];
698
+ /**
699
+ * Resolve an overlay color against the active palette: 'up'/'down'/'accent'
700
+ * map to theme colors, anything else passes through safeColor(), and invalid
701
+ * or missing values fall back to the accent color.
702
+ * @param {any} raw
703
+ * @param {object} pal active theme palette
704
+ * @returns {string} a concrete CSS color
705
+ */
706
+ export declare function resolveOverlayColor(raw: any, pal: object): string;
707
+ /**
708
+ * σ-cone projection from realized per-bar volatility: price bands widening
709
+ * with √h (GBM-style, exp(±z·σ·√h)) over `horizon` future bars.
710
+ * @param {number} lastClose anchor price (bar 0)
711
+ * @param {number} volPerBar per-bar stddev of log returns (from calcRealizedVol)
712
+ * @param {number} horizon future bars (clamped 1–500, default 48)
713
+ * @param {number[]} [levels] σ multipliers, e.g. [1, 2] (each clamped to 0–5)
714
+ * @returns {{horizon: number, levels: number[], bands: Record<string, {up: number[], down: number[]}>}}
715
+ * bands[z].up/.down are arrays indexed by h = 0…horizon ([0] === lastClose)
716
+ */
717
+ export declare function calcVolCone(lastClose: number, volPerBar: number, horizon: number, levels?: number[]): {
718
+ horizon: number;
719
+ levels: number[];
720
+ bands: Record<string, {
721
+ up: number[];
722
+ down: number[];
723
+ }>;
724
+ };
725
+ /**
726
+ * Validate a scenario spec: a ghost path of future prices (bars or API data)
727
+ * plus optional cone settings. Invalid entries are dropped, never thrown.
728
+ *
729
+ * { path: [64000, 65500, {price: 68000}], // future bars 1..N
730
+ * horizon: 48, // alternative/additional: cone-only projection
731
+ * cone: true, // σ-bands from realized vol (default true)
732
+ * levels: [1, 2], // σ multipliers (default [1, 2])
733
+ * color?, label? } // palette keys up|down|accent or safe CSS colors
734
+ *
735
+ * @param {any} spec
736
+ * @returns {null|{path: {h:number, price:number}[], horizon: number,
737
+ * cone: boolean, levels: number[], color: string|null, label: string}}
738
+ */
739
+ export declare function normalizeScenario(spec: any): null | {
740
+ path: {
741
+ h: number;
742
+ price: number;
743
+ }[];
744
+ horizon: number;
745
+ cone: boolean;
746
+ levels: number[];
747
+ color: string | null;
748
+ label: string;
749
+ };
750
+ /**
751
+ * Validate a risk plan: entry + stop define 1R (the risk unit per trade);
752
+ * reward levels are drawn at R multiples beyond the entry. Invalid input
753
+ * is dropped, never thrown — same contract as setOverlays/setScenario.
754
+ *
755
+ * { entry: 64500, stop: 63800, // stop < entry ⇒ long; else short
756
+ * multiples: [1, 2, 3], // R-multiple levels (default [1,2,3])
757
+ * targets: [65900, 67300], // alternative: explicit prices → kR
758
+ * label: 'breakout plan' } // ≤ 40 chars
759
+ *
760
+ * Explicit `targets` are converted to their (signed) R multiple; levels on
761
+ * the wrong side of the entry (negative or ~zero R) are dropped. `multiples`
762
+ * win when both are given. At most 8 levels, each ≤ 20R.
763
+ *
764
+ * @param {any} spec
765
+ * @returns {null|{entry: number, stop: number, risk: number,
766
+ * direction: 'long'|'short', levels: {k: number, price: number}[],
767
+ * maxK: number, label: string}}
768
+ */
769
+ export declare function normalizeRiskPlan(spec: any): null | {
770
+ entry: number;
771
+ stop: number;
772
+ risk: number;
773
+ direction: 'long' | 'short';
774
+ levels: {
775
+ k: number;
776
+ price: number;
777
+ }[];
778
+ maxK: number;
779
+ label: string;
780
+ };
781
+ /**
782
+ * Turn a bar window into an ordered story: the annotation events (pivot
783
+ * highs/lows, volume spikes, gaps, RSI divergences) plus derived **legs** —
784
+ * the move between consecutive opposite pivots ("+12.4% over 38 bars").
785
+ * The timeline drives the bar-walk player and any caption UI.
786
+ *
787
+ * @param {Bar[]} bars full dataset
788
+ * @param {number} i0 first index of the window
789
+ * @param {number} i1 last index of the window
790
+ * @param {{pivot?: number, volMult?: number, gapMult?: number, rsiPeriod?: number}} [opts]
791
+ * pivot window defaults to 8 (denser than the annotations overlay's 20)
792
+ * @returns {{i: number, time: number, type: string, side: string, note: string,
793
+ * legPct?: number, legBars?: number}[]} sorted by index, capped at 60
794
+ */
795
+ export declare function narrateWindow(bars: Bar[], i0: number, i1: number, opts?: {
796
+ pivot?: number;
797
+ volMult?: number;
798
+ gapMult?: number;
799
+ rsiPeriod?: number;
800
+ }): {
801
+ i: number;
802
+ time: number;
803
+ type: string;
804
+ side: string;
805
+ note: string;
806
+ legPct?: number;
807
+ legBars?: number;
808
+ }[];
809
+ /**
810
+ * Stats for a brushed bar range: net move (open of the first bar → close
811
+ * of the last), extremes, and summed volume. Powers the brush-selection
812
+ * overlay and its `wick:brush` event.
813
+ *
814
+ * @param {Bar[]} bars full dataset
815
+ * @param {number} i0 first selected index
816
+ * @param {number} i1 last selected index
817
+ * @returns {null|{bars: number, from: {index: number, time: number},
818
+ * to: {index: number, time: number}, firstOpen: number,
819
+ * lastClose: number, delta: number, deltaPct: number,
820
+ * high: number, low: number, volume: number}}
821
+ */
822
+ export declare function brushStats(bars: Bar[], i0: number, i1: number): null | {
823
+ bars: number;
824
+ from: {
825
+ index: number;
826
+ time: number;
827
+ };
828
+ to: {
829
+ index: number;
830
+ time: number;
831
+ };
832
+ firstOpen: number;
833
+ lastClose: number;
834
+ delta: number;
835
+ deltaPct: number;
836
+ high: number;
837
+ low: number;
838
+ volume: number;
839
+ };
840
+ /** Smoothest cheap easing for viewport pans: slow in, slow out. */
841
+ export declare function easeInOutCubic(t: any): number;
842
+ /**
843
+ * Validate one story scene. Every field is optional except that a scene
844
+ * must be an object; omitted fields simply don't change that aspect of
845
+ * the chart when played. `scenario`/`riskPlan` use a 'clear' sentinel for
846
+ * explicit "remove it" (null input means clear too when the KEY is present).
847
+ *
848
+ * { title: 'The breakout', note: 'What happened…',
849
+ * range: { from, to }, // times (s or ms) — the camera pans there
850
+ * indicators: 'sma:20 rsi:14', // optional indicator string
851
+ * type: 'candles', // optional series type
852
+ * overlays: [...], // optional zones/levels (normalizeOverlays)
853
+ * scenario: {...} | null, // set / clear a scenario
854
+ * riskPlan: {...} | null, // set / clear a risk plan
855
+ * dwell: 2200 } // ms to hold after the pan (500–30000)
856
+ *
857
+ * @returns {object|null} normalized scene, or null for non-objects
858
+ */
859
+ export declare function normalizeScene(scene: any): object | null;
860
+ /**
861
+ * Validate a whole story: normalize each scene, drop junk, cap at 20.
862
+ * @returns {object[]} possibly empty
863
+ */
864
+ export declare function sceneList(story: any): object[];
865
+ /**
866
+ * Tracks other charts viewing the same room: last-sighting timestamps per
867
+ * peer plus the viewport each one is looking at. Pure bookkeeping — the
868
+ * transport (BroadcastChannel, WebSocket, …) lives in the component/app.
869
+ *
870
+ * Peers expire `ttl` ms after their last sighting, so a closed tab fades
871
+ * out of the room without an explicit goodbye.
872
+ */
873
+ export declare class PresenceTracker {
874
+ ttl: number;
875
+ /** @type {Map<string, {id: string, name: string|null, range: {from:number,to:number}|null, at: number}>} */
876
+ peers: Map<string, {
877
+ id: string;
878
+ name: string | null;
879
+ range: {
880
+ from: number;
881
+ to: number;
882
+ } | null;
883
+ at: number;
884
+ }>;
885
+ /** @param {number} [ttl=12000] ms a peer survives without a sighting */
886
+ constructor(ttl?: number);
887
+ /**
888
+ * Record a sighting. `patch.range` ({from,to} times) is validated and
889
+ * normalized; a sighting without a range keeps the previous one.
890
+ * @returns {boolean} true when this sighting is a join (new peer)
891
+ */
892
+ track(id: any, patch?: {}, now?: number): boolean;
893
+ /** @returns {object|null} the removed peer entry, or null when unknown */
894
+ drop(id: any): object | null;
895
+ /** Expire peers not seen within the ttl.
896
+ * @returns {object[]} the peer entries that left */
897
+ sweep(now?: number): object[];
898
+ /** @returns {{id: string, name: string|null, range: object|null, at: number}[]} copies, oldest sighting first */
899
+ list(): {
900
+ id: string;
901
+ name: string | null;
902
+ range: object | null;
903
+ at: number;
904
+ }[];
905
+ }
651
906
  export declare const tfLabelOf: (dtMs: any) => string;
652
907
  /**
653
908
  * Compact, LLM-friendly summary of a bar window: structured fields plus a
@@ -677,3 +932,110 @@ export declare function encodeStateQuery(state: ChartState | null): string;
677
932
  * @returns {ChartState}
678
933
  */
679
934
  export declare function decodeStateQuery(str: string): ChartState;
935
+ /**
936
+ * Tool manifest for LLM/agent control of a chart. Tools map 1:1 onto the
937
+ * public element API; every op through applyChartOps is validated before it
938
+ * touches the chart (LLM output is untrusted input).
939
+ */
940
+ export declare const AI_TOOLS: ({
941
+ tool: string;
942
+ description: string;
943
+ args: {
944
+ indicators?: undefined;
945
+ overlays?: undefined;
946
+ from?: undefined;
947
+ to?: undefined;
948
+ type?: undefined;
949
+ enabled?: undefined;
950
+ low?: undefined;
951
+ high?: undefined;
952
+ };
953
+ } | {
954
+ tool: string;
955
+ description: string;
956
+ args: {
957
+ indicators: string;
958
+ overlays?: undefined;
959
+ from?: undefined;
960
+ to?: undefined;
961
+ type?: undefined;
962
+ enabled?: undefined;
963
+ low?: undefined;
964
+ high?: undefined;
965
+ };
966
+ } | {
967
+ tool: string;
968
+ description: string;
969
+ args: {
970
+ indicators?: undefined;
971
+ overlays: string;
972
+ from?: undefined;
973
+ to?: undefined;
974
+ type?: undefined;
975
+ enabled?: undefined;
976
+ low?: undefined;
977
+ high?: undefined;
978
+ };
979
+ } | {
980
+ tool: string;
981
+ description: string;
982
+ args: {
983
+ indicators?: undefined;
984
+ overlays?: undefined;
985
+ from: string;
986
+ to: string;
987
+ type?: undefined;
988
+ enabled?: undefined;
989
+ low?: undefined;
990
+ high?: undefined;
991
+ };
992
+ } | {
993
+ tool: string;
994
+ description: string;
995
+ args: {
996
+ indicators?: undefined;
997
+ overlays?: undefined;
998
+ from?: undefined;
999
+ to?: undefined;
1000
+ type: string;
1001
+ enabled?: undefined;
1002
+ low?: undefined;
1003
+ high?: undefined;
1004
+ };
1005
+ } | {
1006
+ tool: string;
1007
+ description: string;
1008
+ args: {
1009
+ indicators?: undefined;
1010
+ overlays?: undefined;
1011
+ from?: undefined;
1012
+ to?: undefined;
1013
+ type?: undefined;
1014
+ enabled: string;
1015
+ low: string;
1016
+ high: string;
1017
+ };
1018
+ })[];
1019
+ /**
1020
+ * Compact system prompt for agent control: paste into any LLM alongside the
1021
+ * tool manifest. The model answers with a JSON array of {tool, args} ops.
1022
+ * @returns {string}
1023
+ */
1024
+ export declare function aiPromptText(): string;
1025
+ /**
1026
+ * Validate + apply a list of {tool, args} ops (typically LLM output) to a
1027
+ * chart-like target. Ops are whitelisted and their args validated — an op
1028
+ * never throws; it returns {ok: false, error} instead so the agent can
1029
+ * self-correct. Target contract: getDataWindow(), setAttribute(k, v),
1030
+ * setOverlays(list), clearOverlays(), addAlert(a), setVisibleRange(r),
1031
+ * fit(), and (static) _registry() for indicator name checks.
1032
+ * @param {object} target chart element (or test double)
1033
+ * @param {any} ops
1034
+ * @returns {Array<{ok: boolean, tool?: string, result?: any, error?: string}>}
1035
+ */
1036
+ export declare function applyChartOps(target: object, ops: any): Array<{
1037
+ ok: boolean;
1038
+ tool?: string;
1039
+ result?: any;
1040
+ error?: string;
1041
+ }>;
@@ -0,0 +1,64 @@
1
+ /**
2
+ * Map an event shorthand to the dispatched event name.
3
+ * "range" / "wick:range" → "wick:range" (the 0.x "hab:" prefix is no longer emitted).
4
+ * @param {string} name
5
+ * @returns {string}
6
+ */
7
+ export declare const toWickEventName: (name: string) => string;
8
+ /**
9
+ * camelCase prop name → kebab-case attribute name ("volShading" → "vol-shading").
10
+ * @param {string} key
11
+ * @returns {string}
12
+ */
13
+ export declare const toAttrName: (key: string) => string;
14
+ /**
15
+ * Split React props into chart attrs / event handlers / DOM passthrough /
16
+ * the data array / the overlays array.
17
+ * @param {object} props
18
+ * @returns {{ attrs: object, events: Record<string, Function>, dom: object, data: any, overlays: any }}
19
+ */
20
+ export declare function splitChartProps(props: object): {
21
+ attrs: object;
22
+ events: Record<string, Function>;
23
+ dom: object;
24
+ data: any;
25
+ overlays: any;
26
+ };
27
+ /**
28
+ * Apply split props to a chart element. Every write is guarded so re-running
29
+ * with identical values is a no-op (attributes compared as strings, `data`
30
+ * and `overlays` compared by identity — passing a fresh array is what
31
+ * triggers a redraw).
32
+ * @param {HTMLElement} el
33
+ * @param {{ attrs?: object, data?: any, overlays?: any }} split
34
+ */
35
+ export declare function applyChartProps(el: HTMLElement, split: {
36
+ attrs?: object;
37
+ data?: any;
38
+ overlays?: any;
39
+ }): void;
40
+ /**
41
+ * Full control hook: renders nothing — attach the returned ref to your own
42
+ * <wick-chart> element and pass the same options you would give the component.
43
+ *
44
+ * const { ref, chart } = useWickChart({ data: bars, indicators: 'sma:20', onRange });
45
+ * return <wick-chart ref={ref} style={{ height: 420 }} />;
46
+ * // chart.getDataWindow() etc. once mounted
47
+ *
48
+ * @param {object} [options] any <wick-chart> attribute, plus `data`, `events`
49
+ * and `onXxx`-style handlers (see splitChartProps).
50
+ * @returns {{ ref: (node: any) => void, chart: any }} `chart` is the element
51
+ * instance (or null before mount) for the imperative API.
52
+ */
53
+ export declare function useWickChart(options?: object): {
54
+ ref: (node: any) => void;
55
+ chart: any;
56
+ };
57
+ /**
58
+ * Drop-in React component for <wick-chart>. Attributes ride through
59
+ * createElement (so they exist at first paint and in SSR output) while the
60
+ * hook keeps them in sync on updates; `data` and event handlers never touch
61
+ * React's prop pipeline. Works the same on React 16.8 → 19.
62
+ */
63
+ export declare const WickChart: import("react").ForwardRefExoticComponent<import("react").RefAttributes<any>>;
64
+ export default WickChart;
@@ -0,0 +1,3 @@
1
+ import './wick-chart.js';
2
+ export { WickChart, useWickChart, splitChartProps, applyChartProps, toWickEventName, toAttrName, } from './react-core.js';
3
+ export { default } from './react-core.js';