Acceleration SuperTrend | NAL

NordicAlphaLab · study · 109 行 · 点赞 180 · TradingView 原页

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

Pine Script

// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © NordicAlphaLab
 
//@version=6
indicator("Acceleration SuperTrend | NAL", "Acceleration SuperTrend", overlay = true, behind_chart = false)
 
//                                     ███╗   ██╗ █████╗ ██╗
//                                     ████╗  ██║██╔══██╗██║
//                                     ██╔██╗ ██║███████║██║
//                                     ██║╚██╗██║██╔══██║██║
//                                     ██║ ╚████║██║  ██║███████╗
//                                     ╚═╝  ╚═══╝╚═╝  ╚═╝╚══════╝
 
// ══════════════════════════════════════ ◈ Inputs ◈ ══════════════════════════════════════
 
int rocLen = input.int(14, "ROC Length", minval = 1, group = "Acceleration")
int regimeLen = input.int(10, "Acceleration Lookback", minval = 2, group = "Acceleration")
 
float src = input.source(hl2, "Baseline Source", group = "Baseline & ATR")
int fastLen = input.int(20, "Fast Length", minval = 1, group = "Baseline & ATR")
int slowLen = input.int(40, "Slow Length", minval = 1, group = "Baseline & ATR")
float factor = input.float(1.25, "Band Multiplier", minval = 0.01, step = 0.1, group = "Baseline & ATR")
 
string mode = input.string("Standard", "Color Mode", options = ["Standard", "Nordic", "Simple"], group = "Visuals")
bool showCandles = input.bool(true, "Color Candles", group = "Visuals")
 
if fastLen > slowLen
    runtime.error("Fast Length must be less than or equal to Slow Length.")
 
// ══════════════════════════════════════ ◈ Visuals ◈ ══════════════════════════════════════
 
[col_up, col_dn] = switch mode
    "Nordic" => [color.rgb(46, 161, 255), color.rgb(150, 154, 169)]
    "Simple" => [color.lime, color.red]
    => [color.rgb(0, 255, 200), color.rgb(32, 94, 144)]
color col_nu = color.gray
 
// ══════════════════════════════════════ ◈ Acceleration ◈ ══════════════════════════════════════
 
float acceleration = ta.change(ta.roc(close, rocLen))
float magnitude = math.abs(acceleration)
float peak = ta.highest(magnitude, regimeLen)
float weight = not na(magnitude) and peak > 0.0 ? magnitude / peak : 0.0
 
float baseAlpha = weight * (2.0 / (fastLen + 1)) + (1.0 - weight) * (2.0 / (slowLen + 1))
float atrAlpha = weight * (1.0 / fastLen) + (1.0 - weight) * (1.0 / slowLen)
 
var float basis = na
if not na(src)
    basis := na(basis[1]) ? src : baseAlpha * src + (1.0 - baseAlpha) * basis[1]
 
float tr = ta.tr(true)
float atrSeed = ta.sma(tr, slowLen)
var float adaptiveATR = na
if not na(tr)
    adaptiveATR := na(adaptiveATR[1]) ? atrSeed : atrAlpha * tr + (1.0 - atrAlpha) * adaptiveATR[1]
 
// ══════════════════════════════════════ ◈ SuperTrend ◈ ══════════════════════════════════════
 
float upper = basis + factor * adaptiveATR
float lower = basis - factor * adaptiveATR
float prevUpper = upper[1]
float prevLower = lower[1]
upper := na(prevUpper) or upper < prevUpper or close[1] > prevUpper ? upper : prevUpper
lower := na(prevLower) or lower > prevLower or close[1] < prevLower ? lower : prevLower
 
float trail = na
int direction = na
if not na(adaptiveATR) and not na(basis)
    if na(trail[1])
        direction := 1
    else if trail[1] == prevUpper
        direction := close > upper ? -1 : 1
    else
        direction := close < lower ? 1 : -1
    trail := direction == -1 ? lower : upper
 
// ══════════════════════════════════════ ◈ NAL State ◈ ══════════════════════════════════════
 
int NAL = direction == -1 ? 1 : direction == 1 ? -1 : 0
color col = NAL == 1 ? col_up : NAL == -1 ? col_dn : col_nu
 
bool BullishFlip = NAL == 1 and NAL[1] == -1
bool BearishFlip = NAL == -1 and NAL[1] == 1
 
// ══════════════════════════════════════ ◈ Gradient Geometry ◈ ══════════════════════════════════════
 
// Fade from the active trail to the midpoint of the live candle body.
float body = math.avg(open, close)
float top = math.max(trail, body)
float bottom = math.min(trail, body)
color topColor = color.new(col, trail >= body ? 50 : 100)
color bottomColor = color.new(col, trail >= body ? 100 : 50)
 
// ══════════════════════════════════════ ◈ Plots ◈ ══════════════════════════════════════
 
pBody = plot(body, "Body Anchor", display = display.none)
pBull = plot(NAL == 1 ? trail : na, "Bullish SuperTrend", color = col_up, linewidth = 2, style = plot.style_linebr)
pBear = plot(NAL == -1 ? trail : na, "Bearish SuperTrend", color = col_dn, linewidth = 2, style = plot.style_linebr)
 
fill(pBody, pBull, NAL == 1 ? top : na, NAL == 1 ? bottom : na, topColor, bottomColor, title = "Bullish Gradient", fillgaps = false)
fill(pBody, pBear, NAL == -1 ? top : na, NAL == -1 ? bottom : na, topColor, bottomColor, title = "Bearish Gradient", fillgaps = false)
 
plotcandle(showCandles ? open : na, high, low, close, "Bar Color", color = col, wickcolor = col, bordercolor = col, display = display.pane, force_overlay = true)
 
// ══════════════════════════════════════ ◈ Alerts ◈ ══════════════════════════════════════
 
alertcondition(BullishFlip, "Acceleration SuperTrend Bullish", "Acceleration SuperTrend changed to bullish on {{ticker}} ({{interval}}).")
alertcondition(BearishFlip, "Acceleration SuperTrend Bearish", "Acceleration SuperTrend changed to bearish on {{ticker}} ({{interval}}).")

← 返回列表