本页源码来自 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/
// © EyeAlgo
//@version=6
indicator("Eye Level Strength [EyeAlgo]", "Eye Level Strength", overlay=true, max_boxes_count=500, max_lines_count=500, max_labels_count=500)
// ─────────────────────────── Inputs ───────────────────────────
grpL = "Levels"
pivLen = input.int(10, "Pivot Length (left = right)", minval=2, maxval=50, group=grpL, tooltip="Bars on each side that confirm a swing high / low. A level appears only after this many bars have closed, so it never repaints.")
zoneMult = input.float(0.35, "Zone Width (x ATR)", minval=0.05, maxval=2.0, step=0.05, group=grpL)
mergeMult = input.float(0.60, "Merge Distance (x ATR)", minval=0.1, maxval=3.0, step=0.05, group=grpL, tooltip="A new swing this close to an existing level adds a touch to it instead of making a new level.")
maxBreaks = input.int(2, "Remove Level After Breaks", minval=1, maxval=5, group=grpL)
maxLevels = input.int(40, "Max Levels Tracked", minval=10, maxval=80, group=grpL)
grpT = "Test Rules"
reactATR = input.float(1.0, "Hold = Move Away of (x ATR)", minval=0.25, maxval=5.0, step=0.25, group=grpT, tooltip="A test counts as HELD when price moves this far away from the zone before it closes beyond the zone.")
breakBuf = input.float(0.15, "Break = Close Beyond Zone + (x ATR)", minval=0.0, maxval=1.0, step=0.05, group=grpT)
testWindow = input.int(20, "Test Window (bars)", minval=3, maxval=200, group=grpT, tooltip="If neither hold nor break happens within this many bars, the test has no result and is not counted.")
cooldown = input.int(5, "Test Cooldown (bars)", minval=1, maxval=50, group=grpT)
grpG = "Test Grade"
trendLen = input.int(100, "Trend EMA Length", minval=10, maxval=500, group=grpG)
wickA = input.float(0.50, "Long Wick = Wick ≥ (x candle range)", minval=0.1, maxval=0.9, step=0.05, group=grpG)
calmRatio = input.float(0.80, "Calm Market = ATR ≤ (x its 100-bar average)", minval=0.3, maxval=1.5, step=0.05, group=grpG)
grpD = "Display"
nShow = input.int(3, "Levels Shown per Side", minval=1, maxval=8, group=grpD)
maxDistATR = input.float(8.0, "Hide Levels Beyond (x ATR)", minval=1.0, maxval=50.0, step=0.5, group=grpD)
showLabels = input.bool(true, "Show Level Labels", group=grpD)
markerMode = input.string("A + B", "Test Markers", options=["A only", "A + B", "All", "None"], group=grpD)
maxEvents = input.int(150, "Markers Kept on Chart", minval=10, maxval=300, group=grpD)
showHTF = input.bool(true, "Show Prev Day / Week High-Low Lines", group=grpD)
showDash = input.bool(true, "Show Dashboard", group=grpD)
dashPosIn = input.string("Top Right", "Dashboard Position", options=["Top Right", "Top Left", "Bottom Right", "Bottom Left"], group=grpD)
textSizeIn = input.string("Small", "Text Size", options=["Tiny", "Small", "Normal"], group=grpD)
grpC = "Colors"
supCol = input.color(#089981, "Support", group=grpC)
resCol = input.color(#F23645, "Resistance", group=grpC)
retCol = input.color(#FF9800, "Retest of Flipped Level", group=grpC)
htfCol = input.color(#2962FF, "Prev Day / Week", group=grpC)
fadeCol = input.color(#9598A1, "Failed Test", group=grpC)
txtSize = textSizeIn == "Tiny" ? size.tiny : textSizeIn == "Normal" ? size.normal : size.small
dashPos = dashPosIn == "Top Left" ? position.top_left : dashPosIn == "Bottom Right" ? position.bottom_right : dashPosIn == "Bottom Left" ? position.bottom_left : position.top_right
maxGradeShown = markerMode == "A only" ? 0 : markerMode == "A + B" ? 1 : markerMode == "All" ? 2 : -1
gName = array.from("A", "B", "C")
// ─────────────────────────── Data ───────────────────────────
type Level
float mid
float halfW
int born
int touches
int holds
int breaks
int lastTest
bool flipped
bool wasBroken
int pDir
int pBar
int pGrade
bool pRetest
label pLab
var array<Level> levels = array.new<Level>()
atr = ta.atr(14)
atrAvg = ta.sma(atr, 100)
ema = ta.ema(close, trendLen)
trendUp = close > ema and ema > ema[5]
trendDn = close < ema and ema < ema[5]
ph = ta.pivothigh(high, pivLen, pivLen)
pl = ta.pivotlow(low, pivLen, pivLen)
pdh = request.security(syminfo.tickerid, "D", high[1], lookahead=barmerge.lookahead_on)
pdl = request.security(syminfo.tickerid, "D", low[1], lookahead=barmerge.lookahead_on)
pwh = request.security(syminfo.tickerid, "W", high[1], lookahead=barmerge.lookahead_on)
pwl = request.security(syminfo.tickerid, "W", low[1], lookahead=barmerge.lookahead_on)
f_top(Level lv) => lv.mid + lv.halfW
f_bot(Level lv) => lv.mid - lv.halfW
// measured hold rate per grade on this chart: tests [0..2], holds [3..5]
var array<int> gs = array.from(0, 0, 0, 0, 0, 0)
f_rateNum(int g) => gs.get(g) > 0 ? gs.get(g + 3) * 100.0 / gs.get(g) : na
f_rateTxt(int g) =>
float r = f_rateNum(g)
na(r) ? "learning" : str.tostring(r, "0") + "%"
// Eye Algo test grade, known at the close of the test candle
// close back outside the zone 3 · long wick 2 (half wick 1) · with trend 2 (against 1) · calm market 1 → A ≥ 6, B 4-5, C ≤ 3
f_grade(int dir, float top, float bot) =>
int cloc = dir == 1 ? (close > top ? 0 : close >= bot ? 1 : 2) : (close < bot ? 0 : close <= top ? 1 : 2)
float rng = math.max(high - low, syminfo.mintick)
float wr = dir == 1 ? (math.min(open, close) - low) / rng : (high - math.max(open, close)) / rng
bool withT = dir == 1 ? trendUp : trendDn
bool against = dir == 1 ? trendDn : trendUp
bool calm = nz(atr / atrAvg, 1.0) <= calmRatio
int q = (cloc == 0 ? 3 : cloc == 1 ? 1 : 0) + (wr >= wickA ? 2 : wr >= wickA / 2 ? 1 : 0) + (withT ? 2 : against ? 1 : 0) + (calm ? 1 : 0)
q >= 6 ? 0 : q >= 4 ? 1 : 2
// ─────────────────────────── Events (this bar) ───────────────────────────
bool evTestA_S = false
bool evTestA_R = false
bool evTestS = false
bool evTestR = false
bool evSupBreak = false
bool evResBreak = false
bool evRetest = false
string alertTxt = ""
var array<label> evLabels = array.new<label>()
f_keep(label lb) =>
evLabels.push(lb)
if evLabels.size() > maxEvents
evLabels.shift().delete()
// ─────────────────────────── New levels from confirmed swings ───────────────────────────
f_addSwing(float p) =>
int pb = bar_index - pivLen
float w = zoneMult * atr / 2.0
int best = -1
float bestD = mergeMult * atr
if levels.size() > 0
for i = 0 to levels.size() - 1
float d = math.abs(levels.get(i).mid - p)
if d <= bestD
bestD := d
best := i
if best >= 0
Level lv = levels.get(best)
lv.mid := (lv.mid * lv.touches + p) / (lv.touches + 1)
lv.halfW := w
lv.touches += 1
lv.born := math.min(lv.born, pb)
else
levels.push(Level.new(p, w, pb, 1, 0, 0, bar_index, false, false, 0, 0, 2, false, na))
if levels.size() > maxLevels
int old = -1
for i = 0 to levels.size() - 1
Level c = levels.get(i)
if c.pDir == 0 and (old < 0 or c.lastTest < levels.get(old).lastTest)
old := i
if old >= 0
levels.remove(old)
true
if barstate.isconfirmed and not na(atr) and atr > 0
if not na(ph)
f_addSwing(ph)
if not na(pl)
f_addSwing(pl)
// ─────────────────────────── Tests: grade, hold, break (bar close only) ───────────────────────────
if barstate.isconfirmed and not na(atr) and levels.size() > 0
array<int> kill = array.new<int>()
for i = 0 to levels.size() - 1
Level lv = levels.get(i)
float top = f_top(lv)
float bot = f_bot(lv)
// a new test: price comes into the zone from outside
if lv.pDir == 0 and high >= bot and low <= top and bar_index - lv.lastTest > cooldown
int dir = close[1] > top ? 1 : close[1] < bot ? -1 : 0
if dir != 0
int g = f_grade(dir, top, bot)
lv.pDir := dir
lv.pBar := bar_index
lv.pGrade := g
lv.pRetest := lv.wasBroken
lv.lastTest := bar_index
lv.pLab := na
bool sup = dir == 1
string gtx = gName.get(g) + " " + f_rateTxt(g)
string px = str.tostring(lv.mid, format.mintick)
if g <= maxGradeShown
color c = lv.pRetest ? retCol : sup ? supCol : resCol
label lb = label.new(bar_index, sup ? low : high, (sup ? "▲ " : "▼ ") + gtx, yloc=sup ? yloc.belowbar : yloc.abovebar, style=sup ? label.style_label_up : label.style_label_down, color=color.new(c, g == 0 ? 0 : 35), textcolor=color.white, size=size.tiny, tooltip=(lv.pRetest ? "Retest of flipped level " : sup ? "Support test " : "Resistance test ") + px + "\nGrade " + gName.get(g) + " · measured hold rate for this grade so far: " + f_rateTxt(g))
lv.pLab := lb
f_keep(lb)
if sup
evTestS := true
evTestA_S := evTestA_S or g == 0
else
evTestR := true
evTestA_R := evTestA_R or g == 0
evRetest := evRetest or lv.pRetest
alertTxt += (lv.pRetest ? "Retest " : sup ? "Support test " : "Resistance test ") + px + " grade " + gtx + "; "
// resolve an open test
if lv.pDir != 0
bool broke = lv.pDir == 1 ? close < bot - breakBuf * atr : close > top + breakBuf * atr
bool held = bar_index > lv.pBar and (lv.pDir == 1 ? high - top >= reactATR * atr : bot - low >= reactATR * atr)
int g = lv.pGrade
if broke or held
gs.set(g, gs.get(g) + 1)
if held and not broke
gs.set(g + 3, gs.get(g + 3) + 1)
if broke
string px = str.tostring(lv.mid, format.mintick)
if not na(lv.pLab)
lv.pLab.set_color(color.new(fadeCol, 40))
lv.pLab.set_text(lv.pLab.get_text() + " ✕")
if lv.pDir == 1
evSupBreak := true
alertTxt += "Support break " + px + "; "
else
evResBreak := true
alertTxt += "Resistance break " + px + "; "
lv.breaks += 1
lv.flipped := true
lv.wasBroken := true
lv.pDir := 0
if lv.breaks >= maxBreaks
kill.push(i)
else if held
if not na(lv.pLab)
lv.pLab.set_text(lv.pLab.get_text() + " ✓")
lv.holds += 1
lv.pDir := 0
if lv.pRetest
lv.wasBroken := false
else if bar_index - lv.pBar >= testWindow
lv.pDir := 0
else
bool crossDn = close[1] >= bot - breakBuf * atr and close < bot - breakBuf * atr and close[1] > lv.mid
bool crossUp = close[1] <= top + breakBuf * atr and close > top + breakBuf * atr and close[1] < lv.mid
if crossDn or crossUp
string px = str.tostring(lv.mid, format.mintick)
if crossDn
evSupBreak := true
alertTxt += "Support break " + px + "; "
else
evResBreak := true
alertTxt += "Resistance break " + px + "; "
lv.breaks += 1
lv.flipped := true
lv.wasBroken := true
if lv.breaks >= maxBreaks
kill.push(i)
if kill.size() > 0
for k = kill.size() - 1 to 0
levels.remove(kill.get(k))
// ─────────────────────────── Draw (last bar) ───────────────────────────
f_pick(bool wantSup) =>
array<float> d = array.new<float>()
array<int> ix = array.new<int>()
array<int> out = array.new<int>()
if levels.size() > 0 and not na(atr)
for i = 0 to levels.size() - 1
Level lv = levels.get(i)
if (lv.mid <= close) == wantSup
float dist = math.abs(close - lv.mid)
if dist <= maxDistATR * atr
d.push(dist)
ix.push(i)
if d.size() > 0
array<int> ord = d.sort_indices(order.ascending)
for k = 0 to math.min(nShow, ord.size()) - 1
out.push(ix.get(ord.get(k)))
out
var array<box> dBoxes = array.new<box>()
var array<label> dLabels = array.new<label>()
var array<line> hLines = array.new<line>()
f_heldTxt(Level lv) =>
int t = lv.holds + lv.breaks
t > 0 ? "Held " + str.tostring(lv.holds) + " of " + str.tostring(t) : "Not tested yet"
f_drawLevel(Level lv, bool isSup) =>
color c = isSup ? supCol : resCol
int tr = 88 - math.min(lv.touches, 5) * 5
int x1 = math.max(lv.born, bar_index - 4900)
dBoxes.push(box.new(x1, f_top(lv), bar_index + 1, f_bot(lv), border_color=color.new(c, 35), border_style=lv.flipped ? line.style_dashed : line.style_solid, bgcolor=color.new(c, tr), extend=extend.right))
if showLabels
string tx = (isSup ? "Support " : "Resistance ") + str.tostring(lv.mid, format.mintick) + " · Touches " + str.tostring(lv.touches) + " · " + f_heldTxt(lv) + (lv.flipped ? " · Flipped" : "")
dLabels.push(label.new(bar_index + 3, lv.mid, tx, style=label.style_label_left, color=color.new(c, 100), textcolor=c, size=txtSize))
f_htfLine(float p, string name) =>
if not na(p) and not na(atr) and math.abs(close - p) <= maxDistATR * atr
hLines.push(line.new(bar_index - 30, p, bar_index + 25, p, color=color.new(htfCol, 30), style=line.style_dotted, width=1))
dLabels.push(label.new(bar_index + 25, p, name, style=label.style_label_left, color=color.new(htfCol, 100), textcolor=htfCol, size=size.tiny))
var table dash = table.new(dashPos, 2, 10, bgcolor=color.new(chart.bg_color, 0), frame_color=color.new(chart.fg_color, 80), frame_width=1, border_color=color.new(chart.fg_color, 90), border_width=1)
var bool merged = false
f_cell(int row, string k, string v, color vc) =>
table.cell(dash, 0, row, k, text_color=color.new(chart.fg_color, 25), text_size=txtSize, text_halign=text.align_left)
table.cell(dash, 1, row, v, text_color=vc, text_size=txtSize, text_halign=text.align_right)
f_gradeRow(int g) =>
int t = gs.get(g)
t > 0 ? str.tostring(gs.get(g + 3) * 100.0 / t, "0") + "% (" + str.tostring(t) + " tests)" : "—"
var string lastTxt = "—"
var color lastCol = chart.fg_color
if barstate.isconfirmed and levels.size() > 0
for lv in levels
if lv.pDir != 0 and lv.pBar == bar_index
lastTxt := gName.get(lv.pGrade) + " · " + (lv.pRetest ? "Retest " : lv.pDir == 1 ? "Support " : "Resistance ") + str.tostring(lv.mid, format.mintick)
lastCol := lv.pRetest ? retCol : lv.pDir == 1 ? supCol : resCol
if barstate.islast
for b in dBoxes
b.delete()
dBoxes.clear()
for l in dLabels
l.delete()
dLabels.clear()
for ln in hLines
ln.delete()
hLines.clear()
array<int> sIdx = f_pick(true)
array<int> rIdx = f_pick(false)
for i in sIdx
f_drawLevel(levels.get(i), true)
for i in rIdx
f_drawLevel(levels.get(i), false)
if showHTF
if timeframe.in_seconds() < 86400
f_htfLine(pdh, "Prev Day High")
f_htfLine(pdl, "Prev Day Low")
if timeframe.in_seconds() < 604800
f_htfLine(pwh, "Prev Week High")
f_htfLine(pwl, "Prev Week Low")
if showDash
if not merged
table.merge_cells(dash, 0, 0, 1, 0)
table.merge_cells(dash, 0, 9, 1, 9)
merged := true
table.cell(dash, 0, 0, "Eye Level Strength", text_color=chart.fg_color, text_size=txtSize, text_halign=text.align_center)
string sTxt = "—"
string rTxt = "—"
string verdict = "No level in range"
color vCol = chart.fg_color
if sIdx.size() > 0
Level s = levels.get(sIdx.get(0))
sTxt := str.tostring(s.mid, format.mintick) + " · " + str.tostring((close - s.mid) / atr, "0.0") + " ATR · " + f_heldTxt(s)
if rIdx.size() > 0
Level r = levels.get(rIdx.get(0))
rTxt := str.tostring(r.mid, format.mintick) + " · " + str.tostring((r.mid - close) / atr, "0.0") + " ATR · " + f_heldTxt(r)
if sIdx.size() > 0 or rIdx.size() > 0
float dS = sIdx.size() > 0 ? close - levels.get(sIdx.get(0)).mid : na
float dR = rIdx.size() > 0 ? levels.get(rIdx.get(0)).mid - close : na
bool atS = sIdx.size() > 0 and close <= f_top(levels.get(sIdx.get(0)))
bool atR = rIdx.size() > 0 and close >= f_bot(levels.get(rIdx.get(0)))
if atS
verdict := "Testing Support"
vCol := supCol
else if atR
verdict := "Testing Resistance"
vCol := resCol
else if na(dR) or (not na(dS) and dS < dR)
verdict := "Closer to Support"
vCol := supCol
else
verdict := "Closer to Resistance"
vCol := resCol
int allT = gs.get(0) + gs.get(1) + gs.get(2)
int allH = gs.get(3) + gs.get(4) + gs.get(5)
f_cell(1, "Nearest Support", sTxt, supCol)
f_cell(2, "Nearest Resistance", rTxt, resCol)
f_cell(3, "Last Test", lastTxt, lastCol)
f_cell(4, "Grade A Hold Rate", f_gradeRow(0), supCol)
f_cell(5, "Grade B Hold Rate", f_gradeRow(1), chart.fg_color)
f_cell(6, "Grade C Hold Rate", f_gradeRow(2), resCol)
f_cell(7, "All Tests", allT > 0 ? str.tostring(allH * 100.0 / allT, "0") + "% (" + str.tostring(allT) + " tests)" : "—", chart.fg_color)
f_cell(8, "Now", verdict, vCol)
table.cell(dash, 0, 9, "Hold = " + str.tostring(reactATR, "0.0#") + " ATR move before a close beyond the zone · in-sample, this chart, before costs", text_color=color.new(chart.fg_color, 45), text_size=size.tiny, text_halign=text.align_center)
// ─────────────────────────── Alerts ───────────────────────────
alertcondition(barstate.isconfirmed and evTestA_S, "Grade A Support Test", "Eye Level Strength: grade A support test on {{ticker}} {{interval}}")
alertcondition(barstate.isconfirmed and evTestA_R, "Grade A Resistance Test", "Eye Level Strength: grade A resistance test on {{ticker}} {{interval}}")
alertcondition(barstate.isconfirmed and evTestS, "Support Test (any grade)", "Eye Level Strength: support test on {{ticker}} {{interval}}")
alertcondition(barstate.isconfirmed and evTestR, "Resistance Test (any grade)", "Eye Level Strength: resistance test on {{ticker}} {{interval}}")
alertcondition(barstate.isconfirmed and evRetest, "Retest of Flipped Level", "Eye Level Strength: broken level retested on {{ticker}} {{interval}}")
alertcondition(barstate.isconfirmed and evSupBreak, "Support Break", "Eye Level Strength: support broken on {{ticker}} {{interval}}")
alertcondition(barstate.isconfirmed and evResBreak, "Resistance Break", "Eye Level Strength: resistance broken on {{ticker}} {{interval}}")
if barstate.isconfirmed and alertTxt != ""
alert("Eye Level Strength " + syminfo.ticker + " " + timeframe.period + ": " + alertTxt + "close " + str.tostring(close, format.mintick), alert.freq_once_per_bar_close)