Files
spark/src/client/spark_api/security/SecurityComparer.cs
T
John Colvin f06c1a86ea refactor: Clean up security extension/helper fns
* Define all security-related extension fns in SecurityExtensions
* Rename security extension/helper fns to be in snake_case
2021-12-29 00:50:09 -05:00

151 lines
4.9 KiB
C#

namespace bts.api {
using System;
using System.Collections.Generic;
using System.Linq;
class SecurityComparer : IComparer<Security>, IComparer<SecurityImmutable> {
static readonly SecurityComparer _instance = new();
public static IComparer<Security> instance { get; } = _instance;
public static IComparer<SecurityImmutable> instance_immutable { get; } = _instance;
public static int compare(Security x, Security y) {
if (ReferenceEquals(x, y)) {
return 0;
}
if (x is null) {
return 1;
}
if (y is null) {
return -1;
}
return compare(new SecurityImmutable(x), new SecurityImmutable(y));
}
public static int compare(SecurityImmutable x, SecurityImmutable y) {
int c = non_deterministic_impl();
if (c == 0) {
c = x.ToString().CompareTo(y.ToString());
if (c == 0) {
c = x.Id.CompareTo(y.Id);
}
}
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;
}
var xlegs = new ContractLegEnumerable(x).ToList();
var ylegs = new ContractLegEnumerable(y).ToList();
int max = Math.Min(xlegs.Count, ylegs.Count);
for (int i = 0; i < max; i++) {
var xleg = xlegs[i];
var yleg = ylegs[i];
int comp = _compare(
xleg.Security, xleg.signed_quantity(),
yleg.Security, yleg.signed_quantity());
if (comp != 0) {
return comp;
}
}
return xlegs.Count.CompareTo(ylegs.Count);
}
}
public static int compare(string x_sym, FutureContract x, string y_sym, FutureContract y) {
int sym_comp = x_sym.CompareTo(y_sym);
return sym_comp == 0 ? x.expiration_date().CompareTo(y.expiration_date()) : sym_comp;
}
public static int compare(string x_sym, FutureContractImmutable x, string y_sym, FutureContractImmutable y) {
int sym_comp = x_sym.CompareTo(y_sym);
return sym_comp == 0 ? x.expiration_date().CompareTo(y.expiration_date()) : sym_comp;
}
/// <summary>
/// Compares option/future securities
/// </summary>
/// <exception cref="ArgumentException"/>
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.Option => true,
_ => false
};
if (!is_valid_sec_type(x)) {
throw new ArgumentException("Security not of type Option or Future", nameof(x));
}
if (!is_valid_sec_type(y)) {
throw new ArgumentException("Security not of type Option or Future", nameof(y));
}
// symbol
int comp = string.Compare(x.Symbol, y.Symbol);
if (comp != 0) {
return comp;
}
// expiration
var xexp = x.security_expiration();
var yexp = y.security_expiration();
if (xexp.HasValue) {
if (!yexp.HasValue) {
return -1;
}
comp = xexp.Value.CompareTo(yexp.Value);
if (comp != 0) {
return comp;
}
} else if (yexp.HasValue) {
return 1;
}
// strike
var xk = x.strike();
var yk = y.strike();
if (double.IsNaN(xk) != double.IsNaN(yk)) {
return double.IsNaN(xk) ? -1 : 1;
}
if (!double.IsNaN(xk)) {
comp = xk.CompareTo(yk);
if (comp != 0) {
return comp;
}
}
// security type
comp = x.ContractCase.CompareTo(y.ContractCase);
if (comp != 0) {
return comp;
}
// quantity
comp = xqty.CompareTo(yqty);
if (comp != 0) {
return comp;
}
// venue
comp = x.Exchange.CompareTo(y.Exchange);
if (comp != 0) {
return comp;
}
// id
comp = x.Id.CompareTo(y.Id);
return comp;
}
int IComparer<Security>.Compare(Security x, Security y) =>
compare(x, y);
int IComparer<SecurityImmutable>.Compare(SecurityImmutable x, SecurityImmutable y) =>
compare(x, y);
}
}