本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Reversal Entry Zones", shorttitle="Reversal Entry Zones", overlay=true, max_boxes_count=220, max_lines_count=500, max_labels_count=250)
//══════════════════════════════════════════════════════════════════════════════
// OVERVIEW
// This script builds reversal zones from a confirmed pivot engine.
// Core workflow:
// 1) Track running highs and lows from the selected price source
// 2) Confirm a pivot only when price reverses by a minimum threshold
// 3) Mark the reversal level as bullish or bearish
// 4) Draw a core zone, outer zone, and optional projected zone
// 5) Optionally connect confirmed reversals with a synthetic path
// 6) Summarize the current state in a status panel
//
// Notes:
// - Reversals are confirmed only after the selected threshold is reached.
// - The script is a structural visualization tool, not an execution engine.
//══════════════════════════════════════════════════════════════════════════════
//══════════════════════════════════════════════════════════════════════════════
// INPUTS
//══════════════════════════════════════════════════════════════════════════════
grpSignal = "Confirmation Settings"
confirmBars = input.int(0, "Extra Confirmation Bars", minval=0, maxval=5, group=grpSignal)
grpPreset = "Sensitivity"
preset = input.string("Low", "Preset", options=["Low", "Very Low"], group=grpPreset)
grpCalc = "Reversal Calculation"
calcMode = input.string("Average", "Swing Source", options=["Average", "High/Low"], group=grpCalc)
avgLen = input.int(5, "Average Length", minval=1, maxval=100, group=grpCalc)
atrLen = input.int(5, "ATR Length", minval=1, maxval=100, group=grpCalc)
customAbs = input.float(0.05, "Minimum Absolute Reversal", minval=0.0001, step=0.0001, group=grpCalc)
grpZones = "Zone Display"
showReversalLabels = input.bool(true, "Show Reversal Labels", group=grpZones)
showZones = input.bool(true, "Show Projected Zones", group=grpZones)
maxZones = input.int(8, "Max Zones", minval=1, maxval=50, group=grpZones)
zoneExtend = input.int(10, "Zone Extension", minval=5, maxval=200, group=grpZones)
zoneThicknessPct = input.float(0.03, "Zone Thickness (%)", minval=0.005, maxval=0.5, step=0.005, group=grpZones)
grpLevels = "Reversal Lines"
levelExtend = input.int(10, "Reversal Line Extension", minval=1, maxval=100, group=grpLevels)
maxLevels = input.int(12, "Max Reversal Levels", minval=1, maxval=100, group=grpLevels)
labelSizeOpt = input.string("Normal", "Label Size", options=["Small", "Normal", "Large"], group=grpLevels)
grpPath = "Synthetic Path"
showSyntheticPath = input.bool(true, "Show Synthetic Path", group=grpPath)
syntheticWidth = input.int(1, "Path Width", minval=1, maxval=5, group=grpPath)
syntheticStyleOpt = input.string("Solid", "Path Style", options=["Solid", "Dashed", "Dotted"], group=grpPath)
maxSyntheticLines = input.int(40, "Max Path Segments", minval=5, maxval=200, group=grpPath)
grpColors = "Colors"
bullishColor = input.color(color.green, "Bullish Color", group=grpColors)
bearishColor = input.color(color.red, "Bearish Color", group=grpColors)
syntheticColor = input.color(color.blue, "Synthetic Path Color", group=grpColors)
grpPanel = "Status Panel"
showPanel = input.bool(true, "Show Status Panel", group=grpPanel)
panelPosition = input.string("Bottom Right", "Panel Position", options=["Top Right", "Top Left", "Bottom Right", "Bottom Left"], group=grpPanel)
//══════════════════════════════════════════════════════════════════════════════
// STYLE
//══════════════════════════════════════════════════════════════════════════════
color panelBg = color.rgb(11, 13, 18)
color panelBgAlt = color.rgb(16, 19, 26)
color panelHeaderBg = color.rgb(20, 24, 32)
color panelBorder = color.rgb(42, 48, 60)
color txtPrimary = color.rgb(230, 234, 240)
color txtMuted = color.rgb(136, 145, 160)
labelSize = switch labelSizeOpt
"Small" => size.small
"Normal" => size.normal
"Large" => size.large
=> size.normal
syntheticLineStyle = switch syntheticStyleOpt
"Solid" => line.style_solid
"Dashed" => line.style_dashed
"Dotted" => line.style_dotted
=> line.style_dashed
getPanelPosition(_pos) =>
_pos == "Top Left" ? position.top_left :
_pos == "Bottom Right" ? position.bottom_right :
_pos == "Bottom Left" ? position.bottom_left :
position.top_right
//══════════════════════════════════════════════════════════════════════════════
// STORAGE
//══════════════════════════════════════════════════════════════════════════════
var array<line> reversalLines = array.new<line>()
var array<line> glowLines = array.new<line>()
var array<line> syntheticLines = array.new<line>()
var array<box> coreBoxes = array.new<box>()
var array<box> outerBoxes = array.new<box>()
var array<box> zoneBoxes = array.new<box>()
var int lastReversalDir = 0
var float lastReversalPrice = na
var int lastReversalBar = na
var int prevPathBar = na
var float prevPathPrice = na
//══════════════════════════════════════════════════════════════════════════════
// SENSITIVITY MODEL
//══════════════════════════════════════════════════════════════════════════════
presetATR = switch preset
"Low" => 2.8
"Very Low" => 3.5
=> 2.8
presetPct = switch preset
"Low" => 0.015
"Very Low" => 0.02
=> 0.015
atrNow = ta.atr(atrLen)
reversalThreshold = math.max(close * presetPct / 100.0, math.max(customAbs, presetATR * atrNow))
//══════════════════════════════════════════════════════════════════════════════
// PRICE STREAMS
//══════════════════════════════════════════════════════════════════════════════
hiBase = calcMode == "High/Low" ? high : ta.ema(high, avgLen)
loBase = calcMode == "High/Low" ? low : ta.ema(low, avgLen)
hiRef = hiBase[confirmBars]
loRef = loBase[confirmBars]
hiRawRef = high[confirmBars]
loRawRef = low[confirmBars]
//══════════════════════════════════════════════════════════════════════════════
// CONFIRMED PIVOT ENGINE
// The script tracks running highs/lows and confirms a pivot only after price
// reverses by at least the required threshold.
//══════════════════════════════════════════════════════════════════════════════
var float runHigh = na
var float runLow = na
var float runHighRaw = na
var float runLowRaw = na
var int runHighBar = na
var int runLowBar = na
var int swingDir = 1
var float confirmedPivot = na
var float confirmedPivotRaw = na
var int confirmedPivotBar = na
var bool confirmedPivotIsHigh = false
bool pivotReady = false
if not na(hiRef) and not na(loRef)
if na(runHigh) or na(runLow)
runHigh := hiRef
runLow := loRef
runHighRaw := hiRawRef
runLowRaw := loRawRef
runHighBar := bar_index - confirmBars
runLowBar := bar_index - confirmBars
swingDir := 1
if swingDir == 1
if hiRef > runHigh
runHigh := hiRef
runHighRaw := hiRawRef
runHighBar := bar_index - confirmBars
if runHigh - loRef >= reversalThreshold
confirmedPivot := runHigh
confirmedPivotRaw := runHighRaw
confirmedPivotBar := runHighBar
confirmedPivotIsHigh := true
pivotReady := true
swingDir := -1
runLow := loRef
runLowRaw := loRawRef
runLowBar := bar_index - confirmBars
else
if loRef < runLow
runLow := loRef
runLowRaw := loRawRef
runLowBar := bar_index - confirmBars
if hiRef - runLow >= reversalThreshold
confirmedPivot := runLow
confirmedPivotRaw := runLowRaw
confirmedPivotBar := runLowBar
confirmedPivotIsHigh := false
pivotReady := true
swingDir := 1
runHigh := hiRef
runHighRaw := hiRawRef
runHighBar := bar_index - confirmBars
//══════════════════════════════════════════════════════════════════════════════
// REVERSAL MODULE
// A confirmed low pivot becomes a bullish reversal.
// A confirmed high pivot becomes a bearish reversal.
//══════════════════════════════════════════════════════════════════════════════
bool bullishReversalSignal = pivotReady and not confirmedPivotIsHigh
bool bearishReversalSignal = pivotReady and confirmedPivotIsHigh
if pivotReady
bool isBullishReversal = not confirmedPivotIsHigh
float centerPrice = confirmedPivotRaw
lastReversalDir := isBullishReversal ? 1 : -1
lastReversalPrice := centerPrice
lastReversalBar := confirmedPivotBar
float coreHalf = math.max(atrNow * 0.12, math.abs(centerPrice) * 0.00015)
float outerHalf = coreHalf * 2.15
color baseCol = isBullishReversal ? bullishColor : bearishColor
color textCol = color.white
box outerBox = box.new(
confirmedPivotBar,
centerPrice + outerHalf,
confirmedPivotBar + levelExtend,
centerPrice - outerHalf,
xloc=xloc.bar_index,
bgcolor=color.new(baseCol, 92),
border_color=color.new(baseCol, 58),
border_width=2
)
array.push(outerBoxes, outerBox)
box coreBox = box.new(
confirmedPivotBar,
centerPrice + coreHalf,
confirmedPivotBar + levelExtend,
centerPrice - coreHalf,
xloc=xloc.bar_index,
bgcolor=color.new(baseCol, 80),
border_color=color.new(baseCol, 18),
border_width=3
)
array.push(coreBoxes, coreBox)
if showReversalLabels
label.new(
confirmedPivotBar,
centerPrice + outerHalf,
isBullishReversal ? "BULLISH REVERSAL" : "BEARISH REVERSAL",
style=isBullishReversal ? label.style_label_upper_right : label.style_label_lower_right,
color=color.new(baseCol, 12),
textcolor=textCol,
size=labelSize,
xloc=xloc.bar_index
)
line glowLine = line.new(
confirmedPivotBar,
centerPrice,
confirmedPivotBar + levelExtend,
centerPrice,
xloc=xloc.bar_index,
extend=extend.none,
color=color.new(baseCol, 78),
width=7,
style=line.style_solid
)
array.push(glowLines, glowLine)
line reversalLine = line.new(
confirmedPivotBar,
centerPrice,
confirmedPivotBar + levelExtend,
centerPrice,
xloc=xloc.bar_index,
extend=extend.none,
color=color.new(baseCol, 0),
width=3,
style=line.style_solid
)
array.push(reversalLines, reversalLine)
if showZones
float zoneHalf = (centerPrice * zoneThicknessPct / 100.0) / 2.0
box zoneBox = box.new(
confirmedPivotBar,
centerPrice + zoneHalf,
confirmedPivotBar + zoneExtend,
centerPrice - zoneHalf,
xloc=xloc.bar_index,
bgcolor=color.new(baseCol, 86),
border_color=color.new(baseCol, 20),
border_width=2,
text=isBullishReversal ? "BULLISH ZONE" : "BEARISH ZONE",
text_color=color.new(txtPrimary, 0),
text_size=size.tiny,
text_halign=text.align_center,
text_valign=text.align_center
)
array.push(zoneBoxes, zoneBox)
if showSyntheticPath and not na(prevPathBar) and not na(prevPathPrice)
line syntheticLine = line.new(
prevPathBar,
prevPathPrice,
confirmedPivotBar,
centerPrice,
xloc=xloc.bar_index,
extend=extend.none,
color=color.new(syntheticColor, 8),
width=syntheticWidth,
style=syntheticLineStyle
)
array.push(syntheticLines, syntheticLine)
prevPathBar := confirmedPivotBar
prevPathPrice := centerPrice
//══════════════════════════════════════════════════════════════════════════════
// STATUS DATA
//══════════════════════════════════════════════════════════════════════════════
int barsSinceLastReversal = na(lastReversalBar) ? na : bar_index - lastReversalBar
string barsSinceText = na(barsSinceLastReversal) ? "—" : str.tostring(barsSinceLastReversal)
string lastSignalText = lastReversalDir == 0 ? "Waiting" : lastReversalDir == 1 ? "Bullish Reversal" : "Bearish Reversal"
string lastPriceText = na(lastReversalPrice) ? "—" : str.tostring(lastReversalPrice, format.mintick)
string trackingText = swingDir == 1 ? "Monitoring Bearish Reversal" : "Monitoring Bullish Reversal"
string sourceText = calcMode == "High/Low" ? "High / Low" : "Average"
string thresholdText = str.tostring(reversalThreshold, format.mintick)
string atrText = str.tostring(atrNow, format.mintick)
string activeZonesText = str.tostring(array.size(zoneBoxes))
string presetText = preset
color statusAccent = lastReversalDir == 0 ? color.rgb(94, 106, 130) : lastReversalDir == 1 ? bullishColor : bearishColor
color statusBg = lastReversalDir == 0 ? color.rgb(34, 43, 58) : lastReversalDir == 1 ? color.new(bullishColor, 84) : color.new(bearishColor, 84)
//══════════════════════════════════════════════════════════════════════════════
// STATUS PANEL
//══════════════════════════════════════════════════════════════════════════════
var table panel = table.new(getPanelPosition(panelPosition), 2, 8, frame_color=color.new(panelBorder, 0), frame_width=1, border_width=1, border_color=color.new(panelBorder, 20))
if barstate.islast
table.clear(panel, 0, 0, 1, 7)
if showPanel
table.cell(panel, 0, 0, "REVERSAL ENTRY", text_color=txtPrimary, text_size=size.small, text_halign=text.align_left, bgcolor=panelHeaderBg)
table.cell(panel, 1, 0, "STATUS", text_color=statusAccent, text_size=size.small, text_halign=text.align_center, bgcolor=panelHeaderBg)
table.cell(panel, 0, 1, "Last Signal", text_color=txtMuted, text_size=size.small, text_halign=text.align_left, bgcolor=panelBg)
table.cell(panel, 1, 1, lastSignalText, text_color=txtPrimary, text_size=size.small, text_halign=text.align_center, bgcolor=statusBg)
table.cell(panel, 0, 2, "Signal Price", text_color=txtMuted, text_size=size.small, text_halign=text.align_left, bgcolor=panelBgAlt)
table.cell(panel, 1, 2, lastPriceText, text_color=statusAccent, text_size=size.small, text_halign=text.align_center, bgcolor=panelBgAlt)
table.cell(panel, 0, 3, "Bars Since", text_color=txtMuted, text_size=size.small, text_halign=text.align_left, bgcolor=panelBg)
table.cell(panel, 1, 3, barsSinceText, text_color=txtPrimary, text_size=size.small, text_halign=text.align_center, bgcolor=panelBg)
table.cell(panel, 0, 4, "Threshold", text_color=txtMuted, text_size=size.small, text_halign=text.align_left, bgcolor=panelBgAlt)
table.cell(panel, 1, 4, thresholdText, text_color=txtPrimary, text_size=size.small, text_halign=text.align_center, bgcolor=panelBgAlt)
table.cell(panel, 0, 5, "ATR / Zones", text_color=txtMuted, text_size=size.small, text_halign=text.align_left, bgcolor=panelBg)
table.cell(panel, 1, 5, atrText + " / " + activeZonesText, text_color=txtPrimary, text_size=size.small, text_halign=text.align_center, bgcolor=panelBg)
table.cell(panel, 0, 6, "Tracking", text_color=txtMuted, text_size=size.small, text_halign=text.align_left, bgcolor=panelBgAlt)
table.cell(panel, 1, 6, trackingText, text_color=txtPrimary, text_size=size.small, text_halign=text.align_center, bgcolor=panelBgAlt)
table.cell(panel, 0, 7, "Preset / Source", text_color=txtMuted, text_size=size.small, text_halign=text.align_left, bgcolor=panelBg)
table.cell(panel, 1, 7, presetText + " / " + sourceText, text_color=txtPrimary, text_size=size.small, text_halign=text.align_center, bgcolor=panelBg)
//══════════════════════════════════════════════════════════════════════════════
// CLEANUP
//══════════════════════════════════════════════════════════════════════════════
while array.size(reversalLines) > maxLevels
line oldReversal = array.shift(reversalLines)
line.delete(oldReversal)
while array.size(glowLines) > maxLevels
line oldGlow = array.shift(glowLines)
line.delete(oldGlow)
while array.size(syntheticLines) > maxSyntheticLines
line oldSynthetic = array.shift(syntheticLines)
line.delete(oldSynthetic)
while array.size(coreBoxes) > maxLevels
box oldCore = array.shift(coreBoxes)
box.delete(oldCore)
while array.size(outerBoxes) > maxLevels
box oldOuter = array.shift(outerBoxes)
box.delete(oldOuter)
while array.size(zoneBoxes) > maxZones
box oldZone = array.shift(zoneBoxes)
box.delete(oldZone)
//══════════════════════════════════════════════════════════════════════════════
// ALERTS
//══════════════════════════════════════════════════════════════════════════════
alertcondition(bullishReversalSignal, title="Bullish Reversal Detected", message="Bullish reversal confirmed")
alertcondition(bearishReversalSignal, title="Bearish Reversal Detected", message="Bearish reversal confirmed")