203 lines
8.8 KiB
C#
203 lines
8.8 KiB
C#
namespace bts.api {
|
|
using System;
|
|
using System.Collections.Concurrent;
|
|
using System.Collections.Generic;
|
|
|
|
public static class SecurityFormatter {
|
|
static readonly ConcurrentDictionary<uint, string> _formatted_names_cache = new();
|
|
|
|
public static string format(Security security) =>
|
|
_formatted_names_cache.GetOrAdd(security.Id, _format(security));
|
|
|
|
public static string format(SecurityImmutable security) =>
|
|
_formatted_names_cache.GetOrAdd(security.Id, _ => _format(security));
|
|
|
|
public static string format_contract_legs(IEnumerable<ContractLegImmutable> legs) {
|
|
var future_legs = new ContractLegEnumerable(legs, 1);
|
|
return string.Join("", _format_legs(future_legs));
|
|
}
|
|
|
|
public static string format_future(string symbol, DateTime exp) =>
|
|
format_future(symbol, (uint)exp.Year, (uint)exp.Month, (uint)exp.Day);
|
|
|
|
public static string format_future(string symbol, uint exp_year, uint exp_month, uint exp_day) =>
|
|
$"{symbol}{DateFormatter.Format(exp_year, exp_month, exp_day, DateFormat.DayMonthAbvDiff2000)}";
|
|
|
|
public static string format_future(string symbol, FutureContractImmutable future) =>
|
|
format_future(symbol, future.ExpirationYear, future.ExpirationMonth, future.ExpirationDay);
|
|
|
|
public static string format_future(string symbol, string exch_desc, FutureContractImmutable future) =>
|
|
string.IsNullOrEmpty(exch_desc) ? format_future(symbol, future) : exch_desc;
|
|
|
|
public static string format_option(string symbol, uint exp_year, uint exp_month, uint exp_day, double strike, PutOrCall call_put) {
|
|
var cp = call_put switch {
|
|
PutOrCall.Call => 'C',
|
|
PutOrCall.Put => 'P',
|
|
_ => '?'
|
|
};
|
|
return $"{format_future(symbol, exp_year, exp_month, exp_day)} {strike}{cp}";
|
|
}
|
|
|
|
public static string format_option(string symbol, OptionContractImmutable option) =>
|
|
format_option(symbol, option.ExpirationYear, option.ExpirationMonth, option.ExpirationYear, option.Strike, option.CallPut);
|
|
|
|
public static string format_option(string symbol, string exch_desc, OptionContractImmutable option) =>
|
|
string.IsNullOrEmpty(exch_desc) ? format_option(symbol, option) : exch_desc;
|
|
|
|
static string _format(SecurityImmutable? security) =>
|
|
security.HasValue ? _format(security.Value) : "Unknown-security";
|
|
|
|
static string _format(SecurityImmutable security, bool exchange_specific_formatting = true) {
|
|
switch (security.ContractCase) {
|
|
case Security.ContractOneofCase.Future:
|
|
if (exchange_specific_formatting && security.HasIce) {
|
|
return ExchangeSpecificUitlities.format_security(security);
|
|
}
|
|
return format_future(security.Symbol, security.security_desc(), security.Future);
|
|
case Security.ContractOneofCase.Option:
|
|
return format_option(security.Symbol, security.security_desc(), security.Option);
|
|
case Security.ContractOneofCase.Spread:
|
|
if (exchange_specific_formatting) {
|
|
if (security.HasGlbx) {
|
|
// In case we ever bring it back, pretty printed spread names can be found here:
|
|
// GlbxSecurityFormatter.FormatSpread(security, out string result)
|
|
return security.Glbx.SecurityDesc;
|
|
} else if (security.HasIce) {
|
|
return ExchangeSpecificUitlities.format_security(security);
|
|
}
|
|
}
|
|
return string.Join("", _format_legs(new ContractLegEnumerable(security.Spread.Legs, 1)));
|
|
case Security.ContractOneofCase.Synthetic:
|
|
return security.Synthetic.Name;
|
|
default:
|
|
return "Unknown-security";
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns a shorthand string representation of a security type appropriate for UIs.
|
|
/// </summary>
|
|
public static string format_security_type(SecurityType type) =>
|
|
format_security_type(type.contract_case());
|
|
|
|
public static string format_security_type(Security.ContractOneofCase type) =>
|
|
type switch {
|
|
Security.ContractOneofCase.Future => "FUT",
|
|
Security.ContractOneofCase.Option => "OPT",
|
|
Security.ContractOneofCase.Spread => "SPD",
|
|
Security.ContractOneofCase.Synthetic => "SYN",
|
|
_ => type.ToString(),
|
|
};
|
|
|
|
/// <summary>
|
|
/// Returns a shorthand string representation of a call or put appropriate for UIs (i.e. C or P).
|
|
/// </summary>
|
|
public static string format_call_put(PutOrCall call_put) =>
|
|
call_put.ToString()[..1];
|
|
|
|
/// <summary>
|
|
/// Returns a concise string representation of a security expiration appropriate for UIs (e.g.
|
|
/// es March 22, 2019: es 22 Mar 19).
|
|
/// </summary>
|
|
public static string format_expiration(
|
|
Security.ContractOneofCase sec_type, string symbol, DateTime exp_date) =>
|
|
format_expiration(
|
|
sec_type, symbol, exp_date, false, true, true, DateFormat.DayMonthAbvDiff2000);
|
|
|
|
/// <summary>
|
|
/// Returns a concise string representation of a security expiration (using symbol-mapped
|
|
/// expiration dates, and single-letter month codes) appropriate for UIs (e.g. es March 22,
|
|
/// 2019: [Opt] [es] 22 Mar 19).
|
|
/// </summary>
|
|
public static string format_expiration(
|
|
Security.ContractOneofCase sec_type,
|
|
string symbol,
|
|
DateTime exp_date,
|
|
bool show_type,
|
|
bool show_symbol,
|
|
bool use_mapped_exp) =>
|
|
format_expiration(
|
|
sec_type, symbol, exp_date, show_type, show_symbol, use_mapped_exp, DateFormat.DayMonthAbvDiff2000);
|
|
|
|
/// <summary>
|
|
/// Returns a concise string representation of a security expiration (using symbol-mapped
|
|
/// expiration dates, and single-letter month codes) appropriate for UIs (e.g. es March 22,
|
|
/// 2019: [Opt] [es] 22 Mar 19, or [Opt] [es] 22 H19).
|
|
/// </summary>
|
|
public static string format_expiration(
|
|
Security.ContractOneofCase sec_type,
|
|
string symbol,
|
|
DateTime exp_date,
|
|
bool show_type,
|
|
bool show_symbol,
|
|
bool use_mapped_exp,
|
|
DateFormat date_format = DateFormat.DayMonthAbvDiff2000) {
|
|
string result = DateFormatter.Format(exp_date, date_format);
|
|
if (show_symbol) {
|
|
result = symbol + " " + result;
|
|
}
|
|
if (show_type) {
|
|
result = format_security_type(sec_type) + " " + result;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns a concise string representation of a future trade appropriate for UIs.
|
|
/// </summary>
|
|
#nullable enable
|
|
public static string format_future_trade(
|
|
string symbol, DateTime exp_date, int size, double marginal_price) {
|
|
var exp_str = format_expiration(
|
|
Security.ContractOneofCase.Future, symbol, exp_date, false, true, true);
|
|
return $"{exp_str} {size} lots @ {marginal_price:C}";
|
|
}
|
|
#nullable disable
|
|
|
|
/// <summary>
|
|
/// Returns a concise string representation of an option trade appropriate for UIs.
|
|
/// </summary>
|
|
public static string format_option_trade(
|
|
string symbol,
|
|
DateTime exp_date,
|
|
double strike,
|
|
bool is_call,
|
|
int size,
|
|
double marginal_price
|
|
) {
|
|
var exp_str = format_expiration(Security.ContractOneofCase.Option, symbol, exp_date, false, true, true);
|
|
var cp_str = is_call ? "C" : "P";
|
|
return $"{exp_str} {strike} {cp_str} {size} lots @ {marginal_price:C}";
|
|
}
|
|
|
|
static IEnumerable<string> _format_legs(IEnumerable<ContractLegImmutable> legs) {
|
|
using var en = legs.GetEnumerator();
|
|
if (en.MoveNext()) {
|
|
yield return _get_first_leg_name(en.Current);
|
|
while (en.MoveNext())
|
|
yield return _get_subsequent_leg_name(en.Current);
|
|
}
|
|
}
|
|
|
|
static string _get_first_leg_name(ContractLegImmutable leg) {
|
|
string sec_str = leg.HasSecurity ?
|
|
_format(leg.Security, false) : "NoSecurity";
|
|
return leg.signed_quantity() switch {
|
|
1.0 => sec_str,
|
|
-1.0 => $"-{sec_str}",
|
|
_ => ""
|
|
};
|
|
}
|
|
|
|
static string _get_subsequent_leg_name(ContractLegImmutable leg) {
|
|
string sec_str = leg.HasSecurity ?
|
|
_format(leg.Security, false) : "NoSecurity";
|
|
return leg.signed_quantity() switch {
|
|
1.0 => $"+{sec_str}",
|
|
-1.0 => $"-{sec_str}",
|
|
_ => $"{leg.signed_quantity():+0;-0}{sec_str}",
|
|
};
|
|
}
|
|
}
|
|
}
|