Repository navigation
Add ta.alma, ta.swma, ta.cmo, ta.tsi, ta.range, ta.kcw and ta.sar - #9
Merged
Merged
Conversation
…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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 atoffset * (length - 1)with widthlength / sigma, normalised to sum to 1.floor = truerounds the centre down first. Asigmaof 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 lastlengthchanges. A flat window hasup + downequal 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 oflong_lengthand then one ofshort_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 asta.highestandta.lowest.ta.kcw(series, length, mult, useTrueRange):(upper - lower) / middle.ta.kcandta.kcwnow 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 byinc, up tomax. 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:
tests/script/reference.ts, compared to 1e-9 on the 400-bar fixture with a few parameter sets.bar_indexisbar_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.ta.almaas its example of an unsupported name now usesta.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) andpnpm buildpass locally and CI is green.Closes #1
Closes #2
Closes #3
Closes #4