本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Breakout Trend Zones 100/50", overlay = true, max_labels_count = 500)
// ---------- Inputs ----------
zoneTf = input.timeframe("240", "Zone timeframe (tested on 4H)")
entryLen = input.int(100, "Zone start: breakout of (candles)")
exitLen = input.int(50, "Zone end: break of (candles)")
showLines = input.bool(true, "Show trend-break line")
showChan = input.bool(false, "Show breakout channel")
showLbl = input.bool(true, "Show zone start labels")
showTable = input.bool(true, "Show info table")
grnCol = input.color(color.new(color.green, 85), "Green zone color")
redCol = input.color(color.new(color.red, 85), "Red zone color")
// ---------- Zone engine ----------
// state: 1 = green zone, -1 = red zone, 0 = no zone
f_state(shift) =>
var int st = 0
hiE = ta.highest(high, entryLen)[1]
loE = ta.lowest(low, entryLen)[1]
hiX = ta.highest(high, exitLen)[1]
loX = ta.lowest(low, exitLen)[1]
if close > hiE
st := 1
else if close < loE
st := -1
else if st == 1 and close < loX
st := 0
else if st == -1 and close > hiX
st := 0
[st[shift], hiE[shift], loE[shift], loX[shift], hiX[shift]]
// Higher timeframe: confirmed (closed) candle only, no repaint
[sH, hiEH, loEH, loXH, hiXH] = request.security(syminfo.tickerid, zoneTf, f_state(1),
lookahead = barmerge.lookahead_on)
// Same timeframe: compute on the chart directly
[sL, hiEL, loEL, loXL, hiXL] = f_state(0)
sameTf = timeframe.period == zoneTf
state = sameTf ? sL : sH
hiE = sameTf ? hiEL : hiEH
loE = sameTf ? loEL : loEH
loX = sameTf ? loXL : loXH
hiX = sameTf ? hiXL : hiXH
tfTooHigh = timeframe.in_seconds() > timeframe.in_seconds(zoneTf)
// ---------- Drawing ----------
bgcolor(state == 1 ? grnCol : state == -1 ? redCol : na, title = "Zone background")
breakLine = state == 1 ? loX : state == -1 ? hiX : na
plot(showLines ? breakLine : na, "Trend-break line",
color = state == 1 ? color.green : color.red, style = plot.style_linebr, linewidth = 2)
plot(showChan ? hiE : na, "Breakout channel high", color = color.new(color.green, 50))
plot(showChan ? loE : na, "Breakout channel low", color = color.new(color.red, 50))
greenStart = state == 1 and state[1] != 1
redStart = state == -1 and state[1] != -1
zoneEnd = state == 0 and state[1] != 0
if showLbl and greenStart
label.new(bar_index, low, "Green zone", style = label.style_label_up,
color = color.new(color.green, 0), textcolor = color.white, size = size.tiny)
if showLbl and redStart
label.new(bar_index, high, "Red zone", style = label.style_label_down,
color = color.new(color.red, 0), textcolor = color.white, size = size.tiny)
// ---------- Info table ----------
var table t = table.new(position.top_right, 1, 5, bgcolor = color.new(color.black, 20))
if showTable and barstate.islast
zoneTxt = state == 1 ? "GREEN zone (up-trend)" : state == -1 ? "RED zone (down-trend)" : "No zone"
zoneClr = state == 1 ? color.green : state == -1 ? color.red : color.gray
table.cell(t, 0, 0, "Breakout Trend Zones (" + zoneTf + ")", text_color = color.white, text_size = size.small)
table.cell(t, 0, 1, "Now: " + zoneTxt, text_color = zoneClr, text_size = size.small)
table.cell(t, 0, 2, "Starts: close beyond " + str.tostring(entryLen) + "-candle high/low", text_color = color.white, text_size = size.small)
table.cell(t, 0, 3, "Ends: close beyond " + str.tostring(exitLen) + "-candle low/high", text_color = color.white, text_size = size.small)
table.cell(t, 0, 4, tfTooHigh ? "Use a chart TF <= zone TF" : "Zones only, not signals", text_color = tfTooHigh ? color.orange : color.gray, text_size = size.small)
// ---------- Alerts ----------
alertcondition(greenStart, "Green zone started", "Green zone started (up-trend breakout)")
alertcondition(redStart, "Red zone started", "Red zone started (down-trend breakout)")
alertcondition(zoneEnd, "Zone ended", "Trend zone ended")