Venice 0.751beta

nz.org.venice.quote
Class EODQuoteCache

java.lang.Object
  extended bynz.org.venice.quote.EODQuoteCache

public class EODQuoteCache
extends java.lang.Object

This class contains all the end-of-day stock quotes currently in memory. Its purpose is to cache end-of-day stock quotes so that tasks do not have to query the database or files whenever they need a quote. While this is a cache it does not control when stock quotes are loaded or freed, that is controlled by EODQuoteBundleCache.

Tasks should not directly call this class, but should go through a EODQuoteBundle.

When tasks access quotes in a quote cache, either directly or via a quote bundle they can access quotes in two ways. The first way is specifying the actual date they are interested in, i.e. a TradingDate. The other way is specifying a fast access date offset. The fast access date offset is used when lots of quotes have to be queried as fast as possible.

The latest date in the cache has an offset of 0. The previous trading date (i.e. not a weekend) has offset -1, the previous one to that -2 etc. You can convert to and from fast access dates using dateToOffset(nz.org.venice.util.TradingDate) and offsetToDate(int).

Author:
Andrew Leppard
See Also:
EODQuote, EODQuoteBundle, EODQuoteBundleCache

Method Summary
 boolean containsQuote(Symbol symbol, int dateOffset)
          Return whether we currently have any quotes for the given symbol on the given date
 int dateToOffset(TradingDate date)
          Convert between a date and its fast access date offset.
static void expire()
           
 void free(Symbol symbol, int dateOffset)
          Remove the given quote from the cache.
 TradingDate getFirstDate()
          Get the oldest date in the cache.
 int getFirstDateOffset()
          Get the fast access offset of the oldest date in the cache.
static EODQuoteCache getInstance()
          Create or return the singleton instance of the quote cache.
 TradingDate getLastDate()
          Get the newest date in the cache.
 EODQuote getQuote(Symbol symbol, int dateOffset)
          Get a quote from the cache.
 double getQuote(Symbol symbol, int quoteType, int dateOffset)
          Get a quote from the cache.
 java.util.List getSymbols(int dateOffset)
          Return all the symbols in the cache on the given date.
 java.util.List getSymbols(int firstDateOffset, int lastDateOffset)
          Return all the symbols in the cache between the given date range (inclusive).
static boolean isInstantiated()
          Returns whether this class has been instantiated yet.
 void load(EODQuote quote)
          Load the given quote into the cache.
 void load(Symbol symbol, TradingDate date, long day_volume, float day_low, float day_high, float day_open, float day_close)
          Load the given quote into the cache.
 TradingDate offsetToDate(int dateOffset)
          Convert between a fast access date offset and a date.
 int size()
          Return the number of quotes in the cache.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getInstance

public static EODQuoteCache getInstance()
Create or return the singleton instance of the quote cache.

Returns:
singleton instance of this class

expire

public static void expire()

isInstantiated

public static boolean isInstantiated()
Returns whether this class has been instantiated yet. This is used by the tuning page, which needs to know the number of quotes in the cache. But it doesn't want to be the first to instantiate the cache, because that would cause it to access the quote source.. which might not be set up at that stage.

Returns:
true if this class has been instantiated.

getQuote

public double getQuote(Symbol symbol,
                       int quoteType,
                       int dateOffset)
                throws QuoteNotLoadedException
Get a quote from the cache.

Parameters:
symbol - the symbol to load
quoteType - the quote type, one of Quote.DAY_OPEN, Quote.DAY_CLOSE, Quote.DAY_LOW, Quote.DAY_HIGH, Quote.DAY_VOLUME
dateOffset - fast access date offset
Returns:
the quote
Throws:
QuoteNotLoadedException - if the quote was not in the cache

getQuote

public EODQuote getQuote(Symbol symbol,
                         int dateOffset)
                  throws QuoteNotLoadedException
Get a quote from the cache.

Parameters:
symbol - the symbol to load
dateOffset - fast access date offset
Returns:
the quote
Throws:
QuoteNotLoadedException - if the quote was not in the cache

getSymbols

public java.util.List getSymbols(int dateOffset)
Return all the symbols in the cache on the given date.

Parameters:
dateOffset - fast access date offset
Returns:
list of symbols

getSymbols

public java.util.List getSymbols(int firstDateOffset,
                                 int lastDateOffset)
Return all the symbols in the cache between the given date range (inclusive).

Parameters:
firstDateOffset - fast access offset of first date
lastDateOffset - fast access offset of last date
Returns:
list of symbols

containsQuote

public boolean containsQuote(Symbol symbol,
                             int dateOffset)
Return whether we currently have any quotes for the given symbol on the given date

Parameters:
symbol - symbol
dateOffset - fast access date offset
Returns:
TRUE if we have the quote

load

public void load(EODQuote quote)
Load the given quote into the cache.

Parameters:
quote - the quote

load

public void load(Symbol symbol,
                 TradingDate date,
                 long day_volume,
                 float day_low,
                 float day_high,
                 float day_open,
                 float day_close)
Load the given quote into the cache.

Parameters:
symbol - symbol of quote
date - quote date
day_volume - day volume
day_low - day low
day_high - day high
day_open - day open
day_close - day close

free

public void free(Symbol symbol,
                 int dateOffset)
Remove the given quote from the cache. It's OK if the quote isn't loaded.

Parameters:
symbol - the symbol of the quote to remove
dateOffset - the fast access date offset of the quote to remove

dateToOffset

public int dateToOffset(TradingDate date)
                 throws WeekendDateException
Convert between a date and its fast access date offset.

Parameters:
date - the date
Returns:
fast access date offset
Throws:
WeekendDateException - if the date is on a weekend (there are no fast access date offsets for weekend dates)

offsetToDate

public TradingDate offsetToDate(int dateOffset)
Convert between a fast access date offset and a date.

Parameters:
dateOffset - fast access date offset
Returns:
the date

size

public int size()
Return the number of quotes in the cache.

Returns:
the cache size

getFirstDate

public TradingDate getFirstDate()
Get the oldest date in the cache.

Returns:
the oldest date in cache or null if the cache is empty.

getLastDate

public TradingDate getLastDate()
Get the newest date in the cache.

Returns:
the newest date in cache or null if the cache is empty.

getFirstDateOffset

public int getFirstDateOffset()
Get the fast access offset of the oldest date in the cache.

Returns:
the fast access offset of oldest date in cache or +1 if there are no dates in the cache.

Venice 0.751beta