top of page

Pine Script Library

Public·1 member

Density-based spatial clustering of applications with noise (DBSCAN) MicroClusters



DBSCAN MicroClusters [MarketFragments]


Real-time (online) DBSCAN clustering on bar-range microstructure. The engine

finds dense "micro-clusters" of bars whose range/volatility behavior is

statistically similar -- structure that often accompanies hidden liquidity,

absorption, or impending momentum shifts -- and highlights when a cluster

persists long enough to matter.


This is a port of our ThinkScript DBSCAN engine and the closest-to-textbook

DBSCAN we could build in Pine: exact k-nearest-neighbor identification, core

detection in normalized space, 3-hop transitive density-reachability, and

closest-core border assignment, recomputed on every bar.


Important up front: this is a research preview. It has NOT been forward tested,

and no performance claims are made. See the notes at the bottom.


─────────────────────────────────────────────────────────────

HOW IT WORKS

─────────────────────────────────────────────────────────────


STEP 1 -- THE FEATURE (the "spread" proxy)

Pine has no bid/ask feed, so the clustered feature is a configurable

microstructure proxy: High-Low Range (default), True Range, ATR(14), or

|Close-Open|; mid-price is hl2. Four normalized distance metrics combine it

with price level, time separation, or spread change: Spread1D, Spread+Price,

Spread+Time, Spread+SpreadChange.


STEP 2 -- EPS AUTO-SCALING

Range proxies are 10-100x more dispersed than a quoted bid/ask spread, so

fixed tick tolerances would classify every bar as noise. With auto-scaling ON

(default) the eps tolerances are denominated in the proxy's own standard

deviation over the lookback window -- the eps inputs become sigma multipliers

and the defaults work across symbols and timeframes. Turn it OFF to get raw

tick-denominated eps.


STEP 3 -- k-NEAREST NEIGHBORS (k = 4)

For each bar, the engine computes the distance to each of the last N

(lookback) bars, finds the four nearest neighbors exactly (distance AND bar

offset), and takes the 4th-nearest distance -- the k-distance -- as the local

density reading.


STEP 4 -- CORE / BORDER / NOISE + CLUSTERING

A bar is a CORE point if its k-distance <= 1.0 in normalized space. Non-core

bars with a core among their four nearest neighbors are BORDER points,

assigned to the closest core (not arbitrary inheritance); everything else is

NOISE. Cluster IDs propagate through 3-hop transitive density-reachability: a

new core either joins the cluster it can reach or founds a new one.


STEP 5 -- SIGNAL

When a cluster (ID >= Threshold) persists for at least SignalLength

consecutive bars -- and, if volume weighting is on, buying pressure (buy/sell

imbalance) is above VolumeThreshold -- the SIGNAL prints as translucent aqua

columns. Persistence is the point: the longer a cluster holds, the more

structurally meaningful the regime.


─────────────────────────────────────────────────────────────

SIGNALS

─────────────────────────────────────────────────────────────


Aqua columns Cluster persistence >= SignalLength bars + volume gate.


The forming bar repaints by construction (a High-Low proxy can only expand

intrabar); cluster and signal status settle at bar close. Evaluate on closed

bars.


─────────────────────────────────────────────────────────────

WHAT YOU SEE

─────────────────────────────────────────────────────────────


Colored histogram Cluster ID per bar (green = 1, blue = 2, yellow = 3, ...)

Red bars Noise -- no cluster

Dark gray bars Border points (weaker, but attached to a cluster)

Aqua columns SIGNAL -- cluster persistence + volume gate

Gray lines Neighbor density and k-distance (scaled), optional

Info panel Proxy, k-dist, core/border/noise type, cluster ID, signal

status, buying pressure, eps mode


─────────────────────────────────────────────────────────────

WAYS TO USE IT

─────────────────────────────────────────────────────────────


1. Trend confirmation -- persistent clusters (aqua signal) often precede or

accompany strong moves

2. Mean-reversion scalps -- fade noise (red) when volume is low

3. Volume-filtered entries -- enable UseVolume + VolumeThreshold

4. Multi-mode testing -- run the four DistanceTypes side by side on the same

chart

5. Watch persistence -- the longer a cluster holds, the higher-quality the

structure


─────────────────────────────────────────────────────────────

SETTINGS

─────────────────────────────────────────────────────────────


Core Lookback (50-75 for 5-min charts, 30-40 on tick charts),

Signal Length, Cluster ID Threshold

Distance Metric Distance type (Spread1D is simplest and most stable),

spread proxy

Epsilon Auto-scale toggle (sigma multipliers) or raw tick tolerances

Weighting Volume pressure on/off, scale, threshold

Display Info panel on/off + background transparency, border points,

density lines


On symbols without a volume feed (SPX, VIX, many forex/CFD tickers) buying

pressure is undefined; the indicator falls back to a neutral 50% so signals

are not permanently vetoed, and the panel shows "no volume".


─────────────────────────────────────────────────────────────

IMPORTANT NOTES

─────────────────────────────────────────────────────────────


-- This indicator has NOT been backtested or forward tested

-- No performance claims are made

-- Shared as a research tool for community review

-- Clusters describe structure, not direction; nothing here is a trade

instruction

-- The forming bar settles at close (range proxies expand intrabar)

-- Results will vary by instrument, timeframe, and market conditions

-- This is not financial advice

-- Trading involves substantial risk of loss

-- Past results do not guarantee future performance

-- Use for educational and research purposes only


─────────────────────────────────────────────────────────────

Free for public use

3 Views
Brain with financial data analysis.

Inquiries at :

Important Risk Notice: Trading involves substantial risk of loss. This is educational content only—not advice. Full details here  ------------>  

Proceed only if you're prepared.

tel#: (843) 321-8514

bottom of page