本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Cross-Instrument Cash-Close Ladder Jessnat", overlay=true, max_lines_count=500, max_labels_count=500)
//--------------------------------------------------
// Mode
//--------------------------------------------------
// "Auto" = futures chart (NQ/MNQ/ES/MES) -> shows ETF/NAS100 levels; anything else -> shows futures levels
// "Index Chart" = you chart NAS100/US100/etc and want NQ1! (or ES1!) levels at the right y-position
// "Futures Chart" = you chart NQ1!/ES1! and want SPY/QQQ/NAS100 levels at the right y-position
mode = input.string("Auto", "Mode",
options=["Auto", "Index Chart -> Show Futures Levels", "Futures Chart -> Show ETF Levels"])
//--------------------------------------------------
// Shared Inputs
//--------------------------------------------------
levelsEachSide = input.int(15, "Levels Above/Below", minval=1, maxval=40)
stepSize = input.float(25.0, "Level Step Size (in the LABELED instrument's units)", 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")
showCenter = input.bool(true, "Show Center Label")
decimalsETF = input.int(2, "SPY/QQQ Label Decimals", minval=0, maxval=4)
decimalsIndex = input.int(1, "NAS100 Label Decimals", minval=0, maxval=4)
decimalsFutures = input.int(2, "Futures (NQ/ES) Label Decimals", minval=0, maxval=4)
// --- 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"])
//--------------------------------------------------
// Index Chart mode inputs (chart = NAS100/US100/etc)
//--------------------------------------------------
futuresAnchor = input.symbol("CME_MINI:NQ1!", "Futures Anchor (levels to show on this index chart)")
futuresLabelName = input.string("NQ", "Futures Label Name (e.g. NQ or ES)")
//--------------------------------------------------
// Futures Chart mode inputs (chart = NQ1!/ES1! itself)
//--------------------------------------------------
esAnchor = input.symbol("CME_MINI:ES1!", "ES Anchor")
spyAnchor = input.symbol("AMEX:SPY", "SPY Anchor")
nqAnchor = input.symbol("CME_MINI:NQ1!", "NQ Anchor")
nqTarget = input.string("NAS100", "NQ Chart Target", options=["NAS100", "QQQ"])
qqqAnchor = input.symbol("NASDAQ:QQQ", "QQQ Anchor")
nas100Anchor = input.symbol("OANDA:NAS100USD", "NAS100 Anchor (set to your broker's symbol)")
//--------------------------------------------------
// 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
formatN(float v, int d) => str.tostring(v, "0." + str.repeat("0", d))
formatRatio(float v) => str.tostring(v, "0.00000000")
textSize = resolveSize(textSizeInput)
nearStyle = resolveStyle(nearLineStyle)
farStyle = resolveStyle(farLineStyle)
tablePos = resolvePosition(ratioPosition)
//--------------------------------------------------
// Detect chart family
//--------------------------------------------------
string root = str.upper(syminfo.root)
bool chartIsES = root == "ES" or root == "MES"
bool chartIsNQ = root == "NQ" or root == "MNQ"
bool isIndexMode = mode == "Index Chart -> Show Futures Levels" or (mode == "Auto" and not chartIsES and not chartIsNQ)
bool isES = not isIndexMode and chartIsES
bool isNQ = not isIndexMode and chartIsNQ
bool useNas100 = isNQ and nqTarget == "NAS100"
//--------------------------------------------------
// Pull previous cash closes + build ratio, names, decimals
//--------------------------------------------------
etfTargetSymbol = useNas100 ? nas100Anchor : qqqAnchor
float otherPrevClose = na
float selfPrevClose = na
string otherName = ""
int otherDecimals = 2
float ratio = na // ratio = other / self (other = labeled instrument, self = chart)
bool active = false
if isIndexMode
otherPrevClose := getPrevCashClose(futuresAnchor)
selfPrevClose := getPrevCashClose(syminfo.tickerid)
otherName := futuresLabelName
otherDecimals := decimalsFutures
active := true
else
otherPrevClose := isES ? getPrevCashClose(spyAnchor) : isNQ ? getPrevCashClose(etfTargetSymbol) : na
selfPrevClose := isES ? getPrevCashClose(esAnchor) : isNQ ? getPrevCashClose(nqAnchor) : na
otherName := isES ? "SPY" : isNQ ? (useNas100 ? "NAS100" : "QQQ") : ""
otherDecimals := useNas100 ? decimalsIndex : decimalsETF
active := isES or isNQ
if not na(otherPrevClose) and not na(selfPrevClose) and selfPrevClose != 0
ratio := otherPrevClose / selfPrevClose
string selfName = isIndexMode ? syminfo.ticker : (isES ? "ES" : isNQ ? "NQ" : "")
//--------------------------------------------------
// Live ratio (other / self, live)
//--------------------------------------------------
float liveOther = na
if isIndexMode
liveOther := request.security(futuresAnchor, timeframe.period, close, gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_off)
else if isES
liveOther := request.security(spyAnchor, timeframe.period, close, gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_off)
else if isNQ
liveOther := request.security(etfTargetSymbol, timeframe.period, close, gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_off)
float liveRatio = na
if not na(close) and not na(liveOther) and close != 0
liveRatio := liveOther / close
//--------------------------------------------------
// Base ladder level
//--------------------------------------------------
float baseLevel = na
if isIndexMode
// Chart is the index -> ladder is built in the chart's own units
if not na(close)
baseLevel := math.round(close / stepSize) * stepSize
else
// Chart is futures -> ladder is built in the OTHER instrument's units (NAS100/QQQ/SPY)
// other = self * ratio
if not na(ratio)
float impliedOther = close * ratio
baseLevel := math.round(impliedOther / 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 active and not na(ratio) and ratio != 0 and not na(baseLevel)
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 yLevel = na
float otherLevel = na
if isIndexMode
// y is native to the index chart; futures value = index * ratio
yLevel := baseLevel + i * stepSize
otherLevel := yLevel * ratio
else
// ladder step is in NAS100/ETF units; futures y = other / ratio
otherLevel := baseLevel + i * stepSize
yLevel := otherLevel / 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=yLevel,
x2=endX, y2=yLevel,
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=yLevel,
text=otherName + " " + formatN(otherLevel, otherDecimals),
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, 5, border_width=1)
if barstate.isrealtime or barstate.islast
table.clear(ratioTable, 0, 0, 0, 4)
if showRatioBox and active
int row = 0
table.cell(ratioTable, 0, row, selfName + " -> " + otherName + " Ratio", 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(otherPrevClose)
table.cell(ratioTable, 0, row, "Prev " + otherName + ": " + formatN(otherPrevClose, otherDecimals), text_color=color.silver, bgcolor=color.new(color.black, 15), text_size=size.small)
row += 1
if showPrevCloses and not na(selfPrevClose)
table.cell(ratioTable, 0, row, "Prev " + selfName + ": " + formatN(selfPrevClose, isIndexMode ? decimalsIndex : decimalsFutures), text_color=color.silver, bgcolor=color.new(color.black, 15), text_size=size.small)