Pivots

All functions · nseries package

PivotIndices

func (s Series) PivotIndices(level int, thresholdPercent float64, filter ...[]bool) []int

PivotIndices returns a slice of integers that represents pivot highs and lows in a series, where a high is a value with a lower value either side, and a low is a value with a higher value either side. A specified threshold represents a percentage price movement to be observed to qualify as a pivot. An optional extra slice of booleans can be provided, indicating whether a bar should be considered (which is useful if, for example, inside bars were to be ignored in the determination of a pivot point).

The specified level determines the number of times the price series is processed. When the level is 1, the pivots of the raw prices (which could be thought of as the short-term pivots) will be returned. When the level is 2, the pivots of the short-term pivots (which could be thought of as the medium-term pivots) will be returned. When the level is 3, the pivots of the medium-term pivots (which could be thought of as the long-term pivots) will be returned.

The slice of indices returned will contain positive values for pivot highs and negative values for pivot lows. Non-pivots are excluded from the results.

The results alternate strictly between highs and lows. Consecutive level-1 pivots of the same type (highs with no low between them, or lows with no high between them) collapse to the most extreme of them, the highest high or the lowest low, the later bar winning an exact tie. For example, Series{1, 5, 2, 2, 3, 1}.PivotIndices(1, 0) keeps only bar 1: the pivot highs at bars 1 (5) and 4 (3) collapse to the higher. At levels 2 and above consecutive same-type pivots also collapse to the most extreme of them, but the earlier bar wins an exact tie.

At level 1, when bars are appended, only the most recent entry can change (a later same-type pivot that is at least as extreme replaces it), so earlier entries are final.

Level construction stops once a level produces no pivots, since no higher level can be derived from an empty set; there is no other cap on level.

thresholdPercent is a percentage, so 5 means 5% and 0.05 means 0.05%, not 5%, and at level 1 it is compared strictly in the product form on the candidate pivot’s own price p, as in Pivots: a neighbour must lie strictly below (1 - t/100)*p for a high or strictly above (1 + t/100)*p for a low. A negative, NaN or infinite threshold is invalid and gives an empty, non-nil slice, whatever the level and filter. A threshold of 100 or more is accepted, but on positive prices it allows no level-1 pivot highs, because (1 - t/100)*p is then at most 0.

Pivots

func (s Series) Pivots(level int, thresholdPercent float64, filter ...[]bool) []int

Pivots returns a slice of integers that represents pivot highs and lows in a series, where a high is a value with a lower value either side, and a low is a value with a higher value either side. A specified threshold represents a percentage price movement to be observed to qualify as a pivot. An optional extra slice of booleans can be provided, indicating whether a bar should be considered (which is useful if, for example, inside bars were to be ignored in the determination of a pivot point).

The specified level determines the number of times the price series is processed. When the level is 1, the pivots of the raw prices (which could be thought of as the short-term pivots) will be returned. When the level is 2, the pivots of the short-term pivots (which could be thought of as the medium-term pivots) will be returned. When the level is 3, the pivots of the medium-term pivots (which could be thought of as the long-term pivots) will be returned.

Pivot highs and lows alternate, as in PivotIndices. Consecutive level-1 pivots of the same type (highs with no low between them, or lows with no high between them) collapse to the most extreme of them, the highest high or the lowest low, the later bar winning an exact tie. At levels 2 and above consecutive same-type pivots also collapse to the most extreme of them, but the earlier bar wins an exact tie.

The slice of indices returned will contain lookback offsets from the end of the slice (positive for pivot highs, negative for pivot lows, zero for non-pivots). For example, if the latest pivot high occurred four bars from the end of the input series, the returned slice would contain a value of 4 for that pivot high in the position four back from the end of the returned slice.

At level 1, when bars are appended, only the most recent pivot can change (a later same-type pivot that is at least as extreme replaces it), so earlier pivots are final: their bars do not change, although their lookback offsets grow by one with each appended bar.

Level construction stops once a level produces no pivots, since no higher level can be derived from an empty set; there is no other cap on level.

thresholdPercent is a percentage, so 5 means 5% and 0.05 means 0.05%, not 5%. At level 1 a neighbour must lie strictly below (1 - t/100)*p for a high or strictly above (1 + t/100)*p for a low, the product form on the candidate pivot’s own price p; because (1 + 10/100)*100 is 110.00000000000001 in floating point, a move of exactly 10% does not qualify. A negative, NaN or infinite threshold is invalid and gives all zeros (no pivots). A threshold of 100 or more is accepted, but on positive prices it allows no level-1 pivot highs, because (1 - t/100)*p is then at most 0.