package com.jbooktrader.indicator.price;

import com.jbooktrader.platform.indicator.*;
import jama.*;
import jkalman.*;

public class PriceKalman extends Indicator {
    private final JKalman kalman;
    private final Matrix m;

    public PriceKalman() {
        try {
            kalman = new JKalman(4, 2);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        m = new Matrix(2, 1); // measurement [x]
        // transitions for x, y, dx, dy
        double[][] tr = {{1, 0, 1, 0}, {0, 1, 0, 1}, {0, 0, 1, 0}, {0, 0, 0, 1}};
        kalman.setTransition_matrix(new Matrix(tr));
        kalman.setError_cov_post(kalman.getError_cov_post().identity());

    }

    @Override
    public void reset() {
        value = marketBook.getSnapshot().getPrice();
    }

    @Override
    public void calculate() {
        double price = marketBook.getSnapshot().getPrice();
        kalman.Predict();
        m.set(1, 0, price);
        kalman.Correct(m);
        value = kalman.getState_post().get(1, 0);
    }
}
