475 lines
19 KiB
C#
475 lines
19 KiB
C#
#nullable enable
|
|
namespace mbts.spark {
|
|
using bts.api;
|
|
using bts.collections;
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics.CodeAnalysis;
|
|
using System.Linq;
|
|
using System.Reactive.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
public static class SecurityCriteriaExtensions {
|
|
// Check for null in these methods because they are often called from a SecurityCriteria
|
|
// member stored in a larger ProtoBuf. God forbid such a ProtoBuf changes in a way that
|
|
// deserialization of a previous version yields a null SecurityCriteria member. If
|
|
// SecurityCriteria is null, behave as though all securities are included.
|
|
|
|
public static async Task<IDictionary<uint, SecurityImmutable>> securities(
|
|
this SecurityCriteria self, IObservable<IReadOnlyDictionary<uint, SecurityMD>> md_source) {
|
|
var md_map = await md_source.FirstAsync();
|
|
var all_secs = md_map.Values.Select(md => md.security).ToDictionary(key => key.Id);
|
|
return self is null ? all_secs :
|
|
all_secs.Values.Where(self.includes).ToDictionary(key => key.Id);
|
|
}
|
|
|
|
public static IObservable<IEnumerable<SecurityImmutable>> subscribe(
|
|
this SecurityCriteria? self, IObservable<IReadOnlyDictionary<uint, SecurityMD>> md_source) {
|
|
return self is null ? md_source.Select(kv => kv.Values.Select(smd => smd.security)) :
|
|
md_source.Select(m => m.Values
|
|
.Select(smd => smd.security).Where(self.includes));
|
|
}
|
|
|
|
public static IObservable<IReadOnlyDictionary<uint, SecurityImmutable>> subscribe_map(
|
|
this SecurityCriteria? self, IObservable<IReadOnlyDictionary<uint, SecurityMD>> md_source) {
|
|
if (self is null) {
|
|
return md_source.Select(m => MappedDictionary.create(m, smd => smd.security));
|
|
}
|
|
return md_source.Select(m => m
|
|
.Where(kv => self.includes(kv.Value.security))
|
|
.ToDictionary(kv => kv.Key, kv => kv.Value.security));
|
|
}
|
|
|
|
public static IObservable<IEnumerable<uint>> subscribe_keys(
|
|
this SecurityCriteria self, IObservable<IReadOnlyDictionary<uint, SecurityMD>> md_source) {
|
|
return self is null ? md_source.Select(kv => kv.Values.Select(smd => smd.security.Id)) :
|
|
md_source.Select(m => m.Values
|
|
.Where(smd => self.includes(smd.security))
|
|
.Select(smd => smd.security.Id));
|
|
}
|
|
|
|
public static IObservable<IEnumerable<SecurityMD>> subscribe_md(
|
|
this SecurityCriteria? self, IObservable<IReadOnlyDictionary<uint, SecurityMD>> md_source) =>
|
|
self.is_defined() ?
|
|
md_source.Select(mds =>
|
|
mds.Values.Where(md => self.includes(md.security))) :
|
|
md_source.Select(mds => mds.Values);
|
|
|
|
public static IObservable<IReadOnlyDictionary<uint, SecurityMD>> subscribe_md_map(
|
|
this SecurityCriteria self, IObservable<IReadOnlyDictionary<uint, SecurityMD>> md_source) =>
|
|
md_source.Select(m => m
|
|
.Where(kv => self.includes(kv.Value.security))
|
|
.ToDictionary(kv => kv.Key, kv => kv.Value));
|
|
|
|
/// <summary>
|
|
/// Indicates whether or not <paramref name="criteria"/> is non-null, and has at least 1
|
|
/// defined clause (<see cref="SecurityCriteriaClause.is_defined"/>).
|
|
/// </summary>
|
|
/// <param name="criteria">A SecurityCriteria.</param>
|
|
/// <returns>
|
|
/// true if the criteria is non-null and has at least 1 clause; otherwise false
|
|
/// </returns>
|
|
public static bool is_defined([NotNullWhen(true)] this SecurityCriteria? criteria) =>
|
|
criteria?.clauses is not null &&
|
|
criteria.clauses.Any(c => c.is_defined);
|
|
|
|
/// <summary>
|
|
/// Indicates whether or not <paramref name="criteria"/> specifies exactly one security
|
|
/// </summary>
|
|
/// <param name="criteria">The SecurityCriteria</param>
|
|
/// <returns>true if the criteria specifies exactly one security; otherwise false.</returns>
|
|
public static bool is_single_security(this SecurityCriteria criteria) =>
|
|
criteria.clauses is not null &&
|
|
criteria.clauses.Length > 0 &&
|
|
criteria.clauses.All(is_single_security) &&
|
|
criteria.clauses
|
|
.DistinctOn(c => c.security_keys?.First() ?? throw new InvalidOperationException())
|
|
.CountExact(1);
|
|
|
|
/// <summary>
|
|
/// Indicates whether or not <paramref name="clause"/> specifies exactly one security
|
|
/// </summary>
|
|
/// <param name="criteria">The SecurityCriteriaClause</param>
|
|
/// <returns>true if the criteria specifies exactly one security; otherwise false.</returns>
|
|
public static bool is_single_security(this SecurityCriteriaClause clause) =>
|
|
clause.exchanges.is_null_or_empty() &&
|
|
clause.symbols.is_null_or_empty() &&
|
|
clause.security_types.is_null_or_empty() &&
|
|
clause.names.is_null_or_empty() &&
|
|
clause.security_keys is not null &&
|
|
!clause.security_keys.Distinct().CountExact(1);
|
|
|
|
public static bool includes(
|
|
this SecurityCriteria security_criteria, SecurityImmutable security) =>
|
|
security_criteria.is_defined() &&
|
|
security_criteria.clauses.empty_if_null().Any(c => c.includes(security));
|
|
|
|
public static bool includes(
|
|
this SecurityCriteriaClause clause, SecurityImmutable security) =>
|
|
_to_predicates(clause).AnyAndAll(p => p(security));
|
|
|
|
public static bool is_subset_of(
|
|
this SecurityCriteriaClause clause, SecurityCriteriaClause other) =>
|
|
// exchanges
|
|
(
|
|
other.exchanges is null ||
|
|
other.exchanges.Length == 0 || (
|
|
clause.exchanges is not null &&
|
|
clause.exchanges.Length > 0 &&
|
|
clause.exchanges.All(other.exchanges.Contains)
|
|
)
|
|
) &&
|
|
// symbols
|
|
(
|
|
other.symbols is null ||
|
|
other.symbols.Length == 0 || (
|
|
clause.symbols is not null &&
|
|
clause.symbols.Length > 0 &&
|
|
clause.symbols.All(other.symbols.Contains)
|
|
)
|
|
) &&
|
|
// security_types
|
|
(
|
|
other.security_types is null ||
|
|
other.security_types.Length == 0 || (
|
|
clause.security_types is not null &&
|
|
clause.security_types.Length > 0 &&
|
|
clause.security_types.All(other.security_types.Contains)
|
|
)
|
|
) &&
|
|
// security_keys
|
|
(
|
|
other.security_keys is null ||
|
|
other.security_keys.Length == 0 || (
|
|
clause.security_keys is not null &&
|
|
clause.security_keys.Length > 0 &&
|
|
clause.security_keys.All(other.security_keys.Contains)
|
|
)
|
|
) &&
|
|
// strikes
|
|
(
|
|
other.strikes is null ||
|
|
other.strikes.Length == 0 || (
|
|
clause.strikes is not null &&
|
|
clause.strikes.Length > 0 &&
|
|
clause.strikes.All(other.strikes.Contains)
|
|
)
|
|
) &&
|
|
// callputs
|
|
(
|
|
other.callputs is null ||
|
|
other.callputs.Length == 0 || (
|
|
clause.callputs is not null &&
|
|
clause.callputs.Length > 0 &&
|
|
clause.callputs.All(other.callputs.Contains)
|
|
)
|
|
) &&
|
|
// names
|
|
(
|
|
other.names is null ||
|
|
other.names.Length == 0 || (
|
|
clause.names is not null &&
|
|
clause.names.Length > 0 &&
|
|
clause.names.All(other.names.Contains)
|
|
)
|
|
);
|
|
|
|
public static SecurityCriteriaClause with(this SecurityCriteriaClause clause,
|
|
Venue[]? exchanges = null,
|
|
SecurityType[]? security_types = null,
|
|
string[]? symbols = null,
|
|
DateTime[]? expirations = null,
|
|
uint[]? security_keys = null,
|
|
string? pretty_name = null,
|
|
double[]? strikes = null,
|
|
PutOrCall[]? callputs = null,
|
|
string[]? names = null) {
|
|
return new(
|
|
exchanges ?? clause.exchanges,
|
|
security_types ?? clause.security_types,
|
|
symbols ?? clause.symbols,
|
|
expirations ?? clause.expirations,
|
|
security_keys ?? clause.security_keys,
|
|
pretty_name ?? clause.pretty_name,
|
|
strikes ?? clause.strikes,
|
|
callputs ?? clause.callputs,
|
|
names ?? clause.names);
|
|
}
|
|
|
|
public static IEnumerable<string> fmt_diagnostic_msgs(this SecurityCriteriaClause clause) {
|
|
if (clause.exchanges is not null && clause.exchanges.Length > 0) {
|
|
yield return "Exchanges: " + string.Join(", ", clause.exchanges);
|
|
}
|
|
if (clause.symbols is not null && clause.symbols.Length > 0) {
|
|
yield return "Symbols: " + string.Join(", ", clause.symbols);
|
|
}
|
|
if (clause.security_types is not null && clause.security_types.Length > 0) {
|
|
yield return "Security Types: " + string.Join(", ", clause.security_types);
|
|
}
|
|
if (clause.security_keys is not null && clause.security_keys.Length > 0) {
|
|
yield return "Security Keys: " + string.Join(", ", clause.security_keys);
|
|
}
|
|
if (clause.strikes is not null && clause.strikes.Length > 0) {
|
|
yield return "Strikes: " + string.Join(", ", clause.strikes);
|
|
}
|
|
if (clause.callputs is not null && clause.callputs.Length > 0) {
|
|
yield return "Callputs: " + string.Join(", ", clause.callputs);
|
|
}
|
|
if (clause.names is not null && clause.names.Length > 0) {
|
|
yield return "Names: " + string.Join(", ", clause.names);
|
|
}
|
|
}
|
|
|
|
public static string fmt_pretty(
|
|
this SecurityCriteriaClause clause, Func<uint, string> fmt_sec) =>
|
|
clause.fmt_pretty(
|
|
SecurityCriterionTypes.Security |
|
|
SecurityCriterionTypes.CallPut |
|
|
SecurityCriterionTypes.Strike |
|
|
SecurityCriterionTypes.Symbol |
|
|
SecurityCriterionTypes.SecurityType |
|
|
SecurityCriterionTypes.Expiration |
|
|
SecurityCriterionTypes.Exchange |
|
|
SecurityCriterionTypes.Name,
|
|
fmt_sec);
|
|
|
|
public static string fmt_pretty(
|
|
this SecurityCriteriaClause clause,
|
|
SecurityCriterionTypes criterion_types,
|
|
Func<uint, string> fmt_sec,
|
|
int max_num_strikes = 4,
|
|
string delimiter = "•"
|
|
) {
|
|
criterion_types &= clause.populated_components();
|
|
if (criterion_types.HasFlag(SecurityCriterionTypes.Security) &&
|
|
clause.security_keys is not null &&
|
|
clause.security_keys.Length > 0 &&
|
|
fmt_sec is not null
|
|
) {
|
|
return string.Join(delimiter, clause.security_keys.Select(fmt_sec));
|
|
}
|
|
|
|
var sb = new StringBuilder();
|
|
|
|
if (criterion_types.HasFlag(SecurityCriterionTypes.Symbol)) {
|
|
sb.Append(string.Join(delimiter, clause.symbols.empty_if_null()));
|
|
}
|
|
|
|
if (criterion_types.HasFlag(SecurityCriterionTypes.SecurityType)) {
|
|
if (sb.Length > 0) {
|
|
sb.Append(' ');
|
|
}
|
|
sb.Append(string.Join(
|
|
delimiter, clause.security_types.empty_if_null()));
|
|
}
|
|
|
|
if (criterion_types.HasFlag(SecurityCriterionTypes.Expiration)) {
|
|
if (sb.Length > 0) {
|
|
sb.Append(' ');
|
|
}
|
|
sb.Append(string.Join(delimiter, clause.expirations.empty_if_null()));
|
|
}
|
|
|
|
if (criterion_types.HasFlag(SecurityCriterionTypes.Strike)) {
|
|
if (sb.Length > 0) {
|
|
sb.Append(" K ");
|
|
}
|
|
if (clause.strikes is not null && clause.strikes.Length > max_num_strikes) {
|
|
sb.Append($"{clause.strikes.Min()}...{clause.strikes.Max()}");
|
|
} else {
|
|
sb.Append(string.Join(' ', clause.strikes.empty_if_null().Ordered()));
|
|
}
|
|
}
|
|
|
|
if (criterion_types.HasFlag(SecurityCriterionTypes.CallPut)) {
|
|
if (sb.Length > 0) {
|
|
sb.Append(' ');
|
|
}
|
|
foreach (var cp in clause.callputs.empty_if_null()) {
|
|
sb.Append(cp switch {
|
|
PutOrCall.Call => "C",
|
|
PutOrCall.Put => "P",
|
|
_ => string.Empty
|
|
});
|
|
}
|
|
}
|
|
|
|
if (criterion_types.HasFlag(SecurityCriterionTypes.Exchange)) {
|
|
if (sb.Length > 0) {
|
|
sb.Append(" at ");
|
|
}
|
|
sb.Append(string.Join(delimiter, clause.exchanges.empty_if_null()));
|
|
}
|
|
if (criterion_types.HasFlag(SecurityCriterionTypes.Name)) {
|
|
sb.Append(string.Join(delimiter, clause.names.empty_if_null()));
|
|
}
|
|
return sb.ToString().TrimEnd();
|
|
}
|
|
|
|
public static SecurityCriterionTypes populated_components(
|
|
this SecurityCriteriaClause clause) {
|
|
var result = SecurityCriterionTypes.None;
|
|
if (!clause.exchanges.is_null_or_empty()) {
|
|
result |= SecurityCriterionTypes.Exchange;
|
|
}
|
|
if (!clause.symbols.is_null_or_empty()) {
|
|
result |= SecurityCriterionTypes.Symbol;
|
|
}
|
|
if (!clause.security_types.is_null_or_empty()) {
|
|
result |= SecurityCriterionTypes.SecurityType;
|
|
}
|
|
if (!clause.expirations.is_null_or_empty()) {
|
|
result |= SecurityCriterionTypes.Expiration;
|
|
}
|
|
if (!clause.strikes.is_null_or_empty()) {
|
|
result |= SecurityCriterionTypes.Strike;
|
|
}
|
|
if (!clause.callputs.is_null_or_empty()) {
|
|
result |= SecurityCriterionTypes.CallPut;
|
|
}
|
|
if (!clause.security_keys.is_null_or_empty()) {
|
|
result |= SecurityCriterionTypes.Security;
|
|
}
|
|
if (!clause.names.is_null_or_empty()) {
|
|
result |= SecurityCriterionTypes.Name;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Attempts to convert security names (<see cref="SecurityCriteriaClause.Names"/>) to ids for
|
|
/// faster lookups in a security map
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// When a name is resolved, it is removed from <see cref="SecurityCriteriaClause.Names"/>,
|
|
/// and its corresponding id added to <see cref="SecurityCriteriaClause.Keys"/>
|
|
/// </remarks>
|
|
/// <returns>The number of names that were resolved</returns>
|
|
public static int resolve_names(
|
|
this bts.api.domain.SecurityCriteriaClause api_clause, Func<string, uint?> try_resolve) {
|
|
var orig_num_names = api_clause.Names.Count;
|
|
for (var i = 0; i < api_clause.Names.Count;) {
|
|
var name = api_clause.Names[i];
|
|
var id = try_resolve(name);
|
|
if (id.HasValue) {
|
|
api_clause.Names.RemoveAt(i);
|
|
api_clause.Keys.Add(id.Value);
|
|
} else {
|
|
i++;
|
|
}
|
|
}
|
|
var n_resolved = api_clause.Names.Count - orig_num_names;
|
|
return n_resolved;
|
|
}
|
|
|
|
// public static int resolve_names(
|
|
// this bts.api.domain.SecurityCriteriaClauseImmutable api_clause, Func<string, uint?> try_resolve) {
|
|
// var orig_num_names = api_clause.Names.Count();
|
|
// for (var i = 0; i < api_clause.Names.Count;) {
|
|
// var name = api_clause.Names[i];
|
|
// var id = try_resolve(name);
|
|
// if (id.HasValue) {
|
|
// api_clause.Names.RemoveAt(i);
|
|
// api_clause.Keys.Add(id.Value);
|
|
// } else {
|
|
// i++;
|
|
// }
|
|
// }
|
|
// var n_resolved = api_clause.Names.Count - orig_num_names;
|
|
// return n_resolved;
|
|
// }
|
|
|
|
public static bts.api.domain.SecurityCriteriaClause to_clause(
|
|
this SecurityCriteriaClause clause) {
|
|
var api_clause = new bts.api.domain.SecurityCriteriaClause();
|
|
api_clause.Venues.AddRange(
|
|
clause.exchanges.empty_if_null().Select(e => e.to_api_venue()));
|
|
api_clause.Symbols.AddRange(clause.symbols.empty_if_null());
|
|
api_clause.Keys.AddRange(clause.security_keys.empty_if_null());
|
|
api_clause.SecurityTypes.AddRange(
|
|
clause.security_types.empty_if_null().Select(t => t.to_api_security_type()));
|
|
api_clause.Names.AddRange(clause.names.empty_if_null());
|
|
return api_clause;
|
|
}
|
|
|
|
public static SecurityCriteriaClause to_clause(
|
|
this bts.api.domain.SecurityCriteriaClause api_clause) =>
|
|
api_clause.to_clause(null);
|
|
|
|
public static SecurityCriteriaClause to_clause(
|
|
this bts.api.domain.SecurityCriteriaClause api_clause, Func<string, uint?>? try_resolve) {
|
|
return new(
|
|
api_clause.Venues.WrapSelectorRO(v => v.to_venue()).ToArray(),
|
|
api_clause.SecurityTypes.WrapSelectorRO(t => t.to_security_type()).ToArray(),
|
|
api_clause.Symbols.ToArray(),
|
|
null,
|
|
api_clause.Keys.ToArray(),
|
|
null,
|
|
null,
|
|
null,
|
|
api_clause.Names.ToArray());
|
|
}
|
|
|
|
public static SecurityCriteriaClause to_clause(
|
|
this bts.api.domain.SecurityCriteriaClauseImmutable api_clause) =>
|
|
new SecurityCriteriaClause(
|
|
api_clause.Venues.Select(v => v.to_venue()).ToArray(),
|
|
api_clause.SecurityTypes.Select(t => t.to_security_type()).ToArray(),
|
|
api_clause.Symbols.ToArray(),
|
|
null,
|
|
api_clause.Keys.ToArray(),
|
|
null,
|
|
null,
|
|
null,
|
|
api_clause.Names.ToArray()
|
|
);
|
|
|
|
public static bts.api.domain.SecurityCriteria to_criteria(this SecurityCriteria criteria) {
|
|
var api_criteria = new bts.api.domain.SecurityCriteria();
|
|
if (criteria is null) {
|
|
return api_criteria;
|
|
}
|
|
api_criteria.Clauses.AddRange(criteria.clauses.empty_if_null().Select(c => c.to_clause()));
|
|
return api_criteria;
|
|
}
|
|
|
|
public static SecurityCriteria to_criteria(
|
|
this bts.api.domain.SecurityCriteria api_criteria) =>
|
|
new(api_criteria.Clauses.Select(to_clause));
|
|
|
|
public static SecurityCriteria to_criteria(
|
|
this bts.api.domain.SecurityCriteriaImmutable api_criteria) =>
|
|
new(api_criteria.Clauses.Select(to_clause));
|
|
|
|
static IEnumerable<Func<SecurityImmutable, bool>> _to_predicates(
|
|
SecurityCriteriaClause clause) {
|
|
if (!clause.exchanges.is_null_or_empty()) {
|
|
yield return sec => clause.exchanges.Contains(sec.Exchange);
|
|
}
|
|
if (!clause.symbols.is_null_or_empty()) {
|
|
yield return sec => clause.symbols.Contains(sec.Symbol);
|
|
}
|
|
if (!clause.security_types.is_null_or_empty()) {
|
|
yield return sec => clause.security_types.Contains(sec.security_type());
|
|
}
|
|
if (!clause.expirations.is_null_or_empty()) {
|
|
yield return sec => clause.expirations.Contains(sec.earliest_expiration());
|
|
}
|
|
if (!clause.security_keys.is_null_or_empty()) {
|
|
yield return sec => clause.security_keys.Contains(sec.Id);
|
|
}
|
|
if (!clause.strikes.is_null_or_empty()) {
|
|
yield return sec => sec.HasOption && clause.strikes.Contains(sec.Option.Strike);
|
|
}
|
|
if (!clause.callputs.is_null_or_empty()) {
|
|
yield return sec => sec.HasOption && clause.callputs.Contains(sec.Option.CallPut);
|
|
}
|
|
if (!clause.names.is_null_or_empty()) {
|
|
yield return sec => clause.names.Contains(sec.ToString());
|
|
}
|
|
}
|
|
}
|
|
}
|