From 425b2eff0d115b89d33de7e01e1d96efaa4fb0da Mon Sep 17 00:00:00 2001 From: Eric Harding Date: Mon, 18 Sep 2017 16:20:23 -0400 Subject: [PATCH] spark-180 consistent book read (c++) --- src/lib/md/MDConsumerHandle.cpp | 24 +++++++++++++++++++++++- src/lib/md/MDConsumerHandle.h | 22 +++++++++++++++++++--- 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/src/lib/md/MDConsumerHandle.cpp b/src/lib/md/MDConsumerHandle.cpp index 5f7d19f5b..0683ff68d 100644 --- a/src/lib/md/MDConsumerHandle.cpp +++ b/src/lib/md/MDConsumerHandle.cpp @@ -26,7 +26,7 @@ void MDConsumerHandle::init(const ColdHeader& cheader, const ColdEntry& centry, _cheader = &cheader; } -BTS_EXPORT TimeStamp copy_bbo(const TimeStamp& time, const SingleWriterPriceSizeStore& in_bid, const SingleWriterPriceSizeStore& in_ask, PriceSize& out_bid, PriceSize& out_ask) { +TimeStamp copy_bbo(const TimeStamp& time, const SingleWriterPriceSizeStore& in_bid, const SingleWriterPriceSizeStore& in_ask, PriceSize& out_bid, PriceSize& out_ask) { // We expect the time, in_bid and in_ask to be changing (rapidly) // TimeStamp t; @@ -47,6 +47,28 @@ BTS_EXPORT TimeStamp copy_bbo(const TimeStamp& time, const SingleWriterPriceSize return t; } +TimeStamp copy_depth( + const TimeStamp& time, + const SingleWriterPriceSizeStore* source, + size_t count, + PriceSize* bid, PriceSize* ask) +{ + TimeStamp t; + int attempts = 0; + const int max_attempts = 1619200; + do { + for (size_t x = 0; x < count; ++x) { + bid[x] = source[2*x].load(); + ask[x] = source[2*x+1].load(); + } + t = time; + } while((t != time || t == TimeStamp::invalid()) && attempts < max_attempts); + if (attempts >= max_attempts) { + return TimeStamp::invalid(); + } + return t; +} + TimeStamp MDConsumerHandle::last_market_update() const { if (has_depth()) { return hotd()->timestamp; diff --git a/src/lib/md/MDConsumerHandle.h b/src/lib/md/MDConsumerHandle.h index 5c60bc4ac..ae82c8720 100644 --- a/src/lib/md/MDConsumerHandle.h +++ b/src/lib/md/MDConsumerHandle.h @@ -8,8 +8,8 @@ namespace bts { namespace md2 { -BTS_EXPORT TimeStamp copy_bbo(const TimeStamp& , const SingleWriterPriceSizeStore& , - const SingleWriterPriceSizeStore& , PriceSize& , PriceSize& ); +TimeStamp copy_bbo(const TimeStamp&, const SingleWriterPriceSizeStore&, const SingleWriterPriceSizeStore&, PriceSize&, PriceSize& ); +TimeStamp copy_depth(const TimeStamp& time, const SingleWriterPriceSizeStore* source, size_t count, PriceSize* bid, PriceSize* ask); enum class HandleReadResult { NO_CHANGE, @@ -39,7 +39,6 @@ public: TimeStamp last_market_update() const; - HandleReadResult get_bbo(TimeStamp& time, PriceSize& bid, PriceSize& ask) const { if (!is_initialized()) return HandleReadResult::NO_CHANGE; @@ -65,6 +64,23 @@ public: return HandleReadResult::NO_CHANGE; } + HandleReadResult get_depth(TimeStamp& time, size_t depth, PriceSize* bids, PriceSize* asks) const { + bts_assert_d(depth > 0 && depth <= COCONUT_MAX_DEPTH); + if (!is_initialized()) return HandleReadResult::NO_CHANGE; + if (_cheader->has_depth) { + auto* pdepth = hotd(); + if (pdepth->timestamp > time || pdepth->timestamp == TimeStamp::invalid()) { + TimeStamp temp = copy_depth(pdepth->timestamp, pdepth->book, depth, bids, asks); + if (temp == TimeStamp::invalid()) { return HandleReadResult::BUSY; } + time = temp; + return HandleReadResult::SUCCESS; + } + } + else { + return get_bbo(time, *bids, *asks); + } + return HandleReadResult::NO_CHANGE; + } // Last bbo or depth update PriceSize get_bid(size_t depth) const;