@Override
public MarketBarType ensureBar(final TimeValue date) {
final MarketDoBar bar = loadBar(MarketBarType.CURRENT.field);
final TimeValue currDate = bar.get(MarketBarField.TRADE_DATE);
if (currDate.equals(date)) {
return MarketBarType.CURRENT;
}
final MarketDoBar prev = loadBar(MarketBarType.PREVIOUS.field);
// Check for new trading session
if (currDate.asMillisUTC() < date.asMillisUTC()) {
// log.debug("New session: old=" + currDate + "; new=" + date);
// Roll values to previous
prev.copy(bar);
// Reset current
bar.clear();
bar.set(MarketBarField.TRADE_DATE, date);
// Copy last updated time from previous session for continuity
bar.set(MarketBarField.BAR_TIME, prev.get(MarketBarField.BAR_TIME));
// Reset extended
loadBar(MarketBarType.CURRENT_EXT.field).copy(bar);
// Notify change tracking
setBar(MarketBarType.PREVIOUS, prev);
setBar(MarketBarType.CURRENT, bar);
return MarketBarType.CURRENT;
}
// Check previous bar
final TimeValue prevDate = prev.get(MarketBarField.TRADE_DATE);
if (prevDate.isNull()) {
prev.set(MarketBarField.TRADE_DATE, date);
return MarketBarType.PREVIOUS;
} else if (prevDate.equals(date)) {
return MarketBarType.PREVIOUS;
}
// No match, nothing to update
return MarketBarType.NULL_BAR_TYPE;