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.
This commit is contained in:
1 parent
f65b6b82f2
commit
ada7d4bed1
64 files changed
+2399
-147
No files matched your search
@@ -1,13 +1,13 @@
|
||||
namespace bts.BlueOrb {
|
||||
using BCG;
|
||||
|
||||
using System;
|
||||
using bts.md;
|
||||
|
||||
[DataStreamUserRespresentation("LiveFuturesBooktream")]
|
||||
[DataStreamUserRespresentation(false)]
|
||||
public class LiveFuturesBooktream : ViewStream {
|
||||
public LiveFuturesBooktream(string future_target) {
|
||||
ViewStream contract_str = new LiveFuturesContractStream(future_target);
|
||||
ViewStream bbo_str = new BookStream();
|
||||
public LiveFuturesBooktream(string future_target, MarketDataClient mdClient) {
|
||||
ViewStream contract_str = new LiveFuturesContractStream(future_target, mdClient);
|
||||
ViewStream bbo_str = new BookStream(mdClient);
|
||||
ViewStream rs = new SimpleJoinStream(contract_str, "sec_key", bbo_str, "sec_key");
|
||||
rs = new ProjectStream(rs, "left_sec:sec, left_sec_key:sec_key, bid_price, bid_sz:bid_size, ask_price, ask_sz:ask_size");
|
||||
mySetRealSource(rs);
|
||||
|
||||
+3
-2
@@ -4,13 +4,14 @@
|
||||
using BCG;
|
||||
|
||||
using bts.api;
|
||||
using bts.md;
|
||||
|
||||
[DataStreamUserRespresentation("LiveFuturesContractStream")]
|
||||
public class LiveFuturesContractStream : ViewStream {
|
||||
public LiveFuturesContractStream(string target) {
|
||||
public LiveFuturesContractStream(string target, MarketDataClient mdClient) {
|
||||
var fc = new ContractUtils.FutureContract(target);
|
||||
// temporary
|
||||
ViewStream ins_str = new InstrumentsStream();
|
||||
ViewStream ins_str = new InstrumentsStream(mdClient);
|
||||
//new bts.BlueOrb.CSVReaderStream(@"C:\Temp\instruments.csv");
|
||||
var rs = new LiveFuturesContractStream(ins_str, fc);
|
||||
mySetRealSource(rs);
|
||||
|
||||
+7
-6
@@ -1,15 +1,16 @@
|
||||
namespace bts.BlueOrb {
|
||||
using bts.SilverPine;
|
||||
using BCG;
|
||||
using System;
|
||||
using bts.md;
|
||||
|
||||
[DataStreamUserRespresentation("SpatialArbScoreStream")]
|
||||
[DataStreamUserRespresentation(false)]
|
||||
/// This stream is now created by <see cref="Bts.SharedStreams.StreamFactories.SpatialArbScoreStreamFactory"/>
|
||||
public class SpatialArbScoreStream : ViewStream {
|
||||
public SpatialArbScoreStream(int window_sz_secs, int sample_period_sz)
|
||||
: this(TimeSpan.FromSeconds(window_sz_secs), TimeSpan.FromSeconds(sample_period_sz), ViewStreamDatabase.instance) { }
|
||||
public SpatialArbScoreStream(MarketDataClient mdClient, int window_sz_secs, int sample_period_sz)
|
||||
: this(mdClient, TimeSpan.FromSeconds(window_sz_secs), TimeSpan.FromSeconds(sample_period_sz), ViewStreamDatabase.instance) { }
|
||||
|
||||
public SpatialArbScoreStream(TimeSpan window_sz, TimeSpan sample_period, ViewStreamDatabase vsd) {
|
||||
ViewStream rs = new SpatialArbStatsStream(window_sz, sample_period, vsd);
|
||||
public SpatialArbScoreStream(MarketDataClient mdClient, TimeSpan window_sz, TimeSpan sample_period, ViewStreamDatabase vsd) {
|
||||
ViewStream rs = new SpatialArbStatsStream(mdClient, window_sz, sample_period, vsd);
|
||||
rs = new ThrottleStream(rs, TimeSpan.FromSeconds(1), vsd);
|
||||
rs = new SortStream(rs, "-n_trade_ticks");
|
||||
mySetRealSource(rs);
|
||||
|
||||
+7
-9
@@ -1,17 +1,15 @@
|
||||
namespace bts.BlueOrb {
|
||||
using BCG;
|
||||
|
||||
using SharpCompress.Common.Rar;
|
||||
using bts.md;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
// For all the pairs generated by SpatialFuturePairsStream, this stream
|
||||
// generates the statistics, by appending together a SpreadStatsStream
|
||||
[DataStreamUserRespresentation("SpatialArbStatsStream")]
|
||||
/// This stream is now created by <see cref="Bts.SharedStreams.StreamFactories.SpatialArbStatsStreamFactory"/>
|
||||
[DataStreamUserRespresentation(false)]
|
||||
public class SpatialArbStatsStream : ViewStream{
|
||||
private ViewStream _spatial_pairs_stream;
|
||||
private ViewStreamDatabase _vsd;
|
||||
@@ -20,14 +18,14 @@
|
||||
private ViewStream _bbo_stream;
|
||||
private readonly TimeSpan _window_sz, _sampling_period_sz;
|
||||
|
||||
public SpatialArbStatsStream(int window_sz_seconds, int sampling_period_sz_seconds)
|
||||
: this(TimeSpan.FromSeconds(window_sz_seconds), TimeSpan.FromSeconds(sampling_period_sz_seconds), ViewStreamDatabase.instance) { }
|
||||
public SpatialArbStatsStream(MarketDataClient mdClient, int window_sz_seconds, int sampling_period_sz_seconds)
|
||||
: this(mdClient, TimeSpan.FromSeconds(window_sz_seconds), TimeSpan.FromSeconds(sampling_period_sz_seconds), ViewStreamDatabase.instance) { }
|
||||
|
||||
public SpatialArbStatsStream(TimeSpan window_sz, TimeSpan sampling_period_sz, ViewStreamDatabase vsd) {
|
||||
public SpatialArbStatsStream(MarketDataClient mdClient, TimeSpan window_sz, TimeSpan sampling_period_sz, ViewStreamDatabase vsd) {
|
||||
_vsd = vsd;
|
||||
_window_sz = window_sz;
|
||||
_sampling_period_sz = sampling_period_sz;
|
||||
_spatial_pairs_stream = new SpatialFuturePairsStream(vsd);
|
||||
_spatial_pairs_stream = new SpatialFuturePairsStream(mdClient, vsd);
|
||||
_bbo_stream = new BBOStream(vsd);
|
||||
}
|
||||
|
||||
|
||||
+7
-8
@@ -2,23 +2,22 @@
|
||||
using BCG;
|
||||
|
||||
using bts.api;
|
||||
using bts.md;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
[DataStreamUserRespresentation("SpatialFuturePairsStream")]
|
||||
/// This stream is now created by <see cref="Bts.SharedStreams.StreamFactories.SpatialFuturePairsStreamFactory"/>
|
||||
[DataStreamUserRespresentation(false)]
|
||||
public class SpatialFuturePairsStream : ViewStream {
|
||||
private ViewStream _main_stream;
|
||||
private IBlueTableSchema _schema;
|
||||
private HashSet<string> _key_map = new HashSet<string>();
|
||||
public SpatialFuturePairsStream()
|
||||
: this(ViewStreamDatabase.instance) { }
|
||||
public SpatialFuturePairsStream(MarketDataClient mdClient)
|
||||
: this(mdClient, ViewStreamDatabase.instance) { }
|
||||
|
||||
public SpatialFuturePairsStream(ViewStreamDatabase vsd) {
|
||||
_main_stream = new InstrumentsStream(vsd);
|
||||
public SpatialFuturePairsStream(MarketDataClient mdClient, ViewStreamDatabase vsd) {
|
||||
_main_stream = new InstrumentsStream(mdClient, vsd);
|
||||
_main_stream = new FilterStream(_main_stream, "sec_type='Future'");
|
||||
_schema = new BlueTableSchema("SpatialFutures", new ColumnInfo("id", 0, TypeCode.Int32));
|
||||
_schema = _schema.add_column(TypeCode.String, "s1");
|
||||
|
||||
@@ -18,7 +18,7 @@ namespace bts.BlueOrb{
|
||||
using SecurityKey = System.UInt32;
|
||||
using System.Diagnostics;
|
||||
|
||||
[DataStreamUserRespresentation("books")]
|
||||
[DataStreamUserRespresentation(false)]
|
||||
public class BookStream : ViewStream, IWorker {
|
||||
private readonly TimeSpan _poll_period = TimeSpan.FromMilliseconds(500);
|
||||
private DateTime _last_poll_time = DateTime.MinValue;
|
||||
@@ -39,14 +39,13 @@ namespace bts.BlueOrb{
|
||||
IDisposable? subscription;
|
||||
|
||||
// TODO: add cons for symbol, exchange etc
|
||||
public BookStream()
|
||||
: this(ViewStreamDatabase.instance) {
|
||||
}
|
||||
public BookStream(MarketDataClient mdClient)
|
||||
: this(mdClient, ViewStreamDatabase.instance) { }
|
||||
|
||||
public BookStream(ViewStreamDatabase vsd) {
|
||||
public BookStream(MarketDataClient mdClient, ViewStreamDatabase vsd) {
|
||||
bts.Log.Gen.info("BookStream.cons");
|
||||
_vsd = vsd;
|
||||
_md_client = new MarketDataClient(new MDSource(TimeSpan.FromSeconds(0.5)));
|
||||
_md_client = mdClient;
|
||||
}
|
||||
|
||||
protected override void myOnFirstSubscriber() {
|
||||
|
||||
@@ -43,14 +43,13 @@ namespace bts.BlueOrb {
|
||||
IDisposable? subscription;
|
||||
|
||||
// TODO: add cons for symbol, exchange etc
|
||||
public InstrumentsStream()
|
||||
: this(ViewStreamDatabase.instance) {
|
||||
}
|
||||
public InstrumentsStream(MarketDataClient mdClient)
|
||||
: this(mdClient, ViewStreamDatabase.instance) { }
|
||||
|
||||
public InstrumentsStream(ViewStreamDatabase vsd) {
|
||||
public InstrumentsStream(MarketDataClient mdClient, ViewStreamDatabase vsd) {
|
||||
bts.Log.Gen.info("InstrumentsStream.cons");
|
||||
_vsd = vsd;
|
||||
_md_client = new MarketDataClient(new MDSource(TimeSpan.FromSeconds(0.5)));
|
||||
_md_client = mdClient;
|
||||
}
|
||||
|
||||
protected override void myOnFirstSubscriber() {
|
||||
|
||||
@@ -213,7 +213,14 @@ namespace bts.BlueOrb {
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
internal OHLCVStream(ViewStream source, OHLCVBarSize bar_size) {
|
||||
ViewStream rs = source;
|
||||
rs = new SchemaGuesserStream(rs);
|
||||
if (bar_size == OHLCVBarSize.ONE_DAY) {
|
||||
rs = new DateStandardizationStream(rs, "ts_event", DateConversion.ChopTime);
|
||||
}
|
||||
rs = new NewProjectStream(rs, "symbol:contract");
|
||||
mySetRealSource(rs);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,19 +1,23 @@
|
||||
namespace bts.BlueOrb {
|
||||
using BCG;
|
||||
|
||||
using Bts.DataStreamCore;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
[DataStreamUserRespresentation("nl")]
|
||||
[DataStreamUserRespresentation(false)]
|
||||
public class BlueOrbNLStream : ViewStream {
|
||||
private readonly string _nl_prompt;
|
||||
readonly IDataStreamFactory streamFactory;
|
||||
private ViewStream _nl_to_dsl_stream, _dsl_stream;
|
||||
private IDictionary<string, object> _dp_metadata = new Dictionary<string, object>();
|
||||
public BlueOrbNLStream(string nl_prompt) {
|
||||
public BlueOrbNLStream(string nl_prompt, IDataStreamFactory streamFactory) {
|
||||
_nl_prompt = nl_prompt;
|
||||
this.streamFactory = streamFactory;
|
||||
}
|
||||
|
||||
protected override void myOnFirstSubscriber() {
|
||||
@@ -27,7 +31,7 @@
|
||||
string dsl = r.get<string>("response");
|
||||
_dp_metadata["dsl"] = dsl;
|
||||
Log.Gen.info($"NLStream._handle_dsl_callback dsl: {dsl}");
|
||||
_dsl_stream = new DSLStream(dsl);
|
||||
_dsl_stream = new DSLStream(dsl, streamFactory);
|
||||
_dsl_stream.onChange += _handle_data_cb;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
namespace bts.SilverPine;
|
||||
|
||||
using Spark.Core.Json;
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
public class NLPromptConfig {
|
||||
static NLPromptConfig? instance;
|
||||
|
||||
public static NLPromptConfig Instance =>
|
||||
instance ?? throw new InvalidOperationException(
|
||||
"SetGlobalConfig must be called to initialize config static singleton");
|
||||
|
||||
public List<FewShotPrompt> prompts { get; } = new(0);
|
||||
|
||||
public static void SetGlobalConfig(NLPromptConfig cfg) {
|
||||
// This should only be called once
|
||||
Log.isnull(instance);
|
||||
instance = Log.notnull(cfg);
|
||||
}
|
||||
|
||||
public static NLPromptConfig Default() => new();
|
||||
|
||||
// Represents a pair of a natural language query and its dsl
|
||||
public class FewShotExample {
|
||||
public string? natural_language { get; set; }
|
||||
public string? dsl { get; set; }
|
||||
}
|
||||
|
||||
public class FewShotPrompt {
|
||||
public string? category { get; set; }
|
||||
|
||||
public string? description { get; set; }
|
||||
|
||||
public bool IsEnabled { get; set; } = true;
|
||||
|
||||
// MultiLineDescription is simply here for convenience. It is functionally redundant, as it
|
||||
// serves the same purpose as 'description'. If 'description' is too long, you can break it
|
||||
// up into an array of strings and assign it to MultiLineDescription instead.
|
||||
public List<string>? MultiLineDescription { get; set; }
|
||||
|
||||
// the actual prompt given by the user/CME
|
||||
public string? prompt { get; set; }
|
||||
|
||||
public List<FewShotExample> examples { get; } = new(0);
|
||||
|
||||
public override string ToString() {
|
||||
var desc = description ?? string.Concat(MultiLineDescription.empty_if_null());
|
||||
string rv = $"System: {desc}\n";
|
||||
int ex_no = 0;
|
||||
foreach (FewShotExample ex in examples ?? new(0)) {
|
||||
rv += $"{++ex_no}. Natural Language: \"{ex.natural_language}\"" + "\n";
|
||||
rv += $" DSL: {ex.dsl}\n";
|
||||
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
||||
// Reads normal strings and arrays of lines
|
||||
public class FewShotPromptLinesConverter : JsonConverter<string> {
|
||||
public override string? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) {
|
||||
if (reader.ReadString(out var str)) {
|
||||
return str;
|
||||
}
|
||||
if (reader.ReadArray(out var array)) {
|
||||
return string.Join('\n', array.Select(v => v?.AsString()));
|
||||
}
|
||||
throw new JsonException("Failed to parse FewShotPrompt lines");
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options) =>
|
||||
writer.WriteStringValue(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
namespace bts.SilverPine;
|
||||
|
||||
using BCG;
|
||||
|
||||
using bts.BlueOrb;
|
||||
|
||||
using Bts.DataStreamCore;
|
||||
|
||||
using mbts.utils;
|
||||
|
||||
using System.Collections.Generic;
|
||||
|
||||
public class NLStreamFactory : IRecursiveDataStreamFactory {
|
||||
IDataStreamFactory? compositeFactory;
|
||||
|
||||
public ViewStream? Create(string streamId, IEnumerable<(string? Name, int? Index, object? Value)> args) {
|
||||
if (SupportedStreams.NaturalLanguage.IsMatch(streamId)) {
|
||||
if (compositeFactory is null) {
|
||||
Log.DataFlow.warn("Failed to create NLStream because 'compositeFactory' is null.");
|
||||
return null;
|
||||
}
|
||||
if (args.SingleOrDefault().Value is string prompt) {
|
||||
return new BlueOrbNLStream(prompt, compositeFactory);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public IEnumerable<IDataStreamMetadata> GetSupportedStreams() {
|
||||
yield return SupportedStreams.NaturalLanguage;
|
||||
}
|
||||
|
||||
public void OnCompositeFactory(IDataStreamFactory compositeFactory) =>
|
||||
this.compositeFactory = compositeFactory;
|
||||
|
||||
static class SupportedStreams {
|
||||
public static readonly DataStreamMetadata NaturalLanguage = new() {
|
||||
Name = "nl",
|
||||
AlternateNames = new() { "naturalLanguage" }
|
||||
};
|
||||
|
||||
public static string NlName => ExceptionAssert.not_null(NaturalLanguage.Name);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
namespace Bts.SharedStreams.Services;
|
||||
|
||||
using bts.SilverPine;
|
||||
|
||||
using Bts.DataStreamCore;
|
||||
using Bts.SharedStreams.StreamFactories;
|
||||
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
public static class SharedStreamsServices {
|
||||
public static IServiceCollection AddMarketDataStreams(this IServiceCollection services) =>
|
||||
services.addStreamFactory<BookStreamFactory>()
|
||||
.addStreamFactory<InstrumentStreamFactory>();
|
||||
|
||||
public static IServiceCollection AddNaturalLanguageStreams(this IServiceCollection services, IConfigurationSection configurationSection) {
|
||||
services.Configure<NLPromptConfig>(configurationSection);
|
||||
NLPromptConfig.SetGlobalConfig(configurationSection.Get<NLPromptConfig>() ?? NLPromptConfig.Default());
|
||||
// TODO: integrate config into a NLStream provider (so it can provide config values
|
||||
// instead of the ViewStream's getting them from a static singleton)
|
||||
return services.AddSingleton<NLStreamFactory>()
|
||||
.AddSingleton<IDataStreamFactory, NLStreamFactory>(sp => sp.GetRequiredService<NLStreamFactory>());
|
||||
}
|
||||
|
||||
static IServiceCollection addStreamFactory<T>(this IServiceCollection services) where T : class, IDataStreamFactory =>
|
||||
services.AddSingleton<T>()
|
||||
.AddSingleton<IDataStreamFactory, T>(sp => sp.GetRequiredService<T>());
|
||||
}
|
||||
@@ -0,0 +1,150 @@
|
||||
namespace Bts.SharedStreams.StreamFactories;
|
||||
|
||||
using BCG;
|
||||
|
||||
using bts.api;
|
||||
using bts.md;
|
||||
|
||||
using Bts.DataStreamCore;
|
||||
|
||||
using Spark.Api.MarketData;
|
||||
|
||||
public class BookSchemaBuilder {
|
||||
public int NumLevels { get; set; }
|
||||
public bool ShowNotional { get; set; }
|
||||
public bool ShowQuantity { get; set; } = true;
|
||||
public bool ShowPrice { get; set; } = true;
|
||||
public bool ShowVenue { get; set; }
|
||||
public bool ShowAsset { get; set; } = true;
|
||||
public bool ShowSecurityType { get; set; }
|
||||
public bool ShowSecurityName { get; set; }
|
||||
public bool ShowSecurityId { get; set; } = true;
|
||||
public bool ShowCashPerPoint { get; set; }
|
||||
public string TableName { get; set; } = "Book";
|
||||
|
||||
public IBlueTableSchema ToSchema() {
|
||||
var bd = new BlueTableSchemaBuilder(TableName) {
|
||||
("id", typeof(int))
|
||||
};
|
||||
if (ShowVenue) {
|
||||
bd.Add(WellKnownColumns.Venue);
|
||||
}
|
||||
if (ShowSecurityType) {
|
||||
bd.Add(WellKnownColumns.SecurityType);
|
||||
}
|
||||
if (ShowAsset) {
|
||||
bd.Add(WellKnownColumns.Asset);
|
||||
}
|
||||
if (ShowSecurityName) {
|
||||
bd.Add(WellKnownColumns.SecurityName);
|
||||
}
|
||||
if (ShowSecurityId) {
|
||||
bd.Add(WellKnownColumns.SecurityId);
|
||||
}
|
||||
if (ShowCashPerPoint) {
|
||||
bd.Add(WellKnownColumns.CashPerPoint);
|
||||
}
|
||||
foreach (var side in new[] { Side.Buy, Side.Sell }) {
|
||||
for (int i = 0; i < NumLevels; i++) {
|
||||
if (ShowPrice) {
|
||||
bd.Add(WellKnownColumns.DepthPrice(side, i));
|
||||
}
|
||||
if (ShowQuantity) {
|
||||
bd.Add(WellKnownColumns.DepthQuantity(side, i));
|
||||
}
|
||||
if (ShowNotional) {
|
||||
bd.Add(WellKnownColumns.DepthNotional(side, i));
|
||||
}
|
||||
}
|
||||
}
|
||||
return bd.ToSchema();
|
||||
}
|
||||
|
||||
public BlueTableSchemaAdapter<BookTuple> BookTupleAdapter() {
|
||||
var adapter = new BlueTableSchemaAdapter<BookTuple>(TableName);
|
||||
adapter.AddColumn("id", r => (int)r.security_id);
|
||||
if (ShowVenue) {
|
||||
adapter.AddColumn(WellKnownColumns.Venue.Name, b => b.security.Exchange.ToString());
|
||||
}
|
||||
if (ShowSecurityType) {
|
||||
adapter.AddColumn(WellKnownColumns.SecurityType.Name, b => b.security.security_type().ToString());
|
||||
}
|
||||
if (ShowAsset) {
|
||||
adapter.AddColumn(WellKnownColumns.Asset.Name, b => b.security.Symbol);
|
||||
}
|
||||
if (ShowSecurityName) {
|
||||
adapter.AddColumn(WellKnownColumns.SecurityName.Name, b => b.security.ToString());
|
||||
}
|
||||
if (ShowSecurityId) {
|
||||
adapter.AddColumn(WellKnownColumns.SecurityId.Name, b => b.security_id);
|
||||
}
|
||||
if (ShowCashPerPoint) {
|
||||
adapter.AddColumn(WellKnownColumns.CashPerPoint.Name, b => b.security.ContractSpec.CashPerPoint);
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
public BlueTableSchemaAdapter<BookTuple> BboAdapter() {
|
||||
var adapter = new BlueTableSchemaAdapter<BookTuple>(TableName);
|
||||
adapter.AddColumn("id", r => (int)r.security_id);
|
||||
if (ShowVenue) {
|
||||
adapter.AddColumn(WellKnownColumns.Venue.Name, b => b.security.Exchange.ToString());
|
||||
}
|
||||
if (ShowSecurityType) {
|
||||
adapter.AddColumn(WellKnownColumns.SecurityType.Name, b => b.security.security_type().ToString());
|
||||
}
|
||||
if (ShowAsset) {
|
||||
adapter.AddColumn(WellKnownColumns.Asset.Name, b => b.security.Symbol);
|
||||
}
|
||||
if (ShowSecurityName) {
|
||||
adapter.AddColumn(WellKnownColumns.SecurityName.Name, b => b.security.ToString());
|
||||
}
|
||||
if (ShowSecurityId) {
|
||||
adapter.AddColumn(WellKnownColumns.SecurityId.Name, b => b.security_id);
|
||||
}
|
||||
if (ShowCashPerPoint) {
|
||||
adapter.AddColumn(WellKnownColumns.CashPerPoint.Name, b => b.security.ContractSpec.CashPerPoint);
|
||||
}
|
||||
if (ShowPrice) {
|
||||
adapter.AddColumn(WellKnownColumns.BidPrice.Name, b => b.book.bids[0].price);
|
||||
}
|
||||
if (ShowQuantity) {
|
||||
adapter.AddColumn(WellKnownColumns.BidQuantity.Name, b => b.book.bids[0].size);
|
||||
}
|
||||
if (ShowNotional) {
|
||||
adapter.AddColumn(WellKnownColumns.BidNotional.Name, b => b.book.bids[0].NotionalValue(b.security.ContractSpec.CashPerPoint));
|
||||
}
|
||||
if (ShowPrice) {
|
||||
adapter.AddColumn(WellKnownColumns.AskPrice.Name, b => b.book.asks[0].price);
|
||||
}
|
||||
if (ShowQuantity) {
|
||||
adapter.AddColumn(WellKnownColumns.AskQuantity.Name, b => b.book.asks[0].size);
|
||||
}
|
||||
if (ShowNotional) {
|
||||
adapter.AddColumn(WellKnownColumns.AskNotional.Name, b => b.book.asks[0].NotionalValue(b.security.ContractSpec.CashPerPoint));
|
||||
}
|
||||
return adapter;
|
||||
}
|
||||
|
||||
static Func<BookTuple, double> priceGetter(Side side, int level) =>
|
||||
b => b.book[side, level].price;
|
||||
|
||||
static Func<BookTuple, double> quantityGetter(Side side, int level) =>
|
||||
b => b.book[side, level].size;
|
||||
|
||||
static Func<BookTuple, double> notionalGetter(Side side, int level) =>
|
||||
b => b.book[side, level].NotionalValue(b.security.ContractSpec.CashPerPoint);
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
namespace Bts.SharedStreams.StreamFactories;
|
||||
|
||||
using BCG;
|
||||
|
||||
using bts.md;
|
||||
|
||||
using Bts.DataStream;
|
||||
using Bts.DataStreamCore;
|
||||
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
using System;
|
||||
|
||||
public class BookStreamFactory : DataStreamFactory {
|
||||
public BookStreamFactory(MarketDataClient? mdClient, ILogger<BookStreamFactory> logger) {
|
||||
this.Add<BookStreamParams>("book", CreateBook, logger);
|
||||
this.Add<BookStreamParams>(new[] { "bbo", "LiveBBOStream" }, CreateBbo, logger);
|
||||
|
||||
ViewStream? CreateBook(BookStreamParams streamParams) =>
|
||||
mdClient?.books(streamParams.GetSecurityCriteria())
|
||||
?.AsDataPackets(streamParams.GetSchemaBuilder().BookTupleAdapter())
|
||||
?.AsViewStream()
|
||||
?? throw new InvalidOperationException("Live md not available");
|
||||
|
||||
ViewStream? CreateBbo(BookStreamParams streamParams) =>
|
||||
mdClient?.books(streamParams.GetSecurityCriteria())
|
||||
?.AsDataPackets(streamParams.GetSchemaBuilder().BboAdapter())
|
||||
?.AsViewStream()
|
||||
?? throw new InvalidOperationException("Live md not available");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
namespace Bts.SharedStreams.StreamFactories;
|
||||
|
||||
using bts.api;
|
||||
|
||||
using Bts.DataStreamCore;
|
||||
|
||||
public class BookStreamParams {
|
||||
[StreamArgument(RegexNames = new[] { WellKnownStreamArgumentAliases.VenueRegex })]
|
||||
public Venue? Venue { get; set; }
|
||||
|
||||
[StreamArgument(RegexNames = new[] { WellKnownStreamArgumentAliases.SymbolRegex })]
|
||||
public string? Symbol { get; set; }
|
||||
|
||||
[StreamArgument(RegexNames = new[] { WellKnownStreamArgumentAliases.SecurityTypeRegex })]
|
||||
public SecurityType? SecurityType { get; set; }
|
||||
|
||||
[StreamArgument(RegexNames = new[] { WellKnownStreamArgumentAliases.SecurityNameRegex })]
|
||||
public string? SecurityName { get; set; }
|
||||
|
||||
[StreamArgument(RegexNames = new[] { "$(levels|nLevels|numLevels|n)^" })]
|
||||
public int NumLevels { get; set; } = 5;
|
||||
|
||||
[StreamArgument(RegexNames = new[] { "$(show|include)?(price|px|p)^" })]
|
||||
public bool ShowPrice { get; set; } = true;
|
||||
|
||||
[StreamArgument(RegexNames = new[] { "$(show|include)?(quantity|qty|q)^" })]
|
||||
public bool ShowQuantity { get; set; } = true;
|
||||
|
||||
[StreamArgument(RegexNames = new[] { "$(show|include)?(notional|notl)^" })]
|
||||
public bool ShowNotional { get; set; }
|
||||
|
||||
[StreamArgument(RegexNames = new[] { "$(show|include)?(cashPerPoint|cpp)^" })]
|
||||
public bool ShowCashPerPoint { get; set; } = true;
|
||||
|
||||
public mbts.spark.SecurityCriteria GetSecurityCriteria() {
|
||||
var clause = new mbts.spark.SecurityCriteriaClause(
|
||||
exchanges: Venue.HasValue ? new[] { Venue.Value } : null,
|
||||
security_types: SecurityType.HasValue ? new[] { SecurityType.Value } : null,
|
||||
symbols: !string.IsNullOrEmpty(Symbol) ? new[] { Symbol } : null,
|
||||
names: !string.IsNullOrEmpty(SecurityName) ? new[] { SecurityName } : null);
|
||||
return clause.is_defined ? new(new[] { clause }) : new();
|
||||
}
|
||||
|
||||
public BookSchemaBuilder GetSchemaBuilder() =>
|
||||
new() {
|
||||
NumLevels = NumLevels,
|
||||
ShowVenue = true,
|
||||
ShowSecurityType = true,
|
||||
ShowAsset = true,
|
||||
ShowSecurityName = true,
|
||||
ShowSecurityId = true,
|
||||
ShowPrice = ShowPrice,
|
||||
ShowQuantity = ShowQuantity,
|
||||
ShowNotional = ShowNotional,
|
||||
ShowCashPerPoint = ShowCashPerPoint,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
namespace Bts.SharedStreams.StreamFactories;
|
||||
|
||||
using BCG;
|
||||
|
||||
using bts.api.core;
|
||||
using bts.BlueOrb;
|
||||
using bts.md;
|
||||
|
||||
using Bts.DataStream;
|
||||
using Bts.DataStreamCore;
|
||||
using Bts.SharedStreams.MarketData;
|
||||
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
class InstrumentStreamFactory : DataStreamFactory {
|
||||
public InstrumentStreamFactory(MarketDataClient mdClient, ILogger<InstrumentStreamFactory> logger) {
|
||||
this.Add<InstrumentStreamParams>("instrument", CreateInstrument, logger);
|
||||
this.Add<LiveFutureContractStreamParams>(new[] { "liveFutures", "liveFuturesContract", "LiveFuturesContractStream" }, CreateLiveFutureContract, logger);
|
||||
|
||||
ViewStream? CreateInstrument(InstrumentStreamParams streamParams) =>
|
||||
mdClient.securities(streamParams.GetSecurityCriteria())
|
||||
.Table(s => s.Id)
|
||||
.AsDataPackets(streamParams.GetSchemaBuilder().SecurityAdapter())
|
||||
.AsViewStream();
|
||||
|
||||
ViewStream? CreateLiveFutureContract(LiveFutureContractStreamParams streamParams) =>
|
||||
new LiveFuturesContractStream(streamParams.Target ?? string.Empty, mdClient);
|
||||
}
|
||||
|
||||
public class LiveFutureContractStreamParams {
|
||||
[StreamArgument(RegexNames = new[] { "^(symbol|sym|product|asset|target|tgt)$" })]
|
||||
public string? Target { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
namespace Bts.SharedStreams.StreamFactories;
|
||||
|
||||
using bts.api;
|
||||
|
||||
using Bts.DataStreamCore;
|
||||
|
||||
public class InstrumentStreamParams {
|
||||
[StreamArgument(RegexNames = new[] { WellKnownStreamArgumentAliases.VenueRegex })]
|
||||
public Venue? Venue { get; set; }
|
||||
|
||||
[StreamArgument(RegexNames = new[] { WellKnownStreamArgumentAliases.Symbol })]
|
||||
public string? Symbol { get; set; }
|
||||
|
||||
[StreamArgument(RegexNames = new[] { WellKnownStreamArgumentAliases.SecurityType })]
|
||||
public SecurityType? Type { get; set; }
|
||||
|
||||
[StreamArgument(RegexNames = new[] { WellKnownStreamArgumentAliases.SecurityType })]
|
||||
public string? Name { get; set; }
|
||||
|
||||
[StreamArgument("legs", "nLegs", "numLegs")]
|
||||
public int NumLegs { get; set; } = 5;
|
||||
|
||||
[StreamArgument(RegexNames = new[] { WellKnownStreamArgumentAliases.ShowVenueRegex })]
|
||||
public bool ShowVenue { get; set; } = true;
|
||||
|
||||
[StreamArgument(RegexNames = new[] { WellKnownStreamArgumentAliases.ShowSecurityTypeRegex })]
|
||||
public bool ShowType { get; set; } = true;
|
||||
|
||||
[StreamArgument(RegexNames = new[] { WellKnownStreamArgumentAliases.ShowSymbolRegex })]
|
||||
public bool ShowSymbol { get; set; } = true;
|
||||
|
||||
[StreamArgument(RegexNames = new[] { WellKnownStreamArgumentAliases.ShowCashPerPointRegex })]
|
||||
public bool ShowCashPerPoint { get; set; } = true;
|
||||
|
||||
[StreamArgument(RegexNames = new[] { WellKnownStreamArgumentAliases.ShowLegVenueRegex })]
|
||||
public bool ShowLegVenue { get; set; } = true;
|
||||
|
||||
[StreamArgument(RegexNames = new[] { WellKnownStreamArgumentAliases.ShowLegSecurityTypeRegex })]
|
||||
public bool ShowLegType { get; set; } = true;
|
||||
|
||||
[StreamArgument(RegexNames = new[] { WellKnownStreamArgumentAliases.ShowLegSymbolRegex })]
|
||||
public bool ShowLegSymbol { get; set; } = true;
|
||||
|
||||
[StreamArgument(RegexNames = new[] { WellKnownStreamArgumentAliases.ShowLegCashPerPointRegex })]
|
||||
public bool ShowLegCashPerPoint { get; set; } = true;
|
||||
|
||||
public mbts.spark.SecurityCriteria GetSecurityCriteria() {
|
||||
var clause = new mbts.spark.SecurityCriteriaClause(
|
||||
exchanges: Venue.HasValue ? new[] { Venue.Value } : null,
|
||||
security_types: Type.HasValue ? new[] { Type.Value } : null,
|
||||
symbols: !string.IsNullOrEmpty(Symbol) ? new[] { Symbol } : null,
|
||||
names: !string.IsNullOrEmpty(Name) ? new[] { Name } : null);
|
||||
return clause.is_defined ? new(new[] { clause }) : new();
|
||||
}
|
||||
|
||||
public InstrumentSchemaBuilder GetSchemaBuilder() =>
|
||||
new() {
|
||||
NumLegs = NumLegs,
|
||||
ShowVenue = ShowVenue,
|
||||
ShowType = true,
|
||||
ShowAsset = true,
|
||||
ShowName = true,
|
||||
ShowId = true,
|
||||
ShowCashPerPoint = ShowCashPerPoint,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
namespace Bts.SharedStreams.StreamFactories;
|
||||
|
||||
using bts.api;
|
||||
|
||||
using Spark.Api.Orders;
|
||||
|
||||
public static class WellKnownColumns {
|
||||
public static (string Name, Type Type) SecurityId => ("SecId", typeof(uint));
|
||||
public static (string Name, Type Type) Venue => ("Venue", typeof(string));
|
||||
public static (string Name, Type Type) SecurityType => ("SecType", typeof(string));
|
||||
public static (string Name, Type Type) Asset => ("Asset", typeof(string));
|
||||
public static (string Name, Type Type) SecurityName => ("Sec", typeof(string));
|
||||
public static (string Name, Type Type) CashPerPoint => ("Cpp", typeof(double));
|
||||
public static (string Name, Type Type) Price => ("Price", typeof(double));
|
||||
public static (string Name, Type Type) Quantity => ("Qty", typeof(double));
|
||||
public static (string Name, Type Type) Timestamp => ("Ts", typeof(DateTime));
|
||||
public static (string Name, Type Type) TimestampNanos => ("TsNanos", typeof(long));
|
||||
|
||||
public static (string Name, Type Type) BidPrice => ("BidPrice", typeof(double));
|
||||
public static (string Name, Type Type) BidQuantity => ("BidQty", typeof(double));
|
||||
public static (string Name, Type Type) BidNotional => ("BidNotl", typeof(double));
|
||||
public static (string Name, Type Type) AskPrice => ("AskPrice", typeof(double));
|
||||
public static (string Name, Type Type) AskQuantity => ("AskQty", typeof(double));
|
||||
public static (string Name, Type Type) AskNotional => ("AskNotl", typeof(double));
|
||||
|
||||
public static (string Name, Type Type) DepthPrice(Side side, int level) => ($"{side.BidAskName()}Price{level}", typeof(double));
|
||||
public static (string Name, Type Type) DepthQuantity(Side side, int level) => ($"{side.BidAskName()}Qty{level}", typeof(double));
|
||||
public static (string Name, Type Type) DepthNotional(Side side, int level) => ($"{side.BidAskName()}Notl{level}", typeof(double));
|
||||
|
||||
public static (string Name, Type Type) LegSecurityId(int legNum) => ($"Leg{legNum}SecId", typeof(uint));
|
||||
public static (string Name, Type Type) LegVenue(int legNum) => ($"Leg{legNum}Venue", typeof(string));
|
||||
public static (string Name, Type Type) LegSecurityType(int legNum) => ($"Leg{legNum}SecType", typeof(string));
|
||||
public static (string Name, Type Type) LegAsset(int legNum) => ($"Leg{legNum}Asset", typeof(string));
|
||||
public static (string Name, Type Type) LegSecurityName(int legNum) => ($"Leg{legNum}Sec", typeof(string));
|
||||
public static (string Name, Type Type) LegCashPerPoint(int legNum) => ($"Leg{legNum}Cpp", typeof(double));
|
||||
}
|
||||
Reference in new issue
Block a user