#include #include "SingleCandle.h" ///////////////////////////////////////////////////////////////////// // SingleCandle ///////////////////////////////////////////////////////////////////// bool SingleCandle::operator ==(SingleCandle const &other) const { return (open == other.open) && (high == other.high) && (low == other.low) && (close == other.close) && (volume == other.volume); } bool SingleCandle::operator !=(SingleCandle const &other) const { return !(*this == other); } void SingleCandle::add(double price, int64_t size) { if (empty()) { open = high = low = close = price; volume = size; } else { if (price > high) high = price; if (price < low) low = price; close = price; volume += size; } } // Update the current candle. Assume that other happened after the // data we've already collected. void SingleCandle::operator +=(SingleCandle const &other) { if (other.empty()) return; if (empty()) (*this) = other; else { if (other.high > high) high = other.high; if (other.low < low) low = other.low; close = other.close; volume += other.volume; } } // Update the current candle. Assume that other happened before the // data we've already collected. void SingleCandle::preAppend(SingleCandle const &other) { if (other.empty()) return; if (empty()) (*this) = other; else { if (other.high > high) high = other.high; if (other.low < low) low = other.low; open = other.open; volume += other.volume; } } TclList SingleCandle::debugDump() const { return TclList()<startTime); if (it == destination.end()) { // The item is new. This case is not strictly required. I expect this // to be the common case, so I optimize for this. destination[original->startTime] = original->candle; } else { // Merge the two values. Assume that the unmarshaled value comes before // the value that is already in the map. The other direction would // be simpler. But I think this is how it is likely to be used. SingleCandle &saved = destination[original->startTime]; SingleCandle temp = saved; saved = original->candle; saved += temp; } } return true; } ///////////////////////////////////////////////////////////////////// // ///////////////////////////////////////////////////////////////////// SingleCandle const *findCandle(std::vector< SingleCandleWithTime > const &list, time_t time) { // This call to lower_bound() takes approximately 10 nanoseconds on // joey-mousepad. For comparison a call to getMicroTime() takes about 8x // that long. const auto it = std::lower_bound(list.begin(), list.end(), time, SingleCandleWithTime::Comp()); if (it == list.end()) return NULL; if (it->startTime != time) return NULL; return &it->candle; }