本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Overlay (Bottom of Screen)", shorttitle = "Overlay", overlay = true, scale = scale.none, max_labels_count = 1, max_lines_count = 1)
// ───────── Inputs ─────────
sym = input.symbol("CME_MINI:ES1!", "Symbol", group = "Inputs")
styleOpt = input.string("Line", "Style", options = ["Line", "Candles", "Hollow candles", "Bars", "High/Low lines"], group = "Style")
srcOpt = input.string("Close", "Price source (Line only)", options = ["Open", "High", "Low", "Close", "(H + L)/2", "(H + L + C)/3", "(O + H + L + C)/4"], group = "Style")
lineCol = input.color(#2962FF, "Line", inline = "ln", group = "Style")
lineW = input.int(2, "", minval = 1, maxval = 4, inline = "ln", group = "Style")
upCol = input.color(#089981, "Up", inline = "cd", group = "Style")
downCol = input.color(#F23645, "Down", inline = "cd", group = "Style")
showPLine = input.bool(false, "Price line", group = "Style")
showLabel = input.bool(true, "Price label", group = "Style")
bandPct = input.float(20.0, "Band height (% of screen)", minval = 5, maxval = 100, group = "Placement")
padPct = input.float(0.0, "Bottom padding (% of screen)", minval = 0, maxval = 50, group = "Placement")
stabLen = input.int(1000, "Zoom stability (bars)", minval = 50, maxval = 5000, group = "Placement", tooltip = "Keeps ES near the bottom while you zoom out, before the chart finishes recalculating. Raise it if it still jumps when zooming out a long way.")
// ───────── ES data ─────────
[eO, eH, eL, eC] = request.security(sym, timeframe.period, [open, high, low, close])
esSrc = switch srcOpt
"Open" => eO
"High" => eH
"Low" => eL
"(H + L)/2" => (eH + eL) / 2
"(H + L + C)/3" => (eH + eL + eC) / 3
"(O + H + L + C)/4" => (eO + eH + eL + eC) / 4
=> eC
isLine = styleOpt == "Line"
isCandle = styleOpt == "Candles" or styleOpt == "Hollow candles"
isHollow = styleOpt == "Hollow candles"
isBars = styleOpt == "Bars"
isHL = styleOpt == "High/Low lines"
rangeHi = isLine ? esSrc : eH
rangeLo = isLine ? esSrc : eL
lastVal = isLine ? esSrc : eC
// ───────── ES range on screen ─────────
inView = time >= chart.left_visible_bar_time and time <= chart.right_visible_bar_time
var float esHi = na
var float esLo = na
if inView and not na(rangeHi) and not na(rangeLo)
esHi := na(esHi) ? rangeHi : math.max(esHi, rangeHi)
esLo := na(esLo) ? rangeLo : math.min(esLo, rangeLo)
// ───────── Exact anchors (on the last visible bar) ─────────
isAnchorBar = inView and (time == chart.right_visible_bar_time or barstate.islast)
esRange = esHi - esLo
span = esRange > 0 ? esRange / (bandPct / 100) : esLo * 0.001
floorV = isAnchorBar ? esLo - span * padPct / 100 : na
ceilV = isAnchorBar ? floorV + span : na
// ───────── Backup anchors (on bars off-screen, used while zooming out) ─────────
rollHi = ta.highest(rangeHi, stabLen)
rollLo = ta.lowest(rangeLo, stabLen)
rollRange = rollHi - rollLo
fbCeil = not inView and rollRange > 0 ? rollLo + rollRange / (bandPct / 100) : na
topV = isAnchorBar ? ceilV : fbCeil
plot(topV, "Top anchor", color = color.new(color.black, 100), style = plot.style_circles, editable = false, display = display.pane)
plot(floorV, "Bottom anchor", color = color.new(color.black, 100), style = plot.style_circles, editable = false, display = display.pane)
// ───────── Colours ─────────
isUp = eC >= eO
barCol = isUp ? upCol : downCol
clear = color.new(color.black, 100)
// ───────── Line ─────────
plot(isLine ? esSrc : na, "ES Line", color = lineCol, linewidth = lineW)
// ───────── Candles / Hollow candles ─────────
plotcandle(isCandle ? eO : na, isCandle ? eH : na, isCandle ? eL : na, isCandle ? eC : na, "ES Candles", color = isHollow and isUp ? clear : barCol, wickcolor = barCol, bordercolor = barCol)
// ───────── Bars (OHLC) ─────────
plotbar(isBars ? eO : na, isBars ? eH : na, isBars ? eL : na, isBars ? eC : na, "ES Bars", color = barCol)
// ───────── High / Low lines ─────────
plot(isHL ? eH : na, "ES High", color = lineCol, linewidth = lineW)
plot(isHL ? eL : na, "ES Low", color = color.new(lineCol, 40), linewidth = lineW)
// ───────── Price label + price line ─────────
var label lbl = na
var line pLine = na
if barstate.islast
lbl.delete()
pLine.delete()
tagCol = isLine or isHL ? lineCol : barCol
if showLabel
lbl := label.new(bar_index + 1, lastVal, str.tostring(lastVal, "#,##0.00"), style = label.style_label_left, color = tagCol, textcolor = color.white, size = size.small)
if showPLine
pLine := line.new(bar_index - 1, lastVal, bar_index, lastVal, extend = extend.both, color = tagCol, style = line.style_dotted, width = 1)