So we can create ViewStreams that depend on other services, config, etc. Previously, the PrimitiveManager relied purely on static construction of types through reflection.
409 lines
16 KiB
C#
409 lines
16 KiB
C#
namespace bts.websocket_core {
|
|
using BCG;
|
|
|
|
using bts.api;
|
|
using bts.api.orderman;
|
|
using bts.md;
|
|
|
|
using Bts.DataStream;
|
|
using Bts.DataStreamCore;
|
|
using Bts.DataStreamExpressions;
|
|
|
|
using mbts.spark;
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
using Spark.Api.Data;
|
|
using Spark.Api.MarketData;
|
|
using Spark.Api.Orders;
|
|
using Spark.Core.Collections;
|
|
|
|
using System;
|
|
using System.Diagnostics.CodeAnalysis;
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
|
public class WebsocketSubscriptionHandler : IWebsocketRequestHandler {
|
|
enum SUBSCRIPTION_TYPE {
|
|
BOOKS = 1,
|
|
ORDERS,
|
|
TRADES,
|
|
SECURITIES,
|
|
PRICE_STATS,
|
|
PRICE_TICKERS,
|
|
FILLS,
|
|
DATA_STREAM,
|
|
UNKNOWN = 0
|
|
}
|
|
public bool Valid { get; set; } = true;
|
|
public string Error_message { get; set; } = "";
|
|
public IUniqueID ID { get; }
|
|
public IDisposable? dis;
|
|
public void Dispose() => dis?.Dispose();
|
|
Action<Object> _sm;
|
|
private MarketDataClient _MdClient;
|
|
private OrdersClient _OrdersClient;
|
|
public Action subscribe;
|
|
void set_invalid(string message) {
|
|
Valid = false;
|
|
Error_message = message;
|
|
}
|
|
public void SendMessage(Object _data) {
|
|
var json = new {
|
|
jsonrpc = 2.0,
|
|
method = "subscribe",
|
|
@params = new {
|
|
subscription_id = ID.ToString(),
|
|
data = _data
|
|
},
|
|
id = 11,
|
|
};
|
|
_sm(json);
|
|
}
|
|
private static Action<WebsocketSubscriptionHandler> GetSecuritiesSubscriber(SecurityCriteria cri) {
|
|
return (WebsocketSubscriptionHandler hdlr) => {
|
|
hdlr.dis = hdlr._MdClient.securities_incremental(cri).Subscribe(
|
|
upd =>
|
|
hdlr.SendMessage(WebsocketJsonParsers.to_json<SecurityImmutable, uint>(upd)));
|
|
};
|
|
}
|
|
private static Action<WebsocketSubscriptionHandler> GetTradesSubscriber(TradesSubscriptionFilter cri) {
|
|
return (WebsocketSubscriptionHandler hdlr) => {
|
|
hdlr.dis = hdlr._MdClient.trades(cri).Subscribe(
|
|
upd =>
|
|
hdlr.SendMessage(WebsocketJsonParsers.to_json<TradeInfo>(upd)));
|
|
};
|
|
}
|
|
private static Action<WebsocketSubscriptionHandler> GetBookSubscriber(SecurityCriteria cri) {
|
|
return (WebsocketSubscriptionHandler hdlr) => {
|
|
hdlr.dis = hdlr._MdClient.books(cri).Subscribe(
|
|
upd =>
|
|
hdlr.SendMessage(WebsocketJsonParsers.to_json<BookTuple, uint>(upd)));
|
|
};
|
|
}
|
|
private static Action<WebsocketSubscriptionHandler> GetPriceStatsSubscriber(SecurityCriteria cri) {
|
|
return (WebsocketSubscriptionHandler hdlr) => {
|
|
hdlr.dis = hdlr._MdClient.price_stats(cri).Subscribe(
|
|
upd =>
|
|
hdlr.SendMessage(WebsocketJsonParsers.to_json<PriceStats, uint>(upd)));
|
|
};
|
|
}
|
|
private static Action<WebsocketSubscriptionHandler> GetPriceTickerSubscriber(SecurityCriteria cri) {
|
|
return (WebsocketSubscriptionHandler hdlr) => {
|
|
hdlr.dis = hdlr._MdClient.price_ticker(cri).Subscribe(
|
|
upd =>
|
|
hdlr.SendMessage(WebsocketJsonParsers.to_json<PriceTicker, uint>(upd)));
|
|
};
|
|
}
|
|
private static Action<WebsocketSubscriptionHandler> GetOrdersSubscriber(OrdersSubscriptionFilter cri) {
|
|
return (WebsocketSubscriptionHandler hdlr) => {
|
|
hdlr.dis = hdlr._OrdersClient.orders_with_root(cri).Subscribe(
|
|
upd =>
|
|
hdlr.SendMessage(WebsocketJsonParsers.to_json<OrderRecordTupleWithRoot, uint>(upd)));
|
|
};
|
|
}
|
|
|
|
private static Action<WebsocketSubscriptionHandler> GetDataStreamSubscriber(ViewStream vs, string subId) {
|
|
return Subscribe;
|
|
|
|
void Subscribe(WebsocketSubscriptionHandler hdlr) {
|
|
// Declare dictionaries here to reduce allocations in the callback. Use a lock
|
|
// to be safe (callbacks are likely single-threaded, this is just insurance).
|
|
var syncRoot = new object();
|
|
var removedRows = new Dictionary<int, object>();
|
|
var changedRows = new Dictionary<int, object>();
|
|
var addedRows = new Dictionary<int, object>();
|
|
|
|
hdlr.dis = vs.AsObservable().Subscribe(OnData);
|
|
|
|
void OnData(DataPacket dp) {
|
|
lock (syncRoot) {
|
|
removedRows.Clear();
|
|
changedRows.Clear();
|
|
addedRows.Clear();
|
|
foreach (IBlueRow r in dp.deletedRows) {
|
|
var id = r.Id();
|
|
removedRows[id] = r;
|
|
}
|
|
foreach (IBlueRow r in dp.addedRows) {
|
|
var id = r.Id();
|
|
if (removedRows.Remove(id)) {
|
|
changedRows.Add(id, r);
|
|
} else {
|
|
addedRows.Add(id, r);
|
|
}
|
|
}
|
|
hdlr.SendMessage(new {
|
|
added = addedRows.Values,
|
|
changed = changedRows.Values,
|
|
removed = removedRows.Values,
|
|
});
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private static Action<WebsocketSubscriptionHandler> GetFillsSubscriber(FillsSubscriptionFilter cri) {
|
|
return (WebsocketSubscriptionHandler hdlr) => {
|
|
hdlr.dis = hdlr._OrdersClient.fills(cri).Subscribe(
|
|
upd =>
|
|
hdlr.SendMessage(WebsocketJsonParsers.to_json<FillTuple, string>(upd)));
|
|
};
|
|
}
|
|
|
|
static bool TryGetTradesFilter(WebsocketRequest req, [NotNullWhen(true)] out TradesSubscriptionFilter? trades_filter, out string message) {
|
|
trades_filter = null;
|
|
JObject? filter = req.parameters?["filter"] as JObject;
|
|
if (filter == null) {
|
|
message = "filter not present";
|
|
return false;
|
|
}
|
|
if (!TryGetFilter(req, out SecurityCriteria? cri, out message)) {
|
|
return false;
|
|
}
|
|
trades_filter = new TradesSubscriptionFilter(cri);
|
|
return true;
|
|
}
|
|
static bool TryGetFillsFilter(WebsocketRequest req, [NotNullWhen(true)] out FillsSubscriptionFilter? order_filter, out string message) {
|
|
order_filter = null;
|
|
if (req.parameters?["filter"] is not JObject filter) {
|
|
message = "filter not present";
|
|
return false;
|
|
}
|
|
if (!TryGetFilter(req, out SecurityCriteria? cri, out message) ||
|
|
!WebsocketJsonParsers.TryParse<uint>(filter, "accounts", out uint[]? accounts, out message) ||
|
|
!WebsocketJsonParsers.TryParse<OrderStatus>(filter, "statuses", out OrderStatus[]? statuses, out message)
|
|
) {
|
|
return false;
|
|
}
|
|
order_filter = new FillsSubscriptionFilter(
|
|
accounts,
|
|
cri);
|
|
return true;
|
|
}
|
|
static bool TryGetOrdersFilter(WebsocketRequest req, [NotNullWhen(true)] out OrdersSubscriptionFilter? order_filter, out string message) {
|
|
order_filter = null;
|
|
if (req.parameters?["filter"] is not JObject filter) {
|
|
message = "filter not present";
|
|
return false;
|
|
}
|
|
if (
|
|
!TryGetFilter(req, out SecurityCriteria? cri, out message) ||
|
|
!WebsocketJsonParsers.TryParse<uint>(filter, "accounts", out uint[]? accounts, out message) ||
|
|
!WebsocketJsonParsers.TryParse<OrderStatus>(filter, "statuses", out OrderStatus[]? statuses, out message)
|
|
) {
|
|
return false;
|
|
}
|
|
order_filter = new OrdersSubscriptionFilter(
|
|
accounts,
|
|
cri,
|
|
statuses);
|
|
return true;
|
|
}
|
|
static string SanitizeQuery(string query) {
|
|
if (string.IsNullOrEmpty(query)) return query ?? "";
|
|
// replace fancy quotes with straight quotes
|
|
return query.Replace('“', '"').Replace('”', '"');
|
|
}
|
|
|
|
static bool RemoveNonlMarker(ref string query) {
|
|
if (string.IsNullOrEmpty(query)) return false;
|
|
// remove occurrences of #NONL case-insensitively and word-boundary aware
|
|
// preserves the rest of the string, then trim whitespace
|
|
string newQuery = Regex.Replace(query, @"(?i)\#NONL\b", "");
|
|
bool hadMarker = !string.Equals(newQuery, query, StringComparison.Ordinal);
|
|
query = newQuery.Trim();
|
|
return hadMarker;
|
|
}
|
|
|
|
static string EscapeForNl(string query) {
|
|
if (string.IsNullOrEmpty(query)) return query ?? "";
|
|
// Escape backslash first, then escape double quotes so they are safe inside nl("...")
|
|
return query.Replace("\\", "\\\\").Replace("\"", "\\\"");
|
|
}
|
|
static bool TryGetDataStream(
|
|
WebsocketRequest req,
|
|
IDataStreamFactory streamFactory,
|
|
[NotNullWhen(true)] out ViewStream? stream,
|
|
[NotNullWhen(false)] out string error
|
|
) {
|
|
stream = null;
|
|
error = "";
|
|
var query = (req.parameters?["query"] as JValue)?.Value<string>();
|
|
if (query is null) {
|
|
error = "'params.query' field not found (expected a string)";
|
|
return false;
|
|
}
|
|
// 1. Sanitize
|
|
query = SanitizeQuery(query);
|
|
|
|
// 2. Check for #NONL marker (case-insensitive) and remove it
|
|
bool hasNonl = RemoveNonlMarker(ref query);
|
|
|
|
// 3. Wrap unless #NONL present
|
|
if (!hasNonl) {
|
|
// escape backslashes and double-quotes for embedding inside nl("...")
|
|
var escaped = EscapeForNl(query);
|
|
query = $"nl(\"{escaped}\")";
|
|
}
|
|
var exp = DataStreamExpressionsInterpreter.Evaluate(query);
|
|
if (exp is null) {
|
|
error = "'params.query' expression has invalid syntax";
|
|
return false;
|
|
}
|
|
Exception? exception = null;
|
|
try {
|
|
stream = exp.getView(new Dictionary<string, StatementExpression>(0), streamFactory);
|
|
} catch (Exception ex) {
|
|
exception = ex;
|
|
}
|
|
if (stream is null) {
|
|
error = "Failed to create ViewStream from 'params.query' expression (check for typos in arguments/typenames)";
|
|
if (exception is not null) {
|
|
error += ". " + exception.Message;
|
|
}
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
static bool TryGetFilter(WebsocketRequest req, [NotNullWhen(true)] out SecurityCriteria? cri, out string message) {
|
|
cri = null;
|
|
if (req.parameters?["filter"] is not JObject filter) {
|
|
message = "filter not present";
|
|
return false;
|
|
}
|
|
if (
|
|
!WebsocketJsonParsers.TryParse<Venue>(filter, "venue", out Venue[]? exchanges, out message) ||
|
|
!WebsocketJsonParsers.TryParse<SecurityType>(filter, "kind", out SecurityType[]? types, out message) ||
|
|
!WebsocketJsonParsers.TryParse<PutOrCall>(filter, "call_or_put", out PutOrCall[]? callputs, out message) ||
|
|
!WebsocketJsonParsers.TryParse<DateTime>(filter, "expiration", out DateTime[]? expiration, out message) ||
|
|
!WebsocketJsonParsers.TryParse<WebsocketJsonParsers.StringListWrapper>(filter, "symbol", out WebsocketJsonParsers.StringListWrapper? symbols, out message) ||
|
|
!WebsocketJsonParsers.TryParse<WebsocketJsonParsers.StringListWrapper>(filter, "name", out WebsocketJsonParsers.StringListWrapper? names, out message) ||
|
|
!WebsocketJsonParsers.TryParse<double>(filter, "strikes", out double[]? strikes, out message)
|
|
) {
|
|
return false;
|
|
}
|
|
var clause = new SecurityCriteriaClause(
|
|
exchanges,
|
|
types,
|
|
symbols?.getuprarr(),
|
|
expiration is null
|
|
? Array.Empty<ExpirationDate>()
|
|
: Array.ConvertAll(expiration, ExpirationDate.FixedDate),
|
|
null,
|
|
null,
|
|
strikes,
|
|
callputs,
|
|
names?.getuprarr()
|
|
);
|
|
var cls = new List<SecurityCriteriaClause> { clause };
|
|
cri = new SecurityCriteria(cls);
|
|
return true;
|
|
}
|
|
|
|
public static bool TryCreate(
|
|
WebsocketRequest req,
|
|
MarketDataClient md,
|
|
OrdersClient oc,
|
|
IDataStreamFactory streamFactory,
|
|
Action<Object> sm,
|
|
[NotNullWhen(true)] out WebsocketSubscriptionHandler? hdlr,
|
|
out string status
|
|
) {
|
|
hdlr = null;
|
|
|
|
if (
|
|
!WebsocketJsonParsers.TryParse<SUBSCRIPTION_TYPE>(req.parameters, "type", out SUBSCRIPTION_TYPE? type, out status) || status != "" ||
|
|
!WebsocketJsonParsers.TryParse<WebsocketJsonParsers.StringWrapper>(req.parameters, "subscription_id", out WebsocketJsonParsers.StringWrapper? id, out status) || id == null) {
|
|
return false;
|
|
}
|
|
|
|
Action<WebsocketSubscriptionHandler> sub;
|
|
switch (type) {
|
|
case SUBSCRIPTION_TYPE.BOOKS: {
|
|
if (!TryGetFilter(req, out var cri, out status)) {
|
|
return false;
|
|
}
|
|
sub = GetBookSubscriber(cri);
|
|
break;
|
|
}
|
|
case SUBSCRIPTION_TYPE.PRICE_STATS: {
|
|
if (!TryGetFilter(req, out var cri, out status)) {
|
|
return false;
|
|
}
|
|
sub = GetPriceStatsSubscriber(cri);
|
|
break;
|
|
}
|
|
case SUBSCRIPTION_TYPE.PRICE_TICKERS: {
|
|
if (!TryGetFilter(req, out var cri, out status)) {
|
|
return false;
|
|
}
|
|
sub = GetPriceTickerSubscriber(cri);
|
|
break;
|
|
}
|
|
case SUBSCRIPTION_TYPE.TRADES: {
|
|
if (!TryGetTradesFilter(req, out var cri, out status)) {
|
|
return false;
|
|
}
|
|
sub = GetTradesSubscriber(cri);
|
|
break;
|
|
}
|
|
case SUBSCRIPTION_TYPE.SECURITIES: {
|
|
if (!TryGetFilter(req, out var cri, out status)) {
|
|
return false;
|
|
}
|
|
sub = GetSecuritiesSubscriber(cri);
|
|
break;
|
|
}
|
|
case SUBSCRIPTION_TYPE.FILLS: {
|
|
if (!TryGetFillsFilter(req, out var filter, out status)) {
|
|
return false;
|
|
}
|
|
sub = GetFillsSubscriber(filter);
|
|
|
|
break;
|
|
}
|
|
case SUBSCRIPTION_TYPE.ORDERS: {
|
|
if (!TryGetOrdersFilter(req, out var filter, out status)) {
|
|
return false;
|
|
}
|
|
sub = GetOrdersSubscriber(filter);
|
|
break;
|
|
}
|
|
case SUBSCRIPTION_TYPE.DATA_STREAM: {
|
|
// TODO: make this nullable
|
|
if (!TryGetDataStream(req, streamFactory, out var filter, out status)) {
|
|
return false;
|
|
}
|
|
sub = GetDataStreamSubscriber(filter, id.Value.Val);
|
|
break;
|
|
}
|
|
default: {
|
|
status = "Incorrect subscription type";
|
|
return false;
|
|
}
|
|
}
|
|
hdlr = new WebsocketSubscriptionHandler(id.Value.Val, md, oc, sm, sub);
|
|
status = $"{req.id.ToString()}";
|
|
return true;
|
|
}
|
|
|
|
|
|
private WebsocketSubscriptionHandler(
|
|
string id,
|
|
MarketDataClient md,
|
|
OrdersClient oc,
|
|
Action<Object> sm,
|
|
Action<WebsocketSubscriptionHandler> sub) {
|
|
_sm = sm;
|
|
_MdClient = md;
|
|
_OrdersClient = oc;
|
|
ID = new UniqueID(id);
|
|
subscribe = () => sub(this);
|
|
}
|
|
|
|
}
|
|
}
|