Rajandran R Founder of Marketcalls and Co-Founder Algomojo. Full-Time Derivative Trader. Expert in Designing Trading Systems (Amibroker, Ninjatrader, Metatrader, Python, Pinescript). Trading the markets since 2006. Mentoring Traders on Trading System Designing, Market Profile, Orderflow and Trade Automation.

Kalman Filter and Unscented Kalman Filter AFL in Amibroker using Python ComServer

1 min read

In the last tutorial we explored Kalman filter and how to build kalman filter using pykalman python library. In this section we will be dealing with python com server to integrate Amibroker + Python to compute Kalman Filter and Unscented Kalman Filter Mean Estimation and plot the same in Amibroker.

Kalman Filter Estimate

The Kalman Filter and Unscented Kalman Filter is a unsupervised algorithm for tracking a single object in a continuous state space. Given a sequence of noisy measurements, the Kalman Filter is able to recover the “true state” of the underling object being tracked. The difference between kalman and unscented kalman filter is that while the Kalman Filter restricts dynamics to affine functions, the Unscented Kalman Filter is designed to operate under arbitrary dynamics.

UnScented Kalman Filter Estimate

Python Library Used:

Pandas – Python Data Analysis and Data structure Library(to handle time series data).
PyKalman – Library to compute Kalman Filter and Unscented Kalman Filter.
Scipy(Dependency Library of PyKalman) – Library used for scientific computing and technical computing

Since Kalman Filter is a statistical model it is relatively difficult to code in AFL Programming Language and hence we rely on Amibroker with Python COM Server and relative python libraries which ease our work. Price data is sent from Amibroker to Python Com Server and then Python does the kalman filter computations and returns back to Amibroker.

Steps to follow in Amibroker

1)Download Kalman-AFL Set and Unzip it
2)Copy the file pyKalman.py to \\python2.7\\bin\\ folder. And execute the file with the command python pyKalman.py in your command prompt as shown below

3)Copy the PyAFL – Kalman Filter.afl file and paste the file in \\Amibroker\\Formulas\\Basic Charts Folder

4)Open a New Blank Chart and apply PyAFL – Kalman Filter.afl to it.

5)Now right click over the charts -> goto parameters and change between Kalman and UnScented Kalman to compute the relevant statistical tool.

Incase if you are very new to Python here is a tutorial on how to install python and relevant python librariess

Rajandran R Founder of Marketcalls and Co-Founder Algomojo. Full-Time Derivative Trader. Expert in Designing Trading Systems (Amibroker, Ninjatrader, Metatrader, Python, Pinescript). Trading the markets since 2006. Mentoring Traders on Trading System Designing, Market Profile, Orderflow and Trade Automation.

Algomojo Index Straddle/Strangle Execution Module with Intraday Stoploss

This tutorial focus on how to automate your index straddle/strangle strategy with intraday stop-loss levels with time-based entry and exits using Algomojo Platform and...
Rajandran R
7 min read

Learn How to Login into Algomojo Account using Amibroker

In the last tutorial, we learn how to automate the algomojo login process and in this video tutorial, we are going to learn with...
Rajandran R
1 min read

Amibroker Execution Module – Hedged Index Futures for Directional…

In the last tutorial, we learn the importance of Hedging Futures Position with OTM Options for Reduced Margin. This tutorial focus on how to...
Rajandran R
14 min read

10 Replies to “Kalman Filter and Unscented Kalman Filter AFL in Amibroker…”

  1. Thanks Ranjadran, really very good posts dealing with Python integration.

    Would like your advice in this:

    1. I used your IIR filter as you describe it, but initially got error from Python as the module “pythoncom” was not present. After some googling found that that module comes with the package pywin32 (sourceforge.net/projects/pywin32/files/pywin32). I installed it and followed the rest of your instructions and worked like a charm.
    My first question is to check if this was the right thing to do or do you have a “complete package” that can be deployed for your posts about this subject.

    2. Secondly I tried your kalmar filters, but then again got compiling errors registering the py file. This time the module not present in my system is Pandas. Again, after some googling I see this is a separate package but rather more complicated to install that pywin32 (it is suggested to use Anaconda).
    May you please give advice in how to proceed?

    Thanks again
    Mika

  2. Opps! please forget my previous questions, I had missed the last sentence in your post

    “Incase if you are very new to Python here is a tutorial on how to install python and relevant python librariess”

    I will check the videos.

    Thanks

      1. Hi Ranjadran

        I started from scratch, and noticed that activepyhton 2.7.13.2714 already install most of the libraries you mentioned.
        Again, installing/running IIR filter was a breeze, but Kalman filter requires the pykalman library that I can’t find a way to install…

        Could you please shed some light here?

        Thanks

  3. Hi Rajandran

    Thank you very much for this interesting tutorial which allows me to make my first steps with Python.
    I have to say that I have had some hard times to install all the Python dependancies with the correct versions and bitness. Nevertheless, at the end, I succeed to run the “python pykalman.py” command and to get the magical words “Registering COMserver / Registered: PyKalman”.
    Then, I jumped to Amibroker and open enthusiastically the afl code. To my big disapointment, Amibroker immediately shutdown without any error message. I repeat the operations and always obtain the same behaviour. I have also tested your IIR code with the same result.
    I am using Amibroker 6.19.
    I have slightly corrected your kalman afl code (using the IIR example) by adding “price = nz(price);” to avoid any null value.
    Do you have any idea on what happened?
    Cheers.

  4. Hi Rajandran thx for the instruction how to use python in AB. I was succesfull installing the Pycoint sample.

    But for the Kalmann I always receive the following error “COM error: sour python com server intertal error description: Unexpeced Python Error: TypeError: Internal error – the buffer length is not the sequence lengt!

    Cound it be an python 3.6 error since you created it for 2.x?
    thx Oliver

  5. Hello I installed ANACONDA instead of python . I am not able to see the folder \\python2.7\\bin\\ . Please help. I am using Spider 3.7

Leave a Reply

Get Notifications, Alerts on Market Updates, Trading Tools, Automation & More