OPEN-SOURCE SCRIPT
Updated

CBA Block Tops/Bottoms w/Table

218
How It Works
Separate Block Sizes & Pre-Calculations: • Two inputs let you set the TOP block size and the BOTTOM block size independently (default both 5). • The script pre-calculates the highest high over the TOP block and the lowest low over the BOTTOM block using ta.highest and ta.lowest.

Horizontal Line Customization: • Users can toggle each horizontal line on or off (via showTopLine and showBottomLine). • Colors, widths, and line styles (Solid, Dotted, or Dashed) for TOP and BOTTOM lines are customizable with separate inputs. • When a block ends, if the corresponding line is enabled, a horizontal line is drawn using the user‑selected parameters.

Array Storage and Averaging: • The script appends each block’s TOP value to the tops array and each BOTTOM value to the bottoms array, keeping their sizes to a maximum defined by the lookback (default 25). • A helper function, f_avg(), safely computes the average of the array values.

Table Display: • A table is created with three columns: an index column (with fixed labels “Active”, “Prev”, “Prev-2”, “Prev-3”, “Prev-4”, “Avg”), a TOP column, and a BOTTOM column. • The first five rows display the most recent TOP and BOTTOM signals (“Active” being the most recent), and the final row shows the average values.

This code gives you full control over both the TOP and BOTTOM block calculations and the appearance of their horizontal lines, all while presenting the data in an informative table.
Release Notes
Ai generated response:
The DIFF column represents the difference between the TOP and BOTTOM values for each signal (or group). In simpler terms, for each row of data in your table, it subtracts the BOTTOM value from the TOP value. Here's a deeper breakdown:

What It Shows: The DIFF value tells you how far apart the extreme values are during a specific block of bars. In each signal row, a TOP represents the highest high reached over a certain number of bars, while a BOTTOM represents the lowest low over a different (or the same) number of bars. By subtracting the BOTTOM from the TOP, the DIFF shows the range or gap between these two extremes.

Why It Matters:

Range Measurement: A larger DIFF means there's a bigger gap between the high and low during that block, which could indicate more volatility or a strong directional move.

Trend Insight: Monitoring these differences over time can help you identify periods when the price is moving with significant momentum versus times when it’s consolidating in a narrow range.

Average Analysis: In the averages row, the DIFF shows the average gap between the top and bottom values over your defined lookback period, which might help smooth out noise and spot overall market sentiment or volatility.

Practical Use Cases: If you're using the script for technical analysis or for setting up trading strategies, the DIFF column gives you a quick visual cue about the magnitude of price swings in the corresponding block. This can be particularly handy when designing thresholds for signal confirmations or when assessing risk.

In short, by including the DIFF column, you’re adding an extra level of insight right into your table, making it easier to see not just the extreme values themselves, but also how far apart they are—which is often an indicator of market behavior.
Release Notes
Changed the default for lookback to 20 from 25.
Ai generated response for purpose of DIFF column added to table.
The DIFF column represents the difference between the TOP and BOTTOM values for each signal (or group). In simpler terms, for each row of data in your table, it subtracts the BOTTOM value from the TOP value. Here's a deeper breakdown:

What It Shows: The DIFF value tells you how far apart the extreme values are during a specific block of bars. In each signal row, a TOP represents the highest high reached over a certain number of bars, while a BOTTOM represents the lowest low over a different (or the same) number of bars. By subtracting the BOTTOM from the TOP, the DIFF shows the range or gap between these two extremes.

Why It Matters:

Range Measurement: A larger DIFF means there's a bigger gap between the high and low during that block, which could indicate more volatility or a strong directional move.

Trend Insight: Monitoring these differences over time can help you identify periods when the price is moving with significant momentum versus times when it’s consolidating in a narrow range.

Average Analysis: In the averages row, the DIFF shows the average gap between the top and bottom values over your defined lookback period, which might help smooth out noise and spot overall market sentiment or volatility.

Practical Use Cases: If you're using the script for technical analysis or for setting up trading strategies, the DIFF column gives you a quick visual cue about the magnitude of price swings in the corresponding block. This can be particularly handy when designing thresholds for signal confirmations or when assessing risk.

In short, by including the DIFF column, you’re adding an extra level of insight right into your table, making it easier to see not just the extreme values themselves, but also how far apart they are—which is often an indicator of market behavior.
Release Notes
Added 2 rows in the table under Active for the previous last 2 averages (Prev-1, Prev-2) based on default (20) or your lookback period settings for TOPS or BOTTOMS.
Release Notes
UPDATED SCRIPT:
Persistent LB Line Variables: The script declares persistent line variables for LB TOP and LB BOTTOM lines (three for each). They are updated when a new LB average is computed.

Updating the LB Lines:

Before creating a new LB line, the script deletes the oldest persistent line (if it exists) by checking if not na(lbLineTopPrev2) or if not na(lbLineBottomPrev2).

The persistent line variables are then shifted and a new LB line is drawn spanning the lookback period.

Result: Only the current LB Active, LB Prev-1, and LB Prev-2 lines remain visible on the chart for both TOPs and BOTTOMs.

This approach keeps your chart clean while showing only the current set of LB lines.

Disclaimer

The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.