本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("2-Candle Alternating BOS", overlay=true)
// --- Settings ---
wickBreak = input.bool(false, "Allow Wick Break (Default: False = Body Close)")
// --- Detect 2-Candle Pivot Formations ---
isPeakPattern = (close[2] > open[2]) and (close[1] < open[1])
isValleyPattern = (close[2] < open[2]) and (close[1] > open[1])
peakLevel = math.max(high[1], high[2])
valleyLevel = math.min(low[1], low[2])
var float activePeak = na
var float activeValley = na
// State tracker: 0 = neutral, 1 = uptrend, -1 = downtrend
var int currentTrend = 0
if isPeakPattern
activePeak := peakLevel
if isValleyPattern
activeValley := valleyLevel
// --- Breakout Logic ---
priceBreakUp = wickBreak ? high : close
priceBreakDown = wickBreak ? low : close
// Can only trigger Bullish BOS if not already in an uptrend
bullishBOS = (currentTrend != 1) and not na(activePeak) and ta.crossover(priceBreakUp, activePeak)
// Can only trigger Bearish BOS if not already in a downtrend
bearishBOS = (currentTrend != -1) and not na(activeValley) and ta.crossunder(priceBreakDown, activeValley)
// --- State Updates ---
if bullishBOS
currentTrend := 1
activePeak := na
if bearishBOS
currentTrend := -1
activeValley := na
// --- Vertical-Locked Native Plots ---
plotshape(
bullishBOS,
title="Bullish BOS",
style=shape.labeldown,
location=location.abovebar,
color=color.green,
textcolor=color.white,
text="BOS ↑",
size=size.tiny
)
plotshape(
bearishBOS,
title="Bearish BOS",
style=shape.labelup,
location=location.belowbar,
color=color.red,
textcolor=color.white,
text="BOS ↓",
size=size.tiny
)
// --- Alerts ---
alertcondition(bullishBOS, "Bullish BOS", "Bullish BOS confirmed on {{ticker}} at {{close}}")
alertcondition(bearishBOS, "Bearish BOS", "Bearish BOS confirmed on {{ticker}} at {{close}}")