本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © OlympusCipher
//@version=6
indicator("Supply & Demand Zones with Accurate Stats", overlay = true, max_boxes_count = 500, max_lines_count = 500, max_labels_count = 500, max_bars_back = 500)
// --- Input Parameters ---
grp_base = "Base Consolidation Rules"
minBase = input.int(1, "Min Base Candles", minval = 1, group = grp_base)
maxBase = input.int(5, "Max Base Candles", minval = 1, group = grp_base)
baseThresh = input.float(0.65, "Max Base Body/Range Ratio", minval = 0.1, maxval = 0.95, group = grp_base)
grp_legout = "Explosive Departure Rules"
legOutThresh = input.float(0.55, "Min Leg-Out Body/Range Ratio", minval = 0.3, maxval = 0.95, group = grp_legout)
legOutMult = input.float(1.4, "Leg-Out Range Multiplier vs Base Avg", minval = 1.0, group = grp_legout)
depCandles = input.int(1, "Sustained Departure Candles", minval = 1, group = grp_legout)
grp_visual = "Visual Settings & Custom Colors"
showLines = input.bool(true, "Show Proximal/Distal Lines", group = grp_visual)
showLabels = input.bool(true, "Show Line Labels", group = grp_visual)
demandFillCol = input.color(color.new(#26a69a, 80), "Demand Zone Box Fill", group = grp_visual)
demandLineCol = input.color(#26a69a, "Demand Line & Border Color", group = grp_visual)
supplyFillCol = input.color(color.new(#ef5350, 80), "Supply Zone Box Fill", group = grp_visual)
supplyLineCol = input.color(#ef5350, "Supply Line & Border Color", group = grp_visual)
testedFillCol = input.color(color.new(color.gray, 90), "Tested Zone Box Fill", group = grp_visual)
testedLineCol = input.color(color.gray, "Tested Line Color", group = grp_visual)
grp_stats = "Performance Dashboard"
showTable = input.bool(true, "Show Accuracy Table", group = grp_stats)
// --- Custom Type for Zone Tracking ---
type Zone
box boxObj
line proxLine
line distLine
label proxLbl
label distLbl
bool isDemand
float proximal
float distal
float target
bool isTested
bool isResolved
var Zone[] activeZones = array.new<Zone>()
// --- Global Lifetime Counters ---
var int totalCreated = 0
var int totalWorked = 0
var int totalFailed = 0
// --- Helper Functions ---
isBaseCandle(int shift) =>
rng = high[shift] - low[shift]
bdy = math.abs(close[shift] - open[shift])
rng > 0 and (bdy / rng) <= baseThresh
isLegOutCandle(int shift) =>
rng = high[shift] - low[shift]
bdy = math.abs(close[shift] - open[shift])
rng > 0 and (bdy / rng) >= legOutThresh
isInsideOverlappingBar(int shift) =>
high[shift] <= high[shift + 1] and low[shift] >= low[shift + 1]
// --- Core Zone Identification Logic ---
legOutShift = depCandles
if bar_index >= (maxBase + legOutShift + 5)
if isLegOutCandle(legOutShift)
bool isBullish = close[legOutShift] > open[legOutShift]
int baseCount = 0
int startBaseShift = legOutShift + 1
for i = 0 to maxBase - 1
if isBaseCandle(startBaseShift + i)
baseCount += 1
else
break
if baseCount < minBase and isBaseCandle(startBaseShift)
baseCount := 1
if baseCount >= minBase
if not isInsideOverlappingBar(startBaseShift)
float baseHigh = high[startBaseShift]
float baseLow = low[startBaseShift]
float sumRange = 0.0
for b = 0 to baseCount - 1
int s = startBaseShift + b
baseHigh := math.max(baseHigh, high[s])
baseLow := math.min(baseLow, low[s])
sumRange += (high[s] - low[s])
float avgBaseRange = sumRange / baseCount
float legOutRange = high[legOutShift] - low[legOutShift]
float zoneHeight = baseHigh - baseLow
if zoneHeight < legOutRange and legOutRange >= (avgBaseRange * legOutMult)
bool validDeparture = true
for dep = 1 to depCandles
int dShift = legOutShift - dep
if dShift >= 0
if isBullish
if close[dShift] <= baseHigh or low[dShift] <= baseHigh
validDeparture := false
else
if close[dShift] >= baseLow or high[dShift] >= baseLow
validDeparture := false
if validDeparture
int startBarIdx = bar_index - (startBaseShift + baseCount - 1)
int endBaseBarIdx = bar_index - startBaseShift
int doubleBoxRight = endBaseBarIdx + (baseCount * 2)
if isBullish and close[legOutShift] > baseHigh
float prox = baseHigh
float dist = baseLow
float target = prox + (prox - dist) // 1RR Target
box zb = box.new(left = startBarIdx, top = prox, right = doubleBoxRight, bottom = dist,
bgcolor = demandFillCol, border_color = demandLineCol, xloc = xloc.bar_index)
line pLine = na
line dLine = na
label pLbl = na
label dLbl = na
if showLines
pLine := line.new(x1 = startBarIdx, y1 = prox, x2 = bar_index, y2 = prox, xloc = xloc.bar_index, color = demandLineCol, width = 2)
dLine := line.new(x1 = startBarIdx, y1 = dist, x2 = bar_index, y2 = dist, xloc = xloc.bar_index, color = demandLineCol, style = line.style_dashed)
if showLabels
pLbl := label.new(bar_index, prox, "Demand Proximal", xloc = xloc.bar_index, color = color.new(color.black, 100), textcolor = demandLineCol, style = label.style_label_left, size = size.small)
dLbl := label.new(bar_index, dist, "Demand Distal", xloc = xloc.bar_index, color = color.new(color.black, 100), textcolor = demandLineCol, style = label.style_label_left, size = size.small)
array.push(activeZones, Zone.new(zb, pLine, dLine, pLbl, dLbl, true, prox, dist, target, false, false))
totalCreated += 1
else if not isBullish and close[legOutShift] < baseLow
float prox = baseLow
float dist = baseHigh
float target = prox - (dist - prox) // 1RR Target
box zb = box.new(left = startBarIdx, top = dist, right = doubleBoxRight, bottom = prox,
bgcolor = supplyFillCol, border_color = supplyLineCol, xloc = xloc.bar_index)
line pLine = na
line dLine = na
label pLbl = na
label dLbl = na
if showLines
pLine := line.new(x1 = startBarIdx, y1 = prox, x2 = bar_index, y2 = prox, xloc = xloc.bar_index, color = supplyLineCol, width = 2)
dLine := line.new(x1 = startBarIdx, y1 = dist, x2 = bar_index, y2 = dist, xloc = xloc.bar_index, color = supplyLineCol, style = line.style_dashed)
if showLabels
pLbl := label.new(bar_index, prox, "Supply Proximal", xloc = xloc.bar_index, color = color.new(color.black, 100), textcolor = supplyLineCol, style = label.style_label_left, size = size.small)
dLbl := label.new(bar_index, dist, "Supply Distal", xloc = xloc.bar_index, color = color.new(color.black, 100), textcolor = supplyLineCol, style = label.style_label_left, size = size.small)
array.push(activeZones, Zone.new(zb, pLine, dLine, pLbl, dLbl, false, prox, dist, target, false, false))
totalCreated += 1
// --- Precise Outcome Resolution and Tracking ---
if array.size(activeZones) > 0
for i = array.size(activeZones) - 1 to 0
Zone z = array.get(activeZones, i)
// 1. Extend Lines for Untested Zones
if not z.isTested
if not na(z.proxLine)
line.set_x2(z.proxLine, bar_index)
if not na(z.distLine)
line.set_x2(z.distLine, bar_index)
if not na(z.proxLbl)
label.set_x(z.proxLbl, bar_index)
if not na(z.distLbl)
label.set_x(z.distLbl, bar_index)
// Check for initial mitigation touch
bool touched = z.isDemand ? (low <= z.proximal) : (high >= z.proximal)
if touched
z.isTested := true
box.set_bgcolor(z.boxObj, testedFillCol)
box.set_border_color(z.boxObj, testedLineCol)
if not na(z.proxLine)
line.set_color(z.proxLine, testedLineCol)
if not na(z.distLine)
line.set_color(z.distLine, testedLineCol)
// 2. Failure Check (Triggers immediately if price breaks distal, with or without prior touch)
bool isFailed = z.isDemand ? (low < z.distal) : (high > z.distal)
// 3. Success Check (Requires test first, then reaching 1RR target)
bool isSuccess = z.isDemand ? (high >= z.target) : (low <= z.target)
if isFailed
totalFailed += 1
array.remove(activeZones, i)
else if z.isTested and isSuccess
totalWorked += 1
array.remove(activeZones, i)
// --- Render Updated Table ---
var table statsTable = table.new(position.top_right, 2, 6, bgcolor = color.new(color.black, 20), border_width = 1, border_color = color.gray)
if showTable and barstate.islast
int totalResolved = totalWorked + totalFailed
int activePending = totalCreated - totalResolved
float winRate = totalResolved > 0 ? (totalWorked * 100.0 / totalResolved) : 0.0
table.cell(statsTable, 0, 0, "SD Metric", bgcolor = color.new(#1e88e5, 20), text_color = color.white, text_size = size.small)
table.cell(statsTable, 1, 0, "Value", bgcolor = color.new(#1e88e5, 20), text_color = color.white, text_size = size.small)
table.cell(statsTable, 0, 1, "Total Zones", text_color = color.white, text_size = size.small)
table.cell(statsTable, 1, 1, str.tostring(totalCreated), text_color = color.white, text_size = size.small)
table.cell(statsTable, 0, 2, "Active / Pending", text_color = color.white, text_size = size.small)
table.cell(statsTable, 1, 2, str.tostring(activePending), text_color = color.yellow, text_size = size.small)
table.cell(statsTable, 0, 3, "Worked (1RR)", text_color = color.white, text_size = size.small)
table.cell(statsTable, 1, 3, str.tostring(totalWorked), text_color = #26a69a, text_size = size.small)
table.cell(statsTable, 0, 4, "Failed", text_color = color.white, text_size = size.small)
table.cell(statsTable, 1, 4, str.tostring(totalFailed), text_color = #ef5350, text_size = size.small)
table.cell(statsTable, 0, 5, "Accuracy %", text_color = color.white, text_size = size.small)
table.cell(statsTable, 1, 5, str.tostring(winRate, "#.##") + "%", text_color = winRate >= 50 ? #26a69a : #ef5350, text_size = size.small)