本页源码来自 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("Ultra Order Blocks Zones [JPT]", shorttitle="Ultra OB [JPT]", overlay=true, max_boxes_count=200, max_labels_count=200)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// SETTINGS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
groupDetect = "Order Block Detection"
swingLen = input.int(25, "Swing Length", minval=2, maxval=30, group=groupDetect)
lookback = input.int(12, "Search Previous Candle", minval=2, maxval=50, group=groupDetect)
atrLen = input.int(14, "ATR Length", minval=1, group=groupDetect)
dispMult = input.float(1.2, "Displacement Strength (ATR)", minval=0.1, step=0.1, group=groupDetect)
groupZones = "Zone Management"
maxZones = input.int(18, "Maximum Zones Per Side", minval=1, maxval=50, group=groupZones)
extendZones = input.bool(true, "Extend Zones", group=groupZones)
removeInvalid = input.bool(true, "Remove Invalidated Zones", group=groupZones)
showLabels = input.bool(true, "Show Zone Labels", group=groupZones)
mitigationMode = input.string("Wick", "Mitigation Detection", options=["Wick", "Close"], group=groupZones)
groupVisual = "Visual Settings"
bullColor = input.color(color.new(color.lime, 82), "Bullish Zone", group=groupVisual)
bearColor = input.color(color.new(color.red, 82), "Bearish Zone", group=groupVisual)
bullBorder = input.color(color.lime, "Bullish Border", group=groupVisual)
bearBorder = input.color(color.red, "Bearish Border", group=groupVisual)
mitColor = input.color(color.new(color.orange, 85), "Mitigated Zone", group=groupVisual)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// CALCULATIONS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
atr = ta.atr(atrLen)
swingHigh = ta.highest(high[1], swingLen)
swingLow = ta.lowest(low[1], swingLen)
bullBreak = close > swingHigh and close > open and (close - open) >= atr * dispMult
bearBreak = close < swingLow and close < open and (open - close) >= atr * dispMult
// Find the most recent opposite candle before displacement.
int bullOffset = na
int bearOffset = na
for i = 1 to lookback
if na(bullOffset) and close[i] < open[i]
bullOffset := i
if na(bearOffset) and close[i] > open[i]
bearOffset := i
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// ZONE STORAGE
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
var box[] bullBoxes = array.new_box()
var box[] bearBoxes = array.new_box()
var bool[] bullMitigated = array.new_bool()
var bool[] bearMitigated = array.new_bool()
var float[] bullTops = array.new_float()
var float[] bullBottoms = array.new_float()
var float[] bearTops = array.new_float()
var float[] bearBottoms = array.new_float()
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// CREATE BULLISH ORDER BLOCK
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
newBullOB = bullBreak and not na(bullOffset)
if newBullOB
float zoneTop = high[bullOffset]
float zoneBottom = low[bullOffset]
box b = box.new(left=bar_index - bullOffset, top=zoneTop, right=bar_index, bottom=zoneBottom, xloc=xloc.bar_index, extend=extendZones ? extend.right : extend.none, bgcolor=bullColor, border_color=bullBorder, border_width=1)
if showLabels
box.set_text(b, "BULLISH OB")
box.set_text_color(b, bullBorder)
box.set_text_size(b, size.tiny)
array.push(bullBoxes, b)
array.push(bullMitigated, false)
array.push(bullTops, zoneTop)
array.push(bullBottoms, zoneBottom)
if array.size(bullBoxes) > maxZones
box.delete(array.shift(bullBoxes))
array.shift(bullMitigated)
array.shift(bullTops)
array.shift(bullBottoms)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// CREATE BEARISH ORDER BLOCK
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
newBearOB = bearBreak and not na(bearOffset)
if newBearOB
float zoneTop = high[bearOffset]
float zoneBottom = low[bearOffset]
box b = box.new(left=bar_index - bearOffset, top=zoneTop, right=bar_index, bottom=zoneBottom, xloc=xloc.bar_index, extend=extendZones ? extend.right : extend.none, bgcolor=bearColor, border_color=bearBorder, border_width=1)
if showLabels
box.set_text(b, "BEARISH OB")
box.set_text_color(b, bearBorder)
box.set_text_size(b, size.tiny)
array.push(bearBoxes, b)
array.push(bearMitigated, false)
array.push(bearTops, zoneTop)
array.push(bearBottoms, zoneBottom)
if array.size(bearBoxes) > maxZones
box.delete(array.shift(bearBoxes))
array.shift(bearMitigated)
array.shift(bearTops)
array.shift(bearBottoms)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// BULLISH ZONE MANAGEMENT
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
if array.size(bullBoxes) > 0
for i = array.size(bullBoxes) - 1 to 0
box b = array.get(bullBoxes, i)
float top = array.get(bullTops, i)
float bottom = array.get(bullBottoms, i)
bool touched = mitigationMode == "Wick" ? low <= top : close <= top
bool invalid = close < bottom
if not array.get(bullMitigated, i) and touched
array.set(bullMitigated, i, true)
box.set_bgcolor(b, mitColor)
box.set_border_color(b, color.orange)
box.set_text(b, showLabels ? "BULLISH OB • MITIGATED" : "")
if invalid and removeInvalid
box.delete(b)
array.remove(bullBoxes, i)
array.remove(bullMitigated, i)
array.remove(bullTops, i)
array.remove(bullBottoms, i)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// BEARISH ZONE MANAGEMENT
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
if array.size(bearBoxes) > 0
for i = array.size(bearBoxes) - 1 to 0
box b = array.get(bearBoxes, i)
float top = array.get(bearTops, i)
float bottom = array.get(bearBottoms, i)
bool touched = mitigationMode == "Wick" ? high >= bottom : close >= bottom
bool invalid = close > top
if not array.get(bearMitigated, i) and touched
array.set(bearMitigated, i, true)
box.set_bgcolor(b, mitColor)
box.set_border_color(b, color.orange)
box.set_text(b, showLabels ? "BEARISH OB • MITIGATED" : "")
if invalid and removeInvalid
box.delete(b)
array.remove(bearBoxes, i)
array.remove(bearMitigated, i)
array.remove(bearTops, i)
array.remove(bearBottoms, i)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// ALERTS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
alertcondition(newBullOB, title="New Bullish Order Block", message="Ultra OB [JPT]: A new bullish order block has been detected.")
alertcondition(newBearOB, title="New Bearish Order Block", message="Ultra OB [JPT]: A new bearish order block has been detected.")
bullMitigationEvent = newBullOB ? false : false
bearMitigationEvent = newBearOB ? false : false