本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
// Taught to Trade - Support & Resistance Touch Count
//
// Support and resistance zones drawn from confirmed pivots, with the one number most S/R tools
// never show: how often price actually came back into those zones - compared against the same
// count for plain round-number levels of the same width, over the same window, in the same
// price band.
//
// If pivot zones get entered about as often as arbitrary round numbers, the zones are measuring
// price's general tendency to revisit, not anything special about pivots. That comparison is the
// point of the tool.
//
// Marking where price traded is a fact. Nothing here predicts, ranks or scores anything, and no
// buy/sell signal is sent. Alerts are conditions you arm yourself.
//
// All ta.* calls are at top level. Loops and conditional blocks touch only arrays and var scalars.
indicator("Taught to Trade - Support & Resistance Touch Count", shorttitle = "TT SR TOUCH",
overlay = true, max_boxes_count = 50, max_bars_back = 5000)
// ─────────────────────────────── Inputs ───────────────────────────────
grpZ = "Zones"
pivLen = input.int(10, "Pivot lookback (bars each side)", minval = 2, maxval = 100, group = grpZ,
tooltip = "A pivot is only confirmed this many bars after it forms. Zones come from confirmed pivots only, and counting starts at the confirmation bar.")
nZones = input.int(8, "Zones kept (most recent)", minval = 2, maxval = 20, group = grpZ)
wMult = input.float(0.25, "Zone half-width (x ATR)", minval = 0.05, maxval = 2.0, step = 0.05, group = grpZ,
tooltip = "Zone width is a free parameter. Wider zones are entered more often purely because they are bigger, so it is kept visible and applied identically to the control levels.")
atrLen = input.int(14, "ATR length", minval = 2, group = grpZ)
grpC = "Control"
showCtl = input.bool(true, "Compare against round-number levels", group = grpC,
tooltip = "Counts entries into evenly spaced round-number levels with the same width, over the same window, inside the same price band as the zones.")
stepIn = input.float(0.0, "Round-number step (0 = auto)", minval = 0.0, group = grpC,
tooltip = "Auto picks a round step near four ATRs: 1, 2 or 5 times a power of ten.")
grpD = "Draw"
showZones = input.bool(true, "Zones", group = grpD)
showTbl = input.bool(true, "Readout table", group = grpD)
tblPosIn = input.string("Bottom left", "Table position", group = grpD,
options = ["Bottom left", "Bottom right", "Top left", "Top right", "Middle right"],
tooltip = "Bottom left sits over the oldest bars, away from the price scale and the badges TradingView draws in the bottom-right corner.")
colHi = input.color(color.new(#8A94A6, 0), "Zones from pivot highs", group = grpD, inline = "c1")
colLo = input.color(color.new(#2F6FED, 0), "Zones from pivot lows", group = grpD, inline = "c1")
// ──────────────────────── Series (top level only) ────────────────────────
atr = ta.atr(atrLen)
ph = ta.pivothigh(pivLen, pivLen)
pl = ta.pivotlow(pivLen, pivLen)
atrAtPiv = atr[pivLen]
// ─────────────────────────────── Zone storage ───────────────────────────────
var array<float> zMid = array.new<float>()
var array<float> zHw = array.new<float>()
var array<int> zBorn = array.new<int>()
var array<int> zEnt = array.new<int>()
var array<bool> zIn = array.new<bool>()
var array<bool> zHiSide = array.new<bool>()
var array<box> zBox = array.new<box>()
// ─────────────── Count entries into existing zones (every bar) ───────────────
// An entry is a bar that overlaps the zone after the previous bar did not.
bool entHi = false
bool entLo = false
int nzNow = array.size(zMid)
if nzNow > 0
for i = 0 to nzNow - 1
float m = array.get(zMid, i)
float hw = array.get(zHw, i)
bool inNow = high >= m - hw and low <= m + hw
if inNow and not array.get(zIn, i)
array.set(zEnt, i, array.get(zEnt, i) + 1)
if array.get(zHiSide, i)
entHi := true
else
entLo := true
array.set(zIn, i, inNow)
// ─────────────── Add new zones from pivots confirmed on this bar ───────────────
f_add(float mid, bool isHi, float hw) =>
bool inNow = high >= mid - hw and low <= mid + hw
array.push(zMid, mid)
array.push(zHw, hw)
array.push(zBorn, bar_index)
array.push(zEnt, 0)
array.push(zIn, inNow)
array.push(zHiSide, isHi)
color c = isHi ? colHi : colLo
box b = showZones ? box.new(bar_index - pivLen, mid + hw, bar_index, mid - hw,
border_color = color.new(c, 40), bgcolor = color.new(c, 85), extend = extend.right) : na
array.push(zBox, b)
if array.size(zMid) > nZones
array.shift(zMid)
array.shift(zHw)
array.shift(zBorn)
array.shift(zEnt)
array.shift(zIn)
array.shift(zHiSide)
box old = array.shift(zBox)
if not na(old)
box.delete(old)
true
hwNew = na(atrAtPiv) ? 0.0 : atrAtPiv * wMult
if not na(ph)
f_add(ph, true, hwNew)
if not na(pl)
f_add(pl, false, hwNew)
// ─────────────────────────────── Helpers ───────────────────────────────
f_autoStep(float px, float a) =>
float x = na(a) or a <= 0 ? px * 0.02 : a * 4.0
float mag = math.pow(10.0, math.floor(math.log10(x)))
float r = x / mag
float nice = r < 1.5 ? 1.0 : r < 3.5 ? 2.0 : r < 7.5 ? 5.0 : 10.0
nice * mag
// ─────────────────────────────── Table ───────────────────────────────
bgCol = color.new(#0E1526, 10)
txtCol = color.new(#E6EAF2, 0)
hdrCol = color.new(#2F6FED, 0)
tblPos = tblPosIn == "Bottom right" ? position.bottom_right : tblPosIn == "Top left" ? position.top_left :
tblPosIn == "Top right" ? position.top_right : tblPosIn == "Middle right" ? position.middle_right : position.bottom_left
var table t = table.new(tblPos, 2, 9, border_width = 1, border_color = color.new(#2F6FED, 70))
f_row(int r, string k, string v) =>
table.cell(t, 0, r, k, text_color = txtCol, text_size = size.small, text_halign = text.align_left, bgcolor = bgCol)
table.cell(t, 1, r, v, text_color = txtCol, text_size = size.small, text_halign = text.align_right, bgcolor = bgCol)
if barstate.islast and showTbl
int n = array.size(zMid)
int totEnt = 0
int totExp = 0
int wStart = bar_index
float bandLo = na
float bandHi = na
if n > 0
for i = 0 to n - 1
totEnt += array.get(zEnt, i)
int b0 = array.get(zBorn, i)
totExp += bar_index - b0
wStart := math.min(wStart, b0)
float m = array.get(zMid, i)
float hw = array.get(zHw, i)
bandLo := na(bandLo) ? m - hw : math.min(bandLo, m - hw)
bandHi := na(bandHi) ? m + hw : math.max(bandHi, m + hw)
float zRate = totExp > 0 ? float(totEnt) / totExp * 100.0 : na
// Control: entries into round-number levels, same width rule, same window, same band.
float step = stepIn > 0 ? stepIn : f_autoStep(close, atr)
int W = bar_index - wStart
float cRate = na
int nLev = 0
if showCtl and W > 1 and not na(bandLo) and step > 0
int kLo = int(math.ceil(bandLo / step))
int kHi = int(math.floor(bandHi / step))
nLev := kHi - kLo + 1
if nLev > 0
int cEnt = 0
float hw0 = nz(atr[W]) * wMult
int pa = math.max(kLo, int(math.ceil((low[W] - hw0) / step)))
int pb = math.min(kHi, int(math.floor((high[W] + hw0) / step)))
for off = W - 1 to 0
float hwc = nz(atr[off]) * wMult
int ca = math.max(kLo, int(math.ceil((low[off] - hwc) / step)))
int cb = math.min(kHi, int(math.floor((high[off] + hwc) / step)))
int cur = math.max(0, cb - ca + 1)
int ov = math.max(0, math.min(cb, pb) - math.max(ca, pa) + 1)
cEnt += cur - ov
pa := ca
pb := cb
cRate := float(cEnt) / (nLev * W) * 100.0
string ratioTxt = na(zRate) or na(cRate) or cRate <= 0 ? "-" : str.tostring(zRate / cRate, "#.##") + "x"
table.cell(t, 0, 0, "S/R TOUCH COUNT", text_color = color.white, text_size = size.small, bgcolor = hdrCol)
table.cell(t, 1, 0, str.tostring(n) + " zones", text_color = color.white, text_size = size.small, bgcolor = hdrCol)
f_row(1, "Pivot lookback", str.tostring(pivLen) + " bars each side")
f_row(2, "Zone half-width", str.tostring(wMult) + " x ATR")
f_row(3, "Zone entries", str.tostring(totEnt) + " in " + str.tostring(totExp) + " zone-bars")
f_row(4, "Zones /100 bars", na(zRate) ? "-" : str.tostring(zRate, "#.##"))
f_row(5, "Round levels /100 bars", na(cRate) ? "-" : str.tostring(cRate, "#.##"))
f_row(6, "Round levels used", na(cRate) ? "-" : str.tostring(nLev) + ", step " + str.tostring(step))
f_row(7, "Zones vs round levels", ratioTxt)
f_row(8, "Window", str.tostring(W) + " bars")
// ─────────────── Alerts (conditions you arm yourself) ───────────────
alertcondition(entHi, title = "Price entered a zone from a pivot high",
message = "S/R Touch Count: price entered a zone drawn from a confirmed pivot high.")
alertcondition(entLo, title = "Price entered a zone from a pivot low",
message = "S/R Touch Count: price entered a zone drawn from a confirmed pivot low.")