61 lines
2.6 KiB
C#
61 lines
2.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using static bts.api.Security;
|
|
using security_key = System.UInt32;
|
|
|
|
namespace bts.api {
|
|
public partial class Security {
|
|
public static security_key key(Security s) { return s.Id; }
|
|
}
|
|
|
|
partial struct SecurityImmutable : IComparable<SecurityImmutable>, IComparable {
|
|
public int CompareTo(object obj) =>
|
|
obj is SecurityImmutable ? CompareTo((SecurityImmutable)obj) : -1;
|
|
|
|
public int CompareTo(SecurityImmutable other) =>
|
|
SecurityHelper.Comparer.Compare(this, other);
|
|
|
|
partial void ToStringOverride(ref bool didrun, ref string result) {
|
|
result = SecurityFormatter.format(this);
|
|
didrun = true;
|
|
}
|
|
|
|
partial void EqualsOverride(ref bool didrun, ref bool result, SecurityImmutable other) {
|
|
didrun = true;
|
|
result = Id == other.Id;
|
|
}
|
|
|
|
public SecurityType security_type() {
|
|
switch (ContractCase) {
|
|
case ContractOneofCase.Future: return SecurityType.Future;
|
|
case ContractOneofCase.Option: return SecurityType.Option;
|
|
case ContractOneofCase.Spread: return SecurityType.Spread;
|
|
case ContractOneofCase.Synthetic: return SecurityType.Synthetic;
|
|
default: return SecurityType.Unset;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Traverses the security to get the leaf
|
|
/// exchange securities
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
internal IEnumerable<(security_key, Side, double mult)> get_base_exchange_securities () {
|
|
List<(uint, Side, double)> rv = new List<(uint,Side,double)> ();
|
|
get_base_exchange_securities (this, Side.Buy, 1.0, ref rv);
|
|
return rv;
|
|
}
|
|
|
|
static void get_base_exchange_securities (SecurityImmutable sec, Side side, double mult, ref List<(security_key, Side, double)> legs) {
|
|
Func<Side, Side, Side> resolve_side = (s1, s2) => ((s1 == Side.Buy ? 1 : -1) * (s2 == Side.Buy ? 1 : -1)) == 1 ? Side.Buy : Side.Sell;
|
|
if (sec.ContractCase == ContractOneofCase.Future)
|
|
legs.Add ((sec.Id, side, mult));
|
|
else if (sec.ContractCase == ContractOneofCase.Spread)
|
|
foreach (ContractLegImmutable leg in sec.Spread.Legs)
|
|
get_base_exchange_securities (leg.Security, resolve_side (side, leg.Side), leg.Quantity * mult, ref legs);
|
|
else if (sec.ContractCase == ContractOneofCase.Synthetic)
|
|
foreach (ContractLegImmutable leg in sec.Synthetic.Legs)
|
|
get_base_exchange_securities (leg.Security, resolve_side (side, leg.Side), leg.Quantity * mult, ref legs);
|
|
}
|
|
}
|
|
} |