ml_levels Level-quality gates & structure classifiers

Market_Logic_India · library · 91 行 · 点赞 2 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Market_Logic_India
// ══════════════════════════════════════════════════════════════════════════════
// ml_levels — Level-quality gates & structure classifiers  (Batch-4 foundation library)
// The small, shared decisions that separate a REAL level event from noise: sweep-vs-break,
// displacement-confirmed impulse, retest-holds, volume validation, and nearest-level
// proximity. One shared vocabulary so "swept", "broke", "retested and held", and "validated
// pivot" mean the same thing across every SMC / order-block / support-resistance script.
//   import Market_Logic_India/ml_levels/1 as lvl
//
// DESIGN  Pure classifications over the bar geometry / volume you pass — no drawing, no
// persistent object state (level DETECTION and its box/line lifecycle stay host-side; this
// library judges the events). Nothing repaints: every read is a function of closed-bar
// values. Pair each event with a forward test (ml_calib) before trusting it.
//
// CONCEPT CREDIT  Liquidity sweeps / stop-hunts, break-of-structure, order-block
// displacement and flip-then-retest are public Smart-Money-Concepts / price-action ideas.
// Original, dependency-free Pine v6 packaging; not affiliated with any originator.
// ══════════════════════════════════════════════════════════════════════════════
//@version=6
library("ml_levels", overlay = false)
 
// ── SWEEP vs BREAK ──────────────────────────────────────────────────────────────
// The crucial distinction: a WICK through a level that closes back = a liquidity SWEEP
// (stop-hunt, often a reversal); a BODY close beyond = a genuine BREAK. `side` +1 treats
// `level` as resistance (test from below), -1 as support (test from above).
// Returns: 1 = clean break (closed beyond) · 2 = sweep (pierced, closed back) · 0 = neither.
export sweepOrBreak(series float h, series float l, series float c, series float level, series int side) =>
    int outv = 0
    if side > 0
        bool pierced     = h > level
        bool closedThru  = c > level
        outv := pierced and closedThru ? 1 : pierced and not closedThru ? 2 : 0
    else if side < 0
        bool pierced     = l < level
        bool closedThru  = c < level
        outv := pierced and closedThru ? 1 : pierced and not closedThru ? 2 : 0
    outv
 
// Convenience booleans over sweepOrBreak.
export isSweep(series float h, series float l, series float c, series float level, series int side) =>
    sweepOrBreak(h, l, c, level, side) == 2
export isBreak(series float h, series float l, series float c, series float level, series int side) =>
    sweepOrBreak(h, l, c, level, side) == 1
 
// ── DISPLACEMENT (impulse confirmation) ─────────────────────────────────────────
// An order block / breakout is only trustworthy if price left the zone with force. True
// when the bar body ≥ mult × ATR — an ATR-scaled impulse away.
export displacement(series float o, series float c, series float atr, simple float mult) =>
    (na(atr) or atr <= 0.0) ? false : math.abs(c - o) >= atr * mult
 
// ── VOLUME VALIDATION ───────────────────────────────────────────────────────────
// Only trust a pivot / level formed on conviction: volume above k × its SMA. A cheap
// quality lever across pivot / divergence / S-R / OB detection.
export volValidated(series float v, simple int smaLen, simple float k) =>
    v > ta.sma(v, smaLen) * k
 
// ── FLIP-THEN-RETEST ────────────────────────────────────────────────────────────
// After a level breaks, does the RETEST hold? Broken resistance retested as support (and
// vice-versa) is a higher-conviction continuation event. `role` +1 = broke UP (level now
// support), -1 = broke DOWN (level now resistance). `tol` = how close counts as a retest.
// True when price returned to within tol of the level and closed on the correct (holding) side.
export retestHolds(series float h, series float l, series float c, series float level, series int role, series float tol) =>
    role > 0 ? (l <= level + tol and c > level) : role < 0 ? (h >= level - tol and c < level) : false
 
// ── NEAREST-LEVEL PROXIMITY (HUD) ───────────────────────────────────────────────
// From an array of level prices, the nearest one at-or-above and the nearest below the
// given price. Returns [nearestAbove, nearestBelow] (na when none on that side). For a
// "closest support / resistance + distance" dashboard element.
export nearestLevel(array<float> levels, series float price) =>
    float above = na
    float below = na
    if array.size(levels) > 0
        for i = 0 to array.size(levels) - 1
            float lv = array.get(levels, i)
            if not na(lv)
                if lv >= price
                    above := na(above) ? lv : math.min(above, lv)
                else
                    below := na(below) ? lv : math.max(below, lv)
    [above, below]
 
// Distance to the nearest level in `levels` (absolute price units; na if empty).
export nearestDist(array<float> levels, series float price) =>
    [above, below] = nearestLevel(levels, price)
    float da = na(above) ? na : above - price
    float db = na(below) ? na : price - below
    (na(da) and na(db)) ? na : na(da) ? db : na(db) ? da : math.min(da, db)
 
// ── demo output (library preview only) ──
plot(sweepOrBreak(high, low, close, ta.highest(high, 20)[1], 1), "Sweep(2)/Break(1) of 20-bar high (demo)", color = color.new(#6f9bd8, 0))

← 返回列表