本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("KN - Internal & External Structure Dashboard", overlay=true, max_boxes_count=500, max_lines_count=500)
// ═══════════════════════════════════════════════
// 1. INTERNAL TREND SETTINGS
// ═══════════════════════════════════════════════
internalZZ = input.int(2, "Internal ZigZag Period", minval=2, group="Internal Trend")
showInternalZZ = input.bool(true, "Show Internal ZigZag", group="Internal Trend")
internalZZColor = input.color(color.aqua, "Internal ZigZag Color", group="Internal Trend")
// ═══════════════════════════════════════════════
// 2. EXTERNAL TREND SETTINGS
// ═══════════════════════════════════════════════
externalZZ = input.int(10, "External ZigZag Period", minval=2, group="External Trend")
showExternalZZ = input.bool(true, "Show External ZigZag", group="External Trend")
externalZZColor = input.color(color.orange, "External ZigZag Color", group="External Trend")
// ═══════════════════════════════════════════════
// 3. DASHBOARD SETTINGS
// ═══════════════════════════════════════════════
showTable = input.bool(true, "Show Dashboard", group="Dashboard")
dashboardPosition = input.string(
"Middle Right",
"Dashboard Position",
options=["Top Right", "Middle Right", "Bottom Right"],
group="Dashboard")
tablePosition = switch dashboardPosition
"Top Right" => position.top_right
"Middle Right" => position.middle_right
"Bottom Right" => position.bottom_right
// ═══════════════════════════════════════════════
// 4. COLORS
// ═══════════════════════════════════════════════
bullColor = color.lime
bearColor = color.red
waitColor = color.gray
headerColor = color.black
timeframeColor = color.new(color.black, 10)
// ═══════════════════════════════════════════════
// 5. NO-TRADE SESSION — EUROPE/ZURICH
// ═══════════════════════════════════════════════
showNoTradeBox = input.bool(true, "Show No-Trade Session Box", group="Session")
noTradeBoxColor = input.color(color.red, "No-Trade Box Color", group="Session")
noTradeTransparency = input.int(
88,
"No-Trade Background Transparency",
minval=0,
maxval=100,
group="Session")
noTradeStartHour = input.int(
20,
"No-Trade Start Hour",
minval=0,
maxval=23,
group="Session")
noTradeEndHour = input.int(
5,
"No-Trade End Hour",
minval=0,
maxval=23,
group="Session")
noTradeSession = str.tostring(noTradeStartHour, "00") + "00-" + str.tostring(noTradeEndHour, "00") + "00"
sessionNoTrade = not na(time(timeframe.period, noTradeSession, "Europe/Zurich"))
sessionStart = sessionNoTrade and not sessionNoTrade[1]
sessionEnd = not sessionNoTrade and sessionNoTrade[1]
var box noTradeBox = na
var float sessionHigh = na
var float sessionLow = na
// CREATE SESSION BOX
if showNoTradeBox and sessionStart
sessionHigh := high
sessionLow := low
noTradeBox := box.new(
left=bar_index,
top=sessionHigh,
right=bar_index,
bottom=sessionLow,
xloc=xloc.bar_index,
border_color=color.new(noTradeBoxColor, 100),
border_width=0,
bgcolor=color.new(noTradeBoxColor, noTradeTransparency))
// UPDATE SESSION BOX
if showNoTradeBox and sessionNoTrade
sessionHigh := math.max(nz(sessionHigh, high), high)
sessionLow := math.min(nz(sessionLow, low), low)
if not na(noTradeBox)
box.set_right(noTradeBox, bar_index)
box.set_top(noTradeBox, sessionHigh)
box.set_bottom(noTradeBox, sessionLow)
box.set_bgcolor(noTradeBox, color.new(noTradeBoxColor, noTradeTransparency))
box.set_border_color(noTradeBox, color.new(noTradeBoxColor, 100))
box.set_border_width(noTradeBox, 0)
// RESET AFTER SESSION
if sessionEnd
noTradeBox := na
sessionHigh := na
sessionLow := na
// DELETE BOX WHEN DISABLED
if not showNoTradeBox and not na(noTradeBox)
box.delete(noTradeBox)
noTradeBox := na
sessionHigh := na
sessionLow := na
// ═══════════════════════════════════════════════
// 6. VISIBLE ZIGZAG ENGINE
// ═══════════════════════════════════════════════
f_drawZigZag(int period, bool enabled, color zzColor) =>
var float lastPivotPrice = na
var int lastPivotBar = na
var int lastPivotType = 0
var line activeLine = na
var array<line> zzLines = array.new_line()
var bool wasEnabled = enabled
pivotHigh = ta.pivothigh(high, period, period)
pivotLow = ta.pivotlow(low, period, period)
// DELETE DRAWINGS WHEN DISABLED
if not enabled and wasEnabled
while array.size(zzLines) > 0
line.delete(array.pop(zzLines))
lastPivotPrice := na
lastPivotBar := na
lastPivotType := 0
activeLine := na
// DRAW ZIGZAG
if enabled
// PIVOT HIGH
if not na(pivotHigh)
pivotBar = bar_index - period
if lastPivotType == 1
if pivotHigh > lastPivotPrice
lastPivotPrice := pivotHigh
lastPivotBar := pivotBar
if not na(activeLine)
line.set_xy2(activeLine, pivotBar, pivotHigh)
else
if lastPivotType == -1
activeLine := line.new(
lastPivotBar,
lastPivotPrice,
pivotBar,
pivotHigh,
xloc=xloc.bar_index,
color=zzColor,
width=2)
array.push(zzLines, activeLine)
lastPivotPrice := pivotHigh
lastPivotBar := pivotBar
lastPivotType := 1
// PIVOT LOW
if not na(pivotLow)
pivotBar = bar_index - period
if lastPivotType == -1
if pivotLow < lastPivotPrice
lastPivotPrice := pivotLow
lastPivotBar := pivotBar
if not na(activeLine)
line.set_xy2(activeLine, pivotBar, pivotLow)
else
if lastPivotType == 1
activeLine := line.new(
lastPivotBar,
lastPivotPrice,
pivotBar,
pivotLow,
xloc=xloc.bar_index,
color=zzColor,
width=2)
array.push(zzLines, activeLine)
lastPivotPrice := pivotLow
lastPivotBar := pivotBar
lastPivotType := -1
wasEnabled := enabled
// INTERNAL ZIGZAG
f_drawZigZag(internalZZ, showInternalZZ, internalZZColor)
// EXTERNAL ZIGZAG
f_drawZigZag(externalZZ, showExternalZZ, externalZZColor)
// ═══════════════════════════════════════════════
// 7. STRUCTURAL ENGINE
// ═══════════════════════════════════════════════
f_structTrend(int zigzagPeriod) =>
var float lastPH = na
var float prevPH = na
var float lastPL = na
var float prevPL = na
var int direction = 0
pivotHigh = ta.pivothigh(high, zigzagPeriod, zigzagPeriod)
pivotLow = ta.pivotlow(low, zigzagPeriod, zigzagPeriod)
if not na(pivotHigh)
prevPH := lastPH
lastPH := pivotHigh
if not na(pivotLow)
prevPL := lastPL
lastPL := pivotLow
haveHighs = not na(lastPH) and not na(prevPH)
haveLows = not na(lastPL) and not na(prevPL)
bullishStructure = haveHighs and haveLows and lastPH > prevPH and lastPL > prevPL
bearishStructure = haveHighs and haveLows and lastPH < prevPH and lastPL < prevPL
if bullishStructure
direction := 1
if bearishStructure
direction := -1
bullishBOS = not na(lastPH) and close > lastPH
bearishBOS = not na(lastPL) and close < lastPL
if bullishBOS
direction := 1
if bearishBOS
direction := -1
direction
// ═══════════════════════════════════════════════
// 8. MULTI-TIMEFRAME ENGINE
// ═══════════════════════════════════════════════
// 15 MINUTES
int15 = request.security(
syminfo.tickerid, "15",
f_structTrend(internalZZ)[1],
gaps=barmerge.gaps_off,
lookahead=barmerge.lookahead_on)
ext15 = request.security(
syminfo.tickerid, "15",
f_structTrend(externalZZ)[1],
gaps=barmerge.gaps_off,
lookahead=barmerge.lookahead_on)
// 30 MINUTES
int30 = request.security(
syminfo.tickerid, "30",
f_structTrend(internalZZ)[1],
gaps=barmerge.gaps_off,
lookahead=barmerge.lookahead_on)
ext30 = request.security(
syminfo.tickerid, "30",
f_structTrend(externalZZ)[1],
gaps=barmerge.gaps_off,
lookahead=barmerge.lookahead_on)
// 1 HOUR
int1H = request.security(
syminfo.tickerid, "60",
f_structTrend(internalZZ)[1],
gaps=barmerge.gaps_off,
lookahead=barmerge.lookahead_on)
ext1H = request.security(
syminfo.tickerid, "60",
f_structTrend(externalZZ)[1],
gaps=barmerge.gaps_off,
lookahead=barmerge.lookahead_on)
// ═══════════════════════════════════════════════
// 9. TREND TEXT AND COLORS
// ═══════════════════════════════════════════════
f_trendText(int trend) =>
trend == 1 ? "BULLISH" : trend == -1 ? "BEARISH" : "WAITING"
f_trendColor(int trend) =>
trend == 1 ? bullColor : trend == -1 ? bearColor : waitColor
// ═══════════════════════════════════════════════
// 10. STRUCTURAL DASHBOARD
// ═══════════════════════════════════════════════
var table dashboard = na
var string previousPosition = ""
// RECREATE TABLE WHEN POSITION CHANGES
if barstate.islast
if na(dashboard) or previousPosition != dashboardPosition
if not na(dashboard)
table.delete(dashboard)
dashboard := table.new(
tablePosition,
3,
4,
border_width=1,
border_color=color.gray)
previousPosition := dashboardPosition
if showTable
// HEADERS
table.cell(dashboard, 0, 0, "TIMEFRAME",
bgcolor=headerColor,
text_color=color.white)
table.cell(dashboard, 1, 0, "INTERNAL",
bgcolor=headerColor,
text_color=color.white)
table.cell(dashboard, 2, 0, "EXTERNAL",
bgcolor=headerColor,
text_color=color.white)
// 15M
table.cell(dashboard, 0, 1, "15M",
bgcolor=timeframeColor,
text_color=color.white)
table.cell(dashboard, 1, 1, f_trendText(int15),
bgcolor=f_trendColor(int15),
text_color=color.white)
table.cell(dashboard, 2, 1, f_trendText(ext15),
bgcolor=f_trendColor(ext15),
text_color=color.white)
// 30M
table.cell(dashboard, 0, 2, "30M",
bgcolor=timeframeColor,
text_color=color.white)
table.cell(dashboard, 1, 2, f_trendText(int30),
bgcolor=f_trendColor(int30),
text_color=color.white)
table.cell(dashboard, 2, 2, f_trendText(ext30),
bgcolor=f_trendColor(ext30),
text_color=color.white)
// 1H
table.cell(dashboard, 0, 3, "1H",
bgcolor=timeframeColor,
text_color=color.white)
table.cell(dashboard, 1, 3, f_trendText(int1H),
bgcolor=f_trendColor(int1H),
text_color=color.white)
table.cell(dashboard, 2, 3, f_trendText(ext1H),
bgcolor=f_trendColor(ext1H),
text_color=color.white)
else
table.clear(dashboard, 0, 0, 2, 3)