本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Jos-ProTrader
//@version=6
indicator("Renko Range Breakout Signals ", shorttitle="RRB Setup", overlay=true, max_boxes_count=200, max_labels_count=200)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// INPUTS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
gRange = "1. Range Detection"
rangeLen = input.int(12, "Range Lookback", minval=4, maxval=100, group=gRange)
atrLen = input.int(14, "ATR Length", minval=2, group=gRange)
maxRangeATR = input.float(4.0, "Maximum Range Width (ATR)", minval=0.5, step=0.25, group=gRange)
useAtrFilter = input.bool(false, "Enable ATR Compression Filter", group=gRange)
atrCompression = input.float(0.90, "ATR Compression Threshold", minval=0.2, maxval=2.0, step=0.05, group=gRange)
gBreak = "2. Breakout Settings"
bufferATR = input.float(0.05, "Breakout Buffer (ATR)", minval=0.0, step=0.01, group=gBreak)
confirmClose = input.bool(true, "Require Candle Close Confirmation", group=gBreak)
showRetests = input.bool(true, "Show Breakout Retests", group=gBreak)
retestBars = input.int(10, "Retest Validity (Bars)", minval=1, maxval=50, group=gBreak)
retestTolerance = input.float(0.20, "Retest Tolerance (ATR)", minval=0.05, step=0.05, group=gBreak)
gVisual = "3. Chart Appearance"
showBox = input.bool(true, "Show Range Boxes", group=gVisual)
showMid = input.bool(true, "Show Range Midline", group=gVisual)
showSignals = input.bool(true, "Show BUY / SELL Labels", group=gVisual)
showBreakLines = input.bool(true, "Show Broken Range Levels", group=gVisual)
bullColor = input.color(color.lime, "Bullish Color", group=gVisual)
bearColor = input.color(color.red, "Bearish Color", group=gVisual)
neutralColor = input.color(color.orange, "Consolidation Color", group=gVisual)
boxTransparency = input.int(87, "Box Transparency", minval=0, maxval=100, group=gVisual)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// VOLATILITY AND RANGE CALCULATION
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
atr = ta.atr(atrLen)
atrAverage = ta.sma(atr, atrLen * 3)
rangeTop = ta.highest(high, rangeLen)
rangeBottom = ta.lowest(low, rangeLen)
rangeWidth = rangeTop - rangeBottom
enoughData = bar_index >= rangeLen + atrLen
atrCompressed = not useAtrFilter or (not na(atrAverage) and atr <= atrAverage * atrCompression)
consolidating = enoughData and not na(atr) and atr > 0 and rangeWidth <= atr * maxRangeATR and atrCompressed
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// RANGE STATE
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
var bool rangeActive = false
var float lockedHigh = na
var float lockedLow = na
var float lockedMid = na
var box activeBox = na
var int lastBreakBar = na
var float lastBreakLevel = na
var int breakoutDirection = 0
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// SIGNAL VARIABLES
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
bool buySignal = false
bool sellSignal = false
bool buyRetest = false
bool sellRetest = false
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// RANGE CREATION AND MANAGEMENT
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
if barstate.isconfirmed
// Start a new consolidation range.
if not rangeActive and consolidating and (na(lastBreakBar) or bar_index - lastBreakBar > 2)
rangeActive := true
lockedHigh := rangeTop
lockedLow := rangeBottom
lockedMid := math.avg(lockedHigh, lockedLow)
if showBox
activeBox := box.new(
left=bar_index - rangeLen + 1,
top=lockedHigh,
right=bar_index,
bottom=lockedLow,
xloc=xloc.bar_index,
border_color=neutralColor,
border_width=1,
bgcolor=color.new(neutralColor, boxTransparency))
// Update boundaries while consolidation continues.
else if rangeActive and consolidating
lockedHigh := math.max(lockedHigh, rangeTop)
lockedLow := math.min(lockedLow, rangeBottom)
lockedMid := math.avg(lockedHigh, lockedLow)
if showBox and not na(activeBox)
box.set_right(activeBox, bar_index)
box.set_top(activeBox, lockedHigh)
box.set_bottom(activeBox, lockedLow)
// Check breakout when consolidation ends.
else if rangeActive
float breakoutBuffer = atr * bufferATR
bool bullishBreak = close > lockedHigh + breakoutBuffer and (not confirmClose or close > open)
bool bearishBreak = close < lockedLow - breakoutBuffer and (not confirmClose or close < open)
if bullishBreak
buySignal := true
breakoutDirection := 1
lastBreakLevel := lockedHigh
lastBreakBar := bar_index
if showBox and not na(activeBox)
box.set_right(activeBox, bar_index)
box.set_border_color(activeBox, bullColor)
box.set_bgcolor(activeBox, color.new(bullColor, boxTransparency))
rangeActive := false
else if bearishBreak
sellSignal := true
breakoutDirection := -1
lastBreakLevel := lockedLow
lastBreakBar := bar_index
if showBox and not na(activeBox)
box.set_right(activeBox, bar_index)
box.set_border_color(activeBox, bearColor)
box.set_bgcolor(activeBox, color.new(bearColor, boxTransparency))
rangeActive := false
else
// Consolidation ended without a confirmed breakout.
if showBox and not na(activeBox)
box.set_right(activeBox, bar_index)
rangeActive := false
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// BREAKOUT RETEST ENGINE
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
if barstate.isconfirmed and showRetests and not na(lastBreakBar) and not na(lastBreakLevel)
int breakoutAge = bar_index - lastBreakBar
float tolerance = atr * retestTolerance
if breakoutAge > 0 and breakoutAge <= retestBars
buyRetest := breakoutDirection == 1 and low <= lastBreakLevel + tolerance and low >= lastBreakLevel - tolerance and close > lastBreakLevel and close > open
sellRetest := breakoutDirection == -1 and high >= lastBreakLevel - tolerance and high <= lastBreakLevel + tolerance and close < lastBreakLevel and close < open
if buyRetest or sellRetest
lastBreakBar := na
lastBreakLevel := na
breakoutDirection := 0
else if breakoutAge > retestBars
lastBreakBar := na
lastBreakLevel := na
breakoutDirection := 0
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// CHART DISPLAY
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
plot(showBox and rangeActive ? lockedHigh : na, "Active Range High", color=neutralColor, linewidth=2, style=plot.style_linebr)
plot(showBox and rangeActive ? lockedLow : na, "Active Range Low", color=neutralColor, linewidth=2, style=plot.style_linebr)
plot(showMid and rangeActive ? lockedMid : na, "Range Equilibrium", color=color.new(color.white, 15), linewidth=1, style=plot.style_linebr)
plot(showBreakLines and breakoutDirection != 0 ? lastBreakLevel : na, "Breakout Retest Level", color=breakoutDirection == 1 ? bullColor : bearColor, linewidth=1, style=plot.style_linebr)
plotshape(showSignals and buySignal, title="BUY Breakout", style=shape.labelup, location=location.belowbar, color=bullColor, text="BUY", textcolor=color.white, size=size.small)
plotshape(showSignals and sellSignal, title="SELL Breakout", style=shape.labeldown, location=location.abovebar, color=bearColor, text="SELL", textcolor=color.white, size=size.small)
plotshape(showRetests and buyRetest, title="Bullish Retest", style=shape.triangleup, location=location.belowbar, color=bullColor, text="RETEST", textcolor=bullColor, size=size.tiny)
plotshape(showRetests and sellRetest, title="Bearish Retest", style=shape.triangledown, location=location.abovebar, color=bearColor, text="RETEST", textcolor=bearColor, size=size.tiny)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// ALERTS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
alertcondition(buySignal, "RRB - BUY", "Bullish range breakout on {{ticker}} at {{close}}")
alertcondition(sellSignal, "RRB - SELL", "Bearish range breakout on {{ticker}} at {{close}}")
alertcondition(buyRetest, "RRB - Bullish Retest", "Bullish breakout retest on {{ticker}} at {{close}}")
alertcondition(sellRetest, "RRB - Bearish Retest", "Bearish breakout retest on {{ticker}} at {{close}}")