本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator(title="Josh S&D Zones", overlay=true, max_boxes_count=500)
// ── Inputs ──────────────────────────────────────────────
showImbalance = input.bool(false, "Show Imbalance Candles")
imbColor = input.color(color.new(#000000, 0), "Imbalance Body Color")
minSize = input.float(0.0, "Minimum Imbalance Size", minval=0.0, step=0.01)
maxConsec = input.int(1, "Max Consecutive Imbalances", minval=1, maxval=20)
showRects = input.bool(true, "Show Order Blocks", group="Order Blocks")
obFill = input.color(color.new(#153899, 85), "Fill", group="Order Blocks")
obBorder = input.color(color.new(#153899, 75), "Border", group="Order Blocks")
obText = input.color(color.new(#153899, 0), "Text", group="Order Blocks")
obWidth = input.int(1, "Border Width", minval=1, maxval=5, group="Order Blocks")
obStyle = input.string("Solid", "Border Style", options=["Solid", "Dashed", "Dotted"], group="Order Blocks")
obTextSize = input.string("normal", "Text Size", options=["tiny", "small", "normal", "large", "huge"], group="Order Blocks")
// ── Chart-TF imbalances ─────────────────────────────────
prevOpen = open[1]
prevClose = close[1]
prevBodyTop = math.max(prevOpen, prevClose)
prevBodyBot = math.min(prevOpen, prevClose)
isDown = prevClose < prevOpen
leftHigh = high[2]
leftLow = low[2]
rightHigh = high
rightLow = low
zoneBot = isDown ? rightHigh : leftHigh
zoneTop = isDown ? leftLow : rightLow
hasGapZone = zoneTop > zoneBot
segTop = math.min(prevBodyTop, zoneTop)
segBot = math.max(prevBodyBot, zoneBot)
hasSegment = hasGapZone and segTop > segBot
segmentSize = hasSegment ? segTop - segBot : 0.0
leftCovers = leftHigh >= segTop and leftLow <= segBot
rightCovers = rightHigh >= segTop and rightLow <= segBot
rawImbalance = hasSegment and not leftCovers and not rightCovers and segmentSize >= minSize
var int consecCount = 0
if rawImbalance
consecCount += 1
else
consecCount := 0
showThis = rawImbalance and consecCount <= maxConsec
barcolor(showImbalance and showThis ? imbColor : na, offset=-1)
// ── Order Blocks (current timeframe only) ───────────────
var box[] activeBoxes = array.new<box>()
// Timeframe abbreviation
tf = timeframe.period
tfText = tf == "1D" or tf == "D" ? "1D" : tf == "1W" or tf == "W" ? "1W" : tf == "1M" ? "1M" : tf == "60" ? "1h" : tf == "120" ? "2h" : tf == "180" ? "3h" : tf == "240" ? "4h" : (not na(str.tonumber(tf)) and str.tonumber(tf) < 60 ? tf + "m" : tf)
// Confirmed imbalance
pO = open[2]
pC = close[2]
pBT = math.max(pO, pC)
pBB = math.min(pO, pC)
pDown = pC < pO
zBot = pDown ? high[1] : high[3]
zTop = pDown ? low[3] : low[1]
gap = zTop > zBot
sTop = math.min(pBT, zTop)
sBot = math.max(pBB, zBot)
hasS = gap and sTop > sBot
sSize = hasS ? sTop - sBot : 0
lCov = high[3] >= sTop and low[3] <= sBot
rCov = high[1] >= sTop and low[1] <= sBot
imb = hasS and not lCov and not rCov and sSize >= minSize
var int consec = 0
if imb
consec += 1
else
consec := 0
if showRects and imb and consec <= maxConsec
preO = open[3]
preC = close[3]
preH = high[3]
preL = low[3]
preBodyTop = math.max(preO, preC)
preBodyBot = math.min(preO, preC)
topPrice = pDown ? preH : preBodyTop
botPrice = pDown ? preBodyBot : preL
borderStyle = obStyle == "Dashed" ? line.style_dashed : obStyle == "Dotted" ? line.style_dotted : line.style_solid
b = box.new(bar_index - 3, topPrice, bar_index + 1, botPrice,
border_color=obBorder,
border_width=obWidth,
border_style=borderStyle,
bgcolor=obFill,
extend=extend.right,
text=tfText,
text_color=obText,
text_size=obTextSize)
array.push(activeBoxes, b)
// ── Stop boxes when tagged ──────────────────────────────
if array.size(activeBoxes) > 0
for i = array.size(activeBoxes) - 1 to 0
bx = array.get(activeBoxes, i)
float bTop = box.get_top(bx)
float bBot = box.get_bottom(bx)
if high >= bBot and low <= bTop
box.set_right(bx, bar_index)
box.set_extend(bx, extend.none)
array.remove(activeBoxes, i)