diff --git a/src/client/spark_api/md/MarketDataClient.cs b/src/client/spark_api/md/MarketDataClient.cs index d28ff4347..cd5bacc19 100644 --- a/src/client/spark_api/md/MarketDataClient.cs +++ b/src/client/spark_api/md/MarketDataClient.cs @@ -23,13 +23,19 @@ namespace bts.api.md public ChannelUpdate data; } - readonly ConcurrentDictionary _data = new ConcurrentDictionary(); + readonly ConcurrentDictionary _data = new ConcurrentDictionary(); + readonly ConcurrentDictionary _security_names = new ConcurrentDictionary(); readonly static Lazy _md = new Lazy(() => locate(), true); readonly Lazy _subscription = new Lazy(() => load_request()); - public MarketDataClient() + private MarketDataClient() { } + public static MarketDataClient create() + { + return new MarketDataClient(); + } + static MDRequest load_request() { var req = new MDRequest(); @@ -56,6 +62,12 @@ namespace bts.api.md return null; } + public string lookup_security_name(security_key key) + { + _security_names.TryGetValue(key, out string value); + return value; + } + public Security find_security(Func filter) { return _data.Values.Select(x => x.data.Security).Where(xx => filter(xx)).FirstOrDefault(); @@ -201,6 +213,14 @@ namespace bts.api.md /// public async Task start_pump(TimeSpan? interval = null) { + var rep = await _md.Value.request_names_async(new VoidMessage()); int count = 0; + do + { + foreach (var s in rep.value.Securities) + foreach (var ss in s.Names) + _security_names[ss.Key] = ss.Value; + } + while (!rep.success && count++ < 5); while (true) { var reply = await _md.Value.requestmd_async(_subscription.Value); @@ -246,6 +266,12 @@ namespace bts.api.md } } + public async void run_pump_async(TimeSpan? interval = null) + { + await this.start_pump(interval); + } + + readonly CancellationTokenSource _cts = new CancellationTokenSource(); public void Dispose() { diff --git a/src/client/spark_excel/rtd/formatting/JsonFormatter.cs b/src/client/spark_excel/rtd/formatting/JsonFormatter.cs index 17859bf22..4c23216ca 100644 --- a/src/client/spark_excel/rtd/formatting/JsonFormatter.cs +++ b/src/client/spark_excel/rtd/formatting/JsonFormatter.cs @@ -21,7 +21,7 @@ namespace bts.excel.rtd { if (_data is IEnumerable iem) return format_many(iem, include_headers); - else + else return format_one(_data, include_headers); } @@ -45,10 +45,28 @@ namespace bts.excel.rtd { string json = null; if(data is IMessage imsg) - json = Google.Protobuf.JsonFormatter.Default.Format(imsg); + json = Google.Protobuf.JsonFormatter.Default.Format(imsg); + else if (data is SecurityJoin jn) + { + System.IO.StringWriter writer = new System.IO.StringWriter(); + writer.Write("{ item1: "); + Google.Protobuf.JsonFormatter.Default.WriteValue(writer, jn.message); + if (jn.security != null) + { + writer.Write(", security: "); + Google.Protobuf.JsonFormatter.Default.WriteValue(writer, jn.security); + } + if (jn.security_name != null) + { + writer.Write(", security_name: "); + Google.Protobuf.JsonFormatter.Default.WriteValue(writer, jn.security_name); + } + writer.Write("}"); + json = writer.GetStringBuilder().ToString(); + } else json = JsonConvert.SerializeObject(data); - + JObject jo = JObject.Parse(json); Dictionary results = new Dictionary(); parse_object(jo, results); diff --git a/src/client/spark_excel/rtd/subscriptions/MDSubscribe.cs b/src/client/spark_excel/rtd/subscriptions/MDSubscribe.cs index d21f57ff9..015a56761 100644 --- a/src/client/spark_excel/rtd/subscriptions/MDSubscribe.cs +++ b/src/client/spark_excel/rtd/subscriptions/MDSubscribe.cs @@ -16,11 +16,13 @@ namespace bts.excel.rtd { readonly static Lazy _md = new Lazy(() => { - var md = new MarketDataClient(); - md.run_pump(TimeSpan.FromMilliseconds(333)); + var md = MarketDataClient.create(); + md.run_pump_async(TimeSpan.FromMilliseconds(333)); return md; }, true); + public static MarketDataClient md_client => _md.Value; + static IObservable md_observable(string contract, string symbol, string security_type, string exchange) { security_type = string.IsNullOrEmpty(security_type) ? SecurityType.Future.ToString() : security_type; diff --git a/src/client/spark_excel/rtd/subscriptions/SecurityJoiner.cs b/src/client/spark_excel/rtd/subscriptions/SecurityJoiner.cs new file mode 100644 index 000000000..5a6a34529 --- /dev/null +++ b/src/client/spark_excel/rtd/subscriptions/SecurityJoiner.cs @@ -0,0 +1,66 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; + +using ExcelDna.Integration; +using ExcelDna.Integration.Rtd; +using ExcelDna.Registration; +using ExcelDna.Registration.Utils; +using System.Reactive.Linq; +using bts.api; +using bts.api.md; +using bts.api.core; +using bts.api.orderman; +using bts.co2; +using Google.Protobuf; + +namespace bts.excel.rtd +{ + public class SecurityJoin + { + public IMessage message { get; private set; } + public IMessage security { get; private set;} + public string security_name { get; private set;} + public SecurityJoin(IMessage msg, IMessage sec, string sec_name) => + (message, security, security_name) = (msg, sec, sec_name); + } + + class SecurityJoiner + { + // readonly static Lazy _md = new Lazy(() => + // { + // using (var ctx = new api.core.ServiceContext()) + // { + // return ctx.locate(); + // } + // }, true); + + public static IEnumerable security_join(IEnumerable msgs) + { + List outs = new List(); + foreach (var msg in msgs) + { + if(msg is IMessage imsg) + { + var sk = imsg.Descriptor.FindFieldByName("security_key"); + if (sk != null) + { + uint key = (uint)sk.Accessor.GetValue(imsg); + //Security sec = MDSubscribe.md_client.lookup(key); + // if (sec != null) + // outs.Add(new SecurityJoin(msg, sec, null)); + // else + // { + string name = MDSubscribe.md_client.lookup_security_name(key) ?? string.Empty; + outs.Add(new SecurityJoin(imsg, null, name)); + // } + continue; + } + } + outs.Add(msg); + } + return outs; + } + } +} \ No newline at end of file diff --git a/src/client/spark_excel/rtd/subscriptions/Subscribe.cs b/src/client/spark_excel/rtd/subscriptions/Subscribe.cs index 563a6c785..b9cc27c70 100644 --- a/src/client/spark_excel/rtd/subscriptions/Subscribe.cs +++ b/src/client/spark_excel/rtd/subscriptions/Subscribe.cs @@ -9,13 +9,15 @@ using ExcelDna.Registration.Utils; using System.Reactive.Linq; using bts.api; using bts.api.md; -using bts.api.orderman; using bts.api.core; +using bts.api.orderman; +using bts.co2; namespace bts.excel.rtd { public class Subscribe { + readonly static Lazy _md = new Lazy(() => locate(), true); readonly static Lazy _orders = new Lazy(() => locate(), true); static T locate() where T : class, IApiClient { @@ -39,7 +41,8 @@ namespace bts.excel.rtd return _orders.Value.subscribe_orders().Select(x => { IReadOnlyCollection orders = x.all; - return new JsonFormatter(x.all).format(true); + var join = SecurityJoiner.security_join(orders); + return new JsonFormatter(join).format(true); }); } @@ -52,8 +55,12 @@ namespace bts.excel.rtd static IObservable observable_fills() { - return Observable.Timer(dueTime: TimeSpan.Zero, period: TimeSpan.FromSeconds(1)) - .Select(_ => new[] { DateTime.Now.ToString("HH:mm:ss"), DateTime.UtcNow.ToString("HH:mm:ss") }); + return _orders.Value.subscribe_fills().Select(x => + { + IReadOnlyCollection fills = x.all; + var join = SecurityJoiner.security_join(fills); + return new JsonFormatter(join).format(true); + }); } [ExcelFunction(Category = "BTS", Description = "Subscribe to BTS Spark API Service")] diff --git a/src/client/spark_excel/spark_excel_example.xlsm b/src/client/spark_excel/spark_excel_example.xlsm index 18b7131f7..6887b2d97 100644 Binary files a/src/client/spark_excel/spark_excel_example.xlsm and b/src/client/spark_excel/spark_excel_example.xlsm differ diff --git a/src/client/spark_sdk/samples/SDKMain.cs b/src/client/spark_sdk/samples/SDKMain.cs index 4f1a1d179..a6a936940 100644 --- a/src/client/spark_sdk/samples/SDKMain.cs +++ b/src/client/spark_sdk/samples/SDKMain.cs @@ -15,7 +15,7 @@ namespace bts.sdk { public static void Main(string[] args) { - MarketDataClient client = new MarketDataClient(); + MarketDataClient client = MarketDataClient.create(); CompositeDisposable disp = new CompositeDisposable(client); if (args.FirstOrDefault() == "ui") {