Radial Basis Kernal ATR [BackQuant] for ThinkOrSwim

samer800

Moderator - Expert
VIP
Lifetime
CRyOb1y.png


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
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
441 Online
Create Post

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top