Adaptive Moving Average SD Bands [NJ]

CryptoNejc · study · 77 行 · 点赞 69 · 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("Adaptive Moving Average SD Bands [NJ]", "AMASB", overlay = true, behind_chart = false)
 
// ✧═════════════════✧
// ║      INPUTS      ║
// ✧═════════════════✧
 
const string g1 = "✧ DISPLAY ✧"
colorMode   = input.string("Colored", "Color mode", options = ["Colored", "Neutral"], group = g1)
dispCandles = input.bool(true, "Color candles", group = g1)
 
const string g2 = "✧ CALCULATION ✧"
maSrc = input.source(close, "MA source", group = g2)
maLen = input.int(25, "MA length", group = g2)
sdLen = input.int(34, "SD length", group = g2)
 
// ✧══════════════════════✧
// ║      CALCULATION      ║
// ✧══════════════════════✧
 
// Get MAs
rma  = ta.rma(maSrc, maLen)
swma = ta.swma(maSrc)
lsma = ta.linreg(maSrc, maLen, 1)
 
// Get SD
sd = ta.stdev(close, sdLen)
 
// Get bands
bands(ma) =>
    upperBand = ma + sd
    lowerBand = ma - sd
    [upperBand, lowerBand]
 
[upperRma, lowerRma]   = bands(rma)
[upperSwma, lowerSwma] = bands(swma)
[upperLsma, lowerLsma] = bands(lsma)
 
// Find the bands that are farthest from the price
upperBand = math.max(upperRma, upperSwma, upperLsma)
lowerBand = math.min(lowerRma, lowerSwma, lowerLsma)
 
// ✧═══════════════════════✧
// ║      FINAL SIGNAL      ║
// ✧═══════════════════════✧
 
var int NJ = 0
 
if high > upperBand
    NJ := 1
if low < lowerBand
    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", 
     dispCandles ? sigCol : na,
     dispCandles ? sigCol : na,
     bordercolor = dispCandles ? sigCol : na,
     display = display.all - display.status_line - display.price_scale)
 
// Display bands
plot(NJ == -1 ? upperBand : na, "Upper band", shortCol, 2, plot.style_linebr)
plot(NJ == 1  ? lowerBand : na, "Lower band", longCol, 2, plot.style_linebr)

← 返回列表