fix: Support Spots in security sort impl
This commit is contained in:
1 parent
6e7e1980f5
commit
f5221910c8
2 files changed
+85
-16
No files matched your search
@@ -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<SecurityImmutable> {
|
||||
s => s.Symbol,
|
||||
s => s.Currency
|
||||
}.Compare(x, y),
|
||||
Security.ContractOneofCase.Future =>
|
||||
new ComparerBuilder<SecurityImmutable> {
|
||||
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<SecurityImmutable> {
|
||||
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
|
||||
};
|
||||
|
||||
@@ -20,7 +20,7 @@ namespace mbts.utils {
|
||||
new(comparers);
|
||||
}
|
||||
|
||||
public class ComparerBuilder<T> : IList<IComparer<T>> {
|
||||
public class ComparerBuilder<T> : IList<IComparer<T>>, IComparer<T> {
|
||||
readonly List<IComparer<T>> _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>(TComparers comparers, T? x, T? y)
|
||||
where TComparers : IEnumerable<IComparer<T>> {
|
||||
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<T>[] 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user