本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Range Expansion Radar", overlay=true, max_lines_count=500, max_labels_count=500)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// SETTINGS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
leftBars = input.int(3, "Swing Left Bars", minval=1)
rightBars = input.int(3, "Swing Right Bars", minval=1)
showSwingPoints = input.bool(true, "Show Swing Points")
showRangeLines = input.bool(true, "Show Active Range Lines")
showBreakLabels = input.bool(true, "Show Breakout Labels")
showLiveMove = input.bool(true, "Show Live Breakout Move")
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// PIVOTS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ph = ta.pivothigh(high, leftBars, rightBars)
pl = ta.pivotlow(low, leftBars, rightBars)
// Latest swing values
var float swingHigh = na
var float swingLow = na
var int swingHighBar = na
var int swingLowBar = na
if not na(ph)
swingHigh := ph
swingHighBar := bar_index - rightBars
if not na(pl)
swingLow := pl
swingLowBar := bar_index - rightBars
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// SWING POINTS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
plotshape(
showSwingPoints and not na(ph),
title="Swing High",
style=shape.triangledown,
location=location.abovebar,
offset=-rightBars,
size=size.tiny,
text="SH"
)
plotshape(
showSwingPoints and not na(pl),
title="Swing Low",
style=shape.triangleup,
location=location.belowbar,
offset=-rightBars,
size=size.tiny,
text="SL"
)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// RANGE CALCULATION
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
rangeReady = not na(swingHigh) and not na(swingLow)
rangePct =
rangeReady
? math.abs(swingHigh - swingLow) / math.min(swingHigh, swingLow) * 100.0
: na
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// BREAKOUT STATE
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 1 = bullish breakout active
// -1 = bearish breakout active
// 0 = no breakout
var int breakoutDirection = 0
var float breakoutLevel = na
var float breakoutRangePct = na
var float breakoutExtreme = na
var int breakoutBar = na
// Prevents counting the same range as a breakout repeatedly
var float usedHigh = na
var float usedLow = na
bullBreak =
rangeReady and
close > swingHigh and
close[1] <= swingHigh and
(na(usedHigh) or swingHigh != usedHigh)
bearBreak =
rangeReady and
close < swingLow and
close[1] >= swingLow and
(na(usedLow) or swingLow != usedLow)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// NEW BULLISH BREAKOUT
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
if bullBreak
breakoutDirection := 1
breakoutLevel := swingHigh
breakoutRangePct := rangePct
breakoutExtreme := high
breakoutBar := bar_index
usedHigh := swingHigh
if showBreakLabels
label.new(
bar_index,
low,
"▲ BOS\n" +
"Range: " + str.tostring(breakoutRangePct, "#.##") + "%",
style=label.style_label_up,
textcolor=color.white,
color=color.new(color.green, 0),
size=size.small
)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// NEW BEARISH BREAKOUT
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
if bearBreak
breakoutDirection := -1
breakoutLevel := swingLow
breakoutRangePct := rangePct
breakoutExtreme := low
breakoutBar := bar_index
usedLow := swingLow
if showBreakLabels
label.new(
bar_index,
high,
"▼ BOS\n" +
"Range: " + str.tostring(breakoutRangePct, "#.##") + "%",
style=label.style_label_down,
textcolor=color.white,
color=color.new(color.red, 0),
size=size.small
)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// MAXIMUM MOVE AFTER BREAKOUT
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
if breakoutDirection == 1
breakoutExtreme := math.max(breakoutExtreme, high)
if breakoutDirection == -1
breakoutExtreme := math.min(breakoutExtreme, low)
// Percentage move beyond the broken level
float extensionPct = na
if breakoutDirection == 1 and not na(breakoutLevel)
extensionPct :=
(breakoutExtreme - breakoutLevel) /
breakoutLevel * 100.0
if breakoutDirection == -1 and not na(breakoutLevel)
extensionPct :=
(breakoutLevel - breakoutExtreme) /
breakoutLevel * 100.0
// How many times the previous range has the move expanded?
float expansionRatio = na
if not na(extensionPct) and
not na(breakoutRangePct) and
breakoutRangePct != 0
expansionRatio :=
extensionPct / breakoutRangePct
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// ACTIVE RANGE LINES
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
var line highLine = na
var line lowLine = na
if barstate.islast
if not na(highLine)
line.delete(highLine)
if not na(lowLine)
line.delete(lowLine)
if showRangeLines and rangeReady
highLine := line.new(
swingHighBar,
swingHigh,
bar_index,
swingHigh,
extend=extend.right,
width=2,
color=color.new(color.red, 20)
)
lowLine := line.new(
swingLowBar,
swingLow,
bar_index,
swingLow,
extend=extend.right,
width=2,
color=color.new(color.green, 20)
)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// LIVE BREAKOUT INFO
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
var label liveLabel = na
if barstate.islast
if not na(liveLabel)
label.delete(liveLabel)
if showLiveMove and breakoutDirection != 0
string directionText =
breakoutDirection == 1 ? "▲ Bull Breakout" : "▼ Bear Breakout"
string txt =
directionText +
"\nRange: " +
str.tostring(breakoutRangePct, "#.##") +
"%" +
"\nExtension: " +
str.tostring(extensionPct, "#.##") +
"%" +
"\nExpansion: " +
str.tostring(expansionRatio, "#.##") +
"x"
float labelPrice =
breakoutDirection == 1
? breakoutExtreme
: breakoutExtreme
liveLabel := label.new(
bar_index,
labelPrice,
txt,
style=
breakoutDirection == 1
? label.style_label_down
: label.style_label_up,
textcolor=color.white,
color=
breakoutDirection == 1
? color.new(color.green, 0)
: color.new(color.red, 0),
size=size.normal
)