From cbc142ab15eab9dfe1986f8b721c2cf196830b03 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Sun, 16 Mar 2025 06:11:31 +0000 Subject: [PATCH] fix: Account for rounding error in IcebergAlgo fills Bugs (fixed): * Iceberg orders w/ fractional quantities can get into a state where they send an infinite stream of orders w/ 0 qty Note: I also changed the number of decimal places used when rounding order quantities from 6 to 7 (the level of precision guarenteed by IEEE 754). --- src/lib/domain_model/common.cpp | 34 +++++++++++++++++++ src/lib/domain_model/common.h | 11 +++--- .../orders/algo/algo_orders/IcebergAlgo.cpp | 20 ++++++++--- 3 files changed, 54 insertions(+), 11 deletions(-) create mode 100644 src/lib/domain_model/common.cpp diff --git a/src/lib/domain_model/common.cpp b/src/lib/domain_model/common.cpp new file mode 100644 index 000000000..2ec0ef238 --- /dev/null +++ b/src/lib/domain_model/common.cpp @@ -0,0 +1,34 @@ +#include "common.h" +#include "lib/utils/bts_assert.h" + +namespace bts { + +double CQuantity::round_to_decimal_places(double value, int decimal_places) { + if (std::isnan(value) || std::isinf(value)) { + return value; + } + const bool negate = value < 0; + if (negate) { + value = -value; + } + // Prevent arithmetic overflow (it could have nasty, unpredictable side effects). + bts_assert( + value < static_cast(std::numeric_limits::max())); + const uint32_t int_part = static_cast(value); + double frac_part = value - int_part; + if (frac_part > (1.0 - err)) { + value = int_part + 1; + } else if (frac_part < err) { + value = int_part; + } else { + const double factor = pow(10, decimal_places); + frac_part = std::round(frac_part * factor) / factor; + value = static_cast(int_part) + frac_part; + } + if (negate) { + value = -value; + } + return value; +} + +} // namespace bts diff --git a/src/lib/domain_model/common.h b/src/lib/domain_model/common.h index 512e460d1..9aa2dead5 100644 --- a/src/lib/domain_model/common.h +++ b/src/lib/domain_model/common.h @@ -14,8 +14,8 @@ namespace bts { struct CQuantity { double qty = 0; - constexpr static double precision_digits = 6; - constexpr static double err = 0.0000001; + constexpr static int precision_digits = 7; + constexpr static double err = 0.000001; CQuantity() {} CQuantity(uint32_t whole_qty) : qty(whole_qty) {} CQuantity(uint32_t int_qty, double fractional_qty) @@ -26,10 +26,7 @@ struct CQuantity { return rv; } - static double round_to_decimal_places(double value, int decimal_places) { - double factor = pow(10, decimal_places); - return std::round(value * factor) / factor; - } + static double round_to_decimal_places(double value, int decimal_places); uint32_t value() const { return static_cast(qty); } double full_value() const { return qty; } float full_value_float() const { return static_cast(full_value()); } @@ -102,7 +99,7 @@ struct CQuantity { } bool is_zero() const { return *this == create_new(0.0); } void make_zero_if_negative() { qty = std::max(0.0, qty); } - void round() { qty = round_to_decimal_places(qty, 6); } + void round() { qty = round_to_decimal_places(qty, precision_digits); } }; inline std::ostream &operator<<(std::ostream &out, CQuantity const &qty) { diff --git a/src/lib/orderman/orders/algo/algo_orders/IcebergAlgo.cpp b/src/lib/orderman/orders/algo/algo_orders/IcebergAlgo.cpp index 6bf60f9d5..6d9cf143f 100644 --- a/src/lib/orderman/orders/algo/algo_orders/IcebergAlgo.cpp +++ b/src/lib/orderman/orders/algo/algo_orders/IcebergAlgo.cpp @@ -1,4 +1,6 @@ #include "lib/orderman/orders/algo/algo_orders/IcebergAlgo.h" +#include "lib/domain_model/common.h" +#include "lib/orderman/order_desc.h" #include "lib/orderman/orders/algo/IAlgoServices.h" #include "lib/domain_model/Security.h" @@ -95,7 +97,7 @@ void IcebergAlgo::do_update(api::orderman::Update const &update) { ? update.fractional_quantity().fraction() : 0; const Quantity new_qty{update.quantity(), frac_qty}; - if (new_qty < algo().total_fills()) { + if (new_qty <= algo().total_fills()) { algo().complete(); return; } @@ -136,10 +138,20 @@ void IcebergAlgo::do_update(api::orderman::Update const &update) { void IcebergAlgo::on_child_change(OrderKey k, ChildState const &st) { if (k == me->child_key) { - algo().update_fill_state( - algo().total_fills() + st.last_fills, st.last_price); + Quantity last_fills = st.last_fills; + last_fills.round(); + Quantity total_fills = algo().total_fills(); + total_fills.round(); + total_fills += last_fills; + total_fills.round(); + Quantity qty = algo().quantity(); + qty.round(); + LOG(OrderMan, Info, + "IcebergAlgo::on_child_change. {} {} cumm_filled={}/{}", k, st, + total_fills, qty); + algo().update_fill_state(total_fills, st.last_price); - if (algo().total_fills() >= algo().quantity()) { + if (total_fills >= qty) { algo().complete(); return; }