本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
// Consolidation Box [Badr Edge] · v1.0.0
// Educational chart-marking tool. Markers show what happened on a bar. They are not trade signals.
indicator("Consolidation Box [Badr Edge]", shorttitle = "BE Box", overlay = true, max_boxes_count = 200, max_lines_count = 200)
const color GOLD = #D9B46C
const color TEAL = #4FB8A0
const color ROSE = #E07A6C
const color GREY = #8F939B
const string G_B = "Box"
boxLen = input.int(20, "Minimum box length (bars)", minval = 5, maxval = 200, group = G_B)
tightAtr = input.float(2.5, "Maximum box height (x ATR 50)", minval = 0.5, step = 0.25, group = G_B, tooltip = "A box is drawn when the whole high-to-low range of the last bars fits inside this many average candle ranges. Lower values find fewer, tighter boxes.")
maxLen = input.int(120, "Give up after (bars)", minval = 20, maxval = 1000, group = G_B, tooltip = "A box that has not broken after this many bars is closed without a marker.")
failBars = input.int(5, "Bars to judge a failed breakout", minval = 1, maxval = 50, group = G_B)
const string G_V = "Visuals"
showMid = input.bool(false, "Box midline", group = G_V)
showMarks = input.bool(true, "Breakout markers", group = G_V, tooltip = "Triangle: the first candle that closes outside the box. A marker shows what happened. It is not a trade signal.")
showFail = input.bool(true, "Failed breakout markers", group = G_V, tooltip = "Small cross: price closed back inside the box within the set number of bars.")
const string G_Y = "Style"
colBox = input.color(GOLD, "Box", group = G_Y)
showWm = input.bool(true, "Watermark", group = G_Y)
const string G_A = "Alerts"
useAlerts = input.bool(true, "Alert on breakouts and failed breakouts", group = G_A, tooltip = "Create the alert with \"Any alert() function call\". Fires once, after the bar closes.")
atr = ta.atr(50)
hh = ta.highest(high, boxLen)
ll = ta.lowest(low, boxLen)
// state: 0 searching, 1 box open, 2 closed above and watching, -2 closed below and watching
var int state = 0
var int since = 0
var int born = 0
var int quiet = 0
var float top = na
var float bot = na
var box bx = na
var line md = na
bool brkUp = false
bool brkDn = false
bool failUp = false
bool failDn = false
if barstate.isconfirmed
if state == 0
quiet += 1
if quiet > 3 and not na(atr) and hh - ll <= tightAtr * atr
state := 1
born := bar_index
top := hh
bot := ll
bx := box.new(bar_index - boxLen + 1, top, bar_index, bot, border_color = color.new(colBox, 40), border_width = 1, bgcolor = color.new(colBox, 92))
if showMid
md := line.new(bar_index - boxLen + 1, (top + bot) / 2, bar_index, (top + bot) / 2, color = color.new(colBox, 60), style = line.style_dotted)
else if state == 1
bx.set_right(bar_index)
if showMid
md.set_x2(bar_index)
if close > top
brkUp := true
state := 2
since := 0
else if close < bot
brkDn := true
state := -2
since := 0
else if bar_index - born > maxLen
state := 0
quiet := 0
else
since += 1
bool inside = close <= top and close >= bot
if inside
failUp := state == 2
failDn := state == -2
if inside or since >= failBars
state := 0
quiet := 0
plotshape(showMarks and brkUp, "Close above the box", shape.triangleup, location.belowbar, color.new(TEAL, 0), size = size.tiny)
plotshape(showMarks and brkDn, "Close below the box", shape.triangledown, location.abovebar, color.new(ROSE, 0), size = size.tiny)
plotshape(showFail and failUp, "Breakout above failed", shape.xcross, location.abovebar, color.new(GREY, 10), size = size.tiny)
plotshape(showFail and failDn, "Breakout below failed", shape.xcross, location.belowbar, color.new(GREY, 10), size = size.tiny)
plot(state != 0 ? top : na, "Box top", display = display.data_window)
plot(state != 0 ? bot : na, "Box bottom", display = display.data_window)
if useAlerts
if brkUp
alert(syminfo.ticker + " closed above a consolidation box at " + str.tostring(top, format.mintick), alert.freq_once_per_bar_close)
if brkDn
alert(syminfo.ticker + " closed below a consolidation box at " + str.tostring(bot, format.mintick), alert.freq_once_per_bar_close)
if failUp or failDn
alert(syminfo.ticker + " closed back inside a consolidation box after breaking out", 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)