@tidal-ts/core 0.7.0 → 0.8.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 ADDED
@@ -0,0 +1,43 @@
1
+ # @tidal-ts/core
2
+
3
+ The part of [tidal-ts](https://github.com/tidal-app/tidal-ts) with no drawing in
4
+ it: the technical studies, the volatility data layout, bar windows, the adapter
5
+ for a column-by-column wire format, trading sessions and time zones. Built on
6
+ [pond-ts](https://pond-ts.org).
7
+
8
+ **Pre-1.0: pin an exact version.** Every `@tidal-ts/*` package ships on one
9
+ version number.
10
+
11
+ - [Docs](https://tidal-app.github.io/tidal-ts/docs/packages/core/)
12
+ - [Study catalog](https://tidal-app.github.io/tidal-ts/docs/studies/): every study with a live chart, its
13
+ settings, and how traders use it
14
+
15
+ ## Install
16
+
17
+ ```bash
18
+ npm install --save-exact @tidal-ts/core @pond-ts/process@0.72.0
19
+ npm install pond-ts@~0.72.0 @pond-ts/financial@~0.72.0
20
+ ```
21
+
22
+ The pond packages are peer dependencies: install them once, on the version
23
+ this release names in its `peerDependencies`, so there is only one copy of
24
+ pond-ts in your bundle.
25
+
26
+ ## A study is data
27
+
28
+ ```ts
29
+ import { applyDerived, deriveId, type DeriveSpec } from '@tidal-ts/core';
30
+
31
+ const rsi: DeriveSpec = { op: 'rsi', inputs: ['close'], params: { period: 14 } };
32
+
33
+ // Compute it onto a pond TimeSeries of bars; the values land in this column.
34
+ const withRsi = applyDerived(series, [rsi]);
35
+ const column = deriveId(rsi);
36
+ ```
37
+
38
+ `studyCatalog()` lists every study on offer, and `opParams`, `opOutputs` and
39
+ `studyLevels` describe each one's settings, outputs and guide lines.
40
+
41
+ To draw it, see [`@tidal-ts/chart`](https://www.npmjs.com/package/@tidal-ts/chart).
42
+
43
+ MIT.
package/dist/index.d.ts CHANGED
@@ -8,6 +8,6 @@ export { columnExtent } from './extent.js';
8
8
  export { snapshotToTimeSeries, appendToRows, type PondSnapshot, type PondAppend, type PondValue, type WireColumnSpec, } from './columnar.js';
9
9
  export { exactSessionSegments, inferBarMs, sessionOpenLine, sessionSegments, shiftKeys, sliceBySegments, type Segment, type SessionSegmentOptions, } from './sessions.js';
10
10
  export { monthlyExpirations, thirdFriday } from './expirations.js';
11
- export { holdVolAcrossGrid } from './regrid.js';
11
+ export { holdAcrossGrid, holdVolAcrossGrid, type HeldSchema } from './regrid.js';
12
12
  export { ADOPTED_STUDIES, adoptable, isBandShape, outputMark, studyLevels, studyNeedsColumns, } from './studyCatalog.js';
13
13
  export { DATA_TIME_ZONE, LOCAL_ZONE, TIME_ZONE_CHOICES, resolveTimeZone, timeZoneLabel, type TimeZoneChoice, } from './timeZone.js';
package/dist/index.js CHANGED
@@ -8,7 +8,7 @@ export { columnExtent } from './extent.js';
8
8
  export { snapshotToTimeSeries, appendToRows, } from './columnar.js';
9
9
  export { exactSessionSegments, inferBarMs, sessionOpenLine, sessionSegments, shiftKeys, sliceBySegments, } from './sessions.js';
10
10
  export { monthlyExpirations, thirdFriday } from './expirations.js';
11
- export { holdVolAcrossGrid } from './regrid.js';
11
+ export { holdAcrossGrid, holdVolAcrossGrid } from './regrid.js';
12
12
  // The corpus adoption (TDL-STUDYCAT). `ADOPTED_STUDIES` is exported so the
13
13
  // count is assertable and a widening of `adoptable` is a visible edit.
14
14
  export { ADOPTED_STUDIES, adoptable, isBandShape, outputMark, studyLevels, studyNeedsColumns, } from './studyCatalog.js';
package/dist/regrid.d.ts CHANGED
@@ -1,4 +1,63 @@
1
+ import { TimeSeries, type ColumnDef, type SeriesSchema, type ValueColumn, type ValueColumnsForSchema } from 'pond-ts';
1
2
  import { type VolSeries } from './vol.js';
3
+ type OptionalColumn<C extends ValueColumn> = C extends ColumnDef<infer N, infer K> ? ColumnDef<N, K> & {
4
+ readonly required: false;
5
+ } : never;
6
+ type OptionalColumns<Cs extends readonly ValueColumn[]> = number extends Cs['length'] ? OptionalColumn<Cs[number]>[] : Cs extends readonly [infer H, ...infer T] ? H extends ValueColumn ? T extends readonly ValueColumn[] ? [OptionalColumn<H>, ...OptionalColumns<T>] : [] : [] : [];
7
+ /**
8
+ * The schema {@link holdAcrossGrid} returns: the input's, with every value
9
+ * column optional. A hold makes gaps by design (a day with no row), so a column
10
+ * declared required would be lying, and pond's row and JSON inputs would refuse
11
+ * the series. pond's own `join` and `align` mark their output columns optional
12
+ * for the same reason.
13
+ */
14
+ export type HeldSchema<S extends SeriesSchema> = readonly [
15
+ S[0],
16
+ ...OptionalColumns<ValueColumnsForSchema<S>>
17
+ ];
18
+ /**
19
+ * Re-key `coarse` onto `grid`, holding each row across the grid points inside
20
+ * its own bar. **Every column comes through**, whatever the schema: the vol
21
+ * curve's, a joined comparison's (`cmp_*`), a study's.
22
+ *
23
+ * This is the hold to use on a series that has already been **folded** — a
24
+ * study, or a compare join, computed at the coarse series' own grain. Fold
25
+ * first, then hold, never the other way round: a 20-bar average of a daily
26
+ * series held across one-minute bars is a 20-*minute* average of a step, and
27
+ * reads that day's raw value rather than the average. A join is the same
28
+ * shape: joining two daily series is ~250 rows a year, and joining them after
29
+ * the hold is ~100,000.
30
+ *
31
+ * A row holds over `[key, key + grainMs)`, and only until the next row starts.
32
+ * So a grid point takes the **last row at or before it**, provided it is still
33
+ * inside that row's bar; past the bar it is a gap. That makes three promises:
34
+ *
35
+ * - **A grid point never reads a bar that starts after it**, whatever the
36
+ * coarse bars' phase. (Flooring both sides to a multiple of `grainMs` would
37
+ * hand 14:10 the hourly bar that opens at 14:30.) It reads a bar from the
38
+ * bar's start, so a daily value keyed at midnight is visible from that day's
39
+ * open, as it is on a daily chart.
40
+ * - **A gap stays a gap.** A day the coarse series has no row for is `NaN`
41
+ * (`undefined` for a string column) across that day, not the day before's
42
+ * value carried on. "Nothing published for this session" is not "the same
43
+ * as yesterday".
44
+ * - **A restatement wins.** Rows sharing a key: the later one holds.
45
+ *
46
+ * It assumes the coarse keys mark the **start** of each bar, which is how a
47
+ * daily series keyed at midnight reads. `grainMs` may be larger than a bar (a
48
+ * calendar month, a day that is 23 hours long): the next row still ends the
49
+ * hold. `Infinity` makes it a plain as-of hold, with no gaps.
50
+ *
51
+ * The result has exactly one row per grid point: an empty grid gives an empty
52
+ * series, and an empty coarse series gives all gaps. Value columns come back
53
+ * optional ({@link HeldSchema}).
54
+ *
55
+ * `grid` must be ascending — a series' own keys are. The coarse series must be
56
+ * keyed by `time`, and its value columns must be `number` or `string` (what
57
+ * `TimeSeries.fromColumns` can take back). Anything else throws, naming the
58
+ * column, rather than dropping it.
59
+ */
60
+ export declare function holdAcrossGrid<S extends SeriesSchema>(coarse: TimeSeries<S>, grid: ArrayLike<number>, grainMs: number): TimeSeries<HeldSchema<S>>;
2
61
  /**
3
62
  * Re-key `coarse` onto `times`, holding each source row across every target
4
63
  * timestamp that falls on its day.
@@ -18,5 +77,10 @@ import { type VolSeries } from './vol.js';
18
77
  * `volGrains: ['30m']` answers a `1d` request with `'30m'` via its
19
78
  * last-resort branch, and this would then re-key a 30-minute series onto a daily
20
79
  * grid, silently keeping each day's LAST intraday value.
80
+ *
81
+ * @deprecated Use {@link holdAcrossGrid}, which keeps every column. This one
82
+ * emits `VOL_SCHEMA` columns only, so holding a folded series with it silently
83
+ * drops each joined comparison column and each study.
21
84
  */
22
85
  export declare function holdVolAcrossGrid(coarse: VolSeries, times: readonly number[], name?: string): VolSeries;
86
+ export {};
package/dist/regrid.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { TimeSeries, } from 'pond-ts';
1
2
  import { buildVolSeries, VOL_SCHEMA } from './vol.js';
2
3
  /**
3
4
  * Hold a **coarse** vol series at a **finer** grid — the daily ATM curve drawn on
@@ -27,6 +28,112 @@ import { buildVolSeries, VOL_SCHEMA } from './vol.js';
27
28
  const DAY_MS = 24 * 60 * 60 * 1000;
28
29
  /** The UTC midnight a timestamp belongs to — the daily series' own key. */
29
30
  const dayOf = (t) => Math.floor(t / DAY_MS) * DAY_MS;
31
+ /**
32
+ * Re-key `coarse` onto `grid`, holding each row across the grid points inside
33
+ * its own bar. **Every column comes through**, whatever the schema: the vol
34
+ * curve's, a joined comparison's (`cmp_*`), a study's.
35
+ *
36
+ * This is the hold to use on a series that has already been **folded** — a
37
+ * study, or a compare join, computed at the coarse series' own grain. Fold
38
+ * first, then hold, never the other way round: a 20-bar average of a daily
39
+ * series held across one-minute bars is a 20-*minute* average of a step, and
40
+ * reads that day's raw value rather than the average. A join is the same
41
+ * shape: joining two daily series is ~250 rows a year, and joining them after
42
+ * the hold is ~100,000.
43
+ *
44
+ * A row holds over `[key, key + grainMs)`, and only until the next row starts.
45
+ * So a grid point takes the **last row at or before it**, provided it is still
46
+ * inside that row's bar; past the bar it is a gap. That makes three promises:
47
+ *
48
+ * - **A grid point never reads a bar that starts after it**, whatever the
49
+ * coarse bars' phase. (Flooring both sides to a multiple of `grainMs` would
50
+ * hand 14:10 the hourly bar that opens at 14:30.) It reads a bar from the
51
+ * bar's start, so a daily value keyed at midnight is visible from that day's
52
+ * open, as it is on a daily chart.
53
+ * - **A gap stays a gap.** A day the coarse series has no row for is `NaN`
54
+ * (`undefined` for a string column) across that day, not the day before's
55
+ * value carried on. "Nothing published for this session" is not "the same
56
+ * as yesterday".
57
+ * - **A restatement wins.** Rows sharing a key: the later one holds.
58
+ *
59
+ * It assumes the coarse keys mark the **start** of each bar, which is how a
60
+ * daily series keyed at midnight reads. `grainMs` may be larger than a bar (a
61
+ * calendar month, a day that is 23 hours long): the next row still ends the
62
+ * hold. `Infinity` makes it a plain as-of hold, with no gaps.
63
+ *
64
+ * The result has exactly one row per grid point: an empty grid gives an empty
65
+ * series, and an empty coarse series gives all gaps. Value columns come back
66
+ * optional ({@link HeldSchema}).
67
+ *
68
+ * `grid` must be ascending — a series' own keys are. The coarse series must be
69
+ * keyed by `time`, and its value columns must be `number` or `string` (what
70
+ * `TimeSeries.fromColumns` can take back). Anything else throws, naming the
71
+ * column, rather than dropping it.
72
+ */
73
+ export function holdAcrossGrid(coarse, grid, grainMs) {
74
+ const [keySpec, ...valueSpecs] = coarse.schema;
75
+ if (keySpec.kind !== 'time')
76
+ throw new Error(`holdAcrossGrid: "${coarse.name}" is keyed by ${keySpec.kind}; only a time key can be held`);
77
+ if (!(grainMs > 0))
78
+ throw new Error(`holdAcrossGrid: grainMs must be a positive number of ms, not ${grainMs}`);
79
+ // One walk of both key buffers: for each grid point, the source row it holds
80
+ // (`-1` for a gap). Shared by every column below, so the per-column work is a
81
+ // plain gather. `coarse.length`, not the buffer's: a key buffer may be longer
82
+ // than the series it belongs to.
83
+ const keys = coarse.keyColumn().begin;
84
+ const n = coarse.length;
85
+ const rows = new Int32Array(grid.length);
86
+ let j = -1;
87
+ let prev = -Infinity;
88
+ for (let i = 0; i < grid.length; i += 1) {
89
+ const t = grid[i];
90
+ if (!(t >= prev))
91
+ throw new Error(`holdAcrossGrid: the grid must be ascending; point ${i} (${t}) follows ${prev}`);
92
+ prev = t;
93
+ while (j + 1 < n && keys[j + 1] <= t)
94
+ j += 1;
95
+ rows[i] = j >= 0 && t < keys[j] + grainMs ? j : -1;
96
+ }
97
+ const columns = {
98
+ [keySpec.name]: Float64Array.from(grid),
99
+ };
100
+ for (const spec of valueSpecs) {
101
+ const src = coarse.column(spec.name);
102
+ if (spec.kind === 'number') {
103
+ // Through `at`, not `toFloat64Array()`: the raw buffer does not mark a
104
+ // missing cell (a join's unmatched row reads 0 there). The coarse side is
105
+ // the short one, so this is a few hundred reads.
106
+ const values = new Float64Array(n);
107
+ for (let r = 0; r < n; r += 1) {
108
+ const v = src.at(r);
109
+ values[r] = typeof v === 'number' ? v : NaN;
110
+ }
111
+ const out = new Float64Array(grid.length);
112
+ for (let i = 0; i < grid.length; i += 1) {
113
+ const r = rows[i];
114
+ out[i] = r < 0 ? NaN : values[r];
115
+ }
116
+ columns[spec.name] = out;
117
+ }
118
+ else if (spec.kind === 'string') {
119
+ const out = new Array(grid.length);
120
+ for (let i = 0; i < grid.length; i += 1) {
121
+ const r = rows[i];
122
+ out[i] = r < 0 ? undefined : src.at(r);
123
+ }
124
+ columns[spec.name] = out;
125
+ }
126
+ else {
127
+ throw new Error(`holdAcrossGrid: column "${spec.name}" of "${coarse.name}" is ${spec.kind}; only number and string columns can be held`);
128
+ }
129
+ }
130
+ const schema = [keySpec, ...valueSpecs.map((c) => ({ ...c, required: false }))];
131
+ return TimeSeries.fromColumns({
132
+ name: coarse.name,
133
+ schema: schema,
134
+ columns: columns,
135
+ });
136
+ }
30
137
  /**
31
138
  * Re-key `coarse` onto `times`, holding each source row across every target
32
139
  * timestamp that falls on its day.
@@ -46,6 +153,10 @@ const dayOf = (t) => Math.floor(t / DAY_MS) * DAY_MS;
46
153
  * `volGrains: ['30m']` answers a `1d` request with `'30m'` via its
47
154
  * last-resort branch, and this would then re-key a 30-minute series onto a daily
48
155
  * grid, silently keeping each day's LAST intraday value.
156
+ *
157
+ * @deprecated Use {@link holdAcrossGrid}, which keeps every column. This one
158
+ * emits `VOL_SCHEMA` columns only, so holding a folded series with it silently
159
+ * drops each joined comparison column and each study.
49
160
  */
50
161
  export function holdVolAcrossGrid(coarse, times, name = 'vol') {
51
162
  if (times.length === 0 || coarse.length === 0)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tidal-ts/core",
3
- "version": "0.7.0",
3
+ "version": "0.8.0",
4
4
  "private": false,
5
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
6
  "license": "MIT",
@@ -9,7 +9,7 @@
9
9
  "url": "git+https://github.com/tidal-app/tidal-ts.git",
10
10
  "directory": "packages/core"
11
11
  },
12
- "homepage": "https://github.com/tidal-app/tidal-ts#readme",
12
+ "homepage": "https://tidal-app.github.io/tidal-ts/docs/packages/core/",
13
13
  "type": "module",
14
14
  "main": "dist/index.js",
15
15
  "types": "dist/index.d.ts",
@@ -31,14 +31,14 @@
31
31
  "access": "public"
32
32
  },
33
33
  "peerDependencies": {
34
- "@pond-ts/financial": "^0.70.0",
35
- "@pond-ts/process": "0.70.0",
36
- "pond-ts": "^0.70.0"
34
+ "@pond-ts/financial": "^0.72.0",
35
+ "@pond-ts/process": "0.72.0",
36
+ "pond-ts": "^0.72.0"
37
37
  },
38
38
  "devDependencies": {
39
- "@pond-ts/financial": "^0.70.0",
40
- "@pond-ts/process": "0.70.0",
41
- "pond-ts": "^0.70.0",
39
+ "@pond-ts/financial": "^0.72.0",
40
+ "@pond-ts/process": "0.72.0",
41
+ "pond-ts": "^0.72.0",
42
42
  "typescript": "^5.7.2",
43
43
  "vitest": "^2.1.8"
44
44
  },