From 7a6e9bc3c82930f7aecfa8fe20139ad99b1305bd Mon Sep 17 00:00:00 2001 From: John Colvin Date: Sun, 4 Feb 2024 14:47:36 -0600 Subject: [PATCH] fix rem locator --- src/client/spark_sdk/samples/OrderSample.cs | 15 +- src/client/spark_sdk/samples/SDKMain.cs | 335 ++++++++++---------- 2 files changed, 177 insertions(+), 173 deletions(-) diff --git a/src/client/spark_sdk/samples/OrderSample.cs b/src/client/spark_sdk/samples/OrderSample.cs index 8833f9f98..ed88038eb 100644 --- a/src/client/spark_sdk/samples/OrderSample.cs +++ b/src/client/spark_sdk/samples/OrderSample.cs @@ -25,21 +25,22 @@ namespace bts.sdk readonly List _disps = new List(); IReadOnlyDictionary _securities = new Dictionary(0); + readonly Lazy _orders; + readonly Lazy _admin; public OrderSample( + ServiceContext serviceContext, OrdersClient orders_client, MarketDataClient md_client, IObservable> securities_src ) { _orders_client = orders_client; _md = md_client; + _orders = new(() => serviceContext.locate(), true); + _admin = new(() => serviceContext.locate(), true); _disps.Add(securities_src.Subscribe(secs => _securities = secs)); } - readonly static Lazy _orders = new Lazy(() => Locator.locate(), true); - readonly static Lazy _users = new Lazy(() => Locator.locate(), true); - readonly static Lazy _admin = new Lazy(() => Locator.locate(), true); - void print_update(T update) { Console.WriteLine(update.ToString()); @@ -94,7 +95,10 @@ namespace bts.sdk return resp; } - static void order_setup(Create cr, string username, string accountname) + void order_setup(Create cr, string username, string accountname) => + order_setup(cr, username, accountname, _admin.Value.get_admin_data(new VoidMessage())); + + static void order_setup(Create cr, string username, string accountname, AdminData admin) { cr.NewOrder = new OrderDefinition(); cr.NewOrder.ManualOrderIndicator = ManualOrderIndicator.Yes; @@ -102,7 +106,6 @@ namespace bts.sdk cr.NewOrder.CustomerOrFirm = CustomerOrFirm.Firm; cr.NewOrder.SubType = OrderSubType.None; cr.NewOrder.TimeInForce = TimeInForce.Day; - var admin = _admin.Value.get_admin_data(new VoidMessage()); BTSUser user = admin.Users.FirstOrDefault( x=> username != null ? x.Name.ToLowerInvariant() == username.ToLowerInvariant() : true ); if (user != null) { diff --git a/src/client/spark_sdk/samples/SDKMain.cs b/src/client/spark_sdk/samples/SDKMain.cs index eb9c881f3..7417bfb90 100644 --- a/src/client/spark_sdk/samples/SDKMain.cs +++ b/src/client/spark_sdk/samples/SDKMain.cs @@ -1,167 +1,168 @@ -namespace bts.sdk { - using bts.api.admin; - using bts.api.core; - using bts.api.orderman; - using bts.api.position; - 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 position_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 MarketDataClient(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, position_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); - } - - } -} +namespace bts.sdk { + using bts.api.admin; + using bts.api.core; + using bts.api.orderman; + using bts.api.position; + 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 position_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 MarketDataClient(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, position_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( + ctx, + 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); + } + + } +}