Kalman Trend Levels [BigBeluga] For ThinkOrSwim

OGK

New member
VIP
The author states: Kalman Trend Levels is an advanced trend-following indicator designed to highlight key support and resistance zones based on Kalman filter crossovers. With dynamic trend analysis and actionable signals, it helps traders interpret market direction and momentum shifts effectively.

🔵When to Use:
The Kalman Trend Levels indicator is ideal for traders looking to identify and act upon trend changes and significant price zones. By visualizing key levels and momentum shifts, this tool allows you to:
Define support and resistance zones that align with trend direction.
Identify and react to trend weakening or strengthening via candle color changes.
Use retest signals for potential re-entries at critical levels.
See crossover points and price values to gain a clearer view of trend changes in real time.

With its focus on trend direction, support/resistance, and momentum clarity, Kalman Trend Levels is an essential tool for navigating trending markets, providing actionable insights with every crossover and trend shift.
KDrsKwu.png


Here is the original Tradingview code:
https://www.tradingview.com/script/DZo2pzhg-Kalman-Trend-Levels-BigBeluga/

The new ThinkOrSwim code can be found in the next post.
 
Last edited by a moderator:

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

check the below:

CSS:
#// Indicator for TOS
#// © BigBeluga
#indicator("Kalman Trend Levels [BigBeluga]"
# Converted by Sam4Cok@Samer800    - 12/2024

input timeframe  = AggregationPeriod.MIN;
input extendLines = yes;
input showPriceLabel = yes;
input source = FundamentalType.CLOSE;
input shortLength = 50;
input longLength  = 150;
input showRetestSignals = no; #(false, "Retest Signals")
input colorBars = yes; #(true, "Candle Color")

def na = Double.NaN;
def last = IsNaN(close);
def cap = GetAggregationPeriod();
def tf = Max(Cap, Timeframe);
def tr = TrueRange(high(Period = tf), close(Period = tf), low(Period = tf));
def nATR = WildersAverage(tr, 200) * 0.5;

#// Kalman filter function
Script kalman_filter {
input src = close;
input length = 50;
input R = 0.01;
input Q = 0.1;
    def error_meas = R * length;
    def estimate; def error_est;
    def error_est1 = CompoundValue(1, error_est[1], 1);
    def prediction  = CompoundValue(1, estimate[1], src[1]);
    def kalman_gain = error_est1 / (error_est1 + error_meas);
        estimate = prediction + kalman_gain * (src - prediction);
        error_est = (1 - kalman_gain) * error_est1  + Q / (length);
    plot out = estimate;
}

def short_kalman = kalman_filter(Fundamental(source, PEriod = tf), shortLength);
def long_kalman  = kalman_filter(Fundamental(source, PEriod = tf), longLength);
def trend_up = short_kalman > long_kalman;
def trend_col  = if trend_up then 1 else -1;
def trend_col1 = if short_kalman > short_kalman[2] then 1 else -1;
def candle_col = if colorBars then (if trend_up and short_kalman > short_kalman[2] then 1 else
                                    if !trend_up and short_kalman < short_kalman[2] then -1 else 0) else na;

def change = trend_up - trend_up[1];
def trUp = trend_up and !trend_up[1];
def trDn = trend_up[1] and !trend_up;

def lo_Top = if trUp then (low(Period = tf) + nATR) else if (if extendLines then no else change) then na else lo_Top[1];
def lo_Bot = if trUp then  low(Period = tf) else if (if extendLines then no else change) then na else lo_Bot[1];
def hi_Top = if trDn then  high(Period = tf) else if (if extendLines then no else change) then na else hi_Top[1];
def hi_Bot = if trDn then (high(Period = tf) - nATR) else if (if extendLines then no else change) then na else hi_Bot[1];

#-- lines
plot supUp = if lo_Top then lo_Top else na;
plot supDn = if lo_Bot then lo_Bot else na;

plot resUp = if hi_Top then hi_Top else na;
plot resDn = if hi_Bot then hi_Bot else na;

supUp.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
supDn.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
resUp.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
resDn.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

supUp.SetDefaultColor(Color.DARK_GREEN);
supDn.SetDefaultColor(Color.DARK_GREEN);
resUp.SetDefaultColor(Color.DARK_RED);
resDn.SetDefaultColor(Color.DARK_RED);

AddCloud(if supUp==supUp[1] then supUp else na, supDn, Color.DARK_GREEN);
AddCloud(if resUp==resUp[1] then resUp else na, resDn, Color.DARK_RED);

#-- plot movAvg

plot shortKalman = if short_kalman then short_kalman else na;
plot longKalman = if long_kalman then long_kalman else na;

longKalman.SetLineWeight(2);
shortKalman.AssignValueColor(if trend_col1>0 then Color.CYAN else Color.MAGENTA);
longKalman.AssignValueColor(if trend_col>0 then Color.CYAN else Color.MAGENTA);

AddCloud(shortKalman, longKalman, CreateColor(0, 60, 0), CreateColor(70, 0, 0));

# retset
def touchHi = high < hi_Bot and high[1]>= hi_Bot;
def touchLo = low > lo_Top and low[1]<= lo_Top;

plot retestUp = if showRetestSignals and touchHi[-1] then high else na;
plot retestDn = if showRetestSignals and touchLo[-1] then low else na;

retestUp.SetDefaultColor(Color.LIGHT_RED);
retestDn.SetDefaultColor(Color.LIGHT_GREEN);
retestUp.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);
retestDn.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);

#-- Signals
def cond = close == close(Period = tf);
AddChartBubble(showPriceLabel and cond and trUp, short_kalman, AsDollars(close(Period = tf)), Color.CYAN, no);
AddChartBubble(showPriceLabel and cond and trDn, short_kalman, AsDollars(close(Period = tf)), Color.MAGENTA);
#-- Bar color
AssignPriceColor(if isNaN(candle_col) then Color.CURRENT else
                 if candle_col>0 then Color.GREEN else
                 if candle_col<0 then Color.RED else Color.GRAY);
#-- ENd of CODE
 
check the below:

CSS:
#// Indicator for TOS
#// © BigBeluga
#indicator("Kalman Trend Levels [BigBeluga]"
# Converted by Sam4Cok@Samer800    - 12/2024

input timeframe  = AggregationPeriod.MIN;
input extendLines = yes;
input showPriceLabel = yes;
input source = FundamentalType.CLOSE;
input shortLength = 50;
input longLength  = 150;
input showRetestSignals = no; #(false, "Retest Signals")
input colorBars = yes; #(true, "Candle Color")

def na = Double.NaN;
def last = IsNaN(close);
def cap = GetAggregationPeriod();
def tf = Max(Cap, Timeframe);
def tr = TrueRange(high(Period = tf), close(Period = tf), low(Period = tf));
def nATR = WildersAverage(tr, 200) * 0.5;

#// Kalman filter function
Script kalman_filter {
input src = close;
input length = 50;
input R = 0.01;
input Q = 0.1;
    def error_meas = R * length;
    def estimate; def error_est;
    def error_est1 = CompoundValue(1, error_est[1], 1);
    def prediction  = CompoundValue(1, estimate[1], src[1]);
    def kalman_gain = error_est1 / (error_est1 + error_meas);
        estimate = prediction + kalman_gain * (src - prediction);
        error_est = (1 - kalman_gain) * error_est1  + Q / (length);
    plot out = estimate;
}

def short_kalman = kalman_filter(Fundamental(source, PEriod = tf), shortLength);
def long_kalman  = kalman_filter(Fundamental(source, PEriod = tf), longLength);
def trend_up = short_kalman > long_kalman;
def trend_col  = if trend_up then 1 else -1;
def trend_col1 = if short_kalman > short_kalman[2] then 1 else -1;
def candle_col = if colorBars then (if trend_up and short_kalman > short_kalman[2] then 1 else
                                    if !trend_up and short_kalman < short_kalman[2] then -1 else 0) else na;

def change = trend_up - trend_up[1];
def trUp = trend_up and !trend_up[1];
def trDn = trend_up[1] and !trend_up;

def lo_Top = if trUp then (low(Period = tf) + nATR) else if (if extendLines then no else change) then na else lo_Top[1];
def lo_Bot = if trUp then  low(Period = tf) else if (if extendLines then no else change) then na else lo_Bot[1];
def hi_Top = if trDn then  high(Period = tf) else if (if extendLines then no else change) then na else hi_Top[1];
def hi_Bot = if trDn then (high(Period = tf) - nATR) else if (if extendLines then no else change) then na else hi_Bot[1];

#-- lines
plot supUp = if lo_Top then lo_Top else na;
plot supDn = if lo_Bot then lo_Bot else na;

plot resUp = if hi_Top then hi_Top else na;
plot resDn = if hi_Bot then hi_Bot else na;

supUp.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
supDn.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
resUp.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
resDn.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

supUp.SetDefaultColor(Color.DARK_GREEN);
supDn.SetDefaultColor(Color.DARK_GREEN);
resUp.SetDefaultColor(Color.DARK_RED);
resDn.SetDefaultColor(Color.DARK_RED);

AddCloud(if supUp==supUp[1] then supUp else na, supDn, Color.DARK_GREEN);
AddCloud(if resUp==resUp[1] then resUp else na, resDn, Color.DARK_RED);

#-- plot movAvg

plot shortKalman = if short_kalman then short_kalman else na;
plot longKalman = if long_kalman then long_kalman else na;

longKalman.SetLineWeight(2);
shortKalman.AssignValueColor(if trend_col1>0 then Color.CYAN else Color.MAGENTA);
longKalman.AssignValueColor(if trend_col>0 then Color.CYAN else Color.MAGENTA);

AddCloud(shortKalman, longKalman, CreateColor(0, 60, 0), CreateColor(70, 0, 0));

# retset
def touchHi = high < hi_Bot and high[1]>= hi_Bot;
def touchLo = low > lo_Top and low[1]<= lo_Top;

plot retestUp = if showRetestSignals and touchHi[-1] then high else na;
plot retestDn = if showRetestSignals and touchLo[-1] then low else na;

retestUp.SetDefaultColor(Color.LIGHT_RED);
retestDn.SetDefaultColor(Color.LIGHT_GREEN);
retestUp.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);
retestDn.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);

#-- Signals
def cond = close == close(Period = tf);
AddChartBubble(showPriceLabel and cond and trUp, short_kalman, AsDollars(close(Period = tf)), Color.CYAN, no);
AddChartBubble(showPriceLabel and cond and trDn, short_kalman, AsDollars(close(Period = tf)), Color.MAGENTA);
#-- Bar color
AssignPriceColor(if isNaN(candle_col) then Color.CURRENT else
                 if candle_col>0 then Color.GREEN else
                 if candle_col<0 then Color.RED else Color.GRAY);
#-- ENd of CODE
Thank you so much.
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
312 Online
Create Post

Similar threads

Similar threads

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