ATR Trail + Boxes & Signals — ALIGNED (ATR dir) + HTF + After Close + RANGE box

Aleksin_Aleksandar · study · 129 行 · 点赞 896 · TradingView 原页

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

Pine Script

//@version=6
indicator("ATR Structure Trail Pro & Range Filter (v6)", overlay=true, max_boxes_count=200, max_lines_count=500, max_labels_count=500)
 
//=========================
// INPUTS
//=========================
atrLen    = input.int(14, "ATR Length", minval=1)
atrMult   = input.float(1.5, "ATR Multiplier (Trail)", step=0.1)
 
pivotLen  = input.int(3, "Structure Pivot Len", minval=1)
 
showBoxes   = input.bool(true, "Show Trade/Range Boxes")
rangeBars   = input.int(18, "Top Range Detect Bars", minval=5)
rangeAtrMul = input.float(1.2, "Range Width (<= xATR)", step=0.1)
 
boxAlpha    = input.int(80, "Box Transparency", minval=0, maxval=100)
 
//=========================
// BOX COLORS
//=========================
tradeLongFill  = input.color(color.teal,   "Range Box Fill (LONG)")
tradeShortFill = input.color(color.purple, "Range Box Fill (SHORT)")
 
tradeBorderCol = input.color(color.white,  "Range Box Border Color")
tradeBorderTr  = input.int(70, "Range Box Border Transparency", minval=0, maxval=100)
tradeBorderW   = input.int(1,  "Range Box Border Width", minval=1, maxval=5)
 
//=========================
// ATR TRAIL LINE
//=========================
showAtrLine   = input.bool(true, "Show ATR Trail Line")
atrLongColor  = input.color(color.lime,  "ATR Line Color (LONG)")
atrShortColor = input.color(color.red,   "ATR Line Color (SHORT)")
atrLineWidth  = input.int(2, "ATR Trail Line Width", minval=1, maxval=6)
atrLineStyle  = input.string("Solid", "ATR Trail Line Style", options=["Solid", "Dashed", "Dotted"])
 
// style helper (FIXED: no line-break syntax error)
plotStyle = atrLineStyle == "Dashed" ? plot.style_linebr : atrLineStyle == "Dotted" ? plot.style_circles : plot.style_line
 
//=========================
// ATR
//=========================
atr = ta.atr(atrLen)
off = atr * atrMult
 
//=========================
// MANUAL ADX (ostavljeno)
//=========================
tr = math.max(math.max(high - low, math.abs(high - close[1])), math.abs(low - close[1]))
plusDM  = high - high[1] > low[1] - low ? math.max(high - high[1], 0) : 0
minusDM = low[1] - low > high - high[1] ? math.max(low[1] - low, 0) : 0
 
//=========================
// STRUCTURE PIVOTS (za flip)
//=========================
ph = ta.pivothigh(high, pivotLen, pivotLen)
pl = ta.pivotlow(low,  pivotLen, pivotLen)
 
var float lastPH = na
var float lastPL = na
 
if not na(ph)
    lastPH := ph
if not na(pl)
    lastPL := pl
 
//=========================
// DIRECTION + TRAIL
//=========================
var int   dir   = 0
var float trail = na
 
// flip po strukturi (pivoti)
longFlip  = not na(lastPH) and close > lastPH
shortFlip = not na(lastPL) and close < lastPL
 
if longFlip
    dir := 1
if shortFlip
    dir := -1
 
// ATR trailing (kao na slici 3): prati cenu
// Long: trail ne sme dole => max(prev, close - off)
// Short: trail ne sme gore => min(prev, close + off)
if dir == 1
    base  = close - off
    trail := na(trail) or dir[1] != 1 ? base : math.max(trail, base)
else if dir == -1
    base  = close + off
    trail := na(trail) or dir[1] != -1 ? base : math.min(trail, base)
else
    trail := na
 
//=========================
// ATR TRAIL PLOT (dual color)
//=========================
atrColor = dir == 1 ? atrLongColor : dir == -1 ? atrShortColor : color.gray
plot(showAtrLine ? trail : na, "ATR Trail", color=atrColor, linewidth=atrLineWidth, style=plotStyle)
 
//=========================
// RANGE DETECTION
//=========================
hhNow = ta.highest(high, rangeBars)
llNow = ta.lowest(low,  rangeBars)
isRange = (hhNow - llNow) <= (atr * rangeAtrMul)
 
//=========================
// BOXES
//=========================
var box tradeBox = na
 
if showBoxes and (longFlip or shortFlip)
    tradeBox := box.new(
        bar_index, high, bar_index, low,
        bgcolor=color.new(dir == 1 ? tradeLongFill : tradeShortFill, boxAlpha),
        border_color=color.new(tradeBorderCol, tradeBorderTr),
        border_width=tradeBorderW
    )
 
if showBoxes and not na(tradeBox)
    box.set_right(tradeBox, bar_index)
    box.set_top(tradeBox,    math.max(box.get_top(tradeBox), high))
    box.set_bottom(tradeBox, math.min(box.get_bottom(tradeBox), low))
 
//=========================
// SIGNALS (ONLY ARROWS)
//=========================
plotshape(longFlip,  "Buy Arrow",  shape.triangleup,   location.belowbar, color=color.new(color.lime, 0), size=size.tiny)
plotshape(shortFlip, "Sell Arrow", shape.triangledown, location.abovebar, color=color.new(color.red,  0), size=size.tiny)

← 返回列表