244 lines
8.0 KiB
C#
244 lines
8.0 KiB
C#
namespace bts.websocket_core {
|
|
using bts.api;
|
|
using bts.api.core;
|
|
using bts.md;
|
|
|
|
using mbts.spark;
|
|
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
using Spark.Api.MarketData;
|
|
using Spark.Core;
|
|
|
|
using System.Diagnostics.CodeAnalysis;
|
|
using System.Globalization;
|
|
|
|
public class WebsocketSubscriptionHandler : IWebsocketRequestHandler {
|
|
public IDisposable? dis;
|
|
public void Dispose() => dis?.Dispose();
|
|
public bool Valid { get; set; } = true;
|
|
public string Error_message { get; set; } = "";
|
|
public IUniqueID ID { get; }
|
|
|
|
|
|
Action<Object> _sm;
|
|
private MarketDataClient _MdClient;
|
|
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 Object to_json<T, TKey>(TableUpdate<T, TKey> upd, Func<T, Object> conv) where TKey : IEquatable<TKey> {
|
|
return new {
|
|
added = upd.added.Select(conv),
|
|
removed = upd.removed.Select(conv),
|
|
changed = upd.changed.Select(conv)
|
|
};
|
|
}
|
|
private static Object to_json(BookTuple b) {
|
|
return b.ToJsonObj();
|
|
}
|
|
private static Object to_json(SecurityImmutable s) {
|
|
return s.Exchange.ToString() + "." + s.ToString();
|
|
}
|
|
private static Action<WebsocketSubscriptionHandler> GetSecuritiesSubscriber(SecurityCriteria cri) {
|
|
return (WebsocketSubscriptionHandler hdlr) => {
|
|
hdlr.dis = hdlr._MdClient.securities_incremental(cri).Subscribe(
|
|
upd =>
|
|
hdlr.SendMessage(to_json<SecurityImmutable, uint>(upd, to_json)));
|
|
};
|
|
}
|
|
private static Action<WebsocketSubscriptionHandler> GetBookSubscriber(SecurityCriteria cri) {
|
|
return (WebsocketSubscriptionHandler hdlr) => {
|
|
hdlr.dis = hdlr._MdClient.market_data(cri).Subscribe(
|
|
upd =>
|
|
hdlr.SendMessage(to_json<BookTuple, uint>(upd, to_json)));
|
|
};
|
|
}
|
|
public Action subscribe;
|
|
enum SUBSCRIPTION_TYPE {
|
|
BOOK = 1,
|
|
ORDER,
|
|
TRADES,
|
|
SECURITY,
|
|
UNKNOWN = 0
|
|
}
|
|
void set_invalid(string message) {
|
|
Valid = false;
|
|
Error_message = message;
|
|
}
|
|
static bool TryGetCriteria(WebsocketRequest req, [NotNullWhen(true)] out SecurityCriteria? cri, out string message) {
|
|
cri = null;
|
|
JObject? filter = req.parameters?["filter"] as JObject;
|
|
if (filter == null) {
|
|
message = "filter not present";
|
|
return false;
|
|
}
|
|
Venue[]? exchanges;
|
|
if (!TryParse<Venue>(filter, "venue", out exchanges, out message)) {
|
|
return false;
|
|
}
|
|
SecurityType[]? types;
|
|
if (!TryParse<SecurityType>(filter, "kind", out types, out message)) {
|
|
return false;
|
|
}
|
|
PutOrCall[]? callputs;
|
|
if (!TryParse<PutOrCall>(filter, "call_or_put", out callputs, out message)) {
|
|
return false;
|
|
}
|
|
DateTime[]? expiration;
|
|
if (!TryParse<DateTime>(filter, "expiration", out expiration, out message)) {
|
|
return false;
|
|
}
|
|
string[]? symbols = filter.GetValueOrDefault("symbol")?.ToString()?.Split(",");
|
|
double[]? strikes;
|
|
if (!TryParse<double>(filter, "call_or_put", out strikes, out message)) {
|
|
return false;
|
|
}
|
|
var clause = new SecurityCriteriaClause(
|
|
exchanges,
|
|
types,
|
|
symbols,
|
|
expiration,
|
|
null,
|
|
null,
|
|
strikes,
|
|
callputs
|
|
);
|
|
var cls = new List<SecurityCriteriaClause> { clause };
|
|
cri = new SecurityCriteria(cls);
|
|
return true;
|
|
}
|
|
public static bool TryParse<T>(JObject filter, string key, out T[]? list, out string error) where T : struct {
|
|
T? obj;
|
|
if (TryParse<T>(filter, key, out obj, out error)) {
|
|
list = obj == null ? null : new T[] { (T)(object)obj };
|
|
return true;
|
|
}
|
|
list = null;
|
|
return false;
|
|
}
|
|
public static bool TryParse<T>(JObject filter, string key, out T? obj, [NotNullWhen(false)] out string error) where T : struct {
|
|
obj = null;
|
|
var value = filter.GetValueOrDefault(key)?.ToString();
|
|
if (value == null) {
|
|
error = $"{key} not present";
|
|
return true;
|
|
}
|
|
if (typeof(T).IsEnum) {
|
|
if (Enum.TryParse<T>(value, true, out var result)) {
|
|
obj = result;
|
|
error = "";
|
|
return true;
|
|
}
|
|
} else if (typeof(T) == typeof(double)) {
|
|
double d;
|
|
if (double.TryParse(value, out d)) {
|
|
obj = (T)(object)d;
|
|
error = "";
|
|
return true;
|
|
}
|
|
} else if (typeof(T) == typeof(string)) {
|
|
obj = (T)(object)value;
|
|
error = "";
|
|
return true;
|
|
} else if (typeof(T) == typeof(DateTime)) {
|
|
DateTime dt;
|
|
if (DateTime.TryParseExact(value, "yyyy/MM/dd", CultureInfo.InvariantCulture,
|
|
DateTimeStyles.None, out dt)) {
|
|
obj = (T)(object)dt;
|
|
error = "";
|
|
return true;
|
|
}
|
|
}
|
|
|
|
error = $"{key} is not in the correct format";
|
|
obj = default(T);
|
|
return false;
|
|
|
|
}
|
|
public static bool TryCreate(
|
|
WebsocketRequest req, MarketDataClient md,
|
|
Action<Object> sm,
|
|
[NotNullWhen(true)] out WebsocketSubscriptionHandler? hdlr,
|
|
out string status) {
|
|
hdlr = null;
|
|
|
|
SUBSCRIPTION_TYPE[]? type;
|
|
if (!TryParse<SUBSCRIPTION_TYPE>(req.parameters, "type", out type, out status) || status != "") {
|
|
return false;
|
|
}
|
|
JToken? temp = null;
|
|
if (
|
|
(!req.parameters?.TryGetValue(
|
|
"subscriptionid",
|
|
StringComparison.InvariantCultureIgnoreCase,
|
|
out temp) ?? false)
|
|
|| temp == null) {
|
|
status = "no subscriptionid given";
|
|
|
|
return false;
|
|
}
|
|
uint id = (uint)temp;
|
|
Action<WebsocketSubscriptionHandler> sub;
|
|
switch (type?[0]) {
|
|
case SUBSCRIPTION_TYPE.BOOK: {
|
|
SecurityCriteria? cri;
|
|
if (!TryGetCriteria(req, out cri, out status)) {
|
|
return false;
|
|
}
|
|
sub = GetBookSubscriber(cri);
|
|
hdlr = new WebsocketSubscriptionHandler(id, md, sm, sub);
|
|
status = $"{req.id.ToString()}";
|
|
return true;
|
|
}
|
|
case SUBSCRIPTION_TYPE.TRADES: {
|
|
SecurityCriteria? cri;
|
|
if (!TryGetCriteria(req, out cri, out status)) {
|
|
return false;
|
|
}
|
|
|
|
break;
|
|
}
|
|
case SUBSCRIPTION_TYPE.SECURITY: {
|
|
SecurityCriteria? cri;
|
|
if (!TryGetCriteria(req, out cri, out status)) {
|
|
return false;
|
|
}
|
|
sub = GetSecuritiesSubscriber(cri);
|
|
hdlr = new WebsocketSubscriptionHandler(id, md, sm, sub);
|
|
status = $"{req.id.ToString()}";
|
|
// HandleBookSubsription(cri);
|
|
return true;
|
|
}
|
|
case SUBSCRIPTION_TYPE.ORDER: {
|
|
break;
|
|
}
|
|
default: {
|
|
return false;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
private WebsocketSubscriptionHandler(
|
|
uint id,
|
|
MarketDataClient md,
|
|
Action<Object> sm,
|
|
Action<WebsocketSubscriptionHandler> sub) {
|
|
_sm = sm;
|
|
_MdClient = md;
|
|
ID = new UniqueID((uint)id);
|
|
subscribe = () => sub(this);
|
|
}
|
|
|
|
}
|
|
}
|