From 0cc581793266abf1068dc6cb70cb3a356ac8dac2 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Mon, 23 Sep 2019 22:38:24 -0500 Subject: [PATCH 1/7] expand synthetic leg columns to fit name --- .../spark/Modules/Spreader/ExecutionStrategyEditor.xaml.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/client/spark/Modules/Spreader/ExecutionStrategyEditor.xaml.cs b/src/client/spark/Modules/Spreader/ExecutionStrategyEditor.xaml.cs index 83d205f2d..ef8137e25 100644 --- a/src/client/spark/Modules/Spreader/ExecutionStrategyEditor.xaml.cs +++ b/src/client/spark/Modules/Spreader/ExecutionStrategyEditor.xaml.cs @@ -227,7 +227,7 @@ namespace mbts.ui.spark { } DataGridColumn _column_factory(LegSpreaderParamsTuple l) { - var col = new DataGridColumnInternal(l.leg.Security, ParameterCellTemplateConverter, ParameterCellEditingTemplateConverter) { Width = 75, }; + var col = new DataGridColumnInternal(l.leg.Security, ParameterCellTemplateConverter, ParameterCellEditingTemplateConverter); col.HeaderTemplate = (DataTemplate)FindResource(IsNew ? RemovableColumnHeaderTemplateKey : NonRemovableColumnHeaderTemplateKey); DataGridEx.SetRightBorder(col, new DataGridExVerticalLine(false, true, Colors.DarkGray, 1)); return col; From 016ddf9b6950c83dd2a35a7787229f627376e895 Mon Sep 17 00:00:00 2001 From: Jake Norbut Date: Tue, 24 Sep 2019 08:21:56 -0500 Subject: [PATCH 2/7] fixing pay less definition --- doc/user/content/bts_spark/Spreader/_index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/user/content/bts_spark/Spreader/_index.md b/doc/user/content/bts_spark/Spreader/_index.md index a69a3731e..79a1c6168 100644 --- a/doc/user/content/bts_spark/Spreader/_index.md +++ b/doc/user/content/bts_spark/Spreader/_index.md @@ -154,7 +154,7 @@ insert_anchor_links = "right" - Pay Less - If enabled by checking the checkbox, the spreader will attempt to pay less when the quantity at the level of the calculated - hedge price is more than the threshold defined.  The hedge order is + hedge price is less than the threshold defined.  The hedge order is paid less by the “Payless Ticks” as defined per leg. - Pay Up - If enabled by checking the checkbox, the spreader will pay up when the quantity at the level of the calculated hedge price is From d033275694eed0a88515672c28db2616951b5866 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Mon, 23 Sep 2019 12:26:07 -0500 Subject: [PATCH 3/7] remove unused member OrdersModel.on_account_pnl --- src/client/spark/Orders/OrdersModel.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/client/spark/Orders/OrdersModel.cs b/src/client/spark/Orders/OrdersModel.cs index 278324bcb..920acfca3 100644 --- a/src/client/spark/Orders/OrdersModel.cs +++ b/src/client/spark/Orders/OrdersModel.cs @@ -75,7 +75,6 @@ namespace mbts.spark { .TableCheckedAutoCorrect(EqualityComparer<(SecurityImmutable? sec, PNLUnitImmutable data)>.Default) .Where(t => t.has_changes()) .Cache(); - on_account_pnl = _position_service.subscribei_pnl_account_data().Naggle(interval, scheduler).Table().Cache(); var working_orders = on_orders.Filter(tup => tup.order.is_active()); this.working_orders = on_orders.Scan( @@ -265,7 +264,6 @@ namespace mbts.spark { public IObservable> working_orders { get; } public IObservable> on_pnl { get; } public IObservable> security_account_pnl { get; } - public IObservable> on_account_pnl { get; } public IObservable> on_positions { get; } public IObservable> studio_algos_with_meta { get; } public IObservable> on_manual_fills_securities { get; } From a3730eb20354df481fe56d18f0c6a10aa1686876 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Mon, 23 Sep 2019 18:08:38 -0500 Subject: [PATCH 4/7] Update.naggle merge maintains changes (could get into state where changes became adds/removes) --- src/client/spark_api/api/Update.cs | 99 ++++++++++++++++-------------- 1 file changed, 54 insertions(+), 45 deletions(-) diff --git a/src/client/spark_api/api/Update.cs b/src/client/spark_api/api/Update.cs index 80ed9b1cf..004e81dec 100644 --- a/src/client/spark_api/api/Update.cs +++ b/src/client/spark_api/api/Update.cs @@ -8,7 +8,6 @@ using System.Linq; using System.Reactive.Concurrency; using System.Reactive.Disposables; using System.Reactive.Linq; -using System.Reactive.Subjects; using System.Text; namespace bts.api.core { @@ -94,38 +93,38 @@ namespace bts.api.core { new Update(new T[0], new T[0], new Change[0], new T[0], key_extractor); public Update merge(Update next) { - var comp = new RowEqualityComparer(_key); - var added = this.added as HashSet ?? new HashSet(this.added, comp); + var added = this.added as KeyedValueSet ?? new KeyedValueSet(this.added, _key); var changes = this.changed as ChangeDict ?? new ChangeDict(this.changes, _key); - var removed = this.removed as HashSet ?? new HashSet(this.removed, comp); + var removed = this.removed as KeyedValueSet ?? new KeyedValueSet(this.removed, _key); // Add - added.UnionWith(next.added); + foreach (var v in next.added) { + var k = _key(v); + if (removed.TryPop(k, out var oldval)) { + changes.AddOrMerge(new Change(oldval, v)); + } else { + added[k] = v; + } + } // Change foreach (var c in next.changes) { - if (added.Contains(c.Old)) { - added.Remove(c.Old); - added.Add(c.New); - } - else { + var k = _key(c.Old); + if (!added.TryReplace(k, c.New)) changes.AddOrMerge(c); - } } // Remove foreach (var r in next.removed) { - if (!added.Remove(r)) { - removed.Add(r); - } - changes.Remove(_key(r)); + var k = _key(r); + if (!added.Remove(k)) + removed[k] = changes.TryRemove(k, out var ch) ? ch.Old : r; } return new Update( next.all, added, changes, removed, - _key - ); + _key); } public IEnumerable all { get; } @@ -148,6 +147,39 @@ namespace bts.api.core { #endif } + class KeyedValueSet : IEnumerable { + readonly Dictionary _cache = new Dictionary(); + + public KeyedValueSet(IEnumerable value, Func key) { + foreach (var v in value) + _cache[key(v)] = v; + } + + public TValue this[TKey key] { + set => _cache[key] = value; + } + + public bool Remove(TKey key) => + _cache.Remove(key); + + public bool TryPop(TKey key, out TValue value) => + _cache.TryGetValue(key, out value) && _cache.Remove(key); + + public bool TryReplace(TKey key, TValue value) { + if (_cache.ContainsKey(key)) { + _cache[key] = value; + return true; + } + return false; + } + + public IEnumerator GetEnumerator() => + _cache.Values.GetEnumerator(); + + IEnumerator IEnumerable.GetEnumerator() => + GetEnumerator(); + } + class ChangeDict : IEnumerable> { Dictionary> _changes; Func, TKey> _key; @@ -167,9 +199,11 @@ namespace bts.api.core { return null; } - public void Remove(TKey key) { + public bool Remove(TKey key) => _changes.Remove(key); - } + + public bool TryRemove(TKey key, out Change result) => + _changes.TryGetValue(key, out result) && _changes.Remove(key); #region IEnumerable public IEnumerator> GetEnumerator() { @@ -181,31 +215,6 @@ namespace bts.api.core { #endregion } - struct RowEqualityComparer : IEqualityComparer, IEqualityComparer> - where TKey : IEquatable { - readonly Func _key; - - public RowEqualityComparer(Func key) { - _key = key; - } - - public bool Equals(T x, T y) { - return _key(x).Equals(_key(y)); - } - - public bool Equals(Change x, Change y) { - return Equals(x.New, y.New); - } - - public int GetHashCode(T obj) { - return _key(obj).GetHashCode(); - } - - public int GetHashCode(Change c) { - return GetHashCode(c.New); - } - } - public class TableUpdate where TKey : IEquatable { // warning: this does not have a key_extractor public static readonly TableUpdate empty = new TableUpdate(ImmutableDictionary.Empty, Array.Empty(), Array.Empty>(), Array.Empty(), null); @@ -922,4 +931,4 @@ namespace bts.api.core { $"T is '{typeof(T)}', TKey is '{typeof(TKey)}'"; } } -} \ No newline at end of file +} From 2a36c05d7597d7ddc121224ee505131f15b3b935 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Mon, 23 Sep 2019 18:12:09 -0500 Subject: [PATCH 5/7] fix PNLRowData Equals impl (was not comparing market prices) --- src/client/spark/Modules/Trading/PNL/Data/PNLData.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/client/spark/Modules/Trading/PNL/Data/PNLData.cs b/src/client/spark/Modules/Trading/PNL/Data/PNLData.cs index 9c941fc55..b6a83ae6a 100644 --- a/src/client/spark/Modules/Trading/PNL/Data/PNLData.cs +++ b/src/client/spark/Modules/Trading/PNL/Data/PNLData.cs @@ -501,6 +501,12 @@ namespace bts.shell.pnl { account_name == other.account_name && sod == other.sod && day == other.day && + bid_price.Equals(other.bid_price) && + ask_price.Equals(other.ask_price) && + last_price.Equals(other.last_price) && + mid_price.Equals(other.mid_price) && + settlement_price.Equals(other.settlement_price) && + calc_type.Equals(other.calc_type) && raw_day == other.raw_day && working.summary == other.working.summary; From ffb571d1cfbf0318d8fce5626df6a3ce7623b7b5 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Mon, 23 Sep 2019 18:14:34 -0500 Subject: [PATCH 6/7] fix pnl rounding issues (use 9 decimal places) --- .../spark/Modules/Trading/PNL/VM/PNLRowVM.cs | 20 +++++++++---------- .../spark/view/Controls/DIYGrid/DoubleUtil.cs | 10 ++++++---- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/client/spark/Modules/Trading/PNL/VM/PNLRowVM.cs b/src/client/spark/Modules/Trading/PNL/VM/PNLRowVM.cs index f32cee536..52cfa3ff0 100644 --- a/src/client/spark/Modules/Trading/PNL/VM/PNLRowVM.cs +++ b/src/client/spark/Modules/Trading/PNL/VM/PNLRowVM.cs @@ -97,22 +97,22 @@ 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 = (decimal)DoubleUtil.nan_to_zero(data.day.long_pos); - day_short = (decimal)DoubleUtil.nan_to_zero(data.day.short_pos); - sod_long = (decimal)DoubleUtil.nan_to_zero(data.sod.long_pos); - sod_short = (decimal)DoubleUtil.nan_to_zero(data.sod.short_pos); + 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); } var nd = new PNLNormalizedData( - (decimal)DoubleUtil.nan_to_zero(day_pnl.value), + DoubleUtil.round_to_decimal(day_pnl.value), day_long, day_short, - (decimal)DoubleUtil.nan_to_zero(data.raw_day.long_pos), - (decimal)DoubleUtil.nan_to_zero(data.raw_day.short_pos), - (decimal)DoubleUtil.nan_to_zero(sod_pnl.value), + 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), sod_long, sod_short, - (decimal)DoubleUtil.nan_to_zero(data.working_long_pos), - (decimal)DoubleUtil.nan_to_zero(data.working_short_pos)); + DoubleUtil.round_to_decimal(data.working_long_pos), + DoubleUtil.round_to_decimal(data.working_short_pos)); if (_normalized_data != nd) { _normalized_data = nd; updated?.Invoke(nd); diff --git a/src/client/spark/view/Controls/DIYGrid/DoubleUtil.cs b/src/client/spark/view/Controls/DIYGrid/DoubleUtil.cs index 2ac6f579b..aa2019135 100644 --- a/src/client/spark/view/Controls/DIYGrid/DoubleUtil.cs +++ b/src/client/spark/view/Controls/DIYGrid/DoubleUtil.cs @@ -1,10 +1,9 @@ using System; using System.Linq; -namespace mbts.ui.controls -{ - internal class DoubleUtil - { +namespace mbts.ui.controls { + static class DoubleUtil { + public const int API_DECIMAL_PRECISION = 9; /* smallest such that 1.0+DBL_EPSILON != 1.0 */ private const double EPSILON = 2.2204460492503131e-016; private const double BIG_EPSILON = EPSILON * 10; @@ -36,6 +35,9 @@ namespace mbts.ui.controls (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); From ddc50e1fbacd0b57ea3adeba52caf20c6b711f93 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Mon, 23 Sep 2019 21:57:54 -0500 Subject: [PATCH 7/7] pnl initial calc type was not being applied to data --- src/client/spark/Modules/Trading/PNL/VM/PNLVM.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/client/spark/Modules/Trading/PNL/VM/PNLVM.cs b/src/client/spark/Modules/Trading/PNL/VM/PNLVM.cs index b04c03ead..19b6ff8da 100644 --- a/src/client/spark/Modules/Trading/PNL/VM/PNLVM.cs +++ b/src/client/spark/Modules/Trading/PNL/VM/PNLVM.cs @@ -103,7 +103,7 @@ namespace bts.shell.pnl { load_from(settings); _dispose = Disposable.Create( pnl_source.Operate( - _calculation_type.observe_current(), + _calculation_type.observe_current(true), (data, calc) => data.with(calc_type: calc) ).FilterWithRaw(filter_src, PNLRowData.key_fn, null) .OnDispatcher()