本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Pine Scanner Indicator: QRB Long Breakout Radar", overlay=false)
//==============================================================================
// PINE SCANNER INDICATOR: QRB LONG BREAKOUT RADAR
// FINAL STOCK v1
//
// PURPOSE:
// Screening/discovery only. Designed for TradingView Pine Screener,
// not for trade execution.
//
// CORE IDEA:
// Scan for LONG Quarterly Range Breakout (QRB) opportunities using the previous
// COMPLETED calendar quarter as a fixed reference range.
//
// STATUS MAP:
// -1 = Not Eligible
// 0 = Below Midline
// 1 = Early Watch
// 2 = QRB Building
// 3 = QRB Pre-Breakout Ready
// 4 = QRB Fresh Breakout
// 5 = QRB Retest
// 6 = QRB Expansion
// 7 = QRB Returned Inside
// 8 = Ready Zone but Falling / Flat
//==============================================================================
//------------------------------------------------------------------------------
// Inputs
//------------------------------------------------------------------------------
groupQRB = "QRB Settings"
readyZonePct = input.float(
10.0,
"Ready Zone (% of QRB Range)",
minval=1.0,
maxval=30.0,
step=0.5,
group=groupQRB)
retestZonePct = input.float(
5.0,
"Retest Zone (+/- % of QRB Range)",
minval=0.5,
maxval=20.0,
step=0.5,
group=groupQRB)
retestLookback = input.int(
15,
"Retest Lookback (Trading Days)",
minval=1,
maxval=60,
group=groupQRB)
approachLookback = input.int(
5,
"Approach Direction Lookback (Trading Days)",
minval=1,
maxval=30,
group=groupQRB)
groupQuality = "Current Eligibility Filter"
minPrice = input.float(
10.0,
"Minimum Price",
minval=0.0,
step=1.0,
group=groupQuality)
minAvgVolume = input.int(
500000,
"Minimum Average Daily Volume",
minval=0,
step=50000,
group=groupQuality)
volumeLength = input.int(
20,
"Average Volume Length",
minval=1,
maxval=100,
group=groupQuality)
//------------------------------------------------------------------------------
// Previous completed calendar-quarter levels
//------------------------------------------------------------------------------
f_previousQuarter() =>
bool newQuarterMonth = month == 1 or month == 4 or month == 7 or month == 10
float priorThreeMonthHigh = ta.highest(high[1], 3)
float priorThreeMonthLow = ta.lowest(low[1], 3)
float previousQuarterHigh =
ta.valuewhen(newQuarterMonth, priorThreeMonthHigh, 0)
float previousQuarterLow =
ta.valuewhen(newQuarterMonth, priorThreeMonthLow, 0)
[previousQuarterHigh, previousQuarterLow]
[qrbHigh, qrbLow] = request.security(
syminfo.tickerid,
"1M",
f_previousQuarter(),
gaps=barmerge.gaps_off,
lookahead=barmerge.lookahead_on)
//------------------------------------------------------------------------------
// Core QRB math
//------------------------------------------------------------------------------
validRange =
not na(qrbHigh) and
not na(qrbLow) and
qrbHigh > qrbLow
qrbRange =
validRange ? qrbHigh - qrbLow : na
qrbMidline =
validRange ? (qrbHigh + qrbLow) / 2.0 : na
qrbPositionPct =
validRange
? ((close - qrbLow) / qrbRange) * 100.0
: na
distanceToHighPct =
validRange
? ((qrbHigh - close) / qrbRange) * 100.0
: na
readyStartPct =
100.0 - readyZonePct
//------------------------------------------------------------------------------
// Current eligibility gate
//
// This determines whether a stock is eligible TODAY.
// It does NOT control breakout history.
//------------------------------------------------------------------------------
avgDailyVolume =
ta.sma(volume, volumeLength)
eligible =
validRange and
close >= minPrice and
avgDailyVolume >= minAvgVolume
//------------------------------------------------------------------------------
// Calendar-quarter tracking
//------------------------------------------------------------------------------
newCalendarQuarter =
month != month[1] and
(month == 1 or month == 4 or month == 7 or month == 10)
// First daily bar of the quarter = Day 1.
daysIntoQuarterRaw =
ta.barssince(newCalendarQuarter)
daysIntoQuarter =
not na(daysIntoQuarterRaw)
? daysIntoQuarterRaw + 1
: na
//------------------------------------------------------------------------------
// Breakout history — independent of eligibility
//------------------------------------------------------------------------------
var bool breakoutSeenThisQuarter = false
var int firstBreakoutBarIndex = na
if newCalendarQuarter
breakoutSeenThisQuarter := false
firstBreakoutBarIndex := na
seenBeforeToday =
breakoutSeenThisQuarter
rawFirstBreakout =
validRange and
not seenBeforeToday and
close > qrbHigh
if rawFirstBreakout
breakoutSeenThisQuarter := true
firstBreakoutBarIndex := bar_index
barsSinceFirstBreakout =
not na(firstBreakoutBarIndex)
? bar_index - firstBreakoutBarIndex
: na
freshBreakout =
eligible and
rawFirstBreakout
//------------------------------------------------------------------------------
// Approach Direction
//
// +1 = Rising
// 0 = Flat
// -1 = Falling
//------------------------------------------------------------------------------
priceChange =
close - close[approachLookback]
float approachDirection = na
if eligible and not na(priceChange)
if priceChange > 0
approachDirection := 1.0
else if priceChange < 0
approachDirection := -1.0
else
approachDirection := 0.0
approachRising =
approachDirection == 1.0
//------------------------------------------------------------------------------
// Midline Cross
//------------------------------------------------------------------------------
midlineCross =
eligible and
ta.crossover(close, qrbMidline)
//------------------------------------------------------------------------------
// QRB Retest
//------------------------------------------------------------------------------
retestTolerance =
validRange
? qrbRange * (retestZonePct / 100.0)
: na
touchedRetestZone =
eligible and
breakoutSeenThisQuarter and
low <= qrbHigh + retestTolerance and
high >= qrbHigh - retestTolerance
qrbRetest =
eligible and
breakoutSeenThisQuarter and
not rawFirstBreakout and
not na(barsSinceFirstBreakout) and
barsSinceFirstBreakout >= 1 and
barsSinceFirstBreakout <= retestLookback and
touchedRetestZone and
close >= qrbHigh
//------------------------------------------------------------------------------
// Returned Inside
//------------------------------------------------------------------------------
returnedInside =
eligible and
breakoutSeenThisQuarter and
not rawFirstBreakout and
not qrbRetest and
close <= qrbHigh
//------------------------------------------------------------------------------
// Expansion
//------------------------------------------------------------------------------
qrbExpansion =
eligible and
breakoutSeenThisQuarter and
close > qrbHigh and
not rawFirstBreakout and
not qrbRetest
//------------------------------------------------------------------------------
// QRB Pre-Breakout Ready
//------------------------------------------------------------------------------
preBreakoutReady =
eligible and
not breakoutSeenThisQuarter and
qrbPositionPct >= readyStartPct and
qrbPositionPct <= 100.0 and
approachRising
//------------------------------------------------------------------------------
// Status Code
//------------------------------------------------------------------------------
float statusCode = -1.0
if eligible
if freshBreakout
statusCode := 4.0
else if qrbRetest
statusCode := 5.0
else if qrbExpansion
statusCode := 6.0
else if returnedInside
statusCode := 7.0
else if preBreakoutReady
statusCode := 3.0
else if (
not breakoutSeenThisQuarter and
qrbPositionPct >= readyStartPct and
qrbPositionPct <= 100.0
)
statusCode := 8.0
else if qrbPositionPct >= 70.0
statusCode := 2.0
else if qrbPositionPct >= 50.0
statusCode := 1.0
else
statusCode := 0.0
//------------------------------------------------------------------------------
// Pine Screener columns
// First 10 plots are the primary screener columns.
//------------------------------------------------------------------------------
plot(close, "Price")
plot(qrbHigh, "QRB High")
plot(qrbMidline, "QRB Midline")
plot(qrbLow, "QRB Low")
plot(qrbPositionPct, "QRB Position %")
plot(distanceToHighPct, "Distance to High %")
plot(daysIntoQuarter, "Days Into Quarter")
plot(statusCode, "QRB Status")
plot(approachDirection, "Approach Direction")
plot(breakoutSeenThisQuarter ? 1.0 : 0.0, "Breakout Seen")
//------------------------------------------------------------------------------
// Pine Screener / alert conditions
//------------------------------------------------------------------------------
alertcondition(
freshBreakout,
"QRB Fresh Breakout",
"QRB Fresh Breakout on {{ticker}}")
alertcondition(
qrbRetest,
"QRB Retest",
"QRB Retest on {{ticker}}")
alertcondition(
preBreakoutReady,
"QRB Pre-Breakout Ready",
"QRB Pre-Breakout Ready on {{ticker}}")
alertcondition(
returnedInside,
"QRB Returned Inside",
"QRB Returned Inside on {{ticker}}")
alertcondition(
midlineCross,
"QRB Midline Cross",
"QRB Midline Cross on {{ticker}}")