SPARK-1659 Refactor to use IntPrice everywhere to reduce logic duplication
This commit is contained in:
1 parent
e8b210913c
commit
d6539348ea
7 files changed
+104
-126
No files matched your search
@@ -62,8 +62,7 @@ inline void read_combined(
|
||||
// Compare bids
|
||||
if (direct.price() == implied.price()) {
|
||||
bid[x] = PriceSize(
|
||||
direct.mantissa(),
|
||||
direct.exponent(),
|
||||
direct.int_price(),
|
||||
direct.size() + implied.size(),
|
||||
direct.size());
|
||||
xdb += 1;
|
||||
@@ -92,8 +91,7 @@ inline void read_combined(
|
||||
// Compare asks
|
||||
if (direct.price() == implied.price()) {
|
||||
ask[x] = PriceSize(
|
||||
direct.mantissa(),
|
||||
direct.exponent(),
|
||||
direct.int_price(),
|
||||
direct.size() + implied.size(),
|
||||
direct.size());
|
||||
xda += 1;
|
||||
|
||||
+32
-51
@@ -88,13 +88,13 @@ class PriceSize {
|
||||
uint32_t _direct_size;
|
||||
|
||||
public:
|
||||
PriceSize(int64_t mantissa, int8_t exponent, Size size) :
|
||||
_price(mantissa, exponent),
|
||||
PriceSize(IntPrice price, Size size) :
|
||||
_price(price),
|
||||
_size(size),
|
||||
_direct_size(size)
|
||||
{ }
|
||||
PriceSize(int64_t mantissa, int8_t exponent, Size size, Size direct_size) :
|
||||
_price(mantissa, exponent),
|
||||
PriceSize(IntPrice price, Size size, Size direct_size) :
|
||||
_price(price),
|
||||
_size(size),
|
||||
_direct_size(direct_size)
|
||||
{ }
|
||||
@@ -134,9 +134,9 @@ PriceSize PriceSize_from_double(double price, Size size) {
|
||||
const double rounding = 0.5 * pow10(-PRECISION);
|
||||
price += (price > 0) ? rounding : -rounding;
|
||||
price *= pow10(PRECISION);
|
||||
return PriceSize((int64_t)price, -PRECISION, size);
|
||||
return PriceSize({(int64_t)price, -PRECISION}, size);
|
||||
}
|
||||
return PriceSize((int64_t)price, 0, size);
|
||||
return PriceSize({(int64_t)price, 0}, size);
|
||||
}
|
||||
|
||||
|
||||
@@ -147,51 +147,34 @@ typedef utils::Optimistic<PriceSize> AtomicPriceSize;
|
||||
// This structure can store the same data in 24 bytes so it's more efficient for shared memory
|
||||
struct PriceLevel {
|
||||
PriceLevel() :
|
||||
_mantissa_b(0), _exponent_b(0),
|
||||
_mantissa_a(0), _exponent_a(0),
|
||||
_size_b(0), _size_a(0) {}
|
||||
_bid(), _ask(),
|
||||
_bid_size(0), _ask_size(0) {}
|
||||
PriceLevel(const PriceSize& bid, const PriceSize& ask):
|
||||
_mantissa_b(bid.mantissa()), _exponent_b(bid.exponent()),
|
||||
_mantissa_a(ask.mantissa()), _exponent_a(ask.exponent()),
|
||||
_size_b(bid.size()), _size_a(ask.size())
|
||||
_bid(bid.int_price()), _ask(ask.int_price()),
|
||||
_bid_size(bid.size()), _ask_size(ask.size())
|
||||
{}
|
||||
|
||||
#ifdef _WIN32
|
||||
int64_t _mantissa_b : 56;
|
||||
int64_t _exponent_b : 8;
|
||||
int64_t _mantissa_a : 56;
|
||||
int64_t _exponent_a : 8;
|
||||
#else
|
||||
int64_t _mantissa_b : 56;
|
||||
int8_t _exponent_b : 8;
|
||||
int64_t _mantissa_a : 56;
|
||||
int8_t _exponent_a : 8;
|
||||
#endif
|
||||
uint32_t _size_b;
|
||||
uint32_t _size_a;
|
||||
IntPrice _bid;
|
||||
IntPrice _ask;
|
||||
uint32_t _bid_size;
|
||||
uint32_t _ask_size;
|
||||
|
||||
uint64_t bid_mantissa() const { return _mantissa_b; }
|
||||
uint64_t ask_mantissa() const { return _mantissa_a; }
|
||||
int8_t bid_exponent() const { return _exponent_b; }
|
||||
int8_t ask_exponent() const { return _exponent_a; }
|
||||
uint32_t bid_size() const { return _size_b; }
|
||||
uint32_t ask_size() const { return _size_a; }
|
||||
const IntPrice& bid_price() const { return _bid; }
|
||||
const IntPrice& ask_price() const { return _ask; }
|
||||
uint32_t bid_size() const { return _bid_size; }
|
||||
uint32_t ask_size() const { return _ask_size; }
|
||||
|
||||
bool operator==(const PriceLevel& other) const {
|
||||
return _size_a == other._size_a &&
|
||||
_size_b == other._size_b &&
|
||||
_mantissa_a == other._mantissa_a &&
|
||||
_mantissa_b == other._mantissa_b &&
|
||||
_exponent_a == other._exponent_a &&
|
||||
_exponent_b == other._exponent_b;
|
||||
return _ask_size == other._ask_size &&
|
||||
_bid_size == other._bid_size &&
|
||||
_bid == other._bid &&
|
||||
_ask == other._ask;
|
||||
}
|
||||
bool operator!=(const PriceLevel& other) const {
|
||||
return _size_a != other._size_a ||
|
||||
_size_b != other._size_b ||
|
||||
_mantissa_a != other._mantissa_a ||
|
||||
_mantissa_b != other._mantissa_b ||
|
||||
_exponent_a != other._exponent_a ||
|
||||
_exponent_b != other._exponent_b;
|
||||
return _ask_size != other._ask_size ||
|
||||
_bid_size != other._bid_size ||
|
||||
_bid != other._bid ||
|
||||
_ask != other._ask;
|
||||
}
|
||||
|
||||
bool update(const PriceLevel& temp) {
|
||||
@@ -203,10 +186,10 @@ struct PriceLevel {
|
||||
}
|
||||
|
||||
PriceSize bid() const {
|
||||
return PriceSize(_mantissa_b, _exponent_b, _size_b);
|
||||
return PriceSize(_bid, _bid_size);
|
||||
}
|
||||
PriceSize ask() const {
|
||||
return PriceSize(_mantissa_a, _exponent_a, _size_a);
|
||||
return PriceSize(_ask, _ask_size);
|
||||
}
|
||||
};
|
||||
static_assert(sizeof(PriceLevel) == 24, "PriceLevel should be small");
|
||||
@@ -215,20 +198,18 @@ struct ImpliedPriceLevel : private PriceLevel {
|
||||
ImpliedPriceLevel() : PriceLevel() {}
|
||||
ImpliedPriceLevel(const PriceSize& bid, const PriceSize& ask):
|
||||
PriceLevel(bid,ask) {}
|
||||
using PriceLevel::bid_mantissa;
|
||||
using PriceLevel::ask_mantissa;
|
||||
using PriceLevel::bid_exponent;
|
||||
using PriceLevel::ask_exponent;
|
||||
using PriceLevel::bid_price;
|
||||
using PriceLevel::ask_price;
|
||||
using PriceLevel::bid_size;
|
||||
using PriceLevel::ask_size;
|
||||
using PriceLevel::operator==;
|
||||
using PriceLevel::operator!=;
|
||||
bool operator=(const PriceLevel& other) { return update(other); }
|
||||
PriceSize bid() const {
|
||||
return PriceSize(_mantissa_b, _exponent_b, _size_b, 0);
|
||||
return PriceSize(_bid, _bid_size, 0);
|
||||
}
|
||||
PriceSize ask() const {
|
||||
return PriceSize(_mantissa_a, _exponent_a, _size_a, 0);
|
||||
return PriceSize(_ask, _ask_size, 0);
|
||||
}
|
||||
};
|
||||
static_assert(sizeof(ImpliedPriceLevel) == 24, "PriceLevel should be small");
|
||||
|
||||
@@ -76,9 +76,8 @@ class HotDepthEntry {
|
||||
*ask = direct[depth].ask();
|
||||
}
|
||||
void read_implied(uint32_t depth, PriceSize* bid, PriceSize* ask) const {
|
||||
const auto& d = implied[depth];
|
||||
*bid = PriceSize(d.bid_mantissa(), d.bid_exponent(), d.bid_size(), 0);
|
||||
*ask = PriceSize(d.ask_mantissa(), d.ask_exponent(), d.ask_size(), 0);
|
||||
*bid = implied[depth].bid();
|
||||
*ask = implied[depth].ask();
|
||||
}
|
||||
bool write_direct(uint32_t depth, const PriceSize& bid, const PriceSize& ask) {
|
||||
bts_assert_d(depth < direct.size());
|
||||
|
||||
@@ -32,11 +32,11 @@ namespace md {
|
||||
using namespace co2;
|
||||
|
||||
PriceSize convert(const PriceSizeMessage& psm) {
|
||||
return psm.size() > 0 ? PriceSize(psm.mantissa(), psm.exponent(), psm.size()) : NO_MARKET;
|
||||
return psm.size() > 0 ? PriceSize({psm.mantissa(), (int8_t)psm.exponent()}, psm.size()) : NO_MARKET;
|
||||
}
|
||||
PriceSize trade_pricesize(const co2::TradeMessage& trade) {
|
||||
return trade.size() > 0 ?
|
||||
PriceSize(trade.mantissa(), trade.exponent(), trade.size()) : NO_MARKET;
|
||||
PriceSize({trade.mantissa(), (int8_t)trade.exponent()}, trade.size()) : NO_MARKET;
|
||||
}
|
||||
PriceSize book_at(const google::protobuf::RepeatedPtrField<PriceSizeMessage>& side, int index) {
|
||||
if (side.size() > index) {
|
||||
|
||||
@@ -223,7 +223,7 @@ public:
|
||||
REQUIRE(p2.size() == 6);
|
||||
}
|
||||
|
||||
p = PriceSize(4, -1, 2);
|
||||
p = PriceSize({4, -1}, 2);
|
||||
memory = PriceLevel(p,p);
|
||||
for (auto p2 : { memory.bid(), memory.ask() }) {
|
||||
REQUIRE(p2.price() == 0.4);
|
||||
@@ -616,7 +616,7 @@ public:
|
||||
assign_ids(producers.begin(), producers.end());
|
||||
producer1->get_channel("FOO", SECURITYTYPE_FUTURE, VENUE_BTS).heartbeat();
|
||||
producer1->get_channel("FOOX", SECURITYTYPE_OPTION, VENUE_BTS).heartbeat();
|
||||
for (auto& pro : producers) { pro.update_bbo(TimeStamp::now(), PriceSize(1, 0, 1), PriceSize(2, 0, 2)); }
|
||||
for (auto& pro : producers) { pro.update_bbo(TimeStamp::now(), PriceSize({1, 0}, 1), PriceSize({2, 0}, 2)); }
|
||||
|
||||
// Symbol list for the consumer
|
||||
MDRecoverableConsumer consumer(get_test_symbols(), nullptr);
|
||||
@@ -678,7 +678,7 @@ public:
|
||||
|
||||
// set bbo to a different value
|
||||
for (auto& pro : producers) {
|
||||
pro.update_bbo(TimeStamp::now(), PriceSize(4, 0, 5), PriceSize(6, 0, 7));
|
||||
pro.update_bbo(TimeStamp::now(), PriceSize({4, 0}, 5), PriceSize({6, 0}, 7));
|
||||
}
|
||||
|
||||
// Attempt to recover the managed handles
|
||||
@@ -1673,21 +1673,21 @@ TEST_CASE("MergeImpliedTests", "[md]") {
|
||||
}
|
||||
|
||||
SECTION("partial_Read") {
|
||||
direct_bid[0] = { 700, 1, 1 };
|
||||
direct_bid[1] = { 600, 1, 1 };
|
||||
direct_bid[2] = { 500, 1, 1 };
|
||||
direct_bid[3] = { 400, 1, 1 };
|
||||
direct_bid[0] = { {700, 1}, 1 };
|
||||
direct_bid[1] = { {600, 1}, 1 };
|
||||
direct_bid[2] = { {500, 1}, 1 };
|
||||
direct_bid[3] = { {400, 1}, 1 };
|
||||
|
||||
direct_ask[0] = { 800, 1, 1 };
|
||||
direct_ask[1] = { 900, 1, 1 };
|
||||
direct_ask[2] = { 1000, 1, 1 };
|
||||
direct_ask[3] = { 1100, 1, 1 };
|
||||
direct_ask[0] = { {800, 1}, 1 };
|
||||
direct_ask[1] = { {900, 1}, 1 };
|
||||
direct_ask[2] = { {1000, 1}, 1 };
|
||||
direct_ask[3] = { {1100, 1}, 1 };
|
||||
|
||||
implied_bid[0] = { 700, 1, 2 };
|
||||
implied_bid[1] = { 600, 1, 2 };
|
||||
implied_bid[0] = { {700, 1}, 2 };
|
||||
implied_bid[1] = { {600, 1}, 2 };
|
||||
|
||||
implied_ask[0] = { 800, 1, 2 };
|
||||
implied_ask[1] = { 900, 1, 2 };
|
||||
implied_ask[0] = { {800, 1}, 2 };
|
||||
implied_ask[1] = { {900, 1}, 2 };
|
||||
|
||||
|
||||
ImpliedTests::test_merge(direct_bid,implied_bid,direct_ask,implied_ask, [](const vector<MDConsumerHandle>& consumers) {
|
||||
@@ -1742,21 +1742,21 @@ TEST_CASE("MergeImpliedTests", "[md]") {
|
||||
}
|
||||
|
||||
SECTION("better_implied") {
|
||||
direct_bid[0] = { 500, 1, 1 };
|
||||
direct_bid[1] = { 400, 1, 1 };
|
||||
direct_bid[2] = { 300, 1, 1 };
|
||||
direct_bid[3] = { 200, 1, 1 };
|
||||
direct_bid[0] = { {500, 1}, 1 };
|
||||
direct_bid[1] = { {400, 1}, 1 };
|
||||
direct_bid[2] = { {300, 1}, 1 };
|
||||
direct_bid[3] = { {200, 1}, 1 };
|
||||
|
||||
direct_ask[0] = { 1000, 1, 1 };
|
||||
direct_ask[1] = { 1100, 1, 1 };
|
||||
direct_ask[2] = { 1200, 1, 1 };
|
||||
direct_ask[3] = { 1300, 1, 1 };
|
||||
direct_ask[0] = { {1000, 1}, 1 };
|
||||
direct_ask[1] = { {1100, 1}, 1 };
|
||||
direct_ask[2] = { {1200, 1}, 1 };
|
||||
direct_ask[3] = { {1300, 1}, 1 };
|
||||
|
||||
implied_bid[0] = { 600, 1, 2 };
|
||||
implied_bid[1] = { 500, 1, 2 };
|
||||
implied_bid[0] = { {600, 1}, 2 };
|
||||
implied_bid[1] = { {500, 1}, 2 };
|
||||
|
||||
implied_ask[0] = { 900, 1, 2 };
|
||||
implied_ask[1] = { 1000, 1, 2 };
|
||||
implied_ask[0] = { {900, 1}, 2 };
|
||||
implied_ask[1] = { {1000, 1}, 2 };
|
||||
|
||||
|
||||
ImpliedTests::test_merge(direct_bid,implied_bid,direct_ask,implied_ask, [=](const vector<MDConsumerHandle>& consumers) {
|
||||
@@ -1811,21 +1811,21 @@ TEST_CASE("MergeImpliedTests", "[md]") {
|
||||
}
|
||||
|
||||
SECTION("worse_implied") {
|
||||
direct_bid[0] = { 500, 1, 1 };
|
||||
direct_bid[1] = { 400, 1, 1 };
|
||||
direct_bid[2] = { 300, 1, 1 };
|
||||
direct_bid[3] = { 200, 1, 1 };
|
||||
direct_bid[0] = { {500, 1}, 1 };
|
||||
direct_bid[1] = { {400, 1}, 1 };
|
||||
direct_bid[2] = { {300, 1}, 1 };
|
||||
direct_bid[3] = { {200, 1}, 1 };
|
||||
|
||||
direct_ask[0] = { 1000, 1, 1 };
|
||||
direct_ask[1] = { 1100, 1, 1 };
|
||||
direct_ask[2] = { 1200, 1, 1 };
|
||||
direct_ask[3] = { 1300, 1, 1 };
|
||||
direct_ask[0] = { {1000, 1}, 1 };
|
||||
direct_ask[1] = { {1100, 1}, 1 };
|
||||
direct_ask[2] = { {1200, 1}, 1 };
|
||||
direct_ask[3] = { {1300, 1}, 1 };
|
||||
|
||||
implied_bid[0] = { 400, 1, 2 };
|
||||
implied_bid[1] = { 300, 1, 2 };
|
||||
implied_bid[0] = { {400, 1}, 2 };
|
||||
implied_bid[1] = { {300, 1}, 2 };
|
||||
|
||||
implied_ask[0] = { 1100, 1, 2 };
|
||||
implied_ask[1] = { 1200, 1, 2 };
|
||||
implied_ask[0] = { {1100, 1}, 2 };
|
||||
implied_ask[1] = { {1200, 1}, 2 };
|
||||
|
||||
|
||||
ImpliedTests::test_merge(direct_bid,implied_bid,direct_ask,implied_ask, [=](const vector<MDConsumerHandle>& consumers) {
|
||||
@@ -1898,21 +1898,21 @@ TEST_CASE("MergeImpliedTests", "[md]") {
|
||||
}
|
||||
|
||||
SECTION("worse_implied_bid") {
|
||||
direct_bid[0] = { 500, 1, 1 };
|
||||
direct_bid[1] = { 400, 1, 1 };
|
||||
direct_bid[2] = { 300, 1, 1 };
|
||||
direct_bid[3] = { 200, 1, 1 };
|
||||
direct_bid[0] = { {500, 1}, 1 };
|
||||
direct_bid[1] = { {400, 1}, 1 };
|
||||
direct_bid[2] = { {300, 1}, 1 };
|
||||
direct_bid[3] = { {200, 1}, 1 };
|
||||
|
||||
direct_ask[0] = { 1000, 1, 1 };
|
||||
direct_ask[1] = { 1100, 1, 1 };
|
||||
direct_ask[2] = { 1200, 1, 1 };
|
||||
direct_ask[3] = { 1300, 1, 1 };
|
||||
direct_ask[0] = { {1000, 1}, 1 };
|
||||
direct_ask[1] = { {1100, 1}, 1 };
|
||||
direct_ask[2] = { {1200, 1}, 1 };
|
||||
direct_ask[3] = { {1300, 1}, 1 };
|
||||
|
||||
implied_bid[0] = { 400, 1, 2 };
|
||||
implied_bid[1] = { 300, 1, 2 };
|
||||
implied_bid[0] = { {400, 1}, 2 };
|
||||
implied_bid[1] = { {300, 1}, 2 };
|
||||
|
||||
implied_ask[0] = { 1000, 1, 2 };
|
||||
implied_ask[1] = { 1100, 1, 2 };
|
||||
implied_ask[0] = { {1000, 1}, 2 };
|
||||
implied_ask[1] = { {1100, 1}, 2 };
|
||||
|
||||
|
||||
ImpliedTests::test_merge(direct_bid,implied_bid,direct_ask,implied_ask, [=](const vector<MDConsumerHandle>& consumers) {
|
||||
@@ -1984,21 +1984,21 @@ TEST_CASE("MergeImpliedTests", "[md]") {
|
||||
});
|
||||
}
|
||||
SECTION("worse_implied_ask") {
|
||||
direct_bid[0] = { 500, 1, 1 };
|
||||
direct_bid[1] = { 400, 1, 1 };
|
||||
direct_bid[2] = { 300, 1, 1 };
|
||||
direct_bid[3] = { 200, 1, 1 };
|
||||
direct_bid[0] = { {500, 1}, 1 };
|
||||
direct_bid[1] = { {400, 1}, 1 };
|
||||
direct_bid[2] = { {300, 1}, 1 };
|
||||
direct_bid[3] = { {200, 1}, 1 };
|
||||
|
||||
direct_ask[0] = { 1000, 1, 1 };
|
||||
direct_ask[1] = { 1100, 1, 1 };
|
||||
direct_ask[2] = { 1200, 1, 1 };
|
||||
direct_ask[3] = { 1300, 1, 1 };
|
||||
direct_ask[0] = { {1000, 1}, 1 };
|
||||
direct_ask[1] = { {1100, 1}, 1 };
|
||||
direct_ask[2] = { {1200, 1}, 1 };
|
||||
direct_ask[3] = { {1300, 1}, 1 };
|
||||
|
||||
implied_bid[0] = { 500, 1, 2 };
|
||||
implied_bid[1] = { 400, 1, 2 };
|
||||
implied_bid[0] = { {500, 1}, 2 };
|
||||
implied_bid[1] = { {400, 1}, 2 };
|
||||
|
||||
implied_ask[0] = { 1300, 1, 2 };
|
||||
implied_ask[1] = { 1400, 1, 2 };
|
||||
implied_ask[0] = { {1300, 1}, 2 };
|
||||
implied_ask[1] = { {1400, 1}, 2 };
|
||||
|
||||
|
||||
ImpliedTests::test_merge(direct_bid,implied_bid,direct_ask,implied_ask, [=](const vector<MDConsumerHandle>& consumers) {
|
||||
|
||||
@@ -70,11 +70,11 @@ struct MDPlayer : md::IRelayWriter
|
||||
|
||||
size_t i = 0;
|
||||
for (auto &b : book.bids())
|
||||
bids[i++] = md::PriceSize(b.mantissa(),b.exponent(),b.size());
|
||||
bids[i++] = md::PriceSize({b.mantissa(),(int8_t)b.exponent()},b.size());
|
||||
|
||||
size_t n = 0;
|
||||
for (auto &a : book.asks())
|
||||
asks[n++] = md::PriceSize(a.mantissa(),a.exponent(),a.size());
|
||||
asks[n++] = md::PriceSize({a.mantissa(),(int8_t)a.exponent()},a.size());
|
||||
obs.on_depth(*sec_ptr, bids, asks);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ namespace bts {
|
||||
namespace md {
|
||||
|
||||
inline PriceSize pricesize_of_price_size(const onixs::Decimal price, int8_t display_factor, Size size) {
|
||||
PriceSize result(price.mantissa(), price.exponent() + display_factor, size);
|
||||
PriceSize result({price.mantissa(), (int8_t)(price.exponent() + display_factor)}, size);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user