feat: Add live bbo stream
This commit is contained in:
1 parent
aa740e786c
commit
2014931653
22 files changed
+792
-23
No files matched your search
@@ -0,0 +1,5 @@
|
||||
namespace Bts.DataStreamCore;
|
||||
|
||||
using System;
|
||||
|
||||
public class LiveMarketDataStreamAttribute : Attribute { }
|
||||
@@ -0,0 +1,5 @@
|
||||
namespace Bts.DataStreamCore;
|
||||
|
||||
using System;
|
||||
|
||||
public class MarketDataClientParamAttribute : Attribute { }
|
||||
@@ -0,0 +1,49 @@
|
||||
#nullable enable
|
||||
namespace Bts.DataStreamCore;
|
||||
|
||||
using BCG;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
|
||||
/// <summary>
|
||||
/// This class is a temp hack to pass a shared md client to stream constructors.
|
||||
/// </summary>
|
||||
public class MarketDataStreamFactory {
|
||||
readonly object mdClient;
|
||||
|
||||
public MarketDataStreamFactory(object mdClient) =>
|
||||
this.mdClient = mdClient;
|
||||
|
||||
public ViewStream? Create(Type type, object?[]? args) {
|
||||
var ctrs = type.GetConstructors();
|
||||
int nArgs = args?.Length ?? 0;
|
||||
// Since this class is a temp hack, it isn't super robust. Find a constructor with nArgs + 1
|
||||
// or 2 params (for mdClient and optional viewStreamDatabase), inject the special args and
|
||||
// instantiate the stream. If no such constructor exists, just use the original args.
|
||||
foreach (var ctr in ctrs) {
|
||||
var ctrParams = ctr.GetParameters();
|
||||
if (!ctrParams.TryGetFirst(p => p!.GetCustomAttribute<MarketDataClientParamAttribute>() is not null, out var mdClientParam, out int mdClientIdx)) {
|
||||
continue;
|
||||
}
|
||||
var paramInjections = new List<(int Index, object Value)>(2) {
|
||||
(mdClientIdx, mdClient)
|
||||
};
|
||||
if (ctrParams.TryGetFirst(p => p.ParameterType == typeof(ViewStreamDatabase), out var vsdParam, out int vsdIdx)) {
|
||||
paramInjections.Add((vsdIdx, ViewStreamDatabase.instance));
|
||||
}
|
||||
if (nArgs != ctrParams.Length - paramInjections.Count) {
|
||||
continue;
|
||||
}
|
||||
paramInjections.Sort(Comparer<(int, object)>.Create((a, b) => a.Item1.CompareTo(b.Item1)));
|
||||
var argList = args?.ToList() ?? new();
|
||||
foreach (var (idx, val) in paramInjections) {
|
||||
argList.Insert(idx, val);
|
||||
}
|
||||
return Activator.CreateInstance(type, argList.ToArray()) as ViewStream;
|
||||
}
|
||||
return Activator.CreateInstance(type, args) as ViewStream;
|
||||
}
|
||||
}
|
||||
@@ -8,8 +8,10 @@ namespace BCG {
|
||||
using System.Collections;
|
||||
using System.Reflection;
|
||||
using System.IO;
|
||||
using Bts.DataStreamCore;
|
||||
|
||||
public class PrimitiveManager {
|
||||
static MarketDataStreamFactory mdStreamFactory;
|
||||
private IDictionary myTypeLookupTable = new Hashtable();
|
||||
private static PrimitiveManager mySelf;
|
||||
private ArrayList myAssemblyNames = new ArrayList();
|
||||
@@ -99,6 +101,11 @@ namespace BCG {
|
||||
get { return mySelf = mySelf == null ? new PrimitiveManager() : mySelf; }
|
||||
}
|
||||
|
||||
public static void SetMarketDataClient(object value) {
|
||||
Log.isnull(mdStreamFactory);
|
||||
mdStreamFactory = new(value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the Type name corresponding to the primitive name
|
||||
/// </summary>
|
||||
@@ -123,12 +130,19 @@ namespace BCG {
|
||||
constructorArgs = new object[args.Count];
|
||||
args.CopyTo(constructorArgs, 0);
|
||||
}
|
||||
if (myTypeLookupTable[friendlyName] == null)
|
||||
throw new ApplicationException(friendlyName + " is not the friendly name of any of the loaded types");
|
||||
var type = myTypeLookupTable[friendlyName] as Type
|
||||
?? throw new ApplicationException(friendlyName + " is not the friendly name of any of the loaded types");
|
||||
if (type.HasCustomAttribute<LiveMarketDataStreamAttribute>()) {
|
||||
if (mdStreamFactory is null) {
|
||||
Log.Gen.error("Live md not available for streams");
|
||||
return null;
|
||||
}
|
||||
return mdStreamFactory.Create(type, constructorArgs);
|
||||
}
|
||||
return
|
||||
constructorArgs == null ?
|
||||
Activator.CreateInstance(myTypeLookupTable[friendlyName] as Type) as ViewStream :
|
||||
Activator.CreateInstance(myTypeLookupTable[friendlyName] as Type, constructorArgs) as ViewStream;
|
||||
Activator.CreateInstance(type) as ViewStream :
|
||||
Activator.CreateInstance(type, constructorArgs) as ViewStream;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -9,9 +9,10 @@ using System.Threading;
|
||||
using mbts;
|
||||
using System.Linq;
|
||||
using System.Diagnostics;
|
||||
using Bts.PubSub;
|
||||
|
||||
namespace BCG {
|
||||
public class ViewStreamDatabase : IWorker, IDisposable {
|
||||
public class ViewStreamDatabase : IWorker, IWorkScheduler, IDisposable {
|
||||
internal delegate void TagFireDelegate(string tag);
|
||||
public delegate void DoWorkDelegate();
|
||||
private event DoWorkDelegate onDoWorkStart,
|
||||
@@ -211,7 +212,9 @@ namespace BCG {
|
||||
else
|
||||
_workers.Add(w);
|
||||
}
|
||||
|
||||
void IWorkScheduler.Add(IWorker worker, TimeSpan interval) =>
|
||||
// TODO: respect the interval param
|
||||
addWorker(worker);
|
||||
|
||||
public void removeWorker(IWorker w) {
|
||||
verifyAccess();
|
||||
@@ -220,6 +223,8 @@ namespace BCG {
|
||||
else
|
||||
_workers.Remove(w);
|
||||
}
|
||||
void IWorkScheduler.Remove(IWorker worker) =>
|
||||
removeWorker(worker);
|
||||
|
||||
#region IDisposable Members
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ using BCG;
|
||||
|
||||
using bts;
|
||||
using bts.api.core;
|
||||
using bts.md;
|
||||
|
||||
using Bts.DataStreamQueryAnalyzer;
|
||||
using Bts.PubSub;
|
||||
@@ -26,6 +27,7 @@ static class Program {
|
||||
cfg.MergeWith(DataStreamQueryAnalyzerConfig.Default());
|
||||
PubSubConfig.SetGlobalConfig(Log.notnull(cfg.PubSub));
|
||||
Configuration.initialize(cfg.Sparkd);
|
||||
PrimitiveManager.SetMarketDataClient(new MarketDataClient(new(TimeSpan.FromSeconds(0.5))));
|
||||
Log.Gen.info("Starting DataStreamQueryAnalyzer");
|
||||
var mainForm = new MainForm();
|
||||
if (cfg.StreamFile is not null) {
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
namespace Bts.PubSub;
|
||||
|
||||
using BCG.Utilities.General;
|
||||
|
||||
using System;
|
||||
|
||||
public class AnonymousWorker : IWorker {
|
||||
readonly Action work;
|
||||
|
||||
public AnonymousWorker(Action work) =>
|
||||
this.work = work;
|
||||
|
||||
public void doWork() =>
|
||||
work();
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
namespace Bts.PubSub;
|
||||
|
||||
using BCG.Utilities.General;
|
||||
|
||||
using System;
|
||||
|
||||
public interface IParameterizedWorker : IWorker {
|
||||
string? Group => null;
|
||||
TimeSpan Interval => TimeSpan.Zero;
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
namespace Bts.PubSub;
|
||||
|
||||
using BCG.Utilities.General;
|
||||
|
||||
using System;
|
||||
|
||||
public interface IWorkScheduler {
|
||||
void Add(IWorker worker, TimeSpan interval);
|
||||
void Remove(IWorker worker);
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
namespace Bts.PubSub;
|
||||
|
||||
using BCG.Utilities.General;
|
||||
|
||||
using bts;
|
||||
using bts.collections;
|
||||
using bts.utils;
|
||||
|
||||
using Spark.Core.Threading;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
|
||||
public class WorkScheduler : IWorkScheduler, IDisposable {
|
||||
readonly AtomicGate gate = new();
|
||||
WorkerData[] workers = Array.Empty<WorkerData>();
|
||||
int isDisposed;
|
||||
int cursor;
|
||||
|
||||
public WorkScheduler(bool isBackground, int numThreads, TimeSpan interval) {
|
||||
numThreads = Math.Max(1, numThreads);
|
||||
var threads = new Thread[numThreads];
|
||||
for (int i = 0; i < numThreads; i++) {
|
||||
threads[i] = new Thread(WorkLoop) {
|
||||
IsBackground = isBackground
|
||||
};
|
||||
threads[i].Start();
|
||||
}
|
||||
|
||||
void WorkLoop() {
|
||||
while (Volatile.Read(ref isDisposed) == 0) {
|
||||
var workers = Volatile.Read(ref this.workers);
|
||||
if (workers.Length == 0) {
|
||||
continue;
|
||||
}
|
||||
int cursor = (Interlocked.Increment(ref this.cursor) - 1) % workers.Length;
|
||||
int origin = cursor;
|
||||
do {
|
||||
var w = workers[cursor];
|
||||
if (Interlocked.CompareExchange(ref w.IsBusy, 1, 0) == 0) {
|
||||
if ((DateTime.Now - w.LastWorkTime) < w.Interval) {
|
||||
Log.assert(Interlocked.CompareExchange(ref w.IsBusy, 0, 1) == 1);
|
||||
continue;
|
||||
}
|
||||
w.Worker.doWork();
|
||||
w.LastWorkTime = DateTime.Now;
|
||||
Log.assert(Interlocked.CompareExchange(ref w.IsBusy, 0, 1) == 1);
|
||||
break;
|
||||
}
|
||||
cursor = (Interlocked.Increment(ref this.cursor) - 1) % workers.Length;
|
||||
} while (cursor != origin);
|
||||
Thread.Sleep(interval);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Add(IWorker worker, TimeSpan interval) {
|
||||
using var _ = gate.BeginTransaction();
|
||||
workers = ArrayHelper.add(workers, new(worker, interval));
|
||||
}
|
||||
|
||||
public void Remove(IWorker worker) {
|
||||
using var _ = gate.BeginTransaction();
|
||||
if (workers.WrapSelector(w => w.Worker, null).TryFindIndex(worker, out var i)) {
|
||||
ArrayHelper.remove_at(ref workers, i);
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose() =>
|
||||
Volatile.Write(ref isDisposed, 1);
|
||||
|
||||
class WorkerData {
|
||||
public readonly IWorker Worker;
|
||||
public readonly TimeSpan? Interval;
|
||||
// 1 means busy, 0 means not busy (would be a bool if Interlocked.CompareExchange
|
||||
// had an overload that accepted them).
|
||||
public int IsBusy;
|
||||
public DateTime LastWorkTime = DateTime.MinValue;
|
||||
|
||||
public WorkerData(IWorker worker, TimeSpan? interval) {
|
||||
Worker = worker;
|
||||
Interval = interval;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
namespace Bts.PubSub;
|
||||
|
||||
using BCG.Utilities.General;
|
||||
|
||||
using Spark.Core.Disposables;
|
||||
|
||||
using System;
|
||||
|
||||
public static class WorkSchedulerExtensions {
|
||||
public static void Add(this IWorkScheduler scheduler, IWorker worker) =>
|
||||
scheduler.Add(worker, (worker as IParameterizedWorker)?.Interval ?? TimeSpan.Zero);
|
||||
|
||||
/// <summary>
|
||||
/// Invoke an action (once) on an IWorkScheduler callback.
|
||||
/// </summary>
|
||||
/// <param name="scheduler">The scheduler on which to invoke the action</param>
|
||||
/// <param name="action">The action to invoke</param>
|
||||
public static void Post(this IWorkScheduler scheduler, Action action) {
|
||||
AnonymousWorker? worker = null;
|
||||
worker = new AnonymousWorker(DoWork);
|
||||
scheduler.Add(worker);
|
||||
|
||||
void DoWork() {
|
||||
scheduler.Remove(worker!);
|
||||
action();
|
||||
}
|
||||
}
|
||||
|
||||
public static IDisposable Subscribe(this IWorkScheduler scheduler, IWorker worker, TimeSpan? interval = null) {
|
||||
scheduler.Add(worker, interval ?? TimeSpan.Zero);
|
||||
return Disposable.Create(Dispose);
|
||||
void Dispose() =>
|
||||
scheduler.Remove(worker);
|
||||
}
|
||||
|
||||
public static IDisposable Subscribe(this IWorkScheduler scheduler, Action work, TimeSpan? interval = null) =>
|
||||
scheduler.Subscribe(new AnonymousWorker(work), interval);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,152 @@
|
||||
namespace Bts.SharedStreams.MarketData;
|
||||
|
||||
using BCG;
|
||||
|
||||
using bts.api;
|
||||
using bts.md;
|
||||
|
||||
using Bts.DataStream;
|
||||
using Bts.DataStreamCore;
|
||||
|
||||
using Spark.Api.MarketData;
|
||||
|
||||
public class BookSchemaBuilder {
|
||||
public int NumLevels { get; set; } = 5;
|
||||
public bool ShowVenue { get; set; } = true;
|
||||
public bool ShowAsset { get; set; } = true;
|
||||
public bool ShowSecurityType { get; set; } = true;
|
||||
public bool ShowSecurityName { get; set; } = true;
|
||||
public bool ShowSecurityId { get; set; } = true;
|
||||
public bool ShowCashPerPoint { get; set; } = true;
|
||||
public bool ShowPrice { get; set; } = true;
|
||||
public bool ShowQuantity { get; set; } = true;
|
||||
public bool ShowNotional { get; set; } = true;
|
||||
public string TableName { get; set; } = "Book";
|
||||
|
||||
public IBlueTableSchema ToSchema() {
|
||||
var bd = new BlueTableSchemaBuilder(TableName) {
|
||||
("id", typeof(int))
|
||||
};
|
||||
if (ShowVenue) {
|
||||
bd.Add(WellKnownColumns.Venue);
|
||||
}
|
||||
if (ShowAsset) {
|
||||
bd.Add(WellKnownColumns.Asset);
|
||||
}
|
||||
if (ShowSecurityType) {
|
||||
bd.Add(WellKnownColumns.SecurityType);
|
||||
}
|
||||
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 (ShowAsset) {
|
||||
adapter.AddColumn(WellKnownColumns.Asset.Name, b => b.security.Symbol);
|
||||
}
|
||||
if (ShowSecurityType) {
|
||||
adapter.AddColumn(WellKnownColumns.SecurityType.Name, b => b.security.security_type().ToString());
|
||||
}
|
||||
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 (ShowAsset) {
|
||||
adapter.AddColumn(WellKnownColumns.Asset.Name, b => b.security.Symbol);
|
||||
}
|
||||
if (ShowSecurityType) {
|
||||
adapter.AddColumn(WellKnownColumns.SecurityType.Name, b => b.security.security_type().ToString());
|
||||
}
|
||||
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,66 @@
|
||||
namespace Bts.SharedStreams.MarketData;
|
||||
|
||||
using bts.api;
|
||||
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
public class BookStreamParams {
|
||||
public Venue? Venue { get; set; }
|
||||
public string? Asset { get; set; }
|
||||
public SecurityType? SecurityType { get; set; }
|
||||
public string? Name { get; set; }
|
||||
public string? NameOrAsset { get; set; }
|
||||
public string? SecurityName { get; set; }
|
||||
public int NumLevels { get; set; } = 5;
|
||||
public bool ShowVenue { get; set; } = true;
|
||||
public bool ShowAsset { get; set; } = true;
|
||||
public bool ShowSecurityType { get; set; } = true;
|
||||
public bool ShowSecurityName { get; set; } = true;
|
||||
public bool ShowSecurityId { get; set; } = true;
|
||||
public bool ShowCashPerPoint { get; set; } = true;
|
||||
public bool ShowPrice { get; set; } = true;
|
||||
public bool ShowQuantity { get; set; } = true;
|
||||
public bool ShowNotional { get; set; } = true;
|
||||
|
||||
public Func<SecurityImmutable, bool> GetSecurityFilter() {
|
||||
var nameRx = CreateRegex(Name);
|
||||
var nameOrAssetRx = CreateRegex(NameOrAsset);
|
||||
return SecurityFilter;
|
||||
|
||||
static Regex? CreateRegex(string? pattern) =>
|
||||
pattern is null ? null : new(pattern);
|
||||
|
||||
bool SecurityFilter(SecurityImmutable s) {
|
||||
if (Venue.HasValue && Venue.Value != s.Exchange) {
|
||||
return false;
|
||||
}
|
||||
if (SecurityType.HasValue && SecurityType != s.security_type()) {
|
||||
return false;
|
||||
}
|
||||
if (Asset is not null && Asset != s.Symbol) {
|
||||
return false;
|
||||
}
|
||||
if (nameRx is not null && !nameRx.IsMatch(s.ToString()!)) {
|
||||
return false;
|
||||
}
|
||||
if (nameOrAssetRx is not null && NameOrAsset != s.Symbol && !nameOrAssetRx.IsMatch(s.ToString()!)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public BookSchemaBuilder GetSchemaBuilder() =>
|
||||
new() {
|
||||
NumLevels = NumLevels,
|
||||
ShowVenue = ShowVenue,
|
||||
ShowAsset = ShowAsset,
|
||||
ShowSecurityType = ShowSecurityType,
|
||||
ShowSecurityName = ShowSecurityName,
|
||||
ShowSecurityId = ShowSecurityId,
|
||||
ShowCashPerPoint = ShowCashPerPoint,
|
||||
ShowPrice = ShowPrice,
|
||||
ShowQuantity = ShowQuantity,
|
||||
ShowNotional = ShowNotional,
|
||||
};
|
||||
}
|
||||
@@ -1,26 +1,35 @@
|
||||
namespace bts.BlueOrb
|
||||
{
|
||||
using System;
|
||||
|
||||
namespace bts.BlueOrb {
|
||||
using BCG;
|
||||
|
||||
using bts.api;
|
||||
using bts.md;
|
||||
|
||||
using Bts.DataStream;
|
||||
using Bts.DataStreamCore;
|
||||
using Bts.SharedStreams.MarketData;
|
||||
|
||||
[LiveMarketDataStream]
|
||||
[DataStreamUserRespresentation("LiveBBOStream")]
|
||||
public class LiveBBOStream : ViewStream {
|
||||
public LiveBBOStream(string target)
|
||||
: this(SecurityType.Future, target) { }
|
||||
public LiveBBOStream(SecurityType sec_type, string target) {
|
||||
ViewStream rs = null;
|
||||
switch (sec_type) {
|
||||
case SecurityType.Future:
|
||||
rs = new LiveFuturesBBOStream(target);
|
||||
break;
|
||||
default:
|
||||
Log.assert(false);
|
||||
break;
|
||||
public LiveBBOStream([MarketDataClientParam] MarketDataClient mdClient, ViewStreamDatabase vsd)
|
||||
: this(mdClient, vsd, null, null, null) { }
|
||||
|
||||
public LiveBBOStream([MarketDataClientParam] MarketDataClient mdClient, ViewStreamDatabase vsd, string? venueStr, string? nameOrAsset)
|
||||
: this(mdClient, vsd, venueStr, null, nameOrAsset) { }
|
||||
|
||||
public LiveBBOStream([MarketDataClientParam] MarketDataClient mdClient, ViewStreamDatabase vsd, string? venueStr, string? secTypeStr, string? nameOrAsset) {
|
||||
var opts = new BookStreamParams() {
|
||||
NameOrAsset = nameOrAsset,
|
||||
};
|
||||
if (VenueArgumentParser.TryParse(venueStr, out var venue)) {
|
||||
opts.Venue = venue;
|
||||
}
|
||||
mySetRealSource(rs);
|
||||
if (SecurityTypeArgumentParser.TryParse(secTypeStr, out var secType)) {
|
||||
opts.SecurityType = secType;
|
||||
}
|
||||
var schemaBd = opts.GetSchemaBuilder();
|
||||
var adapter = schemaBd.BboAdapter();
|
||||
var secFilter = opts.GetSecurityFilter();
|
||||
mySetRealSource(mdClient.books(secFilter).AsDataPackets(adapter).OnWorkScheduler(vsd).AsViewStream());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
namespace Bts.SharedStreams.MarketData;
|
||||
|
||||
using bts.api;
|
||||
|
||||
public static class SecurityTypeArgumentParser {
|
||||
public static bool TryParse(string? str, out SecurityType result) {
|
||||
if (str is null) {
|
||||
result = SecurityType.Unset;
|
||||
return false;
|
||||
}
|
||||
if (Enum.TryParse(str, true, out result) || Enum.TryParse("SECURITYTYPE_" + str, true, out result)) {
|
||||
return true;
|
||||
}
|
||||
result = str.ToUpper() switch {
|
||||
"F" or "FUT" or "FUTURE" => SecurityType.Future,
|
||||
"O" or "OPT" or "OPTION" => SecurityType.Option,
|
||||
"SPOT" or "STOCK" => SecurityType.Spot,
|
||||
"SPD" or "SPREAD" or "COMBO" => SecurityType.Spread,
|
||||
_ => SecurityType.Unset,
|
||||
};
|
||||
return result != SecurityType.Unset;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
namespace Bts.SharedStreams.MarketData;
|
||||
|
||||
using bts.api;
|
||||
|
||||
public static class VenueArgumentParser {
|
||||
public static bool TryParse(string? str, out Venue result) {
|
||||
if (str is null) {
|
||||
result = Venue.Unset;
|
||||
return false;
|
||||
}
|
||||
if (Enum.TryParse(str, true, out result) || Enum.TryParse("VENUE_" + str, true, out result)) {
|
||||
return true;
|
||||
}
|
||||
result = str.ToUpper() switch {
|
||||
"CME" or "XCME" or "CEC" or "XCEC" => Venue.Glbx,
|
||||
"CBOT" or "CBT" or "XCBT" => Venue.Cboe,
|
||||
_ => Venue.Unset,
|
||||
};
|
||||
return result != Venue.Unset;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
namespace Bts.SharedStreams;
|
||||
|
||||
using bts.api;
|
||||
|
||||
using Spark.Api.Orders;
|
||||
|
||||
public static class WellKnownColumns {
|
||||
public static (string Name, Type Type) AskNotional => (WellKnownColumnNames.AskNotional, typeof(double));
|
||||
public static (string Name, Type Type) AskPrice => (WellKnownColumnNames.AskPrice, typeof(double));
|
||||
public static (string Name, Type Type) AskQuantity => (WellKnownColumnNames.AskQuantity, typeof(double));
|
||||
public static (string Name, Type Type) Asset => (WellKnownColumnNames.Asset, typeof(string));
|
||||
public static (string Name, Type Type) BidNotional => (WellKnownColumnNames.BidNotional, typeof(double));
|
||||
public static (string Name, Type Type) BidPrice => (WellKnownColumnNames.BidPrice, typeof(double));
|
||||
public static (string Name, Type Type) BidQuantity => (WellKnownColumnNames.BidQuantity, typeof(double));
|
||||
public static (string Name, Type Type) CashPerPoint => (WellKnownColumnNames.CashPerPoint, typeof(double));
|
||||
public static (string Name, Type Type) Price => (WellKnownColumnNames.Price, typeof(double));
|
||||
public static (string Name, Type Type) Quantity => (WellKnownColumnNames.Quantity, typeof(double));
|
||||
public static (string Name, Type Type) SecurityId => (WellKnownColumnNames.SecurityId, typeof(uint));
|
||||
public static (string Name, Type Type) SecurityName => (WellKnownColumnNames.SecurityName, typeof(string));
|
||||
public static (string Name, Type Type) SecurityType => (WellKnownColumnNames.SecurityType, typeof(string));
|
||||
public static (string Name, Type Type) Timestamp => (WellKnownColumnNames.Timestamp, typeof(DateTime));
|
||||
public static (string Name, Type Type) Venue => (WellKnownColumnNames.Venue, typeof(string));
|
||||
|
||||
public static (string Name, Type Type) DepthNotional(Side side, int level) => (WellKnownColumnNames.DepthNotional(side, level), typeof(double));
|
||||
public static (string Name, Type Type) DepthPrice(Side side, int level) => (WellKnownColumnNames.DepthPrice(side, level), typeof(double));
|
||||
public static (string Name, Type Type) DepthQuantity(Side side, int level) => (WellKnownColumnNames.DepthQuantity(side, level), typeof(double));
|
||||
|
||||
public static (string Name, Type Type) LegAsset(int legNum) => (WellKnownColumnNames.LegAsset(legNum), typeof(string));
|
||||
public static (string Name, Type Type) LegCashPerPoint(int legNum) => (WellKnownColumnNames.LegCashPerPoint(legNum), typeof(double));
|
||||
public static (string Name, Type Type) LegExpiration(int legNum) => (WellKnownColumnNames.LegExpiration(legNum), typeof(DateTime));
|
||||
public static (string Name, Type Type) LegSecurityId(int legNum) => (WellKnownColumnNames.LegSecurityId(legNum), typeof(uint));
|
||||
public static (string Name, Type Type) LegSecurityName(int legNum) => (WellKnownColumnNames.LegSecurityName(legNum), typeof(string));
|
||||
public static (string Name, Type Type) LegSecurityType(int legNum) => (WellKnownColumnNames.LegSecurityType(legNum), typeof(string));
|
||||
public static (string Name, Type Type) LegVenue(int legNum) => (WellKnownColumnNames.LegVenue(legNum), typeof(string));
|
||||
}
|
||||
|
||||
public static class WellKnownColumnNames {
|
||||
public const string AskNotional = "ask_notl";
|
||||
public const string AskPrice = "ask_price";
|
||||
public const string AskQuantity = "ask_qty";
|
||||
public const string Asset = "asset";
|
||||
public const string BidNotional = "bid_notl";
|
||||
public const string BidPrice = "bid_price";
|
||||
public const string BidQuantity = "bid_qty";
|
||||
public const string CashPerPoint = "cpp";
|
||||
public const string Currency = "currency";
|
||||
public const string Expiration = "exp";
|
||||
public const string NumLegs = "num_legs";
|
||||
public const string Price = "price";
|
||||
public const string Quantity = "qty";
|
||||
public const string SecurityId = "sec_id";
|
||||
public const string SecurityName = "sec_name";
|
||||
public const string SecurityType = "sec_type";
|
||||
public const string Timestamp = "ts_event";
|
||||
public const string TradingStatus = "trd_status";
|
||||
public const string Venue = "venue";
|
||||
|
||||
public static string DepthNotional(Side side, int level) => $"{side.BidAskNameLower()}_notl_{level}";
|
||||
public static string DepthPrice(Side side, int level) => $"{side.BidAskNameLower()}_price_{level}";
|
||||
public static string DepthQuantity(Side side, int level) => $"{side.BidAskNameLower()}_qty_{level}";
|
||||
|
||||
public static string LegAsset(int legNum) => $"leg_{legNum}_{Asset}";
|
||||
public static string LegCashPerPoint(int legNum) => $"leg_{legNum}_{CashPerPoint}";
|
||||
public static string LegExpiration(int legNum) => $"leg_{legNum}_{Expiration}";
|
||||
public static string LegSecurityId(int legNum) => $"leg_{legNum}_{SecurityId}";
|
||||
public static string LegSecurityName(int legNum) => $"leg_{legNum}_{SecurityName}";
|
||||
public static string LegSecurityType(int legNum) => $"leg_{legNum}_{SecurityType}";
|
||||
public static string LegVenue(int legNum) => $"leg_{legNum}_{Venue}";
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
namespace bts.md {
|
||||
using bts.api;
|
||||
using bts.api.core;
|
||||
|
||||
using mbts;
|
||||
@@ -21,9 +22,19 @@ namespace bts.md {
|
||||
public List<PriceSize> bids { get; }
|
||||
public List<PriceSize> asks { get; }
|
||||
|
||||
public List<PriceSize> this[Side side] =>
|
||||
side switch {
|
||||
Side.Buy => bids,
|
||||
Side.Sell => asks,
|
||||
_ => throw new ArgumentException("Invalid side", nameof(side)),
|
||||
};
|
||||
|
||||
public static Book empty(uint security_id) =>
|
||||
new(security_id, new(0), new(0));
|
||||
|
||||
public PriceSize this[Side side, int level] =>
|
||||
this[side][level];
|
||||
|
||||
public bool Equals(Book other) =>
|
||||
security_id == other.security_id &&
|
||||
bids.SequenceEqual(other.bids) &&
|
||||
|
||||
@@ -41,4 +41,9 @@ namespace bts.md {
|
||||
public override int GetHashCode() =>
|
||||
HashCode.Combine(size, direct_size, implied_size, price);
|
||||
}
|
||||
|
||||
public static class PriceSizeExtensions {
|
||||
public static double NotionalValue(this PriceSize priceSize, double cashPerPoint) =>
|
||||
priceSize.price * priceSize.size * cashPerPoint;
|
||||
}
|
||||
}
|
||||
@@ -394,6 +394,20 @@ namespace Spark.Api.Orders {
|
||||
return false;
|
||||
}
|
||||
|
||||
public static string BidAskName(this Side side) =>
|
||||
side switch {
|
||||
Side.Buy => "Bid",
|
||||
Side.Sell => "Ask",
|
||||
_ => side.ToString(),
|
||||
};
|
||||
|
||||
public static string BidAskNameLower(this Side side) =>
|
||||
side switch {
|
||||
Side.Buy => "bid",
|
||||
Side.Sell => "ask",
|
||||
_ => side.ToString().ToLower(),
|
||||
};
|
||||
|
||||
// public static Brush badge_fill(AlgoDefinitionMetadata? algo_meta, OrderMetadata order_meta) =>
|
||||
// new SolidColorBrush(badge_color(algo_meta, order_meta));
|
||||
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
using System.Reactive.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using System.Threading;
|
||||
using bts.md;
|
||||
|
||||
class Program {
|
||||
[Flags]
|
||||
@@ -39,6 +40,7 @@
|
||||
PubSubConfig.SetGlobalConfig(Log.notnull(opts.PubSub));
|
||||
var service_ctx = ServiceContext.create(opts.sparkd);
|
||||
await service_ctx.login(opts.user, opts.password);
|
||||
PrimitiveManager.SetMarketDataClient(new MarketDataClient(new(TimeSpan.FromSeconds(0.5))));
|
||||
|
||||
var sync = new bts.websocket_core.WebsocketDataSync(service_ctx);
|
||||
var gwy = new bts.websocket_core.WebsocketConnectionGateway();
|
||||
|
||||
@@ -0,0 +1,156 @@
|
||||
#nullable enable
|
||||
namespace Spark.Core.Threading;
|
||||
|
||||
using bts;
|
||||
|
||||
using Spark.Core.Disposables;
|
||||
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
public class AtomicGate {
|
||||
const int maxAttemptsBeforeSleep = 32;
|
||||
static readonly int[] increasingSleeps = new[] { 1, 10, 100, 200, 500, 1000 };
|
||||
static int idCounter = 0;
|
||||
|
||||
readonly string name;
|
||||
readonly int id = Interlocked.Increment(ref idCounter);
|
||||
readonly object syncRoot = new();
|
||||
int versionIn = 0;
|
||||
int versionOut = 0;
|
||||
|
||||
public AtomicGate(string? name = null) =>
|
||||
this.name = name ?? string.Empty;
|
||||
|
||||
public bool IsBusy => Volatile.Read(ref versionIn) != Volatile.Read(ref versionOut);
|
||||
|
||||
public void Enter() {
|
||||
int n;
|
||||
for (n = 0; n < maxAttemptsBeforeSleep; n++) {
|
||||
if (TryEnter()) {
|
||||
return;
|
||||
}
|
||||
Thread.Yield();
|
||||
}
|
||||
for (n = 0; n < increasingSleeps.Length; n++) {
|
||||
if (TryEnter()) {
|
||||
return;
|
||||
}
|
||||
sleep(increasingSleeps[n]);
|
||||
}
|
||||
while (!TryEnter()) {
|
||||
sleep(increasingSleeps[^1]);
|
||||
}
|
||||
}
|
||||
|
||||
public bool TryEnter() {
|
||||
// var vin = Volatile.Read(ref versionIn);
|
||||
// var vout = Volatile.Read(ref versionOut);
|
||||
// Log.DataFlow.diag($$"""[atomic_gate_try_entering.json]{ "id": {{id}}, "name": "{{name}}", "vin": {{vin}}, "vout": {{vout}}, "ts": "{{DateTime.Now}}" }""");
|
||||
bool res = Impl();
|
||||
// vin = Volatile.Read(ref versionIn);
|
||||
// vout = Volatile.Read(ref versionOut);
|
||||
// Log.DataFlow.diag($$"""[atomic_gate_try_entered.json]{ "id": {{id}}, "name": "{{name}}", "vin": {{vin}}, "vout": {{vout}}, "res": {{res}}, "ts": "{{DateTime.Now}}" }""");
|
||||
return res;
|
||||
|
||||
bool Impl() {
|
||||
lock (syncRoot) {
|
||||
if (versionIn == versionOut) {
|
||||
versionIn++;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public void Execute(Action action) {
|
||||
Enter();
|
||||
try {
|
||||
action();
|
||||
} finally {
|
||||
Exit();
|
||||
}
|
||||
}
|
||||
|
||||
public bool TryExecute(Action action) {
|
||||
if (!TryEnter()) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
action();
|
||||
} finally {
|
||||
Exit();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task<bool> TryExecuteAsync(Func<Task> asyncAction) {
|
||||
if (!TryEnter()) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
await asyncAction();
|
||||
} finally {
|
||||
Exit();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task<(bool Executed, T? Result)> TryExecuteAsync<T>(Func<Task<T>> asyncAction) {
|
||||
if (!TryEnter()) {
|
||||
return (false, default);
|
||||
}
|
||||
T? res;
|
||||
try {
|
||||
res = await asyncAction();
|
||||
} finally {
|
||||
Exit();
|
||||
}
|
||||
return (true, res);
|
||||
}
|
||||
|
||||
public IDisposable BeginTransaction() {
|
||||
int version = Interlocked.Increment(ref versionIn) - 1;
|
||||
int n = 0;
|
||||
var end = Disposable.Create(EndTransaction);
|
||||
for (n = 0; n < maxAttemptsBeforeSleep; n++) {
|
||||
if (Volatile.Read(ref versionOut) == version) {
|
||||
return end;
|
||||
}
|
||||
Thread.Yield();
|
||||
}
|
||||
for (n = 0; n < increasingSleeps.Length; n++) {
|
||||
if (Volatile.Read(ref versionOut) == version) {
|
||||
return end;
|
||||
}
|
||||
sleep(increasingSleeps[n], version);
|
||||
}
|
||||
while (Volatile.Read(ref versionOut) != version) {
|
||||
sleep(increasingSleeps[^1], version);
|
||||
}
|
||||
return end;
|
||||
|
||||
void EndTransaction() =>
|
||||
Log.assert(Interlocked.CompareExchange(ref versionOut, version + 1, version) == version);
|
||||
}
|
||||
|
||||
public Task<IDisposable> BeginTransactionAsync() =>
|
||||
Task.Run(BeginTransaction);
|
||||
|
||||
public void Exit() {
|
||||
lock (syncRoot) {
|
||||
Log.assert(versionIn == versionOut + 1);
|
||||
versionOut = versionIn;
|
||||
}
|
||||
}
|
||||
|
||||
void sleep(int sleepMillis, int? version = null) {
|
||||
// var vout = Volatile.Read(ref versionOut);
|
||||
// var vin = Volatile.Read(ref versionIn);
|
||||
// Log.DataFlow.warn($$"""[atomic_gate_sleep.json]{ "id": {id}, "name": "{{name}}", "vin": {vin}, "vout": {{vout}}, "v": {{version}}, "dur": {{sleepMillis}}ms, "ts": "{{DateTime.Now}}" }""");
|
||||
Thread.Sleep(sleepMillis);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user