
Author Message:
The Radial Basis Kernel ATR is a trading indicator that combines the classic Average True Range (ATR) with advanced Radial Basis Function (RBF) kernel smoothing. This innovative approach creates a highly adaptive and precise tool for detecting volatility, identifying trends, and providing dynamic support and resistance levels.
More Details: https://www.tradingview.com/v/OZR1iWDE/
CODE:
CSS:
#// Indicator for TOS
#// © BackQuant
# indicator( "Radial Basis Kernal ATR [BackQuant]", shorttitle="RBF Kernal ATR [BackQuant]"
# Converted by Sam4Cok@Samer800 - 01/2025
input timeframe = AggregationPeriod.MIN;
input showKernalAtrLine = yes; #, "Plot RBF Kernal Atr on Chart?")
input source = FundamentalType.CLOSE; #, "Source", group = "RBF Kernal")
input KernelLength = 5; #, "Kernel Length", minval=1, group = "RBF Kernal")
input GammaAdjustmentFactor = 0.1; #, "Gamma Adjustment Factor", step=0.01, minval=0.01, group = "RBF Kernal")
input atrLength = 14; #, "ATR Period", group = " Atr")
input atrMultiplier = 1.7; #, "Factor", step = 0.01, group = " Atr")
input colorBars = no; #, "Paint Candles According to trend?")
input showKernalAtrMovAvg = no; #, "Show Atr Moving Average as Confluence?",group = "Confluence")
input movingAverageType = AverageType.EXPONENTIAL; # title="MA Type", options=["SMA", "Hull", "Ema", "Wma", ""]
input movingAverageLength = 50; #, "Moving Average Period", group = "Confluence")
def na = Double.NaN;
def last = isNaN(close);
def cap = GetAggregationPeriod();
def tf = Max(cap, timeframe);
def inSrc = Fundamental(source, Period = tf);
Script nz {
input src = close;
def bool = if isNaN(src) then 0 else src;
plot out = bool;
}
#// Radial Basis Function Kernel
def mean = Average(inSrc, KernelLength);
def var_sum = fold i = 0 to KernelLength with p do
p + Sqr(Fundamental(source, Period = tf)[i] - mean);
def variance = var_sum / KernelLength;
def gamma = GammaAdjustmentFactor / (2 * variance);
def weight_sum = fold j = 0 to KernelLength with q do
q + exp(-gamma * Sqr(inSrc - Fundamental(source, Period = tf)[j]));
def kernel_sum = fold k = 0 to KernelLength with r do
r + Fundamental(source, Period = tf)[k] * exp(-gamma * Sqr(inSrc - Fundamental(source, Period = tf)[k]));
def RBFKernel = kernel_sum / weight_sum;
#// Function Out
def tr = if !tr[1] then (high(Period = tf) - low(Period = tf)) else
TrueRange(high(Period = tf), close(Period = tf), low(Period = tf));
def nATR = WildersAverage(tr, atrLength);
def trueRange = nATR * atrMultiplier;
def trueRangeUpper = RBFKernel + trueRange;
def trueRangeLower = RBFKernel - trueRange;
def Atr;
def Atr1 = if isNaN(Atr[1]) then RBFKernel else
if !Atr[1] then RBFKernel else Atr[1];
if trueRangeLower > Atr1 {
ATR = trueRangeLower;
} else if trueRangeUpper < Atr1 {
ATR = trueRangeUpper;
} else {
ATR = Atr1;
}
def maOut = MovingAverage(movingAverageType, Atr, movingAverageLength);
#// long and short conditions
def AtrLong = nz(Atr > Atr[1]);
def AtrShort = nz(Atr < Atr[1]);
def Trend = if AtrLong then 1 else if AtrShort then -1 else Trend[1];
#-- plot
plot atrLine = if showKernalAtrLine and !last and Atr then Atr else na;
plot movAvg = if showKernalAtrMovAvg and !last and maOut then maOut else na; # "Moving Average"
movAvg.SetDefaultColor(Color.WHITE);
atrLine.SetLineWeight(2);
atrLine.AssignValueColor(if Trend > 0 then Color.GREEN else
if Trend < 0 then Color.RED else Color.GRAY);
#-- Signal
def sigUp = Trend > 0 and Trend != Trend[1];
def sigDn = Trend < 0 and Trend != Trend[1];
plot arrowUp = if !last and sigUp then low else na;
plot arrowDn = if !last and sigDn then high else na;
arrowUp.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
arrowDn.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
arrowUp.SetDefaultColor(Color.CYAN);
arrowDn.SetDefaultColor(Color.MAGENTA);
#-- Bar Color.
AssignPriceColor(if !colorBars then Color.CURRENT else
if Trend >0 then if close > Atr then Color.GREEN else
if close > maOut then Color.DARK_GREEN else Color.LIGHT_ORANGE else
if Trend <0 then if close < Atr then Color.RED else
if close < maOut then Color.DARK_RED else Color.LIGHT_ORANGE else Color.GRAY);
#-- END of CODE