From f5221910c8bca78a241b670ee0f3f565bf98ad94 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Fri, 4 Nov 2022 17:37:19 -0400 Subject: [PATCH] fix: Support Spots in security sort impl --- .../spark_api/security/SecurityComparer.cs | 74 +++++++++++++++++-- .../spark_core/utils/ComparerBuilder.cs | 27 ++++--- 2 files changed, 85 insertions(+), 16 deletions(-) diff --git a/src/client/spark_api/security/SecurityComparer.cs b/src/client/spark_api/security/SecurityComparer.cs index a701db8c5..ffdea48b5 100644 --- a/src/client/spark_api/security/SecurityComparer.cs +++ b/src/client/spark_api/security/SecurityComparer.cs @@ -1,4 +1,6 @@ namespace bts.api { + using mbts.utils; + using System; using System.Collections.Generic; using System.Linq; @@ -31,14 +33,73 @@ namespace bts.api { } } return c; + // this has been observed to be non-determinisitc with certain ICE securities // (it is determenistic with other exchanges..as far as we know). int non_deterministic_impl() { - if (x.ContractCase == Security.ContractOneofCase.Future) { - return y.ContractCase == Security.ContractOneofCase.Future ? compare(x.Symbol, x.Future, y.Symbol, y.Future) : -1; - } else if (y.ContractCase == Security.ContractOneofCase.Future) { - return 1; - } + // | Type 1 | Type 2 | Comp Hierarchy | + // |------------------------------------------| + // | SPOT | SPOT | sym, curr | + // | SPOT | FUT | sym, curr | + // | SPOT | OPT | SPOT < OPT | + // | SPOT | SPREAD | * SPOT < SPREAD | + // | SPOT | SYNTH | * SPOT < SYNTH | + // | FUT | FUT | sym, exp, curr | + // | FUT | OPT | * FUT < OPT | + // | FUT | SPREAD | * FUT < SPREAD | + // | FUT | SYNTH | * FUT < SYNTH | + // | OPT | OPT | sym, exp, curr, k, c/p | + // | OPT | SPREAD | OPT <=> first leg | + // | OPT | SYNTH | OPT <=> first leg | + // | SPREAD | SPREAD | legs <=> legs | + // | SPREAD | SYNTH | legs <=> legs | + // + // * comp result is constant (there is no comp hierarchy) + + return x.ContractCase switch { + Security.ContractOneofCase.Future => + y.ContractCase switch { + Security.ContractOneofCase.Spot => + new ComparerBuilder { + s => s.Symbol, + s => s.Currency + }.Compare(x, y), + Security.ContractOneofCase.Future => + new ComparerBuilder { + s => s.Symbol, + s => s.Future.expiration_date() + }.Compare(x, y), + Security.ContractOneofCase.Option or + Security.ContractOneofCase.Spread or + Security.ContractOneofCase.Synthetic => -1, + _ => 1, + }, + Security.ContractOneofCase.Spot => + y.ContractCase switch { + Security.ContractOneofCase.Spot or + Security.ContractOneofCase.Future => + new ComparerBuilder { + s => s.Symbol, + s => s.Currency + }.Compare(x, y), + _ => -1, + }, + Security.ContractOneofCase.Option or + Security.ContractOneofCase.Spread or + Security.ContractOneofCase.Synthetic => + y.ContractCase switch { + Security.ContractOneofCase.Spread or + Security.ContractOneofCase.Synthetic or + Security.ContractOneofCase.Option => compare_flat_legs(), + _ => 1, + }, + Security.ContractOneofCase.None => + y.ContractCase == Security.ContractOneofCase.None ? 0 : -1, + _ => throw new InvalidOperationException(), + }; + } + + int compare_flat_legs() { var xlegs = new ContractLegEnumerable(x).ToList(); var ylegs = new ContractLegEnumerable(y).ToList(); int max = Math.Min(xlegs.Count, ylegs.Count); @@ -73,7 +134,8 @@ namespace bts.api { static int _compare(SecurityImmutable x, double xqty, SecurityImmutable y, double yqty) { static bool is_valid_sec_type(SecurityImmutable sec) => sec.ContractCase switch { - Security.ContractOneofCase.Future => true, + Security.ContractOneofCase.Spot or + Security.ContractOneofCase.Future or Security.ContractOneofCase.Option => true, _ => false }; diff --git a/src/client/spark_core/utils/ComparerBuilder.cs b/src/client/spark_core/utils/ComparerBuilder.cs index 389982ab9..9b65ee632 100644 --- a/src/client/spark_core/utils/ComparerBuilder.cs +++ b/src/client/spark_core/utils/ComparerBuilder.cs @@ -20,7 +20,7 @@ namespace mbts.utils { new(comparers); } - public class ComparerBuilder : IList> { + public class ComparerBuilder : IList>, IComparer { readonly List> _comparers; public ComparerBuilder() => @@ -98,6 +98,20 @@ namespace mbts.utils { return to_comparer(); } + public int Compare(T? x, T? y) => + _compare(_comparers, x, y); + + static int _compare(TComparers comparers, T? x, T? y) + where TComparers : IEnumerable> { + foreach (var comp in comparers) { + int result = comp.Compare(x, y); + if (result != 0) { + return result; + } + } + return 0; + } + IEnumerator IEnumerable.GetEnumerator() => _comparers.GetEnumerator(); @@ -107,15 +121,8 @@ namespace mbts.utils { public _Comparer(IComparer[] comparers) => _comparers = comparers; - public int Compare(T? x, T? y) { - foreach (var comp in _comparers) { - int result = comp.Compare(x, y); - if (result != 0) { - return result; - } - } - return 0; - } + public int Compare(T? x, T? y) => + _compare(_comparers, x, y); } } }