Indicator · Pine Script v4 · On the chart

Poor Man's Volume Clusters

by akhilman

Poor Man's Volume Clusters calculates an approximation of volume profile clusters over fixed bar blocks and plots them directly on the price chart.

View on GitHub

Repaints Archived by its author

GitHub stars27on the repository
Pine versionv4older, still runs
Lines of code257
Settings5inputs
LicenceMPL-2.0
Last update2 years ago11 Jan 2024

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new indicator, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart, then adjust the settings with the gear icon next to its name.

Source Code

257 lines of Pine Script v4 from clusters.pine, published under MPL-2.0. Keep the author's credit and licence lines when you reuse it.

clusters.pine
//@version=4
study("Poor man's volume clusters", "PMVolClust", overlay=true, max_bars_back={{max_bars_back}})

block_size = input({{block_size}}, "Bars in profile block", minval=10, maxval={{max_bars_back//3}})
visual_row_width = input({{visual_row_width}}, "Visual row width", minval=10)
show_vpoc = input(true, "Show volume point of control")
filter_size = input({{filter_size}}, "Profile maximum high pass filter size in bars")
use_custom_volume_source = input(false, "Use custom volume source")
volume_source_symbol = input("", "Custom volume source symbol", input.symbol)

BOUNARY_COLOR = color.{{boundary_color}}
{%- for color in block_colors %}
PROFILE{{loop.index}}_COLOR = color.{{color}}
{%- endfor %}
VPOC_COLOR = color.{{vpoc_color}}

custom_volume = use_custom_volume_source ? security(volume_source_symbol, timeframe.period, volume, lookahead=barmerge.lookahead_on) : volume

////
// Calculate profiles
///

block_index = floor(bar_index / block_size)
block_bar = bar_index % block_size

rolling_high = highest(block_bar + 1)
rolling_low = lowest(block_bar + 1)

block_high = rolling_high[block_bar+1]
block_low = rolling_low[block_bar+1]

// plot(block_high, color=color.green)
// plot(block_low, color=color.green)

block_height = (block_high - block_low)
row_height = block_height / {{n_rows}}
n_rows_affected = 0

{% for n in range(n_rows) %}
row{{n}}_low = block_low + row_height * {{n}}
row{{n}}_high = block_low + row_height * {{n+1}}
row{{n}}_price = (row{{n}}_low + row{{n}}_high ) / 2
row{{n}}_value = 0.0
if high[block_size] > row{{n}}_low and low[block_size] < row{{n}}_high
    row{{n}}_value := custom_volume[block_size]
    n_rows_affected := n_rows_affected + 1
{% endfor %}

{% for n in range(n_rows) -%}
row{{n}}_value := row{{n}}_value / n_rows_affected / row_height
{% endfor %}

// plot(row0_value, color=color.red, style=plot.style_linebr)

if block_bar != 0
    {% for n in range(n_rows) -%}
    row{{n}}_value := row{{n}}_value + row{{n}}_value[1]
    {% endfor %}

float vpoc_price = na
highest_row_value = 0.0
if block_bar == block_size - 1
    {% for n in range(n_rows) -%}
    if highest_row_value < row{{n}}_value
        highest_row_value := row{{n}}_value
        vpoc_price := row{{n}}_price
    {% endfor %}
else
    highest_row_value := highest_row_value[1]

highest_row_value_avg = ema(highest_row_value, filter_size)

// plot(row0_value)
// plot(highest_row_value, color=color.yellow)
// plot(highest_row_value_avg, color=color.blue)

{% for n in range(n_rows) -%}
row{{n}}_width = floor(visual_row_width * row{{n}}_value / highest_row_value_avg[block_bar+1])
{% endfor %}

// plot(row0_value, color=color.red, style=plot.style_linebr, offset=-block_size)

////
// Draw history blocks
///

// draw boudns

bounds_high_y = block_bar != block_size -1 ? block_high[block_bar+1] : na
bounds_low_y = block_bar != block_size -1 ? block_low[block_bar+1] : na
plot(bounds_high_y, color=BOUNARY_COLOR, style=plot.style_linebr, offset=-block_size*2, title="History block high")
plot(bounds_low_y, color=BOUNARY_COLOR, style=plot.style_linebr, offset=-block_size*2, title="History block low")

// draw profile

{% for color in block_colors %}
{% set pn = (loop.length - loop.index + 1) % loop.length + 1 -%}
{% set num_colors = loop.length -%}

history{{pn}}_block_bar = (block_index + {{loop.index}}) % {{num_colors}} * block_size + block_bar

{% for n in range(n_rows) -%}
history{{pn}}_row{{n}}_price = row{{n}}_price[history{{pn}}_block_bar+1]
history{{pn}}_row{{n}}_width = min({{block_size * num_colors - 2}}, row{{n}}_width[history{{pn}}_block_bar+1])
history{{pn}}_row{{n}}_y = history{{pn}}_block_bar <= history{{pn}}_row{{n}}_width ? history{{pn}}_row{{n}}_price : na
{% endfor %}

{% for n in range(n_rows) -%}
plot(history{{pn}}_row{{n}}_y, color=PROFILE{{pn}}_COLOR, style=plot.style_linebr, linewidth=2, offset=-block_size*2, title="History {{pn}} row {{n}}")
{% endfor %}
{% endfor %}

// draw vpoc

vpoc_y = show_vpoc ? vpoc_price : na
plotshape(vpoc_y, title="VPoC", location=location.absolute, color=VPOC_COLOR, style=shape.diamond, size=size.tiny, offset=-block_size*2)

////
// Caclulate and draw last three blocks
///

{% for rn in range(3) -%}
var line recent{{rn}}_high_line = na
var line recent{{rn}}_low_line = na
{% if rn < 2 %}
var label recent{{rn}}_vpoc_label = na
{% endif %}
{% for n in range(n_rows) -%}
var line recent{{rn}}_row{{n}}_line = na
{% endfor -%}
{% endfor %}

if barstate.islast
    {% for rn in range(3) -%}
    recent{{rn}}_start_i = block_size * {{rn}} + block_bar
    recent{{rn}}_end_i = max(block_size * {{rn}} - (block_size - block_bar) + 1, 0)

    recent{{rn}}_color_n = (block_index[recent{{rn}}_end_i] + 3) % {{block_colors | length}}
    recent{{rn}}_color = {% for color in block_colors -%}
    {% if not loop.first %}else {% endif %}if recent{{rn}}_color_n == {{ loop.index - 1 }}
        color.{{ color }}
    {% endfor %}

    if not na(time[recent{{rn}}_start_i]) and recent{{rn}}_start_i - recent{{rn}}_end_i > 5

        // high/low bounds

        if na(recent{{rn}}_low_line)
            recent{{rn}}_low_line := line.new(bar_index, high, bar_index, high, color=BOUNARY_COLOR)
        if na(recent{{rn}}_high_line)
            recent{{rn}}_high_line := line.new(bar_index, low, bar_index, low, color=BOUNARY_COLOR)

        recent{{rn}}_block_high = rolling_high[recent{{rn}}_end_i]
        recent{{rn}}_block_low = rolling_low[recent{{rn}}_end_i]

        line.set_xloc(recent{{rn}}_low_line, time[recent{{rn}}_start_i], time[recent{{rn}}_end_i], xloc.bar_time)
        line.set_y1(recent{{rn}}_low_line, recent{{rn}}_block_low)
        line.set_y2(recent{{rn}}_low_line, recent{{rn}}_block_low)

        line.set_xloc(recent{{rn}}_high_line, time[recent{{rn}}_start_i], time[recent{{rn}}_end_i], xloc.bar_time)
        line.set_y1(recent{{rn}}_high_line, recent{{rn}}_block_high)
        line.set_y2(recent{{rn}}_high_line, recent{{rn}}_block_high)

        // calculate profile rows

        recent{{rn}}_block_height = recent{{rn}}_block_high - recent{{rn}}_block_low
        recent{{rn}}_row_height = recent{{rn}}_block_height / {{n_rows}}

        {% for n in range(n_rows) -%}
        recent{{rn}}_row{{n}}_low = recent{{rn}}_block_low + recent{{rn}}_row_height * {{n}}
        recent{{rn}}_row{{n}}_high = recent{{rn}}_block_low + recent{{rn}}_row_height * {{n+1}}
        recent{{rn}}_row{{n}}_value = 0.0
        {% endfor %}

        for i = recent{{rn}}_start_i to recent{{rn}}_end_i + 1
            bar_n_rows_affected = 0
            {% for n in range(n_rows) -%}
            bar_row{{n}}_value = 0.0
            if low[i] < recent{{rn}}_row{{n}}_high and high[i] > recent{{rn}}_row{{n}}_low
                bar_row{{n}}_value := custom_volume[i]
                bar_n_rows_affected := bar_n_rows_affected + 1
            {% endfor %}
            {% for n in range(n_rows) -%}
            recent{{rn}}_row{{n}}_value := recent{{rn}}_row{{n}}_value + bar_row{{n}}_value / bar_n_rows_affected / recent{{rn}}_row_height
            {% endfor %}

        recent{{rn}}_avg_i = min(recent{{rn}}_start_i + 1 + block_size * {{rn + 2}}, bar_index)
        {% for n in range(n_rows) -%}
        recent{{rn}}_row{{n}}_price = (recent{{rn}}_row{{n}}_low + recent{{rn}}_row{{n}}_high ) / 2
        recent{{rn}}_row{{n}}_width = floor(visual_row_width * recent{{rn}}_row{{n}}_value / highest_row_value_avg[recent{{rn}}_avg_i])
        {% endfor %}

        // calculate vpoc

        float recent{{rn}}_vpoc_price = na
        float recent{{rn}}_highes_row_value = 0
        {% for n in range(n_rows) -%}
        if recent{{rn}}_highes_row_value < recent{{rn}}_row{{n}}_value
            recent{{rn}}_highes_row_value := recent{{rn}}_row{{n}}_value
            recent{{rn}}_vpoc_price := recent{{rn}}_row{{n}}_price
        {% endfor %}

        // draw profile rows

        {% for n in range(n_rows) -%}
        if na(recent{{rn}}_row{{n}}_line)
            recent{{rn}}_row{{n}}_line := line.new(bar_index, close, bar_index, close, width=2)
        {% endfor %}

        {% for n in range(n_rows) %}
        line.set_xloc(recent{{rn}}_row{{n}}_line, time[recent{{rn}}_start_i], time_close[max(recent{{rn}}_start_i - recent{{rn}}_row{{n}}_width, 0)], xloc.bar_time)
        line.set_y1(recent{{rn}}_row{{n}}_line, recent{{rn}}_row{{n}}_price)
        line.set_y2(recent{{rn}}_row{{n}}_line, recent{{rn}}_row{{n}}_price)
        line.set_color(recent{{rn}}_row{{n}}_line, recent{{rn}}_color)
        {% endfor %}

        {% if rn < 2 %}
        // draw vpoc

        if show_vpoc
            if na(recent{{rn}}_vpoc_label)
                recent{{rn}}_vpoc_label := label.new(bar_index, close, yloc=yloc.price, style=label.style_diamond, size=size.tiny, color=VPOC_COLOR)

            label.set_xloc(recent{{rn}}_vpoc_label, time[recent{{rn}}_start_i + 1], xloc.bar_time)
            label.set_y(recent{{rn}}_vpoc_label, recent{{rn}}_vpoc_price)

        if not show_vpoc and not na(recent{{rn}}_vpoc_label)
            label.delete(recent{{rn}}_vpoc_label)
            recent{{rn}}_vpoc_label := na
        {% endif %}

        0

    else

        // remove unused lines

        if not na(recent{{rn}}_low_line)
            line.delete(recent{{rn}}_low_line)
            recent{{rn}}_low_line := na
        if not na(recent{{rn}}_high_line)
            line.delete(recent{{rn}}_high_line)
            recent{{rn}}_high_line := na
        {% if rn < 2 %}
        if not na(recent{{rn}}_vpoc_label)
            label.delete(recent{{rn}}_vpoc_label)
            recent{{rn}}_vpoc_label := na
        {% endif %}
        {% for n in range(n_rows) -%}
        if not na(recent{{rn}}_row{{n}}_line)
            line.delete(recent{{rn}}_row{{n}}_line)
            recent{{rn}}_row{{n}}_line := na
        {% endfor %}

        0

    {% endfor %}

Overview

Poor Man's Volume Clusters divides price ranges into discrete horizontal rows, aggregates trading volume per level, and optionally highlights the volume point of control (VPOC). Settings allow adjusting block size, row width, high-pass filter sizing, and specifying an external volume symbol. When an external volume source is enabled, it uses security calls with lookahead enabled, which introduces potential lookahead bias on historical bars.

Details

Published by

akhilman on GitHub

Type

Indicator, Pine Script v4

Draws

On the price chart

Category

Volume, Support & resistance

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

  • Reads higher-timeframe data with lookahead on, so the history can show signals that were not there in real time

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
Visual row widthinput{{visual_row_width}}visual_row_width
Show volume point of controlbooltrueshow_vpoc
Profile maximum high pass filter size in barsinput{{filter_size}}filter_size
Use custom volume sourceboolfalseuse_custom_volume_source
Custom volume source symbolsymbolvolume_source_symbol
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

This overlay indicator draws a fixed-range volume profile on the price chart, splitting traded activity into up and down volume at each price level. It also plots a sentiment profile, a developing point of control, and value-area high and low around a configur

Indicator v5 VolumeSupport & resistance
Updated 2 months ago · MIT GitHub repository of Volume Profile and Indicator by DGT

MGIC-tracker

by Allen1212
303 stars

The MGIC-tracker is a TradingView indicator that overlays ARK Invest trade records onto stock price charts. It plots buy and sell labels at the exact timestamp of each transaction, showing share volume and ETF weight. Users select a specific ETF type via setti

Indicator v4 Support & resistanceVolume
Updated 2 years ago · None stated GitHub repository of MGIC-tracker
218 stars

Comprehensive Volume Profile is an overlay indicator that calculates and plots volume distribution across specified time ranges such as daily sessions, weekly, monthly, or custom bar lengths. It identifies key structural price levels including the Point of Con

Indicator v6 VolumeSupport & resistance
Updated 3 months ago · None stated GitHub repository of Comprehensive Volume Profile

Moon Dev GEX Arrows

by moondevonyt
92 stars

This overlay indicator plots option strike walls and an optional session VWAP on the price chart for SPY or ES. It spaces major strikes by the selected instrument, draws a set number of strikes on each side, and marks wall-trap and sweep events after a cooldow

Indicator v5 Support & resistanceSessions & time
Updated 8 weeks ago · Custom licence GitHub repository of Moon Dev GEX Arrows

More indicators

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers