Systemicalpha

Confirmed Peak / Trough

systemic:core-pivotIndicatorsfunction1 cr

Inputs

NameTypeDescription
Confirmation BarsintegerEnter a constant whole number \(N\) of at least 1. The candidate must stand against \(N\) earlier and \(N\) later **usable numeric values**. Its level can first appear on the \(N\)th later usable value, so at least \(2N+1\) usable values are needed for any confirmation.
SeriesfloatarrayConnect the numeric series whose turning points you want to study: for example, a price, a daily high or low, or an indicator value. Both output levels retain this input's unit. A constant series cannot create a strict peak or trough.

Outputs

NameTypeDescription
Last PeakfloatarrayThe value of the most recently **confirmed** high swing, in the Series input's unit. It first appears when the required later usable values have arrived, then holds until another peak is confirmed. It is a reference level, not a forecast or an instruction to sell.
Last TroughfloatarrayThe value of the most recently **confirmed** low swing, in the Series input's unit. It first appears when the required later usable values have arrived, then holds until another trough is confirmed. It is a reference level, not a forecast or an instruction to buy.

A price can look like a turning point today and be surpassed tomorrow. Confirmed Peak / Trough waits for a chosen number of later values before calling a high or low a swing. It gives you two running reference levels: the last confirmed peak and the last confirmed trough. Connect closing prices to ask about turns in closes; connect an oscillator to ask a different question about that oscillator.

The crucial timing rule is simple: a level first appears when it is confirmed, never on the earlier bar where the turn occurred. A strategy using this output does not get to know the future.

What qualifies as a swing?

Set Confirmation Bars to (N), a whole number of at least 1. Consider a candidate value (X_i) among the usable numeric values, in their observed order:

Peak at i  ⟺  Xi>max⁡(Xi−N,…,Xi−1)andXi≥max⁡(Xi+1,…,Xi+N),Trough at i  ⟺  Xi<min⁡(Xi−N,…,Xi−1)andXi≤min⁡(Xi+1,…,Xi+N).\begin{aligned} \text{Peak at }i &\iff X_i > \max(X_{i-N},\ldots,X_{i-1}) \quad\text{and}\quad X_i \geq \max(X_{i+1},\ldots,X_{i+N}),\\ \text{Trough at }i &\iff X_i < \min(X_{i-N},\ldots,X_{i-1}) \quad\text{and}\quad X_i \leq \min(X_{i+1},\ldots,X_{i+N}). \end{aligned}

In ordinary terms, a peak must beat each of the previous (N) values and hold up against each of the next (N). A trough is the mirror image. The comparison is strict against the past and allows equality in the future. If several consecutive values make a flat top or bottom, the first can qualify; a later equal value cannot qualify while an earlier equal value is among its preceding (N).

The comparison uses the input series itself. Connecting closing prices identifies turns in closing prices; connecting daily highs identifies turns in daily highs. Neither output predicts the next turn.

When does the level become available?

The candidate needs (N) earlier and (N) later usable values. The earliest possible confirmation therefore needs (2N+1) usable values. A confirmed level appears on the (N)th later usable value. Until another peak is confirmed, Last Peak keeps the previous peak value; Last Trough behaves independently. Before its first confirmation, each side is null.

Here is a small arithmetic sequence, not a market-price series. With (N=2), the trough of 1 at bar 3 becomes available at bar 5; the peak of 4 at bar 5 becomes available at bar 7.

Bar01234567
Series13212432
Last Peaknullnullnullnullnullnullnull4
Last Troughnullnullnullnullnull111
timeline
    title N = 2 - value at the turn versus value available
    Bar 3 : 1 is a possible trough
    Bar 4 : 2 is the first later value
    Bar 5 : 4 confirms trough 1
          : 4 is a possible peak
    Bar 6 : 3 is the first later value for the peak
    Bar 7 : 2 confirms peak 4

The high of 3 at bar 1 is not a confirmed peak: it has only one earlier value. Plotting a symbol over bar 5 to show where the peak of 4 happened can help explain a chart, but a live rule could first use that level at bar 7. This distinction also matters when testing a strategy: using the level at its original turning point would use information that had not arrived yet. TradingView's example of plotting past pivots shows exactly why a beautiful historical chart can mislead.

Build with the confirmed levels

  • Breakout reference. Connect the closing-price series to Greater Than (>) → First Value and Last Peak to Second Value. A true result says the current close is above the latest confirmed high in closes; it can remain true on several bars, so it does not by itself identify the first crossing. Check the level's age and market context; a break alone does not establish a durable trend.
  • Risk boundary. Use Last Trough as a reference when designing a long position's exit rule, or Last Peak for a short position. The level is a historical reference, not a guaranteed support or resistance line. StockCharts' support and resistance guide explains why traders examine prior reaction highs and lows.
  • Compare swings. Plot the two outputs alongside a price or indicator series. Rising confirmed troughs can help you study whether pullbacks are occurring at higher levels. This module retains only the latest level on each side, so it does not by itself compare two historical troughs.

Smaller (N) recognizes shorter swings sooner and can confirm more levels. Larger (N) compares more neighbors and takes longer to confirm; it does not set a minimum move size. Neither setting eliminates false signals. The outputs may hold an old level while price moves far away, and a newly confirmed peak or trough is not a trade instruction on its own.

Gaps, zero, and aligned data

A null gap has no stored observation. A stored NaN has a timestamp but is not a usable numeric value. This indicator skips both when counting the (N) values on either side of a candidate. It leaves the output null at either kind of input gap, even if an earlier level was confirmed; the held level resumes on the next usable position. Zero is a valid number: it participates in comparisons and can itself be a confirmed trough or peak.

If an upstream series is repeated to align with a finer time grid, each repeated numeric position counts as a value here. That can delay or change which swings qualify. Choose the source and time grid with the intended bar spacing in mind; (N) counts usable values, not a fixed duration on the clock.

In a live run, each output contains only its newest value. It can be null during warm-up or when the newest input is null or NaN. With the same earlier data, adding later bars does not move a confirmed level back to the original turning point. Levels depend on the history supplied to the calculation; if older data falls outside it, a held level may disappear until another swing is confirmed.

Rendering diagrams...
© 2026 WanoLabs. All rights reservedAlpha build v0.1.5