本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This source code is subject to the terms of the Mozilla Public License 2.0
// © Dots3Red
// TradingView: https://www.tradingview.com/u/Dots3Red/
//@version=6
indicator("OTE Zones - Entry Depth Statistics [Dots3Red]",
shorttitle = "OTE Zones [D3R]",
overlay = true,
max_lines_count = 500,
max_boxes_count = 500,
max_labels_count = 500)
// COLORS
C_NEUTRAL = color.new(#64748b, 0)
C_GREEN = color.new(#00c896, 0)
C_RED = color.new(#ff4466, 0)
C_AMBER = color.new(#ffb700, 0)
C_BG = color.new(#131722, 5)
C_TXT = color.new(#f8fafc, 0)
C_DIM = color.new(#94a3b8, 0)
C_BORD = color.new(#334155, 0)
// INPUTS
GRP_SW = "📐 Swing & Structure"
GRP_OTE = "📏 OTE Levels"
GRP_ST = "🎯 Outcome Grading"
GRP_VIS = "🎨 Visualization"
GRP_COL = "🎨 Colors"
GRP_HUD = "🖥️ Dashboard"
pivot_leg = input.int(8, "Pivot Leg (bars each side)", minval=3, maxval=30, group=GRP_SW)
min_swing_atr = input.float(3.0, "Min Swing Size (×ATR)", minval=0.5, maxval=20.0, step=0.5, group=GRP_SW,
tooltip="Swings smaller than this are ignored — the OTE concept needs a real impulse leg to be meaningful.")
ote_shallow = input.float(0.618, "Shallow Level", minval=0.5, maxval=0.9, step=0.001, group=GRP_OTE)
ote_mid = input.float(0.705, "Mid Level", minval=0.5, maxval=0.9, step=0.001, group=GRP_OTE)
ote_deep = input.float(0.79, "Deep Level", minval=0.5, maxval=0.95, step=0.001, group=GRP_OTE)
touch_tol = input.float(0.15, "Touch Tolerance (×ATR)", minval=0.05, maxval=1.0, step=0.05, group=GRP_ST)
outcome_atr = input.float(1.0, "Continuation Confirmation (×ATR)", minval=0.3, maxval=5.0, step=0.1, group=GRP_ST,
tooltip="Price must move this far back in the original swing direction to grade the touch as continued.")
invalid_atr = input.float(0.5, "Invalidation Buffer (×ATR)", minval=0.1, maxval=3.0, step=0.1, group=GRP_ST,
tooltip="Price closing beyond the swing origin by this margin invalidates the zone entirely.")
outcome_bars = input.int(15, "Outcome Window (bars)", minval=5, maxval=100, group=GRP_ST)
show_zone = input.bool(true, "OTE Zone Fill", group=GRP_VIS)
show_labels = input.bool(true, "Tier Stat Labels", group=GRP_VIS)
col_bull_zone = input.color(#00f0ff, "Bullish OTE Zone", group=GRP_COL)
col_bear_zone = input.color(#ff00aa, "Bearish OTE Zone", group=GRP_COL)
show_hud = input.bool(true, "Show Dashboard", group=GRP_HUD)
hud_pos = input.string("Top Right", "Position",
options=["Top Right","Top Left","Bottom Right","Bottom Left"], group=GRP_HUD)
// CONTEXT
atr_raw = ta.atr(14)
ph = ta.pivothigh(high, pivot_leg, pivot_leg)
pl = ta.pivotlow(low, pivot_leg, pivot_leg)
var float last_ph = na
var int last_ph_bar = na
var float last_pl = na
var int last_pl_bar = na
var bool ote_active = false
var bool ote_is_bull = false
var float swing_hi = na
var float swing_lo = na
var int swing_hi_bar = na
var int swing_lo_bar = na
f_ote_price(float r) =>
ote_is_bull ? swing_hi - (swing_hi - swing_lo) * r : swing_lo + (swing_hi - swing_lo) * r
// Drawing storage
var box ote_box = na
var line[] ote_lines = array.new<line>()
var label[] ote_labels = array.new<label>()
var int pend_bar = na
var float pend_ref = na
var int pend_tier = na
var int[] tier_total = array.new<int>(3, 0)
var int[] tier_continued = array.new<int>(3, 0)
f_clear_ote() =>
if not na(ote_box)
box.delete(ote_box)
if array.size(ote_lines) > 0
for i = 0 to array.size(ote_lines) - 1
line.delete(array.get(ote_lines, i))
array.clear(ote_lines)
if array.size(ote_labels) > 0
for i = 0 to array.size(ote_labels) - 1
label.delete(array.get(ote_labels, i))
array.clear(ote_labels)
f_draw_ote() =>
f_clear_ote()
int x1 = math.min(swing_hi_bar, swing_lo_bar)
int x2 = bar_index + 15
color zc = ote_is_bull ? col_bull_zone : col_bear_zone
float shallow_p = f_ote_price(ote_shallow)
float deep_p = f_ote_price(ote_deep)
float top = math.max(shallow_p, deep_p)
float bot = math.min(shallow_p, deep_p)
box new_box = show_zone ? box.new(x1, top, x2, bot,
border_color=color.new(zc, 55), bgcolor=color.new(zc, 88)) : na
float levels3 = f_ote_price(ote_shallow)
float levelm = f_ote_price(ote_mid)
float leveld = f_ote_price(ote_deep)
array.push(ote_lines, line.new(x1, levels3, x2, levels3, color=color.new(zc, 30), width=1, style=line.style_dashed))
array.push(ote_lines, line.new(x1, levelm, x2, levelm, color=color.new(zc, 15), width=2))
array.push(ote_lines, line.new(x1, leveld, x2, leveld, color=color.new(zc, 30), width=1, style=line.style_dashed))
if show_labels
for i = 0 to 2
int t = array.get(tier_total, i)
int c = array.get(tier_continued, i)
string tname = i == 0 ? str.tostring(ote_shallow) : i == 1 ? str.tostring(ote_mid) : str.tostring(ote_deep)
string s = tname
if t > 0
s := s + " | " + str.tostring(math.round(float(c) / float(t) * 100.0)) + "% continued (n=" + str.tostring(t) + ")"
else
s := s + " | no touches yet"
float ly = i == 0 ? levels3 : i == 1 ? levelm : leveld
array.push(ote_labels, label.new(x2, ly, s,
style=label.style_label_left,
color=color.new(#000000, 100), textcolor=zc, size=11))
new_box
bool new_ote = false
if barstate.isconfirmed
// UPDATE PIVOTS
if not na(ph)
last_ph := ph
last_ph_bar := bar_index - pivot_leg
if not na(pl)
last_pl := pl
last_pl_bar := bar_index - pivot_leg
bool bull_break = not na(last_ph) and not na(last_pl) and last_pl_bar > last_ph_bar and close > last_ph and close[1] <= last_ph
bool bear_break = not na(last_ph) and not na(last_pl) and last_ph_bar > last_pl_bar and close < last_pl and close[1] >= last_pl
if bull_break and math.abs(close - last_pl) >= min_swing_atr * atr_raw
ote_active := true
ote_is_bull := true
swing_hi := close
swing_hi_bar := bar_index
swing_lo := last_pl
swing_lo_bar := last_pl_bar
pend_bar := na
new_ote := true
ote_box := f_draw_ote()
if bear_break and math.abs(last_ph - close) >= min_swing_atr * atr_raw
ote_active := true
ote_is_bull := false
swing_lo := close
swing_lo_bar := bar_index
swing_hi := last_ph
swing_hi_bar := last_ph_bar
pend_bar := na
new_ote := true
ote_box := f_draw_ote()
// GRADE PENDING TOUCH
if ote_active and not na(pend_bar)
bool continued = ote_is_bull ? (close - pend_ref) >= outcome_atr * atr_raw
: (pend_ref - close) >= outcome_atr * atr_raw
bool timed = (bar_index - pend_bar) >= outcome_bars
if continued or timed
array.set(tier_total, pend_tier, array.get(tier_total, pend_tier) + 1)
if continued
array.set(tier_continued, pend_tier, array.get(tier_continued, pend_tier) + 1)
pend_bar := na
ote_box := f_draw_ote()
if ote_active and na(pend_bar)
float p_shallow = f_ote_price(ote_shallow)
float p_mid = f_ote_price(ote_mid)
float p_deep = f_ote_price(ote_deep)
float px = ote_is_bull ? low : high
bool correct_side = ote_is_bull ? close > swing_lo : close < swing_hi
if correct_side
if math.abs(px - p_deep) <= touch_tol * atr_raw
pend_bar := bar_index
pend_ref := close
pend_tier := 2
else if math.abs(px - p_mid) <= touch_tol * atr_raw
pend_bar := bar_index
pend_ref := close
pend_tier := 1
else if math.abs(px - p_shallow) <= touch_tol * atr_raw
pend_bar := bar_index
pend_ref := close
pend_tier := 0
// INVALIDATION: price closes beyond the swing origin
bool invalidated = ote_active and (ote_is_bull ? close < swing_lo - invalid_atr * atr_raw
: close > swing_hi + invalid_atr * atr_raw)
if invalidated
ote_active := false
pend_bar := na
f_clear_ote()
ote_box := na
// EXTEND ZONE WHILE ACTIVE
if ote_active and not invalidated and not new_ote
if not na(ote_box)
box.set_right(ote_box, bar_index + 15)
if array.size(ote_lines) > 0
for i = 0 to array.size(ote_lines) - 1
line.set_x2(array.get(ote_lines, i), bar_index + 15)
if array.size(ote_labels) > 0
for i = 0 to array.size(ote_labels) - 1
label.set_x(array.get(ote_labels, i), bar_index + 16)
// DASHBOARD
f_hud_pos(string s) =>
switch s
"Top Right" => position.top_right
"Top Left" => position.top_left
"Bottom Right" => position.bottom_right
=> position.bottom_left
var table hud = table.new(f_hud_pos(hud_pos), 2, 6,
bgcolor=C_BG, border_color=C_BORD,
border_width=1, frame_color=C_BORD, frame_width=2)
if show_hud and barstate.islast
table.cell(hud, 0, 0, "OTE AI [D3R]", text_color=C_TXT,
bgcolor=color.new(#1e293b, 0), text_size=11, text_halign=text.align_center)
table.merge_cells(hud, 0, 0, 1, 0)
table.cell(hud, 0, 1, "Active Zone", text_color=C_DIM, bgcolor=color.new(#1e293b, 40), text_size=11)
table.cell(hud, 1, 1, not ote_active ? "—" : ote_is_bull ? "▲ Bullish" : "▼ Bearish",
text_color=not ote_active ? C_DIM : ote_is_bull ? col_bull_zone : col_bear_zone,
bgcolor=color.new(#1e293b, 40), text_size=11, text_halign=text.align_center)
for i = 0 to 2
int t = array.get(tier_total, i)
int c = array.get(tier_continued, i)
string lbl = i == 0 ? str.tostring(ote_shallow) : i == 1 ? str.tostring(ote_mid) : str.tostring(ote_deep)
string rowval = t > 0 ? str.tostring(math.round(float(c) / float(t) * 100.0)) + "% (n=" + str.tostring(t) + ")" : "—"
color rc = t == 0 ? C_DIM : float(c) / float(t) >= 0.5 ? C_GREEN : C_RED
table.cell(hud, 0, 2 + i, lbl, text_color=C_DIM,
bgcolor=color.new(i % 2 == 0 ? #1e293b : #0f172a, 40), text_size=11)
table.cell(hud, 1, 2 + i, rowval, text_color=rc,
bgcolor=color.new(i % 2 == 0 ? #1e293b : #0f172a, 40), text_size=11, text_halign=text.align_center)
int gt = array.get(tier_total,0) + array.get(tier_total,1) + array.get(tier_total,2)
table.cell(hud, 0, 5, "Total Touches", text_color=C_DIM, bgcolor=color.new(#1e293b, 40), text_size=11)
table.cell(hud, 1, 5, str.tostring(gt), text_color=C_TXT, bgcolor=color.new(#1e293b, 40), text_size=11, text_halign=text.align_center)
// ALERTS
alertcondition(new_ote, "New OTE Zone", "D3R OTE AI: new optimal trade entry zone formed after a structure break")