refactor: Consolidate format_expiration fns into single fn

This commit is contained in:
John Colvin committed 2022-08-07 13:51:54 -04:00
1 parent bb068cf6b2
commit 1faef425ab
4 files changed
+12 -43

No files matched your search

@@ -41,8 +41,7 @@ namespace mbts.analytics {
}
var exp_str = col_key is null ?
"(NULL)" :
SecurityFormatter.format_expiration(
Security.ContractOneofCase.Option, col_key.symbol, col_key.expiration);
SecurityFormatter.format_expiration(col_key.expiration, symbol: col_key.symbol);
Log.Ui.warn($"ComboFutureRow does not have data for the requested expiration: {exp_str}");
return null;
}
@@ -50,8 +50,7 @@ namespace mbts.analytics {
return cell;
}
var exp_str = col_key == null ?
"(NULL)" : SecurityFormatter.format_expiration(
Security.ContractOneofCase.Option, col_key.symbol, col_key.expiration);
"(NULL)" : SecurityFormatter.format_expiration(col_key.expiration, col_key.symbol);
Log.Ui.warn($"ComboOptionRow does not have data for the requested expiration: {exp_str}.");
return null;
}
@@ -29,8 +29,7 @@
if (item is not SecurityExpiration exp) {
return string.Empty;
}
return SecurityFormatter.format_expiration(
exp.security_type.contract_case(), exp.symbol, exp.expiration);
return SecurityFormatter.format_expiration(exp.expiration, exp.symbol);
}
}
}
@@ -95,49 +95,23 @@
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);
#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(
Security.ContractOneofCase sec_type,
string symbol,
DateTime exp_date,
bool show_type,
bool show_symbol,
bool use_mapped_exp,
string? symbol = null,
Security.ContractOneofCase? type = null,
DateFormat date_format = DateFormat.DayMonthAbvDiff2000) {
string result = DateFormatter.Format(exp_date, date_format);
if (show_symbol) {
var result = DateFormatter.Format(exp_date, date_format);
if (symbol is not null) {
result = symbol + " " + result;
}
if (show_type) {
result = format_security_type(sec_type) + " " + result;
if (type.HasValue) {
result = format_security_type(type.Value) + " " + result;
}
return result;
}
@@ -145,11 +119,9 @@
/// <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, double size, double marginal_price) {
var exp_str = format_expiration(
Security.ContractOneofCase.Future, symbol, exp_date, false, true, true);
var exp_str = format_expiration(exp_date, symbol: symbol);
return $"{exp_str} {size} lots @ {marginal_price:C}";
}
#nullable disable
@@ -165,7 +137,7 @@
double size,
double marginal_price
) {
var exp_str = format_expiration(Security.ContractOneofCase.Option, symbol, exp_date, false, true, true);
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}";
}