MAD Adaptive RSI [NJ]

CryptoNejc · study · 92 行 · 点赞 44 · 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/
// © CryptoNejc
 
//@version=6
indicator("MAD Adaptive RSI [NJ]", "MAR", overlay = false, behind_chart = false)
 
// ✧═════════════════✧
// ║      INPUTS      ║
// ✧═════════════════✧
 
const string g1 = "✧ DISPLAY ✧"
colorMode = input.string("Colored", "Color mode", options = ["Colored", "Neutral"], group = g1)
displayColCandles = input.bool(true, "Color candles", group = g1)
 
const string g2 = "✧ CALCULATION ✧"
madLen = input.int(29, "MAD length", group = g2)
madSmooth = input.int(15, "MAD smoothing", group = g2)
madMulti = input.float(1.6, "MAD multiplier", step = 0.05, group = g2)
thr = input.int(53, "Threshold base", group = g2)
 
// ✧══════════════════════✧
// ║      CALCULATION      ║
// ✧══════════════════════✧
 
// MAD
mad(src, len) =>
 
    // Save source values
    array<float> srcArray = array.new<float>()
    for i = 0 to len - 1
        array.push(srcArray, src[i])
 
    // Get median of source values
    med = array.median(srcArray)
 
    // Absolute deviations from median
    array<float> devArray = array.new<float>()
    for i = 0 to len - 1
        array.push(devArray, math.abs(array.get(srcArray, i) - med))
 
    // Get median of absolute deviations
    mad = array.median(devArray)
 
// MAD
mad = mad(close, madLen)
 
// Normalize, smooth and cap MAD
madPct = (mad / close) * 100
smoothMadPct = ta.sma(madPct, madSmooth)
capMadPct = math.min(smoothMadPct * madMulti, 10)
 
// RSI
rsi = ta.rsi(close, 14)
 
// Adaptive thresholds
longThr = thr + capMadPct
shortThr = thr - capMadPct
 
// ✧═══════════════════════✧
// ║      FINAL SIGNAL      ║
// ✧═══════════════════════✧
 
var int NJ = 0
 
if ta.crossover(rsi, longThr)
    NJ := 1
if ta.crossunder(rsi, shortThr)
    NJ := -1
 
// ✧════════════════✧
// ║      PLOTS      ║
// ✧════════════════✧
 
// Colors
longCol  = colorMode == "Colored" ? color.rgb(0, 136, 100) : colorMode == "Neutral" ? color.rgb(179, 179, 179) : na
shortCol = colorMode == "Colored" ? color.rgb(120, 0, 32) : colorMode == "Neutral" ? color.rgb(60, 60, 60) : na
sigCol =  NJ == 1 ? longCol : NJ == -1 ? shortCol : na
 
// Color candles
plotcandle(
     open, high, low, close,
     "Colored candles", 
     displayColCandles ? sigCol : na,
     displayColCandles ? sigCol : na,
     bordercolor = displayColCandles ? sigCol : na,
     display = display.all - display.status_line - display.price_scale,
     force_overlay = true)
 
// Display RSI
plot(rsi, "RSI", sigCol, 2)
plot(longThr, "Upper threshold", longCol, linestyle = plot.linestyle_dashed)
plot(shortThr, "Lower threshold", shortCol, linestyle = plot.linestyle_dashed)

← 返回列表