Swings

All functions · nseries package

Swings

func (s Series) Swings(opn Series, high Series, low Series, cls Series, hint ...int) []int

Swings returns a slice of integers that represents swing highs and lows in a series, where zero indicates no swing, a positive value indicates a swing high, and a negative value indicates a swing low. The magnitude of the value indicates how many values back in the series the high/low occurred. Unlike SwingsHL, outside bars are treated as directional bars based on whether the open is greater than the close (down bar) or less than the close (up bar). Inside bars are handled the same way as in SwingsHL. An optional hint parameter can be provided to specify the first known swing. A nonzero hint outside the high/low input’s index range returns len(high) zeros. Zero means no hint.

When one bar is both a swing high and a swing low (an adjacent pair in the raw list), the encoded result can hold only one value for that bar and keeps the later of the two.

From this release, a high followed by a reversal on the same bar is now reported as a high and a low on that bar rather than as two consecutive highs.

Revision: the result describes the whole input, and a swing is reported only once a later bar confirms it. When bars are appended, a final snap pass can move the most recent swing to a later bar while the following bars extend it, and new swings are added after it. Earlier swings have not been seen to change: re-running on every prefix of 500 random walks (149,000 prefix steps, 3 October 2026) revised the latest swing 1,712 times and an earlier one never. That is an observation, not a guarantee. For a swing list that is append-only by construction, see SwingPointIndices with SwingAppendOnly.

SwingsHL

func (s Series) SwingsHL(high Series, low Series, hint ...int) []int

SwingsHL returns a slice of integers that represents swing highs and lows in a series, where zero indicates no swing, a positive value indicates a swing high, and a negative value indicates a swing low. The magnitude of the value indicates how many values back in the series the high/low occurred. For example, for a series of 10 bars with a swing high on the 3rd bar, a low on the 5th bar, and a high on the 9th bar, the returned series would be: [0, 0, 7, 0, 0, -4, 0, 0, 1, 0]. The swings are calculated according to Brent Penfold’s algorithm. An optional hint parameter can be provided to specify the first known swing. If positive, it indicates a swing high at that bar index; if negative, it indicates a swing low at the absolute value of that index. No swing is reported before the hint bar, and none is reported on the hint bar with the opposite sign; earlier bars are still read to classify inside, outside and directional bars. The hinted swing itself can be replaced by a later, more extreme swing of the same kind, as the snap pass may replace any latest swing. A nonzero hint outside the high/low input’s index range returns len(high) zeros. Zero means no hint.

When one bar is both a swing high and a swing low (an adjacent pair in the raw list), the encoded result can hold only one value for that bar and keeps the later of the two.

From this release, a stacked outside bar no longer re-reports a swing it has already reported (which could put entries out of bar order), and a hinted call no longer panics on such windows. Duplicate outside bars no longer break the alternation of highs and lows, and hinted calls no longer report swings before the hint bar or a zero entry.

Revision: the result describes the whole input, and a swing is reported only once a later bar confirms it (stacked outside bars can confirm swings several bars back). When bars are appended, a final snap pass can move the most recent swing to a later bar while the following bars extend it, and new swings are added after it. Earlier swings have not been seen to change: re-running on every prefix of 500 random walks (149,000 prefix steps, 3 October 2026) revised the latest swing 32 times and an earlier one never. That is an observation, not a guarantee. For a swing list that is append-only by construction, see SwingPointIndices with SwingAppendOnly.

SwingsPath

func (s Series) SwingsPath(highs Series, lows Series, paths []float64) []int