Files
spark/src/DataStreamSuite/SharedStreams/StreamFactories/SpreadBookSchemaBuilder.cs
T

79 lines
3.2 KiB
C#

namespace Bts.SharedStreams.StreamFactories;
using bts.api;
using bts.collections;
using Bts.DataStreamCore;
public class SpreadBookSchemaBuilder {
public int NumLegs { get; set; } = 2;
public int NumLevels { get; set; } = 3;
public bool ShowPrice { get; set; } = true;
public bool ShowQuantity { get; set; } = true;
public bool ShowNotional { get; set; }
public bool ShowVenue { get; set; }
public bool ShowAsset { get; set; }
public bool ShowSecurityName { get; set; } = true;
public bool ShowLegVenue { get; set; }
public bool ShowLegAsset { get; set; }
public bool ShowLegSecurityType { get; set; }
public bool ShowLegSecurityName { get; set; } = true;
public bool ShowLegSecurityId { get; set; } = true;
public bool ShowLegCashPerPoint { get; set; }
public bool ShowTimestamp { get; set; } = false;
public string TableName { get; set; } = "SpreadBook";
public BlueTableSchemaAdapter<SpreadBookRow> SpreadBookRowAdapter() {
var adapter = new BlueTableSchemaAdapter<SpreadBookRow>(TableName, r => (int)r.SecurityId);
if (ShowSecurityName) {
adapter.AddColumn(WellKnownColumns.SecurityName.Name, r => r.SecurityName);
}
if (ShowTimestamp) {
adapter.AddColumn(WellKnownColumns.Timestamp.Name, r => DateTime.MinValue);
}
for (int i = 0; i < NumLegs; i++) {
if (ShowLegVenue) {
adapter.AddColumn(WellKnownColumns.LegVenue(i).Name, r => (GetLeg(r)?.Venue)?.ToString() ?? string.Empty);
}
if (ShowLegSecurityType) {
adapter.AddColumn(WellKnownColumns.LegSecurityType(i).Name, r => (GetLeg(r)?.Type)?.ToString() ?? string.Empty);
}
if (ShowLegSecurityName) {
adapter.AddColumn(WellKnownColumns.LegSecurityName(i).Name, r => GetLeg(r)?.Name ?? string.Empty);
}
if (ShowLegSecurityId) {
adapter.AddColumn(WellKnownColumns.LegSecurityId(i).Name, r => GetLeg(r)?.Id ?? 0);
}
if (ShowLegCashPerPoint) {
adapter.AddColumn(WellKnownColumns.LegCashPerPoint(i).Name, r => GetLeg(r)?.CashPerPoint ?? 1);
}
SpreadBookRow.Leg? GetLeg(SpreadBookRow r) =>
r.Legs.as_mutable().TryGetAt(i, out var l) ? l : null;
}
foreach (var side in new[] { Side.Buy, Side.Sell }) {
for (int i = 0; i < NumLevels; i++) {
if (ShowPrice) {
adapter.AddColumn(WellKnownColumns.DepthPrice(side, i).Name, priceGetter(side, i));
}
if (ShowQuantity) {
adapter.AddColumn(WellKnownColumns.DepthQuantity(side, i).Name, quantityGetter(side, i));
}
if (ShowNotional) {
adapter.AddColumn(WellKnownColumns.DepthNotional(side, i).Name, notionalGetter(side, i));
}
}
}
return adapter;
static Func<SpreadBookRow, double> priceGetter(Side side, int level) =>
b => b.Book[side, level].Price;
static Func<SpreadBookRow, double> quantityGetter(Side side, int level) =>
b => b.Book[side, level].Quantity;
static Func<SpreadBookRow, double> notionalGetter(Side side, int level) =>
b => b.Book[side, level].NotionalValue;
}
}