Trends ATR

RafaelZioni · study · 41 行 · 点赞 265 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

//@version=6
indicator('Trends ATR', overlay = true)
 
// ── inputs ──
atrLen   = input.int(200, 'ATR length', minval = 1)
mult     = input.float(1.0, 'Step size (× ATR)', minval = 0.1, step = 0.1)
upCol    = input.color(color.lime, 'Up color')
dnCol    = input.color(color.red, 'Down color')
lineW    = input.int(3, 'Trend line width', minval = 1, maxval = 5)
showBand = input.bool(true, 'Show band')
 
step = ta.atr(atrLen) * mult
 
// ── step levels: price must move a full step beyond the level to shift it ──
f_step(float lvl, float src, float s) =>
    na(lvl) ? src : src > lvl + s ? lvl + s : src < lvl - s ? lvl - s : lvl
 
var float closeLvl = na   // trend line: stepped close
var float openLvl  = na   // stepped open
var float baseLvl  = na   // base: previous stepped-open level, updated on each shift
 
if not na(step)
    closeLvl := f_step(closeLvl, close, step)
    newOpen = f_step(openLvl, open, step)
    if not na(openLvl) and newOpen != openLvl
        baseLvl := openLvl
    openLvl := newOpen
 
// ── trend ──
isUp     = closeLvl > baseLvl
trendCol = isUp ? upCol : dnCol
flipUp   = isUp and not isUp[1]
flipDn   = not isUp and isUp[1]
 
// ── plots ──
pT = plot(closeLvl, 'Trend line', trendCol, lineW, plot.style_stepline)
pB = plot(baseLvl, 'Base line', color.new(trendCol, 60), 1, plot.style_stepline)
fill(pT, pB, showBand ? color.new(trendCol, 85) : na, 'Trend band')
 
alertcondition(flipUp, 'Trend turned up', 'Trends ATR: trend turned UP')
alertcondition(flipDn, 'Trend turned down', 'Trends ATR: trend turned DOWN')

← 返回列表