Skip to content

Add ta.alma, ta.swma, ta.cmo, ta.tsi, ta.range, ta.kcw and ta.sar - #9

Merged
Kernel-Hunter merged 1 commit into
mainfrom
script-ta-batch
Nov 1, 2026
Merged

Kernel-Hunter merged 1 commit into
mainfrom
script-ta-batch

Conversation

@Kernel-Hunter

Copy link
Copy Markdown
Owner

Adds seven ta.* functions to Kandle Script, each written from its published definition.

  • ta.alma(series, length, offset, sigma, floor): Arnaud Legoux moving average. Gaussian weights centred at offset * (length - 1) with width length / sigma, normalised to sum to 1. floor = true rounds the centre down first. A sigma of 0 or less gives na.
  • ta.swma(source): 4-bar average with weights 1/6, 2/6, 2/6, 1/6.
  • ta.cmo(source, length): Chande momentum oscillator over the last length changes. A flat window has up + down equal to 0, so the result is na.
  • ta.tsi(source, short_length, long_length): Blau's true strength index. The one-bar change is smoothed by an EMA of long_length and then one of short_length, and divided by the absolute change smoothed the same way. The result is between -1 and 1, and na when there is no movement.
  • ta.range(source, length): highest minus lowest, using the same window helper as ta.highest and ta.lowest.
  • ta.kcw(series, length, mult, useTrueRange): (upper - lower) / middle. ta.kc and ta.kcw now share one helper.
  • ta.sar(start, inc, max): Wilder's parabolic SAR. Bar 0 is na. The first trend is up if the second bar closes above the first. The stop never goes past the two previous lows in an uptrend, or the two previous highs in a downtrend. A new extreme raises the factor by inc, up to max. The trend flips when price crosses the stop (a touch does not count), and the new stop is the old extreme point, or the far side of the bar if that is further.

State is kept per bar index like the other built-ins, so re-running the live bar gives the same value.

I didn't add SAR to the "agrees with the chart helpers" test. The chart's Parabolic SAR is the one built into klinecharts, not a helper in src/chart/ta.ts, and its rules differ: it starts in a downtrend from a stop of 0, raises the factor before the first move, clamps against the current and previous bar instead of the two previous bars, and doesn't check the reversal bar's range. So the two won't match bar for bar.

Tests:

  • Whole-array references for each function in tests/script/reference.ts, compared to 1e-9 on the 400-bar fixture with a few parameter sets.
  • Hand-computed cases on small series. ALMA with offset 0.5 on a straight line gives the middle value, SWMA of bar_index is bar_index - 1.5, CMO and TSI on a constant change give 100 and 1 and give na on a flat series, KCW is checked on bars of constant range, and a 7-bar SAR worked out by hand covers a touch that doesn't flip and a reversal.
  • Warm-up and na: every check covers the warm-up bars, plus na inside the ALMA window, a bad ALMA sigma, and na SAR arguments.
  • The parser test that used ta.alma as its example of an unsupported name now uses ta.cog.

The README list under "What is supported" has the new names. Editor completion reads the registry, so it picks them up without changes.

pnpm test (623 tests) and pnpm build pass locally and CI is green.

Closes #1
Closes #2
Closes #3
Closes #4

…Kandle Script

- ta.alma: Arnaud Legoux moving average with the optional floor argument.
- ta.swma: 4-bar symmetric weighted average (1, 2, 2, 1 over 6).
- ta.cmo: Chande momentum oscillator. A flat window gives na.
- ta.tsi: Blau's true strength index, between -1 and 1.
- ta.range: highest minus lowest over the window.
- ta.kcw: Keltner Channel width, sharing its code with ta.kc.
- ta.sar: Wilder's parabolic SAR with per-bar state.

Each one is checked against a whole-array reference in tests/script/reference.ts
and against hand-computed values, including warm-up bars and na input.

Closes #1
Closes #2
Closes #3
Closes #4
@Kernel-Hunter
Kernel-Hunter merged commit 7d93b30 into main Nov 1, 2026
2 checks passed
@Kernel-Hunter
Kernel-Hunter deleted the script-ta-batch branch November 1, 2026 15:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant