本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("DIA / US30 Cash-Close Ladder Jessnat", overlay=true, max_lines_count=500, max_labels_count=500)
//--------------------------------------------------
// Inputs
//--------------------------------------------------
levelsEachSide = input.int(15, "Levels Above/Below", minval=1, maxval=40)
stepSize = input.float(0.5, "DIA Step Size", minval=0.01, step=0.01)
lineLenBars = input.int(80, "Line Length Left (bars)", minval=10, maxval=500)
futureBars = input.int(50, "Push Labels Right (bars)", minval=1, maxval=200)
extendLeft = input.bool(false, "Extend Lines Left Infinitely")
decimalsETF = input.int(2, "DIA Decimals", minval=0, maxval=4)
decimalsFut = input.int(0, "US30 Decimals", minval=0, maxval=4)
showCenter = input.bool(true, "Show Center Label")
// --- Center Line ---
centerLineColor = input.color(#808080, "Center Line Color")
centerLineWidth = input.int(1, "Center Line Width", minval=1, maxval=4)
centerOpacity = input.int(30, "Center Line Opacity", minval=0, maxval=100)
centerTextColor = input.color(#808080, "Center Text Color")
// --- Near Lines ---
nearZone = input.int(3, "Near Zone (levels from center)", minval=1, maxval=20)
nearLineColor = input.color(#808080, "Near Line Color")
nearLineWidth = input.int(1, "Near Line Width", minval=1, maxval=4)
nearOpacity = input.int(60, "Near Line Opacity", minval=0, maxval=100)
nearTextColor = input.color(#808080, "Near Text Color")
nearLineStyle = input.string("Dotted", "Near Line Style", options=["Solid","Dashed","Dotted"])
// --- Far Lines ---
farLineColor = input.color(#606060, "Far Line Color")
farLineWidth = input.int(1, "Far Line Width", minval=1, maxval=4)
farOpacity = input.int(60, "Far Line Opacity", minval=0, maxval=100)
farTextColor = input.color(#606060, "Far Text Color")
farLineStyle = input.string("Dotted", "Far Line Style", options=["Solid","Dashed","Dotted"])
// --- Labels ---
textSizeInput = input.string("Tiny", "Text Size", options=["Tiny","Small","Normal","Large"])
// --- Ratio Box ---
showRatioBox = input.bool(true, "Show Ratio Box")
showLiveRatio = input.bool(true, "Show Live Ratio")
showLockedRatio = input.bool(true, "Show Locked Ratio")
showPrevCloses = input.bool(true, "Show Previous Closes")
ratioPosition = input.string("Top Right", "Ratio Box Position", options=["Top Right","Top Left","Bottom Right","Bottom Left"])
// --- Anchors ---
// "Use Chart Symbol" ON = the ratio is built from whatever US30 you have on the chart
// (YM1!, MYM1!, a US30 CFD, etc.) so the levels match that exact feed.
// OFF = uses the US30 Anchor below.
useChartAnchor = input.bool(true, "Use Chart Symbol as US30 Anchor")
usAnchor = input.symbol("CBOT_MINI:YM1!", "US30 Anchor")
diaAnchor = input.symbol("AMEX:DIA", "DIA Anchor")
// --- Manual Close Override ---
// Leave any value at 0 to keep auto-fetching that symbol.
var string GM = "Manual Close Override"
useManualClose = input.bool(false, "Use Manual Close Prices", group=GM)
manualDIA = input.float(0.0, "Manual DIA Close", group=GM, minval=0, step=0.01)
manualUS30 = input.float(0.0, "Manual US30 Close", group=GM, minval=0, step=1.0)
//--------------------------------------------------
// Helpers
//--------------------------------------------------
getPrevCashClose(string symbol) =>
request.security(
symbol, "1",
ta.valuewhen(not na(time("1", "1559-1600", "America/New_York")), close, 0),
gaps=barmerge.gaps_off,
lookahead=barmerge.lookahead_off)
resolveStyle(string s) =>
s == "Solid" ? line.style_solid : s == "Dashed" ? line.style_dashed : line.style_dotted
resolveSize(string s) =>
s == "Large" ? size.large : s == "Normal" ? size.normal : s == "Small" ? size.small : size.tiny
resolvePosition(string s) =>
s == "Top Left" ? position.top_left : s == "Bottom Right" ? position.bottom_right : s == "Bottom Left" ? position.bottom_left : position.top_right
fmt(int d) => d == 0 ? "0" : "0." + str.repeat("0", d)
formatETF(float v) => str.tostring(v, fmt(decimalsETF))
formatFut(float v) => str.tostring(v, fmt(decimalsFut))
formatRatio(float v) => str.tostring(v, "0.00000000")
textSize = resolveSize(textSizeInput)
nearStyle = resolveStyle(nearLineStyle)
farStyle = resolveStyle(farLineStyle)
tablePos = resolvePosition(ratioPosition)
string etfName = "DIA"
string futName = "US30"
//--------------------------------------------------
// Pull previous cash closes (auto)
//--------------------------------------------------
string futSymbol = useChartAnchor ? syminfo.tickerid : usAnchor
float futPrevClose = getPrevCashClose(futSymbol)
float etfPrevClose = getPrevCashClose(diaAnchor)
//--------------------------------------------------
// Apply manual overrides if enabled
//--------------------------------------------------
float effETF = (useManualClose and manualDIA > 0) ? manualDIA : etfPrevClose
float effFut = (useManualClose and manualUS30 > 0) ? manualUS30 : futPrevClose
//--------------------------------------------------
// Locked ratio (built from effective closes)
//--------------------------------------------------
float ratio = na
if not na(effFut) and not na(effETF) and effETF != 0
ratio := effFut / effETF
//--------------------------------------------------
// Live ratio
//--------------------------------------------------
float liveETF = request.security(diaAnchor, timeframe.period, close, gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_off)
float liveRatio = na
if not na(close) and not na(liveETF) and liveETF != 0
liveRatio := close / liveETF
//--------------------------------------------------
// Center
//--------------------------------------------------
float impliedETF = na
if not na(ratio)
impliedETF := close / ratio
float baseETF = na
if not na(impliedETF)
baseETF := math.round(impliedETF / stepSize) * stepSize
//--------------------------------------------------
// Storage
//--------------------------------------------------
var line[] ladderLines = array.new_line()
var label[] ladderLabels = array.new_label()
clearDrawings() =>
if array.size(ladderLines) > 0
for i = 0 to array.size(ladderLines) - 1
line.delete(array.get(ladderLines, i))
array.clear(ladderLines)
if array.size(ladderLabels) > 0
for i = 0 to array.size(ladderLabels) - 1
label.delete(array.get(ladderLabels, i))
array.clear(ladderLabels)
//--------------------------------------------------
// Draw ladder
//--------------------------------------------------
if barstate.isrealtime or barstate.islast
clearDrawings()
if not na(ratio) and not na(baseETF)
int startX = bar_index - lineLenBars
int endX = bar_index + futureBars
int labelX = bar_index + futureBars
extend_dir = extendLeft ? extend.left : extend.none
for i = -levelsEachSide to levelsEachSide
float etfLevel = baseETF + i * stepSize
float futLevel = etfLevel * ratio
bool isCenter = i == 0
bool isNear = math.abs(i) <= nearZone and not isCenter
color lineCol = isCenter ? color.new(centerLineColor, centerOpacity) : isNear ? color.new(nearLineColor, nearOpacity) : color.new(farLineColor, farOpacity)
color txtCol = isCenter ? centerTextColor : isNear ? nearTextColor : farTextColor
int lw = isCenter ? centerLineWidth : isNear ? nearLineWidth : farLineWidth
line ln = line.new(
x1=startX, y1=futLevel,
x2=endX, y2=futLevel,
xloc=xloc.bar_index,
extend=extend_dir,
color=lineCol,
style=isCenter ? line.style_solid : isNear ? nearStyle : farStyle,
width=lw)
array.push(ladderLines, ln)
if showCenter or not isCenter
label lb = label.new(
x=labelX, y=futLevel,
text=etfName + " " + formatETF(etfLevel),
xloc=xloc.bar_index,
yloc=yloc.price,
style=label.style_label_left,
color=color.new(color.black, 100),
textcolor=txtCol,
size=textSize)
array.push(ladderLabels, lb)
//--------------------------------------------------
// Ratio table
//--------------------------------------------------
var table ratioTable = table.new(tablePos, 1, 6, border_width=1)
if barstate.isrealtime or barstate.islast
table.clear(ratioTable, 0, 0, 0, 5)
if showRatioBox
int row = 0
string headerText = etfName + " -> " + futName + " Ratio" + (useManualClose ? " [MANUAL]" : "")
table.cell(ratioTable, 0, row, headerText, text_color=color.white, bgcolor=color.new(color.black, 0), text_size=size.small)
row += 1
if showLockedRatio and not na(ratio)
table.cell(ratioTable, 0, row, "Locked: " + formatRatio(ratio), text_color=color.white, bgcolor=color.new(color.black, 15), text_size=size.small)
row += 1
if showLiveRatio and not na(liveRatio)
table.cell(ratioTable, 0, row, "Live: " + formatRatio(liveRatio), text_color=color.white, bgcolor=color.new(color.black, 15), text_size=size.small)
row += 1
if showPrevCloses and not na(effFut)
string futLabel = (useManualClose and manualUS30 > 0) ? "Manual " + futName : "Prev " + futName
table.cell(ratioTable, 0, row, futLabel + ": " + formatFut(effFut), text_color=color.silver, bgcolor=color.new(color.black, 15), text_size=size.small)
row += 1
if showPrevCloses and not na(effETF)
string etfLabel = (useManualClose and manualDIA > 0) ? "Manual " + etfName : "Prev " + etfName
table.cell(ratioTable, 0, row, etfLabel + ": " + formatETF(effETF), text_color=color.silver, bgcolor=color.new(color.black, 15), text_size=size.small)