Location Scan Entry / Add / Trim Tool v6

renderingnature1 · study · 325 行 · 点赞 22 · 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/
// © renderingnature1
 
//@version=6
indicator("Location Scan Entry / Add / Trim Tool v6", overlay=true, max_labels_count=300, max_lines_count=300)
 
// ── INPUTS ─────────────────────────────────────
groupCore = "Core"
groupMode = "Mode"
groupBot  = "Bottom Zones"
groupTop  = "Top Zones"
groupView = "Display"
 
maLen    = input.int(200, "Long MA", minval=50, group=groupCore)
trendLen = input.int(50, "Trend MA", minval=10, group=groupCore)
zLen     = input.int(50, "Z-Score Length", minval=10, group=groupCore)
atrLen   = input.int(14, "ATR Length", minval=5, group=groupCore)
rsiLen   = input.int(14, "RSI Length", minval=2, group=groupCore)
 
bbLen    = input.int(20, "BBWP BB Length", minval=5, group=groupCore)
bbwpLen  = input.int(100, "BBWP Lookback", minval=20, group=groupCore)
 
useBasis  = input.bool(false, "Owning Mode / Use Cost Basis", group=groupMode)
costBasis = input.float(0.0, "Cost Basis", step=0.01, group=groupMode)
 
purpleZ   = input.float(-1.7, "Purple Washout Z", step=0.1, group=groupBot)
greenZ    = input.float(-2.4, "Green Strong Bottom Z", step=0.1, group=groupBot)
purpleATR = input.float(-1.8, "Purple Washout ATR", step=0.25, group=groupBot)
greenATR  = input.float(-2.8, "Green Strong Bottom ATR", step=0.25, group=groupBot)
addPct    = input.float(-8.0, "Green Add % Below Basis", step=1.0, group=groupBot)
 
orangeZ   = input.float(1.8, "Orange Hot Warning Z", step=0.1, group=groupTop)
orangeATR = input.float(2.0, "Orange Hot Warning ATR", step=0.25, group=groupTop)
trimPct   = input.float(20.0, "Profit Zone % Above Basis", step=1.0, group=groupTop)
 
hotLookback = input.int(8, "Hot Lookback For Red", minval=3, group=groupTop)
 
cooldown  = input.int(20, "Bars Between Same Color", minval=1, group=groupView)
lineBars  = input.int(60, "Dotted Line Length", minval=5, group=groupView)
showMA    = input.bool(true, "Show MAs", group=groupView)
showLines = input.bool(true, "Show Green/Red Dotted Levels", group=groupView)
showTable = input.bool(true, "Show Tiny Table", group=groupView)
 
// ── CORE ───────────────────────────────────────
ma200 = ta.sma(close, maLen)
ma50  = ta.sma(close, trendLen)
atr   = ta.atr(atrLen)
rsi   = ta.rsi(close, rsiLen)
 
basisOk = useBasis and costBasis > 0
basisPL = basisOk ? ((close - costBasis) / costBasis) * 100 : na
 
mean = ta.sma(close, zLen)
dev  = ta.stdev(close, zLen)
z    = dev != 0 ? (close - mean) / dev : 0.0
 
distATR = atr != 0 ? (close - ma200) / atr : 0.0
 
trendUp   = close > ma200 and ma50 > ma200
trendWeak = close < ma200 and ma50 < ma200
trendText = trendUp ? "UP" : trendWeak ? "WEAK" : "MIXED"
 
// ── BBWP STYLE VOLATILITY ──────────────────────
bbBasis = ta.sma(close, bbLen)
bbDev   = ta.stdev(close, bbLen)
bbWidth = bbBasis != 0 ? ((bbDev * 4.0) / bbBasis) * 100.0 : 0.0
bbwp    = ta.percentrank(bbWidth, bbwpLen)
 
compressed = bbwp <= 25
exhausted  = bbwp >= 70 and bbwp < bbwp[1]
 
// ── STRUCTURE HELPERS ──────────────────────────
lowerHigh = high < high[1]
bearClose = close < open and close < close[1]
rsiRoll   = rsi < rsi[1]
 
strongBull = close > high[1] and close > open
impulseUp  = close > close[1] and close[1] > close[2]
 
bullReclaim = strongBull and impulseUp
bearFailure = lowerHigh and bearClose and rsiRoll
 
// ── BOTTOM CONDITIONS ────────────────────────── (Improved)
 
// Base extreme
bottomExtreme = z <= greenZ and distATR <= greenATR and rsi <= 44
 
// Strong reclaim / panic reversal
panicBottom =
     bottomExtreme and
     (close > high[1] or (close > open and close > close[1] and volume > volume[1]))
 
// Capitulation, but BBWP no longer has to be perfect
capitulationBottom =
     bottomExtreme and
     low < low[1] and
     close > low and
     close > close[1] and
     (bbwp >= 45 or bbwp[1] >= 55)
 
// Safe barssince handling
barsSinceLow30 = nz(ta.barssince(low == ta.lowest(low, 30)), 999)
 
// Slow bleed recovery bottom
grindBottom =
     z <= -2.0 and
     distATR <= -2.2 and
     rsi <= 46 and
     rsi > rsi[1] and
     close > close[1] and
     (close >= open or close > hl2) and
     barsSinceLow30 <= 8
 
// Higher-low reclaim bottom
structureBottom =
     z <= -1.5 and
     distATR <= -0.8 and
     rsi <= 48 and
     rsi > rsi[1] and
     close > close[1] and
     close > high[1]
 
// Liquidation wick bottom
wickBottom =
     z <= -1.3 and
     rsi <= 45 and
     distATR <= -0.5 and
     low == ta.lowest(low, 20) and
     (high - low) > ta.atr(14) * 1.5 and
     volume > ta.sma(volume, 20) and
     ((close - low) / math.max(high - low, syminfo.mintick)) >= 0.55
 
strongGreen = panicBottom or capitulationBottom or grindBottom or structureBottom or wickBottom
 
// Purple watch signal
bottomStretch = z <= purpleZ and distATR <= purpleATR and rsi <= 48
 
earlyTurn =
     z <= purpleZ - 0.3 and
     rsi <= 43 and
     rsi > rsi[1] and
     close > close[1]
 
sellingSlows = (close > close[1] and rsi > rsi[1]) or low >= low[1]
 
baseBottom =
     trendWeak and
     z <= purpleZ and
     distATR <= purpleATR + 0.5 and
     bbwp <= 55 and
     sellingSlows
 
purpleRaw = basisOk ?
     basisPL <= addPct * 0.50 and (bottomStretch or earlyTurn or baseBottom) :
     bottomStretch or earlyTurn or baseBottom
 
greenRaw = basisOk ?
     basisPL <= addPct and strongGreen :
     strongGreen
 
// ── TOP CONDITIONS ─────────────────────────────
 
// Orange = hot / stop adding / begin scale-out thinking
topStretch = z >= orangeZ and rsi >= 60
 
wasHotRecently =
     ta.highest(z, hotLookback) >= orangeZ and
     ta.highest(distATR, hotLookback) >= orangeATR
 
orangeRaw = basisOk ?
     basisPL >= trimPct * 0.60 and topStretch :
     topStretch
 
// Red = hot recently + buyer failure + downside follow-through
confirmDown = close < close[1] or close < open
 
redRaw = basisOk ?
     basisPL >= trimPct * 0.60 and wasHotRecently and bearFailure and confirmDown :
     wasHotRecently and bearFailure and confirmDown
 
// Strong signals override weaker ones
purpleRaw := purpleRaw and not greenRaw
orangeRaw := orangeRaw and not redRaw
 
// ── FIRST TOUCH + COOLDOWN ─────────────────────
purpleEnter = purpleRaw and not purpleRaw[1]
greenEnter  = greenRaw and not greenRaw[1]
orangeEnter = orangeRaw and not orangeRaw[1]
redEnter    = redRaw and not redRaw[1]
 
purpleDot = purpleEnter and nz(ta.barssince(purpleEnter[1]), 10000) > cooldown
orangeDot = orangeEnter and nz(ta.barssince(orangeEnter[1]), 10000) > cooldown
 
var int lastGreenBar = na
greenDot = greenEnter and (na(lastGreenBar) or bar_index - lastGreenBar > cooldown * 2)
 
if greenDot
    lastGreenBar := bar_index
 
var int lastRedBar = na
redDot = redEnter and (na(lastRedBar) or bar_index - lastRedBar > cooldown * 2)
 
if redDot
    lastRedBar := bar_index
 
// ── SCALE OUT LOGIC ────────────────────────────
orangeBars = ta.barssince(not orangeRaw)
orangeStack =
     redRaw ? 3 :
     orangeRaw and orangeRaw[1] ? 2 :
     orangeRaw ? 1 :
     0
 
inProfit = basisOk and basisPL > 0
 
scaleAction =
     redRaw and inProfit ? "HEAVY TRIM / EXIT" :
     redRaw ? "RED FAILURE" :
     orangeStack >= 2 and inProfit ? "TRIM MORE 25-50%" :
     orangeStack >= 2 ? "HOT - DO NOT CHASE" :
     orangeStack == 1 and inProfit ? "START TRIM 10-25%" :
     orangeStack == 1 ? "HOT - STOP ADDING" :
     greenRaw ? "ADD / ENTRY ZONE" :
     purpleRaw ? "WATCH FOR BOTTOM" :
     "NO CLEAN EDGE"
 
actionColor =
     redRaw ? color.red :
     orangeStack >= 2 ? color.orange :
     orangeStack == 1 ? color.yellow :
     greenRaw ? color.lime :
     purpleRaw ? color.purple :
     color.white
 
// ── DOTS ───────────────────────────────────────
plotshape(purpleDot, title="Purple Washout", style=shape.circle, location=location.belowbar, color=color.purple, size=size.tiny)
plotshape(greenDot, title="Strong Green Bottom", style=shape.circle, location=location.belowbar, color=color.lime, size=size.small)
plotshape(orangeDot, title="Orange Hot Warning", style=shape.circle, location=location.abovebar, color=color.orange, size=size.tiny)
plotshape(redDot, title="Red Failure After Hot", style=shape.circle, location=location.abovebar, color=color.red, size=size.small)
 
// ── DOTTED LEVELS ──────────────────────────────
if showLines and greenDot
    line.new(bar_index, low, bar_index + lineBars, low, color=color.lime, style=line.style_dotted, width=1)
 
if showLines and redDot
    line.new(bar_index, high, bar_index + lineBars, high, color=color.red, style=line.style_dotted, width=1)
 
// ── PLOTS ──────────────────────────────────────
plot(showMA ? ma200 : na, "200 MA", color=color.white, linewidth=2)
plot(showMA ? ma50 : na, "50 MA", color=color.yellow, linewidth=1)
plot(basisOk ? costBasis : na, "Cost Basis", color=color.aqua, linewidth=2, style=plot.style_linebr)
// MA Labels
var label ma200Label = na
var label ma50Label  = na
 
if barstate.islast
    if not na(ma200Label)
        label.delete(ma200Label)
    if not na(ma50Label)
        label.delete(ma50Label)
 
    ma200Label := label.new(
         bar_index + 2, ma200,
         "200 MA  $" + str.tostring(math.round(ma200)),
         style=label.style_label_left,
         color=color.new(color.white, 100),
         textcolor=color.white,
         size=size.small)
 
    ma50Label := label.new(
         bar_index + 2, ma50,
         "50 MA  $" + str.tostring(math.round(ma50)),
         style=label.style_label_left,
         color=color.new(color.white, 100),
         textcolor=color.yellow,
         size=size.small)
 
// ── TABLE ──────────────────────────────────────
var table t = table.new(position.bottom_right, 2, 8, bgcolor=color.new(color.black, 82), border_width=1)
 
if showTable and barstate.islast
    state =
         greenRaw  ? "GREEN BOTTOM" :
         purpleRaw ? "PURPLE WATCH" :
         redRaw    ? "RED FAILURE" :
         orangeRaw ? "ORANGE HOT" :
                     "WAIT"
 
    stateColor =
         greenRaw  ? color.lime :
         purpleRaw ? color.purple :
         redRaw    ? color.red :
         orangeRaw ? color.orange :
                     color.white
 
    table.cell(t, 0, 0, "STATE", text_color=color.white, bgcolor=color.gray)
    table.cell(t, 1, 0, state, text_color=stateColor)
 
    table.cell(t, 0, 1, "ACTION", text_color=color.white, bgcolor=color.gray)
    table.cell(t, 1, 1, scaleAction, text_color=actionColor)
 
    table.cell(t, 0, 2, "P/L", text_color=color.white, bgcolor=color.gray)
    table.cell(t, 1, 2, basisOk ? str.tostring(basisPL, "#.##") + "%" : "N/A",
         text_color=basisOk and basisPL >= 0 ? color.lime : color.red)
 
    table.cell(t, 0, 3, "TREND", text_color=color.white, bgcolor=color.gray)
    table.cell(t, 1, 3, trendText, text_color=trendUp ? color.lime : trendWeak ? color.red : color.yellow)
 
    table.cell(t, 0, 4, "Z", text_color=color.white, bgcolor=color.gray)
    table.cell(t, 1, 4, str.tostring(z, "#.##"), text_color=z < 0 ? color.lime : color.red)
 
    table.cell(t, 0, 5, "ATR DIST", text_color=color.white, bgcolor=color.gray)
    table.cell(t, 1, 5, str.tostring(distATR, "#.##"), text_color=distATR < 0 ? color.lime : color.red)
 
    table.cell(t, 0, 6, "BBWP", text_color=color.white, bgcolor=color.gray)
    table.cell(t, 1, 6, str.tostring(bbwp, "#"), text_color=bbwp >= 80 ? color.red : bbwp <= 25 ? color.lime : color.white)
 
    table.cell(t, 0, 7, "MODE", text_color=color.white, bgcolor=color.gray)
    table.cell(t, 1, 7, basisOk ? "OWNING" : "SCOUTING", text_color=color.white)
 
// ── ALERTS ─────────────────────────────────────
alertcondition(purpleDot, "Purple Watch", "Location Scan: washout watch.")
alertcondition(greenDot, "Green Bottom", "Location Scan: strong bottom/add zone.")
alertcondition(orangeDot, "Orange Hot Warning", "Location Scan: hot zone / stop adding / start trim consideration.")
alertcondition(redDot, "Red Failure After Hot", "Location Scan: hot move failed / heavy trim consideration.")

← 返回列表