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 diff --git a/docker/linux b/docker/linux index e13bb5d8d..c40e6eba4 160000 --- a/docker/linux +++ b/docker/linux @@ -1 +1 @@ -Subproject commit e13bb5d8df14171a9a5abfdb8b6b4ecbc519032a +Subproject commit c40e6eba4ba72480a68cf784b6ffbcacc3fbf4af diff --git a/externals b/externals index b5ca26428..6d8f64ca5 160000 --- a/externals +++ b/externals @@ -1 +1 @@ -Subproject commit b5ca264286e2a7033b7ce6ad0000254dea03dc8a +Subproject commit 6d8f64ca5cee3acbf9addb51428f25d54b9f906a 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; 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; 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/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() diff --git a/src/client/spark/Modules/Trading/PNL/View/PNLRowCollectionView.cs b/src/client/spark/Modules/Trading/PNL/View/PNLRowCollectionView.cs index 083556ab7..d3dec5aa3 100644 --- a/src/client/spark/Modules/Trading/PNL/View/PNLRowCollectionView.cs +++ b/src/client/spark/Modules/Trading/PNL/View/PNLRowCollectionView.cs @@ -373,7 +373,9 @@ namespace bts.shell.pnl { var less_last = data - last_data; var plus_new = less_last + new_data; data += new_data - last_data; - Log.assert(plus_new == data); + if (plus_new != data) { + Log.Ui.warn($"Detected PNL arithmetic rounding error in the group sum for '{string.Join(" ", GroupNamePath)}' (this bug is under investigation)."); + } last_data = new_data; } void on_is_selected_changed(bool is_selected) { diff --git a/src/client/spark/Orders/OrdersModel.cs b/src/client/spark/Orders/OrdersModel.cs index 5126d8406..82baf8ce5 100644 --- a/src/client/spark/Orders/OrdersModel.cs +++ b/src/client/spark/Orders/OrdersModel.cs @@ -81,7 +81,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( @@ -272,7 +271,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; } 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); diff --git a/src/client/spark_api/api/Update.cs b/src/client/spark_api/api/Update.cs index b63d243a4..92bf6a471 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.Remove(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.Replace(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 Remove(TKey key, out TValue value) => + _cache.TryGetValue(key, out value) && _cache.Remove(key); + + public bool Replace(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); @@ -928,4 +937,4 @@ namespace bts.api.core { $"T is '{typeof(T)}', TKey is '{typeof(TKey)}'"; } } -} \ No newline at end of file +} diff --git a/src/lib/config/defaults/keycode.conf b/src/lib/config/defaults/keycode.conf index 49832fb86..5b41d0705 100644 --- a/src/lib/config/defaults/keycode.conf +++ b/src/lib/config/defaults/keycode.conf @@ -1,9 +1,13 @@ + + #nusers: 1 -#allow concurrent : false -#expires: 2019-Sep-30 00:00:00 637053984000000000 -#generation_time: 2019-Jun-18 18:25:44.333280 5248650809870720704 +#allow concurrent : true +#expires: 2019-Dec-31 00:00:00 637133472000000000 +#generation_time: 2019-Sep-30 15:52:10.434845 5248740573731736354 EncryptedKeyCode { value { - keycode: "pgtv/OmievnpmNlgxTYuN801Hbetgu7ZGkHnlGWtK+DBrKSEgKxr5lGvCW1ld/jsztkWhKHDh0iediMDjrPm4+C5qmXlt4JJlAFvKolMN8HVC59m82fECvakcqKq28WSEjf6/DuPJNP1ZwxjcmKouxx1LZ15VlNkECtNrC9pkCnifazbfQZGV3kD08zMEyw/RPmOaVLFFeav8lwI5UpcjPYBIK1rJowXdGSvm1CJViwG5Z2JbdHR39QbKjVx112d5VbVtBq9nO3i0FEKvobjN/Gzcz/5x4TgQtEShKsbmgEN+MBVL3AItQlcFzZvE/05JLmfACUFmk5Eu0a3DlLs3A==" + keycode: "fVZlub92ulkXME+uZSyO9+WmyAXYKD735Qzxxfdj4G+SqEDTuwfNxOZVPS3TCFQqWPDmgjKTbbvfqIrGe3icxkVObUcUoS7lBXRXFrSvzOfs9eTz9RqHRvquDidnOzKxmSXsxV2bMb21UqBCJFeTzb4Q6Agp8EtjXJRc57UMVVygvdXg528S3ocBIq1V4W5NYraFn5WpqrTuBhpYbnsZAHi77s/EEwUT+6la3J2OHPtbiQnB6JFb0zsExMuzE4gwp79ojuQ5pnV2DXBZFNwxYvFyBUU/ac6oU89saZ/ay1DbOjz76TNqnNFewZvzdXZl3y6+X1eblX2streH2pn7gg==" } -} \ No newline at end of file +} + + diff --git a/src/lib/config/protos/toggles.proto b/src/lib/config/protos/toggles.proto index f1733290f..5c3c87ee0 100644 --- a/src/lib/config/protos/toggles.proto +++ b/src/lib/config/protos/toggles.proto @@ -58,3 +58,7 @@ message EnsureOnScreenWithWorkAreaRect { // Should verify that the code works with every monitor arrangement before enabling by default. optional bool enabled = 1 [default = false]; } + +message IgnoreNansInDayPnL { + optional bool enabled = 1 [default = true]; +} \ No newline at end of file diff --git a/src/lib/orderman/actors/FillStore.cpp b/src/lib/orderman/actors/FillStore.cpp index c5a492fa7..ef5ebfb60 100644 --- a/src/lib/orderman/actors/FillStore.cpp +++ b/src/lib/orderman/actors/FillStore.cpp @@ -58,12 +58,10 @@ void FillStore::init() { fill.set_fill_kind(Fill::FILL_KIND_SOD); auto sec = me->oracle.get_security(SecurityKey(fill.security_key())); if (sec) { - if (config::get().enabled() && - securities::is_expired(*sec)) - continue; - else { - auto fk = kvp.first; - me->known_fills.insert(fk); + auto fk = kvp.first; + me->known_fills.insert(fk); + if (!config::get().enabled() || + !securities::is_expired(*sec)) { stored_batch.emplace_back(fill, *sec); } } else diff --git a/src/server/sparkd/position/PositionServicePNLHelper.cpp b/src/server/sparkd/position/PositionServicePNLHelper.cpp index 403c9b52b..a547f6643 100644 --- a/src/server/sparkd/position/PositionServicePNLHelper.cpp +++ b/src/server/sparkd/position/PositionServicePNLHelper.cpp @@ -4,6 +4,7 @@ #include "lib/os/os.h" #include "PositionServicePNLHelper.h" #include "lib/config/protos/cme_reference_data.pb.h" +#include "lib/config/protos/toggles.pb.h" #include "lib/config/config.h" #include "lib/utils/PBUtils.h" @@ -217,6 +218,15 @@ double PositionServicePNLHelper::_compute_day_pnl ( (md - u.pos().day_long_avg_price()), short_pnl = std::abs(u.pos().day_short_pos()) * u.cash_value() * (u.pos().day_short_avg_price() - md); + + if(config::get().enabled()) + { + if(bts::os::is_nan(long_pnl)) + long_pnl = 0.0; + if(bts::os::is_nan(short_pnl)) + short_pnl = 0.0; + } + pnl = long_pnl + short_pnl; break; }