SPARK-954 Update synthetic book merge to floating point sizes

This commit is contained in:
Eric Harding committed 2018-11-23 11:29:35 -05:00
1 parent 61a112c221
commit ca2a6a1e60
3 files changed
+56 -30

No files matched your search

+1 -1
View File
@@ -209,7 +209,7 @@
"type": "cppdbg",
"request": "launch",
"program": "/home/bts/spark/build/ninja_debug/src/server/sparkd/sparkd",
"args": ["test", "SPARK-955"],
"args": ["test", "SyntheticBook+1-2"],
"stopAtEntry": false,
"cwd": "/home/bts/spark/build/ninja_debug/src/server/sparkd/",
"environment": [],
+52 -28
View File
@@ -85,7 +85,7 @@ TimeStamp copy_depth(
/////////////////////////////
struct Level {
double price;
uint16_t size;
double size;
};
struct DepthTraversal {
std::array<Level, COCONUT_MAX_DEPTH> levels;
@@ -100,28 +100,38 @@ struct DepthTraversal {
levels[x].size = depth[x].size();
}
}
double try_take1() {
if (head >= levels.size()) return os::nan();
if (levels[head].size >= qty) {
levels[head].size -= qty;
return levels[head].price * price_mult;
double available_size() const {
double available = 0.0;
auto cur = head;
bool partial = false;
while(cur < levels.size() && available < 1) {
available += levels[cur].size / qty;
if (available < 1.0) partial = true;
++cur;
}
else {
double price = 0;
size_t count = 0;
while(count < qty) {
if (levels[head].size >= 1) {
price += levels[head].price;
levels[head].size -= 1;
count += 1;
}
else {
head++;
if (head >= levels.size()) return os::nan();
}
if (partial && available >= 1.0) return 1.0;
return available;
}
double consume(double size) {
bts_assert_d(size <= available_size());
double take = size * qty;
double price = 0.0;
while(take > 0) {
if (levels[head].size >= take) {
price += levels[head].price * price_mult * (take/qty);
levels[head].size -= take;
if (levels[head].size <= 0) ++head;
break;
}
else {
double available = levels[head].size;
price += levels[head].price * price_mult * (available/qty);
levels[head].size = 0;
++head;
take -= available;
}
return (price / qty) * price_mult;
}
return price/size;
}
};
@@ -129,13 +139,20 @@ void copy_depth(size_t count, api::Side side, const api::ContractSpecification&
size_t out_index = 0;
bool first = true;
while(out_index < count) {
double price = 0.0;
for (auto& dt : leg_travs) {
double temp = dt.try_take1();
price += temp;
}
if (os::is_nan(price)) {
double price = 0.0;
// Find the leg with the minimum available size
double size = std::numeric_limits<double>().max();
for (const auto& dt : leg_travs) {
auto sz = dt.available_size();
// If we can't get 1.0 from any leg we're done
if (sz <= size) size = sz;
if (size < 1.0) break;
}
size = (int64_t)size;
if (size < 1.0) {
// We've run out of book so we nan out any remaining levels and finish
size_t start = first ? out_index : out_index+1;
for (size_t x=start; x < count; ++x) {
@@ -145,6 +162,13 @@ void copy_depth(size_t count, api::Side side, const api::ContractSpecification&
return;
}
// Take that size from each leg
for (auto& dt : leg_travs) {
double temp = dt.consume(size);
price += temp;
}
bts_assert_d(!os::is_nan(price));
// Round to tick size
price = securities::checked_round_to_quote_price(spec, price, side);
@@ -154,13 +178,13 @@ void copy_depth(size_t count, api::Side side, const api::ContractSpecification&
}
if (price == out_price[out_index].price) {
out_price[out_index].size += 1;
out_price[out_index].size += size;
}
else {
out_index += 1;
if (out_index < count) {
out_price[out_index].price = price;
out_price[out_index].size = 1;
out_price[out_index].size = size;
}
}
}
@@ -263,6 +263,7 @@ TEST_CASE("SPARK-955", "[md][synthetic]") {
expect_market(synthetic_handle,
// If we do strict price this is right. If we're pessimistic we get {-4.25,2}
{ {-4.00, 1}, {-4.25, 1}, { -5.00, 3}, {-5.25, 3}, {-5.50,1}, {-5.75, 6} },
// { {-4.25, 2}, { -5.00, 3}, {-5.25, 3}, {-5.50,1}, {-5.75, 6} },
{ {5.00, 3}, {5.25, 1}, {6.00,3}, {6.50,6} });
}
@@ -271,7 +272,7 @@ TEST_CASE("SPARK-954", "[md][synthetic]") {
vector<Co2TestInstrument> test_instr;
SECTION("price multiplier = 0.9,1.1") {
tie(synthetic, test_instr) = make_securities({ {0.9, 0.025, api::SIDE_BUY}, {1.1, 0.025, api::SIDE_SELL } });
tie(synthetic, test_instr) = make_securities({ {0.9, 0.25, api::SIDE_BUY}, {1.1, 0.25, api::SIDE_SELL } });
auto leg1 = test_instr[0];
auto leg2 = test_instr[1];
MDConsumerHandle synthetic_handle(synthetic);
@@ -288,6 +289,7 @@ TEST_CASE("SPARK-954", "[md][synthetic]") {
}
}
TEST_CASE("Synthetic_net_change", "[md][synthetic]") {
shared_ptr<SyntheticStrategy> synthetic;
vector<Co2TestInstrument> test_instr;