Files
spark/src/DataStreamSuite/SharedStreams/StreamFactories/InstrumentSchemaBuilder.cs
T
John Colvin ada7d4bed1 refactor: Load IDataStreamFactory(s) with dependency injection
So we can create ViewStreams that depend on other services, config, etc.
Previously, the PrimitiveManager relied purely on static construction of
types through reflection.
2025-11-14 14:52:07 -05:00

79 lines
3.2 KiB
C#

namespace Bts.SharedStreams.StreamFactories;
using BCG;
using bts.api;
using Bts.DataStreamCore;
public class InstrumentSchemaBuilder {
public bool ShowVenue { get; set; } = true;
public bool ShowAsset { get; set; } = true;
public bool ShowType { get; set; } = true;
public bool ShowName { get; set; } = true;
public bool ShowId { get; set; } = true;
public bool ShowCashPerPoint { get; set; } = true;
public bool ShowLegId { get; set; } = true;
public bool ShowLegVenue { get; set; }
public bool ShowLegAsset { get; set; }
public bool ShowLegType { get; set; }
public bool ShowLegName { get; set; } = true;
public bool ShowLegCashPerPoint { get; set; }
public int NumLegs { get; set; } = 2;
public string TableName { get; set; } = "Instrument";
public BlueTableSchemaAdapter<SecurityImmutable> SecurityAdapter() {
var adapter = new BlueTableSchemaAdapter<SecurityImmutable>(TableName);
adapter.AddColumn("id", r => (int)r.Id);
if (ShowVenue) {
adapter.AddColumn(WellKnownColumns.Venue.Name, b => b.Exchange.ToString());
}
if (ShowType) {
adapter.AddColumn(WellKnownColumns.SecurityType.Name, b => b.security_type().ToString());
}
if (ShowAsset) {
adapter.AddColumn(WellKnownColumns.Asset.Name, b => b.Symbol);
}
if (ShowName) {
adapter.AddColumn(WellKnownColumns.SecurityName.Name, b => b.ToString());
}
if (ShowId) {
adapter.AddColumn(WellKnownColumns.SecurityId.Name, b => b.Id);
}
if (ShowCashPerPoint) {
adapter.AddColumn(WellKnownColumns.CashPerPoint.Name, b => b.ContractSpec.CashPerPoint);
}
for (int i = 0; i < NumLegs; i++) {
if (ShowLegVenue) {
adapter.AddColumn(WellKnownColumns.LegVenue(i).Name, Get(l => l.Security.Exchange.ToString(), null));
}
if (ShowLegType) {
adapter.AddColumn(WellKnownColumns.LegSecurityType(i).Name, Get(l => l.Security.security_type().ToString(), null));
}
if (ShowLegAsset) {
adapter.AddColumn(WellKnownColumns.LegAsset(i).Name, Get(l => l.Security.Symbol, null));
}
if (ShowLegName) {
adapter.AddColumn(WellKnownColumns.LegSecurityName(i).Name, Get(l => l.Security.ToString(), null));
}
if (ShowLegId) {
adapter.AddColumn(WellKnownColumns.LegSecurityId(i).Name, Get(l => (int)l.Security.Id, 0));
}
if (ShowLegCashPerPoint) {
adapter.AddColumn(WellKnownColumns.LegCashPerPoint(i).Name, Get(l => l.Security.ContractSpec.CashPerPoint, double.NaN));
}
Func<SecurityImmutable, T> Get<T>(Func<ContractLegImmutable, T> getValue, T defaultValue) =>
s => {
ContractLegImmutable? leg = s.ContractCase switch {
Security.ContractOneofCase.Spread => s.Spread.Legs.Skip(i).TryGetFirst(out var l) ? l : null,
Security.ContractOneofCase.Synthetic => s.Synthetic.Legs.Skip(i).TryGetFirst(out var l) ? l : null,
_ => null,
};
return leg.HasValue ? getValue(leg.Value) : defaultValue;
};
}
return adapter;
}
}