本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
// Key Level Break and Retest [Badr Edge] · v1.0.0
// Educational chart-marking tool. Markers show what happened on a bar. They are not trade signals.
indicator("Key Level Break and Retest [Badr Edge]", shorttitle = "BE Retest", overlay = true, max_labels_count = 50)
const color GOLD = #D9B46C
const color GOLD2 = #F3DEB0
const color TEAL = #4FB8A0
const color ROSE = #E07A6C
const color GREY = #8F939B
const string G_L = "Levels"
usePD = input.bool(true, "Prior day high / low", group = G_L)
useMon = input.bool(true, "Monday high / low", group = G_L, tooltip = "Used from Tuesday until the week ends.")
usePW = input.bool(false, "Prior week high / low", group = G_L)
const string G_R = "Break and retest"
waitBars = input.int(20, "Bars allowed for the retest", minval = 2, maxval = 200, group = G_R, tooltip = "After a candle closes through a level, the script waits this many bars for price to come back to it.")
tolAtr = input.float(0.10, "Touch tolerance (x ATR 14)", minval = 0, step = 0.05, group = G_R, tooltip = "How close price must come to the level to count as a retest.")
const string G_V = "Visuals"
showRetest = input.bool(true, "Retest markers", group = G_V, tooltip = "Triangle: after a close through a level, price came back to the level and the candle closed on the breakout side. A marker shows what happened. It is not a trade signal.")
showFail = input.bool(true, "Failed break markers", group = G_V, tooltip = "Small cross: after a close through a level, a candle closed back on the other side.")
showLabels = input.bool(true, "Level labels", group = G_V)
const string G_Y = "Style"
showWm = input.bool(true, "Watermark", group = G_Y)
const string G_A = "Alerts"
useAlerts = input.bool(true, "Alert on retests and failed breaks", group = G_A, tooltip = "Create the alert with \"Any alert() function call\". Fires once, after the bar closes.")
tfOK = timeframe.isintraday and timeframe.in_seconds() <= 3600
tol = ta.atr(14) * tolAtr
[pdh, pdl] = request.security(syminfo.tickerid, "D", [high[1], low[1]], lookahead = barmerge.lookahead_on)
[pwh, pwl] = request.security(syminfo.tickerid, "W", [high[1], low[1]], lookahead = barmerge.lookahead_on)
// Monday range by trading day (for CME futures Monday starts on Sunday 18:00 New York time)
newTD = ta.change(time_tradingday) != 0
tdIsMon = dayofweek(time_tradingday, "UTC") == dayofweek.monday
var float monH = na
var float monL = na
var int monTD = na
var bool monDone = false
if newTD and tdIsMon
monH := high
monL := low
monTD := time_tradingday
monDone := false
else if tdIsMon and not na(monH)
monH := math.max(monH, high)
monL := math.min(monL, low)
if newTD and not tdIsMon and not na(monH)
monDone := true
monLive = monDone and not na(monTD) and time_tradingday - monTD < 6 * 86400000
mH = monLive ? monH : na
mL = monLive ? monL : na
// State per level: 0 idle, 1 closed above and waiting, -1 closed below and waiting
retest(float lvl, bool on) =>
var int st = 0
var int since = 0
bool up = false
bool dn = false
bool fu = false
bool fd = false
if na(lvl) or lvl != lvl[1] or not on or not tfOK
st := 0
else if barstate.isconfirmed
if st == 0
if close > lvl and close[1] <= lvl
st := 1
since := 0
else if close < lvl and close[1] >= lvl
st := -1
since := 0
else
since += 1
if st == 1
if close < lvl
fu := true
st := 0
else if low <= lvl + tol
up := true
st := 0
else
if close > lvl
fd := true
st := 0
else if high >= lvl - tol
dn := true
st := 0
if st != 0 and since >= waitBars
st := 0
[up, dn, fu, fd]
[u1, d1, fu1, fd1] = retest(pdh, usePD)
[u2, d2, fu2, fd2] = retest(pdl, usePD)
[u3, d3, fu3, fd3] = retest(mH, useMon)
[u4, d4, fu4, fd4] = retest(mL, useMon)
[u5, d5, fu5, fd5] = retest(pwh, usePW)
[u6, d6, fu6, fd6] = retest(pwl, usePW)
upAny = u1 or u2 or u3 or u4 or u5 or u6
dnAny = d1 or d2 or d3 or d4 or d5 or d6
fuAny = fu1 or fu2 or fu3 or fu4 or fu5 or fu6
fdAny = fd1 or fd2 or fd3 or fd4 or fd5 or fd6
show(float v, bool on) => on and tfOK and v == v[1] ? v : na
plot(show(pdh, usePD), "Prior day high", color.new(GOLD, 15), 1, plot.style_linebr)
plot(show(pdl, usePD), "Prior day low", color.new(GOLD, 15), 1, plot.style_linebr)
plot(show(mH, useMon), "Monday high", color.new(GREY, 25), 1, plot.style_linebr)
plot(show(mL, useMon), "Monday low", color.new(GREY, 25), 1, plot.style_linebr)
plot(show(pwh, usePW), "Prior week high", color.new(GOLD2, 35), 1, plot.style_linebr)
plot(show(pwl, usePW), "Prior week low", color.new(GOLD2, 35), 1, plot.style_linebr)
plotshape(showRetest and upAny, "Retest held above a level", shape.triangleup, location.belowbar, color.new(TEAL, 0), size = size.tiny)
plotshape(showRetest and dnAny, "Retest held below a level", shape.triangledown, location.abovebar, color.new(ROSE, 0), size = size.tiny)
plotshape(showFail and fuAny, "Break above failed", shape.xcross, location.abovebar, color.new(GREY, 10), size = size.tiny)
plotshape(showFail and fdAny, "Break below failed", shape.xcross, location.belowbar, color.new(GREY, 10), size = size.tiny)
var array<label> tags = array.new<label>()
tag(float v, string txt, color c, bool on) =>
if on and not na(v)
tags.push(label.new(bar_index + 3, v, txt, style = label.style_label_left, color = color.new(color.black, 100), textcolor = c, size = size.small))
if barstate.islast and showLabels and tfOK
for t in tags
t.delete()
tags.clear()
tag(pdh, "PDH", GOLD, usePD)
tag(pdl, "PDL", GOLD, usePD)
tag(mH, "MON H", GREY, useMon)
tag(mL, "MON L", GREY, useMon)
tag(pwh, "PWH", GOLD2, usePW)
tag(pwl, "PWL", GOLD2, usePW)
if useAlerts
if upAny
alert(syminfo.ticker + " retested a key level from above and closed above it", alert.freq_once_per_bar_close)
if dnAny
alert(syminfo.ticker + " retested a key level from below and closed below it", alert.freq_once_per_bar_close)
if fuAny
alert(syminfo.ticker + " closed back below a key level after breaking above it", alert.freq_once_per_bar_close)
if fdAny
alert(syminfo.ticker + " closed back above a key level after breaking below it", alert.freq_once_per_bar_close)
var table wm = table.new(position.bottom_right, 1, 1)
if barstate.islast and showWm
wm.cell(0, 0, "BADR EDGE", text_color = color.new(GREY, 72), text_size = size.small)
var table note = table.new(position.top_right, 1, 1)
if barstate.islast and not tfOK
note.cell(0, 0, "Key Level Break and Retest works on 1m to 1h charts.", text_color = GOLD, bgcolor = color.new(color.black, 20), text_size = size.small)