ATR Move Multiple (X Days)

Benjamin_Trades · study · 61 行 · 点赞 6 · TradingView 原页

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

Pine Script

//@version=6
indicator("ATR Move Multiple (X Days)", "ATR Mult", overlay=true)
 
// ── Inputs ──────────────────────────────────────────
lookbackDays = input.int(20, "Lookback Days (X)", minval=1, maxval=500, 
     tooltip="How many bars back to measure the price move")
 
atrPeriod    = input.int(100, "ATR Period", minval=1, maxval=500, 
     tooltip="ATR length in bars (default 100)")
 
// Table appearance
showTable     = input.bool(true, "Show Number on Chart")
tablePos      = input.string("Top Right", "Table Position", 
     options=["Top Right", "Top Left", "Bottom Right", "Bottom Left"])
 
textSize      = input.string("Large", "Text Size", 
     options=["Tiny", "Small", "Normal", "Large", "Huge"])
textColor     = input.color(color.white, "Text Color")
bgColor       = input.color(color.new(color.black, 75), "Background Color")
borderColor   = input.color(color.gray, "Border Color")
borderWidth   = input.int(1, "Border Width", minval=0, maxval=5)
 
// Color of the number itself (overrides text color when positive/negative)
usePosNegColor = input.bool(true, "Color Number by Direction")
posColor      = input.color(color.lime, "Positive Color")
negColor      = input.color(color.red, "Negative Color")
 
// ── Calculations ────────────────────────────────────
atrValue    = ta.atr(atrPeriod)
priceChange = close - close[lookbackDays]
atrMultiple = atrValue != 0 ? priceChange / atrValue : 0.0
 
// ── Display table ───────────────────────────────────
pos = switch tablePos
    "Top Right"    => position.top_right
    "Top Left"     => position.top_left
    "Bottom Right" => position.bottom_right
    "Bottom Left"  => position.bottom_left
    => position.top_right
 
sizeVal = switch textSize
    "Tiny"   => size.tiny
    "Small"  => size.small
    "Normal" => size.normal
    "Large"  => size.large
    "Huge"   => size.huge
    => size.large
 
var table t = table.new(pos, 1, 2, bgcolor=bgColor, border_width=borderWidth, border_color=borderColor)
 
if barstate.islast and showTable
    multColor = usePosNegColor ? (atrMultiple >= 0 ? posColor : negColor) : textColor
    sign      = atrMultiple >= 0 ? "+" : ""
    
    table.cell(t, 0, 0, "Past " + str.tostring(lookbackDays) + " bars", 
               text_color=textColor, text_size=size.small, bgcolor=bgColor)
    table.cell(t, 0, 1, sign + str.tostring(atrMultiple, "#.##") + " ATR", 
               text_color=multColor, text_size=sizeVal, bgcolor=bgColor)
 
// Optional hidden plot so you can also view it in a separate pane if desired
plot(atrMultiple, "ATR Multiple", display=display.none)

← 返回列表