refactor: Get rid of DoubleUtil
* Consolidate functionality in MathUtils and NumericExtensions * Define gui-related epsilons in WPFHelper and HighDpiExtensions
This commit is contained in:
1 parent
174a7baaf4
commit
0a8a3bf6f2
12 files changed
+86
-98
No files matched your search
@@ -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<double>((x, y) => MathUtils.marginal_equals(x, y, DoubleUtil.MIN_SCROLL_DELTA));
|
||||
EqualityComparer.Create<double>((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);
|
||||
}
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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<string> {
|
||||
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); }
|
||||
|
||||
@@ -1159,7 +1159,7 @@ namespace mbts.ui.controls {
|
||||
void on_horizontal_scroll(object sender, RoutedPropertyChangedEventArgs<double> 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();
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
/// <summary>
|
||||
/// Returns <see cref="double.NaN"/> if both <paramref name="v1"/> and <paramref name="v2"/> are nan, otherwise returns the sum of the non-nan values (if only one value is nan, the other value is returned).
|
||||
/// </summary>
|
||||
/// <param name="v1">One of the two values to add.</param>
|
||||
/// <param name="v2">One of the two values to add.</param>
|
||||
/// <returns>The sum of the two values, with special handling for nans.</returns>
|
||||
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);
|
||||
|
||||
/// <summary>
|
||||
/// Returns <see cref="double.NaN"/> if both <paramref name="left"/> and <paramref name="right"/> are nan. Otherwise, if only one is nan, it is substituted for zero, and the difference of the resultant values is returned.
|
||||
/// </summary>
|
||||
/// <param name="left">The left operand (minuend) in the subtraction op.</param>
|
||||
/// <param name="right">The right operand (subtrahend) to be used in the subtraction op.</param>
|
||||
/// <returns>The difference of the two values, with special handling for nans.</returns>
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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) =>
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -17,6 +17,11 @@ using System.Windows.Threading;
|
||||
|
||||
namespace mbts.ui {
|
||||
static class WPFHelper {
|
||||
/// <summary>
|
||||
/// Arbitrary small number used to determine whether or not scroll changes are significant
|
||||
/// </summary>
|
||||
public const double MIN_SCROLL_DELTA = 0.000015;
|
||||
|
||||
public delegate void PropertyChangedCallback<TValue, TOwner>(TOwner owner, TValue old_value, TValue new_value);
|
||||
|
||||
public static void bind(this DependencyObject target, DependencyProperty target_property, object source, params DependencyProperty[] source_properties) {
|
||||
|
||||
@@ -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<double> epsilon_eq_comparer = new EpsilonComparer();
|
||||
public static readonly IComparer<double> 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));
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
/// <summary>
|
||||
/// Computes the sum of a sequence of <see cref="double"/> values (skips non-real numbers)
|
||||
/// </summary>
|
||||
public static double sum_real(this IEnumerable<double> values) {
|
||||
double res = 0;
|
||||
foreach (var v in values) {
|
||||
if (MathUtils.is_real(v)) {
|
||||
res += v;
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user