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
|
||||
|
||||
|
||||
Reference in new issue
Block a user