83 lines
3.6 KiB
C#
83 lines
3.6 KiB
C#
using System.Collections.Concurrent;
|
|
using System.Collections.Generic;
|
|
using static bts.api.Security;
|
|
|
|
namespace bts.api {
|
|
public struct FutureLeg {
|
|
public readonly Venue exchange;
|
|
public readonly string symbol, exchange_desc;
|
|
public readonly FutureContractImmutable future;
|
|
public readonly int quantity;
|
|
|
|
public FutureLeg(Venue exchange, string symbol, string exchange_desc, FutureContractImmutable future, int quantity) {
|
|
this.exchange = exchange;
|
|
this.symbol = symbol;
|
|
this.exchange_desc = exchange_desc;
|
|
this.future = future;
|
|
this.quantity = quantity;
|
|
}
|
|
}
|
|
|
|
static class SecurityFormatter {
|
|
static readonly ConcurrentDictionary<uint, string> _formatted_names_cache = new ConcurrentDictionary<uint, string>();
|
|
|
|
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 = SecurityHelper.get_flat_future_legs(legs);
|
|
return string.Join("", _format_future_legs(future_legs));
|
|
}
|
|
|
|
public static string format_future(string symbol, string exch_desc, FutureContractImmutable future) =>
|
|
string.IsNullOrEmpty(exch_desc) ?
|
|
$"{symbol}{DateFormatter.Format(future.ExpirationYear, future.ExpirationMonth, future.ExpirationDay, DateFormat.MonthCodeLastDigitYear)}" :
|
|
exch_desc;
|
|
|
|
static string _format(SecurityImmutable? security) {
|
|
switch (security?.ContractCase) {
|
|
case ContractOneofCase.Future: return format_future(security?.Symbol, security?.security_desc(), security.Value.Future);
|
|
case ContractOneofCase.Spread:
|
|
if (security?.HasGlbx == true) {
|
|
// 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;
|
|
}
|
|
return string.Join("", _format_future_legs(SecurityHelper.get_flat_future_legs(security?.Spread.Legs)));
|
|
case ContractOneofCase.Synthetic: return security?.Synthetic.Name;
|
|
default: return "Unknown-security";
|
|
}
|
|
}
|
|
|
|
static IEnumerable<string> _format_future_legs(IEnumerable<FutureLeg> future_legs) {
|
|
using (var en = future_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(FutureLeg fl) {
|
|
string sym_exp_name = format_future(fl.symbol, fl.exchange_desc, fl.future);
|
|
switch (fl.quantity) {
|
|
case 1: return sym_exp_name;
|
|
case -1: return $"-{sym_exp_name}";
|
|
default: return fl.quantity > 0 ? $"{fl.quantity}{sym_exp_name}" : $"{fl.quantity:-0}{sym_exp_name}";
|
|
}
|
|
}
|
|
|
|
static string _get_subsequent_leg_name(FutureLeg fl) {
|
|
string sym_exp_name = format_future(fl.symbol, fl.exchange_desc, fl.future);
|
|
switch (fl.quantity) {
|
|
case 1: return $"+{sym_exp_name}";
|
|
case -1: return $"-{sym_exp_name}";
|
|
default: return $"{fl.quantity:+0;-0}{sym_exp_name}";
|
|
}
|
|
}
|
|
}
|
|
} |