Kalman filter

Here you can post and download custom indicators. PLEASE: Do not start topics unless you are posting your own indicator, they will be moved to appropriate section even if you do.

Moderator: admin

Kalman filter

Postby Alexander.Gettinger » Mon Dec 19, 2011 1:14 pm

The indicator implements Kalman filter (http://en.wikipedia.org/wiki/Kalman_filter)

Formulas:
Kalman[i]=Error+Velocity[i], where
Error=Kalman[i-1]+Distance*ShK,
Velocity[i]=Velocity[i-1]+Distance*K/100,
Distance=Price[i]-Kalman[i-1],
ShK=sqrt(Sharpness*K/100).

if Velocity>0, Kalman have a UP color and if Velocity<0, Kalman have a DN color.

Kalman_Filter.png


Download:
Kalman_Filter.lua
(2.43 KiB) Downloaded 1944 times

Kalman_Filter Overlay.lua
(12.54 KiB) Downloaded 649 times

Kalman_Filter with Alert.lua
(11.2 KiB) Downloaded 1096 times

This indicator will provide Audio / Email Alert Kalman filter Lines color change.

Kalman_FilterCross Alert.png

This indicator will provide Audio / Email Alert on Cross of Two Kalman filter Lines.
Kalman_FilterCross Alert.lua
(10.89 KiB) Downloaded 1279 times

Dec 25, 2015: Compatibility issue Fix. _Alert helper is not longer needed.
Alexander.Gettinger
FXCodeBase: Confirmed User
 
Posts: 3785
Joined: Wed Mar 31, 2010 9:40 pm
Location: Russia, Omsk

Re: Kalman filter

Postby Coondawg71 » Fri Oct 11, 2013 8:36 am

Can we please request a Signal/Alert which acknowledges when two periods of the Kalman Filter cross. Indicator can plot a dot at point of cross with user parameters to select size and color or the alert.

buy: Fast Kalman Filter (2.0/2.0) crosses UP through Slow Kalman Filter (0.50/0.5).

sell: Fast Kalman Filter (2.0/2.0) crosses DOWN through Slow Kalman Filter (0.50/0.50).

thanks,

sjc
Attachments
Kalman Filter cross.png
Fast and Slow Kalman cross
User avatar
Coondawg71
FXCodeBase: Graduate
 
Posts: 324
Joined: Sat Jan 15, 2011 11:45 am

Re: Kalman filter

Postby Apprentice » Sat Oct 12, 2013 4:48 am

Your request is added to the development list.
User avatar
Apprentice
FXCodeBase: Confirmed User
 
Posts: 36341
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia

Re: Kalman filter

Postby Apprentice » Sat Oct 12, 2013 6:36 am

Kalman_FilterCross Alert Added.
User avatar
Apprentice
FXCodeBase: Confirmed User
 
Posts: 36341
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia

Re: Kalman filter

Postby Patrick Sweet » Thu Nov 14, 2013 4:39 am

Hi,

Quick question.

Below is code from Kalman filter.

Is 'K' updated with each new period close, or is 'K' held constant to the value we input at the start of the indie? Sorry I cannot tell. I add the code here so you do not have to look it up. Thank you!

Code: Select all
local first;
local source = nil;
local K;
local Sharpness;
local ColorMode;
local KalmanFilter;
local Velocity;
local ShK;

function Prepare()
    source = instance.source;
    K=instance.parameters.K;
    Sharpness=instance.parameters.Sharpness;
    ColorMode=instance.parameters.ColorMode;
    first = source:first()+2;
    Velocity = instance:addInternalStream(first, 0);
    local name = profile:id() .. "(" .. source:name() .. ", " .. instance.parameters.K .. ", " .. instance.parameters.Sharpness .. ")";
    instance:name(name);
    KalmanFilter = instance:addStream("KalmanFilter", core.Line, name .. ".KalmanFilter", "KalmanFilter", instance.parameters.MainClr, first);
    KalmanFilter:setWidth(instance.parameters.widthLinReg);
    KalmanFilter:setStyle(instance.parameters.styleLinReg);
    ShK=math.sqrt(Sharpness*K/100);
end

function Update(period, mode)
   if (period>first) then
    local Distance=source[period]-KalmanFilter[period-1];
    local Error=KalmanFilter[period-1]+Distance*ShK;
    Velocity[period]=Velocity[period-1]+Distance*K/100;
    KalmanFilter[period]=Error+Velocity[period];
    if ColorMode then
     if Velocity[period]>=0 then
      KalmanFilter:setColor(period,instance.parameters.UPclr);
     else
      KalmanFilter:setColor(period,instance.parameters.DNclr);
     end
    end
   elseif period==first then
    Velocity[period]=0;
    KalmanFilter[period]=source[period];
   end
end
Patrick Sweet
 
Posts: 72
Joined: Fri Apr 26, 2013 4:23 am

Re: Kalman filter

Postby Apprentice » Thu Nov 14, 2013 5:00 am

'K' is held constant to the value we input at the start of the indicator.
User avatar
Apprentice
FXCodeBase: Confirmed User
 
Posts: 36341
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia

Re: Kalman filter

Postby Patrick Sweet » Thu Nov 14, 2013 7:08 pm

Thanks. Then I am confused.
The Filter here looks like it does adapt...that is it looks like the Filter adapts (even with K constant as you say). But my eyes may deceive me.

We input K and it remains constant.

It may be terminology and my ignorance. Likely the latter.

But I thought the adaptive part of the of the KF was 'K'.
I thought 'K' would adjust over time (please see text below). And the KF implementation is working pretty well, but I am wondering if we are missing something.

I am sorry to bother. But what is adapting if K is constant? I am not saying the implementation here is wrong. I am just curious about what is happening so I understand 'the hammer' I am using when I swing it.



This below is what I read recently about KAMA..................Whihc may have confused me.

Basically, we start out estimating our guess of the the average and covariance of the hidden series based upon measurements of the observable series, which in this case are simply the normal parameters N(mean, std) used to generate the random walk. From there, the linear matrix equations are used to estimate the values of cov x and x, using linear matrix operations. The key is that once an estimate is made, the value of the covariance of x is then checked against the actual observable time series value, y, and a parameter called K is adjusted to update the prior estimates. Each time K is updated, the value of the estimate of x is updated via:
xt_new_est=xt_est + K*(zt - H*x_est). The value of K generally converges to a stable value, when the underlying series is truly gaussian (as seen in fig 1. during the start of the series, it learns). After a few iterations, the optimal value of K is pretty stable, so the model has learned or adapted to the underlying series.
Patrick Sweet
 
Posts: 72
Joined: Fri Apr 26, 2013 4:23 am

Re: Kalman filter

Postby Apprentice » Fri Nov 15, 2013 3:14 am

Code: Select all
local Distance=source[period]-KalmanFilter[period-1];
    local Error=KalmanFilter[period-1]+Distance*ShK;
    Velocity[period]=Velocity[period-1]+Distance*K/100;
    KalmanFilter[period]=Error+Velocity[period];

Adjustment is based on the difference between the closing price and the previous values ​​of KalmanFilter.
User avatar
Apprentice
FXCodeBase: Confirmed User
 
Posts: 36341
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia

Re: Kalman filter

Postby Patrick Sweet » Fri Nov 15, 2013 5:20 pm

Hi,

First, I like the implementation. It is good. And you guys are GREAT! :D

But I think we are missing something. ;)

I see a nice updating in the code based on user constants that set the 'error'.

But I think Kalman is more than updating. I love what you do. I am not trying to start a debate. I sure I would lose rather quickly!

But what I see is updating based on a set of constants applied to the distance (and velocity) from the past periods to the current period at close. Correct?

Kalman uses a 'prediction' for the current value, then compares that prediction to the actual value after close, to adapt the error factor in the formula? I don't see this happening or am I missing this?

In short, Kalman in TStation (a GOOD implementation!) :D But I think is actually not maximising what Kalman does.

I think I see a real value for the current period being placed at close, when Kalman already has a number at OPEN for the current period and uses that. Then at close, it checks the 'error' in that estimate, then slowly adapts K or Shk to make better and better estimates for the current period. I think? And recurses forward (not backward like Hodrick-Prescott Filter) thus avoiding repainting troubles.

But the magnitude of the 'error' in any given period is mostly a function of the constants I put in and price movement and the constants that are entered never 'adapt' to real data.. ShK=math.sqrt(Sharpness*K/100) and these are constants. The only variable for error is 'Distance' and the last observation (I think I see below) but there is no 'adaptation' for the calculation of error based on previous errors...it is just a new value based on the most recent price move (and the constants applied to that).

local Error=KalmanFilter[period-1]+Distance*ShK;
Velocity[period]=Velocity[period-1]+Distance*K/100;
KalmanFilter[period]=Error+Velocity[period];

So the new KalmanFilter value is an update based on what happened, whereas and the 'predictive' loop that would inform the 'kalman error term' is missing in this implementation?

If I actually understand Kalman correctly, it is entirely possible that I do not undersand the TS code and for this I apologise for taking your time to even read this. You guys are GREAT! But are we not missing something?

Below is an excerpt on Kalman......................Thanks Patrick

But are we not missing the true 'adaptiveness' of Kalman?

The Kalman filter can be written as a single equation, however it is most often conceptualized as two distinct phases: "Predict" and "Update". The predict phase uses the state estimate from the previous timestep to produce an estimate of the state at the current timestep. This predicted state estimate is also known as the a priori state estimate because, although it is an estimate of the state at the current timestep, it does not include observation information from the current timestep. In the update phase, the current a priori prediction is combined with current observation information to refine the state estimate. This improved estimate is termed the a posteriori state estimate
Patrick Sweet
 
Posts: 72
Joined: Fri Apr 26, 2013 4:23 am

Re: Kalman filter

Postby Apprentice » Sat Nov 16, 2013 3:25 am

its entirety possible to have multiple versions ot this indicator.
As stated, this is a translation of MQ4 template.
If u have implementation, formula or description you want to use on MarketScope.
Please post it below.
User avatar
Apprentice
FXCodeBase: Confirmed User
 
Posts: 36341
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia

Next

Return to Custom Indicators

Who is online

Users browsing this forum: No registered users and 57 guests