Kalman filter

 
Hello;
I have completed a preliminary version of a Kalman filter and it is available for downloading from http://www.gammarat.com/Forex/BlackBoxes/Kalman/ . You will need the three files:

1) GRFKalmanFilter.mq4 - put this in the indicators directory
2) GRFMatrixMath.mq4 - put this in the libraries directory
3) GRFMatrixMath.mqh - put this in the include directory

Items 1) and 3) need to be compiled. The GRFKalmanFilter.mq4.txt file is only there for those without MQL4, so they can see the algorithm.

A Kalman filter works similarly to a moving average, except that the lag is much less; and it works by adjusting itself to the level of noise, rather than a fixed averaging length. The most important parameter in this indicator is the Suppress_dB variable. By increasing the variable, higher frequency information is treated as noise, and the trend line becomes smoother. By decreasing it, higher frequency noise is allowed in, and the trend line becomes less smooth. It is normally set at 0, though for Yen crosses I find setting it higher (to around 6) works better.

The extra lines - the dashed yellow and red lines, and the dashed red lines, and the solid red lines - are calculated off the one sided deviations on either side of the trend line. Their smoothness is adjusted by increasing or decreasing the Samples parameter. They can be turned off altogether by setting the DevLevel ( 1 or 2) to zero.

The variables "PredictBars" give estimates of the position of the trend line in Bars ahead from the last point. If they are set to 0, the results are not calculated.

I tend to run three filters at a time on a pair - one, with the Suppress_dB set to zero, that plots the main trend and the error regions; one set at -20 or -30, with the DevLevel variables set to 0 and the colour to light blue, that I treat as a "fast line", and one set at 20 or 30, as a slow line. In a trend I treat the yellow dashed lines as entry and exit levels. Sometimes I add another Kalman indicator, with DevLevel2 set to zero and Devlevel1 to 4 or 5 (and change the line type to solid red). Thiis I use as a worst acceptable Stop loss. It should never be touched if you have properly gauged the trend, and the trend is in place.

The matrix math library is very primitive, but works for this. I hope to get a standard C DLL installed in the near future. I think ThomasB has got me set on the right path now..

The code is under "copyleft", which means you can basically do anything that you want with it, except copyright it. I would be happy to hear about any changes you make, or think should be made; as for me, I am working on turning the code into a function, so it can be used like any other running average, or included in an intelligent filter bank.

Cheers;
CB
 
Looks very nice - I will play with it a bit. I just missed comments in the code (Matrix library) but I will figure out how it works anyway.
 
ThomasB:
Looks very nice - I will play with it a bit. I just missed comments in the code (Matrix library) but I will figure out how it works anyway.

Comments in the code??? Lol...(Sorry...) But I will put them in someday. But if I get the C language matrix libraries running as a DLL, then I won't need to (and they'll be a lot faster).

FWIW - the matrix operations are pretty simple, and follow basic linear algebra. So they aren't real robust. But if you have questions, let me know.

Addded - BTW, I do tend to put my arguments in input first, and then output, as in
MatrixAdd(double inputA[], double inputB[] double & outputC[]), rather than the other way as many MQL4 functions seem to be. Perhaps I should change this habit? Let me know what you or others think...

Cheers;
Chris
 

Been off for awhile building additins to the KF, including autoigain (which gets rid of the need to estimate what gain is best) and autostart (which finds the appropriate starting value for the autogain. The mq4 program is available at http://gammarat.com/Forex/BlackBoxes/Kalman/GRFKalmanAGASJW.mq4 ; whie a typical template for it's use is at http://gammarat.com/Forex/BlackBoxes/Kalman/grfkalmanagasjw_hourly.tpl

Make sure your houly buffers have access to as much data as possible (usually 2 years worth, or more). Generally I sell/buy off the turning points of the fast line (thick, Aqua) on the template, using the main trend indicator (heavy yellow as a guide) The dashed lines represent upper and lower deviations, the various thin lines represent different averaging lengths representing different trend behaviours, useful for estimating stop-losses and targets, and the strength of the move.

The code is (or should be) self-contained (no libraries needed)

Below is a typical chart.
Cheers;
CB


 
Halifax:
Hello;
I have completed a preliminary version of a Kalman filter and it is available for downloading from http://www.gammarat.com/Forex/BlackBoxes/Kalman/ . You will need the three files:

1) GRFKalmanFilter.mq4 - put this in the indicators directory
2) GRFMatrixMath.mq4 - put this in the libraries directory
3) GRFMatrixMath.mqh - put this in the include directory

Items 1) and 3) need to be compiled. The GRFKalmanFilter.mq4.txt file is only there for those without MQL4, so they can see the algorithm.

A Kalman filter works similarly to a moving average, except that the lag is much less; and it works by adjusting itself to the level of noise, rather than a fixed averaging length. The most important parameter in this indicator is the Suppress_dB variable. By increasing the variable, higher frequency information is treated as noise, and the trend line becomes smoother. By decreasing it, higher frequency noise is allowed in, and the trend line becomes less smooth. It is normally set at 0, though for Yen crosses I find setting it higher (to around 6) works better.

The extra lines - the dashed yellow and red lines, and the dashed red lines, and the solid red lines - are calculated off the one sided deviations on either side of the trend line. Their smoothness is adjusted by increasing or decreasing the Samples parameter. They can be turned off altogether by setting the DevLevel ( 1 or 2) to zero.

The variables "PredictBars" give estimates of the position of the trend line in Bars ahead from the last point. If they are set to 0, the results are not calculated.

I tend to run three filters at a time on a pair - one, with the Suppress_dB set to zero, that plots the main trend and the error regions; one set at -20 or -30, with the DevLevel variables set to 0 and the colour to light blue, that I treat as a "fast line", and one set at 20 or 30, as a slow line. In a trend I treat the yellow dashed lines as entry and exit levels. Sometimes I add another Kalman indicator, with DevLevel2 set to zero and Devlevel1 to 4 or 5 (and change the line type to solid red). Thiis I use as a worst acceptable Stop loss. It should never be touched if you have properly gauged the trend, and the trend is in place.

The matrix math library is very primitive, but works for this. I hope to get a standard C DLL installed in the near future. I think ThomasB has got me set on the right path now..

The code is under "copyleft", which means you can basically do anything that you want with it, except copyright it. I would be happy to hear about any changes you make, or think should be made; as for me, I am working on turning the code into a function, so it can be used like any other running average, or included in an intelligent filter bank.

Cheers;
CB


Hello Halifax,

I would like to download and test Your scripts with pleasure, but when I try to dowload them by using http://www.gammarat.com/Forex/BlackBoxes/Kalman/ link, I'm receiving message:

"You don't have permission to access /Forex/BlackBoxes/Kalman/ on this server"

Could You make me able to download Your files?


Best Regards,

Daniel

 

i am a student from university of colombo, srilanka

i am currently doing a research Forex forcasting by using Kalman filter (e.g take only US $ forcasting)

i am unable to estimate it transition matrix

cann u plzzz help me to do it.......


 

how to estimate matrices of the kalman filter???

 
Halifax:
Hello;
I have completed a preliminary version of a Kalman filter and it is available for downloading from http://www.gammarat.com/Forex/BlackBoxes/Kalman/ . You will need the three files:

1) GRFKalmanFilter.mq4 - put this in the indicators directory
2) GRFMatrixMath.mq4 - put this in the libraries directory
3) GRFMatrixMath.mqh - put this in the include directory

Items 1) and 3) need to be compiled. The GRFKalmanFilter.mq4.txt file is only there for those without MQL4, so they can see the algorithm.

A Kalman filter works similarly to a moving average, except that the lag is much less; and it works by adjusting itself to the level of noise, rather than a fixed averaging length. The most important parameter in this indicator is the Suppress_dB variable. By increasing the variable, higher frequency information is treated as noise, and the trend line becomes smoother. By decreasing it, higher frequency noise is allowed in, and the trend line becomes less smooth. It is normally set at 0, though for Yen crosses I find setting it higher (to around 6) works better.

The extra lines - the dashed yellow and red lines, and the dashed red lines, and the solid red lines - are calculated off the one sided deviations on either side of the trend line. Their smoothness is adjusted by increasing or decreasing the Samples parameter. They can be turned off altogether by setting the DevLevel ( 1 or 2) to zero.

The variables "PredictBars" give estimates of the position of the trend line in Bars ahead from the last point. If they are set to 0, the results are not calculated.

I tend to run three filters at a time on a pair - one, with the Suppress_dB set to zero, that plots the main trend and the error regions; one set at -20 or -30, with the DevLevel variables set to 0 and the colour to light blue, that I treat as a "fast line", and one set at 20 or 30, as a slow line. In a trend I treat the yellow dashed lines as entry and exit levels. Sometimes I add another Kalman indicator, with DevLevel2 set to zero and Devlevel1 to 4 or 5 (and change the line type to solid red). Thiis I use as a worst acceptable Stop loss. It should never be touched if you have properly gauged the trend, and the trend is in place.

The matrix math library is very primitive, but works for this. I hope to get a standard C DLL installed in the near future. I think ThomasB has got me set on the right path now..

The code is under "copyleft", which means you can basically do anything that you want with it, except copyright it. I would be happy to hear about any changes you make, or think should be made; as for me, I am working on turning the code into a function, so it can be used like any other running average, or included in an intelligent filter bank.

Cheers;
CB


 

unable to access above link

http://www.gammarat.com/Forex/BlackBoxes/Kalman/

it gives the following errror

Forbidden

You don't have permission to access /Forex/BlackBoxes/Kalman/ on this server.

Apache/2.2.3 (Red Hat) Server at www.gammarat.com Port 80

how to download the files??

 
Why do you expect a link to still work after more than 5 years? And why do you think anyone on this site would know?
 

looks nice.how can i get the indicator?

thanks!

Reason: