OPEN-SOURCE SCRIPT
Cập nhật Valid Highs & Lows (Structure Break)

# Valid Highs & Lows (Structure Break)
Most swing-detection tools mark a high the moment price turns down from it. That is a guess: at the time it is drawn, nothing has confirmed that the turn matters. This indicator uses a stricter rule — the one discretionary traders apply by eye but rarely write down.
**A swing high is only valid once price breaks the last swing low. A swing low is only valid once price breaks the last swing high.**
In other words, a turning point earns the label by producing a structure break. Until that happens, it is a candidate, not a point.
## What it draws
- **H** — a confirmed valid high, placed at the exact bar of the highest high reached since the previous valid low.
- **L** — a confirmed valid low, placed at the lowest low reached since the previous valid high.
- **Red / green lines** — the current valid high and valid low, extended to the right. These are the levels the structure is built on.
- **h? / l? (gray, dashed)** — the pending candidate: the extreme reached so far that has not yet been confirmed by a break. It updates live and disappears the moment it is either confirmed or replaced.
Points strictly alternate: high, low, high, low.
## How it works
The indicator tracks local pivots to define the break reference, and separately tracks the running extreme since the last confirmed point.
When price breaks the reference in the opposite direction, the running extreme becomes the new valid point. The break reference moves with price, so in a trend it is the most recent higher low (or lower high) that has to give way — not an old level from months back.
## Inputs
**Bars on each side** — how significant a local swing must be to serve as the break reference. Lower values produce more points, marked sooner, including minor structure. Higher values produce fewer, larger points, confirmed later. This is the only parameter that changes the character of the output; start at 5 and adjust to the scale you actually trade.
**Confirm break on close** — when enabled, the break only counts if a candle closes beyond the reference. Disabled, a wick is enough. Close-based is more conservative and produces fewer false structure breaks.
**Show pending candidate** — toggles the gray h? / l? marker.
## What to expect
**Confirmation is late by design.** By the time a high is validated, price has already broken the previous low, so it is well below that high. This is inherent to the definition, not a flaw. If you need a signal at the extreme itself, this is the wrong tool.
**The most recent swing is almost always pending.** The right edge of the chart will typically show a gray candidate rather than a confirmed point. That is the honest state of the market — the turn has not yet proven itself.
**Confirmed points do not repaint.** Once an H or L is printed it stays where it is. The gray candidate does move, which is the whole point of showing it in a different colour.
## Using it
The valid high and valid low define the active structure. A sequence of rising valid lows and rising valid highs is an uptrend; falling ones are a downtrend. A break of the current valid low in an uptrend is the first objective evidence that the trend has changed.
The indicator also publishes a hidden plot, **Structure bias**, returning 1 when the last confirmed point was a low (structure up) and -1 when it was a high (structure down). You can feed it into another script with `input.source` to use the structure state as a filter.
Alerts are available for both confirmations.
## Notes
This is an indicator, not a strategy. It marks structure — it does not generate entries, place stops, or size positions.
Most swing-detection tools mark a high the moment price turns down from it. That is a guess: at the time it is drawn, nothing has confirmed that the turn matters. This indicator uses a stricter rule — the one discretionary traders apply by eye but rarely write down.
**A swing high is only valid once price breaks the last swing low. A swing low is only valid once price breaks the last swing high.**
In other words, a turning point earns the label by producing a structure break. Until that happens, it is a candidate, not a point.
## What it draws
- **H** — a confirmed valid high, placed at the exact bar of the highest high reached since the previous valid low.
- **L** — a confirmed valid low, placed at the lowest low reached since the previous valid high.
- **Red / green lines** — the current valid high and valid low, extended to the right. These are the levels the structure is built on.
- **h? / l? (gray, dashed)** — the pending candidate: the extreme reached so far that has not yet been confirmed by a break. It updates live and disappears the moment it is either confirmed or replaced.
Points strictly alternate: high, low, high, low.
## How it works
The indicator tracks local pivots to define the break reference, and separately tracks the running extreme since the last confirmed point.
When price breaks the reference in the opposite direction, the running extreme becomes the new valid point. The break reference moves with price, so in a trend it is the most recent higher low (or lower high) that has to give way — not an old level from months back.
## Inputs
**Bars on each side** — how significant a local swing must be to serve as the break reference. Lower values produce more points, marked sooner, including minor structure. Higher values produce fewer, larger points, confirmed later. This is the only parameter that changes the character of the output; start at 5 and adjust to the scale you actually trade.
**Confirm break on close** — when enabled, the break only counts if a candle closes beyond the reference. Disabled, a wick is enough. Close-based is more conservative and produces fewer false structure breaks.
**Show pending candidate** — toggles the gray h? / l? marker.
## What to expect
**Confirmation is late by design.** By the time a high is validated, price has already broken the previous low, so it is well below that high. This is inherent to the definition, not a flaw. If you need a signal at the extreme itself, this is the wrong tool.
**The most recent swing is almost always pending.** The right edge of the chart will typically show a gray candidate rather than a confirmed point. That is the honest state of the market — the turn has not yet proven itself.
**Confirmed points do not repaint.** Once an H or L is printed it stays where it is. The gray candidate does move, which is the whole point of showing it in a different colour.
## Using it
The valid high and valid low define the active structure. A sequence of rising valid lows and rising valid highs is an uptrend; falling ones are a downtrend. A break of the current valid low in an uptrend is the first objective evidence that the trend has changed.
The indicator also publishes a hidden plot, **Structure bias**, returning 1 when the last confirmed point was a low (structure up) and -1 when it was a high (structure down). You can feed it into another script with `input.source` to use the structure state as a filter.
Alerts are available for both confirmations.
## Notes
This is an indicator, not a strategy. It marks structure — it does not generate entries, place stops, or size positions.
Phát hành các Ghi chú
Most swing-point indicators mark a pivot as soon as it has N bars on each side. That tells you a local extreme formed; it does not tell you the market has accepted it. This script uses the stricter definition: a swing high is only valid once price breaks the last swing low, and a swing low is only valid once price breaks the last swing high. Until that break happens, the point is a candidate, not a structure point.How it works
The script tracks the highest high and the lowest low reached since the last confirmed point. While a high is pending, it watches the last pivot low; the moment price breaks it, the highest high reached since the previous point is stamped as a valid H, and the script flips to waiting for a low. The reverse follows: with a low pending, it watches the last pivot high, and a break upward stamps the lowest low as a valid L. So the valid point is always the true extreme of the leg, not whichever pivot happened to print last.
What you see
H / L labels on confirmed points, placed on the bar that actually made the extreme.
Extended levels from the last valid high and low — the structure currently in force.
Gray "h?" / "l?" marking the candidate that has not been validated yet.
Dotted break level — the pivot price has to take out for the pending candidate to become valid. This is the level to watch; everything else is history.
Settings
Bars on each side controls how significant a swing must be to serve as the break reference — higher values give fewer, larger structure points and slower confirmation. Confirm break on close decides whether a wick through the level counts or only a close beyond it. Validate only on closed bars keeps anything from appearing or disappearing while the current candle is still forming. The break levels can be hidden, or limited to the one currently in play.
Alerts
Two conditions: valid high confirmed and valid low confirmed. Set them to trigger on bar close if you are using Confirm break on close.
Notes
A pivot is only known N bars after it forms, so the break level appears with that lag. This is confirmation delay, not repainting — the script cannot know a pivot before the bars to its right exist. The structure bias is exported as a plot (+1 after a valid low, −1 after a valid high) and the valid high and low are available in the data window, so other scripts can read them.
***Added the break levels — the pivot price must take out to validate the pending point, with an option to show only the one in play. Valid high and low are now exported to the data window. Fixed an edge case where a single bar could validate a high and a low at once***
Mã nguồn mở
Theo đúng tinh thần TradingView, tác giả của tập lệnh này đã công bố nó dưới dạng mã nguồn mở, để các nhà giao dịch có thể xem xét và xác minh chức năng. Chúc mừng tác giả! Mặc dù bạn có thể sử dụng miễn phí, hãy nhớ rằng việc công bố lại mã phải tuân theo Nội quy.
Thông báo miễn trừ trách nhiệm
Thông tin và các ấn phẩm này không nhằm mục đích, và không cấu thành, lời khuyên hoặc khuyến nghị về tài chính, đầu tư, giao dịch hay các loại khác do TradingView cung cấp hoặc xác nhận. Đọc thêm tại Điều khoản Sử dụng.
Mã nguồn mở
Theo đúng tinh thần TradingView, tác giả của tập lệnh này đã công bố nó dưới dạng mã nguồn mở, để các nhà giao dịch có thể xem xét và xác minh chức năng. Chúc mừng tác giả! Mặc dù bạn có thể sử dụng miễn phí, hãy nhớ rằng việc công bố lại mã phải tuân theo Nội quy.
Thông báo miễn trừ trách nhiệm
Thông tin và các ấn phẩm này không nhằm mục đích, và không cấu thành, lời khuyên hoặc khuyến nghị về tài chính, đầu tư, giao dịch hay các loại khác do TradingView cung cấp hoặc xác nhận. Đọc thêm tại Điều khoản Sử dụng.