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,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}";
|
||||
}
|
||||
Reference in new issue
Block a user