Risk On / Risk Off - Index EMA Slope

etfbreakouts · study · 51 行 · 点赞 12 · TradingView 原页

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

Pine Script

//@version=6
indicator("Risk On / Risk Off - Index EMA Slopes v6", overlay = true, scale = scale.none)
 
indexSymbol = input.symbol("NASDAQ:IXIC", "Reference index")
// An empty symbol otherwise means the chart symbol in request.security().
referenceTicker = ticker.standard(indexSymbol == "" ? "NASDAQ:IXIC" : indexSymbol)
signalTf = input.timeframe("D", "Calculation timeframe")
useAngle = input.bool(true, "Require minimum angles", tooltip = "Off: both EMAs only need to be rising. On: both must also meet their minimum angles.")
min10 = input.int(25, "10 EMA minimum angle (degrees)", minval = 0, maxval = 89, step = 1)
min21 = input.int(25, "21 EMA minimum angle (degrees)", minval = 0, maxval = 89, step = 1)
angleScale = input.float(5.0, "Angle sensitivity", minval = 0.1, maxval = 100.0, step = 0.1, tooltip = "Scales ATR-normalized slopes before converting to degrees. Higher = easier RISK ON. At 5, a 25-degree threshold requires an EMA rise of about 0.093 ATR per bar. Set 1 for the original formula. This defines a mathematical angle, not the screen angle.")
showBg = input.bool(true, "Color chart background", group = "Background")
riskOnColor = input.color(color.new(color.green, 90), "RISK ON color / opacity", group = "Background", tooltip = "Use the opacity slider inside this color picker. 100% = solid; 0% = invisible.")
riskOffColor = input.color(color.new(color.red, 90), "RISK OFF color / opacity", group = "Background", tooltip = "Use the opacity slider inside this color picker. 100% = solid; 0% = invisible.")
 
// Avoid silently sampling daily signals on weekly/monthly charts.
if timeframe.in_seconds() > timeframe.in_seconds(signalTf)
    runtime.error("Use a chart timeframe at or below the calculation timeframe.")
 
// Evaluate EMAs, changes, and ATR INSIDE the reference index context.
// Angle = atan(angleScale * (EMA today - EMA previous bar) / ATR(14))
// converted to degrees. This scale is configurable, not fitted to past dates.
// With sensitivity 5, 25 degrees = tan(25 degrees) / 5 = 0.09326 ATR/bar.
indexMetrics() =>
    ema10 = ta.ema(close, 10)
    ema21 = ta.ema(close, 21)
    atr14 = ta.atr(14)
    change10 = ema10 - ema10[1]
    change21 = ema21 - ema21[1]
    angle10 = atr14 > 0 ? math.atan(angleScale * change10 / atr14) * 180.0 / math.pi : na
    angle21 = atr14 > 0 ? math.atan(angleScale * change21 / atr14) * 180.0 / math.pi : na
    ready = not na(change10) and not na(change21) and (not useAngle or (not na(angle10) and not na(angle21)))
    pass10 = change10 > 0 and (not useAngle or angle10 >= min10)
    pass21 = change21 > 0 and (not useAngle or angle21 >= min21)
    riskOn = ready and pass10 and pass21
    // Choose the background INSIDE the index context, from the same decision
    // returned to the table. No chart-price series is used by bgcolor().
    // Preserve the opacity chosen in each color picker without overriding it.
    referenceBg = not ready ? color(na) : (riskOn ? riskOnColor : riskOffColor)
    [angle10, angle21, ready, pass10, pass21, riskOn, referenceBg]
 
// One reference-index request supplies BOTH the table and every background bar.
// Live reference bars can change until they close. No future data is requested.
[a10, a21, ready, pass10, pass21, riskOn, indexBackground] = request.security(referenceTicker, signalTf, indexMetrics(), gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_off, ignore_invalid_symbol = false)
 
statusColor = not ready ? color.gray : riskOn ? color.green : color.red
bgcolor(showBg ? indexBackground : na, title = "Reference index Risk On / Risk Off background", editable = false)
 
var table panel = table.new(position.top_right, 1, 1, border_width = 1)
if barstate.islast
    table.cell(panel, 0, 0, riskOn ? "RISK ON" : "RISK OFF", text_color = color.white, bgcolor = statusColor)

← 返回列表