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).
This commit is contained in:
1 parent
3980f2d030
commit
cbc142ab15
3 files changed
+54
-11
No files matched your search
@@ -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<double>(std::numeric_limits<uint32_t>::max()));
|
||||
const uint32_t int_part = static_cast<uint32_t>(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<double>(int_part) + frac_part;
|
||||
}
|
||||
if (negate) {
|
||||
value = -value;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
} // namespace bts
|
||||
@@ -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<uint32_t>(qty); }
|
||||
double full_value() const { return qty; }
|
||||
float full_value_float() const { return static_cast<float>(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) {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user