@tidal-ts/core 0.1.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/LICENSE +21 -0
- package/dist/aggregate.d.ts +193 -0
- package/dist/aggregate.js +288 -0
- package/dist/columnar.d.ts +61 -0
- package/dist/columnar.js +63 -0
- package/dist/derive.d.ts +555 -0
- package/dist/derive.js +740 -0
- package/dist/expirations.d.ts +85 -0
- package/dist/expirations.js +151 -0
- package/dist/extent.d.ts +20 -0
- package/dist/extent.js +44 -0
- package/dist/fixture.d.ts +64 -0
- package/dist/fixture.js +116 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +16 -0
- package/dist/join.d.ts +31 -0
- package/dist/join.js +36 -0
- package/dist/regrid.d.ts +22 -0
- package/dist/regrid.js +85 -0
- package/dist/sessions.d.ts +141 -0
- package/dist/sessions.js +232 -0
- package/dist/studyCatalog.d.ts +129 -0
- package/dist/studyCatalog.js +361 -0
- package/dist/timeZone.d.ts +51 -0
- package/dist/timeZone.js +80 -0
- package/dist/types.d.ts +20 -0
- package/dist/types.js +12 -0
- package/dist/vol.d.ts +160 -0
- package/dist/vol.js +252 -0
- package/package.json +50 -0
package/dist/vol.d.ts
ADDED
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import { TimeSeries } from 'pond-ts';
|
|
2
|
+
import { type BarInterval } from './types.js';
|
|
3
|
+
/** The standard ATM tenor set (calendar days) — the daily feed's per-tenor
|
|
4
|
+
* columns. Ascending; `21` (~1 month) is the default. */
|
|
5
|
+
export declare const VOL_TENORS: readonly [5, 21, 42, 63, 84, 105, 126, 189, 252, 378, 504];
|
|
6
|
+
export type VolTenor = (typeof VOL_TENORS)[number];
|
|
7
|
+
/** The tenor a fresh ATM-vol series shows until another is picked (~1 month). */
|
|
8
|
+
export declare const DEFAULT_VOL_TENOR = 21;
|
|
9
|
+
/**
|
|
10
|
+
* Column ids for the feed's two ATM vol families at a tenor. The id doubles as
|
|
11
|
+
* the chart series id (instance identity), so these must be stable +
|
|
12
|
+
* collision-free.
|
|
13
|
+
*
|
|
14
|
+
* **Both are ATM implied vol.** The feed publishes two ATM implied curves that
|
|
15
|
+
* differ only in which **expected-earnings-move estimate was censored out** of
|
|
16
|
+
* the curve — a fitted estimate (`iEMove`) versus a sample of past moves
|
|
17
|
+
* (`hEMove`) — not implied versus historical. Four lines of evidence, recorded
|
|
18
|
+
* in the application this package came from: a shared curve parameterisation,
|
|
19
|
+
* the pairing with the two expected-move column families, realized vol living
|
|
20
|
+
* in its own dataset, and the two columns coinciding exactly wherever the
|
|
21
|
+
* earnings count is 0.
|
|
22
|
+
*
|
|
23
|
+
* That matters here because `hvCol` was `historicalCol` and surfaced as
|
|
24
|
+
* **"Realized Vol"** until 2026-08-24 — so the default chart drew two implied
|
|
25
|
+
* curves and named one of them realized. A fifth line of evidence, measured over
|
|
26
|
+
* 36,003 rows of a daily history table while fixing the label:
|
|
27
|
+
*
|
|
28
|
+
* | check | result |
|
|
29
|
+
* | ------------------------------------------- | ----------------- |
|
|
30
|
+
* | `hv21` bit-identical to `iv21` | **63.4%** of rows |
|
|
31
|
+
* | `corr(hv21, iv21)` | 0.9748 |
|
|
32
|
+
* | `corr(hv21, realized)` vs `corr(iv21, …)` | 0.8196 vs 0.8391 |
|
|
33
|
+
* | …on the 13,178 rows where `hv21 != iv21` | 0.8147 vs 0.8617 |
|
|
34
|
+
*
|
|
35
|
+
* The last row is the one that closes the question. It rules out the charitable
|
|
36
|
+
* reading — a genuine historical fit that falls back to implied when unavailable
|
|
37
|
+
* — because on exactly the rows where such a fit would reveal itself, `hv` tracks
|
|
38
|
+
* realized vol *worse* than implied does. There is no realized signal hiding
|
|
39
|
+
* under the duplication. (Realized is `√(mean(ccVar, 21)·252)`; correlation is
|
|
40
|
+
* scale-invariant, so that estimator's own calibration does not affect the
|
|
41
|
+
* comparison.)
|
|
42
|
+
*
|
|
43
|
+
* Names, therefore: `iv` keeps its meaning, and `hv` is a neutral abbreviation of
|
|
44
|
+
* the feed's own field rather than a claim about it — this package does not
|
|
45
|
+
* adopt the vendor's column name, and the name it does use asserts nothing.
|
|
46
|
+
* The user-facing label is `ATM Vol (h-censored)`.
|
|
47
|
+
*/
|
|
48
|
+
export declare const impliedCol: (tenor: number) => string;
|
|
49
|
+
/**
|
|
50
|
+
* `hv{t}` — the feed's second ATM implied curve, and **not historical or realized vol**
|
|
51
|
+
* despite the letter.
|
|
52
|
+
*
|
|
53
|
+
* It is ATM *implied* vol under the other earnings-censoring basis; see
|
|
54
|
+
* {@link impliedCol} for the full evidence and measurements. Named for the column
|
|
55
|
+
* rather than for a meaning precisely because the meaning was guessed wrong once:
|
|
56
|
+
* this was `historicalCol` and surfaced as "Realized Vol" until 2026-08-24, and an
|
|
57
|
+
* identifier that asserts a quantity leaves the bug in the code after it is fixed
|
|
58
|
+
* on screen.
|
|
59
|
+
*
|
|
60
|
+
* Carries its own comment rather than deferring entirely to `impliedCol`, because
|
|
61
|
+
* hovering `hvCol` in an editor is exactly when a reader needs to be told this.
|
|
62
|
+
*/
|
|
63
|
+
export declare const hvCol: (tenor: number) => string;
|
|
64
|
+
/**
|
|
65
|
+
* **Realized** vol at a tenor — `rv{t}` / `rvCen{t}`, from the feed's own
|
|
66
|
+
* realized-volatility dataset, and the first realized series on this feed that is
|
|
67
|
+
* not derived client-side.
|
|
68
|
+
*
|
|
69
|
+
* These are trailing windows, already annualized and rooted at the source. That
|
|
70
|
+
* detail is load-bearing: the terminal's existing `√(ccVar·252)` is the SAME
|
|
71
|
+
* quantity at window length one (the source's per-day column literally is our
|
|
72
|
+
* `ccVar`), and it cannot be turned into these by smoothing it here. `√` is
|
|
73
|
+
* concave, so averaging the already-rooted series lands ~19% low — the variance has
|
|
74
|
+
* to be averaged first.
|
|
75
|
+
*
|
|
76
|
+
* `rvCen` is earnings-censored on the same convention as {@link hvCol}, which is
|
|
77
|
+
* what finally makes realized-versus-implied a *same-basis* comparison: `rvCen{t}`
|
|
78
|
+
* against `hv{t}`, rather than one censored series against one uncensored one.
|
|
79
|
+
*
|
|
80
|
+
* Null before 2017-11-30 — the source does not reach further back, where the ATM
|
|
81
|
+
* columns start in 2012.
|
|
82
|
+
*/
|
|
83
|
+
export declare const rvCol: (tenor: number) => string;
|
|
84
|
+
export declare const rvCenCol: (tenor: number) => string;
|
|
85
|
+
/**
|
|
86
|
+
* ATM vol **slope** at a tenor — the daily skew measure, from
|
|
87
|
+
* `SurfaceFixedTermHist`.
|
|
88
|
+
*
|
|
89
|
+
* It is `atmVol × (skewU1 − skewD1)`: the fitted curve one grid step above ATM
|
|
90
|
+
* minus one step below, in **absolute vol points**, not a ratio.
|
|
91
|
+
*
|
|
92
|
+
* **Negative is normal equity skew** — downside vol above upside — and more
|
|
93
|
+
* negative is a steeper crash premium. Worth stating because the sign is the whole
|
|
94
|
+
* signal and getting it backwards would be worse than not having it: verified
|
|
95
|
+
* against 34,159 expiries, and corroborated on the loaded data three ways (SPY is
|
|
96
|
+
* steeper than every single name; 252d is ~1.5× the 21d; SPY's 2020 median is
|
|
97
|
+
* nearly 3× calm 2024).
|
|
98
|
+
*
|
|
99
|
+
* Carries its own unit rather than sharing the vol `%` axis. It is a *difference*
|
|
100
|
+
* of two vols, an order of magnitude smaller and signed, so putting it on the ATM
|
|
101
|
+
* axis would flatten both curves into a line.
|
|
102
|
+
*/
|
|
103
|
+
export declare const skewCol: (tenor: number) => string;
|
|
104
|
+
/** Columns of a vol series — `time`, the two ATM term structures (`iv{t}` +
|
|
105
|
+
* `hv{t}`, percent — see {@link impliedCol} for what each one is and is not),
|
|
106
|
+
* plus the daily variance + expected-move scalars a daily history row carries:
|
|
107
|
+
* `ccVar`/`hlVar` (close-close / high-low variance, raw decimals) and
|
|
108
|
+
* `hEMove`/`iEMove` (historical / implied expected move, percent). Built from
|
|
109
|
+
* {@link VOL_TENORS} so the term structure is the schema, not a fixed handful
|
|
110
|
+
* of series. */
|
|
111
|
+
export declare const VOL_SCHEMA: readonly [{
|
|
112
|
+
readonly name: "time";
|
|
113
|
+
readonly kind: "time";
|
|
114
|
+
}, ...{
|
|
115
|
+
name: string;
|
|
116
|
+
kind: "number";
|
|
117
|
+
}[], {
|
|
118
|
+
readonly name: "ccVar";
|
|
119
|
+
readonly kind: "number";
|
|
120
|
+
}, {
|
|
121
|
+
readonly name: "hlVar";
|
|
122
|
+
readonly kind: "number";
|
|
123
|
+
}, {
|
|
124
|
+
readonly name: "hEMove";
|
|
125
|
+
readonly kind: "number";
|
|
126
|
+
}, {
|
|
127
|
+
readonly name: "iEMove";
|
|
128
|
+
readonly kind: "number";
|
|
129
|
+
}];
|
|
130
|
+
export type VolSeries = TimeSeries<typeof VOL_SCHEMA>;
|
|
131
|
+
/** A vol series' columns keyed by name — `time` plus every `iv{t}`/`hv{t}`. A
|
|
132
|
+
* non-finite cell is a gap (columnar ingress tolerates it). */
|
|
133
|
+
export type VolColumns = Readonly<Record<string, readonly number[]>>;
|
|
134
|
+
/** Build a {@link VolSeries} from its columns (ascending in time) — the one place
|
|
135
|
+
* the vol `TimeSeries` is constructed, shared by the fixture and the real
|
|
136
|
+
* ticker-history adapter. Columnar (not row) ingress so a non-finite cell reads
|
|
137
|
+
* as a gap rather than being rejected. */
|
|
138
|
+
export declare function buildVolSeries(name: string, columns: VolColumns): VolSeries;
|
|
139
|
+
export interface VolSeriesOptions {
|
|
140
|
+
bars?: number;
|
|
141
|
+
interval?: BarInterval;
|
|
142
|
+
endMs?: number;
|
|
143
|
+
seed?: number;
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Generate a deterministic vol **term structure** for a symbol: a base level
|
|
147
|
+
* that drifts + spikes, with each tenor an `hv`/`iv` pair around it — longer
|
|
148
|
+
* tenors sit a touch higher and feel a stress spike less (a plausible
|
|
149
|
+
* upward-sloping curve). Seeded by symbol so a ticker's surface is stable across
|
|
150
|
+
* stories/tests/dev.
|
|
151
|
+
*
|
|
152
|
+
* **Known fidelity gap.** This models `hv` as a noisy leader and `iv` as its
|
|
153
|
+
* smoothed tracker, which is what the columns were believed to be. Production is
|
|
154
|
+
* not like that: `hv21` is bit-identical to `iv21` on 63.4% of rows and
|
|
155
|
+
* correlates at 0.97 (see {@link impliedCol}). So mock shows two visibly
|
|
156
|
+
* distinct lines where real data shows one line most of the time. Harmless for
|
|
157
|
+
* layout, axis and interaction work — which is what fixtures are for here — but
|
|
158
|
+
* do not read a *relationship* between these two series off mock data.
|
|
159
|
+
*/
|
|
160
|
+
export declare function generateVolSeries(symbol: string, options?: VolSeriesOptions): VolSeries;
|
package/dist/vol.js
ADDED
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
import { TimeSeries } from 'pond-ts';
|
|
2
|
+
import { BAR_INTERVAL_MS } from './types.js';
|
|
3
|
+
/**
|
|
4
|
+
* The **volatility** series schema + deterministic fixtures — the vol terminal's
|
|
5
|
+
* data shape.
|
|
6
|
+
*
|
|
7
|
+
* A vol series is `time`-keyed with two ATM columns per standard tenor —
|
|
8
|
+
* `iv{tenor}` (implied) and `hv{tenor}` (the feed's second ATM family; see
|
|
9
|
+
* {@link impliedCol} for why it is not named for a meaning) — the term
|
|
10
|
+
* structure. A "metric" is a column and a tenor is just another column, so
|
|
11
|
+
* several tenors plot as independent series (compare tenors / read the curve); a
|
|
12
|
+
* series id doubles as its column name (instance identity — `iv63` is ATM
|
|
13
|
+
* implied at 63 days). All values are percent. Extraction-ready for
|
|
14
|
+
* `@pond-ts/finance`.
|
|
15
|
+
*
|
|
16
|
+
* GARCH / skew are intentionally absent: the daily feed has no source, and a
|
|
17
|
+
* placeholder column reads as a dead chip. They return as computed columns via
|
|
18
|
+
* the indicator/derive seam (PLAN item 4).
|
|
19
|
+
*/
|
|
20
|
+
function mulberry32(seed) {
|
|
21
|
+
let a = seed >>> 0;
|
|
22
|
+
return () => {
|
|
23
|
+
a |= 0;
|
|
24
|
+
a = (a + 0x6d2b79f5) | 0;
|
|
25
|
+
let t = Math.imul(a ^ (a >>> 15), 1 | a);
|
|
26
|
+
t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
|
|
27
|
+
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
function symbolSeed(symbol, salt) {
|
|
31
|
+
let h = 2166136261;
|
|
32
|
+
for (const ch of `${symbol}:${salt}`) {
|
|
33
|
+
h ^= ch.charCodeAt(0);
|
|
34
|
+
h = Math.imul(h, 16777619);
|
|
35
|
+
}
|
|
36
|
+
return h >>> 0;
|
|
37
|
+
}
|
|
38
|
+
const REFERENCE_EPOCH_MS = 1_704_067_200_000;
|
|
39
|
+
function round2(n) {
|
|
40
|
+
return Math.round(n * 100) / 100;
|
|
41
|
+
}
|
|
42
|
+
function round6(n) {
|
|
43
|
+
return Math.round(n * 1e6) / 1e6;
|
|
44
|
+
}
|
|
45
|
+
/** The standard ATM tenor set (calendar days) — the daily feed's per-tenor
|
|
46
|
+
* columns. Ascending; `21` (~1 month) is the default. */
|
|
47
|
+
export const VOL_TENORS = [5, 21, 42, 63, 84, 105, 126, 189, 252, 378, 504];
|
|
48
|
+
/** The tenor a fresh ATM-vol series shows until another is picked (~1 month). */
|
|
49
|
+
export const DEFAULT_VOL_TENOR = 21;
|
|
50
|
+
/**
|
|
51
|
+
* Column ids for the feed's two ATM vol families at a tenor. The id doubles as
|
|
52
|
+
* the chart series id (instance identity), so these must be stable +
|
|
53
|
+
* collision-free.
|
|
54
|
+
*
|
|
55
|
+
* **Both are ATM implied vol.** The feed publishes two ATM implied curves that
|
|
56
|
+
* differ only in which **expected-earnings-move estimate was censored out** of
|
|
57
|
+
* the curve — a fitted estimate (`iEMove`) versus a sample of past moves
|
|
58
|
+
* (`hEMove`) — not implied versus historical. Four lines of evidence, recorded
|
|
59
|
+
* in the application this package came from: a shared curve parameterisation,
|
|
60
|
+
* the pairing with the two expected-move column families, realized vol living
|
|
61
|
+
* in its own dataset, and the two columns coinciding exactly wherever the
|
|
62
|
+
* earnings count is 0.
|
|
63
|
+
*
|
|
64
|
+
* That matters here because `hvCol` was `historicalCol` and surfaced as
|
|
65
|
+
* **"Realized Vol"** until 2026-08-24 — so the default chart drew two implied
|
|
66
|
+
* curves and named one of them realized. A fifth line of evidence, measured over
|
|
67
|
+
* 36,003 rows of a daily history table while fixing the label:
|
|
68
|
+
*
|
|
69
|
+
* | check | result |
|
|
70
|
+
* | ------------------------------------------- | ----------------- |
|
|
71
|
+
* | `hv21` bit-identical to `iv21` | **63.4%** of rows |
|
|
72
|
+
* | `corr(hv21, iv21)` | 0.9748 |
|
|
73
|
+
* | `corr(hv21, realized)` vs `corr(iv21, …)` | 0.8196 vs 0.8391 |
|
|
74
|
+
* | …on the 13,178 rows where `hv21 != iv21` | 0.8147 vs 0.8617 |
|
|
75
|
+
*
|
|
76
|
+
* The last row is the one that closes the question. It rules out the charitable
|
|
77
|
+
* reading — a genuine historical fit that falls back to implied when unavailable
|
|
78
|
+
* — because on exactly the rows where such a fit would reveal itself, `hv` tracks
|
|
79
|
+
* realized vol *worse* than implied does. There is no realized signal hiding
|
|
80
|
+
* under the duplication. (Realized is `√(mean(ccVar, 21)·252)`; correlation is
|
|
81
|
+
* scale-invariant, so that estimator's own calibration does not affect the
|
|
82
|
+
* comparison.)
|
|
83
|
+
*
|
|
84
|
+
* Names, therefore: `iv` keeps its meaning, and `hv` is a neutral abbreviation of
|
|
85
|
+
* the feed's own field rather than a claim about it — this package does not
|
|
86
|
+
* adopt the vendor's column name, and the name it does use asserts nothing.
|
|
87
|
+
* The user-facing label is `ATM Vol (h-censored)`.
|
|
88
|
+
*/
|
|
89
|
+
export const impliedCol = (tenor) => `iv${tenor}`;
|
|
90
|
+
/**
|
|
91
|
+
* `hv{t}` — the feed's second ATM implied curve, and **not historical or realized vol**
|
|
92
|
+
* despite the letter.
|
|
93
|
+
*
|
|
94
|
+
* It is ATM *implied* vol under the other earnings-censoring basis; see
|
|
95
|
+
* {@link impliedCol} for the full evidence and measurements. Named for the column
|
|
96
|
+
* rather than for a meaning precisely because the meaning was guessed wrong once:
|
|
97
|
+
* this was `historicalCol` and surfaced as "Realized Vol" until 2026-08-24, and an
|
|
98
|
+
* identifier that asserts a quantity leaves the bug in the code after it is fixed
|
|
99
|
+
* on screen.
|
|
100
|
+
*
|
|
101
|
+
* Carries its own comment rather than deferring entirely to `impliedCol`, because
|
|
102
|
+
* hovering `hvCol` in an editor is exactly when a reader needs to be told this.
|
|
103
|
+
*/
|
|
104
|
+
export const hvCol = (tenor) => `hv${tenor}`;
|
|
105
|
+
/**
|
|
106
|
+
* **Realized** vol at a tenor — `rv{t}` / `rvCen{t}`, from the feed's own
|
|
107
|
+
* realized-volatility dataset, and the first realized series on this feed that is
|
|
108
|
+
* not derived client-side.
|
|
109
|
+
*
|
|
110
|
+
* These are trailing windows, already annualized and rooted at the source. That
|
|
111
|
+
* detail is load-bearing: the terminal's existing `√(ccVar·252)` is the SAME
|
|
112
|
+
* quantity at window length one (the source's per-day column literally is our
|
|
113
|
+
* `ccVar`), and it cannot be turned into these by smoothing it here. `√` is
|
|
114
|
+
* concave, so averaging the already-rooted series lands ~19% low — the variance has
|
|
115
|
+
* to be averaged first.
|
|
116
|
+
*
|
|
117
|
+
* `rvCen` is earnings-censored on the same convention as {@link hvCol}, which is
|
|
118
|
+
* what finally makes realized-versus-implied a *same-basis* comparison: `rvCen{t}`
|
|
119
|
+
* against `hv{t}`, rather than one censored series against one uncensored one.
|
|
120
|
+
*
|
|
121
|
+
* Null before 2017-11-30 — the source does not reach further back, where the ATM
|
|
122
|
+
* columns start in 2012.
|
|
123
|
+
*/
|
|
124
|
+
export const rvCol = (tenor) => `rvcc${tenor}`;
|
|
125
|
+
export const rvCenCol = (tenor) => `rvccen${tenor}`;
|
|
126
|
+
/**
|
|
127
|
+
* ATM vol **slope** at a tenor — the daily skew measure, from
|
|
128
|
+
* `SurfaceFixedTermHist`.
|
|
129
|
+
*
|
|
130
|
+
* It is `atmVol × (skewU1 − skewD1)`: the fitted curve one grid step above ATM
|
|
131
|
+
* minus one step below, in **absolute vol points**, not a ratio.
|
|
132
|
+
*
|
|
133
|
+
* **Negative is normal equity skew** — downside vol above upside — and more
|
|
134
|
+
* negative is a steeper crash premium. Worth stating because the sign is the whole
|
|
135
|
+
* signal and getting it backwards would be worse than not having it: verified
|
|
136
|
+
* against 34,159 expiries, and corroborated on the loaded data three ways (SPY is
|
|
137
|
+
* steeper than every single name; 252d is ~1.5× the 21d; SPY's 2020 median is
|
|
138
|
+
* nearly 3× calm 2024).
|
|
139
|
+
*
|
|
140
|
+
* Carries its own unit rather than sharing the vol `%` axis. It is a *difference*
|
|
141
|
+
* of two vols, an order of magnitude smaller and signed, so putting it on the ATM
|
|
142
|
+
* axis would flatten both curves into a line.
|
|
143
|
+
*/
|
|
144
|
+
export const skewCol = (tenor) => `skew${tenor}`;
|
|
145
|
+
/** Columns of a vol series — `time`, the two ATM term structures (`iv{t}` +
|
|
146
|
+
* `hv{t}`, percent — see {@link impliedCol} for what each one is and is not),
|
|
147
|
+
* plus the daily variance + expected-move scalars a daily history row carries:
|
|
148
|
+
* `ccVar`/`hlVar` (close-close / high-low variance, raw decimals) and
|
|
149
|
+
* `hEMove`/`iEMove` (historical / implied expected move, percent). Built from
|
|
150
|
+
* {@link VOL_TENORS} so the term structure is the schema, not a fixed handful
|
|
151
|
+
* of series. */
|
|
152
|
+
export const VOL_SCHEMA = [
|
|
153
|
+
{ name: 'time', kind: 'time' },
|
|
154
|
+
...VOL_TENORS.flatMap((t) => [
|
|
155
|
+
{ name: impliedCol(t), kind: 'number' },
|
|
156
|
+
{ name: hvCol(t), kind: 'number' },
|
|
157
|
+
{ name: rvCol(t), kind: 'number' },
|
|
158
|
+
{ name: rvCenCol(t), kind: 'number' },
|
|
159
|
+
{ name: skewCol(t), kind: 'number' },
|
|
160
|
+
]),
|
|
161
|
+
{ name: 'ccVar', kind: 'number' },
|
|
162
|
+
{ name: 'hlVar', kind: 'number' },
|
|
163
|
+
{ name: 'hEMove', kind: 'number' },
|
|
164
|
+
{ name: 'iEMove', kind: 'number' },
|
|
165
|
+
];
|
|
166
|
+
/** Build a {@link VolSeries} from its columns (ascending in time) — the one place
|
|
167
|
+
* the vol `TimeSeries` is constructed, shared by the fixture and the real
|
|
168
|
+
* ticker-history adapter. Columnar (not row) ingress so a non-finite cell reads
|
|
169
|
+
* as a gap rather than being rejected. */
|
|
170
|
+
export function buildVolSeries(name, columns) {
|
|
171
|
+
return TimeSeries.fromColumns({
|
|
172
|
+
name,
|
|
173
|
+
schema: VOL_SCHEMA,
|
|
174
|
+
columns: columns,
|
|
175
|
+
sort: true,
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Generate a deterministic vol **term structure** for a symbol: a base level
|
|
180
|
+
* that drifts + spikes, with each tenor an `hv`/`iv` pair around it — longer
|
|
181
|
+
* tenors sit a touch higher and feel a stress spike less (a plausible
|
|
182
|
+
* upward-sloping curve). Seeded by symbol so a ticker's surface is stable across
|
|
183
|
+
* stories/tests/dev.
|
|
184
|
+
*
|
|
185
|
+
* **Known fidelity gap.** This models `hv` as a noisy leader and `iv` as its
|
|
186
|
+
* smoothed tracker, which is what the columns were believed to be. Production is
|
|
187
|
+
* not like that: `hv21` is bit-identical to `iv21` on 63.4% of rows and
|
|
188
|
+
* correlates at 0.97 (see {@link impliedCol}). So mock shows two visibly
|
|
189
|
+
* distinct lines where real data shows one line most of the time. Harmless for
|
|
190
|
+
* layout, axis and interaction work — which is what fixtures are for here — but
|
|
191
|
+
* do not read a *relationship* between these two series off mock data.
|
|
192
|
+
*/
|
|
193
|
+
export function generateVolSeries(symbol, options = {}) {
|
|
194
|
+
const { bars = 180, interval = '1d', endMs = REFERENCE_EPOCH_MS, seed } = options;
|
|
195
|
+
const rng = mulberry32(seed ?? symbolSeed(symbol, 'vol'));
|
|
196
|
+
const stepMs = BAR_INTERVAL_MS[interval];
|
|
197
|
+
const spikeAt = Math.floor(bars * 0.68);
|
|
198
|
+
const time = [];
|
|
199
|
+
const cols = { ccVar: [], hlVar: [], hEMove: [], iEMove: [] };
|
|
200
|
+
for (const t of VOL_TENORS) {
|
|
201
|
+
cols[impliedCol(t)] = [];
|
|
202
|
+
cols[hvCol(t)] = [];
|
|
203
|
+
cols[rvCol(t)] = [];
|
|
204
|
+
cols[rvCenCol(t)] = [];
|
|
205
|
+
cols[skewCol(t)] = [];
|
|
206
|
+
}
|
|
207
|
+
// Per-tenor smoothed `iv` (tracks its `hv`, a touch slower).
|
|
208
|
+
const atm = {};
|
|
209
|
+
let level = 19 + rng() * 3; // ~19–22%
|
|
210
|
+
for (const t of VOL_TENORS)
|
|
211
|
+
atm[t] = level;
|
|
212
|
+
for (let i = 0; i < bars; i++) {
|
|
213
|
+
time.push(endMs - (bars - 1 - i) * stepMs);
|
|
214
|
+
// base level mean-reverts to ~20; a transient spike decays after `spikeAt`
|
|
215
|
+
level += (20 - level) * 0.03 + (rng() - 0.5) * 1.2;
|
|
216
|
+
const spike = i >= spikeAt ? 26 * Math.exp(-(i - spikeAt) / 14) : 0;
|
|
217
|
+
for (const t of VOL_TENORS) {
|
|
218
|
+
// Term structure: longer tenors sit higher + flatter, and damp the spike.
|
|
219
|
+
const lift = Math.log(t / 21) * 1.1; // ~0 at 21d, positive for longer
|
|
220
|
+
const damp = 0.45 + 21 / (21 + t); // 1.0 at 5d → ~0.5 at 504d
|
|
221
|
+
const hv = Math.max(6, level + lift + spike * damp + (rng() - 0.5) * 3);
|
|
222
|
+
const iv = (atm[t] ?? level) + (hv - (atm[t] ?? level)) * 0.45; // iv tracks hv, smoother
|
|
223
|
+
atm[t] = iv;
|
|
224
|
+
cols[hvCol(t)].push(round2(hv));
|
|
225
|
+
cols[impliedCol(t)].push(round2(iv));
|
|
226
|
+
// Realized sits NEAR implied but wanders independently — a vol-risk-premium
|
|
227
|
+
// sign that flips, which is the real relationship. The censored twin runs a
|
|
228
|
+
// touch lower, since removing the earnings jump can only reduce the measure.
|
|
229
|
+
const rv = Math.max(4, iv + (rng() - 0.45) * 6);
|
|
230
|
+
cols[rvCol(t)].push(round2(rv));
|
|
231
|
+
cols[rvCenCol(t)].push(round2(Math.max(3, rv - 0.4 - rng() * 1.6)));
|
|
232
|
+
// NEGATIVE — normal equity skew, downside vol above upside — and steeper at
|
|
233
|
+
// longer tenors, matching the measured ~1.5× from 21d to 252d. The fixture
|
|
234
|
+
// reproduces the SIGN and the term shape deliberately: those are the two
|
|
235
|
+
// things a reader would take away from a mock chart, and a fixture that got
|
|
236
|
+
// either backwards would teach the opposite of the data.
|
|
237
|
+
const steepen = 0.6 + Math.log(t / 5) * 0.35;
|
|
238
|
+
cols[skewCol(t)].push(round2(-(1.2 * steepen + rng() * 0.5)));
|
|
239
|
+
}
|
|
240
|
+
// Variance + expected-move scalars, tracking the same level (mock analogues of
|
|
241
|
+
// the daily feed's columns). Variance is a raw decimal — a daily close-close
|
|
242
|
+
// variance ~ (σ/√252)²; expected move ~ a 21-day move, in percent.
|
|
243
|
+
const sigma = level / 100;
|
|
244
|
+
const dailyVar = (sigma * sigma) / 252;
|
|
245
|
+
cols.ccVar.push(round6(dailyVar));
|
|
246
|
+
cols.hlVar.push(round6(dailyVar * 1.5)); // high-low runs a touch wider
|
|
247
|
+
const emove = level * Math.sqrt(21 / 252); // ~monthly move, percent
|
|
248
|
+
cols.hEMove.push(round2(emove));
|
|
249
|
+
cols.iEMove.push(round2(emove * 1.08)); // implied a touch richer
|
|
250
|
+
}
|
|
251
|
+
return buildVolSeries(`${symbol}:vol`, { time, ...cols });
|
|
252
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tidal-ts/core",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"private": false,
|
|
5
|
+
"description": "Financial time-series vocabulary on pond-ts: the study/derive seam, the vol schema, bar windows, the columnar adapter and session helpers.",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/tidal-app/tidal-ts.git",
|
|
10
|
+
"directory": "packages/core"
|
|
11
|
+
},
|
|
12
|
+
"homepage": "https://github.com/tidal-app/tidal-ts#readme",
|
|
13
|
+
"type": "module",
|
|
14
|
+
"main": "dist/index.js",
|
|
15
|
+
"types": "dist/index.d.ts",
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"import": "./dist/index.js"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"sideEffects": false,
|
|
23
|
+
"files": [
|
|
24
|
+
"dist",
|
|
25
|
+
"!dist/**/*.test.*"
|
|
26
|
+
],
|
|
27
|
+
"engines": {
|
|
28
|
+
"node": ">=22"
|
|
29
|
+
},
|
|
30
|
+
"publishConfig": {
|
|
31
|
+
"access": "public"
|
|
32
|
+
},
|
|
33
|
+
"peerDependencies": {
|
|
34
|
+
"@pond-ts/financial": "^0.69.0",
|
|
35
|
+
"@pond-ts/process": "0.69.0",
|
|
36
|
+
"pond-ts": "^0.69.0"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@pond-ts/financial": "^0.69.0",
|
|
40
|
+
"@pond-ts/process": "0.69.0",
|
|
41
|
+
"pond-ts": "^0.69.0",
|
|
42
|
+
"typescript": "^5.7.2",
|
|
43
|
+
"vitest": "^2.1.8"
|
|
44
|
+
},
|
|
45
|
+
"scripts": {
|
|
46
|
+
"build": "rm -rf dist && tsc -p tsconfig.json",
|
|
47
|
+
"typecheck": "tsc -p tsconfig.test.json",
|
|
48
|
+
"test": "vitest run"
|
|
49
|
+
}
|
|
50
|
+
}
|