OPEN-SOURCE SCRIPT

Liquidity Spectrum Visualizer [BigBeluga] [Optimized]

292
This version of Liquidity Spectrum Visualizer (© BigBeluga) has been optimized to improve execution speed and reduce script load times without altering the visual output or analytical logic of the original indicator. The key improvements focus on reducing computational complexity, eliminating redundant calculations, and minimizing expensive function calls within loops.

Core Optimization Changes
Single-Pass Volume Binning (O(N) instead of O(N×M))
Original: For each bin (100) the script iterated through every bar (lookback), resulting in ~20,000 operations.
Optimized: Each bar is processed once to directly calculate its bin index. This reduces the loop complexity from O(N×M) to O(N), where N = lookback.

Precomputed Min/Max Values
Original: array.min() and array.max() were repeatedly called inside loops, re-scanning arrays hundreds of times.
Optimized: Min and max are computed once before all calculations and reused, reducing computational overhead.

Reduced Label Creation
Original: Labels were created in every iteration, potentially hundreds of times per update — a very expensive operation in Pine.
Optimized: Only two labels are created for significant high and low levels, cutting down label calls by ~99%.

Efficient Resource Management
All boxes and lines are cleared once before re-rendering instead of being deleted individually inside nested loops.
Optional gradient rendering and POC drawing remain, but only after binning is complete.

Performance Evaluation
The most important change is the reduction of loop complexity — instead of performing around 20,000 iterations per update, the optimized version now processes only about 200. This reduces execution time and makes the indicator much lighter.

Function calls such as min() and max() are now calculated only once instead of hundreds of times, which removes unnecessary overhead. Likewise, label creation has been reduced from hundreds of labels per refresh to just two, further improving performance.

As a result, the average loading time of the indicator dropped from roughly 1.5–3 seconds to about 0.05–0.2 seconds on typical datasets.

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.