client MD refactor

This commit is contained in:
Kevin Darby committed 2018-12-10 12:13:11 -05:00
1 parent 24f0b48f9d
commit e07d602cf5
17 files changed
+187 -137

No files matched your search

@@ -25,6 +25,10 @@ namespace mbts.co2
Co2NativeMethods.set_relay_token(_relay, token.Token);
}
public void set_token(AuthTokenImmutable token) {
Co2NativeMethods.set_relay_token(_relay, token.Token);
}
public async Task preload_securities(MDRequest.Types.Flags flags=MDRequest.Types.Flags.Security) {
var securities = UserSymbol.get_all();
foreach (var future in securities.Where(s => s.type == SecurityType.Future)) {
-3
View File
@@ -54,9 +54,6 @@ namespace bts.md
bool m = (_misc as IMDUpdate).update(smd, ref ml);
last_update_time = new[] { bl, ll, ml }.Min();
if (smd.md.market_timestamp > last_update_time)
{
}
return b || l || m;
}
}
+1 -1
View File
@@ -38,7 +38,7 @@ namespace bts.md
Task.Run(run);
}
public void set_token(AuthToken token)
public void set_token(AuthTokenImmutable token)
{
_relay.set_token(token);
}
+63 -21
View File
@@ -24,8 +24,10 @@ namespace bts.md
readonly ConcurrentDictionary<SecurityKey, SecurityMD> _data = new ConcurrentDictionary<SecurityKey, SecurityMD>();
readonly List<IDisposable> _disp = new List<IDisposable>();
public MarketDataClient(AuthToken token = null)
readonly uint _default_pump_ms;
public MarketDataClient(uint default_pump_ms = 100, AuthToken token = null)
{
_default_pump_ms = default_pump_ms;
if (token != null)
set_auth_token(token);
@@ -35,40 +37,36 @@ namespace bts.md
_data.AddOrUpdate(entry.Key, entry.Value, (_, __) => entry.Value);
}));
}
public void set_auth_token(AuthToken toke)
{
_subscription.set_token(toke);
}
public IEnumerable<SecurityImmutable> securities => _data.Values.Select(x => x.security);
public IObservable<IEnumerable<SecurityImmutable>> on_securities
{
get { return _subscription.on_securities.Select(x => x.Values.Select(xx => xx.security)); }
}
public IObservable<IMDCombined> subscribe_combined(Product product)
public void set_auth_token(AuthTokenImmutable? toke)
{
Func<SecurityImmutable, bool> where = (x => x.Exchange == product.Exchange &&
x.Symbol == product.Symbol && x.security_type() == product.Type);
return subscribe_combined(where);
if(toke.HasValue)
_subscription.set_token(toke.Value);
}
public IObservable<IMDBook> subscribe_book(Func<SecurityImmutable, bool> filter)
public IObservable<IMDBook> subscribe_book(Func<SecurityImmutable, bool> filter,
TimeSpan? update_interval = null, IScheduler scheduler = null)
{
return _subscribe<MDBook>(filter);
return _subscribe<MDBook>(filter, update_interval, scheduler);
}
public IObservable<IMDLast> subscribe_last(Func<SecurityImmutable, bool> filter)
public IObservable<IMDLast> subscribe_last(Func<SecurityImmutable, bool> filter,
TimeSpan? update_interval = null, IScheduler scheduler = null)
{
return _subscribe<MDLast>(filter);
return _subscribe<MDLast>(filter, update_interval, scheduler);
}
public IObservable<IMDMisc> subscribe_misc(Func<SecurityImmutable, bool> filter)
public IObservable<IMDMisc> subscribe_misc(Func<SecurityImmutable, bool> filter,
TimeSpan? update_interval = null, IScheduler scheduler = null)
{
return _subscribe<MDMisc>(filter);
return _subscribe<MDMisc>(filter, update_interval, scheduler);
}
public IObservable<IMDCombined> subscribe_combined(Func<SecurityImmutable, bool> filter)
public IObservable<IMDCombined> subscribe_combined(Func<SecurityImmutable, bool> filter,
TimeSpan? update_interval = null, IScheduler scheduler = null)
{
return _subscribe<MDCombined>(filter);
return _subscribe<MDCombined>(filter, update_interval, scheduler);
}
IObservable<T> _subscribe<T>(Func<SecurityImmutable, bool> filter,
@@ -90,6 +88,50 @@ namespace bts.md
update_interval, scheduler);
}
public IObservable<IEnumerable<IMDBook>> subscribe_book(IEnumerable<SecurityKey> ids, TimeSpan? update_interval = null,
IScheduler scheduler = null)
{
return _subscribe<MDBook>(ids, update_interval, scheduler);
}
public IObservable<IEnumerable<IMDLast>> subscribe_last(IEnumerable<SecurityKey> ids, TimeSpan? update_interval = null,
IScheduler scheduler = null)
{
return _subscribe<MDLast>(ids, update_interval, scheduler);
}
public IObservable<IEnumerable<IMDMisc>> subscribe_misc(IEnumerable<SecurityKey> ids, TimeSpan? update_interval = null,
IScheduler scheduler = null)
{
return _subscribe<MDMisc>(ids, update_interval, scheduler);
}
public IObservable<IEnumerable<IMDCombined>> subscribe_combined(IEnumerable<SecurityKey> ids, TimeSpan? update_interval = null,
IScheduler scheduler = null)
{
return _subscribe<MDCombined>(ids, update_interval, scheduler);
}
IObservable<IEnumerable<T>> _subscribe<T>(IEnumerable<SecurityKey> ids, TimeSpan? update_interval = null,
IScheduler scheduler = null) where T : IMDData, IMDUpdate, new()
{
List<Slot<T>> slots = ids.Where(ii => _data.TryGetValue(ii, out SecurityMD _)).Select(x =>
{
_data.TryGetValue(x, out SecurityMD smd);
return new Slot<T>(smd);
}).ToList();
return timer_pump<IEnumerable<T>>((last_timer_tick, observer) =>
{
List<T> updates = new List<T>();
foreach (var slot in slots)
{
bool did_update = slot.data.update(slot.secmd, ref slot.last_update_time);
if (slot.last_update_time > last_timer_tick)
updates.Add(slot.data);
}
if(updates.Count > 0)
observer.OnNext(updates);
},
update_interval, scheduler);
}
class Slot<T> where T : IMDData, IMDUpdate, new()
{
public Slot(SecurityMD md)
@@ -106,7 +148,7 @@ namespace bts.md
IObservable<T> timer_pump<T>(Action<ulong, IObserver<T>> tick, TimeSpan? interval, IScheduler scheduler = null)
{
if(!interval.HasValue)
interval = TimeSpan.FromMilliseconds(100);
interval = TimeSpan.FromMilliseconds(_default_pump_ms);
ulong last_update_time = 0;
return Observable.Create<T>(observer =>
{
@@ -1,23 +0,0 @@
using System;
using System.Threading.Tasks;
using bts.api;
using bts.api.core;
namespace bts.api
{
public class Authenticator
{
readonly static Lazy<user.AuthService> _auth = new Lazy<user.AuthService>(() => Locator.locate<user.AuthService>(), true);
AuthToken _token = null;
public Authenticator() { }
public AuthToken last_token => _token;
public async Task<AuthToken> login(string login, string password)
{
var res = await _auth.Value.authenticate_login_async(new user.AuthenticateLoginRequest { Login = login, Password = password });
if (res.success)
_token = res.value.Token;
return _token;
}
}
}
+6 -1
View File
@@ -11,6 +11,11 @@ namespace bts.api
public static T locate<T>(string qualifier="") where T : class, IApiClient
{
return _context.Value.locate<T>(qualifier);
}
}
static public async Task<bool> login(string username, string password)
{
return await _context.Value.login(username, password);
}
static public AuthTokenImmutable? token => _context.Value.token;
}
}
@@ -1,21 +1,22 @@
using System;
using bts.md;
namespace bts.excel.rtd
{
class BookMDFormatter : IExcelFormatter
{
readonly co2.ChannelUpdate _update;
public BookMDFormatter(co2.ChannelUpdate upd)
readonly IMDBook _update;
public BookMDFormatter(IMDBook upd)
{
_update = upd;
}
public static double format_md(long mantissa, int exponent)
public object[,] format(bool include_headers = false)
{
return mantissa * Math.Pow(10, exponent);
return format(_update, include_headers);
}
public object[,] format(bool include_headers = false)
static public object[,] format(IMDBook update, bool include_headers = false)
{
const int cols = 5;
const int rows = 30;
@@ -24,21 +25,21 @@ namespace bts.excel.rtd
for (int j = 0; j < cols; j++)
ret[i, j] = string.Empty;
if (_update.Direct != null)
if (update != null)
{
int idx = 0;
for (int j = _update.Direct.Asks.Count - 1; j >= 0 && idx < rows; j--)
for (int j = update.asks.Count - 1; j >= 0 && idx < rows; j--)
{
var ask = _update.Direct.Asks[j];
ret[idx, 1] = format_md(ask.Mantissa, ask.Exponent);
ret[idx, 2] = ask.Size;
var ask = update.asks[j];
ret[idx, 1] = ask.price;
ret[idx, 2] = ask.size;
idx++;
}
for (int j = 0; j < _update.Direct.Bids.Count && idx < rows; j++)
for (int j = 0; j < update.bids.Count && idx < rows; j++)
{
var bid = _update.Direct.Bids[j];
ret[idx, 1] = format_md(bid.Mantissa, bid.Exponent);
ret[idx, 0] = bid.Size;
var bid = update.bids[j];
ret[idx, 1] = bid.price;
ret[idx, 0] = bid.size;
idx++;
}
}
@@ -1,4 +1,5 @@
using System;
using bts.md;
namespace bts.excel.rtd
{
@@ -6,7 +7,7 @@ namespace bts.excel.rtd
{
public static IExcelFormatter make_formatter(object data)
{
if(data is co2.ChannelUpdate upd)
if(data is IMDBook upd)
return new BookMDFormatter(upd);
else
{
@@ -1,16 +1,22 @@
using System;
using bts.md;
namespace bts.excel.rtd
{
class MiscMDFormatter : IExcelFormatter
{
readonly co2.ChannelUpdate _update;
public MiscMDFormatter(co2.ChannelUpdate upd)
readonly IMDMisc _update;
public MiscMDFormatter(IMDMisc upd)
{
_update = upd;
}
public object[,] format(bool include_headers = false)
{
return format(_update, include_headers);
}
static public object[,] format(IMDMisc update, bool include_headers = false)
{
return new object[0, 0];
}
@@ -1,17 +1,22 @@
using System;
using bts.md;
namespace bts.excel.rtd
{
class TopOfBookMDFormatter : IExcelFormatter
{
readonly co2.ChannelUpdate _update;
public TopOfBookMDFormatter(co2.ChannelUpdate upd)
readonly IMDBook _update;
public TopOfBookMDFormatter(IMDBook upd)
{
_update = upd;
}
public object[,] format(bool include_headers = false)
{
return format(_update, include_headers);
}
static public object[,] format(IMDBook update, bool include_headers = false)
{
const int cols = 4;
const int rows = 1;
@@ -20,22 +25,22 @@ namespace bts.excel.rtd
for (int j = 0; j < cols; j++)
ret[i, j] = string.Empty;
if (_update.Direct != null)
if (update != null)
{
int idx = 0;
for (int j = 0; j < _update.Direct.Asks.Count && idx < rows; j++)
for (int j = 0; j < update.asks.Count && idx < rows; j++)
{
var ask = _update.Direct.Asks[j];
ret[idx, 2] = BookMDFormatter.format_md(ask.Mantissa, ask.Exponent);
ret[idx, 3] = ask.Size;
var ask = update.asks[j];
ret[idx, 2] = ask.price;
ret[idx, 3] = ask.size;
idx++;
}
idx = 0;
for (int j = 0; j < _update.Direct.Bids.Count && idx < rows; j++)
for (int j = 0; j < update.bids.Count && idx < rows; j++)
{
var bid = _update.Direct.Bids[j];
ret[idx, 1] = BookMDFormatter.format_md(bid.Mantissa, bid.Exponent);
ret[idx, 0] = bid.Size;
var bid = update.bids[j];
ret[idx, 1] = bid.price;
ret[idx, 0] = bid.size;
idx++;
}
}
@@ -1,86 +1,95 @@
using System;
using bts.md;
namespace bts.excel.rtd
{
class TopicMDFormatter : IExcelFormatter
{
readonly co2.ChannelUpdate _update;
readonly IMDCombined _update;
readonly string _topic;
public TopicMDFormatter(co2.ChannelUpdate upd, string topic)
public TopicMDFormatter(IMDCombined upd, string topic)
{
_update = upd;
_topic = topic;
}
public object[,] format(bool include_headers = false)
{
return format(_update, _topic, include_headers);
}
static public object[,] format(IMDCombined update, string topic, bool include_headers = false)
{
var retval = new object[1, 1];
switch (_topic.ToLower())
if(update == null || string.IsNullOrEmpty(topic))
return retval;
switch (topic.ToLower())
{
case "bid":
case "bidprice":
retval[0, 0] = BookMDFormatter.format_md(_update?.Direct?.Bids?[0]?.Mantissa ?? 0, _update?.Direct?.Bids?[0]?.Exponent ?? 0);
retval[0, 0] = update.bids?[0]?.price;
break;
case "ask":
case "askprice":
retval[0, 0] = BookMDFormatter.format_md(_update?.Direct?.Asks?[0]?.Mantissa ?? 0, _update?.Direct?.Asks?[0]?.Exponent ?? 0);
retval[0, 0] = update.asks?[0]?.price;
break;
case "bidsize":
case "bidqty":
retval[0, 0] = _update?.Direct?.Bids?[0]?.Size;
retval[0, 0] = update.bids?[0]?.size;
break;
case "asksize":
case "askqty":
retval[0, 0] = _update?.Direct?.Asks?[0]?.Size;
retval[0, 0] = update.asks?[0]?.size;
break;
case "impliedbid":
case "impliedbidprice":
retval[0, 0] = BookMDFormatter.format_md(_update?.Implied?.Bids?[0]?.Mantissa ?? 0, _update?.Implied?.Bids?[0]?.Exponent ?? 0);
retval[0, 0] = update.bids?[0]?.price; //TODO is this the first price with impsz > 0?
break;
case "impliedask":
case "impliedaskprice":
retval[0, 0] = BookMDFormatter.format_md(_update?.Implied?.Asks?[0]?.Mantissa ?? 0, _update?.Implied?.Asks?[0]?.Exponent ?? 0);
retval[0, 0] = update.asks?[0]?.price;
break;
case "impliedbidsize":
case "impliedbidqty":
retval[0, 0] = _update?.Implied?.Bids?[0]?.Size;
retval[0, 0] = update.bids?[0]?.implied_size;
break;
case "impliedasksize":
case "impliedaskqty":
retval[0, 0] = _update?.Implied?.Asks?[0]?.Size;
retval[0, 0] = update.asks?[0]?.implied_size;
break;
case "settlement":
case "settlementprice":
retval[0, 0] = BookMDFormatter.format_md(_update?.Misc?.Settlement?.Mantissa ?? 0, _update?.Misc?.Settlement?.Exponent ?? 0);
retval[0, 0] = update.settlement_price;
break;
case "previoussettlement":
case "previoussettlementprice":
retval[0, 0] = BookMDFormatter.format_md(_update?.Misc?.PreviousSettlementPrice?.Mantissa ?? 0, _update?.Misc?.PreviousSettlementPrice?.Exponent ?? 0);
retval[0, 0] = update.previous_settlement_price;
break;
case "open":
case "openprice":
retval[0, 0] = _update?.Misc?.OpenPrice;
retval[0, 0] = update.open_price;
break;
case "close":
case "closeprice":
retval[0, 0] = _update?.Misc?.ClosePrice;
retval[0, 0] = update.close_price;
break;
case "high":
case "highprice":
retval[0, 0] = _update?.Misc?.SessionHigh;
retval[0, 0] = update.session_high;
break;
case "low":
case "lowprice":
retval[0, 0] = _update?.Misc?.SessionLow;
retval[0, 0] = update.session_low;
break;
case "last":
case "lastprice":
retval[0, 0] = BookMDFormatter.format_md(_update?.LastTrade?.Mantissa ?? 0, _update?.LastTrade?.Exponent ?? 0);
retval[0, 0] = update?.last.price;
break;
case "lastsize":
case "lastqty":
retval[0, 0] = _update?.LastTrade?.Size;
retval[0, 0] = update?.last.size;
break;
}
return retval;
@@ -14,15 +14,19 @@ namespace bts.excel.rtd
{
public class MDSubscribe
{
static Authenticator _auth = new Authenticator();
[ExcelFunction(Category = "BTS", Description = "Log in to BTS Spark")]
async public static void spark_login(string login, string password, ExcelAsyncHandle handle)
{
try
{
var toke = await _auth.login(login, password);
_md.set_auth_token(toke);
handle.SetResult(toke?.Token ?? "Could not log in check user/pass");
var res = await Locator.login(login, password);
if (res && Locator.token.HasValue)
{
_md.set_auth_token(Locator.token);
handle.SetResult("Login Successful");
}
else
handle.SetResult("Login Error: Could not log in check user/pass");
}
catch(Exception ex)
{
@@ -62,10 +66,10 @@ namespace bts.excel.rtd
if (string.IsNullOrEmpty(contract))
return Observable.Return("Can't Parse Contract: " + contract ?? string.Empty);
Product prod = new Product() { Exchange = ven, Type = sectype, Symbol = symbol };
Func<Security, bool> filter = (x => x.Glbx.SecurityDesc == contract); //TODO GLBX
Func<SecurityImmutable, bool> where = (x => x.Exchange == ven && x.Symbol == symbol && x.security_type() == sectype &&
x.HasGlbx && x.Glbx.SecurityDesc == contract);
var stream = market_data(ven).subscribe(prod).Where(x => filter(x.Security));
var stream = market_data(ven).subscribe_combined(where);
return stream;
}
@@ -81,8 +85,8 @@ namespace bts.excel.rtd
return ObservableRtdUtil.Observe(nameof(spark_md_book), param,
() => md_observable(contract, symbol, security_type, exchange).Select(x =>
{
if (x is co2.ChannelUpdate upd)
return new BookMDFormatter(upd).format();
if (x is IMDBook upd)
return BookMDFormatter.format(upd);
return x;
}));
// security_type = string.IsNullOrEmpty(security_type) ? SecurityType.Future.ToString() : security_type;
@@ -107,8 +111,8 @@ namespace bts.excel.rtd
return ObservableRtdUtil.Observe(nameof(spark_md_top_of_book), param,
() => md_observable(contract, symbol, security_type, exchange).Select(x =>
{
if (x is co2.ChannelUpdate upd)
return new TopOfBookMDFormatter(upd).format();
if (x is IMDBook upd)
return TopOfBookMDFormatter.format(upd);
return x;
}));
}
@@ -124,8 +128,8 @@ namespace bts.excel.rtd
return ObservableRtdUtil.Observe(nameof(spark_md_misc), param,
() => md_observable(contract, symbol, security_type, exchange).Select(x =>
{
if (x is co2.ChannelUpdate upd)
return new MiscMDFormatter(upd).format();
if (x is IMDMisc upd)
return MiscMDFormatter.format(upd);
return x;
}));
@@ -143,8 +147,8 @@ namespace bts.excel.rtd
return ObservableRtdUtil.Observe(nameof(spark_md), param,
() => md_observable(contract, symbol, security_type, exchange).Select(x =>
{
if (x is co2.ChannelUpdate upd)
return new TopicMDFormatter(upd, topic).format();
if (x is IMDCombined upd)
return TopicMDFormatter.format(upd, topic);
return x;
}));
}
@@ -9,8 +9,7 @@ using System.Reactive.Concurrency;
using bts.api;
using bts.api.core;
using bts.co2;
using bts.api.md;
using bts.md;
namespace bts.sdk
{
@@ -24,7 +23,7 @@ namespace bts.sdk
_md = client;
}
void print_update(ChannelUpdate update)
void print_update(IMDData update)
{
Console.WriteLine(update.ToString());
Console.WriteLine();
@@ -46,12 +45,14 @@ namespace bts.sdk
return st;
}
public void print_channel(string exch, string sectype, string symbol)
public void print_product(string exch, string sectype, string symbol)
{
Venue ven = parse_venue(exch);
SecurityType st = parse_security_type(sectype);
Product prod = new Product() { Exchange = ven, Type = st, Symbol = symbol };
_disps.Add(_md.subscribe(prod).Subscribe(x =>
Func<SecurityImmutable, bool> where = (x => x.Exchange == ven && x.Symbol == symbol && x.security_type() == st);
_disps.Add(_md.subscribe_combined(where).Subscribe(x =>
{
print_update(x);
}));
@@ -61,11 +62,11 @@ namespace bts.sdk
{
Venue ven = parse_venue(exch);
SecurityType st = parse_security_type(sectype);
Product prod = new Product() { Exchange = ven, Type = st, Symbol = symbol };
Func<Security, bool> filter = (x => x.Glbx.SecurityDesc == security_desc);
//if you have a security_id, we could make a more specifics subscription, but this is a command line driver....
_disps.Add(_md.subscribe(prod).Where(x => filter(x.Security)).Subscribe(x =>
Func<SecurityImmutable, bool> where = (x => x.Exchange == ven && x.Symbol == symbol && x.security_type() == st &&
x.HasGlbx && x.Glbx.SecurityDesc == security_desc);
_disps.Add(_md.subscribe_combined(where).Subscribe(x =>
{
print_update(x);
}));
+2 -2
View File
@@ -7,7 +7,7 @@ using System.Threading.Tasks;
using System.Reactive.Linq;
using bts.api;
using bts.api.md;
using bts.md;
using bts.api.core;
using bts.api.orderman;
using bts.api.user;
@@ -58,7 +58,7 @@ namespace bts.sdk
else
cr.NewOrder.CustomDef = new CustomOrderDefinition() { Market = new MarketOrderDefinition() };
var sec = await _md.find_security_async(x => x.Glbx.SecurityDesc == security_desc.ToUpper());
cr.NewOrder.SecurityKey = sec.Id;
cr.NewOrder.SecurityKey = sec.Value.Id;
Result<Response> resp = await _orders.Value.create_order_async(cr);
print_update(resp);
if(resp.value != null)
+10 -12
View File
@@ -6,7 +6,7 @@ using System.Reactive.Disposables;
using bts.api;
using bts.api.core;
using bts.api.md;
using bts.md;
namespace bts.sdk
{
@@ -14,10 +14,8 @@ namespace bts.sdk
public class SDKMain
{
private static void user_test()
{
var ctx = new ServiceContext();
var s = ctx.locate<api.user.AuthService>();
{
var s = Locator.locate<api.user.AuthService>();
var r = new api.user.GetUsersRequest();
var resp = s.get_users(r);
Console.WriteLine("{0}", resp);
@@ -31,7 +29,12 @@ namespace bts.sdk
}
else
{
MarketDataClient client = new MarketDataClient();
uint ms = 100;
string p = args.FirstOrDefault(x => x.Contains("--pump="));
if (p != null)
ms = uint.Parse(p.Replace("--pump=", string.Empty));
MarketDataClient client = new MarketDataClient(ms);
CompositeDisposable disp = new CompositeDisposable(client);
if (args.FirstOrDefault() == "ui")
{
@@ -56,7 +59,7 @@ namespace bts.sdk
}
else if (args.Length >= 4)
{
s.print_channel(args[1], args[2], args[3]);
s.print_product(args[1], args[2], args[3]);
}
else
md_help();
@@ -73,11 +76,6 @@ namespace bts.sdk
else
help();
int ms = 100;
string p = args.FirstOrDefault(x => x.Contains("--pump="));
if (p != null)
ms = int.Parse(p.Replace("--pump=", string.Empty));
client.run_pump(TimeSpan.FromMilliseconds(ms));
wait_for_stop(disp);
}
}
+1 -1
View File
@@ -2,7 +2,7 @@ using System;
using System.Linq;
using bts.api;
using bts.api.md;
using bts.md;
namespace bts.sdk.ui
{
+1 -1
View File
@@ -11,7 +11,7 @@ using System.Threading.Tasks;
using System.Windows.Forms;
using bts.api;
using bts.api.md;
using bts.md;
namespace bts.sdk.ui
{