本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("PROBE Key Levels", shorttitle = "PROBE Levels", overlay = true, max_lines_count = 20, max_labels_count = 20, dynamic_requests = true)
// First draft. All boundaries use New York calendar timestamps, including DST.
// Previous-day close means the last minute closing at 16:00, NOT settlement.
const string TZ = "America/New_York"
const int MINUTE_MS = 60000
const string DISPLAY = "Display"
const string CLOSE = "Previous day close"
const string ASIA = "Asian session - 18:00 to 00:00 NY"
const string LONDON = "London session - 00:00 to 06:00 NY"
const string DAY = "Previous day - full futures session"
const string WEEK = "Previous week - full futures week"
const string PRE = "Premarket - 18:00 to 09:30 NY"
const string NY_OPEN = "NY Open - 09:30 to 16:00 NY"
showPdc = input.bool(true, "Previous day close", inline = "pdc", group = CLOSE)
pdcColor = input.color(color.gray, "Color", inline = "pdc", group = CLOSE, tooltip = "Close of the native one-minute candle ending at 16:00 NY in the previous observed trading session. Not exchange settlement or the 17:00 close.")
closeMinTf = input.timeframe("1", "Min chart TF", inline = "closeTf", group = CLOSE)
closeMaxTf = input.timeframe("1D", "Max chart TF", inline = "closeTf", group = CLOSE)
showAh = input.bool(true, "Asian high", inline = "ah", group = ASIA)
ahColor = input.color(color.aqua, "Color", inline = "ah", group = ASIA)
showAl = input.bool(true, "Asian low", inline = "al", group = ASIA)
alColor = input.color(color.aqua, "Color", inline = "al", group = ASIA)
asiaMinTf = input.timeframe("1", "Min chart TF", inline = "asiaTf", group = ASIA)
asiaMaxTf = input.timeframe("1D", "Max chart TF", inline = "asiaTf", group = ASIA)
showLh = input.bool(true, "London high", inline = "lh", group = LONDON)
lhColor = input.color(color.orange, "Color", inline = "lh", group = LONDON)
showLl = input.bool(true, "London low", inline = "ll", group = LONDON)
llColor = input.color(color.orange, "Color", inline = "ll", group = LONDON)
londonMinTf = input.timeframe("1", "Min chart TF", inline = "londonTf", group = LONDON)
londonMaxTf = input.timeframe("1D", "Max chart TF", inline = "londonTf", group = LONDON)
showPdh = input.bool(true, "Previous day high", inline = "pdh", group = DAY)
pdhColor = input.color(color.blue, "Color", inline = "pdh", group = DAY)
showPdl = input.bool(true, "Previous day low", inline = "pdl", group = DAY)
pdlColor = input.color(color.blue, "Color", inline = "pdl", group = DAY)
dayMinTf = input.timeframe("1", "Min chart TF", inline = "dayTf", group = DAY)
dayMaxTf = input.timeframe("1D", "Max chart TF", inline = "dayTf", group = DAY)
showPwh = input.bool(true, "Previous week high", inline = "pwh", group = WEEK)
pwhColor = input.color(color.fuchsia, "Color", inline = "pwh", group = WEEK)
showPwl = input.bool(true, "Previous week low", inline = "pwl", group = WEEK)
pwlColor = input.color(color.fuchsia, "Color", inline = "pwl", group = WEEK)
weekMinTf = input.timeframe("1", "Min chart TF", inline = "weekTf", group = WEEK)
weekMaxTf = input.timeframe("1D", "Max chart TF", inline = "weekTf", group = WEEK)
showPmh = input.bool(true, "Premarket high", inline = "pmh", group = PRE)
pmhColor = input.color(color.teal, "Color", inline = "pmh", group = PRE)
showPml = input.bool(true, "Premarket low", inline = "pml", group = PRE)
pmlColor = input.color(color.teal, "Color", inline = "pml", group = PRE)
preMinTf = input.timeframe("1", "Min chart TF", inline = "preTf", group = PRE)
preMaxTf = input.timeframe("1D", "Max chart TF", inline = "preTf", group = PRE)
showNyOpen = input.bool(true, "NY Open", inline = "nyOpen", group = NY_OPEN)
nyOpenColor = input.color(color.green, "Color", inline = "nyOpen", group = NY_OPEN, tooltip = "Exact 09:30 New York opening price. Solid line, visible only until 16:00 NY; this same reference window applies to metals.")
nyMinTf = input.timeframe("1", "Min chart TF", inline = "nyTf", group = NY_OPEN)
nyMaxTf = input.timeframe("1D", "Max chart TF", inline = "nyTf", group = NY_OPEN)
lineStyle = input.string("Dashed", "Line style", options = ["Solid", "Dashed", "Dotted"], group = DISPLAY)
lineWidth = input.int(1, "Line width", minval = 1, maxval = 4, group = DISPLAY)
showLabels = input.bool(true, "Show labels", group = DISPLAY)
labelSize = input.string("Normal", "Label size", options = ["Tiny", "Small", "Normal", "Large"], group = DISPLAY)
compactNames = input.bool(true, "Abbreviated names", group = DISPLAY)
showPrices = input.bool(true, "Show prices in labels", group = DISPLAY)
showDeveloping = input.bool(true, "Show developing session levels", group = DISPLAY, tooltip = "Live session ranges change until their cutoff. Labels show (developing). Disable to show only completed session ranges.")
labelOffset = input.int(2, "Label offset (chart bars)", minval = 0, maxval = 50, group = DISPLAY)
showStatus = input.bool(true, "Show status panel", group = DISPLAY)
f_tf_visible(string minimum, string maximum) =>
float minimumSeconds = timeframe.in_seconds(minimum)
float maximumSeconds = timeframe.in_seconds(maximum)
if minimumSeconds > maximumSeconds
runtime.error("Each level group's Min chart TF must be less than or equal to its Max chart TF.")
float chartSeconds = timeframe.in_seconds()
chartSeconds >= minimumSeconds and chartSeconds <= maximumSeconds
bool closeTfVisible = f_tf_visible(closeMinTf, closeMaxTf)
bool asiaTfVisible = f_tf_visible(asiaMinTf, asiaMaxTf)
bool londonTfVisible = f_tf_visible(londonMinTf, londonMaxTf)
bool dayTfVisible = f_tf_visible(dayMinTf, dayMaxTf)
bool weekTfVisible = f_tf_visible(weekMinTf, weekMaxTf)
bool preTfVisible = f_tf_visible(preMinTf, preMaxTf)
bool nyTfVisible = f_tf_visible(nyMinTf, nyMaxTf)
f_day_anchor(int t) =>
int y = year(t, TZ)
int m = month(t, TZ)
int d = dayofmonth(t, TZ)
int today = timestamp(TZ, y, m, d, 18, 0)
t >= today ? today : timestamp(TZ, y, m, d - 1, 18, 0)
f_week_anchor(int dayKey) =>
int daysSinceSunday = (dayofweek(dayKey, TZ) - dayofweek.sunday + 7) % 7
timestamp(TZ, year(dayKey, TZ), month(dayKey, TZ), dayofmonth(dayKey, TZ) - daysSinceSunday, 18, 0)
f_next_day_time(int anchor, int h, int m) =>
timestamp(TZ, year(anchor, TZ), month(anchor, TZ), dayofmonth(anchor, TZ) + 1, h, m)
type LevelState
int dayKey = na
int weekKey = na
bool dayReady = false
bool weekReady = false
bool londonReady = false
float dayHigh = na
float dayLow = na
float weekHigh = na
float weekLow = na
float dayClose = na
float pdc = na
float pdh = na
float pdl = na
float pwh = na
float pwl = na
float ah = na
float al = na
float lh = na
float ll = na
float pmh = na
float pml = na
int dayHighAt = na
int dayLowAt = na
int weekHighAt = na
int weekLowAt = na
int dayCloseAt = na
int pdcAt = na
int pdhAt = na
int pdlAt = na
int pwhAt = na
int pwlAt = na
int ahAt = na
int alAt = na
int lhAt = na
int llAt = na
int pmhAt = na
int pmlAt = na
float nyOpen = na
int nyOpenAt = na
method ingest(LevelState this, int t, int tc, float h, float l, float c, float o) =>
int dk = f_day_anchor(t)
int wk = f_week_anchor(dk)
// Sunday-Thursday anchors own Monday-Friday sessions. Ignore maintenance.
int anchorDow = dayofweek(dk, TZ)
bool tradingBar = anchorDow >= dayofweek.sunday and anchorDow <= dayofweek.thursday and hour(t, TZ) != 17
if tradingBar and not na(h) and not na(l)
if na(this.weekKey) or wk != this.weekKey
this.pwh := this.weekReady ? this.weekHigh : na
this.pwl := this.weekReady ? this.weekLow : na
this.pwhAt := this.weekReady ? this.weekHighAt : na
this.pwlAt := this.weekReady ? this.weekLowAt : na
this.weekKey := wk
this.weekReady := t == wk
this.weekHigh := na
this.weekLow := na
this.weekHighAt := na
this.weekLowAt := na
if na(this.dayKey) or dk != this.dayKey
this.pdh := this.dayReady ? this.dayHigh : na
this.pdl := this.dayReady ? this.dayLow : na
this.pdc := this.dayClose
this.pdhAt := this.dayReady ? this.dayHighAt : na
this.pdlAt := this.dayReady ? this.dayLowAt : na
this.pdcAt := this.dayCloseAt
this.dayKey := dk
this.dayReady := t == dk
this.londonReady := false
this.dayHigh := na
this.dayLow := na
this.dayClose := na
this.ah := na
this.al := na
this.lh := na
this.ll := na
this.pmh := na
this.pml := na
this.dayHighAt := na
this.dayLowAt := na
this.dayCloseAt := na
this.ahAt := na
this.alAt := na
this.lhAt := na
this.llAt := na
this.pmhAt := na
this.pmlAt := na
this.nyOpen := na
this.nyOpenAt := na
// Preserve the first candle at an equal extreme; move only on a new one.
if na(this.dayHigh) or h > this.dayHigh
this.dayHighAt := t
if na(this.dayLow) or l < this.dayLow
this.dayLowAt := t
if na(this.weekHigh) or h > this.weekHigh
this.weekHighAt := t
if na(this.weekLow) or l < this.weekLow
this.weekLowAt := t
this.dayHigh := na(this.dayHigh) ? h : math.max(this.dayHigh, h)
this.dayLow := na(this.dayLow) ? l : math.min(this.dayLow, l)
this.weekHigh := na(this.weekHigh) ? h : math.max(this.weekHigh, h)
this.weekLow := na(this.weekLow) ? l : math.min(this.weekLow, l)
int asiaEnd = f_next_day_time(dk, 0, 0)
int londonEnd = f_next_day_time(dk, 6, 0)
int preEnd = f_next_day_time(dk, 9, 30)
if t < asiaEnd
if na(this.ah) or h > this.ah
this.ahAt := t
if na(this.al) or l < this.al
this.alAt := t
this.ah := na(this.ah) ? h : math.max(this.ah, h)
this.al := na(this.al) ? l : math.min(this.al, l)
if t >= asiaEnd and t < londonEnd
if t == asiaEnd
this.londonReady := true
if na(this.lh) or h > this.lh
this.lhAt := t
if na(this.ll) or l < this.ll
this.llAt := t
this.lh := na(this.lh) ? h : math.max(this.lh, h)
this.ll := na(this.ll) ? l : math.min(this.ll, l)
if t < preEnd
if na(this.pmh) or h > this.pmh
this.pmhAt := t
if na(this.pml) or l < this.pml
this.pmlAt := t
this.pmh := na(this.pmh) ? h : math.max(this.pmh, h)
this.pml := na(this.pml) ? l : math.min(this.pml, l)
if tc == f_next_day_time(dk, 16, 0)
this.dayClose := c
this.dayCloseAt := tc
if t == preEnd and not na(o)
this.nyOpen := o
this.nyOpenAt := t
string root = str.upper(syminfo.root)
bool supported = root == "ES" or root == "MES" or root == "NQ" or root == "MNQ" or root == "YM" or root == "MYM" or root == "GC" or root == "MGC" or root == "SI" or root == "SIL" or root == "HG" or root == "MHG"
if barstate.isfirst
if not chart.is_standard
runtime.error("PROBE Key Levels requires a standard candlestick chart.")
if not supported
runtime.error("PROBE Key Levels supports ES/NQ/YM and GC/SI/HG, including their supported micro futures.")
if not timeframe.isintraday or timeframe.in_seconds() < 60
runtime.error("Use a standard intraday chart of 1 minute or higher. Daily, weekly, tick and seconds charts are not supported in this draft.")
// CME futures name their full ETH session "regular" and cash RTH "us_regular".
if syminfo.session != session.regular and syminfo.session != session.extended
runtime.error("Enable the chart's extended/electronic trading session. PROBE Key Levels needs overnight bars for full-session ranges.")
var LevelState levels = LevelState.new()
bool sourceAvailable = false
if timeframe.in_seconds() == 60
levels.ingest(time, time_close, high, low, close, open)
sourceAvailable := true
else
// Ordered native minutes, never the entire high/low of a boundary-crossing bar.
[times, closesAt, highs, lows, closes, opens] = request.security_lower_tf(syminfo.tickerid, "1", [time, time_close, high, low, close, open])
sourceAvailable := array.size(times) > 0
if sourceAvailable
for i = 0 to array.size(times) - 1
levels.ingest(array.get(times, i), array.get(closesAt, i), array.get(highs, i), array.get(lows, i), array.get(closes, i), array.get(opens, i))
f_style() =>
lineStyle == "Solid" ? line.style_solid : lineStyle == "Dotted" ? line.style_dotted : line.style_dashed
f_size() =>
labelSize == "Tiny" ? size.tiny : labelSize == "Small" ? size.small : labelSize == "Large" ? size.large : size.normal
var array<line> drawings = array.new<line>()
var array<label> captions = array.new<label>()
var table status = table.new(position.top_right, 1, 2)
if barstate.islast
while array.size(drawings) > 0
line.delete(array.pop(drawings))
while array.size(captions) > 0
label.delete(array.pop(captions))
int nowTime = barstate.isrealtime ? timenow : time_close
bool currentDay = not na(levels.dayKey) and f_day_anchor(barstate.isrealtime ? timenow : time_close - 1) == levels.dayKey
bool available = sourceAvailable and currentDay
int nyCloseTime = f_next_day_time(levels.dayKey, 16, 0)
bool beforeNyClose = currentDay and nowTime < nyCloseTime
bool asiaDeveloping = currentDay and nowTime < f_next_day_time(levels.dayKey, 0, 0)
bool londonDeveloping = currentDay and nowTime >= f_next_day_time(levels.dayKey, 0, 0) and nowTime < f_next_day_time(levels.dayKey, 6, 0)
bool preDeveloping = currentDay and nowTime < f_next_day_time(levels.dayKey, 9, 30)
array<float> prices = array.from(levels.pdc, levels.dayReady ? levels.ah : na, levels.dayReady ? levels.al : na, levels.londonReady ? levels.lh : na, levels.londonReady ? levels.ll : na, levels.pdh, levels.pdl, levels.pwh, levels.pwl, levels.dayReady ? levels.pmh : na, levels.dayReady ? levels.pml : na)
array<int> origins = array.from(levels.pdcAt, levels.ahAt, levels.alAt, levels.lhAt, levels.llAt, levels.pdhAt, levels.pdlAt, levels.pwhAt, levels.pwlAt, levels.pmhAt, levels.pmlAt)
array<bool> enabled = array.from(showPdc and closeTfVisible, showAh and asiaTfVisible, showAl and asiaTfVisible, showLh and londonTfVisible, showLl and londonTfVisible, showPdh and dayTfVisible, showPdl and dayTfVisible, showPwh and weekTfVisible, showPwl and weekTfVisible, showPmh and preTfVisible, showPml and preTfVisible)
array<bool> developing = array.from(false, asiaDeveloping, asiaDeveloping, londonDeveloping, londonDeveloping, false, false, false, false, preDeveloping, preDeveloping)
array<color> colors = array.from(pdcColor, ahColor, alColor, lhColor, llColor, pdhColor, pdlColor, pwhColor, pwlColor, pmhColor, pmlColor)
array<string> names = compactNames ? array.from("Prev Day Close", "Asia H", "Asia L", "London H", "London L", "Prev Day High", "Prev Day Low", "Prev Week High", "Prev Week Low", "PMH", "PML") : array.from("Prev Day Close", "Asian High", "Asian Low", "London High", "London Low", "Prev Day High", "Prev Day Low", "Prev Week High", "Prev Week Low", "Premarket High", "Premarket Low")
array<float> mergedPrices = array.new<float>()
array<string> mergedNames = array.new<string>()
array<color> mergedColors = array.new<color>()
int unavailableCount = 0
int visibleCount = 0
for i = 0 to 10
bool wanted = array.get(enabled, i)
float price = array.get(prices, i)
int start = array.get(origins, i)
bool live = array.get(developing, i)
bool visible = wanted and beforeNyClose and available and not na(price) and not na(start) and (showDeveloping or not live)
if wanted and beforeNyClose and (not available or na(price) or na(start))
unavailableCount += 1
if visible
visibleCount += 1
color tint = array.get(colors, i)
// Each level retains its own origin, even when its label is combined.
// All reference lines end at this trading day's NY close.
array.push(drawings, line.new(x1 = start, y1 = price, x2 = nyCloseTime, y2 = price, xloc = xloc.bar_time, extend = extend.none, color = tint, style = f_style(), width = lineWidth))
float tickPrice = math.round_to_mintick(price)
int match = array.indexof(mergedPrices, tickPrice)
string title = array.get(names, i) + (live ? " (developing)" : "")
if match >= 0
array.set(mergedNames, match, array.get(mergedNames, match) + " / " + title)
else
array.push(mergedPrices, tickPrice)
array.push(mergedNames, title)
array.push(mergedColors, tint)
bool nySession = currentDay and nowTime >= f_next_day_time(levels.dayKey, 9, 30) and nowTime < nyCloseTime
if showNyOpen and nyTfVisible and nySession
if available and not na(levels.nyOpen) and not na(levels.nyOpenAt)
visibleCount += 1
array.push(drawings, line.new(x1 = levels.nyOpenAt, y1 = levels.nyOpen, x2 = nyCloseTime, y2 = levels.nyOpen, xloc = xloc.bar_time, extend = extend.none, color = nyOpenColor, style = line.style_solid, width = lineWidth))
float nyPrice = math.round_to_mintick(levels.nyOpen)
int match = array.indexof(mergedPrices, nyPrice)
if match >= 0
array.set(mergedNames, match, array.get(mergedNames, match) + " / NY Open")
else
array.push(mergedPrices, nyPrice)
array.push(mergedNames, "NY Open")
array.push(mergedColors, nyOpenColor)
else
unavailableCount += 1
if showLabels and array.size(mergedPrices) > 0
int labelTime = math.min(nyCloseTime, chart.right_visible_bar_time + int(timeframe.in_seconds() * 1000) * labelOffset)
for i = 0 to array.size(mergedPrices) - 1
float price = array.get(mergedPrices, i)
string title = array.get(mergedNames, i) + (showPrices ? " " + str.tostring(price, format.mintick) : "")
array.push(captions, label.new(labelTime, price, title, xloc = xloc.bar_time, style = label.style_label_down, color = color.new(color.black, 100), textcolor = array.get(mergedColors, i), size = f_size()))
if showStatus or unavailableCount > 0
string message = not sourceAvailable ? "One-minute data unavailable for this chart bar." : not currentDay ? "Waiting for the current futures session." : not beforeNyClose ? "NY close reached. Levels resume with the next 18:00 session." : unavailableCount > 0 ? str.tostring(unavailableCount) + " selected levels awaiting session data/history." : "Selected levels ready."
table.cell(status, 0, 0, "PROBE Levels | " + str.tostring(visibleCount) + " levels", text_color = chart.fg_color, bgcolor = color.new(chart.bg_color, 10))
table.cell(status, 0, 1, message, text_color = unavailableCount > 0 ? color.orange : chart.fg_color, bgcolor = color.new(chart.bg_color, 10))
else
table.clear(status, 0, 0, 0, 1)