本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Gold Key Levels + SMT Confirmation", "Key Levels", overlay = true, max_lines_count = 500, max_labels_count = 500, max_boxes_count = 200)
const string NY = "America/New_York"
// ---------------- Inputs ----------------
gH = "Higher-timeframe levels"
showPD = input.bool(true, "Previous Day High/Low", group = gH, inline = "pd")
colPD = input.color(#FF9800, "", group = gH, inline = "pd")
showPW = input.bool(true, "Previous Week High/Low", group = gH, inline = "pw")
colPW = input.color(#2962FF, "", group = gH, inline = "pw")
showPM = input.bool(true, "Previous Month High/Low", group = gH, inline = "pm")
colPM = input.color(#9C27B0, "", group = gH, inline = "pm")
showPQ = input.bool(true, "Previous Quarter High/Low", group = gH, inline = "pq")
colPQ = input.color(#E91E63, "", group = gH, inline = "pq")
showPY = input.bool(false, "Previous Year High/Low", group = gH, inline = "py")
colPY = input.color(#FFD600, "", group = gH, inline = "py")
showPH = input.bool(true, "Previous Hour High/Low", group = gH, inline = "ph")
colPH = input.color(#9E9E9E, "", group = gH, inline = "ph")
gO = "Opens (dashed lines)"
showDO = input.bool(true, "Daily Open", group = gO, inline = "do")
colDO = input.color(#FF9800, "", group = gO, inline = "do")
showWO = input.bool(false, "Weekly Open", group = gO, inline = "wo")
colWO = input.color(#2962FF, "", group = gO, inline = "wo")
showMO = input.bool(false, "Monthly Open", group = gO, inline = "mo")
colMO = input.color(#9C27B0, "", group = gO, inline = "mo")
showYO = input.bool(true, "Yearly Open", group = gO, inline = "yo")
colYO = input.color(#FFD600, "", group = gO, inline = "yo")
gS = "Sessions (New York time)"
showAsia = input.bool(true, "Asia", group = gS, inline = "as")
asiaSess = input.session("1800-0300", "", group = gS, inline = "as")
colAsia = input.color(#00BCD4, "", group = gS, inline = "as")
showLon = input.bool(true, "London", group = gS, inline = "ln")
lonSess = input.session("0300-0830", "", group = gS, inline = "ln")
colLon = input.color(#4CAF50, "", group = gS, inline = "ln")
showNY = input.bool(true, "New York", group = gS, inline = "ny")
nySess = input.session("0830-1700", "", group = gS, inline = "ny")
colNY = input.color(#F44336, "", group = gS, inline = "ny")
showBoxes = input.bool(true, "Shade sessions on chart", group = gS)
gSt = "Style"
lineW = input.int(1, "Line width", 1, 4, group = gSt)
lblOff = input.int(8, "Label offset (bars to the right)", 0, 100, group = gSt)
showPrice = input.bool(true, "Show price on labels", group = gSt)
hideSwept = input.bool(false, "Hide levels once taken (instead of fading)", group = gSt)
gC = "Confirmation (SMT)"
showSMT = input.bool(true, "Show confirmation table", group = gC)
compSym = input.symbol("COMEX:SI1!", "Compare with", group = gC)
inverse = input.bool(false, "Inverse correlation (tick this for DXY)", group = gC)
tblPosIn = input.string("Top Right", "Table position", options = ["Top Right", "Bottom Right", "Bottom Left", "Top Left"], group = gC)
// ---------------- Level object ----------------
type Level
float price
int born
int kind
string name
color col
line ln
label lb
bool swept = false
lblText(string nm, float px) =>
showPrice ? nm + " " + str.tostring(px, format.mintick) : nm
newLevel(float px, int x1, int kd, string nm, color c) =>
int x2 = bar_index + lblOff
line ln = line.new(x1, px, x2, px, color = c, width = lineW, style = kd == 0 ? line.style_dashed : line.style_solid)
label lb = label.new(x2, px, lblText(nm, px), style = label.style_label_left, color = color(na), textcolor = c, size = size.small)
Level.new(px, bar_index, kd, nm, c, ln, lb)
method kill(Level this) =>
if not na(this)
this.ln.delete()
this.lb.delete()
method update(Level this) =>
int x2 = bar_index + lblOff
this.ln.set_x2(x2)
this.lb.set_x(x2)
if this.kind != 0 and not this.swept and bar_index > this.born
bool hit = this.kind == 1 ? high > this.price : low < this.price
if hit
this.swept := true
color faded = color.new(this.col, 65)
this.ln.set_color(faded)
this.ln.set_style(line.style_dotted)
this.lb.set_textcolor(faded)
if hideSwept
this.ln.set_color(color(na))
this.lb.set_text("")
alert(syminfo.ticker + ": " + this.name + " taken at " + str.tostring(this.price, format.mintick), alert.freq_once_per_bar)
refresh(Level cur, float px, bool trig, bool show, int x1, int kd, string nm, color c) =>
Level res = cur
bool first = na(cur) and show and not na(px)
if trig or first
if not na(cur)
cur.kill()
res := na
if show and not na(px)
res := newLevel(px, math.max(x1, bar_index - 4000), kd, nm, c)
if not na(res)
res.update()
res
tfBelow(string tf) =>
timeframe.in_seconds() < timeframe.in_seconds(tf)
// ---------------- Session tracker ----------------
sessionHL(string sess) =>
bool sIn = not na(time(timeframe.period, sess, NY))
var float sHi = na
var float sLo = na
var int sStart = na
var float dHi = na
var float dLo = na
var int dStart = na
bool sStarted = sIn and not sIn[1]
bool sEnded = not sIn and sIn[1]
if sStarted
sHi := high
sLo := low
sStart := bar_index
else if sIn
sHi := math.max(sHi, high)
sLo := math.min(sLo, low)
if sEnded
dHi := sHi
dLo := sLo
dStart := sStart
[sIn, sStarted, sEnded, sHi, sLo, sStart, dHi, dLo, dStart]
drawBox(bool startNow, bool inS, box bx, int sb, float hi, float lo, color c, string txt) =>
box res = bx
if startNow
res := box.new(sb, hi, bar_index, lo, border_color = color.new(c, 70), bgcolor = color.new(c, 92), text = txt, text_color = color.new(c, 40), text_size = size.tiny, text_halign = text.align_left, text_valign = text.align_top)
else if inS and not na(res)
res.set_right(bar_index)
res.set_top(hi)
res.set_bottom(lo)
res
// ---------------- Higher-timeframe data ----------------
[pdH, pdL, dOpen] = request.security(syminfo.tickerid, "D", [high[1], low[1], open], lookahead = barmerge.lookahead_on)
[pwH, pwL, wOpen] = request.security(syminfo.tickerid, "W", [high[1], low[1], open], lookahead = barmerge.lookahead_on)
[pmH, pmL, mOpen] = request.security(syminfo.tickerid, "M", [high[1], low[1], open], lookahead = barmerge.lookahead_on)
[pqH, pqL] = request.security(syminfo.tickerid, "3M", [high[1], low[1]], lookahead = barmerge.lookahead_on)
[pyH, pyL, yOpen] = request.security(syminfo.tickerid, "12M", [high[1], low[1], open], lookahead = barmerge.lookahead_on)
[phH, phL] = request.security(syminfo.tickerid, "60", [high[1], low[1]], lookahead = barmerge.lookahead_on)
newD = timeframe.change("D")
newW = timeframe.change("W")
newM = timeframe.change("M")
newQ = timeframe.change("3M")
newY = timeframe.change("12M")
newH = timeframe.change("60")
var Level lPDH = na
var Level lPDL = na
var Level lPWH = na
var Level lPWL = na
var Level lPMH = na
var Level lPML = na
var Level lPQH = na
var Level lPQL = na
var Level lPYH = na
var Level lPYL = na
var Level lPHH = na
var Level lPHL = na
var Level lDO = na
var Level lWO = na
var Level lMO = na
var Level lYO = na
lPDH := refresh(lPDH, pdH, newD, showPD and tfBelow("D"), bar_index, 1, "PDH", colPD)
lPDL := refresh(lPDL, pdL, newD, showPD and tfBelow("D"), bar_index, -1, "PDL", colPD)
lPWH := refresh(lPWH, pwH, newW, showPW and tfBelow("W"), bar_index, 1, "PWH", colPW)
lPWL := refresh(lPWL, pwL, newW, showPW and tfBelow("W"), bar_index, -1, "PWL", colPW)
lPMH := refresh(lPMH, pmH, newM, showPM and tfBelow("M"), bar_index, 1, "PMH", colPM)
lPML := refresh(lPML, pmL, newM, showPM and tfBelow("M"), bar_index, -1, "PML", colPM)
lPQH := refresh(lPQH, pqH, newQ, showPQ and tfBelow("3M"), bar_index, 1, "PQH", colPQ)
lPQL := refresh(lPQL, pqL, newQ, showPQ and tfBelow("3M"), bar_index, -1, "PQL", colPQ)
lPYH := refresh(lPYH, pyH, newY, showPY and tfBelow("12M"), bar_index, 1, "PYH", colPY)
lPYL := refresh(lPYL, pyL, newY, showPY and tfBelow("12M"), bar_index, -1, "PYL", colPY)
lPHH := refresh(lPHH, phH, newH, showPH and tfBelow("60"), bar_index, 1, "1H High", colPH)
lPHL := refresh(lPHL, phL, newH, showPH and tfBelow("60"), bar_index, -1, "1H Low", colPH)
lDO := refresh(lDO, dOpen, newD, showDO and tfBelow("D"), bar_index, 0, "Daily Open", colDO)
lWO := refresh(lWO, wOpen, newW, showWO and tfBelow("W"), bar_index, 0, "Weekly Open", colWO)
lMO := refresh(lMO, mOpen, newM, showMO and tfBelow("M"), bar_index, 0, "Monthly Open", colMO)
lYO := refresh(lYO, yOpen, newY, showYO and tfBelow("12M"), bar_index, 0, "Yearly Open", colYO)
// ---------------- Sessions ----------------
sessOK = timeframe.isintraday and timeframe.in_seconds() <= 3600
[aIn, aSt, aEnd, aHi, aLo, aSB, aDH, aDL, aDS] = sessionHL(asiaSess)
[lIn, lSt, lEnd, lHi, lLo, lSB, lDH, lDL, lDS] = sessionHL(lonSess)
[nIn, nSt, nEnd, nHi, nLo, nSB, nDH, nDL, nDS] = sessionHL(nySess)
var box aBox = na
var box lBox = na
var box nBox = na
aBox := drawBox(showBoxes and showAsia and sessOK and aSt, aIn, aBox, aSB, aHi, aLo, colAsia, "Asia")
lBox := drawBox(showBoxes and showLon and sessOK and lSt, lIn, lBox, lSB, lHi, lLo, colLon, "London")
nBox := drawBox(showBoxes and showNY and sessOK and nSt, nIn, nBox, nSB, nHi, nLo, colNY, "New York")
var Level lAH = na
var Level lAL = na
var Level lLH = na
var Level lLL = na
var Level lNH = na
var Level lNL = na
lAH := refresh(lAH, aDH, aEnd, showAsia and sessOK, aDS, 1, "Asia H", colAsia)
lAL := refresh(lAL, aDL, aEnd, showAsia and sessOK, aDS, -1, "Asia L", colAsia)
lLH := refresh(lLH, lDH, lEnd, showLon and sessOK, lDS, 1, "London H", colLon)
lLL := refresh(lLL, lDL, lEnd, showLon and sessOK, lDS, -1, "London L", colLon)
lNH := refresh(lNH, nDH, nEnd, showNY and sessOK, nDS, 1, "NY H", colNY)
lNL := refresh(lNL, nDL, nEnd, showNY and sessOK, nDS, -1, "NY L", colNY)
// ---------------- SMT confirmation ----------------
sweepState(string asS, string loS) =>
var float xHi = na
var float xLo = na
var float xPDH = na
var float xPDL = na
var bool tPDH = false
var bool tPDL = false
if timeframe.change("D")
xPDH := xHi
xPDL := xLo
xHi := high
xLo := low
tPDH := false
tPDL := false
else
xHi := na(xHi) ? high : math.max(xHi, high)
xLo := na(xLo) ? low : math.min(xLo, low)
tPDH := tPDH or (not na(xPDH) and high > xPDH)
tPDL := tPDL or (not na(xPDL) and low < xPDL)
[s1In, s1St, s1End, s1Hi, s1Lo, s1SB, s1DH, s1DL, s1DS] = sessionHL(asS)
[s2In, s2St, s2End, s2Hi, s2Lo, s2SB, s2DH, s2DL, s2DS] = sessionHL(loS)
var bool tAH = false
var bool tAL = false
var bool tLH = false
var bool tLL = false
if s1St
tAH := false
tAL := false
if s2St
tLH := false
tLL := false
if not s1In and not na(s1DH)
tAH := tAH or high > s1DH
tAL := tAL or low < s1DL
if not s2In and not na(s2DH)
tLH := tLH or high > s2DH
tLL := tLL or low < s2DL
[tPDH, tPDL, tAH, tAL, tLH, tLL]
[gPDH, gPDL, gAH, gAL, gLH, gLL] = sweepState(asiaSess, lonSess)
[cPDH, cPDL, cAH, cAL, cLH, cLL] = request.security(compSym, timeframe.period, sweepState(asiaSess, lonSess))
bool kPDH = inverse ? cPDL : cPDH
bool kPDL = inverse ? cPDH : cPDL
bool kAH = inverse ? cAL : cAH
bool kAL = inverse ? cAH : cAL
bool kLH = inverse ? cLL : cLH
bool kLL = inverse ? cLH : cLL
bool bearSMT = sessOK and (gPDH != kPDH or gAH != kAH or gLH != kLH)
bool bullSMT = sessOK and (gPDL != kPDL or gAL != kAL or gLL != kLL)
if bearSMT and not bearSMT[1]
alert(syminfo.ticker + ": possible BEARISH SMT vs " + compSym, alert.freq_once_per_bar)
if bullSMT and not bullSMT[1]
alert(syminfo.ticker + ": possible BULLISH SMT vs " + compSym, alert.freq_once_per_bar)
tblPosition = switch tblPosIn
"Top Right" => position.top_right
"Bottom Right" => position.bottom_right
"Bottom Left" => position.bottom_left
=> position.top_left
var table tbl = table.new(tblPosition, 3, 8, bgcolor = color.new(#131722, 15), border_color = color.new(color.gray, 70), border_width = 1)
mark(bool b) =>
b ? "✔" : "–"
if showSMT and barstate.islast
compName = str.split(compSym, ":").last() + (inverse ? " (inv)" : "")
txtC = color.white
tbl.cell(0, 0, "Level", text_color = txtC, text_size = size.small)
tbl.cell(1, 0, syminfo.ticker, text_color = txtC, text_size = size.small)
tbl.cell(2, 0, compName, text_color = txtC, text_size = size.small)
names = array.from("PDH", "PDL", "Asia H", "Asia L", "London H", "London L")
gArr = array.from(gPDH, gPDL, gAH, gAL, gLH, gLL)
cArr = array.from(kPDH, kPDL, kAH, kAL, kLH, kLL)
for i = 0 to 5
bool g = gArr.get(i)
bool c = cArr.get(i)
color bg = g != c ? color.new(#FF9800, 55) : color(na)
tbl.cell(0, i + 1, names.get(i), text_color = txtC, text_size = size.small, bgcolor = bg)
tbl.cell(1, i + 1, mark(g), text_color = txtC, text_size = size.small, bgcolor = bg)
tbl.cell(2, i + 1, mark(c), text_color = txtC, text_size = size.small, bgcolor = bg)
string smtTxt = bearSMT and bullSMT ? "Mixed" : bearSMT ? "Bearish SMT ↓" : bullSMT ? "Bullish SMT ↑" : "No divergence"
color smtCol = bearSMT and not bullSMT ? color.new(color.red, 30) : bullSMT and not bearSMT ? color.new(color.green, 30) : color.new(color.gray, 60)
tbl.cell(0, 7, "SMT", text_color = txtC, text_size = size.small)
tbl.cell(1, 7, smtTxt, text_color = txtC, text_size = size.small, bgcolor = smtCol)
tbl.merge_cells(1, 7, 2, 7)