@tidal-ts/terminal 0.2.2 → 0.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.
@@ -1,5 +1,5 @@
1
1
  import { assign, enqueueActions, setup } from 'xstate';
2
- import { COMPARE_PREFIX, hasPairOp, usesCompare, deriveId, opParams, opInputs, opNeedsColumns, opIsBand, opIsMulti, TARGET_ROLES, opSharesSourceAxis, studyTag, inputNames, isPairOp, isValidSpec, partStudyLabel, readPairParts, specUnit, substituteInput, } from '@tidal-ts/core';
2
+ import { COMPARE_PREFIX, hasPairOp, usesCompare, deriveId, opParams, opInputs, opNeedsColumns, opIsBand, opIsMulti, TARGET_ROLES, opSharesSourceAxis, studyTag, inputNames, isPickedInput, isPairOp, isValidSpec, partStudyLabel, readPairParts, specUnit, studyInputOf, substituteInput, } from '@tidal-ts/core';
3
3
  /**
4
4
  * The bar columns each data SOURCE carries — the one fact that decides whether a
5
5
  * multi-column study is offerable on a series. Only the price series has bars;
@@ -392,7 +392,10 @@ function toConfig(e, id, axis = e.axis) {
392
392
  /** The spec a study of `target` at `(op, period)` induces — reads the target's
393
393
  * DATA column, or **nests** the target's own spec when the target is derived
394
394
  * (the engine reads a string input as a raw column, so composition nests;
395
- * content-addressing makes the seated copy and the nested copy one node). */
395
+ * content-addressing makes the seated copy and the nested copy one node). A
396
+ * MULTI-OUTPUT target is nested as one PICKED output, its primary
397
+ * (`studyInputOf`) — never bare, which would read whichever output it happens
398
+ * to declare first (TDL-STUDYCHAIN). */
396
399
  const studySpec = (target, op, period) => {
397
400
  // Params come from the REGISTRY, not from a list here: an op is tuned by
398
401
  // exactly what it declares, so a two-param study (a band's `period` +
@@ -420,7 +423,7 @@ const studySpec = (target, op, period) => {
420
423
  // and `deriveId` dedupes them. That is correct — ATR(14) of the bars is one
421
424
  // series — and it is why the picker has to refuse the op where its columns
422
425
  // are absent (`opNeedsColumns`) rather than let the fold skip it silently.
423
- const self = target.derive ?? target.column;
426
+ const self = studyInputOf(target.derive ?? target.column);
424
427
  const inputs = opInputs(op).map((r) =>
425
428
  // A target role takes the target; a bar role takes its own column. No
426
429
  // default and not a target role should be unreachable (a benchmark role is
@@ -430,7 +433,7 @@ const studySpec = (target, op, period) => {
430
433
  return {
431
434
  op,
432
435
  ...(declared.length > 0 ? { params } : {}),
433
- inputs: inputs.length > 0 ? inputs : [target.derive ?? target.column],
436
+ inputs: inputs.length > 0 ? inputs : [self],
434
437
  };
435
438
  };
436
439
  /** A study `SeriesConfig` layered on a target — same source, and the same axis
@@ -450,10 +453,17 @@ function studyConfig(target, op, period, id) {
450
453
  // group, keyed by its own id, and its own unit for the labels.
451
454
  const shares = opSharesSourceAxis(op);
452
455
  const own = specUnit(derive, (col) => (col === target.column ? (target.unit ?? '') : ''));
456
+ // Which output of a multi-output target this study reads, named in the label
457
+ // (`Price · DONCHIAN(20) · Middle · SMA(10)`) so the choice is never silent.
458
+ const picked = derive.inputs.find(isPickedInput);
453
459
  return {
454
460
  id,
455
- ...(shares ? {} : { axisGroup: id }),
456
- label: `${target.label} · ${studyTag(derive)}`, // legend chip
461
+ // Sharing means sharing the SOURCE's scale — which is the source's own axis
462
+ // group when it has one. An SMA of a MACD (or of an RSI) left out of that
463
+ // group landed on the row's shared axis instead, a line near zero drawn on
464
+ // the price's scale.
465
+ ...(shares ? (target.axisGroup ? { axisGroup: target.axisGroup } : {}) : { axisGroup: id }),
466
+ label: `${target.label}${picked ? ` · ${picked.output}` : ''} · ${studyTag(derive)}`, // legend chip
457
467
  family: target.family ?? target.label, // main line = the source's name
458
468
  tenor: target.tenor, // carry the source's tenor into the params sub-line
459
469
  // A study is the same reading, smoothed — so it takes its metric's COLOUR
@@ -550,7 +560,9 @@ const underCompare = (input) => typeof input === 'string'
550
560
  input.startsWith(COMPARE_PREFIX)
551
561
  ? input
552
562
  : `${COMPARE_PREFIX}${input}`
553
- : { ...input, inputs: input.inputs.map(underCompare) };
563
+ : isPickedInput(input)
564
+ ? { from: underCompare(input.from), output: input.output }
565
+ : { ...input, inputs: input.inputs.map(underCompare) };
554
566
  /** "Is this column a catalog metric's?" — {@link layeredOn}'s test for the
555
567
  * derived metrics that stand alone instead of travelling with a source. A
556
568
  * seated config's id no longer says where it came from, so catalog-ness is
@@ -585,7 +597,10 @@ const unitOfColumn = (scope) => columnUnits(scope.catalog, flatConfigs(scope.row
585
597
  * metric, then the compare rebind rewrites the LEAVES — so a compare-bound
586
598
  * studied leg computes the study over the compare columns. */
587
599
  const boundLeg = (leg, input) => {
588
- let graph = leg.spec ?? leg.column;
600
+ // A multi-output leg (a band) reads its PRIMARY output, picked, exactly as a
601
+ // study of it does — nested bare it read whichever output was declared first,
602
+ // so `Donchian − Price` was silently the TOP edge minus the price.
603
+ let graph = studyInputOf(leg.spec ?? leg.column);
589
604
  let label = leg.label;
590
605
  if (input.study) {
591
606
  graph = { op: input.study.op, params: { period: input.study.period }, inputs: [graph] };
@@ -793,8 +808,14 @@ export function respecAllowed(scope, id, name, value) {
793
808
  // own colour, axis, eye and study pipeline, tied together by `SeriesGroup`.
794
809
  // Splitting and re-joining are one edit read in two directions.
795
810
  /** A leg's graph input as a seated config would carry it: a raw column has no
796
- * spec, a derived leg carries its own. */
797
- const legParts = (leg) => typeof leg === 'string' ? { column: leg } : { column: deriveId(leg), derive: leg };
811
+ * spec, a derived leg carries its own, and a PICKED leg is its source study —
812
+ * the config that publishes the output (a band leg splits out as the band). */
813
+ const legParts = (leg) => {
814
+ if (typeof leg === 'string')
815
+ return { column: leg };
816
+ const spec = isPickedInput(leg) ? leg.from : leg;
817
+ return { column: deriveId(spec), derive: spec };
818
+ };
798
819
  /** The colour leg B takes when a pair splits — the first palette key neither
799
820
  * leg A nor anything else in the row is already using, so two fresh lines are
800
821
  * never the same colour. Falls back to leg A's if the palette is exhausted. */
@@ -860,7 +881,7 @@ adding = false) {
860
881
  const id = pairIdFree ? ((pairIdFree = false), pair.id) : mintId(seq++);
861
882
  if (id !== pair.id)
862
883
  plan.mints += 1;
863
- const unit = typeof input === 'string' ? unitOf(input) : specUnit(input, unitOf);
884
+ const unit = derive ? specUnit(derive, unitOf) : unitOf(column);
864
885
  // A spread arrives UNLINKED because it oscillates; its LEGS do not — they
865
886
  // are the raw metrics, and the whole point of drawing both is comparing
866
887
  // them, which needs ONE scale. So a created leg is placed like any new
@@ -1365,18 +1386,11 @@ export const terminalMachine = setup({
1365
1386
  // live — the same move `b544480` made for multi-output targets.
1366
1387
  if (!studyOfferable(target.source, event.op))
1367
1388
  return false;
1368
- // A MULTI-OUTPUT study cannot be a study target. Its `column` is a spec id
1369
- // naming several columns and none of its own, so `sma(macd(...))` would
1370
- // read a column that does not exist: the spec is well-formed, `deriveId`
1371
- // names it happily, and the fold quietly produces nothing — a config
1372
- // sitting in the panel computing silence, which is the worst failure
1373
- // shape. Which output such a study should read is a real question the
1374
- // engine has no way to answer (a MACD's line? its histogram?), so refuse
1375
- // rather than guess. The `Add ▸ Study` menu hides on these too; this is
1376
- // the guard that covers the picker, which stays open across picks and
1377
- // appends to the chain TAIL.
1378
- if (target.derive && opIsMulti(target.derive.op))
1379
- return false;
1389
+ // A MULTI-OUTPUT target is fine: `studySpec` reads ONE of its outputs,
1390
+ // picked by name (its primary), so the study has a column to read and the
1391
+ // label says which. This used to refuse a MACD, because a bare nested
1392
+ // multi-output spec read "output 0" — and let a band through, where the
1393
+ // same rule silently smoothed a Donchian's top edge (TDL-STUDYCHAIN).
1380
1394
  // A study takes at most ONE study of its own: a metric may carry several
1381
1395
  // study CHAINS, but a chain never branches (Peter, 2026-09-12). The panel
1382
1396
  // reads a chain top-down as a pipeline, and a branch has no reading in
@@ -1432,7 +1446,11 @@ export const terminalMachine = setup({
1432
1446
  // whatever the bindings, and two legs drawing one line if unjoined. The
1433
1447
  // same metric ACROSS roles (or under different studies) differs after
1434
1448
  // binding, so George's canonical case passes.
1435
- const name = (i) => (typeof i === 'string' ? i : deriveId(i));
1449
+ const name = (i) => typeof i === 'string'
1450
+ ? i
1451
+ : isPickedInput(i)
1452
+ ? `${deriveId(i.from)}#${i.output}`
1453
+ : deriveId(i);
1436
1454
  if (name(spec.inputs[0]) === name(spec.inputs[1]))
1437
1455
  return false;
1438
1456
  // UNJOINED: no spread is ever seated, so the test is whether the two legs
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tidal-ts/terminal",
3
- "version": "0.2.2",
3
+ "version": "0.3.0",
4
4
  "private": false,
5
5
  "description": "The terminal machine — an XState v5 statechart that owns a chart's configuration (rows, series, studies, pairs, axes, presets) and never its data.",
6
6
  "license": "MIT",
@@ -36,8 +36,8 @@
36
36
  "xstate": "^5.28.0"
37
37
  },
38
38
  "dependencies": {
39
- "@tidal-ts/chart": "0.2.2",
40
- "@tidal-ts/core": "0.2.2"
39
+ "@tidal-ts/chart": "0.3.0",
40
+ "@tidal-ts/core": "0.3.0"
41
41
  },
42
42
  "devDependencies": {
43
43
  "@pond-ts/charts": "^0.70.0",