joined fills and orders with security
This commit is contained in:
1 parent
8bcec6b898
commit
fe3b46eb14
7 files changed
+131
-12
No files matched your search
@@ -23,13 +23,19 @@ namespace bts.api.md
|
||||
public ChannelUpdate data;
|
||||
}
|
||||
|
||||
readonly ConcurrentDictionary<security_key, Data> _data = new ConcurrentDictionary<security_key, Data>();
|
||||
readonly ConcurrentDictionary<security_key, Data> _data = new ConcurrentDictionary<security_key, Data>();
|
||||
readonly ConcurrentDictionary<security_key, string> _security_names = new ConcurrentDictionary<security_key, string>();
|
||||
readonly static Lazy<MarketDataRelay> _md = new Lazy<MarketDataRelay>(() => locate(), true);
|
||||
readonly Lazy<MDRequest> _subscription = new Lazy<MDRequest>(() => 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<Security, bool> filter)
|
||||
{
|
||||
return _data.Values.Select(x => x.data.Security).Where(xx => filter(xx)).FirstOrDefault();
|
||||
@@ -201,6 +213,14 @@ namespace bts.api.md
|
||||
/// <returns></returns>
|
||||
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()
|
||||
{
|
||||
|
||||
@@ -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<string, object> results = new Dictionary<string, object>();
|
||||
parse_object(jo, results);
|
||||
|
||||
@@ -16,11 +16,13 @@ namespace bts.excel.rtd
|
||||
{
|
||||
readonly static Lazy<MarketDataClient> _md = new Lazy<MarketDataClient>(() =>
|
||||
{
|
||||
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<object> md_observable(string contract, string symbol, string security_type, string exchange)
|
||||
{
|
||||
security_type = string.IsNullOrEmpty(security_type) ? SecurityType.Future.ToString() : security_type;
|
||||
|
||||
@@ -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<MarketDataRelay> _md = new Lazy<MarketDataRelay>(() =>
|
||||
// {
|
||||
// using (var ctx = new api.core.ServiceContext())
|
||||
// {
|
||||
// return ctx.locate<MarketDataRelay>();
|
||||
// }
|
||||
// }, true);
|
||||
|
||||
public static IEnumerable security_join(IEnumerable msgs)
|
||||
{
|
||||
List<object> outs = new List<object>();
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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<MarketDataRelay> _md = new Lazy<MarketDataRelay>(() => locate<MarketDataRelay>(), true);
|
||||
readonly static Lazy<OrderService> _orders = new Lazy<OrderService>(() => locate<OrderService>(), true);
|
||||
static T locate<T>() where T : class, IApiClient
|
||||
{
|
||||
@@ -39,7 +41,8 @@ namespace bts.excel.rtd
|
||||
return _orders.Value.subscribe_orders().Select(x =>
|
||||
{
|
||||
IReadOnlyCollection<OrderRecord> 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<object> 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<Fill> 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")]
|
||||
|
||||
Binary file not shown.
@@ -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")
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user