Hello JBTers,

Consider this challenge: over a time window of fixed length (let's say 2 
hour time window), I'd like to know the high price and low price in that 
window. The straightforward solution is to iterate over the prices in the 
window and compute the min and max values, which has to be done every time 
an old price drops from the window and the new price is added to the 
window. This works, but it's very inefficient. It requires N operations 
where N is the length of the time window. This makes strategy optimization 
which uses this indicator painfully slow. All other indicators in JBT 
require just one operation to update the indicator.

So, the challenge is to to make the min/max calculation in a moving window 
more computationally efficient. I attached the inefficient solution, where 
the update() method has to be called every time before min or max can be 
returned.

Any takers to improve this?

Thanks,
Eugene.



-- 
You received this message because you are subscribed to the Google Groups 
"JBookTrader" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/jbooktrader?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


package com.jbooktrader.platform.util.movingwindow;

/**
 * @author Eugene Kononov
 */
public class MovingWindow {
    protected final double[] elements;
    private int start, end;
    protected boolean isFull;
    private final int capacity;

    public MovingWindow(int capacity) {
        this.capacity = capacity;
        elements = new double[capacity];
    }

    public int getCapacity() {
        return capacity;
    }

    public double get(int position) {
        int index = (start + position - 1) % capacity;
        if (index < 0) {
            index = capacity + index;
        }

        return elements[index];
    }

    public double getFirst() {
        return get(0);
    }

    public double getLast() {
        return get(capacity - 1);
    }

    public void add(double value) {
        elements[end] = value;
        end = (end + 1) % capacity;
        if (end == start) {
            start = (start + 1) % capacity;
            isFull = true;
        }
    }

    public boolean isFull() {
        return isFull;
    }

    public void clear() {
        isFull = false;
        start = end = 0;
        for (int index = 0; index < capacity; index++) {
            elements[index] = 0;
        }
    }
}
package com.jbooktrader.platform.util.movingwindow;

/**
 * @author Eugene Kononov
 */
public class MovingWindowMinMax  extends MovingWindow {
    private double min, max;

    public MovingWindowMinMax(int capacity) {
        super(capacity);
    }

    public double getMin() {
        return min;
    }

    public double getMax() {
        return max;
    }

    public void update() {
        min = max = elements[0];
        int capacity = getCapacity();
        for (int index = 1; index < capacity; index++) {
            double element = elements[index];
            if (element > max) {
                max = element;
            } else if (element < min) {
                min = element;
            }
        }
    }
}

Reply via email to