diff --git a/src/lib/md/IntPrice.h b/src/lib/md/IntPrice.h index 73689cb45..3c35a6a11 100644 --- a/src/lib/md/IntPrice.h +++ b/src/lib/md/IntPrice.h @@ -87,6 +87,9 @@ class IntPrice { _exponent = temp._exponent; return *this; } + IntPrice operator -() { + return IntPrice(-_mantissa, _exponent); + } IntPrice operator * (const IntPrice& other) const { return op(other, [](int64_t m1, int64_t m2) { return m1 * m2; }); } diff --git a/src/server/sparkd/cmds/spartan/md/mdp3/utils/book_utils.cpp b/src/server/sparkd/cmds/spartan/md/mdp3/utils/book_utils.cpp index 1e73e0f9b..fe5900f17 100644 --- a/src/server/sparkd/cmds/spartan/md/mdp3/utils/book_utils.cpp +++ b/src/server/sparkd/cmds/spartan/md/mdp3/utils/book_utils.cpp @@ -116,11 +116,16 @@ void compute_synthetic_book( legs[i].curr_level_qty_used += MDP3Quantity(limiting_factor); auto marginal_cost = md::INT_NAN; for (auto& l : legs) { + // The book's prices have already been multiplied by abs(leg_qty); + // Subtract the leg's cost if qty is negative, otherwise add it + // to the synthetic's marginal cost. + auto lcost = l.curr_price(); + if (l.leg_qty.value() < 0) + lcost = -lcost; if (marginal_cost.is_nan()) - marginal_cost = l.curr_price() * l.leg_qty.value(); + marginal_cost = lcost; else - marginal_cost = - marginal_cost + l.curr_price() * l.leg_qty.value(); + marginal_cost += lcost; } dest.emplace_back(marginal_cost, MDP3Quantity(limiting_factor)); }