Now you are starting to talk about what I write every day for work.

First thing that comes to mind for a sliding window is to use a linked
list.   Add to the end, remove from the begging.

For running sums, you do something like this (very rough pseudocode):


LinkedList Xs;
LinkedLIst Ys;

void calculate(newX, newY)
{
     Xs.addLast(newX);
     Ys.addLast(newY);

     oldX = Xs.getFirst();
     oldY = Ys.getFirst();
     Xs.removeFirst();
     Ys.removeFirst();


     Xsum -= oldX;
     Ysum -= oldY;
     XXsum -= oldX * oldX;
     XYsum -= oldX * oldY;

     Xsum += newX;
     Ysum += newY;
     XXsum += newX * newX;
     XYsum += newX * newY;

}


Now you don't waste so much time summing squares, etc.

Another bit of advice: for large amounts of accumulation, integer values may
be more predictable, as they don't accumulate weird bits of noise from
floating point errors.  I have not verified this actually happens in the
real world.

And another option is to store less data and create "weighted average bars",
covering longer amounts of time.






On Tue, Aug 4, 2009 at 8:22 AM, nonlinear5 <[email protected]> wrote:

>
> I have a new indicator, called DepthPriceCorrelation. It's not in the
> release yet, but it's in SVN
>
> http://code.google.com/p/jbooktrader/source/browse/trunk/source/com/jbooktrader/indicator/depth/DepthPriceCorrelation.java
>
> The indicator is based on the idea that current price can be
> considered "fair" when the correlation between market depth balances
> and market prices is positive. This positive correlation occurs when:
>
> -- high depth balances are accompanied by higher prices
> or
> -- low depth balances are accompanied by lower prices
>
> When the correlation is negative, the prices are moving in the
> direction opposite from the direction of depth balances, and I call it
> a "high tension" condition. This is when my strategy gets into a
> position on the bet that the tension will ease and the correlation
> will return to its "normal" positive value. Here is an example of such
> a strategy:
>
> http://code.google.com/p/jbooktrader/source/browse/trunk/source/com/jbooktrader/strategy/TensionSeeker.java
>
> Now, to calculate the correlation, the indicator simply updates the
> running sums for prices and balances, and then uses a standard
> correlation coefficient formula to come up with the result. This is
> very efficient, and it works well. The problem is, sometime in the
> second half of the trading session, the indicator becomes too "stale",
> because it uses all the data accumulated so far during the trading
> session. So, at say, 2pm, the indicator would represent the
> correlation between balances and prices based on all the data from
> 9:30am to 2pm, while my strategy is looking for a shorter term
> correlation, such as the last 2 hours. It's certainly possible to
> recalculate the indicator based on this 2-hour moving window, but it
> would be very computationally expensive, since every time, I would
> need to loop through the last two hours of values, instead of simply
> updating the running sums.
>
> So, here is the question for the algorithmically inclined. How do I
> *efficiently* calculate the correlation between X and Y in a moving
> time window?
>
>
>
>
> >
>

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

Reply via email to