hooked subscriptions up
This commit is contained in:
1 parent
a0bd207d93
commit
390d901463
10 files changed
+301
-81
No files matched your search
@@ -0,0 +1,5 @@
|
||||
namespace bts.api {
|
||||
public interface IJsonFormatter<T> {
|
||||
static abstract System.Object ToJsonObj(T obj);
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,7 @@ namespace Spark.Api.MarketData {
|
||||
|
||||
using System;
|
||||
|
||||
public readonly struct BookTuple : IEquatable<BookTuple> {
|
||||
public readonly struct BookTuple : IEquatable<BookTuple>, IJsonFormatter<BookTuple> {
|
||||
readonly TickDisplaySettingsImmutable? _quantity_display;
|
||||
|
||||
public BookTuple(
|
||||
@@ -35,9 +35,9 @@ namespace Spark.Api.MarketData {
|
||||
|
||||
public override int GetHashCode() =>
|
||||
security.Id.GetHashCode();
|
||||
public Object ToJsonObj() => new {
|
||||
secuirty = security.ToString(),
|
||||
book = book.ToJsonObj()
|
||||
public static Object ToJsonObj(BookTuple obj) => new {
|
||||
secuirty = obj.security.ToString(),
|
||||
book = obj.book.ToJsonObj()
|
||||
};
|
||||
public static bool operator ==(BookTuple left, BookTuple right) =>
|
||||
left.Equals(right);
|
||||
|
||||
@@ -7,7 +7,7 @@ namespace Spark.Api.MarketData {
|
||||
/// <summary>
|
||||
/// Price/trade statistics for a particular security (slow moving)
|
||||
/// </summary>
|
||||
public readonly struct PriceStats : IEquatable<PriceStats> {
|
||||
public readonly struct PriceStats : IEquatable<PriceStats>, IJsonFormatter<PriceStats> {
|
||||
public PriceStats(SecurityImmutable security, double open, double close, double settlement) {
|
||||
this.security = security;
|
||||
this.open = open;
|
||||
@@ -37,5 +37,11 @@ namespace Spark.Api.MarketData {
|
||||
|
||||
public static bool operator !=(PriceStats left, PriceStats right) =>
|
||||
!(left == right);
|
||||
public static System.Object ToJsonObj(PriceStats obj) => new {
|
||||
open = obj.open,
|
||||
close = obj.close,
|
||||
settlement = obj.settlement,
|
||||
security = obj.security.Exchange.ToString() + "." + obj.security.ToString()
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -10,7 +10,7 @@ namespace Spark.Api.MarketData {
|
||||
/// <summary>
|
||||
/// Price/trade statistics for a particular security (fast moving)
|
||||
/// </summary>
|
||||
public readonly struct PriceTicker : IEquatable<PriceTicker> {
|
||||
public readonly struct PriceTicker : IEquatable<PriceTicker>, IJsonFormatter<PriceTicker> {
|
||||
public PriceTicker(
|
||||
SecurityImmutable security,
|
||||
TickDisplaySettingsImmutable quantity_display,
|
||||
@@ -91,5 +91,18 @@ namespace Spark.Api.MarketData {
|
||||
|
||||
public static bool operator !=(PriceTicker left, PriceTicker right) =>
|
||||
!(left == right);
|
||||
public static System.Object ToJsonObj(PriceTicker obj) => new {
|
||||
security = obj.security.Exchange.ToString() + "." + obj.security.ToString(),
|
||||
high = obj.high,
|
||||
low = obj.low,
|
||||
volume = obj.volume,
|
||||
bid = obj.bid,
|
||||
bid_quantity = obj.bid_quantity,
|
||||
ask = obj.ask,
|
||||
ask_quantity = obj.ask_quantity,
|
||||
mark = obj.mark,
|
||||
last = obj.last,
|
||||
last_quantity = obj.last_quantity
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,7 @@ using mbts.analytics;
|
||||
using Spark.Core;
|
||||
|
||||
namespace mbts.spark {
|
||||
public readonly struct TradeInfo {
|
||||
public readonly struct TradeInfo : bts.api.IJsonFormatter<TradeInfo> {
|
||||
public readonly SecurityImmutable security;
|
||||
public readonly TradeMessageImmutable trade;
|
||||
public readonly GreeksTuple? greeks_tuple;
|
||||
@@ -22,6 +22,13 @@ namespace mbts.spark {
|
||||
this.greeks_tuple = greeks_tuple;
|
||||
this.volume = volume;
|
||||
}
|
||||
public static System.Object ToJsonObj(TradeInfo obj) => new {
|
||||
secuirty = obj.security.Exchange.ToString() + "." + obj.security.ToString(),
|
||||
price = obj.trade.price(),
|
||||
timestamp = obj.trade.Time,
|
||||
quantity = obj.volume,
|
||||
agg_side = obj.trade.AggressorSide.ToString()
|
||||
};
|
||||
}
|
||||
|
||||
public static class TradeInfoExtensions {
|
||||
|
||||
@@ -10,7 +10,7 @@ using System;
|
||||
using static bts.api.orderman.Fill.Types;
|
||||
|
||||
namespace mbts.spark {
|
||||
public sealed class FillTuple : IEquatable<FillTuple> {
|
||||
public sealed class FillTuple : IEquatable<FillTuple>, IJsonFormatter<FillTuple> {
|
||||
public static readonly Func<FillTuple, string> key_fn = ft => ft.fill.FillKey;
|
||||
public readonly FillImmutable fill;
|
||||
public readonly SecurityImmutable? security;
|
||||
@@ -79,6 +79,11 @@ namespace mbts.spark {
|
||||
fill.FillKey.GetHashCode(),
|
||||
order.HasValue ? order.Value.Key.GetHashCode() : 0,
|
||||
security.HasValue ? security.Value.Id.GetHashCode() : 0);
|
||||
public static Object ToJsonObj(FillTuple obj) => new {
|
||||
fill_avg_price = obj.fill.AvgPrice,
|
||||
security = obj.security.ToString(),
|
||||
order = obj._order_tuple != null ? OrderRecordTupleWithRoot.ToJsonObj((OrderRecordTupleWithRoot)obj._order_tuple) : ""
|
||||
};
|
||||
}
|
||||
|
||||
static class FillTupleExtensions {
|
||||
@@ -99,7 +104,7 @@ namespace mbts.spark {
|
||||
|
||||
public static string user(this FillTuple fill_tuple) =>
|
||||
fill_tuple.order.HasValue ? fill_tuple.order.Value.user() : string.Empty;
|
||||
|
||||
|
||||
public static uint user_id(this FillTuple fill_tuple) =>
|
||||
fill_tuple.order?.user_id() ?? 0;
|
||||
}
|
||||
|
||||
@@ -53,8 +53,8 @@ namespace Spark.Api.Orders {
|
||||
public static bool operator !=(OrderRecordTuple left, OrderRecordTuple right) =>
|
||||
!(left == right);
|
||||
}
|
||||
|
||||
public readonly struct OrderRecordTupleWithRoot : IEquatable<OrderRecordTupleWithRoot> {
|
||||
|
||||
public readonly struct OrderRecordTupleWithRoot : IEquatable<OrderRecordTupleWithRoot>, IJsonFormatter<OrderRecordTupleWithRoot> {
|
||||
readonly OrderRecordTuple? _root;
|
||||
|
||||
public OrderRecordTupleWithRoot(OrderRecordTuple order, OrderRecordTuple? root = null) {
|
||||
@@ -77,7 +77,18 @@ namespace Spark.Api.Orders {
|
||||
|
||||
public override int GetHashCode() =>
|
||||
HashCode.Combine(order, _root);
|
||||
|
||||
public static Object ToJsonObj(OrderRecordTupleWithRoot obj) => new {
|
||||
key = obj.key,
|
||||
root_key = obj.root.key,
|
||||
security = obj.order.security.ToString(),
|
||||
parent_key = obj.order.order.ParentKey,
|
||||
kind = obj.order.algo_type.display_name(),
|
||||
price = obj.order.order.price(), //how to get this
|
||||
quantity = obj.order.order.quantity(),
|
||||
side = obj.order.order.side(),
|
||||
status = obj.order.order.status(),
|
||||
status_message = obj.order.order.last_msg()
|
||||
};
|
||||
public static bool operator ==(OrderRecordTupleWithRoot left, OrderRecordTupleWithRoot right) =>
|
||||
left.Equals(right);
|
||||
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
using static bts.api.Security;
|
||||
|
||||
using security_key = System.UInt32;
|
||||
|
||||
namespace bts.api {
|
||||
@@ -8,7 +10,7 @@ namespace bts.api {
|
||||
using TickDisplaySettingsImmutable = domain.TickDisplaySettingsImmutable;
|
||||
|
||||
public partial class Security {
|
||||
public static security_key key(Security s) { return s.Id; }
|
||||
public static security_key key(Security s) { return s.Id; }
|
||||
|
||||
public static bool is_perpetual_exp(DateTime exp) =>
|
||||
exp.Year < 2000 || exp.Year > 2999;
|
||||
@@ -84,7 +86,7 @@ namespace bts.api {
|
||||
}
|
||||
}
|
||||
|
||||
partial struct SecurityImmutable : IComparable<SecurityImmutable>, IComparable {
|
||||
partial struct SecurityImmutable : IComparable<SecurityImmutable>, IComparable, IJsonFormatter<SecurityImmutable> {
|
||||
public int CompareTo(object obj) =>
|
||||
obj is SecurityImmutable ? CompareTo((SecurityImmutable)obj) : -1;
|
||||
|
||||
@@ -95,7 +97,9 @@ namespace bts.api {
|
||||
result = SecurityFormatter.format(this);
|
||||
didrun = true;
|
||||
}
|
||||
|
||||
public static Object ToJsonObj(SecurityImmutable s) {
|
||||
return s.Exchange.ToString() + "." + s.ToString();
|
||||
}
|
||||
partial void EqualsOverride(ref bool didrun, ref bool result, SecurityImmutable other) {
|
||||
didrun = true;
|
||||
result = Id == other.Id;
|
||||
@@ -106,22 +110,22 @@ namespace bts.api {
|
||||
/// exchange securities
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
internal IEnumerable<(security_key, Side, double mult)> get_base_exchange_securities () {
|
||||
List<(uint, Side, double)> rv = new List<(uint,Side,double)> ();
|
||||
get_base_exchange_securities (this, Side.Buy, 1.0, ref rv);
|
||||
internal IEnumerable<(security_key, Side, double mult)> get_base_exchange_securities() {
|
||||
List<(uint, Side, double)> rv = new List<(uint, Side, double)>();
|
||||
get_base_exchange_securities(this, Side.Buy, 1.0, ref rv);
|
||||
return rv;
|
||||
}
|
||||
|
||||
static void get_base_exchange_securities (SecurityImmutable sec, Side side, double mult, ref List<(security_key, Side, double)> legs) {
|
||||
static void get_base_exchange_securities(SecurityImmutable sec, Side side, double mult, ref List<(security_key, Side, double)> legs) {
|
||||
Func<Side, Side, Side> resolve_side = (s1, s2) => ((s1 == Side.Buy ? 1 : -1) * (s2 == Side.Buy ? 1 : -1)) == 1 ? Side.Buy : Side.Sell;
|
||||
if (sec.ContractCase == ContractOneofCase.Future)
|
||||
legs.Add ((sec.Id, side, mult));
|
||||
legs.Add((sec.Id, side, mult));
|
||||
else if (sec.ContractCase == ContractOneofCase.Spread)
|
||||
foreach (ContractLegImmutable leg in sec.Spread.Legs)
|
||||
get_base_exchange_securities (leg.Security, resolve_side (side, leg.Side), leg.Quantity * mult, ref legs);
|
||||
get_base_exchange_securities(leg.Security, resolve_side(side, leg.Side), leg.Quantity * mult, ref legs);
|
||||
else if (sec.ContractCase == ContractOneofCase.Synthetic)
|
||||
foreach (ContractLegImmutable leg in sec.Synthetic.Legs)
|
||||
get_base_exchange_securities (leg.Security, resolve_side (side, leg.Side), leg.Quantity * mult, ref legs);
|
||||
get_base_exchange_securities(leg.Security, resolve_side(side, leg.Side), leg.Quantity * mult, ref legs);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -13,6 +13,7 @@ namespace bts.websocket_core {
|
||||
using Spark.Api.Reactive;
|
||||
using Spark.Api.Orders;
|
||||
using Spark.Api.Admin;
|
||||
using bts.api.position;
|
||||
|
||||
|
||||
/// <summary>
|
||||
@@ -112,7 +113,7 @@ namespace bts.websocket_core {
|
||||
// }
|
||||
|
||||
MarketDataClient? mdClient;
|
||||
MarketDataClient MdClient {
|
||||
MarketDataClient Md_Client {
|
||||
get => Log.notnull<MarketDataClient>(mdClient);
|
||||
set {
|
||||
Log.isnull(mdClient);
|
||||
@@ -127,6 +128,14 @@ namespace bts.websocket_core {
|
||||
_md_source = value;
|
||||
}
|
||||
}
|
||||
OrdersClient? _orders_client;
|
||||
OrdersClient Orders_Client {
|
||||
get => Log.notnull<OrdersClient>(_orders_client);
|
||||
set {
|
||||
Log.isnull<OrdersClient>(_orders_client);
|
||||
_orders_client = value;
|
||||
}
|
||||
}
|
||||
WebsocketConnectionGateway? gateway;
|
||||
WebsocketConnectionGateway Gateway {
|
||||
get => Log.notnull<WebsocketConnectionGateway>(gateway);
|
||||
@@ -157,22 +166,22 @@ namespace bts.websocket_core {
|
||||
public void init(WebsocketConnectionGateway gtwy) {
|
||||
Gateway = gtwy;
|
||||
clients = new ClientsStore();
|
||||
// broadcast = new Thread(() => BroadcastToClients());
|
||||
// var service_ctx = new ServiceContext();
|
||||
// var order_service = service_ctx.locate<OrderService>();
|
||||
// var admin_service = service_ctx.locate<AdminService>();
|
||||
// var admin_client = new AdminClient(admin_service, UserTuple.admin);
|
||||
// var md_relay = service_ctx.locate<MarketDataRelay>();
|
||||
// var md_source = new MDSource(TimeSpan.FromSeconds(0.1));
|
||||
// var sec_resolver = new SecurityResolver(md_relay, md_source.subscribe_securities());
|
||||
// var orders_client = new OrdersClient(
|
||||
// order_service, admin_client, sec_resolver, TimeSpan.FromSeconds(0.1));
|
||||
|
||||
|
||||
TimeSpan md_throttle = new TimeSpan(10000);
|
||||
MD_Source = new MDSource(md_throttle);
|
||||
MdClient = new(MD_Source);
|
||||
Md_Client = new(MD_Source);
|
||||
_securities_sub = MD_Source.subscribe_securities().Subscribe(sec_map => _secuirties = sec_map);
|
||||
var service_ctx = new ServiceContext();
|
||||
var order_service = service_ctx.locate<OrderService>();
|
||||
var position_service = service_ctx.locate<PositionService>();
|
||||
var admin_service = service_ctx.locate<AdminService>();
|
||||
var admin_client = new AdminClient(admin_service, UserTuple.admin);
|
||||
var md_relay = service_ctx.locate<MarketDataRelay>();
|
||||
var sec_resolver = new SecurityResolver(md_relay, MD_Source.subscribe_securities());
|
||||
Orders_Client = new OrdersClient(
|
||||
order_service, position_service, admin_client, sec_resolver, TimeSpan.FromSeconds(0.1));
|
||||
|
||||
|
||||
|
||||
// broadcast.Start();
|
||||
}
|
||||
private uint get_security(Venue v, string sec_name) {
|
||||
@@ -249,7 +258,8 @@ namespace bts.websocket_core {
|
||||
string status;
|
||||
if (!WebsocketSubscriptionHandler.TryCreate(
|
||||
req,
|
||||
MdClient,
|
||||
Md_Client,
|
||||
Orders_Client,
|
||||
obj => send_message(obj, req.client_id),
|
||||
out hdlr,
|
||||
out status)) {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
namespace bts.websocket_core {
|
||||
using bts.api;
|
||||
using bts.api.core;
|
||||
using bts.api.orderman;
|
||||
using bts.md;
|
||||
|
||||
using mbts.spark;
|
||||
@@ -9,8 +10,10 @@ namespace bts.websocket_core {
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
using Spark.Api.MarketData;
|
||||
using Spark.Api.Orders;
|
||||
using Spark.Core;
|
||||
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Globalization;
|
||||
|
||||
@@ -24,6 +27,7 @@ namespace bts.websocket_core {
|
||||
|
||||
Action<Object> _sm;
|
||||
private MarketDataClient _MdClient;
|
||||
private OrdersClient _OrdersClient;
|
||||
public void SendMessage(Object _data) {
|
||||
var json = new {
|
||||
jsonrpc = 2.0,
|
||||
@@ -43,9 +47,18 @@ namespace bts.websocket_core {
|
||||
changed = upd.changed.Select(conv)
|
||||
};
|
||||
}
|
||||
private static Object to_json(BookTuple b) {
|
||||
return b.ToJsonObj();
|
||||
private static Object to_json<T>(IList<T> upd, Func<T, Object> conv)
|
||||
where T : IJsonFormatter<T> {
|
||||
return upd.Select(item => conv(item));
|
||||
}
|
||||
public static Object to_json<T, TKey>(TableUpdate<T, TKey> table)
|
||||
where TKey : IEquatable<TKey>
|
||||
where T : IJsonFormatter<T> =>
|
||||
to_json(table, T.ToJsonObj);
|
||||
public static Object to_json<T>(IList<T> list)
|
||||
where T : IJsonFormatter<T> =>
|
||||
to_json(list, T.ToJsonObj);
|
||||
|
||||
private static Object to_json(SecurityImmutable s) {
|
||||
return s.Exchange.ToString() + "." + s.ToString();
|
||||
}
|
||||
@@ -53,14 +66,49 @@ namespace bts.websocket_core {
|
||||
return (WebsocketSubscriptionHandler hdlr) => {
|
||||
hdlr.dis = hdlr._MdClient.securities_incremental(cri).Subscribe(
|
||||
upd =>
|
||||
hdlr.SendMessage(to_json<SecurityImmutable, uint>(upd, to_json)));
|
||||
hdlr.SendMessage(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(to_json<TradeInfo>(upd)));
|
||||
};
|
||||
}
|
||||
private static Action<WebsocketSubscriptionHandler> GetBookSubscriber(SecurityCriteria cri) {
|
||||
return (WebsocketSubscriptionHandler hdlr) => {
|
||||
hdlr.dis = hdlr._MdClient.books(cri).Subscribe(
|
||||
upd =>
|
||||
hdlr.SendMessage(to_json<BookTuple, uint>(upd, to_json)));
|
||||
hdlr.SendMessage(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(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(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(to_json<OrderRecordTupleWithRoot, uint>(upd)));
|
||||
};
|
||||
}
|
||||
private static Action<WebsocketSubscriptionHandler> GetFillsSubscriber(FillsSubscriptionFilter cri) {
|
||||
return (WebsocketSubscriptionHandler hdlr) => {
|
||||
hdlr.dis = hdlr._OrdersClient.fills(cri).Subscribe(
|
||||
upd =>
|
||||
hdlr.SendMessage(to_json<FillTuple, string>(upd)));
|
||||
};
|
||||
}
|
||||
public Action subscribe;
|
||||
@@ -69,12 +117,78 @@ namespace bts.websocket_core {
|
||||
ORDER,
|
||||
TRADES,
|
||||
SECURITY,
|
||||
PRICE_STATS,
|
||||
PRICE_TICKER,
|
||||
FILLS,
|
||||
UNKNOWN = 0
|
||||
}
|
||||
void set_invalid(string message) {
|
||||
Valid = false;
|
||||
Error_message = message;
|
||||
}
|
||||
static bool TryGetTradesFilter(WebsocketRequest req, [NotNullWhen(true)] out TradesSubscriptionFilter? trades_filter, out string message) {
|
||||
SecurityCriteria? cri;
|
||||
trades_filter = null;
|
||||
JObject? filter = req.parameters?["filter"] as JObject;
|
||||
if (filter == null) {
|
||||
message = "filter not present";
|
||||
return false;
|
||||
}
|
||||
if (!TryGetCriteria(req, out 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) {
|
||||
SecurityCriteria? cri;
|
||||
order_filter = null;
|
||||
JObject? filter = req.parameters?["filter"] as JObject;
|
||||
if (filter == null) {
|
||||
message = "filter not present";
|
||||
return false;
|
||||
}
|
||||
if (!TryGetCriteria(req, out cri, out message)) {
|
||||
return false;
|
||||
}
|
||||
uint[]? accounts;
|
||||
if (!TryParse<uint>(filter, "accounts", out accounts, out message)) {
|
||||
return false;
|
||||
}
|
||||
OrderStatus[]? statuses;
|
||||
if (!TryParse<OrderStatus>(filter, "statuses", out 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) {
|
||||
SecurityCriteria? cri;
|
||||
order_filter = null;
|
||||
JObject? filter = req.parameters?["filter"] as JObject;
|
||||
if (filter == null) {
|
||||
message = "filter not present";
|
||||
return false;
|
||||
}
|
||||
if (!TryGetCriteria(req, out cri, out message)) {
|
||||
return false;
|
||||
}
|
||||
uint[]? accounts;
|
||||
if (!TryParse<uint>(filter, "accounts", out accounts, out message)) {
|
||||
return false;
|
||||
}
|
||||
OrderStatus[]? statuses;
|
||||
if (!TryParse<OrderStatus>(filter, "statuses", out statuses, out message)) {
|
||||
return false;
|
||||
}
|
||||
order_filter = new OrdersSubscriptionFilter(
|
||||
accounts,
|
||||
cri,
|
||||
statuses);
|
||||
return true;
|
||||
}
|
||||
static bool TryGetCriteria(WebsocketRequest req, [NotNullWhen(true)] out SecurityCriteria? cri, out string message) {
|
||||
cri = null;
|
||||
JObject? filter = req.parameters?["filter"] as JObject;
|
||||
@@ -98,6 +212,7 @@ namespace bts.websocket_core {
|
||||
if (!TryParse<DateTime>(filter, "expiration", out expiration, out message)) {
|
||||
return false;
|
||||
}
|
||||
// string[]? symbols = (string[]?)(filter.GetValueOrDefault("symbol")?.ToString()?.Split(",").Select(s => s.ToUpper()));
|
||||
string[]? symbols = filter.GetValueOrDefault("symbol")?.ToString()?.Split(",");
|
||||
double[]? strikes;
|
||||
if (!TryParse<double>(filter, "call_or_put", out strikes, out message)) {
|
||||
@@ -126,6 +241,39 @@ namespace bts.websocket_core {
|
||||
list = null;
|
||||
return false;
|
||||
}
|
||||
public static bool TryParse<T>(string value, string key, [NotNullWhen(true)] out T? obj, [NotNull] out string error) where T : struct {
|
||||
error = "";
|
||||
if (typeof(T).IsEnum && Enum.TryParse<T>(value, true, out var result)) {
|
||||
obj = result;
|
||||
return true;
|
||||
}
|
||||
if (typeof(T) == typeof(double) && double.TryParse(value, out var d)) {
|
||||
obj = (T)(object)d;
|
||||
return true;
|
||||
|
||||
}
|
||||
if (typeof(T) == typeof(uint) && uint.TryParse(value, out var uin)) {
|
||||
obj = (T)(object)uin;
|
||||
return true;
|
||||
}
|
||||
if (typeof(T) == typeof(string)) {
|
||||
obj = (T)(object)value;
|
||||
return true;
|
||||
}
|
||||
if (typeof(T) == typeof(List<string>)) {
|
||||
|
||||
|
||||
}
|
||||
if (typeof(T) == typeof(DateTime) && DateTime.TryParseExact(value, "yyyy/MM/dd", CultureInfo.InvariantCulture,
|
||||
DateTimeStyles.None, out var dt)) {
|
||||
obj = (T)(object)dt;
|
||||
return true;
|
||||
|
||||
}
|
||||
error = $"{key} is not in the correct format";
|
||||
obj = 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();
|
||||
@@ -133,40 +281,11 @@ namespace bts.websocket_core {
|
||||
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;
|
||||
return TryParse<T>(value, key, out obj, out error);
|
||||
|
||||
}
|
||||
public static bool TryCreate(
|
||||
WebsocketRequest req, MarketDataClient md,
|
||||
WebsocketRequest req, MarketDataClient md, OrdersClient oc,
|
||||
Action<Object> sm,
|
||||
[NotNullWhen(true)] out WebsocketSubscriptionHandler? hdlr,
|
||||
out string status) {
|
||||
@@ -179,7 +298,7 @@ namespace bts.websocket_core {
|
||||
JToken? temp = null;
|
||||
if (
|
||||
(!req.parameters?.TryGetValue(
|
||||
"subscriptionid",
|
||||
"subscription_id",
|
||||
StringComparison.InvariantCultureIgnoreCase,
|
||||
out temp) ?? false)
|
||||
|| temp == null) {
|
||||
@@ -196,17 +315,39 @@ namespace bts.websocket_core {
|
||||
return false;
|
||||
}
|
||||
sub = GetBookSubscriber(cri);
|
||||
hdlr = new WebsocketSubscriptionHandler(id, md, sm, sub);
|
||||
hdlr = new WebsocketSubscriptionHandler(id, md, oc, sm, sub);
|
||||
status = $"{req.id.ToString()}";
|
||||
return true;
|
||||
}
|
||||
case SUBSCRIPTION_TYPE.TRADES: {
|
||||
case SUBSCRIPTION_TYPE.PRICE_STATS: {
|
||||
SecurityCriteria? cri;
|
||||
if (!TryGetCriteria(req, out cri, out status)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
break;
|
||||
sub = GetPriceStatsSubscriber(cri);
|
||||
hdlr = new WebsocketSubscriptionHandler(id, md, oc, sm, sub);
|
||||
status = $"{req.id.ToString()}";
|
||||
return true;
|
||||
}
|
||||
case SUBSCRIPTION_TYPE.PRICE_TICKER: {
|
||||
SecurityCriteria? cri;
|
||||
if (!TryGetCriteria(req, out cri, out status)) {
|
||||
return false;
|
||||
}
|
||||
sub = GetPriceTickerSubscriber(cri);
|
||||
hdlr = new WebsocketSubscriptionHandler(id, md, oc, sm, sub);
|
||||
status = $"{req.id.ToString()}";
|
||||
return true;
|
||||
}
|
||||
case SUBSCRIPTION_TYPE.TRADES: {
|
||||
TradesSubscriptionFilter? cri;
|
||||
if (!TryGetTradesFilter(req, out cri, out status)) {
|
||||
return false;
|
||||
}
|
||||
sub = GetTradesSubscriber(cri);
|
||||
hdlr = new WebsocketSubscriptionHandler(id, md, oc, sm, sub);
|
||||
status = $"{req.id.ToString()}";
|
||||
return true;
|
||||
}
|
||||
case SUBSCRIPTION_TYPE.SECURITY: {
|
||||
SecurityCriteria? cri;
|
||||
@@ -214,27 +355,45 @@ namespace bts.websocket_core {
|
||||
return false;
|
||||
}
|
||||
sub = GetSecuritiesSubscriber(cri);
|
||||
hdlr = new WebsocketSubscriptionHandler(id, md, sm, sub);
|
||||
hdlr = new WebsocketSubscriptionHandler(id, md, oc, sm, sub);
|
||||
status = $"{req.id.ToString()}";
|
||||
// HandleBookSubsription(cri);
|
||||
return true;
|
||||
}
|
||||
case SUBSCRIPTION_TYPE.FILLS: {
|
||||
FillsSubscriptionFilter? filter;
|
||||
if (!TryGetFillsFilter(req, out filter, out status)) {
|
||||
return false;
|
||||
}
|
||||
sub = GetFillsSubscriber(filter);
|
||||
hdlr = new WebsocketSubscriptionHandler(id, md, oc, sm, sub);
|
||||
status = $"{req.id.ToString()}";
|
||||
return true;
|
||||
|
||||
}
|
||||
case SUBSCRIPTION_TYPE.ORDER: {
|
||||
break;
|
||||
OrdersSubscriptionFilter? filter;
|
||||
if (!TryGetOrdersFilter(req, out filter, out status)) {
|
||||
return false;
|
||||
}
|
||||
sub = GetOrdersSubscriber(filter);
|
||||
hdlr = new WebsocketSubscriptionHandler(id, md, oc, sm, sub);
|
||||
status = $"{req.id.ToString()}";
|
||||
return true;
|
||||
}
|
||||
default: {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
private WebsocketSubscriptionHandler(
|
||||
uint id,
|
||||
MarketDataClient md,
|
||||
OrdersClient oc,
|
||||
Action<Object> sm,
|
||||
Action<WebsocketSubscriptionHandler> sub) {
|
||||
_sm = sm;
|
||||
_MdClient = md;
|
||||
_OrdersClient = oc;
|
||||
ID = new UniqueID((uint)id);
|
||||
subscribe = () => sub(this);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user