Merge branch 'master' of bitbucket.org:bluetradesys/spark into admin_data_no_trading_permission

This commit is contained in:
Jake Norbut committed 2019-02-15 10:17:46 -06:00
commit d7da2ef953
160 files changed
+3899 -2084

No files matched your search

+1
View File
@@ -26,6 +26,7 @@ src/server/sparkd/orderman/perf_analysis/logs/
src/server/sparkd/orderman/perf_analysis/trace_data/
.fake
*.fsx.lock
*.code-workspace
src/lib/orderman/perf_analysis/logs/latency_data/
trace_data/
/src/lib/orderman/perf_analysis/last_pretty_log.csv
+2 -2
View File
@@ -6,7 +6,7 @@
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceRoot}/build/ninja_debug/src/server/sparkd/sparkd",
"program": "${workspaceRoot}/build/ninja_debug/server/sparkd/sparkd",
"args": ["run"],
"stopAtEntry": false,
"cwd": "${workspaceRoot}/build/ninja_debug/src/server/sparkd",
@@ -209,7 +209,7 @@
"type": "cppdbg",
"request": "launch",
"program": "/home/bts/spark/build/ninja_debug/src/server/sparkd/sparkd",
"args": ["test","AuthToken"],
"args": ["test", "[md]"],
"stopAtEntry": false,
"cwd": "/home/bts/spark/build/ninja_debug/src/server/sparkd/",
"environment": [],
+21
View File
@@ -1,5 +1,26 @@
# Release Notes
## 1.5.0
### User Visible Changes
#### User Interface Improvements
- Removed security key field from most grids
- Removed trade key field from time and sales grid
- Fixed the floating order book so that rows are no longer
highlighted after an order is modified
#### Bug Fixes
- Fixed the "Since Open" time filter to use the exchange open time rather than 8:30 AM
#### Algos
- Added OCO Algo
- An OCO order creates a relationship between a child order and a 'stop' or 'other' order such that when one of these is cancelled, both cancel, or when one is filled, the other order's size is debited in the amount of the fill. Composing this order with Cover simulates scalping functionality
#### SDK
- Improved SDK documentation and usability
- Dynamic orders are now properly identified in the order book
### Internal Changes
- Improved robustness of limit system test
- Extensive market data refactoring allow for security definition updates
## 1.4.11
### New Features
- Floating Order Book
@@ -32,7 +32,7 @@ namespace bts {
if (!orders_success) {
_error_reporter.report_error($"Unable to delete synthetic security ({id}): unable to determine whether or not security ({id}) has working orders (deletion is not permitted while there are working orders).");
Log.Ui.warn($"Ignoring request to delete synthetic security ({id}) because the orders lookup timed out (must confirm no working orders).");
} else if (orders.state.Values.Any(o => o.Def.SecurityKey == id && o.is_non_terminal())) {
} else if (orders.state.Values.Any(o => o.order.Def.SecurityKey == id && o.order.is_non_terminal())) {
Log.Ui.warn($"Ignoring request to delete synthetic security ({id}) because non-terminal-orders were detected.");
} else {
ExecutionStrategyImmutable? strategy = await _orders_model.spreader.lookup_strategy_async(id);
+7 -5
View File
@@ -14,19 +14,21 @@ using SecurityKey = System.UInt32;
using System.Collections.Concurrent;
using System.Threading;
using bts.api.user;
using bts.md;
namespace bts {
[Export(typeof(IInitializable))]
class MDDaemon : bts.md.MDSource, IObserver<List<SecurityAndHandle>>, IInitializable, IDisposable {
class MDDaemon : md.MDSource, IObserver<List<SecurityAndHandle>>, IInitializable, IDisposable {
readonly Lazy<IBus> _bus;
readonly Lazy<AuthTokenImmutable> _token;
readonly Lazy<SyntheticSecurityService> _ss;
readonly Dictionary<SecurityKey, SecurityMD> _deleted_synthetics = new Dictionary<SecurityKey, SecurityMD>();
static uint get_naggle() => mbts.config.Configuration.get<bts.md.RelayOptions>().naggle_ms;
static uint get_naggle() => mbts.config.Configuration.get<md.RelayOptions>().naggle_ms;
[ImportingConstructor]
public MDDaemon(Lazy<IBus> bus, Lazy<AuthTokenImmutable> token, Lazy<SyntheticSecurityService> ss) : base(get_naggle(), true) {
public MDDaemon(Lazy<IBus> bus, Lazy<AuthTokenImmutable> token, Lazy<SyntheticSecurityService> ss) :
base(get_naggle(), MDSourceOptions.DeferInit) {
_bus = bus;
_token = token;
_ss = ss;
@@ -44,14 +46,14 @@ namespace bts {
if (!deleted_synthetic_keys.Contains(kvp.Key)) {
_exchange_sec_map.TryAdd(kvp.Key, kvp.Value);
_deleted_synthetics.Remove(kvp.Key);
_version += 1;
_version = DateTime.UtcNow;
}
}
// Remove any newly deleted synthetics and store them for potential undelete
foreach (var key in deleted_synthetic_keys) {
if (_exchange_sec_map.TryRemove(key, out SecurityMD deleted)) {
_deleted_synthetics[key] = deleted;
_version += 1;
_version = DateTime.UtcNow;
}
}
// Post full dictionary of syntheics here
+11 -9
View File
@@ -19,7 +19,7 @@ namespace bts.shell {
readonly OrdersModel _model;
readonly ErrorReporter _error_reporter;
IReadOnlyDictionary<uint, SecurityMD> _md_map;
TableUpdate<OrderRecordImmutable, uint> _orders;
TableUpdate<(SecurityImmutable? sec, OrderRecordImmutable order), uint> _orders;
IDisposable _dispose;
[ImportingConstructor]
@@ -57,14 +57,16 @@ namespace bts.shell {
}
void _show_modify_order(uint order_key) {
if (_orders.state.TryGetValue(order_key, out OrderRecordImmutable ori) && _md_map.TryGetValue(ori.Def.SecurityKey, out SecurityMD smd)
&& (ori.HasDef && ori.Def.HasCustomDef && !(ori.Def.CustomDef.HasStop || ori.Def.CustomDef.HasStopLimit)))
_show_order_window(new OrderModificationDialog {
DataContext = new ExistingOrderTicketVM(_model,
Observable.Return(new OrderRecordTuple(ori, smd)),
Observable.Return(_model.factory.default_ladder_settings(smd.security).calculator_buttons)
)
});
if (_orders.state.TryGetValue(order_key, out var ori) && ori.sec.HasValue
&& (ori.order.HasDef && ori.order.Def.HasCustomDef && !(ori.order.Def.CustomDef.HasStop || ori.order.Def.CustomDef.HasStopLimit))) {
var md = _md_map.TryGetValue(ori.order.security_key());
_show_order_window(new OrderModificationDialog {
DataContext = new ExistingOrderTicketVM(_model,
Observable.Return((md,ori.order)),
Observable.Return(_model.factory.default_ladder_settings(ori.sec.Value).calculator_buttons)
)
});
}
}
}
}
+4 -1
View File
@@ -5,7 +5,7 @@ using bts.api.orderman;
using bts.api.persistence;
using bts.api.position;
using bts.api.synthetic;
using bts.api.user;
using bts.co2;
using bts.metrics;
using bts.support;
using System.ComponentModel.Composition;
@@ -41,5 +41,8 @@ namespace Spark {
[Export]
public MarketDataAuxiliaryService md_auxiliary_service => context.locate<MarketDataAuxiliaryService>();
[Export]
public MarketDataRelay md_replay => context.locate<MarketDataRelay>();
}
}
@@ -96,7 +96,7 @@ namespace bts.shell {
public static GridColumnSettings day_change => new GridColumnSettings(col_id(Columns.DayChange), DefaultColumnHeaders.DayChange, width: DefaultColumnWidths.Price, horizontal_alignment: System.Windows.HorizontalAlignment.Right);
public static GridColumnSettings session_low => new GridColumnSettings(col_id(Columns.SessionLow), DefaultColumnHeaders.SessionLow, width: DefaultColumnWidths.Price, horizontal_alignment: System.Windows.HorizontalAlignment.Right);
public static GridColumnSettings session_high => new GridColumnSettings(col_id(Columns.SessionHigh), DefaultColumnHeaders.SessionHigh, width: DefaultColumnWidths.Price, horizontal_alignment: System.Windows.HorizontalAlignment.Right);
public static GridColumnSettings security_key => new GridColumnSettings(col_id(Columns.SecurityKey), DefaultColumnHeaders.SecurityKey, width: DefaultColumnWidths.SecurityKey);
public static GridColumnSettings security_key => new GridColumnSettings(col_id(Columns.SecurityKey), DefaultColumnHeaders.SecurityKey, is_visible: false, width: DefaultColumnWidths.SecurityKey);
@@ -38,7 +38,6 @@ namespace mbts.spark {
price_filter.load_state(filters.price);
size_filter.load_state(filters.size);
aggressor_filter.load_state(filters.aggressor_sides);
trade_key_filter.load_state(filters.trade_key);
security_key_filter.load_state(filters.security_key);
}
// Columns
@@ -47,7 +46,6 @@ namespace mbts.spark {
add_column(TimeAndSalesView.Columns.Price, DefaultColumnHeaders.Price, DefaultColumnWidths.Price, nameof(Row.price), price_filter, DataGridFilterKnownTemplates.DoubleMinMax, System.Windows.HorizontalAlignment.Right);
add_column(TimeAndSalesView.Columns.Size, DefaultColumnHeaders.Size, DefaultColumnWidths.Size, nameof(Row.size), size_filter, DataGridFilterKnownTemplates.UIntMinMax, System.Windows.HorizontalAlignment.Right);
add_column(TimeAndSalesView.Columns.Aggressor, DefaultColumnHeaders.Aggressor, DefaultColumnWidths.Aggressor, nameof(Row.aggressor_side), aggressor_filter, DataGridFilterKnownTemplates.EnumValues);
add_column(TimeAndSalesView.Columns.TradeKey, DefaultColumnHeaders.TradeKey, DefaultColumnWidths.TradeKey, nameof(Row.trade_key), trade_key_filter, DataGridFilterKnownTemplates.Regex);
add_column(TimeAndSalesView.Columns.SecurityKey, DefaultColumnHeaders.SecurityKey, DefaultColumnWidths.SecurityKey, nameof(Row.security_key), security_key_filter, DataGridFilterKnownTemplates.Regex);
void add_column(TimeAndSalesView.Columns col_key, string header, double width, string sort_member_path, IDataGridColumnFilter filter, string filter_template, System.Windows.HorizontalAlignment h_alignment = System.Windows.HorizontalAlignment.Left) =>
columns.Add(new TimeAndSalesColumn(
@@ -66,7 +64,7 @@ namespace mbts.spark {
_resources.AddRange(
show_ladder,
DataGridFilterHelper.hook_filters(
rows_view, time_filter, security_filter, price_filter, size_filter, aggressor_filter, trade_key_filter, security_key_filter),
rows_view, time_filter, security_filter, price_filter, size_filter, aggressor_filter, security_key_filter),
settings_manager.subscribe_current().OnDispatcher().Subscribe(grid_settings_manager)
);
rows_view.set_sort_descriptions(startup_data.sorting_criterion, columns);
@@ -103,7 +101,6 @@ namespace mbts.spark {
public IDataGridColumnPersistentFilter<Row, UIntMinMaxState> size_filter { get; } = new UIntMinMax<Row>(r => r.size, nameof(Row.size));
public IDataGridColumnPersistentFilter<Row, AggressorSide[]> aggressor_filter { get; } = DiscreteValuesFilterVM.Create<Row, AggressorSide>(EnumHelper.get_values<AggressorSide>().ToArray(), r => r.aggressor_side, nameof(Row.aggressor_side));
public IDataGridColumnPersistentFilter<Row, string> security_key_filter { get; } = new RegexFilterVM<Row>(r => r.security_key.ToString(), nameof(Row.security_key));
public IDataGridColumnPersistentFilter<Row, string> trade_key_filter { get; } = new RegexFilterVM<Row>(r => r.trade_key.ToString(), nameof(Row.trade_key));
FilterStates _get_filter_states() =>
new FilterStates {
@@ -111,7 +108,6 @@ namespace mbts.spark {
security = security_filter.get_state(),
price = price_filter.get_state(),
size = size_filter.get_state(),
trade_key = trade_key_filter.get_state(),
security_key = security_key_filter.get_state(),
aggressor_sides = aggressor_filter.get_state()
};
@@ -175,7 +171,6 @@ namespace mbts.spark {
case TimeAndSalesView.Columns.Price: return _price_text;
case TimeAndSalesView.Columns.Size: return _size_text;
case TimeAndSalesView.Columns.Aggressor: return _aggressor_text;
case TimeAndSalesView.Columns.TradeKey: return _trade_key_text;
case TimeAndSalesView.Columns.SecurityKey: return _security_key_text;
default: return null;
}
@@ -7,7 +7,7 @@ namespace mbts.ui.spark {
/// Interaction logic for TimeAndSalesView.xaml
/// </summary>
partial class TimeAndSalesView : UserControl {
public enum Columns { Time, Security, Price, Size, OrderType, TradeKey, SecurityKey, Aggressor }
public enum Columns { Time, Security, Price, Size, OrderType, SecurityKey, Aggressor }
public TimeAndSalesView() =>
InitializeComponent();
@@ -84,8 +84,7 @@ namespace bts.shell {
public static GridColumnSettings price => new GridColumnSettings(col_id(Columns.Price), DefaultColumnHeaders.Price, width: DefaultColumnWidths.Price, horizontal_alignment: System.Windows.HorizontalAlignment.Right);
public static GridColumnSettings size => new GridColumnSettings(col_id(Columns.Size), DefaultColumnHeaders.Size, width: DefaultColumnWidths.Size, horizontal_alignment: System.Windows.HorizontalAlignment.Right);
public static GridColumnSettings aggressor => new GridColumnSettings(col_id(Columns.Aggressor), DefaultColumnHeaders.Aggressor, width: DefaultColumnWidths.Aggressor);
public static GridColumnSettings trade_key => new GridColumnSettings(col_id(Columns.TradeKey), DefaultColumnHeaders.TradeKey, width: DefaultColumnWidths.TradeKey);
public static GridColumnSettings security_key => new GridColumnSettings(col_id(Columns.SecurityKey), DefaultColumnHeaders.SecurityKey, width: DefaultColumnWidths.SecurityKey);
public static GridColumnSettings security_key => new GridColumnSettings(col_id(Columns.SecurityKey), DefaultColumnHeaders.SecurityKey, is_visible:false, width: DefaultColumnWidths.SecurityKey);
public static IEnumerable<SortingCriteria> default_sorting_criterion {
get {
@@ -100,7 +99,6 @@ namespace bts.shell {
yield return price;
yield return size;
yield return aggressor;
yield return trade_key;
yield return security_key;
}
}
@@ -117,8 +115,8 @@ namespace bts.shell {
public DoubleMinMaxState price;
[ProtoMember(4)]
public UIntMinMaxState size;
[ProtoMember(5)]
public string trade_key;
// [ProtoMember(5)]
// ProtoMember 5 cannot be used further
[ProtoMember(6)]
public string security_key;
[ProtoMember(7)]
@@ -66,7 +66,7 @@ namespace bts.shell {
public static GridColumnSettings description => new GridColumnSettings(col_id(Columns.Description), DefaultColumnHeaders.Description, width: DefaultColumnWidths.Description);
public static GridColumnSettings n_legs => new GridColumnSettings(col_id(Columns.n_legs), DefaultColumnHeaders.N_Legs, width: DefaultColumnWidths.N_Legs);
public static GridColumnSettings n_working_orders => new GridColumnSettings(col_id(Columns.n_working_orders), DefaultColumnHeaders.N_Working_Orders, width: DefaultColumnWidths.N_Working_Orders);
public static GridColumnSettings security_key => new GridColumnSettings(col_id(Columns.SecurityKey), DefaultColumnHeaders.SecurityKey, width: DefaultColumnWidths.SecurityKey);
public static GridColumnSettings security_key => new GridColumnSettings(col_id(Columns.SecurityKey), DefaultColumnHeaders.SecurityKey, is_visible: false, width: DefaultColumnWidths.SecurityKey);
public static IEnumerable<SortingCriteria> default_sorting_criterion {
get {
@@ -39,7 +39,7 @@ namespace mbts.spark {
user_filter = DiscreteValuesFilterVM.Create<Row, string>(_model.factory.subscribe_users().Select(d => d.Values), r => r.user, nameof(Row.user));
security_filter = DiscreteValuesFilterVM.Create<Row, SecurityImmutable, SecurityCriteria>(
fills_source.with_respect_to(
ft => ft.security_md.HasValue ? ft.security_md.Value.security : SecurityDataHelper.Defaults.SecurityImmutable,
ft => ft.security ?? SecurityDataHelper.Defaults.SecurityImmutable,
s => s.Id, ft => ft.key
).Select(t => (t.added.Values, t.removed.Values)),
r => r.security,
@@ -56,7 +56,7 @@ namespace mbts.spark {
add_col(Columns.Account, DefaultColumnHeaders.Account, r => r.account, nameof(Row.account), account_filter, DataGridFilterKnownTemplates.DiscreteValues, DefaultColumnWidths.Account);
add_col(Columns.Exchange, DefaultColumnHeaders.Exchange, r => r.exchange.ToString(), nameof(Row.exchange), exchange_filter, DataGridFilterKnownTemplates.DiscreteValues, DefaultColumnWidths.Exchange);
add_col(Columns.TimeInForce, DefaultColumnHeaders.TimeInForce, r => r.time_in_force.ToString(), nameof(Row.time_in_force), time_in_force_filter, DataGridFilterKnownTemplates.DiscreteValues, DefaultColumnWidths.TimeInForce);
columns.Add(new OrderTypeColumn<Columns>(Columns.OrderType, new Binding(nameof(Row.order_type)), new Binding(nameof(Row.order_metadata)), order_type_filter) { id = col_id(Columns.OrderType), sort_member_path = nameof(Row.order_type) });
columns.Add(new OrderTypeColumn<Columns>(Columns.OrderType, new Binding(nameof(Row.order_type)), new Binding(nameof(Row.order_metadata)), new Binding(nameof(Row.dynamic_order_type_display_name)), order_type_filter) { id = col_id(Columns.OrderType), sort_member_path = nameof(Row.order_type) });
columns.Add(new SideColumn<Row, Columns>(Columns.Side, r => r.side, side_filter) { id = col_id(Columns.Side), sort_member_path = nameof(Row.side) });
add_col(Columns.Size, DefaultColumnHeaders.Size, r => r.size.ToString(), nameof(Row.size), size_filter, DataGridFilterKnownTemplates.UIntMinMax, DefaultColumnWidths.Size);
add_col(Columns.Price, DefaultColumnHeaders.Price, r => r.format_price(r.price), nameof(Row.price), price_filter, DataGridFilterKnownTemplates.DoubleMinMax, DefaultColumnWidths.Price);
@@ -186,7 +186,8 @@ namespace mbts.spark {
readonly FillImmutable _fill;
readonly OrderRecordImmutable _order;
public Row(FillTuple fill_tuple, OrdersModel model) : base(fill_tuple.security_md.HasValue ? fill_tuple.security_md.Value.security : SecurityDataHelper.Defaults.SecurityImmutable) {
public Row(FillTuple fill_tuple, OrdersModel model) :
base(fill_tuple.security ?? SecurityDataHelper.Defaults.SecurityImmutable) {
_fill = fill_tuple.fill;
_order = fill_tuple.order ?? OrderDataHelper.Defaults.OrderRecordImmutable;
_model = model;
@@ -196,6 +197,7 @@ namespace mbts.spark {
public DateTime fill_time => _fill.Timestamp.ToDateTimeOffset().LocalDateTime;
public TimeInForce time_in_force => _order.time_in_force();
public OrderType order_type => _order.order_type();
public string dynamic_order_type_display_name => _order.dynamic_order_type_display_name();
public OrderMetadata order_metadata => _model.factory.get_metadata(_order.Def.CustomDef.CustomCase);
public string account { get; private set; }
public string user => _order.user();
@@ -86,7 +86,7 @@ namespace bts.shell {
public static GridColumnSettings fill_kind => new GridColumnSettings(col_id(Columns.FillKind), DefaultColumnHeaders.FillKind, width: DefaultColumnWidths.FillKind);
public static GridColumnSettings fill_key => new GridColumnSettings(col_id(Columns.FillKey), DefaultColumnHeaders.FillKey, width: DefaultColumnWidths.FillKey);
public static GridColumnSettings order_key => new GridColumnSettings(col_id(Columns.OrderKey), DefaultColumnHeaders.OrderKey, width: DefaultColumnWidths.OrderKey);
public static GridColumnSettings security_key => new GridColumnSettings(col_id(Columns.SecurityKey), DefaultColumnHeaders.SecurityKey, width: DefaultColumnWidths.SecurityKey);
public static GridColumnSettings security_key => new GridColumnSettings(col_id(Columns.SecurityKey), DefaultColumnHeaders.SecurityKey, is_visible: false, width: DefaultColumnWidths.SecurityKey);
public static IEnumerable<SortingCriteria> default_sorting_criterion {
get {
yield return new SortingCriteria(fill_time.id, false);
@@ -30,7 +30,7 @@ namespace mbts.spark {
interface ICVOrderBookColumn : IKeyedColumn<Columns>, IGridColumn {
string sort_member_path { get; }
ListSortDirection? SortDirection { get; set; }
IDataGridColumnFilter<OrderRecordTuple> filter { get; }
IDataGridColumnFilter<OrderSecurity> filter { get; }
IComparer<OrderBookRowVM> comparer { get; }
}
@@ -65,9 +65,9 @@ namespace mbts.spark {
public OrderBookColumnCollection(IBus bus, OrdersModel model, IObservable<(IEnumerable<SecurityImmutable> added, IEnumerable<SecurityImmutable> removed)> order_securities) {
// security
Add(security_column = new SecurityColumn(
DiscreteValuesFilterVM.Create<OrderRecordTuple, SecurityImmutable, SecurityCriteria>(
DiscreteValuesFilterVM.Create<OrderSecurity, SecurityImmutable, SecurityCriteria>(
order_securities,
ort => ort.security_md.HasValue ? ort.security_md.Value.security : SecurityDataHelper.Defaults.SecurityImmutable,
ort => ort.security ?? SecurityDataHelper.Defaults.SecurityImmutable,
SecurityCriteria.from_securities,
SecurityCriteria.get_to_securities(bus),
nameof(OrderBookRowVM.security),
@@ -81,9 +81,9 @@ namespace mbts.spark {
nameof(OrderBookRowVM.symbol),
r => r.symbol,
DefaultColumnWidths.Symbol,
DiscreteValuesFilterVM.Create<OrderRecordTuple, string>(
DiscreteValuesFilterVM.Create<OrderSecurity, string>(
mbts.co2.UserSymbol.get_all().Select(u => u.symbol).Distinct().ToList(),
ort => ort.security_md.HasValue ? ort.security_md.Value.security.Symbol : string.Empty,
ort => ort.security?.Symbol ?? string.Empty,
nameof(OrderBookRowVM.symbol)
),
DataGridFilterKnownTemplates.DiscreteValues,
@@ -95,7 +95,7 @@ namespace mbts.spark {
nameof(OrderBookRowVM.time_created),
r => r.time_created_text,
DefaultColumnWidths.Time,
new TimeFilterVM<OrderRecordTuple>(
new TimeFilterVM<OrderSecurity>(
ort => ort.order.created(),
nameof(OrderBookRowVM.time_created)),
DataGridFilterKnownTemplates.TimeFilter,
@@ -107,7 +107,7 @@ namespace mbts.spark {
nameof(OrderBookRowVM.last_change),
r => r.last_change_text,
DefaultColumnWidths.Time,
new TimeFilterVM<OrderRecordTuple>(
new TimeFilterVM<OrderSecurity>(
ort => ort.order.last_change(),
nameof(OrderBookRowVM.last_change)),
DataGridFilterKnownTemplates.TimeFilter,
@@ -119,7 +119,7 @@ namespace mbts.spark {
nameof(OrderBookRowVM.user),
r => r.user,
DefaultColumnWidths.User,
DiscreteValuesFilterVM.Create<OrderRecordTuple, string>(
DiscreteValuesFilterVM.Create<OrderSecurity, string>(
model.factory.subscribe_users().Select(d => d.Values),
ort => ort.order.user(),
nameof(OrderBookRowVM.user)),
@@ -132,7 +132,7 @@ namespace mbts.spark {
nameof(OrderBookRowVM.account),
r => r.account,
DefaultColumnWidths.Account,
DiscreteValuesFilterVM.Create<OrderRecordTuple, string, string[]>(
DiscreteValuesFilterVM.Create<OrderSecurity, string, string[]>(
model.factory.subscribe_accounts().Select(d => d.Values),
ort => ort.order.account(),
vals => vals.ToArray(),
@@ -147,9 +147,9 @@ namespace mbts.spark {
nameof(OrderBookRowVM.exchange),
r => r.exchange.ToString(),
DefaultColumnWidths.Exchange,
DiscreteValuesFilterVM.Create<OrderRecordTuple, Venue>(
DiscreteValuesFilterVM.Create<OrderSecurity, Venue>(
EnumHelper.get_values_except(Venue.Unset).ToList(),
ort => ort.security_md.HasValue ? ort.security_md.Value.security.Exchange : Venue.Unset,
ort => ort.security.exchange_or_default(),
nameof(OrderBookRowVM.exchange)),
DataGridFilterKnownTemplates.EnumValues,
_wrap_comparer(r => r.exchange)));
@@ -160,7 +160,7 @@ namespace mbts.spark {
nameof(OrderBookRowVM.time_in_force),
r => r.time_in_force.ToString(),
DefaultColumnWidths.TimeInForce,
DiscreteValuesFilterVM.Create<OrderRecordTuple, TimeInForce>(
DiscreteValuesFilterVM.Create<OrderSecurity, TimeInForce>(
EnumHelper.get_values<TimeInForce>().ToList(),
ort => ort.order.time_in_force(),
nameof(OrderBookRowVM.time_in_force)),
@@ -168,7 +168,7 @@ namespace mbts.spark {
_wrap_comparer(r => r.time_in_force)));
// order type
Add(order_type_column = new OrderBookOrderTypeColumn(
DiscreteValuesFilterVM.Create<OrderRecordTuple, OrderType>(
DiscreteValuesFilterVM.Create<OrderSecurity, OrderType>(
utilities.OrderFactory.all_order_types,
ort => ort.order.order_type(),
nameof(OrderBookRowVM.order_type))));
@@ -179,7 +179,7 @@ namespace mbts.spark {
nameof(OrderBookRowVM.order_sub_type),
r => r.order_sub_type_text,
80,
DiscreteValuesFilterVM.Create<OrderRecordTuple, OrderSubType>(
DiscreteValuesFilterVM.Create<OrderSecurity, OrderSubType>(
EnumHelper.get_values<OrderSubType>().ToList(),
ort => ort.order.sub_type(),
nameof(OrderBookRowVM.order_sub_type)),
@@ -187,20 +187,20 @@ namespace mbts.spark {
_wrap_comparer(r => r.order_sub_type)));
// side
Add(side_column = new SideColumn(
DiscreteValuesFilterVM.Create<OrderRecordTuple, Side>(
DiscreteValuesFilterVM.Create<OrderSecurity, Side>(
new[] { Side.Buy, Side.Sell },
ort=> ort.order.side(),
nameof(OrderBookRowVM.side))));
// quantity
Add(qty_column = new SizeColumn(
new UIntMinMax<OrderRecordTuple>(ort => ort.order.quantity(), nameof(OrderBookRowVM.quantity))));
new UIntMinMax<OrderSecurity>(ort => ort.order.quantity(), nameof(OrderBookRowVM.quantity))));
// edit quantity column
// not usually part of the main order book, but it's useful to add it
// rather than debugging the popup window
//Add(edit_qty_column = new EditableQtyColumn());
// filled quantity
Add(filled_qty_column = new FilledColumn(
new UIntMinMax<OrderRecordTuple>(ort => ort.order.filled(), nameof(OrderBookRowVM.filled_quantity))));
new UIntMinMax<OrderSecurity>(ort => ort.order.filled(), nameof(OrderBookRowVM.filled_quantity))));
// price
Add(price_column = new SimpleTextColumn<DoubleMinMaxState>(
Columns.Price,
@@ -208,7 +208,7 @@ namespace mbts.spark {
nameof(OrderBookRowVM.price),
r => r.price_text,
DefaultColumnWidths.Price,
new DoubleMinMax<OrderRecordTuple>(
new DoubleMinMax<OrderSecurity>(
ort => ort.order.price(),
nameof(OrderBookRowVM.price)),
DataGridFilterKnownTemplates.DoubleMinMax,
@@ -224,7 +224,7 @@ namespace mbts.spark {
nameof(OrderBookRowVM.avg_price),
r => r.avg_price_text,
DefaultColumnWidths.Price,
new DoubleMinMax<OrderRecordTuple>(
new DoubleMinMax<OrderSecurity>(
ort => ort.order.avg_price(),
nameof(OrderBookRowVM.avg_price)),
DataGridFilterKnownTemplates.DoubleMinMax,
@@ -236,7 +236,7 @@ namespace mbts.spark {
nameof(OrderBookRowVM.last_price),
r => r.last_price_text,
DefaultColumnWidths.Price,
new DoubleMinMax<OrderRecordTuple>(
new DoubleMinMax<OrderSecurity>(
ort => ort.order.last_price(),
nameof(OrderBookRowVM.last_price)),
DataGridFilterKnownTemplates.DoubleMinMax,
@@ -248,7 +248,7 @@ namespace mbts.spark {
nameof(OrderBookRowVM.status),
r => r.status.ToString(),
80,
DiscreteValuesFilterVM.Create<OrderRecordTuple, OrderStatus>(
DiscreteValuesFilterVM.Create<OrderSecurity, OrderStatus>(
EnumHelper.get_values<OrderStatus>().ToList(),
ort => ort.order.status(),
nameof(OrderBookRowVM.status)),
@@ -256,7 +256,7 @@ namespace mbts.spark {
_wrap_comparer(r => r.status)));
// last message
Add(last_message_column = new OrderBookLastMessageColumn(
new RegexFilterVM<OrderRecordTuple>(
new RegexFilterVM<OrderSecurity>(
ort => ort.order.last_msg(),
nameof(OrderBookRowVM.last_msg))));
// security key
@@ -266,7 +266,7 @@ namespace mbts.spark {
nameof(OrderBookRowVM.security_key),
r => r.security_key.ToString(),
DefaultColumnWidths.SecurityKey,
new RegexFilterVM<OrderRecordTuple>(
new RegexFilterVM<OrderSecurity>(
ort => ort.order.security_key().ToString(),
nameof(OrderBookRowVM.security_key)),
DataGridFilterKnownTemplates.Regex,
@@ -278,7 +278,7 @@ namespace mbts.spark {
nameof(OrderBookRowVM.order_key),
r => r.order_key.ToString(),
DefaultColumnWidths.OrderKey,
new RegexFilterVM<OrderRecordTuple>(
new RegexFilterVM<OrderSecurity>(
ort => ort.order.Key.ToString(),
nameof(OrderBookRowVM.order_key)),
DataGridFilterKnownTemplates.Regex,
@@ -290,7 +290,7 @@ namespace mbts.spark {
nameof(OrderBookRowVM.parent_order_key),
r => r.parent_order_key.ToString(),
DefaultColumnWidths.ParentKey,
new RegexFilterVM<OrderRecordTuple>(
new RegexFilterVM<OrderSecurity>(
ort => ort.order.ParentKey.ToString(),
nameof(OrderBookRowVM.parent_order_key)),
DataGridFilterKnownTemplates.Regex,
@@ -389,9 +389,9 @@ namespace mbts.spark {
}
}
public Func<OrderRecordTuple, bool> get_filter() {
public Func<OrderSecurity, bool> get_filter() {
var active_filters = this.OfType<ICVOrderBookColumn>().Where(c => c.filter.active.GetValueOrDefault()).Select(c => c.filter).ToArray();
return active_filters.Length == 0 ? null : new Func<OrderRecordTuple, bool>(r => active_filters.All(f => f.filter(r)));
return active_filters.Length == 0 ? null : new Func<OrderSecurity, bool>(r => active_filters.All(f => f.filter(r)));
}
public OrderBookSortingData get_sorting_data() {
@@ -481,7 +481,7 @@ namespace mbts.spark {
GlyphContext _root_glyph_context, _child_glyph_context;
protected OrderbookColumnBase(Columns col_key, string header, Func<OrderBookRowVM, string> get_text, string sort_prop, double width,
IDataGridColumnPersistentFilter<OrderRecordTuple, T> filter, string filter_template, IComparer<OrderBookRowVM> comparer) : base(col_key) {
IDataGridColumnPersistentFilter<OrderSecurity, T> filter, string filter_template, IComparer<OrderBookRowVM> comparer) : base(col_key) {
id = key.ToString();
Width = width;
Header = header;
@@ -499,10 +499,10 @@ namespace mbts.spark {
}
}
public IDataGridColumnPersistentFilter<OrderRecordTuple, T> filter { get; }
public IDataGridColumnPersistentFilter<OrderSecurity, T> filter { get; }
public IComparer<OrderBookRowVM> comparer { get; }
IDataGridColumnFilter<OrderRecordTuple> ICVOrderBookColumn.filter => filter;
IDataGridColumnFilter<OrderSecurity> ICVOrderBookColumn.filter => filter;
IDataGridColumnFilter IFilterableColumn.filter => filter;
string IFilterableColumn.filter_template => _filter_template;
ListSortDirection? ICVOrderBookColumn.SortDirection { get => _cached_sort_direction; set => SortDirection = value; }
@@ -516,7 +516,7 @@ namespace mbts.spark {
public class SimpleTextColumn<TFilterState> : OrderbookColumnBase<TFilterState>, IRenderColumn<OrderBookRowVM> {
public SimpleTextColumn(Columns key, string header, string sort_prop, Func<OrderBookRowVM, string> get_text, double width,
IDataGridColumnPersistentFilter<OrderRecordTuple, TFilterState> filter, string filter_template, IComparer<OrderBookRowVM> comparer)
IDataGridColumnPersistentFilter<OrderSecurity, TFilterState> filter, string filter_template, IComparer<OrderBookRowVM> comparer)
: base(key, header, get_text, sort_prop, width, filter, filter_template, comparer) { }
public void draw(OrderBookRowVM row, DrawingContext dc, ColumnGeometry position, double row_height) =>
@@ -526,7 +526,7 @@ namespace mbts.spark {
#region Explicit Column Defs
public class SideColumn : OrderbookColumnBase<Side[]>, IRenderColumn<OrderBookRowVM> {
public SideColumn(IDataGridColumnPersistentFilter<OrderRecordTuple, Side[]> filter)
public SideColumn(IDataGridColumnPersistentFilter<OrderSecurity, Side[]> filter)
: base(Columns.Side, "B/S", r => r.side.ToString(), nameof(OrderBookRowVM.side), DefaultColumnWidths.Side, filter, DataGridFilterKnownTemplates.EnumValues, _wrap_comparer(r => r.side)) { }
public void draw(OrderBookRowVM row, DrawingContext dc, ColumnGeometry position, double row_height) =>
@@ -535,7 +535,7 @@ namespace mbts.spark {
}
public class SizeColumn : OrderbookColumnBase<UIntMinMaxState>, IRenderColumn<OrderBookRowVM> {
public SizeColumn(IDataGridColumnPersistentFilter<OrderRecordTuple, UIntMinMaxState> filter)
public SizeColumn(IDataGridColumnPersistentFilter<OrderSecurity, UIntMinMaxState> filter)
: base(Columns.Size, "Qty", r => r.quantity.ToString(), nameof(OrderBookRowVM.quantity), DefaultColumnWidths.Size, filter, DataGridFilterKnownTemplates.UIntMinMax, _wrap_comparer(r => r.quantity)) {
sort_member_path = nameof(OrderBookRowVM.quantity);
}
@@ -546,7 +546,7 @@ namespace mbts.spark {
}
public class FilledColumn : OrderbookColumnBase<UIntMinMaxState>, IRenderColumn<OrderBookRowVM> {
public FilledColumn(IDataGridColumnPersistentFilter<OrderRecordTuple, UIntMinMaxState> filter)
public FilledColumn(IDataGridColumnPersistentFilter<OrderSecurity, UIntMinMaxState> filter)
: base(Columns.Filled, "Filled Qty", r => r.filled_quantity.ToString(), nameof(OrderBookRowVM.filled_quantity), DefaultColumnWidths.Size, filter, DataGridFilterKnownTemplates.UIntMinMax, _wrap_comparer(r => r.filled_quantity)) {
sort_member_path = nameof(OrderBookRowVM.filled_quantity);
}
@@ -558,8 +558,8 @@ namespace mbts.spark {
public class OrderBookOrderTypeColumn : OrderTypeColumn<Columns>, ICVOrderBookColumn {
ListSortDirection? _cached_sort_direction;
public OrderBookOrderTypeColumn(IDataGridColumnPersistentFilter<OrderRecordTuple, OrderType[]> filter)
: base(Columns.OrderType, new Binding(nameof(OrderBookRowVM.order_type)), new Binding(nameof(OrderBookRowVM.order_metadata)), filter) {
public OrderBookOrderTypeColumn(IDataGridColumnPersistentFilter<OrderSecurity, OrderType[]> filter)
: base(Columns.OrderType, new Binding(nameof(OrderBookRowVM.order_type)), new Binding(nameof(OrderBookRowVM.order_metadata)), new Binding(nameof(OrderBookRowVM.dynamic_order_type_display_name)), filter) {
id = key.ToString();
this.filter = filter;
sort_member_path = nameof(OrderBookRowVM.order_type);
@@ -567,10 +567,10 @@ namespace mbts.spark {
_cached_sort_direction = SortDirection;
}
public new IDataGridColumnPersistentFilter<OrderRecordTuple, OrderType[]> filter { get; }
public new IDataGridColumnPersistentFilter<OrderSecurity, OrderType[]> filter { get; }
public IComparer<OrderBookRowVM> comparer => _wrap_comparer(r => r.order_type);
IDataGridColumnFilter<OrderRecordTuple> ICVOrderBookColumn.filter => filter;
IDataGridColumnFilter<OrderSecurity> ICVOrderBookColumn.filter => filter;
ListSortDirection? ICVOrderBookColumn.SortDirection { get => _cached_sort_direction; set => SortDirection = value; }
protected override void OnSortDirectionChanged(ListSortDirection? new_value) {
@@ -582,7 +582,7 @@ namespace mbts.spark {
public class OrderBookLastMessageColumn : TextColumn<Columns>, ICVOrderBookColumn {
ListSortDirection? _cached_sort_direction;
public OrderBookLastMessageColumn(IDataGridColumnPersistentFilter<OrderRecordTuple, string> filter)
public OrderBookLastMessageColumn(IDataGridColumnPersistentFilter<OrderSecurity, string> filter)
: base(Columns.LastMessage, new Binding(nameof(OrderBookRowVM.last_msg)), filter, DataGridFilterKnownTemplates.Regex) {
id = key.ToString();
this.filter = filter;
@@ -592,10 +592,10 @@ namespace mbts.spark {
tooltip_binding = new Binding(nameof(OrderBookRowVM.last_msg));
}
public new IDataGridColumnPersistentFilter<OrderRecordTuple, string> filter { get; }
public new IDataGridColumnPersistentFilter<OrderSecurity, string> filter { get; }
public IComparer<OrderBookRowVM> comparer => _wrap_comparer(r => r.last_msg);
IDataGridColumnFilter<OrderRecordTuple> ICVOrderBookColumn.filter => filter;
IDataGridColumnFilter<OrderSecurity> ICVOrderBookColumn.filter => filter;
ListSortDirection? ICVOrderBookColumn.SortDirection { get => _cached_sort_direction; set => SortDirection = value; }
protected override void OnSortDirectionChanged(ListSortDirection? new_value) {
@@ -611,7 +611,7 @@ namespace mbts.spark {
ListSortDirection? _cached_sort_direction;
public SecurityColumn(IDataGridColumnPersistentFilter<OrderRecordTuple, SecurityCriteria> filter) : base(Columns.Security) {
public SecurityColumn(IDataGridColumnPersistentFilter<OrderSecurity, SecurityCriteria> filter) : base(Columns.Security) {
id = key.ToString();
this.filter = filter;
Header = DefaultColumnHeaders.Security;
@@ -620,10 +620,10 @@ namespace mbts.spark {
_cached_sort_direction = SortDirection;
}
public IDataGridColumnPersistentFilter<OrderRecordTuple, SecurityCriteria> filter { get; }
public IDataGridColumnPersistentFilter<OrderSecurity, SecurityCriteria> filter { get; }
public IComparer<OrderBookRowVM> comparer => _wrap_comparer(r => r.security, SecurityHelper.Comparer.InstanceImmutable.to_nullable());
IDataGridColumnFilter<OrderRecordTuple> ICVOrderBookColumn.filter => filter;
IDataGridColumnFilter<OrderSecurity> ICVOrderBookColumn.filter => filter;
IDataGridColumnFilter IFilterableColumn.filter => filter;
string IFilterableColumn.filter_template => DataGridFilterKnownTemplates.DiscreteValues;
ListSortDirection? ICVOrderBookColumn.SortDirection { get => _cached_sort_direction; set => SortDirection = value; }
@@ -651,7 +651,6 @@ namespace mbts.spark {
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
grid.Children.Add(toggle_button);
grid.Children.Add(security_view);
grid.HorizontalAlignment = HorizontalAlignment.Center;
grid.VerticalAlignment = VerticalAlignment.Center;
//
var border = new Border { Child = grid };
@@ -1,5 +1,7 @@
using bts;
using bts.api;
using bts.api.core;
using bts.api.orderman;
using bts.collections;
using bts.utils;
using mbts.utils;
@@ -13,26 +15,26 @@ using System.Reactive.Linq;
namespace mbts.spark {
class OrderBookRowCollection : ObservableCollection<OrderBookRowVM> {
static readonly IEqualityComparer<INode<OrderBookRowVM>> _node_equality_comparer = EqualityComparer.Create<INode<OrderBookRowVM>, uint>(n => n.value.order_key);
readonly Func<OrderRecordTuple, OrderBookRowVM> _row_factory;
readonly Func<OrderSecurity, OrderBookRowVM> _row_factory;
/// Critical: Any and all row mutations <see cref="OrderBookRowVM"/> must be executed through <see cref="SortedTree{T, TKey}.TryReplace(TKey, Func{T, T})"/>.
/// Mutations can change the sort-position of a row, and <see cref="_tree"/> will be broken if its elements aren't properly sorted (it relies heavily on binary-search logic).
SortedTree<OrderBookRowVM, uint> _tree;
IComparer<OrderBookRowVM> _comparer;
Func<OrderRecordTuple, bool> _filter;
Func<OrderSecurity, bool> _filter;
bool _is_refresh_pending;
int _deferment_count = 0;
/// When processing changed-items, the <see cref="SortedTree{T, TKey}.TryReplace(TKey, Func{T, T})"/> method is used to update the changed row. It is not expected to fail (return false),
/// but if it does, we don't want to overwhelm the logs with a bunch of useless noise in case it starts happening repeatedly, hence the int fields below.
/// <see cref="_max_replace_misses_logged"/>, <see cref="_n_replace_misses_logged"/>, <see cref="update(IIncrementalTableUpdateTransform{OrderRecordTuple, uint})"/>.
/// <see cref="_max_replace_misses_logged"/>, <see cref="_n_replace_misses_logged"/>, <see cref="update(IIncrementalTableUpdateTransform{OrderSecurity, uint})"/>.
const int _max_replace_misses_logged = 10;
int _n_replace_misses_logged;
public OrderBookRowCollection(Func<OrderRecordTuple, OrderBookRowVM> row_factory, IComparer<OrderBookRowVM> comparer) {
public OrderBookRowCollection(Func<OrderSecurity, OrderBookRowVM> row_factory, IComparer<OrderBookRowVM> comparer) {
_row_factory = row_factory;
_tree = _mk_tree(_comparer = comparer);
}
public Func<OrderRecordTuple, bool> filter {
public Func<OrderSecurity, bool> filter {
get => _filter;
set {
_filter = value;
@@ -144,24 +146,24 @@ namespace mbts.spark {
});
}
public void update(IIncrementalTableUpdateTransform<OrderRecordTuple, uint> table) {
public void update(TableUpdate<(SecurityImmutable? sec,OrderRecordImmutable order),uint> table) {
IEnumerable<INode<OrderBookRowVM>> impacted_branch_roots;
int n_removed = table.removed.Count();
if (n_removed == total_row_count) {
// full refresh (most likely this is the first update or a server restart)
_tree = _mk_tree(_comparer, table.added.Select(_row_factory));
_tree = _mk_tree(_comparer, table.added.Select(x => _row_factory(x)));
impacted_branch_roots = _tree.RootNodes;
} else {
foreach (OrderRecordTuple ort in table.removed)
foreach (OrderSecurity ort in table.removed)
_tree.Remove(ort.order.Key);
foreach (Change<OrderRecordTuple> change in table.changes) {
foreach (var change in table.changes) {
if (!_tree.TryReplace(change.New.order.Key, r => { r.update(change.New); return r; })) {
// uh oh, replace failed! Let's log this, but not a million times (this seems like the kind of error that, if it happens once, could happen a lot).
if (_n_replace_misses_logged++ < _max_replace_misses_logged)
Log.Ui.error($"{nameof(OrderBookRowCollection)} failed to process Changed {nameof(OrderRecordTuple)} in update (old:{change.Old.key}, new:{change.New.key}). This is the '{_n_replace_misses_logged}' time this has happened in this OrderBook.");
Log.Ui.error($"{nameof(OrderBookRowCollection)} failed to process Changed {nameof(OrderSecurity)} in update (old:{change.Old.order.Key}, new:{change.New.order.Key}). This is the '{_n_replace_misses_logged}' time this has happened in this OrderBook.");
}
}
foreach (OrderRecordTuple ort in table.added)
foreach (OrderSecurity ort in table.added)
_tree.Insert(_row_factory(ort));
impacted_branch_roots =
table.added.Concat(table.changed).Select(ort => ort.order.Key).Select(_tree.GetRootNode)
@@ -19,14 +19,15 @@ namespace mbts.spark {
IRaiseCanExecuteChangedCommand _show_order_modify_cmd, _toggle_expanded_cmd, _cancel_cmd, _modify_order_quantity, _modify_order_price;
bool _is_parent, _is_expanded, _is_selected;
int _depth_level;
SecurityMD? _security_md => order_tuple.security_md;
SecurityImmutable? _security => order_tuple.security;
// for mini-ob
uint _prospective_quantity;
double _prospective_price;
public OrderBookRowVM(OrderBookVM owner, OrderRecordTuple order_tuple, bool is_expanded) {
public OrderBookRowVM(OrderBookVM owner, OrderSecurity order_tuple, bool is_expanded) {
_owner = owner;
this.order_tuple = order_tuple;
is_visible = !is_child;
@@ -49,19 +50,20 @@ namespace mbts.spark {
public event Action invalidated;
// Core
public OrderRecordTuple order_tuple { get; private set; }
public OrderSecurity order_tuple { get; private set; }
public OrderRecordImmutable order => order_tuple.order;
public OrderMetadata order_metadata => _owner.get_metadata(this);
public uint order_key => order.Key;
public uint parent_order_key => order.ParentKey;
public uint security_key => order.security_key();
public SecurityImmutable? security => _security_md.security();
public SecurityImmutable? security => _security;
public ContractSpecificationImmutable? contract_spec => security.spec_or_default();
public string security_text => security.display_name();
public Venue exchange => _security_md.exchange();
public string symbol => _security_md.symbol();
public Venue exchange => _security.exchange_or_default();
public string symbol => _security.symbol_or_default();
// Def
public CustomOrderDefinition.CustomOneofCase order_type => order.order_type();
public string dynamic_order_type_display_name => order.dynamic_order_type_display_name();
public OrderSubType order_sub_type => order.sub_type();
public string order_sub_type_text => _fmt_order_sub_type(order_sub_type);
public Side side => order.side();
@@ -130,7 +132,10 @@ namespace mbts.spark {
public ICommand modify_order_quantity => _modify_order_quantity ??
(_modify_order_quantity = new DelegateCommand(
(desired_quantity) => _owner.modify_order_quantity(this, (uint)desired_quantity),
(desired_quantity) => {
_owner.modify_order_quantity(this, (uint)desired_quantity);
_unselect_me();
},
(desired_quantity) =>
(desired_quantity != null && this.status == OrderStatus.Working && (uint)desired_quantity != this.quantity)
|| is_qty_dirty
@@ -139,21 +144,29 @@ namespace mbts.spark {
public ICommand modify_order_price => _modify_order_price ??
(_modify_order_price = new DelegateCommand(
(desired_price) => _owner.modify_order_price(this, (double)desired_price),
(desired_price) => {
_owner.modify_order_price(this, (double)desired_price);
_unselect_me();
},
(desired_price) =>
(desired_price != null && this.status == OrderStatus.Working && (double)desired_price != this.price)
|| is_price_dirty
)
);
);
private void _unselect_me() {
this.is_selected = false;
_owner.current_row = null;
}
// These fns present a way for the orderbook to avoid noisy, intermittent collection changes during "Expand/Collapse All" ops,
// when incremental changes are ignored in favor of a full reset, after 'is_expanded' is updated for all rows.
internal void expand_internal() => Set(ref _is_expanded, true);
internal void collapse_internal() => Set(ref _is_expanded, false);
public void update(OrderRecordTuple order_tuple) {
public void update(OrderSecurity order_tuple) {
Log.assert(order_tuple.order.Key == this.order.Key, "OrderBookRowVM: update_order was called with an order with the wrong key.");
bool had_security = _security_md.HasValue;
bool had_security = _security.HasValue;
// _show_order_modify_cmd is lazy (instantiated in the default_command getter); it might be null, don't not force instantiation
bool? could_modify_execute = _show_order_modify_cmd?.CanExecute(null);
OrderRecordImmutable old_order = order;
@@ -178,7 +191,7 @@ namespace mbts.spark {
RaisePropertyChanged(nameof(price));
RaisePropertyChanged(nameof(is_price_dirty));
}
if (!had_security && _security_md.HasValue) {
if (!had_security && _security.HasValue) {
RaisePropertyChanged(nameof(security), nameof(security_text));
// need to raise invalidated to refresh the price cells (the security.ContractSpec determines how they are formatted - Ticks!)
invalidate = true;
@@ -3,7 +3,6 @@ using bts.api;
using bts.api.orderman;
using bts.shell;
using bts.spark;
using bts.utils;
using mbts.persist;
using mbts.ui;
using mbts.ui.controls;
@@ -21,9 +20,15 @@ using System.Windows.Controls;
using System.Windows.Input;
using static bts.shell.OrderBookWindowFactory;
using static mbts.spark.OrderBookView;
using _GridSettingsManager = mbts.ui.GridSettingsManager<mbts.spark.OrderBookRowVM, mbts.spark.OrderRecordTuple, bts.shell.INotificationServiceDataCache, mbts.OrderRecordTuplePredicate, bts.shell.OrderBookWindowFactory.OrderBookGridSettings>;
using _GridSettingsManager =
mbts.ui.GridSettingsManager<
mbts.spark.OrderBookRowVM,
bts.api.orderman.OrderSecurity,
bts.shell.INotificationServiceDataCache,
mbts.OrderRecordTuplePredicate,
bts.shell.OrderBookWindowFactory.OrderBookGridSettings>;
namespace mbts.spark {
namespace mbts.spark {
partial class OrderBookVM : ViewModelBase, IDisposable, IHandleRowSelection, IHandleGridSorting, IWillLetYouKnowWhenIDispose {
readonly IBus _bus;
readonly OrdersModel _model;
@@ -40,7 +45,7 @@ namespace mbts.spark {
_bus = bus;
_model = model;
_settings_manager = settings_manager;
_columns = new OrderBookColumnCollection(bus, model, model.on_order_securities.Select(u => (u.added.Values.Select(smd => smd.security), u.removed.Values.Select(smd => smd.security))));
_columns = new OrderBookColumnCollection(bus, model, model.on_order_securities.Select(x => (x.added.Values, x.removed.Values)));
// we use an ObservableAppendCollection b/c the mini-ob needs a cancel column at the front
columns = new ObservableAppendCollection<DIYGrid.ColumnBase>(_columns);
_row_collection = new OrderBookRowCollection(ort => new OrderBookRowVM(this, ort, rows_are_initially_expanded), _columns.get_sorting_data().comparer) { filter = _columns.get_filter() };
@@ -51,7 +56,7 @@ namespace mbts.spark {
expand_all_rows_command = new DelegateCommand(_row_collection.expand_all);
grid_settings_manager = _create_grid_settings_manager(owner_title, bus, notification_service, model, rows, core_columns);
_resources = new List<IDisposable> {
model.on_order_tuples_table.OnDispatcher().Subscribe(_row_collection.update),
model.on_orders.OnDispatcher().Subscribe(_row_collection.update),
grid_settings_manager,
Disposable.Create(() => {
_columns.filter_changed -= on_filter_changed;
@@ -76,7 +81,19 @@ namespace mbts.spark {
public ObservableAppendCollection<DIYGrid.ColumnBase> columns { get; }
public ObservableCollection<DIYGrid.ColumnBase> core_columns => _columns;
public ObservableCollection<OrderBookRowVM> rows => _row_collection;
public OrderBookRowVM current_row { get => _current_row.Value; set => _current_row.Value = value ?? _selected_rows.FirstOrDefault(); }
public OrderBookRowVM current_row {
get => _current_row.Value;
set {
_current_row.Value = value;
// this is so that when the current_row is set to null from
// the row_vm, we propagate the change up to the OrderBookWindow
// so that the cancel and modify commands can_execute is updated.
// Note that this is really only for if/when the edit_xxx columns
// from the mini-ob are present in regular orderbook, else this is
// a no-op.
selected_order_changed?.Invoke(current_order);
}
}
public OrderRecordImmutable? current_order => current_row?.order;
public IEnumerable<OrderRecordImmutable> selected_orders => _selected_rows.Select(r => r.order);
public _GridSettingsManager grid_settings_manager { get; }
@@ -213,13 +230,13 @@ namespace mbts.spark {
return new _GridSettingsManager(
owner_title, bus, notification_service, model, rows, columns,
() => {
var pvm = PredicateBuilderVMWrapper.create<OrderRecordTuple, OrderRecordTuplePredicate, OrderRecordTuplePredicate.PredicateTypeOneofCase>(
var pvm = PredicateBuilderVMWrapper.create<OrderSecurity, OrderRecordTuplePredicate, OrderRecordTuplePredicate.PredicateTypeOneofCase>(
OrderRecordTuplePredicateVMFactory.get_oneofs(bus, model), rp => rp.predicate_type_case);
pvm.selected_predicate_type = pvm.predicate_types.FirstOrDefault();
return pvm;
},
col_id => {
var pvm = PredicateBuilderVMWrapper.create<OrderRecordTuple, OrderRecordTuplePredicate, OrderRecordTuplePredicate.PredicateTypeOneofCase>(
var pvm = PredicateBuilderVMWrapper.create<OrderSecurity, OrderRecordTuplePredicate, OrderRecordTuplePredicate.PredicateTypeOneofCase>(
OrderRecordTuplePredicateVMFactory.get_oneofs(bus, model), rp => rp.predicate_type_case);
pvm.selected_predicate_type = _corresponding_predicate_type(EnumHelper.parse_or_default<Columns>(col_id));
return pvm;
@@ -16,7 +16,7 @@ using System.Windows.Data;
using System.Windows.Media;
//using IOrderBookWSM = mbts.persist.IWindowSettingsManager<mbts.spark.GridSettingsWithNotifications<mbts.spark.OrderRecordTuple, mbts.OrderRecordTuplePredicate>>;
using OrderBookGridSettings = bts.shell.OrderBookWindowFactory.OrderBookGridSettings;
using OrderBookGridSettingsVM = mbts.spark.GridSettingsVM<mbts.spark.OrderRecordTuple, mbts.OrderRecordTuplePredicate, bts.shell.OrderBookWindowFactory.OrderBookGridSettings>;
using OrderBookGridSettingsVM = mbts.spark.GridSettingsVM<bts.api.orderman.OrderSecurity, mbts.OrderRecordTuplePredicate, bts.shell.OrderBookWindowFactory.OrderBookGridSettings>;
namespace mbts.spark {
internal class OrderBookWindow : SparkWindow<OrderBookWindowFactory.OrderBookStartupData>, ISharedWindow {
@@ -51,7 +51,7 @@ namespace mbts.spark {
_settings_manager = settings_manager;
}
public override OrderBookWindowFactory.OrderBookStartupData SaveState => _state = _vm.create();
public override OrderBookWindowFactory.OrderBookStartupData SaveState => _state = _vm?.create();
public override string window_type => _window_type;
public override UIElement Content => _view;
@@ -86,12 +86,12 @@ namespace mbts.spark {
);
_cancel_buy_orders_command = SparkCommands.CancelBuyOrders.complete(
_model.working_orders.Select(os => os.Values.Where(o => o.side() == Side.Buy).Select(o => o.Key)),
_model.working_orders.Select(os => os.Values.Where(o => o.order.side() == Side.Buy).Select(o => o.order.Key)),
wbids => wbids.Any()
);
_cancel_sell_orders_command = SparkCommands.CancelSellOrders.complete(
_model.working_orders.Select(os => os.Values.Where(o => o.side() == Side.Sell).Select(o => o.Key)),
_model.working_orders.Select(os => os.Values.Where(o => o.order.side() == Side.Sell).Select(o => o.order.Key)),
wasks => wasks.Any()
);
@@ -167,11 +167,9 @@ namespace mbts.spark {
}
private void _vm_selected_order_changed(OrderRecordImmutable? e) {
if (e.HasValue) {
_modify_order_command.raiseCanExecuteChanged();
_cancel_selected_orders_command.raiseCanExecuteChanged();
_recreate_selected_orders_command.raiseCanExecuteChanged();
}
}
}
}
@@ -112,7 +112,7 @@ namespace bts.shell {
public static GridColumnSettings last_price => new GridColumnSettings(Columns.LastPrice.ToString(), "Last Filled", width: DIYGrid.DefaultColumnWidths.LastPrice, horizontal_alignment: HorizontalAlignment.Right);
public static GridColumnSettings status => new GridColumnSettings(Columns.Status.ToString(), "Status", width: 80);
public static GridColumnSettings last_message => new GridColumnSettings(Columns.LastMessage.ToString(), "Last Message", width: DIYGrid.DefaultColumnWidths.LastMessage);
public static GridColumnSettings security_key => new GridColumnSettings(Columns.SecurityKey.ToString(), DIYGrid.DefaultColumnHeaders.SecurityKey, width: DIYGrid.DefaultColumnWidths.SecurityKey);
public static GridColumnSettings security_key => new GridColumnSettings(Columns.SecurityKey.ToString(), DIYGrid.DefaultColumnHeaders.SecurityKey, is_visible: false, width: DIYGrid.DefaultColumnWidths.SecurityKey);
public static GridColumnSettings order_key => new GridColumnSettings(Columns.OrderKey.ToString(), DIYGrid.DefaultColumnHeaders.OrderKey, width: DIYGrid.DefaultColumnWidths.OrderKey);
public static GridColumnSettings parent_key => new GridColumnSettings(Columns.ParentKey.ToString(), "Parent Key", width: DIYGrid.DefaultColumnWidths.ParentKey);
@@ -233,7 +233,7 @@ namespace bts.shell {
}
[ProtoContract]
public class OrderBookGridSettings : IGridSettingsWithNotifications<OrderRecordTuple, OrderRecordTuplePredicate>, IEquatable<OrderBookGridSettings> {
public class OrderBookGridSettings : IGridSettingsWithNotifications<OrderSecurity, OrderRecordTuplePredicate>, IEquatable<OrderBookGridSettings> {
[ProtoMember(1)]
public GridSettings general { get; set; }
[ProtoMember(2)]
@@ -248,7 +248,7 @@ namespace bts.shell {
}
public IReadOnlyList<OrderRecordTuplePredicateAnimations> predicate_animations => _predicate_animations ?? (_predicate_animations = new List<OrderRecordTuplePredicateAnimations>());
IEnumerable<IPredicateAnimationsData<OrderRecordTuple, OrderRecordTuplePredicate>> IGridSettingsWithNotifications<OrderRecordTuple, OrderRecordTuplePredicate>.predicate_animations => predicate_animations;
IEnumerable<IPredicateAnimationsData<OrderSecurity, OrderRecordTuplePredicate>> IGridSettingsWithNotifications<OrderSecurity, OrderRecordTuplePredicate>.predicate_animations => predicate_animations;
public OrderBookGridSettings with(GridSettings general) =>
new OrderBookGridSettings(general ?? this.general, predicate_animations);
@@ -1,4 +1,6 @@
using bts.api;
using bts.api.core;
using bts.api.orderman;
using bts.spark;
using mbts;
using mbts.collections;
@@ -9,6 +11,8 @@ using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Reactive.Linq;
using OrderTuple = System.ValueTuple<bts.api.SecurityImmutable?, bts.api.orderman.OrderRecordImmutable>;
namespace bts.shell {
interface INotificationService {
@@ -16,13 +20,13 @@ namespace bts.shell {
INotificationServiceDataCache data_cache { get; }
IDisposable link_to_activity(IPredicate<Activity> predicate, SoundNotification sound);
IDisposable link_to_activity(INotificationPredicate<Activity> toast_notification_predicate);
IDisposable link_to_order(IPredicate<OrderRecordTuple> predicate, SoundNotification sound);
IDisposable link_to_order(INotificationPredicate<OrderRecordTuple> toast_notification_predicate);
IDisposable link_to_order(IPredicate<OrderTuple> predicate, SoundNotification sound);
IDisposable link_to_order(INotificationPredicate<OrderTuple> toast_notification_predicate);
}
static class NotificationServiceExtensions {
public static IDisposable link<T>(this INotificationService notification_service, IPredicate<T> predicate, SoundNotification sound) {
if (predicate is IPredicate<OrderRecordTuple> orp)
if (predicate is IPredicate<OrderTuple> orp)
return notification_service.link_to_order(orp, sound);
if (predicate is IPredicate<Activity> ap)
return notification_service.link_to_activity(ap, sound);
@@ -30,7 +34,7 @@ namespace bts.shell {
}
public static IDisposable link<T>(this INotificationService notification_service, INotificationPredicate<T> toast_notification_predicate) {
if (toast_notification_predicate is INotificationPredicate<OrderRecordTuple> orp)
if (toast_notification_predicate is INotificationPredicate<OrderTuple> orp)
return notification_service.link_to_order(orp);
if (toast_notification_predicate is INotificationPredicate<Activity> ap)
return notification_service.link_to_activity(ap);
@@ -41,7 +45,7 @@ namespace bts.shell {
[Export(typeof(INotificationService))]
partial class NotificationService : INotificationService, IDisposable {
readonly NotificationProcessor<Activity> _activity_notifications;
readonly NotificationProcessor<OrderRecordTuple> _order_notifications;
readonly NotificationProcessor<OrderTuple> _order_notifications;
readonly DataCache _data_cache = new DataCache();
IDisposable _dispose;
@@ -50,10 +54,10 @@ namespace bts.shell {
public NotificationService(IBus bus, INotifier notifier, OrdersModel orders_model) {
this.notifier = notifier;
_activity_notifications = new NotificationProcessor<Activity>(notifier);
_order_notifications = new NotificationProcessor<OrderRecordTuple>(notifier);
_order_notifications = new NotificationProcessor<OrderTuple>(notifier);
_dispose = Disposable.Create(
bus.subscribe(SparkTags.LogActivity).Subscribe(_on_activity),
orders_model.on_order_tuples.Subscribe(_on_orders)
orders_model.on_orders.Subscribe(_on_orders)
);
}
@@ -63,17 +67,17 @@ namespace bts.shell {
public IDisposable link_to_activity(IPredicate<Activity> predicate, SoundNotification sound) => _activity_notifications.link(predicate, sound);
public IDisposable link_to_activity(INotificationPredicate<Activity> toast_notification_predicate) => _activity_notifications.link(toast_notification_predicate);
public IDisposable link_to_order(IPredicate<OrderRecordTuple> predicate, SoundNotification sound) => _order_notifications.link(predicate, sound);
public IDisposable link_to_order(INotificationPredicate<OrderRecordTuple> toast_notification_predicate) => _order_notifications.link(toast_notification_predicate);
public IDisposable link_to_order(IPredicate<OrderTuple> predicate, SoundNotification sound) => _order_notifications.link(predicate, sound);
public IDisposable link_to_order(INotificationPredicate<OrderTuple> toast_notification_predicate) => _order_notifications.link(toast_notification_predicate);
public void Dispose() => Disposable.dispose(ref _dispose);
void _on_activity(Activity activity) =>
_activity_notifications.process_item(activity);
void _on_orders(IIncrementalUpdate<OrderRecordTuple, uint> update) {
void _on_orders(TableUpdate<(SecurityImmutable?,OrderRecordImmutable),uint> update) {
// must update notification_service_data_cache first, some of the predicate/triggers (esp. ToastNotifications) will use the notification_service_data_cache to lookup order-history
_data_cache.update(update);
foreach (var o in update.added.Values)
_data_cache.update(update.as_iincremental());
foreach (var o in update.added)
_order_notifications.process_item(o);
}
@@ -1,4 +1,6 @@
using mbts.spark;
using bts.api;
using bts.api.orderman;
using mbts.spark;
using mbts.utils;
using System;
using System.Collections.Generic;
@@ -13,15 +15,21 @@ namespace bts.shell {
interface INotificationServiceDataCache {
DateTime last_order_reset_time { get; }
bool is_first_order_update { get; }
IReadOnlyDictionary<uint, List<TimeStampedData<OrderRecordTuple>>> order_history { get; }
IReadOnlyDictionary<uint, List<TimeStampedData<OrderSecurity>>> order_history { get; }
}
static class NotificationServiceDataCacheExtensions {
public static OrderRecordTuple get_previous_order_state(this INotificationServiceDataCache notification_service_data_cache, uint key) =>
notification_service_data_cache.order_history.TryGetValue(key, out List<TimeStampedData<OrderRecordTuple>> l) && l.Count > 1 ? l[l.Count - 2].data : null;
public static OrderSecurity? get_previous_order_state(this INotificationServiceDataCache notification_service_data_cache, uint key) {
if (notification_service_data_cache.order_history.TryGetValue(key, out List<TimeStampedData<OrderSecurity>> l)) {
if (l.Count > 1) {
return l[l.Count - 2].data;
}
}
return null;
}
}
class TimeStampedData<T> {
struct TimeStampedData<T> {
public readonly T data;
public readonly DateTime time_stamp;
@@ -34,14 +42,15 @@ namespace bts.shell {
partial class NotificationService : INotificationService, IDisposable {
class DataCache : INotificationServiceDataCache {
const int _buffer_size = 10;
readonly Dictionary<uint, OrderRecordTuple> _order_cache = new Dictionary<uint, OrderRecordTuple>();
readonly Dictionary<uint, List<TimeStampedData<OrderRecordTuple>>> _order_history = new Dictionary<uint, List<TimeStampedData<OrderRecordTuple>>>();
readonly Dictionary<uint, OrderSecurity> _order_cache = new Dictionary<uint, OrderSecurity>();
readonly Dictionary<uint, List<TimeStampedData<OrderSecurity>>> _order_history = new Dictionary<uint, List<TimeStampedData<OrderSecurity>>>();
public IReadOnlyDictionary<uint, List<TimeStampedData<OrderSecurity>>> order_history => _order_history;
public IReadOnlyDictionary<uint, List<TimeStampedData<OrderRecordTuple>>> order_history => _order_history;
public DateTime last_order_reset_time { get; private set; }
public bool is_first_order_update { get; private set; }
public void update(IIncrementalUpdate<OrderRecordTuple, uint> data) {
internal void update(IIncrementalUpdate<(SecurityImmutable? sec, OrderRecordImmutable order), uint> data) {
DateTime now = DateTime.Now;
if (data.removed.Count == _order_cache.Count) {
last_order_reset_time = now;
@@ -51,7 +60,7 @@ namespace bts.shell {
foreach (var kv in data.removed) {
if (data.added.ContainsKey(kv.Key)) {
_order_cache[kv.Key] = kv.Value;
_order_history[kv.Key].Add(new TimeStampedData<OrderRecordTuple>(kv.Value, now));
_order_history[kv.Key].Add(new TimeStampedData<OrderSecurity>(kv.Value, now));
} else {
_order_cache.Remove(kv.Key);
// TODO: maybe these shouldn't be removed?
@@ -65,9 +74,9 @@ namespace bts.shell {
continue;
_order_cache.Add(kv.Key, kv.Value);
_order_history.AddOrUpdate(kv.Key,
k => new List<TimeStampedData<OrderRecordTuple>> { new TimeStampedData<OrderRecordTuple>(kv.Value, now) },
k => new List<TimeStampedData<OrderSecurity>> { new TimeStampedData<OrderSecurity>(kv.Value, now) },
(k, l) => {
l.Add(new TimeStampedData<OrderRecordTuple>(kv.Value, now));
l.Add(new TimeStampedData<OrderSecurity>(kv.Value, now));
if (l.Count >= 10)
l.RemoveAt(0);
});
@@ -60,14 +60,14 @@ namespace bts.shell {
public static IToastNotification order_filled(OrderRecordImmutable oldval, OrderRecordImmutable newval, SecurityImmutable? security) =>
new OrderFilledToastNotification(oldval, newval, security);
public static IToastNotification order_filled(OrderRecordTuple oldval, OrderRecordTuple newval) =>
new OrderFilledToastNotification(oldval.order, newval.order, newval.security_md?.security);
public static IToastNotification order_filled(OrderSecurity oldval, OrderSecurity newval) =>
new OrderFilledToastNotification(oldval.order, newval.order, newval.security);
public static IToastNotification order_completed(OrderRecordImmutable order, SecurityImmutable? security) =>
new OrderCompletedToastNotification(order, security);
public static IToastNotification order_completed(OrderRecordTuple order) =>
new OrderCompletedToastNotification(order.order, order.security_md?.security);
public static IToastNotification order_completed(OrderSecurity order) =>
new OrderCompletedToastNotification(order.order, order.security);
public static IToastNotification order_status_changed(uint key, OrderStatus? old_status, OrderStatus new_status, SecurityImmutable? security) =>
new OrderStatusChangeToastNotification(key, old_status, new_status, security);
@@ -19,7 +19,7 @@ namespace mbts.spark {
readonly Property<CustomOrderDefinition.CustomOneofCase> _current_order_type;
readonly Property<bool> _price_dirty, _size_dirty, _account_dirty, _time_in_force_dirty, _order_type_dirty;
public ExistingOrderTicketVM(OrdersModel model, IObservable<OrderRecordTuple> order_source, IObservable<int[]> calculator_buttons) {
public ExistingOrderTicketVM(OrdersModel model, IObservable<(SecurityMD? security_md, OrderRecordImmutable order)> order_source, IObservable<int[]> calculator_buttons) {
uint? key = null;
var security_md_source = order_source.Where(ot => ot.security_md.HasValue).Select(ot => ot.security_md.Value);
//
+25
View File
@@ -0,0 +1,25 @@
using System;
namespace bts.api.orderman {
public readonly struct FillSecurity : IEquatable<FillSecurity> {
public FillSecurity(SecurityImmutable? sec, FillImmutable fill) {
this.sec = sec;
this.fill = fill;
}
public FillSecurity((SecurityImmutable? sec, FillImmutable fill) t) {
sec = t.sec;
fill = t.fill;
}
public static implicit operator FillSecurity(ValueTuple<SecurityImmutable?, FillImmutable> x) => new FillSecurity(x);
public SecurityImmutable? sec { get; }
public FillImmutable fill { get; }
public override bool Equals(object obj) => (obj is FillSecurity os) && Equals(os);
public bool Equals(FillSecurity other) => fill.FillKey == other.fill.FillKey && sec == other.sec;
public override int GetHashCode() => (sec, fill).GetHashCode();
public static bool operator ==(FillSecurity left, FillSecurity right) => left.Equals(right);
public static bool operator !=(FillSecurity left, FillSecurity right) => !left.Equals(right);
}
}
+5 -5
View File
@@ -10,14 +10,14 @@ namespace mbts.spark {
class FillTuple : IEquatable<FillTuple> {
public readonly FillImmutable fill;
public readonly OrderRecordImmutable? order;
public readonly SecurityMD? security_md;
public readonly SecurityImmutable? security;
public readonly FillTupleKey key;
public FillTuple(FillImmutable fill, OrderRecordImmutable? order, SecurityMD? security_md) {
public FillTuple(FillImmutable fill, OrderRecordImmutable? order, SecurityImmutable? security) {
this.fill = fill;
this.order = order;
this.security_md = security_md;
key = new FillTupleKey(fill.FillKey, order.HasValue ? order.Value.Key : 0u, security_md.HasValue ? security_md.Value.security.Id : 0u);
this.security = security;
key = new FillTupleKey(fill.FillKey, order.HasValue ? order.Value.Key : 0u, security?.Id ?? 0);
}
public string account {
@@ -31,7 +31,7 @@ namespace mbts.spark {
public bool Equals(FillTuple other) => other != null
&& other.order.HasValue == order.HasValue
&& other.security_md.HasValue == security_md.HasValue
&& other.security.HasValue == security.HasValue
&& other.fill.Equals(fill);
public override bool Equals(object obj) => Equals(obj as FillTuple);
+28 -71
View File
@@ -1,4 +1,5 @@
using bts.api.core;
using bts.api;
using bts.api.core;
using bts.api.orderman;
using mbts.collections;
using mbts.utils;
@@ -14,15 +15,14 @@ namespace mbts.spark {
// returns a ref-count Observable.Create-wrapper that initializes/destroys the [hot-observable] FillTupleSource.
// (initializes when the first subscription is made, and destroys when the last subscription is disposed).
public static IObservable<IIncrementalUpdate<FillTuple, FillTupleKey>> create_observable(
IObservable<IReadOnlyDictionary<uint, SecurityMD>> securities,
IObservable<TableUpdate<OrderRecordImmutable, uint>> orders,
IObservable<TableUpdate<FillImmutable, string>> fills) {
IObservable<TableUpdate<(SecurityImmutable?,OrderRecordImmutable), uint>> orders,
IObservable<TableUpdate<(SecurityImmutable?,FillImmutable), string>> fills) {
FillTupleSource source = null;
int ref_count = 0;
return Observable.Create<IIncrementalUpdate<FillTuple, FillTupleKey>>(observer => {
if (Interlocked.Increment(ref ref_count) == 1) {
SparkUtils.lazy_assert(source == null, () => fmt_assert_msg("source should be null, there must be a leak."));
source = new FillTupleSource(securities, orders, fills);
source = new FillTupleSource(orders, fills);
}
IDisposable sub = source.Subscribe(observer);
return Disposable.Create(() => {
@@ -38,19 +38,16 @@ namespace mbts.spark {
readonly Dictionary<FillTupleKey, FillTuple> _state = new Dictionary<FillTupleKey, FillTuple>();
readonly SetMap<uint, string> _order_fills = new SetMap<uint, string>();
readonly SetMap<uint, string> _security_fills = new SetMap<uint, string>();
IReadOnlyDictionary<string, FillImmutable> _fills_cache = new Dictionary<string, FillImmutable>(0);
IReadOnlyDictionary<uint, OrderRecordImmutable> _orders_cache = new Dictionary<uint, OrderRecordImmutable>(0);
IReadOnlyDictionary<uint, SecurityMD> _security_md_cache = new Dictionary<uint, SecurityMD>(0);
IReadOnlyDictionary<string, (SecurityImmutable? sec, FillImmutable fill)> _fills_cache = new Dictionary<string, (SecurityImmutable?, FillImmutable)>();
IReadOnlyDictionary<uint, (SecurityImmutable?,OrderRecordImmutable)> _orders_cache = new Dictionary<uint, (SecurityImmutable?,OrderRecordImmutable)>();
IDisposable _dispose;
object _lock = new object();
FillTupleSource(
IObservable<IReadOnlyDictionary<uint, SecurityMD>> securities,
IObservable<TableUpdate<OrderRecordImmutable, uint>> orders,
IObservable<TableUpdate<FillImmutable, string>> fills) =>
IObservable<TableUpdate<(SecurityImmutable?,OrderRecordImmutable), uint>> orders,
IObservable<TableUpdate<(SecurityImmutable?,FillImmutable), string>> fills) =>
//
_dispose = Disposable.Create(
securities.Subscribe(_update_securities),
orders.Subscribe(_update_orders),
fills.AddRemoveOnly().Subscribe(_update_fills));
@@ -63,19 +60,19 @@ namespace mbts.spark {
public void Dispose() => Disposable.dispose(ref _dispose);
void _update_fills(LegacyTableUpdate<FillImmutable, string> fills) {
void _update_fills(LegacyTableUpdate<(SecurityImmutable?,FillImmutable), string> fills) {
lock (_lock) {
var removed = new Dictionary<FillTupleKey, FillTuple>();
var added = new Dictionary<FillTupleKey, FillTuple>();
foreach (FillImmutable fill in fills.removed) {
FillTuple old_ft = new FillTuple(fill, _get_order(fill.OrderKey), _get_security_md(fill.SecurityKey));
foreach (var (security,fill) in fills.removed) {
FillTuple old_ft = new FillTuple(fill, _get_order(fill.OrderKey), security);
removed.Add(old_ft.key, old_ft);
_security_fills.remove(fill.SecurityKey, fill.FillKey);
_order_fills.remove(fill.OrderKey, fill.FillKey);
}
foreach (FillImmutable fill in fills.added) {
FillTuple new_ft = new FillTuple(fill, _get_order(fill.OrderKey), _get_security_md(fill.SecurityKey));
foreach (var (security,fill) in fills.added) {
FillTuple new_ft = new FillTuple(fill, _get_order(fill.OrderKey), security);
added.Add(new_ft.key, new_ft);
_security_fills.add(fill.SecurityKey, fill.FillKey);
_order_fills.add(fill.OrderKey, fill.FillKey);
@@ -85,35 +82,33 @@ namespace mbts.spark {
}
}
void _update_orders(TableUpdate<OrderRecordImmutable, uint> orders) {
void _update_orders(TableUpdate<(SecurityImmutable?,OrderRecordImmutable order), uint> orders) {
lock (_lock) {
Dictionary<FillTupleKey, FillTuple> added = new Dictionary<FillTupleKey, FillTuple>(), removed = new Dictionary<FillTupleKey, FillTuple>();
HashSet<uint> changed_keys = new HashSet<uint>();
foreach (var o in orders.removed) {
foreach (var (_,o) /*winking owl*/ in orders.removed) {
if (_order_fills.try_lookup(o.Key, out ISet<string> fill_keys)) {
OrderRecordImmutable? curr_order = null;
if (orders.try_get(o.Key, out OrderRecordImmutable newval)) {
curr_order = newval;
if (orders.try_get(o.Key, out var newval)) {
curr_order = newval.order;
changed_keys.Add(o.Key);
}
IEnumerable<FillImmutable> fills = fill_keys.Select(fk => _fills_cache[fk]);
foreach (FillImmutable fill in fills) {
SecurityMD? sec_md = _get_security_md(fill.SecurityKey);
FillTuple old_ft = new FillTuple(fill, o, sec_md);
var fills = fill_keys.Select(fk => _fills_cache[fk]);
foreach (var (security, fill) in fills) {
FillTuple old_ft = new FillTuple(fill, o, security);
removed.Add(old_ft.key, old_ft);
FillTuple new_ft = new FillTuple(fill, curr_order, sec_md);
FillTuple new_ft = new FillTuple(fill, curr_order, security);
added.Add(new_ft.key, new_ft);
}
}
}
foreach (var o in orders.added) {
foreach (var (_,o) in orders.added) {
if (!changed_keys.Contains(o.Key) && _order_fills.try_lookup(o.Key, out ISet<string> fill_keys)) {
IEnumerable<FillImmutable> fills = fill_keys.Select(fk => _fills_cache[fk]);
foreach (FillImmutable fill in fills) {
SecurityMD? sec_md = _get_security_md(fill.SecurityKey);
FillTuple old_ft = new FillTuple(fill, null, sec_md);
var fills = fill_keys.Select(fk => _fills_cache[fk]);
foreach (var (security,fill) in fills) {
FillTuple old_ft = new FillTuple(fill, null, security);
removed.Add(old_ft.key, old_ft);
FillTuple new_ft = new FillTuple(fill, o, sec_md);
FillTuple new_ft = new FillTuple(fill, o, security);
added.Add(new_ft.key, new_ft);
}
}
@@ -123,46 +118,8 @@ namespace mbts.spark {
}
}
void _update_securities(IReadOnlyDictionary<uint, SecurityMD> securities) {
lock (_lock) {
Dictionary<FillTupleKey, FillTuple> added = new Dictionary<FillTupleKey, FillTuple>(), removed = new Dictionary<FillTupleKey, FillTuple>();
_security_md_cache.Keys.difference(securities.Keys, out IEnumerable<uint> added_keys, out IEnumerable<uint> removed_keys);
foreach (var k in removed_keys) {
if (_security_fills.try_lookup(k, out ISet<string> fill_keys)) {
IEnumerable<FillImmutable> fills = fill_keys.Select(fk => _fills_cache[fk]);
SecurityMD old_sec_md = _security_md_cache[k];
foreach (FillImmutable fill in fills) {
OrderRecordImmutable? order = _get_order(fill.OrderKey);
FillTuple old_ft = new FillTuple(fill, order, old_sec_md);
removed.Add(old_ft.key, old_ft);
FillTuple new_ft = new FillTuple(fill, order, null);
added.Add(new_ft.key, new_ft);
}
}
}
foreach (var k in added_keys) {
if (_security_fills.try_lookup(k, out ISet<string> fill_keys)) {
IEnumerable<FillImmutable> fills = fill_keys.Select(fk => _fills_cache[fk]);
SecurityMD new_sec_md = securities[k];
foreach (FillImmutable fill in fills) {
OrderRecordImmutable? order = _get_order(fill.OrderKey);
FillTuple old_ft = new FillTuple(fill, order, null);
removed.Add(old_ft.key, old_ft);
FillTuple new_ft = new FillTuple(fill, order, new_sec_md);
added.Add(new_ft.key, new_ft);
}
}
}
_security_md_cache = securities;
_apply_changes(added, removed);
}
}
SecurityMD? _get_security_md(uint key) =>
_security_md_cache.TryGetValue(key, out SecurityMD smd) ? smd : new SecurityMD?();
OrderRecordImmutable? _get_order(uint key) =>
_orders_cache.TryGetValue(key, out OrderRecordImmutable o) ? o : new OrderRecordImmutable?();
_orders_cache.TryGetValue(key, out var o) ? o.Item2 : new OrderRecordImmutable?();
void _apply_changes(Dictionary<FillTupleKey, FillTuple> added, Dictionary<FillTupleKey, FillTuple> removed) {
foreach (var kv in removed)
@@ -11,6 +11,7 @@ using System.Linq;
using System.Reactive.Linq;
using System.Text;
using System.Threading.Tasks;
using OrderPair = System.ValueTuple<bts.api.SecurityImmutable?, bts.api.orderman.OrderRecordImmutable>;
namespace bts.spark {
internal class Activity {
@@ -26,7 +27,7 @@ namespace bts.spark {
static readonly IEqualityComparer<OrderRecordImmutable> _id_comparer =
bts.utils.EqualityComparer.Create<OrderRecordImmutable, uint>(a => a.Key);
public OrderActivitySummarizer(IBus bus, IObservable<TableUpdate<OrderRecordImmutable, uint>> orders) {
public OrderActivitySummarizer(IBus bus, IObservable<TableUpdate<(SecurityImmutable? sec,OrderRecordImmutable order), uint>> orders) {
_bus = bus;
_dispose = Disposable.Create(
bus.subscribe(SparkTags.SecuritiesTag).Subscribe(sec => _security = sec),
@@ -38,11 +39,11 @@ namespace bts.spark {
#region formatting
(SecurityImmutable?, ContractSpecificationImmutable?) get_security(uint security_key) {
SecurityImmutable? try_get_security(uint security_key) {
if (_security.TryGetValue(security_key, out SecurityMD md)) {
return (md.security, md.security.ContractSpec);
return md.security;
}
return (null, null);
return null;
}
public static string format_order_success(uint key, double price, uint size, Side side, ContractSpecificationImmutable? spec) {
return $"({key}) {side.ToString().ToLower()} {size} @ {spec.format(price)}";
@@ -123,24 +124,23 @@ namespace bts.spark {
#endregion
void orders_updated(TableUpdate<OrderRecordImmutable, uint> update) {
void orders_updated(TableUpdate<(SecurityImmutable? sec, OrderRecordImmutable order), uint> update) {
foreach (var order in update.added) {
if (!string.IsNullOrEmpty(order.State.LastMsg) && order.State.Created.ToDateTime().Date == DateTime.Today) {
(var security, var spec) = get_security(order.Def.SecurityKey);
if (security.HasValue) {
log_status(ActivityLogCategory.warn, security, order.State.LastMsg);
foreach (var o in update.added) {
if (!string.IsNullOrEmpty(o.order.State.LastMsg) && o.order.State.Created.ToDateTime().Date == DateTime.Today) {
if (o.sec.HasValue) {
log_status(ActivityLogCategory.warn, o.sec.Value, o.order.State.LastMsg);
}
}
}
foreach (var order in update.changes) {
var neworder = order.New;
var oldorder = order.Old;
(var security, var spec) = get_security(neworder.Def.SecurityKey);
var neworder = order.New.order;
var oldorder = order.Old.order;
var security = order.New.sec ?? order.Old.sec;
//
// description
var mod_desc = modified_description(oldorder, neworder, spec);
var mod_desc = modified_description(oldorder, neworder, security?.ContractSpec);
if (!string.IsNullOrEmpty(mod_desc)) {
log_status(ActivityLogCategory.modify, security, mod_desc);
}
@@ -171,28 +171,30 @@ namespace bts.spark {
}
internal void on_send_order(OrderDefinitionImmutable order, double price, uint size, uint? key, string error) {
(var security, var spec) = get_security(order.SecurityKey);
var security = try_get_security(order.SecurityKey);
log_status(ActivityLogCategory.order, security, key.HasValue ?
format_order_success(key.Value, price, size, order.Side, spec) :
format_order_fail(price, size, order.Side, spec, error));
format_order_success(key.Value, price, size, order.Side, security?.ContractSpec) :
format_order_fail(price, size, order.Side, security?.ContractSpec, error));
}
internal void on_cancel_order(OrderRecordImmutable order, bool success, string error) {
(var security, var spec) = get_security(order.Def.SecurityKey);
var security = try_get_security(order.Def.SecurityKey);
log_status(ActivityLogCategory.cancel, security, success ?
format_cancel_success(order, spec):
format_cancel_error(order, spec, error));
format_cancel_success(order, security?.ContractSpec):
format_cancel_error(order, security?.ContractSpec, error));
}
internal void update_order(OrderRecordImmutable order, double price, uint size) {
(var security, var spec) = get_security(order.Def.SecurityKey);
var security = try_get_security(order.Def.SecurityKey);
var spec = security?.ContractSpec;
log_status(ActivityLogCategory.modify,
security,
$"({order.Key}) change from {order.State.Quantity}@{spec.format(order.State.Price)} to {size}@{spec.format(price)}");
}
internal void update_order_failed(OrderRecordImmutable order, double price, uint size) {
(var security, var spec) = get_security(order.Def.SecurityKey);
var security = try_get_security(order.Def.SecurityKey);
var spec = security?.ContractSpec;
log_status(ActivityLogCategory.modify,
security,
$"({order.Key}) failed to change from {order.State.Quantity}@{spec.format(order.State.Price)} to {size}@{spec.format(price)}");
@@ -25,6 +25,8 @@ namespace mbts.ui.spark {
public DataTemplate JoinOrderTemplate { get; set; }
public DataTemplate TopOrderTemplate { get; set; }
public DataTemplate CoverOrderTemplate { get; set; }
public DataTemplate OCOOrderTemplate { get; set; }
public DataTemplate PricerOrderTemplate { get; set; }
public DataTemplate OffsetOrderTemplate { get; set; }
public DataTemplate TriggerOrderTemplate { get; set; }
public DataTemplate IcebergOrderTemplate { get; set; }
@@ -43,6 +45,8 @@ namespace mbts.ui.spark {
case CustomOrderDefinition.CustomOneofCase.Join: return JoinOrderTemplate;
case CustomOrderDefinition.CustomOneofCase.Top: return TopOrderTemplate;
case CustomOrderDefinition.CustomOneofCase.Cover: return CoverOrderTemplate;
case CustomOrderDefinition.CustomOneofCase.Oco : return OCOOrderTemplate;
case CustomOrderDefinition.CustomOneofCase.Pricer: return PricerOrderTemplate;
case CustomOrderDefinition.CustomOneofCase.Offset: return OffsetOrderTemplate;
case CustomOrderDefinition.CustomOneofCase.Trigger: return TriggerOrderTemplate;
case CustomOrderDefinition.CustomOneofCase.Iceberg: return IcebergOrderTemplate;
@@ -1,47 +0,0 @@
using bts.api.orderman;
using bts.utils;
using mbts.utils;
using System;
namespace mbts.spark {
struct OrderRecordTupleKey : IEquatable<OrderRecordTupleKey> {
public readonly uint order_key, security_key;
public OrderRecordTupleKey(uint order_key, uint security_key) {
this.order_key = order_key;
this.security_key = security_key;
}
public bool Equals(OrderRecordTupleKey other) =>
other.order_key == order_key &&
other.security_key == security_key;
public override bool Equals(object obj) =>
obj is OrderRecordTupleKey k && Equals(k);
public override int GetHashCode() =>
EqualityComparer.aggregate_hash(
(int)order_key,
(int)security_key);
}
class OrderRecordTuple : IEquatable<OrderRecordTuple> {
public readonly OrderRecordImmutable order;
public readonly SecurityMD? security_md;
public readonly OrderRecordTupleKey key;
public OrderRecordTuple(OrderRecordImmutable order, SecurityMD? security_md) {
this.order = order;
this.security_md = security_md;
key = new OrderRecordTupleKey(order.Key, security_md.HasValue ? security_md.Value.security.Id : 0u);
}
public bool Equals(OrderRecordTuple other) => other != null
&& other.order.Equals(order)
&& other.security_md.HasValue == security_md.HasValue;
public override bool Equals(object obj) => Equals(obj as OrderRecordTuple);
public override int GetHashCode() => key.GetHashCode();
public override string ToString() => $"order:{order.Key}, sec:{security_md.display_name()}";
}
}
@@ -1,147 +0,0 @@
using bts.api.core;
using bts.api.orderman;
using mbts.collections;
using mbts.utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Linq;
using System.Reactive.Subjects;
namespace mbts.spark {
class OrderRecordTupleSource : IDisposable {
readonly object _lock = new object();
// subjects
readonly Subject<IIncrementalUpdate<OrderRecordTuple, uint>> _subject = new Subject<IIncrementalUpdate<OrderRecordTuple, uint>>();
readonly Subject<IIncrementalTableUpdateTransform<OrderRecordTuple, uint>> _table_subject = new Subject<IIncrementalTableUpdateTransform<OrderRecordTuple, uint>>();
readonly Subject<IIncrementalUpdate<SecurityMD, uint>> _order_securities_subject = new Subject<IIncrementalUpdate<SecurityMD, uint>>();
// data cache
readonly Dictionary<uint, OrderRecordTuple> _state = new Dictionary<uint, OrderRecordTuple>();
readonly SetMap<uint, uint> _security_orders = new SetMap<uint, uint>();
readonly IDictionary<uint, SecurityMD> _security_md_cache = new Dictionary<uint, SecurityMD>(0);
IReadOnlyDictionary<uint, OrderRecordImmutable> _orders_cache = new Dictionary<uint, OrderRecordImmutable>(0);
IDisposable _dispose;
public OrderRecordTupleSource(IObservable<IReadOnlyDictionary<uint, SecurityMD>> securities, IObservable<TableUpdate<OrderRecordImmutable, uint>> orders) =>
_dispose = Disposable.Create(
securities.Subscribe(_update_securities),
orders.Subscribe(_update_orders));
public IObservable<IIncrementalUpdate<OrderRecordTuple, uint>> subscribe_incremental_update() =>
Observable.Create<IIncrementalUpdate<OrderRecordTuple, uint>>(observer => {
lock (_lock) {
observer.OnNext(new IncrementalUpdate<OrderRecordTuple, uint>(new Dictionary<uint, OrderRecordTuple>(_state), new Dictionary<uint, OrderRecordTuple>(0)));
return _subject.Subscribe(observer);
}
});
public IObservable<IIncrementalTableUpdateTransform<OrderRecordTuple, uint>> subscribe_incremental_table_update() =>
Observable.Create<IIncrementalTableUpdateTransform<OrderRecordTuple, uint>>(observer => {
lock (_lock) {
observer.OnNext(IncrementalTableUpdateTranform<OrderRecordTuple, uint>.init(_state.Values));
return _table_subject.Subscribe(observer);
}
});
public IObservable<IIncrementalUpdate<SecurityMD, uint>> subscribe_order_securities() =>
Observable.Create<IIncrementalUpdate<SecurityMD, uint>>(observer => {
lock (_lock) {
var init = new Dictionary<uint, SecurityMD>();
foreach (uint sec_key in _security_orders.Keys) {
if (_security_md_cache.TryGetValue(sec_key, out SecurityMD smd))
init.Add(sec_key, smd);
}
observer.OnNext(new IncrementalUpdate<SecurityMD, uint>(init, new Dictionary<uint, SecurityMD>(0)));
return _order_securities_subject.Subscribe(observer);
}
});
public void Dispose() => Disposable.dispose(ref _dispose);
void _update_orders(TableUpdate<OrderRecordImmutable, uint> orders) {
lock (_lock) {
Dictionary<uint, OrderRecordTuple> removed = new Dictionary<uint, OrderRecordTuple>(), added = new Dictionary<uint, OrderRecordTuple>();
Dictionary<uint, SecurityMD> added_securities = new Dictionary<uint, SecurityMD>(), removed_securities = new Dictionary<uint, SecurityMD>();
foreach (OrderRecordImmutable order in orders.removed) {
uint sec_key = order.security_key();
OrderRecordTuple old_ft = new OrderRecordTuple(order, _get_security_md(sec_key));
removed.Add(old_ft.order.Key, old_ft);
if (_security_orders.removes_key(sec_key, order.Key) && old_ft.security_md.HasValue)
removed_securities.Add(sec_key, old_ft.security_md.Value);
}
foreach (Change<OrderRecordImmutable> ch in orders.changes) {
// orders cannot change securities; ch.New can use the same security_md as ch.Old
var sec_md = _get_security_md(ch.Old.security_key());
removed.Add(ch.Old.Key, new OrderRecordTuple(ch.Old, sec_md));
added.Add(ch.New.Key, new OrderRecordTuple(ch.New, sec_md));
}
foreach (OrderRecordImmutable order in orders.added) {
uint sec_key = order.security_key();
OrderRecordTuple new_ort = new OrderRecordTuple(order, _get_security_md(sec_key));
added.Add(new_ort.order.Key, new_ort);
if (_security_orders.adds_key(sec_key, order.Key) && new_ort.security_md.HasValue)
added_securities.Add(sec_key, new_ort.security_md.Value);
}
_orders_cache = orders.state;
_apply_changes(added, removed, added_securities, removed_securities);
}
}
void _update_securities(IReadOnlyDictionary<uint, SecurityMD> securities) {
lock (_lock) {
Dictionary<uint, OrderRecordTuple> added = new Dictionary<uint, OrderRecordTuple>(), removed = new Dictionary<uint, OrderRecordTuple>();
Dictionary<uint, SecurityMD> added_securities = new Dictionary<uint, SecurityMD>(), removed_securities = new Dictionary<uint, SecurityMD>();
_security_md_cache.Keys.difference(securities.Keys, out IEnumerable<uint> added_keys, out IEnumerable<uint> removed_keys);
/// Warning: <see cref="_security_md_cache"/> must NOT be reassigned with <see cref="securities"/>; the latter is always the same object.
/// This would mean that the <see cref="GlobalExtensions.difference{T}(IEnumerable{T}, IEnumerable{T}, out IEnumerable{T}, out IEnumerable{T})"/>
/// call above would literally be calculating the difference between the keys of the same dictionary!
foreach (var k in removed_keys.ToList()) {
if (_security_orders.try_lookup(k, out ISet<uint> order_keys)) {
SecurityMD old_sec_md = _security_md_cache[k];
removed_securities.Add(k, old_sec_md);
IEnumerable<OrderRecordImmutable> orders = order_keys.Select(fk => _orders_cache[fk]);
foreach (OrderRecordImmutable order in orders) {
OrderRecordTuple old_ft = new OrderRecordTuple(order, old_sec_md);
removed.Add(old_ft.order.Key, old_ft);
OrderRecordTuple new_ft = new OrderRecordTuple(order, null);
added.Add(new_ft.order.Key, new_ft);
}
}
_security_md_cache.Remove(k);
}
foreach (var k in added_keys.ToList()) {
SecurityMD new_sec_md = securities[k];
if (_security_orders.try_lookup(k, out ISet<uint> order_keys)) {
added_securities.Add(k, new_sec_md);
IEnumerable<OrderRecordImmutable> orders = order_keys.Select(fk => _orders_cache[fk]);
foreach (OrderRecordImmutable order in orders) {
OrderRecordTuple old_ft = new OrderRecordTuple(order, null);
removed.Add(old_ft.order.Key, old_ft);
OrderRecordTuple new_ft = new OrderRecordTuple(order, new_sec_md);
added.Add(new_ft.order.Key, new_ft);
}
}
_security_md_cache.Add(k, new_sec_md);
}
_apply_changes(added, removed, added_securities, removed_securities);
}
}
SecurityMD? _get_security_md(uint key) =>
_security_md_cache.TryGetValue(key, out SecurityMD smd) ? smd : new SecurityMD?();
OrderRecordImmutable? _get_order(uint key) =>
_orders_cache.TryGetValue(key, out OrderRecordImmutable o) ? o : new OrderRecordImmutable?();
void _apply_changes(Dictionary<uint, OrderRecordTuple> added, Dictionary<uint, OrderRecordTuple> removed, Dictionary<uint, SecurityMD> added_securities, Dictionary<uint, SecurityMD> removed_securities) {
foreach (var kv in removed)
_state.Remove(kv.Key);
foreach (var kv in added)
_state.Add(kv.Key, kv.Value);
var update = new IncrementalUpdate<OrderRecordTuple, uint>(added, removed);
_subject.OnNext(update);
_table_subject.OnNext(update.as_table());
_order_securities_subject.OnNext(new IncrementalUpdate<SecurityMD, uint>(added_securities, removed_securities));
}
}
}
+25
View File
@@ -0,0 +1,25 @@
using System;
namespace bts.api.orderman {
public readonly struct OrderSecurity: IEquatable<OrderSecurity> {
public OrderSecurity(SecurityImmutable? sec, OrderRecordImmutable order) {
this.security = sec;
this.order = order;
}
public OrderSecurity((SecurityImmutable? sec, OrderRecordImmutable order) t) {
security = t.sec;
order = t.order;
}
public static implicit operator OrderSecurity(ValueTuple<SecurityImmutable?, OrderRecordImmutable> x) => new OrderSecurity(x);
public SecurityImmutable? security { get; }
public OrderRecordImmutable order { get; }
public override bool Equals(object obj) => (obj is OrderSecurity os) && Equals(os);
public bool Equals(OrderSecurity other) => order.Equals(other.order) && security == other.security;
public override int GetHashCode() => (security, order).GetHashCode();
public static bool operator == (OrderSecurity left, OrderSecurity right) => left.Equals(right);
public static bool operator != (OrderSecurity left, OrderSecurity right) => !left.Equals(right);
}
}
@@ -126,6 +126,8 @@ namespace mbts.spark
return _child_order_choices;
}
}
public bool has_child => _order_def_tags.Any();
public IOrderTypeVM child_order
{
@@ -0,0 +1,257 @@
using bts.api;
using bts.api.orderman;
using mbts.ui;
using mbts.ui.utility;
using mbts.utils;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Reactive.Linq;
using static bts.api.orderman.CustomOrderDefinition;
namespace mbts.spark
{
static partial class OrderTypeVMFactory
{
class InlineOCOOrderProperties
{
public InlineOCOOrderProperties(OrderTemplateHeader selected_child_order_type, IReadOnlyCollection<OrderTemplateHeader> child_order_types)
{
ExceptionAssert.InvalidOperation.That(child_order_types.Contains(selected_child_order_type), $"Child order type {selected_child_order_type} is not in child_order_types");
this.selected_child_order_type = new InlineUserProperty<OrderTemplateHeader>(selected_child_order_type, true);
this.child_order_types = new InlineUserProperty<IReadOnlyCollection<OrderTemplateHeader>>(child_order_types, true);
var othertype = CustomOneofCase.Limit;
this.selected_other_order_type = new InlineUserProperty<OrderTemplateHeader>(new OrderTemplateHeader(othertype), true);
this.other_order_types = new InlineUserProperty<IReadOnlyCollection<OrderTemplateHeader>>(child_order_types, true);
other_ticks = new InlineUserProperty<int>(1, false);
other_side = new InlineUserProperty<bts.api.RelativeSide>(bts.api.RelativeSide.Same, false);
}
public readonly InlineUserProperty<OrderTemplateHeader> selected_child_order_type;
public readonly InlineUserProperty<IReadOnlyCollection<OrderTemplateHeader>> child_order_types;
public readonly InlineUserProperty<OrderTemplateHeader> selected_other_order_type;
public readonly InlineUserProperty<IReadOnlyCollection<OrderTemplateHeader>> other_order_types;
public readonly InlineUserProperty<int> other_ticks;
public readonly InlineUserProperty<bts.api.RelativeSide> other_side;
}
class OCOOrderVM : OrderTypeVMBase<OCOOrderDefinition>, IHierarchicalOrderTypeVM, IDisposable
{
readonly IObservable<SecurityMD> _security_md_source;
SecurityImmutable? _security;
ObservableCollection<OrderTemplateHeader> _order_types = new ObservableCollection<OrderTemplateHeader>();
ObservableAutoWrapper<IOrderTypeVM, OrderTemplateHeader> _child_order_choices;
InlineOCOOrderProperties _inline_props;
IOrderTypeVM _child_order;
IDisposable _dispose, _child_order_errors_sub;
ObservableAutoWrapper<IOrderTypeVM, OrderTemplateHeader> _other_order_choices;
IOrderTypeVM _other_order;
IDisposable _other_order_errors_sub;
public int other_ticks { get; set; } = 1;
public bts.api.RelativeSide other_side { get; set; } = bts.api.RelativeSide.Same;
public OCOOrderVM(OrdersModel model, IObservable<SecurityMD> security_md_source, string id, string display_name)
: this(model, security_md_source, security_md_source.Select(smd =>
new InlineOCOOrderProperties(new OrderTemplateHeader(model.factory.default_order_type(smd.security)), model.factory.default_order_types(smd.security).Select(t => new OrderTemplateHeader(t)).ToList())), id, display_name)
{ }
public OCOOrderVM(OrdersModel model, IObservable<SecurityMD> security_md_source,
IObservable<InlineOCOOrderProperties> inline_properties, string id, string display_name) : base(model, id, display_name)
{
_security_md_source = security_md_source;
_dispose = Disposable.Create(
security_md_source.OnDispatcher()
.Subscribe(smd => _security = smd.security),
inline_properties.OnDispatcher()
.Subscribe(p =>
{
if (_order_types.Count == 0 || p.child_order_types.force_override)
{
_order_types.SetItems(p.child_order_types.value);
}
if (_child_order_choices != null && (_child_order == null || p.selected_child_order_type.force_override))
{
_select_child_order(p.selected_child_order_type.value.key.type);
}
if (_other_order_choices != null && (_other_order == null || p.selected_other_order_type.force_override))
{
_select_other_order(p.selected_other_order_type.value.key.type);
}
if (p.other_ticks.force_override)
{
other_ticks = p.other_ticks.value;
RaisePropertyChanged(nameof(other_ticks));
}
if (p.other_side.force_override)
{
other_side = p.other_side.value;
RaisePropertyChanged(nameof(other_side));
}
_inline_props = p;
_validate();
})
);
_validate();
}
public override void assign_to(CustomOrderDefinition value)
{
if (_child_order == null)
_select_child_order(model.factory.default_order_type(_security.Value, true));
if (_other_order == null )
_select_other_order(model.factory.default_order_type(_security.Value, true));
_validate();
if (!HasErrors)
{
var i = factory.oco_order();
i.OtherTicks = other_ticks;
i.OtherSide = other_side;
child_order.assign_to(i.Child);
if (other_order != null)
other_order.assign_to(i.Other);
value.Oco = i;
}
}
public IEnumerable<bts.api.RelativeSide> other_side_types => EnumHelper.get_values_except<bts.api.RelativeSide>(bts.api.RelativeSide.Undefined);
public ICollection<IOrderTypeVM> child_order_types
{
get
{
if (_child_order_choices == null)
_child_order_choices = new ObservableAutoWrapper<IOrderTypeVM, OrderTemplateHeader>(_order_types, ot => create(ot.key.type, ot.key.name, ot.display_name, model, _security_md_source));
return _child_order_choices;
}
}
public ICollection<IOrderTypeVM> other_order_types
{
get
{
if (_other_order_choices == null)
_other_order_choices = new ObservableAutoWrapper<IOrderTypeVM, OrderTemplateHeader>(_order_types, ot => create(ot.key.type, ot.key.name, ot.display_name, model, _security_md_source));
return _other_order_choices;
}
}
public IOrderTypeVM child_order
{
get
{
init();
return _child_order;
}
set
{
Disposable.dispose(ref _child_order_errors_sub);
_child_order = value;
_child_order?.init();
if (_child_order is INotifyDataErrorInfo ndei)
{
ndei.ErrorsChanged += on_errors_changed;
void on_errors_changed(object s, DataErrorsChangedEventArgs e) =>
_validate();
_child_order_errors_sub = Disposable.Create(() => ndei.ErrorsChanged -= on_errors_changed);
}
_validate();
RaisePropertyChanged(nameof(child_order));
}
}
public IOrderTypeVM other_order
{
get
{
init();
return _other_order;
}
set
{
Disposable.dispose(ref _other_order_errors_sub);
_other_order = value;
_other_order?.init();
if (_other_order is INotifyDataErrorInfo ndei)
{
ndei.ErrorsChanged += on_errors_changed;
void on_errors_changed(object s, DataErrorsChangedEventArgs e) =>
_validate();
_other_order_errors_sub = Disposable.Create(() => ndei.ErrorsChanged -= on_errors_changed);
}
_validate();
RaisePropertyChanged(nameof(other_order));
}
}
public void Dispose()
{
Disposable.dispose(ref _dispose);
if (_child_order_choices != null)
_child_order_choices.OfType<IDisposable>().DisposeAll();
if (_other_order_choices != null)
_other_order_choices.OfType<IDisposable>().DisposeAll();
}
public override void init()
{
if (_child_order == null && _security.HasValue)
_select_child_order();
if (_other_order == null && _security.HasValue)
_select_other_order();
_validate();
}
void _select_child_order()
{
CustomOneofCase ot = _inline_props != null && _inline_props.selected_child_order_type.has_value ?
_inline_props.selected_child_order_type.value.key.type :
model.factory.default_order_type(_security.Value, true);
_select_child_order(ot);
}
void _select_child_order(CustomOneofCase ot)
{
_child_order = child_order_types.FirstOrDefault(vm => vm.order_type == ot);
if (_child_order == null)
_child_order = child_order_types.FirstOrDefault();
RaisePropertyChanged(nameof(child_order));
}
void _select_other_order()
{
CustomOneofCase ot = _inline_props != null && _inline_props.selected_other_order_type.has_value ?
_inline_props.selected_other_order_type.value.key.type :
model.factory.default_order_type(_security.Value, true);
_select_other_order(ot);
}
void _select_other_order(CustomOneofCase ot)
{
_other_order = other_order_types.FirstOrDefault(vm => vm.order_type == ot);
if (_other_order == null)
_other_order = other_order_types.FirstOrDefault();
RaisePropertyChanged(nameof(other_order));
}
void _validate()
{
if (_child_order == null || (_child_order is INotifyDataErrorInfo ndei && ndei.HasErrors))
set_error(nameof(child_order), string.Empty, true);
else clear_error(nameof(child_order));
if ((_other_order == null ) || (_other_order is INotifyDataErrorInfo hdei && hdei.HasErrors))
set_error(nameof(other_order), string.Empty, true);
else clear_error(nameof(other_order));
}
}
}
}
@@ -15,11 +15,12 @@ namespace mbts.spark {
public static IOrderTypeVM stop(OrdersModel model, string id, string display_name) => new StopOrderVM(model, id, display_name);
public static IOrderTypeVM stop_limit(OrdersModel model, IObservable<SecurityMD> security_md_source, string id, string display_name) => new StopLimitOrderVM(model, security_md_source, id, display_name);
public static IOrderTypeVM time_box(OrdersModel model, IObservable<SecurityMD> security_md_source, string id, string display_name) => new TimeBoxOrderVM(model, security_md_source, id, display_name);
public static IOrderTypeVM tick(OrdersModel model, IObservable<SecurityMD> security_md_source, string id, string display_name) => new TickOrderVM(model, security_md_source, id, display_name);
public static IOrderTypeVM lean(OrdersModel model, IObservable<SecurityMD> security_md_source, string id, string display_name) => new LeanOrderVM(model, security_md_source, id, display_name);
public static IOrderTypeVM join(OrdersModel model, IObservable<SecurityMD> security_md_source, string id, string display_name) => new JoinOrderVM(model, security_md_source, id, display_name);
public static IOrderTypeVM top(OrdersModel model, IObservable<SecurityMD> security_md_source, string id, string display_name) => new TopOrderVM(model, security_md_source, id, display_name);
public static IOrderTypeVM cover(OrdersModel model, IObservable<SecurityMD> security_md_source, string id, string display_name) => new CoverOrderVM(model, security_md_source, id, display_name);
public static IOrderTypeVM oco(OrdersModel model, IObservable<SecurityMD> security_md_source, string id, string display_name) => new OCOOrderVM(model, security_md_source, id, display_name);
public static IOrderTypeVM pricer(OrdersModel model, IObservable<SecurityMD> security_md_source, string id, string display_name) => new PricerOrderVM(model, security_md_source, id, display_name);
public static IOrderTypeVM offset(OrdersModel model, IObservable<SecurityMD> security_md_source, string id, string display_name) => new OffsetOrderVM(model, security_md_source, id, display_name);
public static IOrderTypeVM trigger(OrdersModel model, IObservable<SecurityMD> security_md_source, string id, string display_name) => new TriggerOrderVM(model, security_md_source, id, display_name);
public static IOrderTypeVM iceberg(OrdersModel model, IObservable<SecurityMD> security_md_source, string id, string display_name) => new IcebergOrderVM(model, security_md_source, id, display_name);
@@ -34,11 +35,12 @@ namespace mbts.spark {
case CustomOneofCase.Stop: return stop(model, id, display_name);
case CustomOneofCase.StopLimit: return stop_limit(model, security_md_source, id, display_name);
case CustomOneofCase.TimeBox: return time_box(model, security_md_source, id, display_name);
case CustomOneofCase.Tick: return tick(model, security_md_source, id, display_name);
case CustomOneofCase.Lean: return lean(model, security_md_source, id, display_name);
case CustomOneofCase.Join: return join(model, security_md_source, id, display_name);
case CustomOneofCase.Top: return top(model, security_md_source, id, display_name);
case CustomOneofCase.Cover: return cover(model, security_md_source, id, display_name);
case CustomOneofCase.Oco : return oco(model, security_md_source, id, display_name);
case CustomOneofCase.Pricer: return pricer(model, security_md_source, id, display_name);
case CustomOneofCase.Offset: return offset(model, security_md_source, id, display_name);
case CustomOneofCase.Trigger: return trigger(model, security_md_source, id, display_name);
case CustomOneofCase.Iceberg: return iceberg(model, security_md_source, id, display_name);
@@ -15,52 +15,42 @@ namespace mbts.spark
{
static partial class OrderTypeVMFactory
{
class InlineTickOrderProperties
class InlinePricerOrderProperties
{
public InlineTickOrderProperties (OrderTemplateHeader selected_child_order_type, IReadOnlyCollection<OrderTemplateHeader> child_order_types)
public InlinePricerOrderProperties (OrderTemplateHeader selected_child_order_type, IReadOnlyCollection<OrderTemplateHeader> child_order_types)
{
ExceptionAssert.InvalidOperation.That(child_order_types.Contains(selected_child_order_type), $"Child order type {selected_child_order_type} is not in child_order_types");
this.selected_child_order_type = new InlineUserProperty<OrderTemplateHeader>(selected_child_order_type, true);
this.child_order_types = new InlineUserProperty<IReadOnlyCollection<OrderTemplateHeader>>(child_order_types, true);
tick_offset_on_deploy = new InlineUserProperty<uint>(0, false);
cross_threshold = new InlineUserProperty<bts.spark.QuantityMeasure>(
new bts.spark.QuantityMeasure() { Ratio = 1.0, Absolute = 1}, false);
cross_ticks = new InlineUserProperty<uint>(0, false);
max_chase_ticks = new InlineUserProperty<uint>(10, false);
detach_on_toward = new InlineUserProperty<bool>(true, false);
price_offset_ticks = new InlineUserProperty<int>(0, false);
price_factor = new InlineUserProperty<double>(0, false);
}
public readonly InlineUserProperty<OrderTemplateHeader> selected_child_order_type;
public readonly InlineUserProperty<IReadOnlyCollection<OrderTemplateHeader>> child_order_types;
public readonly InlineUserProperty<uint> tick_offset_on_deploy;
public readonly InlineUserProperty<bts.spark.QuantityMeasure> cross_threshold;
public readonly InlineUserProperty<uint> cross_ticks;
public readonly InlineUserProperty<uint> max_chase_ticks;
public readonly InlineUserProperty<bool> detach_on_toward;
public readonly InlineUserProperty<int> price_offset_ticks;
public readonly InlineUserProperty<double> price_factor;
}
class TickOrderVM : OrderTypeVMBase<TickOrderDefinition>, IHierarchicalOrderTypeVM, IDisposable
class PricerOrderVM : OrderTypeVMBase<PricerOrderDefinition>, IHierarchicalOrderTypeVM, IDisposable
{
readonly IObservable<SecurityMD> _security_md_source;
SecurityImmutable? _security;
ObservableCollection<OrderTemplateHeader> _order_types = new ObservableCollection<OrderTemplateHeader>();
ObservableAutoWrapper<IOrderTypeVM, OrderTemplateHeader> _child_order_choices;
InlineTickOrderProperties _inline_props;
InlinePricerOrderProperties _inline_props;
IOrderTypeVM _child_order;
IDisposable _dispose, _child_order_errors_sub;
public uint tick_offset_on_deploy { get; set; } = 0;
public bts.spark.QuantityMeasure cross_threshold { get; set; } = new bts.spark.QuantityMeasure() { Ratio = 1.0, Absolute = 1 };
public uint cross_ticks { get; set; } = 0;
public uint max_chase_ticks { get; set; } = 10;
public bool detach_on_toward { get; set; } = true;
public int price_offset_ticks { get; set; } = 0;
public double price_factor { get; set; } = 1.0;
public TickOrderVM(OrdersModel model, IObservable<SecurityMD> security_md_source, string id, string display_name)
public PricerOrderVM(OrdersModel model, IObservable<SecurityMD> security_md_source, string id, string display_name)
: this(model, security_md_source, security_md_source.Select(smd =>
new InlineTickOrderProperties(new OrderTemplateHeader(model.factory.default_order_type(smd.security)), model.factory.default_order_types(smd.security).Select(t => new OrderTemplateHeader(t)).ToList())), id, display_name) { }
new InlinePricerOrderProperties(new OrderTemplateHeader(model.factory.default_order_type(smd.security)), model.factory.default_order_types(smd.security).Select(t => new OrderTemplateHeader(t)).ToList())), id, display_name) { }
public TickOrderVM(OrdersModel model, IObservable<SecurityMD> security_md_source,
IObservable<InlineTickOrderProperties> inline_properties, string id, string display_name) : base(model, id, display_name)
public PricerOrderVM(OrdersModel model, IObservable<SecurityMD> security_md_source,
IObservable<InlinePricerOrderProperties> inline_properties, string id, string display_name) : base(model, id, display_name)
{
_security_md_source = security_md_source;
_dispose = Disposable.Create(
@@ -76,31 +66,16 @@ namespace mbts.spark
{
_select_child_order(p.selected_child_order_type.value.key.type);
}
if (p.tick_offset_on_deploy.force_override)
if (p.price_offset_ticks.force_override)
{
tick_offset_on_deploy = p.tick_offset_on_deploy.value;
RaisePropertyChanged(nameof(tick_offset_on_deploy));
price_offset_ticks = p.price_offset_ticks.value;
RaisePropertyChanged(nameof(price_offset_ticks));
}
if (p.cross_threshold.force_override)
if (p.price_factor.force_override)
{
cross_threshold = p.cross_threshold.value;
RaisePropertyChanged(nameof(cross_threshold));
}
if (p.cross_ticks.force_override)
{
cross_ticks = p.cross_ticks.value;
RaisePropertyChanged(nameof(cross_ticks));
}
if (p.max_chase_ticks.force_override)
{
max_chase_ticks = p.max_chase_ticks.value;
RaisePropertyChanged(nameof(max_chase_ticks));
}
if (p.detach_on_toward.force_override)
{
detach_on_toward = p.detach_on_toward.value;
RaisePropertyChanged(nameof(detach_on_toward));
}
price_factor = p.price_factor.value;
RaisePropertyChanged(nameof(price_factor));
}
_inline_props = p;
_validate();
})
@@ -115,14 +90,11 @@ namespace mbts.spark
_validate();
if (!HasErrors)
{
var i = factory.tick_order();
i.TickOffsetOnDeploy = tick_offset_on_deploy;
i.CrossThreshold = cross_threshold;
i.CrossTicks = cross_ticks;
i.MaxChaseTicks = max_chase_ticks;
i.DetachOnToward = detach_on_toward;
var i = factory.pricer_order();
i.PriceOffsetTicks = price_offset_ticks;
i.PriceFactor = price_factor;
child_order.assign_to(i.Child);
value.Tick = i;
value.Pricer = i;
}
}
+48 -33
View File
@@ -16,8 +16,10 @@ using System.Linq;
using System.Reactive.Concurrency;
using System.Reactive.Linq;
using System.Threading.Tasks;
using OrderUpdate = bts.api.core.TableUpdate<(bts.api.SecurityImmutable? sec, bts.api.orderman.OrderRecordImmutable order), uint>;
namespace mbts.spark {
[Export]
class OrdersModel : IDisposable {
readonly IBus _bus;
@@ -34,7 +36,8 @@ namespace mbts.spark {
[ImportingConstructor]
private OrdersModel(IBus bus, OrderFactory factory, OrderService service, PositionService position_service,
SpreaderModel spreader, ErrorReporter error, DynamicOrderHelper dynamic_helper, AdminModel admin_model) {
SpreaderModel spreader, ErrorReporter error, DynamicOrderHelper dynamic_helper, AdminModel admin_model,
SecurityStore sstore) {
_bus = bus;
_factory = factory;
_service = service;
@@ -44,26 +47,39 @@ namespace mbts.spark {
_dynamic_helper = dynamic_helper;
var scheduler = ThreadPoolScheduler.Instance;
var interval = TimeSpan.FromMilliseconds(100);
on_orders = _service.subscribei_orders().Naggle(interval, scheduler).PermissionFilter(admin_model.on_admin_data).Table().Cache();
on_orders =
_service
.subscribei_orders()
.Naggle(interval, scheduler)
.PermissionFilter(admin_model.on_admin_data)
.join(sstore, order => order.security_key())
.Table().Cache();
on_positions = _position_service.subscribei_position_data().Naggle(interval, scheduler).PermissionFilter(admin_model.on_admin_data).Table().Cache();
working_orders =
on_orders.Scan(
ImmutableDictionary<uint,OrderRecordImmutable>.Empty,
ImmutableDictionary<uint,OrderSecurity>.Empty,
(workers, table) => _update_working_orders(workers, table))
.Replay(1).RefCount();
var order_record_tuple_source = new OrderRecordTupleSource(bus.subscribe(SparkTags.SecuritiesTag), on_orders);
on_order_tuples = order_record_tuple_source.subscribe_incremental_update();
on_order_tuples_table = order_record_tuple_source.subscribe_incremental_table_update();
on_order_securities = order_record_tuple_source.subscribe_order_securities();
on_fills = _service.subscribei_fills().Naggle(interval, scheduler).PermissionFilter(admin_model.on_admin_data).Table().Cache();
on_order_securities =
on_orders
.as_iincremental()
.filter(t => t.sec.HasValue)
.Select(t => t.sec.Value);
on_fills =
_service.subscribei_fills()
.Naggle(interval, scheduler)
.PermissionFilter(admin_model.on_admin_data)
.join(sstore, f => f.SecurityKey)
.Table().Cache();
_activity = new OrderActivitySummarizer(bus, on_orders);
on_fill_tuples = FillTupleSource.create_observable(bus.subscribe(SparkTags.SecuritiesTag), on_orders, on_fills);
on_fill_tuples = FillTupleSource.create_observable(on_orders, on_fills);
_security_orders_demuxer = new TableUpdateDemuxer<OrderRecordImmutable, uint, uint>(o => o.Key, o => o.security_key());
_security_positions_demuxer = new TableUpdateDemuxer<PositionUnitImmutable, int, uint>(o => o.RecordId, p => p.SecurityId);
_dispose = Disposable.Create(
on_orders.AddRemoveOnly().Subscribe(_security_orders_demuxer),
//on_orders.AddRemoveOnly().Subscribe(_security_orders_demuxer),
on_positions.AddRemoveOnly().Subscribe(_security_positions_demuxer),
order_record_tuple_source,
_bus.subscribe(SparkTags.CreateOrder).Subscribe(a => send_order(a.definition, a.price, a.size).ignore()),
_bus.subscribe(SparkTags.RecreateOrder).Subscribe(a => a.ForEach(x => send_order(x.definition, x.price, x.size).ignore())),
_bus.subscribe(SparkTags.CancelOrders).Subscribe(x => cancel_orders(x).ignore()),
@@ -76,13 +92,11 @@ namespace mbts.spark {
public OrderFactory factory => _factory;
public SpreaderModel spreader => _spreader;
public DynamicOrderHelper dynamic_helper => _dynamic_helper;
public IObservable<TableUpdate<OrderRecordImmutable, uint>> on_orders { get; }
public IObservable<IIncrementalUpdate<OrderRecordTuple, uint>> on_order_tuples { get; }
public IObservable<IIncrementalTableUpdateTransform<OrderRecordTuple, uint>> on_order_tuples_table { get; }
public IObservable<IIncrementalUpdate<SecurityMD, uint>> on_order_securities { get; }
public IObservable<TableUpdate<FillImmutable, string>> on_fills { get; }
public IObservable<OrderUpdate> on_orders { get; }
public IObservable<IIncrementalUpdate<SecurityImmutable, uint>> on_order_securities { get; }
public IObservable<TableUpdate<(SecurityImmutable?,FillImmutable), string>> on_fills { get; }
public IObservable<IIncrementalUpdate<FillTuple, FillTupleKey>> on_fill_tuples { get; }
public IObservable<IReadOnlyDictionary<uint,OrderRecordImmutable>> working_orders { get; }
public IObservable<IReadOnlyDictionary<uint,OrderSecurity>> working_orders { get; }
public IObservable<TableUpdate<PositionUnitImmutable, int>> on_positions { get; }
public IObservable<ITableUpdateTransform<OrderRecordImmutable, uint>> on_security_orders(uint security) =>
@@ -153,24 +167,25 @@ namespace mbts.spark {
public void Dispose() =>
Disposable.dispose(ref _dispose);
static ImmutableDictionary<uint, OrderRecordImmutable> _update_working_orders(ImmutableDictionary<uint, OrderRecordImmutable> target, TableUpdate<OrderRecordImmutable, uint> table) {
static ImmutableDictionary<uint, OrderSecurity>
_update_working_orders(ImmutableDictionary<uint, OrderSecurity> target, OrderUpdate table) {
var clone = target.ToBuilder();
foreach (var o in table.removed) {
if (o.is_active()) {
clone.Remove(o.Key);
if (o.order.is_active()) {
clone.Remove(o.order.Key);
}
}
foreach (var o in table.changed) {
if (o.is_active()) {
clone[o.Key] = o;
foreach (var o in table.changes) {
if (o.Old.order.is_active()) {
clone.Remove(o.Old.order.Key);
}
else {
clone.Remove(o.Key);
if (o.New.order.is_active()) {
clone.Add(o.New.order.Key, o.New);
}
}
foreach (var o in table.added) {
if (o.is_active()) {
clone.Add(o.Key, o);
if (o.order.is_active()) {
clone.Add(o.order.Key, o);
}
}
return clone.ToImmutableDictionary();
@@ -185,8 +200,8 @@ namespace mbts.spark {
public static async Task<bool> cancel_order(this OrdersModel orders_model, uint order_id) {
var tu = await orders_model.on_orders.FirstAsync();
if (tu.state.TryGetValue(order_id, out OrderRecordImmutable order)) {
return await orders_model.cancel_order(order);
if (tu.state.TryGetValue(order_id, out var o)) {
return await orders_model.cancel_order(o.order);
}
return false;
}
@@ -195,8 +210,8 @@ namespace mbts.spark {
var tu = await model.on_orders.FirstAsync();
var orders =
tu.state.Values
.Where(o => o.is_non_terminal() && pred(o))
.Select(async o => { return (o, await model.cancel_order(o)); } );
.Where(o => o.order.is_non_terminal() && pred(o.order))
.Select(async o => { return (o.order, await model.cancel_order(o.order)); } );
return await Task.WhenAll(orders);
}
@@ -304,8 +319,8 @@ namespace mbts.spark {
public static async Task<OrderRecordImmutable?> order_from_key(this OrdersModel orders_model, uint order_key) {
(var tu, var success) = await orders_model.on_orders.FirstAsyncTask(t => t.state.ContainsKey(order_key), TimeSpan.FromSeconds(3));
if (success && tu.state.TryGetValue(order_key, out OrderRecordImmutable rec)) {
return rec;
if (success && tu.state.TryGetValue(order_key, out var rec)) {
return rec.order;
}
return null;
}
@@ -26,7 +26,7 @@ namespace mbts {
}
[ProtoContract]
class OrderCanceledEventPredicate : IPredicateBuilder<OrderRecordTuple, INotificationServiceDataCache>, IEquatable<OrderCanceledEventPredicate> {
class OrderCanceledEventPredicate : IPredicateBuilder<OrderSecurity, INotificationServiceDataCache>, IEquatable<OrderCanceledEventPredicate> {
public enum PredicateTypeOneofCase {
[Description("(any order)")]
AnyOrder,
@@ -45,13 +45,13 @@ namespace mbts {
}
}
public IPredicate<OrderRecordTuple> create(object obj) =>
public IPredicate<OrderSecurity> create(object obj) =>
create_predicate((INotificationServiceDataCache)obj);
public IPredicate<OrderRecordTuple> create_predicate(INotificationServiceDataCache data_cache) =>
public IPredicate<OrderSecurity> create_predicate(INotificationServiceDataCache data_cache) =>
OrderEventPredicateHelper.create_status_transition_predicate(OrderStatus.Canceled, security, data_cache);
public INotificationPredicate<OrderRecordTuple> create_toast(INotificationServiceDataCache data_cache) =>
public INotificationPredicate<OrderSecurity> create_toast(INotificationServiceDataCache data_cache) =>
new MyToaster(security?.create(data_cache), data_cache);
public bool Equals(OrderCanceledEventPredicate other) => other != null && other.security.safe_equals(other.security);
@@ -61,7 +61,7 @@ namespace mbts {
static bool _is_canceled(OrderRecordImmutable o) =>
o.HasState && o.State.Status == OrderStatus.Canceled;
class MyToaster : INotificationPredicate<OrderRecordTuple>, IEquatable<MyToaster> {
class MyToaster : INotificationPredicate<OrderSecurity>, IEquatable<MyToaster> {
readonly IPredicate<SecurityImmutable> _security_predicate;
readonly INotificationServiceDataCache _data_cache;
@@ -70,12 +70,13 @@ namespace mbts {
_data_cache = data_cache;
}
public IToastNotification trigger(OrderRecordTuple order) =>
OrderEventPredicateHelper.check_order_transition_trigger(order, _is_canceled, _security_predicate, _data_cache, out OrderRecordTuple oldval) ?
ToastNotifications.order_status_changed(order.order.Key, OrderEventPredicateHelper.get_nullable_status(oldval), OrderStatus.Canceled, order.security_md.HasValue ? order.security_md.Value.security : new SecurityImmutable?()) :
public IToastNotification trigger(OrderSecurity order) =>
OrderEventPredicateHelper.check_order_transition_trigger(order, _is_canceled, _security_predicate, _data_cache, out var oldval) ?
ToastNotifications.order_status_changed(order.order.Key,
OrderEventPredicateHelper.get_nullable_status(oldval), OrderStatus.Canceled, order.security) :
ToastNotifications.empty;
bool IPredicate<OrderRecordTuple>.trigger(OrderRecordTuple order) =>
bool IPredicate<OrderSecurity>.trigger(OrderSecurity order) =>
OrderEventPredicateHelper.check_order_transition_predicate(order, _is_canceled, _security_predicate, _data_cache);
public bool Equals(MyToaster other) =>
@@ -26,7 +26,7 @@ namespace mbts {
}
[ProtoContract]
class OrderCompletedEventPredicate : IPredicateBuilder<OrderRecordTuple, INotificationServiceDataCache>, IEquatable<OrderCompletedEventPredicate> {
class OrderCompletedEventPredicate : IPredicateBuilder<OrderSecurity, INotificationServiceDataCache>, IEquatable<OrderCompletedEventPredicate> {
public enum PredicateTypeOneofCase {
[Description("(any order)")]
AnyOrder,
@@ -45,13 +45,13 @@ namespace mbts {
}
}
public IPredicate<OrderRecordTuple> create(object obj) =>
public IPredicate<OrderSecurity> create(object obj) =>
create_predicate((INotificationServiceDataCache)obj);
public IPredicate<OrderRecordTuple> create_predicate(INotificationServiceDataCache data_cache) =>
public IPredicate<OrderSecurity> create_predicate(INotificationServiceDataCache data_cache) =>
OrderEventPredicateHelper.create_status_transition_predicate(OrderStatus.Completed, security, data_cache);
public INotificationPredicate<OrderRecordTuple> create_toast(INotificationServiceDataCache data_cache) =>
public INotificationPredicate<OrderSecurity> create_toast(INotificationServiceDataCache data_cache) =>
new MyToaster(security?.create(data_cache), data_cache);
public bool Equals(OrderCompletedEventPredicate other) => other != null && other.security.safe_equals(other.security);
@@ -61,7 +61,7 @@ namespace mbts {
static bool _is_completed(OrderRecordImmutable o) =>
o.HasState && o.State.Status == OrderStatus.Completed;
class MyToaster : INotificationPredicate<OrderRecordTuple>, IEquatable<MyToaster> {
class MyToaster : INotificationPredicate<OrderSecurity>, IEquatable<MyToaster> {
readonly IPredicate<SecurityImmutable> _security_predicate;
readonly INotificationServiceDataCache _data_cache;
@@ -70,11 +70,11 @@ namespace mbts {
_data_cache = data_cache;
}
public IToastNotification trigger(OrderRecordTuple order) =>
public IToastNotification trigger(OrderSecurity order) =>
OrderEventPredicateHelper.check_order_transition_predicate(order, _is_completed, _security_predicate, _data_cache) ?
ToastNotifications.order_completed(order) : ToastNotifications.empty;
bool IPredicate<OrderRecordTuple>.trigger(OrderRecordTuple order) =>
bool IPredicate<OrderSecurity>.trigger(OrderSecurity order) =>
OrderEventPredicateHelper.check_order_transition_predicate(order, _is_completed, _security_predicate, _data_cache);
public bool Equals(MyToaster other) =>
@@ -11,16 +11,16 @@ namespace mbts {
/// <summary>
/// Checks if the order has transitioned from 'not passing the predicate, or not existing' to 'passing the predicate.'
/// </summary>
public static bool check_order_transition_predicate(OrderRecordTuple order, Func<OrderRecordImmutable, bool> order_predicate_core, IPredicate<SecurityImmutable> security_predicate, INotificationServiceDataCache data_cache) =>
check_order_transition_trigger(order, order_predicate_core, security_predicate, data_cache, out OrderRecordTuple _);
public static bool check_order_transition_predicate(OrderSecurity order, Func<OrderRecordImmutable, bool> order_predicate_core, IPredicate<SecurityImmutable> security_predicate, INotificationServiceDataCache data_cache) =>
check_order_transition_trigger(order, order_predicate_core, security_predicate, data_cache, out var _);
/// <summary>
/// Checks if the order has transitioned from 'not passing the predicate, or not existing' to 'passing the predicate.' Oldval will be null if the order did not previously exist, or does not currently pass the predicate.
/// </summary>
public static bool check_order_transition_trigger(OrderRecordTuple order, Func<OrderRecordImmutable, bool> order_predicate_core, IPredicate<SecurityImmutable> security_predicate, INotificationServiceDataCache data_cache, out OrderRecordTuple oldval) {
public static bool check_order_transition_trigger(OrderSecurity order, Func<OrderRecordImmutable, bool> order_predicate_core, IPredicate<SecurityImmutable> security_predicate, INotificationServiceDataCache data_cache, out OrderSecurity? oldval) {
if (check_security_predicate(order, security_predicate) && order_predicate_core(order.order)) {
oldval = data_cache.get_previous_order_state(order.order.Key);
return oldval == null ? !data_cache.is_first_order_update : !order_predicate_core(oldval.order);
return oldval == null ? !data_cache.is_first_order_update : !order_predicate_core(oldval.Value.order);
}
oldval = null;
return false;
@@ -29,19 +29,19 @@ namespace mbts {
/// <summary>
/// Checks if the 'security predicate is null,' or 'it is not null, and the order has a non-null security which passes.'
/// </summary>
public static bool check_security_predicate(OrderRecordTuple order, IPredicate<SecurityImmutable> security_predicate) =>
security_predicate == null || (order.security_md.HasValue && security_predicate.trigger(order.security_md.Value.security));
public static bool check_security_predicate(OrderSecurity order, IPredicate<SecurityImmutable> security_predicate) =>
security_predicate == null || (order.security.HasValue && security_predicate.trigger(order.security.Value));
public static OrderStatus? get_nullable_status(OrderRecordTuple order) {
if (order == null && order.order.HasState)
return order.order.State.Status;
public static OrderStatus? get_nullable_status(OrderSecurity? order) {
if (order?.order.HasState == true)
return order?.order.State.Status;
return null;
}
public static IPredicate<OrderRecordTuple> create_status_transition_predicate(OrderStatus status, SecurityPredicate security_predicate, INotificationServiceDataCache data_cache) =>
public static IPredicate<OrderSecurity> create_status_transition_predicate(OrderStatus status, SecurityPredicate security_predicate, INotificationServiceDataCache data_cache) =>
new OrderStatusTransitionPredicate(status, security_predicate?.create(data_cache), data_cache);
class OrderStatusTransitionPredicate : IPredicate<OrderRecordTuple>, IEquatable<OrderStatusTransitionPredicate> {
class OrderStatusTransitionPredicate : IPredicate<OrderSecurity>, IEquatable<OrderStatusTransitionPredicate> {
readonly IPredicate<SecurityImmutable> _security_predicate;
readonly INotificationServiceDataCache _data_cache;
readonly OrderStatus _status;
@@ -52,7 +52,7 @@ namespace mbts {
_data_cache = data_cache;
}
public bool trigger(OrderRecordTuple value) =>
public bool trigger(OrderSecurity value) =>
check_order_transition_predicate(value, o => o.HasState && o.State.Status == _status, _security_predicate, _data_cache);
public bool Equals(OrderStatusTransitionPredicate other) =>
@@ -1,4 +1,5 @@
using bts.api;
using bts.api.orderman;
using bts.shell;
using mbts.spark;
using mbts.ui.settings;
@@ -13,8 +14,9 @@ namespace mbts {
{ OrderFilledEventPredicate.PredicateTypeOneofCase.AnyOrder, PredicateBuilderVMWrapper.create<OrderFilledEventPredicate>(p => p.security = null) },
{ OrderFilledEventPredicate.PredicateTypeOneofCase.SecurityOrder, SecurityCriteriaVMFactory.create<OrderFilledEventPredicate>(bus, o => o.security?.is_value ?? new SecurityCriteria(),
(p, s) => {
if (p.security == null)
if (p.security == null) {
p.security = new SecurityPredicate();
}
p.security.is_value = s;
})
}
@@ -22,7 +24,7 @@ namespace mbts {
}
[ProtoContract]
class OrderFilledEventPredicate : IPredicateBuilder<OrderRecordTuple, INotificationServiceDataCache>, IEquatable<OrderFilledEventPredicate> {
class OrderFilledEventPredicate : IPredicateBuilder<OrderSecurity, INotificationServiceDataCache>, IEquatable<OrderFilledEventPredicate> {
public enum PredicateTypeOneofCase {
[PredicateCase("(any order)")]
AnyOrder,
@@ -35,19 +37,21 @@ namespace mbts {
public PredicateTypeOneofCase predicate_type_case {
get {
if (security != null)
if (security != null) {
return PredicateTypeOneofCase.SecurityOrder;
}
return PredicateTypeOneofCase.AnyOrder;
}
}
public IPredicate<OrderRecordTuple> create(object obj) =>
public IPredicate<OrderSecurity> create(object obj) =>
create_predicate((INotificationServiceDataCache)obj);
public IPredicate<OrderRecordTuple> create_predicate(INotificationServiceDataCache notification_service_data_cache) =>
public IPredicate<OrderSecurity> create_predicate(INotificationServiceDataCache notification_service_data_cache) =>
new FillTrigger(_create_model(notification_service_data_cache));
public INotificationPredicate<OrderRecordTuple> create_toast(INotificationServiceDataCache notification_service_data_cache) =>
public INotificationPredicate<OrderSecurity> create_toast(INotificationServiceDataCache notification_service_data_cache) =>
new FillToastNotificationTrigger(_create_model(notification_service_data_cache));
public bool Equals(OrderFilledEventPredicate other) => other != null && other.security.safe_equals(other.security);
@@ -66,35 +70,43 @@ namespace mbts {
_security_predicate = security_predicate;
}
public (bool trigger, OrderRecordTuple oldval, OrderRecordTuple newval) get_filled_order_args(OrderRecordTuple ort) {
// Please review carfully
public (OrderSecurity oldval, OrderSecurity newval)? get_filled_order_args(OrderSecurity ort) {
if (ort.order.is_exchange_order() && OrderEventPredicateHelper.check_security_predicate(ort, _security_predicate)) {
if (_data_cache.get_previous_order_state(ort.order.Key) is OrderRecordTuple oldval)
return (ort.order.filled() > oldval.order.filled(), oldval, ort);
if (_data_cache.get_previous_order_state(ort.order.Key) is var oldval && oldval.HasValue) {
if (ort.order.filled() > oldval?.order.filled()) {
return (oldval.Value, ort);
}
}
else if (!_data_cache.is_first_order_update && ort.order.filled() > 0) {
// old order will be a copy of new order, except with filledQuantity set to 0
var old_state = ort.order.State.CloneToMutable();
old_state.FilledQuantity = 0;
return (true, new OrderRecordTuple(ort.order.with(state: old_state), ort.security_md), ort);
return (new OrderSecurity(ort.security, ort.order.with(state: old_state)), ort);
}
}
return (false, null, null);
return null;
}
}
class FillTrigger : IPredicate<OrderRecordTuple> {
class FillTrigger : IPredicate<OrderSecurity> {
protected readonly FillTriggerModel _model;
public FillTrigger(FillTriggerModel model) => _model = model;
public bool trigger(OrderRecordTuple order) => _model.get_filled_order_args(order).trigger;
public bool trigger(OrderSecurity order) => _model.get_filled_order_args(order).HasValue;
}
class FillToastNotificationTrigger : FillTrigger, INotificationPredicate<OrderRecordTuple> {
class FillToastNotificationTrigger : FillTrigger, INotificationPredicate<OrderSecurity> {
public FillToastNotificationTrigger(FillTriggerModel model) : base(model) { }
public new IToastNotification trigger(OrderRecordTuple value) {
(var trigger, var oldval, var newval) = _model.get_filled_order_args(value);
return trigger ? ToastNotifications.order_filled(oldval, newval) : ToastNotifications.empty;
public new IToastNotification trigger(OrderSecurity value) {
var args = _model.get_filled_order_args(value);
if (args.HasValue) {
return ToastNotifications.order_filled(args.Value.oldval, args.Value.newval);
}
return ToastNotifications.empty;
//return args ? ToastNotifications.order_filled(oldval, newval) : ToastNotifications.empty;
}
}
}
@@ -61,7 +61,7 @@ namespace mbts {
}
[ProtoContract]
class OrderRecordTuplePredicate : INotificationPredicateBuilder<OrderRecordTuple, INotificationServiceDataCache>, IEquatable<OrderRecordTuplePredicate> {
class OrderRecordTuplePredicate : INotificationPredicateBuilder<OrderSecurity, INotificationServiceDataCache>, IEquatable<OrderRecordTuplePredicate> {
SecurityPredicate _security;
SidePredicate _side;
DoublePredicate _price, _last_price, _avg_price;
@@ -206,14 +206,14 @@ namespace mbts {
}
}
public IPredicate<OrderRecordTuple> create_predicate(INotificationServiceDataCache data_cache) {
public IPredicate<OrderSecurity> create_predicate(INotificationServiceDataCache data_cache) {
switch (predicate_type_case) {
case PTOC.Security: return wrap(_security, r => r.security_md.HasValue ? r.security_md.Value.security : SecurityDataHelper.Defaults.SecurityImmutable);
case PTOC.Security: return wrap(_security, r => r.security ?? SecurityDataHelper.Defaults.SecurityImmutable);
case PTOC.Time: return wrap(_time_created, r => r.order.created());
case PTOC.LastChange: return wrap(_last_change, r => r.order.last_change());
case PTOC.User: return wrap(_user, r => r.order.user_id());
case PTOC.Account: return wrap(_account, r => r.order.account_id());
case PTOC.Exchange: return wrap(_exchange, r => r.security_md.HasValue ? r.security_md.Value.security.Exchange : Venue.Unset);
case PTOC.Exchange: return wrap(_exchange, r => r.security.exchange_or_default());
case PTOC.TimeInForce: return wrap(_time_in_force, r => r.order.time_in_force());
case PTOC.OrderType: return wrap(_order_type, r => r.order.order_type());
case PTOC.SubType: return wrap(_order_sub_type, r => r.order.sub_type());
@@ -234,14 +234,14 @@ namespace mbts {
default: return null;
}
//
IPredicate<OrderRecordTuple> wrap<T>(IPredicateBuilder<T> trigger, Func<OrderRecordTuple, T> select) =>
trigger.create(data_cache).wrap(select);
IPredicate<OrderSecurity> wrap<T>(IPredicateBuilder<T> trigger, Func<OrderSecurity, T> select) =>
trigger.create(data_cache).wrap<OrderSecurity,T>(select);
}
IPredicate<OrderRecordTuple> IPredicateBuilder<OrderRecordTuple>.create(object obj) =>
IPredicate<OrderSecurity> IPredicateBuilder<OrderSecurity>.create(object obj) =>
create_predicate((INotificationServiceDataCache)obj);
public INotificationPredicate<OrderRecordTuple> create_notification_predicate(INotificationServiceDataCache notification_service_data_cache) {
public INotificationPredicate<OrderSecurity> create_notification_predicate(INotificationServiceDataCache notification_service_data_cache) {
if (!is_toast_enabled)
return null;
switch (predicate_type_case) {
@@ -252,7 +252,7 @@ namespace mbts {
}
}
INotificationPredicate<OrderRecordTuple> INotificationPredicateBuilder<OrderRecordTuple>.create_notification_predicate(object obj) =>
INotificationPredicate<OrderSecurity> INotificationPredicateBuilder<OrderSecurity>.create_notification_predicate(object obj) =>
create_notification_predicate((INotificationServiceDataCache)obj);
public bool Equals(OrderRecordTuplePredicate other) {
@@ -325,8 +325,8 @@ namespace mbts {
return EqualityComparer.phase_hash(raw_hash, (int)ptoc);
}
static string _get_order_placed_toast_msg(OrderRecordTuple order) =>
$"Order Sent: {order.security_md.display_name()} {order.order.short_description(order.security_md.spec_or_default())}";
static string _get_order_placed_toast_msg(OrderSecurity order) =>
$"Order Sent: {order.security.display_name()} {order.order.short_description(order.security.spec_or_default())}";
void _set<T>(ref T field, T value) {
if (value != null) {
@@ -359,7 +359,7 @@ namespace mbts {
}
[ProtoContract]
class OrderRecordTuplePredicateAnimations : IPredicateAnimationsData<OrderRecordTuple, OrderRecordTuplePredicate> {
class OrderRecordTuplePredicateAnimations : IPredicateAnimationsData<OrderSecurity, OrderRecordTuplePredicate> {
[ProtoMember(1)]
OrderRecordTuplePredicate _predicate;
[ProtoMember(2)]
+30
View File
@@ -0,0 +1,30 @@
using mbts;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Reactive.Linq;
namespace bts {
class MDLookup : IDisposable {
IReadOnlyDictionary<uint, SecurityMD> _state;
IDisposable _dispose;
[ImportingConstructor]
public MDLookup(IObservable<IReadOnlyDictionary<uint, SecurityMD>> src) {
_dispose = src.Subscribe(s => _state = s);
}
public void Dispose() { Disposable.dispose(ref _dispose); }
public SecurityMD? get_md(uint key) {
if (_state?.TryGetValue(key, out var md) == true) {
return md;
}
return null;
}
public SecurityMD? get_md(uint? key) {
if (key.HasValue) return get_md(key.Value);
return null;
}
}
}
+261
View File
@@ -0,0 +1,261 @@
using bts.api;
using bts.api.core;
using bts.co2;
using mbts;
using mbts.spark;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.ComponentModel.Composition;
using System.Linq;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using System.Threading.Tasks;
using security_key = System.UInt32;
namespace bts {
[Export]
public class SecurityStore : IDisposable {
readonly MarketDataRelay _md;
readonly IBus _bus;
IDisposable _dispose;
IReadOnlyDictionary<security_key, SecurityMD> _live_securities;
readonly ConcurrentDictionary<security_key, SecurityImmutable> _cache = new ConcurrentDictionary<security_key, SecurityImmutable>();
readonly Subject<IReadOnlyDictionary<security_key, SecurityImmutable>> _cache_subject = new Subject<IReadOnlyDictionary<security_key, SecurityImmutable>>();
[ImportingConstructor]
public SecurityStore(MarketDataRelay md, IBus bus) {
_md = md;
_bus = bus;
_dispose = bus.subscribe(SparkTags.SecuritiesTag).Subscribe(dict => _live_securities = dict);
_cache_subject.OnNext(_cache);
}
IObservable<IReadOnlyDictionary<security_key, SecurityMD>> on_securities => _bus.subscribe(SparkTags.SecuritiesTag);
SecurityImmutable? try_get_local(security_key key, IReadOnlyDictionary<security_key, SecurityMD> live=null) {
// Try live securities first
live = live ?? _live_securities;
if (live != null) {
if (live.TryGetValue(key, out SecurityMD value)) {
return value.security;
}
}
// Then try cached securities
if (_cache.TryGetValue(key, out SecurityImmutable security)) {
return security;
}
return null;
}
async Task<SecurityImmutable?> get_security(security_key key, IReadOnlyDictionary<security_key, SecurityMD> live) {
var local_sec = try_get_local(key, live);
if (local_sec.HasValue) {
return local_sec;
}
// Query the server for the value
var result = await _md.get_securities_async(new SecurityKeyList() {
Keys = { key }
});
if (result.success) {
var security = result.value.Security.FirstOrDefault();
if (security != null) {
return try_cache_security(new SecurityImmutable(security));
}
}
// Give up :(
Log.Md.warn($"Failed to get security for key {key}");
return null;
}
async Task<IReadOnlyDictionary<security_key, SecurityImmutable>> get_securities(IEnumerable<security_key> keys, IReadOnlyDictionary<security_key, SecurityMD> live) {
var result = new Dictionary<security_key, SecurityImmutable>();
var missing = new SecurityKeyList();
foreach (var key in keys) {
var s = try_get_local(key);
if (s.HasValue) {
result.Add(key, s.Value);
}
else {
missing.Keys.Add(key);
}
}
if (missing.Keys.Count > 0) {
var res = await _md.get_securities_async(missing);
if (res.success) {
foreach (var security in res.value.Security) {
result.Add(security.Id, try_cache_security(new SecurityImmutable(security)));
}
}
}
return result;
}
// Request any missing keys from the server but do not wait for them to arrive
IReadOnlyDictionary<security_key, SecurityImmutable> get_and_ensure_securities(IEnumerable<security_key> keys) {
var result = new Dictionary<security_key, SecurityImmutable>();
var missing = new HashSet<security_key>();
foreach (var key in keys) {
var security = try_get_local(key);
if (security.HasValue) {
result.TryAdd(key, security.Value);
}
else {
missing.Add(key);
}
}
// Update cache in the background
if (missing.Count > 0) {
Task.Run(async () => {
var skl = new SecurityKeyList();
skl.Keys.AddRange(missing);
var r = await _md.get_securities_async(skl);
if (r.success) {
var retrieved = r.value.Security.Select(s => s.Id);
Log.Md.info($"Loaded securities from cold storage [{string.Join(",", retrieved)}]");
foreach (var security in r.value.Security) {
try_cache_security(new SecurityImmutable(security));
}
}
});
}
return result;
}
SecurityImmutable try_cache_security(SecurityImmutable s) {
if (_live_securities == null || !_live_securities.TryGetValue(s.Id, out SecurityMD live)) {
_cache.TryAdd(s.Id, s);
_cache_subject.OnNext(_cache);
return s;
}
return live.security;
}
///////////////
// PUBLIC
///////////////
#region public
// Guaranteed to return a security if it has ever been seen by the system
public Task<SecurityImmutable?> get_security(security_key key) {
return get_security(key, _live_securities);
}
// Guaranteed to return a security if it has ever been seen by the system
public Task<IReadOnlyDictionary<security_key, SecurityImmutable>> get_securities(IEnumerable<security_key> keys) {
return get_securities(keys, _live_securities);
}
public void Dispose() {
Disposable.dispose(ref _dispose);
}
public IObservable<Update<(SecurityImmutable? sec, T val), TKey>> join<T,TKey>(
IObservable<Update<T, TKey>> source, Func<T,security_key> skeyfn) where TKey : IEquatable<TKey> {
return Observable.Create<Update<(SecurityImmutable?, T), TKey>>(observer => {
var state = ImmutableDictionary<TKey, (SecurityImmutable? s, T t)>.Empty;
var gate = new object();
// We get thie function from the first source update
Func<(SecurityImmutable?,T t), TKey> rowkeyfn = null;
void on_securities_updated(object ambiguous) {
if (rowkeyfn == null) return;
lock (gate) {
// If this is null we don't have any rows
var changed = new List<Change<(SecurityImmutable? s, T t)>>();
foreach (var entry in state.Values) {
if (entry.s == null) {
var security = try_get_local(skeyfn(entry.t), ambiguous as IReadOnlyDictionary<security_key,SecurityMD>);
if (security.HasValue) {
changed.Add(
new Change<(SecurityImmutable?, T)>(entry, (security, entry.t)));
}
}
}
if (changed.Count > 0) {
// update state
var builder = state.ToBuilder();
foreach (var up in changed) {
builder[rowkeyfn(up.New)] = up.New;
}
state = builder.ToImmutable();
// fire update
observer.OnNext(
new Update<(SecurityImmutable?, T), TKey>(
state.Values,
Enumerable.Empty<(SecurityImmutable?,T)>(),
changed,
Enumerable.Empty<(SecurityImmutable?,T)>(),
rowkeyfn));
}
}
}
return Disposable.Create(
// Fire update when source changes
source.Subscribe((update) => {
lock (gate) {
rowkeyfn = rowkeyfn ?? (i => update.key_extractor(i.Item2));
var relevant_securities = get_and_ensure_securities(update.added.Concat(update.changed).Select(skeyfn));
var added = new List<(SecurityImmutable?, T)>();
var changed = new List<Change<(SecurityImmutable? s, T t)>>();
var removed = new List<(SecurityImmutable?, T)>();
foreach (var x in update.added) {
if (relevant_securities.TryGetValue(skeyfn(x), out SecurityImmutable s)) {
added.Add((s, x));
}
else {
added.Add((null, x));
}
}
foreach (var x in update.removed) {
if (state.TryGetValue(update.key_extractor(x), out (SecurityImmutable?, T) value)) {
removed.Add(value);
}
}
foreach (var ch in update.changes) {
SecurityImmutable? si =
relevant_securities.TryGetValue(skeyfn(ch.New), out SecurityImmutable s) ?
s : (SecurityImmutable?)null;
changed.Add(new Change<(SecurityImmutable?, T)>((si, ch.Old), (si, ch.New)));
}
var builder = state.ToBuilder();
builder.RemoveRange(update.removed.Select(update.key_extractor));
builder.RemoveRange(changed.Select(ch => rowkeyfn(ch.Old)));
foreach (var a in added) {
builder.Add(rowkeyfn(a), a);
}
foreach (var ch in changed) {
builder.Add(rowkeyfn(ch.New), ch.New);
}
state = builder.ToImmutable();
observer.OnNext(new Update<(SecurityImmutable?, T t), TKey>(
state.Values, added, changed, removed, rowkeyfn));
}
}),
// Also fire update when securities change
on_securities.Subscribe(on_securities_updated),
// Also fire update when cache changes
_cache_subject.Subscribe(on_securities_updated)
);
});
}
#endregion
}
public static class SecurityStoreExtensions {
public static IObservable<Update<(SecurityImmutable? sec, T val), TKey>>
join<T,TKey>(this IObservable<Update<T, TKey>> source, SecurityStore store, Func<T,security_key> skeyfn) where TKey : IEquatable<TKey> {
return store.join(source, skeyfn);
}
}
}
@@ -211,6 +211,8 @@ namespace mbts.ui {
public static BTSColor lean_order { get { return new BTSColor(255, 170, 210); } }
public static BTSColor join_order { get { return new BTSColor(200, 171, 232); } }
public static BTSColor cover_order { get { return new BTSColor(134, 232, 224); } }
public static BTSColor oco_order { get { return new BTSColor(110, 94, 11); } }
public static BTSColor pricer_order { get { return new BTSColor(170, 132, 150); } }
public static BTSColor trigger_order { get { return new BTSColor(172, 255, 179); } }
public static BTSColor off_order { get { return new BTSColor(0x8a, 0x00, 0x5f); } }//8a005f
public static BTSColor top_order { get { return new BTSColor(0xdd, 0x7a, 0x27); } }//dd7a27
@@ -233,6 +233,7 @@ namespace mbts
return has_value;
}
}
}
@@ -1,4 +1,5 @@
using bts.api.core;
using bts.collections;
using mbts.collections;
using System;
using System.Collections.Generic;
@@ -26,9 +27,6 @@ namespace mbts.utils {
}
class IncrementalUpdate<T, TKey> : IIncrementalUpdate<T, TKey> where TKey : IEquatable<TKey> {
public IncrementalUpdate()
: this(new Dictionary<TKey, T>(0), new Dictionary<TKey, T>(0)) { }
public IncrementalUpdate(IReadOnlyDictionary<TKey, T> added, IReadOnlyDictionary<TKey, T> removed) {
this.added = added;
this.removed = removed;
@@ -305,6 +303,40 @@ namespace mbts.utils {
state_clone.Add(kv.Key, kv.Value);
return new PivotTableUpdateTransform<T, TKey, TResult, TResultKey>(state_clone, added.Values.ToList(), removed.Values.ToList(), _key_map, _selector, _result_key, _key);
}
}
}
public static IObservable<IIncrementalUpdate<T, TKey>> as_iincremental<T, TKey>(this IObservable<TableUpdate<T, TKey>> src) where TKey : IEquatable<TKey> {
return src.Select(as_iincremental);
}
public static IIncrementalUpdate<T, TKey> as_iincremental<T, TKey>(this TableUpdate<T, TKey> update) where TKey : IEquatable<TKey> {
var key = update.key_extractor;
var added = update.added.ToDictionary(key);
var removed = update.removed.ToDictionary(key);
foreach (var ch in update.changes) {
added[key(ch.New)] = ch.New;
removed[key(ch.Old)] = ch.Old;
}
return new IncrementalUpdate<T, TKey>(added, removed);
}
public static IObservable<IIncrementalUpdate<TRes, TKey>>
Select<TSrc, TRes, TKey>(this IObservable<IIncrementalUpdate<TSrc, TKey>> src, Func<TSrc, TRes> map) where TKey : IEquatable<TKey> {
return src.Select(update => {
return new IncrementalUpdate<TRes, TKey>(
new MappedDictionary<TKey, TSrc, TRes>(update.added, map),
new MappedDictionary<TKey, TSrc, TRes>(update.removed, map));
});
}
public static IObservable<IIncrementalUpdate<T, TKey>>
Where<T, TKey>(this IObservable<IIncrementalUpdate<T, TKey>> src, Func<T, bool> where) where TKey : IEquatable<TKey> {
return src.Select(update => {
return new IncrementalUpdate<T, TKey>(
new FilteredDictionary<TKey, T>(update.added, where),
new FilteredDictionary<TKey, T>(update.removed, where));
});
}
}
}
@@ -51,7 +51,7 @@ namespace bts.proto {
static class DateTimeProviderExtensions {
public static DateTime get_date_time(this DateTimeProvider dtp) {
switch (dtp.mode) {
case DateTimeProviderMode.CMEOpen: return MarketSchedule.get_cme_gen_open();
case DateTimeProviderMode.CMEOpen: return MarketSchedule.get_start_of_trading_day();
case DateTimeProviderMode.Today: return DateTime.Today;
case DateTimeProviderMode.Explicit:
default: return dtp.explicit_date.GetValueOrDefault();
@@ -36,7 +36,6 @@ namespace mbts.ui.controls {
Symbol = 78,
Time = 112,
TimeInForce = 47,
TradeKey = 76,
User = 53,
Aggressor = 80,
Volume = 74,
@@ -71,7 +70,6 @@ namespace mbts.ui.controls {
ImpliedSize = "Qty (I)",
Symbol = "Symbol",
TimeInForce = "TIF",
TradeKey = "Trade Key",
User = "User",
Time = "Time",
Aggressor = "Aggressor",
@@ -6,11 +6,12 @@ using System.Windows.Data;
namespace mbts.ui.controls {
partial class DIYGrid {
internal class OrderTypeColumn<TColKey> : FrameworkElementColumnBase<TColKey>, IFilterableColumn {
readonly BindingBase _order_type_binding, _metadata_binding;
readonly BindingBase _order_type_binding, _metadata_binding, _display_name_binding;
public OrderTypeColumn(TColKey key, BindingBase order_type_binding, BindingBase metadata_binding, IDataGridColumnFilter filter) : base(key) {
public OrderTypeColumn(TColKey key, BindingBase order_type_binding, BindingBase metadata_binding, BindingBase display_name_binding, IDataGridColumnFilter filter) : base(key) {
_order_type_binding = order_type_binding;
_metadata_binding = metadata_binding;
_display_name_binding = display_name_binding;
this.filter = filter;
Header = DefaultColumnHeaders.OrderType;
Width = DefaultColumnWidths.OrderType;
@@ -28,6 +29,7 @@ namespace mbts.ui.controls {
};
v.SetBinding(OrderTypeItem.OrderTypeProperty, _order_type_binding);
v.SetBinding(OrderTypeItem.MetadataProperty, _metadata_binding);
v.SetBinding(OrderTypeItem.DisplayNameProperty, _display_name_binding);
if (font_weight_binding != null) {
v.SetBinding(FontWeightProperty, font_weight_binding);
} else {
@@ -17,7 +17,8 @@ using System.Windows.Media;
using static mbts.ui.controls.DIYGrid;
namespace mbts.ui {
class GridSettingsManager<TRow, TRowData, TPredicateBuilderArg, TPredicateBuilder, TSettingsData> : ViewModelBase, IValueFactory<TSettingsData>, IObserver<TSettingsData>, IDisposable
class GridSettingsManager<TRow, TRowData, TPredicateBuilderArg, TPredicateBuilder, TSettingsData> :
ViewModelBase, IValueFactory<TSettingsData>, IObserver<TSettingsData>, IDisposable
where TRow : IRowVM
where TPredicateBuilder : IPredicateBuilder<TRowData, TPredicateBuilderArg>, new()
where TSettingsData : class, IGridSettingsWithNotifications<TRowData, TPredicateBuilder> {
@@ -14,7 +14,7 @@ namespace mbts.spark.filters {
TimeFilterMode _mode;
TimeTextTuple _begin, _end;
DateTime? _begin_dt, _end_dt;
readonly DateTime _cme_open = MarketSchedule.get_cme_gen_open();
readonly DateTime _cme_open = MarketSchedule.get_start_of_trading_day();
public TimeFilterVM(Func<TRow, DateTime> get_row_val, string prop_name) : base(get_row_val, prop_name) =>
_set_default_values();
@@ -166,51 +166,6 @@
</Grid>
</DataTemplate>
</usp:OrderModificationTemplateSelector.TimeBoxOrderTemplate>
<usp:OrderModificationTemplateSelector.TickOrderTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="4" />
<RowDefinition Height="Auto"/>
<RowDefinition Height="4" />
<RowDefinition Height="Auto"/>
<RowDefinition Height="4" />
<RowDefinition Height="Auto"/>
<RowDefinition Height="4" />
<RowDefinition Height="Auto"/>
<RowDefinition Height="4" />
<RowDefinition Height="Auto"/>
<RowDefinition Height="4" />
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="12"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="1" VerticalAlignment="Center">Cross Thresh</TextBlock>
<uag:QuantityMeasureControl Grid.Row="1" Grid.Column="2" QuantityMeasure="{Binding cross_threshold}"/>
<TextBlock Grid.Row="3" VerticalAlignment="Center">Cross Ticks</TextBlock>
<c:TickUpDownUInt Grid.Row="3" Grid.Column="2" Value="{Binding cross_ticks}"/>
<TextBlock Grid.Row="5" VerticalAlignment="Center">Max Chase Ticks</TextBlock>
<c:TickUpDownUInt Grid.Row="5" Grid.Column="2" Value="{Binding max_chase_ticks}"/>
<CheckBox Grid.Row="7" Grid.Column="2" VerticalAlignment="Center" Margin="0,0,0,0" IsChecked="{Binding detach_on_toward}">Detach on Toward</CheckBox>
<TextBlock Grid.Row="9" VerticalAlignment="Center">Initial Tick Offset</TextBlock>
<c:TickUpDownUInt Grid.Row="9" Grid.Column="2" Value="{Binding tick_offset_on_deploy}"/>
<TextBlock Grid.Row="11">Child</TextBlock>
<uag:OrderDetailsPopupControl
Grid.Row="11" Grid.Column="2"
IsOpen="{Binding child_order.editable_properties_open, FallbackValue=False}"
ItemsSource="{Binding child_order_types}"
SelectedItem="{Binding child_order}"
HasEditableProperties="{Binding child_order.has_editable_properties, FallbackValue=False}"
/>
</Grid>
</DataTemplate>
</usp:OrderModificationTemplateSelector.TickOrderTemplate>
<usp:OrderModificationTemplateSelector.LeanOrderTemplate>
<DataTemplate>
<Grid>
@@ -379,16 +334,16 @@
Orientation="Horizontal"
Grid.Row="1"
>
<TextBlock VerticalAlignment="Center" >Hedge Ticks</TextBlock>
<c:HelpButton Margin="4,0,0,0" ToolTip="This is the number of ticks to offset the hedge." />
<TextBlock VerticalAlignment="Center" >Profit Ticks</TextBlock>
<c:HelpButton Margin="4,0,0,0" ToolTip="This is the number of ticks in your favor to offset the profit/hedge order." />
</StackPanel>
<c:TickUpDownInt Grid.Row="1" Grid.Column="2" Value="{Binding hedge_ticks}"/>
<StackPanel
Orientation="Horizontal"
Grid.Row="3"
>
<TextBlock VerticalAlignment="Center">Hedge</TextBlock>
<c:HelpButton Margin="4,0,0,0" ToolTip="This is the type of the hedge order to send for each fill of Child Order." />
<TextBlock VerticalAlignment="Center">Profit Order</TextBlock>
<c:HelpButton Margin="4,0,0,0" ToolTip="This is the type of the profit/hedge order to send for each fill of Child Order." />
</StackPanel>
<uag:OrderDetailsPopupControl
Grid.Row="3" Grid.Column="2"
@@ -415,6 +370,68 @@
</Grid>
</DataTemplate>
</usp:OrderModificationTemplateSelector.CoverOrderTemplate>
<usp:OrderModificationTemplateSelector.OCOOrderTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="4" />
<RowDefinition Height="Auto"/>
<RowDefinition Height="4" />
<RowDefinition Height="Auto"/>
<RowDefinition Height="4" />
<RowDefinition Height="Auto"/>
<RowDefinition Height="4" />
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="12"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<StackPanel Orientation="Horizontal" Grid.Row="1" >
<TextBlock VerticalAlignment="Center" >Stop Ticks</TextBlock>
<c:HelpButton Margin="4,0,0,0" ToolTip="This is the number of ticks away from the main order to start the 'stop' order." />
</StackPanel>
<c:TickUpDownInt Grid.Row="1" Grid.Column="2" Value="{Binding other_ticks}"/>
<StackPanel Orientation="Horizontal" Grid.Row="3" >
<TextBlock VerticalAlignment="Center" >Stop Side</TextBlock>
<c:HelpButton Margin="4,0,0,0" ToolTip="The Side of the 'stop' order" />
</StackPanel>
<ComboBox Grid.Row="3" Grid.Column="2" ItemsSource="{Binding other_side_types}" ItemTemplate="{StaticResource EnumDescriptionTemplate}" SelectedItem="{Binding other_side}"/>
<StackPanel
Orientation="Horizontal"
Grid.Row="5"
>
<TextBlock VerticalAlignment="Center">Stop Order</TextBlock>
<c:HelpButton Margin="4,0,0,0" ToolTip="This is the type of the 'stop' order to send." />
</StackPanel>
<uag:OrderDetailsPopupControl
Grid.Row="5" Grid.Column="2"
IsOpen="{Binding other_order.editable_properties_open, FallbackValue=False}"
ItemsSource="{Binding other_order_types}"
SelectedItem="{Binding other_order}"
HasEditableProperties="{Binding other_order.has_editable_properties, FallbackValue=False}"
/>
<StackPanel
Orientation="Horizontal"
Grid.Row="7"
>
<TextBlock VerticalAlignment="Center">Child</TextBlock>
<c:HelpButton Margin="4,0,0,0" ToolTip="This is the type of order to be placed when the algo is triggered." />
</StackPanel>
<uag:OrderDetailsPopupControl
Grid.Row="7" Grid.Column="2"
IsOpen="{Binding child_order.editable_properties_open, FallbackValue=False}"
ItemsSource="{Binding child_order_types}"
SelectedItem="{Binding child_order}"
HasEditableProperties="{Binding child_order.has_editable_properties, FallbackValue=False}"
/>
</Grid>
</DataTemplate>
</usp:OrderModificationTemplateSelector.OCOOrderTemplate>
<usp:OrderModificationTemplateSelector.OffsetOrderTemplate>
<DataTemplate>
<Grid>
@@ -773,9 +790,11 @@
</usp:DynamicOrderParamTemplateSelector>
</ItemsControl.ItemTemplateSelector>
</ItemsControl>
<TextBlock Grid.Row="3">Child</TextBlock>
<TextBlock Grid.Row="3"
Visibility="{Binding has_child, Converter={StaticResource BooleanToVisibilityConverter}}" >Child</TextBlock>
<uag:OrderDetailsPopupControl
Grid.Row="3" Grid.Column="2"
Grid.Row="3" Grid.Column="2"
Visibility="{Binding has_child, Converter={StaticResource BooleanToVisibilityConverter}}"
IsOpen="{Binding child_order.editable_properties_open, FallbackValue=False}"
ItemsSource="{Binding child_order_types}"
SelectedItem="{Binding child_order}"
@@ -69,6 +69,9 @@ namespace mbts.spark {
public static CustomOrderDefinition.CustomOneofCase order_type(this OrderRecordImmutable order) =>
order.HasDef && order.Def.HasCustomDef ? order.Def.CustomDef.CustomCase : CustomOrderDefinition.CustomOneofCase.None;
public static string dynamic_order_type_display_name(this OrderRecordImmutable order) =>
order.HasDef && order.Def.HasCustomDef && order.Def.CustomDef.HasDynamic ? order.Def.CustomDef.Dynamic.DynamicOrderType : string.Empty;
public static OrderSubType sub_type(this OrderRecordImmutable order) =>
order.HasDef ? order.Def.SubType : OrderSubType.None;
@@ -48,9 +48,6 @@ namespace mbts.spark {
}
#endregion
public static string display_name(this SecurityMD? security_md) =>
(security_md?.security).display_name();
public static string display_name(this SecurityImmutable? security) =>
security.HasValue ? security.Value.ToString() : "unknown security";
@@ -59,11 +56,11 @@ namespace mbts.spark {
public static SecurityImmutable? security(this SecurityMD? security_md) =>
security_md.HasValue ? new SecurityImmutable?(security_md.Value.security) : new SecurityImmutable?();
public static Venue exchange(this SecurityMD? security_md) =>
security_md.HasValue ? security_md.Value.security.Exchange : Venue.Unset;
public static Venue exchange_or_default(this SecurityImmutable? security) =>
security?.Exchange ?? Venue.Unset;
public static string symbol(this SecurityMD? security_md) =>
security_md.HasValue ? security_md.Value.security.Symbol : "unknown";
public static string symbol_or_default(this SecurityImmutable? security) =>
security?.Symbol ?? "unknown";
public static async Task<SecurityImmutable> get_security_async(IBus bus, uint security_key) =>
await bus.subscribe(SparkTags.SecuritiesTag).Where(map => map.ContainsKey(security_key)).Select(map => map[security_key].security).FirstAsync();
+11 -6
View File
@@ -134,8 +134,14 @@ namespace mbts.spark {
// Here we manage N orders to 1 price slot in the ladder
Dictionary<double, HashSet<uint>> orders_used_in_this_update = new Dictionary<double, HashSet<uint>>(_equality);
var ladders_orders = working.Values.Where(o => o.Def.SecurityKey == security.Id && (o.HasState && o.State.DisplayStatus == OrderState.Types.DisplayStatus.Visible));
foreach (var wo in ladders_orders) {
var ladders_orders =
working.Values
.Where(o =>
o.order.Def.SecurityKey == security.Id &&
(o.order.HasState && o.order.State.DisplayStatus == OrderState.Types.DisplayStatus.Visible));
foreach (var osec in ladders_orders) {
var wo = osec.order;
var slot_price_target = wo.price();
var order_type = wo.order_type();
var filled = wo.filled();
@@ -186,10 +192,9 @@ namespace mbts.spark {
var traversal_candidate = only_order;
bool exit_early = false;
while (!exit_early && traversal_candidate.ParentKey != 0) {
var found_parent = working.Where(o => o.Key == traversal_candidate.ParentKey);
if (found_parent.Count() == 1)
traversal_candidate = found_parent.First().Value;
else
if (working.TryGetFirst(o => o.Key == traversal_candidate.ParentKey, out var first))
traversal_candidate = first.Value.order;
else
exit_early = true;
}
if (!exit_early)
@@ -1,4 +1,5 @@
using bts.api;
using bts;
using bts.api;
using mbts.utils;
using System;
using System.ComponentModel.Composition;
@@ -8,18 +9,18 @@ namespace mbts.spark {
class PAndLModel : IDisposable {
readonly IBus _bus;
readonly OrdersModel _order_model;
//readonly AccountService _account_service;
readonly MDLookup _mdlookup;
IDisposable _dispose;
[ImportingConstructor]
private PAndLModel(IBus bus, OrdersModel order_model/*, AccountService account_service*/) {
_bus = bus;
_order_model = order_model;
//_account_service = account_service;
_mdlookup = new MDLookup(bus.subscribe(SparkTags.SecuritiesTag));
}
public IObservable<ITableUpdateTransform<PnLData, FillTupleKey>> on_pnl_data =>
_order_model.on_fill_tuples.convert(ft => new PnLData(ft)).table(pd => pd.key);
_order_model.on_fill_tuples.Select(ft => new PnLData(ft, _mdlookup.get_md(ft.security?.Id))).table(pd => pd.key);
//public async Task<Account[]> get_accounts() {
// var request = await _account_service.get_accounts_async(new GetAccountsRequest());
@@ -28,31 +28,10 @@ namespace mbts.spark {
static readonly int sod_hh = (int)config.Configuration.get<bts.config.SODTime>().sod_hh;
static readonly int sod_mm = (int)config.Configuration.get<bts.config.SODTime>().sod_mm;
static readonly TimeZoneInfo central_tzi = TimeZoneInfo.FindSystemTimeZoneById(cst_id);
static readonly DateTime _start_of_trading_day_utc;
static readonly DateTime _adjusted_central_sod;
static readonly DateTime unadjusted_central_sod = TimeZoneInfo.ConvertTimeFromUtc(
DateTime.UtcNow.Date.Add(new TimeSpan(sod_hh, sod_mm, 0)),
central_tzi
);
static MarketSchedule() {
var cme_schedule = config.Configuration.get<bts.md.CMETradingSchedule>();
_cme_gen_open_hrs = cme_schedule?.gen_open_hour ?? 8.5;
_adjusted_central_sod = !central_tzi.IsDaylightSavingTime(unadjusted_central_sod) ? unadjusted_central_sod.AddHours(1) : unadjusted_central_sod;
// Note that this isn't the acutal start of the trading day
// but it's close enough for pnl purposes.
// In reality, instruments have different schedules
// if now is less than the current sod, then we me move the sod back a day to account for the trading day start
if (_adjusted_central_sod >= TimeZoneInfo.ConvertTime(DateTime.Now, central_tzi)) {
_adjusted_central_sod = _adjusted_central_sod.AddDays(-1);
}
var local_time_zone_sod = TimeZoneInfo.ConvertTime(_adjusted_central_sod, central_tzi, TimeZoneInfo.Local);
_start_of_trading_day_utc = local_time_zone_sod.ToUniversalTime();
}
// for object initializers
public MarketSchedule() { }
@@ -73,6 +52,27 @@ namespace mbts.spark {
return last_open;
}
// DateTime? used here b/c we can't set a default parameter with DateTime.Now
// since it's not a compile-time constant
public static DateTime get_start_of_trading_day(DateTime? target_datetime = null, DateTimeKind dtk = DateTimeKind.Local ) {
if (target_datetime == null) target_datetime = DateTime.Now;
var dst_adjusted_central_sod = new DateTimeOffset(
new DateTime(target_datetime.Value.Date.Add(new TimeSpan(sod_hh, sod_mm, 0)).Ticks, DateTimeKind.Unspecified),
central_tzi.BaseUtcOffset
);
// if we're not currently past the start of the trading day, then the start must be yesterday
var temp = dst_adjusted_central_sod;
if (temp >= TimeZoneInfo.ConvertTime(target_datetime.Value, central_tzi)) {
temp = dst_adjusted_central_sod.AddDays(-1);
}
var local_time_zone_sod = TimeZoneInfo.ConvertTime(temp.DateTime, central_tzi, TimeZoneInfo.Local);
if (dtk == DateTimeKind.Utc) {
return local_time_zone_sod.ToUniversalTime();
}
return local_time_zone_sod;
}
public static DateTime get_utctimestamp_as_localtime(string s) {
return DateTimeOffset.ParseExact(s, "yyyyMMddHHmmssffffff", null, System.Globalization.DateTimeStyles.AssumeUniversal).ToLocalTime().LocalDateTime;
}
+9 -5
View File
@@ -28,19 +28,23 @@ namespace mbts.spark {
class PnLData {
readonly FillTuple _fill_tuple;
readonly SecurityMD? _md;
public PnLData(FillTuple fill_tuple) =>
public PnLData(FillTuple fill_tuple, SecurityMD? md) {
// It is entirely possible for the FillTuple to have a security but for no SecrityMD to be available. For example: expired security
_md = md;
_fill_tuple = fill_tuple;
}
public FillTupleKey key => _fill_tuple.key;
public FillImmutable fill => _fill_tuple.fill;
public SecurityMD? sec_md => _fill_tuple.security_md;
public SecurityMD? sec_md => _md;
public SecurityImmutable? security => _fill_tuple.security;
public OrderRecordImmutable? order => _fill_tuple.order;
public string account => _fill_tuple.account;
public Venue? exchange => sec_md?.security.Exchange;
public string symbol => sec_md?.security.Symbol;
public SecurityImmutable? security => sec_md?.security;
public Venue? exchange => security?.Exchange;
public string symbol => security?.Symbol;
public double buy_quantity => (fill.Side == Side.Buy) ? fill.LastShares : 0;
public double sell_quantity => (fill.Side == Side.Sell) ? fill.LastShares : 0;
public SecurityType? security_type => security?.security_type();
@@ -25,7 +25,6 @@ namespace mbts.spark.utilities {
static readonly OrderMetadata _default_order_metadata = new OrderMetadata { KeyBinding = "", Color = BTSColors.Orders.spreader_order };
static readonly CustomOrderDefinition.CustomOneofCase[]
// The following standard types are not yet ready for production use:
// CustomOrderDefinition.CustomOneofCase.Tick,
// CustomOrderDefinition.CustomOneofCase.Dynamic,
_standard_types = new[] {
CustomOrderDefinition.CustomOneofCase.Market,
@@ -37,6 +36,7 @@ namespace mbts.spark.utilities {
CustomOrderDefinition.CustomOneofCase.Lean,
CustomOrderDefinition.CustomOneofCase.Join,
CustomOrderDefinition.CustomOneofCase.Cover,
CustomOrderDefinition.CustomOneofCase.Oco,
CustomOrderDefinition.CustomOneofCase.Offset,
CustomOrderDefinition.CustomOneofCase.Top,
CustomOrderDefinition.CustomOneofCase.Trigger,
@@ -228,6 +228,15 @@ namespace mbts.spark.utilities {
Child = custom_order(),
Hedge = custom_order(),
};
public OCOOrderDefinition oco_order() => new OCOOrderDefinition
{
Child = custom_order(),
Other = custom_order(),
};
public PricerOrderDefinition pricer_order() => new PricerOrderDefinition
{
Child = custom_order(),
};
public OffsetOrderDefinition offset_order() => new OffsetOrderDefinition
{
Child = custom_order(),
@@ -262,6 +271,8 @@ namespace mbts.spark.utilities {
case CustomOrderDefinition.CustomOneofCase.Join: rv.Join = join_order(); break;
case CustomOrderDefinition.CustomOneofCase.Top: rv.Top = top_order(); break;
case CustomOrderDefinition.CustomOneofCase.Cover: rv.Cover = cover_order(); break;
case CustomOrderDefinition.CustomOneofCase.Oco: rv.Oco = oco_order(); break;
case CustomOrderDefinition.CustomOneofCase.Pricer: rv.Pricer = pricer_order(); break;
case CustomOrderDefinition.CustomOneofCase.Offset: rv.Offset = offset_order(); break;
case CustomOrderDefinition.CustomOneofCase.Trigger: rv.Trigger = trigger_order(); break;
case CustomOrderDefinition.CustomOneofCase.Spreader: rv.Spreader = spreader_order(); break;
@@ -285,6 +296,8 @@ namespace mbts.spark.utilities {
case CustomOrderDefinition.CustomOneofCase.Join: return new OrderMetadata { KeyBinding = "J", Color = BTSColors.Orders.join_order };
case CustomOrderDefinition.CustomOneofCase.Top: return new OrderMetadata { KeyBinding = "D", Color = BTSColors.Orders.top_order };
case CustomOrderDefinition.CustomOneofCase.Cover: return new OrderMetadata { KeyBinding = "C", Color = BTSColors.Orders.cover_order };
case CustomOrderDefinition.CustomOneofCase.Oco: return new OrderMetadata { KeyBinding = "C", Color = BTSColors.Orders.oco_order };
case CustomOrderDefinition.CustomOneofCase.Pricer: return new OrderMetadata { KeyBinding = "C", Color = BTSColors.Orders.pricer_order };
case CustomOrderDefinition.CustomOneofCase.Offset: return new OrderMetadata { KeyBinding = "O", Color = BTSColors.Orders.tick_order };
case CustomOrderDefinition.CustomOneofCase.Trigger: return new OrderMetadata { KeyBinding = "T", Color = BTSColors.Orders.trigger_order };
case CustomOrderDefinition.CustomOneofCase.Spreader: goto default;
@@ -306,6 +319,8 @@ namespace mbts.spark.utilities {
case OrderState.CustomOneofCase.Join: return new OrderMetadata { KeyBinding = "J", Color = BTSColors.Orders.join_order };
case OrderState.CustomOneofCase.Top: return new OrderMetadata { KeyBinding = "D", Color = BTSColors.Orders.top_order };
case OrderState.CustomOneofCase.Cover: return new OrderMetadata { KeyBinding = "C", Color = BTSColors.Orders.cover_order };
case OrderState.CustomOneofCase.Oco: return new OrderMetadata { KeyBinding = "C", Color = BTSColors.Orders.oco_order };
case OrderState.CustomOneofCase.Pricer: return new OrderMetadata { KeyBinding = "C", Color = BTSColors.Orders.pricer_order };
case OrderState.CustomOneofCase.Offset: return new OrderMetadata { KeyBinding = "O", Color = BTSColors.Orders.tick_order };
case OrderState.CustomOneofCase.Trigger: return new OrderMetadata { KeyBinding = "T", Color = BTSColors.Orders.trigger_order };
case OrderState.CustomOneofCase.Spreader: goto default;
@@ -345,6 +360,8 @@ namespace mbts.spark.utilities {
case nameof(JoinOrderDefinition): return CustomOrderDefinition.CustomOneofCase.Join;
case nameof(TopOrderDefinition): return CustomOrderDefinition.CustomOneofCase.Top;
case nameof(CoverOrderDefinition): return CustomOrderDefinition.CustomOneofCase.Cover;
case nameof(OCOOrderDefinition): return CustomOrderDefinition.CustomOneofCase.Oco;
case nameof(PricerOrderDefinition): return CustomOrderDefinition.CustomOneofCase.Pricer;
case nameof(OffsetOrderDefinition): return CustomOrderDefinition.CustomOneofCase.Offset;
case nameof(TriggerOrderDefinition): return CustomOrderDefinition.CustomOneofCase.Trigger;
case nameof(DynamicOrderDefinition): return CustomOrderDefinition.CustomOneofCase.Dynamic;
+49 -7
View File
@@ -64,15 +64,19 @@ namespace bts.api.core {
public Change<T> merge(Change<T> newer) {
return new Change<T>(Old, newer.New);
}
public void Deconstruct(out T Old, out T New) {
Old = this.Old;
New = this.New;
}
public override string ToString() {
return $"{{{Old}=>{New}}}";
}
}
public class Update<T, TKey> where TKey : IEquatable<TKey> {
private Func<T, TKey> _key;
readonly Func<T, TKey> _key;
internal Update(
IReadOnlyCollection<T> all,
IEnumerable<T> all,
IEnumerable<T> added,
IEnumerable<Change<T>> changes,
IEnumerable<T> removed,
@@ -122,7 +126,7 @@ namespace bts.api.core {
);
}
public IReadOnlyCollection<T> all { get; }
public IEnumerable<T> all { get; }
public IEnumerable<T> added { get; }
public IEnumerable<Change<T>> changes { get; }
public IEnumerable<T> changed => changes.Select(c => c.New);
@@ -208,11 +212,13 @@ namespace bts.api.core {
ImmutableDictionary<TKey, T> init,
IEnumerable<T> added,
IEnumerable<Change<T>> changed,
IEnumerable<T> removed) {
IEnumerable<T> removed,
Func<T,TKey> maybe_key_extractor) {
_state = init;
this.added = added;
this.changes = changed;
this.removed = removed;
this.key_extractor = maybe_key_extractor;
}
public IReadOnlyDictionary<TKey, T> state { get { return _state; } }
@@ -229,12 +235,13 @@ namespace bts.api.core {
foreach (var i in update.changed) {
builder[update.key(i)] = i;
}
return new TableUpdate<T, TKey>(builder.ToImmutableDictionary(), update.added, update.changes, update.removed);
return new TableUpdate<T, TKey>(builder.ToImmutableDictionary(), update.added, update.changes, update.removed, update.key_extractor);
}
internal TableUpdate<T, TKey> as_snapshot() {
return new TableUpdate<T, TKey>(_state, _state.Values.ToList(), new Change<T>[0], new T[0]);
return new TableUpdate<T, TKey>(_state, _state.Values.ToList(), new Change<T>[0], new T[0], key_extractor);
}
internal Func<T,TKey> key_extractor { get; private set; }
}
internal class LegacyTableUpdate<T, TKey> where TKey : IEquatable<TKey> {
@@ -283,7 +290,7 @@ namespace bts.api.core {
public static IObservable<TableUpdate<T,TKey>> Table<T,TKey>(this IObservable<Update<T,TKey>> source) where TKey : IEquatable<TKey> =>
source.Scan(
new TableUpdate<T,TKey>(ImmutableDictionary<TKey,T>.Empty, new T[0], new Change<T>[0], new T[0]),
new TableUpdate<T,TKey>(ImmutableDictionary<TKey,T>.Empty, new T[0], new Change<T>[0], new T[0], null),
(table, update) => TableUpdate<T,TKey>.update(table,update));
// Fold updates and emit at max 1/interval
@@ -415,5 +422,40 @@ namespace bts.api.core {
changed = diffs;
removed = rems;
}
// This method does everything lazily so it's cheap until the collections are enumerated
public static IObservable<Update<T2, TKey>>
Select<T1, T2, TKey>(this IObservable<Update<T1, TKey>> src, Func<T1, T2> map, Func<T2, TKey> key) where TKey : IEquatable<TKey> {
return src.Select(update => {
return new Update<T2, TKey>(
update.all.Select(map),
update.added.Select(map),
update.changes.Select(ch => new Change<T2>(map(ch.Old), map(ch.New))),
update.removed.Select(map),
key);
});
}
// Constructs a new dictionary so not as cheap as Select on the Update<> type
public static IObservable<TableUpdate<T2, TKey>>
Select<T1, T2, TKey>(this IObservable<TableUpdate<T1, TKey>> src, Func<T1, T2> map, Func<T2, TKey> key) where TKey : IEquatable<TKey> {
var cache = ImmutableDictionary<TKey, T2>.Empty;
return src.Select(update => {
var added = update.added.Select(map);
var removed = update.removed.Select(map);
var changes = update.changes.Select(ch => new Change<T2>(map(ch.Old), map(ch.New)));
var builder = cache.ToBuilder();
builder.RemoveRange(removed.Select(key));
builder.AddRange(added.Select(t => new KeyValuePair<TKey,T2>(key(t), t)));
foreach (var (o,n) in changes) {
Debug.Assert(key(o).Equals(key(n)));
builder[key(o)] = n;
}
cache = builder.ToImmutable();
return new TableUpdate<T2, TKey>(cache, added, changes, removed, key);
});
}
}
}
+13 -7
View File
@@ -15,20 +15,26 @@ using System.Reactive.Subjects;
namespace bts.md
{
[Flags]
enum MDSourceOptions {
DeferInit = 1,
}
class MDSource : IObserver<List<SecurityAndHandle>>, IDisposable
{
protected readonly MDConsumer _consumer = new MDConsumer();
protected readonly Co2RelayControl _relay;
protected long _version = 0;
protected DateTime _version = DateTime.MinValue;
protected readonly ConcurrentDictionary<SecurityKey, SecurityMD> _exchange_sec_map = new ConcurrentDictionary<SecurityKey, SecurityMD>();
protected List<IDisposable> _dispose = new List<IDisposable>();
protected readonly MDSourceOptions _options;
public MDSource(uint naggle_ms, bool defer_init = false)
{
public MDSource(uint naggle_ms, MDSourceOptions options = 0)
{
_options = options;
_relay = new Co2RelayControl(_consumer, naggle_ms);
_relay.preload_securities((co2.MDRequest.Types.Flags)0xff).ignore();
_dispose.Add(_consumer.on_security.Subscribe(this));
if(!defer_init)
if(!options.HasFlag(MDSourceOptions.DeferInit))
initialize();
}
@@ -57,18 +63,18 @@ namespace bts.md
var smd = new SecurityMD(new SecurityImmutable(v.security), v.handle);
_exchange_sec_map.AddOrUpdate(smd.security.Id, smd, (_, __) => smd);
}
_version += 1;
_version = DateTime.UtcNow;
}
}
public void Dispose() => Disposable.dispose(_dispose);
async protected Task run()
{
long run_version = 0;
DateTime run_version = DateTime.MinValue;
while (true)
{
var exch = _exchange_sec_map;
long current_version = _version;
var current_version = _version;
if (current_version > run_version)
{
run_version = current_version;
@@ -9,6 +9,14 @@ namespace System.Collections.Generic {
else update_value(key, value);
}
public static bool TryAdd<TKey, TValue>(this IDictionary<TKey, TValue> dict, TKey key, TValue value) {
if (dict.ContainsKey(key)) return false;
else {
dict[key] = value;
return true;
}
}
public static void AddOrUpdate<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, Func<TKey, TValue> add_value_factory, Func<TKey, TValue, TValue> update_value) {
if (!dictionary.TryGetValue(key, out TValue value))
dictionary.Add(key, add_value_factory(key));
@@ -77,5 +85,20 @@ namespace System.Collections.Generic {
/// <returns>true if the dictionary contains an element with the specified key, AND <paramref name="should_pop"/> returns true; otherwise, false.</returns>
public static bool TryPopValueIf<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, Func<TValue, bool> should_pop) =>
dictionary.TryGetValue(key, out TValue result) && should_pop(result) && dictionary.Remove(key);
public static T? TryGetValue<TKey, T>(this IDictionary<TKey, T> src, TKey key) where T : struct {
if (src.TryGetValue(key, out T value)) {
return value;
}
return null;
}
public static T? TryGetValue<TKey, T>(this IReadOnlyDictionary<TKey, T> src, TKey key) where T : struct {
if (src.TryGetValue(key, out T value)) {
return value;
}
return null;
}
}
}
@@ -65,5 +65,25 @@ namespace bts.test {
$"Expected: {item} in [{aa}]. {msg}", file, line);
}
}
public static void throws<T>(Action runme, string msg="", [CallerFilePath] string file="", [CallerLineNumber] int line=0) where T : Exception {
bool threw = false;
try {
runme();
}
catch (T) {
threw = true;
}
catch (Exception e) {
throw new AssertFailture(
$"Expected '{typeof(T).Name}' but threw '{e.GetType().Name}'", file, line);
}
if (!threw) {
throw new AssertFailture(
msg ?? $"Expected '{typeof(T).Name}' exception but ran successfully.", file, line);
}
}
}
}
@@ -0,0 +1,86 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace bts.collections {
public class FilteredDictionary<TKey, T> : IReadOnlyDictionary<TKey, T> {
readonly IReadOnlyDictionary<TKey, T> _dict;
readonly Func<T, bool> _filter;
int? _count;
public FilteredDictionary(IReadOnlyDictionary<TKey, T> src, Func<T, bool> filter) {
_dict = src;
_filter = filter;
_count = null;
}
public T this[TKey key] {
get {
var result = _dict[key];
if (_filter(result)) {
return result;
}
throw new KeyNotFoundException(key.ToString());
}
}
public IEnumerable<TKey> Keys => _dict.Where(kvp => _filter(kvp.Value)).Select(kvp => kvp.Key);
public IEnumerable<T> Values => _dict.Values.Where(_filter);
public int Count {
get {
if (_count.HasValue) return _count.Value;
_count = Values.Count();
return _count.Value;
}
}
public bool ContainsKey(TKey key) => Keys.Contains(key);
public IEnumerator<KeyValuePair<TKey, T>> GetEnumerator() {
return new FilteredEnumerator<KeyValuePair<TKey,T>>(_dict.GetEnumerator(), kvp => _filter(kvp.Value));
}
public bool TryGetValue(TKey key, out T value) {
if (_dict.TryGetValue(key, out var val)) {
if (_filter(val)) {
value = val;
return true;
}
}
value = default;
return false;
}
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
public class FilteredEnumerator<T> : IEnumerator<T> {
readonly Func<T, bool> _filter;
readonly IEnumerator<T> _src;
public FilteredEnumerator(IEnumerator<T> src, Func<T, bool> filter) {
_filter = filter;
_src = src;
}
public T Current => _src.Current;
object IEnumerator.Current => Current;
public void Dispose() => _src.Dispose();
public bool MoveNext() {
do {
var ok = _src.MoveNext();
if (!ok) return false;
} while (!_filter(_src.Current));
return true;
}
public void Reset() { _src.MoveNext(); }
}
}
@@ -0,0 +1,64 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace bts.collections {
internal class MappedDictionary<TKey, TSrc, TRes> : IReadOnlyDictionary<TKey, TRes> {
readonly IReadOnlyDictionary<TKey, TSrc> _dict;
readonly Func<TSrc, TRes> _map;
public MappedDictionary(IReadOnlyDictionary<TKey, TSrc> src, Func<TSrc, TRes> map) {
_dict = src;
_map = map;
}
public TRes this[TKey key] => _map(_dict[key]);
public IEnumerable<TKey> Keys => _dict.Keys;
public IEnumerable<TRes> Values => _dict.Values.Select(_map);
public int Count => _dict.Count;
public bool ContainsKey(TKey key) => _dict.ContainsKey(key);
public IEnumerator<KeyValuePair<TKey, TRes>> GetEnumerator() {
return new MappedEnumerator<KeyValuePair<TKey, TSrc>, KeyValuePair<TKey, TRes>>(
_dict.GetEnumerator(),
kvp => new KeyValuePair<TKey, TRes>(kvp.Key, _map(kvp.Value)));
}
public bool TryGetValue(TKey key, out TRes value) {
if (_dict.TryGetValue(key, out TSrc src)) {
value = _map(src);
return true;
}
value = default;
return false;
}
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
public class MappedEnumerator<TSrc, TRes> : IEnumerator<TRes> {
readonly IEnumerator<TSrc> _src;
readonly Func<TSrc, TRes> _map;
public MappedEnumerator(IEnumerator<TSrc> src, Func<TSrc, TRes> map) {
_src = src;
_map = map;
}
public TRes Current => _map(_src.Current);
object IEnumerator.Current => Current;
public void Dispose() { _src.Dispose(); }
public bool MoveNext() => _src.MoveNext();
public void Reset() { _src.Reset(); }
}
}
+30 -14
View File
@@ -1,8 +1,31 @@
# BTS Spark
Our offering consists of a client UI [Spark Client](https://www.bluetradingsystems.com/images/Images/spark/02.06.19/Ladder.png), a high performance professional futures front end, connected via our proprietary protocol over TCP to our Spark Server, which in turn connects to execution venues via their native protocols (typically FIX/TCP for execution and some proprietary protocol over UDP multicast for market data).
![alt text](https://www.bluetradingsystems.com/images/Images/api/API.png)
# BTS Spark Client SDK
#### Getting Started
We currently offer two API extension points. This repository, our BTS **Spark Client SDK** allows users to control our Spark Server externally, that is, out of process, using the exact same protocol our own Spark Client does. In other words, a client application developed via the Spark Client SDK, say a VB .NET UI, would be able to provide any functionality our own UI provides.
## Git
We currently offer bindings for the Spark protocol to C++, C# and Excel clients. Since the underlying transport is TCP, clients developed via the Spark Client SDK can be located anywhere in the world, as long as they can reach the Spark Server via TCP. We do require a VPN or a secure network, since the Spark Protocol is currently not yet encrypted.
Our second API extension point is intended for users that require adding functionality to our Spark Server. If you are interested in extending the server process through custom order types, market data sources, or execution gateways, please see the [Spark Server SDK](https://github.com/BlueTradingSystems/spark_sdk).
### In Depth
* [Interface Overview](doc/Overview.md)
* [Threading Notes](doc/Threading.md)
## Getting Started
### Git
* First clone the spark_client_sdk repo
@@ -11,7 +34,7 @@
```
## Build
### Build
* Use msbuild to target windows / .net 4.71
* Install studio here if you don't have it: https://visualstudio.microsoft.com/downloads/
* Use VSCode for editing, it's better: https://code.visualstudio.com/
@@ -24,7 +47,7 @@ spark_client_sdk\src\spark_sdk> msbuild
* spark_sdk binaries will be produced in the bin\Debug directory
## Connecting to Spark Server
### Connecting to Spark Server
* Start your exe using the -ConfigServer parameter to point at a running Spark Server
```
@@ -35,7 +58,7 @@ spark_client_sdk\src\spark_sdk> msbuild
include: "http://location:8000/config/"
```
## Examples
### Examples
* Make sure you can connect to a running server
* run spark_sdk.exe with no parameters to see help
```
@@ -58,22 +81,15 @@ Arguments:
* [Driver](src/spark_sdk/samples/SDKMain.cs)
## Excel
### Excel
```
spark_sdk\bin\Debug\net471> start spark_excel-addin64-packed.xll
```
* This will start excel and load the bts addin
* Check the [```excel```](excel/) directory for an example spreadsheet
## Notes
* Our client side API is based on Google's protocol buffers; the message types are all protos. https://developers.google.com/protocol-buffers/
* We extended the protoc compiler to generate c#, c++, excel and js bindings to the services provided by our server process (https://github.com/BlueTradingSystems/spark_sdk)
* ### Dotnet core
### Dotnet core
* Right now we only support c# Windows 10 clients, but the package targets both net471 and .net core 2.1.6, so in theory one could run a .net core or c++ client on linux with a little work.
+103
View File
@@ -0,0 +1,103 @@
## Spark Client SDK Overview
The high level description of our order entry interface is as follows:
```
service OrderService {
// RPCs
rpc create_order(Create) returns (Response)
rpc cancel_order(Cancel) returns (Response)
rpc update_order(Update) returns (Response)
// Streams
rpc orders (OrdersSub) returns (stream OrderRecord);
rpc fills (FillsSub) returns (stream Fill);
}
```
This description uses the Google Protocol Buffers syntax to define the server interface (we use our own little compiler to generate bindings to this interface in C#, C++ and Excel). Using this server interface we can create, cancel or update orders, using the Remote Procedure Calls (RPCs) defined these orders can be exchange native orders or our own high level algo orders, or new order types created by users via the Spark Server SDK). We can monitor order state by subscribing to the orders stream above, which produces a a stream of OrderRecord objects, each capturing the latest state of a particular order, as it goes through it life cycle.
An OrderRecord looks like this:
```
message OrderRecord {
uint32 key;
uint32 parent_key;
OrderDefinition def;
OrderState state;
}
```
We have an order key, a possibly null parent order key, an OrderDefinition record, which defines the basic elements of the order, namely its type, side, security, account, etc, and an OrderState record, which captures the latest state of the order:
```
message OrderState {
// Standard order state, shared by all order types
OrderStatus status ;
double price ;
uint32 quantity ;
uint32 filled_quantity;
double last_price;
double avg_price;
string last_msg;
google.protobuf.Timestamp created;
google.protobuf.Timestamp last_change;
// Custom order state, specific to the order type in question
oneof custom {
// native order types
MarketOrderState market;
LimitOrderState limit;
StopOrderState stop;
StopLimitOrderState stop_limit;
// algo order types
IcebergOrderState iceberg;
SpreaderOrderState spreader;
TimeBoxOrderState time_box;
TickOrderState tick;
LeanOrderState lean;
JoinOrderState join;
TopOrderState top;evident
TriggerOrderState trigger;
CoverOrderState cover;
OffsetOrderState offset;
DynamicOrderState dynamic;
}
}
```
This OrderState record consists of a common part, applicable to all order types, and a custom part that is specific to the particular order type the record is associated with. Most of the standard fields should be self-explanatory, the latest price, the latest quantity, the filled quantity, etc. The status enumerate field tells you which state the order is in. We currently define the following states:
```
enum OrderStatus {
// The order has been constructed but not acted on. Inactive orders
// can be abandoned without consequences. This state should only be
// used internally by the order manager, i.e. it should never appear
// in externally visible orders.
ORDER_STATUS_INACTIVE = 0;
// The order is currently working, i.e. its algorithm is operating.
ORDER_STATUS_WORKING = 1;
// The order has completed successfully and accomplished its
// goal. This is a terminal state.
ORDER_STATUS_COMPLETED = 2;
// The order was terminated orderly before it had a chance to
// complete. This is a terminal state.
ORDER_STATUS_CANCELED = 3;
// The order has encountered an unexpected condition from which it
// cannot recover. This a terminal state.
ORDER_STATUS_ERROR = 4;
// The last known state for this order was ORDER_STATUS_WORKING, but
// since then the execution gateway has signaled a disconnect. This
// is not a terminal state.
ORDER_STATUS_DISCONNECTED = 5;
// The order is initializiYou can ng. It cannot be modified at this
point.
ORDER_STATUS_PENDING_WORKING = 6;
// The order is invalid and was rejected by Spark (not the
// exchange), on creation. This is a terminal state.
ORDER_STATUS_REJECTED_BY_SPARK= 7;
}
```
### Threading
Check out our API Threading Doc [here](Threading.md).
+74
View File
@@ -0,0 +1,74 @@
# Client API Threading
BTS API provides two types of calls. There are RPC (remote procedure calls) which accept an argument and return a value and there are streams of tabular data which we call subscriptions. All communication to and from the server happens on a connection thread owned by the ServiceContext object and data must be marshaled back to the right context when the call is complete.
## RPC
### C#
All API calls provide both a synchronous and [asynchronous](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/async) version. If you are calling the API from a WPF or Windows Forms application you should always prefer the async version of the method because it will efficiently make the call and resume execution on the correct thread without blocking or lagging user interactions. The synchronous calls are intended for use in console applications where nothing else needs use of the thread until the data is available.
The async version of the method will return a `Result<T>` type which is used to signal if an error occurred during the call. You should test the Result.success to see if the call succeeded before using Result.value.
```csharp
var s = context.locate<api.user.AuthService>();
var result = await s.get_users_async(new GetUsersRequest());
if (result.success) {
Console.WriteLine(result.value);
}
else {
Console.WriteLine($"Error {result.error_message}");
}
```
### C++
All C++ API calls also provide both a blocking synchronous version and a callback based asynchronous version. In the asynchronous case the callback will execute on the ServiceContext thread and you will need to handle marshaling that data to the appropriate thread. Just like in c# the callback will receive a `Result<T>` which will indicate success or failure. `Result.value` returns an `std::shared_ptr` so you have control over the lifetime of the value.
## Subscriptions
BTS provides streaming updates for some types of data. These updates are incremental in nature and will provide a snapshot of the current state on a new subscription. Streaming updates use the [ReactiveX library](http://reactivex.io) to manage subscribing and unsubscribing. This library provides a rich set of operators that you can use to slide and dice the data streams based on your specific application requirements.
### C#
The subscription callback will arrive on the ServiceContext thread but it can be easily marshaled to the thread of your choice using the [ObserveOn](http://reactivex.io/documentation/operators/observeon.html) operator. For a WPF or Windows Forms application the most common use of this will be [ObserveOnDispatcher]() which will simply execute the callback in the context of the original calling thread. By your choice of where to place the `ObserveOnDispatcher` call in your reactive call chain you can choose whether other operators execute before after switching threads.
In this example the `Where` and `SelectMany` operators will execute on `ServiceContext` connection thread and only the resulting matches will be marshaled across to the calling thread. It is safe to unsubscribe by disposing `sub` from any thread.
```csharp
var orders = context.locate<OrderService>();
ObservableCollection<OrderRecord> orders_with_errors;
IDisposable sub =
orders
.subscribe_orders()
.SelectMany(update => update.added)
.Where(o => o.State.Status == OrderStatus.Error)
.ObserveOnDispatcher()
.Subscribe(o => {
orders_with_errors.Add(o);
});
```
### C++
The C++ subscription callbacks use the [RxCpp](https://github.com/ReactiveX/RxCpp) library which implements the [ReactiveX](http://reactivex.io) operators. The subscribe call will return an `rxcpp::composite_subscription` which implements [RAII](https://en.wikipedia.org/wiki/Resource_acquisition_is_initialization) and will unsubscribe when it is destroyed.
```cpp
#include "lib/api/public/ServiceContext.h"
#include "lib/api/services/OrderService.client.pb.h"
#include <rxcpp/rx.hpp>
#include <google/protobuf/util/json_util.h>
//...
bts::api::ServiceContext ctx;
auto client = ctx.locate<bts::api::orderman::OrderService>();
auto sub =
client.subscribe_orders().subscribe([=](const bts::api::orderman::OrdersUpdate& update) {
std::string result;
for(const auto& order : update.added()) {
auto status = google::protobuf::util::MessageToJsonString(*order, &result, {});
std::cout << "New order: " << result << std::endl;
}
});
```
+1
View File
@@ -61,6 +61,7 @@
<None Include="$(OutputPath)\net471\spark_excel_example_2018.xlsm" pack="true" PackagePath=".\excel\spark_excel_example_2018.xlsm" />
<None Include="$(OutputPath)\net471\spark_excel-addin64-packed.xll" pack="true" PackagePath=".\excel\spark_excel-addin64-packed.xll" />
<None Include="$(OutputPath)\net471\spark_excel-addin64.dna" pack="true" PackagePath=".\excel\spark_excel-addin64.dna" />
<None Include="doc/**" pack="true" PackagePath=".\doc\" />
</ItemGroup>
<Target Name="CopyProjectReferencesToPackage" DependsOnTargets="ResolveReferences" Condition= " '$(SparkRoot)' != '' ">
@@ -124,10 +124,16 @@ namespace bts.tests
over_limit_order_key = over_limit_order_result.value.OrderKey;
no_limit_order_key = no_limit_order_result.value.OrderKey;
bool waiting_for_orders = true;
var orders = _order_service.subscribei_orders().Subscribe(ords =>
{
waiting_for_orders = false;
IEnumerable<OrderRecordImmutable> filtered_orders = ords.all.Where(x => x.Key == under_limit_order_key || x.Key == over_limit_order_key || x.Key == no_limit_order_key);
Assert.IsTrue(filtered_orders.Count() == 3, "There should be 3 matching orders... there are not");
foreach(var o in filtered_orders)
{
if(o.Key == under_limit_order_key) {
@@ -142,6 +148,9 @@ namespace bts.tests
}
});
while(waiting_for_orders) {
await Task.Delay(100);
}
// Restore the Limits to the original values
bool success = false;
@@ -0,0 +1,85 @@
using bts.collections;
using System;
using System.Collections.Generic;
using System.Linq;
namespace bts.test {
[TestGroup]
public class FilteredDictionaryTests {
IEnumerable<int> range = Enumerable.Range(5,6);
Dictionary<int, int> source;
IReadOnlyDictionary<int, int> filtered;
public FilteredDictionaryTests() {
// Create a source dictionary
source = Enumerable.Range(1, 10).ToDictionary(x => x);
// create a filtered dictionary
filtered = new FilteredDictionary<int, int>(source, x => x >= 5);
}
void ignore<T>(T t) { }
[Test]
public void index() {
foreach (var i in Enumerable.Range(-10,20)) {
if (range.Contains(i)) {
Assert.AreEqual(i, filtered[i]);
}
else {
Assert.throws<KeyNotFoundException>(() => ignore(filtered[i]));
}
}
}
[Test]
public void keys() {
Assert.SequenceEqual(range, filtered.Keys);
}
[Test]
public void values() {
Assert.SequenceEqual(range, filtered.Values);
}
[Test]
public void count() {
Assert.AreEqual(range.Count(), filtered.Count);
Assert.AreEqual(range.Count(), filtered.Count());
}
[Test]
public void contains_key() {
foreach (var i in Enumerable.Range(-10, 20)) {
if (range.Contains(i)) {
Assert.IsTrue(filtered.ContainsKey(i));
}
else {
Assert.IsFalse(filtered.ContainsKey(i));
}
}
}
[Test]
public void enumerator() {
Assert.SequenceEqual(
range.Select(i => new KeyValuePair<int,int>(i, i)), filtered);
}
[Test]
public void try_get_value() {
foreach (var i in Enumerable.Range(-10, 20)) {
if (range.Contains(i)) {
Assert.IsTrue(filtered.TryGetValue(i, out var _));
}
else {
Assert.IsFalse(filtered.TryGetValue(i, out var _));
}
}
}
[Test]
public void FilterAndMap() {
string transform(int i) => (i * i).ToString();
var mapped = new MappedDictionary<int, int, string>(filtered, transform);
Assert.SequenceEqual(
range.Select(transform),
mapped.Values);
}
}
}
@@ -0,0 +1,71 @@
using bts.collections;
using System.Collections.Generic;
using System.Linq;
namespace bts.test {
[TestGroup]
public class MappedDictionaryTests {
IEnumerable<int> range = Enumerable.Range(1, 10);
Dictionary<int, int> source;
IReadOnlyDictionary<int, string> mapped;
public MappedDictionaryTests() {
// Create a source dictionary
source = range.ToDictionary(x => x);
// create a filtered dictionary
mapped = new MappedDictionary<int, int, string>(source, x => x.ToString());
}
[Test]
public void index() {
foreach(var i in range) {
Assert.AreEqual(i.ToString(), mapped[i]);
}
}
[Test]
public void keys() {
Assert.SequenceEqual(range, mapped.Keys);
}
[Test]
public void values() {
Assert.SequenceEqual(range.Select(x => x.ToString()), mapped.Values);
}
[Test]
public void count() {
Assert.AreEqual(range.Count(), mapped.Count);
}
[Test]
public void contains_key() {
foreach (var key in range) {
Assert.IsTrue(mapped.ContainsKey(key));
}
Assert.IsFalse(mapped.ContainsKey(range.Max() + 1));
Assert.IsFalse(mapped.ContainsKey(range.Min() - 1));
}
[Test]
public void enumerator() {
Assert.SequenceEqual(range, mapped.Select(kvp => kvp.Key));
Assert.SequenceEqual(
range.Select(x => x.ToString()),
mapped.Select(kvp => kvp.Value));
}
[Test]
public void try_get_value() {
foreach (var i in Enumerable.Range(-20, 20)) {
Assert.AreEqual(
range.Contains(i),
mapped.TryGetValue(i, out string _));
}
}
[Test]
public void MapAndFilter() {
var set = new HashSet<string>(new[] { "4", "5" });
var filtered = new FilteredDictionary<int, string>(mapped, set.Contains);
Assert.AreEqual(2, filtered.Count);
Assert.SequenceEqual(
Enumerable.Range(4, 2).Select(i => (i, i.ToString())),
filtered.Select(kvp => (kvp.Key, kvp.Value)));
}
}
}
+146 -47
View File
@@ -1,12 +1,12 @@
using bts.api.core;
using System.Collections.Generic;
using bts.test;
using System;
using System.Reactive.Linq;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Concurrency;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using System.Threading.Tasks;
namespace mbts.api.Test {
[TestGroup]
@@ -44,7 +44,7 @@ namespace mbts.api.Test {
static int id(Item i) => i.id;
Item mkitem() {
return new Item(++_id, Guid.NewGuid().ToString().Substring(0,8));
return new Item(++_id, Guid.NewGuid().ToString().Substring(0, 8));
}
IObservable<Update<Item, int>> validate_incremental(IObservable<Update<Item, int>> source) {
@@ -68,7 +68,7 @@ namespace mbts.api.Test {
// This will mostly be collapsing to add()
for (var count = 1; count < total; ++count) {
var orig = raw.Take(count).reconstitute(id).ToEnumerable().Last();
var nag = await
var nag = await
validate_incremental(
raw
.Take(count)
@@ -80,7 +80,7 @@ namespace mbts.api.Test {
// Do it again with TableUpdate
for (var count = 1; count < total; ++count) {
var orig = raw.Take(count).reconstitute(id).Table().ToEnumerable().Last();
var nag = await
var nag = await
validate_incremental(
raw
.Take(count)
@@ -94,35 +94,32 @@ namespace mbts.api.Test {
// collapse rolling window
var update_list = raw.reconstitute(id).ToEnumerable().ToList();
var window = 10;
for (int x = 0; x+window < total; x += 5) {
for (int x = 0; x + window < total; x += 5) {
var start = update_list[x];
var end = update_list[x + window];
var super_update = update_list.Skip(x).Take(window+1).Aggregate((u1, u2) => u1.merge(u2));
var super_update = update_list.Skip(x).Take(window + 1).Aggregate((u1, u2) => u1.merge(u2));
start.merge(super_update);
Assert.SequenceEqual(end.all, super_update.all);
}
}
void validate_filter(IObservable<RawUpdate<Item>> raw, IObservable<Update<Item, int>> filtered, Func<Item, bool> pred)
{
void validate_filter(IObservable<RawUpdate<Item>> raw, IObservable<Update<Item, int>> filtered, Func<Item, bool> pred) {
var total = raw.ToEnumerable().Count();
// Validate that update is right at each point
for (var count = 1; count < total; ++count)
{
var orig = raw.Take(count).reconstitute(id).ToEnumerable().Last();
for (var count = 1; count < total; ++count) {
var orig = raw.Take(count).reconstitute(id).ToEnumerable().Last();
var next = filtered.Take(count).ToEnumerable().Last();
Assert.That(next.all.All(pred));
Assert.That(next.added.All(pred));
Assert.That(next.removed.All(pred));
Assert.That(next.changed.All(pred));
Assert.That(next.changed.All(pred));
}
// Do it again with TableUpdate
for (var count = 1; count < total; ++count)
{
for (var count = 1; count < total; ++count) {
var orig = raw.Take(count).reconstitute(id).Table().ToEnumerable().Last();
var next = filtered.Take(count).ToEnumerable().Last();
Assert.That(next.added.All(pred));
@@ -132,36 +129,32 @@ namespace mbts.api.Test {
}
void validate_filter_sub(IObservable<RawUpdate<Item>> raw, IObservable<Update<Item, int>> filtered, BehaviorSubject<Func<Item, bool>> pred)
{
raw.reconstitute(x => x.id).CombineLatest(filtered, (r, f) => Tuple.Create(r, f)).Subscribe(tup =>
{
void validate_filter_sub(IObservable<RawUpdate<Item>> raw, IObservable<Update<Item, int>> filtered, BehaviorSubject<Func<Item, bool>> pred) {
raw.reconstitute(x => x.id).CombineLatest(filtered, (r, f) => Tuple.Create(r, f)).Subscribe(tup => {
Assert.That(tup.Item2.all.All(pred.Value));
Assert.That(tup.Item2.added.All(pred.Value));
Assert.That(!tup.Item2.removed.Any(pred.Value));
Assert.That(!tup.Item2.removed.Any(pred.Value));
Assert.That(tup.Item2.changed.All(pred.Value));
Dictionary<int, Item> rawd = tup.Item1.added.ToDictionary(x => x.id);
Dictionary<int, Item> fd = tup.Item2.added.ToDictionary(x => x.id);
foreach (var kv in rawd)
{
if(pred.Value(kv.Value))
foreach (var kv in rawd) {
if (pred.Value(kv.Value)) {
Assert.That(fd.ContainsKey(kv.Key));
else
}
else {
Assert.That(!fd.ContainsKey(kv.Key));
}
}
});
}
[Test]
public void test_filter()
{
var raw = Observable.Create<RawUpdate<Item>>(observer =>
{
public void test_filter() {
var raw = Observable.Create<RawUpdate<Item>>(observer => {
RawUpdate<Item> rup = new RawUpdate<Item>();
for (int x = 0; x < 50; ++x)
{
for (int x = 0; x < 50; ++x) {
rup.Clear();
var item = _item_pool[x];
rup.added_items.Add(item);
@@ -169,39 +162,35 @@ namespace mbts.api.Test {
observer.OnNext(rup);
}
var rand = new Random();
for (int r = 0; r < rand.Next(4, 30); r += rand.Next(1, 2))
{
for (int r = 0; r < rand.Next(4, 30); r += rand.Next(1, 2)) {
rup.Clear();
rup.removed_keys.Add(_item_pool[r].id);
observer.OnNext(rup);
}
rup.Clear();
for (int c = 31; c < rand.Next(32, 50); c += rand.Next(1, 2))
{
for (int c = 31; c < rand.Next(32, 50); c += rand.Next(1, 2)) {
var ch = new Item(_item_pool[c].id, _item_pool[c].name + "_change");
rup.changed_items.Add(ch);
rup.changed_keys.Add(ch.id);
rup.changed_keys.Add(ch.id);
}
observer.OnNext(rup);
observer.OnCompleted();
observer.OnCompleted();
return Disposable.empty;
});
Func<Item, bool> evens = (x => x.id % 2 == 0);
Func<Item, bool> odds = (x => x.id % 2 == 1);
var predeven = Observable.Create<Func<Item, bool>>(observer =>
{
observer.OnNext(evens);
var predeven = Observable.Create<Func<Item, bool>>(observer => {
observer.OnNext(evens);
observer.OnCompleted();
return Disposable.empty;
});
var filtered = raw.reconstitute(x=> x.id).Filter(predeven);
var filtered = raw.reconstitute(x => x.id).Filter(predeven);
validate_filter(raw, filtered, evens);
var predodd = Observable.Create<Func<Item, bool>>(observer =>
{
var predodd = Observable.Create<Func<Item, bool>>(observer => {
observer.OnNext(odds);
observer.OnCompleted();
return Disposable.empty;
@@ -212,13 +201,13 @@ namespace mbts.api.Test {
var sub= new BehaviorSubject<Func<Item, bool>>(evens);
var sub = new BehaviorSubject<Func<Item, bool>>(evens);
filtered = raw.reconstitute(x => x.id).Filter(sub);
validate_filter_sub(raw, filtered, sub);
sub.OnNext(odds);
sub.OnCompleted();
}
}
[Test]
public async Task test_add() {
@@ -249,7 +238,7 @@ namespace mbts.api.Test {
rup.added_keys.AddRange(_item_pool.Take(20).Select(id));
observer.OnNext(rup);
for (int x = 0; x < 50; x+=2) {
for (int x = 0; x < 50; x += 2) {
rup.Clear();
var item = _item_pool[x];
rup.removed_keys.Add(item.id);
@@ -347,11 +336,121 @@ namespace mbts.api.Test {
public void test_merge_begin_end_state() {
int key(string s) => s[0];
var empty = new string[0];
var a2 = new Update<string, int>(new[] { "a2" }, empty, new[] { new Change<string>("a1", "a2") }, empty, key);
var a2 = new Update<string, int>(new[] { "a2" }, empty, new[] { new Change<string>("a1", "a2") }, empty, key);
var a3 = new Update<string, int>(new[] { "a3" }, empty, new[] { new Change<string>("a2", "a3") }, empty, key);
var merged = a2.merge(a3);
Assert.SequenceEqual(new[] { new Change<string>("a1", "a3") }, merged.changes);
}
T id<T>(T t) => t;
[Test,Ignore("SPARK-1988")]
public void test_reconstitute_twice() {
var empty = new int[0];
RawUpdate<int> create(int val) => new RawUpdate<int>() { added_items = { val }, added_keys = { val } };
var input =
Enumerable.Range(1, 10)
.Select(create)
.ToObservable()
.reconstitute(id);
var expected = Enumerable.Range(1, 10).Select(i => Enumerable.Range(1, i)).SelectMany(id);
var a = input.ToEnumerable().SelectMany(u => u.all).ToList();
var b = input.ToEnumerable().SelectMany(u => u.all).ToList();
Assert.SequenceEqual(expected, a);
Assert.SequenceEqual(expected, b);
}
[Test]
public void test_select_update() {
var empty = new int[0];
RawUpdate<int> create(int val) => new RawUpdate<int>() { added_items = { val }, added_keys = { val } };
int square(int i) => i * i;
var input =
Enumerable.Range(1, 10)
.Select(create) // IEnumerable<RawUpdate>
.ToObservable() // IObservable<RawUpdate>
.reconstitute(id); // IObservable<Update>
var output =
input
.Select(square, id) // function under test
.ToEnumerable()
.ToList(); //REMOVE THIS WHEN SPARK-1988 is fixed.
var expected = Enumerable.Range(1, 10).Select(square);
Assert.SequenceEqual(expected, output.SelectMany(u => u.added));
var expected_all = Enumerable.Range(1, 10).Select(i => Enumerable.Range(1, i).Select(square)).SelectMany(id);
var accumulated = output.SelectMany(u => u.all).ToList();
Assert.SequenceEqual(expected_all, accumulated);
}
[Test]
public void test_select_tableupdate() {
var empty = new int[0];
RawUpdate<int> create(int val) => new RawUpdate<int>() { added_items = { val }, added_keys = { val } };
int square(int i) => i * i;
var input =
Enumerable.Range(1, 10)
.Select(create) // IEnumerable<RawUpdate>
.ToObservable() // IObservable<RawUpdate>
.reconstitute(id); // IObservable<Update>
var output =
input
.Table()
.Select(square, id) // function under test
.ToEnumerable()
.ToList(); //REMOVE THIS WHEN SPARK-1988 is fixed.
var expected = Enumerable.Range(1, 10).Select(square);
Assert.SequenceEqual(expected, output.SelectMany(u => u.added));
var expected_all = Enumerable.Range(1, 10).Select(i => Enumerable.Range(1, i).Select(square)).SelectMany(id);
var accumulated = output.SelectMany(u => u.state.Values).ToList();
Assert.SequenceEqual(expected_all, accumulated);
}
//public class Box {
// public int value;
// public override string ToString() {
// return value.ToString();
// }
//}
//[Test]
//public void test_scan() {
// var obs =
// Enumerable.Range(1, 5)
// .ToObservable()
// .Scan(
// new Box { value = 0 },
// (box, i) => {
// box.value += i;
// return box;
// });
// var a = obs.Select(box => box.value).ToEnumerable();
// var b = obs.Select(box => box.value).ToEnumerable();
// Assert.SequenceEqual(a, b);
//}
//[Test]
//public void test_scan2() {
// var obs =
// Enumerable.Range(1, 5)
// .ToObservable()
// .Scan<int, Func<Box>>(
// () => new Box { value = 0 },
// (getbox, i) => {
// var box = getbox();
// box.value += i;
// return () => box;
// });
// var a = obs.Select(box => box().value).ToEnumerable().ToList();
// var b = obs.Select(box => box().value).ToEnumerable().ToList();
// Assert.SequenceEqual(a, b);
//}
}
}
-1
View File
@@ -6,7 +6,6 @@ cp -r $2/files/* $1
for filename in $1/*; do
if [[ $filename == *.xz ]]; then
echo Decompressing $filename
unxz $filename
fi
done
+1 -1
View File
@@ -1 +1 @@
set(sources api/apicore.proto;api/apigateway.proto;api/apioptions.proto;api/core/ApiClientInterop.cpp;api/core/ApiServerInterop.cpp;api/public/ApiServer.cpp;api/public/ClientChannel.cpp;api/public/QuickbusClientChannel.cpp;api/public/ServerChannel.cpp;api/public/ServiceContext.cpp;api/service_helpers/AdminServiceUtils.cpp;api/services/AdminService.proto;api/services/ApiUnitTestService.proto;api/services/AuthService.proto;api/services/ErrorReportService.proto;api/services/HelloService.proto;api/services/MarketDataAuxiliaryService.proto;api/services/MarketDataRelay.proto;api/services/OrderService.proto;api/services/OutOfProcessURLListener.proto;api/services/PersistenceService.proto;api/services/PositionService.proto;api/services/ShellDiagnosticService.proto;api/services/ShellLatencyService.proto;api/services/SyntheticSecurityService.proto;api/test/ApiTests.cpp;api/test/HelloServiceServer.cpp;config/ConfigCheck.cpp;config/ConfigDownloader.cpp;config/ConfigErrorCollector.cpp;config/ConfigLog.cpp;config/ConfigPropertyEvaluator.cpp;config/ConfigValueQueue.cpp;config/ConfigurationException.cpp;config/ConfigurationState.cpp;config/PreprocessInputStream.cpp;config/config.cpp;config/defaults/MarketData.conf;config/defaults/Ports.conf;config/defaults/Symbols.conf;config/defaults/cme_reference_data.conf;config/defaults/edge.conf;config/defaults/jetfix.conf;config/defaults/logging.conf;config/defaults/orderman_cfg.conf;config/defaults/orders_fills_cleanup.conf;config/defaults/spreader.conf;config/defaults/testkeys.conf;config/defaults/threads.conf;config/defaults/toggles.conf;config/extensions/extensions.proto;config/invalid/invalid.proto;config/protos/ExchangeSimCfg.proto;config/protos/MarketData.proto;config/protos/Ports.proto;config/protos/Symbols.proto;config/protos/UI.proto;config/protos/cme_reference_data.proto;config/protos/jetfix.proto;config/protos/logging.proto;config/protos/mercury_cfg.proto;config/protos/orderman_cfg.proto;config/protos/orders_fills_cleanup.proto;config/protos/perflog_cfg.proto;config/protos/quickbus_cfg.proto;config/protos/spreader.proto;config/protos/system.proto;config/protos/testkeys.proto;config/protos/threads.proto;config/protos/toggles.proto;config/tests/HTTPServer.cpp;config/tests/config_ref_tests.cpp;config/tests/config_tests.cpp;config/text_format/text_format.cpp;domain_model/Security.cpp;domain_model/Security.proto;domain_model/SecurityTests.cpp;domain_model/tests/domain_tests.cpp;expression/BooleanGrammar.cpp;expression/ExpressionEvaluator.cpp;expression/ParserExceptions.cpp;expression/Value.cpp;expression/tests/expression_parser_tests.cpp;logging/LoggerHub.cpp;logging/Sink.cpp;logging/logging.cpp;logging/tests/logging_tests.cpp;md/Diag/MDPeriodicDumper.cpp;md/Diag/ShmDumper.cpp;md/MDConsumer.cpp;md/MDConsumerHandle.cpp;md/MDConsumerInterop.cpp;md/MDProducer.cpp;md/MDProducerHandle.cpp;md/MDRecoverableConsumer.cpp;md/MemorySegmentUtils.cpp;md/PlatformLoader.cpp;md/PriceSize.cpp;md/SecurityDB.cpp;md/SecurityIDRecorder.cpp;md/SettlementRecorder.cpp;md/Shm/FixedSecurity.cpp;md/SyntheticSecurityService.cpp;md/cme/MDP3Utils.cpp;md/common.cpp;md/protos/co2.proto;md/relay/QueryServer.cpp;md/relay/RelayClient.cpp;md/relay/RelayRecorder.cpp;md/test/Co2ManagedTestHelper.cpp;md/test/Co2TestInstrument.cpp;md/test/md2_tests.cpp;md/test/synthetic_book_merge_tests.cpp;os/os.cpp;os/tests/os_tests.cpp;quickbus/core/LocalNode.cpp;quickbus/core/Quickbus.cpp;quickbus/core/SubscriptionID.cpp;quickbus/core/TableID.cpp;quickbus/message_model/Update.cpp;quickbus/message_model/merge_updates.cpp;quickbus/network/QBConnection.cpp;quickbus/network/QBConnectionManager.cpp;quickbus/network/endpoint_description.cpp;quickbus/network/quickbus_msgs.proto;quickbus/pubsub/TablePublisher.cpp;quickbus/pubsub/TableSubscriber.cpp;quickbus/pubsub/tests/qb_pubsub_tests.cpp;quickbus/routing/INode.cpp;quickbus/routing/NodeID.cpp;quickbus/routing/Router.cpp;quickbus/rpc/RPCClient.cpp;quickbus/rpc/RPCServer.cpp;quickbus/rpc/RPCSessionID.cpp;quickbus/rpc/rpc_utils.cpp;quickbus/rpc/tests/qb_rpc_tests.cpp;quickbus/tests/quickbus_tests.cpp;sparkproto/SparkProtos.proto;testing/catch_test_runner.cpp;utils/AuthToken.cpp;utils/Dispatcher.cpp;utils/GrowableSPSCQueue.cpp;utils/HTTPStream.cpp;utils/PBUtils.cpp;utils/Puid.cpp;utils/SecureLogon.cpp;utils/SharedMemory.cpp;utils/Sqlite_utils.cpp;utils/Uuid.cpp;utils/datetime.cpp;utils/download.cpp;utils/file_utils.cpp;utils/init_spark_lib.cpp;utils/is_unittest_environment.cpp;utils/stats.cpp;utils/tests/AuthTokenTests.cpp;utils/thread_manager.cpp)
set(sources api/apicore.proto;api/apigateway.proto;api/apioptions.proto;api/core/ApiClientInterop.cpp;api/core/ApiServerInterop.cpp;api/public/ApiServer.cpp;api/public/ClientChannel.cpp;api/public/QuickbusClientChannel.cpp;api/public/ServerChannel.cpp;api/public/ServiceContext.cpp;api/service_helpers/AdminServiceUtils.cpp;api/services/AdminService.proto;api/services/ApiUnitTestService.proto;api/services/AuthService.proto;api/services/ErrorReportService.proto;api/services/HelloService.proto;api/services/MarketDataAuxiliaryService.proto;api/services/MarketDataRelay.proto;api/services/OrderService.proto;api/services/OutOfProcessURLListener.proto;api/services/PersistenceService.proto;api/services/PositionService.proto;api/services/ShellDiagnosticService.proto;api/services/ShellLatencyService.proto;api/services/SyntheticSecurityService.proto;api/test/ApiTests.cpp;api/test/HelloServiceServer.cpp;config/ConfigCheck.cpp;config/ConfigDownloader.cpp;config/ConfigErrorCollector.cpp;config/ConfigLog.cpp;config/ConfigPropertyEvaluator.cpp;config/ConfigValueQueue.cpp;config/ConfigurationException.cpp;config/ConfigurationState.cpp;config/PreprocessInputStream.cpp;config/config.cpp;config/defaults/MarketData.conf;config/defaults/Ports.conf;config/defaults/Symbols.conf;config/defaults/cme_reference_data.conf;config/defaults/edge.conf;config/defaults/jetfix.conf;config/defaults/logging.conf;config/defaults/orderman_cfg.conf;config/defaults/orders_fills_cleanup.conf;config/defaults/spreader.conf;config/defaults/testkeys.conf;config/defaults/threads.conf;config/defaults/toggles.conf;config/extensions/extensions.proto;config/invalid/invalid.proto;config/protos/ExchangeSimCfg.proto;config/protos/MarketData.proto;config/protos/Ports.proto;config/protos/Symbols.proto;config/protos/UI.proto;config/protos/cme_reference_data.proto;config/protos/jetfix.proto;config/protos/logging.proto;config/protos/mercury_cfg.proto;config/protos/orderman_cfg.proto;config/protos/orders_fills_cleanup.proto;config/protos/perflog_cfg.proto;config/protos/quickbus_cfg.proto;config/protos/spreader.proto;config/protos/system.proto;config/protos/testkeys.proto;config/protos/threads.proto;config/protos/toggles.proto;config/tests/HTTPServer.cpp;config/tests/config_ref_tests.cpp;config/tests/config_tests.cpp;config/text_format/text_format.cpp;domain_model/Security.cpp;domain_model/Security.proto;domain_model/SecurityTests.cpp;domain_model/tests/domain_tests.cpp;expression/BooleanGrammar.cpp;expression/ExpressionEvaluator.cpp;expression/ParserExceptions.cpp;expression/Value.cpp;expression/tests/expression_parser_tests.cpp;jetfix/codec/FIXArray.cpp;jetfix/common/OutFIX.cpp;jetfix/connection/AsyncReader.cpp;jetfix/connection/IConnection.cpp;jetfix/connection/SocketConnection.cpp;jetfix/gateway/CoreGateway.cpp;jetfix/gateway/JetFIXGateway.cpp;jetfix/gateway/Session.cpp;jetfix/store/FIXFileStore.cpp;jetfix/store/session_name.cpp;jetfix/tests/async_reader_tests.cpp;jetfix/tests/decoding_tests.cpp;jetfix/tests/fix_encoding_stream_tests.cpp;jetfix/tests/fix_fragment_tests.cpp;jetfix/tests/jetfix_performance_tests.cpp;jetfix/tests/sample_fix.cpp;jetfix/tests/store_tests.cpp;logging/LoggerHub.cpp;logging/Sink.cpp;logging/logging.cpp;logging/tests/logging_tests.cpp;md/Diag/MDPeriodicDumper.cpp;md/Diag/ShmDumper.cpp;md/MDConsumer.cpp;md/MDConsumerHandle.cpp;md/MDConsumerInterop.cpp;md/MDProducer.cpp;md/MDProducerHandle.cpp;md/MDRecoverableConsumer.cpp;md/MemorySegmentUtils.cpp;md/PlatformLoader.cpp;md/PriceSize.cpp;md/SecurityDB.cpp;md/SecurityIDRecorder.cpp;md/SettlementRecorder.cpp;md/Shm/FixedSecurity.cpp;md/SyntheticSecurityService.cpp;md/cme/MDP3Utils.cpp;md/common.cpp;md/protos/co2.proto;md/relay/QueryServer.cpp;md/relay/RelayClient.cpp;md/relay/RelayRecorder.cpp;md/test/Co2ManagedTestHelper.cpp;md/test/Co2TestInstrument.cpp;md/test/md2_tests.cpp;md/test/synthetic_book_merge_tests.cpp;mercury/actors/IPollableActor.cpp;mercury/broker/Broker.cpp;mercury/broker/BrokerProxy.cpp;mercury/broker/ILocalBroker.cpp;mercury/broker/IRemoteBroker.cpp;mercury/common/mercury_common.cpp;mercury/events/TimerEvent.cpp;mercury/executor/Executor.cpp;mercury/topics/Topic.cpp;metrics/metrics.cpp;metrics/system_metrics.cpp;orderman/actors/AsyncFactory.cpp;orderman/actors/ExecutionGateway.cpp;orderman/actors/FillStore.cpp;orderman/actors/MDSupplementarySource.cpp;orderman/actors/OrderStore.cpp;orderman/actors/SODPosition.cpp;orderman/actors/SecuritySrc.cpp;orderman/actors/ServiceBridge.cpp;orderman/actors/order_manager/OrderBuilder.cpp;orderman/actors/order_manager/OrderManager.cpp;orderman/actors/order_manager/OrderServicesData.cpp;orderman/events/CancelRejectEvent.cpp;orderman/events/NewExecutionReportEvent.cpp;orderman/limiter/LimitStore.cpp;orderman/limiter/Limiter.cpp;orderman/limiter/ProductLimitNode.cpp;orderman/limiter/ProductLimitTree.cpp;orderman/limiter/SecurityLimitNode.cpp;orderman/limiter/SecurityLimitTree.cpp;orderman/limiter/tests/limit_tests.cpp;orderman/order_factory/DynaLine truncated
+1 -1
View File
@@ -1 +1 @@
set(sources api/apicore.proto;api/apigateway.proto;api/apioptions.proto;api/core/ApiClientInterop.cpp;api/core/ApiServerInterop.cpp;api/public/ApiServer.cpp;api/public/ClientChannel.cpp;api/public/QuickbusClientChannel.cpp;api/public/ServerChannel.cpp;api/public/ServiceContext.cpp;api/service_helpers/AdminServiceUtils.cpp;api/services/AdminService.proto;api/services/ApiUnitTestService.proto;api/services/AuthService.proto;api/services/ErrorReportService.proto;api/services/HelloService.proto;api/services/MarketDataAuxiliaryService.proto;api/services/MarketDataRelay.proto;api/services/OrderService.proto;api/services/OutOfProcessURLListener.proto;api/services/PersistenceService.proto;api/services/PositionService.proto;api/services/ShellDiagnosticService.proto;api/services/ShellLatencyService.proto;api/services/SyntheticSecurityService.proto;api/test/ApiTests.cpp;api/test/HelloServiceServer.cpp;config/ConfigCheck.cpp;config/ConfigDownloader.cpp;config/ConfigErrorCollector.cpp;config/ConfigLog.cpp;config/ConfigPropertyEvaluator.cpp;config/ConfigValueQueue.cpp;config/ConfigurationException.cpp;config/ConfigurationState.cpp;config/PreprocessInputStream.cpp;config/config.cpp;config/defaults/MarketData.conf;config/defaults/Ports.conf;config/defaults/Symbols.conf;config/defaults/cme_reference_data.conf;config/defaults/edge.conf;config/defaults/jetfix.conf;config/defaults/logging.conf;config/defaults/orderman_cfg.conf;config/defaults/orders_fills_cleanup.conf;config/defaults/spreader.conf;config/defaults/testkeys.conf;config/defaults/threads.conf;config/defaults/toggles.conf;config/extensions/extensions.proto;config/invalid/invalid.proto;config/protos/ExchangeSimCfg.proto;config/protos/MarketData.proto;config/protos/Ports.proto;config/protos/Symbols.proto;config/protos/UI.proto;config/protos/cme_reference_data.proto;config/protos/jetfix.proto;config/protos/logging.proto;config/protos/mercury_cfg.proto;config/protos/orderman_cfg.proto;config/protos/orders_fills_cleanup.proto;config/protos/perflog_cfg.proto;config/protos/quickbus_cfg.proto;config/protos/spreader.proto;config/protos/system.proto;config/protos/testkeys.proto;config/protos/threads.proto;config/protos/toggles.proto;config/tests/HTTPServer.cpp;config/tests/config_ref_tests.cpp;config/tests/config_tests.cpp;config/text_format/text_format.cpp;domain_model/Security.cpp;domain_model/Security.proto;domain_model/SecurityTests.cpp;domain_model/tests/domain_tests.cpp;expression/BooleanGrammar.cpp;expression/ExpressionEvaluator.cpp;expression/ParserExceptions.cpp;expression/Value.cpp;expression/tests/expression_parser_tests.cpp;logging/LoggerHub.cpp;logging/Sink.cpp;logging/logging.cpp;logging/tests/logging_tests.cpp;md/Diag/MDPeriodicDumper.cpp;md/Diag/ShmDumper.cpp;md/MDConsumer.cpp;md/MDConsumerHandle.cpp;md/MDConsumerInterop.cpp;md/MDProducer.cpp;md/MDProducerHandle.cpp;md/MDRecoverableConsumer.cpp;md/MemorySegmentUtils.cpp;md/PlatformLoader.cpp;md/PriceSize.cpp;md/SecurityDB.cpp;md/SecurityIDRecorder.cpp;md/SettlementRecorder.cpp;md/Shm/FixedSecurity.cpp;md/SyntheticSecurityService.cpp;md/cme/MDP3Utils.cpp;md/common.cpp;md/protos/co2.proto;md/relay/QueryServer.cpp;md/relay/RelayClient.cpp;md/relay/RelayRecorder.cpp;md/test/Co2ManagedTestHelper.cpp;md/test/Co2TestInstrument.cpp;md/test/md2_tests.cpp;md/test/synthetic_book_merge_tests.cpp;os/os.cpp;os/tests/os_tests.cpp;quickbus/core/LocalNode.cpp;quickbus/core/Quickbus.cpp;quickbus/core/SubscriptionID.cpp;quickbus/core/TableID.cpp;quickbus/message_model/Update.cpp;quickbus/message_model/merge_updates.cpp;quickbus/network/QBConnection.cpp;quickbus/network/QBConnectionManager.cpp;quickbus/network/endpoint_description.cpp;quickbus/network/quickbus_msgs.proto;quickbus/pubsub/TablePublisher.cpp;quickbus/pubsub/TableSubscriber.cpp;quickbus/pubsub/tests/qb_pubsub_tests.cpp;quickbus/routing/INode.cpp;quickbus/routing/NodeID.cpp;quickbus/routing/Router.cpp;quickbus/rpc/RPCClient.cpp;quickbus/rpc/RPCServer.cpp;quickbus/rpc/RPCSessionID.cpp;quickbus/rpc/rpc_utils.cpp;quickbus/rpc/tests/qb_rpc_tests.cpp;quickbus/tests/quickbus_tests.cpp;sparkproto/SparkProtos.proto;testing/catch_test_runner.cpp;utils/AuthToken.cpp;utils/Dispatcher.cpp;utils/GrowableSPSCQueue.cpp;utils/HTTPStream.cpp;utils/PBUtils.cpp;utils/Puid.cpp;utils/SecureLogon.cpp;utils/SharedMemory.cpp;utils/Sqlite_utils.cpp;utils/Uuid.cpp;utils/datetime.cpp;utils/download.cpp;utils/file_utils.cpp;utils/init_spark_lib.cpp;utils/is_unittest_environment.cpp;utils/stats.cpp;utils/tests/AuthTokenTests.cpp;utils/thread_manager.cpp)
set(sources api/apicore.proto;api/apigateway.proto;api/apioptions.proto;api/core/ApiClientInterop.cpp;api/core/ApiServerInterop.cpp;api/public/ApiServer.cpp;api/public/ClientChannel.cpp;api/public/QuickbusClientChannel.cpp;api/public/ServerChannel.cpp;api/public/ServiceContext.cpp;api/service_helpers/AdminServiceUtils.cpp;api/services/AdminService.proto;api/services/ApiUnitTestService.proto;api/services/AuthService.proto;api/services/ErrorReportService.proto;api/services/HelloService.proto;api/services/MarketDataAuxiliaryService.proto;api/services/MarketDataRelay.proto;api/services/OrderService.proto;api/services/OutOfProcessURLListener.proto;api/services/PersistenceService.proto;api/services/PositionService.proto;api/services/ShellDiagnosticService.proto;api/services/ShellLatencyService.proto;api/services/SyntheticSecurityService.proto;api/test/ApiTests.cpp;api/test/HelloServiceServer.cpp;config/ConfigCheck.cpp;config/ConfigDownloader.cpp;config/ConfigErrorCollector.cpp;config/ConfigLog.cpp;config/ConfigPropertyEvaluator.cpp;config/ConfigValueQueue.cpp;config/ConfigurationException.cpp;config/ConfigurationState.cpp;config/PreprocessInputStream.cpp;config/config.cpp;config/defaults/MarketData.conf;config/defaults/Ports.conf;config/defaults/Symbols.conf;config/defaults/cme_reference_data.conf;config/defaults/edge.conf;config/defaults/jetfix.conf;config/defaults/logging.conf;config/defaults/orderman_cfg.conf;config/defaults/orders_fills_cleanup.conf;config/defaults/spreader.conf;config/defaults/testkeys.conf;config/defaults/threads.conf;config/defaults/toggles.conf;config/extensions/extensions.proto;config/invalid/invalid.proto;config/protos/ExchangeSimCfg.proto;config/protos/MarketData.proto;config/protos/Ports.proto;config/protos/Symbols.proto;config/protos/UI.proto;config/protos/cme_reference_data.proto;config/protos/jetfix.proto;config/protos/logging.proto;config/protos/mercury_cfg.proto;config/protos/orderman_cfg.proto;config/protos/orders_fills_cleanup.proto;config/protos/perflog_cfg.proto;config/protos/quickbus_cfg.proto;config/protos/spreader.proto;config/protos/system.proto;config/protos/testkeys.proto;config/protos/threads.proto;config/protos/toggles.proto;config/tests/HTTPServer.cpp;config/tests/config_ref_tests.cpp;config/tests/config_tests.cpp;config/text_format/text_format.cpp;domain_model/Security.cpp;domain_model/Security.proto;domain_model/SecurityTests.cpp;domain_model/tests/domain_tests.cpp;expression/BooleanGrammar.cpp;expression/ExpressionEvaluator.cpp;expression/ParserExceptions.cpp;expression/Value.cpp;expression/tests/expression_parser_tests.cpp;logging/LoggerHub.cpp;logging/Sink.cpp;logging/logging.cpp;logging/tests/logging_tests.cpp;md/Diag/MDPeriodicDumper.cpp;md/Diag/ShmDumper.cpp;md/MDClient.cpp;md/MDConsumer.cpp;md/MDConsumerHandle.cpp;md/MDConsumerInterop.cpp;md/MDProducer.cpp;md/MDProducerHandle.cpp;md/MemorySegmentUtils.cpp;md/PlatformLoader.cpp;md/PriceSize.cpp;md/SecurityCache.cpp;md/SecurityDB.cpp;md/SecurityIDRecorder.cpp;md/SecurityOracle.cpp;md/SettlementRecorder.cpp;md/Shm/FixedSecurity.cpp;md/SyntheticSecurityService.cpp;md/cme/MDP3Utils.cpp;md/common.cpp;md/protos/co2.proto;md/relay/QueryServer.cpp;md/relay/RelayClient.cpp;md/relay/RelayRecorder.cpp;md/test/Co2ManagedTestHelper.cpp;md/test/Co2TestInstrument.cpp;md/test/md2_tests.cpp;md/test/synthetic_book_merge_tests.cpp;os/os.cpp;os/tests/os_tests.cpp;quickbus/core/LocalNode.cpp;quickbus/core/Quickbus.cpp;quickbus/core/SubscriptionID.cpp;quickbus/core/TableID.cpp;quickbus/message_model/Update.cpp;quickbus/message_model/merge_updates.cpp;quickbus/network/QBConnection.cpp;quickbus/network/QBConnectionManager.cpp;quickbus/network/endpoint_description.cpp;quickbus/network/quickbus_msgs.proto;quickbus/pubsub/TablePublisher.cpp;quickbus/pubsub/TableSubscriber.cpp;quickbus/pubsub/tests/qb_pubsub_tests.cpp;quickbus/routing/INode.cpp;quickbus/routing/NodeID.cpp;quickbus/routing/Router.cpp;quickbus/rpc/RPCClient.cpp;quickbus/rpc/RPCServer.cpp;quickbus/rpc/RPCSessionID.cpp;quickbus/rpc/rpc_utils.cpp;quickbus/rpc/tests/qb_rpc_tests.cpp;quickbus/tests/quickbus_tests.cpp;sparkproto/SparkProtos.proto;testing/catch_test_runner.cpp;utils/AuthToken.cpp;utils/Dispatcher.cpp;utils/GrowableSPSCQueue.cpp;utils/HTTPStream.cpp;utils/PBUtils.cpp;utils/Puid.cpp;utils/SecureLogon.cpp;utils/SharedMemory.cpp;utils/Sqlite_utils.cpp;utils/Uuid.cpp;utils/datetime.cpp;utils/download.cpp;utils/file_utils.cpp;utils/init_spark_lib.cpp;utils/is_unittest_environment.cpp;utils/stats.cpp;utils/tests/AuthTokenTests.cpp;utils/thread_manager.cpp)
+7 -1
View File
@@ -14,14 +14,20 @@ message ExpirationNameList {
bts.api.Product product = 1;
map<uint32,string> names = 2;
}
message ExpirationNameListReply {
repeated ExpirationNameList securities = 2;
}
message SecurityKeyList {
repeated uint32 keys = 1;
}
service MarketDataRelay {
option (bts.api.service_description)="Poll the market data service for updates";
rpc requestmd(bts.co2.MDRequest) returns (bts.co2.MDReply);
rpc request_products(bts.api.VoidMessage) returns (ProductListReply);
rpc request_names(bts.api.VoidMessage) returns (ExpirationNameListReply);
rpc get_securities(SecurityKeyList) returns (bts.api.SecurityList);
}
+17
View File
@@ -73,6 +73,8 @@ message CustomOrderDefinition
TriggerOrderDefinition trigger = 22;
CoverOrderDefinition cover = 23;
OffsetOrderDefinition offset = 24;
OCOOrderDefinition oco = 25;
PricerOrderDefinition pricer = 26;
DynamicOrderDefinition dynamic = 1000;
}
@@ -154,6 +156,17 @@ message OffsetOrderDefinition {
uint32 tick_offset_on_deploy = 1;
CustomOrderDefinition child = 6;
}
message OCOOrderDefinition {
CustomOrderDefinition other = 1;
int32 other_ticks = 2;
api.RelativeSide other_side = 4;
CustomOrderDefinition child = 3;
}
message PricerOrderDefinition {
int32 price_offset_ticks = 1;
double price_factor = 2;
CustomOrderDefinition child = 3;
}
message DynamicOrderDefinition {
string dynamic_order_type = 1;
map<string, DynamicOrderParam> order_params = 2;
@@ -203,6 +216,8 @@ message OrderState {
TriggerOrderState trigger = 21;
CoverOrderState cover = 22;
OffsetOrderState offset = 24;
OCOOrderState oco = 25;
PricerOrderState pricer = 26;
DynamicOrderState dynamic = 255; // do not use anything bigger than 255, or things will break
}
@@ -276,6 +291,8 @@ message TopOrderState {}
message TriggerOrderState {}
message CoverOrderState {}
message OffsetOrderState {}
message OCOOrderState {}
message PricerOrderState {}
message DynamicOrderState {}
enum OrderStatus {
+1 -1
View File
@@ -53,7 +53,7 @@ ConfigDownloader::Result ConfigDownloader::get(const string& url, stringstream&
curl_easy_setopt(_curl, CURLOPT_WRITEDATA, &out_stream);
curl_easy_setopt(_curl, CURLOPT_VERBOSE, 0);
curl_easy_setopt(_curl, CURLOPT_FAILONERROR, 1);
curl_easy_setopt(_curl, CURLOPT_CONNECTTIMEOUT, 30);
curl_easy_setopt(_curl, CURLOPT_CONNECTTIMEOUT, 6);
curl_easy_setopt(_curl, CURLOPT_SOCKOPTFUNCTION, no_linger);
CURLcode res = curl_easy_perform(_curl);
+20
View File
@@ -35,3 +35,23 @@ CMEOptions {
}
}
MDBootDelayWindow {
rule: "DeploymentType.type == 'dev'"
value {
delay_seconds: 0
}
}
MDBootDelayWindow {
value {
delay_seconds: 120
standard {
start: "16:30"
end: "16:40"
}
sunday {
start: "14:00"
end: "14:10"
}
}
}
+14 -2
View File
@@ -4,8 +4,9 @@ package bts.md;
message MarketDataConfig {
// Allows you to turn off in-proc market data entirely
optional bool use_md = 1 [default = true];
optional bool use_replay = 2 [default=false];
optional bool use_sim = 3 [default=true];
optional bool use_onixs = 2 [default=true];
optional bool use_replay = 3 [default=false];
optional bool use_sim = 4 [default=true];
}
message PlatformMap
@@ -14,6 +15,17 @@ message PlatformMap
optional string glbx_platform = 1 [default = "PLATFORM_SIM"];
}
// LOCAL TIME
message MDBootDelayWindow {
message TimeSpan {
optional string start = 1 [default = "16:30"];
optional string end = 2 [default = "16:40"];
}
optional uint32 delay_seconds = 1 [default = 120];
optional TimeSpan standard = 2;
optional TimeSpan sunday = 3;
}
//
// Options that apply to market data server
+12
View File
@@ -1,6 +1,7 @@
syntax="proto3";
package bts.api;
option csharp_namespace="bts.api";
option cc_enable_arenas=true;
import "lib/api/apioptions.proto";
enum Venue {
@@ -66,6 +67,14 @@ enum Side {
SIDE_SELL = 50;
}
enum RelativeSide {
RELATIVESIDE_UNDEFINED = 0;
RELATIVESIDE_OPPOSITE = 1;
RELATIVESIDE_SAME = 2;
RELATIVESIDE_BUY = 49;
RELATIVESIDE_SELL = 50;
}
enum PutOrCall {
PUTORCALL_PUT = 0;
PUTORCALL_CALL = 1;
@@ -163,6 +172,9 @@ message Security {
ICEExchangeData ice = 230;
}
}
message SecurityList {
repeated Security security = 1;
}
message Product {
bts.api.Venue exchange = 1;
+6 -4
View File
@@ -1,5 +1,6 @@
#include "MDPeriodicDumper.h"
#include "lib/md/MDConsumer.h"
#include "lib/md/SecurityCache.h"
#include "lib/utils/TimeStamp.h"
#include "lib/utils/Dispatcher.h"
#include "lib/utils/datetime.h"
@@ -25,7 +26,7 @@ namespace md {
struct Entry {
uint32_t seq;
MDConsumerHandle handle;
std::unique_ptr<api::Security> security;
SecurityPtr security;
};
std::string print_book(
@@ -158,12 +159,13 @@ void run_periodic_dumper(const std::set<UserSymbol>& symbols, uint32_t seq_mod,
auto thread_name = "tail-" + qualifier;
threading::configure_current_thread(thread_name);
MDConsumer consumer(symbols, nullptr);
SecurityCache cache;
MDConsumer consumer(cache, nullptr, &symbols);
// read all securities into a vector
std::vector<Entry> entries;
consumer.walk_security_list([&](const api::Security& sec, MDConsumerHandle handle) {
entries.push_back({ 0, handle, std::make_unique<api::Security>(sec) });
consumer.walk_security_list([&](const SecurityPtr& sec, MDConsumerHandle handle) {
entries.push_back({ 0, handle, sec });
});
if (!monitor_all) {
+96
View File
@@ -0,0 +1,96 @@
#include "MDClient.h"
#include "get_symbols.h"
#include "SecurityCache.h"
#include "SecurityOracle.h"
#include "lib/utils/TimeStamp.h"
#include <set>
namespace bts {
namespace md {
struct MDClientImpl{
utils::Dispatcher* _dispatcher;
api::ISyntheticSecurityService* _ss;
SecurityCache _cache;
std::unique_ptr<utils::DispatcherTimer> _security_timer;
std::unique_ptr<utils::DispatcherTimer> _recovery_timer;
std::set<UserSymbol> _symbols;
MDConsumer _consumer;
utils::Event<MDConsumer> _event;
MDClientImpl(
utils::Dispatcher* d,
api::ISyntheticSecurityService* s,
const std::set<UserSymbol>* symbols):
_dispatcher(d),
_ss(s),
_security_timer(),
_recovery_timer(),
_symbols(symbols ? *symbols : get_system_md_symbols()),
_consumer(_cache, _ss, &_symbols),
_event()
{
if (d) {
_security_timer =
std::make_unique<utils::DispatcherTimer>(*d, config::get<RelayOptions>().security_scan_interval(),[this]() { scan_securities(); }),
_recovery_timer =
std::make_unique<utils::DispatcherTimer>(*d, config::get<RelayOptions>().recovery_interval(), [this]() { check_heartbeats(); }),
_security_timer->start();
_recovery_timer->start();
}
}
void check_heartbeats() {
if (!_consumer.all_heartbeats_ok()) {
MDConsumer temp(_cache,_ss);
if (temp.all_heartbeats_ok()) {
_consumer.swap(temp);
LOG(md,Info, "Recovering from bad MD. Old consumer {} new consumer {}", _consumer.id(), temp.id());
// Subscribers are responsible for keeping the old consumer reference if they need it
_event.publish(_consumer);
}
}
}
size_t scan_securities() {
if (_consumer.all_heartbeats_ok()) {
TimeStamp start = TimeStamp::now();
auto count = _consumer.scan_securities();
auto delta = (TimeStamp::now() - start);
if (delta.milliseconds() > 10) {
LOG(md,Diag,"Scanned handles: {}ms found: {} securities. " ,
delta.milliseconds(),count);
}
return count;
}
return 0;
}
};
MDClient::MDClient(
utils::Dispatcher* d,
api::ISyntheticSecurityService* s,
const std::set<UserSymbol>* symbols) :
_self(new MDClientImpl(d,s, symbols))
{ }
MDClient::~MDClient() {
}
size_t MDClient::scan_securities() { return _self->scan_securities(); }
void MDClient::check_heartbeats() { return _self->check_heartbeats(); }
utils::EventSubscriber<MDConsumer>& MDClient::on_consumer() {
return _self->_event;
}
MDConsumer& MDClient::current_consumer() const {
return _self->_consumer;
}
SecurityOracle MDClient::oracle() const {
return SecurityOracle(*this);
}
std::set<UserSymbol> MDClient::symbols() const {
return _self->_symbols;
}
}}//namespace
+37
View File
@@ -0,0 +1,37 @@
#pragma once
#include "MDConsumer.h"
#include "lib/utils/Dispatcher.h"
#include "lib/utils/Event.h"
#include "ISyntheticSecurityService.h"
#include "UserSymbol.h"
#include <memory>
#include <set>
namespace bts {
namespace md {
class SecurityOracle;
struct MDClientImpl;
class MDClient {
std::unique_ptr<MDClientImpl> _self;
public:
// Will check for recovery
MDClient(
utils::Dispatcher* d=nullptr,
api::ISyntheticSecurityService* ss=nullptr,
const std::set<UserSymbol>* s=nullptr);
~MDClient();
// Check heartbeats and attempt recovery if they are bad
void check_heartbeats();
size_t scan_securities();
utils::EventSubscriber<MDConsumer>& on_consumer();
MDConsumer& current_consumer() const;
SecurityOracle oracle() const;
std::set<UserSymbol> symbols() const;
};
}}//namespace
+87 -59
View File
@@ -2,6 +2,7 @@
#include "MDConsumer.h"
#include "MemorySegmentUtils.h"
#include "PlatformLoader.h"
#include "SecurityCache.h"
#include "lib/config/config.h"
#include "lib/utils/SharedList.h"
#include "lib/utils/WaitFreeQueue.h"
@@ -39,15 +40,15 @@ bool match_symbol(const string& val, const string& b) {
return b.empty() || (val == b);
}
typedef shared_ptr<Security> PSecurity;
typedef shared_ptr<Security> MutSecurityPtr;
struct SecurityLess {
bool operator()(const PSecurity& left, const PSecurity& right) {
bool operator()(const SecurityPtr& left, const SecurityPtr& right) {
return left->id() < right->id();
}
};
PSecurity convert_psecurity(const FixedSecurity& security) {
PSecurity psec = make_shared<Security>();
MutSecurityPtr convert_psecurity(const FixedSecurity& security) {
auto psec = make_shared<Security>();
convert_security(security, psec.get());
return psec;
}
@@ -306,7 +307,7 @@ class SyntheticConsumerInst {
// LOCK only public methods
Mutex _workmutex;
shared_ptr<api::ISyntheticSecurityService> _ss;
api::ISyntheticSecurityService* _ss;
map<security_key, StrategyPtr> _data;
vector<shared_ptr<Security>> _pending;
set<security_key> _seen;
@@ -314,9 +315,10 @@ class SyntheticConsumerInst {
bool _initialized;
int _security_sub;
int _meta_sub;
std::vector<std::unique_ptr<spark::ExecutionStrategy>> _old_strategies;
public:
SyntheticConsumerInst(std::shared_ptr<api::ISyntheticSecurityService>& ss, const IGetHandle& gh) :
SyntheticConsumerInst(api::ISyntheticSecurityService* ss, const IGetHandle& gh) :
_workmutex(),
_ss(ss),
_data(),
@@ -325,7 +327,8 @@ class SyntheticConsumerInst {
_get_handle(gh),
_initialized(false),
_security_sub(0),
_meta_sub(0)
_meta_sub(0),
_old_strategies()
{
}
@@ -418,8 +421,13 @@ private:
// update security metadata
auto iter = _data.find(m.security_key());
if (iter != _data.end()) {
iter->second->strategy.CopyFrom(m.strategy());
auto updated = std::make_unique<spark::ExecutionStrategy>();
updated->CopyFrom(m.strategy());
iter->second->strategy.swap(updated);
iter->second->updated = TimeStamp::now();
// Keep the old strategy alive because it could be in use.
// metadata is changed so seldomly this is not a significant use of memory
if (updated != nullptr) _old_strategies.push_back(std::move(updated));
}
}
@@ -475,7 +483,7 @@ private:
if (legs.size() == (size_t)strategy.security->synthetic().legs_size()) {
auto s = make_shared<SyntheticStrategy>();
s->security.CopyFrom(*strategy.security);
s->strategy.CopyFrom(strategy.strategy);
s->strategy = std::make_unique<spark::ExecutionStrategy>(strategy.strategy);
s->legs = legs;
s->updated = TimeStamp::now();
result.insert(
@@ -523,8 +531,28 @@ private:
// Pimple for MDConsumer
///////////////////////////////////////////////////////////////////////////////////////////////////
class MDConsumerImpl {
typedef tbb::queuing_mutex Mutex;
Mutex _workmutex;
vector<shared_ptr<MDConsumerInst>> _instances;
std::shared_ptr<SyntheticConsumerInst> _sci;
map<security_key, shared_ptr<MDConsumerHandle>> _deferred_handles;
vector<SecurityCallback> _callbacks;
mutable SecurityCache _cache;
// co2::Platform _platform;
public:
MDConsumerImpl(const set<UserSymbol>& symbols, std::shared_ptr<api::ISyntheticSecurityService>& ss, bool shared, co2::Platform platform = co2::PLATFORM_UNSET ) {
MDConsumerImpl(
const set<UserSymbol>& symbols,
SecurityCache& cache,
api::ISyntheticSecurityService* ss,
bool shared, co2::Platform platform) :
_workmutex(),
_instances(),
_sci(),
_deferred_handles(),
_callbacks(),
_cache(cache)
{
std::ostringstream msg;
for (const auto& symbol : symbols) {
if (msg.tellp() > 0) { msg << ", "; }
@@ -556,16 +584,21 @@ public:
if (ss != nullptr) {
_sci = std::make_shared<SyntheticConsumerInst>(ss, [=](const set<security_key>& keys) { return get_handles(keys); });
}
// Add the SecurityCache to on_security so it can cache discovered securities
_callbacks.push_back([this](const SecurityPtr& security, MDConsumerHandle) {
_cache.add_live_security(security);
});
}
~MDConsumerImpl() { }
// This is a terrible name
size_t walk_sec_list(SecurityCallback cb, const string& symbol, SecurityType type) {
size_t result = 0;
vector<PSecurity> securities;
vector<MutSecurityPtr> securities;
for (auto& inst : _instances) {
if (match_symbol(inst->symbol(), symbol) && match_type(inst->type(), type)) {
result += inst->walk_sec_list([&](PSecurity security) { securities.push_back(security); }, symbol, type);
result += inst->walk_sec_list([&](MutSecurityPtr security) { securities.push_back(security); }, symbol, type);
}
}
auto complete_securities = fill_leg_details(securities);
@@ -573,7 +606,7 @@ public:
for (const auto& security : complete_securities) {
auto find_result = handles.find(security->id());
if (find_result != handles.end()) {
cb(*security, find_result->second);
cb(security, find_result->second);
}
}
return result;
@@ -602,7 +635,8 @@ public:
return empty_handle;
}
map<security_key, MDConsumerHandle> get_handles(const vector<PSecurity>& securities) {
template <class Security> //maybe const
map<security_key, MDConsumerHandle> get_handles(const vector<Security>& securities) {
set<security_key> keys;
for (const auto& sec : securities) {
keys.insert(sec->id());
@@ -641,8 +675,8 @@ public:
return handle;
}
vector<PSecurity> fill_leg_details(const vector<PSecurity>& securities, int depth=0) {
vector<PSecurity> result;
vector<SecurityPtr> fill_leg_details(const vector<MutSecurityPtr>& securities, int depth=0) {
vector<SecurityPtr> result;
if (securities.empty() || depth > 64) return result;
//
// Gather ids for legs
@@ -655,14 +689,14 @@ public:
}
}
// If there are no spreads everything is (by definition) resolved already
if (keys.empty()) return securities;
if (keys.empty()) return vector<SecurityPtr>{securities.begin(), securities.end()};
//
// gather handles for direct legs
auto handles = get_handles(keys);
//
// Some of the legs will themselves be spreads
vector<PSecurity> legs;
vector<MutSecurityPtr> legs;
legs.reserve(handles.size());
for (const auto& pair : handles) {
if (pair.second.is_initialized()) {
@@ -670,9 +704,8 @@ public:
}
}
// recurse to find details
legs = fill_leg_details(legs, depth+1);
map<security_key, PSecurity> leg_map;
for (auto& leg : legs) {
map<security_key, SecurityPtr> leg_map;
for (auto& leg : fill_leg_details(legs, depth+1)) {
leg_map.insert(make_pair(leg->id(), move(leg)));
}
@@ -705,7 +738,7 @@ public:
if (_callbacks.empty() && _deferred_handles.empty()) return 0;
// Call each _instance and pass it a callback
for (auto& inst : _instances) {
vector<PSecurity> found_securities;
vector<MutSecurityPtr> found_securities;
inst->scan_new_securities([&](const ColdEntry& entry) {
// Check if any deferred handles need this security (could be sped up with get_handles() but it isn't hot
auto iter = _deferred_handles.find(entry.security.key);
@@ -719,10 +752,11 @@ public:
if (resolve_legs && !found_securities.empty()) {
sort(found_securities.begin(), found_securities.end(), SecurityLess());
vector<PSecurity> resolved_securities = fill_leg_details(found_securities);
vector<SecurityPtr> resolved_securities = fill_leg_details(found_securities);
sort(resolved_securities.begin(), resolved_securities.end(), SecurityLess());
// Extract the list of unresolved securities
vector<PSecurity> unresolved_securities;
vector<SecurityPtr> unresolved_securities;
set_difference(found_securities.begin(), found_securities.end(),
resolved_securities.begin(), resolved_securities.end(),
inserter(unresolved_securities, unresolved_securities.begin()), SecurityLess());
@@ -748,7 +782,7 @@ public:
if (find_result != handles.end()) {
// Pass new securities to each callback
for (auto& cb : _callbacks) {
cb(*security, find_result->second);
cb(security, find_result->second);
}
}
else {
@@ -758,13 +792,13 @@ public:
}
count += resolved_securities.size();
}
else /*!resolve_legs*/ {
else /* do NOT resolve_legs*/ {
auto handles = get_handles(found_securities);
for (const auto& security : found_securities) {
auto find_result = handles.find(security->id());
if (find_result != handles.end()) {
for (auto& cb : _callbacks) {
cb(*security, find_result->second);
cb(security, find_result->second);
}
}
else {
@@ -788,11 +822,12 @@ public:
}
// call all subscribers
for (auto& cb : _callbacks) {
cb(security, handle);
cb(std::make_shared<Security>(security), handle);
}
}
}
_cache.flush_to_disk();
return count;
}
@@ -803,17 +838,20 @@ public:
// Note: We have to lock over the initial walk because if scan_securities() finds a new security concurrently with this walk
// it might mark it seen without this callback getting called.
for (auto& inst : _instances) {
vector<PSecurity> securities;
vector<MutSecurityPtr> securities;
inst->walk_old_securities([&](const ColdEntry& entry) {
securities.push_back(convert_psecurity(entry.security));
});
auto filled_securities = resolve_legs ? fill_leg_details(securities) : securities;
auto filled_securities =
resolve_legs ?
fill_leg_details(securities) :
vector<SecurityPtr>{securities.begin(), securities.end()};
auto handles = get_handles(filled_securities);
for (const auto& security : filled_securities) {
auto find_result = handles.find(security->id());
bts_assert_d(find_result != handles.end());
if (find_result != handles.end()) {
cb(*security, find_result->second);
cb(security, find_result->second);
}
}
}
@@ -822,7 +860,7 @@ public:
for (const auto& item : _sci->get_old_synthetics()) {
const auto& security = item->security;
auto handle = MDConsumerHandle(item);
cb(security, handle);
cb(std::make_shared<api::Security>(security), handle);
}
}
// store the callback
@@ -832,6 +870,10 @@ public:
scan_securities(resolve_legs);
}
SecurityPtr get_security(SecurityKey key) const {
return _cache.get_security(key);
}
MDConsumerChannel get_channel(const std::string& symbol, SecurityType type, Venue exchange) const {
for (auto& inst : _instances) {
if ((inst->symbol() == symbol && inst->type() == type && inst->exchange() == exchange) ||
@@ -858,14 +900,6 @@ public:
return result;
}
private:
typedef tbb::queuing_mutex Mutex;
Mutex _workmutex;
vector<shared_ptr<MDConsumerInst>> _instances;
std::shared_ptr<SyntheticConsumerInst> _sci;
map<security_key, shared_ptr<MDConsumerHandle>> _deferred_handles;
vector<SecurityCallback> _callbacks;
// co2::Platform _platform;
};
@@ -873,27 +907,20 @@ private:
// MDConsumer public interface, forwards to pimple
///////////////////////////////////////////////////////////////////////////////////////////////////
MDConsumer::MDConsumer(std::shared_ptr<api::ISyntheticSecurityService>& synt, bool shared) : _pimpl() {
set<UserSymbol> all_symbols;
get_system_md_symbols(VENUE_UNSET, SECURITYTYPE_UNSET, all_symbols);
MDConsumer::MDConsumer(
SecurityCache& cache,
api::ISyntheticSecurityService* ss,
const std::set<UserSymbol>* symbols,
bool shared,
co2::Platform platform) : _pimpl() {
_pimpl.reset(new MDConsumerImpl(all_symbols, synt, shared, co2::PLATFORM_UNSET));
}
MDConsumer::MDConsumer(const UserSymbol& s, std::shared_ptr<api::ISyntheticSecurityService> synt, bool shared) :
_pimpl()
{
set<UserSymbol> symbols;
symbols.insert(s);
_pimpl.reset(new MDConsumerImpl(symbols, synt, shared, co2::PLATFORM_UNSET));
}
MDConsumer::MDConsumer(set<UserSymbol> const& symbols, std::shared_ptr<api::ISyntheticSecurityService> s, bool shared, co2::Platform platform) :
_pimpl(new MDConsumerImpl(symbols, s, shared, platform)) {}
set<UserSymbol> all_symbols;
if (symbols == nullptr || symbols->empty()) {
all_symbols = get_system_md_symbols();
symbols = &all_symbols;
}
MDConsumer::MDConsumer(api::Venue ven, co2::Platform platform) : _pimpl() {
set<UserSymbol> all_symbols;
get_system_md_symbols(ven, SECURITYTYPE_UNSET, all_symbols);
std::shared_ptr<api::ISyntheticSecurityService> null = nullptr;
_pimpl.reset(new MDConsumerImpl(all_symbols, null, true, platform));
_pimpl.reset(new MDConsumerImpl(*symbols, cache, ss, shared, platform));
}
MDConsumer::~MDConsumer() {}
@@ -911,6 +938,7 @@ size_t MDConsumer::walk_security_list(SecurityCallback cb, const string& symbol,
size_t MDConsumer::scan_securities(bool resolve_legs) { return _pimpl->scan_securities(resolve_legs); }
void MDConsumer::on_security(SecurityCallback cb, bool resolve_legs) { _pimpl->on_security(cb, resolve_legs); }
const api::Security* MDConsumer::get_security(SecurityKey key) const { return _pimpl->get_security(key).get(); }
MDConsumerChannel MDConsumer::get_channel(const std::string& symbol, SecurityType type, Venue exchange) const {
return _pimpl->get_channel(symbol, type, exchange);
+14 -8
View File
@@ -7,6 +7,7 @@
#include "MDConsumerHandle.h"
#include "UserSymbol.h"
#include "ISyntheticSecurityService.h"
#include "SecurityPtr.h"
#include <functional>
#include <memory>
@@ -18,22 +19,23 @@
namespace bts {
namespace md {
/* typedef void (security_callback)(const Security& security); */
typedef std::function<void(const Security&, MDConsumerHandle handle)> SecurityCallback;
typedef std::function<void(const SecurityPtr&, MDConsumerHandle handle)> SecurityCallback;
using std::size_t;
class MDConsumerImpl;
class MDConsumerInst;
class MDConsumerChannel;
class SecurityCache;
class MDConsumer
{
public:
// Consumer for all symbols
MDConsumer(std::shared_ptr<api::ISyntheticSecurityService>&, bool shared=true);
MDConsumer(const UserSymbol&, std::shared_ptr<api::ISyntheticSecurityService>, bool shared=true);
// Consumer for a given set of symbols
MDConsumer(std::set<UserSymbol> const& symbols, std::shared_ptr<api::ISyntheticSecurityService>, bool shared=true, co2::Platform platform=co2::PLATFORM_UNSET);
MDConsumer(api::Venue, co2::Platform);
MDConsumer(
SecurityCache&,
api::ISyntheticSecurityService* ss,
const std::set<UserSymbol>* sym = nullptr,
bool shared=true,
co2::Platform platform=co2::PLATFORM_UNSET);
~MDConsumer();
bool all_heartbeats_ok() const;
@@ -51,6 +53,10 @@ public:
size_t scan_securities(bool resolve_legs = true);
void on_security(SecurityCallback cb, bool resolve_legs = true);
// Retrieve any security that has been seen in the past
// You still need to call scan_securities() periodically to discover new securities
const api::Security* get_security(SecurityKey key) const;
MDConsumerChannel get_channel(const std::string& symbol, SecurityType type, Venue exchange) const;
MDConsumerChannel get_channel(const UserSymbol&) const;
+13 -8
View File
@@ -16,7 +16,7 @@ MDConsumerHandle::MDConsumerHandle() {
_type = HANDLETYPE_VOID;
static_assert(sizeof(MDConsumerHandle) <= 32, "");
}
MDConsumerHandle::MDConsumerHandle(const std::shared_ptr<SyntheticStrategy>& def) {
MDConsumerHandle::MDConsumerHandle(const std::shared_ptr<const SyntheticStrategy>& def) {
memset(this, 0, sizeof(MDConsumerHandle));
synthetic.def = def;
_type = HANDLETYPE_SYNTHETIC;
@@ -223,6 +223,17 @@ void copy_synthetic_side(size_t count, api::Side side, const api::ContractSpecif
}
}
const spark::LegSpreaderParams& get_spreader_params(const SyntheticStrategy& synth, security_key key) {
auto strategy = synth.strategy.get();
if (strategy) {
auto iter = strategy->leg_params_map().find(key);
if (iter != strategy->leg_params_map().end()) {
return iter->second;
}
}
return spark::LegSpreaderParams::default_instance();
}
TimeStamp copy_synthetic_depth(const SyntheticStrategy& synth, size_t count, PriceSize* bid, PriceSize* ask) {
small_vector<DepthTraversal> bid_leg_travs;
small_vector<DepthTraversal> ask_leg_travs;
@@ -232,13 +243,7 @@ TimeStamp copy_synthetic_depth(const SyntheticStrategy& synth, size_t count, Pri
for (size_t x = 0; x < synth.legs.size(); ++x) {
std::array<PriceSize, COCONUT_MAX_DEPTH> bids, asks;
TimeStamp t;
auto key = synth.legs[x].key();
auto iter = synth.strategy.leg_params_map().find(key);
const spark::LegSpreaderParams& leg_params =
iter == synth.strategy.leg_params_map().end() ?
spark::LegSpreaderParams::default_instance() :
iter->second;
const spark::LegSpreaderParams& leg_params = get_spreader_params(synth, synth.legs[x].key());
auto mdtype = leg_params.market_data_type();
HandleReadResult r;
if (mdtype == spark::LegSpreaderParams::DIRECT) {
+3 -3
View File
@@ -14,7 +14,7 @@ namespace md {
class MDConsumerHandle;
struct SyntheticStrategy {
spark::ExecutionStrategy strategy;
std::unique_ptr<spark::ExecutionStrategy> strategy;
api::Security security;
std::vector<MDConsumerHandle> legs;
TimeStamp updated;
@@ -147,7 +147,7 @@ class BTS_EXPORT MDConsumerHandle
{
public:
MDConsumerHandle();
explicit MDConsumerHandle(const std::shared_ptr<SyntheticStrategy>&);
explicit MDConsumerHandle(const std::shared_ptr<const SyntheticStrategy>&);
~MDConsumerHandle();
MDConsumerHandle(const MDConsumerHandle& other);
@@ -335,7 +335,7 @@ private:
const ColdHeader* cheader;
};
struct SyntheticHandle {
std::shared_ptr<SyntheticStrategy> def;
std::shared_ptr<const SyntheticStrategy> def;
};
HandleType _type;
union {
+12 -16
View File
@@ -2,6 +2,7 @@
#include "common.h"
#include "UserSymbol.h"
#include "MDConsumer.h"
#include "SecurityCache.h"
#include "get_symbols.h"
#include "relay/RelayClient.h"
#include "lib/utils/Dispatcher.h"
@@ -26,17 +27,12 @@ struct BlitUserSymbol {
};
CAPI MDConsumer* create_md_consumer(BlitUserSymbol* usersymbols, uint32_t count, bool shared) {
std::shared_ptr<api::ISyntheticSecurityService> pnull = nullptr;
if (usersymbols == nullptr || count == 0) {
return new MDConsumer(pnull, shared);
}
else {
std::set<UserSymbol> symbols;
for (size_t x = 0; x < count; ++x) {
symbols.insert(UserSymbol(usersymbols[x].symbol, usersymbols[x].exchange, usersymbols[x].type));
}
return new MDConsumer(symbols, pnull, shared);
SecurityCache cache;
std::set<UserSymbol> symbols;
for (size_t x = 0; x < count; ++x) {
symbols.insert(UserSymbol(usersymbols[x].symbol, usersymbols[x].exchange, usersymbols[x].type));
}
return new MDConsumer(cache, nullptr/*synthetics*/, &symbols, shared);
}
CAPI void destroy_md_consumer(MDConsumer* consumer) {
@@ -64,10 +60,10 @@ typedef void(*security_cb)(security_key, const char*, size_t, MDConsumerHandle&
CAPI size_t md_consumer_walk_security_list(MDConsumer* consumer, security_cb cb) {
bts_assert(consumer);
std::string buffer;
return consumer->walk_security_list([&](const Security& security, MDConsumerHandle handle) {
return consumer->walk_security_list([&](const SecurityPtr& security, MDConsumerHandle handle) {
buffer.clear();
security.SerializePartialToString(&buffer);
cb(security.id(), buffer.c_str(), buffer.size(), handle);
security->SerializePartialToString(&buffer);
cb(security->id(), buffer.c_str(), buffer.size(), handle);
});
}
@@ -94,10 +90,10 @@ void safe_call_clr_callback(security_cb cb, security_key key, const char* buffer
CAPI void md_consumer_set_on_security(MDConsumer* consumer, security_cb cb) {
bts_assert(consumer);
consumer->on_security([=](const Security& security, MDConsumerHandle handle) {
consumer->on_security([=](const SecurityPtr& security, MDConsumerHandle handle) {
std::string buffer;
security.SerializePartialToString(&buffer);
safe_call_clr_callback(cb, security.id(), buffer.c_str(), buffer.size(), handle);
security->SerializePartialToString(&buffer);
safe_call_clr_callback(cb, security->id(), buffer.c_str(), buffer.size(), handle);
});
}
#endif
-157
View File
@@ -1,157 +0,0 @@
#ifndef NO_CPP0X
#include "MDRecoverableConsumer.h"
#include "PlatformLoader.h"
#include "lib/logging/logging.h"
#include <algorithm>
using namespace std;
namespace bts {
namespace md {
set<UserSymbol> get_symbols(Venue v=api::VENUE_UNSET);
MDRecoverableConsumer::MDRecoverableConsumer(std::shared_ptr<api::ISyntheticSecurityService> ss) :
_symbols(get_symbols()),
_ss(ss),
_current(init(_symbols, ss)),
_old(),
_callbacks(),
_seen(),
_lock ()
{}
MDRecoverableConsumer::MDRecoverableConsumer(Venue exchange, std::shared_ptr<api::ISyntheticSecurityService> ss) : // (visual studio 2012) MDRecoverableConsumer(get_symbols(exchange))
_symbols(get_symbols(exchange)),
_ss(ss),
_current(init(_symbols, ss)),
_old(),
_callbacks(),
_seen(),
_lock ()
{}
MDRecoverableConsumer::MDRecoverableConsumer(std::set<UserSymbol> symbols, std::shared_ptr<api::ISyntheticSecurityService> ss) :
_symbols(symbols),
_ss(ss),
_current(init(symbols, ss)),
_old(),
_callbacks(),
_seen(),
_lock ()
{
}
MDRecoverableConsumer::~MDRecoverableConsumer() {}
unique_ptr<MDRecoverableConsumer::MDConsumerGroup> MDRecoverableConsumer::init(std::set<UserSymbol>& symbols, std::shared_ptr<api::ISyntheticSecurityService>& ss) {
return std::make_unique<MDConsumerGroup>(symbols, ss);
}
size_t MDRecoverableConsumer::walk_security_list(SecurityCallback callback) {
return _current->consumer.walk_security_list(callback);
}
void MDRecoverableConsumer::on_security(SecurityCallback cb) {
// Add a filter to the callback to avoid calling the on_security callback with duplicates
auto filtered_cb = [this,cb](const Security& security, MDConsumerHandle handle) {
auto key = security.id();
if (_seen.find(key) == _seen.end()) {
_seen.insert(key);
cb(security, handle);
}
};
_current->consumer.on_security(filtered_cb);
Mutex::scoped_lock l (_lock);
_callbacks.push_back(filtered_cb);
}
size_t MDRecoverableConsumer::scan_securities() {
return _current->consumer.scan_securities();
}
std::shared_ptr<MDConsumerHandle> MDRecoverableConsumer::get_managed_handle(security_key key) {
Mutex::scoped_lock l (_lock);
auto handle = _current->consumer.get_managed_handle(key);
_current->handles.push_back(handle);
return handle;
}
bool MDRecoverableConsumer::all_heartbeats_ok() const {
return _current->consumer.all_heartbeats_ok();
}
bool MDRecoverableConsumer::try_recover() {
Mutex::scoped_lock l (_lock);
// Replace current with a new one (if the new one is better)
auto temp = init(_symbols, _ss);
if (!temp->consumer.all_heartbeats_ok()) return false;
std::swap(temp, _current);
// push current to old
_old.push_back(std::move(temp));
// copy old callbacks so we can reinstate them
auto old_callbacks = _callbacks;
// Add an on_security callback to current to check the old handles
l.release();
// re-instante old callbacks
for (auto &cb : old_callbacks) {
_current->consumer.on_security(cb);
}
_current->consumer.on_security([&](const Security& security, MDConsumerHandle newhandle) {
Mutex::scoped_lock l (_lock);
for (auto& group : _old) {
for (auto& handle : group->handles) {
if (handle->key() == security.id()) {
// update the handle! (if this is called guarunteed success)
*handle = newhandle;
bts_assert(handle->is_initialized());
_current->handles.push_back(move(handle));
}
}
}
cleanup();
});
return true;
}
// cleanup should only ever be called while _lock is locked
void MDRecoverableConsumer::cleanup() {
for (auto& group : _old) {
auto& handles = group->handles;
handles.erase(
remove_if(
handles.begin(),
handles.end(),
[](shared_ptr<MDConsumerHandle>& handle) { return handle == nullptr; }),
handles.end());
}
auto old_sz = _old.size ();
_old.erase(
remove_if(
_old.begin(),
_old.end(),
[](unique_ptr<MDConsumerGroup>& pgroup) -> bool { return pgroup->handles.empty(); }),
_old.end());
if (old_sz > _old.size ()) {
LOG (md,Info,"Cleaned up {} MDConsumer instances" ,old_sz - _old.size () );
}
}
//
// Utility functions
set<UserSymbol> get_symbols(Venue exchange) {
set<UserSymbol> all_symbols;
get_system_md_symbols(exchange, api::SECURITYTYPE_UNSET, all_symbols);
return all_symbols;
}
}}//namespace
#endif
-66
View File
@@ -1,66 +0,0 @@
#pragma once
#include "lib/utils/bts_no_copy.h"
#include "common.h"
#include "MDConsumer.h"
#include "MDConsumerHandle.h"
#include "UserSymbol.h"
#include <set>
#include <memory>
#include <tbb/mutex.h>
namespace bts {
namespace md {
class BTS_EXPORT MDRecoverableConsumer
{
public:
MDRecoverableConsumer(std::shared_ptr<api::ISyntheticSecurityService> ss);
// Map only a certain exchange for more granular recovery
MDRecoverableConsumer(Venue, std::shared_ptr<api::ISyntheticSecurityService> ss);
MDRecoverableConsumer(std::set<UserSymbol> symbols, std::shared_ptr<api::ISyntheticSecurityService> ss);
~MDRecoverableConsumer();
//
// Security functions
size_t walk_security_list(SecurityCallback callback);
void on_security(SecurityCallback cb);
// Scans for added securities - updates managed handles
size_t scan_securities();
//
// Data Access
std::shared_ptr<MDConsumerHandle> get_managed_handle(security_key key);
//
// Recover from publisher failure
bool all_heartbeats_ok() const;
bool try_recover();
private:
BTS_NO_COPY(MDRecoverableConsumer);
void cleanup();
struct MDConsumerGroup {
MDConsumerGroup(
std::set<UserSymbol>& symbols,
std::shared_ptr<api::ISyntheticSecurityService>& ss) : consumer(symbols, ss), handles() {}
MDConsumer consumer;
std::vector<std::shared_ptr<MDConsumerHandle>> handles;
};
std::unique_ptr<MDConsumerGroup> init(std::set<UserSymbol>& symbols, std::shared_ptr<api::ISyntheticSecurityService>& ss);
std::set<UserSymbol> _symbols;
std::shared_ptr<api::ISyntheticSecurityService> _ss;
std::unique_ptr<MDConsumerGroup> _current;
std::vector<std::unique_ptr<MDConsumerGroup>> _old;
std::vector<SecurityCallback> _callbacks;
std::set<security_key> _seen;
typedef tbb::mutex Mutex;
Mutex _lock;
};
}}//namespace
+6
View File
@@ -172,6 +172,12 @@ size_t get_system_md_symbols(const md::PlatformConfig &config,
return get_system_md_symbols(v, st, out_result);
}
std::set<UserSymbol> get_system_md_symbols() {
std::set<UserSymbol> all_symbols;
get_system_md_symbols(VENUE_UNSET, SECURITYTYPE_UNSET, all_symbols);
return all_symbols;
}
extern "C" BTS_EXPORT Platform platform_for_exchange(Venue v) {
const auto &pmap = config::get<PlatformMap>();
co2::Platform platform;
+91
View File
@@ -0,0 +1,91 @@
#include "SecurityCache.h"
#include "SecurityDB.h"
#include "lib/utils/Dispatcher.h"
#include <unordered_map>
#include <tbb/concurrent_queue.h>
#include <mutex>
namespace bts {
namespace md {
struct SecurityCacheImpl {
std::mutex cache_mutex;
std::unordered_map<SecurityKey, SecurityPtr> cache;
tbb::concurrent_queue<SecurityPtr> update_queue;
void enqueue_update_db(const SecurityPtr&);
void flush();
};
void SecurityCacheImpl::enqueue_update_db(const SecurityPtr& security) {
update_queue.push(security);
}
void SecurityCacheImpl::flush() {
if (!update_queue.empty()) {
std::vector<SecurityPtr> work;
SecurityPtr temp;
while(update_queue.try_pop(temp)) { work.push_back(temp); }
if (!work.empty()) {
if (db::insert_or_replace(work) == Result::error) {
// If we fail to insert then retry later
for (auto& s : work) update_queue.push(std::move(s));
}
}
}
}
//////////////////
SecurityCache::SecurityCache() : _me(std::make_shared<SecurityCacheImpl>()) {}
SecurityCache::~SecurityCache() {}
SecurityPtr SecurityCache::try_get_security(SecurityKey key) const {
std::lock_guard<std::mutex> lock(_me->cache_mutex);
auto iter = _me->cache.find(key);
if (iter != _me->cache.end()) {
return iter->second;
}
return nullptr;
}
SecurityPtr SecurityCache::get_security(SecurityKey key) {
std::unique_lock<std::mutex> lock(_me->cache_mutex);
auto iter = _me->cache.find(key);
if (iter != _me->cache.end()) {
return iter->second;
}
else {
// unlock for db read
lock.unlock();
auto security = db::find_security(key);
if (security) {
LOG(md,Info, "Loaded security from database: {} {}", security->symbol(), security->id());
return add_db_security(security);
}
return security;
}
}
SecurityPtr SecurityCache::add_db_security(const SecurityPtr& security) {
bts_assert(security);
std::lock_guard<std::mutex> lock(_me->cache_mutex);
SecurityKey key(security->id());
auto result = _me->cache.insert(std::make_pair(SecurityKey(security->id()), security));
// If result was inserted result.first->second is equal to security
// If the value was already present it must be a recently inserted 'live' security and we should use it
return result.first->second;
}
bool SecurityCache::add_live_security(const SecurityPtr& security) {
bts_assert(security);
std::lock_guard<std::mutex> lock(_me->cache_mutex);
SecurityKey key(security->id());
// Insert but don't overwrite existing
auto result = _me->cache.insert(std::make_pair(SecurityKey(security->id()), security));
_me->enqueue_update_db(security);
return result.second;
}
void SecurityCache::flush_to_disk() { _me->flush(); }
}}//ns
+36
View File
@@ -0,0 +1,36 @@
#pragma once
#include "lib/domain_model/Security.pb.h"
#include "MDConsumerHandle.h"
#include "lib/domain_model/SecurityKey.h"
#include "SecurityPtr.h"
#include <memory>
namespace bts {
namespace md {
// Get security on demand
// Store security+handle
struct SecurityCacheImpl;
class SecurityCache {
std::shared_ptr<SecurityCacheImpl> _me;
public:
SecurityCache();
~SecurityCache();
//Load security from cache with fallback to database
SecurityPtr get_security(SecurityKey key);
// Get the security if it's in the cache otherwise return nullptr. Does not query security database.
SecurityPtr try_get_security(SecurityKey key) const;
// returns true if the cache was updated
bool add_live_security(const SecurityPtr& security);
SecurityPtr add_db_security(const SecurityPtr& security);
void flush_to_disk();
};
}}//ns
+100 -86
View File
@@ -1,7 +1,9 @@
#include "SecurityDB.h"
#ifndef WIN32
#include "lib/utils/Sqlite_utils.h"
#include <SQLiteCpp/SQLiteCpp.h>
#include <sqlite/sqlite3.h>
#include "lib/config/config.h"
#include "lib/config/protos/system.pb.h"
#include "lib/md/Shm/FixedSecurity.h"
@@ -15,6 +17,7 @@
using std::string;
using namespace bts::utils;
using namespace SQLite;
namespace bts {
namespace md {
@@ -25,7 +28,7 @@ namespace db {
* key, created_date, expiration_date
*/
sqlite3_ptr open_db() {
std::unique_ptr<SQLite::Database> open_db() {
// Directory location on disk
auto db_dir = config::get<config::System>().data_path() + "databases/md/";
auto db_path = db_dir + "/securities.sqlite";
@@ -34,20 +37,14 @@ sqlite3_ptr open_db() {
}
// Create table if it does not exist
auto dbptr = sqlite::open_db(db_path.c_str());
bts_assert(dbptr);
auto db = dbptr.get();
sqlite3_busy_timeout(db, 9999);
sqlite::exec_nofail(db, "PRAGMA journal_mode=WAL");
sqlite::exec_nofail(db, "PRAGMA syncronous=0");
auto db = std::make_unique<Database>(db_path, OPEN_READWRITE | OPEN_CREATE);
db->setBusyTimeout(9999);
db->exec("PRAGMA journal_mode=WAL");
db->exec("PRAGMA syncronous=0");
db->exec("CREATE TABLE IF NOT EXISTS securities (key INTEGER primary key, symbol TEXT NOT NULL, venue INTEGER NOT NULL, type INTEGER NOT NULL, security_blob BLOB NOT NULL, expiration INTEGER NOT NULL);");
db->exec("CREATE TABLE IF NOT EXISTS metadata (key INTEGER PRIMARY KEY, data BLOB NOT NULL);");
sqlite::exec_nofail(db,
"CREATE TABLE IF NOT EXISTS securities (key INTEGER primary key, symbol TEXT NOT NULL, venue INTEGER NOT NULL, type INTEGER NOT NULL, security_blob BLOB NOT NULL, expiration INTEGER NOT NULL);");
sqlite::exec_nofail(db,
"CREATE TABLE IF NOT EXISTS metadata (key INTEGER PRIMARY KEY, data BLOB NOT NULL);");
return dbptr;
return db;
}
datetime::Date sec_expiration_cut_off()
@@ -59,44 +56,45 @@ datetime::Date sec_expiration_cut_off()
return datetime::Date(pt.date().year(),pt.date().month(),pt.date().day());
}
void apply_compatibilit_mutations(Security& security) {
// Migrate from old tick stuff
if (security.has_glbx()) {
if (security.has_glbx()) {
if (security.contract_spec().main_fraction() == 0) {
security.mutable_contract_spec()->set_main_fraction(security.glbx().do_not_use_main_fraction());
}
if (security.contract_spec().sub_fraction() == 0) {
security.mutable_contract_spec()->set_sub_fraction(security.glbx().do_not_use_sub_fraction());
}
if (security.contract_spec().price_display_format() == 0) {
security.mutable_contract_spec()->set_price_display_format(security.glbx().do_not_use_price_display_format());
}
}
}
}
size_t load_securities(
const std::set<UserSymbol>& symbols,
const std::function<void(const api::Security&)>& callback) {
size_t count = 0;
auto dbptr = open_db();
auto db = open_db();
// Load all existing securities and call the callback
auto db = dbptr.get();
auto select_stmt = sqlite::prepare_query(db, "select key,symbol,venue,type,security_blob from securities where expiration > ?");
auto select = select_stmt.get();
sqlite3_bind_int(select, 1, sec_expiration_cut_off().to_short());
auto result = sqlite3_step(select);
SQLite::Statement select_stmt(*db, "select key,symbol,venue,type,security_blob from securities where expiration > :exp");
select_stmt.bind(":exp", sec_expiration_cut_off().to_short());
Security security;
while (result == SQLITE_ROW) {
security_key security_key = sqlite3_column_int(select, 0);
auto symbol = std::string(
(char*)sqlite3_column_text(select, 1),
sqlite3_column_bytes(select, 1));
auto venue = (api::Venue)sqlite3_column_int(select, 2);
auto type = (api::SecurityType)sqlite3_column_int(select, 3);
while (select_stmt.tryExecuteStep() == SQLITE_ROW) {
security_key security_key = select_stmt.getColumn("key");
std::string symbol = select_stmt.getColumn("symbol");
api::Venue venue = (api::Venue)(int) select_stmt.getColumn("venue");
auto type = (api::SecurityType)(int) select_stmt.getColumn("type");
UserSymbol us(symbol, venue, type);
if (symbols.find(us) != symbols.end()) {
auto data = sqlite3_column_blob(select, 4);
auto len = sqlite3_column_bytes(select, 4);
// Migrate from old tick stuff
if (security.ParseFromArray(data, len)) {
if (security.has_glbx()) {
if (security.contract_spec().main_fraction() == 0) {
security.mutable_contract_spec()->set_main_fraction(security.glbx().do_not_use_main_fraction());
}
if (security.contract_spec().sub_fraction() == 0) {
security.mutable_contract_spec()->set_sub_fraction(security.glbx().do_not_use_sub_fraction());
}
if (security.contract_spec().price_display_format() == 0) {
security.mutable_contract_spec()->set_price_display_format(security.glbx().do_not_use_price_display_format());
}
}
auto blob = select_stmt.getColumn("security_blob");
if (security.ParseFromArray(blob, blob.getBytes())) {
apply_compatibilit_mutations(security);
bts_assert(security_key == security.id());
count++;
callback(security);
@@ -105,64 +103,85 @@ size_t load_securities(
LOG(md, Warn, "Failed to parse security from database. id = {}", security_key);
}
}
result = sqlite3_step(select);
}
return count;
}
void store_if_new(const std::vector<Security*>& securities) {
auto dbptr = open_db();
auto insert = sqlite::prepare_query(dbptr, "insert or ignore into securities (key, symbol, venue, type, security_blob, expiration) values (?,?,?,?,?,?)");
sqlite::exec_nofail(dbptr, "BEGIN TRANSACTION");
std::string buffer;
for (const api::Security* psecurity : securities) {
if (psecurity && psecurity->SerializeToString(&buffer)) {
SQLITE_CHK(sqlite3_bind_int(insert.get(), 1, psecurity->id()), dbptr);
SQLITE_CHK(sqlite3_bind_text(insert.get(), 2, psecurity->symbol().c_str(), psecurity->symbol().size(), SQLITE_STATIC), dbptr);
SQLITE_CHK(sqlite3_bind_int(insert.get(), 3, psecurity->exchange()), dbptr);
SQLITE_CHK(sqlite3_bind_int(insert.get(), 4, securitytype_of(*psecurity)), dbptr);
SQLITE_CHK(sqlite3_bind_blob(insert.get(), 5, buffer.c_str(), buffer.size(), SQLITE_STATIC), dbptr);
SQLITE_CHK(sqlite3_bind_int(insert.get(), 6, expiration(*psecurity).to_short()), dbptr);
sqlite3_step(insert.get());
sqlite3_reset(insert.get());
SecurityPtr find_security(SecurityKey key) {
try {
auto db = open_db();
SQLite::Statement select(*db, "select security_blob from securities where key = :key");
select.bind(":key", key.value());
if(select.tryExecuteStep() == SQLITE_ROW) {
auto result = std::make_shared<Security>();
auto blob = select.getColumn(0);
if(result->ParseFromArray(blob, blob.getBytes())) {
apply_compatibilit_mutations(*result);
return result;
}
}
}
sqlite::exec_nofail(dbptr, "COMMIT TRANSACTION");
catch(const SQLite::Exception& e) {
LOG(md,Error, "Error reading security database. {}", std::string(e.what()));
}
return nullptr;
}
Result insert_or_replace(const std::vector<SecurityPtr>& securities) {
try {
auto db = open_db();
SQLite::Statement insert(*db,"insert or replace into securities (key, symbol, venue, type, security_blob, expiration) values (?,?,?,?,?,?)");
SQLite::Transaction transaction(*db);
std::string buffer;
for (const auto& psecurity : securities) {
if (psecurity && psecurity->SerializeToString(&buffer)) {
insert.bind(1, psecurity->id());
insert.bind(2, psecurity->symbol());
insert.bind(3, psecurity->exchange());
insert.bind(4, securitytype_of(*psecurity));
insert.bind(5, buffer);
insert.bind(6, expiration(*psecurity).to_short());
insert.exec();
insert.reset();
}
}
transaction.commit();
}
catch(const SQLite::Exception& e) {
LOG(md,Warn, "Error writing to security db (retry pending): {}", std::string(e.what()));
return Result::error;
}
return Result::ok;
}
std::vector<api::synthetic::Metadata> load_metadata() {
std::vector<api::synthetic::Metadata> values;
auto dbptr = open_db();
auto db = dbptr.get();
auto select_stmt = sqlite::prepare_query(db, "select key,data from metadata");
auto select = select_stmt.get();
auto result = sqlite3_step(select);
while(result == SQLITE_ROW) {
uint32_t key = sqlite3_column_int(select, 0);
auto blob = sqlite3_column_blob(select, 1);
auto len = sqlite3_column_bytes(select, 1);
api::synthetic::Metadata data;
if (data.ParseFromArray(blob, len)) {
bts_assert(key == data.security_key());
values.push_back(data);
auto db = open_db();
SQLite::Statement select(*db, "select key,data from metadata");
while(select.tryExecuteStep() == SQLITE_ROW) {
uint32_t key = select.getColumn(0);
auto blob = select.getColumn(1);
api::synthetic::Metadata meta;
if (meta.ParseFromArray(blob, blob.getBytes())) {
bts_assert(key == meta.security_key());
values.push_back(meta);
}
else {
LOG(md, Warn, "Failed to parse security metadata for security_key={}", key);
}
result = sqlite3_step(select);
}
return values;
}
bool store_metadata(const api::synthetic::Metadata& data) {
auto dbptr = open_db();
auto insert = sqlite::prepare_query(dbptr, "insert or replace into metadata (key, data) values (?,?)");
auto db = open_db();
SQLite::Statement insert(*db, "insert or replace into metadata (key, data) values (?,?)");
SQLITE_CHK(sqlite3_bind_int(insert.get(), 1, data.security_key()), dbptr);
insert.bind(1, data.security_key());
std::string buffer = data.SerializeAsString();
SQLITE_CHK(sqlite3_bind_blob(insert.get(), 2, buffer.c_str(), buffer.size(), SQLITE_STATIC), dbptr);
auto result = sqlite3_step(insert.get());
insert.bind(2, buffer);
auto result = insert.tryExecuteStep();
if (result != SQLITE_DONE) {
LOG(md, Warn, "Failed to insert metadata. sqlite result {}", result);
return false;
@@ -180,13 +199,8 @@ size_t load_securities(
const std::function<void(const api::Security&)>& callback) {
return 0;
}
void store_if_new(const std::vector<Security*>& handles) {}
// std::vector<api::synthetic::Metadata> load_metadata() {
// std::vector<api::synthetic::Metadata> result;
// return result;
// }
// void store_metadata(const api::synthetic::Metadata&) {
// }
Result insert_or_replace(const std::vector<SecurityPtr>& handles) { return Result::error; }
SecurityPtr find_security(SecurityKey) { return nullptr; }
#endif
+6 -1
View File
@@ -3,10 +3,13 @@
#include "lib/domain_model/common.h"
#include "lib/domain_model/Security.pb.h"
#include "lib/utils/datetime.h"
#include "lib/utils/ResultEnum.h"
#include "lib/md/MDProducerHandle.h"
#include "lib/api/services/SyntheticSecurityService.pb.h"
#include "common.h"
#include "UserSymbol.h"
#include "lib/domain_model/SecurityKey.h"
#include "SecurityPtr.h"
#include <set>
#include <vector>
@@ -25,7 +28,9 @@ size_t load_securities(
const std::set<UserSymbol>& symbols,
const std::function<void(const api::Security&)>& callback);
void store_if_new(const std::vector<Security*>& handles);
SecurityPtr find_security(SecurityKey);
Result insert_or_replace(const std::vector<SecurityPtr>&);
// metadata
std::vector<api::synthetic::Metadata> load_metadata();
Loaded 100 of 160 files, more files were not shown because too many files have changed in this diff. Show more