JMZ indicator V8.3

JBwells · study · 330 行 · 点赞 15 · TradingView 原页

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

Pine Script

//@version=6
indicator("AMT V8.3 | Orochi-Style Balance Profiles", shorttitle="AMT V8.3", overlay=true, max_bars_back=500, max_boxes_count=500, max_lines_count=100)
 
// ───── 1. BALANCE DETECTION ─────
g1 = "1. Balance Detection"
rangeBars = input.int(18, "Range detection bars", minval=8, maxval=60, group=g1)
atrLength = input.int(14, "ATR length", minval=2, group=g1)
rangeATR = input.float(5.0, "Maximum range size / ATR", minval=1, step=0.25, group=g1)
minCrosses = input.int(3, "Minimum midpoint crossings", minval=2, maxval=15, group=g1)
minInside = input.float(0.60, "Minimum closes inside range", minval=0.3, maxval=1, step=0.05, group=g1)
minBalanceBars = input.int(12, "Minimum balance length", minval=6, maxval=60, group=g1)
 
// ───── 2. AUCTION COMPLETION ─────
g2 = "2. Auction Completion"
departureATR = input.float(0.50, "Breakout distance / ATR", minval=0.1, step=0.05, group=g2)
impulseBodyATR = input.float(0.70, "Impulse candle body / ATR", minval=0.1, step=0.05, group=g2)
confirmBars = input.int(3, "Outside closes to confirm", minval=2, maxval=10, group=g2)
reentryATR = input.float(0.10, "Reentry tolerance / ATR", minval=0, step=0.05, group=g2)
cooldown = input.int(3, "Cooldown after departure", minval=0, maxval=20, group=g2)
maxBalanceBars = input.int(300, "Maximum balance length", minval=60, maxval=350, group=g2)
 
// ───── 3. VOLUME PROFILE ─────
g3 = "3. Volume Profile"
rows = input.int(32, "Price rows", minval=12, maxval=40, group=g3)
valuePercent = input.float(68, "Value area %", minval=50, maxval=95, group=g3)
histWidth = input.int(18, "Histogram width", minval=4, maxval=35, group=g3)
historyCount = input.int(12, "Historical balances to show", minval=1, maxval=12, group=g3)
showDeveloping = input.bool(true, "Show developing balance", group=g3)
smoothing = input.float(0.20, "Histogram smoothing", minval=0, maxval=0.75, step=0.05, group=g3)
 
// ───── 4. APPEARANCE ─────
g4 = "4. Appearance"
valueColor = input.color(color.new(color.teal, 55), "Value area", group=g4)
tailColor = input.color(color.new(color.gray, 85), "Outside value", group=g4)
pocColor = input.color(color.new(color.fuchsia, 40), "POC row", group=g4)
levelColor = input.color(color.aqua, "VAH / VAL", group=g4)
pocLineColor = input.color(color.fuchsia, "POC line", group=g4)
 
// ───── BALANCE DETECTION ─────
atr = ta.atr(atrLength)
windowHigh = ta.highest(high, rangeBars)
windowLow = ta.lowest(low, rangeBars)
windowMid = (windowHigh + windowLow) / 2.0
windowSize = windowHigh - windowLow
 
float insideCount = 0.0
int crosses = 0
 
for i = 0 to rangeBars - 1
    if close[i] >= windowLow + windowSize * 0.15 and close[i] <= windowHigh - windowSize * 0.15
        insideCount += 1
 
for i = 0 to rangeBars - 2
    if (close[i] > windowMid and close[i + 1] <= windowMid) or (close[i] < windowMid and close[i + 1] >= windowMid)
        crosses += 1
 
bool candidateBalance = not na(atr) and windowSize <= atr * rangeATR and insideCount / rangeBars >= minInside and crosses >= minCrosses
 
// ───── PROFILE CALCULATION ─────
f_profile(int count, int offset) =>
    float top = -1e20
    float bottom = 1e20
 
    for i = 0 to count - 1
        top := math.max(top, high[i + offset])
        bottom := math.min(bottom, low[i + offset])
 
    float step = math.max((top - bottom) / rows, syminfo.mintick)
    array<float> raw = array.new_float(rows, 0.0)
 
    for i = 0 to count - 1
        int idx = i + offset
        float hi = high[idx]
        float lo = low[idx]
        float vol = nz(volume[idx], 0.0)
        float candleRange = hi - lo
 
        if candleRange <= 0
            int bin = math.max(0, math.min(rows - 1, int(math.floor((close[idx] - bottom) / step))))
            array.set(raw, bin, array.get(raw, bin) + vol)
        else
            for b = 0 to rows - 1
                float binLow = bottom + b * step
                float binHigh = binLow + step
                float shared = math.max(0.0, math.min(hi, binHigh) - math.max(lo, binLow))
                if shared > 0
                    array.set(raw, b, array.get(raw, b) + vol * shared / candleRange)
 
    array<float> hist = array.new_float(rows, 0.0)
 
    for b = 0 to rows - 1
        float center = array.get(raw, b)
        float below = b > 0 ? array.get(raw, b - 1) : center
        float above = b < rows - 1 ? array.get(raw, b + 1) : center
        float smoothed = center * (1.0 - smoothing) + (below + above) * 0.5 * smoothing
        array.set(hist, b, smoothed)
 
    float total = 0.0
    float maxVolume = 0.0
    int pocIndex = 0
 
    for b = 0 to rows - 1
        float v = array.get(hist, b)
        total += v
        if v > maxVolume
            maxVolume := v
            pocIndex := b
 
    int vaLowIndex = pocIndex
    int vaHighIndex = pocIndex
    float included = array.get(hist, pocIndex)
 
    while included < total * valuePercent / 100.0 and (vaLowIndex > 0 or vaHighIndex < rows - 1)
        float below = vaLowIndex > 0 ? array.get(hist, vaLowIndex - 1) : -1.0
        float above = vaHighIndex < rows - 1 ? array.get(hist, vaHighIndex + 1) : -1.0
 
        if above >= below and vaHighIndex < rows - 1
            vaHighIndex += 1
            included += array.get(hist, vaHighIndex)
        else if vaLowIndex > 0
            vaLowIndex -= 1
            included += array.get(hist, vaLowIndex)
        else
            break
 
    float vah = math.min(top, bottom + (vaHighIndex + 1) * step)
    float val = bottom + vaLowIndex * step
    float poc = bottom + (pocIndex + 0.5) * step
 
    [hist, bottom, top, step, pocIndex, vaLowIndex, vaHighIndex, maxVolume, vah, val, poc]
 
// ───── DRAW PROFILE ─────
f_draw(int count, int offset, int leftBar, int rightBar) =>
    array<box> boxes = array.new_box()
    array<line> lines = array.new_line()
 
    [hist, bottom, top, step, pocIndex, vaLowIndex, vaHighIndex, maxVolume, vah, val, poc] = f_profile(count, offset)
 
    if maxVolume > 0 and rightBar > leftBar
        int width = math.max(1, math.min(histWidth, rightBar - leftBar))
 
        for b = 0 to rows - 1
            float v = array.get(hist, b)
 
            if v > 0
                int w = math.max(1, int(math.round(width * v / maxVolume)))
                float y1 = bottom + b * step
                float y2 = math.min(top, y1 + step)
                color c = b == pocIndex ? pocColor : b >= vaLowIndex and b <= vaHighIndex ? valueColor : tailColor
 
                box bx = box.new(rightBar - w, y2, rightBar, y1, bgcolor=c, border_color=color.new(c, 100))
                array.push(boxes, bx)
 
        array.push(lines, line.new(leftBar, vah, rightBar, vah, color=levelColor, width=2))
        array.push(lines, line.new(leftBar, val, rightBar, val, color=levelColor, width=2))
        array.push(lines, line.new(leftBar, poc, rightBar, poc, color=pocLineColor, width=2))
 
    [boxes, lines]
 
// ───── DRAWING STORAGE ─────
var array<box> historicalBoxes = array.new_box()
var array<line> historicalLines = array.new_line()
var array<int> historicalSizes = array.new_int()
var array<box> liveBoxes = array.new_box()
var array<line> liveLines = array.new_line()
 
// ───── AUCTION STATE ─────
var int state = 0
var int balanceStart = na
var int lastEnd = na
var float rangeHigh = na
var float rangeLow = na
var int departureStart = na
var int departureDirection = 0
var int departureCloses = 0
 
// Exported profile levels for Confluence V1.
var float exportedVAH = na
var float exportedVAL = na
var float exportedPOC = na
 
// ───── TRIM OLD DRAWINGS ─────
f_trim(array<box> allBoxes, array<line> allLines, array<int> sizes) =>
    if array.size(sizes) > historyCount
        int oldest = array.shift(sizes)
 
        for i = 0 to oldest - 1
            if array.size(allBoxes) > 0
                box.delete(array.shift(allBoxes))
 
        for i = 0 to 2
            if array.size(allLines) > 0
                line.delete(array.shift(allLines))
 
// ───── AUCTION ENGINE ─────
if barstate.isconfirmed
    if state == 0
        bool ready = na(lastEnd) or bar_index > lastEnd + cooldown + rangeBars
 
        if ready and candidateBalance
            int proposedStart = bar_index - rangeBars + 1
 
            if not na(lastEnd)
                proposedStart := math.max(proposedStart, lastEnd + 1)
 
            balanceStart := proposedStart
            rangeHigh := windowHigh
            rangeLow := windowLow
            departureStart := na
            departureCloses := 0
            state := 1
 
    else if state == 1
        int balanceLength = bar_index - balanceStart + 1
        bool breakUp = close > rangeHigh + atr * departureATR
        bool breakDown = close < rangeLow - atr * departureATR
        bool impulsive = math.abs(close - open) >= atr * impulseBodyATR
 
        if balanceLength >= minBalanceBars and impulsive and (breakUp or breakDown)
            departureStart := bar_index
            departureDirection := breakUp ? 1 : -1
            departureCloses := 1
            state := 2
        else
            bool closeNearRange = close >= rangeLow - atr * departureATR and close <= rangeHigh + atr * departureATR
 
            if closeNearRange
                rangeHigh := math.max(rangeHigh, high)
                rangeLow := math.min(rangeLow, low)
 
    else if state == 2
        bool up = departureDirection == 1
        bool backInside = close >= rangeLow - atr * reentryATR and close <= rangeHigh + atr * reentryATR
        bool stillOutside = up ? close > rangeHigh + atr * departureATR : close < rangeLow - atr * departureATR
 
        if backInside
            state := 1
            departureStart := na
            departureCloses := 0
        else
            departureCloses := stillOutside ? departureCloses + 1 : 0
 
            if departureCloses >= confirmBars
                int endBar = departureStart - 1
                int count = endBar - balanceStart + 1
                int offset = bar_index - endBar
 
                if count >= minBalanceBars and count <= maxBalanceBars
                    // Publish this completed balance's levels.
                    [histExport, bottomExport, topExport, stepExport, pocIdxExport, lowIdxExport, highIdxExport, maxExport, vahExport, valExport, pocExport] = f_profile(count, offset)
                    exportedVAH := vahExport
                    exportedVAL := valExport
                    exportedPOC := pocExport
 
                    [newBoxes, newLines] = f_draw(count, offset, balanceStart, endBar)
 
                    if array.size(newBoxes) > 0
                        array.push(historicalSizes, array.size(newBoxes))
 
                        for bx in newBoxes
                            array.push(historicalBoxes, bx)
 
                        for ln in newLines
                            array.push(historicalLines, ln)
 
                        f_trim(historicalBoxes, historicalLines, historicalSizes)
 
                lastEnd := endBar
                state := 0
                departureStart := na
                departureCloses := 0
 
    // Preserve the auction if it reaches the maximum length.
    if state != 0 and not na(balanceStart)
        int totalLength = bar_index - balanceStart + 1
 
        if totalLength > maxBalanceBars
            int endBar = state == 2 and not na(departureStart) ? departureStart - 1 : bar_index - 1
            int count = endBar - balanceStart + 1
            int offset = bar_index - endBar
 
            if count >= minBalanceBars and count <= maxBalanceBars
                [histExport, bottomExport, topExport, stepExport, pocIdxExport, lowIdxExport, highIdxExport, maxExport, vahExport, valExport, pocExport] = f_profile(count, offset)
                exportedVAH := vahExport
                exportedVAL := valExport
                exportedPOC := pocExport
 
                [newBoxes, newLines] = f_draw(count, offset, balanceStart, endBar)
 
                if array.size(newBoxes) > 0
                    array.push(historicalSizes, array.size(newBoxes))
 
                    for bx in newBoxes
                        array.push(historicalBoxes, bx)
 
                    for ln in newLines
                        array.push(historicalLines, ln)
 
                    f_trim(historicalBoxes, historicalLines, historicalSizes)
 
            lastEnd := endBar
            state := 0
            departureStart := na
            departureCloses := 0
 
// ───── DEVELOPING PROFILE ─────
if barstate.islast
    for bx in liveBoxes
        box.delete(bx)
    array.clear(liveBoxes)
 
    for ln in liveLines
        line.delete(ln)
    array.clear(liveLines)
 
    if showDeveloping and state != 0 and not na(balanceStart)
        int rightBar = state == 2 and not na(departureStart) ? departureStart - 1 : bar_index
        int count = rightBar - balanceStart + 1
        int offset = bar_index - rightBar
 
        if count >= minBalanceBars and count <= maxBalanceBars
            [newBoxes, newLines] = f_draw(count, offset, balanceStart, rightBar)
            liveBoxes := newBoxes
            liveLines := newLines
 
// ───── CONNECTION OUTPUTS ─────
// Invisible plots read by AMT Confluence V1.
plot(exportedVAH, "Completed VAH", display=display.none)
plot(exportedVAL, "Completed VAL", display=display.none)
plot(exportedPOC, "Completed POC", display=display.none)

← 返回列表