本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
// CDC Trading LABN
indicator('ICT Opening Gaps & EHPDA', shorttitle = 'ICT Opening Gaps', max_polylines_count = 100, overlay = true, max_boxes_count = 200, max_lines_count = 500, max_labels_count = 500)
// ───────────────────────────────────────────────
// Settings
// ───────────────────────────────────────────────
showNWOG = input.bool(true, 'Show NWOG', group = 'Gaps')
showNDOG = input.bool(true, 'Show NDOG', group = 'Gaps')
nwogCountInput = input.int(5, 'NWOG gaps to show', minval = 1, maxval = 20, group = 'Gaps')
ndogCountInput = input.int(5, 'NDOG gaps to show', minval = 1, maxval = 20, group = 'Gaps')
// Compact braces. Keep the original 1–20 history limit per gap type.
gapDisplay = input.string('Rectangle', 'Gap display', options=['Zone', 'Rectangle', 'Zone + Rectangle'], group='Gap Rectangle')
braceOffset = input.int(10, 'Horizontal offset (bars)', minval=-500, maxval=350, group='Gap Rectangle')
braceLength = input.int(8, 'Rectangle width (bars)', minval=1, maxval=100, group='Gap Rectangle')
braceWeight = input.int(1, 'Line thickness', minval=1, maxval=4, group='Gap Rectangle')
braceMid = input.bool(true, 'Show midpoint (50%)', group='Gap Rectangle')
braceQuarters = input.bool(true, 'Show quarters (25%, 75%)', group='Gap Rectangle')
braceNames = input.bool(true, 'Show name beside rectangle', group='Gap Rectangle')
braceNameGap = input.int(2, 'Name gap (bars)', minval=0, maxval=20, group='Gap Rectangle')
braceOwnColor = input.bool(true, 'Use each gap color', group='Gap Rectangle')
braceColor = input.color(color.gray, 'Custom rectangle color', group='Gap Rectangle')
bool drawZones = gapDisplay != 'Rectangle'
bool drawBraces = gapDisplay != 'Zone'
// Colors
nwogColor = input.color(#ba68c8, 'NWOG color', inline = 'c', group = 'Colors')
ndogColor = input.color(#5b9cf6, 'NDOG color', inline = 'c', group = 'Colors')
ehColor = input.color(color.orange, 'Event Horizon', group = 'Event Horizon')
showEH = input.bool(false, 'Show Event Horizon', group = 'Event Horizon')
// Labels & Price
showGapTypeLabel = input.bool(false, 'Show NWOG / NDOG name', group = 'Labels')
showDateLabel = input.bool(true, 'Show Date in Label', group = 'Labels')
showPriceLabels = input.bool(false, 'Show Price Levels', group = 'Labels')
dateFormat = input.string('dd.MM', 'Date Format', options = ['dd.MM', 'MM.dd', 'dd.MM.yy', 'MM.dd.yy'], group = 'Labels')
// Advanced
use5to6Gap = input.bool(false, "'5pm–6pm' Gap Mode (Intraday)", tooltip = 'Uses 4H bars to detect Sunday/Monday gap in intraday charts', group = 'Advanced')
// Info Table
showInfoTable = input.bool(false, 'Show Info Table', group = 'Info Table')
tablePosition = input.string('Top Right', 'Position', options = ['Top Left', 'Top Right', 'Bottom Left', 'Bottom Right'], group = 'Info Table')
tableSize = input.string('Small', 'Size', options = ['Tiny', 'Small', 'Normal'], group = 'Info Table')
// Alerts
enableAlerts = input.bool(false, 'Enable Alerts', group = 'Alerts')
alertNewNWOG = input.bool(false, 'Alert on New NWOG', group = 'Alerts')
alertNewNDOG = input.bool(false, 'Alert on New NDOG', group = 'Alerts')
alertGapFill = input.bool(false, 'Alert on Gap Fill', group = 'Alerts')
alertEHActive = input.bool(false, 'Alert on Event Horizon Active', group = 'Alerts')
// ───────────────────────────────────────────────
// Data fetching - Weekly
// ───────────────────────────────────────────────
// FIX 2026-10-22: never use exchange settlement as the closing price.
// SET ON/OFF in the chart no longer changes the requested D/W futures closes.
// `off` selects the actual last traded close, which NDOG and NWOG require.
string actualCloseTicker = ticker.modify(syminfo.tickerid, settlement_as_close = settlement_as_close.off)
weekOpen = request.security(actualCloseTicker, '1W', open, lookahead = barmerge.lookahead_on)
weekClose1 = request.security(actualCloseTicker, '1W', close[1], lookahead = barmerge.lookahead_on)
float weekHi = math.max(weekOpen, weekClose1)
float weekLo = math.min(weekOpen, weekClose1)
// ───────────────────────────────────────────────
// Data fetching - Daily
// ───────────────────────────────────────────────
dayOpen = request.security(actualCloseTicker, '1D', open, lookahead = barmerge.lookahead_on)
dayClose1 = request.security(actualCloseTicker, '1D', close[1], lookahead = barmerge.lookahead_on)
float dayHi = math.max(dayOpen, dayClose1)
float dayLo = math.min(dayOpen, dayClose1)
// ───────────────────────────────────────────────
// For 5pm–6pm mode (uses 4H to detect Sunday close → Monday open)
// ───────────────────────────────────────────────
isNewWeek_4h = request.security(syminfo.tickerid, '240', ta.change(time('W')) != 0, lookahead = barmerge.lookahead_on)
prevClose_4h = request.security(syminfo.tickerid, '240', close[1], lookahead = barmerge.lookahead_on)
currOpen_4h = request.security(syminfo.tickerid, '240', open, lookahead = barmerge.lookahead_on)
var customWeekHi = float(na)
var customWeekLo = float(na)
if use5to6Gap and timeframe.isintraday and isNewWeek_4h
customWeekHi := math.max(prevClose_4h, currOpen_4h)
customWeekLo := math.min(prevClose_4h, currOpen_4h)
customWeekLo
// Override if 5pm–6pm mode active
float finalWeekHi = use5to6Gap ? customWeekHi : weekHi
float finalWeekLo = use5to6Gap ? customWeekLo : weekLo
// ───────────────────────────────────────────────
// UDT: GapData
// ───────────────────────────────────────────────
type GapData
box area
line topLine
line midLine
line botLine
label topLbl
label midLbl
label botLbl
float top
float bot
int barIndex
int timeValue
bool filled
polyline brace = na
array<line> marks = na
label nameLabel = na
color gapColor = na
string gapName = ''
var nwogList = array.new<GapData>()
var ndogList = array.new<GapData>()
var ehLine = line(na)
var lastEHState = false
// ───────────────────────────────────────────────
// Time to next close calculation
// ───────────────────────────────────────────────
getTimeToClose(string tf) =>
int currentTime = time
int tfMillis = timeframe.in_seconds(tf) * 1000
int elapsed = currentTime % tfMillis
int remaining = tfMillis - elapsed
int hours = math.floor(remaining / 3600000)
int minutes = math.floor(remaining % 3600000 / 60000)
str.format('{0}h {1}m', hours, minutes)
// ───────────────────────────────────────────────
// Create gap visuals
// ───────────────────────────────────────────────
createGap(float hi, float lo, int barIdx, int t, string prefix) =>
GapData result = na
if not na(hi) and not na(lo) and hi != lo
color gapColor = prefix == 'NWOG' ? nwogColor : ndogColor
float mid = math.avg(hi, lo)
string labelText = (showGapTypeLabel ? prefix : '') + (showGapTypeLabel and showDateLabel ? ' | ' : '') + (showDateLabel ? str.format_time(t, dateFormat, syminfo.timezone) : '')
box b = na
line topL = na
line midL = na
line botL = na
if drawZones
b := box.new(t, hi, t, lo, xloc=xloc.bar_time, bgcolor=color.new(gapColor, 90), border_width=0, text=labelText, text_color=color.black, text_size=size.small)
topL := line.new(t, hi, t, hi, xloc=xloc.bar_time, color=color.new(gapColor, 40), extend=extend.right)
midL := line.new(t, mid, t, mid, xloc=xloc.bar_time, color=gapColor, style=line.style_dotted, extend=extend.right)
botL := line.new(t, lo, t, lo, xloc=xloc.bar_time, color=color.new(gapColor, 40), extend=extend.right)
label topLbl = label(na)
label midLbl = label(na)
label botLbl = label(na)
if showPriceLabels
topLbl := label.new(bar_index, hi, str.tostring(hi, format.mintick), color = color.new(color.black, 90), textcolor = color.white, style = label.style_label_lower_left)
midLbl := label.new(bar_index, mid, str.tostring(mid, format.mintick), color = color.new(color.black, 90), textcolor = color.white, style = label.style_label_left)
botLbl := label.new(bar_index, lo, str.tostring(lo, format.mintick), color = color.new(color.black, 90), textcolor = color.white, style = label.style_label_upper_left)
botLbl
result := GapData.new(b, topL, midL, botL, topLbl, midLbl, botLbl, hi, lo, barIdx, t, false)
result.gapColor := gapColor
result.gapName := labelText
result
result
// ───────────────────────────────────────────────
// Check if gap is filled
// ───────────────────────────────────────────────
checkGapFilled(GapData gap) =>
bool isFilled = false
if not na(gap) and not gap.filled
if high >= gap.top and low <= gap.bot
isFilled := true
isFilled
isFilled
// ───────────────────────────────────────────────
// Update lists and clean old gaps
// ───────────────────────────────────────────────
// Brace geometry is anchored to exact prices, with chart-bar horizontal sizing.
updateBrace(GapData g) =>
int x = bar_index + braceOffset
int tip = x + braceLength
float h = g.top - g.bot
float mid = math.avg(g.top, g.bot)
color c = braceOwnColor ? g.gapColor : braceColor
array<chart.point> pts = array.new<chart.point>()
array.push(pts, chart.point.from_index(x, g.top))
array.push(pts, chart.point.from_index(tip, g.top))
array.push(pts, chart.point.from_index(tip, g.bot))
array.push(pts, chart.point.from_index(x, g.bot))
if not na(g.brace)
polyline.delete(g.brace)
g.brace := polyline.new(pts, closed=true, xloc=xloc.bar_index, line_color=c, fill_color=color.new(c, 90), line_width=braceWeight)
color centerColor = color.rgb(int(color.r(c)*0.65), int(color.g(c)*0.65), int(color.b(c)*0.65))
if na(g.marks)
g.marks := array.new<line>()
for i = 0 to 4
float y = g.bot + h * i / 4.0
bool visible = i == 0 or i == 4 or (i == 2 ? braceMid : braceQuarters)
color markColor = visible ? (i == 2 ? centerColor : c) : color.new(c, 100)
int right = tip
if array.size(g.marks) <= i
array.push(g.marks, line.new(x, y, right, y, xloc=xloc.bar_index, color=markColor, width=braceWeight))
else
line ln = array.get(g.marks, i)
line.set_xy1(ln, x, y)
line.set_xy2(ln, right, y)
line.set_color(ln, markColor)
if braceNames
if na(g.nameLabel)
g.nameLabel := label.new(tip+braceNameGap, mid, g.gapName, style=label.style_label_left, color=color.new(color.black,100), textcolor=c, size=size.tiny)
else
label.set_xy(g.nameLabel, tip+braceNameGap, mid)
g
deleteBrace(GapData g) =>
if not na(g.brace)
polyline.delete(g.brace)
if not na(g.marks)
for ln in g.marks
line.delete(ln)
if not na(g.nameLabel)
label.delete(g.nameLabel)
updateVisual(GapData g) =>
if not na(g.area)
box.set_right(g.area, time)
line.set_x2(g.topLine, time)
line.set_x2(g.midLine, time)
line.set_x2(g.botLine, time)
if drawBraces
updateBrace(g)
if showPriceLabels
label.set_x(g.topLbl, bar_index)
label.set_x(g.midLbl, bar_index)
label.set_x(g.botLbl, bar_index)
updateList(array<GapData> list, GapData newGap, int maxCount) =>
if not na(newGap)
array.unshift(list, newGap)
if array.size(list) > maxCount
old = array.pop(list)
deleteBrace(old)
box.delete(old.area)
line.delete(old.topLine)
line.delete(old.midLine)
line.delete(old.botLine)
if showPriceLabels
label.delete(old.topLbl)
label.delete(old.midLbl)
label.delete(old.botLbl)
// ───────────────────────────────────────────────
// Main logic: Detect gaps
// ───────────────────────────────────────────────
// NWOG
var justCreatedNWOG = false
if showNWOG and (timeframe.isdaily or timeframe.isintraday)
if ta.change(time('W')) != 0
gap = createGap(finalWeekHi, finalWeekLo, bar_index, time, 'NWOG')
updateList(nwogList, gap, nwogCountInput)
justCreatedNWOG := true
if enableAlerts and alertNewNWOG and not na(gap)
alert('🔔 New NWOG formed at ' + syminfo.ticker + ' | Top: ' + str.tostring(gap.top) + ' | Bottom: ' + str.tostring(gap.bot), alert.freq_once_per_bar)
else
justCreatedNWOG := false
justCreatedNWOG
// NDOG
var justCreatedNDOG = false
if showNDOG and timeframe.isintraday
if ta.change(time('D')) != 0
if dayofweek != dayofweek.sunday or not showNWOG
gap = createGap(dayHi, dayLo, bar_index, time, 'NDOG')
updateList(ndogList, gap, ndogCountInput)
justCreatedNDOG := true
if enableAlerts and alertNewNDOG and not na(gap)
alert('🔔 New NDOG formed at ' + syminfo.ticker + ' | Top: ' + str.tostring(gap.top) + ' | Bottom: ' + str.tostring(gap.bot), alert.freq_once_per_bar)
else
justCreatedNDOG := false
justCreatedNDOG
// ───────────────────────────────────────────────
// Check for filled gaps and send alerts
// ───────────────────────────────────────────────
if enableAlerts and alertGapFill
for gap in nwogList
if checkGapFilled(gap)
gap.filled := true
alert('✅ NWOG filled at ' + syminfo.ticker + ' | Price crossed gap: ' + str.tostring(gap.top) + ' - ' + str.tostring(gap.bot), alert.freq_once_per_bar)
for gap in ndogList
if checkGapFilled(gap)
gap.filled := true
alert('✅ NDOG filled at ' + syminfo.ticker + ' | Price crossed gap: ' + str.tostring(gap.top) + ' - ' + str.tostring(gap.bot), alert.freq_once_per_bar)
// ───────────────────────────────────────────────
// Update visuals to extend to current bar
// ───────────────────────────────────────────────
if barstate.islast
for g in nwogList
updateVisual(g)
for g in ndogList
updateVisual(g)
// ───────────────────────────────────────────────
// Event Horizon (EHPDA)
// ───────────────────────────────────────────────
var currentEHActive = false
if showEH and barstate.islast
array<GapData> combined = array.new<GapData>()
if showNWOG and array.size(nwogList) > 0
for g in nwogList
array.push(combined, g)
else if showNDOG and array.size(ndogList) > 0
for g in ndogList
array.push(combined, g)
if array.size(combined) >= 2
// Sort by bar index
int n = array.size(combined)
for i = 0 to n - 2 by 1
for j = 0 to n - i - 2 by 1
if array.get(combined, j).barIndex > array.get(combined, j + 1).barIndex
GapData temp = array.get(combined, j)
array.set(combined, j, array.get(combined, j + 1))
array.set(combined, j + 1, temp)
GapData g1 = array.get(combined, array.size(combined) - 2)
GapData g2 = array.get(combined, array.size(combined) - 1)
bool gap1Above = g1.bot > g2.top
bool gap2Above = g2.bot > g1.top
if gap1Above or gap2Above
float ehLevel = gap2Above ? math.avg(g2.bot, g1.top) : math.avg(g1.bot, g2.top)
if na(ehLine)
ehLine := line.new(na, na, na, na, xloc=xloc.bar_time, color = ehColor, style = line.style_dashed)
ehLine
line.set_xy1(ehLine, g2.timeValue, ehLevel)
line.set_xy2(ehLine, time, ehLevel)
currentEHActive := true
if enableAlerts and alertEHActive and not lastEHState
alert('🎯 Event Horizon ACTIVE at ' + syminfo.ticker + ' | Level: ' + str.tostring(ehLevel), alert.freq_once_per_bar)
else
if not na(ehLine)
line.delete(ehLine)
ehLine := na
ehLine
currentEHActive := false
currentEHActive
else
if not na(ehLine)
line.delete(ehLine)
ehLine := na
ehLine
currentEHActive := false
currentEHActive
lastEHState := currentEHActive
// ───────────────────────────────────────────────
// Info Table
// ───────────────────────────────────────────────
var table infoTable = na
if showInfoTable and barstate.islast
string pos = tablePosition == 'Top Left' ? position.top_left : tablePosition == 'Top Right' ? position.top_right : tablePosition == 'Bottom Left' ? position.bottom_left : position.bottom_right
string size = tableSize == 'Tiny' ? size.tiny : tableSize == 'Small' ? size.small : size.normal
if na(infoTable)
infoTable := table.new(pos, 2, 6, bgcolor = color.new(color.black, 85), border_width = 1, border_color = color.new(color.gray, 50))
infoTable
table.cell(infoTable, 0, 0, 'NWOG/NDOG Info', text_color = color.white, text_size = size, bgcolor = color.new(color.blue, 70))
table.merge_cells(infoTable, 0, 0, 1, 0)
int nwogCount = showNWOG ? array.size(nwogList) : 0
table.cell(infoTable, 0, 1, 'Active NWOG:', text_color = color.gray, text_size = size)
table.cell(infoTable, 1, 1, str.tostring(nwogCount), text_color = color.white, text_size = size)
int ndogCount = showNDOG ? array.size(ndogList) : 0
table.cell(infoTable, 0, 2, 'Active NDOG:', text_color = color.gray, text_size = size)
table.cell(infoTable, 1, 2, str.tostring(ndogCount), text_color = color.white, text_size = size)
string ehStatus = currentEHActive ? '✓ Active' : '✗ Inactive'
color ehStatusColor = currentEHActive ? color.lime : color.gray
table.cell(infoTable, 0, 3, 'Event Horizon:', text_color = color.gray, text_size = size)
table.cell(infoTable, 1, 3, ehStatus, text_color = ehStatusColor, text_size = size)
string timeToWeekly = getTimeToClose('1W')
table.cell(infoTable, 0, 4, 'Next Week Close:', text_color = color.gray, text_size = size)
table.cell(infoTable, 1, 4, timeToWeekly, text_color = color.white, text_size = size)
string timeToDaily = getTimeToClose('1D')
table.cell(infoTable, 0, 5, 'Next Day Close:', text_color = color.gray, text_size = size)
table.cell(infoTable, 1, 5, timeToDaily, text_color = color.white, text_size = size)