本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Cedric | Support & Resistance Zones V1.5", overlay = true, max_boxes_count = 50, max_bars_back = 2500)
// DETECTION
lookbackBars = input.int(1000, "Historical search (bars)", minval = 80, maxval = 2000, group = "Detection", tooltip = "Search history for candidate zones. Old levels are only promoted if their most recent independent interaction is still relevant.")
pivotBars = input.int(5, "Swing confirmation bars", minval = 2, maxval = 25, group = "Detection")
mergeATR = input.float(0.75, "Merge distance (ATR)", minval = 0.15, maxval = 2.5, step = 0.05, group = "Detection")
reactionGap = input.int(8, "Minimum bars between reactions", minval = 1, maxval = 100, group = "Detection")
minTouches = input.int(2, "Minimum independent reactions", minval = 1, maxval = 8, group = "Detection")
maxDistancePct = input.float(300.0, "Maximum price distance (%)", minval = 5, maxval = 500, step = 5, group = "Detection", tooltip = "Safety cap only. Ranking primarily depends on how recently price interacted with a zone, not its distance from the current price.")
// DISPLAY
maxZones = input.int(6, "Maximum visible zones", minval = 1, maxval = 20, group = "Display")
zoneWidth = input.float(1.0, "Zone width multiplier", minval = 0.3, maxval = 3.0, step = 0.1, group = "Display")
widthATR = input.float(0.30, "Base half-width (ATR)", minval = 0.05, maxval = 1.5, step = 0.05, group = "Display")
zoneColor = input.color(color.rgb(101, 139, 247), "Zone color", group = "Display")
zoneTransparency = input.int(72, "Fill transparency", minval = 0, maxval = 95, group = "Display")
showPrices = input.bool(true, "Show price labels", group = "Display")
priceLabelMode = input.string("Adaptive", "Price label rounding", options = ["Adaptive", "Exact"], group = "Display", tooltip = "Adaptive rounds only the printed reference price. Rectangle placement and calculations remain unchanged.")
roundingPct = input.float(2.5, "Adaptive rounding step (% of level)", minval = 0.25, maxval = 5.0, step = 0.25, group = "Display", tooltip = "Chooses a convenient step near this percentage of the level's value. The zone never moves.")
// Choose a human-friendly rounding step. 1 / 2.5 / 5 / 10 times a power of ten.
// All rounding is LABEL-ONLY, never changes the detection center or box boundaries.
niceStep(float level) =>
float target = math.max(math.abs(level) * roundingPct / 100.0, syminfo.mintick)
float magnitude = math.pow(10.0, math.floor(math.log10(target)))
float best = magnitude
float bestError = 1e20
for multiplier in array.from(1.0, 2.5, 5.0, 10.0)
float candidate = multiplier * magnitude
float err = math.abs(math.log(target / candidate))
if err < bestError
best := candidate
bestError := err
math.max(best, syminfo.mintick)
formatLevel(float level) =>
float step = niceStep(level)
float rounded = math.round(level / step) * step
// Snap to the instrument's minimum tick for valid price labels.
rounded := math.round(rounded / syminfo.mintick) * syminfo.mintick
str.tostring(rounded, "#.########")
atr = ta.atr(14)
ph = ta.pivothigh(high, pivotBars, pivotBars)
pl = ta.pivotlow(low, pivotBars, pivotBars)
// last_bar_index anchors the scan to the latest chart candle, not the first
// historical candle TradingView happened to load when zooming.
int startIndex = math.max(0, last_bar_index - lookbackBars + 1)
var array<float> centers = array.new_float()
var array<float> avgATRs = array.new_float()
var array<int> hits = array.new_int()
var array<int> lastBars = array.new_int()
var array<int> firstBars = array.new_int()
var array<box> drawings = array.new_box()
addPivot(float price, float pivotATR, int pivotIndex) =>
if not na(price) and not na(pivotATR) and pivotATR > 0 and pivotIndex >= startIndex
int found = -1
float bestDistance = 1e20
if array.size(centers) > 0
for j = 0 to array.size(centers) - 1
float previous = array.get(centers, j)
float tolerance = mergeATR * math.max(pivotATR, array.get(avgATRs, j))
float distance = math.abs(price - previous)
if distance <= tolerance and distance < bestDistance
found := j
bestDistance := distance
if found == -1
array.push(centers, price)
array.push(avgATRs, pivotATR)
array.push(hits, 1)
array.push(lastBars, pivotIndex)
array.push(firstBars, pivotIndex)
else
int previousReaction = array.get(lastBars, found)
if pivotIndex - previousReaction >= reactionGap
int n = array.get(hits, found)
array.set(centers, found, (array.get(centers, found) * n + price) / (n + 1))
array.set(avgATRs, found, (array.get(avgATRs, found) * n + pivotATR) / (n + 1))
array.set(hits, found, n + 1)
array.set(lastBars, found, pivotIndex)
0
if barstate.isconfirmed and bar_index >= startIndex
if not na(ph)
addPivot(ph, atr[pivotBars], bar_index - pivotBars)
if not na(pl)
addPivot(pl, atr[pivotBars], bar_index - pivotBars)
// Draw a current snapshot only. A confirmed pivot takes pivotBars candles to appear.
if barstate.islast
if array.size(drawings) > 0
for b in drawings
box.delete(b)
array.clear(drawings)
array<bool> chosen = array.new_bool(array.size(centers), false)
array<float> shownCenters = array.new_float()
array<float> shownHalfWidths = array.new_float()
for rank = 0 to maxZones - 1
int winner = -1
float winningScore = -1e20
if array.size(centers) > 0
for j = 0 to array.size(centers) - 1
int n = array.get(hits, j)
float c = array.get(centers, j)
float pctDistance = math.abs(c - close) / math.max(math.abs(close), syminfo.mintick) * 100.0
if not array.get(chosen, j) and n >= minTouches and pctDistance <= maxDistancePct
float halfWidth = math.max(array.get(avgATRs, j) * widthATR, c * 0.002) * zoneWidth
bool overlaps = false
if array.size(shownCenters) > 0
for k = 0 to array.size(shownCenters) - 1
if math.abs(c - array.get(shownCenters, k)) < (halfWidth + array.get(shownHalfWidths, k)) * 1.15
overlaps := true
if not overlaps
float age = float(last_bar_index - array.get(lastBars, j))
float distanceATR = math.abs(c - close) / math.max(atr, syminfo.mintick)
// V1.5: By far the strongest factor is WHEN price last interacted
// with this zone, not when the zone was first formed.
// Independent touches add a smaller, capped quality bonus.
// Distance is a very small tie-breaker, so an old nearby
// level cannot easily displace an important recent one.
float recentScore = 35.0 * math.exp(-age / 110.0)
float reactionScore = math.min(float(n), 5.0) * 2.0
float proximityScore = 2.0 / (1.0 + distanceATR / 15.0)
float score = recentScore + reactionScore + proximityScore
if score > winningScore
winner := j
winningScore := score
if winner != -1
array.set(chosen, winner, true)
float c = array.get(centers, winner)
float h = math.max(array.get(avgATRs, winner) * widthATR, c * 0.002) * zoneWidth
array.push(shownCenters, c)
array.push(shownHalfWidths, h)
string labelText = showPrices ? (priceLabelMode == "Adaptive" ? formatLevel(c) : str.tostring(c, format.mintick)) : ""
// Anchor each zone at its first qualifying swing, not the lookback boundary.
int firstSwingIndex = array.get(firstBars, winner)
box b = box.new(left = firstSwingIndex, top = c + h, right = last_bar_index + 8, bottom = c - h, xloc = xloc.bar_index, bgcolor = color.new(zoneColor, zoneTransparency), border_color = color.new(zoneColor, 100), text = labelText, text_color = color.black, text_size = size.small, text_halign = text.align_right, text_valign = text.align_center)
array.push(drawings, b)