本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Bias Pause Break", shorttitle="BPB", overlay=true, max_boxes_count=500)
// SETTINGS
emaLength = input.int(20, "EMA Length", minval=1)
emaSource = input.source(ohlc4, "EMA Source")
atrLength = input.int(14, "ATR Length", minval=1)
minDistance = input.float(0.0, "Minimum pause distance from EMA (ATR)", minval=0.0, step=0.05)
minImpulse = input.float(0.0, "Minimum impulse body (ATR)", minval=0.0, step=0.1)
maxPauseRatio = input.float(1.0, "Max pause body / impulse body", minval=0.1, maxval=2.0, step=0.1)
maxPauseCandles = input.int(2, "Maximum pause candles", minval=1, maxval=2)
validationCandles = input.int(3, "Candles to monitor after pause", minval=1, maxval=10)
// OVEREXTENSION
overextensionATR = input.float(1.5, "Overextended distance (ATR)", minval=0.1, step=0.1)
requireSlope = input.bool(false, "Require EMA slope")
showEMA = input.bool(true, "Show EMA")
enablePauseAlerts = input.bool(true, "Enable pause alerts")
enableBreakAlerts = input.bool(true, "Enable breakout alerts")
alertOverextended = input.bool(true, "Include overextended setups in alerts")
bullishColor = input.color(color.teal, "Bullish box")
bearishColor = input.color(color.red, "Bearish box")
extendedColor = input.color(color.orange, "Overextended box")
// EMA AND ATR
ema20 = ta.ema(emaSource, emaLength)
atr = ta.atr(atrLength)
plot(showEMA ? ema20 : na, title="EMA 20 OHLC4", color=color.rgb(65, 145, 255), linewidth=2)
// MULTIPLE INDEPENDENT SETUPS
var array<int> directions = array.new_int()
var array<int> startBars = array.new_int()
var array<int> pauseCounts = array.new_int()
var array<int> monitorCounts = array.new_int()
var array<float> firstHighs = array.new_float()
var array<float> firstLows = array.new_float()
var array<float> pauseHighs = array.new_float()
var array<float> pauseLows = array.new_float()
var array<float> impulseBodies = array.new_float()
var array<float> pauseGaps = array.new_float()
var array<bool> breakoutFlags = array.new_bool()
var array<bool> extendedFlags = array.new_bool()
var array<box> boxes = array.new_box()
// Tracks realtime breakout notifications per setup.
varip array<int> alertedStarts = array.new_int()
// CANDLE MEASUREMENTS
previousBody = math.abs(close[1] - open[1])
currentBody = math.abs(close - open)
impulseLargeEnough = previousBody >= atr[1] * minImpulse
pauseSmallEnough = currentBody <= previousBody * maxPauseRatio
// IMPULSE RULES
// The impulse can touch or cross EMA.
// Only its close must be on the correct side.
bullishImpulse = close[1] > open[1] and close[1] > ema20[1]
bearishImpulse = close[1] < open[1] and close[1] < ema20[1]
// PAUSE RULES
// Neither body nor wick may touch EMA.
bullishPauseClear = low > ema20 + atr * minDistance
bearishPauseClear = high < ema20 - atr * minDistance
bullishSlope = not requireSlope or ema20 > ema20[1]
bearishSlope = not requireSlope or ema20 < ema20[1]
validBullishPause = bullishImpulse and close < open and impulseLargeEnough and pauseSmallEnough and bullishPauseClear and bullishSlope
validBearishPause = bearishImpulse and close > open and impulseLargeEnough and pauseSmallEnough and bearishPauseClear and bearishSlope
// INTRABAR BREAKOUT ALERTS
if barstate.isrealtime and enableBreakAlerts and array.size(directions) > 0
for i = 0 to array.size(directions) - 1
int dir = array.get(directions, i)
int start = array.get(startBars, i)
float level = dir == 1 ? array.get(firstHighs, i) : array.get(firstLows, i)
bool crossed = dir == 1 ? high > level : low < level
bool emaTouched = dir == 1 ? low <= ema20 : high >= ema20
bool alreadyAlerted = array.includes(alertedStarts, start)
bool alreadyBroken = array.get(breakoutFlags, i)
bool extended = array.get(extendedFlags, i)
if crossed and not emaTouched and not alreadyAlerted and not alreadyBroken and (alertOverextended or not extended)
array.push(alertedStarts, start)
// Prevent unlimited alert-history growth.
if array.size(alertedStarts) > 500
array.shift(alertedStarts)
string side = dir == 1 ? "LONG" : "SHORT"
string warning = extended ? " | OVEREXTENDED" : ""
string message = "BPB BREAKOUT " + side + warning + " | " + syminfo.ticker + " | TF: " + timeframe.period + " | Level: " + str.tostring(level, format.mintick)
alert(message, alert.freq_all)
// PROCESS CONFIRMED CANDLES
if barstate.isconfirmed
// UPDATE EXISTING SETUPS
if array.size(directions) > 0
for i = array.size(directions) - 1 to 0
int dir = array.get(directions, i)
int start = array.get(startBars, i)
int count = array.get(pauseCounts, i)
int monitored = array.get(monitorCounts, i)
float firstHigh = array.get(firstHighs, i)
float firstLow = array.get(firstLows, i)
float pHigh = array.get(pauseHighs, i)
float pLow = array.get(pauseLows, i)
float iBody = array.get(impulseBodies, i)
float pGap = array.get(pauseGaps, i)
bool broke = array.get(breakoutFlags, i)
box currentBox = array.get(boxes, i)
// BREAKOUT: FIRST PAUSE CANDLE LEVEL
bool breakoutNow = dir == 1 ? high > firstHigh : low < firstLow
// EMA TOUCH: WICK CONTACT INVALIDATES
bool emaTouched = dir == 1 ? low <= ema20 : high >= ema20
// POSSIBLE SECOND PAUSE
bool opposite = dir == 1 ? close < open : close > open
bool sizeOK = currentBody <= iBody * maxPauseRatio
bool emaOK = dir == 1 ? bullishPauseClear : bearishPauseClear
bool validSecond = count == 1 and monitored == 0 and bar_index == start + 1 and maxPauseCandles == 2 and not breakoutNow and opposite and sizeOK and emaOK
if validSecond
// INCLUDE SECOND PAUSE CANDLE
pHigh := math.max(pHigh, high)
pLow := math.min(pLow, low)
array.set(pauseCounts, i, 2)
array.set(pauseHighs, i, pHigh)
array.set(pauseLows, i, pLow)
// Recalculate distance using closest pause wick.
float secondGap = dir == 1 ? low - ema20 : ema20 - high
pGap := math.min(pGap, secondGap)
array.set(pauseGaps, i, pGap)
// Final pause classification.
bool extended = pGap >= atr * overextensionATR
array.set(extendedFlags, i, extended)
color boxColor = extended ? extendedColor : (dir == 1 ? bullishColor : bearishColor)
box.set_border_color(currentBox, boxColor)
box.set_bgcolor(currentBox, color.new(boxColor, 85))
// Only pause candles determine box height.
box.set_right(currentBox, bar_index + 1)
box.set_top(currentBox, pHigh)
box.set_bottom(currentBox, pLow)
else
// MONITOR FOLLOWING CANDLES
monitored += 1
array.set(monitorCounts, i, monitored)
// Extend horizontally without changing height.
box.set_right(currentBox, bar_index + 1)
// Invalidation requires a CLOSE beyond pause.
bool invalidClose = dir == 1 ? close < pLow : close > pHigh
// Or ANY contact with EMA.
bool invalidSetup = invalidClose or emaTouched
if invalidSetup
// DELETE INVALID BOX
box.delete(currentBox)
else
// Record successful breakout.
if breakoutNow
broke := true
array.set(breakoutFlags, i, true)
// End monitoring after specified window.
if monitored >= validationCandles
if not broke
box.delete(currentBox)
// Remove invalid or completed setup state.
if invalidSetup or monitored >= validationCandles
array.remove(directions, i)
array.remove(startBars, i)
array.remove(pauseCounts, i)
array.remove(monitorCounts, i)
array.remove(firstHighs, i)
array.remove(firstLows, i)
array.remove(pauseHighs, i)
array.remove(pauseLows, i)
array.remove(impulseBodies, i)
array.remove(pauseGaps, i)
array.remove(breakoutFlags, i)
array.remove(extendedFlags, i)
array.remove(boxes, i)
// DETECT NEW SETUPS INDEPENDENTLY
if validBullishPause or validBearishPause
int dir = validBullishPause ? 1 : -1
// Distance from closest wick to EMA.
float gap = dir == 1 ? low - ema20 : ema20 - high
// Overextended when gap >= 1.5 ATR by default.
bool extended = gap >= atr * overextensionATR
color boxColor = extended ? extendedColor : (dir == 1 ? bullishColor : bearishColor)
// Box includes pause only, never the impulse.
box newBox = box.new(
left=bar_index,
top=high,
right=bar_index + 1,
bottom=low,
xloc=xloc.bar_index,
border_color=boxColor,
border_width=1,
bgcolor=color.new(boxColor, 85))
array.push(directions, dir)
array.push(startBars, bar_index)
array.push(pauseCounts, 1)
array.push(monitorCounts, 0)
array.push(firstHighs, high)
array.push(firstLows, low)
array.push(pauseHighs, high)
array.push(pauseLows, low)
array.push(impulseBodies, previousBody)
array.push(pauseGaps, gap)
array.push(breakoutFlags, false)
array.push(extendedFlags, extended)
array.push(boxes, newBox)
// PAUSE FORMATION ALERT
if enablePauseAlerts and barstate.isrealtime and (alertOverextended or not extended)
string side = dir == 1 ? "BULLISH" : "BEARISH"
string warning = extended ? " | OVEREXTENDED" : ""
float level = dir == 1 ? high : low
string message = "BPB PAUSE FORMED " + side + warning + " | " + syminfo.ticker + " | TF: " + timeframe.period + " | Break level: " + str.tostring(level, format.mintick)
alert(message, alert.freq_all)