refactor: Get rid of old MarketDataClient in sdk examples
This commit is contained in:
1 parent
655f27543b
commit
ff65a5ca5b
10 files changed
+206
-647
No files matched your search
@@ -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<PriceSize> bids { get; }
|
||||
List<PriceSize> asks { get; }
|
||||
}
|
||||
|
||||
class MDBook : IMDBook, IMDUpdate
|
||||
{
|
||||
public MDBook()
|
||||
{
|
||||
}
|
||||
public SecurityImmutable security { get; private set; }
|
||||
|
||||
public List<PriceSize> bids { get; private set; }
|
||||
public List<PriceSize> 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()));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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<PriceSize> bids => _book.bids;
|
||||
public List<PriceSize> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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<SecurityKey, SecurityMD> _data = new ConcurrentDictionary<SecurityKey, SecurityMD>();
|
||||
readonly List<IDisposable> _disp = new List<IDisposable>();
|
||||
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<SecurityImmutable> securities => _data.Values.Select(x => x.security);
|
||||
public IObservable<IEnumerable<SecurityImmutable>> 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<IMDBook> > subscribe_book(Func<SecurityImmutable, bool> filter,
|
||||
TimeSpan? update_interval = null, IScheduler scheduler = null)
|
||||
{
|
||||
return _subscribe<MDBook>(filter, update_interval, scheduler);
|
||||
}
|
||||
public IObservable< IEnumerable<IMDLast> > subscribe_last(Func<SecurityImmutable, bool> filter,
|
||||
TimeSpan? update_interval = null, IScheduler scheduler = null)
|
||||
{
|
||||
return _subscribe<MDLast>(filter, update_interval, scheduler);
|
||||
}
|
||||
public IObservable< IEnumerable<IMDMisc> > subscribe_misc(Func<SecurityImmutable, bool> filter,
|
||||
TimeSpan? update_interval = null, IScheduler scheduler = null)
|
||||
{
|
||||
return _subscribe<MDMisc>(filter, update_interval, scheduler);
|
||||
}
|
||||
public IObservable< IEnumerable<IMDCombined> > subscribe_combined(Func<SecurityImmutable, bool> filter,
|
||||
TimeSpan? update_interval = null, IScheduler scheduler = null)
|
||||
{
|
||||
return _subscribe<MDCombined>(filter, update_interval, scheduler);
|
||||
}
|
||||
|
||||
IObservable<IEnumerable<T>> _subscribe<T>(Func<SecurityImmutable, bool> filter,
|
||||
TimeSpan? update_interval = null, IScheduler scheduler = null) where T : IMDData, IMDUpdate, new()
|
||||
{
|
||||
List<Slot<T>> slots = new List<Slot<T>>(); int data_size = 0;
|
||||
return timer_pump<IEnumerable<T>>((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<T>(xx)).ToList();
|
||||
data_size = _data.Count;
|
||||
}
|
||||
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)
|
||||
if(did_update)
|
||||
updates.Add(slot.data);
|
||||
}
|
||||
if(updates.Count > 0)
|
||||
observer.OnNext(updates);
|
||||
},
|
||||
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)
|
||||
if(did_update)
|
||||
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)
|
||||
{
|
||||
secmd = md;
|
||||
last_update_time = 0;
|
||||
data = new T();
|
||||
}
|
||||
public SecurityMD secmd;
|
||||
public ulong last_update_time;
|
||||
public T data;
|
||||
}
|
||||
|
||||
IObservable<T> timer_pump<T>(Action<ulong, IObserver<T>> tick, TimeSpan? interval, IScheduler scheduler = null)
|
||||
{
|
||||
if(!interval.HasValue)
|
||||
interval = TimeSpan.FromMilliseconds(_default_pump_ms);
|
||||
ulong last_update_time = 0;
|
||||
return Observable.Create<T>(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<SecurityMD> find(Func<SecurityImmutable, bool> filter)
|
||||
{
|
||||
Func<SecurityMD, bool> filt = (SecurityMD secmd) => filter(secmd.security);
|
||||
return _data.Values.Where(filt);
|
||||
}
|
||||
|
||||
public SecurityImmutable? find_security(Func<SecurityImmutable, bool> 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<SecurityImmutable?> find_security_async(Func<SecurityImmutable, bool> filter, TimeSpan? timeout = null)
|
||||
{
|
||||
if (!timeout.HasValue)
|
||||
{
|
||||
timeout = TimeSpan.FromMilliseconds(10000);
|
||||
}
|
||||
CancellationTokenSource src = new CancellationTokenSource(timeout.Value);
|
||||
SecurityImmutable? security = await Task.Run<SecurityImmutable?>(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
|
||||
}
|
||||
}
|
||||
@@ -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<IDisposable> _disps = new List<IDisposable>();
|
||||
public MarketDataSample(MarketDataClient client)
|
||||
public MarketDataSample(IncrementalMarketDataClient client)
|
||||
{
|
||||
_md = client;
|
||||
}
|
||||
|
||||
void print_update(IEnumerable<IMDData> updates)
|
||||
void print_update<T>(IEnumerable<T> 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<SecurityImmutable, bool> 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<SecurityImmutable, bool> 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));
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
@@ -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<IDisposable> _disps = new List<IDisposable>();
|
||||
public OrderSample(MarketDataClient client)
|
||||
{
|
||||
_md = client;
|
||||
IReadOnlyDictionary<uint, SecurityImmutable> _securities =
|
||||
new Dictionary<uint, SecurityImmutable>(0);
|
||||
|
||||
public OrderSample(
|
||||
OrdersClient orders_client,
|
||||
IncrementalMarketDataClient md_client,
|
||||
IObservable<IReadOnlyDictionary<uint, SecurityImmutable>> securities_src
|
||||
) {
|
||||
_orders_client = orders_client;
|
||||
_md = md_client;
|
||||
_disps.Add(securities_src.Subscribe(secs => _securities = secs));
|
||||
}
|
||||
|
||||
readonly static Lazy<OrderService> _orders = new Lazy<OrderService>(() => Locator.locate<OrderService>(), 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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<api.user.AuthService>();
|
||||
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 <exchange> <security_type> <symbol> [<security_desc>]");
|
||||
Console.WriteLine(" order <account> <exchange> <security_type> <symbol> <security_desc> <signed_quantity> [<price>]");
|
||||
Console.WriteLine("Arguments:");
|
||||
Console.WriteLine(" --user=<bts user name>");
|
||||
Console.WriteLine(" --pass=<password>");
|
||||
Console.WriteLine(" --pump=<pump_interval_in_ms=100 default>");
|
||||
Environment.Exit(1);
|
||||
}
|
||||
|
||||
static void order_help()
|
||||
{
|
||||
Console.WriteLine("order <exchange> <security_type> <symbol> <security_desc> <signed_quantity> [<price>] [<user>] [<account>]");
|
||||
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 <exchange> <security_type> <symbol>");
|
||||
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<api.user.AuthService>();
|
||||
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<OrderService>();
|
||||
var admin_service = ctx.locate<AdminService>();
|
||||
var md_relay = ctx.locate<MarketDataRelay>();
|
||||
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 <exchange> <security_type> <symbol> [<security_desc>]");
|
||||
Console.WriteLine(" order <account> <exchange> <security_type> <symbol> <security_desc> <signed_quantity> [<price>]");
|
||||
Console.WriteLine("Arguments:");
|
||||
Console.WriteLine(" --user=<bts user name>");
|
||||
Console.WriteLine(" --pass=<password>");
|
||||
Console.WriteLine(" --pump=<pump_interval_in_ms=100 default>");
|
||||
Environment.Exit(1);
|
||||
}
|
||||
|
||||
static void order_help()
|
||||
{
|
||||
Console.WriteLine("order <exchange> <security_type> <symbol> <security_desc> <signed_quantity> [<price>] [<user>] [<account>]");
|
||||
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 <exchange> <security_type> <symbol>");
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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
|
||||
#endif
|
||||
Reference in new issue
Block a user