Dear Eugene, 

here is my take on this. 
It predates your question, as I ran into this problem some time back. 
(Thus, the interface does not match yours exactly)
It contains an improvement insofar, as updating is only necessary if a max 
or min 
value is removed. 
One could go beyond this in case value repetition occurs (rather likely for 
base data, less for indicators as they change based on history)
In this case several max or min values may be at the same time part of the 
window. 
One could count them and wait with updating until the last one drops out. 
As I focussed on indicators, this optimization is not contained. 

I hope it is what you are looking for. 
If there are further optimizations, I would be interested as well, of 
course.

Cheers
 Klaus



Am Samstag, 23. März 2013 10:47:43 UTC-3 schrieb Eugene Kononov:
>
> 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.indicator.myexperiments;

public class MySimpleMovingWindow {
    private final double[] buffer;
    private double max = Double.MIN_VALUE, min = Double.MAX_VALUE;   // new for max, min FCT
    private int start = 0, end = 0;
    private int entries = 0; // Zahl der belegten Eintr?ge
    private int size = 0; // Gr??e des Puffers
    private boolean isFull;

    public MySimpleMovingWindow(int bsize) {
        buffer = new double[bsize];
        size = bsize; 
    }

    public void add(double value) {
        double oldestValue = buffer[end];
        
        buffer[end] = value;
        end = (end + 1) % size;
        
        // Achtung: falls entries < size, dann sind alle Eintr?ge zwischen 0 und entries-1
        //     belegt, sonst sind alle belegt und entries = size !!
        //  Diese Eigenschaft wird in den Schleifen genutzt, um nicht in unbelegten Pl?tzen 
        // zu suchen.
        
        // Max, Min Ops
        // If Max-Value 
        if (value > max) max = value;
        else if (oldestValue == max) // search for a new max value
        {
        	max = buffer[0];
        	for (int i = 1; i <= entries - 1; i++) 
        		if (buffer[i] > max) max = buffer[i];
        }
        if (value < min) min = value;
        else if (oldestValue == min) // search for a new min value
        {
        	min = buffer[0];
        	for (int i = 1; i <= entries - 1; i++) 
        		if (buffer[i] < min) min = buffer[i];
        }

        if (end == start) {
            start = (start + 1) % size;
            isFull = true;
        } else {
        	entries++;
        }
    }

    public boolean isFull() {
        return isFull;
    }

  
    public double getOldest() {
        return buffer[end];
    }
    
    public double getMax() {
        return max;
    }

    public double getMin() {
        return min;
    }

    public void clear() {
        isFull = false;
        start = end = 0;
        max = Double.MIN_VALUE;
        min = Double.MAX_VALUE;
        entries = 0; 
        
        for (int i = 0; i < buffer.length; i++) {
            buffer[i] = 0;
        }
    }
}

Reply via email to