Market structure
Swing Points
Confirmed swing highs and lows marked with the pivot lookback made explicit, so the turning points you trade from are the ones the chart can actually prove rather than the ones hindsight picks out.
indicator
//@version=6
indicator("Swing Points", "Swings", overlay = true, max_labels_count = 500)
leftBars = input.int(5, "Bars to the left", minval = 1, group = "Settings")
rightBars = input.int(5, "Bars to the right", minval = 1, group = "Settings")
showLabels = input.bool(true, "Label each swing with its price", group = "Settings")
highCol = input.color(#EF5350, "Swing high", group = "Style")
lowCol = input.color(#26A69A, "Swing low", group = "Style")
swingHigh = ta.pivothigh(high, leftBars, rightBars)
swingLow = ta.pivotlow(low, leftBars, rightBars)
foundHigh = not na(swingHigh)
foundLow = not na(swingLow)
plotshape(foundHigh, "Swing high", location = location.abovebar, style = shape.triangledown, color = highCol, size = size.tiny, offset = -rightBars)
plotshape(foundLow, "Swing low", location = location.belowbar, style = shape.triangleup, color = lowCol, size = size.tiny, offset = -rightBars)
if showLabels and foundHigh
label.new(bar_index - rightBars, swingHigh, str.tostring(swingHigh, format.mintick), style = label.style_label_down, color = color.new(highCol, 85), textcolor = highCol, size = size.tiny)
if showLabels and foundLow
label.new(bar_index - rightBars, swingLow, str.tostring(swingLow, format.mintick), style = label.style_label_up, color = color.new(lowCol, 85), textcolor = lowCol, size = size.tiny)
alertcondition(foundHigh, "Swing high confirmed", "A swing high has just been confirmed")
alertcondition(foundLow, "Swing low confirmed", "A swing low has just been confirmed")
This runs in TradingView, not here
Pine Script only executes inside TradingView. Paste the source into the Pine Editor and add it to a chart to see it plotted.
What it will not do
- A pivot needs the bars to its right before it can be confirmed, so every mark appears that many bars late. Nothing here is available in real time.
- Widening the lookback gives fewer and more meaningful swings but confirms them even later. There is no setting that avoids the trade-off.
- Marks are placed back in history at the pivot bar. Do not read a fresh mark as something happening on the current bar.
Written from this description
Mark confirmed swing highs and lows using a pivot lookback of a configurable number of bars either side, label each one with its price, and alert when a new swing is confirmed.
Educational only, not financial advice. The maths is simple arithmetic on the numbers you enter; it knows nothing about your broker, fees, slippage or the market. Something wrong with it? Say so in Site Feedback.