180 lines
7.9 KiB
C#
180 lines
7.9 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}{format_expiration(new((int)exp_year, (int)exp_month, (int)exp_day))}";
|
|
|
|
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.ExpirationDay, 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-security1";
|
|
|
|
static string _format(SecurityImmutable security, bool exchange_specific_formatting = true) {
|
|
switch (security.ContractCase) {
|
|
case Security.ContractOneofCase.Future:
|
|
if (exchange_specific_formatting && security.HasIce) {
|
|
return ExchangeSpecificUtilities.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) {
|
|
switch (security.ExchangeDataCase) {
|
|
case Security.ExchangeDataOneofCase.Glbx: return security.Glbx.SecurityDesc;
|
|
case Security.ExchangeDataOneofCase.Ice: return ExchangeSpecificUtilities.format_security(security);
|
|
case Security.ExchangeDataOneofCase.Deribit: return security.Deribit.SecurityDesc;
|
|
}
|
|
}
|
|
return string.Join("", _format_legs(new ContractLegEnumerable(security.Spread.Legs, 1)));
|
|
case Security.ContractOneofCase.Synthetic:
|
|
return security.Synthetic.Name;
|
|
case Security.ContractOneofCase.Spot:
|
|
return security.security_desc();
|
|
default:
|
|
return "Unknown-security2";
|
|
}
|
|
}
|
|
|
|
/// <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];
|
|
|
|
#nullable enable
|
|
/// <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(
|
|
DateTime exp_date,
|
|
string? symbol = null,
|
|
Security.ContractOneofCase? type = null,
|
|
DateFormat date_format = DateFormat.DayMonthAbvDiff2000) {
|
|
var result = Security.is_perpetual_exp(exp_date) ?
|
|
"Perp" : DateFormatter.Format(exp_date, date_format);
|
|
if (symbol is not null) {
|
|
result = symbol + " " + result;
|
|
}
|
|
if (type.HasValue) {
|
|
if (result.Length == 0) {
|
|
result = format_security_type(type.Value);
|
|
} else {
|
|
result = $"[{format_security_type(type.Value)}] {result}";
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns a concise string representation of a future trade appropriate for UIs.
|
|
/// </summary>
|
|
public static string format_future_trade(
|
|
string symbol, DateTime exp_date, double size, double marginal_price) {
|
|
var exp_str = format_expiration(exp_date, symbol: symbol);
|
|
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,
|
|
double size,
|
|
double marginal_price
|
|
) {
|
|
var exp_str = format_expiration(exp_date, symbol: symbol);
|
|
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}",
|
|
_ => $"{leg.signed_quantity()}{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}",
|
|
};
|
|
}
|
|
}
|
|
}
|