Files
spark/src/client/spark_api/security/SecurityHelper.cs
T

67 lines
3.2 KiB
C#

//using bts.config;
using Google.Protobuf.Collections;
using mbts;
using mbts.config;
using System;
using System.Collections.Generic;
using System.Linq;
using static bts.api.Security;
using FutureLeg = System.Tuple<bts.api.Venue, string, bts.api.FutureContract, int>;
using bts.log;
namespace bts.api
{
internal static class SecurityHelper
{
//static SecurityHelper() {
// try {
// ExpirationDisplayOffset offset_list = Configuration.get<ExpirationDisplayOffset>();
// _offset_map = offset_list.entries.ToDictionary(e => e.security_name, e => e);
// }
// catch (Exception ex) { Logger.error(Subsystem.ui, $"Failed to get Configuration for type {typeof(ExpirationDisplayOffset)}: {ex}"); }
//}
#region Private Members
//private static readonly IDictionary<string, ExpirationDisplayOffsetEntry> _offset_map;
private static IEnumerable<FutureLeg> _get_flat_future_legs(RepeatedField<ContractLeg> legs, int qty) {
foreach (var l in legs) {
switch (l.Security.ContractCase) {
case ContractOneofCase.Future: yield return new FutureLeg(l.Security.Exchange, l.Security.Symbol, l.Security.Future, qty * l.SignedQuantity()); break;
case ContractOneofCase.Spread: foreach (var tup in _get_flat_future_legs(l.Security.Spread.Legs, qty * l.SignedQuantity())) yield return tup; break;
case ContractOneofCase.Synthetic: foreach (var tup in _get_flat_future_legs(l.Security.Synthetic.Legs, qty * l.SignedQuantity())) yield return tup; break;
}
}
}
#endregion
public static IEnumerable<FutureLeg> get_flat_future_legs(Security sec) {
switch (sec.ContractCase) {
case ContractOneofCase.Spread: return _get_flat_future_legs(sec.Spread.Legs, 1);
case ContractOneofCase.Synthetic: return _get_flat_future_legs(sec.Synthetic.Legs, 1);
default: return Enumerable.Empty<FutureLeg>();
}
}
#region Extensions
public static DateTime RawExpDate(this FutureContract future) { return new DateTime((int)future.ExpirationYear, (int)future.ExpirationMonth, (int)future.ExpirationDay); }
// probably do not need MappedExpDates in spark, leaving the method-infrastructure alone just in case
public static DateTime MappedExpDate(this FutureContract future) {
return RawExpDate(future);
//ExpirationDisplayOffsetEntry offset_entry;
//if (_offset_map.TryGetValue(future.Symbol, out offset_entry)) {
// if (offset_entry.addition_type == ExpirationOffsetType.type.Month) return future.RawExpDate().AddMonths(offset_entry.addition_count);
// else if (offset_entry.addition_type == ExpirationOffsetType.type.MonthMoveToStart) {
// DateTime rv = future.RawExpDate().AddMonths(offset_entry.addition_count);
// return new DateTime(rv.Year, rv.Month, 1);
// }
//}
//return future.RawExpDate();
}
public static int SignedQuantity(this ContractLeg leg) { return leg.Side == BuySell.Buy ? (int)leg.Quantity : -(int)leg.Quantity; }
#endregion
}
}