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

122 lines
4.0 KiB
C#

namespace bts.SilverPine;
using BCG;
using bts.api;
using bts.api.core;
using bts.SilverPine.StreamFactory.Logging;
using bts.SilverPine.Tables;
using Bts.DataStream;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System.Collections.Generic;
using System.Reactive.Linq;
public class MboStreamFactory : IDataStreamFactory {
readonly DatabaseConfig config;
ILogger logger;
public MboStreamFactory(IOptions<DatabaseConfig> config, ILogger<BboStreamFactory> logger)
: this(config.Value, logger) { }
public MboStreamFactory(DatabaseConfig config, ILogger<BboStreamFactory> logger) {
this.config = config;
this.logger = logger;
}
public static IObservable<TableUpdate<MboRow, int>>? CreateTables(
DatabaseConfig config, string venue, string symbol, DateTime beginDate, DateTime endDate) {
return new HistoricDataLoader(config, null)
.LoadRowsAsync(new HistoricDataParams {
DataType = "mbo",
Venue = venue,
Symbol = symbol,
BeginDate = beginDate,
EndDate = endDate,
TsNanosCol = "ts_recv_ns"})
.ToObservable()
.Scan(TableUpdate<MboRow, int>.mk_empty(r => r.Id), AddPage)
.Where(t => t.has_changes());
TableUpdate<MboRow, int> AddPage(TableUpdate<MboRow, int> res, IBlueRowCollection rows) {
var bd = res.to_builder();
int i = res.state.Count;
foreach (IBlueRow br in rows) {
var sendTimestampNanos = br.get<long>("ts_event_ns");
var recvTimestampNanos = br.get<long>("ts_recv_ns");
var sideStr = br.get<string>("side");
var side = sideStr[0] switch {
'A' => Side.Sell,
'B' => Side.Buy,
'N' => Side.Undefined,
_ => throw new InvalidOperationException($"Unsupported side '{sideStr}'"),
};
var r = new MboRow() {
Id = i++,
SendTimestamp = DateTime.UnixEpoch.Add(new TimeSpan(sendTimestampNanos / 100)),
Timestamp = DateTime.UnixEpoch.Add(new TimeSpan(recvTimestampNanos / 100)),
SequenceNumber = br.get<int>("sequence"),
Venue = venue,
Symbol = symbol,
Contract = symbol,
Side = side,
Price = br.get("price", double.NaN),
Quantity = br.get<double>("size"),
OrderId = br.get<int>("order_id"),
Action = br.get<string>("action")
};
bd.add_or_update(r);
}
return bd.to_table();
}
}
public ViewStream? Create(string streamId, IEnumerable<(string? Name, int? Index, object? Value)> args) {
if (SupportedStreams.Mbo.IsMatch(streamId)) {
return createMbo(args);
}
return null;
}
public IEnumerable<IDataStreamMetadata> GetSupportedStreams() {
yield return SupportedStreams.Mbo;
}
ViewStream? createMbo(IEnumerable<(string? Name, int? Index, object? Value)> args) {
var argStore = new StreamFactoryArgStore(args);
var venue = argStore.GetVenueString();
if (venue is null) {
logger.LogMissingParam("mbo", "venue");
return null;
}
var symbol = argStore.GetSymbol();
if (symbol is null) {
logger.LogMissingParam("mbo", "symbol");
return null;
}
var begin = argStore.GetDateTime("begin");
if (!begin.HasValue) {
logger.LogMissingParam("mbo", "begin");
return null;
}
var end = argStore.GetDateTime("end");
if (!end.HasValue) {
logger.LogMissingParam("mbo", "end");
return null;
}
var vs = CreateTables(config, venue, symbol, begin.Value, end.Value)
?.AsDataPackets(MboRow.Schema)
?.AsViewStream();
return vs;
}
static class SupportedStreams {
public static readonly DataStreamMetadata Mbo = new() {
Name = "mbo"
};
}
}