本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Daily ATR Targets", shorttitle="D-ATR", overlay=true, max_labels_count=100)
// ===== Settings =====
G1 = "ATR"
method = input.string("ATR (Wilder)", "Calculation method", options=["ATR (Wilder)", "SMA True Range", "Average body |C-O|", "Average range H-L"], group=G1)
len = input.int(14, "Period (days)", minval=1, maxval=200, group=G1)
anchorSrc = input.string("Day open", "Levels anchor", options=["Day open", "Previous close"], group=G1)
G2 = "Levels"
show1 = input.bool(true, "Level 1", inline="l1", group=G2)
lvl1 = input.float(0.5, "x ATR", minval=0.05, step=0.05, inline="l1", group=G2)
show2 = input.bool(true, "Level 2", inline="l2", group=G2)
lvl2 = input.float(1.0, "x ATR", minval=0.05, step=0.05, inline="l2", group=G2)
show3 = input.bool(true, "Level 3", inline="l3", group=G2)
lvl3 = input.float(1.5, "x ATR", minval=0.05, step=0.05, inline="l3", group=G2)
colUp = input.color(color.new(#26a69a, 0), "Up color", inline="c", group=G2)
colDn = input.color(color.new(#ef5350, 0), "Down color", inline="c", group=G2)
colAn = input.color(color.new(#b2b5be, 0), "Anchor color", inline="c", group=G2)
showExt = input.bool(true, "Low+ATR / High-ATR dots", group=G2)
colExt = input.color(color.new(#ffb74d, 0), "Extremes color", group=G2)
G3 = "Table"
showTbl = input.bool(true, "Show table", group=G3)
tblPos = input.string(position.top_right, "Position", options=[position.top_left, position.top_center, position.top_right, position.middle_left, position.middle_right, position.bottom_left, position.bottom_center, position.bottom_right], group=G3)
tblSizeS = input.string(size.small, "Text size", options=[size.tiny, size.small, size.normal], group=G3)
// ===== Daily data (completed days only) =====
validTf = 86400 > timeframe.in_seconds() or timeframe.in_seconds() == 86400
f_daily() =>
atrW = ta.atr(len)
trS = ta.sma(ta.tr(true), len)
body = ta.sma(math.abs(close - open), len)
rng = ta.sma(high - low, len)
sel = method == "ATR (Wilder)" ? atrW : method == "SMA True Range" ? trS : method == "Average body |C-O|" ? body : rng
hit = ta.sma((high - low) > sel[1] ? 1.0 : 0.0, 30)
[sel[1], atrW[1], body[1], rng[1], sel[6], hit[1]]
[atrD, atrWD, bodyD, rngD, atr5D, hitD] = request.security(syminfo.tickerid, "D", f_daily(), lookahead=barmerge.lookahead_on)
// ===== Current day =====
newDay = timeframe.change("D")
var float dOpen = na
var float pClose = na
var float dHigh = na
var float dLow = na
if newDay
pClose := close[1]
dOpen := open
dHigh := high
dLow := low
else
dHigh := math.max(nz(dHigh, high), high)
dLow := math.min(nz(dLow, low), low)
anchor = anchorSrc == "Day open" ? dOpen : pClose
// ===== Levels =====
ok = validTf and not na(atrD) and not na(anchor)
up1 = ok ? anchor + atrD * lvl1 : na
up2 = ok ? anchor + atrD * lvl2 : na
up3 = ok ? anchor + atrD * lvl3 : na
dn1 = ok ? anchor - atrD * lvl1 : na
dn2 = ok ? anchor - atrD * lvl2 : na
dn3 = ok ? anchor - atrD * lvl3 : na
extUp = ok ? dLow + atrD : na
extDn = ok ? dHigh - atrD : na
tgtUp = ok ? anchor + atrD : na
tgtDn = ok ? anchor - atrD : na
plot(ok ? anchor : na, "Anchor", color=newDay ? na : colAn, linewidth=1)
plot(show1 ? up1 : na, "+L1", color=newDay ? na : color.new(colUp, 40), linewidth=1)
plot(show2 ? up2 : na, "+L2", color=newDay ? na : colUp, linewidth=2)
plot(show3 ? up3 : na, "+L3", color=newDay ? na : color.new(colUp, 40), linewidth=1)
plot(show1 ? dn1 : na, "-L1", color=newDay ? na : color.new(colDn, 40), linewidth=1)
plot(show2 ? dn2 : na, "-L2", color=newDay ? na : colDn, linewidth=2)
plot(show3 ? dn3 : na, "-L3", color=newDay ? na : color.new(colDn, 40), linewidth=1)
plot(showExt ? extUp : na, "Low+ATR", color=newDay ? na : color.new(colExt, 30), linewidth=1, style=plot.style_circles)
plot(showExt ? extDn : na, "High-ATR", color=newDay ? na : color.new(colExt, 30), linewidth=1, style=plot.style_circles)
// ===== Alerts =====
var bool hitUpFlag = false
var bool hitDnFlag = false
bool upNow = false
bool dnNow = false
if newDay
hitUpFlag := false
hitDnFlag := false
if ok
if not hitUpFlag and high > tgtUp
hitUpFlag := true
upNow := true
if not hitDnFlag and tgtDn > low
hitDnFlag := true
dnNow := true
alertcondition(upNow, "Upside target reached (1 ATR)", "Price moved +1 daily ATR from the anchor")
alertcondition(dnNow, "Downside target reached (1 ATR)", "Price moved -1 daily ATR from the anchor")
// ===== Table =====
bgc = color.new(#131722, 8)
var table tbl = table.new(tblPos, 2, 14, bgcolor=bgc, border_width=1, border_color=color.new(#363a45, 0), frame_width=1, frame_color=color.new(#363a45, 0))
f_row(int r, string k, string v, color vc) =>
table.cell(tbl, 0, r, k, text_color=color.new(color.white, 25), text_halign=text.align_left, bgcolor=bgc, text_size=tblSizeS)
table.cell(tbl, 1, r, v, text_color=vc, text_halign=text.align_left, bgcolor=bgc, text_size=tblSizeS)
f_num(float v, string f) =>
na(v) ? "-" : str.tostring(v, f)
f_bar(float pct) =>
float p = na(pct) ? 0.0 : pct
int n = math.max(0, math.min(10, int(math.round(p / 10))))
str.repeat("#", n) + str.repeat("-", 10 - n)
if showTbl and barstate.islast
string fmt = format.mintick
color cW = color.white
float rangeT = nz(dHigh, high) - nz(dLow, low)
float pctUsed = na(atrD) ? na : rangeT / atrD * 100
float move = close - nz(dOpen, open)
float moveATR = na(atrD) ? na : move / atrD
bool hitUp = ok and dHigh > tgtUp
bool hitDn = ok and tgtDn > dLow
float remUp = tgtUp - close
float remDn = close - tgtDn
color usedCol = na(pctUsed) ? cW : 50 > pctUsed ? color.new(#26a69a, 0) : 80 > pctUsed ? color.new(#fdd835, 0) : 100 > pctUsed ? color.new(#ff9800, 0) : color.new(#ef5350, 0)
string status = na(pctUsed) ? "-" : 50 > pctUsed ? "lots of room left" : 80 > pctUsed ? "room left" : 100 > pctUsed ? "near the norm" : "norm exceeded"
float volChg = na(atr5D) ? na : (atrD / atr5D - 1) * 100
string volTxt = na(volChg) ? "-" : (volChg > 0 ? "UP +" : "DOWN ") + str.tostring(volChg, "#.#") + "%"
color volCol = na(volChg) ? cW : volChg > 0 ? color.new(#ff9800, 0) : color.new(#26a69a, 0)
table.cell(tbl, 0, 0, "Daily ATR " + syminfo.ticker, text_color=color.new(#ffd54f, 0), text_halign=text.align_left, bgcolor=color.new(#1e2a44, 0), text_size=tblSizeS)
table.cell(tbl, 1, 0, validTf ? "" : "timeframe above 1D", text_color=color.new(#ef5350, 0), text_halign=text.align_left, bgcolor=color.new(#1e2a44, 0), text_size=tblSizeS)
f_row(1, "Method", method + " / " + str.tostring(len) + "d", cW)
f_row(2, "ATR (D)", f_num(atrD, fmt) + " (" + f_num(atrD / close * 100, "#.##") + "%)", color.new(#ffd54f, 0))
f_row(3, "Anchor: " + anchorSrc, f_num(anchor, fmt), cW)
f_row(4, "Target UP (1 ATR)", f_num(tgtUp, fmt), colUp)
f_row(5, "Target DOWN (1 ATR)", f_num(tgtDn, fmt), colDn)
f_row(6, "Used today", f_bar(pctUsed) + " " + f_num(pctUsed, "#") + "%", usedCol)
f_row(7, "Day range H-L", f_num(rangeT, fmt) + " (" + f_num(rangeT / atrD, "#.##") + " ATR) " + status, usedCol)
f_row(8, "Move from open", (move > 0 ? "+" : "") + f_num(move, fmt) + " (" + f_num(moveATR, "#.##") + " ATR)", move > 0 ? colUp : colDn)
f_row(9, "To target UP", hitUp ? "reached" : f_num(remUp, fmt) + " (" + f_num(remUp / atrD * 100, "#") + "% ATR)", hitUp ? colUp : cW)
f_row(10, "To target DOWN", hitDn ? "reached" : f_num(remDn, fmt) + " (" + f_num(remDn / atrD * 100, "#") + "% ATR)", hitDn ? colDn : cW)
f_row(11, "Days H-L above ATR (30d)", na(hitD) ? "-" : str.tostring(hitD * 100, "#") + "%", cW)
f_row(12, "ATR vs 5 days ago", volTxt, volCol)
f_row(13, "Other methods", "Wilder " + f_num(atrWD, fmt) + " | Body " + f_num(bodyD, fmt) + " | H-L " + f_num(rngD, fmt), cW)