本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("OTE + Фибоначчи + Rejection Blocks", shorttitle = "OTE RB", overlay = true, max_boxes_count = 100, max_lines_count = 400, max_labels_count = 100, max_bars_back = 1000)
const string G_OTE = "OTE and Fibonacci levels"
const string G_RB = "Rejection Blocks"
const string G_POC = "Wick volume cluster"
const string G_VIEW = "Appearance"
bool showOTE = input.bool(true, "Show OTE and Fibonacci", group = G_OTE)
int oteSwing = input.int(10, "Swing strength", minval = 2, group = G_OTE, tooltip = "A swing needs this number of bars on both sides for confirmation. Higher values filter smaller swings.")
int oteDepth = input.int(5, "Anchor swing depth", minval = 1, maxval = 20, group = G_OTE, tooltip = "Number of earlier opposite swings considered for the Fibonacci anchor. 1 uses the nearest swing; higher values select a wider range.")
bool requireChannel = input.bool(true, "Require channel direction", group = G_OTE, tooltip = "Long OTE requires Higher High plus Higher Low. Short OTE requires Lower High plus Lower Low. No zone is shown in a range.")
bool hideBrokenOTE = input.bool(true, "Hide OTE after base break", group = G_OTE, tooltip = "Long OTE is hidden after its anchor low breaks. Short OTE is hidden after its anchor high breaks.")
int fibExtend = input.int(80, "Forward extension, bars", minval = 10, maxval = 500, group = G_OTE)
bool showFibLabels = input.bool(true, "Show ratio labels", group = G_OTE)
bool showOTEZone = input.bool(true, "Highlight OTE zone 0.62–0.79", group = G_OTE)
bool showEquilibrium = input.bool(true, "Highlight 0.5 equilibrium", group = G_OTE)
color fibColor = input.color(color.new(color.silver, 28), "Regular level color", group = G_OTE)
color oteColor = input.color(color.new(#00BCD4, 86), "OTE zone fill", group = G_OTE)
bool showRB = input.bool(true, "Show Rejection Blocks", group = G_RB)
int rbSwing = input.int(5, "Liquidity swing strength", minval = 1, group = G_RB, tooltip = "Number of bars required on both sides to confirm a pivot high or low that can be swept.")
int maxRB = input.int(12, "Maximum active blocks", minval = 1, maxval = 30, group = G_RB)
int rbExtend = input.int(10, "Extension beyond current bar", minval = 1, maxval = 100, group = G_RB, tooltip = "Active blocks, internal levels, and POC extend dynamically to the current bar plus this number of bars.")
float minWickPct = input.float(25.0, "Minimum dominant wick, %", minval = 5, maxval = 90, step = 5, group = G_RB, tooltip = "Safety filter against nearly wickless candles. The primary condition is that the wick is longer than the body, so smaller pin bars are retained.")
float minWickBody = input.float(1.0, "Minimum wick / body ratio", minval = 0.5, maxval = 10, step = 0.1, group = G_RB, tooltip = "Primary rejection filter: the dominant wick must exceed the body. Total candle size is not important.")
float minBodyPct = input.float(4.0, "Minimum body size, %", minval = 1, maxval = 40, step = 1, group = G_RB, tooltip = "Filters doji and indecision candles. Body direction is not required.")
float maxOppositeWickPct = input.float(35.0, "Maximum opposite wick, %", minval = 5, maxval = 49, step = 1, group = G_RB, tooltip = "If the opposite wick is also large, the candle is treated as indecision and no block is created.")
float invalidationPct = input.float(50.0, "Mitigation threshold, %", minval = 5, maxval = 100, step = 5, group = G_RB, tooltip = "Maximum allowed revisit depth. The block and its levels are removed once this percentage is reached. Default: 50%.")
bool showQuadrants = input.bool(true, "Show 25% / 50% / 75% levels", group = G_RB)
float labelPosition = input.float(72.0, "Block label position, %", minval = 40, maxval = 90, step = 1, group = G_RB, tooltip = "0% is the left edge and 100% is the right edge. The default places the label slightly before the right edge.")
bool showPOC = input.bool(true, "Show wick cluster POC", group = G_POC, tooltip = "Estimated maximum-volume level inside the wick. It distributes OHLCV volume through price buckets; it is not an exchange footprint.")
int pocBins = input.int(8, "Price buckets inside wick", minval = 4, maxval = 20, group = G_POC)
int pocLookback = input.int(120, "Volume-profile lookback, bars", minval = 20, maxval = 500, group = G_POC, tooltip = "Number of recent bars used to estimate POC when the block is born.")
color pocBullColor = input.color(color.new(#00BCD4, 10), "Bullish POC color", group = G_POC)
color pocBearColor = input.color(color.new(#F06292, 10), "Bearish POC color", group = G_POC)
bool showPivots = input.bool(false, "Mark confirmed OTE swings", group = G_VIEW)
color bullColor = input.color(color.new(#06D6A0, 84), "Bullish block color", group = G_VIEW)
color bearColor = input.color(color.new(#EF476F, 84), "Bearish block color", group = G_VIEW)
color quadrantColor = input.color(color.new(color.white, 54), "Internal level color", group = G_VIEW)
string chartLanguage = input.string("English", "Chart label language", options = ["English", "Русский"], group = G_VIEW, tooltip = "Changes text drawn on the chart. TradingView does not allow Settings labels themselves to change dynamically.")
// Оценочный профиль: объём свечи пропорционально распределяется по её диапазону.
f_wickPoc(float wickTop, float wickBottom, int bins, int lookback) =>
array<float> profile = array.new_float(bins, 0.0)
float bucket = (wickTop - wickBottom) / bins
int lastBar = math.min(lookback, bar_index)
if bucket > 0
for i = 0 to lastBar
float barTop = high[i]
float barBottom = low[i]
float span = math.max(barTop - barBottom, syminfo.mintick)
float barVolume = nz(volume[i], 0)
if barTop >= wickBottom and barBottom <= wickTop and barVolume > 0
for k = 0 to bins - 1
float bucketBottom = wickBottom + bucket * k
float bucketTop = bucketBottom + bucket
float overlap = math.max(0.0, math.min(barTop, bucketTop) - math.max(barBottom, bucketBottom))
if overlap > 0
array.set(profile, k, array.get(profile, k) + barVolume * overlap / span)
int peakIndex = 0
float peakVolume = -1.0
for k = 0 to bins - 1
float bucketVolume = array.get(profile, k)
if bucketVolume > peakVolume
peakVolume := bucketVolume
peakIndex := k
peakVolume > 0 ? wickBottom + bucket * (peakIndex + 0.5) : na
var array<float> liquidityHighs = array.new_float()
var array<float> liquidityLows = array.new_float()
var array<float> oteHighs = array.new_float()
var array<int> oteHighBars = array.new_int()
var array<float> oteLows = array.new_float()
var array<int> oteLowBars = array.new_int()
var array<box> rbBoxes = array.new_box()
var array<bool> rbIsBull = array.new_bool()
var array<int> rbBornBars = array.new_int()
var array<line> rbQ25 = array.new_line()
var array<line> rbQ50 = array.new_line()
var array<line> rbQ75 = array.new_line()
var array<line> rbPocs = array.new_line()
var array<label> rbNames = array.new_label()
phOTE = ta.pivothigh(high, oteSwing, oteSwing)
plOTE = ta.pivotlow(low, oteSwing, oteSwing)
if not na(phOTE)
array.push(oteHighs, phOTE)
array.push(oteHighBars, bar_index - oteSwing)
if not na(plOTE)
array.push(oteLows, plOTE)
array.push(oteLowBars, bar_index - oteSwing)
while array.size(oteHighs) > 40
array.shift(oteHighs)
array.shift(oteHighBars)
while array.size(oteLows) > 40
array.shift(oteLows)
array.shift(oteLowBars)
phRB = ta.pivothigh(high, rbSwing, rbSwing)
plRB = ta.pivotlow(low, rbSwing, rbSwing)
if not na(phRB)
array.push(liquidityHighs, phRB)
if not na(plRB)
array.push(liquidityLows, plRB)
while array.size(liquidityHighs) > 80
array.shift(liquidityHighs)
while array.size(liquidityLows) > 80
array.shift(liquidityLows)
float candleRange = math.max(high - low, syminfo.mintick)
float candleBody = math.abs(close - open)
float upperWick = high - math.max(open, close)
float lowerWick = math.min(open, close) - low
bool notDoji = candleBody / candleRange * 100 >= minBodyPct
bool bearShape = upperWick / candleRange * 100 >= minWickPct and upperWick >= candleBody * minWickBody and lowerWick / candleRange * 100 <= maxOppositeWickPct
bool bullShape = lowerWick / candleRange * 100 >= minWickPct and lowerWick >= candleBody * minWickBody and upperWick / candleRange * 100 <= maxOppositeWickPct
bool newBearRB = false
bool newBullRB = false
if showRB and array.size(liquidityHighs) > 0
for i = array.size(liquidityHighs) - 1 to 0
float level = array.get(liquidityHighs, i)
if high > level
bool bodyBelowLevel = math.max(open, close) <= level
newBearRB := bodyBelowLevel and notDoji and bearShape
array.remove(liquidityHighs, i)
if newBearRB
break
if showRB and array.size(liquidityLows) > 0
for i = array.size(liquidityLows) - 1 to 0
float level = array.get(liquidityLows, i)
if low < level
bool bodyAboveLevel = math.min(open, close) >= level
newBullRB := bodyAboveLevel and notDoji and bullShape
array.remove(liquidityLows, i)
if newBullRB
break
if newBearRB or newBullRB
bool isBull = newBullRB
float zoneTop = isBull ? math.min(open, close) : high
float zoneBottom = isBull ? low : math.max(open, close)
float zoneHeight = zoneTop - zoneBottom
color blockColor = isBull ? bullColor : bearColor
color borderColor = isBull ? color.new(#06D6A0, 15) : color.new(#EF476F, 15)
box newBox = box.new(bar_index, zoneTop, bar_index + rbExtend, zoneBottom, bgcolor = blockColor, border_color = borderColor)
array.push(rbBoxes, newBox)
array.push(rbIsBull, isBull)
array.push(rbBornBars, bar_index)
line q25 = showQuadrants ? line.new(bar_index, zoneBottom + zoneHeight * 0.25, bar_index + rbExtend, zoneBottom + zoneHeight * 0.25, color = quadrantColor, style = line.style_dotted) : na
line q50 = showQuadrants ? line.new(bar_index, zoneBottom + zoneHeight * 0.50, bar_index + rbExtend, zoneBottom + zoneHeight * 0.50, color = quadrantColor, style = line.style_dotted) : na
line q75 = showQuadrants ? line.new(bar_index, zoneBottom + zoneHeight * 0.75, bar_index + rbExtend, zoneBottom + zoneHeight * 0.75, color = quadrantColor, style = line.style_dotted) : na
array.push(rbQ25, q25)
array.push(rbQ50, q50)
array.push(rbQ75, q75)
float poc = showPOC ? f_wickPoc(zoneTop, zoneBottom, pocBins, pocLookback) : na
line pocLine = not na(poc) ? line.new(bar_index, poc, bar_index + rbExtend, poc, color = isBull ? pocBullColor : pocBearColor, width = 3) : na
array.push(rbPocs, pocLine)
int labelX = bar_index + int(math.round(rbExtend * labelPosition / 100))
string blockText = chartLanguage == "Русский" ? "Блок отклонения" : "Rejection Block"
label nameLabel = label.new(labelX, (zoneTop + zoneBottom) / 2, blockText, style = label.style_none, textcolor = isBull ? #06D6A0 : #EF476F, size = size.small)
array.push(rbNames, nameLabel)
if array.size(rbBoxes) > 0
for i = array.size(rbBoxes) - 1 to 0
box currentBox = array.get(rbBoxes, i)
bool isBull = array.get(rbIsBull, i)
int bornBar = array.get(rbBornBars, i)
float top = box.get_top(currentBox)
float bottom = box.get_bottom(currentBox)
float height = math.max(top - bottom, syminfo.mintick)
float penetration = isBull ? math.max(0.0, top - low) / height * 100 : math.max(0.0, high - bottom) / height * 100
bool invalid = bar_index > bornBar and penetration >= invalidationPct
int liveRight = bar_index + rbExtend
line q25Live = array.get(rbQ25, i)
line q50Live = array.get(rbQ50, i)
line q75Live = array.get(rbQ75, i)
line pocLive = array.get(rbPocs, i)
label nameLive = array.get(rbNames, i)
box.set_right(currentBox, liveRight)
if not na(q25Live)
line.set_x2(q25Live, liveRight)
if not na(q50Live)
line.set_x2(q50Live, liveRight)
if not na(q75Live)
line.set_x2(q75Live, liveRight)
if not na(pocLive)
line.set_x2(pocLive, liveRight)
if not na(nameLive)
label.set_x(nameLive, bar_index + int(math.round(rbExtend * labelPosition / 100)))
label.set_text(nameLive, chartLanguage == "Русский" ? "Блок отклонения" : "Rejection Block")
if invalid
box.delete(currentBox)
line q25 = array.get(rbQ25, i)
line q50 = array.get(rbQ50, i)
line q75 = array.get(rbQ75, i)
line pocLine = array.get(rbPocs, i)
label nameLabel = array.get(rbNames, i)
if not na(q25)
line.delete(q25)
if not na(q50)
line.delete(q50)
if not na(q75)
line.delete(q75)
if not na(pocLine)
line.delete(pocLine)
if not na(nameLabel)
label.delete(nameLabel)
array.remove(rbBoxes, i)
array.remove(rbIsBull, i)
array.remove(rbBornBars, i)
array.remove(rbQ25, i)
array.remove(rbQ50, i)
array.remove(rbQ75, i)
array.remove(rbPocs, i)
array.remove(rbNames, i)
while array.size(rbBoxes) > maxRB
box.delete(array.shift(rbBoxes))
array.shift(rbIsBull)
array.shift(rbBornBars)
line q25 = array.shift(rbQ25)
line q50 = array.shift(rbQ50)
line q75 = array.shift(rbQ75)
line pocLine = array.shift(rbPocs)
label nameLabel = array.shift(rbNames)
if not na(q25)
line.delete(q25)
if not na(q50)
line.delete(q50)
if not na(q75)
line.delete(q75)
if not na(pocLine)
line.delete(pocLine)
if not na(nameLabel)
label.delete(nameLabel)
plotshape(showPivots and not na(phOTE), title = "Confirmed swing high", style = shape.triangledown, location = location.abovebar, offset = -oteSwing, color = color.new(#EF476F, 0), size = size.tiny)
plotshape(showPivots and not na(plOTE), title = "Confirmed swing low", style = shape.triangleup, location = location.belowbar, offset = -oteSwing, color = color.new(#06D6A0, 0), size = size.tiny)
var array<line> fibLines = array.new_line()
var array<label> fibLabels = array.new_label()
var box oteBox = na
if barstate.islast
for item in fibLines
line.delete(item)
array.clear(fibLines)
for item in fibLabels
label.delete(item)
array.clear(fibLabels)
if not na(oteBox)
box.delete(oteBox)
oteBox := na
bool haveStructure = array.size(oteHighs) >= 2 and array.size(oteLows) >= 2
if showOTE and haveStructure
int lastHighIndex = array.size(oteHighs) - 1
int lastLowIndex = array.size(oteLows) - 1
float lastHigh = array.get(oteHighs, lastHighIndex)
float previousHigh = array.get(oteHighs, lastHighIndex - 1)
float lastLow = array.get(oteLows, lastLowIndex)
float previousLow = array.get(oteLows, lastLowIndex - 1)
bool channelUp = lastHigh > previousHigh and lastLow > previousLow
bool channelDown = lastHigh < previousHigh and lastLow < previousLow
bool fallbackUp = array.get(oteHighBars, lastHighIndex) > array.get(oteLowBars, lastLowIndex)
bool upLeg = channelUp ? true : channelDown ? false : fallbackUp
bool directionAllowed = not requireChannel or channelUp or channelDown
float startPrice = na
int startBar = na
if directionAllowed and upLeg
int checked = 0
for i = array.size(oteLows) - 1 to 0
if checked < oteDepth
float candidatePrice = array.get(oteLows, i)
if na(startPrice) or candidatePrice < startPrice
startPrice := candidatePrice
startBar := array.get(oteLowBars, i)
checked += 1
else if directionAllowed
int checked = 0
for i = array.size(oteHighs) - 1 to 0
if checked < oteDepth
float candidatePrice = array.get(oteHighs, i)
if na(startPrice) or candidatePrice > startPrice
startPrice := candidatePrice
startBar := array.get(oteHighBars, i)
checked += 1
float endPrice = na
if not na(startBar)
int barsFromAnchor = math.min(bar_index - startBar, 999)
for i = 0 to barsFromAnchor
float candidateExtreme = upLeg ? high[i] : low[i]
if na(endPrice) or (upLeg ? candidateExtreme > endPrice : candidateExtreme < endPrice)
endPrice := candidateExtreme
bool oteBroken = not na(startPrice) and (upLeg ? low < startPrice : high > startPrice)
if not na(startPrice) and not na(startBar) and not na(endPrice) and startPrice != endPrice and (not hideBrokenOTE or not oteBroken)
float span = math.abs(endPrice - startPrice)
ratios = array.from(0.0, 0.5, 0.618, 0.62, 0.705, 0.79, 1.0)
for ratio in ratios
float y = upLeg ? endPrice - span * ratio : endPrice + span * ratio
bool isOTE = ratio >= 0.62 and ratio <= 0.79
bool isEQ = ratio == 0.5
color lineColor = isOTE ? #00BCD4 : isEQ and showEquilibrium ? #F9A825 : fibColor
line newLine = line.new(startBar, y, bar_index + fibExtend, y, color = lineColor, width = isOTE ? 2 : 1)
array.push(fibLines, newLine)
if showFibLabels
label newLabel = label.new(bar_index + fibExtend, y, str.tostring(ratio, "#.###"), style = label.style_label_left, textcolor = lineColor, color = color.new(color.black, 100), size = size.tiny)
array.push(fibLabels, newLabel)
if showOTEZone
float p62 = upLeg ? endPrice - span * 0.62 : endPrice + span * 0.62
float p79 = upLeg ? endPrice - span * 0.79 : endPrice + span * 0.79
oteBox := box.new(startBar, math.max(p62, p79), bar_index + fibExtend, math.min(p62, p79), bgcolor = oteColor, border_color = color.new(#00BCD4, 100))
alertcondition(newBearRB, "New bearish Rejection Block", "A bearish Rejection Block formed after a liquidity sweep.")
alertcondition(newBullRB, "New bullish Rejection Block", "A bullish Rejection Block formed after a liquidity sweep.")