Add fmt_pretty ext fn to SecurityCriteriaClause

This commit is contained in:
John Colvin committed 2021-12-29 00:50:10 -05:00
1 parent f0b8d3d44f
commit 83773d1335
2 files changed
+111 -1

No files matched your search

@@ -1,10 +1,11 @@
using bts.api;
using bts.api;
using mbts.ui.utility;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using System.Linq;
using System.Reactive.Linq;
using System.Threading.Tasks;
@@ -117,6 +118,24 @@ namespace mbts.spark {
(other.callputs.is_null_or_empty() ||
(!clause.callputs.is_null_or_empty() && clause.callputs.All(other.callputs.Contains)));
public static SecurityCriteriaClause with(this SecurityCriteriaClause clause,
Venue[] exchanges = null,
SecurityType[] security_types = null,
string[] symbols = null,
uint[] security_keys = null,
string pretty_name = null,
double[] strikes = null,
PutOrCall[] callputs = null) {
return new SecurityCriteriaClause(
exchanges ?? clause.exchanges,
security_types ?? clause.security_types,
symbols ?? clause.symbols,
security_keys ?? clause.security_keys,
pretty_name ?? clause.pretty_name,
strikes ?? clause.strikes,
callputs ?? clause.callputs);
}
public static IEnumerable<string> fmt_diagnostic_msgs(this SecurityCriteriaClause clause) {
if (clause.exchanges != null && clause.exchanges.Length > 0)
yield return "Exchanges: " + string.Join(", ", clause.exchanges);
@@ -134,6 +153,83 @@ namespace mbts.spark {
}
}
public static string fmt_pretty(this SecurityCriteriaClause clause, Func<uint, string> fmt_sec) =>
clause.fmt_pretty(EnumHelper.all_flags<SecurityCriterionTypes>(), fmt_sec);
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.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;
}
return result;
}
public static string fmt_pretty(this SecurityCriteriaClause clause, SecurityCriterionTypes criterion_types, Func<uint, string> fmt_sec, int max_num_strikes = 4) {
criterion_types &= clause.populated_components();
if (criterion_types.HasFlag(SecurityCriterionTypes.Security) && !clause.security_keys.is_null_or_empty() && fmt_sec != null) {
return string.Join(ui.SpecialCharacters.DotChar, clause.security_keys.Select(fmt_sec));
}
var sb = new StringBuilder();
if (criterion_types.HasFlag(SecurityCriterionTypes.Symbol)) {
sb.Append(string.Join(ui.SpecialCharacters.DotChar, clause.symbols));
}
if (criterion_types.HasFlag(SecurityCriterionTypes.SecurityType)) {
if (sb.Length > 0) {
sb.Append(' ');
}
sb.Append(string.Join(ui.SpecialCharacters.DotChar, clause.security_types));
}
if (criterion_types.HasFlag(SecurityCriterionTypes.Strike)) {
if (sb.Length > 0) {
sb.Append(" K ");
}
if (clause.strikes.Length > max_num_strikes) {
sb.Append($"{clause.strikes.Min()}...{clause.strikes.Max()}");
} else {
sb.Append(string.Join(' ', clause.strikes.Ordered()));
}
}
if (criterion_types.HasFlag(SecurityCriterionTypes.CallPut)) {
if (sb.Length > 0) {
sb.Append(' ');
}
foreach (var cp in clause.callputs) {
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(ui.SpecialCharacters.DotChar, clause.exchanges));
}
return sb.ToString().TrimEnd();
}
static IEnumerable<Func<SecurityImmutable, bool>> _to_predicates(SecurityCriteriaClause clause) {
if (clause.exchanges != null && clause.exchanges.Length > 0)
yield return sec => clause.exchanges.Contains(sec.Exchange);
@@ -0,0 +1,14 @@
namespace mbts.spark {
using System;
[Flags]
enum SecurityCriterionTypes {
None = 0,
Security = 1,
CallPut = 1 << 1,
Strike = 1 << 2,
Symbol = 1 << 3,
SecurityType = 1 << 4,
Exchange = 1 << 5
}
}