From ff65a5ca5b7ec69c4dedd36943a51b483bc376bf Mon Sep 17 00:00:00 2001 From: John Colvin Date: Thu, 2 Feb 2023 00:03:02 -0500 Subject: [PATCH] refactor: Get rid of old MarketDataClient in sdk examples --- src/client/spark_api/MarketData/MDBook.cs | 59 ---- src/client/spark_api/MarketData/MDCombined.cs | 61 ---- src/client/spark_api/MarketData/MDLast.cs | 40 --- src/client/spark_api/MarketData/MDMisc.cs | 67 ---- .../spark_api/MarketData/MarketDataClient.cs | 253 -------------- .../spark_sdk/samples/MarketDataSample.cs | 25 +- src/client/spark_sdk/samples/OrderSample.cs | 27 +- src/client/spark_sdk/samples/SDKMain.cs | 313 +++++++++--------- src/client/spark_sdk/samples/UISample.cs | 4 +- src/client/spark_sdk/uisamples/SparkSDKUI.cs | 4 +- 10 files changed, 206 insertions(+), 647 deletions(-) delete mode 100644 src/client/spark_api/MarketData/MDBook.cs delete mode 100644 src/client/spark_api/MarketData/MDCombined.cs delete mode 100644 src/client/spark_api/MarketData/MDLast.cs delete mode 100644 src/client/spark_api/MarketData/MDMisc.cs delete mode 100644 src/client/spark_api/MarketData/MarketDataClient.cs diff --git a/src/client/spark_api/MarketData/MDBook.cs b/src/client/spark_api/MarketData/MDBook.cs deleted file mode 100644 index 2d5a6c5da..000000000 --- a/src/client/spark_api/MarketData/MDBook.cs +++ /dev/null @@ -1,59 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; - -using bts.api; -using mbts; -using mbts.co2; - -namespace bts.md -{ - interface IMDUpdate - { - bool update(SecurityMD smd, ref ulong last_update_time); - } - - public interface IMDData - { - SecurityImmutable security { get; } - } - - public interface IMDBook : IMDData - { - List bids { get; } - List asks { get; } - } - - class MDBook : IMDBook, IMDUpdate - { - public MDBook() - { - } - public SecurityImmutable security { get; private set; } - - public List bids { get; private set; } - public List asks { get; private set; } - - bool IMDUpdate.update(SecurityMD smd, ref ulong last_update_time) - { - this.security = smd.security; - NativePriceSize[] bids = null, asks = null; - if (smd.md.get_depth(ref last_update_time, ref bids, ref asks, NativePriceHelpers.COCONUT_MAX_DEPTH)) - { - this.bids = bids.Select(x => new PriceSize(x.size, x.direct_size, x.implied_size, x.price)).ToList(); - this.asks = asks.Select(x => new PriceSize(x.size, x.direct_size, x.implied_size, x.price)).ToList(); - return true; - } - return false; - } - - public override string ToString() - { - return security.ToString() + Environment.NewLine + - "Bid BidSz BidISz" + Environment.NewLine + - string.Join("\n", bids.Select(x => x.ToString())) + Environment.NewLine + - "Ask AskSz AskISz" + Environment.NewLine + - string.Join("\n", asks.Select(x => x.ToString())); - } - } -} \ No newline at end of file diff --git a/src/client/spark_api/MarketData/MDCombined.cs b/src/client/spark_api/MarketData/MDCombined.cs deleted file mode 100644 index a3d817ffc..000000000 --- a/src/client/spark_api/MarketData/MDCombined.cs +++ /dev/null @@ -1,61 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using bts.api; -using mbts; - -namespace bts.md -{ - public interface IMDCombined : IMDData, IMDMisc, IMDLast, IMDBook - { - } - - class MDCombined : IMDCombined, IMDUpdate - { - MDBook _book = new MDBook(); - MDLast _last = new MDLast(); - MDMisc _misc = new MDMisc(); - public MDCombined() - { - } - public SecurityImmutable security { get; private set; } - - public ulong total_volume => _misc.total_volume; - public double settlement_price => _misc.settlement_price; - public DateTime settlement_date => _misc.settlement_date; - public double previous_settlement_price => _misc.previous_settlement_price; - public DateTime previous_settlement_date => _misc.previous_settlement_date; - - public double open_price => _misc.open_price; - public DateTime open_date => _misc.open_date; - public double close_price => _misc.close_price; - public DateTime close_date => _misc.close_date; - public double session_high => _misc.session_high; - public double session_low => _misc.session_low; - public double fixing_price => _misc.fixing_price; - - public PriceSize last => _last.last; - public ulong total_volume2 => _last.total_volume; - public DateTime last_time => _last.last_time; - - public List bids => _book.bids; - public List asks => _book.asks; - - - bool IMDUpdate.update(SecurityMD smd, ref ulong last_update_time) - { - this.security = smd.security; - var bl = last_update_time; - bool b = ((IMDUpdate)_book).update(smd, ref bl); - - var ll = last_update_time; - bool l = ((IMDUpdate)_last).update(smd, ref ll); - - var ml = last_update_time; - bool m = ((IMDUpdate)_misc).update(smd, ref ml); - - last_update_time = new[] { bl, ll, ml }.Max(); - return b || l || m; - } - } -} \ No newline at end of file diff --git a/src/client/spark_api/MarketData/MDLast.cs b/src/client/spark_api/MarketData/MDLast.cs deleted file mode 100644 index 78b3c3294..000000000 --- a/src/client/spark_api/MarketData/MDLast.cs +++ /dev/null @@ -1,40 +0,0 @@ -using System; -using bts.api; -using mbts; -using mbts.co2; - -namespace bts.md -{ - public interface IMDLast : IMDData - { - PriceSize last{ get; } - ulong total_volume { get; } - DateTime last_time { get; } - } - - class MDLast : IMDLast, IMDUpdate - { - public MDLast() - { - } - public SecurityImmutable security { get; private set; } - - public PriceSize last { get; private set; } - public ulong total_volume { get; private set; } - public DateTime last_time { get; private set; } - - bool IMDUpdate.update(SecurityMD smd, ref ulong last_update_time) - { - this.security = smd.security; - NativePriceSize x; DateTime ts; ulong vol; - if (smd.md.get_last_trade(ref last_update_time, out x, out ts, out vol)) - { - last = new PriceSize(x.size, x.direct_size, x.implied_size, x.price); - total_volume = vol; - last_time = ts; - return true; - } - return false; - } - } -} \ No newline at end of file diff --git a/src/client/spark_api/MarketData/MDMisc.cs b/src/client/spark_api/MarketData/MDMisc.cs deleted file mode 100644 index cefc1eccf..000000000 --- a/src/client/spark_api/MarketData/MDMisc.cs +++ /dev/null @@ -1,67 +0,0 @@ -using System; -using bts.api; -using mbts; - -namespace bts.md -{ - public interface IMDMisc : IMDData - { - ulong total_volume { get; } - double settlement_price { get; } - DateTime settlement_date { get; } - double previous_settlement_price { get; } - DateTime previous_settlement_date { get; } - - double open_price { get; } - DateTime open_date { get; } - double close_price { get; } - DateTime close_date { get; } - double session_high { get; } - double session_low { get; } - double fixing_price { get; } - } - - class MDMisc : IMDMisc, IMDUpdate - { - public MDMisc() - { - } - public SecurityImmutable security { get; private set; } - - public ulong total_volume { get; private set; } - public double settlement_price { get; private set; } - public DateTime settlement_date { get; private set; } - public double previous_settlement_price { get; private set; } - public DateTime previous_settlement_date { get; private set; } - - public double open_price { get; private set; } - public DateTime open_date { get; private set; } - public double close_price { get; private set; } - public DateTime close_date { get; private set; } - public double session_high { get; private set; } - public double session_low { get; private set; } - public double fixing_price { get; private set; } - - bool IMDUpdate.update(SecurityMD smd, ref ulong last_update_time) - { - this.security = smd.security; - if (smd.md.misc_timestamp > last_update_time) - { - total_volume = smd.md.total_volume; - settlement_price = smd.md.settlement_price; - settlement_date = smd.md.settlement_date; - previous_settlement_price = smd.md.previous_settlement_price; - previous_settlement_date = smd.md.previous_settlement_date; - - open_price = smd.md.open_price; - open_date = smd.md.open_date; - close_price = smd.md.close_price; - close_date = smd.md.close_date; - session_high = smd.md.session_high; - session_low = smd.md.session_low; - fixing_price = smd.md.fixing_price; - } - return false; - } - } -} \ No newline at end of file diff --git a/src/client/spark_api/MarketData/MarketDataClient.cs b/src/client/spark_api/MarketData/MarketDataClient.cs deleted file mode 100644 index d80b3a09a..000000000 --- a/src/client/spark_api/MarketData/MarketDataClient.cs +++ /dev/null @@ -1,253 +0,0 @@ -using System; -using System.Linq; -using System.Collections.Generic; -using System.Collections.Concurrent; -using System.Threading; -using System.Threading.Tasks; -using System.Reactive.Linq; -using System.Reactive.Concurrency; -using System.Reactive.Subjects; - -using bts.api; -using bts.api.core; -using bts.co2; -using mbts; -using SecurityKey = System.UInt32; - -namespace bts.md -{ - - public class MarketDataClient : IDisposable - { - static readonly TimeSpan _naggle_throttle = TimeSpan.FromMilliseconds(100); - readonly static bts.md.MDSource _subscription = new MDSource(_naggle_throttle); - - readonly ConcurrentDictionary _data = new ConcurrentDictionary(); - readonly List _disp = new List(); - readonly uint _default_pump_ms; - public MarketDataClient(uint default_pump_ms = 250, AuthTokenImmutable? token = null) - { - _default_pump_ms = default_pump_ms; - if (token != null) - set_auth_token(token); - - _disp.Add(_subscription.subscribe_securities().Subscribe( md => - { - foreach (var entry in md) - _data.AddOrUpdate(entry.Key, entry.Value, (_, __) => entry.Value); - })); - } - public IEnumerable securities => _data.Values.Select(x => x.security); - public IObservable> on_securities - { - get { return _subscription.subscribe_securities().Select(x => x.Values.Select(xx => xx.security)); } - } - - public void set_auth_token(AuthTokenImmutable? toke) - { - if(toke.HasValue) - _subscription.set_token(toke.Value); - } - public IObservable< IEnumerable > subscribe_book(Func filter, - TimeSpan? update_interval = null, IScheduler scheduler = null) - { - return _subscribe(filter, update_interval, scheduler); - } - public IObservable< IEnumerable > subscribe_last(Func filter, - TimeSpan? update_interval = null, IScheduler scheduler = null) - { - return _subscribe(filter, update_interval, scheduler); - } - public IObservable< IEnumerable > subscribe_misc(Func filter, - TimeSpan? update_interval = null, IScheduler scheduler = null) - { - return _subscribe(filter, update_interval, scheduler); - } - public IObservable< IEnumerable > subscribe_combined(Func filter, - TimeSpan? update_interval = null, IScheduler scheduler = null) - { - return _subscribe(filter, update_interval, scheduler); - } - - IObservable> _subscribe(Func filter, - TimeSpan? update_interval = null, IScheduler scheduler = null) where T : IMDData, IMDUpdate, new() - { - List> slots = new List>(); int data_size = 0; - return timer_pump>((last_timer_tick, observer) => - { - //if the number of securities has changed, or this is the first call - if (data_size != _data.Count) - { - slots = _data.Values.Where(x => filter(x.security)).Select(xx => new Slot(xx)).ToList(); - data_size = _data.Count; - } - List updates = new List(); - 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) - if(did_update) - updates.Add(slot.data); - } - if(updates.Count > 0) - observer.OnNext(updates); - }, - update_interval, scheduler); - } - - public IObservable> subscribe_book(IEnumerable ids, TimeSpan? update_interval = null, - IScheduler scheduler = null) - { - return _subscribe(ids, update_interval, scheduler); - } - public IObservable> subscribe_last(IEnumerable ids, TimeSpan? update_interval = null, - IScheduler scheduler = null) - { - return _subscribe(ids, update_interval, scheduler); - } - public IObservable> subscribe_misc(IEnumerable ids, TimeSpan? update_interval = null, - IScheduler scheduler = null) - { - return _subscribe(ids, update_interval, scheduler); - } - public IObservable> subscribe_combined(IEnumerable ids, TimeSpan? update_interval = null, - IScheduler scheduler = null) - { - return _subscribe(ids, update_interval, scheduler); - } - - IObservable> _subscribe(IEnumerable ids, TimeSpan? update_interval = null, - IScheduler scheduler = null) where T : IMDData, IMDUpdate, new() - { - List> slots = ids.Where(ii => _data.TryGetValue(ii, out SecurityMD _)).Select(x => - { - _data.TryGetValue(x, out SecurityMD smd); - return new Slot(smd); - }).ToList(); - return timer_pump>((last_timer_tick, observer) => - { - List updates = new List(); - 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) - if(did_update) - updates.Add(slot.data); - } - if(updates.Count > 0) - observer.OnNext(updates); - }, - update_interval, scheduler); - } - - class Slot where T : IMDData, IMDUpdate, new() - { - public Slot(SecurityMD md) - { - secmd = md; - last_update_time = 0; - data = new T(); - } - public SecurityMD secmd; - public ulong last_update_time; - public T data; - } - - IObservable timer_pump(Action> tick, TimeSpan? interval, IScheduler scheduler = null) - { - if(!interval.HasValue) - interval = TimeSpan.FromMilliseconds(_default_pump_ms); - ulong last_update_time = 0; - return Observable.Create(observer => - { - return Observable.Timer(interval.Value, interval.Value, scheduler ?? Scheduler.Default).Subscribe(_ => - { - tick(last_update_time, observer); - last_update_time = (ulong)DateTime.UtcNow.Ticks; - }); - }); - } - - public void Dispose() - { - _disp.ForEach(x => x.Dispose()); - } - - #region security search - - public SecurityImmutable? lookup(SecurityKey key) - { - if (_data.TryGetValue(key, out SecurityMD value)) - return value.security; - else - return null; - } - - public bool lookup_security_desc(SecurityKey key, out string security_desc) - { - security_desc = null; - var sec = lookup(key); - if (sec.HasValue) - { - security_desc = sec.Value.security_desc(); - return true; - } - return false; - } - - public bool lookup_security_key_from_desc(string security_desc, out SecurityKey key) - { - key = 0; - if (find(x => x.security_desc() == security_desc).TryGetFirst(out var fnd)) { - key = fnd.security.Id; - return true; - } - return false; - } - - IEnumerable find(Func filter) - { - Func filt = (SecurityMD secmd) => filter(secmd.security); - return _data.Values.Where(filt); - } - - public SecurityImmutable? find_security(Func filter, TimeSpan? timeout = null) - { - var deadline = DateTime.UtcNow; - if (timeout.HasValue) deadline += timeout.Value; - do { - if (find(filter).TryGetFirst(out var fnd)) { - return fnd.security; - } - Thread.Sleep(100); - } while (DateTime.UtcNow < deadline); - return null; - } - - public async Task find_security_async(Func filter, TimeSpan? timeout = null) - { - if (!timeout.HasValue) - { - timeout = TimeSpan.FromMilliseconds(10000); - } - CancellationTokenSource src = new CancellationTokenSource(timeout.Value); - SecurityImmutable? security = await Task.Run(async () => - { - SecurityImmutable? sec = null; - do - { - sec = find_security(filter); - if (sec == null) - await Task.Delay(500); - } - while (sec == null); - return sec; - }, src.Token); - - return security ?? throw new KeyNotFoundException("Can't find security for filter"); ; - } - - - #endregion - } -} diff --git a/src/client/spark_sdk/samples/MarketDataSample.cs b/src/client/spark_sdk/samples/MarketDataSample.cs index caeb3274c..226c7201b 100644 --- a/src/client/spark_sdk/samples/MarketDataSample.cs +++ b/src/client/spark_sdk/samples/MarketDataSample.cs @@ -10,20 +10,21 @@ using System.Reactive.Concurrency; using bts.api; using bts.api.core; using bts.md; +using mbts.spark; namespace bts.sdk { public class MarketDataSample : IDisposable { - readonly MarketDataClient _md; + readonly IncrementalMarketDataClient _md; readonly List _disps = new List(); - public MarketDataSample(MarketDataClient client) + public MarketDataSample(IncrementalMarketDataClient client) { _md = client; } - void print_update(IEnumerable updates) + void print_update(IEnumerable updates) { foreach (var update in updates) { @@ -53,11 +54,13 @@ 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 where = (x => x.Exchange == ven && x.Symbol == symbol && x.security_type() == st); + var criteria = new SecurityCriteria(new SecurityCriteriaClause[] { + new(new[] { ven }, new[] { st }, new[] { symbol }) + }); - _disps.Add(_md.subscribe_book(where).Subscribe(x => + _disps.Add(_md.market_data(criteria).Subscribe(x => { - print_update(x); + print_update(x.added.Concat(x.changed)); })); } @@ -68,10 +71,14 @@ namespace bts.sdk Func where = (x => x.Exchange == ven && x.Symbol == symbol && x.security_type() == st && x.Glbx.SecurityDesc == security_desc); - - _disps.Add(_md.subscribe_book(where).Subscribe(x => + var criteria = new SecurityCriteria(new SecurityCriteriaClause[] { + new(new[] { ven }, new[] { st }, new[] { symbol }) + }); + var md_source = _md.market_data(criteria) + .Filter(b => b.security.security_desc() == security_desc); + _disps.Add(md_source.Subscribe(x => { - print_update(x); + print_update(x.added.Concat(x.changed)); })); } diff --git a/src/client/spark_sdk/samples/OrderSample.cs b/src/client/spark_sdk/samples/OrderSample.cs index 31f16eed3..529892c0c 100644 --- a/src/client/spark_sdk/samples/OrderSample.cs +++ b/src/client/spark_sdk/samples/OrderSample.cs @@ -13,17 +13,27 @@ using bts.api.orderman; using bts.api.user; using bts.api.admin; using bts.co2; +using Spark.Api.Orders; namespace bts.sdk { public class OrderSample : IDisposable { - readonly MarketDataClient _md; + readonly IncrementalMarketDataClient _md; + readonly OrdersClient _orders_client; readonly List _disps = new List(); - public OrderSample(MarketDataClient client) - { - _md = client; + IReadOnlyDictionary _securities = + new Dictionary(0); + + public OrderSample( + OrdersClient orders_client, + IncrementalMarketDataClient md_client, + IObservable> securities_src + ) { + _orders_client = orders_client; + _md = md_client; + _disps.Add(securities_src.Subscribe(secs => _securities = secs)); } readonly static Lazy _orders = new Lazy(() => Locator.locate(), true); @@ -58,7 +68,8 @@ namespace bts.sdk else cr.NewOrder.CustomDef = new CustomOrderDefinition() { Market = new MarketOrderDefinition() }; Console.WriteLine("Fetching security details..."); - var sec = _md.find_security(x => x.Glbx.SecurityDesc == security_desc.ToUpper(), TimeSpan.FromSeconds(30)); + // _orders_client.orders() + var sec = _find_sec(security_desc); if (!sec.HasValue) { Console.WriteLine($"Failed to find security {security_desc}."); return null; @@ -119,6 +130,12 @@ namespace bts.sdk { _disps.ForEach(x => x.Dispose()); } + + SecurityImmutable? _find_sec(uint sec_id) => + _securities.TryGetValue(sec_id, out var sec) ? sec : null; + + SecurityImmutable? _find_sec(string sec_desc) => + _securities.Values.FirstOrNull(s => s.security_desc() == sec_desc); } } diff --git a/src/client/spark_sdk/samples/SDKMain.cs b/src/client/spark_sdk/samples/SDKMain.cs index 857db8664..aa4707fc1 100644 --- a/src/client/spark_sdk/samples/SDKMain.cs +++ b/src/client/spark_sdk/samples/SDKMain.cs @@ -1,149 +1,164 @@ -using System; -using System.Linq; -using System.Threading; -using System.Threading.Tasks; -using System.Reactive.Disposables; - -using bts.api; -using bts.api.core; -using bts.md; - -namespace bts.sdk -{ - - public class SDKMain - { - private static void user_test(ServiceContext ctx) - { - var s = ctx.locate(); - var r = new api.user.GetUsersRequest(); - var resp = s.get_users(r); - Console.WriteLine("{0}", resp); - } - public static void Main(string[] args) - { - BTS.init(); - Log.Sdk.info("Welcome to the BTS Client Side SDK example"); - var ctx = new ServiceContext(); - - string u = args.FirstOrDefault(x => x.Contains("--user=")); - string p = args.FirstOrDefault(x => x.Contains("--pass=")); - u = u?.Replace("--user=", string.Empty) ?? string.Empty; - p = p?.Replace("--pass=", string.Empty) ?? string.Empty; - - var lr = ctx.login(u,p).Result; - if (!lr.success) - { - Console.WriteLine("Login failure: " + lr.reason); - help(); - } - - if (args.FirstOrDefault() == "users") - { - user_test(ctx); - } - else - { - uint ms = 100; - string pu = args.FirstOrDefault(x => x.Contains("--pump=")); - if (pu != null) - ms = uint.Parse(p.Replace("--pump=", string.Empty)); - - MarketDataClient client = new MarketDataClient(ms, ctx.token); - using(var disp = new CompositeDisposable(client)) { - if (args.Length > 0 && args.FirstOrDefault().Contains("md")) - { - var s = new MarketDataSample(client); - if (args.FirstOrDefault() == "md") - { - if (args.Length >= 5) - { - s.print_security(args[1], args[2], args[3], args[4]); - } - else if (args.Length >= 4) - { - s.print_product(args[1], args[2], args[3]); - } - else - md_help(); - } - else - md_help(); - } - else if (args.FirstOrDefault() == "order") - { - OrderSample os = new OrderSample(client); - disp.Add(os); - order_sample(os, u, args); - } - else - help(); - - wait_for_stop(); - } - } - } - - static void order_sample(OrderSample os, string user, string[] args) - { - if (args.Length >= 10) //--user and --pass are in args[] - { - os.send_order(user, args[1], args[2], args[3], args[4], args[5], args[6], args[7]); - } - else if (args.Length >= 9) - { - os.send_order(user, args[1], args[2], args[3], args[4], args[5], args[6]); - } - else - order_help(); - } - - static void wait_for_stop() - { - Console.WriteLine("Hit q to stop"); - ConsoleKeyInfo c; - do - { - c = Console.ReadKey(); - Console.WriteLine(); - } while (c.KeyChar != 'q'); - } - - static void help() - { - Console.WriteLine("BTS Spark SDK"); - Console.WriteLine("Usages: spark_sdk [command] [arguments]"); - Console.WriteLine(); - Console.WriteLine("Commands:"); - Console.WriteLine(" md []"); - Console.WriteLine(" order []"); - Console.WriteLine("Arguments:"); - Console.WriteLine(" --user="); - Console.WriteLine(" --pass="); - Console.WriteLine(" --pump="); - Environment.Exit(1); - } - - static void order_help() - { - Console.WriteLine("order [] [] []"); - Console.WriteLine("This command sends a limit or market DAY order for the specified security_desc. Leave off the price for a market order"); - Console.WriteLine(" example: order \"ACC A1\" GLBX Future ES ESM8 10 2650.0"); - Console.WriteLine(" order \"ACC A1\" SIM Future ES SIM:ESM8 -10 2650.0"); - Console.WriteLine(" order \"ACC A1\" GLBX Future ES ESM8 10 (this is a market order)"); - Environment.Exit(1); - } - - static void md_help() - { - Console.WriteLine("md "); - Console.WriteLine("This command subscribes to market data for an exchange-type-symbol channel and writes to console."); - Console.WriteLine("Marketdata is read off the wire on the same thread as is used to read and print"); - Console.WriteLine(" example: md GLBX Future ES --user=bts --pass=password"); - Console.WriteLine(" md SIM Future ES SIM:ESZ8 --user=bts --pass=password"); - Console.WriteLine(" md GLBX Spread ES --user=bts --pass=password"); - Environment.Exit(1); - } - - } -} +namespace bts.sdk { + using bts.api.admin; + using bts.api.core; + using bts.api.orderman; + using bts.co2; + using bts.md; + + using Spark.Api.Admin; + using Spark.Api.Orders; + using Spark.Api.Reactive; + + using System; + using System.Linq; + using System.Reactive.Disposables; + using System.Reactive.Linq; + + public class SDKMain + { + private static void user_test(ServiceContext ctx) + { + var s = ctx.locate(); + var r = new api.user.GetUsersRequest(); + var resp = s.get_users(r); + Console.WriteLine("{0}", resp); + } + public static void Main(string[] args) + { + BTS.init(); + Log.Sdk.info("Welcome to the BTS Client Side SDK example"); + var ctx = new ServiceContext(); + + string u = args.FirstOrDefault(x => x.Contains("--user=")); + string p = args.FirstOrDefault(x => x.Contains("--pass=")); + u = u?.Replace("--user=", string.Empty) ?? string.Empty; + p = p?.Replace("--pass=", string.Empty) ?? string.Empty; + + var lr = ctx.login(u,p).Result; + if (!lr.success) + { + Console.WriteLine("Login failure: " + lr.reason); + help(); + } + + if (args.FirstOrDefault() == "users") + { + user_test(ctx); + } + else + { + uint ms = 100; + string pu = args.FirstOrDefault(x => x.Contains("--pump=")); + if (pu != null) + ms = uint.Parse(p.Replace("--pump=", string.Empty)); + + var order_service = ctx.locate(); + var admin_service = ctx.locate(); + var md_relay = ctx.locate(); + var md_source = new MDSource(TimeSpan.FromSeconds(0.1)); + var md_client = new IncrementalMarketDataClient(md_source); + var admin_client = new AdminClient(admin_service, UserTuple.admin); + var sec_resolver = new SecurityResolver(md_relay, md_source.subscribe_securities()); + var orders_client = new OrdersClient(order_service, admin_client, sec_resolver); + + using(var disp = new CompositeDisposable(md_source)) { + if (args.Length > 0 && args.FirstOrDefault().Contains("md")) + { + var s = new MarketDataSample(md_client); + if (args.FirstOrDefault() == "md") + { + if (args.Length >= 5) + { + s.print_security(args[1], args[2], args[3], args[4]); + } + else if (args.Length >= 4) + { + s.print_product(args[1], args[2], args[3]); + } + else + md_help(); + } + else + md_help(); + } + else if (args.FirstOrDefault() == "order") + { + var os = new OrderSample( + orders_client, + md_client, + md_source.subscribe_securities().Select(sec_map => + sec_map.ToDictionary(kv => kv.Key, kv => kv.Value.security))); + disp.Add(os); + order_sample(os, u, args); + } + else + help(); + + wait_for_stop(); + } + } + } + + static void order_sample(OrderSample os, string user, string[] args) + { + if (args.Length >= 10) //--user and --pass are in args[] + { + os.send_order(user, args[1], args[2], args[3], args[4], args[5], args[6], args[7]); + } + else if (args.Length >= 9) + { + os.send_order(user, args[1], args[2], args[3], args[4], args[5], args[6]); + } + else + order_help(); + } + + static void wait_for_stop() + { + Console.WriteLine("Hit q to stop"); + ConsoleKeyInfo c; + do + { + c = Console.ReadKey(); + Console.WriteLine(); + } while (c.KeyChar != 'q'); + } + + static void help() + { + Console.WriteLine("BTS Spark SDK"); + Console.WriteLine("Usages: spark_sdk [command] [arguments]"); + Console.WriteLine(); + Console.WriteLine("Commands:"); + Console.WriteLine(" md []"); + Console.WriteLine(" order []"); + Console.WriteLine("Arguments:"); + Console.WriteLine(" --user="); + Console.WriteLine(" --pass="); + Console.WriteLine(" --pump="); + Environment.Exit(1); + } + + static void order_help() + { + Console.WriteLine("order [] [] []"); + Console.WriteLine("This command sends a limit or market DAY order for the specified security_desc. Leave off the price for a market order"); + Console.WriteLine(" example: order \"ACC A1\" GLBX Future ES ESM8 10 2650.0"); + Console.WriteLine(" order \"ACC A1\" SIM Future ES SIM:ESM8 -10 2650.0"); + Console.WriteLine(" order \"ACC A1\" GLBX Future ES ESM8 10 (this is a market order)"); + Environment.Exit(1); + } + + static void md_help() + { + Console.WriteLine("md "); + Console.WriteLine("This command subscribes to market data for an exchange-type-symbol channel and writes to console."); + Console.WriteLine("Marketdata is read off the wire on the same thread as is used to read and print"); + Console.WriteLine(" example: md GLBX Future ES --user=bts --pass=password"); + Console.WriteLine(" md SIM Future ES SIM:ESZ8 --user=bts --pass=password"); + Console.WriteLine(" md GLBX Spread ES --user=bts --pass=password"); + Environment.Exit(1); + } + + } +} diff --git a/src/client/spark_sdk/samples/UISample.cs b/src/client/spark_sdk/samples/UISample.cs index 97ae9fded..64934935c 100644 --- a/src/client/spark_sdk/samples/UISample.cs +++ b/src/client/spark_sdk/samples/UISample.cs @@ -8,8 +8,8 @@ namespace bts.sdk.ui { public class UISample : IDisposable { - readonly MarketDataClient _md; - public UISample(MarketDataClient client) + readonly IncrementalMarketDataClient _md; + public UISample(IncrementalMarketDataClient client) { _md = client; } diff --git a/src/client/spark_sdk/uisamples/SparkSDKUI.cs b/src/client/spark_sdk/uisamples/SparkSDKUI.cs index fadb12e0a..82eddbacf 100644 --- a/src/client/spark_sdk/uisamples/SparkSDKUI.cs +++ b/src/client/spark_sdk/uisamples/SparkSDKUI.cs @@ -18,7 +18,7 @@ namespace bts.sdk.ui public partial class SparkSDKUI : Form { private DataGridView _dg = new DataGridView(); - public SparkSDKUI(MarketDataClient client, Product prod, string security_desc, int interval) + public SparkSDKUI(IncrementalMarketDataClient client, Product prod, string security_desc, int interval) { InitializeComponent(); @@ -31,4 +31,4 @@ namespace bts.sdk.ui -#endif \ No newline at end of file +#endif