Files
spark/src/DataStreamSuite/SharedStreams/StreamFactories/BookStreamFactory.cs
T
2025-11-18 11:56:46 -05:00

204 lines
8.7 KiB
C#

namespace Bts.SharedStreams.StreamFactories;
using BCG;
using bts.BlueOrb;
using bts.collections;
using Bts.DataStream;
using Bts.DataStreamCore;
using Bts.DataStreamCore.StreamFactories;
using Bts.PubSub;
using Microsoft.Extensions.Logging;
using Spark.Api.MarketData;
using Spark.Core.Reactive;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Reactive.Linq;
public class BookStreamFactory : DataStreamFactory, IRecursiveDataStreamFactory {
readonly LiveBboStreamFactory liveBboFactory;
readonly IMarketDataClient? mdClient;
readonly ILogger logger;
readonly IWorkScheduler workScheduler;
IDataStreamFactory? compositeFactory = null;
public BookStreamFactory(IMarketDataClient? mdClient, ILogger<BookStreamFactory> logger, ViewStreamDatabase workScheduler) {
this.mdClient = mdClient;
this.logger = logger;
this.workScheduler = workScheduler;
this.Add<BookStreamParams>(new[] { "book" }, CreateBook, logger);
this.Add<BookStreamParams>(new[] { "bbo" }, CreateBbo, logger);
this.Add<BookStreamParams>(new[] { "bboPriceSummary" }, createBboPriceSummary, logger);
liveBboFactory = new LiveBboStreamFactory(mdClient, logger);
Add(liveBboFactory);
ViewStream? CreateBook(BookStreamParams streamParams) {
if (!checkHasLiveMd()) {
return null;
}
return this.mdClient.Books(streamParams.GetSecurityFilter())
.AsDataPackets(streamParams.GetSchemaBuilder().BookTupleAdapter())
.AsViewStream()
.TimeSeries(streamParams.BufferSize, WellKnownColumnNames.Timestamp);
}
ViewStream? CreateBbo(BookStreamParams streamParams) {
if (!checkHasLiveMd()) {
return null;
}
return this.mdClient.Books(streamParams.GetSecurityFilter())
.AsDataPackets(streamParams.GetSchemaBuilder().BboAdapter())
.AsViewStream()
.TimeSeries(streamParams.BufferSize, WellKnownColumnNames.Timestamp);
}
}
public void OnCompositeFactory(IDataStreamFactory compositeFactory) {
this.compositeFactory = compositeFactory;
liveBboFactory.OnCompositeFactory(compositeFactory);
}
ViewStream? createBboPriceSummary(BookStreamParams streamParams) {
if (!checkHasLiveMd()) {
return null;
}
var secCriteria = streamParams.GetSecurityFilter();
mdClient.Books(secCriteria).WithHistory().Select(t => t.Table);
// var bboStream = mdClient.Books(secCriteria).AsDataPackets(streamParams.GetSchemaBuilder().BboAdapter());
var bboStream = liveBboFactory.Create(streamParams)
?? throw new ArgumentException("Failed to create bbo stream", nameof(streamParams));
var bboTimeSeriesStream = new TimeSeriesStream(bboStream, streamParams.BufferSize, WellKnownColumnNames.Timestamp);
ViewStream? tradeStream = null;
if (streamParams.Asset == "ES" && compositeFactory is not null) {
tradeStream = compositeFactory.Create("ReplayFutureLiveTradeStream", new (string?, int?, object?)[] { ("contract", 0, streamParams.Asset) });
if (tradeStream is null) {
logger.LogError("Failed to create trade stream for bboPriceSummary stream");
return null;
}
tradeStream = tradeStream.RenameColumns(true, ("trade_price", WellKnownColumnNames.Price), ("trade_size", WellKnownColumnNames.Quantity));
} else {
var tradeSchemaBd = new TradeSchemaBuilder();
var tradeSchema = tradeSchemaBd.ToSchema();
var tradeAdapter = tradeSchemaBd.TradeInfoAdapter();
tradeStream = LiveTradeStream.TradeStream(mdClient.Trades(secCriteria), tradeSchema, tradeAdapter).AsViewStream();
}
var bboPriceSummaryStream = new BboPriceSummaryStream(bboStream.AsObservable(), tradeStream.AsObservable(), workScheduler, TimeSpan.FromSeconds(1));
return bboPriceSummaryStream;
}
static bool checkHasLiveMd([NotNullWhen(true)] IMarketDataClient? mdClient, ILogger? logger = null) {
if (mdClient is not null) {
return true;
}
logger?.LogError("Market data is unavailable (md client is null)");
return false;
}
[MemberNotNullWhen(true, nameof(mdClient))]
bool checkHasLiveMd() =>
checkHasLiveMd(mdClient, logger);
class LiveBboStreamFactory : IDataStreamFactory, IRecursiveDataStreamFactory {
static readonly IDataStreamMetadata metadata = new DataStreamMetadata() {
Name = "LiveBBOStream",
AlternateNames = new(),
Signatures = new() {
new (string?, Type?)[] {
("symbol", typeof(string))
},
new (string?, Type?)[] {
("secType", typeof(string)),
("symbol", typeof(string)),
}
}
};
readonly IMarketDataClient? mdClient;
readonly ILogger logger;
IDataStreamFactory? compositeFactory = null;
public LiveBboStreamFactory(IMarketDataClient? mdClient, ILogger logger) {
this.mdClient = mdClient;
this.logger = logger;
}
public void OnCompositeFactory(IDataStreamFactory compositeFactory) =>
this.compositeFactory = compositeFactory;
public ViewStream? Create(string streamId, IEnumerable<StreamArgument> args) {
if (!metadata.IsMatch(streamId)) {
return null;
}
var argList = args.ToList();
switch (argList.Count) {
case 1: {
var sym = argList[0].Value?.ToString() ?? throw new ArgumentException("Symbol arg is null or empty", nameof(args));
if (sym == "ES" && compositeFactory is not null) {
return compositeFactory.Create("ReplayFutureLiveBBOStream", new (string?, int?, object?)[] { ("contract", 0, sym) });
}
var streamParams = new BookStreamParams() {
Asset = sym
};
return mdClient?.Books(streamParams.GetSecurityFilter())
?.AsDataPackets(streamParams.GetSchemaBuilder().BboAdapter())
?.AsViewStream()
?? throw new InvalidOperationException("Live md not available");
}
case 2: {
var secTypeStr = argList[0].Value?.ToString() ?? string.Empty;
if (!MarketDataArgStore.TryParseSecurityType(secTypeStr, out var secType)) {
throw new ArgumentException("Failed to parse SecurityType arg", nameof(args));
}
var sym = argList[1].Value?.ToString() ?? throw new ArgumentException("Symbol arg is null or empty", nameof(args));
if (sym == "ES" && secType == bts.api.SecurityType.Future && compositeFactory is not null) {
return compositeFactory.Create("ReplayFutureLiveBBOStream", new (string?, int?, object?)[] { ("contract", 0, sym) });
}
var streamParams = new BookStreamParams() {
Type = secType,
Asset = sym,
};
return mdClient?.Books(streamParams.GetSecurityFilter())
?.AsDataPackets(streamParams.GetSchemaBuilder().BboAdapter())
?.AsViewStream()
?? throw new InvalidOperationException("Live md not available");
}
default:
return null;
}
}
public ViewStream? Create(BookStreamParams streamParams) {
var secType = streamParams.Type;
if (streamParams.Asset == "ES" && (!secType.HasValue || secType.Value == bts.api.SecurityType.Future) && compositeFactory is not null) {
return compositeFactory.Create("ReplayFutureLiveBBOStream", new (string?, int?, object?)[] { ("contract", 0, streamParams.Asset) });
}
if (!checkHasLiveMd()) {
return null;
}
return mdClient.Books(streamParams.GetSecurityFilter())
.AsDataPackets(streamParams.GetSchemaBuilder().BboAdapter())
.AsViewStream();
}
public IEnumerable<IDataStreamMetadata> GetSupportedStreams() {
yield return metadata;
}
[MemberNotNullWhen(true, nameof(mdClient))]
bool checkHasLiveMd() =>
BookStreamFactory.checkHasLiveMd(mdClient, logger);
}
public class LiveBboStreamParams {
[StreamArgument(RegexNames = new[] { WellKnownStreamArgumentAliases.SecurityTypeRegex }, Position = 0, IsRequired = false)]
public string? SecurityType { get; set; }
[StreamArgument(RegexNames = new[] { WellKnownStreamArgumentAliases.AssetRegex }, Position = 1, IsRequired = false)]
public string? ContinuousContract { get; set; }
}
}