From be68e006bf530246bc69f93b7cb1c5ed0ecbe8e6 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Mon, 18 Jul 2022 13:06:15 -0500 Subject: [PATCH] fix (rec rept): Use tick-factors to avoid rounding errors Bugs (fixed): * Reconcile Report can show false positive price mismatch Cause: The epsilon const (1E-12) used in the price comparison too small. Evidently, the Deribit-position prices provided to the Reconcile Report are only accurate up to 9 decimal places. --- .../ReconcileReport/ReconcileReportRowData.cs | 20 +++++++++++--- .../spark_core/data/ticks/TickExtensions.cs | 26 +++++++++++++++++++ src/client/spark_core/data/ticks/TickUtils.cs | 23 +++++++++++++--- 3 files changed, 62 insertions(+), 7 deletions(-) diff --git a/src/client/spark/Modules/Trading/ReconcileReport/ReconcileReportRowData.cs b/src/client/spark/Modules/Trading/ReconcileReport/ReconcileReportRowData.cs index 647f64ea6..1a5d055c4 100644 --- a/src/client/spark/Modules/Trading/ReconcileReport/ReconcileReportRowData.cs +++ b/src/client/spark/Modules/Trading/ReconcileReport/ReconcileReportRowData.cs @@ -2,10 +2,13 @@ namespace Spark.Trading { using bts; using bts.api; + using bts.api.domain; using bts.api.orderman; using bts.api.position; using bts.utils; + using mbts; + using mbts.spark; using mbts.utils; using Spark.Api.Position; @@ -139,17 +142,26 @@ namespace Spark.Trading { /// positions are undefined (i.e., the quantity is finite positive), then the position is /// considered a proper match, and both the 'price' and 'quantity' flags are returned. /// - public static ReconcileReportMatchType match_type(this ReconcileReportRowData rd) { + public static ReconcileReportMatchType match_type( + this ReconcileReportRowData rd, TickFactorsImmutable? qty_tick_factors = null) { if (!rd.has_exch_position() && !rd.has_sys_position()) { return ReconcileReportMatchType.Price | ReconcileReportMatchType.Quantity; } + // allow for rounding errors var mt = ReconcileReportMatchType.None; - // allow for rounding error - if (rd.sys_quantity().epsilon_equals(rd.exch_quantity())) { + + // qty + var qty_comp = qty_tick_factors?.to_tick_factors()?.tick_equality_comparer() + ?? MathUtils.epsilon_eq_comparer; + if (qty_comp.Equals(rd.sys_quantity(), rd.exch_quantity())) { mt |= ReconcileReportMatchType.Quantity; } - if (rd.sys_price().epsilon_equals(rd.exch_price())) { + + // price + var price_comp = (rd.security?.ContractSpec)?.tick_equality_comparer() + ?? MathUtils.epsilon_eq_comparer; + if (price_comp.Equals(rd.sys_price(), rd.exch_price())) { mt |= ReconcileReportMatchType.Price; } return mt; diff --git a/src/client/spark_core/data/ticks/TickExtensions.cs b/src/client/spark_core/data/ticks/TickExtensions.cs index 3d827d0cf..62ff927be 100644 --- a/src/client/spark_core/data/ticks/TickExtensions.cs +++ b/src/client/spark_core/data/ticks/TickExtensions.cs @@ -9,6 +9,7 @@ namespace mbts { using Spark.Core; using System; + using System.Collections.Generic; using System.Globalization; public static class TickExtensions { @@ -74,6 +75,31 @@ namespace mbts { public static double reduced_base(this TickFactors tick) => MathUtils.factor_out(tick.tick_base, 10); + public static bool tick_equals(this TickFactors tick, double x, double y) { + var x_units = MathUtils.round_fractional_units(x, tick.tick_unit_frac()); + var y_units = MathUtils.round_fractional_units(x, tick.tick_unit_frac()); + return x_units == y_units; + } + + public static bool tick_equals(this TickFactors tick, T x, T y) + where T : IFloatingPoint { + var x_units = MathUtils.round_fractional_units(x, tick.tick_unit_frac()); + var y_units = MathUtils.round_fractional_units(x, tick.tick_unit_frac()); + return NumericCastConverter.convert(x_units) == + NumericCastConverter.convert(y_units); + } + + public static IEqualityComparer tick_equality_comparer(this TickFactors tick) + where T : IFloatingPoint => + EqualityComparer.Create( + (a, b) => tick_equals(tick, a, b), + x => NumericCastConverter.convert(MathUtils.round_fractional_units(x, tick.tick_unit_frac()))); + + public static IEqualityComparer tick_equality_comparer(this TickFactors tick) => + EqualityComparer.Create( + (a, b) => tick_equals(tick, a, b), + x => (int)MathUtils.round_fractional_units(x, tick.tick_unit_frac())); + public static string format_without_decimal_part(this TickFactors tick, double value) => format(tick, value, double.NaN, false, _nan_text, true); diff --git a/src/client/spark_core/data/ticks/TickUtils.cs b/src/client/spark_core/data/ticks/TickUtils.cs index 8227d1d71..04d189b49 100644 --- a/src/client/spark_core/data/ticks/TickUtils.cs +++ b/src/client/spark_core/data/ticks/TickUtils.cs @@ -3,6 +3,9 @@ using bts.data.ticks; using bts.utils; using mbts.co2; using mbts.user.settings; + +using Spark.Core; + using System; using System.Collections.Generic; using System.Linq; @@ -178,14 +181,28 @@ namespace mbts.spark { else return _get(spec).tickdown(value, (uint)count); } - public static bool tick_equals(this ContractSpecificationImmutable contract, double x, double y) => - (int)MathUtils.round_fractional_units(x, contract.LowTick) == (int)MathUtils.round_fractional_units(y, contract.LowTick); + public static bool tick_equals( + this ContractSpecificationImmutable contract, T x, T y) + where T : IFloatingPoint { + var xd = NumericCastConverter.convert(x); + var yd = NumericCastConverter.convert(y); + return (int)MathUtils.round_fractional_units(xd, contract.LowTick) == + (int)MathUtils.round_fractional_units(yd, contract.LowTick); + } - public static IEqualityComparer tick_equality_comparer(this ContractSpecificationImmutable contract) => + public static IEqualityComparer tick_equality_comparer( + this ContractSpecificationImmutable contract) => EqualityComparer.Create( (a, b) => tick_equals(contract, a, b), x => (int)MathUtils.round_fractional_units(x, contract.LowTick)); + public static IEqualityComparer tick_equality_comparer( + this ContractSpecificationImmutable contract) where T : IFloatingPoint => + EqualityComparer.Create( + (a, b) => tick_equals(contract, a, b), + x => (int)MathUtils.round_fractional_units( + NumericCastConverter.convert(x), contract.LowTick)); + public static int compare_tick_fallback_to_double(this ContractSpecificationImmutable contract, double x, double y) => tick_equals(contract, x, y) ? 0 : x.CompareTo(y);