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.
This commit is contained in:
1 parent
b1a1f83ea8
commit
be68e006bf
3 files changed
+62
-7
No files matched your search
@@ -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.
|
||||
/// </summary>
|
||||
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;
|
||||
|
||||
@@ -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<T>(this TickFactors tick, T x, T y)
|
||||
where T : IFloatingPoint<T> {
|
||||
var x_units = MathUtils.round_fractional_units(x, tick.tick_unit_frac<T>());
|
||||
var y_units = MathUtils.round_fractional_units(x, tick.tick_unit_frac<T>());
|
||||
return NumericCastConverter<T, int>.convert(x_units) ==
|
||||
NumericCastConverter<T, int>.convert(y_units);
|
||||
}
|
||||
|
||||
public static IEqualityComparer<T> tick_equality_comparer<T>(this TickFactors tick)
|
||||
where T : IFloatingPoint<T> =>
|
||||
EqualityComparer.Create<T>(
|
||||
(a, b) => tick_equals(tick, a, b),
|
||||
x => NumericCastConverter<T, int>.convert(MathUtils.round_fractional_units(x, tick.tick_unit_frac<T>())));
|
||||
|
||||
public static IEqualityComparer<double> tick_equality_comparer(this TickFactors tick) =>
|
||||
EqualityComparer.Create<double>(
|
||||
(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);
|
||||
|
||||
|
||||
@@ -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<T>(
|
||||
this ContractSpecificationImmutable contract, T x, T y)
|
||||
where T : IFloatingPoint<T> {
|
||||
var xd = NumericCastConverter<T, double>.convert(x);
|
||||
var yd = NumericCastConverter<T, double>.convert(y);
|
||||
return (int)MathUtils.round_fractional_units(xd, contract.LowTick) ==
|
||||
(int)MathUtils.round_fractional_units(yd, contract.LowTick);
|
||||
}
|
||||
|
||||
public static IEqualityComparer<double> tick_equality_comparer(this ContractSpecificationImmutable contract) =>
|
||||
public static IEqualityComparer<double> tick_equality_comparer(
|
||||
this ContractSpecificationImmutable contract) =>
|
||||
EqualityComparer.Create<double>(
|
||||
(a, b) => tick_equals(contract, a, b),
|
||||
x => (int)MathUtils.round_fractional_units(x, contract.LowTick));
|
||||
|
||||
public static IEqualityComparer<T> tick_equality_comparer<T>(
|
||||
this ContractSpecificationImmutable contract) where T : IFloatingPoint<T> =>
|
||||
EqualityComparer.Create<T>(
|
||||
(a, b) => tick_equals(contract, a, b),
|
||||
x => (int)MathUtils.round_fractional_units(
|
||||
NumericCastConverter<T, double>.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);
|
||||
|
||||
|
||||
Reference in new issue
Block a user