diff --git a/src/client/spark/Modules/Trading/Ladder/View/LadderOrdersHost.cs b/src/client/spark/Modules/Trading/Ladder/View/LadderOrdersHost.cs index c185e2825..ab64d5704 100644 --- a/src/client/spark/Modules/Trading/Ladder/View/LadderOrdersHost.cs +++ b/src/client/spark/Modules/Trading/Ladder/View/LadderOrdersHost.cs @@ -194,7 +194,7 @@ namespace mbts.spark { void _position_panel(PriceCellComponents price_cell, double price) { int index = _parent._row_for_price(price); var pos_comp = - EqualityComparer.Create((x, y) => MathUtils.marginal_equals(x, y, DoubleUtil.MIN_SCROLL_DELTA)); + EqualityComparer.Create((x, y) => MathUtils.marginal_equals(x, y, WPFHelper.MIN_SCROLL_DELTA)); WPFHelper.set_if_changed(price_cell.panel, LeftProperty, 0d); WPFHelper.set_if_changed(price_cell.panel, TopProperty, index * _row_height, pos_comp); } diff --git a/src/client/spark/Modules/Trading/PNL/Data/PNLData.cs b/src/client/spark/Modules/Trading/PNL/Data/PNLData.cs index 47b6285af..4a86e639e 100644 --- a/src/client/spark/Modules/Trading/PNL/Data/PNLData.cs +++ b/src/client/spark/Modules/Trading/PNL/Data/PNLData.cs @@ -384,10 +384,10 @@ namespace bts.shell.pnl { } /// Sod public double sod_pnl => effective_cpp * - DoubleUtil.nan_to_zero_sum( + new[] { sod_long_avg_price_delta_raw * sod_long_pos, sod_short_avg_price_delta_raw * sod_short_pos - ); + }.sum_real(); // long public double sod_long_pos => sod.long_pos; public double sod_long_avg_price => effective_cpp * sod.long_avg; @@ -414,10 +414,10 @@ namespace bts.shell.pnl { /// Day public double day_pnl => effective_cpp * - DoubleUtil.nan_to_zero_sum( + new[] { day_long_avg_price_delta_raw * day_long_pos, day_short_avg_price_delta_raw * day_short_pos - ); + }.sum_real(); // long public double day_long_pos => day.long_pos; public double day_long_avg_price => effective_cpp * day.long_avg; @@ -453,27 +453,27 @@ namespace bts.shell.pnl { public double display_raw_day_short_pos => qty_conversion.to_display(raw_day_short_pos); /// Total - public double total_pnl => DoubleUtil.nan_to_zero_sum(sod_pnl, day_pnl); + public double total_pnl => new[] { sod_pnl, day_pnl }.sum_real(); // long - public double total_long_pos => DoubleUtil.nan_to_zero_sum(sod.long_pos, day.long_pos); + public double total_long_pos => new[] { sod.long_pos, day.long_pos }.sum_real(); public double total_long_avg_price => effective_cpp * total_long_avg_price_raw; - public double total_long_avg_price_raw => DoubleUtil.nan_to_zero_sum(sod.long_avg, day.long_avg); + public double total_long_avg_price_raw => new[] { sod.long_avg, day.long_avg }.sum_real(); public double total_long_avg_price_delta => effective_cpp * total_long_avg_price_delta_raw; public double total_long_avg_price_delta_raw => MathUtils.weighted_average( sod_long_avg_price_delta_raw, sod.long_pos, day_long_avg_price_delta_raw, day.long_pos); // short - public double total_short_pos => DoubleUtil.nan_to_zero_sum(sod.short_pos, day.short_pos); + public double total_short_pos => new[] { sod.short_pos, day.short_pos }.sum_real(); public double total_short_avg_price => effective_cpp * total_short_avg_price_raw; - public double total_short_avg_price_raw => DoubleUtil.nan_to_zero_sum(sod.short_avg, day.short_avg); + public double total_short_avg_price_raw => new[] { sod.short_avg, day.short_avg }.sum_real(); public double total_short_avg_price_delta => effective_cpp * total_short_avg_price_delta_raw; public double total_short_avg_price_delta_raw => MathUtils.weighted_average( sod_short_avg_price_delta_raw, sod.short_pos, day_short_avg_price_delta_raw, day.short_pos); // net - public double total_net_pos => DoubleUtil.nan_to_zero_sum(sod.long_pos, -sod.short_pos, day.long_pos, -day.short_pos); + public double total_net_pos => new[] { sod.long_pos, -sod.short_pos, day.long_pos, -day.short_pos }.Where(n => n.is_real()).Sum(); public double total_net_avg_price_delta => effective_cpp * total_net_avg_price_delta_raw; public double total_net_avg_price_delta_raw => MathUtils.weighted_average( diff --git a/src/client/spark/Modules/Trading/PNL/VM/PNLRowVM.cs b/src/client/spark/Modules/Trading/PNL/VM/PNLRowVM.cs index bfe446d31..af4fb386e 100644 --- a/src/client/spark/Modules/Trading/PNL/VM/PNLRowVM.cs +++ b/src/client/spark/Modules/Trading/PNL/VM/PNLRowVM.cs @@ -107,31 +107,31 @@ namespace bts.shell.pnl { if (security.HasValue && security.Value.ContractCase != Security.ContractOneofCase.Future) { day_long = day_short = sod_long = sod_short = 0; } else { - day_long = DoubleUtil.round_to_decimal(data.day.long_pos); - day_short = DoubleUtil.round_to_decimal(data.day.short_pos); - sod_long = DoubleUtil.round_to_decimal(data.sod.long_pos); - sod_short = DoubleUtil.round_to_decimal(data.sod.short_pos); + day_long = data.day.long_pos.round_to_decimal(); + day_short = data.day.short_pos.round_to_decimal(); + sod_long = data.sod.long_pos.round_to_decimal(); + sod_short = data.sod.short_pos.round_to_decimal(); } var nd = new PNLNormalizedData( - DoubleUtil.round_to_decimal(day_pnl.value), + day_pnl.value.round_to_decimal(), day_long, day_short, - DoubleUtil.round_to_decimal(data.raw_day.long_pos), - DoubleUtil.round_to_decimal(data.raw_day.short_pos), - DoubleUtil.round_to_decimal(sod_pnl.value), + data.raw_day.long_pos.round_to_decimal(), + data.raw_day.short_pos.round_to_decimal(), + sod_pnl.value.round_to_decimal(), sod_long, sod_short, - DoubleUtil.round_to_decimal(data.working_long_pos), - DoubleUtil.round_to_decimal(data.working_short_pos), + data.working_long_pos.round_to_decimal(), + data.working_short_pos.round_to_decimal(), // display - DoubleUtil.round_to_decimal(data.display_day_long_pos), - DoubleUtil.round_to_decimal(data.display_day_short_pos), - DoubleUtil.round_to_decimal(data.display_raw_day_long_pos), - DoubleUtil.round_to_decimal(data.display_raw_day_short_pos), - DoubleUtil.round_to_decimal(data.display_sod_long_pos), - DoubleUtil.round_to_decimal(data.display_sod_short_pos), - DoubleUtil.round_to_decimal(data.display_working_long_pos), - DoubleUtil.round_to_decimal(data.display_working_short_pos)); + data.display_day_long_pos.round_to_decimal(), + data.display_day_short_pos.round_to_decimal(), + data.display_raw_day_long_pos.round_to_decimal(), + data.display_raw_day_short_pos.round_to_decimal(), + data.display_sod_long_pos.round_to_decimal(), + data.display_sod_short_pos.round_to_decimal(), + data.display_working_long_pos.round_to_decimal(), + data.display_working_short_pos.round_to_decimal()); if (_normalized_data != nd) { _normalized_data = nd; updated?.Invoke(nd); diff --git a/src/client/spark/view/Controls/DIYGrid/Columns/ColumnBase.cs b/src/client/spark/view/Controls/DIYGrid/Columns/ColumnBase.cs index 7aa569933..bb3f43dd2 100644 --- a/src/client/spark/view/Controls/DIYGrid/Columns/ColumnBase.cs +++ b/src/client/spark/view/Controls/DIYGrid/Columns/ColumnBase.cs @@ -1,7 +1,9 @@ using bts; +using bts.utils; using mbts.spark; using mbts.spark.filters; +using mbts.ui.dpi; using mbts.ui.settings; using System; @@ -336,8 +338,9 @@ namespace mbts.ui.controls { model.width_changed -= on_model_width_changed; }); void on_model_width_changed(double value) { - if (!DoubleUtil.AreClose(value, Width)) + if (!value.epsilon_equals(Width, HighDpiExtensions.EPSILON)) { SetCurrentValue(WidthProperty, value); + } } void on_width_changed(double value) { model.width_changed -= on_model_width_changed; diff --git a/src/client/spark/view/Controls/DIYGrid/Columns/DIYGridColumn.cs b/src/client/spark/view/Controls/DIYGrid/Columns/DIYGridColumn.cs index f4487a9df..286be4024 100644 --- a/src/client/spark/view/Controls/DIYGrid/Columns/DIYGridColumn.cs +++ b/src/client/spark/view/Controls/DIYGrid/Columns/DIYGridColumn.cs @@ -1,20 +1,21 @@ -using bts.utils; +namespace mbts.ui.controls { + using bts.utils; -using mbts.spark; -using mbts.spark.filters; -using mbts.ui.settings; -using mbts.ui.shell; + using mbts.spark; + using mbts.spark.filters; + using mbts.ui.dpi; + using mbts.ui.settings; + using mbts.ui.shell; -using System; -using System.Collections; -using System.Collections.Generic; -using System.ComponentModel; -using System.Reflection; -using System.Runtime.CompilerServices; -using System.Windows; -using System.Windows.Media; + using System; + using System.Collections; + using System.Collections.Generic; + using System.ComponentModel; + using System.Reflection; + using System.Runtime.CompilerServices; + using System.Windows; + using System.Windows.Media; -namespace mbts.ui.controls { class DIYGridColumn : DIYGrid.ColumnBase { public DIYGridColumn() : base() { @@ -95,8 +96,9 @@ namespace mbts.ui.controls { public double width { get => _width; set { - if (_set(ref _width, value, DoubleUtil.AreClose)) + if (_set(ref _width, value, (x, y) => x.epsilon_equals(y, HighDpiExtensions.EPSILON))) { width_changed?.Invoke(value); + } } } public bool is_visible { get => _is_visible; set => _set(ref _is_visible, value); } diff --git a/src/client/spark/view/Controls/DIYGrid/DIYGrid.cs b/src/client/spark/view/Controls/DIYGrid/DIYGrid.cs index 4a247d49d..446db4eef 100644 --- a/src/client/spark/view/Controls/DIYGrid/DIYGrid.cs +++ b/src/client/spark/view/Controls/DIYGrid/DIYGrid.cs @@ -1159,7 +1159,7 @@ namespace mbts.ui.controls { void on_horizontal_scroll(object sender, RoutedPropertyChangedEventArgs e) { if (_header_scroll_viewer != null) { if (_header_horizontal_scroll_bar != null) { - if (!DoubleUtil.AreClose(e.NewValue, _header_horizontal_scroll_bar.Value)) { + if (!e.NewValue.epsilon_equals(_header_horizontal_scroll_bar.Value, HighDpiExtensions.EPSILON)) { _header_scroll_viewer.ScrollToHorizontalOffset(e.NewValue); invalidate_column_layout(); } diff --git a/src/client/spark/view/Controls/DIYGrid/DoubleUtil.cs b/src/client/spark/view/Controls/DIYGrid/DoubleUtil.cs deleted file mode 100644 index c6c57232d..000000000 --- a/src/client/spark/view/Controls/DIYGrid/DoubleUtil.cs +++ /dev/null @@ -1,49 +0,0 @@ -using System; -using System.Linq; - -namespace mbts.ui.controls { - static class DoubleUtil { - public const int API_DECIMAL_PRECISION = 9; - // arbitrary small number that can be used to determine whether or not scroll changes are significant - public const double MIN_SCROLL_DELTA = 0.000015; - /* smallest such that 1.0+DBL_EPSILON != 1.0 */ - private const double EPSILON = 2.2204460492503131e-016; - private const double BIG_EPSILON = EPSILON * 10; - - public static double nan_to_zero(double value) => - double.IsNaN(value) ? 0.0 : value; - /// - /// Returns if both and are nan, otherwise returns the sum of the non-nan values (if only one value is nan, the other value is returned). - /// - /// One of the two values to add. - /// One of the two values to add. - /// The sum of the two values, with special handling for nans. - public static double nan_to_zero_sum(double v1, double v2) => - double.IsNaN(v1) ? - (double.IsNaN(v2) ? double.NaN : v2) : - (double.IsNaN(v2) ? v1 : v1 + v2); - - public static double nan_to_zero_sum(params double[] values) => - values.Aggregate(0.0, (sum, d) => double.IsNaN(d) ? sum : sum + d); - - /// - /// Returns if both and are nan. Otherwise, if only one is nan, it is substituted for zero, and the difference of the resultant values is returned. - /// - /// The left operand (minuend) in the subtraction op. - /// The right operand (subtrahend) to be used in the subtraction op. - /// The difference of the two values, with special handling for nans. - public static double nan_to_zero_diff(double left, double right) => - double.IsNaN(left) ? - (double.IsNaN(right) ? double.NaN : -right) : - (double.IsNaN(right) ? left : left - right); - - public static decimal round_to_decimal(double value, int decimals = API_DECIMAL_PRECISION) => - double.IsNaN(value) ? 0 : (decimal)Math.Round(value, decimals, MidpointRounding.AwayFromZero); - - internal static bool AreClose(double v1, double v2) { - if (v1 == v2) return true; - double delta = Math.Abs(v1 - v2); - return delta < BIG_EPSILON; - } - } -} diff --git a/src/client/spark/view/Extensions/HighDpiExtensions.cs b/src/client/spark/view/Extensions/HighDpiExtensions.cs index 17d923661..09c835ebc 100644 --- a/src/client/spark/view/Extensions/HighDpiExtensions.cs +++ b/src/client/spark/view/Extensions/HighDpiExtensions.cs @@ -16,6 +16,8 @@ using Point = System.Windows.Point; namespace mbts.ui.dpi { public static class HighDpiExtensions { + public const double EPSILON = 2.2204460492503131e-015; + public static readonly double dpi_ratio, pixel_unit; @@ -139,10 +141,12 @@ namespace mbts.ui.dpi { RoundLayoutValue(value, dpi_ratio); public static double RoundLayoutValue(double value, double dpiScale) { - if (DoubleUtil.AreClose(dpiScale, 1.0)) + dpiScale.epsilon_equals(1.0); + if (dpiScale.epsilon_equals(1.0, EPSILON)) { return Math.Round(value); + } double result = Math.Round(value * dpiScale) / dpiScale; - return DoubleHelper.IsNaNOrInfinity(result) || DoubleUtil.AreClose(result, double.MaxValue) ? value : result; + return DoubleHelper.IsNaNOrInfinity(result) || result.epsilon_equals(double.MaxValue, EPSILON) ? value : result; } public static Point RoundToLayout(this Point point) => diff --git a/src/client/spark/view/Extensions/ScrollViewerBehavior.cs b/src/client/spark/view/Extensions/ScrollViewerBehavior.cs index b5ba6fd15..3f80ba85c 100644 --- a/src/client/spark/view/Extensions/ScrollViewerBehavior.cs +++ b/src/client/spark/view/Extensions/ScrollViewerBehavior.cs @@ -14,7 +14,7 @@ namespace mbts.ui { double voff = DoubleHelper.nan_cast(e.NewValue); // Ignore small deltas to prevent strange layout rounding issues/loops. // Be mindful that small deltas are not accumulated into a single larger delta. - if (!double.IsNaN(voff) && !DoubleHelper.are_close(DoubleHelper.nan_cast(e.OldValue), voff, DoubleUtil.MIN_SCROLL_DELTA)) { + if (!double.IsNaN(voff) && !DoubleHelper.are_close(DoubleHelper.nan_cast(e.OldValue), voff, WPFHelper.MIN_SCROLL_DELTA)) { scroll.InvokeOnLoaded(() => scroll.ScrollToVerticalOffset(voff), () => scroll.GetValue(VerticalOffsetProperty) is double curr_voff && curr_voff == voff); } diff --git a/src/client/spark/view/Utility/WPFHelper.cs b/src/client/spark/view/Utility/WPFHelper.cs index c5f53f8f8..aac26de08 100644 --- a/src/client/spark/view/Utility/WPFHelper.cs +++ b/src/client/spark/view/Utility/WPFHelper.cs @@ -17,6 +17,11 @@ using System.Windows.Threading; namespace mbts.ui { static class WPFHelper { + /// + /// Arbitrary small number used to determine whether or not scroll changes are significant + /// + public const double MIN_SCROLL_DELTA = 0.000015; + public delegate void PropertyChangedCallback(TOwner owner, TValue old_value, TValue new_value); public static void bind(this DependencyObject target, DependencyProperty target_property, object source, params DependencyProperty[] source_properties) { diff --git a/src/client/spark_api/utils/MathUtils.cs b/src/client/spark_api/utils/MathUtils.cs index 428ee0969..52f180049 100644 --- a/src/client/spark_api/utils/MathUtils.cs +++ b/src/client/spark_api/utils/MathUtils.cs @@ -5,6 +5,8 @@ namespace bts.utils { static class MathUtils { public const double EPSILON = 1E-12; + public const int API_DECIMAL_PRECISION = 9; + public static readonly IEqualityComparer epsilon_eq_comparer = new EpsilonComparer(); public static readonly IComparer epsilon_comparer = new ErrorMarginComparer.DoubleImpl(EPSILON); @@ -193,6 +195,9 @@ namespace bts.utils { return result; } + public static decimal round_to_decimal(double value, int decimals = API_DECIMAL_PRECISION) => + double.IsNaN(value) ? 0 : (decimal)Math.Round(value, decimals, MidpointRounding.AwayFromZero); + public static bool is_int(double d) => epsilon_equals(d, Math.Round(d)); diff --git a/src/client/spark_api/utils/NumericExtensions.cs b/src/client/spark_api/utils/NumericExtensions.cs index 30be8ffd2..c0b3108d1 100644 --- a/src/client/spark_api/utils/NumericExtensions.cs +++ b/src/client/spark_api/utils/NumericExtensions.cs @@ -1,4 +1,6 @@ namespace bts.utils { + using System.Collections.Generic; + public static class NumericExtensions { public static bool is_nan(this double value) => double.IsNaN(value); @@ -33,6 +35,9 @@ namespace bts.utils { public static double factor_out(this double value, double factor, out int n_factors_removed) => MathUtils.factor_out(value, factor, out n_factors_removed); + public static decimal round_to_decimal(this double value, int decimals = MathUtils.API_DECIMAL_PRECISION) => + MathUtils.round_to_decimal(value, decimals); + public static int safe_divide(this int value, int divisor) => MathUtils.safe_divide(value, divisor); @@ -53,5 +58,18 @@ namespace bts.utils { public static double safe_divide(this double value, double divisor) => MathUtils.safe_divide(value, divisor); + + /// + /// Computes the sum of a sequence of values (skips non-real numbers) + /// + public static double sum_real(this IEnumerable values) { + double res = 0; + foreach (var v in values) { + if (MathUtils.is_real(v)) { + res += v; + } + } + return res; + } } }