Kalman Filter

ebtrader

New member
Anyone try coding the Kalman Filter? EasyLanguage version is out there but I have yet to see someone able to convert it to ThinkScript.
 
Solution
@bp805
Code:
input src = hl2;
input length = 24;
input showcross = yes;
input gain = 10000;
input k = yes;

script hma {
    input _src = 0;
    input _length = 0;
    def avgType = AverageType.WEIGHTED;

    plot return = MovingAverage(avgType, (2 * MovingAverage(avgType, _src, _length /
                  2)) - MovingAverage(avgType, _src, _length), Round(Sqrt(_length)));
}

script hma3 {
    input _length = 0;
    def p = _length / 2;
    def avgType = AverageType.WEIGHTED;

    plot return = MovingAverage(avgType, 3 * MovingAverage(avgType, close, p / 3) -
                  MovingAverage(avgType, close, p / 2) - MovingAverage(avgType,
                  close, p), p);
}

script kahlman {
    input x = 0;
    input g = 0;
    def...
Here's a Kalman that I have from a while back

input n = 21;

plot Kalman = WMA(((WMA(close, 3) * 3) + (WMA(close[1], 2) * 2) + close[2] + Average(close, n)) / 7, n);
 
# Simple Kalman Filter Compared to different Averages
# Mobius
# 2.22.2016

input n = 8;
input Kalman = yes;
input SMA = yes;
input EMA = yes;
input Weighted_MA = yes;

plot Data = WMA(((close * 3) + (close[1] * 2) + close[2] + Average(close, n)) / 7, n);
Data.SetDefaultColor(Color.Cyan);
Data.SetHiding(!Kalman);
AddLabel(1, "Kalman Filter", Color.Cyan);

plot avg = Average(close, n);
avg.SetDefaultColor(Color.Yellow);
avg.SetHiding(!SMA);
AddLabel(1, "SMA", Color.Yellow);

plot ExAvg = ExpAverage(close, n);
ExAvg.SetDefaultColor(Color.Red);
ExAvg.SetHiding(!EMA);
AddLabel(1, "EMA", Color.Red);

plot WtAvg = WMA(close, n);
WtAvg.SetDefaultColor(Color.Green);
WtAvg.SetHiding(!Weighted_MA);
AddLabel(1, "Weighted MA", Color.Green);
 
Don't know why, but I have Kolmogorov-Feller linked to Kalman - curious cross-overs

declare lower;
input n = 20;
def s = fold i = 1 to n with d = 0 do d+log(i);
def f = fold j = 1 to n with m = 0 do m+log(getvalue(close,j,n))*log(j);
def k = fold g = 1 to n with c = 0 do c+power(log(g),2);
def alpha = ((Sum(log(Close),n)*s) - (n*f))/((s*s)-n*k);
def a = exp(((Sum(Close,n)-alpha*s)/n)/1000);
plot al = a;

al.assignvaluecolor(if al > al[1] then color.blue else if al < al[1] then color.red else color.white);
plot trig = al[1];

#Blackledge "FINANCIAL FORECASTING USING THE
#KOLMOGOROV-FELLER EQUATION"
#growex

plot Data = WMA(((a * 3) + (a[1] * 2) + a[2] + Average(a, n)) / 7, n);
Data.SetDefaultColor(Color.Cyan);
 
Hi all, trying to create a cross and having challenges.

original code:

input n = 21;

plot Kalman = WMA(((WMA(close, 3) * 3) + (WMA(close[1], 2) * 2) + close[2] + Average(close, n)) / 7, n);

Trying to do a cross above from kalman to SMA with an arrow alert and sound when it crosses. Below is how far i got and keeps giving me error. Can someone help :)

Kalman(100) crosses above SimpleMovingAvg("length" = 200)."SMA"


thank you
 
@tradebook Could you explain how you are using this indicator?
Kalman SMA200 Cross with Arrows, Label, and Alerts
Shared Link: http://tos.mx/r92Iueu
Click here for --> Easiest way to load shared links
Ruby:
# ########################################################
# Kalman SMA200 cross
# by @tradebook 7/2021

input n = 21;
input showLabel = yes ;
DefineGlobalColor("LitePink", CreateColor (225, 220, 180)) ;

plot Kalman = WMA(((WMA(close, 3) * 3) + (WMA(close[1], 2) * 2) + close[2] + Average(close, n)) / 7, n);
plot avg200 = MovingAverage(AverageType.SIMPLE, close, 200);
AddCloud(Kalman, avg200, Color.green, GlobalColor("LitePink"));

plot UpArrow = if  Kalman crosses above avg200  then low else double.NaN ;
UpArrow.SetPaintingStrategy(PaintingStrategy.ARROW_up);
UpArrow.SetDefaultColor(color.blue) ;

plot DownArrow = if  Kalman crosses below avg200 then high else double.NaN ;
DownArrow.SetPaintingStrategy(PaintingStrategy.ARROW_down);
DownArrow.SetDefaultColor(color.magenta) ;

AddLabel(showLabel,
      if Kalman crosses above avg200 then "Buy"  else
      if Kalman crosses below avg200 then "Sell" else "Kalman: " +round(Kalman,1),
      if Kalman crosses above avg200 then color.green else
      if Kalman crosses below avg200 then color.red   else color.gray);

Alert(Kalman crosses above avg200, "Buy" , Alert.Bar, Sound.Bell);
Alert(Kalman crosses below avg200, "Sell" , Alert.Bar, Sound.Bell);
znJXGoh.png
 
I have been playing with the below indicator in Trading View and looks interesting, but I primarily use TOS. I know some skilled folks have converted indicators from Trading View to Thinkscript before. Is anyone able to give this a shot at converting? Its way over my head. Thanks for any help that can be provided.

https://www.tradingview.com/script/0p99baTO-hull-trend-with-kahlman/

UoyxGCu.jpg


Code:
//@version=4
study("Hull Trend with Kahlman", shorttitle="HMA-Kahlman Trend", overlay=true)

src       = input(hl2,   "Price Data")
length    = input(24,    "Lookback")
showcross = input(true,  "Show cross over/under")
gain      = input(10000, "Gain")
k         = input(true,  "Use Kahlman")

hma(_src, _length) =>
    wma((2 * wma(_src, _length / 2)) - wma(_src, _length), round(sqrt(_length)))
   
hma3(_src, _length) =>
    p = length/2
    wma(wma(close,p/3)*3 - wma(close,p/2) - wma(close,p),p)

kahlman(x, g) =>
    kf = 0.0
    dk = x - nz(kf[1], x)
    smooth = nz(kf[1],x)+dk*sqrt((g/10000)*2)
    velo = 0.0
    velo := nz(velo[1],0) + ((g/10000)*dk)
    kf := smooth+velo
 
a = k ? kahlman(hma(src, length), gain) : hma(src, length)
b = k ? kahlman(hma3(src, length), gain) : hma3(src, length)
c = b > a ? color.lime : color.red
crossdn = a > b and a[1] < b[1]
crossup = b > a and b[1] < a[1]

p1 = plot(a,color=c,linewidth=1,transp=75)
p2 = plot(b,color=c,linewidth=1,transp=75)
fill(p1,p2,color=c,transp=55)
plotshape(showcross and crossdn ? a : na, location=location.absolute, style=shape.labeldown, color=color.red, size=size.tiny, text="S", textcolor=color.white, transp=0, offset=-1)
plotshape(showcross and crossup ? a : na, location=location.absolute, style=shape.labelup, color=color.green, size=size.tiny, text="B", textcolor=color.white, transp=0, offset=-1)
 
Last edited by a moderator:
@bp805
Code:
input src = hl2;
input length = 24;
input showcross = yes;
input gain = 10000;
input k = yes;

script hma {
    input _src = 0;
    input _length = 0;
    def avgType = AverageType.WEIGHTED;

    plot return = MovingAverage(avgType, (2 * MovingAverage(avgType, _src, _length /
                  2)) - MovingAverage(avgType, _src, _length), Round(Sqrt(_length)));
}

script hma3 {
    input _length = 0;
    def p = _length / 2;
    def avgType = AverageType.WEIGHTED;

    plot return = MovingAverage(avgType, 3 * MovingAverage(avgType, close, p / 3) -
                  MovingAverage(avgType, close, p / 2) - MovingAverage(avgType,
                  close, p), p);
}

script kahlman {
    input x = 0;
    input g = 0;
    def kf;
    def dk = x - if IsNaN(kf[1]) then x else kf[1];
    def smooth = if IsNan(kf[1]) then x else kf[1] + dk * Sqrt((g / 10000) * 2);
    def velo = if IsNaN(velo[1]) then 0 else velo[1] + ((g / 10000) * dk);
    kf = smooth + velo;

    plot return = kf;
}

plot a = if k then kahlman(hma(src, length), gain) else hma(src, length);
plot b = if k then kahlman(hma3(length), gain) else hma3(length);

AddCloud(b, a, color.GREEN, color.RED);
AddChartBubble(b crosses above a, low, "B", color.GREEN, No);
AddChartBubble(b crosses below a, high, "S", color.RED);
 
Solution

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
335 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