EMA Distance % + ATR %

buttigieg_sean · study · 145 行 · 点赞 11 · TradingView 原页

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

Pine Script

//@version=6
indicator("EMA Distance % + ATR %", shorttitle="EMA Dist + ATR %", overlay=false)
 
// ─────────────────────────────────────────────────────────────
// Inputs
// ─────────────────────────────────────────────────────────────
 
groupCalc = "Calculation Settings"
 
emaLength = input.int(
     8,
     title="EMA Length",
     minval=1,
     group=groupCalc
)
 
atrLength = input.int(
     20,
     title="ATR Length",
     minval=1,
     group=groupCalc
)
 
 
// ─────────────────────────────────────────────────────────────
// Calculations
// ─────────────────────────────────────────────────────────────
 
emaValue = ta.ema(close, emaLength)
atrValue = ta.atr(atrLength)
 
// Signed percentage distance from EMA.
distancePct = emaValue != 0 ? ((close - emaValue) / emaValue) * 100 : na
 
// ATR expressed as a percentage of current price.
atrPct = close != 0 ? (atrValue / close) * 100 : na
 
 
// ─────────────────────────────────────────────────────────────
// Colours
// ─────────────────────────────────────────────────────────────
 
color emaColor =
     distancePct > 0 ? color.rgb(38, 166, 154) :
     distancePct < 0 ? color.rgb(239, 83, 80) :
     color.gray
 
color atrColor = color.rgb(66, 165, 245)
 
 
// ─────────────────────────────────────────────────────────────
// Plots
// ─────────────────────────────────────────────────────────────
 
plot(
     distancePct,
     title="Distance from EMA (%)",
     color=emaColor,
     linewidth=2
)
 
plot(
     atrPct,
     title="ATR (% of Price)",
     color=atrColor,
     linewidth=2
)
 
hline(
     0,
     title="Zero % Reference",
     color=color.new(color.gray, 55),
     linestyle=hline.style_dotted,
     linewidth=1
)
 
 
// ─────────────────────────────────────────────────────────────
// Current-value table
// ─────────────────────────────────────────────────────────────
 
var table valueTable = table.new(
     position.top_right,
     2,
     2,
     bgcolor=color.new(color.black, 15),
     frame_color=color.new(color.gray, 60),
     frame_width=1,
     border_color=color.new(color.gray, 75),
     border_width=1
)
 
if barstate.islast
 
    string distanceText =
         na(distancePct) ? "N/A" :
         (distancePct > 0 ? "+" : "") +
         str.tostring(distancePct, "#.00") + "%"
 
    string atrText =
         na(atrPct) ? "N/A" :
         str.tostring(atrPct, "#.00") + "%"
 
    string emaLabel = "Distance from EMA" + str.tostring(emaLength)
    string atrLabel = "ATR(" + str.tostring(atrLength) + ")"
 
    table.cell(
         valueTable,
         0,
         0,
         emaLabel,
         text_color=color.silver,
         text_halign=text.align_left,
         bgcolor=color.new(color.black, 15)
    )
 
    table.cell(
         valueTable,
         1,
         0,
         distanceText,
         text_color=emaColor,
         text_halign=text.align_right,
         bgcolor=color.new(color.black, 15)
    )
 
    table.cell(
         valueTable,
         0,
         1,
         atrLabel,
         text_color=color.silver,
         text_halign=text.align_left,
         bgcolor=color.new(color.black, 15)
    )
 
    table.cell(
         valueTable,
         1,
         1,
         atrText,
         text_color=atrColor,
         text_halign=text.align_right,
         bgcolor=color.new(color.black, 15)
    )

← 返回列表