diff --git a/.gitignore b/.gitignore index 5fe2ccc4d..e0769acc0 100644 --- a/.gitignore +++ b/.gitignore @@ -24,5 +24,7 @@ project.lock.json src/server/sparkd/orderman/perf_analysis/data src/server/sparkd/orderman/perf_analysis/logs/ src/server/sparkd/orderman/perf_analysis/trace_data/ +.fake +*.fsx.lock src/lib/orderman/perf_analysis/logs/latency_data/ trace_data/ diff --git a/.vscode/launch.json b/.vscode/launch.json index 4c3483004..c8d1c78ce 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -42,7 +42,7 @@ "stopAtEntry": false, "externalConsole": true, "requireExactSource": false - }, + }, { "name": "Excel Launch", "type": "clr", @@ -54,7 +54,18 @@ "externalConsole": true, "requireExactSource": false }, - { + { + "name": "Client Tests", + "type": "clr", + "request": "launch", + "program": "${workspaceRoot}/build/Debug/bin/net471/sparkcmd.exe", + "args": ["test","test_filter"], + "cwd": "${workspaceRoot}", + "stopAtEntry": false, + "externalConsole": true, + "requireExactSource": false + }, + { "name": "Client Attach", "type": "clr", "request": "attach", @@ -198,7 +209,7 @@ "type": "cppdbg", "request": "launch", "program": "/home/bts/spark/build/ninja_debug/src/server/sparkd/sparkd", - "args": ["test", "[md]"], + "args": ["test","Co2RelayTests"], "stopAtEntry": false, "cwd": "/home/bts/spark/build/ninja_debug/src/server/sparkd/", "environment": [], diff --git a/ops_notes.txt b/ops_notes.txt index 0ffdf4188..6d1493c4b 100644 --- a/ops_notes.txt +++ b/ops_notes.txt @@ -1,3 +1,6 @@ +1.3.5 +- Change in market data wire format. Old shell will get only direct market data instead of merged which may not match what the servers is driving from. + 1.3.4 - Changed default logging config for Windows. If Logging config is overridden for a system it should be adjusted to override only linux. diff --git a/src/client/spark/Admin/AdminModel.cs b/src/client/spark/Admin/AdminModel.cs index 52efce6db..ebf235a1c 100644 --- a/src/client/spark/Admin/AdminModel.cs +++ b/src/client/spark/Admin/AdminModel.cs @@ -11,18 +11,22 @@ namespace mbts.spark { [Export] class AdminModel : IDisposable { readonly string _user_name; + public string user_name => _user_name; + readonly BehaviorSubject> _accounts_subject = new BehaviorSubject>(new Dictionary()), - _users_subject = new BehaviorSubject>(new Dictionary()); + _users_subject = new BehaviorSubject>(new Dictionary()); IReadOnlyDictionary _account_map = new Dictionary(), _user_map = new Dictionary(); - IDisposable _dispose; + readonly BehaviorSubject _wrapper_subject = new BehaviorSubject(new AdminModelWrapper()); + IDisposable _dispose; [ImportingConstructor] AdminModel(AdminService service, [Import("username")]string user_name) { _user_name = user_name; _dispose = service.subscribei_admin_data().Select(update => { + _wrapper_subject.OnNext(new AdminModelWrapper(update.all.TryFirst(), _user_name)); IEnumerable active_users = from admin_data in update.all from bts_user in admin_data.Users @@ -52,9 +56,10 @@ namespace mbts.spark { } public IObservable> on_users => _users_subject.AsObservable(); - public IObservable> on_accounts => _accounts_subject.AsObservable(); + public IObservable> on_accounts => _accounts_subject.AsObservable(); public IObservable> on_user_tuples => on_users.Select(d => d.Select(kv => new UserTuple(kv.Key, kv.Value)).ToList()); public IObservable> on_account_tuples => on_accounts.Select(d => d.Select(kv => new AccountTuple(kv.Key, kv.Value)).ToList()); + public IObservable on_admin_data => _wrapper_subject.AsObservable(); public UserTuple? get_current_user() { foreach (var kv in _user_map) { diff --git a/src/client/spark/Admin/AdminModelWrapper.cs b/src/client/spark/Admin/AdminModelWrapper.cs index eada2a9c8..eedc3c0b3 100644 --- a/src/client/spark/Admin/AdminModelWrapper.cs +++ b/src/client/spark/Admin/AdminModelWrapper.cs @@ -4,29 +4,56 @@ using System; using System.Collections.Generic; using System.Linq; -namespace mbts.spark { - class AdminModelWrapper{ - private bts.api.admin.AdminDataImmutable _admin_data; - public AdminModelWrapper(bts.api.admin.AdminDataImmutable admin_data){ - _admin_data=admin_data; +namespace mbts.spark +{ + class AdminModelWrapper + { + readonly AdminDataImmutable? _admin_data; + readonly string _user_name; + readonly List _allowed_accounts; + readonly Dictionary _allowed_account_dict = new Dictionary(); + public AdminModelWrapper(AdminDataImmutable? admin_data = null, string user_name = null) + { + _admin_data = admin_data; + _user_name = user_name ?? string.Empty; + _allowed_accounts = search_admin(u => u.Name == _user_name).Select(x => x.Item1).ToList(); + foreach(var val in _allowed_accounts) + _allowed_account_dict[val.Name] = val.Name; } - // for a given user, returns the accounts the user is a - // part of along with the permissions - public IEnumerable> get_trading_permissions(UInt32 user_id){ + public IEnumerable> get_trading_permissions(UInt32 user_id) + { + return search_admin(u => u.Id == user_id); + } + public IEnumerable viewable_accounts() + { + return _allowed_accounts; + } + public bool can_view_account(string account) + { + return _allowed_account_dict.ContainsKey(account) || _user_name.ToLower() == "bts"; + } + + IEnumerable> search_admin(Func predicate) + { var rv = new List>(); - if(_admin_data!=null){ - var users = _admin_data.Users.Where(u=>u.Id==user_id); - if(users!=null && users.Count()==1){ + if (_admin_data.HasValue) + { + var users = _admin_data.Value.Users.Where(predicate); + if (users != null && users.Count() == 1) + { var user = users.ElementAt(0); - if(user.HasUserData && user.UserData.HasAccountPermissions){ - foreach (var kv in user.UserData.AccountPermissions.AccountPermissions){ - var account_id=kv.Key; - var accounts=_admin_data.Accounts.Where(a=>a.Id==account_id); - if(accounts!=null&&accounts.Count()==1){ - var account=accounts.ElementAt(0); - var tp=kv.Value.TradingPermission; - var rv_val=new TradingPermissionUnitImmutable(tp); - rv.Add(Tuple.Create(account,rv_val)); + if (user.HasUserData && user.UserData.HasAccountPermissions) + { + foreach (var kv in user.UserData.AccountPermissions.AccountPermissions) + { + var account_id = kv.Key; + var accounts = _admin_data.Value.Accounts.Where(a => a.Id == account_id); + if (accounts != null && accounts.Count() == 1) + { + var account = accounts.ElementAt(0); + var tp = kv.Value.TradingPermission; + var rv_val = new TradingPermissionUnitImmutable(tp); + rv.Add(Tuple.Create(account, rv_val)); } } } diff --git a/src/client/spark/Admin/PermissionFilters.cs b/src/client/spark/Admin/PermissionFilters.cs new file mode 100644 index 000000000..115187a45 --- /dev/null +++ b/src/client/spark/Admin/PermissionFilters.cs @@ -0,0 +1,66 @@ +using bts; +using bts.api; +using bts.api.core; +using bts.api.orderman; +using bts.api.position; +using bts.spark; +using mbts.collections; +using mbts.spark; +using mbts.spark.utilities; +using mbts.utils; +using System; +using System.Collections.Generic; +using System.Collections.Immutable; +using System.Collections.Specialized; +using System.Linq; +using System.Reactive.Concurrency; +using System.Reactive.Linq; + +namespace bts.api.core +{ + + static class PermissionFilters + { + public static IObservable> PermissionFilter(this IObservable> source, AdminModel am) + { + var pred = Observable.Create>( observer => + { + return am.on_admin_data.Subscribe(x => + { + Func filter = (z => x.can_view_account(z.Account)); + observer.OnNext(filter); + }); + }); + + return source.Filter(pred); + } + + public static IObservable> PermissionFilter(this IObservable> source, AdminModel am) + { + var pred = Observable.Create>(observer => + { + return am.on_admin_data.Subscribe(x => + { + Func filter = (z => x.can_view_account(z.account())); + observer.OnNext(filter); + }); + }); + + return source.Filter(pred); + } + + public static IObservable> PermissionFilter(this IObservable> source, AdminModel am) + { + var pred = Observable.Create>(observer => + { + return am.on_admin_data.Subscribe(x => + { + Func filter = (z => x.can_view_account(z.Account)); + observer.OnNext(filter); + }); + }); + + return source.Filter(pred); + } + } +} \ No newline at end of file diff --git a/src/client/spark/Daemons/ShellServices.cs b/src/client/spark/Daemons/ShellServices.cs index 25a8e9f8e..a57ac2547 100644 --- a/src/client/spark/Daemons/ShellServices.cs +++ b/src/client/spark/Daemons/ShellServices.cs @@ -1,5 +1,6 @@ using bts.api.admin; using bts.api.core; +using bts.api.md.auxiliary; using bts.api.orderman; using bts.api.persistence; using bts.api.position; @@ -40,5 +41,8 @@ namespace Spark { [Export] public PositionService position_service => context.locate(); + + [Export] + public MarketDataAuxiliaryService md_auxiliary_service => context.locate(); } } \ No newline at end of file diff --git a/src/client/spark/Images/Icons.cs b/src/client/spark/Images/Icons.cs index adc9118a4..f9fee789c 100644 --- a/src/client/spark/Images/Icons.cs +++ b/src/client/spark/Images/Icons.cs @@ -20,7 +20,7 @@ namespace mbts.ui { static readonly string base_uri = "pack://application:,,,/Images/Icons/"; private static ResourceDictionary _resources = (ResourceDictionary)Application.LoadComponent(new Uri("/Images/Vector/Icons.xaml", UriKind.RelativeOrAbsolute)); - + private static Uri uri(string filename) { return new Uri(base_uri + filename); } @@ -94,12 +94,12 @@ namespace mbts.ui { public static ImageSource CancelBuys { get { return load_resource_and_freeze("CancelBuys"); } } public static ImageSource CancelSells { get { return load_resource_and_freeze("CancelSells"); } } public static ImageSource CancelSelected { get { return load_resource_and_freeze("CancelSelected"); } } - + public static ImageSource LadderCancelOrders { get { return load_resource_and_freeze("LadderCancelOrders"); } } public static ImageSource LadderCancelBuys { get { return load_resource_and_freeze("LadderCancelBuys"); } } public static ImageSource LadderCancelSells { get { return load_resource_and_freeze("LadderCancelSells"); } } - public static ImageSource Synthetics { get { return load_resource_and_freeze("Synthetics"); } } + public static ImageSource Synthetics { get { return load_resource_and_freeze("Synthetics"); } } public static ImageSource AddSynthetic { get { return load_resource_and_freeze("AddSynthetic"); } } public static ImageSource RemoveSynthetic { get { return load_resource_and_freeze("RemoveSynthetic"); } } public static ImageSource ModifySynthetic { get { return load_resource_and_freeze("ModifySynthetic"); } } @@ -126,7 +126,10 @@ namespace mbts.ui { public static ImageSource Wrench { get { return load_resource_and_freeze("Wrench"); } } public static ImageSource RightArrow { get { return (load_resource_and_freeze("RightArrow")); } } public static ImageSource DownArrow { get { return (load_resource_and_freeze("DownArrow")); } } - public static ImageSource Support => load_resource_and_freeze("SupportIcon"); + public static ImageSource Factory { get { return (load_resource_and_freeze("Factory")); } } + public static ImageSource DotsVertical { get { return (load_resource_and_freeze("DotsVertical")); } } + public static ImageSource HandPointingOverDocument => load_resource_and_freeze("HandPointingOverDocument"); + public static ImageSource Support => load_resource_and_freeze("SupportIcon"); public static ImageSource create_icon(string geometric_path_key) => create_icon(geometric_path_key, Brushes.Black); diff --git a/src/client/spark/Images/Vector/Icons.xaml b/src/client/spark/Images/Vector/Icons.xaml index 7460c5d49..d6341a5a2 100644 --- a/src/client/spark/Images/Vector/Icons.xaml +++ b/src/client/spark/Images/Vector/Icons.xaml @@ -813,8 +813,48 @@ M0,0L63.999001,0 63.999001,48.792999 0,48.792999z"> + + + + + + + + + - - - - - + + - - - - - - - - - - - - - - + + + + + + + + + + + + + + + diff --git a/src/client/spark/Modules/Spreader/SyntheticManagementView.xaml.cs b/src/client/spark/Modules/Spreader/SyntheticManagementView.xaml.cs index 8714268be..c3b7e6bea 100644 --- a/src/client/spark/Modules/Spreader/SyntheticManagementView.xaml.cs +++ b/src/client/spark/Modules/Spreader/SyntheticManagementView.xaml.cs @@ -1,12 +1,17 @@ -using mbts.spark.filters; +using mbts.spark; +using mbts.ui.controls; using System.Windows.Controls; namespace mbts.ui.spark { /// /// Interaction logic for SyntheticManagementView.xaml /// - internal partial class SyntheticManagementView : UserControl { + partial class SyntheticManagementView : UserControl { + + public enum Columns { Name, Description, n_legs, n_working_orders, SecurityKey} public SyntheticManagementView() => InitializeComponent(); } + + class SyntheticManagementRowView : DIYGrid.HighPerformanceRow { } } \ No newline at end of file diff --git a/src/client/spark/Modules/Spreader/SyntheticManagementWindow.cs b/src/client/spark/Modules/Spreader/SyntheticManagementWindow.cs index 8bccc80d9..d7efda3eb 100644 --- a/src/client/spark/Modules/Spreader/SyntheticManagementWindow.cs +++ b/src/client/spark/Modules/Spreader/SyntheticManagementWindow.cs @@ -2,8 +2,11 @@ using bts.api; using bts.shell; using bts.spark; +using mbts.persist; using mbts.ui; +using mbts.ui.settings; using mbts.ui.spark; +using mbts.ui.utility; using mbts.utils; using System; using System.Collections.Generic; @@ -13,27 +16,33 @@ using System.Windows; using static bts.shell.SyntheticManagementWindowFactory; namespace mbts.spark { - class SyntheticManagementWindow : SparkWindow { + class SyntheticManagementWindow : SparkWindow, ISlowWindow { const string _window_type = "SyntheticManagement"; SingleValuePump _vm = new SingleValuePump(); StartupData _state; OrdersModel _orders_model; SyntheticManagementView _view; IDisposable _subscriptions; + IReadOnlyList _commands; + readonly IWindowSettingsManager _settings_manager; - public static SyntheticManagementWindow create(SyntheticManagementWindowFactory factory, IBus bus, OrdersModel orders_model, StartupData state) { - var window = new SyntheticManagementWindow(factory, bus, orders_model); - window.initialize(state); - return window; - } - SyntheticManagementWindow(SyntheticManagementWindowFactory factory, IBus bus, OrdersModel orders_model) + public SyntheticManagementWindow(SyntheticManagementWindowFactory factory, IBus bus, OrdersModel orders_model, StartupData state, WindowSettingsManager settings_manager) : base("Synthetic Management", bus, factory, SparkCommands.ShowSyntheticManagement.icon) { _orders_model = orders_model; + _state = state; + _settings_manager = settings_manager; + Icon.Freeze(); + } + + public void thread_init() { + Log.notnull(_state); + var vm_connectable_builder = new ConnectableSeriesBuilder(_vm.observable); var current_row_source = vm_connectable_builder.create(vm => vm.current_row_observable); var current_row_connectable_builder = new ConnectableSeriesBuilder(current_row_source); - Commands = new SparkCommand[] { + + _commands = new SparkCommand[] { SparkCommand.create("Add", Icons.AddSynthetic, SparkTags.ShowSyntheticCreateTag).complete( () => new SecurityCriteria(), () => true ), SparkCommand.create("Remove", Icons.RemoveSynthetic, SparkTags.SyntheticDeleteTag).complete( @@ -51,7 +60,24 @@ namespace mbts.spark { filters = new OrderBookWindowFactory.FilterStates { security_criteria = new SecurityCriteria(_vm.value.row_vms.Select(r => r.security)) } - }) + } + ), SparkCommands.window_settings_command(new DialogCommand(() => { + var vm = new WindowSettingsDialogVM( + _settings_manager, + us => us.synthetic_management_grid, + UserSettings.FileExtensions.GRID, + s => _vm.value.grid_settings_manager.create_vm( + // we need to get the most up to date GridSettings when launching b/c users may + // have changed some settings on the grid directly, like column widths. + s.with(_vm.value.columns.Select( c => c.create())), + this.observe_title() + ) + ); + // TODO: bind to main window's Title + var dialog = new SimpleGridSettingsDialog { Title = "Synthetic Management Grid Settings", DataContext = vm }; + vm.RequestClose += (s, e) => WindowUtilities.force_close(dialog); + return dialog; + })) }; _subscriptions = Disposable.Create( @@ -69,21 +95,21 @@ namespace mbts.spark { uint select_security_id(RowModificationParams? rmp) => rmp.HasValue ? rmp.Value.security.Id : 0; (SecurityImmutable, ExecutionStrategyImmutable) select_row_strategy_tuple(RowStrategy? rs) => rs.HasValue ? (rs.Value.security, rs.Value.strategy) : default; bool can_execute(RowModificationParams? rmp) => rmp.HasValue && !rmp.Value.has_working_orders; - } - - public void initialize(StartupData state) { - Log.notnull(state); - _state = state; - _vm.value = new SyntheticManagementDialogVM(_bus, _orders_model, state.sorting_criteria, state.filters ?? new FilterStates()); + + _vm.value = new SyntheticManagementDialogVM(_bus, _orders_model, _settings_manager, _state.sorting_criteria, _state.filters ?? new FilterStates()); if (_view == null) _view = new SyntheticManagementView { DataContext = _vm.value }; else _view.DataContext = _vm.value; } public override string window_type => _window_type; public override UIElement Content => _view; - public override IEnumerable Commands { get; } - public override StartupData SaveState => _state = new StartupData(_vm.value.rows_view.GetSortingCriteria(), _vm.value.get_filter_states()); - public override double MinWidth => 700; + public override IEnumerable Commands => _commands; + public override StartupData SaveState => _state = _vm.value.create(); + public override double MinWidth => + StartupData.DefaultGridSettings.columns.Sum(gcs => gcs.width) + + SPARK_MENU_COLLAPSED_WIDTH + + LAUNCHER_BUTTON_BORDER_WIDTH + + EXTRA_FOR_NO_HORIZONTAL_SCROLLBAR_WIDTH; public override void Dispose() { Disposable.dispose(ref _vm); diff --git a/src/client/spark/Modules/Spreader/SyntheticManagementWindowFactory.cs b/src/client/spark/Modules/Spreader/SyntheticManagementWindowFactory.cs index 11eb6adb2..afb327aa7 100644 --- a/src/client/spark/Modules/Spreader/SyntheticManagementWindowFactory.cs +++ b/src/client/spark/Modules/Spreader/SyntheticManagementWindowFactory.cs @@ -1,30 +1,46 @@ using bts.api; +using mbts.persist; using mbts.spark; using mbts.spark.filters; using mbts.ui; +using mbts.ui.settings; using ProtoBuf; using System; using System.Collections.Generic; using System.Reactive.Linq; using System.Threading.Tasks; +using static mbts.ui.controls.DIYGrid; +using static mbts.ui.spark.SyntheticManagementView; namespace bts.shell { class SyntheticManagementWindowFactory : ShellWindowFactory { readonly IBus _bus; readonly OrdersModel _orders_model; + readonly UserSettingsHub _settings_hub; - public SyntheticManagementWindowFactory(SpreaderShellModule module, IBus bus, OrdersModel orders_model) : base(module, "SyntheticManagement") { + public SyntheticManagementWindowFactory(SpreaderShellModule module, IBus bus, UserSettingsHub settings_hub, OrdersModel orders_model) : base(module, "SyntheticManagement") { _bus = bus; _orders_model = orders_model; + _settings_hub = settings_hub; _dispose = bus.subscribe(SparkTags.ShowSyntheticManagementTag).OnDispatcher().Subscribe(show); } + public static string col_id(Columns col) => col.ToString(); + public override Task load(StartupData state) => - Task.FromResult(SyntheticManagementWindow.create(this, _bus, _orders_model, state)); + Task.FromResult(_create(state)); public async void show(SparkTags.Nothing nothing) => _bus.post(SparkTags.ShowWindow, await load(new StartupData())); + IShellWindow _create(StartupData state) { + var settings_manager = new WindowSettingsManager(_settings_hub, + state.grid, + get: us => us.synthetic_management_grid, + set: (us, gs) => us.with(provided_synthetic_management_gridsettings: gs)); + return new SyntheticManagementWindow(this, _bus, _orders_model, state, settings_manager); + } + [ProtoContract] public class StartupData { public const int Version = 1; @@ -33,11 +49,41 @@ namespace bts.shell { public readonly List sorting_criteria; [ProtoMember(2)] public readonly FilterStates filters; + [ProtoMember(3)] + public readonly GridSettings grid; - public StartupData() : this(new List(), null) { } - public StartupData(List sorting_criteria, FilterStates filters) { + public StartupData() : this(new List(), null, null) { } + public StartupData(List sorting_criteria, FilterStates filters, GridSettings grid = default) { this.sorting_criteria = sorting_criteria; this.filters = filters; + this.grid = grid; + } + + public static GridSettings DefaultGridSettings => GridSettings.Default.with(DefaultColumns.all, DefaultColumns.default_sorting_criterion); + + static class DefaultColumns { + public static GridColumnSettings name => new GridColumnSettings(col_id(Columns.Name), DefaultColumnHeaders.Name, width: DefaultColumnWidths.Security); + 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 IEnumerable default_sorting_criterion { + get { + yield return new SortingCriteria(name.id, true); + } + } + + public static IEnumerable all { + get { + yield return name; + yield return description; + yield return n_legs; + yield return n_working_orders; + yield return security_key; + } + } + } } diff --git a/src/client/spark/Modules/Spreader/VM/ParameterRows/Leg/MarketDataCalculationModel.cs b/src/client/spark/Modules/Spreader/VM/ParameterRows/Leg/MarketDataCalculationModel.cs new file mode 100644 index 000000000..f526589a3 --- /dev/null +++ b/src/client/spark/Modules/Spreader/VM/ParameterRows/Leg/MarketDataCalculationModel.cs @@ -0,0 +1,18 @@ +using mbts.ui.spark; +using LegMarketDataCalculation = bts.spark.LegSpreaderParams.Types.LegMarketDataCalculation; + +namespace mbts.spark { + partial class SyntheticEditorVM { + class MarketDataCalculationModel : RowModelBase { + public override string display_name { get; } = "Market Data Calculation"; + public override string help_text { get; } + public override string man_page { get; } = string.Empty; + + public override LegMarketDataCalculation get_value(LegSpreaderParamsTuple leg_tup) => + leg_tup.leg_params.LegMdCalc; + + public override void set_value(LegSpreaderParamsTuple leg_tup, LegMarketDataCalculation value) => + leg_tup.leg_params.LegMdCalc = value; + } + } +} \ No newline at end of file diff --git a/src/client/spark/Modules/Spreader/VM/ParameterRows/Leg/MarketDataTypeModel.cs b/src/client/spark/Modules/Spreader/VM/ParameterRows/Leg/MarketDataTypeModel.cs new file mode 100644 index 000000000..4e1125380 --- /dev/null +++ b/src/client/spark/Modules/Spreader/VM/ParameterRows/Leg/MarketDataTypeModel.cs @@ -0,0 +1,18 @@ +using mbts.ui.spark; +using MarketDataType = bts.spark.LegSpreaderParams.Types.MarketDataType; + +namespace mbts.spark { + partial class SyntheticEditorVM { + class MarketDataTypeModel : RowModelBase { + public override string display_name { get; } = "Market Data Type"; + public override string help_text { get; } + public override string man_page { get; } = string.Empty; + + public override MarketDataType get_value(LegSpreaderParamsTuple leg_tup) => + leg_tup.leg_params.MarketDataType; + + public override void set_value(LegSpreaderParamsTuple leg_tup, MarketDataType value) => + leg_tup.leg_params.MarketDataType = value; + } + } +} \ No newline at end of file diff --git a/src/client/spark/Modules/Spreader/VM/SyntheticEditorVM.cs b/src/client/spark/Modules/Spreader/VM/SyntheticEditorVM.cs index 96d766b6b..92322964b 100644 --- a/src/client/spark/Modules/Spreader/VM/SyntheticEditorVM.cs +++ b/src/client/spark/Modules/Spreader/VM/SyntheticEditorVM.cs @@ -203,6 +203,8 @@ namespace mbts.spark { new IsQuoteModel(), new HedgeRatioModel(), new QuoteHiddenModel(), + new MarketDataTypeModel(), + //new MarketDataCalculationModel(), new QuoteThrottleEnabledModel(quote_throttle_inside_ticks, quote_throttle_outside_ticks), quote_throttle_inside_ticks, quote_throttle_outside_ticks, diff --git a/src/client/spark/Modules/Spreader/VM/SyntheticManagementDialogVM.cs b/src/client/spark/Modules/Spreader/VM/SyntheticManagementDialogVM.cs index d8d6a31dd..489ba2935 100644 --- a/src/client/spark/Modules/Spreader/VM/SyntheticManagementDialogVM.cs +++ b/src/client/spark/Modules/Spreader/VM/SyntheticManagementDialogVM.cs @@ -1,22 +1,31 @@ using bts.api; using bts.shell; using bts.spark; +using mbts.persist; using mbts.spark.filters; using mbts.ui; +using mbts.ui.controls; +using mbts.ui.settings; using System; using System.Collections.Generic; +using System.Collections.ObjectModel; using System.Linq; using System.Reactive.Linq; +using System.Windows.Data; using System.Windows.Input; using static bts.shell.SyntheticManagementWindowFactory; +using static mbts.ui.controls.DIYGrid; +using static mbts.ui.spark.SyntheticManagementView; namespace mbts.spark { class SyntheticManagementDialogVM : SecurityMDGrid { readonly IBus _bus; readonly OrdersModel _orders_model; + IWindowSettingsManager _settings_manager; - public SyntheticManagementDialogVM(IBus bus, OrdersModel orders_model) : this(bus, orders_model, null, null) { } - public SyntheticManagementDialogVM(IBus bus, OrdersModel orders_model, IReadOnlyCollection sorting, FilterStates filters) + + public SyntheticManagementDialogVM(IBus bus, OrdersModel orders_model, IWindowSettingsManager settings_manager) : this(bus, orders_model, settings_manager, null, null) { } + public SyntheticManagementDialogVM(IBus bus, OrdersModel orders_model, IWindowSettingsManager settings_manager, IReadOnlyCollection sorting, FilterStates filters) : base(orders_model.spreader.strategy_tuple.Select(map => map.Values), sorting) { _bus = bus; _orders_model = orders_model; @@ -28,7 +37,41 @@ namespace mbts.spark { n_working_orders_filter.load_state(filters.n_working_orders); security_key_filter.load_state(filters.security_key); } - _dispose = DataGridFilterHelper.hook_filters(rows_view, create_time_filter, name_filter, description_filter, n_legs_filter, n_working_orders_filter, security_key_filter); + _settings_manager = settings_manager; + // columns + add_col(Columns.Name, DefaultColumnHeaders.Name, r => r.name, nameof(Row.name), name_filter, DataGridFilterKnownTemplates.Regex, DefaultColumnWidths.Security); + add_col(Columns.Description, DefaultColumnHeaders.Description, r => r.description, nameof(Row.description), description_filter, DataGridFilterKnownTemplates.Regex, DefaultColumnWidths.Description); + add_col(Columns.n_legs, DefaultColumnHeaders.N_Legs, r => r.n_legs.ToString(), nameof(Row.n_legs), n_legs_filter, DataGridFilterKnownTemplates.IntMinMax, DefaultColumnWidths.N_Legs); + columns.Add(new HyperlinkElementColumn( + Columns.n_working_orders, + new Binding(nameof(Row.n_working_orders)), + new Binding(nameof(Row.show_order_book)), + n_working_orders_filter, + DataGridFilterKnownTemplates.UIntMinMax) { + id = col_id(Columns.n_working_orders), + Header = DefaultColumnHeaders.N_Working_Orders, + Width = DefaultColumnWidths.N_Working_Orders + } + ); + add_col(Columns.SecurityKey, DefaultColumnHeaders.SecurityKey, r => r.security_key.ToString(), nameof(Row.security_key), security_key_filter, DataGridFilterKnownTemplates.Regex, DefaultColumnWidths.SecurityKey); + + // + void add_col(Columns col_key, string header, Func get_text, string prop, IDataGridColumnFilter filter, string filter_template, double width) => + columns.Add(new RenderTextColumn(col_key, get_text, filter, filter_template) { + id = col_id(col_key), + Header = header, + sort_member_path = prop, + Width = width + }); + // + grid_settings_manager = new SimpleGridSettingsManager(columns, new Dictionary { }, row_vms); + // this will initialize the immutable properties that correspond to the col_settings_flags above (e.g. Bid/Ask font_color_override, etc.) + grid_settings_manager.load_from(settings_manager.current_settings()); + + _resources.AddRange( + DataGridFilterHelper.hook_filters(rows_view, create_time_filter, name_filter, description_filter, n_legs_filter, n_working_orders_filter, security_key_filter), + settings_manager.subscribe_current().OnDispatcher().Subscribe(grid_settings_manager) + ); } protected override Row row_factory((ExecutionStrategyImmutable? strategy, SecurityMD md) row_model) => @@ -36,6 +79,20 @@ namespace mbts.spark { protected override void on_row_removed(Row row) => row.Dispose(); + public ObservableCollection columns { get; } = new ObservableCollection(); + public SimpleGridSettingsManager grid_settings_manager { get; } + + public GridSettings get_grid_save_state() { + if (_settings_manager.instance_settings == null) + return null; + else + return grid_settings_manager.create(); + } + + public StartupData create() => + new StartupData(rows_view.get_sorting_criteria(columns), _get_filter_states(), get_grid_save_state()); + + #region Filters public IDataGridColumnPersistentFilter create_time_filter { get; } = new TimeFilterVM(r => r.create_time, nameof(Row.create_time)); public IDataGridColumnPersistentFilter name_filter { get; } = new RegexFilterVM(r => r.name, nameof(Row.name)); @@ -44,7 +101,7 @@ namespace mbts.spark { public IDataGridColumnPersistentFilter n_working_orders_filter { get; } = new UIntMinMax(r => r.n_working_orders, nameof(Row.n_legs)); public IDataGridColumnPersistentFilter security_key_filter { get; } = new RegexFilterVM(r => r.security_key.ToString(), nameof(Row.security_key)); - public FilterStates get_filter_states() => + FilterStates _get_filter_states() => new FilterStates { description = description_filter.get_state(), create_time = create_time_filter.get_state(), @@ -55,7 +112,7 @@ namespace mbts.spark { }; #endregion - public class Row : SecurityMDRow, IEquatable { + public class Row : SecurityMDRow, IEquatable, IRowVM, IActionableRowVM { public readonly ExecutionStrategyImmutable? strategy; readonly IBus _bus; @@ -85,6 +142,17 @@ namespace mbts.spark { public uint n_working_orders => _n_working_orders.Value; public ICommand show_ladder { get; } public ICommand show_order_book { get; } + public ICommand default_command => show_ladder; + + + public ObservableCollection row_animations => new ObservableCollection(); + + public IReadOnlyList cell_stores => new IGridCellStore[0]; + + + public event Action invalidated; + + public void raise_invalidated() => invalidated?.Invoke(); public bool Equals(Row other) => other != null && @@ -96,6 +164,8 @@ namespace mbts.spark { public override int GetHashCode() => security_key.GetHashCode(); + + void _show_ladder() => _bus.post(SparkTags.ShowLadderTag, md); diff --git a/src/client/spark/Modules/Trading/Fills/FillsVM.cs b/src/client/spark/Modules/Trading/Fills/FillsVM.cs index f4de61a8c..ab88250b9 100644 --- a/src/client/spark/Modules/Trading/Fills/FillsVM.cs +++ b/src/client/spark/Modules/Trading/Fills/FillsVM.cs @@ -1,6 +1,7 @@ using bts.api; using bts.api.orderman; using bts.spark; +using mbts.persist; using mbts.spark.filters; using mbts.spark.utilities; using mbts.ui; @@ -13,6 +14,7 @@ using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Reactive.Linq; +using System.Reactive.Subjects; using System.Windows.Data; using static bts.api.orderman.Fill.Types; using static bts.shell.FillsWindowFactory; @@ -23,13 +25,16 @@ using OrderType = bts.api.orderman.CustomOrderDefinition.CustomOneofCase; namespace mbts.spark { class FillsVM : SecurityGrid { readonly OrdersModel _model; - - public FillsVM(IBus bus, OrdersModel model, StartupData state) - : this(bus, model, _get_observable(bus, model, state), state.sorting_criteria, state.filters) { } + IWindowSettingsManager _settings_manager; - FillsVM(IBus bus, OrdersModel model, IObservable> fills_source, IReadOnlyCollection sorting_criteria, FilterStates filters) - : base(RXCollection.CreateIncrementalUpdate(fills_source), sorting_criteria) { + + public FillsVM(IBus bus, OrdersModel model, IObservable state_obs, IWindowSettingsManager settings_manager) + : this(bus, model, state_obs, _get_observable(bus, model, state_obs), settings_manager) { } + + FillsVM(IBus bus, OrdersModel model, IObservable state_obs, IConnectableObservable> fills_source, IWindowSettingsManager settings_manager) + : base(RXCollection.CreateIncrementalUpdate(fills_source), null) { _model = model; + _settings_manager = settings_manager; account_filter = DiscreteValuesFilterVM.Create(_model.factory.subscribe_accounts().Select(d => d.Values), r => r.account, nameof(Row.account)); user_filter = DiscreteValuesFilterVM.Create(_model.factory.subscribe_users().Select(d => d.Values), r => r.user, nameof(Row.user)); security_filter = DiscreteValuesFilterVM.Create( @@ -44,30 +49,6 @@ namespace mbts.spark { SecurityDataHelper.EqualityComparer.IDComparer, Comparer.Default ); - if (filters != null) { - fill_time_filter.load_state(filters.fill_time); - time_in_force_filter.load_state(filters.time_in_force); - order_type_filter.load_state(filters.order_type); - account_filter.load_state(filters.account); - user_filter.load_state(filters.user); - exchange_filter.load_state(filters.exchange); - security_filter.load_state(filters.security); - side_filter.load_state(filters.side); - price_filter.load_state(filters.price); - size_filter.load_state(filters.size); - fill_kind_filter.load_state(filters.fill_kind); - fill_key_filter.load_state(filters.fill_key); - order_key_filter.load_state(filters.order_key); - security_key_filter.load_state(filters.security_key); - } else { - fill_time_filter.load_state( - new TimeFilterTuple(ui.controls.TimeFilterMode.CMEOpen, - TimeTextTuple.FromDateTime(DateTime.Today), - TimeTextTuple.FromDateTime(DateTime.Today.AddDays(1)) - ) - ); - fill_kind_filter.load_state(EnumHelper.get_values_except(FillKind.Sod).ToArray()); - } // columns add_col(Columns.Security, DefaultColumnHeaders.Security, r => r.security.ToString(), nameof(Row.security), security_filter, DataGridFilterKnownTemplates.DiscreteValues, DefaultColumnWidths.Security); add_col(Columns.FillTime, DefaultColumnHeaders.FillTime, r => r.fill_time.ToString("M/d h:mm:ss tt"), nameof(Row.fill_time), fill_time_filter, DataGridFilterKnownTemplates.TimeFilter, DefaultColumnWidths.Time); @@ -75,30 +56,83 @@ 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.OrderType, new Binding(nameof(Row.order_type)), new Binding(nameof(Row.order_metadata)), order_type_filter) { sort_member_path = nameof(Row.order_type) }); - columns.Add(new SideColumn(Columns.Side, r => r.side, side_filter) { sort_member_path = nameof(Row.side) }); + columns.Add(new OrderTypeColumn(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 SideColumn(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); - add_col(Columns.FillKind, "Fill Kind", r => r.fill_kind.ToString(), nameof(Row.fill_kind), fill_kind_filter, DataGridFilterKnownTemplates.DiscreteValues, 70); + add_col(Columns.FillKind, DefaultColumnHeaders.FillKind, r => r.fill_kind.ToString(), nameof(Row.fill_kind), fill_kind_filter, DataGridFilterKnownTemplates.DiscreteValues, DefaultColumnWidths.FillKind); add_col(Columns.FillKey, DefaultColumnHeaders.FillKey, r => r.fill_key.ToString(), nameof(Row.fill_key), fill_key_filter, DataGridFilterKnownTemplates.Regex, DefaultColumnWidths.FillKey); add_col(Columns.OrderKey, DefaultColumnHeaders.OrderKey, r => r.order_key.ToString(), nameof(Row.order_key), order_key_filter, DataGridFilterKnownTemplates.Regex, DefaultColumnWidths.OrderKey); add_col(Columns.SecurityKey, DefaultColumnHeaders.SecurityKey, r => r.security_key.ToString(), nameof(Row.security_key), security_key_filter, DataGridFilterKnownTemplates.Regex, DefaultColumnWidths.SecurityKey); // void add_col(Columns col_key, string header, Func get_text, string prop, IDataGridColumnFilter filter, string filter_template, double width) => - columns.Add(new RenderTextColumn(col_key, get_text, filter, filter_template) { + columns.Add(new RenderTextColumn(col_key, get_text, filter, filter_template) { + id = col_id(col_key), Header = header, sort_member_path = prop, Width = width }); + grid_settings_manager = new SimpleGridSettingsManager(columns, new Dictionary { }, row_vms); + // this will initialize the immutable properties that correspond to the col_settings_flags above (e.g. Bid/Ask font_color_override, etc.) + grid_settings_manager.load_from(settings_manager.current_settings()); + // - _resources.Add(DataGridFilterHelper.hook_filters(rows_view, - fill_time_filter, time_in_force_filter, order_type_filter, account_filter, user_filter, exchange_filter, - security_filter, side_filter, price_filter, size_filter, fill_kind_filter, fill_key_filter, order_key_filter, security_key_filter - )); + _resources.AddRange( + state_obs.OnDispatcher().Subscribe(s => + { + using (rows_view.DeferRefresh()) { + if (s.grid != null) { + // this means we are not using defaults, + // Note: settings_manager in turn notifies grid_settings_manager of this update + _settings_manager.instance_settings = s.grid; + } + if (s.filters is FilterStates filters) { + fill_time_filter.load_state(filters.fill_time); + time_in_force_filter.load_state(filters.time_in_force); + order_type_filter.load_state(filters.order_type); + account_filter.load_state(filters.account); + user_filter.load_state(filters.user); + exchange_filter.load_state(filters.exchange); + security_filter.load_state(filters.security); + side_filter.load_state(filters.side); + price_filter.load_state(filters.price); + size_filter.load_state(filters.size); + fill_kind_filter.load_state(filters.fill_kind); + fill_key_filter.load_state(filters.fill_key); + order_key_filter.load_state(filters.order_key); + security_key_filter.load_state(filters.security_key); + } else { + fill_time_filter.load_state( + new TimeFilterTuple(ui.controls.TimeFilterMode.CMEOpen, + TimeTextTuple.FromDateTime(DateTime.Today), + TimeTextTuple.FromDateTime(DateTime.Today.AddDays(1)) + ) + ); + fill_kind_filter.load_state(EnumHelper.get_values_except(FillKind.Sod).ToArray()); + } + rows_view.set_sort_descriptions(s.sorting_criterion, columns); + } + }), + DataGridFilterHelper.hook_filters(rows_view, + fill_time_filter, time_in_force_filter, order_type_filter, account_filter, user_filter, exchange_filter, + security_filter, side_filter, price_filter, size_filter, fill_kind_filter, fill_key_filter, order_key_filter, security_key_filter + ), + settings_manager.subscribe_current().OnDispatcher().Subscribe(grid_settings_manager), + fills_source.Connect() + ); } public ObservableCollection columns { get; } = new ObservableCollection(); + public SimpleGridSettingsManager grid_settings_manager { get; } + + public GridSettings get_grid_save_state() { + if (_settings_manager.instance_settings == null) + return null; + else + return grid_settings_manager.create(); + } + #region Filters public IDataGridColumnPersistentFilter fill_time_filter { get; } = new TimeFilterVM(r => r.fill_time, nameof(Row.fill_time)); public IDataGridColumnPersistentFilter time_in_force_filter { get; } = DiscreteValuesFilterVM.Create(EnumHelper.get_values().ToLArray(), r => r.time_in_force, nameof(Row.time_in_force)); @@ -144,6 +178,9 @@ namespace mbts.spark { } else return model.on_fill_tuples; } + static IConnectableObservable> _get_observable(IBus bus, OrdersModel model, IObservable state) => + state.SelectManyInSeries(s => _get_observable(bus, model, s)); + public class Row : SecurityRow, IEquatable, IRowVM { readonly OrdersModel _model; readonly FillImmutable _fill; diff --git a/src/client/spark/Modules/Trading/Fills/FillsWindow.cs b/src/client/spark/Modules/Trading/Fills/FillsWindow.cs index 325ac30f3..cb4e56609 100644 --- a/src/client/spark/Modules/Trading/Fills/FillsWindow.cs +++ b/src/client/spark/Modules/Trading/Fills/FillsWindow.cs @@ -2,9 +2,16 @@ using bts.api; using bts.shell; using bts.spark; +using mbts.persist; using mbts.ui; +using mbts.ui.settings; using mbts.ui.spark; +using mbts.ui.utility; using mbts.utils; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reactive.Linq; using System.Windows; using StartupData = bts.shell.FillsWindowFactory.StartupData; @@ -13,29 +20,76 @@ namespace mbts.spark { const string _window_type = "Fills", _thread_id = "Fills"; readonly AllowOnce _init_guard = new AllowOnce("Fills"); readonly OrdersModel _orders_model; + readonly IWindowSettingsManager _settings_manager; FillsVM _vm; FillsView _view; StartupData _state; + IReadOnlyList _commands; + IDisposable _subscriptions; - public FillsWindow(FillsWindowFactory factory, IBus bus, OrdersModel orders_model, StartupData state) + public FillsWindow(FillsWindowFactory factory, IBus bus, OrdersModel orders_model, StartupData state, WindowSettingsManager settings_manager) : base("Fills", bus, factory, SparkCommands.ShowFills.icon) { _orders_model = orders_model; _state = state; + _settings_manager = settings_manager; } + event Action _state_changed; + public override string window_type => _window_type; public override UIElement Content => _view; - public override StartupData SaveState => _state = new StartupData(_state.orders, _vm.rows_view.GetSortingCriteria(), _vm.get_filters()); - public override double MinWidth => 1050 + SPARK_MENU_COLLAPSED_WIDTH + LAUNCHER_BUTTON_BORDER_WIDTH; + public override IEnumerable Commands => _commands; + + public override StartupData SaveState => _state = new StartupData(_state.orders, _vm.rows_view.get_sorting_criteria(_vm.columns), _vm.get_filters(), _vm.get_grid_save_state()); + public override double MinWidth => + StartupData.DefaultGridSettings.columns.Sum(gcs => gcs.width) + + SPARK_MENU_COLLAPSED_WIDTH + + LAUNCHER_BUTTON_BORDER_WIDTH + + EXTRA_FOR_NO_HORIZONTAL_SCROLLBAR_WIDTH; + public string thread_id => _thread_id; public void thread_init() { _init_guard.check_access(); Log.notnull(_state); - _vm = new FillsVM(_bus, _orders_model, _state); + var state_src = Observable.FromEvent, FillsWindowFactory.StartupData>(cb => _state_changed += cb, cb => _state_changed -= cb).StartWith(_state); + _vm = new FillsVM(_bus, _orders_model, state_src, _settings_manager); _view = new FillsView { DataContext = _vm }; + + _commands = new List { + // wrap the grid settings view factory in a dialogCommand here since the vm is destroyed/recreated when the security criteria changes + SparkCommands.window_settings_command(new DialogCommand(() => { + var vm = new WindowSettingsDialogVM( + _settings_manager, + us => us.fills_grid, + UserSettings.FileExtensions.GRID, + s => _vm.grid_settings_manager.create_vm( + // we need to get the most up to date GridSettings when launching b/c users may + // have changed some settings on the grid directly, like column widths. + s.with(_vm.columns.Select( c => c.create())), + this.observe_title() + ) + ); + // TODO: bind to main window's Title + var dialog = new SimpleGridSettingsDialog { Title = $"{_window_type} Grid Settings", DataContext = vm }; + vm.RequestClose += (s, e) => WindowUtilities.force_close(dialog); + return dialog; + })) + }; + _subscriptions = Disposable.Create(() => + _state_changed?.Invoke( + _state = new StartupData(_state.orders, + _vm.rows_view.get_sorting_criteria(_vm.columns), + _vm.get_filters(), + _vm.grid_settings_manager.create() + ) + ) + ); } - public override void Dispose() => Disposable.dispose(ref _vm); + public override void Dispose() { + Disposable.dispose(ref _vm); + Commands.OfType().DisposeAll(); + } } } \ No newline at end of file diff --git a/src/client/spark/Modules/Trading/Fills/FillsWindowFactory.cs b/src/client/spark/Modules/Trading/Fills/FillsWindowFactory.cs index a4021e500..b4e789c2b 100644 --- a/src/client/spark/Modules/Trading/Fills/FillsWindowFactory.cs +++ b/src/client/spark/Modules/Trading/Fills/FillsWindowFactory.cs @@ -1,49 +1,118 @@ using bts.api; using bts.api.orderman; +using mbts.persist; using mbts.spark; using mbts.spark.filters; using mbts.ui; +using mbts.ui.settings; using ProtoBuf; using System; using System.Collections.Generic; +using System.Linq; using System.Reactive.Linq; using System.Threading.Tasks; +using static mbts.ui.controls.DIYGrid; +using static mbts.ui.spark.FillsView; namespace bts.shell { class FillsWindowFactory : ShellWindowFactory { readonly IBus _bus; readonly OrdersModel _orders_model; + readonly UserSettingsHub _settings_hub; - public FillsWindowFactory(TradingShellModule module, IBus bus, OrdersModel orders_model) + public FillsWindowFactory(TradingShellModule module, IBus bus, UserSettingsHub settings_hub, OrdersModel orders_model) : base(module, "Fills") { _bus = bus; _orders_model = orders_model; + _settings_hub = settings_hub; _dispose = bus.subscribe(SparkTags.ShowFillsTag).OnDispatcher().Subscribe(show); } + public static string col_id(Columns col) => col.ToString(); + public override Task load(StartupData state) => - Task.FromResult(new FillsWindow(this, _bus, _orders_model, state)); + Task.FromResult(_create(state)); public async void show(StartupData state) => _bus.post(SparkTags.ShowWindow, await load(state)); + IShellWindow _create(StartupData state) { + var settings_manager = new WindowSettingsManager(_settings_hub, + state.grid, + get: us => us.fills_grid, + set: (us, gs) => us.with(provided_fills_gridsettings: gs)); + return new FillsWindow(this, _bus, _orders_model, state, settings_manager); + } + [ProtoContract] public class StartupData { public const int Version = 1; - public StartupData() { } - public StartupData(uint[] orders) : this(orders, null, null) { } - public StartupData(FilterStates filters) : this(null, null, filters) { } - public StartupData(uint[] orders, List sorting_criteria, FilterStates filters) { - this.orders = orders; - this.sorting_criteria = sorting_criteria; - this.filters = filters; - } + [ProtoMember(1)] public uint[] orders; [ProtoMember(2)] - public List sorting_criteria; + List _sorting_criteria; [ProtoMember(3)] public FilterStates filters; + [ProtoMember(4)] + public GridSettings grid; + + public StartupData() { } + public StartupData(uint[] orders) : this(orders, null, null) { } + public StartupData(FilterStates filters) : this(null, null, filters) { } + public StartupData(uint[] orders, List sorting_criteria, FilterStates filters, GridSettings grid = default) { + this.orders = orders; + this._sorting_criteria = sorting_criteria; + this.filters = filters; + this.grid = grid; + } + + public static GridSettings DefaultGridSettings => GridSettings.Default.with(DefaultColumns.all, DefaultColumns.default_sorting_criterion); + public IReadOnlyList sorting_criterion => _sorting_criteria ?? (_sorting_criteria = DefaultColumns.default_sorting_criterion.ToList()); + public static StartupData Default() => + new StartupData(null, null, new FilterStates(), null); + + static class DefaultColumns { + public static GridColumnSettings security => new GridColumnSettings(col_id(Columns.Security), DefaultColumnHeaders.Security, width: DefaultColumnWidths.Security); + public static GridColumnSettings fill_time => new GridColumnSettings(col_id(Columns.FillTime), DefaultColumnHeaders.FillTime, width: DefaultColumnWidths.Time); + public static GridColumnSettings user => new GridColumnSettings(col_id(Columns.User), DefaultColumnHeaders.User, width: DefaultColumnWidths.User); + public static GridColumnSettings account => new GridColumnSettings(col_id(Columns.Account), DefaultColumnHeaders.Account, width: DefaultColumnWidths.Account); + public static GridColumnSettings exchange => new GridColumnSettings(col_id(Columns.Exchange), DefaultColumnHeaders.Exchange, width: DefaultColumnWidths.Exchange); + public static GridColumnSettings time_in_force => new GridColumnSettings(col_id(Columns.TimeInForce), DefaultColumnHeaders.TimeInForce, width: DefaultColumnWidths.TimeInForce); + public static GridColumnSettings order_type => new GridColumnSettings(col_id(Columns.OrderType), DefaultColumnHeaders.OrderType, width: DefaultColumnWidths.OrderType); + public static GridColumnSettings side => new GridColumnSettings(col_id(Columns.Side), DefaultColumnHeaders.Side, width: DefaultColumnWidths.Side); + public static GridColumnSettings quantity => new GridColumnSettings(col_id(Columns.Size), DefaultColumnHeaders.Side, width: DefaultColumnWidths.Size, horizontal_alignment: System.Windows.HorizontalAlignment.Right); + public static GridColumnSettings price => new GridColumnSettings(col_id(Columns.Price), DefaultColumnHeaders.Price, width: DefaultColumnWidths.Price, horizontal_alignment: System.Windows.HorizontalAlignment.Right); + 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 IEnumerable default_sorting_criterion { + get { + yield return new SortingCriteria(fill_time.id, false); + } + } + + public static IEnumerable all { + get { + yield return security; + yield return fill_time; + yield return user; + yield return account; + yield return exchange; + yield return time_in_force; + yield return order_type; + yield return side; + yield return quantity; + yield return price; + yield return fill_kind; + yield return fill_key; + yield return order_key; + yield return security_key; + } + } + } + } [ProtoContract] diff --git a/src/client/spark/Modules/Trading/LadderWindowFactory.cs b/src/client/spark/Modules/Trading/LadderWindowFactory.cs index 76a1c32e2..ff6a809de 100644 --- a/src/client/spark/Modules/Trading/LadderWindowFactory.cs +++ b/src/client/spark/Modules/Trading/LadderWindowFactory.cs @@ -4,6 +4,7 @@ using mbts.config; using mbts.persist; using mbts.spark; using mbts.ui; +using mbts.ui.spark; using ProtoBuf; using System; using System.Collections.Generic; @@ -22,15 +23,17 @@ namespace bts.shell { readonly IWindowManager _wm; readonly UserSettingsHub _settings_hub; readonly OrdersModel _orders_model; + readonly MarketDataAuxiliaryModel _md_auxiliary_model; readonly int _max_ladders_open; IReadOnlyDictionary _securities = new Dictionary(); - public LadderWindowFactory(IShellModule owner, IBus bus, IWindowManager wm, UserSettingsHub settings_hub, OrdersModel orders_model) + public LadderWindowFactory(IShellModule owner, IBus bus, IWindowManager wm, UserSettingsHub settings_hub, OrdersModel orders_model, MarketDataAuxiliaryModel md_auxiliary_model) : base(owner, "SparkLadder") { _bus = bus; _wm = wm; _settings_hub = settings_hub; _orders_model = orders_model; + _md_auxiliary_model = md_auxiliary_model; var knobs = Configuration.get(); _max_ladders_open = (int)(knobs.max_open_ladders); _security_timeout = TimeSpan.FromSeconds(Math.Max(15, knobs.security_timeout_secs)); @@ -56,7 +59,7 @@ namespace bts.shell { IShellWindow create_ladder_window(SecurityMD md, LadderSettings state_settings) { var id = Interlocked.Increment(ref _ladder_count) % Environment.ProcessorCount; var view = new Lazy(() => { - var model = new LadderModel(md, _orders_model); + var model = new LadderModel(md, _orders_model, _md_auxiliary_model); var settings_manager = new WindowSettingsManager( _settings_hub, state_settings, us => sanitize_ladder_settings(us.ladder), @@ -85,6 +88,12 @@ namespace bts.shell { s.session_high_line = LadderSettings.Default.session_high_line; if (s.session_range_fill == null) s.session_range_fill = LadderSettings.Default.session_range_fill; + if (s.volume_column == null) + s.volume_column = LadderSettings.Default.volume_column; + if (s.size_display == null) + s.size_display = MarketSizeDisplay.get_default(); + if (s.size_display.normal == null) + s.size_display = s.size_display.with(MarketSizeDisplay.get_default().normal); s.row_height = LadderRender.measure_row_height(s); return s; } diff --git a/src/client/spark/Modules/Trading/OrderBook/OrderBookColumns.cs b/src/client/spark/Modules/Trading/OrderBook/OrderBookColumns.cs index fa1695f6b..61d980532 100644 --- a/src/client/spark/Modules/Trading/OrderBook/OrderBookColumns.cs +++ b/src/client/spark/Modules/Trading/OrderBook/OrderBookColumns.cs @@ -3,6 +3,7 @@ using bts.api.orderman; using bts.utils; using mbts.spark.filters; using mbts.ui; +using mbts.ui.controls; using mbts.ui.converters; using mbts.ui.utility; using mbts.ui.utility.text; @@ -208,7 +209,7 @@ namespace mbts.spark { "Parent Key", nameof(OrderBookRowVM.parent_order_key), r => r.parent_order_key.store, - 80, + DefaultColumnWidths.ParentKey, new RegexFilterVM( r => r.parent_order_key.text, value_path(nameof(OrderBookRowVM.parent_order_key))), @@ -321,6 +322,7 @@ namespace mbts.spark { ListSortDirection? _cached_sort_direction; readonly string _filter_template; protected readonly Func _get_cell_store; + GlyphContext _root_glyph_context, _child_glyph_context; protected OrderbookColumnBase(Columns col_key, string header, Func get_cell_store, string sort_prop, double width, IDataGridColumnPersistentFilter filter, string filter_template) : base(col_key) { id = key.ToString(); @@ -331,6 +333,12 @@ namespace mbts.spark { this.filter = filter; _filter_template = filter_template; _cached_sort_direction = SortDirection; + font_params_changed += refresh_glyph_contexts; + refresh_glyph_contexts(); + void refresh_glyph_contexts() { + _root_glyph_context = GlyphContext.create(font_family, RenderUtils.root_font_weight); + _child_glyph_context = GlyphContext.create(font_family, RenderUtils.child_font_weight); + } } public IDataGridColumnPersistentFilter filter { get; } @@ -346,6 +354,7 @@ namespace mbts.spark { _cached_sort_direction = new_value; base.OnSortDirectionChanged(new_value); } + protected GlyphContext _get_glyph_context(OrderBookRowVM row) => row.is_child ? _child_glyph_context : _root_glyph_context; } public class SimpleTextColumn : OrderbookColumnBase, IRenderColumn { @@ -355,7 +364,7 @@ namespace mbts.spark { public void draw(OrderBookRowVM row, DrawingContext dc, ColumnGeometry position, double row_height) { var cell = _get_cell_store(row); if (!string.IsNullOrEmpty(cell.current.text)) - CellRender.draw_text(dc, RenderUtils.get_glyph_context(row), font_size, cell.current.text, position, RowHeight, + CellRender.draw_text(dc, _get_glyph_context(row), font_size, cell.current.text, position, RowHeight, font_brush, null, horizontal_alignment, VerticalAlignment.Center, DefaultCellPadding, cell.animations); } } @@ -366,7 +375,7 @@ namespace mbts.spark { : base(Columns.Side, "B/S", r => r.side.store, nameof(OrderBookRowVM.side), DefaultColumnWidths.Side, filter, DataGridFilterKnownTemplates.EnumValues) { } public void draw(OrderBookRowVM row, DrawingContext dc, ColumnGeometry position, double row_height) => - CellRender.draw_text(dc, RenderUtils.get_glyph_context(row), font_size, row.side.text, position, RowHeight, + CellRender.draw_text(dc, _get_glyph_context(row), font_size, row.side.text, position, RowHeight, RenderUtils.get_side_brush(row), null, horizontal_alignment, VerticalAlignment.Center, DefaultCellPadding, row.side.store.animations); } @@ -377,18 +386,18 @@ namespace mbts.spark { } public void draw(OrderBookRowVM row, DrawingContext dc, ColumnGeometry position, double row_height) => - CellRender.draw_text(dc, RenderUtils.get_glyph_context(row), font_size, row.quantity.text, position, RowHeight, + CellRender.draw_text(dc, _get_glyph_context(row), font_size, row.quantity.text, position, RowHeight, RenderUtils.get_side_brush(row), null, horizontal_alignment, VerticalAlignment.Center, DefaultCellPadding, row.quantity.store.animations); } public class FilledColumn : OrderbookColumnBase, IRenderColumn { public FilledColumn(IDataGridColumnPersistentFilter filter) - : base(Columns.Filled, "Filled Qty", r => r.filled_quantity.store, nameof(OrderBookRowVM.filled_quantity), DefaultColumnWidths.Size, filter, DataGridFilterKnownTemplates.UIntMinMax) { + : base(Columns.Filled, "Filled Qty", r => r.filled_quantity.store, nameof(OrderBookRowVM.filled_quantity), DefaultColumnWidths.Filled, filter, DataGridFilterKnownTemplates.UIntMinMax) { sort_member_path = nameof(OrderBookRowVM.filled_quantity); } public void draw(OrderBookRowVM row, DrawingContext dc, ColumnGeometry position, double row_height) => - CellRender.draw_text(dc, RenderUtils.get_glyph_context(row), font_size, row.filled_quantity.text, position, RowHeight, + CellRender.draw_text(dc, _get_glyph_context(row), font_size, row.filled_quantity.text, position, RowHeight, RenderUtils.get_side_brush(row), null, horizontal_alignment, VerticalAlignment.Center, DefaultCellPadding, row.filled_quantity.store.animations); } @@ -397,26 +406,26 @@ namespace mbts.spark { : base(Columns.Price, "Price", r => r.price.store, nameof(OrderBookRowVM.price), DefaultColumnWidths.Price, filter, DataGridFilterKnownTemplates.DoubleMinMax) { } public void draw(OrderBookRowVM row, DrawingContext dc, ColumnGeometry position, double row_height) => - CellRender.draw_text(dc, RenderUtils.get_glyph_context(row), font_size, row.price.text, + CellRender.draw_text(dc, _get_glyph_context(row), font_size, row.price.text, position, RowHeight, font_brush, null, horizontal_alignment, VerticalAlignment.Center, DefaultCellPadding, row.price.store.animations); } public class AvgFilledColumn : OrderbookColumnBase, IRenderColumn { public AvgFilledColumn(IDataGridColumnPersistentFilter filter) - : base(Columns.AvgFilled, "Avg Filled", r => r.avg_price.store, nameof(OrderBookRowVM.avg_price), DefaultColumnWidths.Price, filter, DataGridFilterKnownTemplates.DoubleMinMax) { } + : base(Columns.AvgFilled, "Avg Filled", r => r.avg_price.store, nameof(OrderBookRowVM.avg_price), DefaultColumnWidths.AveragePrice, filter, DataGridFilterKnownTemplates.DoubleMinMax) { } public void draw(OrderBookRowVM row, DrawingContext dc, ColumnGeometry position, double row_height) { - CellRender.draw_text(dc, RenderUtils.get_glyph_context(row), font_size, row.avg_price.text, + CellRender.draw_text(dc, _get_glyph_context(row), font_size, row.avg_price.text, position, RowHeight, font_brush, null, horizontal_alignment, VerticalAlignment.Center, DefaultCellPadding, row.avg_price.store.animations); } } public class LastFilledColumn : OrderbookColumnBase, IRenderColumn { public LastFilledColumn(IDataGridColumnPersistentFilter filter) - : base(Columns.LastPrice, "Last Filled", r => r.last_price.store, nameof(OrderBookRowVM.last_price), DefaultColumnWidths.Price, filter, DataGridFilterKnownTemplates.DoubleMinMax) { } + : base(Columns.LastPrice, "Last Filled", r => r.last_price.store, nameof(OrderBookRowVM.last_price), DefaultColumnWidths.LastPrice, filter, DataGridFilterKnownTemplates.DoubleMinMax) { } public void draw(OrderBookRowVM row, DrawingContext dc, ColumnGeometry position, double row_height) => - CellRender.draw_text(dc, RenderUtils.get_glyph_context(row), font_size, row.last_price.text, + CellRender.draw_text(dc, _get_glyph_context(row), font_size, row.last_price.text, position, RowHeight, font_brush, null, horizontal_alignment, VerticalAlignment.Center, DefaultCellPadding, row.last_price.store.animations); } @@ -425,7 +434,7 @@ namespace mbts.spark { : base(Columns.Time, "Time", r => r.time_created.store, nameof(OrderBookRowVM.time_created), DefaultColumnWidths.Time, filter, DataGridFilterKnownTemplates.TimeFilter) { } public void draw(OrderBookRowVM row, DrawingContext dc, ColumnGeometry position, double row_height) => - CellRender.draw_text(dc, RenderUtils.get_glyph_context(row), font_size, row.time_created.text, + CellRender.draw_text(dc, _get_glyph_context(row), font_size, row.time_created.text, position, RowHeight, font_brush, null, horizontal_alignment, VerticalAlignment.Center, DefaultCellPadding, row.time_created.store.animations); } @@ -434,7 +443,7 @@ namespace mbts.spark { : base(Columns.LastChange, "Last Change", r => r.last_change.store, nameof(OrderBookRowVM.last_change), DefaultColumnWidths.Time, filter, DataGridFilterKnownTemplates.TimeFilter) { } public void draw(OrderBookRowVM row, DrawingContext dc, ColumnGeometry position, double row_height) => - CellRender.draw_text(dc, RenderUtils.get_glyph_context(row), font_size, row.last_change.text, + CellRender.draw_text(dc, _get_glyph_context(row), font_size, row.last_change.text, position, RowHeight, font_brush, null, horizontal_alignment, VerticalAlignment.Center, DefaultCellPadding, row.last_change.store.animations); } @@ -470,7 +479,7 @@ namespace mbts.spark { id = key.ToString(); this.filter = filter; Header = "Last Message"; - Width = 80; + Width = DIYGrid.DefaultColumnWidths.LastMessage; sort_member_path = nameof(OrderBookRowVM.last_msg); tooltip_binding = new Binding(nameof(OrderBookRowVM.last_msg)); } @@ -521,7 +530,9 @@ namespace mbts.spark { toggle_button.SetBinding(ToggleButton.VisibilityProperty, new Binding(nameof(OrderBookRowVM.is_parent)) { Converter = _expander_visiblilty_converter }); toggle_button.SetResourceReference(ToggleButton.StyleProperty, ExpandCollapseToggleStyleKey); // - var security_view = new TextBlock { FontFamily = font_family, FontSize = font_size, Foreground = font_brush }; + var security_view = new TextBlock { Foreground = font_brush }; + security_view.SetBinding(TextBlock.FontFamilyProperty, new Binding(nameof(font_family)) { Source = this }); + security_view.SetBinding(TextBlock.FontSizeProperty, new Binding(nameof(font_size)) { Source = this }); security_view.SetBinding(TextBlock.TextProperty, $"{nameof(OrderBookRowVM.security)}.{nameof(IGridCell.text)}"); security_view.SetBinding(TextBlock.HorizontalAlignmentProperty, new Binding(nameof(horizontal_alignment)) { Source = this }); security_view.SetBinding(TextBlock.ForegroundProperty, new Binding(nameof(font_brush)) { Source = this }); @@ -548,7 +559,10 @@ namespace mbts.spark { #endregion static class RenderUtils { - public static readonly GlyphContext _child_glyph_context = TextUtils.StandardGlyphContext.with(FontWeights.Light); + public static FontWeight + root_font_weight = FontWeights.Normal, + child_font_weight = FontWeights.Light; + public static double font_size = FontSizes.Small; public static readonly IValueConverter @@ -567,7 +581,6 @@ namespace mbts.spark { return mb; } - public static GlyphContext get_glyph_context(OrderBookRowVM r) => r.is_child ? _child_glyph_context : TextUtils.StandardGlyphContext; public static Brush get_side_brush(OrderBookRowVM r) => r.side.value == Side.Buy ? BTSColors.MarketData.bid : BTSColors.MarketData.ask; public static BindingBase get_font_weight_binding() => new Binding(nameof(OrderBookRowVM.is_child)) { Converter = _font_weight_converter }; } diff --git a/src/client/spark/Modules/Trading/OrderBook/OrderBookVM.cs b/src/client/spark/Modules/Trading/OrderBook/OrderBookVM.cs index 04bfd3d7e..b07c705ba 100644 --- a/src/client/spark/Modules/Trading/OrderBook/OrderBookVM.cs +++ b/src/client/spark/Modules/Trading/OrderBook/OrderBookVM.cs @@ -4,6 +4,7 @@ using bts.api.core; using bts.api.orderman; using bts.shell; using mbts.collections; +using mbts.persist; using mbts.ui; using mbts.ui.controls; using mbts.ui.settings; @@ -23,12 +24,17 @@ using System.Windows.Data; using System.Windows.Input; using static bts.shell.OrderBookWindowFactory; using static mbts.spark.OrderBookView; +using _GridSettingsManager = mbts.ui.GridSettingsManager; namespace mbts.spark { partial class OrderBookVM : DisposableViewModelBase, IHandleRowSelection { readonly LiveSortingColumKeys _live_sorting_columns; readonly Property _current_row; + readonly Subject<(IEnumerable added, IEnumerable removed)> _order_securities_subject = + new Subject<(IEnumerable added, IEnumerable removed)>(); + readonly IWindowSettingsManager _settings_manager; OrderBookStartupData _state; + Dictionary _order_rows_cache = new Dictionary(); Dictionary _branch_filter_roots = new Dictionary(); ObservableCollection _order_rows = new ObservableCollection(); @@ -36,7 +42,8 @@ namespace mbts.spark { SetMap _parent_to_children = new SetMap(); SetMap _security_orders = new SetMap(); - public OrderBookVM(IObservable owner_title, IBus bus, INotificationService notification_service, OrdersModel model) { + public OrderBookVM(IObservable owner_title, IBus bus, INotificationService notification_service, OrdersModel model, IWindowSettingsManager settings_manager) { + _settings_manager = settings_manager; var order_securities_subject = new Subject<(IEnumerable added, IEnumerable removed)>(); columns = new OrderBookColumnCollection(bus, model, order_securities_subject); rows_view = CollectionViewSource.GetDefaultView(_order_rows); @@ -63,6 +70,7 @@ namespace mbts.spark { }); grid_settings_manager = _create_grid_settings_manager(owner_title, bus, notification_service, model, _order_rows, rows_view, columns); + grid_settings_manager.load_from(OrderBookStartupData.DefaultGrid); _live_sorting_columns = new LiveSortingColumKeys(grid_settings_manager.live_sorting_props); SetMap missing_security_orders = new SetMap(); @@ -71,6 +79,7 @@ namespace mbts.spark { _dispose = Disposable.Create( bus.subscribe(SparkTags.SecuritiesTag).OnDispatcher().Subscribe(on_securities), model.on_orders.OnDispatcher().Subscribe(on_orders), + settings_manager.subscribe_current().OnDispatcher().Subscribe(grid_settings_manager), grid_settings_manager, columns, order_securities_subject); @@ -215,7 +224,7 @@ namespace mbts.spark { public OrderRecordImmutable? current_order => current_row?.order; public IEnumerable selected_orders => _selected_rows.Select(r => r.order); - public GridSettingsManager grid_settings_manager { get; } + public _GridSettingsManager grid_settings_manager { get; } public ICommand collapse_all_rows_command { get; } public ICommand expand_all_rows_command { get; } @@ -225,14 +234,17 @@ namespace mbts.spark { } public OrderBookStartupData create() { - var (grid, animations) = grid_settings_manager.create(); - return new OrderBookStartupData(columns.create(), grid, animations.Select(a => new OrderRecordTuplePredicateAnimations(a.predicate_builder, a.cell_animations, a.row_animations, a.sound_notifications))); + if (_settings_manager.instance_settings == null) + return new OrderBookStartupData(columns.create(), use_defaults: true); + var gs = grid_settings_manager.create(); + return new OrderBookStartupData(columns.create(), gs.general, gs.predicate_animations, use_defaults: false); } public void load_from(OrderBookStartupData state) { if (state == null) return; _state = state; - grid_settings_manager.load_from((state.grid, state.predicate_animations)); + var gs = state.grid_settings_wrapper(); + if (gs != null) grid_settings_manager.load_from(gs); columns.load_from(state.filters); } @@ -242,7 +254,7 @@ namespace mbts.spark { protected override void Dispose(bool disposing) => Disposable.dispose(_order_rows); - static GridSettingsManager _create_grid_settings_manager( + static _GridSettingsManager _create_grid_settings_manager( IObservable owner_title, IBus bus, INotificationService notification_service, OrdersModel model, ObservableCollection rows, ICollectionView rows_view, ObservableCollection columns) { // make font-weight immutable for all columns (the font-weight rule is hard-coded: Regular for root-orders, Light for all others) @@ -254,7 +266,7 @@ namespace mbts.spark { foreach (var c in new[] { Columns.Side, Columns.Size, Columns.Filled }) { col_settings_flags[c.ToString()] = GridColumnSettingsFeatureFlags.ImmutableFontColor | GridColumnSettingsFeatureFlags.ImmutableFontWeight; } - return new GridSettingsManager( + return new _GridSettingsManager( owner_title, bus, notification_service, model, rows, rows_view, columns, () => { var pvm = PredicateBuilderVMWrapper.create( @@ -267,7 +279,9 @@ namespace mbts.spark { OrderRecordTuplePredicateVMFactory.get_oneofs(bus, model), rp => rp.predicate_type_case); pvm.selected_predicate_type = _corresponding_predicate_type(EnumHelper.parse_or_default(col_id)); return pvm; - }, notification_service.data_cache, col_settings_flags); + }, + (gen, anims) => new OrderBookGridSettings(gen, anims.Select(a => new OrderRecordTuplePredicateAnimations(a.predicate_builder, a.cell_animations, a.row_animations, a.sound_notifications))), + notification_service.data_cache, col_settings_flags); } static OrderRecordTuplePredicate.PredicateTypeOneofCase _corresponding_predicate_type(Columns col) { diff --git a/src/client/spark/Modules/Trading/OrderBook/OrderBookWindow.cs b/src/client/spark/Modules/Trading/OrderBook/OrderBookWindow.cs index cce32129f..398d78020 100644 --- a/src/client/spark/Modules/Trading/OrderBook/OrderBookWindow.cs +++ b/src/client/spark/Modules/Trading/OrderBook/OrderBookWindow.cs @@ -2,18 +2,27 @@ using bts.api.orderman; using bts.shell; using bts.spark; +using mbts.persist; using mbts.ui; +using mbts.ui.converters; +using mbts.ui.spark; +using mbts.ui.utility; using System; using System.Collections.Generic; using System.Linq; using System.Reactive.Linq; using System.Windows; +using System.Windows.Data; using System.Windows.Media; +//using IOrderBookWSM = mbts.persist.IWindowSettingsManager>; +using OrderBookGridSettings = bts.shell.OrderBookWindowFactory.OrderBookGridSettings; +using OrderBookGridSettingsVM = mbts.spark.GridSettingsVM; namespace mbts.spark { internal class OrderBookWindow : SparkWindow, ISharedWindow { - const string _window_type = "OrderBook", ORDERBOOK_THREAD_ID = "OrderBook"; + const string _window_type = "OrderBook", ORDERBOOK_THREAD_ID = "OrderBook", _default_title = "Order Book"; readonly INotificationService _notification_service; + readonly IWindowSettingsManager _settings_manager; OrderBookWindowFactory.OrderBookStartupData _state; OrdersModel _model; OrderBookVM _vm; @@ -32,12 +41,14 @@ namespace mbts.spark { , _toggle_expand_all_command , _show_grid_settings_command; - public OrderBookWindow(OrdersModel model, OrderBookWindowFactory factory, IBus bus, INotificationService notification_service, OrderBookWindowFactory.OrderBookStartupData state, ImageSource icon) - : base("Order Book", bus, factory, SparkCommands.ShowOrders.icon) { + public OrderBookWindow(OrdersModel model, OrderBookWindowFactory factory, IBus bus, INotificationService notification_service, + OrderBookWindowFactory.OrderBookStartupData state, ImageSource icon, IWindowSettingsManager settings_manager) + : base(_default_title, bus, factory, SparkCommands.ShowOrders.icon) { _model = model; _factory = factory; _notification_service = notification_service; _state = state; + _settings_manager = settings_manager; } public override OrderBookWindowFactory.OrderBookStartupData SaveState => _state = _vm.create(); @@ -45,8 +56,11 @@ namespace mbts.spark { public override string window_type => _window_type; public override UIElement Content => _view; public override IEnumerable Commands => _commands; - public override double MinWidth => 1577; - + public override double MinWidth => + OrderBookWindowFactory.OrderBookStartupData.DefaultGrid.general.columns.Sum(gcs => gcs.width) + + SPARK_MENU_COLLAPSED_WIDTH + + LAUNCHER_BUTTON_BORDER_WIDTH + + EXTRA_FOR_NO_HORIZONTAL_SCROLLBAR_WIDTH; public string thread_id => ORDERBOOK_THREAD_ID; public override void Dispose() { @@ -55,7 +69,7 @@ namespace mbts.spark { } public void thread_init() { - _vm = new OrderBookVM(this.observe_title(), _bus, _notification_service, _model); + _vm = new OrderBookVM(this.observe_title(), _bus, _notification_service, _model, _settings_manager); _vm.load_from(_state); _view = new OrderBookView(_vm); @@ -106,9 +120,32 @@ namespace mbts.spark { Icons.DownArrow, _vm.expand_all_rows_command); - _show_grid_settings_command = SparkCommands.window_settings_command(_vm.grid_settings_manager.launch_settings_editor); + _show_grid_settings_command = SparkCommands.window_settings_command(new DialogCommand(() => { + var vm = new WindowSettingsDialogVM( + _settings_manager, + us => us.order_book_grid, + UserSettings.FileExtensions.GRID, + s => { + if (s.general is null) + s = OrderBookWindowFactory.OrderBookStartupData.DefaultGrid; + return _vm.grid_settings_manager.create_vm( + // we need to get the most up to date GridSettings when launching b/c users may + // have changed some settings on the grid directly, like column widths. + s.general.with(_vm.columns.Select(c => c.create())), + this.observe_title() + ); + } + ); + // TODO: bind to main window's Title + var dialog = new SimpleGridSettingsDialog { DataContext = vm }; + dialog.SetBinding(Window.TitleProperty, new Binding($"{nameof(vm.instance_vm)}.{nameof(OrderBookGridSettingsVM.owner_title)}") { + Converter = ValueConverter.create(str => (string.IsNullOrEmpty(str) ? _default_title : str) + " Settings") + }); + vm.RequestClose += (s, e) => WindowUtilities.force_close(dialog); + return dialog; + })); - _commands = new SparkCommand[] { + _commands = new SparkCommand[] { _show_fills_command, _modify_order_command, _cancel_selected_orders_command, diff --git a/src/client/spark/Modules/Trading/OrderBook/OrderBookWindowFactory.cs b/src/client/spark/Modules/Trading/OrderBook/OrderBookWindowFactory.cs index c3037472b..78d8978fd 100644 --- a/src/client/spark/Modules/Trading/OrderBook/OrderBookWindowFactory.cs +++ b/src/client/spark/Modules/Trading/OrderBook/OrderBookWindowFactory.cs @@ -1,8 +1,9 @@ using bts.api; using bts.api.orderman; using bts.spark; -using Google.Protobuf.Collections; +using bts.utils; using mbts; +using mbts.persist; using mbts.spark; using mbts.spark.filters; using mbts.ui; @@ -11,6 +12,7 @@ using mbts.ui.settings; using ProtoBuf; using System; using System.Collections.Generic; +using System.ComponentModel; using System.Reactive.Linq; using System.Threading.Tasks; using System.Windows; @@ -22,19 +24,22 @@ namespace bts.shell { readonly INotificationService _notification_service; readonly INotificationServiceDataCache _notification_service_data_cache; readonly OrdersModel _model; + readonly UserSettingsHub _settings_hub; - public OrderBookWindowFactory(IShellModule owner, IBus bus, INotificationService notification_service, INotificationServiceDataCache notification_service_data_cache, OrdersModel model) : base(owner, "SparkOrderBookWindow") { + public OrderBookWindowFactory(IShellModule owner, IBus bus, INotificationService notification_service, INotificationServiceDataCache notification_service_data_cache, OrdersModel model, UserSettingsHub settings_hub) : base(owner, "SparkOrderBookWindow") { _bus = bus; _notification_service = notification_service; _notification_service_data_cache = notification_service_data_cache; _model = model; - + _settings_hub = settings_hub; _dispose = bus.subscribe(SparkTags.ShowOrderBookTag).OnDispatcher().Subscribe(show); } public override Task load(OrderBookStartupData state) { - var w = new OrderBookWindow(_model, this, _bus, _notification_service, state, SparkCommands.ShowOrders.icon) as IShellWindow; - return Task.FromResult(w); + var settings_manager = new WindowSettingsManager(_settings_hub, state.grid_settings_wrapper(), + us => us.order_book_grid, (us, s) => us.with(order_book_grid: s)); + var w = new OrderBookWindow(_model, this, _bus, _notification_service, state, SparkCommands.ShowOrders.icon, settings_manager.as_non_shared()); + return Task.FromResult(w); } async void show(OrderBookStartupData startup_data) { @@ -49,27 +54,42 @@ namespace bts.shell { [ProtoMember(10)] public FilterStates filters; [ProtoMember(15)] + [Obsolete] public GridSettings grid; [ProtoMember(20)] + [Obsolete] List _predicate_animations; + [ProtoMember(25)] + public bool use_defaults; public OrderBookStartupData() { } - public OrderBookStartupData(FilterStates filters, GridSettings grid = default, IEnumerable predicate_animations = null) { + public OrderBookStartupData(FilterStates filters, GridSettings grid = default, IEnumerable predicate_animations = null, bool use_defaults = false) { this.filters = filters; + this.use_defaults = use_defaults; +#pragma warning disable CS0612 // Type or member is obsolete this.grid = grid; _predicate_animations = new List(); if (predicate_animations != null) _predicate_animations.AddRange(predicate_animations); +#pragma warning restore CS0612 // Type or member is obsolete } +#pragma warning disable CS0612 // Type or member is obsolete + [Obsolete] public IReadOnlyList predicate_animations => _predicate_animations ?? (_predicate_animations = new List()); + public OrderBookGridSettings grid_settings_wrapper() => use_defaults ? null : new OrderBookGridSettings(grid, predicate_animations); +#pragma warning restore CS0612 // Type or member is obsolete public static OrderBookStartupData Default => new OrderBookStartupData( new FilterStates { statuses = new[] { OrderStatus.Working } - }, GridSettings.Default.with(DefaultColumns.all, DefaultColumns.sorting_criterion)); + }, _default_general_grid, use_defaults: true); + + public static OrderBookGridSettings DefaultGrid => new OrderBookGridSettings(_default_general_grid); + + static GridSettings _default_general_grid => GridSettings.Default.with(DefaultColumns.all, DefaultColumns.sorting_criterion); static class DefaultColumns { public static GridColumnSettings security => new GridColumnSettings(Columns.Security.ToString(), DIYGrid.DefaultColumnHeaders.Security, width: DIYGrid.DefaultColumnWidths.Security); @@ -85,14 +105,14 @@ namespace bts.shell { public static GridColumnSettings side => new GridColumnSettings(Columns.Side.ToString(), "B/S", width: DIYGrid.DefaultColumnWidths.Side); public static GridColumnSettings quantity => new GridColumnSettings(Columns.Size.ToString(), "Qty", width: DIYGrid.DefaultColumnWidths.Size, horizontal_alignment: HorizontalAlignment.Right); public static GridColumnSettings price => new GridColumnSettings(Columns.Price.ToString(), DIYGrid.DefaultColumnHeaders.Price, width: DIYGrid.DefaultColumnWidths.Price, horizontal_alignment: HorizontalAlignment.Right); - public static GridColumnSettings filled_quantity => new GridColumnSettings(Columns.Filled.ToString(), "Filled Qty", width: DIYGrid.DefaultColumnWidths.Size, horizontal_alignment: HorizontalAlignment.Right); - public static GridColumnSettings avg_price => new GridColumnSettings(Columns.AvgFilled.ToString(), "Avg Filled", width: DIYGrid.DefaultColumnWidths.Price, horizontal_alignment: HorizontalAlignment.Right); - public static GridColumnSettings last_price => new GridColumnSettings(Columns.LastPrice.ToString(), "Last Filled", width: DIYGrid.DefaultColumnWidths.Price, horizontal_alignment: HorizontalAlignment.Right); + public static GridColumnSettings filled_quantity => new GridColumnSettings(Columns.Filled.ToString(), "Filled Qty", width: DIYGrid.DefaultColumnWidths.Filled, horizontal_alignment: HorizontalAlignment.Right); + public static GridColumnSettings avg_price => new GridColumnSettings(Columns.AvgFilled.ToString(), "Avg Filled", width: DIYGrid.DefaultColumnWidths.AveragePrice, horizontal_alignment: HorizontalAlignment.Right); + 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: 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 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.OrderKey); + public static GridColumnSettings parent_key => new GridColumnSettings(Columns.ParentKey.ToString(), "Parent Key", width: DIYGrid.DefaultColumnWidths.ParentKey); public static IEnumerable sorting_criterion { get { @@ -171,5 +191,38 @@ namespace bts.shell { [ProtoMember(20)] public string[] symbols; } + + [ProtoContract] + public class OrderBookGridSettings : IGridSettingsWithNotifications, IEquatable { + [ProtoMember(1)] + public GridSettings general { get; set; } + [ProtoMember(2)] + List _predicate_animations; + + public OrderBookGridSettings() { } + public OrderBookGridSettings(GridSettings general = default, IEnumerable predicate_animations = null) { + this.general = general; + _predicate_animations = new List(); + if (predicate_animations != null) + _predicate_animations.AddRange(predicate_animations); + } + + public IReadOnlyList predicate_animations => _predicate_animations ?? (_predicate_animations = new List()); + IEnumerable> IGridSettingsWithNotifications.predicate_animations => predicate_animations; + + public OrderBookGridSettings with(GridSettings general) => + new OrderBookGridSettings(general ?? this.general, predicate_animations); + + public bool Equals(OrderBookGridSettings other) => + other != null && + EqualityComparer.null_check_equals(general, other.general) && + EqualityComparer.safe_sequence_equals(predicate_animations, other.predicate_animations); + + public override bool Equals(object obj) => Equals(obj as OrderBookGridSettings); + public override int GetHashCode() => + EqualityComparer.aggregate_hash( + EqualityComparer.safe_get_hash_code(general), + EqualityComparer.safe_aggregate_hash(predicate_animations)); + } } } \ No newline at end of file diff --git a/src/client/spark/Modules/Trading/PAndLWindowFactory.cs b/src/client/spark/Modules/Trading/PAndLWindowFactory.cs index 270b95ad4..27eba0300 100644 --- a/src/client/spark/Modules/Trading/PAndLWindowFactory.cs +++ b/src/client/spark/Modules/Trading/PAndLWindowFactory.cs @@ -1,30 +1,38 @@ using bts.api; using bts.api.orderman; using bts.spark; +using mbts.persist; using mbts.spark; using mbts.spark.filters; using mbts.ui; +using mbts.ui.settings; using mbts.ui.spark; using ProtoBuf; using System; using System.Collections.Generic; +using System.Linq; using System.Reactive.Linq; using System.Threading.Tasks; +using static mbts.spark.PAndLView; +using static mbts.ui.controls.DIYGrid; namespace bts.shell { internal class PAndLWindowFactory : ShellWindowFactory { readonly IBus _bus; readonly PAndLModel _model; - public PAndLWindowFactory(IShellModule owner, IBus bus, PAndLModel model) : base(owner, "SparkPAndL") { + readonly UserSettingsHub _settings_hub; + + public PAndLWindowFactory(IShellModule owner, IBus bus, UserSettingsHub settings_hub, PAndLModel model) : base(owner, "SparkPAndL") { _bus = bus; _model = model; - + _settings_hub = settings_hub; _dispose = bus.subscribe(SparkTags.ShowPAndLTag).OnDispatcher().Subscribe(show); } + public static string col_id(Columns col) => col.ToString(); + public override Task load(PAndLStartupData state) { - var w = new PAndLWindow(_model, this, _bus, state, SparkCommands.ShowPAndL.icon) as IShellWindow; - return Task.FromResult(w); + return Task.FromResult(_create(state)); } async void show(PAndLStartupData startup_data) { @@ -32,6 +40,14 @@ namespace bts.shell { _bus.post(SparkTags.ShowWindow, w); } + IShellWindow _create(PAndLStartupData state) { + var settings_manager = new WindowSettingsManager(_settings_hub, + state.grid, + get: us => us.pnl_grid, + set: (us, gs) => us.with(provided_pnl_gridsettings: gs)); + return new PAndLWindow(_model, this, _bus, state, SparkCommands.ShowPAndL.icon, settings_manager); + } + [ProtoContract] public class PAndLStartupData { @@ -40,21 +56,75 @@ namespace bts.shell { /// public PAndLStartupData() { } - public PAndLStartupData(List sorting_criteria, FilterStates filters, pnl_calc_type calc_type) { - this.sort_criteria = sorting_criteria; + public PAndLStartupData(List sorting_criteria, FilterStates filters, pnl_calc_type calc_type, GridSettings grid = default) { + _sort_criteria = sorting_criteria; this.filters = filters; this.calc_type = calc_type; + this.grid = grid; } + public static GridSettings DefaultGridSettings => GridSettings.Default.with(DefaultColumns.all, DefaultColumns.default_sorting_criterion); + + public IReadOnlyList sorting_criterion => _sort_criteria ?? (_sort_criteria = DefaultColumns.default_sorting_criterion.ToList()); + public static PAndLStartupData Default() => + new PAndLStartupData(null, new FilterStates(), pnl_calc_type.midpoint); + + [ProtoMember(5)] - public readonly List sort_criteria; + List _sort_criteria; [ProtoMember(10)] public FilterStates filters; [ProtoMember(15)] public pnl_calc_type calc_type; //[ProtoMember(20)] // ProtoMember 20 cannot be used further + [ProtoMember(25)] + public GridSettings grid; + static class DefaultColumns { + + public static GridColumnSettings account => new GridColumnSettings(col_id(Columns.Account), DefaultColumnHeaders.Account, width: DefaultColumnWidths.Account); + public static GridColumnSettings exchange => new GridColumnSettings(col_id(Columns.Exchange), DefaultColumnHeaders.Exchange, width: DefaultColumnWidths.Exchange); + public static GridColumnSettings symbol => new GridColumnSettings(col_id(Columns.Symbol), DefaultColumnHeaders.Symbol, width: DefaultColumnWidths.Symbol); + public static GridColumnSettings security => new GridColumnSettings(col_id(Columns.Security), DefaultColumnHeaders.Security, width: DefaultColumnWidths.Security); + public static GridColumnSettings security_type => new GridColumnSettings(col_id(Columns.SecurityType), DefaultColumnHeaders.SecurityType, width: DefaultColumnWidths.SecurityType); + public static GridColumnSettings start_net_position => new GridColumnSettings(col_id(Columns.StartNetPos), DefaultColumnHeaders.StartNetPos, width: DefaultColumnWidths.Start_Net_Pos, horizontal_alignment: System.Windows.HorizontalAlignment.Right); + public static GridColumnSettings start_pnl => new GridColumnSettings(col_id(Columns.StartPNL), DefaultColumnHeaders.StartPnL, width: DefaultColumnWidths.PnL_Generic, horizontal_alignment: System.Windows.HorizontalAlignment.Right); + public static GridColumnSettings day_long_pnl => new GridColumnSettings(col_id(Columns.DayLong), DefaultColumnHeaders.DayLong, width: DefaultColumnWidths.PnL_Generic, horizontal_alignment: System.Windows.HorizontalAlignment.Right); + public static GridColumnSettings day_short_pnl => new GridColumnSettings(col_id(Columns.DayShort), DefaultColumnHeaders.DayShort, width: DefaultColumnWidths.PnL_Generic, horizontal_alignment: System.Windows.HorizontalAlignment.Right); + public static GridColumnSettings day_pnl => new GridColumnSettings(col_id(Columns.DayPNL), DefaultColumnHeaders.DayPnL, width: DefaultColumnWidths.PnL_Generic, horizontal_alignment: System.Windows.HorizontalAlignment.Right); + public static GridColumnSettings total_net_pos => new GridColumnSettings(col_id(Columns.TotalNetPos), DefaultColumnHeaders.TotalNetPos, width: DefaultColumnWidths.Total_Net_Pos, horizontal_alignment: System.Windows.HorizontalAlignment.Right); + public static GridColumnSettings total_pnl => new GridColumnSettings(col_id(Columns.TotalPNL), DefaultColumnHeaders.TotalPnL, width: DefaultColumnWidths.PnL_Generic, horizontal_alignment: System.Windows.HorizontalAlignment.Right); + public static GridColumnSettings pnl_price => new GridColumnSettings(col_id(Columns.PNLPrice), DefaultColumnHeaders.PnLPrice, width: DefaultColumnWidths.PnL_Generic, horizontal_alignment: System.Windows.HorizontalAlignment.Right); + + public static IEnumerable default_sorting_criterion { + get { + yield return new SortingCriteria(account.id, true); + yield return new SortingCriteria(exchange.id, true); + yield return new SortingCriteria(symbol.id, true); + yield return new SortingCriteria(security.id, true); + } + } + + public static IEnumerable all { + get { + yield return account; + yield return exchange; + yield return symbol; + yield return security; + yield return security_type; + yield return start_net_position; + yield return start_pnl; + yield return day_long_pnl; + yield return day_short_pnl; + yield return day_pnl; + yield return total_net_pos; + yield return total_pnl; + yield return pnl_price; + } + } + + } } [ProtoContract] diff --git a/src/client/spark/Modules/Trading/TradingShellModule.cs b/src/client/spark/Modules/Trading/TradingShellModule.cs index 9924f00ff..22a6c2f14 100644 --- a/src/client/spark/Modules/Trading/TradingShellModule.cs +++ b/src/client/spark/Modules/Trading/TradingShellModule.cs @@ -1,4 +1,5 @@ using bts.api; +using bts.api.md.auxiliary; using bts.spark; using mbts.persist; using mbts.spark; @@ -19,11 +20,12 @@ namespace bts.shell { PAndLWindowFactory _plwf; [ImportingConstructor] - public TradingShellModule(IBus bus, IWindowManager wm, INotificationService notification_service, INotificationServiceDataCache notification_service_data_cache, UserSettingsHub settings_hub, OrdersModel orders_model, PAndLModel pandl_model) { - _lwf = new LadderWindowFactory(this, bus, wm, settings_hub, orders_model); - _obwf = new OrderBookWindowFactory(this, bus, notification_service, notification_service_data_cache, orders_model); - _fwf = new FillsWindowFactory(this, bus, orders_model); - _plwf = new PAndLWindowFactory(this, bus, pandl_model); + public TradingShellModule(IBus bus, IWindowManager wm, INotificationService notification_service, INotificationServiceDataCache notification_service_data_cache, + UserSettingsHub settings_hub, OrdersModel orders_model, PAndLModel pandl_model, MarketDataAuxiliaryModel md_auxiliary_model) { + _lwf = new LadderWindowFactory(this, bus, wm, settings_hub, orders_model, md_auxiliary_model); + _obwf = new OrderBookWindowFactory(this, bus, notification_service, notification_service_data_cache, orders_model, settings_hub); + _fwf = new FillsWindowFactory(this, bus, settings_hub, orders_model); + _plwf = new PAndLWindowFactory(this, bus, settings_hub, pandl_model); } public ImageSource icon => Icons.Trades; diff --git a/src/client/spark/Orders/OrdersModel.cs b/src/client/spark/Orders/OrdersModel.cs index 52241bf9e..2f3e9ee16 100644 --- a/src/client/spark/Orders/OrdersModel.cs +++ b/src/client/spark/Orders/OrdersModel.cs @@ -34,7 +34,7 @@ namespace mbts.spark { [ImportingConstructor] private OrdersModel(IBus bus, OrderFactory factory, OrderService service, PositionService position_service, - SpreaderModel spreader, ErrorReporter error, DynamicOrderHelper dynamic_helper) { + SpreaderModel spreader, ErrorReporter error, DynamicOrderHelper dynamic_helper, AdminModel admin_model) { _bus = bus; _factory = factory; _service = service; @@ -44,15 +44,15 @@ namespace mbts.spark { _dynamic_helper = dynamic_helper; var scheduler = ThreadPoolScheduler.Instance; var interval = TimeSpan.FromMilliseconds(100); - on_orders = _service.subscribei_orders().Naggle(interval, scheduler).Table().Cache(); - on_positions = _position_service.subscribei_position_data().Naggle(interval, scheduler).Table().Cache(); + on_orders = _service.subscribei_orders().Naggle(interval, scheduler).PermissionFilter(admin_model).Table().Cache(); + on_positions = _position_service.subscribei_position_data().Naggle(interval, scheduler).PermissionFilter(admin_model).Table().Cache(); working_orders = on_orders.Scan( ImmutableDictionary.Empty, (workers, table) => _update_working_orders(workers, table)) .Replay(1).RefCount(); on_order_tuples = OrderRecordTupleSource.create_observable(bus.subscribe(SparkTags.SecuritiesTag), on_orders); - on_fills = _service.subscribei_fills().Naggle(interval, scheduler).Table().Cache(); + on_fills = _service.subscribei_fills().Naggle(interval, scheduler).PermissionFilter(admin_model).Table().Cache(); _activity = new OrderActivitySummarizer(bus, on_orders); on_fill_tuples = FillTupleSource.create_observable(bus.subscribe(SparkTags.SecuritiesTag), on_orders, on_fills); _security_orders_demuxer = new TableUpdateDemuxer(o => o.Key, o => o.security_key()); diff --git a/src/client/spark/Spark.csproj b/src/client/spark/Spark.csproj index 76a20bc1a..b6f3b57a2 100644 --- a/src/client/spark/Spark.csproj +++ b/src/client/spark/Spark.csproj @@ -75,6 +75,11 @@ Code + + + MSBuild:Compile + + diff --git a/src/client/spark/core/Bus/SparkCommands.cs b/src/client/spark/core/Bus/SparkCommands.cs index f134ace8a..bb516d5c3 100644 --- a/src/client/spark/core/Bus/SparkCommands.cs +++ b/src/client/spark/core/Bus/SparkCommands.cs @@ -113,10 +113,10 @@ namespace bts.spark // TODO: remove this method, eventually want all windows to use window_settings_command() public static SparkCommand change_securities_command(Func<(PartialCommand, SecurityCriteria)> get_param) => - SparkCommand.create("Settings", Icons.Admin, SparkTags.ShowAndEditPickMultiple, get_param); + SparkCommand.create("Securities", Icons.HandPointingOverDocument, SparkTags.ShowAndEditPickMultiple, get_param); public static PartialCommand change_securities_callback(string window_name) => - SparkCommand.create(window_name, Icons.Admin, new SparkTags.SparkTag($"Change {window_name} securities callback")); + SparkCommand.create(window_name, Icons.HandPointingOverDocument, new SparkTags.SparkTag($"Change {window_name} securities callback")); public static SparkCommand window_settings_command(IDialogCommand launch_settings_editor) => new SparkCommandWrapper("Settings", Icons.Admin, launch_settings_editor); diff --git a/src/client/spark/core/Exceptions/ExceptionBuilder.cs b/src/client/spark/core/Exceptions/ExceptionBuilder.cs index 71d91df74..262f451fe 100644 --- a/src/client/spark/core/Exceptions/ExceptionBuilder.cs +++ b/src/client/spark/core/Exceptions/ExceptionBuilder.cs @@ -5,6 +5,9 @@ namespace mbts.utils { public static class Argument { public static ArgumentException must_be(string description, string name) => new ArgumentException($"Must be {description}.", name); + + public static ArgumentException unexpected_case(T value, string name) => + new ArgumentException($"Unexpected {typeof(T)} '{value}.'", name); } } } \ No newline at end of file diff --git a/src/client/spark/core/FontsAndColors/BTSColors.cs b/src/client/spark/core/FontsAndColors/BTSColors.cs index 24d8cbb59..cb52e8c2e 100644 --- a/src/client/spark/core/FontsAndColors/BTSColors.cs +++ b/src/client/spark/core/FontsAndColors/BTSColors.cs @@ -608,6 +608,12 @@ namespace mbts.ui { public static BTSColor positive_diff { get { return Color.FromRgb(0, 90, 50); } } } + public static class LadderSpecial { + public static BTSColor passive_volume_bar { get; } = Colors.SlateBlue; + public static BTSColor total_volume_bar { get; } = 0x009B69; + public static BTSColor default_column_background { get; } = 0xBABABA; + } + // Style Guide Colors public static Color brand_lightest { get { return UI.brand_lightest; } } public static Brush brand_lightest_brush { get { return new SolidColorBrush(brand_lightest); } } diff --git a/src/client/spark/core/GlobalExtensions/GenericCollectionExtensions.cs b/src/client/spark/core/GlobalExtensions/GenericCollectionExtensions.cs index 5a121369f..900a84bdb 100644 --- a/src/client/spark/core/GlobalExtensions/GenericCollectionExtensions.cs +++ b/src/client/spark/core/GlobalExtensions/GenericCollectionExtensions.cs @@ -271,9 +271,6 @@ namespace System.Collections.Generic { #endregion #region Max - public static T Max(this IEnumerable enumerable, Func select_value) where T : IComparable => - Max(enumerable, select_value, default(T)); - public static T Max(this IEnumerable enumerable, Func select_value, T return_if_empty) where T : IComparable { var etr = enumerable.GetEnumerator(); if (!etr.MoveNext()) return return_if_empty; diff --git a/src/client/spark/core/GlobalExtensions/GlobalExtensions.cs b/src/client/spark/core/GlobalExtensions/GlobalExtensions.cs index 944ce0305..1bdd74b4d 100644 --- a/src/client/spark/core/GlobalExtensions/GlobalExtensions.cs +++ b/src/client/spark/core/GlobalExtensions/GlobalExtensions.cs @@ -153,40 +153,6 @@ namespace mbts return rv.ToDictionary(kv => kv.Key, kv => kv.Value); } - //this overloaded declaration, (as opposed to one function with comp = null) allows observable_collection.sync_with to be passed into functions as a parameterless Action - public static void sync_with(this IList dest, IEnumerable src) { dest.sync_with(src, EqualityComparer.Default); } - public static void sync_with(this IList dest, IEnumerable src, IEqualityComparer comp) { - Log.notnull(dest); - if (src == null) { - dest.Clear(); - return; - } - var srcList = src as IList ?? src.ToList(); - var changes = bts.utils.Diff.diff(dest, srcList, comp).ToList(); - // Apply the diff - for(int i = 0; i < changes.Count; ++i) { - var c = changes[i]; - // removes - for (var x = 0; x < c.deletedLeft; ++x) { - dest.RemoveAt(c.StartLeft); - } - // inserts - for (var x = 0; x < c.insertedRight; ++x) { - dest.Insert(c.StartLeft + x, srcList[c.StartRight + x]); - } - - // Adjust start index of remaining changes - for (int ii = i + 1; ii < changes.Count; ++ii) { - var cc = changes[ii]; - var delta = c.insertedRight - c.deletedLeft; - if (cc.StartLeft >= c.StartLeft) { - cc.StartLeft += delta; - changes[ii] = cc;//remember: struct - } - } - } - } - public static bool sync_with_setwise(this ObservableCollection dest, IEnumerable src) { return dest.sync_with_setwise(src, EqualityComparer.Default); } public static bool sync_with_setwise(this ObservableCollection dest, IEnumerable src, IEqualityComparer comp) { if (!src.is_distinct(comp)) { diff --git a/src/client/spark/core/GlobalExtensions/TickExtensions.cs b/src/client/spark/core/GlobalExtensions/TickExtensions.cs index 648f9b4b7..2ed66ffbc 100644 --- a/src/client/spark/core/GlobalExtensions/TickExtensions.cs +++ b/src/client/spark/core/GlobalExtensions/TickExtensions.cs @@ -103,9 +103,23 @@ namespace mbts { return _round_tick(tuf, val); } - public static (int n_low, int n_high) tickdiff(this RegionalTickFactors regional_tick, double x, double y) { - if (DoubleHelper.IsNaNOrInfinity(x)) throw new ArgumentException("Cannot be NaN or Infinity.", nameof(x)); - if (DoubleHelper.IsNaNOrInfinity(y)) throw new ArgumentException("Cannot be NaN or Infinity.", nameof(y)); + public static bool try_get_tickdiff(this RegionalTickFactors regional_tick, double x, double y, out int n_low, out int n_high) { + if (DoubleHelper.IsNaNOrInfinity(x) || DoubleHelper.IsNaNOrInfinity(y)) { + n_low = default; + n_high = default; + return false; + } + (n_low, n_high) = tickdiff_unchecked(regional_tick, x, y); + return true; + } + + /// + /// Callers should be certain that and are not nan or infinity. + /// + /// + /// + /// + internal static (int n_low, int n_high) tickdiff_unchecked(this RegionalTickFactors regional_tick, double x, double y) { double ltie = regional_tick.low_tick_inclusive_end; double raw_diff = x - y; if (x < ltie) { diff --git a/src/client/spark/core/ShellInterface/UserSettings/UserSettings.cs b/src/client/spark/core/ShellInterface/UserSettings/UserSettings.cs index 23c2cb0c7..73c79bb5a 100644 --- a/src/client/spark/core/ShellInterface/UserSettings/UserSettings.cs +++ b/src/client/spark/core/ShellInterface/UserSettings/UserSettings.cs @@ -1,5 +1,5 @@ -using bts.proto; -using Google.Protobuf.WellKnownTypes; +using Google.Protobuf.WellKnownTypes; +using mbts.ui.settings; using ProtoBuf; using System; @@ -12,7 +12,12 @@ namespace mbts.spark { public static UserSettings factory_defaults => new UserSettings { _ladder = LadderSettings.Default, - _notification = NotificationSettings.Default + _notification = NotificationSettings.Default, + _market_view_grid = bts.shell.MarketViewWindowFactory.StartupData.DefaultGridSettings, + _fills_grid = bts.shell.FillsWindowFactory.StartupData.DefaultGridSettings, + _time_and_sales_grid = bts.shell.TimeAndSalesWindowFactory.StartupData.DefaultGridSettings, + _synthetic_management_grid = bts.shell.SyntheticManagementWindowFactory.StartupData.DefaultGridSettings, + _order_book_grid = bts.shell.OrderBookWindowFactory.OrderBookStartupData.DefaultGrid }; [ProtoMember(1)] @@ -21,19 +26,53 @@ namespace mbts.spark { // This way, when they are enabled, there won't be any backwards compatability issues to worry about, as no clients will have persisted them yet. NotificationSettings _notification; + [ProtoMember(5)] + GridSettings _market_view_grid; + [ProtoMember(6)] + GridSettings _time_and_sales_grid; + [ProtoMember(7)] + GridSettings _fills_grid; + [ProtoMember(8)] + GridSettings _synthetic_management_grid; + [ProtoMember(9)] + GridSettings _pnl_grid; + [ProtoMember(10)] + bts.shell.OrderBookWindowFactory.OrderBookGridSettings _order_book_grid; + public UserSettings() { } public LadderSettings ladder => _ladder ?? (_ladder = LadderSettings.Default); public NotificationSettings notification => _notification ?? (_notification = NotificationSettings.Default); + public GridSettings market_view_grid => _market_view_grid ?? (_market_view_grid = bts.shell.MarketViewWindowFactory.StartupData.DefaultGridSettings); + public GridSettings time_and_sales_grid => _time_and_sales_grid ?? (_time_and_sales_grid = GridSettings.Default.with(bts.shell.TimeAndSalesWindowFactory.StartupData.DefaultColumns.all)); + public GridSettings fills_grid => _fills_grid ?? (_fills_grid = bts.shell.FillsWindowFactory.StartupData.DefaultGridSettings); + public GridSettings synthetic_management_grid => _synthetic_management_grid ?? (_synthetic_management_grid = bts.shell.SyntheticManagementWindowFactory.StartupData.DefaultGridSettings); + public GridSettings pnl_grid => _pnl_grid ?? (_pnl_grid = bts.shell.PAndLWindowFactory.PAndLStartupData.DefaultGridSettings); + public bts.shell.OrderBookWindowFactory.OrderBookGridSettings order_book_grid => _order_book_grid ?? (_order_book_grid = bts.shell.OrderBookWindowFactory.OrderBookStartupData.Default.grid_settings_wrapper()); - public UserSettings with(LadderSettings ladder = null, NotificationSettings notification = null) => + public UserSettings with(LadderSettings ladder = null, + NotificationSettings notification = null, + GridSettings provided_market_view_gridsettings = null, + GridSettings time_and_sales = null, + GridSettings provided_fills_gridsettings = null, + GridSettings provided_synthetic_management_gridsettings = null, + GridSettings provided_pnl_gridsettings = null, + bts.shell.OrderBookWindowFactory.OrderBookGridSettings order_book_grid = null) => new UserSettings { _ladder = ladder ?? _ladder, - _notification = notification ?? _notification + _notification = notification ?? _notification, + _market_view_grid = provided_market_view_gridsettings ?? _market_view_grid, + _time_and_sales_grid = time_and_sales ?? _time_and_sales_grid, + _fills_grid = provided_fills_gridsettings ?? _fills_grid, + _synthetic_management_grid = provided_synthetic_management_gridsettings ?? _synthetic_management_grid, + _pnl_grid = provided_pnl_gridsettings ?? _pnl_grid, + _order_book_grid = order_book_grid ?? _order_book_grid }; public static class FileExtensions { - public const string LADDER = "ladder"; + public const string + LADDER = "ladder", + GRID = "grid"; } } diff --git a/src/client/spark/core/ShellInterface/UserSettings/UserSettingsHub.cs b/src/client/spark/core/ShellInterface/UserSettings/UserSettingsHub.cs index 7d8219af5..be09f4632 100644 --- a/src/client/spark/core/ShellInterface/UserSettings/UserSettingsHub.cs +++ b/src/client/spark/core/ShellInterface/UserSettings/UserSettingsHub.cs @@ -16,7 +16,8 @@ namespace mbts.persist { const string NAME_SPACE = "settings", DEFAULT_KEY = "default"; IUserPersist _persist; UserSettings _default_settings; - readonly Subject _default_settings_subject = new Subject(); + readonly BehaviorSubject _default_settings_subject = new BehaviorSubject(UserSettings.factory_defaults); + readonly object _default_settings_lock = new object(); [ImportingConstructor] public UserSettingsHub(IUserPersist persist) { @@ -33,7 +34,12 @@ namespace mbts.persist { /// Assuming there aren't any rogue agents saving UserSettings by themselves, there should never be any discrepancies. public IObservable subscribe_default_settings() => _default_settings_subject.AsObservable(); - internal UserSettings current => _default_settings ?? UserSettings.factory_defaults; + internal UserSettings current { + get { + lock (_default_settings_lock) + return _default_settings ?? UserSettings.factory_defaults; + } + } public async Task save_default(UserSettings value) { _set_and_notify(value); @@ -41,9 +47,11 @@ namespace mbts.persist { } void _set_and_notify(UserSettings value) { - _default_settings = value; - // notify observers before persisting so the changes are reflected immediately in the ui - _default_settings_subject.OnNext(value); + lock (_default_settings_lock) { + _default_settings = value; + // notify observers before persisting so the changes are reflected immediately in the ui + _default_settings_subject.OnNext(value); + } } } } \ No newline at end of file diff --git a/src/client/spark/core/ShellInterface/UserSettings/WindowSettings/IWindowSettingsManager.cs b/src/client/spark/core/ShellInterface/UserSettings/WindowSettings/IWindowSettingsManager.cs index e6734260f..7fca8e3e9 100644 --- a/src/client/spark/core/ShellInterface/UserSettings/WindowSettings/IWindowSettingsManager.cs +++ b/src/client/spark/core/ShellInterface/UserSettings/WindowSettings/IWindowSettingsManager.cs @@ -1,7 +1,9 @@ using mbts.spark; +using mbts.utils; using System; using System.Linq; using System.Reactive.Linq; +using System.Reactive.Subjects; using System.Threading.Tasks; namespace mbts.persist { @@ -11,8 +13,15 @@ namespace mbts.persist { event Action instance_settings_changed; IObservable subscribe_default(); Task save_default_settings(T value); + IWindowSettingsManagerNonShared as_non_shared(); } + /// + /// An implementation of IWindowSettingsManager that uses a clone of the actual default settings; + /// provides a convenient way to avoid problems that arise from sharing a single default settings object among multiple window instances. + /// + interface IWindowSettingsManagerNonShared : IWindowSettingsManager, IDisposable where T : class { } + class WindowSettingsManager : IWindowSettingsManager where T : class { readonly UserSettingsHub _hub; readonly Func _get; @@ -43,6 +52,42 @@ namespace mbts.persist { public IObservable subscribe_default() => _hub.subscribe_default_settings().Select(_get).DistinctUntilChanged(); + + public IWindowSettingsManagerNonShared as_non_shared() => + new NonSharedWrapper(this); + + class NonSharedWrapper : IWindowSettingsManagerNonShared { + readonly WindowSettingsManager _wsm; + readonly BehaviorSubject _default_subject; + IDisposable _dispose; + + public NonSharedWrapper(WindowSettingsManager wsm) { + _wsm = wsm; + _default_subject = new BehaviorSubject(_wsm.default_settings ?? wsm._get(UserSettings.factory_defaults)); + _dispose = wsm.subscribe_default().Subscribe(s => + _default_subject.OnNext(default_settings = MiscUtils.clone_proto(s))); + } + + public T instance_settings { + get => _wsm.instance_settings; + set => _wsm.instance_settings = value; + } + public T default_settings { get; private set; } + public event Action instance_settings_changed { + add => _wsm.instance_settings_changed += value; + remove => _wsm.instance_settings_changed -= value; + } + public async Task save_default_settings(T value) => + await _wsm.save_default_settings(value); + + public IObservable subscribe_default() => + _default_subject.AsObservable(); + + public IWindowSettingsManagerNonShared as_non_shared() => + new NonSharedWrapper(_wsm); + + public void Dispose() => Disposable.dispose(ref _dispose); + } } static class WindowSettingsManagerExtensions { @@ -54,8 +99,7 @@ namespace mbts.persist { Observable.CombineLatest( Observable.FromEvent, T>(cb => wsm.instance_settings_changed += cb, cb => wsm.instance_settings_changed -= cb) .StartWith(wsm.instance_settings), - wsm.subscribe_default() - .StartWith(wsm.default_settings), + wsm.subscribe_default(), (i, d) => i ?? d ).Where(v => v != null) .DistinctUntilChanged(); diff --git a/src/client/spark/core/Utility/CollectionHelper.cs b/src/client/spark/core/Utility/CollectionHelper.cs index 1b83ea870..2cbbec8f6 100644 --- a/src/client/spark/core/Utility/CollectionHelper.cs +++ b/src/client/spark/core/Utility/CollectionHelper.cs @@ -1,5 +1,4 @@ using System; -using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; @@ -14,16 +13,13 @@ namespace mbts.utils { public static void sync_with_never_duplicate(ObservableCollection dest, IEnumerable src, IEqualityComparer comp) { int i = 0; foreach (var val in src.empty_if_null()) { - int curr_index = dest.TakeWhile(dval => !comp.Equals(dval, val)).Count(); - if (curr_index == dest.Count) { - dest.Add(val); - } else if (curr_index == i) { + int curr_index = dest.IndexOf(val); + if (curr_index == i) { continue; - } else if (curr_index < dest.Count) { + } else if (curr_index > 0 && curr_index < dest.Count) { dest.RemoveAt(curr_index); - dest.Insert(i, val); } - ++i; + dest.Insert(i++, val); } } @@ -49,5 +45,12 @@ namespace mbts.utils { ++i; } } + + public static TResult[] convert_array(T[] source, Func selector) { + TResult[] rv = new TResult[source.Length]; + for (int i = 0; i < rv.Length; i++) + rv[i] = selector(source[i]); + return rv; + } } } \ No newline at end of file diff --git a/src/client/spark/core/Utility/KeyDrivenObservable.cs b/src/client/spark/core/Utility/KeyDrivenObservable.cs index 940705abf..9e3b668ed 100644 --- a/src/client/spark/core/Utility/KeyDrivenObservable.cs +++ b/src/client/spark/core/Utility/KeyDrivenObservable.cs @@ -17,7 +17,8 @@ namespace mbts.utils { /// class KeyDrivenObservableSeries : ISubject, IDisposable { readonly Func> _selector; - object _lock = new object(); + readonly object _lock = new object(); + bool _is_disposed; Subject _value_subject = new Subject(); IDisposable _current_key_sub; @@ -31,15 +32,19 @@ namespace mbts.utils { // need to lock bc simultaneous callbacks on diff threads could generate 2 subscriptions at once, // only 1 of which would get disposed in the next callback. Disposable.dispose(ref _current_key_sub); - _current_key_sub = _selector(key)?.Subscribe(_value_subject); + if (!_is_disposed) + _current_key_sub = _selector(key)?.Subscribe(_value_subject); } } public IDisposable Subscribe(IObserver observer) => _value_subject.Subscribe(observer); public void Dispose() { - Disposable.dispose(ref _current_key_sub); - Disposable.dispose(ref _value_subject); + lock (_lock) { + _is_disposed = true; + Disposable.dispose(ref _current_key_sub); + Disposable.dispose(ref _value_subject); + } } } diff --git a/src/client/spark/core/Utility/RefValueContainer.cs b/src/client/spark/core/Utility/RefValueContainer.cs new file mode 100644 index 000000000..1938241c0 --- /dev/null +++ b/src/client/spark/core/Utility/RefValueContainer.cs @@ -0,0 +1,36 @@ +namespace mbts.utils { + /// + /// Facilities multi-threaded read/write operations to a particular reference. + /// + /// The reference type to be read/written. + class RefValueContainer where T : class { + readonly object _lock = new object(); + T _value; + bool _is_dirty; + + /// + /// Returns the current value if it has not been read already, otherwise returns null. + /// + public T read_new_value() { + lock (_lock) { + if (_is_dirty) { + _is_dirty = false; + return _value; + } + return null; + } + } + + public void write_value(T value) { + lock (_lock) { + _is_dirty = true; + _value = value; + } + } + + public void mark_dirty() { + lock (_lock) + _is_dirty = true; + } + } +} \ No newline at end of file diff --git a/src/client/spark/core/Utility/ViewModelBase.cs b/src/client/spark/core/Utility/ViewModelBase.cs index 34ef4b2ae..317393440 100644 --- a/src/client/spark/core/Utility/ViewModelBase.cs +++ b/src/client/spark/core/Utility/ViewModelBase.cs @@ -110,7 +110,7 @@ namespace mbts.ui.shell #endregion #region automatic property - protected delegate void PropertyChange(T oldval, T newval); + protected internal delegate void PropertyChange(T oldval, T newval); // use nameof() to add compile time checks protected Property backing(string name, T default_value = default(T)) => diff --git a/src/client/spark/core/data/Proto/ColorShowProto.cs b/src/client/spark/core/data/Proto/ColorShowProto.cs new file mode 100644 index 000000000..12b2acfe2 --- /dev/null +++ b/src/client/spark/core/data/Proto/ColorShowProto.cs @@ -0,0 +1,47 @@ +using bts.utils; +using mbts.ui; +using ProtoBuf; +using System; +using System.Windows.Media; + +namespace bts.proto { + [ProtoContract] + struct ColorShowProto : IEquatable { + [ProtoMember(1)] + readonly uint _value; + [ProtoMember(2)] + readonly bool _show; + + public ColorShowProto(Color value, bool show = true) + : this(value.rgb_color(), show) { } + + public ColorShowProto(uint value, bool show = true) { + _value = value; + _show = show; + } + + public Color color => _value.rgb_color(); + public bool show => _show; + + public Color? result { + get { + if (_show) + return color; + return null; + } + } + + public bool Equals(ColorShowProto other) => + _value == other._value && + _show == other._show; + + public override bool Equals(object obj) => + obj is ColorShowProto ncp && Equals(ncp); + + public override int GetHashCode() => + EqualityComparer.aggregate_hash(_value.GetHashCode(), _show.GetHashCode()); + + public static bool operator ==(ColorShowProto a, ColorShowProto b) => a.Equals(b); + public static bool operator !=(ColorShowProto a, ColorShowProto b) => !a.Equals(b); + } +} \ No newline at end of file diff --git a/src/client/spark/view/Controls/DIYGrid/CellProperty.cs b/src/client/spark/view/Controls/DIYGrid/CellProperty.cs index d21e1f98e..758d0474d 100644 --- a/src/client/spark/view/Controls/DIYGrid/CellProperty.cs +++ b/src/client/spark/view/Controls/DIYGrid/CellProperty.cs @@ -15,7 +15,7 @@ namespace mbts.spark { value = initial_value; } - event Action<(T oldval, T newval)> _value_set; + protected event Action<(T oldval, T newval)> _value_set; public T value { get; private set; } public string text => _text ?? (_text = _format(value)); diff --git a/src/client/spark/view/Controls/DIYGrid/CellRender/HyperlinkCell.cs b/src/client/spark/view/Controls/DIYGrid/CellRender/HyperlinkCell.cs index 887a379cf..8d9e326cb 100644 --- a/src/client/spark/view/Controls/DIYGrid/CellRender/HyperlinkCell.cs +++ b/src/client/spark/view/Controls/DIYGrid/CellRender/HyperlinkCell.cs @@ -24,7 +24,7 @@ namespace mbts.ui.controls { bool _is_mouse_over_text, _can_execute; Pen _underline_pen; - static HyperlinkCell() => + static HyperlinkCell() { WPFHelper.override_metadata( HorizontalAlignmentProperty, FrameworkPropertyMetadataOptions.AffectsArrange | @@ -33,6 +33,13 @@ namespace mbts.ui.controls { o._horizontal_alignment = v; o._glyph_run = null; }, HorizontalAlignment.Left); + WPFHelper.override_metadata( + WidthProperty, + FrameworkPropertyMetadataOptions.AffectsArrange | + FrameworkPropertyMetadataOptions.AffectsMeasure | + FrameworkPropertyMetadataOptions.AffectsRender, + (o, v) => o._glyph_run = null); + } public HyperlinkCell() { _background = Background; @@ -279,24 +286,50 @@ namespace mbts.ui.controls { drawingContext.DrawGlyphRun(_foreground, _glyph_run); if (_is_mouse_over_text) { // draw underline - var pt = new Point(Padding.Left, 0.5 * (ActualHeight + _font_size) + 1); + Point pt; + switch (_horizontal_alignment) { + case HorizontalAlignment.Left: + pt = new Point(Padding.Left, 0.5 * (ActualHeight + _font_size) + 1); + break; + case HorizontalAlignment.Right: + pt = new Point(ActualWidth - Padding.Right - _glyph_width, 0.5 * (ActualHeight + _font_size) + 1); + break; + case HorizontalAlignment.Center: + default: + pt = new Point(0.5 * (ActualWidth - _glyph_width), 0.5 * (ActualHeight + _font_size) + 1); + break; + } drawingContext.DrawLine(_underline_pen, pt, new Point(pt.X + _glyph_width, pt.Y)); + } drawingContext.Pop(); } - var bgo = GetBackgroundOverlay(this); - if (bgo != null) - drawingContext.DrawRectangle(bgo, null, rect); + if (bg != null) { + var bgo = GetBackgroundOverlay(this); + if (bgo != null) + drawingContext.DrawRectangle(bgo, null, rect); + } + } + + bool _point_horizontally_over_text(double x) { + switch (_horizontal_alignment) { + case HorizontalAlignment.Left: + return x >= _padding.Left && x <= _padding.Left + _glyph_width; + case HorizontalAlignment.Right: + return x >= ActualWidth - Padding.Right - _glyph_width && x <= ActualWidth - _padding.Right; + case HorizontalAlignment.Center: + default: + return x >= 0.5 * (ActualWidth - _glyph_width) && x <= 0.5 * (ActualWidth - _glyph_width) + _glyph_width; + } } void _check_mouse_over_text(MouseEventArgs e) { var pt = e.GetPosition(this); bool over_text = - _glyph_run != null && - pt.X >= _padding.Left && - pt.X <= _padding.Left + _glyph_width && - pt.Y >= _padding.Top && - pt.Y <= _padding.Top + _font_size; + _glyph_run != null && + _point_horizontally_over_text(pt.X) && + pt.Y >= _padding.Top && + pt.Y <= _padding.Top + _font_size; if (_is_mouse_over_text != over_text) _toggle_mouse_over_text(); } diff --git a/src/client/spark/view/Controls/DIYGrid/Columns/ColumnBase.cs b/src/client/spark/view/Controls/DIYGrid/Columns/ColumnBase.cs index 75d8b145c..3edb17e7d 100644 --- a/src/client/spark/view/Controls/DIYGrid/Columns/ColumnBase.cs +++ b/src/client/spark/view/Controls/DIYGrid/Columns/ColumnBase.cs @@ -16,22 +16,40 @@ namespace mbts.ui.controls { partial class DIYGrid { public static class DefaultColumnWidths { public const double - Account = 68, - Exchange = 74, + Account = 80, + Exchange = 86, FillKey = 68, OrderKey = 78, OrderType = 88, - Price = 56, + MViewPrice = 56, + Price = 68, + AveragePrice = 76, + LastPrice = 78, + Settlement = 82, Security = 80, SecurityKey = 90, + SecurityType = 96, Side = 47, Size = 48, - Symbol = 48, - Time = 100, + ImpliedDirectMergedSize = 68, + Filled = 56, + Symbol = 78, + Time = 112, TimeInForce = 47, TradeKey = 76, User = 53, - Aggressor = 80; + Aggressor = 80, + Volume = 74, + FillKind = 70, + Description = 300, + N_Legs = 68, + N_Working_Orders = 123, + Start_Net_Pos = 100, + Total_Net_Pos = 100, + PnL_Generic = 80, + Working = 72, + LastMessage = 100, + ParentKey = 100; } public static class DefaultColumnHeaders { @@ -45,14 +63,41 @@ namespace mbts.ui.controls { Price = "Price", Security = "Security", SecurityKey = "Security Key", + SecurityType = "Security Type", Side = "Side", Size = "Qty", + MergedSize = "Qty (M)", + DirectSize = "Qty (D)", + ImpliedSize = "Qty (I)", Symbol = "Symbol", TimeInForce = "TIF", TradeKey = "Trade Key", User = "User", Time = "Time", - Aggressor = "Aggressor"; + Aggressor = "Aggressor", + Volume = "Volume", + Last = "Last", + Pos = "Pos", + Working = "Working", + Bid = "Bid", + Ask = "Ask", + Settlement = "Settlement", + DayChange = "Change", + SessionLow = "Low", + SessionHigh = "High", + FillKind = "Fill Kind", + Name = "Name", + Description = "Description", + N_Legs = "# Legs", + N_Working_Orders = "# Working Orders", + StartNetPos = "Start Net Pos", + StartPnL = "Start PnL", + DayLong = "Day Long", + DayShort = "Day Short", + DayPnL = "Day PnL", + TotalNetPos = "Total Net Pos", + TotalPnL = "Total PnL", + PnLPrice = "PnL Price"; } public interface IKeyedColumn { @@ -158,7 +203,7 @@ namespace mbts.ui.controls { public virtual void load_from(GridColumnSettings settings) { if (settings == null) return; - using (ReflectionHelper.detect_property_changes(this, raise_prop_changed, nameof(is_visible), nameof(is_collapsed), nameof(font_weight_override), nameof(font_weight), + using (ReflectionHelper.detect_property_changes(this, _raise_prop_changed, nameof(is_visible), nameof(is_collapsed), nameof(font_weight_override), nameof(font_weight), nameof(font_color_override), nameof(font_color), nameof(font_brush), nameof(font_family), nameof(font_size), nameof(horizontal_alignment), nameof(horizontal_offset))) { // id = settings.id; @@ -176,21 +221,24 @@ namespace mbts.ui.controls { Width = original_width; } // - void raise_prop_changed(string prop) => - OnPropertyChanged(new PropertyChangedEventArgs(prop)); } // 'fallback' font-params (aka 'grid-level' font-params) public void set_fallback_font_params(FontFamily font_family, double font_size, Color font_color, FontWeight font_weight) { - _fallback_font_family = font_family; - _fallback_font_size = font_size; - _fallback_font_color = font_color; - _fallback_font_weight = font_weight; + using (ReflectionHelper.detect_property_changes(this, _raise_prop_changed, nameof(font_family), nameof(font_size), nameof(font_color), nameof(font_weight))) { + _fallback_font_family = font_family; + _fallback_font_size = font_size; + _fallback_font_color = font_color; + _fallback_font_weight = font_weight; + } font_params_changed?.Invoke(); } protected virtual void OnSortDirectionChanged(ListSortDirection? new_value) => sort_direction_changed?.Invoke(this, new_value); + + void _raise_prop_changed(string prop) => + OnPropertyChanged(new PropertyChangedEventArgs(prop)); } internal abstract class ColumnBase : ColumnBase, IKeyedColumn, IGridColumn { diff --git a/src/client/spark/view/Controls/DIYGrid/Columns/FrameworkElementColumns/HyperlinkColumn.cs b/src/client/spark/view/Controls/DIYGrid/Columns/FrameworkElementColumns/HyperlinkColumn.cs index f76c24da7..258740e29 100644 --- a/src/client/spark/view/Controls/DIYGrid/Columns/FrameworkElementColumns/HyperlinkColumn.cs +++ b/src/client/spark/view/Controls/DIYGrid/Columns/FrameworkElementColumns/HyperlinkColumn.cs @@ -29,9 +29,13 @@ namespace mbts.ui.controls { background_overlay = selection_fill; public override FrameworkElement view_factory() { - var hl = new HyperlinkCell { Foreground = font_brush, Background = background, FontSize = font_size, Padding = padding }; + var hl = new HyperlinkCell { Foreground = font_brush, Background = background, Padding = padding }; hl.SetBinding(HyperlinkCell.TextProperty, _binding); + hl.SetBinding(HyperlinkCell.FontFamilyProperty, new Binding(nameof(font_family)) { Source = this }); + hl.SetBinding(HyperlinkCell.FontSizeProperty, new Binding(nameof(font_size)) { Source = this }); + hl.SetBinding(HyperlinkCell.HorizontalAlignmentProperty, new Binding(nameof(horizontal_alignment)) { Source = this }); hl.SetBinding(HyperlinkCell.CommandProperty, _command_binding); + hl.SetBinding(HyperlinkCell.WidthProperty, new Binding(nameof(ActualWidth)) { Source = this }); if (background_overlay != null) { hl.SetBinding(BackgroundOverlayProperty, new Binding(nameof(ListBoxItem.IsSelected)) { Converter = new AnonymousValueConverter(val => val ? background_overlay : null), diff --git a/src/client/spark/view/Controls/DIYGrid/Columns/FrameworkElementColumns/TextColumn.cs b/src/client/spark/view/Controls/DIYGrid/Columns/FrameworkElementColumns/TextColumn.cs index 931dae6b6..6f22c40ef 100644 --- a/src/client/spark/view/Controls/DIYGrid/Columns/FrameworkElementColumns/TextColumn.cs +++ b/src/client/spark/view/Controls/DIYGrid/Columns/FrameworkElementColumns/TextColumn.cs @@ -22,8 +22,10 @@ namespace mbts.ui.controls { public VerticalAlignment vertical_alignment { get; set; } = VerticalAlignment.Center; public override FrameworkElement view_factory() { - var v = new TextCell { FontFamily = font_family, FontSize = font_size, Foreground = font_brush, HorizontalAlignment = horizontal_alignment, VerticalAlignment = vertical_alignment }; + var v = new TextCell { Foreground = font_brush, HorizontalAlignment = horizontal_alignment, VerticalAlignment = vertical_alignment }; v.SetBinding(TextCell.TextProperty, _binding); + v.SetBinding(TextCell.FontFamilyProperty, new Binding(nameof(font_family)) { Source = this }); + v.SetBinding(TextCell.FontSizeProperty, new Binding(nameof(font_size)) { Source = this }); try_set_binding(TextCell.BackgroundProperty, background_binding); try_set_binding(TextCell.ForegroundProperty, foreground_binding); try_set_binding(TextCell.FontWeightProperty, font_weight_binding); diff --git a/src/client/spark/view/Controls/DIYGrid/Columns/FrameworkElementColumns/TimeAndSalesColumn.cs b/src/client/spark/view/Controls/DIYGrid/Columns/FrameworkElementColumns/TimeAndSalesColumn.cs index 162cd05f4..dafdbe936 100644 --- a/src/client/spark/view/Controls/DIYGrid/Columns/FrameworkElementColumns/TimeAndSalesColumn.cs +++ b/src/client/spark/view/Controls/DIYGrid/Columns/FrameworkElementColumns/TimeAndSalesColumn.cs @@ -1,21 +1,26 @@ -using mbts.spark; +using bts.shell; +using mbts.spark; using mbts.spark.filters; +using mbts.ui.spark; using mbts.ui.utility.text; using System; using System.Windows.Media; namespace mbts.ui.controls { partial class DIYGrid { - internal class TimeAndSalesColumn : RenderTextColumn { - - readonly Func _get_text; + internal class TimeAndSalesColumn : RenderTextColumn, IKeyedColumn { readonly Func _get_brush; - public TimeAndSalesColumn(Func get_text, Func get_brush, IDataGridColumnFilter filter = null, string filter_template = null, GlyphContext glyph_context = null) : base(get_text, filter, filter_template, glyph_context) { - _get_text = get_text; + public TimeAndSalesColumn(TimeAndSalesView.Columns key, Func get_brush, IDataGridColumnFilter filter = null, string filter_template = null) + : base(r => r[key], filter, filter_template) { + this.key = key; + // id is used for settings (another case of ProtoBufs vs Generics - strings seemed like the easiest way to provide an identifier for the common GridColumnSettings proto) + id = TimeAndSalesWindowFactory.col_id(key); _get_brush = get_brush; } + public TimeAndSalesView.Columns key { get; } + public override void draw(TimeAndSalesVM.Row row, DrawingContext dc, ColumnGeometry position, double row_height) { CellRender.draw_text(dc , glyph_context diff --git a/src/client/spark/view/Controls/DIYGrid/Columns/RenderColumn.cs b/src/client/spark/view/Controls/DIYGrid/Columns/RenderColumn.cs index c3a57b55b..f6bb51015 100644 --- a/src/client/spark/view/Controls/DIYGrid/Columns/RenderColumn.cs +++ b/src/client/spark/view/Controls/DIYGrid/Columns/RenderColumn.cs @@ -20,18 +20,20 @@ namespace mbts.ui.controls { } public class RenderTextColumn : RenderColumnBase, IRenderColumn, IFilterableColumn { - readonly Func _get_text; + protected readonly Func _get_text; public RenderTextColumn( Func get_text, IDataGridColumnFilter filter = null, - string filter_template = null, - GlyphContext glyph_context = null) + string filter_template = null) : base() { _get_text = get_text; this.filter = filter; this.filter_template = filter_template; - this.glyph_context = glyph_context ?? TextUtils.StandardGlyphContext; + font_params_changed += refresh_glyph_context; + refresh_glyph_context(); + void refresh_glyph_context() => + glyph_context = GlyphContext.create(font_family, font_weight); } public IDataGridColumnFilter filter { get; } @@ -39,7 +41,7 @@ namespace mbts.ui.controls { public Brush background { get; set; } public VerticalAlignment vertical_alignment { get; set; } = VerticalAlignment.Center; public Thickness padding { get; set; } = DefaultCellPadding; - public GlyphContext glyph_context { get; set; } = TextUtils.StandardGlyphContext; + public GlyphContext glyph_context { get; private set; } public override void draw(TRowVM row, DrawingContext dc, ColumnGeometry position, double row_height) => CellRender.draw_text( diff --git a/src/client/spark/view/Controls/DIYGrid/DIYGridResources.xaml b/src/client/spark/view/Controls/DIYGrid/DIYGridResources.xaml index 0f60b2b9d..0d77a7a05 100644 --- a/src/client/spark/view/Controls/DIYGrid/DIYGridResources.xaml +++ b/src/client/spark/view/Controls/DIYGrid/DIYGridResources.xaml @@ -335,6 +335,8 @@ + + diff --git a/src/client/spark/view/Controls/DIYGrid/Settings/GridColumnSettings.cs b/src/client/spark/view/Controls/DIYGrid/Settings/GridColumnSettings.cs index 2ad519fc1..e6ed6953f 100644 --- a/src/client/spark/view/Controls/DIYGrid/Settings/GridColumnSettings.cs +++ b/src/client/spark/view/Controls/DIYGrid/Settings/GridColumnSettings.cs @@ -92,7 +92,7 @@ namespace mbts.ui.settings { public GridColumnSettings with( string display_name = null, bool? is_visible = null, bool? is_collapsed = null, NullableFontWeightProto? font_weight_override = null, NullableColorProto? font_color_override = null, - double? width = _default_width, HorizontalAlignment? horizontal_alignment = _default_horizontal_alignment, double? horizontal_offset = null) => + double? width = null, HorizontalAlignment? horizontal_alignment = null, double? horizontal_offset = null) => // new GridColumnSettings(_id, display_name ?? this.display_name, diff --git a/src/client/spark/view/Controls/DIYGrid/Settings/GridColumnSettingsVM.cs b/src/client/spark/view/Controls/DIYGrid/Settings/GridColumnSettingsVM.cs index 3d9db0ad0..8e042dcc6 100644 --- a/src/client/spark/view/Controls/DIYGrid/Settings/GridColumnSettingsVM.cs +++ b/src/client/spark/view/Controls/DIYGrid/Settings/GridColumnSettingsVM.cs @@ -4,7 +4,6 @@ using mbts.ui; using mbts.ui.controls; using mbts.ui.settings; using mbts.ui.shell; -using mbts.ui.utilities; using mbts.ui.utility; using System; using System.Collections.Generic; @@ -28,22 +27,15 @@ namespace mbts.spark { ImmutableFontWeight } - partial class GridSettingsVM { - public interface IColumnVM : IValueFactory<(GridColumnSettings settings, ILookup animations)>, IDisposable { + partial class GridSettingsVM { + public interface IColumnVM : ISimpleColumnVM, IValueFactory<(GridColumnSettings settings, ILookup animations)>, IDisposable { ObservableEditableCollection predicate_animations { get; } - string id { get; } - string display_name { get; set; } - double width { get; set; } - bool is_visible { get; set; } - bool is_font_color_overridden { get; set; } - ColorPickerPopupVM font_color_override { get; } - bool is_font_weight_overridden { get; set; } - FontWeight font_weight_override { get; set; } - void apply_flags(GridColumnSettingsFeatureFlags flags); + new (GridColumnSettings settings, ILookup animations) create(); + new void load_from((GridColumnSettings settings, ILookup animations) args); } public class ColumnVM : DisposableViewModelBase, IColumnVM { - public readonly GridSettingsVM owner; + public readonly GridSettingsVM owner; readonly Property _id, _display_name; readonly Property _width; @@ -52,7 +44,7 @@ namespace mbts.spark { readonly Property _font_weight_override; GridColumnSettings _settings; - public ColumnVM(GridSettingsVM owner) { + public ColumnVM(GridSettingsVM owner) { this.owner = owner; _id = backing(nameof(id), string.Empty); _display_name = backing(nameof(display_name), string.Empty); @@ -107,6 +99,7 @@ namespace mbts.spark { _horizontal_alignment.set(_settings.horizontal_alignment); _is_visible.set(_settings.is_visible); _is_font_weight_overridden.set(_settings.font_weight_override.HasValue); + _is_font_color_overridden.set(_settings.font_color_override.HasValue); if (is_font_weight_overridden) { _font_weight_override.set(_settings.font_weight_override.Value); } @@ -133,6 +126,12 @@ namespace mbts.spark { } public override string ToString() => $"{display_name} ({id})"; + + GridColumnSettings IValueFactory.create() => + create().settings; + + void IValueFactory.load_from(GridColumnSettings value) => + load_from((value, new Dictionary().ToLookup(kv => kv.Key, kv => kv.Value))); } public class PredicateCellAnimationVM : DisposableViewModelBase, IValueFactory<(TPredicateBuilder predicate, IEnumerable animations)> { diff --git a/src/client/spark/view/Controls/DIYGrid/Settings/GridSettings.cs b/src/client/spark/view/Controls/DIYGrid/Settings/GridSettings.cs index 5be7c36a5..fffee8e28 100644 --- a/src/client/spark/view/Controls/DIYGrid/Settings/GridSettings.cs +++ b/src/client/spark/view/Controls/DIYGrid/Settings/GridSettings.cs @@ -1,7 +1,9 @@ using bts.proto; +using bts.utils; using mbts.ui.controls; using mbts.ui.converters; using ProtoBuf; +using System; using System.Collections.Generic; using System.ComponentModel; using System.Windows; @@ -10,7 +12,7 @@ using System.Windows.Media; namespace mbts.ui.settings { [ProtoContract] - class GridSettings { + class GridSettings : IEquatable { const string _default_font_family = "Segoe UI"; const double _default_font_size = 11; const uint _default_font_color = 0; @@ -94,6 +96,7 @@ namespace mbts.ui.settings { public FontWeight font_weight => FontWeight.FromOpenTypeWeight(_font_weight); public ColorOpacityProto selection_highlight => _selection_highlight; public IReadOnlyList columns => _columns; + [Obsolete] // TODO: remove sorting_criterion from GridSettings entirely. This should be part of IShellWindow's StartupData, since it is an instance-specific setting, and prob not part of the shared defaults public IReadOnlyList sorting_criterion => _sorting_criterion; public static GridSettings get_default() => @@ -110,5 +113,32 @@ namespace mbts.ui.settings { sorting_criterion ?? _sorting_criterion); public GridSettings clone() => with(); + + public bool Equals(GridSettings other) => + other != null && + font_family == other.font_family && + font_size == other.font_size && + font_weight == other.font_weight && + font_color == other.font_color && + selection_highlight == other.selection_highlight && + EqualityComparer.safe_sequence_equals(columns, other.columns) && +#pragma warning disable CS0612 // Type or member is obsolete + EqualityComparer.safe_sequence_equals(sorting_criterion, other.sorting_criterion); +#pragma warning restore CS0612 // Type or member is obsolete + + public override bool Equals(object obj) => + Equals(obj as GridSettings); + + public override int GetHashCode() => + EqualityComparer.aggregate_hash( + font_family.GetHashCode(), + font_size.GetHashCode(), + font_weight.GetHashCode(), + font_color.GetHashCode(), + selection_highlight.GetHashCode(), + EqualityComparer.safe_aggregate_hash(columns), +#pragma warning disable CS0612 // Type or member is obsolete + EqualityComparer.safe_aggregate_hash(sorting_criterion)); +#pragma warning restore CS0612 // Type or member is obsolete } } \ No newline at end of file diff --git a/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsManager.cs b/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsManager.cs index 22e31b721..e384e1c82 100644 --- a/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsManager.cs +++ b/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsManager.cs @@ -1,4 +1,5 @@ -using bts.api; +using bts; +using bts.api; using bts.shell; using mbts.collections; using mbts.spark; @@ -17,10 +18,10 @@ using System.Windows.Media; using static mbts.ui.controls.DIYGrid; namespace mbts.ui { - class GridSettingsManager : - ViewModelBase, IValueFactory<(GridSettings grid, IEnumerable> predicate_animations_data)>, IDisposable + class GridSettingsManager : ViewModelBase, IValueFactory, IObserver, IDisposable where TRow : IRowVM - where TPredicateBuilder : IPredicateBuilder, new() { + where TPredicateBuilder : IPredicateBuilder, new() + where TSettingsData : class, IGridSettingsWithNotifications { // public readonly ObservableCollection live_sorting_props; @@ -39,12 +40,18 @@ namespace mbts.ui { readonly DelegateProperty _font_color, _selection_highlight; readonly SetMap, AnimationDefinitionWrapper> _animation_defs = new SetMap, AnimationDefinitionWrapper>(); readonly TPredicateBuilderArg _arg; + readonly Func> _create_predicate_vm; + readonly Func> _create_column_predicate_vm; + readonly Func>, TSettingsData> _create_settings_obj; + readonly IReadOnlyDictionary _column_flags; IEnumerable> _predicate_animations_data; public GridSettingsManager( IObservable owner_title, IBus bus, INotificationService notification_service, OrdersModel orders_model, ObservableCollection rows, ICollectionView rows_view, ObservableCollection columns, Func> create_predicate_vm, - Func> create_column_predicate_vm, TPredicateBuilderArg arg, IReadOnlyDictionary column_flags = null) { + Func> create_column_predicate_vm, + Func>, TSettingsData> create_settings_obj, + TPredicateBuilderArg arg, IReadOnlyDictionary column_flags = null) { // _bus = bus; _notification_service = notification_service; @@ -52,7 +59,11 @@ namespace mbts.ui { _rows = rows; _rows_view = rows_view; _columns = columns; + _create_predicate_vm = create_predicate_vm; + _create_column_predicate_vm = create_column_predicate_vm; + _create_settings_obj = create_settings_obj; _arg = arg; + _column_flags = column_flags.ToDictionary(kv => kv.Key, kv => ImmutableColumnSettingStore.create(kv.Value)); _grid_settings = backing(nameof(grid_settings), GridSettings.Default); _font_family = backing(nameof(font_family), () => new FontFamily(grid_settings.font_family), nameof(grid_settings)); _font_weight = backing(nameof(font_weight), () => grid_settings.font_weight, nameof(grid_settings)); @@ -72,10 +83,10 @@ namespace mbts.ui { // return pa; //} Window create_settings_view() { - var vm = new GridSettingsVM(owner_title, _bus, _orders_model, create_predicate_vm, create_column_predicate_vm); + var vm = new GridSettingsVM(owner_title, _bus, _orders_model, create_predicate_vm, create_column_predicate_vm, _create_settings_obj); // need to refresh before launching the settings-view bc users can chanve some grid-column-settings properties directly from the grid (e.g. width, is_collapsed, etc.) _grid_settings.set(grid_settings.with(columns.Select(c => c.create()))); - vm.load_from((grid_settings, _predicate_animations_data)); + vm.load(_create_settings_obj(grid_settings, _predicate_animations_data)); if (column_flags != null) { foreach (var col in vm.columns) { if (column_flags.TryGetValue(col.id, out GridColumnSettingsFeatureFlags f)) @@ -84,10 +95,10 @@ namespace mbts.ui { } var window = new GridSettingsDialog { DataContext = vm }; vm.committed += () => { - var (grid, predicate_animations_data) = vm.create(); + TSettingsData sd = vm.get_result(); // IMPORTANT: don't overwrite current sorting - grid = grid.with(sorting_criterion: rows_view.get_sorting_criteria(columns)); - load_from((grid, predicate_animations_data)); + sd = _create_settings_obj(sd.general.with(sorting_criterion: rows_view.get_sorting_criteria(columns)), sd.predicate_animations); + load_from(sd); }; vm.RequestClose += (s, e) => WindowUtilities.force_close(window); return window; @@ -107,20 +118,37 @@ namespace mbts.ui { public SolidColorBrush selection_highlight => _selection_highlight.Value; public IDialogCommand launch_settings_editor { get; } - public (GridSettings grid, IEnumerable> predicate_animations_data) create() { - // IMPORTANT: don't overwrite current sorting - _grid_settings.set(grid_settings.with(_columns.Select(c => c.create()), _rows_view.get_sorting_criteria(_columns))); - return (grid_settings, _predicate_animations_data); + public GridSettingsVM create_vm(GridSettings settings, IObservable title) { + var vm = new GridSettingsVM(title, _bus, _orders_model, _create_predicate_vm, _create_column_predicate_vm, _create_settings_obj, _column_flags.ToDictionary(kv => kv.Key, kv => kv.Value.flags)); + vm.load(_create_settings_obj(settings, _predicate_animations_data)); + return vm; } - public void load_from((GridSettings grid, IEnumerable> predicate_animations_data) value) { + public TSettingsData create() { + // IMPORTANT: don't overwrite current sorting + _grid_settings.set(grid_settings.with(_columns.Select(c => { + var col_settings = c.create(); + if (_column_flags.TryGetValue(c.id, out ImmutableColumnSettingStore store)) + col_settings = store.apply(col_settings); + return col_settings; + }), _rows_view.get_sorting_criteria(_columns))); + return _create_settings_obj(grid_settings, _predicate_animations_data); + } + + public void load_from(TSettingsData value) => _load_from_core(value, true); + + void _load_from_core(TSettingsData value, bool reset_immutables) { Disposable.dispose(_notification_subs); - var grid = value.grid ?? GridSettings.Default; + var grid = value.general ?? GridSettings.Default; _grid_settings.set(grid); foreach (ColumnBase col in _columns) col.set_fallback_font_params(new FontFamily(grid.font_family), grid.font_size, grid.font_color.rgb_color(), grid.font_weight); - _rows_view.set_sort_descriptions(grid.sorting_criterion, _columns); - _predicate_animations_data = value.predicate_animations_data; + if (reset_immutables) { +#pragma warning disable CS0612 // Type or member is obsolete + _rows_view.set_sort_descriptions(grid.sorting_criterion, _columns); +#pragma warning restore CS0612 // Type or member is obsolete + } + _predicate_animations_data = value.predicate_animations; _animation_defs.clear(); foreach (var pa in _predicate_animations_data) { var predicate = pa.predicate_builder.create_predicate(_arg); @@ -140,6 +168,12 @@ namespace mbts.ui { // ** in the unfortunate event that we botch backwards compatability, and the columns don't bear a 1:1 relationship with the column-settings // don't add insult to injury by crashing ** if (grid_settings.columns != null && _columns != null) { + if (reset_immutables) { + foreach (var kv in _column_flags) { + if (grid.columns.TryGetFirst(c => c.id == kv.Key, out GridColumnSettings cs)) + kv.Value.store(cs); + } + } var col_settings_id_map = grid_settings.columns.ToDictionary(cs => id_key(cs.id)); var columns_id_map = _columns.ToDictionary(c => id_key(c.id)); if (col_settings_id_map.Count == columns_id_map.Count && col_settings_id_map.Count == grid_settings.columns.Count) { @@ -159,5 +193,26 @@ namespace mbts.ui { IGridColumn lookup_column(string col_id) => _columns.OfType>().FirstOrDefault(c => string.Equals(col_id, c.id, StringComparison.CurrentCultureIgnoreCase)); } + + void IObserver.OnNext(TSettingsData sd) { + if (sd == null) return; + try { + var grid = sd.general; + if (grid.columns != null) { + var cols_with_applied_flags = grid.columns.ToList(); + for (int i = 0; i < cols_with_applied_flags.Count; i++) { + if (_column_flags.TryGetValue(cols_with_applied_flags[i].id, out ImmutableColumnSettingStore store)) { + cols_with_applied_flags[i] = store.apply(cols_with_applied_flags[i]); + } + } + sd = _create_settings_obj(grid.with(cols_with_applied_flags), sd.predicate_animations); + } + _load_from_core(sd, false); + } catch (Exception ex) { + Log.Ui.error($"Error loading GridSettings (as observer): {ex}"); + } + } + void IObserver.OnError(Exception error) { } + void IObserver.OnCompleted() { } } } \ No newline at end of file diff --git a/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsVM.cs b/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsVM.cs index aa4264d68..1666e0fc0 100644 --- a/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsVM.cs +++ b/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsVM.cs @@ -1,5 +1,6 @@ using bts.api; using bts.shell; +using bts.utils; using mbts.collections; using mbts.collections.extensions; using mbts.ui; @@ -18,14 +19,38 @@ using CellAnimation = mbts.ui.CellAnimation; using FontFamily = System.Windows.Media.FontFamily; namespace mbts.spark { - interface IGridSettingsFactory : IValueFactory<(GridSettings grid, IEnumerable> predicate_animations_data)> - where TPredicateBuilder : IPredicateBuilder, new() { } + interface IGridSettingsWithNotifications where TPredicateBuilder : IPredicateBuilder { + GridSettings general { get; } + IEnumerable> predicate_animations { get; } + } - partial class GridSettingsVM : DisposableViewModelBase, IGridSettingsFactory, IRequestViewClose - where TPredicateBuilder : IPredicateBuilder, new() { + class GridSettingsWithNotifications : IEquatable> where TPredicateBuilder : IPredicateBuilder { + public readonly GridSettings grid; + public readonly IEnumerable> predicate_animations_data; + public GridSettingsWithNotifications(GridSettings grid, IEnumerable> predicate_animations_data) { + this.grid = grid; + this.predicate_animations_data = predicate_animations_data; + } + + public bool Equals(GridSettingsWithNotifications other) => + other != null && + EqualityComparer.null_check_equals(grid, other.grid) && + EqualityComparer.safe_sequence_equals(predicate_animations_data, other.predicate_animations_data); + + public override bool Equals(object obj) => Equals(obj as GridSettingsWithNotifications); + public override int GetHashCode() => + EqualityComparer.aggregate_hash( + EqualityComparer.safe_get_hash_code(grid), + EqualityComparer.safe_aggregate_hash(predicate_animations_data)); + } + + partial class GridSettingsVM : DisposableViewModelBase, IRequestViewClose, IWindowSettingsVM, ITemplateSelectorKey + where TPredicateBuilder : IPredicateBuilder, new() + where TSettingsData :class, IGridSettingsWithNotifications { // protected readonly Func> _create_predicate_vm; protected readonly Func> _create_column_predicate_vm; + readonly Func>, TSettingsData> _create_settings_obj; readonly Property _current_column; readonly Property _font_family; readonly Property _font_weight; @@ -33,16 +58,22 @@ namespace mbts.spark { readonly Property _font_size; readonly Property _show_predicate_animations, _show_cell_predicate_animations; readonly OrdersModel _orders_model; + readonly IReadOnlyDictionary _column_flags; public GridSettingsVM( IObservable owner_title, IBus bus, OrdersModel orders_model, - Func> create_predicate_vm, Func> create_column_predicate_vm) { + Func> create_predicate_vm, + Func> create_column_predicate_vm, + Func>, TSettingsData> create_settings_obj, + IReadOnlyDictionary column_flags = null) { // _owner_title = backing(nameof(owner_title), owner_title); this.bus = bus; _orders_model = orders_model; _create_predicate_vm = create_predicate_vm; _create_column_predicate_vm = create_column_predicate_vm; + _create_settings_obj = create_settings_obj; + _column_flags = column_flags ?? new Dictionary(); _font_family = backing(nameof(font_family), TextUtils.StandardFontFamily); _font_size = backing(nameof(font_size), FontSizes.Small); _font_weight = backing(nameof(font_weight), FontWeights.Normal); @@ -68,6 +99,7 @@ namespace mbts.spark { public ColorPickerPopupVM font_color { get; } = new ColorPickerPopupVM(); public ColorPickerPopupVM selection_highlight { get; } = new ColorPickerPopupVM(); public double font_size { get => _font_size.Value; set => _font_size.set(value); } + public bool enable_sound_triggers => true; public bool show_predicate_animations { get => _show_predicate_animations.Value; set => _show_predicate_animations.set(value); } public bool show_cell_predicate_animations { get => _show_cell_predicate_animations.Value; set => _show_cell_predicate_animations.set(value); } public ObservableEditableCollection predicate_animations { get; } @@ -80,7 +112,9 @@ namespace mbts.spark { new DialogSkirtItem("Apply", new DelegateCommand(() => committed?.Invoke())) }; - public (GridSettings grid, IEnumerable> predicate_animations_data) create() { + public string key => ui.spark.GridOrLadderSettingsSelectorKeys.SimpleGrid; + + public TSettingsData get_result() { var animation_map = predicate_animations .Select(p => p.create()) .GroupBy(d => d.predicate) @@ -94,22 +128,22 @@ namespace mbts.spark { foreach (IGrouping g in animations) animation_map.AddOrUpdate(g.Key, p => animation_tuple(c: g), (p, t) => t.cell_animations.AddRange(g)); } - return (new GridSettings(font_family.name(), font_size, font_weight, font_color.color.rgb_color(), selection_highlight.to_proto(), col_settings), + return _create_settings_obj(new GridSettings(font_family.name(), font_size, font_weight, font_color.color.rgb_color(), selection_highlight.to_proto(), col_settings), animation_map.Select(kv => PredicateAnimations.create_data(kv.Key, kv.Value.cell_animations, kv.Value.row_animations, kv.Value.sounds))); // (List row_animations, List sounds, List cell_animations) animation_tuple(IEnumerable r = null, IEnumerable s = null, IGrouping c = null) => (r?.ToList() ?? new List(), s?.ToList() ?? new List(), c?.ToList() ?? new List()); } - public void load_from((GridSettings grid, IEnumerable> predicate_animations_data) value) { - _font_family.set(new FontFamily(value.grid.font_family)); - _font_weight.set(value.grid.font_weight); - font_color.load_from(value.grid.font_color.rgb_solid_brush()); - selection_highlight.load_from(value.grid.selection_highlight.to_brush()); - _font_size.set(value.grid.font_size); + public void load(TSettingsData sd) { + _font_family.set(new FontFamily(sd.general.font_family)); + _font_weight.set(sd.general.font_weight); + font_color.load_from(sd.general.font_color.rgb_solid_brush()); + selection_highlight.load_from(sd.general.selection_highlight.to_brush()); + _font_size.set(sd.general.font_size); var col_animations = new Dictionary>>(); predicate_animations.Clear(); - foreach (var pa in value.predicate_animations_data) { + foreach (var pa in sd.predicate_animations) { if (pa.row_animations.Count > 0 || pa.sound_notifications.Count > 0) { var vm = new PredicateRowAnimationVM(_create_predicate_vm()); vm.load_from((pa.predicate_builder, pa.row_animations, pa.sound_notifications)); @@ -122,9 +156,14 @@ namespace mbts.spark { } } var curr_col_id = current_column?.id; - columns.SetItems(value.grid.columns.Select(col => { + columns.SetItems(sd.general.columns.Select(col => { var vm = new ColumnVM(this); vm.load_from((col, col_animations.GetValueOrDefault(col.id)?.ToFlatLookup(kv => kv.Key, kv => kv.Value))); + // reapply our favorite column flags + // this comes in handy when performing Load From Factory - the editor will contain + // a consistent view of what can be overridden and what cannot + if (_column_flags.TryGetValue(vm.id, out GridColumnSettingsFeatureFlags flags)) + vm.apply_flags(flags); return vm; })); if (curr_col_id == null || !columns.TryGetFirst(c => c.id == curr_col_id, out IColumnVM curr_column)) diff --git a/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsView.xaml b/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsView.xaml index a6adf52c3..7fc1656d4 100644 --- a/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsView.xaml +++ b/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsView.xaml @@ -342,9 +342,8 @@ - - Font - + Font + @@ -359,6 +358,9 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/client/spark/view/Controls/DIYGrid/Settings/SimpleGridSettingsDialog.xaml.cs b/src/client/spark/view/Controls/DIYGrid/Settings/SimpleGridSettingsDialog.xaml.cs new file mode 100644 index 000000000..5503a853b --- /dev/null +++ b/src/client/spark/view/Controls/DIYGrid/Settings/SimpleGridSettingsDialog.xaml.cs @@ -0,0 +1,19 @@ +using System.Windows; + +namespace mbts.ui.spark { + /// + /// Interaction logic for SimpleGridSettingsDialog.xaml + /// + public partial class SimpleGridSettingsDialog : Window { + public SimpleGridSettingsDialog() => + InitializeComponent(); + + public const string ResetToFactoryKey = "Reset", CopyFromOtherKey = "Copy"; + } + + static class GridOrLadderSettingsSelectorKeys { + public const string + SimpleGrid = nameof(SimpleGrid), + Ladder = nameof(Ladder); + } +} \ No newline at end of file diff --git a/src/client/spark/view/Controls/DIYGrid/Settings/SimpleGridSettingsManager.cs b/src/client/spark/view/Controls/DIYGrid/Settings/SimpleGridSettingsManager.cs new file mode 100644 index 000000000..e1b1d22bc --- /dev/null +++ b/src/client/spark/view/Controls/DIYGrid/Settings/SimpleGridSettingsManager.cs @@ -0,0 +1,121 @@ +using mbts.spark; +using mbts.ui.settings; +using mbts.ui.shell; +using mbts.utils; +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Linq; +using System.Reactive.Linq; +using System.Windows; +using System.Windows.Media; +using static mbts.ui.controls.DIYGrid; + +namespace mbts.ui { + class SimpleGridSettingsManager : ViewModelBase, IObserver { + readonly ObservableCollection _columns; + readonly IReadOnlyCollection _row_vms; + readonly Property _grid_settings; + readonly DelegateProperty _font_family; + readonly DelegateProperty _font_weight; + readonly DelegateProperty _font_size; + readonly DelegateProperty _font_color, _selection_highlight; + readonly IReadOnlyDictionary _column_flags; + + public SimpleGridSettingsManager(ObservableCollection columns) + : this(columns, new Dictionary(), null) { } + + public SimpleGridSettingsManager(ObservableCollection columns, IReadOnlyDictionary column_flags, IReadOnlyCollection row_vms) { + _columns = columns; + _column_flags = column_flags.ToDictionary(kv => kv.Key, kv => ImmutableColumnSettingStore.create(kv.Value)); + _grid_settings = backing(nameof(grid_settings), GridSettings.Default); + _font_family = backing(nameof(font_family), () => new FontFamily(grid_settings.font_family), nameof(grid_settings)); + _font_weight = backing(nameof(font_weight), () => grid_settings.font_weight, nameof(grid_settings)); + _font_size = backing(nameof(font_size), () => grid_settings.font_size, nameof(font_size)); + _font_color = backing(nameof(font_color), () => grid_settings.font_color.rgb_solid_brush().frozen(), nameof(grid_settings)); + _selection_highlight = backing(nameof(selection_highlight), () => grid_settings.selection_highlight.to_brush().frozen(), nameof(grid_settings)); + _row_vms = row_vms; + } + + public GridSettings grid_settings => _grid_settings.Value; + public FontFamily font_family => _font_family.Value; + public FontWeight font_weight => _font_weight.Value; + public double font_size => _font_size.Value; + public SolidColorBrush font_color => _font_color.Value; + public SolidColorBrush selection_highlight => _selection_highlight.Value; + + public SimpleGridSettingsVM create_vm(GridSettings settings, IObservable title) { + var vm = new SimpleGridSettingsVM(title, _column_flags.ToDictionary(kv => kv.Key, kv => kv.Value.flags)); + vm.load_from(settings); + return vm; + } + + public GridSettings create() { + // IMPORTANT: don't overwrite current sorting + _grid_settings.set(grid_settings.with(_columns.Select(c => { + var col_settings = c.create(); + if (_column_flags.TryGetValue(c.id, out ImmutableColumnSettingStore store)) + col_settings = store.apply(col_settings); + return col_settings; + }))); + return grid_settings; + } + + /// + /// This will overwrite the columns' immutable-value store (unlike the IObserver<> 'OnNext' implementation) + /// + public void load_from(GridSettings grid) => _load_from_core(grid, true); + + void IObserver.OnCompleted() { } + void IObserver.OnError(Exception error) { } + void IObserver.OnNext(GridSettings grid) { + if (grid.columns != null) { + var cols_with_applied_flags = grid.columns.ToList(); + for (int i = 0; i < cols_with_applied_flags.Count; i++) { + if (_column_flags.TryGetValue(cols_with_applied_flags[i].id, out ImmutableColumnSettingStore store)) { + cols_with_applied_flags[i] = store.apply(cols_with_applied_flags[i]); + } + } + grid = grid.with(cols_with_applied_flags); + } + _load_from_core(grid, false); + } + + void _load_from_core(GridSettings grid, bool reset_immutable_flag_stores) { + grid = grid ?? GridSettings.Default; + _grid_settings.set(grid); + foreach (ColumnBase col in _columns) + col.set_fallback_font_params(new FontFamily(grid.font_family), grid.font_size, grid.font_color.rgb_color(), grid.font_weight); + // ** in the unfortunate event that we botch backwards compatability, and the columns don't bear a 1:1 relationship with the column-settings + // don't add insult to injury by crashing ** + if (grid_settings.columns != null) { + if (reset_immutable_flag_stores) { + foreach (var kv in _column_flags) { + if (grid.columns.TryGetFirst(c => c.id == kv.Key, out GridColumnSettings cs)) + kv.Value.store(cs); + } + } + if (_columns != null) { + var col_settings_id_map = grid_settings.columns.ToDictionary(cs => id_key(cs.id)); + var columns_id_map = _columns.ToDictionary(c => id_key(c.id)); + if (col_settings_id_map.Count == columns_id_map.Count && col_settings_id_map.Count == grid_settings.columns.Count) { + CollectionHelper.sync_with_never_duplicate(_columns, grid_settings.columns, cs => { + throw new InvalidOperationException(); + }, c => id_key(c.id), cs => id_key(cs.id)); + } + // ** don't assume that columns:column_settings have a perfect 1:1 relationship (they should, but it is possible we accidentally break the settings as grids/uis change) ** + foreach (var col in _columns) { + if (col_settings_id_map.TryGetValue(id_key(col.id), out GridColumnSettings cs)) + col.load_from(cs); + } + } + // invalidate all the rows to draw them again to reflect the latest settings + if (_row_vms != null) + foreach (var r in _row_vms) r.raise_invalidated(); + + // + string id_key(string id) => id.ToLower(); + } + } + } +} diff --git a/src/client/spark/view/Controls/DIYGrid/Settings/SimpleGridSettingsVM.cs b/src/client/spark/view/Controls/DIYGrid/Settings/SimpleGridSettingsVM.cs new file mode 100644 index 000000000..55c695217 --- /dev/null +++ b/src/client/spark/view/Controls/DIYGrid/Settings/SimpleGridSettingsVM.cs @@ -0,0 +1,102 @@ +using mbts.ui; +using mbts.ui.controls; +using mbts.ui.settings; +using mbts.ui.shell; +using mbts.ui.utility.text; +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Linq; +using System.Reactive.Linq; +using System.Windows; +using FontFamily = System.Windows.Media.FontFamily; + +namespace mbts.spark { + interface ISimpleColumnVM : IValueFactory { + string id { get; } + string display_name { get; set; } + double width { get; set; } + bool is_visible { get; set; } + bool is_font_color_overridden { get; set; } + ColorPickerPopupVM font_color_override { get; } + bool is_font_weight_overridden { get; set; } + FontWeight font_weight_override { get; set; } + void apply_flags(GridColumnSettingsFeatureFlags flags); + } + + partial class SimpleGridSettingsVM : DisposableViewModelBase, IValueFactory, IWindowSettingsVM, ITemplateSelectorKey { + readonly Property _current_column; + readonly Property _font_family; + readonly Property _font_weight; + readonly Property _font_display_name, _owner_title; + readonly Property _font_size; + readonly Property _show_predicate_animations, _show_cell_predicate_animations; + readonly IReadOnlyDictionary _column_flags; + + public SimpleGridSettingsVM(IObservable owner_title, IReadOnlyDictionary column_flags = null) { + _column_flags = column_flags ?? new Dictionary(); + _owner_title = backing(nameof(owner_title), owner_title); + _font_family = backing(nameof(font_family), TextUtils.StandardFontFamily); + _font_size = backing(nameof(font_size), FontSizes.Small); + _font_weight = backing(nameof(font_weight), FontWeights.Normal); + _font_display_name = backing(nameof(font_display_name), () => TextUtils.display_name(font_family, font_size), nameof(font_family), nameof(font_size)); + _show_predicate_animations = backing(nameof(show_predicate_animations), true); + _show_cell_predicate_animations = backing(nameof(show_cell_predicate_animations), true); + _current_column = backing(nameof(current_column)); + } + + public event EventHandler RequestClose; + public event Action committed; + + public string owner_title => _owner_title.Value; + public ObservableCollection columns { get; } = new ObservableCollection(); + public ISimpleColumnVM current_column { get => _current_column.Value; set => _current_column.set(value); } + public string font_display_name => _font_display_name.Value; + public FontFamily font_family { get => _font_family.Value; set => _font_family.set(value); } + public FontWeight font_weight { get => _font_weight.Value; set => _font_weight.set(value); } + public ColorPickerPopupVM font_color { get; } = new ColorPickerPopupVM(); + public ColorPickerPopupVM selection_highlight { get; } = new ColorPickerPopupVM(); + public double font_size { get => _font_size.Value; set => _font_size.set(value); } + public bool enable_sound_triggers => false; + public bool show_predicate_animations { get => _show_predicate_animations.Value; set => _show_predicate_animations.set(value); } + public bool show_cell_predicate_animations { get => _show_cell_predicate_animations.Value; set => _show_cell_predicate_animations.set(value); } + public IReadOnlyList dialog_skirt_items => new[] { + new DialogSkirtItem("OK", new DelegateCommand(() => { + committed?.Invoke(); + RequestClose?.Invoke(this, new EventArgs()); + })) { is_default = true }, + new DialogSkirtItem("Cancel", new DelegateCommand(() => RequestClose?.Invoke(this, new EventArgs()))) { is_cancel = true }, + new DialogSkirtItem("Apply", new DelegateCommand(() => committed?.Invoke())) + }; + + public string key => ui.spark.GridOrLadderSettingsSelectorKeys.SimpleGrid; + + public GridSettings create() => + new GridSettings(font_family.name(), font_size, font_weight, font_color.color.rgb_color(), selection_highlight.to_proto(), columns.Select(c => c.create())); + + public void load_from(GridSettings value) { + _font_family.set(new FontFamily(value.font_family)); + _font_weight.set(value.font_weight); + font_color.load_from(value.font_color.rgb_solid_brush()); + selection_highlight.load_from(value.selection_highlight.to_brush()); + _font_size.set(value.font_size); + var curr_col_id = current_column?.id; + columns.SetItems(value.columns.Select(col => { + var vm = new SimpleColumnVM(this); + vm.load_from(col); + // reapply our favorite column flags + // this comes in handy when performing Load From Factory - the editor will contain + // a consistent view of what can be overridden and what cannot + if (_column_flags.TryGetValue(vm.id, out GridColumnSettingsFeatureFlags flags)) + vm.apply_flags(flags); + return vm; + })); + if (curr_col_id == null || !columns.TryGetFirst(c => c.id == curr_col_id, out ISimpleColumnVM curr_column)) + curr_column = columns.FirstOrDefault(); + _current_column.set(curr_column); + } + + void IWindowSettingsVM.load(GridSettings value) => load_from(value); + GridSettings IWindowSettingsVM.get_result() => create(); + } +} \ No newline at end of file diff --git a/src/client/spark/view/Controls/TickFactorsPicker/TickFactorsPicker.xaml.cs b/src/client/spark/view/Controls/TickFactorsPicker/TickFactorsPicker.xaml.cs index 37c782162..7d14d8064 100644 --- a/src/client/spark/view/Controls/TickFactorsPicker/TickFactorsPicker.xaml.cs +++ b/src/client/spark/view/Controls/TickFactorsPicker/TickFactorsPicker.xaml.cs @@ -66,6 +66,8 @@ namespace mbts.ui.controls { public ICollectionView TickUnitPresetsView { get; } static IEnumerable _get_tick_unit_presets(double tick_base) { + if (DoubleHelper.IsNaNOrInfinity(tick_base)) + return Enumerable.Empty(); double reduced_base = MathUtils.factor_out(tick_base, 10, out int base_log10); return reduced_base == 1 ? _get_dbased_tick_unit_presets(base_log10) : diff --git a/src/client/spark/view/Converters/AnonymousValueConverter.cs b/src/client/spark/view/Converters/AnonymousValueConverter.cs index 5b7a396f5..10882f471 100644 --- a/src/client/spark/view/Converters/AnonymousValueConverter.cs +++ b/src/client/spark/view/Converters/AnonymousValueConverter.cs @@ -9,6 +9,9 @@ namespace mbts.ui.converters { public static IMultiValueConverter create(Func convert) => new AnonymousMultiValueConverter(convert); + + public static IMultiValueConverter create(Func convert) => + new AnonymousMultiValueConverter(convert); } public class AnonymousValueConverter : IValueConverter { @@ -50,4 +53,17 @@ namespace mbts.ui.converters { public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) => throw new NotImplementedException(); } + + public class AnonymousMultiValueConverter : IMultiValueConverter { + readonly Func _convert; + + public AnonymousMultiValueConverter(Func convert) => _convert = convert; + + public TOut ErrorValue { get; set; } + + public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) => + ObjectHelper.try_convert_args(values, out TIn1 a, out TIn2 b, out TIn3 c) ? _convert(a, b, c) : ErrorValue; + + public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) => throw new NotImplementedException(); + } } \ No newline at end of file diff --git a/src/client/spark/view/Converters/NumericSignBrushConverter.cs b/src/client/spark/view/Converters/NumericSignBrushConverter.cs index f3e434d75..da6e261b3 100644 --- a/src/client/spark/view/Converters/NumericSignBrushConverter.cs +++ b/src/client/spark/view/Converters/NumericSignBrushConverter.cs @@ -4,28 +4,48 @@ using System.Windows.Data; using System.Windows.Media; namespace mbts.ui.converters { - public class NumericSignBrushConverter : IValueConverter { - static readonly SolidColorBrush _error_brush = new SolidColorBrush(Colors.DimGray); + struct SignedDataStore { + public readonly T positive, neutral, negative, error; - SolidColorBrush _negative_brush = new SolidColorBrush(Colors.Red), - _neutral_brush = new SolidColorBrush(Colors.Black), - _positive_brush = new SolidColorBrush(Colors.Green); + public SignedDataStore(T positive, T neutral, T negative, T error) { + this.positive = positive; + this.neutral = neutral; + this.negative = negative; + this.error = error; + } + } - static NumericSignBrushConverter() => - _error_brush.Freeze(); + static class SignedDataStoreExtensions { + public static T get(this SignedDataStore store, double value) { + if (double.IsNaN(value)) return store.error; + else if (value > 0) return store.positive; + else if (value < 0) return store.negative; + else return store.neutral; + } - public Color PositiveColor { get => _positive_brush.Color; set => (_positive_brush = new SolidColorBrush(value) { Opacity = _opacity }).Freeze(); } - public Color NeutralColor { get => _neutral_brush.Color; set => (_neutral_brush = new SolidColorBrush(value) { Opacity = _opacity }).Freeze(); } - public Color NegativeColor { get => _negative_brush.Color; set => (_negative_brush = new SolidColorBrush(value) { Opacity = _opacity }).Freeze(); } + public static SignedDataStore with_positive(this SignedDataStore store, T positive) => new SignedDataStore(positive, store.neutral, store.negative, store.error); + public static SignedDataStore with_neutral(this SignedDataStore store, T neutral) => new SignedDataStore(store.positive, neutral, store.negative, store.error); + public static SignedDataStore with_negative(this SignedDataStore store, T negative) => new SignedDataStore(store.positive, store.neutral, negative, store.error); + } - private double _opacity = 1.0; - public Double Opacity { - get { return _opacity; } + class NumericSignBrushConverter : IValueConverter { + SignedDataStore _brushes = new SignedDataStore( + Brushes.Green, Brushes.Black, Brushes.Red, Brushes.DimGray); + + public Color PositiveColor { get => _brushes.positive.Color; set => _brushes = _brushes.with_positive(new SolidColorBrush(value) { Opacity = _opacity }.frozen()); } + public Color NeutralColor { get => _brushes.neutral.Color; set => _brushes = _brushes.with_neutral(new SolidColorBrush(value) { Opacity = _opacity }.frozen()); } + public Color NegativeColor { get => _brushes.negative.Color; set => _brushes = _brushes.with_negative(new SolidColorBrush(value) { Opacity = _opacity }.frozen()); } + + double _opacity = 1.0; + public double Opacity { + get => _opacity; set { _opacity = value; - (_positive_brush = new SolidColorBrush(_positive_brush.Color) { Opacity = _opacity }).Freeze(); - (_negative_brush = new SolidColorBrush(_negative_brush.Color) { Opacity = _opacity }).Freeze(); - (_neutral_brush = new SolidColorBrush(_neutral_brush.Color) { Opacity = _opacity }).Freeze(); + _brushes = new SignedDataStore( + _brushes.positive.with(value).frozen(), + _brushes.neutral.with(value).frozen(), + _brushes.negative.with(value).frozen(), + _brushes.error.with(value).frozen()); } } public IComparable Zero { get; set; } = 0.0; @@ -33,9 +53,9 @@ namespace mbts.ui.converters { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { var comparable = value as IComparable; if (comparable == null || comparable is double && double.IsNaN((double)value)) - return _error_brush; + return _brushes.error; int comp = comparable.CompareTo(Zero); - return comp == 0 ? _neutral_brush : comp > 0 ? _positive_brush : _negative_brush; + return comp == 0 ? _brushes.neutral : comp > 0 ? _brushes.positive : _brushes.negative; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => diff --git a/src/client/spark/view/Extensions/Command/FileDialogCommand.cs b/src/client/spark/view/Extensions/Command/FileDialogCommand.cs index 21e67e1aa..6e52b47cd 100644 --- a/src/client/spark/view/Extensions/Command/FileDialogCommand.cs +++ b/src/client/spark/view/Extensions/Command/FileDialogCommand.cs @@ -51,6 +51,8 @@ namespace mbts.ui { try { using (var stream = File.Open(file, FileMode.Open, FileAccess.Read)) on_import(ProtoBufSerializationHelper.deserialize(stream)); + } catch (ProtoBuf.ProtoException e) when (e.Message.StartsWith("Invalid wire-type;")) { + BTSMessageBox.Show("This doesn't appear to be the correct settings file for this window.\r\n\r\nPlease try importing a different settings file.", error_msg_title ?? "Import", BTSMessageBoxButtons.OK, System.Windows.MessageBoxImage.Hand, true, false, true); } catch (Exception ex) { BTSMessageBox.Show($"Error importing {type_name ?? "item"} at '{file}': {ex}.", error_msg_title ?? "Import", BTSMessageBoxButtons.OK, System.Windows.MessageBoxImage.Error, true, false, true); } diff --git a/src/client/spark/view/Extensions/WPFExtensions.cs b/src/client/spark/view/Extensions/WPFExtensions.cs index 5aa34a548..127405dc1 100644 --- a/src/client/spark/view/Extensions/WPFExtensions.cs +++ b/src/client/spark/view/Extensions/WPFExtensions.cs @@ -98,6 +98,11 @@ namespace mbts.ui { return brush; } + public static SolidColorBrush with(this SolidColorBrush brush, double opacity) { + brush.Opacity = opacity; + return brush; + } + public static Pen frozen(this Pen pen) { pen.Freeze(); return pen; @@ -296,11 +301,6 @@ namespace mbts.ui { return new Size(ft.Width, ft.Height); } - // TODO: mark this method Obsolete ([John]I decided against doing it right now because this is part of a large change that still needs to be merged, - // and adding all the supress warnings to the calling code may add complications). - public static List GetSortingCriteria(this ICollectionView collection_view) => - collection_view.SortDescriptions.Select(sd => new SortingCriteria(sd)).ToList(); - public static void SetSortingCriteria(this ICollectionView collection_view, IEnumerable sorting_criteria) { collection_view.SortDescriptions.Clear(); if (sorting_criteria != null) diff --git a/src/client/spark/view/Spark/Core/TickUtils.cs b/src/client/spark/view/Spark/Core/TickUtils.cs index fad161d3e..903039173 100644 --- a/src/client/spark/view/Spark/Core/TickUtils.cs +++ b/src/client/spark/view/Spark/Core/TickUtils.cs @@ -1,9 +1,7 @@ using bts.api; using bts.utils; using mbts.co2; -using mbts.spark.utilities; using mbts.user.settings; -using mbts.utils; using System; using System.Collections.Concurrent; using System.Collections.Generic; @@ -62,16 +60,16 @@ namespace mbts.spark { return high; } - public static (int n_low, int n_high) tick_diff(ContractSpecificationImmutable spec, double x, double y) => - get_regional_ticks(spec).tickdiff(x, y); - - public static int normalized_tick_diff(ContractSpecificationImmutable spec, double x, double y) { - var (n_low, n_high) = tick_diff(spec, x, y); - return n_low + n_high; + public static int? tickdiff(this ContractSpecificationImmutable spec, double x, double y) { + if (get_regional_ticks(spec).try_get_tickdiff(x, y, out int n_low, out int n_high)) + return n_low + n_high; + return null; } - public static int normalized_tick_diff(SecurityImmutable security, double x, double y) => - normalized_tick_diff(security.spec_or_default(), x, y); + public static int tickdiff_unchecked(this ContractSpecificationImmutable spec, double x, double y) { + (int n_low, int n_high) = get_regional_ticks(spec).tickdiff_unchecked(x, y); + return n_low + n_high; + } public static double mid_tick(ContractSpecificationImmutable spec, double bid, double ask) { double raw_mid; diff --git a/src/client/spark/view/Spark/Ladder/LadderGridHost.cs b/src/client/spark/view/Spark/Ladder/LadderGridHost.cs index f2248f4e1..aceefbcf8 100644 --- a/src/client/spark/view/Spark/Ladder/LadderGridHost.cs +++ b/src/client/spark/view/Spark/Ladder/LadderGridHost.cs @@ -1,9 +1,11 @@ using bts; using bts.api; +using bts.api.md.auxiliary; using bts.utils; using mbts.co2; using mbts.ui; using mbts.ui.controls; +using mbts.ui.spark; using mbts.ui.utility.text; using mbts.utils; using System; @@ -38,9 +40,13 @@ namespace mbts.spark { _bid_vis_tup = new VisualGlyphTuple(), _ask_vis_tup = new VisualGlyphTuple(), _price_vis_tup = new VisualGlyphTuple(), - _last_vis_tup = new VisualGlyphTuple(); + _last_vis_tup = new VisualGlyphTuple(), + _volume_vis_tup = new VisualGlyphTuple(); - IDisposable _settings_sub, _tick_sub; + readonly RefValueContainer> _volume_container = + new RefValueContainer>(); + + IDisposable _settings_sub, _volume_sub, _tick_sub; ScrollViewer _scrollViewer; LadderSettings _settings; LadderVM _vm; @@ -53,6 +59,7 @@ namespace mbts.spark { double _last_origin_price; MarketBreakLines _mkt_break_lines; NativePriceSize _last_trade; + Func _fmt_size = MDDisplayHelper.get_formatter_nps(MarketSizeDisplay.get_default()); public LadderGridHost() { AllowDrop = true; @@ -79,7 +86,13 @@ namespace mbts.spark { }; } - enum ColumnType { Order, Bid, Price, Ask, Last, None } + public enum ColumnType { Order, Bid, Price, Ask, Last, Volume, None } + + public event Action visible_columns_changed; + + public IEnumerable visible_columns => + columns.Where(c => c.settings.show) + .Select(c => c.column_type); public SecurityImmutable security => _vm != null ? _vm.security : SecurityDataHelper.Defaults.SecurityImmutable; public ICommand RecenterCommand { get; } @@ -149,6 +162,29 @@ namespace mbts.spark { lgh._adjust_column_width(lgh.columns.First(c => c.column_type == ColumnType.Ask), width_adjustment); } + public static readonly DependencyProperty VolumeColumnWidthProperty = + WPFHelper.create_dp( + nameof(VolumeColumnWidth), + FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, + (o, oldval, newval) => o._adjust_column_width(o.columns.First(c => c.column_type == ColumnType.Volume), (double)newval - (double)oldval), + 50u); + + public uint VolumeColumnWidth { + get => (uint)GetValue(VolumeColumnWidthProperty); + set => SetValue(VolumeColumnWidthProperty, value); + } + + public static readonly DependencyProperty LastColumnWidthProperty = + WPFHelper.create_dp( + nameof(LastColumnWidth), + FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, + (o, oldval, newval) => o._adjust_column_width(o.columns.First(c => c.column_type == ColumnType.Last), (double)newval - (double)oldval), + 50u); + + public uint LastColumnWidth { + get => (uint)GetValue(LastColumnWidthProperty); + set => SetValue(LastColumnWidthProperty, value); + } #region Command public ICommand Command { @@ -297,6 +333,7 @@ namespace mbts.spark { case ColumnType.Bid: new_col_width = (int)bid_column_width; break; case ColumnType.Price: new_col_width = (int)price_column_width; break; case ColumnType.Ask: new_col_width = (int)ask_column_width; break; + case ColumnType.Volume: new_col_width = (int)VolumeColumnWidth; break; default: new_col_width = (int)Math.Max((double)column.settings.width + adjustment, _column_min_width); break; } int true_adj = new_col_width - (int)column.settings.width; @@ -321,6 +358,10 @@ namespace mbts.spark { security.ContractSpec); break; case ColumnType.Ask: render_market_col(_asks); break; + case ColumnType.Volume: + _volume_container.mark_dirty(); + _refresh_volume_at_price(); + break; } if (Array.IndexOf(columns, column) <= Array.IndexOf(columns, _price_column)) { if (_mkt_break_lines.low_render_index.HasValue) @@ -330,7 +371,7 @@ namespace mbts.spark { } void render_market_col(NativePriceSize[] depth) { var mid = LadderRender.index_of(_price_list, _last_origin_price, _price_comparer); - LadderRender.render_sizes(column.visual_tup.visual, column.visual_tup.glyph_context, mid, _price_list, depth, _settings.row_height, security.ContractSpec, column.settings); + LadderRender.render_sizes(column.visual_tup.visual, column.visual_tup.glyph_context, mid, _price_list, depth, _settings.row_height, security.ContractSpec, column.settings, _fmt_size); } } _apply_column_visual_offset(); @@ -340,30 +381,46 @@ namespace mbts.spark { Column _price_column; void _set_columns() { double x = 0; - columns = new[] { + var col_list = new List { new Column(_settings.orders_column, ColumnType.Order, null, x), new Column(_settings.bid_column, ColumnType.Bid, _bid_vis_tup, (x += _settings.orders_column.show ? _settings.orders_column.width : 0)), - _price_column = new Column(_settings.price_column, ColumnType.Price, _price_vis_tup, (x += _settings.bid_column.show ? _settings.bid_column.width : 0)), - new Column(_settings.ask_column, ColumnType.Ask, _ask_vis_tup, (x += _settings.price_column.show ? _settings.price_column.width : 0)), - new Column(_settings.last_column, ColumnType.Last, _last_vis_tup, (x += _settings.ask_column.show ? _settings.ask_column.width : 0)) + (_price_column = new Column(_settings.price_column, ColumnType.Price, _price_vis_tup, (x += _settings.bid_column.show ? _settings.bid_column.width : 0))), + new Column(_settings.ask_column, ColumnType.Ask, _ask_vis_tup, (x += _settings.price_column.show ? _settings.price_column.width : 0)) }; + if (security.ContractCase != Security.ContractOneofCase.Synthetic) { + col_list.Add(new Column(_settings.volume_column.general, ColumnType.Volume, _volume_vis_tup, (x += _settings.ask_column.show ? _settings.ask_column.width : 0))); + col_list.Add(new Column(_settings.last_column, ColumnType.Last, _last_vis_tup, (x += _settings.volume_column.general.show ? _settings.volume_column.general.width : 0))); + } + columns = col_list.ToArray(); } #endregion void _connect_vm() { - Disposable.dispose(ref _settings_sub); - Disposable.dispose(ref _tick_sub); + _dispose_resources(); _vm = DataContext as LadderVM; if (_vm != null) { + _vm.OnDisposed += _on_vm_disposed; _prices_updated = 0; _price_comparer = Comparer.Create((x, y) => security.ContractSpec.tick_equals(x, y) ? 0 : y.CompareTo(x)); _price_dirty = true; _vm.on_tick += _tick; _settings_sub = _vm.settings.OnDispatcher().Subscribe(_settings_updated); + _volume_sub = _vm.model.md_auxiliary_model.security_volume_at_price(security.Id).Subscribe(t => _volume_container.write_value(t.state.Values)); _tick_sub = Disposable.Create(() => _vm.on_tick -= _tick); } } + void _on_vm_disposed(bool obj) => + _dispose_resources(); + + void _dispose_resources() { + Disposable.dispose(ref _settings_sub); + Disposable.dispose(ref _volume_sub); + Disposable.dispose(ref _tick_sub); + if (_vm != null) + _vm.OnDisposed -= _on_vm_disposed; + } + void _scroll_to_center() { if (_scrollViewer != null && _settings != null) { int index = _price_list != null && !double.IsNaN(_last_origin_price) ? @@ -391,7 +448,7 @@ namespace mbts.spark { if (_settings != null) { ulong unused = 0; bool last_trade_changed = _vm.md.get_last_trade(ref unused, out _last_trade, out DateTime _, out ulong __); - bool depth_changed = _vm.md.get_depth(ref _prices_updated, ref _bids, ref _asks); + bool depth_changed = _vm.md.get_depth(ref _prices_updated, ref _bids, ref _asks, NativePriceHelpers.COCONUT_MAX_DEPTH); _mkt_break_lines.refresh(); bool prices_changed = _price_dirty || @@ -409,7 +466,6 @@ namespace mbts.spark { _refresh_session_high_line(); _refresh_session_range(); _refresh_last_trade(); - _refresh_depth(); _refresh_sizes(); } else { if (_mkt_break_lines.bid_dirty) @@ -429,13 +485,10 @@ namespace mbts.spark { if (last_index > 0) LadderRender.render_last(_last_vis_tup.visual, _last_vis_tup.glyph_context, _last_trade, last_index, _settings.row_height, _settings.last_column); } - if (depth_changed) { - Log.assert_d(_price_list != null); - int origin_index = LadderRender.index_of(_price_list, _mkt_break_lines.origin, _price_comparer); - LadderRender.render_sizes(_bid_vis_tup.visual, _bid_vis_tup.glyph_context, origin_index, _price_list, _bids, _settings.row_height, security.ContractSpec, _settings.bid_column); - LadderRender.render_sizes(_ask_vis_tup.visual, _ask_vis_tup.glyph_context, origin_index, _price_list, _asks, _settings.row_height, security.ContractSpec, _settings.ask_column); - } + if (depth_changed) + _refresh_sizes(); } + _refresh_volume_at_price(); _last_origin_price = _mkt_break_lines.origin; _price_dirty = false; } @@ -474,15 +527,21 @@ namespace mbts.spark { void _refresh_sizes() { Log.assert_d(_price_list != null); int origin_index = LadderRender.index_of(_price_list, _mkt_break_lines.origin, _price_comparer); - LadderRender.render_sizes(_bid_vis_tup.visual, _bid_vis_tup.glyph_context, origin_index, _price_list, _bids, _settings.row_height, security.ContractSpec, _settings.bid_column); - LadderRender.render_sizes(_ask_vis_tup.visual, _ask_vis_tup.glyph_context, origin_index, _price_list, _asks, _settings.row_height, security.ContractSpec, _settings.ask_column); + LadderRender.render_sizes(_bid_vis_tup.visual, _bid_vis_tup.glyph_context, origin_index, _price_list, _bids, _settings.row_height, security.ContractSpec, _settings.bid_column, _fmt_size); + LadderRender.render_sizes(_ask_vis_tup.visual, _ask_vis_tup.glyph_context, origin_index, _price_list, _asks, _settings.row_height, security.ContractSpec, _settings.ask_column, _fmt_size); } - void _refresh_depth() { - Log.assert_d(_price_list != null); - int origin_index = LadderRender.index_of(_price_list, _mkt_break_lines.origin, _price_comparer); - LadderRender.render_sizes(_bid_vis_tup.visual, _bid_vis_tup.glyph_context, origin_index, _price_list, _bids, _settings.row_height, security.ContractSpec, _settings.bid_column); - LadderRender.render_sizes(_ask_vis_tup.visual, _ask_vis_tup.glyph_context, origin_index, _price_list, _asks, _settings.row_height, security.ContractSpec, _settings.ask_column); + /// + /// Conditionally refreshes volume at price (if value has changed since last refresh). + /// + void _refresh_volume_at_price() { + if (!_settings.volume_column.general.show) + return; + var vapls = _volume_container.read_new_value(); + if (vapls != null) { + Log.assert_d(_price_list != null); + LadderRender.render_volumes(_volume_vis_tup.visual, _volume_vis_tup.glyph_context, _price_list, vapls, _settings.row_height, security.ContractSpec, _settings.volume_column); + } } static readonly TimeSpan _price_out_of_range_render_interval = TimeSpan.FromSeconds(5); @@ -518,8 +577,8 @@ namespace mbts.spark { SparkUtils.lazy_warn(!double.IsNaN(_mkt_break_lines.origin) && _mkt_break_lines.origin >= low && _mkt_break_lines.origin <= high, () => fmt_log_msg($"market break ({_mkt_break_lines.origin}) is unacceptable for the market range ({low} - {high})")); int prev_row_count = _row_count; - int n_ticks_to_low = TickUtils.normalized_tick_diff(security.ContractSpec, _mkt_break_lines.origin, low); - int n_ticks_to_high = TickUtils.normalized_tick_diff(security.ContractSpec, high, _mkt_break_lines.origin); + int n_ticks_to_low = security.ContractSpec.tickdiff(_mkt_break_lines.origin, low) ?? 0; + int n_ticks_to_high = security.ContractSpec.tickdiff(high, _mkt_break_lines.origin) ?? 0; int desired_row_count = Math.Max(n_ticks_to_low, n_ticks_to_high) * 2 + 1; _row_count = Math.Min(_max_row_count, desired_row_count); if (_row_count != prev_row_count) { @@ -595,18 +654,40 @@ namespace mbts.spark { #endregion void _settings_updated(LadderSettings s) { - bool init = _settings == null; - double prev_row_height = init ? double.NaN : _settings.row_height; + bool init; + double prev_row_height; + if (_settings == null) { + init = true; + prev_row_height = double.NaN; + } else { + init = false; + prev_row_height = _settings.row_height; + // This is a general issue with default-settings: + // "Not every property in the settings object should be shared across windows." + // Column widths are one such property (they are changed from the actual Ladder window, not the settings dialog). + // Below is a hack which prevents overwriting current column widths; until we come up with a better solution this will suffice. + // TODO: fix this by moving such properties from the settings proto into a separate proto (or come up with a better workaround/solution) + copy_col_width(v => v.orders_column); + copy_col_width(v => v.bid_column); + copy_col_width(v => v.price_column); + copy_col_width(v => v.ask_column); + copy_col_width(v => v.volume_column.general); + copy_col_width(v => v.last_column); + void copy_col_width(Func select_col) => + select_col(s).width = select_col(_settings).width; + } // _settings = s; _bid_vis_tup.glyph_context = glyph_context(s.bid_column); _ask_vis_tup.glyph_context = glyph_context(s.ask_column); _price_vis_tup.glyph_context = glyph_context(s.price_column); _last_vis_tup.glyph_context = glyph_context(s.last_column); + _volume_vis_tup.glyph_context = glyph_context(s.volume_column.general); GlyphContext glyph_context(LadderColumnSettings cs) => GlyphContext.create(cs.font_family, cs.font_weight); + _fmt_size = MDDisplayHelper.get_formatter_nps(_settings.size_display); _set_columns(); _apply_column_visual_offset(); if (prev_row_height != _settings.row_height) @@ -619,15 +700,22 @@ namespace mbts.spark { // Orders column orders_column_width = s.orders_column.width; _ordershost.update_settings(_price_list, _price_comparer, s.row_height, s.orders_column); - foreach (var col in columns) - if (col.settings.show && col.visual_tup != null) { - _visualHost.AddVisualChild(col.visual_tup.visual); - switch (col.column_type) { - case ColumnType.Bid: bid_column_width = col.settings.width; break; - case ColumnType.Price: price_column_width = col.settings.width; break; - case ColumnType.Ask: ask_column_width = col.settings.width; break; + foreach (var col in columns) { + if (col.visual_tup != null) { + if (col.settings.show) { + _visualHost.AddVisualChild(col.visual_tup.visual); + switch (col.column_type) { + case ColumnType.Bid: bid_column_width = col.settings.width; break; + case ColumnType.Price: price_column_width = col.settings.width; break; + case ColumnType.Ask: ask_column_width = col.settings.width; break; + case ColumnType.Volume: + VolumeColumnWidth = col.settings.width; + _volume_container.mark_dirty(); + break; + } } } + } _visualHost.AddVisualChild(_mid_mkt_break_vis); _visualHost.AddVisualChild(_ask_mkt_break_vis); _visualHost.AddVisualChild(_bid_mkt_break_vis); @@ -643,6 +731,7 @@ namespace mbts.spark { if (init) { _scroll_to_center(); } + visible_columns_changed?.Invoke(); } void _init_tactile_adorner() { @@ -652,7 +741,7 @@ namespace mbts.spark { } void _render_grid_background() { - LadderRender.render_grid(_gridvis, columns, _row_count, _settings.row_height, _settings.grid_line_color); + LadderRender.render_grid(_gridvis, columns.Where(c => c.settings.show), _row_count, _settings.row_height, _settings.grid_line_color); } class VisualGlyphTuple { @@ -759,7 +848,7 @@ namespace mbts.spark { } int _get_session_range_size() => - TickUtils.normalized_tick_diff(_owner.security.spec_or_default(), _session_high, _session_low); + _owner.security.spec_or_default().tickdiff(_session_high, _session_low) ?? 0; LadderSettings _settings => _owner._settings; diff --git a/src/client/spark/view/Spark/Ladder/LadderModel.cs b/src/client/spark/view/Spark/Ladder/LadderModel.cs index 78af88464..7576b8374 100644 --- a/src/client/spark/view/Spark/Ladder/LadderModel.cs +++ b/src/client/spark/view/Spark/Ladder/LadderModel.cs @@ -6,10 +6,12 @@ using System.Threading.Tasks; namespace mbts.spark { class LadderModel { readonly OrdersModel _order_gateway; + readonly MarketDataAuxiliaryModel _md_auxiliary_model; - public LadderModel(SecurityMD source, OrdersModel ordergw) { + public LadderModel(SecurityMD source, OrdersModel ordergw, MarketDataAuxiliaryModel md_auxiliary_model) { security_md = source; _order_gateway = ordergw; + _md_auxiliary_model = md_auxiliary_model; } public enum CancelType { All, Bids, Asks } @@ -18,6 +20,7 @@ namespace mbts.spark { public SecurityImmutable security => security_md.security; public MDConsumerHandle md => security_md.md; public OrdersModel order_gateway => _order_gateway; + public MarketDataAuxiliaryModel md_auxiliary_model => _md_auxiliary_model; public Task cancel_orders(CancelType ct) { switch (ct) { diff --git a/src/client/spark/view/Spark/Ladder/LadderSettings.cs b/src/client/spark/view/Spark/Ladder/LadderSettings.cs index e71eca84d..39a528650 100644 --- a/src/client/spark/view/Spark/Ladder/LadderSettings.cs +++ b/src/client/spark/view/Spark/Ladder/LadderSettings.cs @@ -5,6 +5,9 @@ using ProtoBuf; using System; using System.Linq; using System.Windows; +using bts.proto; +using mbts.ui.spark; +using System.Collections.Generic; namespace mbts.spark { // IMPORTANT: BE SURE TO ADD ANY NEW PROPERTIES TO THE EQUALS FUNCTION! @@ -74,6 +77,74 @@ namespace mbts.spark { public static bool operator !=(LadderColumnSettings x, LadderColumnSettings y) => !(x == y); } + [ProtoContract] + class HistogramBarAnimation : IEquatable { + [ProtoMember(1)] + public ColorShowProto bars { get; set; } + [ProtoMember(2)] + public bool show_numbers { get; set; } + + public bool active => bars.show || show_numbers; + + public bool Equals(HistogramBarAnimation other) => + other != null && + bars == other.bars && + show_numbers == other.show_numbers; + + public override bool Equals(object obj) => Equals(obj as HistogramBarAnimation); + public override int GetHashCode() => + EqualityComparer.aggregate_hash( + bars.GetHashCode(), + show_numbers.GetHashCode()); + } + + [ProtoContract] + class VolumeColumnSettings : IEquatable { + public static VolumeColumnSettings Default { + get { + var gen = LadderColumnSettings.Default; + gen.show = false; + gen.background = BTSColors.LadderSpecial.default_column_background; + return new VolumeColumnSettings { + general = gen, + buy = new HistogramBarAnimation { bars = new ColorShowProto((uint)BTSColors.MarketData.bid, false) }, + sell = new HistogramBarAnimation { bars = new ColorShowProto((uint)BTSColors.MarketData.ask, false) }, + passive = new HistogramBarAnimation { bars = new ColorShowProto((uint)BTSColors.LadderSpecial.passive_volume_bar, false) }, + total = new HistogramBarAnimation { bars = new ColorShowProto((uint)BTSColors.LadderSpecial.total_volume_bar), show_numbers = true } + }; + } + } + + [ProtoMember(1)] + public LadderColumnSettings general { get; set; } + [ProtoMember(2)] + public HistogramBarAnimation buy { get; set; } + [ProtoMember(3)] + public HistogramBarAnimation sell { get; set; } + [ProtoMember(4)] + public HistogramBarAnimation passive { get; set; } + [ProtoMember(5)] + public HistogramBarAnimation total{ get; set; } + + public bool Equals(VolumeColumnSettings other) => + other != null && + general.Equals(other.general) && + buy.safe_equals(other.buy) && + sell.safe_equals(other.sell) && + passive.safe_equals(other.passive) && + total.safe_equals(other.total); + + public override bool Equals(object obj) => Equals(obj as VolumeColumnSettings); + + public override int GetHashCode() => + EqualityComparer.aggregate_hash( + general.GetHashCode(), + buy.GetHashCode(), + sell.GetHashCode(), + passive.GetHashCode(), + total.GetHashCode()); + } + [ProtoContract] class LadderMarketBreakLine : IEquatable { [ProtoMember(1)] @@ -116,12 +187,12 @@ namespace mbts.spark { ask_column = LadderColumnSettings.Default, price_column = LadderColumnSettings.Default, last_column = LadderColumnSettings.Default; - orders_column.background = 0xBABABA; + orders_column.background = BTSColors.LadderSpecial.default_column_background; bid_column.foreground = 0xFFFFFF; bid_column.background = BTSColors.MarketData.bid; ask_column.foreground = 0xFFFFFF; ask_column.background = BTSColors.MarketData.ask; - price_column.background = 0xBABABA; + price_column.background = BTSColors.LadderSpecial.default_column_background; last_column.background = 0xFFFFFF; return new LadderSettings { row_height = 20, @@ -131,6 +202,7 @@ namespace mbts.spark { ask_column = ask_column, price_column = price_column, last_column = last_column, + volume_column = VolumeColumnSettings.Default, calculator_buttons = new[] { 1, 5, 10, 25, 50, 100 }, scroll_bar_visible = false, default_order_type = CustomOrderDefinition.CustomOneofCase.Limit, @@ -143,7 +215,8 @@ namespace mbts.spark { session_low_line = new LadderMarketBreakLine { color = BTSColors.MarketData.session_low, show = true }, session_high_line = new LadderMarketBreakLine { color = BTSColors.MarketData.session_high, show = true }, session_range_fill = new LadderMarketBreakLine { color = BTSColors.MarketData.session_range_fill, show = true }, - alternate_order_size = 0 + alternate_order_size = 0, + size_display = MarketSizeDisplay.get_default() }; } } @@ -200,6 +273,10 @@ namespace mbts.spark { public LadderMarketBreakLine session_high_line { get; set; } [ProtoMember(24)] public LadderMarketBreakLine session_range_fill { get; set; } + [ProtoMember(25)] + public VolumeColumnSettings volume_column { get; set; } + [ProtoMember(26)] + public MarketSizeDisplay size_display { get; set; } public bool Equals(LadderSettings other) => other != null && @@ -223,7 +300,9 @@ namespace mbts.spark { alternate_order_size == other.alternate_order_size && session_low_line.safe_equals(other.session_low_line) && session_high_line.safe_equals(other.session_high_line) && - session_range_fill.safe_equals(other.session_range_fill); + session_range_fill.safe_equals(other.session_range_fill) && + volume_column.safe_equals(other.volume_column) && + size_display.safe_equals(other.size_display); public override bool Equals(object obj) => Equals(obj as LadderSettings); @@ -249,6 +328,22 @@ namespace mbts.spark { alternate_order_size.GetHashCode(), EqualityComparer.safe_get_hash_code(session_low_line), EqualityComparer.safe_get_hash_code(session_high_line), - EqualityComparer.safe_get_hash_code(session_range_fill)); + EqualityComparer.safe_get_hash_code(session_range_fill), + EqualityComparer.safe_get_hash_code(volume_column), + EqualityComparer.safe_get_hash_code(size_display)); + } + + static class LadderSettingsExtensions { + public static IEnumerable all_columns(this LadderSettings settings) { + yield return settings.orders_column; + yield return settings.bid_column; + yield return settings.price_column; + yield return settings.ask_column; + yield return settings.volume_column.general; + yield return settings.last_column; + } + + public static IEnumerable visible_columns(this LadderSettings settings) => + settings.all_columns().Where(c => c.show); } } \ No newline at end of file diff --git a/src/client/spark/view/Spark/Ladder/LadderSettingsVM.cs b/src/client/spark/view/Spark/Ladder/LadderSettingsVM.cs index 982e843e1..4047a6ca4 100644 --- a/src/client/spark/view/Spark/Ladder/LadderSettingsVM.cs +++ b/src/client/spark/view/Spark/Ladder/LadderSettingsVM.cs @@ -3,6 +3,7 @@ using mbts.collections; using mbts.ui; using mbts.ui.controls.color_picker; using mbts.ui.shell; +using mbts.ui.spark; using System; using System.Collections.Generic; using System.Collections.ObjectModel; @@ -12,7 +13,7 @@ using System.Windows.Media; using TextUtils = mbts.ui.utility.text.TextUtils; namespace mbts.spark { - class LadderSettingsVM : ViewModelBase, IWindowSettingsVM { + class LadderSettingsVM : ViewModelBase, IWindowSettingsVM, ITemplateSelectorKey { readonly Property _badge_style; readonly Property _alternate_order_size; readonly DelegateProperty _row_height; @@ -22,7 +23,8 @@ namespace mbts.spark { _bid = new LadderColumnSettingsVM("Bid"), _price = new LadderColumnSettingsVM("Price"), _ask = new LadderColumnSettingsVM("Ask"), - _last = new LadderColumnSettingsVM("Last"); + _last = new LadderColumnSettingsVM("Last", true); + VolumeColumnVM _volume = new VolumeColumnVM(); LadderMarketBreakLineVM _bid_mkt_line = new LadderMarketBreakLineVM("Bid"), _ask_mkt_line = new LadderMarketBreakLineVM("Ask"), @@ -31,7 +33,8 @@ namespace mbts.spark { _session_high_mkt_line = new LadderMarketBreakLineVM("Session High", 2, 1, false), _session_range_fill = new LadderMarketBreakLineVM("Session Range", 0, 0, false); - public LadderSettingsVM() { + public LadderSettingsVM(bool is_synthetic) { + this.is_synthetic = is_synthetic; size_presets = new ObservableEditableCollection>(_create_size_preset_vm); size_presets.CollectionChanged += (s, e) => RaisePropertyChanged(nameof(size_presets_text)); _badge_style = backing(nameof(badge_style)); @@ -65,7 +68,10 @@ namespace mbts.spark { set => _alternate_order_size.Value = value; } + public MarketSizeDisplayVM size_display { get; } = new MarketSizeDisplayVM(); + public double row_height => _row_height.Value; + public bool is_synthetic { get; } public IEnumerable column_items { get { @@ -73,7 +79,10 @@ namespace mbts.spark { yield return _bid; yield return _price; yield return _ask; - yield return _last; + if (!is_synthetic) { + yield return _volume.general; + yield return _last; + } } } @@ -81,7 +90,9 @@ namespace mbts.spark { public LadderColumnSettingsVM bid_column => _bid; public LadderColumnSettingsVM price_column => _price; public LadderColumnSettingsVM ask_column => _ask; + public LadderColumnSettingsVM volume_column => _volume.general; public LadderColumnSettingsVM last_column => _last; + public VolumeColumnVM volume => _volume; public LadderMarketBreakLineVM session_range_fill => _session_range_fill; public ObservableEditableCollection> size_presets { get; } public string size_presets_text => string.Join(", ", size_presets); @@ -97,15 +108,19 @@ namespace mbts.spark { } } + public string key => GridOrLadderSettingsSelectorKeys.Ladder; + public void load(LadderSettings settings) { _current = settings; badge_style = settings.badge_style; alternate_order_size = settings.alternate_order_size; + size_display.load_from(settings.size_display); size_presets.SetItems(settings.calculator_buttons.empty_if_null().Select(s => _create_size_preset_vm((uint)s))); _orders.assign_from(_current.orders_column); _bid.assign_from(_current.bid_column); _ask.assign_from(_current.ask_column); _price.assign_from(_current.price_column); + _volume.assign_from(_current.volume_column); _last.assign_from(_current.last_column); _bid_mkt_line.assign_from(_current.bid_break_line); _ask_mkt_line.assign_from(_current.ask_break_line); @@ -119,11 +134,13 @@ namespace mbts.spark { var clone = SparkUtils.clone_proto(_current); clone.badge_style = badge_style; clone.alternate_order_size = alternate_order_size; + clone.size_display = size_display.create(); clone.calculator_buttons = size_presets.Select(vm => (int)vm.value).ToArray(); clone.orders_column = _orders.get_settings(); clone.bid_column = _bid.get_settings(); clone.price_column = _price.get_settings(); clone.ask_column = _ask.get_settings(); + clone.volume_column = _volume.get_settings(); clone.last_column = _last.get_settings(); clone.bid_break_line = _bid_mkt_line.get_settings(); clone.ask_break_line = _ask_mkt_line.get_settings(); @@ -142,18 +159,22 @@ namespace mbts.spark { return vm; } - public class LadderColumnSettingsVM : ViewModelBase { + public class LadderColumnSettingsVM : ViewModelBase, ITemplateSelectorKey { readonly Property _font_size; readonly Property _foreground, _background; readonly Property _typeface; readonly DelegateProperty _font_family; readonly DelegateProperty _font_weight; readonly DelegateProperty _font_display_name; + readonly Property _show; + readonly bool _can_toggle_visibility; - public LadderColumnSettingsVM(string name) { + public LadderColumnSettingsVM(string name, bool can_toggle_visibility = false) { this.name = name; + _can_toggle_visibility = can_toggle_visibility; foreground_palettes = new ObservableCollection(ColorPalette.all_presets().Select(p => new ColorPaletteVM(p))); background_palettes = new ObservableCollection(ColorPalette.all_presets().Select(p => new ColorPaletteVM(p))); + _show = backing(nameof(show), true); _typeface = backing(nameof(typeface), TextUtils.StandardTypeface); _font_family = backing(nameof(font_family), () => typeface.FontFamily, nameof(typeface)); _font_weight = backing(nameof(font_weight), () => typeface.Weight, nameof(typeface)); @@ -172,7 +193,7 @@ namespace mbts.spark { public string font_display_name => _font_display_name.Value; public double font_size { get => _font_size.Value; set => _font_size.Value = value; } public uint width { get; set; } - public bool show { get; set; } + public bool show { get => _show.Value; set => _show.Value = value; } public Color foreground { get => _foreground.Value; set => _foreground.Value = value; } public Color background { get => _background.Value; set => _background.Value = value; } public ObservableCollection foreground_palettes { get; } @@ -180,6 +201,7 @@ namespace mbts.spark { public event Action font_size_changed; public virtual string sample_text => "999-56789.01234"; + string ITemplateSelectorKey.key => _can_toggle_visibility ? ui.spark.LadderSettingsView.ColumnVisibilityToggleTemplateKey : null; public void assign_from(LadderColumnSettings settings) { typeface = new Typeface( @@ -253,5 +275,119 @@ namespace mbts.spark { show = show }; } + + public class VolumeColumnVM : ViewModelBase { + readonly DelegateProperty _any_visible; + public VolumeColumnVM() { + buy = new VolumeAtPriceAnimationVM("Buy", new VolumeAtPriceSampleData(this, 112, 62, 174)); + sell = new VolumeAtPriceAnimationVM("Sell", new VolumeAtPriceSampleData(this, 93, 78, 155)); + passive = new VolumeAtPriceAnimationVM("Passive", new VolumeAtPriceSampleData(this, 72, 127, 198)); + total = new VolumeAtPriceAnimationVM("Total", new VolumeAtPriceSampleData(this, 81, 100, 144)); + _any_visible = backing(nameof(any_visible), () => volume_components.Any(vc => vc.show_numbers || vc.bars.enabled)); + foreach (var vc in volume_components) { + vc.show_numbers_changed += _any_visible.refresh; + vc.bars.enabled_changed += _any_visible.refresh; + foreach (var _vc in volume_components) { + vc.show_numbers_changed += _vc.sample_data.refresh_numbers_text; + } + } + } + + public LadderColumnSettingsVM general { get; } = new LadderColumnSettingsVM("Volume", true); + public VolumeAtPriceAnimationVM buy { get; } + public VolumeAtPriceAnimationVM sell { get; } + public VolumeAtPriceAnimationVM passive { get; } + public VolumeAtPriceAnimationVM total { get; } + public bool any_visible => volume_components.Any(vc => vc.show_numbers || vc.bars.enabled); + + public IEnumerable volume_components { + get { + yield return buy; + yield return sell; + yield return passive; + yield return total; + } + } + public double max_total_volume => volume_components.Max(v => v.sample_data.total); + + public void assign_from(VolumeColumnSettings settings) { + general.assign_from(settings.general); + buy.assign_from(settings.buy); + sell.assign_from(settings.sell); + passive.assign_from(settings.passive); + total.assign_from(settings.total); + } + + public VolumeColumnSettings get_settings() => + new VolumeColumnSettings { + general = general.get_settings(), + buy = buy.get_settings(), + sell = sell.get_settings(), + passive = passive.get_settings(), + total = total.get_settings(), + }; + } + + internal class VolumeAtPriceSampleData : ViewModelBase { + readonly VolumeColumnVM _owner; + + public VolumeAtPriceSampleData(VolumeColumnVM owner, uint b, uint s, uint p) { + _owner = owner; + buy = b; + sell = s; + passive = p; + } + + public uint buy { get; } + public uint sell { get; } + public uint passive { get; } + public uint total => buy + sell + passive; + public string numbers_text => string.Join("/", _visible_number); + + IEnumerable _visible_number { + get { + if (_owner.buy.show_numbers) + yield return buy; + if (_owner.sell.show_numbers) + yield return sell; + if (_owner.passive.show_numbers) + yield return passive; + if (_owner.total.show_numbers) + yield return total; + } + } + + public void refresh_numbers_text() => RaisePropertyChanged(nameof(numbers_text)); + } + + public class VolumeAtPriceAnimationVM : ViewModelBase { + readonly Property _show_numbers; + + internal VolumeAtPriceAnimationVM(string name, VolumeAtPriceSampleData sample_data) { + this.name = name; + this.sample_data = sample_data; + _show_numbers = backing(nameof(show_numbers), false); + _show_numbers.Changed += (oldval, newval) => show_numbers_changed?.Invoke(); + } + + public event Action show_numbers_changed; + public string name { get; } + public EnabledValueVM bars { get; } = new EnabledValueVM(); + public ObservableCollection palettes { get; } = new ObservableCollection(ColorPalette.all_presets().Select(p => new ColorPaletteVM(p))); + public bool show_numbers { get => _show_numbers.Value; set => _show_numbers.set(value); } + public VolumeAtPriceSampleData sample_data { get; } + + public void assign_from(HistogramBarAnimation settings) { + bars.enabled = settings.bars.show; + bars.value = settings.bars.color; + show_numbers = settings.show_numbers; + } + + public HistogramBarAnimation get_settings() => + new HistogramBarAnimation { + bars = new bts.proto.ColorShowProto(bars.value, bars.enabled), + show_numbers = show_numbers + }; + } } } \ No newline at end of file diff --git a/src/client/spark/view/Spark/Ladder/LadderSettingsView.xaml b/src/client/spark/view/Spark/Ladder/LadderSettingsView.xaml index fa2b377ba..eeceec542 100644 --- a/src/client/spark/view/Spark/Ladder/LadderSettingsView.xaml +++ b/src/client/spark/view/Spark/Ladder/LadderSettingsView.xaml @@ -15,12 +15,15 @@ 4 8 + 24 8 24 4 6 8 24 + + @@ -30,6 +33,24 @@ + + @@ -67,6 +88,25 @@ + + + + + + + + + + + + + + + + + + + @@ -93,261 +133,465 @@ Alternate Order Qty - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/client/spark/view/Spark/Ladder/LadderSettingsView.xaml.cs b/src/client/spark/view/Spark/Ladder/LadderSettingsView.xaml.cs index c95184598..44108667c 100644 --- a/src/client/spark/view/Spark/Ladder/LadderSettingsView.xaml.cs +++ b/src/client/spark/view/Spark/Ladder/LadderSettingsView.xaml.cs @@ -1,4 +1,5 @@ using mbts.ui.converters; +using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Media; @@ -8,12 +9,26 @@ namespace mbts.ui.spark { /// Interaction logic for LadderSettingsView.xaml /// partial class LadderSettingsView : UserControl { - public const string OrdersColumnSampleTemplateKey = "OrdersColumnSampleTemplate"; + public const string + OrdersColumnSampleTemplateKey = nameof(OrdersColumnSampleTemplateKey), + ColumnVisibilityToggleTemplateKey = nameof(ColumnVisibilityToggleTemplateKey); public LadderSettingsView() => InitializeComponent(); public static IMultiValueConverter MktBreakLineBrushConverter = ValueConverter.create((color, show) => show ? new SolidColorBrush(color) : Brushes.Transparent); + + public static IMultiValueConverter VolumeBarWidthConverter => + ValueConverter.create((value, max, container_width) => container_width * (double)value / (double)max); + + public static readonly DependencyProperty VolumeComponentValueProperty = + DependencyProperty.RegisterAttached("VolumeComponentValue", + typeof(uint), + typeof(LadderSettingsView), + new PropertyMetadata(0u)); + + public static uint GetVolumeComponentValue(DependencyObject d) => (uint)d.GetValue(VolumeComponentValueProperty); + public static void SetVolumeComponentValue(DependencyObject d, uint value) => d.SetValue(VolumeComponentValueProperty, value); } } \ No newline at end of file diff --git a/src/client/spark/view/Spark/Ladder/LadderVM.cs b/src/client/spark/view/Spark/Ladder/LadderVM.cs index f4a6969c9..905b5c072 100644 --- a/src/client/spark/view/Spark/Ladder/LadderVM.cs +++ b/src/client/spark/view/Spark/Ladder/LadderVM.cs @@ -19,7 +19,7 @@ using System.Windows.Input; using System.Windows.Media; namespace mbts.spark { - class LadderVM : DisposableViewModelBase, IKeyListener { + class LadderVM : DisposableViewModelBase, IWillLetYouKnowWhenIDispose, IKeyListener { readonly IBus _bus; readonly LadderModel _model; readonly Property _high, _low, _change, _open, _settlement, _position; @@ -36,7 +36,8 @@ namespace mbts.spark { _model = model; _equality = TickUtils.tick_equality_comparer(_model.security.ContractSpec); _slot_price_vs_orders = new Dictionary(_equality); - _settings_manager = settings_manager; + var non_shared_wsm = settings_manager.as_non_shared(); + _settings_manager = non_shared_wsm; order_properties_vm = new OrderPropertiesEditorVM(_model.order_gateway, Observable.Return(_model.security_md), _settings_manager.subscribe_current().Select(s => new InlineOrderProperties(_model.order_gateway, s))); _high = backing(nameof(high), double.NaN); _low = backing(nameof(low), double.NaN); @@ -64,8 +65,14 @@ namespace mbts.spark { show_book_command = new DelegateCommand(_show_book); show_ticket_command = new DelegateCommand(_show_ticket); settings_command = new DialogCommand(() => { - var vm = new WindowSettingsDialogVM(settings_manager, s => s.ladder, UserSettings.FileExtensions.LADDER); - var dialog = new ui.spark.LadderSettingsDialog { + var vm = new WindowSettingsDialogVM( + settings_manager, s => s.ladder, UserSettings.FileExtensions.LADDER, + s => { + var svm = new LadderSettingsVM(security.ContractCase == Security.ContractOneofCase.Synthetic); + svm.load(s); + return svm; + }); + var dialog = new ui.spark.SimpleGridSettingsDialog { DataContext = vm, Title = $"Ladder Settings - '{security}'" }; @@ -77,6 +84,7 @@ namespace mbts.spark { _dispose = Disposable.Create( settings_command, + non_shared_wsm, order_properties_vm, order_properties_vm.observe_account(true).Select(a => a?.name ?? string.Empty).Subscribe(account_positions), account_positions, @@ -167,7 +175,8 @@ namespace mbts.spark { orders.sync_with(_slot_price_vs_orders.Values); }), - WorkLoop.Start($"Ladder '{security}'", _tick) + WorkLoop.Start($"Ladder '{security}'", _tick), + Disposable.Create(() => _on_disposed_impl?.Invoke(true)) ); } @@ -198,6 +207,11 @@ namespace mbts.spark { } } + event Action _on_disposed_impl; + event Action IWillLetYouKnowWhenIDispose.OnDisposed { + add => _on_disposed_impl += value; + remove => _on_disposed_impl -= value; + } public SecurityImmutable security => _model.security; public double high => _high.Value; public double low => _low.Value; diff --git a/src/client/spark/view/Spark/Ladder/LadderView.xaml b/src/client/spark/view/Spark/Ladder/LadderView.xaml index 53164d14e..68c43b28f 100644 --- a/src/client/spark/view/Spark/Ladder/LadderView.xaml +++ b/src/client/spark/view/Spark/Ladder/LadderView.xaml @@ -422,33 +422,7 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - + _column_headers_presenter.Columns.SetItems( + _ladder.visible_columns.Select(t => ColumnFactory.create(_ladder, t))); this.vm = vm; DataContext = vm; DataContextChanged += datacontext_changed; @@ -28,5 +33,49 @@ namespace mbts.spark { e.Handled = true; e.UseDefaultCursors = true; } + + static class ColumnFactory { + public static GridViewColumn create(LadderGridHost host, LadderGridHost.ColumnType type) { + string header = null, width_prop = null; + switch (type) { + case LadderGridHost.ColumnType.Order: + header = "Orders"; + width_prop = nameof(LadderGridHost.orders_column_width); + break; + case LadderGridHost.ColumnType.Bid: + header = "Bid"; + width_prop = nameof(LadderGridHost.bid_column_width); + break; + case LadderGridHost.ColumnType.Price: + header = "Price"; + width_prop = nameof(LadderGridHost.price_column_width); + break; + case LadderGridHost.ColumnType.Ask: + header = "Ask"; + width_prop = nameof(LadderGridHost.ask_column_width); + break; + case LadderGridHost.ColumnType.Volume: + header = "Volume"; + width_prop = nameof(LadderGridHost.VolumeColumnWidth); + break; + case LadderGridHost.ColumnType.Last: + header = "Last"; + width_prop = nameof(LadderGridHost.LastColumnWidth); + break; + default: throw ExceptionBuilder.Argument.unexpected_case(type, nameof(type)); + } + return _create(host, header, width_prop); + } + + static GridViewColumn _create(LadderGridHost host, string header, string width_prop) { + var gvc = new GridViewColumn { Header = header }; + BindingOperations.SetBinding(gvc, GridViewColumn.WidthProperty, new Binding(width_prop) { + Source = host, + UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged, + Mode = BindingMode.TwoWay + }); + return gvc; + } + } } } \ No newline at end of file diff --git a/src/client/spark/view/Spark/Ladder/LaderRender.cs b/src/client/spark/view/Spark/Ladder/LaderRender.cs index 337c3fffd..4a8ddf334 100644 --- a/src/client/spark/view/Spark/Ladder/LaderRender.cs +++ b/src/client/spark/view/Spark/Ladder/LaderRender.cs @@ -1,8 +1,10 @@ using bts; using bts.api; +using bts.api.md.auxiliary; using mbts.co2; using mbts.ui; using mbts.ui.controls; +using mbts.ui.spark; using mbts.ui.utility.text; using System; using System.Collections.Generic; @@ -42,13 +44,8 @@ namespace mbts.spark { // IMPORTANT: this method ONLY WORKS if Badges use the BadgeRender.margin/border_thickness/padding values. // TODO: elminate the dependency on BadgeRender.margin/border_thickness/padding by integrating them into settings. public static double measure_row_height(LadderSettings settings) => - Math.Max(new[] { - settings.bid_column.font_size, - settings.price_column.font_size, - settings.ask_column.font_size, - settings.last_column.font_size - }.Max() + row_padding.Top + row_padding.Bottom, - BadgeRender.measure_height( + Math.Max(settings.visible_columns().Max(c => c.font_size, 0) + row_padding.Top + row_padding.Bottom, + BadgeRender.measure_height( settings.badge_style, Badge.DefaultBorderThickness, Badge.DefaultPadding, settings.orders_column.font_size ) + Badge.DefaultMargin.Top + Badge.DefaultMargin.Bottom); @@ -128,7 +125,8 @@ namespace mbts.spark { NativePriceSize[] rungs, double row_height, ContractSpecificationImmutable spec, - LadderColumnSettings settings + LadderColumnSettings settings, + Func fmt ) { //var background = new SolidColorBrush(settings.background.rgb_color()); using (var dc = dv.RenderOpen()) @@ -139,22 +137,114 @@ namespace mbts.spark { var foreground = settings.foreground.rgb_brush(); int curr_index = origin_index; double curr_price = prices[curr_index]; - foreach (var nps in rungs) { - if (nps.is_nan) break; - double price = nps.price; - uint size = nps.size; + for (int i = 0; i < rungs.Length; i++) { + if (rungs[i].is_nan) break; + double price = rungs[i].price; // prices are in descending order, so get diff from curr_price - price - int tick_diff = TickUtils.normalized_tick_diff(spec, curr_price, price); + int tick_diff = spec.tickdiff_unchecked(curr_price, price); curr_index += tick_diff; curr_price = price; if (curr_index >= 0 && curr_index < prices.Length) { - var gr = format_text(size.ToString(), curr_index, row_height, settings, glyph_context); + var gr = format_text(fmt(rungs[i]), curr_index, row_height, settings, glyph_context); dc.DrawGlyphRun(foreground, gr); } else break; } } } + public static void render_volumes( + DrawingVisual dv, + GlyphContext glyph_context, + double[] prices, + IEnumerable data, + double row_height, + ContractSpecificationImmutable spec, + VolumeColumnSettings settings + ) { + if (!data.Any()) { + _clear_visual(dv); + return; + } + /// create a list of 'render components' (buy/sell/passive). This does not include 'total volume,' which is treated a little differently + /// ( for details) + var components = new List<(Func get_volue, Brush bars, bool show_numbers)>(); + if (settings.buy.active) + components.Add((v => v.BuyVolume, settings.buy.bars.result?.to_brush(), settings.buy.show_numbers)); + if (settings.sell.active) + components.Add((v => v.SellVolume, settings.sell.bars.result?.to_brush(), settings.sell.show_numbers)); + if (settings.passive.active) + components.Add((v => v.PassiveVolume, settings.passive.bars.result?.to_brush(), settings.passive.show_numbers)); + /// + Brush total_brush = null; + if (settings.total.bars.result.HasValue) { + total_brush = new SolidColorBrush(settings.total.bars.result.Value); + } else if (components.Count == 0) { + _clear_visual(dv); + return; + } + double max = data.Max(v => v.Volume); + Brush text_brush = settings.general.foreground.rgb_brush(); + _render_cells(dv, row_height, data.Select(d => (d.Price, d)), settings.general, prices, spec, render_cell); + /// local methods + void render_cell(DrawingContext dc, int r, VolumeAtPriceLevelImmutable vap) { + double y = row_height * r; + // render the total first; each component (buy/sell/passive) will be rendered on top of the total + if (total_brush != null) { + render_bar(dc, 0, y, vap.Volume, total_brush, out double _); + } + List numbers_text = new List(); + double x_bar = 0; + foreach ((var get_volume, Brush bars, bool show_numbers) in components) { + uint v = get_volume(vap); + render_bar(dc, x_bar, y, v, bars, out double width); + x_bar += width; + if (show_numbers) + numbers_text.Add(v.ToString()); + } + if (settings.total.show_numbers) { + numbers_text.Add(vap.Volume.ToString()); + } + if (numbers_text.Count > 0) { + string str = string.Join("/", numbers_text); + GlyphRun gr = glyph_context.get_glyph_run(str, settings.general.font_size, settings.general.width, row_height, HorizontalAlignment.Center, 0, VerticalAlignment.Center, y, true); + dc.DrawGlyphRun(text_brush, gr); + } + } + + void render_bar(DrawingContext dc, double x, double y, double v, Brush brush, out double width) { + if (v == 0) { + width = 0; + return; + } + double pct = v / max; + width = pct * settings.general.width; + dc.DrawRectangle(brush, null, new Rect(x, y, width, row_height - 1)); + } + } + + public delegate void RenderCell(DrawingContext dc, int row, T data_item); + + static void _render_cells(DrawingVisual dv, double row_height, IEnumerable<(double price, T value)> data, LadderColumnSettings settings, double[] prices, ContractSpecificationImmutable spec, RenderCell render_cell) { + using (var dc = dv.RenderOpen()) + using (_draw_with_clip(dc, settings, row_height, prices.Length)) { + if (data.is_null_or_empty()) + return; + int curr_index = 0; + double curr_price = prices[curr_index]; + foreach (var (price, value) in data) { + if (double.IsNaN(price)) + continue; + // prices are in descending order, so get diff from curr_price - price + int tick_diff = spec.tickdiff(curr_price, price) ?? 0; + curr_index += tick_diff; + curr_price = price; + if (curr_index >= 0 && curr_index < prices.Length) { + render_cell(dc, curr_index, value); + } else break; + } + } + } + internal static void render_last(DrawingVisual dv, GlyphContext glyph_context, NativePriceSize last, int index, double row_height, LadderColumnSettings settings) { using (var dc = dv.RenderOpen()) { var gr = format_text(last.size.ToString(), index, row_height, settings, glyph_context); @@ -182,5 +272,9 @@ namespace mbts.spark { dc.PushClip(new RectangleGeometry(new Rect(0, 0, settings.width, row_height * row_count))); return Disposable.Create(dc.Pop); } + + static void _clear_visual(DrawingVisual dv) { + using (dv.RenderOpen()) { } + } } } \ No newline at end of file diff --git a/src/client/spark/view/Spark/PAndL/PAndLVM.cs b/src/client/spark/view/Spark/PAndL/PAndLVM.cs index 6a3305db3..a5ca17a3a 100644 --- a/src/client/spark/view/Spark/PAndL/PAndLVM.cs +++ b/src/client/spark/view/Spark/PAndL/PAndLVM.cs @@ -11,14 +11,19 @@ using System.Linq; using System.Reactive.Linq; using System.Windows.Data; using static bts.shell.PAndLWindowFactory; -using static bts.api.orderman.Fill.Types; +using static mbts.ui.controls.DIYGrid; +using static mbts.spark.PAndLView; +using System.Windows.Media; +using System.Windows; +using mbts.persist; +using mbts.ui.settings; namespace mbts.spark { class PAndLVM : DisposableViewModelBase { - - public PAndLVM(IBus bus, PAndLModel model, PAndLStartupData startup_data) { + public PAndLVM(IBus bus, PAndLModel model, PAndLStartupData startup_data, IWindowSettingsManager settings_manager) { _bus = bus; _model = model; + _settings_manager = settings_manager; _selected_pnl_calc_type = backing(nameof(selected_pnl_calc_type)); // get initial value from settings // else default to midpoint @@ -60,22 +65,51 @@ namespace mbts.spark { pnl_price_filter.load_state(fs.pnl_price); security_type_filter.load_state(fs.security_types); } - // restore sort state or supply default value - if (startup_data?.sort_criteria?.Any() == true) { - pnl_view.SetSortingCriteria(startup_data.sort_criteria); - } else { - pnl_view.SetSortingCriteria(new List() { - new SortingCriteria(nameof(PnLSummaryRow.account), true) - , new SortingCriteria(nameof(PnLSummaryRow.exchange_string), true) - , new SortingCriteria(nameof(PnLSummaryRow.symbol), true) - , new SortingCriteria(nameof(PnLSummaryRow.security_text), true) - }); - } pnl_view.CollectionChanged += Pnl_view_CollectionChanged; + add_col(Columns.Account, DefaultColumnHeaders.Account, r => r.account, nameof(PnLSummaryRow.account), DefaultColumnWidths.Account, account_filter, DataGridFilterKnownTemplates.Regex); + add_col(Columns.Exchange, DefaultColumnHeaders.Exchange, r => r.exchange_string, nameof(PnLSummaryRow.exchange), DefaultColumnWidths.Exchange, exchange_string_filter, DataGridFilterKnownTemplates.Regex); + add_col(Columns.Symbol, DefaultColumnHeaders.Symbol, r => r.symbol, nameof(PnLSummaryRow.symbol), DefaultColumnWidths.Symbol, symbol_filter, DataGridFilterKnownTemplates.Regex); + add_col(Columns.Security, DefaultColumnHeaders.Security, r => r.security_text, nameof(PnLSummaryRow.security), DefaultColumnWidths.Security, security_filter, DataGridFilterKnownTemplates.Regex); + add_col(Columns.SecurityType, DefaultColumnHeaders.SecurityType, r => r.security_type.ToString(), nameof(PnLSummaryRow.security_type), DefaultColumnWidths.SecurityType, security_type_filter, DataGridFilterKnownTemplates.DiscreteValues); + add_col(Columns.StartNetPos, DefaultColumnHeaders.StartNetPos, r => r.start_net_position.ToString(), nameof(PnLSummaryRow.start_net_position), DefaultColumnWidths.Start_Net_Pos, start_position_filter, DataGridFilterKnownTemplates.DoubleMinMax, HorizontalAlignment.Right); + add_col(Columns.StartPNL, DefaultColumnHeaders.StartPnL, r => r.start_pnl_text, nameof(PnLSummaryRow.start_pnl), DefaultColumnWidths.PnL_Generic, start_pnl_filter, DataGridFilterKnownTemplates.DoubleMinMax, HorizontalAlignment.Right, r => r.start_pnl_background); + add_col(Columns.DayLong, DefaultColumnHeaders.DayLong, r => r.day_long_quantity.ToString(), nameof(PnLSummaryRow.day_long_quantity), DefaultColumnWidths.PnL_Generic, day_long_quantity_filter, DataGridFilterKnownTemplates.UIntMinMax, HorizontalAlignment.Right); + add_col(Columns.DayShort, DefaultColumnHeaders.DayShort, r => r.day_short_quantity.ToString(), nameof(PnLSummaryRow.day_short_quantity), DefaultColumnWidths.PnL_Generic, day_short_quantity_filter, DataGridFilterKnownTemplates.UIntMinMax, HorizontalAlignment.Right); + add_col(Columns.DayPNL, DefaultColumnHeaders.DayPnL, r => r.day_pnl_text, nameof(PnLSummaryRow.day_pnl), 90, day_pnl_filter, DataGridFilterKnownTemplates.DoubleMinMax, HorizontalAlignment.Right, r => r.day_pnl_background); + add_col(Columns.TotalNetPos, DefaultColumnHeaders.TotalNetPos, r => r.total_net_position.ToString(), nameof(PnLSummaryRow.total_net_position), DefaultColumnWidths.Total_Net_Pos, total_net_position_filter, DataGridFilterKnownTemplates.DoubleMinMax, HorizontalAlignment.Right); + add_col(Columns.TotalPNL, DefaultColumnHeaders.TotalPnL, r => r.total_pnl_text, nameof(PnLSummaryRow.total_pnl), DefaultColumnWidths.PnL_Generic, total_pnl_filter, DataGridFilterKnownTemplates.DoubleMinMax, HorizontalAlignment.Right, r => r.total_pnl_background); + add_col(Columns.PNLPrice, DefaultColumnHeaders.PnLPrice, r => r.pnl_price_text, nameof(PnLSummaryRow.pnl_price), DefaultColumnWidths.PnL_Generic, pnl_price_filter, DataGridFilterKnownTemplates.DoubleMinMax, HorizontalAlignment.Right); + void add_col(Columns key, string header, Func get_text, string sort_prop, double width, IDataGridColumnFilter filter, string filter_template, HorizontalAlignment h_align = HorizontalAlignment.Left, Func get_background = null) { + var col = get_background == null ? + new RenderTextColumn(key, get_text, filter, filter_template) : + new PnLDelegateBackgroundColumn(key, get_text, get_background, filter, filter_template); + col.Header = header; + col.Width = width; + col.sort_member_path = sort_prop; + col.horizontal_alignment = h_align; + col.id = col_id(key); + columns.Add(col); + } + + // restore sort state or supply default value + pnl_view.set_sort_descriptions(startup_data.sorting_criterion, columns); + + var col_settings_flags = new Dictionary { + { Columns.StartPNL.ToString(), GridColumnSettingsFeatureFlags.ImmutableFontColor }, + { Columns.DayPNL.ToString(), GridColumnSettingsFeatureFlags.ImmutableFontColor }, + { Columns.TotalPNL.ToString(), GridColumnSettingsFeatureFlags.ImmutableFontColor }, + }; + + grid_settings_manager = new SimpleGridSettingsManager(columns, col_settings_flags, _pnl_data); + // this will initialize the immutable properties that correspond to the col_settings_flags above (e.g. Bid/Ask font_color_override, etc.) + grid_settings_manager.load_from(settings_manager.current_settings()); + + // setup the main Disposable and // hook up grid filters _dispose = Disposable.Create(pnl, + settings_manager.subscribe_current().OnDispatcher().Subscribe(grid_settings_manager), DataGridFilterHelper.hook_filters(pnl_view, account_filter, exchange_string_filter, @@ -96,6 +130,15 @@ namespace mbts.spark { ); } + public ObservableCollection columns { get; } = new ObservableCollection(); + public SimpleGridSettingsManager grid_settings_manager { get; } + public GridSettings get_grid_save_state() { + if (_settings_manager.instance_settings == null) + return null; + else + return grid_settings_manager.create(); + } + private void Pnl_view_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { _update_totals(); } @@ -221,12 +264,13 @@ namespace mbts.spark { #endregion - public List get_sorting_states() => pnl_view.GetSortingCriteria(); + public List get_sorting_states() => pnl_view.get_sorting_criteria(columns); #endregion readonly IBus _bus; readonly PAndLModel _model; + IWindowSettingsManager _settings_manager; static readonly TimeSpan _UPDATE_PERIOD = TimeSpan.FromSeconds(1); static readonly TimeSpan _POSITION_UPDATE_PERIOD = TimeSpan.FromMilliseconds(333); readonly Property _selected_pnl_calc_type; @@ -235,6 +279,14 @@ namespace mbts.spark { DateTime _last_pnl_update_time = DateTime.MinValue; ObservableCollection _pnl_data = new ObservableCollection(); - } + class PnLDelegateBackgroundColumn : RenderTextColumn { + readonly Func _get_brush; -} + public PnLDelegateBackgroundColumn(Columns key, Func get_text, Func get_brush, IDataGridColumnFilter filter, string filter_template) + : base(key, get_text, filter, filter_template) => _get_brush = get_brush; + + public override void draw(PnLSummaryRow row, DrawingContext dc, ColumnGeometry position, double row_height) => + CellRender.draw_text(dc, glyph_context, font_size, _get_text(row), position, row_height, font_brush, _get_brush.Invoke(row), horizontal_alignment, VerticalAlignment.Center, DefaultCellPadding); + } + } +} \ No newline at end of file diff --git a/src/client/spark/view/Spark/PAndL/PAndLView.xaml b/src/client/spark/view/Spark/PAndL/PAndLView.xaml index 3f75eaa5a..82f340436 100644 --- a/src/client/spark/view/Spark/PAndL/PAndLView.xaml +++ b/src/client/spark/view/Spark/PAndL/PAndLView.xaml @@ -1,240 +1,122 @@ - - - - - - - - - - - - + + + + + + + + + + + + + - - - - + + + + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - + + + \ No newline at end of file diff --git a/src/client/spark/view/Spark/PAndL/PAndLView.xaml.cs b/src/client/spark/view/Spark/PAndL/PAndLView.xaml.cs index 631b73e7d..19871786a 100644 --- a/src/client/spark/view/Spark/PAndL/PAndLView.xaml.cs +++ b/src/client/spark/view/Spark/PAndL/PAndLView.xaml.cs @@ -1,11 +1,15 @@ -using mbts.spark.filters; +using mbts.ui.controls; using System.Windows.Controls; namespace mbts.spark { internal partial class PAndLView : UserControl { + public enum Columns { Account, Exchange, Symbol, Security, SecurityType, StartNetPos, StartPNL, DayLong, DayShort, DayPNL, TotalNetPos, TotalPNL, PNLPrice } + public PAndLView(PAndLVM vm) { InitializeComponent(); DataContext = vm; } } + + class PnLRowView : DIYGrid.HighPerformanceRow { } } \ No newline at end of file diff --git a/src/client/spark/view/Spark/PAndL/PAndLWindow.cs b/src/client/spark/view/Spark/PAndL/PAndLWindow.cs index 0fa58bd01..ec1b37d34 100644 --- a/src/client/spark/view/Spark/PAndL/PAndLWindow.cs +++ b/src/client/spark/view/Spark/PAndL/PAndLWindow.cs @@ -1,7 +1,11 @@ using bts.api; using bts.shell; using bts.spark; +using mbts.persist; using mbts.ui; +using mbts.ui.settings; +using mbts.ui.spark; +using mbts.ui.utility; using System; using System.Collections.Generic; using System.Linq; @@ -12,17 +16,21 @@ using System.Windows.Media; namespace mbts.spark { internal class PAndLWindow : SparkWindow, ISharedWindow { - const string _window_type = "PAndL", PNL_THREAD_ID = "PnL"; + readonly IWindowSettingsManager _settings_manager; PAndLWindowFactory.PAndLStartupData _startup_data; PAndLModel _model; PAndLView _view; PAndLVM _vm; PAndLWindowFactory _factory; - public PAndLWindow(PAndLModel model, PAndLWindowFactory factory, IBus bus, PAndLWindowFactory.PAndLStartupData startup_data, ImageSource Icon ) : base("P&L", bus, factory, SparkCommands.ShowPAndL.icon) { + IReadOnlyList _commands; + + public PAndLWindow(PAndLModel model, PAndLWindowFactory factory, IBus bus, PAndLWindowFactory.PAndLStartupData startup_data, ImageSource Icon, WindowSettingsManager settings_manager) : base("P&L", bus, factory, SparkCommands.ShowPAndL.icon) { _model = model; _factory = factory; _startup_data = startup_data; + _settings_manager = settings_manager; + Icon.Freeze(); } public override string window_type => _window_type; @@ -30,20 +38,48 @@ namespace mbts.spark { new PAndLWindowFactory.PAndLStartupData( _vm.get_sorting_states(), _vm.get_filters_states(), - _vm.selected_pnl_calc_type + _vm.selected_pnl_calc_type, + _vm.get_grid_save_state() ); public override UIElement Content => _view; - public override double MinWidth => 894; + public override IEnumerable Commands => _commands; + + public override double MinWidth => + PAndLWindowFactory.PAndLStartupData.DefaultGridSettings.columns.Sum(gcs => gcs.width) + + SPARK_MENU_COLLAPSED_WIDTH + + LAUNCHER_BUTTON_BORDER_WIDTH + + EXTRA_FOR_NO_HORIZONTAL_SCROLLBAR_WIDTH; + public string thread_id => PNL_THREAD_ID; public void thread_init() { - _vm = new PAndLVM(_bus, _model, _startup_data); + _vm = new PAndLVM(_bus, _model, _startup_data, _settings_manager); _view = new PAndLView(_vm); _view.DataContext = _vm; + _commands = new List { + SparkCommands.window_settings_command(new DialogCommand(() => { + var vm = new WindowSettingsDialogVM( + _settings_manager, + us => us.pnl_grid, + UserSettings.FileExtensions.GRID, + s => _vm.grid_settings_manager.create_vm( + // we need to get the most up to date GridSettings when launching b/c users may + // have changed some settings on the grid directly, like column widths. + s.with(_vm.columns.Select( c => c.create())), + this.observe_title() + ) + ); + // TODO: bind to main window's Title + var dialog = new SimpleGridSettingsDialog { Title = "PnL Grid Settings", DataContext = vm }; + vm.RequestClose += (s, e) => WindowUtilities.force_close(dialog); + return dialog; + })) + }; } public override void Dispose() { Disposable.dispose(ref _vm); + Commands.OfType().DisposeAll(); } diff --git a/src/client/spark/view/Spark/PAndL/PnLSummaryRow.cs b/src/client/spark/view/Spark/PAndL/PnLSummaryRow.cs index b2de339a5..0f509ece0 100644 --- a/src/client/spark/view/Spark/PAndL/PnLSummaryRow.cs +++ b/src/client/spark/view/Spark/PAndL/PnLSummaryRow.cs @@ -1,36 +1,46 @@ using bts.api; using bts.api.orderman; using mbts.co2; +using mbts.ui; +using mbts.ui.controls; +using mbts.ui.converters; using mbts.ui.shell; using System; using System.Collections.Generic; +using System.Collections.ObjectModel; using System.Linq; -using System.Text; -using System.Threading.Tasks; +using System.Windows.Media; namespace mbts.spark { - - - internal class PnLSummaryRow : ViewModelBase, IEquatable { + class PnLSummaryRow : ViewModelBase, IEquatable, DIYGrid.IRowVM { + static readonly SignedDataStore _brushes; + readonly PnLDoubleBrushCell _start_pnl, _day_pnl, _total_pnl; + readonly PnLPriceCell _pnl_price, _weighted_average_buy, _weighted_average_sell; (string account, Venue? exchange, string symbol, SecurityImmutable? security, SecurityMD? sec_md, SecurityType? sec_type) key; - int _start_net_position, _total_net_position; - uint _day_long_quantity, _day_short_quantity; - double _total_pnl, _sod_pnl, _start_pnl, _pnl_price, _weighted_average_buy, _weighted_average_sell; IEnumerable _fills; double _driver; static readonly Dictionary cpp_dict = config.Configuration.get().cpp_entry; + static PnLSummaryRow() { + _brushes = new SignedDataStore(mk_brush(Colors.Green), mk_brush(Colors.Black), mk_brush(Colors.Red), mk_brush(Colors.Black)); + Brush mk_brush(Color c) => + new SolidColorBrush(c) { Opacity = 0.33 }.frozen(); + } public PnLSummaryRow(pnl_calc_type calc_type, (string, Venue?, string, SecurityImmutable?, SecurityMD?, SecurityType?) key, uint day_buy_quant, uint day_sell_quant, int net_pos, int sod_pos, double wavg_buy, double wavg_sell, IEnumerable fills) { this.key = key; + _start_pnl = new PnLDoubleBrushCell(this, nameof(start_pnl)); + _day_pnl = new PnLDoubleBrushCell(this, nameof(day_pnl)); + _total_pnl = new PnLDoubleBrushCell(this, nameof(total_pnl)); day_long_quantity = day_buy_quant; day_short_quantity = day_sell_quant; total_net_position = net_pos; start_net_position = sod_pos; - weighted_average_buy = wavg_buy; - weighted_average_sell = wavg_sell; + _pnl_price = new PnLPriceCell(this, nameof(pnl_price)); + _weighted_average_buy = new PnLPriceCell(this, nameof(weighted_average_buy), wavg_buy); + _weighted_average_sell = new PnLPriceCell(this, nameof(weighted_average_sell), wavg_sell); _fills = fills; // set the initial pnl and pnl_price when there has been a @@ -66,9 +76,9 @@ namespace mbts.spark { } } - public void update_pnl(pnl_calc_type pnl_request) { if (key.sec_md.HasValue) { + bool dirty = false; double cpp = _get_cpp_for_security(key.security.Value); if (pnl_request.HasFlag(pnl_calc_type.bid_or_ask)) { ulong bbo_ref = 0; @@ -86,10 +96,10 @@ namespace mbts.spark { start_pnl_total += _tick_pnl; } } - total_pnl = summary_pnl; - day_pnl = day_pnl_total; - start_pnl = start_pnl_total; - pnl_price = Double.NaN; + _total_pnl.on_next_distinct(summary_pnl, ref dirty); + _day_pnl.on_next_distinct(day_pnl_total, ref dirty); + _start_pnl.on_next_distinct(start_pnl_total, ref dirty); + _pnl_price.on_next_distinct(double.NaN, ref dirty); } else { if (PnLCalculator.get_driver(key.sec_md.Value, pnl_request, ref _driver)) { double summary_pnl = 0.0; @@ -104,17 +114,17 @@ namespace mbts.spark { start_pnl_total += _tick_pnl; } } - total_pnl = summary_pnl; - day_pnl = day_pnl_total; - start_pnl = start_pnl_total; - pnl_price = _driver; + _total_pnl.on_next_distinct(summary_pnl, ref dirty); + _day_pnl.on_next_distinct(day_pnl_total, ref dirty); + _start_pnl.on_next_distinct(start_pnl_total, ref dirty); + _pnl_price.on_next_distinct(_driver, ref dirty); } } + if (dirty) + raise_invalidated(); } } - void ignore() {/*These setters are just for WPF databinding :(*/ throw new NotImplementedException(); } - // probably need to come up with a GetHashCode() while we're in here public bool Equals(PnLSummaryRow other) { return other != null && @@ -140,145 +150,64 @@ namespace mbts.spark { return $"account: {account} exchange: {exchange_string} symbol: {symbol} security: {security_text} buy_quantity: {day_long_quantity} sell_quantity: {day_short_quantity} pnl: {total_pnl}"; } - public string account { - get => key.account; - set => ignore(); - } - - public Venue exchange { - get { - return key.exchange.HasValue ? key.exchange.Value : Venue.Unset; - } - set => ignore(); - } - - public string exchange_string { - get { - return exchange.ToString(); - } - set => ignore(); - } - - public string symbol { - get { - return key.symbol; - } - set => ignore(); - } - + public event Action invalidated; + // non-numeric + public string account => key.account; + public Venue exchange => key.exchange ?? Venue.Unset; + public string exchange_string => exchange.ToString(); + public string symbol => key.symbol; public SecurityImmutable? security => key.security; public SecurityType security_type => key.sec_type.GetValueOrDefault(); + public string security_text => security.display_name(); + // positions + public int start_net_position { get; } + public int total_net_position { get; } + public uint day_long_quantity { get; } + public uint day_short_quantity { get; } + // start pnl + public double start_pnl => _start_pnl.value; + public string start_pnl_text => _start_pnl.text; + public Brush start_pnl_background => _start_pnl.background; + // day pnl + public double day_pnl => _day_pnl.value; + public string day_pnl_text => _day_pnl.text; + public Brush day_pnl_background => _day_pnl.background; + // total pnl + public double total_pnl => _total_pnl.value; + public string total_pnl_text => _total_pnl.text; + public Brush total_pnl_background => _total_pnl.background; + // pnl price + public double pnl_price => _pnl_price.value; + public string pnl_price_text => _pnl_price.text; + // pnl weighted averages + public double weighted_average_buy => _weighted_average_buy.value; + public string weighted_average_buy_text => _weighted_average_buy.text; + public double weighted_average_sell => _weighted_average_sell.value; + public string weighted_average_sell_text => _weighted_average_sell.text; - public string security_text { - get => security.HasValue ? security.Value.ToString() : "Unknown Security"; - set => ignore(); - } - - - public int start_net_position { - get => _start_net_position; - set { - if (_start_net_position != value) { - _start_net_position = value; - RaisePropertyChanged(nameof(start_net_position)); - } - } - } - public double start_pnl { - get => _start_pnl; - set { - if (_start_pnl != value) { - _start_pnl = value; - RaisePropertyChanged(); - } - } - } - public uint day_long_quantity { - get => _day_long_quantity; - set { - if (_day_long_quantity != value) { - _day_long_quantity = value; - RaisePropertyChanged(nameof(day_long_quantity)); - } - } - } - public uint day_short_quantity { - get => _day_short_quantity; - set { - if (_day_short_quantity != value) { - _day_short_quantity = value; - RaisePropertyChanged(nameof(day_short_quantity)); - } - } - } - public double day_pnl { - get => _sod_pnl; - set { - if (_sod_pnl != value) { - _sod_pnl = value; - RaisePropertyChanged(); - } - } - } - - - public int total_net_position { - get => _total_net_position; - set { - if (_total_net_position != value) { - _total_net_position = value; - RaisePropertyChanged(nameof(total_net_position)); - } - } - } - - public double total_pnl { - get => _total_pnl; - set { - if (_total_pnl != value) { - _total_pnl = value; - RaisePropertyChanged(nameof(total_pnl)); - } - } - } - - - - - public double pnl_price { - get => _pnl_price; - set { - if (_pnl_price != value) { - _pnl_price = value; - RaisePropertyChanged(nameof(pnl_price)); - } - } - } - - public double weighted_average_buy { - get => _weighted_average_buy; - set { - if (_weighted_average_buy != value) { - _weighted_average_buy = value; - RaisePropertyChanged(nameof(weighted_average_buy)); - } - } - } - - public double weighted_average_sell { - get => _weighted_average_sell; - set { - if (_weighted_average_sell != value) { - _weighted_average_sell = value; - RaisePropertyChanged(nameof(weighted_average_sell)); - } - } - } + public void raise_invalidated() => invalidated?.Invoke(); + ObservableCollection DIYGrid.IRowVM.row_animations { get; } = new ObservableCollection(); + IReadOnlyList DIYGrid.IRowVM.cell_stores => new DIYGrid.IGridCellStore[0]; //public IEnumerable sod_fills => _fills.Where(f => f.Timestamp.ToDateTime() >= _start_of_day); //public IEnumerable pos_fills => _fills.Where(f => f.Timestamp.ToDateTime() < _start_of_day); + class PnLDoubleBrushCell : CellProperty { + Brush _background; + + public PnLDoubleBrushCell(PnLSummaryRow owner, string prop) : base(d => double.IsNaN(d) ? string.Empty : d.ToString("N2"), double.NaN) => + _value_set += _ => { + _background = null; + owner.RaisePropertyChanged(prop); + }; + + public Brush background => _background ?? (_background = _brushes.get(value)); + } + + class PnLPriceCell : CellProperty { + public PnLPriceCell(PnLSummaryRow owner, string prop, double init = 0) + : base(price => owner.security.spec_or_default().format(price), init) => + _value_set += _ => owner.RaisePropertyChanged(prop); + } } - - -} +} \ No newline at end of file diff --git a/src/client/spark/view/Spark/SparkWindow.cs b/src/client/spark/view/Spark/SparkWindow.cs index 1432a42c8..a65b7d983 100644 --- a/src/client/spark/view/Spark/SparkWindow.cs +++ b/src/client/spark/view/Spark/SparkWindow.cs @@ -15,6 +15,7 @@ namespace bts.spark { protected const double SPARK_MENU_COLLAPSED_WIDTH = 52, LAUNCHER_BUTTON_BORDER_WIDTH = 5, + EXTRA_FOR_NO_HORIZONTAL_SCROLLBAR_WIDTH = 18, TITLE_PANEL_HEIGHT = 30, COLUMN_HEADER_HEIGHT = 27, SPARK_MENU_ITEM_HEIGHT = 45, diff --git a/src/client/spark/view/Spark/Utilities/MDHandleExtensions.cs b/src/client/spark/view/Spark/Utilities/MDHandleExtensions.cs index 7b4c3a68d..379235c88 100644 --- a/src/client/spark/view/Spark/Utilities/MDHandleExtensions.cs +++ b/src/client/spark/view/Spark/Utilities/MDHandleExtensions.cs @@ -9,8 +9,8 @@ namespace mbts.co2 { public static double mid(this MDConsumerHandle handle) { ulong unused = 0; handle.get_bbo(ref unused, out NativePriceSize bid, out NativePriceSize ask); - if (bid == NativePriceSize.no_market) return ask.price; - if (ask == NativePriceSize.no_market) return bid.price; + if (bid == NativePriceSize.NO_MARKET) return ask.price; + if (ask == NativePriceSize.NO_MARKET) return bid.price; return (ask.price + bid.price) / 2; } diff --git a/src/client/spark/view/Spark/Utilities/WindowSettingsDialogVM.cs b/src/client/spark/view/Spark/Utilities/WindowSettingsDialogVM.cs index ccc6a5bf1..5c5867fb3 100644 --- a/src/client/spark/view/Spark/Utilities/WindowSettingsDialogVM.cs +++ b/src/client/spark/view/Spark/Utilities/WindowSettingsDialogVM.cs @@ -11,66 +11,70 @@ namespace mbts.spark { T get_result(); } - class WindowSettingsDialogVM : ViewModelBase, IRequestViewClose where T : class, IEquatable where TVM : IWindowSettingsVM, new() { - readonly Property _default_tab_selected, _use_default; - readonly DialogSkirtItem _sync_to_factory_item, _sync_to_default_item, _sync_to_instance_item, _ok_item, _apply_item, _cancel_item; + class WindowSettingsDialogVM : ViewModelBase, IRequestViewClose where T : class, IEquatable where TVM : IWindowSettingsVM { + readonly Property _use_default; + readonly DialogSkirtItem _ok_item, _apply_item, _cancel_item, _apply_to_defaults_item; readonly DelegateProperty> _left_skirt_items; + readonly List _resources = new List(2); // initialize with 2 capacity bc the only potential disposables are the instance/default vms - public WindowSettingsDialogVM(IWindowSettingsManager settings_manager, Func get_factory_settings, string settings_file_ext) { - var settings = settings_manager.current_settings(); - _default_tab_selected = backing(nameof(default_tab_selected), settings_manager.instance_settings == null); - _use_default = backing(nameof(use_default), default_tab_selected); - default_vm = new TVM(); - default_vm.load(settings_manager.default_settings); - instance_vm = new TVM(); - instance_vm.load(settings_manager.current_settings()); + public WindowSettingsDialogVM(IWindowSettingsManager settings_manager, Func get_factory_settings, string settings_file_ext, Func vm_factory) { + // by default, each new window will use their own instance settings + _use_default = backing(nameof(use_default), settings_manager.instance_settings == null); + default_vm = vm_factory(settings_manager.default_settings); + // instance vm is what will always be visible on the screen + instance_vm = vm_factory(settings_manager.current_settings()); // items const string import_group = "i", export_group = "e"; (string name, string[] extensions) file_filter = ($"{settings_file_ext} settings files", new[] { settings_file_ext }); - var import_to_custom_cmd = FileDialogCommand.import_proto(s => instance_vm.load(s), () => !use_default, settings_file_ext, filter_items: file_filter); - var export_to_custom_cmd = FileDialogCommand.export_proto(() => instance_vm.get_result(), () => !use_default, filter_items: file_filter); - _use_default.Changed += (_, __) => { - import_to_custom_cmd.RaiseCanExecuteChanged(); - export_to_custom_cmd.RaiseCanExecuteChanged(); - }; + var import_to_custom_cmd = FileDialogCommand.import_proto(s => instance_vm.load(s), type_name: settings_file_ext, filter_items: file_filter); + var export_to_custom_cmd = FileDialogCommand.export_proto(() => instance_vm.get_result(), filter_items: file_filter); action_menu_items = new IMenuItemVM[] { - new MenuItemVM("Import File to Default...", FileDialogCommand.import_proto(s => default_vm.load(s), null, settings_file_ext), Icons.Import, import_group), - new MenuItemVM("Import File to Custom...", import_to_custom_cmd, Icons.Import, import_group), - new MenuItemVM("Export Default to File...", FileDialogCommand.export_proto(() => default_vm.get_result(), filter_items: file_filter), Icons.Export, export_group), - new MenuItemVM("Export Custom to File...", export_to_custom_cmd, Icons.Export, export_group) + new MenuItemVM("Load Settings File ...", import_to_custom_cmd, Icons.Import, import_group), + new MenuItemVM("Save Settings File ...", export_to_custom_cmd, Icons.Export, export_group), + new MenuItemVM("Load Factory Defaults", + new DelegateCommand(() => instance_vm.load(get_factory_settings(UserSettings.factory_defaults))), + Icons.Factory + ) }; - _left_skirt_items = backing(nameof(left_skirt_items), left_skirt_factory, nameof(default_tab_selected)); - _sync_to_factory_item = new DialogSkirtItem( - "Reset to Factory", - new DelegateCommand(() => (default_tab_selected ? default_vm : instance_vm).load(get_factory_settings(UserSettings.factory_defaults))), - ui.spark.LadderSettingsDialog.ResetToFactoryKey); - _sync_to_default_item = new DialogSkirtItem( - "Copy from Default", - new DelegateCommand(() => instance_vm.load(settings_manager.default_settings)), - ui.spark.LadderSettingsDialog.CopyFromOtherKey); - _sync_to_instance_item = new DialogSkirtItem( - "Copy from Custom", - new DelegateCommand(() => default_vm.load(instance_vm.get_result())), - ui.spark.LadderSettingsDialog.CopyFromOtherKey); - _apply_item = new DialogSkirtItem("Apply", new DelegateCommand(apply)); + _left_skirt_items = backing(nameof(left_skirt_items), left_skirt_factory); + _apply_item = new DialogSkirtItem("Apply", new DelegateCommand(() => { + use_default = false; + apply(); + })); _ok_item = new DialogSkirtItem("OK", new DelegateCommand(() => { + use_default = false; apply(); close(); })) { is_default = true }; _cancel_item = new DialogSkirtItem("Cancel", new DelegateCommand(close)) { is_cancel = true }; + _apply_to_defaults_item = new DialogSkirtItem("Save As Defaults", new DelegateCommand(() => { + use_default = true; + apply(); + close(); + }), ui.spark.LadderSettingsDialog.ResetToFactoryKey); + // maybe dispose vms + if (instance_vm is IDisposable dispose_ivm) + _resources.Add(dispose_ivm); + if (default_vm is IDisposable dispose_dvm) + _resources.Add(dispose_dvm); // local methods void apply() { - var my_default = default_vm.get_result(); - if (!my_default.safe_equals(settings_manager.default_settings)) - settings_manager.save_default_settings(my_default).ignore(); if (!use_default) { settings_manager.instance_settings = instance_vm.get_result(); - } else settings_manager.instance_settings = null; + } else { + settings_manager.instance_settings = null; + + // copy the current instance settings and persist + var my_default = instance_vm.get_result(); + if (!my_default.safe_equals(settings_manager.default_settings)) + settings_manager.save_default_settings(my_default).ignore(); + } } + void close() => RequestClose?.Invoke(this, new EventArgs()); + IEnumerable left_skirt_factory() { - yield return _sync_to_factory_item; - yield return default_tab_selected ? _sync_to_instance_item : _sync_to_default_item; + yield return _apply_to_defaults_item; }; } @@ -87,14 +91,12 @@ namespace mbts.spark { yield return _cancel_item; } } - public bool default_tab_selected { get => _default_tab_selected.Value; set => _default_tab_selected.Value = value; } public bool use_default { get => _use_default.Value; - set { - _use_default.Value = value; - if (value) - default_tab_selected = true; - } + set => _use_default.Value = value; } + + public void Dispose() => + Disposable.dispose(_resources); } } \ No newline at end of file diff --git a/src/client/spark/view/Utility/EnabledValueVM.cs b/src/client/spark/view/Utility/EnabledValueVM.cs index e3acce1d5..db174bcb4 100644 --- a/src/client/spark/view/Utility/EnabledValueVM.cs +++ b/src/client/spark/view/Utility/EnabledValueVM.cs @@ -1,4 +1,5 @@ using mbts.ui.shell; +using System; namespace mbts.ui { public class EnabledValueVM : ViewModelBase { @@ -8,10 +9,13 @@ namespace mbts.ui { public EnabledValueVM() { _enabled = backing(nameof(enabled)); _value = backing(nameof(value)); + _enabled.Changed += (_, __) => enabled_changed?.Invoke(); } public bool enabled { get => _enabled.Value; set => _enabled.set(value); } public T value { get => _value.Value; set => _value.set(value); } + + public event Action enabled_changed; } public class NamedValueVM { diff --git a/src/client/spark/view/Utility/MDDisplayHelper.cs b/src/client/spark/view/Utility/MDDisplayHelper.cs new file mode 100644 index 000000000..f0ae41665 --- /dev/null +++ b/src/client/spark/view/Utility/MDDisplayHelper.cs @@ -0,0 +1,284 @@ +using bts.utils; +using mbts.co2; +using mbts.spark; +using mbts.ui.shell; +using mbts.utils; +using ProtoBuf; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace mbts.ui.spark { + [ProtoContract] + class MarketSizeDisplay : IEquatable { + [ProtoMember(1)] + public readonly SizeFormattingElement[] normal; + [ProtoMember(2)] + public readonly SizeFormattingElement[] implied_present; + + public MarketSizeDisplay() { } + public MarketSizeDisplay(SizeFormattingElement[] normal, SizeFormattingElement[] implied_present) { + this.normal = normal; + this.implied_present = implied_present; + } + + public enum SizeFormattingElementType { + Merged, + Direct, + Implied, + Const + } + + public static MarketSizeDisplay get_default() => + new MarketSizeDisplay(SizeFormattingElement.merged, SizeFormattingElement.merged_asterisk); + + public MarketSizeDisplay with(SizeFormattingElement[] normal = null, SizeFormattingElement[] implied_present = null) => + new MarketSizeDisplay(normal ?? this.normal, implied_present ?? this.implied_present); + + public bool Equals(MarketSizeDisplay other) => + other != null && + EqualityComparer.safe_sequence_equals(normal, other.normal) && + EqualityComparer.safe_sequence_equals(implied_present, other.implied_present); + + public override bool Equals(object obj) => + Equals(obj as MarketSizeDisplay); + + public override int GetHashCode() => + EqualityComparer.aggregate_hash(implied_present.GetHashCode(), normal.GetHashCode()); + + [ProtoContract] + public struct SizeFormattingElement : IEquatable { + [ProtoMember(1)] + public readonly SizeFormattingElementType type; + [ProtoMember(2)] + public readonly string data; + + SizeFormattingElement(SizeFormattingElementType type, string data = null) { + this.type = type; + this.data = data; + } + + public bool Equals(SizeFormattingElement other) => other.type == type && string.Equals(data, other.data); + public override bool Equals(object obj) => obj is SizeFormattingElement other && Equals(other); + public override int GetHashCode() => EqualityComparer.aggregate_hash(type.GetHashCode(), EqualityComparer.safe_get_hash_code(data)); + public override string ToString() => type == SizeFormattingElementType.Const ? data ?? string.Empty : type.ToString(); + + public static bool operator ==(SizeFormattingElement a, SizeFormattingElement b) => a.Equals(b); + public static bool operator !=(SizeFormattingElement a, SizeFormattingElement b) => !a.Equals(b); + + public static SizeFormattingElement[] direct => new[] { new SizeFormattingElement(SizeFormattingElementType.Direct) }; + public static SizeFormattingElement[] implied => new[] { new SizeFormattingElement(SizeFormattingElementType.Implied) }; + public static SizeFormattingElement[] direct_implied => new[] { + new SizeFormattingElement(SizeFormattingElementType.Direct), + new SizeFormattingElement(SizeFormattingElementType.Const, " ("), + new SizeFormattingElement(SizeFormattingElementType.Implied), + new SizeFormattingElement(SizeFormattingElementType.Const, ")") + }; + public static SizeFormattingElement[] merged => new[] { new SizeFormattingElement(SizeFormattingElementType.Merged) }; + public static SizeFormattingElement[] merged_asterisk => new[] { new SizeFormattingElement(SizeFormattingElementType.Merged), new SizeFormattingElement(SizeFormattingElementType.Const, "*") }; + public static SizeFormattingElement[] merged_implied => new[] { + new SizeFormattingElement(SizeFormattingElementType.Merged), + new SizeFormattingElement(SizeFormattingElementType.Const, " ("), + new SizeFormattingElement(SizeFormattingElementType.Implied), + new SizeFormattingElement(SizeFormattingElementType.Const, ")") + }; + public static SizeFormattingElement[] merged_direct => new[] { + new SizeFormattingElement(SizeFormattingElementType.Merged), + new SizeFormattingElement(SizeFormattingElementType.Const, " ("), + new SizeFormattingElement(SizeFormattingElementType.Direct), + new SizeFormattingElement(SizeFormattingElementType.Const, ")") + }; + public static SizeFormattingElement[] merged_direct_implied => new[] { + new SizeFormattingElement(SizeFormattingElementType.Merged), + new SizeFormattingElement(SizeFormattingElementType.Const, " ("), + new SizeFormattingElement(SizeFormattingElementType.Direct), + new SizeFormattingElement(SizeFormattingElementType.Const, ":"), + new SizeFormattingElement(SizeFormattingElementType.Implied), + new SizeFormattingElement(SizeFormattingElementType.Const, ")") + }; + } + } + + static class MDDisplayHelper { + /// This delegate should be created by calling + public delegate string SizeFormatter(uint merged, uint implied); + /// The private delegates below use refs bc the returned by + /// internally uses several s to add different text components to the resultant string. + /// The refs, therefor, avoid making extra copies of the variables. + /// These are private members, so any code-readability cost is confined to a limited scope. + delegate void _SizeFormatter(ref uint merged, ref uint implied, ref StringBuilder sb); + delegate void _SizeFormatterNPS(ref NativePriceSize nps, ref StringBuilder sb); + + public static Func get_formatter_nps(MarketSizeDisplay size_display) { + _SizeFormatterNPS implied_absent = _get_formatter_nps(size_display.normal); + _SizeFormatterNPS implied_present = _get_formatter_nps(size_display.implied_present ?? size_display.normal); + return nps => { + StringBuilder sb = new StringBuilder(); + (nps.implied_size > 0 ? implied_present : implied_absent)(ref nps, ref sb); + return sb.ToString(); + }; + } + + public static SizeFormatter get_formatter(MarketSizeDisplay size_display) { + _SizeFormatter implied_absent = _get_formatter(size_display.normal); + _SizeFormatter implied_present = _get_formatter(size_display.implied_present ?? size_display.normal); + return (merged, implied) => { + StringBuilder sb = new StringBuilder(); + (implied > 0 ? implied_present : implied_absent)(ref merged, ref implied, ref sb); + return sb.ToString(); + }; + } + + static _SizeFormatterNPS _get_formatter_nps(MarketSizeDisplay.SizeFormattingElement[] fmt_elements) { + _SizeFormatterNPS[] formatters = fmt_elements.is_null_or_empty() ? new _SizeFormatterNPS[0] : CollectionHelper.convert_array(fmt_elements, _get_formatter_nps); + return new _SizeFormatterNPS((ref NativePriceSize nps, ref StringBuilder sb) => { + foreach (var f in formatters) + f(ref nps, ref sb); + }); + } + + static _SizeFormatter _get_formatter(MarketSizeDisplay.SizeFormattingElement[] fmt_elements) { + _SizeFormatter[] formatters = fmt_elements.is_null_or_empty() ? new _SizeFormatter[0] : CollectionHelper.convert_array(fmt_elements, _get_formatter); + return new _SizeFormatter((ref uint m, ref uint i, ref StringBuilder sb) => { + foreach (var f in formatters) + f(ref m, ref i, ref sb); + }); + } + + static _SizeFormatterNPS _get_formatter_nps(MarketSizeDisplay.SizeFormattingElement fmt_element) { + switch (fmt_element.type) { + case MarketSizeDisplay.SizeFormattingElementType.Const: return SizeTokenFormatters.char_formatter_nps(fmt_element.data ?? string.Empty); + case MarketSizeDisplay.SizeFormattingElementType.Direct: return SizeTokenFormatters.direct_formatter_nps(); + case MarketSizeDisplay.SizeFormattingElementType.Implied: return SizeTokenFormatters.implied_formatter_nps(); + case MarketSizeDisplay.SizeFormattingElementType.Merged: return SizeTokenFormatters.merged_formatter_nps(); + default: throw ExceptionBuilder.Argument.unexpected_case(fmt_element.type, nameof(fmt_element.type)); + } + } + + static _SizeFormatter _get_formatter(MarketSizeDisplay.SizeFormattingElement fmt_element) { + switch (fmt_element.type) { + case MarketSizeDisplay.SizeFormattingElementType.Const: return SizeTokenFormatters.char_formatter(fmt_element.data ?? string.Empty); + case MarketSizeDisplay.SizeFormattingElementType.Direct: return SizeTokenFormatters.direct_formatter(); + case MarketSizeDisplay.SizeFormattingElementType.Implied: return SizeTokenFormatters.implied_formatter(); + case MarketSizeDisplay.SizeFormattingElementType.Merged: return SizeTokenFormatters.merged_formatter(); + default: throw ExceptionBuilder.Argument.unexpected_case(fmt_element.type, nameof(fmt_element.type)); + } + } + + static class SizeTokenFormatters { + public static _SizeFormatter char_formatter(string data) { + string local = data.ToString(); + return new _SizeFormatter((ref uint m, ref uint i, ref StringBuilder sb) => sb.Append(local)); + } + public static _SizeFormatter merged_formatter() => new _SizeFormatter((ref uint m, ref uint i, ref StringBuilder sb) => sb.Append(m)); + public static _SizeFormatter direct_formatter() => new _SizeFormatter((ref uint m, ref uint i, ref StringBuilder sb) => sb.Append(m - i)); + public static _SizeFormatter implied_formatter() => new _SizeFormatter((ref uint m, ref uint i, ref StringBuilder sb) => sb.Append(i)); + + public static _SizeFormatterNPS char_formatter_nps(string data) { + string local = data.ToString(); + return new _SizeFormatterNPS((ref NativePriceSize _, ref StringBuilder sb) => sb.Append(local)); + } + public static _SizeFormatterNPS merged_formatter_nps() => new _SizeFormatterNPS((ref NativePriceSize nps, ref StringBuilder sb) => sb.Append(nps.size)); + public static _SizeFormatterNPS direct_formatter_nps() => new _SizeFormatterNPS((ref NativePriceSize nps, ref StringBuilder sb) => sb.Append(nps.direct_size)); + public static _SizeFormatterNPS implied_formatter_nps() => new _SizeFormatterNPS((ref NativePriceSize nps, ref StringBuilder sb) => sb.Append(nps.implied_size)); + } + } + + class MarketSizeDisplayVM : ViewModelBase, IValueFactory { + readonly IEqualityComparer _comp = EqualityComparer.Create((a, b) => EqualityComparer.safe_sequence_equals(a, b)); + readonly Property _selected_format, _selected_alternate_implied_format; + readonly DelegateProperty _can_choose_alternate_implied_format; + + public MarketSizeDisplayVM() { + _selected_format = backing(nameof(selected_format)); + _selected_alternate_implied_format = backing(nameof(selected_alternate_implied_format)); + _can_choose_alternate_implied_format = backing(nameof(can_choose_alternate_implied_format), () => + _comp.Equals(MarketSizeDisplay.SizeFormattingElement.merged, selected_format?.value)); + sample_items = new[] { + new SampleItem(90, 0, this), + new SampleItem(75, 25, this) + }; + _selected_format.Changed += refresh_sample_items; + _selected_alternate_implied_format.Changed += refresh_sample_items; + void refresh_sample_items(FormattingVM oldval, FormattingVM newval) { + _can_choose_alternate_implied_format.refresh(); + foreach (var si in sample_items) + si.refresh(); + } + } + + public IEnumerable format_options { get; } = new[] { + MarketSizeDisplay.SizeFormattingElement.direct, + MarketSizeDisplay.SizeFormattingElement.implied, + MarketSizeDisplay.SizeFormattingElement.direct_implied, + MarketSizeDisplay.SizeFormattingElement.merged_implied, + MarketSizeDisplay.SizeFormattingElement.merged_direct_implied, + MarketSizeDisplay.SizeFormattingElement.merged + }.Select(ts => new FormattingVM(ts)).ToArray(); + + public IEnumerable alternate_implied_format_options { get; } = new[] { + MarketSizeDisplay.SizeFormattingElement.merged, + MarketSizeDisplay.SizeFormattingElement.merged_asterisk, + MarketSizeDisplay.SizeFormattingElement.merged_implied, + MarketSizeDisplay.SizeFormattingElement.merged_direct, + MarketSizeDisplay.SizeFormattingElement.merged_direct_implied + }.Select(ts => new FormattingVM(ts)).ToArray(); + + public FormattingVM selected_format { + get => _selected_format.Value; + set => _selected_format.Value = value; + } + public FormattingVM selected_alternate_implied_format { + get => _selected_alternate_implied_format.Value; + set => _selected_alternate_implied_format.Value = value; + } + + public bool can_choose_alternate_implied_format => _can_choose_alternate_implied_format.Value; + + public IEnumerable sample_items { get; } + + public MarketSizeDisplay create() { + var normal_fmt = selected_format?.value; + var alt_implied_fmt = can_choose_alternate_implied_format ? selected_alternate_implied_format?.value : normal_fmt; + return new MarketSizeDisplay(normal_fmt, alt_implied_fmt); + } + + public void load_from(MarketSizeDisplay msd) { + selected_format = format_options.FirstOrDefault(f => _comp.Equals(f.value, msd.normal)); + if (can_choose_alternate_implied_format) + selected_alternate_implied_format = alternate_implied_format_options.FirstOrDefault(f => _comp.Equals(f.value, msd.implied_present)); + } + + public class FormattingVM : ViewModelBase, IEquatable { + public FormattingVM(MarketSizeDisplay.SizeFormattingElement[] tokens) => + value = tokens; + + public MarketSizeDisplay.SizeFormattingElement[] value { get; } + + public bool Equals(FormattingVM other) => + other != null && + EqualityComparer.safe_sequence_equals(value, other.value); + + public override bool Equals(object obj) => Equals(obj as FormattingVM); + public override int GetHashCode() => EqualityComparer.safe_aggregate_hash(value); + public override string ToString() => string.Join(string.Empty, value); + } + + public class SampleItem : ViewModelBase { + readonly DelegateProperty _display; + + public SampleItem(uint direct, uint implied, MarketSizeDisplayVM owner) { + this.direct = direct; + this.implied = implied; + _display = backing(nameof(display), () => MDDisplayHelper.get_formatter(owner.create())(merged, implied)); + } + + public uint direct { get; } + public uint implied { get; } + public uint merged => direct + implied; + public string display => _display.Value; + public void refresh() => _display.refresh(); + } + } +} \ No newline at end of file diff --git a/src/client/spark/view/Utility/WPFHelper.cs b/src/client/spark/view/Utility/WPFHelper.cs index dad292484..275ac1ea2 100644 --- a/src/client/spark/view/Utility/WPFHelper.cs +++ b/src/client/spark/view/Utility/WPFHelper.cs @@ -38,6 +38,9 @@ namespace mbts.ui { public static DependencyProperty create_dp(string name, PropertyChangedCallback callback, TValue default_value = default) => DependencyProperty.Register(name, typeof(TValue), typeof(TOwner), new PropertyMetadata(default_value, prop_changed_callback(callback))); + public static DependencyProperty create_dp(string name, FrameworkPropertyMetadataOptions options, PropertyChangedCallback callback, TValue default_value = default) => + DependencyProperty.Register(name, typeof(TValue), typeof(TOwner), new FrameworkPropertyMetadata(default_value, options, prop_changed_callback(callback))); + public static DependencyPropertyKey create_ro_dp(string name, TValue default_value = default) => DependencyProperty.RegisterReadOnly(name, typeof(TValue), typeof(TOwner), new PropertyMetadata(default_value)); diff --git a/src/client/spark_api/api/Update.cs b/src/client/spark_api/api/Update.cs index 2853882db..6f9604a01 100644 --- a/src/client/spark_api/api/Update.cs +++ b/src/client/spark_api/api/Update.cs @@ -129,6 +129,7 @@ namespace bts.api.core { public IEnumerable removed { get; } public TKey key(T t) { return _key(t); } + internal Func key_extractor => _key; #if DEBUG public override string ToString() { @@ -347,5 +348,72 @@ namespace bts.api.core { return source.Select(t => new LegacyTableUpdate(t)); } + public static IObservable> Filter(this IObservable> source, IObservable> predicate) where TKey : IEquatable + { + return Observable.Create>(observer => + { + Update unfiltered_cache = null, filtered_cache = null; + Func pred_cache = null; + Log.DataFlow.diag($"Create Filter {typeof(T).Name}"); + + return source.CombineLatest(predicate, (upd, pred) => (upd, pred)).Subscribe( tup => + { + bool pred_changed = !object.ReferenceEquals(tup.pred, pred_cache) && (pred_cache != null); + pred_cache = tup.pred; + unfiltered_cache = tup.upd; + if (pred_changed && filtered_cache != null) + { + //clear out existing + var clear = new Update( + (IReadOnlyCollection)Array.Empty(), + Enumerable.Empty(), + Enumerable.Empty>(), + filtered_cache.all, unfiltered_cache.key_extractor); + observer.OnNext(clear); + + var refilter = unfiltered_cache.all.Where(tup.pred).ToList(); + var readd = new Update( + refilter, refilter, + Enumerable.Empty>(), + Enumerable.Empty(), unfiltered_cache.key_extractor); + observer.OnNext(readd); + filtered_cache = readd; + } + else + { + IReadOnlyCollection all; IEnumerable added, removed; IEnumerable> changed; + apply_predicate(tup.pred, tup.upd, out all, out added, out changed, out removed); + var next = new Update(all, added, changed, removed, tup.upd.key_extractor); + observer.OnNext(next); + filtered_cache = next; + } + }, + () => observer.OnCompleted()); + }); + } + + private static void apply_predicate(Func predicate, Update cache, + out IReadOnlyCollection all, out IEnumerable added, + out IEnumerable> changed, out IEnumerable removed) where TKey : IEquatable + { + var adds = new List(); + adds.AddRange(cache.added.Where(predicate)); + var rems = new List(); + rems.AddRange(cache.removed.Where(predicate)); + var diffs = new List>(); + foreach (var change in cache.changes) + { + if(!predicate(change.Old) && predicate(change.New)) + adds.Add(change.New); + else if(predicate(change.Old) && !predicate(change.New)) + rems.Add(change.Old); + else if(predicate(change.Old) && predicate(change.New)) + diffs.Add(change); + } + all = cache.all.Where(predicate).ToList(); + added = adds; + changed = diffs; + removed = rems; + } } } \ No newline at end of file diff --git a/src/client/spark_api/md/MarketDataClient.cs b/src/client/spark_api/md/MarketDataClient.cs index 793259546..748ffdea8 100644 --- a/src/client/spark_api/md/MarketDataClient.cs +++ b/src/client/spark_api/md/MarketDataClient.cs @@ -22,6 +22,13 @@ namespace bts.api.md public long last_update_time; public ChannelUpdate data; } + + const UInt32 _all_flags = (uint)( + MDRequest.Types.Flags.Security | + MDRequest.Types.Flags.Bbo | + MDRequest.Types.Flags.Direct | + MDRequest.Types.Flags.Implied | + MDRequest.Types.Flags.LastTrade); readonly ConcurrentDictionary _data = new ConcurrentDictionary(); readonly ConcurrentDictionary _security_key_to_desc= new ConcurrentDictionary(); @@ -95,8 +102,9 @@ namespace bts.api.md public async Task find_security_async(Func filter, TimeSpan? timeout = null) { - if(!timeout.HasValue) + if(!timeout.HasValue) { timeout = TimeSpan.FromMilliseconds(int.MaxValue-1); + } CancellationTokenSource src = new CancellationTokenSource(timeout.Value); Security security = await Task.Run(async () => { @@ -117,10 +125,7 @@ namespace bts.api.md public IObservable subscribe(Product product, TimeSpan? update_interval = null, IScheduler scheduler = null) { - return subscribe(product, - (System.UInt32)(MDRequest.Types.Flags.Security | MDRequest.Types.Flags.Bbo | - MDRequest.Types.Flags.Depth | MDRequest.Types.Flags.LastTrade), - update_interval, scheduler); + return subscribe(product, _all_flags, update_interval, scheduler); } @@ -167,10 +172,7 @@ namespace bts.api.md public IObservable subscribe(Security security, TimeSpan? update_interval = null, IScheduler scheduler = null) { - return subscribe(security, - (System.UInt32)(MDRequest.Types.Flags.Security | MDRequest.Types.Flags.Bbo | - MDRequest.Types.Flags.Depth | MDRequest.Types.Flags.LastTrade), - update_interval, scheduler); + return subscribe(security, _all_flags, update_interval, scheduler); } /// @@ -261,8 +263,8 @@ namespace bts.api.md if (_data.TryGetValue(update.Security.Id, out data)) { //mergefrom concats repeated fields - if (update.Book != null) - data.data.Book = null; + if (update.Direct != null) + data.data.Direct = null; //this can tear, but the book won't tear, so maybe it's ok data.data.MergeFrom(update); data.last_update_time = now; diff --git a/src/client/spark_core/Co2/AtomicPriceSize.cs b/src/client/spark_core/Co2/AtomicPriceSize.cs new file mode 100644 index 000000000..2c6922602 --- /dev/null +++ b/src/client/spark_core/Co2/AtomicPriceSize.cs @@ -0,0 +1,42 @@ +using System; +using System.Threading; + +namespace mbts.co2 { + struct TimeStamp { + Int64 _value; + public UInt64 time => (UInt64)Interlocked.Read(ref _value); + public static UInt64 invalid_value => UInt64.MaxValue; + } + + // This type cannot be generic because we will get error CS0208 + //internal struct Optimistic where T : struct {... + internal struct AtomicPriceSize { + TimeStamp _time; + NativePriceSize _value; + void store(NativePriceSize nps) { + _time = new TimeStamp(); + _value = new NativePriceSize(); + throw new NotImplementedException(); + } + public (UInt64 time, NativePriceSize value) load() { + UInt64 temp; + NativePriceSize result; + int attempts = 0; + do { + ++attempts; + temp = _time.time; + result = _value; + } while ((temp != _time.time || temp == TimeStamp.invalid_value) && attempts < 1619200); + return (temp, result); + } + public bool load_if_new(ref UInt64 time, out NativePriceSize t) { + if (_time.time > time || _time.time == TimeStamp.invalid_value) { + (time, t) = load(); + return (time != TimeStamp.invalid_value); + } + t = NativePriceSize.NO_MARKET; + return false; + } + } + +} diff --git a/src/client/spark_core/Co2/Co2NativeMethods.cs b/src/client/spark_core/Co2/Co2NativeMethods.cs index 41adc8d4d..1276d1ca9 100644 --- a/src/client/spark_core/Co2/Co2NativeMethods.cs +++ b/src/client/spark_core/Co2/Co2NativeMethods.cs @@ -56,8 +56,6 @@ namespace mbts.co2 [DllImport("libspark")] internal static extern void md_consumer_set_on_security(IntPtr consumer, raw_security_callback on_security); internal unsafe delegate void raw_just_security_callback(security_key key, byte* buffer, UInt64 buffer_size); - [DllImport("libspark")] - internal unsafe static extern void md_consumer_get_single_security(MDConsumerHandle.ColdEntry* entry, raw_just_security_callback on_security); [DllImport("libspark")] internal unsafe static extern bool md_consumer_is_heart_beating(MDConsumerHandle.ColdHeader* header); @@ -106,16 +104,34 @@ namespace mbts.co2 [DllImport("libspark")] internal static extern void co2_get_sample_cold_header(ref MDConsumerHandle.ColdHeader entry); + public class TestMDHandle : IDisposable { + IntPtr _ptr; + internal TestMDHandle(IntPtr p) { _ptr = p; } + public void Dispose() { + if (_ptr != IntPtr.Zero) { + co2_destroy_test_md(_ptr); + _ptr = IntPtr.Zero; + } + } + } + public static TestMDHandle produce_test_md() { + return new TestMDHandle(co2_produce_test_md()); + } [DllImport("libspark")] - internal static extern IntPtr co2_produce_test_md(); + private static extern IntPtr co2_produce_test_md(); [DllImport("libspark")] - internal static extern void co2_destroy_test_md(IntPtr handle); + private static extern void co2_destroy_test_md(IntPtr handle); [DllImport("libspark")] internal static extern void co2_get_zero_and_nan_book(ref NativePriceLevel nps); + [DllImport("libspark")] + internal static extern void co2_get_nan_price_size(out NativePriceSize ps); [DllImport("libspark")] internal static extern void co2_get_sample_datetime(ref utils.LLDateTime now, ref utils.LLDateTime utc_now); + [DllImport("libspark")] + internal static extern void co2_test_get_constants(out ulong max_depth, out ulong max_implied_depth, out sbyte nan_exponent); + } } diff --git a/src/client/spark_core/Co2/IntPrice.cs b/src/client/spark_core/Co2/IntPrice.cs new file mode 100644 index 000000000..989fa4063 --- /dev/null +++ b/src/client/spark_core/Co2/IntPrice.cs @@ -0,0 +1,58 @@ +using System; +using System.Runtime.InteropServices; + +namespace mbts.co2 { + + [StructLayout(LayoutKind.Explicit)] + public readonly struct IntPrice : IEquatable { + [FieldOffset(0)] + readonly long _mantissa; + [FieldOffset(7)] + readonly sbyte _exponent; + + public IntPrice(long mantissa, sbyte exponent) { + _mantissa = mantissa; + _exponent = exponent; + } + public long mantissa => NativePriceHelpers.decode(_mantissa); + public sbyte exponent => _exponent; + public bool is_nan => _exponent == NativePriceHelpers.COCONUT_NAN_EXPONENT; + public double price() { + return NativePriceHelpers.to_double(mantissa, exponent); + } + + public bool Equals(IntPrice other) { + return _exponent == other._exponent && _mantissa == other._mantissa; + } + public override bool Equals(object obj) { + return base.Equals(obj); + } + public static bool operator ==(IntPrice a, IntPrice b) { + return a.Equals(b); + } + public static bool operator !=(IntPrice a, IntPrice b) { + return !a.Equals(b); + } + public static bool operator < (IntPrice a, IntPrice b) { + if (a.exponent == b.exponent) { + return a.mantissa < b.mantissa; + } + return a.price() < b.price(); + } + public static bool operator > (IntPrice a, IntPrice b) { + if (a.exponent == b.exponent) { + return a.mantissa > b.mantissa; + } + return a.price() > b.price(); + } + public override string ToString() { + return $"{price():0.00}"; + } + public override int GetHashCode() { + return _mantissa.GetHashCode(); + } + + public static IntPrice NaN = new IntPrice(0,127); + } + +} diff --git a/src/client/spark_core/Co2/MDConsumerHandle.cs b/src/client/spark_core/Co2/MDConsumerHandle.cs index f57aae440..3f5c6502e 100644 --- a/src/client/spark_core/Co2/MDConsumerHandle.cs +++ b/src/client/spark_core/Co2/MDConsumerHandle.cs @@ -1,4 +1,6 @@ -using bts.co2; +using bts; +using bts.api; +using bts.co2; using System; using System.Collections.Generic; using System.IO; @@ -8,11 +10,8 @@ using System.Text; using System.Threading; using System.Threading.Tasks; using security_key = System.UInt32; -using bts.api; -using bts; -namespace mbts.co2 -{ +namespace mbts.co2 { unsafe class MDConsumerHandle { // Once is_initialized goes true it never goes false @@ -23,11 +22,6 @@ namespace mbts.co2 // The exchange key can change at CBOE. This numeric_id will always be correct public ulong numeric_id { get { return _cold->numeric_id; } } - public Security get_security() { - Security result = null; - Co2NativeMethods.md_consumer_get_single_security(_cold, (key, buffer, length) => result = ProtoBuf.Serializer.Deserialize(new UnmanagedMemoryStream(buffer, (long)length))); - return result; - } // This property exists for specific performance optimization purposes // Most callers should just use get_bbo/get_depth @@ -37,35 +31,97 @@ namespace mbts.co2 var updated = market_timestamp; if (updated > time && updated != TimeStamp.invalid_value) { if (has_depth) { - bid = _hot_depth->bid(0); - ask = _hot_depth->ask(0); + var bids = new NativePriceSize[1]; + var asks = new NativePriceSize[1]; + var result = get_depth(ref time, ref bids, ref asks, 1); + bid = bids[0]; + ask = asks[0]; } else { + Log.assert_d(false); bid = _hot->book.bid; ask = _hot->book.ask; } time = updated; return true; } - bid = ask = NativePriceSize.no_market; + bid = ask = NativePriceSize.NO_MARKET; return false; } - public void get_depth(ref NativePriceSize[] bids, ref NativePriceSize[] asks) { - ulong unused = 0; - get_depth(ref unused, ref bids, ref asks); + private unsafe static void copy_merged(in HotDepthEntry hot, uint count, NativePriceSize[] bid, NativePriceSize[] ask) { + Log.assert_d(bid.Length >= count); + Log.assert_d(ask.Length >= count); + uint xda = 0, xdb = 0, xia = 0, xib = 0; + if (hot.implied(xib).bid.is_nan) xib = NativePriceHelpers.COCONUT_MAX_IMPLIED_DEPTH; + if (hot.implied(xia).ask.is_nan) xia = NativePriceHelpers.COCONUT_MAX_IMPLIED_DEPTH; + for (uint x = 0; x < count; ++x) { + if (xib < NativePriceHelpers.COCONUT_MAX_IMPLIED_DEPTH) { + var direct = hot.direct(xdb).bid; + var implied = hot.implied(xib).bid; + + // Compare bids + if (direct.int_price == implied.int_price) { + bid[x] = new NativePriceSize(direct.int_price, direct.size, implied.size); + xdb += 1; + xib += 1; + } + // Bids grow down, take the higher price + else if (implied.is_nan || implied.int_price < direct.int_price) { + bid[x] = direct; + xdb += 1; + } + else { + bid[x] = implied; + xib += 1; + } + } + else { + bid[x] = hot.direct(xdb).bid; + xdb += 1; + } + + if (xia < NativePriceHelpers.COCONUT_MAX_IMPLIED_DEPTH) { + var direct = hot.direct(xda).ask; + var implied = hot.implied(xia).ask; + + // Compare asks + if (direct.int_price == implied.int_price) { + ask[x] = new NativePriceSize( direct.int_price, direct.size, implied.size); + xda += 1; + xia += 1; + } + // asks grow up, take the lower price + else if (implied.is_nan || direct.int_price < implied.int_price) { + ask[x] = direct; + xda += 1; + } + else { + ask[x] = implied; + xia += 1; + } + } + else { + ask[x] = hot.direct(xda).ask; + xda += 1; + } + } } - public bool get_depth(ref ulong time, ref NativePriceSize[] bids, ref NativePriceSize[] asks) { + + public bool get_depth(ref ulong time, ref NativePriceSize[] bids, ref NativePriceSize[] asks, uint depth) { if (!has_depth) { throw new InvalidOperationException("Cannot get depth data from a handle that does not offer depth!"); } var updated = market_timestamp; - if (updated > time && updated != UInt64.MaxValue) { - if (bids == null || bids.Length < 10) bids = new NativePriceSize[10]; - if (asks == null || asks.Length < 10) asks = new NativePriceSize[10]; - for (int depth = 0; depth < 10; ++depth) { - bids[depth] = _hot_depth->bid(depth); - asks[depth] = _hot_depth->ask(depth); + Log.assert(depth <= NativePriceHelpers.COCONUT_MAX_DEPTH); + if (updated > time && updated != TimeStamp.invalid_value) { + if (bids == null || bids.Length < depth) { + bids = new NativePriceSize[depth]; } + + if (asks == null || asks.Length < depth) { + asks = new NativePriceSize[depth]; + } + copy_merged(*_hot_depth, depth, bids, asks); time = updated; return true; } @@ -77,31 +133,25 @@ namespace mbts.co2 total_volume = _cold->total_volume; return (_cold->last_trade.load_if_new(ref time, out ps)); } - public ulong misc_timestamp { get { return _cold->misc_timestamp; } } + public ulong misc_timestamp { get { return _cold->misc_timestamp; } } - public MiscMessage get_misc() { - return new MiscMessage() { - SettlementPrice = _cold->settlement_price, - }; - } + public ulong total_volume { get { return _cold->total_volume; } } - public ulong total_volume { get { return _cold->total_volume; } } + public double settlement_price { get { return has_depth ? _hot_depth->settlement.price() : _hot->settlement.price(); } } + public DateTime settlement_date { get { return _cold->settlement_date; } } + public double previous_settlement_price { get { return _cold->previous_settlement_price.price(); } } + public DateTime previous_settlement_date { get { return _cold->previous_settlement_date; } } - public double settlement_price { get { return _cold->settlement_price;} } - public DateTime settlement_date { get { return _cold->settlement_date;} } - public double previous_settlement_price { get { return _cold->previous_settlement_price;} } - public DateTime previous_settlement_date { get { return _cold->previous_settlement_date;} } + public double open_price { get { return _cold->open_price; } } + public DateTime open_date { get { return _cold->open_date; } } - public double open_price { get { return _cold->open_price;} } - public DateTime open_date { get { return _cold->open_date; } } - - public double close_price { get { return _cold->close_price;} } - public DateTime close_date { get { return _cold->close_date; } } - public double session_high { get { return _cold->session_high;} } - public double session_low { get { return _cold->session_low; } } + public double close_price { get { return _cold->close_price; } } + public DateTime close_date { get { return _cold->close_date; } } + public double session_high { get { return _cold->session_high; } } + public double session_low { get { return _cold->session_low; } } // public double session_high_bid { get { return _cold->session_high_bid; } } // public double session_low_offer { get { return _cold->session_low_offer; } } - public double fixing_price { get { return _cold->fixing_price; } } + public double fixing_price { get { return _cold->fixing_price; } } // // Internal @@ -130,25 +180,25 @@ namespace mbts.co2 #region structures [StructLayout(LayoutKind.Sequential)] - internal struct HotEntry - { + internal struct HotEntry { // we don't actually need to decode timestamp. If it goes up data has changed public readonly TimeStamp timestamp; // 8 public readonly NativePriceLevel book; // 24 public readonly security_key key; // 4 public readonly UInt32 sequence; // 4 + public readonly IntPrice settlement; // 8 public readonly byte _status; // 1 public CoconutSecurityStatus status { get { return (CoconutSecurityStatus)_status; } } } [StructLayout(LayoutKind.Sequential)] - internal struct HotDepthEntry - { + internal struct HotDepthEntry { public readonly TimeStamp timestamp; // 8 public readonly security_key key; // 4 public readonly UInt32 sequence; // 4 - + public readonly IntPrice settlement; // 8 + // The ByValArray type would be more elegant but it does not allocate inline. // [MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)] @@ -164,48 +214,38 @@ namespace mbts.co2 private readonly NativePriceLevel direct8; private readonly NativePriceLevel direct9; // 2 implied levels - private readonly NativePriceLevel implied0; - private readonly NativePriceLevel implied1; + private readonly ImpliedPriceLevel implied0; + private readonly ImpliedPriceLevel implied1; private readonly byte _status; public CoconutSecurityStatus status { get { return (CoconutSecurityStatus)_status; } } - public NativePriceSize bid(int depth) { + public NativePriceLevel direct(uint depth) { switch (depth) { - case 0: return direct0.bid; - case 1: return direct1.bid; - case 2: return direct2.bid; - case 3: return direct3.bid; - case 4: return direct4.bid; - case 5: return direct5.bid; - case 6: return direct6.bid; - case 7: return direct7.bid; - case 8: return direct8.bid; - case 9: return direct9.bid; + case 0: return direct0; + case 1: return direct1; + case 2: return direct2; + case 3: return direct3; + case 4: return direct4; + case 5: return direct5; + case 6: return direct6; + case 7: return direct7; + case 8: return direct8; + case 9: return direct9; default: throw new ArgumentException("depth"); } } - public NativePriceSize ask(int depth) { + public ImpliedPriceLevel implied(uint depth) { switch (depth) { - case 0: return direct0.ask; - case 1: return direct1.ask; - case 2: return direct2.ask; - case 3: return direct3.ask; - case 4: return direct4.ask; - case 5: return direct5.ask; - case 6: return direct6.ask; - case 7: return direct7.ask; - case 8: return direct8.ask; - case 9: return direct9.ask; + case 0: return implied0; + case 1: return implied1; default: throw new ArgumentException("depth"); } } - } - [StructLayout(LayoutKind.Sequential, Size = 384)] - internal struct ColdEntry - { + [StructLayout(LayoutKind.Sequential, Size = 376)] + internal struct ColdEntry { public int exchange_status; // Last trade public AtomicPriceSize last_trade; @@ -214,9 +254,8 @@ namespace mbts.co2 //// Settlement public UInt64 misc_timestamp; - public double settlement_price; public utils.LLDate settlement_date; - public double previous_settlement_price; + public IntPrice previous_settlement_price; public utils.LLDate previous_settlement_date; //// Opening/closing @@ -240,8 +279,7 @@ namespace mbts.co2 } [StructLayout(LayoutKind.Sequential)] - internal struct ColdHeader - { + internal struct ColdHeader { internal uint version; internal ulong count; internal ulong segment_count; diff --git a/src/client/spark_core/Co2/NativePriceHelpers.cs b/src/client/spark_core/Co2/NativePriceHelpers.cs new file mode 100644 index 000000000..40fc131ed --- /dev/null +++ b/src/client/spark_core/Co2/NativePriceHelpers.cs @@ -0,0 +1,43 @@ +using bts; + +namespace mbts.co2 { + public static class NativePriceHelpers { + static double[] s_powers10 = { + 1E-20, 1E-19, 1E-18, 1E-17, 1E-16, 1E-15, 1E-14, 1E-13, 1E-12, 1E-11, 1E-10, 1E-9, 1E-8, 1E-7, 1E-6, 1E-5, 1E-4, 1E-3, 1E-2, 1E-1, + 1E0, + 1E1, 1E2, 1E3, 1E4, 1E5, 1E6, 1E7, 1E8, 1E9, 1E10, 1E11, 1E12, 1E13, 1E14, 1E15, 1E16, 1E17, 1E18, 1E19, 1E20 + }; + static int s_max_abs_pow10 = s_powers10.Length/2; + + public static double pow10(int power) { + int offset = power + s_max_abs_pow10; + Log.assert_d(offset >= 0 && offset < s_powers10.Length); + if (offset >= 0 && offset < s_powers10.Length) { + return s_powers10[offset]; + } + return double.NaN; + } + internal static long decode(long m) { + long mantissa = m & 0xFFFFFFFFFFFFFF; + // If the 56th bit is set this is negative + const long bit56 = 0x80000000000000; + if ((mantissa & bit56) != 0) { + // Compute 2's compliment manually + mantissa = -1 * (((~mantissa) & (bit56 - 1)) + 1); + } + return mantissa; + } + + public static double to_double(long mantissa, sbyte exponent) { + if (exponent == COCONUT_NAN_EXPONENT) { + return double.NaN; + } + return mantissa * pow10(exponent); + } + + public const uint COCONUT_MAX_DEPTH = 10; + public const uint COCONUT_MAX_IMPLIED_DEPTH = 2; + internal const sbyte COCONUT_NAN_EXPONENT = 127; + } + +} diff --git a/src/client/spark_core/Co2/PriceSize.cs b/src/client/spark_core/Co2/PriceSize.cs index 8f1e14e9f..3b91b089c 100644 --- a/src/client/spark_core/Co2/PriceSize.cs +++ b/src/client/spark_core/Co2/PriceSize.cs @@ -1,107 +1,110 @@ using bts; using System; -using System.Collections.Generic; -using System.Linq; using System.Runtime.InteropServices; -using System.Text; -using System.Threading; -using System.Threading.Tasks; -namespace mbts.co2 -{ +namespace mbts.co2 { - [StructLayout(LayoutKind.Explicit)] + [StructLayout(LayoutKind.Sequential)] public readonly struct NativePriceLevel { - [FieldOffset(0)] - readonly Int64 _mantissa_b; // : 56; - [FieldOffset(7)] - readonly sbyte _exponent_b; // : 8; - [FieldOffset(8)] - readonly Int64 _mantissa_a; // : 56; - [FieldOffset(15)] - readonly sbyte _exponent_a; //: 8; - [FieldOffset(16)] - readonly UInt32 _size_b; - [FieldOffset(20)] - readonly UInt32 _size_a; - - long decode(long m) { - long mantissa = m & 0xFFFFFFFFFFFFFF; - // If the 56th bit is set this is negative - const long bit56 = 0x80000000000000; - if ((mantissa & bit56) != 0) { - // Compute 2's compliment manually - mantissa = -1 * (((~mantissa) & (bit56 - 1)) + 1); - } - return mantissa; + readonly IntPrice _bid; + readonly IntPrice _ask; + readonly uint _bid_size; + readonly uint _ask_size; + public NativePriceLevel(IntPrice bid, uint bid_size, IntPrice ask, uint ask_size) { + _bid = bid; + _bid_size = bid_size; + _ask = ask; + _ask_size = ask_size; } - public Int64 bid_mantissa => decode(_mantissa_b); - public sbyte bid_exponent => _exponent_b; - public Int64 ask_mantissa => decode(_mantissa_a); - public sbyte ask_exponent => _exponent_a; - public UInt32 bid_size => _size_b; - public UInt32 ask_size => _size_a; + public NativePriceSize bid => new NativePriceSize(_bid, _bid_size); + public NativePriceSize ask => new NativePriceSize(_ask, _ask_size); - public NativePriceSize bid => new NativePriceSize(bid_mantissa, bid_exponent, bid_size); - public NativePriceSize ask => new NativePriceSize(ask_mantissa, ask_exponent, ask_size); - } + public void Deconstruct(out NativePriceSize bid, out NativePriceSize ask) { + bid = this.bid; + ask = this.ask; + } - public static class NativePriceSizeHelpers { - static double[] s_powers10 = { - 1E-20, 1E-19, 1E-18, 1E-17, 1E-16, 1E-15, 1E-14, 1E-13, 1E-12, 1E-11, 1E-10, 1E-9, 1E-8, 1E-7, 1E-6, 1E-5, 1E-4, 1E-3, 1E-2, 1E-1, - 1E0, - 1E1, 1E2, 1E3, 1E4, 1E5, 1E6, 1E7, 1E8, 1E9, 1E10, 1E11, 1E12, 1E13, 1E14, 1E15, 1E16, 1E17, 1E18, 1E19, 1E20 - }; - static int s_max_abs_pow10 = s_powers10.Length/2; - - public static double pow10(int power) { - int offset = power + s_max_abs_pow10; - Log.assert_d(offset >= 0 && offset < s_powers10.Length); - if (offset >= 0 && offset < s_powers10.Length) { - return s_powers10[offset]; - } - return double.NaN; + internal static NativePriceLevel empty => new NativePriceLevel(IntPrice.NaN, 0, IntPrice.NaN, 0); + public override string ToString() { + return $"b [{bid.price} {bid.size}] a [{ask.price} {ask.size}]"; + } + } + public readonly struct ImpliedPriceLevel { + readonly IntPrice _bid; + readonly IntPrice _ask; + readonly uint _bid_size; + readonly uint _ask_size; + public ImpliedPriceLevel(IntPrice bid, uint bid_size, IntPrice ask, uint ask_size) { + _bid = bid; + _bid_size = bid_size; + _ask = ask; + _ask_size = ask_size; + } + public NativePriceSize bid => new NativePriceSize(_bid, 0, _bid_size); + public NativePriceSize ask => new NativePriceSize(_ask, 0, _ask_size); + public void Deconstruct(out NativePriceSize bid, out NativePriceSize ask) { + bid = this.bid; + ask = this.ask; } } - public readonly struct NativePriceSize : IEquatable { - public NativePriceSize(Int64 mantissa, sbyte exponent, uint size) { - this.mantissa = mantissa; - this.exponent = exponent; - this.size = size; + readonly IntPrice _price; + readonly uint _size; + readonly uint _direct_size; + + static NativePriceSize() { + Log.assert(Marshal.SizeOf() == 16); } - public static NativePriceSize no_market = new NativePriceSize(0, 0, 0); + public NativePriceSize(IntPrice price, uint size) { + _price = price; + _size = size; + _direct_size = size; + } + public NativePriceSize(long mantissa, sbyte exponent, uint size) { + _price = new IntPrice(mantissa, exponent); + _size = size; + _direct_size = size; + } + public NativePriceSize(IntPrice price, uint direct_size, uint implied_size) { + _price = price; + _size = direct_size + implied_size; + _direct_size = direct_size; + } - public Int64 mantissa { get; } - public sbyte exponent {get;} + public static NativePriceSize NO_MARKET = new NativePriceSize(IntPrice.NaN, 0); - public double price => (is_nan) ? double.NaN : mantissa * NativePriceSizeHelpers.pow10(exponent); + public IntPrice int_price => _price; + public long mantissa => _price.mantissa; + public sbyte exponent => _price.exponent; + public uint size => _size; + public uint direct_size => _direct_size; + public uint implied_size => _size - _direct_size; + public double price => _price.price(); + public bool is_nan => _price.is_nan; - public bool is_nan => this == no_market; - - public uint size { get; } public override string ToString() { - return "{0:0.00} {1}".fmt(price, size); + return "{0} {1} ({2})".fmt(price, size, implied_size); } public override bool Equals(object obj) { if (obj is NativePriceSize p) return Equals(p); return base.Equals(obj); } + public override int GetHashCode() { - return bts.utils.EqualityComparer.aggregate_hash(mantissa.GetHashCode(), exponent.GetHashCode(), size.GetHashCode()); + return bts.utils.EqualityComparer.aggregate_hash(_price.GetHashCode(), size.GetHashCode(), _direct_size.GetHashCode()); } - public bool Equals(NativePriceSize other) { - return size == other.size && - mantissa == other.mantissa && - exponent == other.exponent; - } + public bool Equals(NativePriceSize other) => + size == other.size && + _direct_size == other._direct_size && + _price.Equals(other._price); + public static bool operator ==(NativePriceSize a, NativePriceSize b) { return a.Equals(b); } @@ -109,86 +112,5 @@ namespace mbts.co2 return !a.Equals(b); } } - - struct TimeStamp { - Int64 _value; - public UInt64 time => (UInt64)Interlocked.Read(ref _value); - public static UInt64 invalid_value => UInt64.MaxValue; - } - - // This type cannot be generic because we will get error CS0208 - //internal struct Optimistic where T : struct {... - internal struct AtomicPriceSize { - TimeStamp _time; - NativePriceSize _value; - void store(NativePriceSize nps) { - _time = new TimeStamp(); - _value = new NativePriceSize(); - throw new NotImplementedException(); - } - public (UInt64 time, NativePriceSize value) load() { - UInt64 temp; - NativePriceSize result; - int attempts = 0; - do { - ++attempts; - temp = _time.time; - result = _value; - } while ((temp != _time.time || temp == TimeStamp.invalid_value) && attempts < 1619200); - return (temp, result); - } - public bool load_if_new(ref UInt64 time, out NativePriceSize t) { - if (_time.time > time || _time.time == TimeStamp.invalid_value) { - (time, t) = load(); - return (time != TimeStamp.invalid_value); - } - t = NativePriceSize.no_market; - return false; - } - } - - public struct PriceSize : IEquatable - { - public PriceSize(double price, uint size) - { - this.price = price; - this.size = size; - } - - public PriceSize(NativePriceSize nps) { - price = nps.price; - size = nps.size; - } - - public static implicit operator PriceSize(NativePriceSize nps) { - return new PriceSize(nps); - } - - public static PriceSize no_market = NativePriceSize.no_market; - - public double price { get; } - public uint size { get; } - - public override string ToString() { - return "{0:0.00} {1}".fmt(price, size); - } - - public override bool Equals(object obj) { - if (obj is PriceSize p) return Equals(p); - return base.Equals(obj); - } - public override int GetHashCode() { - return bts.utils.EqualityComparer.aggregate_hash(price.GetHashCode(), size.GetHashCode()); - } - - public bool Equals(PriceSize other) { - return price == other.price && size == other.size; - } - public static bool operator ==(PriceSize a, PriceSize b) { - return a.Equals(b); - } - public static bool operator !=(PriceSize a, PriceSize b) { - return !a.Equals(b); - } - } + } diff --git a/src/client/spark_core/TestRunner/TestAssert.cs b/src/client/spark_core/TestRunner/TestAssert.cs index 9351f1d79..5bc09ae02 100644 --- a/src/client/spark_core/TestRunner/TestAssert.cs +++ b/src/client/spark_core/TestRunner/TestAssert.cs @@ -29,6 +29,11 @@ namespace bts.test { throw new AssertFailture($"Expected: 'false' got 'true'. {msg}", file, line); } } + public static void IsNaN(double value, string msg = "", [CallerFilePath] string file = "", [CallerLineNumber] int line = 0) { + if (!double.IsNaN(value)) { + throw new AssertFailture($"Expected: NaN got {value}", file, line); + } + } public static void AreEqual(T a, T b, string msg="", [CallerFilePath] string file="", [CallerLineNumber] int line=0) { if (!a.Equals(b)) { throw new AssertFailture( diff --git a/src/client/spark_core/TestRunner/TestResultReceiver.cs b/src/client/spark_core/TestRunner/TestResultReceiver.cs index 72a052c84..9063a0c59 100644 --- a/src/client/spark_core/TestRunner/TestResultReceiver.cs +++ b/src/client/spark_core/TestRunner/TestResultReceiver.cs @@ -50,7 +50,9 @@ namespace bts.test { var unused = wr.GetResponseAsync(); } + public void print_result(TestResult result) { + ResultCount += 1; if (_defer) { _deferred.Add(result); return; @@ -93,6 +95,10 @@ namespace bts.test { _fail++; ConsoleEx.Write("TIMEOUT", ConsoleColor.DarkYellow); break; + case TestResult.Types.Step.Crash: + _fail++; + ConsoleEx.Write("CRASH", ConsoleColor.Red); + break; } Console.WriteLine(); } @@ -112,6 +118,7 @@ namespace bts.test { } public int ErrorCount => _fail; + public int ResultCount { get; set; } public void Dispose() { _running = false; diff --git a/src/client/spark_core/TestRunner/TestRunner.cs b/src/client/spark_core/TestRunner/TestRunner.cs index be3f09bb4..9a1e6f436 100644 --- a/src/client/spark_core/TestRunner/TestRunner.cs +++ b/src/client/spark_core/TestRunner/TestRunner.cs @@ -21,6 +21,7 @@ namespace bts.test { var listening = trr.listen(); trr.print_begin(); foreach (var t in tests) { + int count = trr.ResultCount; var res = run_one(t, TimeSpan.FromSeconds(timeout)); if (res == TIMEOUT) { trr.print_result(new TestResult() { @@ -28,6 +29,15 @@ namespace bts.test { Status = TestResult.Types.Step.Timeout }); } + else { + // If we didn't time out and no results were reported we crashed or something + if (count == trr.ResultCount) { + trr.print_result(new TestResult() { + TestName = t, + Status = TestResult.Types.Step.Crash + }); + } + } } trr.print_summary(); return trr.ErrorCount; diff --git a/src/client/spark_core/utils/GlobalExtensions.cs b/src/client/spark_core/utils/GlobalExtensions.cs index 8a094fdb6..b872b32ae 100644 --- a/src/client/spark_core/utils/GlobalExtensions.cs +++ b/src/client/spark_core/utils/GlobalExtensions.cs @@ -1,4 +1,5 @@ -using System; +using bts; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -32,5 +33,45 @@ namespace mbts { const int kind_shift = 62; return new DateTime((long)(ll & ticks_mask), (DateTimeKind)(ll >> kind_shift)); } + + //this overloaded declaration, (as opposed to one function with comp = null) allows observable_collection.sync_with to be passed into functions as a parameterless Action + public static void sync_with(this IList dest, IEnumerable src) { dest.sync_with(src, EqualityComparer.Default); } + public static void sync_with(this IList dest, IEnumerable src, IEqualityComparer comp) { + Log.notnull(dest); + if (src == null) { + dest.Clear(); + return; + } + var srcList = src as IList ?? src.ToList(); + var changes = bts.utils.Diff.diff(dest, srcList, comp).ToList(); + // Apply the diff + for(int i = 0; i < changes.Count; ++i) { + var c = changes[i]; + // removes + for (var x = 0; x < c.deletedLeft; ++x) { + dest.RemoveAt(c.StartLeft); + } + // inserts + for (var x = 0; x < c.insertedRight; ++x) { + dest.Insert(c.StartLeft + x, srcList[c.StartRight + x]); + } + // Adjust start index of remaining changes + for (int ii = i + 1; ii < changes.Count; ++ii) { + var cc = changes[ii]; + var delta = c.insertedRight - c.deletedLeft; + if (cc.StartLeft >= c.StartLeft) { + cc.StartLeft += delta; + changes[ii] = cc;//remember: struct + } + } + } + } + + public static T? TryFirst(this IEnumerable src) where T : struct + { + foreach(var v in src) + return v; + return null; + } } } \ No newline at end of file diff --git a/src/client/spark_excel/rtd/formatting/BookMDFormatter.cs b/src/client/spark_excel/rtd/formatting/BookMDFormatter.cs index 613b6b759..b676e4877 100644 --- a/src/client/spark_excel/rtd/formatting/BookMDFormatter.cs +++ b/src/client/spark_excel/rtd/formatting/BookMDFormatter.cs @@ -24,19 +24,19 @@ namespace bts.excel.rtd for (int j = 0; j < cols; j++) ret[i, j] = string.Empty; - if (_update.Book != null) + if (_update.Direct != null) { int idx = 0; - for (int j = _update.Book.Asks.Count - 1; j >= 0 && idx < rows; j--) + for (int j = _update.Direct.Asks.Count - 1; j >= 0 && idx < rows; j--) { - var ask = _update.Book.Asks[j]; + var ask = _update.Direct.Asks[j]; ret[idx, 1] = format_md(ask.Mantissa, ask.Exponent); ret[idx, 2] = ask.Size; idx++; } - for (int j = 0; j < _update.Book.Bids.Count && idx < rows; j++) + for (int j = 0; j < _update.Direct.Bids.Count && idx < rows; j++) { - var bid = _update.Book.Bids[j]; + var bid = _update.Direct.Bids[j]; ret[idx, 1] = format_md(bid.Mantissa, bid.Exponent); ret[idx, 0] = bid.Size; idx++; diff --git a/src/client/spark_excel/rtd/formatting/TopMDFormatter.cs b/src/client/spark_excel/rtd/formatting/TopMDFormatter.cs index b3accfa97..5a8e65fb7 100644 --- a/src/client/spark_excel/rtd/formatting/TopMDFormatter.cs +++ b/src/client/spark_excel/rtd/formatting/TopMDFormatter.cs @@ -20,20 +20,20 @@ namespace bts.excel.rtd for (int j = 0; j < cols; j++) ret[i, j] = string.Empty; - if (_update.Book != null) + if (_update.Direct != null) { int idx = 0; - for (int j = 0; j < _update.Book.Asks.Count && idx < rows; j++) + for (int j = 0; j < _update.Direct.Asks.Count && idx < rows; j++) { - var ask = _update.Book.Asks[j]; + var ask = _update.Direct.Asks[j]; ret[idx, 2] = BookMDFormatter.format_md(ask.Mantissa, ask.Exponent); ret[idx, 3] = ask.Size; idx++; } idx = 0; - for (int j = 0; j < _update.Book.Bids.Count && idx < rows; j++) + for (int j = 0; j < _update.Direct.Bids.Count && idx < rows; j++) { - var bid = _update.Book.Bids[j]; + var bid = _update.Direct.Bids[j]; ret[idx, 1] = BookMDFormatter.format_md(bid.Mantissa, bid.Exponent); ret[idx, 0] = bid.Size; idx++; diff --git a/src/client/spark_test/Co2/Co2ImpliedTests.cs b/src/client/spark_test/Co2/Co2ImpliedTests.cs new file mode 100644 index 000000000..5b9b22cee --- /dev/null +++ b/src/client/spark_test/Co2/Co2ImpliedTests.cs @@ -0,0 +1,18 @@ +using bts.test; +using mbts.co2; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.InteropServices; +using System.Text; +using System.Threading.Tasks; + +namespace bts.test { + + [TestGroup] + class Co2ImpliedTests { + [Test] + public void co2_test_implied_merge() { + } + } +} diff --git a/src/client/spark_test/Co2/Co2Tests.cs b/src/client/spark_test/Co2/Co2Tests.cs index 3fba67d3d..a5f1b292a 100644 --- a/src/client/spark_test/Co2/Co2Tests.cs +++ b/src/client/spark_test/Co2/Co2Tests.cs @@ -43,9 +43,15 @@ namespace mbts.co2.tests Assert.AreEqual(ps.price, 2030); Assert.IsTrue(ps.size == 1); - ps = NativePriceSize.no_market; + ps = NativePriceSize.NO_MARKET; Assert.IsTrue(double.IsNaN(ps.price)); Assert.IsTrue(ps.size == 0); + + ps = new NativePriceSize(100, 100, 100); + Co2NativeTestMethods.co2_get_nan_price_size(out ps); + Assert.IsTrue(double.IsNaN(ps.price)); + Assert.AreEqual(ps.size, 0u); + Assert.AreEqual(ps.exponent, NativePriceSize.NO_MARKET.exponent); } [Test] @@ -66,6 +72,7 @@ namespace mbts.co2.tests Assert.IsTrue(entry.status == CoconutSecurityStatus.PacketLoss); Assert.IsTrue(entry.key == 1234); Assert.IsTrue(entry.sequence == 777); + Assert.AreEqual(entry.settlement.price(), 98.6); } string fmt(double a) { @@ -80,13 +87,19 @@ namespace mbts.co2.tests Assert.IsTrue(entry_size == (ulong)Marshal.SizeOf(entry)); Assert.IsTrue(entry.status == CoconutSecurityStatus.PacketLoss); Assert.IsTrue(entry.sequence == 888); - for (int x = 0; x < 10; ++x) { - var bid = entry.bid(x); + Assert.AreEqual(entry.settlement.price(), 98.6); + for (uint x = 0; x < NativePriceHelpers.COCONUT_MAX_DEPTH; ++x) { + (var bid, var ask) = entry.direct(x); Assert.IsTrue(bid.price == x + 0.5); Assert.IsTrue(bid.size == x + 1); - var ask = entry.ask(x); Assert.AreEqual(fmt(ask.price), fmt(x+0.6)); - //Assert.IsTrue(ask.price == x + 0.6); + Assert.IsTrue(ask.size == x + 2); + } + for (uint x = 0; x < NativePriceHelpers.COCONUT_MAX_IMPLIED_DEPTH; ++x) { + (var bid, var ask) = entry.implied(x); + Assert.AreEqual(77, (bid.price)); + Assert.IsTrue(bid.size == x + 1); + Assert.AreEqual(88, ask.price); Assert.IsTrue(ask.size == x + 2); } } @@ -114,9 +127,8 @@ namespace mbts.co2.tests Assert.IsTrue(entry.total_volume == 12345); Assert.IsTrue(compare_date(entry.last_trade_time, DateTime.UtcNow)); //// Settlement - Assert.AreEqual(entry.settlement_price, 3.3); Assert.IsTrue(entry.settlement_date == new DateTime(2015, 9, 3)); - Assert.AreEqual(entry.previous_settlement_price, 3.3); + Assert.AreEqual(entry.previous_settlement_price.price(), 3.3); Assert.IsTrue(entry.previous_settlement_date == new DateTime(2015, 9, 2)); //// Open/close Assert.IsTrue(entry.open_price == 4.4); @@ -150,21 +162,19 @@ namespace mbts.co2.tests Assert.IsTrue(header.gap_count == 104); } - [Test,Ignore("")] + [Test] public void enumerate_securities() { - // config.Configuration.initialize_for_test(); - IntPtr test_md = Co2NativeTestMethods.co2_produce_test_md(); - using (Disposable.Create(() => Co2NativeTestMethods.co2_destroy_test_md(test_md))) { + using (var test_md = Co2NativeTestMethods.produce_test_md()) { using (var consumer = new MDConsumer(new[] { - new UserSymbol("FOOM", Venue.Unset, SecurityType.Future), - new UserSymbol("FOOXM", Venue.Unset, SecurityType.Option) + new UserSymbol("FOOM", Venue.Bts, SecurityType.Future), + new UserSymbol("FOOXM", Venue.Bts, SecurityType.Option), }, false)) { var securities = consumer.all_securities().ToList(); foreach (var s in securities) { Assert.IsTrue(s.security.Id == s.handle.key); //Assert.IsTrue(s.security.numeric_id == s.handle.numeric_id); } - Assert.IsTrue(securities.Count() == 24); + Assert.AreEqual(securities.Count(), 24); var dict = securities.Select(s => s.security).GroupBy(s => s.Symbol).ToDictionary(group => group.Key, group => group.ToList()); Assert.IsTrue(dict.Count == 2); Assert.IsTrue(dict.All(kvp => kvp.Value.Count == 12)); @@ -172,16 +182,14 @@ namespace mbts.co2.tests } } - [Test,Ignore("")] + [Test] public void on_securities() { - config.Configuration.initialize_for_test(); - IntPtr test_md = Co2NativeTestMethods.co2_produce_test_md(); - using (Disposable.Create(() => Co2NativeTestMethods.co2_destroy_test_md(test_md))) { + using (var test_md = Co2NativeTestMethods.produce_test_md()) { using (var consumer = new MDConsumer(new[] { - new UserSymbol("FOOM", Venue.Unset, SecurityType.Future), - new UserSymbol("FOOXM", Venue.Unset, SecurityType.Option) + new UserSymbol("FOOM", Venue.Bts, SecurityType.Future), + new UserSymbol("FOOXM", Venue.Bts, SecurityType.Option) }, false)) { - var securities = new List(); + var securities = new List(); consumer.on_security.Subscribe(secs => { foreach (var s in secs) { Assert.IsTrue(s.security.Id == s.handle.key); @@ -197,11 +205,10 @@ namespace mbts.co2.tests foreach (var sec in securities) { var handle = consumer.try_get_handle(sec.Id); Assert.IsNotNull(handle); - Assert.IsTrue(handle.get_security().Id == sec.Id); Assert.AreEqual(sec.Id, handle.key); Assert.IsTrue(handle.market_timestamp > 0); ulong time = 0; - NativePriceSize bid,ask; + NativePriceSize bid, ask; handle.get_bbo(ref time, out bid, out ask); Assert.IsTrue(bid.price == 1.1); Assert.IsTrue(bid.size == 101); @@ -217,7 +224,7 @@ namespace mbts.co2.tests Assert.IsTrue(trade_date.Year == 2015 && trade_date.Month == 9 && trade_date.Day == 22); Assert.IsTrue(total_volume == 4200); Assert.IsTrue(handle.settlement_date.Year == 2000 && handle.settlement_date.Day == 9 && handle.settlement_date.Month == 8); - Assert.IsTrue(handle.settlement_price == 6.6); + Assert.AreEqual(handle.settlement_price, 6.6); Assert.IsTrue(handle.open_price == 7.7); Assert.IsTrue(handle.open_date.Year == 2001 && handle.open_date.Month == 1 && handle.open_date.Day == 2); Assert.IsTrue(handle.close_price == 8.8); @@ -235,20 +242,28 @@ namespace mbts.co2.tests [Test] public void price_size_nan() { - NativePriceLevel npl = new NativePriceLevel(); + NativePriceLevel npl = NativePriceLevel.empty; Assert.IsTrue(npl.bid.is_nan); - Assert.IsTrue(double.IsNaN(npl.bid.price)); + Assert.IsNaN(npl.bid.price); Assert.IsTrue(npl.ask.is_nan); - Assert.IsTrue(double.IsNaN(npl.ask.price)); + Assert.IsNaN(npl.ask.price); Co2NativeTestMethods.co2_get_zero_and_nan_book(ref npl); Assert.AreEqual(npl.ask.price, 0); Assert.AreEqual(npl.ask.size, 1u); Assert.AreEqual(npl.bid.size, 0u); Assert.IsTrue(npl.bid.is_nan); - Assert.IsTrue(double.IsNaN(npl.bid.price)); + Assert.IsNaN(npl.bid.price); } + [Test] + public void co2_constants_test() { + Co2NativeTestMethods.co2_test_get_constants(out ulong max_depth, out ulong max_implied_depth, out sbyte nan); + Assert.AreEqual(max_depth, NativePriceHelpers.COCONUT_MAX_DEPTH); + Assert.AreEqual(max_implied_depth, NativePriceHelpers.COCONUT_MAX_IMPLIED_DEPTH); + Assert.AreEqual(nan, NativePriceHelpers.COCONUT_NAN_EXPONENT); + } + [Test] public void lldatetime_test() { utils.LLDateTime now = DateTime.MinValue, utc_now = DateTime.MinValue; @@ -269,5 +284,7 @@ namespace mbts.co2.tests assert_about_equal(dt_utc_now.Minute, DateTime.UtcNow.Minute); assert_about_equal(dt_utc_now.Second, DateTime.UtcNow.Second); } + + } } diff --git a/src/client/spark/Tests/mcore/SyncronizeCollectionTests.cs b/src/client/spark_test/Collections/SyncronizeCollectionTests.cs similarity index 96% rename from src/client/spark/Tests/mcore/SyncronizeCollectionTests.cs rename to src/client/spark_test/Collections/SyncronizeCollectionTests.cs index 5eb1d2d79..7389f39e0 100644 --- a/src/client/spark/Tests/mcore/SyncronizeCollectionTests.cs +++ b/src/client/spark_test/Collections/SyncronizeCollectionTests.cs @@ -1,5 +1,4 @@ -using NUnit.Framework; -using System; +using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Collections.Specialized; @@ -8,10 +7,11 @@ using System.Text; using System.Threading.Tasks; using mbts; using System.Threading; +using bts.test; namespace bts.mcore { - [TestFixture] + [TestGroup] class SyncronizeCollectionTests { private void swap(List list, int a, int b) { diff --git a/src/client/spark/Tests/mcore/SecurityTests.cs b/src/client/spark_test/Security/SecurityTests.cs similarity index 73% rename from src/client/spark/Tests/mcore/SecurityTests.cs rename to src/client/spark_test/Security/SecurityTests.cs index 041b0f644..63104702a 100644 --- a/src/client/spark/Tests/mcore/SecurityTests.cs +++ b/src/client/spark_test/Security/SecurityTests.cs @@ -1,14 +1,14 @@ -using NUnit.Framework; -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using bts.api; +using bts.test; namespace bts.mcore { - [TestFixture] + [TestGroup] class SecurityTests { [Test] public void test_security_equal() { @@ -16,10 +16,10 @@ namespace bts.mcore SecurityImmutable b = new Security() { Id = 42 }; SecurityImmutable c = new Security() { Id = 4 }; - Assert.True(a == b); - Assert.True(a.Equals(b)); - Assert.False(a == c); - Assert.False(a.Equals(c)); + Assert.IsTrue(a == b); + Assert.IsTrue(a.Equals(b)); + Assert.IsFalse(a == c); + Assert.IsFalse(a.Equals(c)); Dictionary dict = new Dictionary(); dict[a] = 1; diff --git a/src/client/spark/Tests/MakePublic.cs b/src/client/spark_test/Utils/MakePublic.cs similarity index 100% rename from src/client/spark/Tests/MakePublic.cs rename to src/client/spark_test/Utils/MakePublic.cs diff --git a/src/client/spark_test/api/ApiUpdateTests.cs b/src/client/spark_test/api/ApiUpdateTests.cs index dd65b8edb..791d9a773 100644 --- a/src/client/spark_test/api/ApiUpdateTests.cs +++ b/src/client/spark_test/api/ApiUpdateTests.cs @@ -6,6 +6,7 @@ using System.Reactive.Linq; using System.Threading.Tasks; using System.Linq; using System.Reactive.Concurrency; +using System.Reactive.Subjects; namespace mbts.api.Test { [TestGroup] @@ -104,6 +105,121 @@ namespace mbts.api.Test { } + void validate_filter(IObservable> raw, IObservable> filtered, Func 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(); + 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)); + } + + // Do it again with TableUpdate + 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)); + Assert.That(next.removed.All(pred)); + Assert.That(next.changed.All(pred)); + } + } + + + void validate_filter_sub(IObservable> raw, IObservable> filtered, BehaviorSubject> 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.changed.All(pred.Value)); + + Dictionary rawd = tup.Item1.added.ToDictionary(x => x.id); + Dictionary fd = tup.Item2.added.ToDictionary(x => x.id); + foreach (var kv in rawd) + { + if(pred.Value(kv.Value)) + Assert.That(fd.ContainsKey(kv.Key)); + else + Assert.That(!fd.ContainsKey(kv.Key)); + } + }); + } + + + [Test] + public void test_filter() + { + var raw = Observable.Create>(observer => + { + RawUpdate rup = new RawUpdate(); + for (int x = 0; x < 50; ++x) + { + rup.Clear(); + var item = _item_pool[x]; + rup.added_items.Add(item); + rup.added_keys.Add(item.id); + observer.OnNext(rup); + } + var rand = new Random(); + 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)) + { + var ch = new Item(_item_pool[c].id, _item_pool[c].name + "_change"); + rup.changed_items.Add(ch); + rup.changed_keys.Add(ch.id); + } + observer.OnNext(rup); + + observer.OnCompleted(); + return Disposable.empty; + }); + + Func evens = (x => x.id % 2 == 0); + Func odds = (x => x.id % 2 == 1); + var predeven = Observable.Create>(observer => + { + observer.OnNext(evens); + observer.OnCompleted(); + return Disposable.empty; + }); + + var filtered = raw.reconstitute(x=> x.id).Filter(predeven); + validate_filter(raw, filtered, evens); + + var predodd = Observable.Create>(observer => + { + observer.OnNext(odds); + observer.OnCompleted(); + return Disposable.empty; + }); + + filtered = raw.reconstitute(x => x.id).Filter(predodd); + validate_filter(raw, filtered, odds); + + + + var sub= new BehaviorSubject>(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() { // Add 1 item 10x diff --git a/src/lib/api/services/ApiUnitTestService.proto b/src/lib/api/services/ApiUnitTestService.proto index a711ba570..a5e56e044 100644 --- a/src/lib/api/services/ApiUnitTestService.proto +++ b/src/lib/api/services/ApiUnitTestService.proto @@ -48,7 +48,8 @@ message TestResult { TEARDOWN = 2; PASS = 3; TIMEOUT = 4; - IGNORED = 5; + CRASH = 5; + IGNORED = 6; } string test_name = 1; string message = 3; diff --git a/src/lib/api/services/MarketDataAuxiliaryService.proto b/src/lib/api/services/MarketDataAuxiliaryService.proto index 74f3938dd..559f4f3f4 100644 --- a/src/lib/api/services/MarketDataAuxiliaryService.proto +++ b/src/lib/api/services/MarketDataAuxiliaryService.proto @@ -9,9 +9,12 @@ message VolumeAtPriceLevel{ uint32 security_id = 20; double price = 30; uint32 volume = 40; + uint32 buy_volume = 50; + uint32 sell_volume = 60; + uint32 passive_volume = 70; } service MarketDataAuxiliaryService{ option (bts.api.service_description)="Provides non-core supplementary market data"; rpc volume_at_price(bts.api.VoidMessage) returns (stream VolumeAtPriceLevel); -} \ No newline at end of file +} diff --git a/src/lib/config/defaults/MarketData.conf b/src/lib/config/defaults/MarketData.conf index 20357897b..17d800d6b 100644 --- a/src/lib/config/defaults/MarketData.conf +++ b/src/lib/config/defaults/MarketData.conf @@ -22,29 +22,16 @@ PublisherNamespace { CMEOptions { value { feed_engine { - name: "default" + name: "mdp3feed" # udp_socket_buffer_size: 425984 # channels_per_thread: 1 spin_time: 0 } channel { id: "*" - feed_engine: "default" + feed_engine: "mdp3feed" use_tcp_replay: false } } } -CMEOptions { - rule: "'MDP3_LL' = System.qualifier" - value { - feed_engine { - name: "driver" - #num_threads: 2 - } - channel { - id: "310" - feed_engine: "driver" - } - } -} diff --git a/src/lib/config/defaults/threads.conf b/src/lib/config/defaults/threads.conf index 02c0d6f44..59db51890 100644 --- a/src/lib/config/defaults/threads.conf +++ b/src/lib/config/defaults/threads.conf @@ -1,7 +1,8 @@ Threads { value { - thread_group {name_rx: "mdp3feed" core: 0} - thread_group {name_rx: "oman_orders" core: 1} - thread_group {name_rx: ".*" exclude:true core:0 core:1} + # thread_group {name_rx: "mdp3feed" core: 0} + # thread_group {name_rx: "oman_orders" core: 1} + # thread_group {name_rx: ".*" exclude:true core:0 core:1} + thread_group {name_rx: ".*" } } } \ No newline at end of file diff --git a/src/lib/config/protos/ExchangeSimCfg.proto b/src/lib/config/protos/ExchangeSimCfg.proto index 790ef5779..6cf1f7dc1 100644 --- a/src/lib/config/protos/ExchangeSimCfg.proto +++ b/src/lib/config/protos/ExchangeSimCfg.proto @@ -3,5 +3,11 @@ package bts.config; message ExchangeSim { optional bool merge_with_md = 1 [default=true]; + optional bool forward_implied = 2 [default = false]; + oneof source { + string replay_url = 11 [default = "http://mdreplay.bluetradesys.local/spark/futures.coconut"]; + string replay_file = 12 [default = "futures.coconut"]; + bool local_relay = 13; + } } diff --git a/src/lib/config/protos/MarketData.proto b/src/lib/config/protos/MarketData.proto index df6c5f924..d5ed6cc8b 100755 --- a/src/lib/config/protos/MarketData.proto +++ b/src/lib/config/protos/MarketData.proto @@ -5,6 +5,7 @@ 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]; } message PlatformMap @@ -78,10 +79,6 @@ message CMEOptions optional string thread_group = 10 [default=""]; // if present this will be used to retrieve affinity settings from Threads config key optional uint32 udp_socket_buffer_size = 11 [default = 33554432]; //32mb } - enum BookType { - BOOKTYPE_CONSOLIDATED = 0; - BOOKTYPE_DIRECT = 1; - } message CMEChannel { repeated string id = 2; optional string feed_engine = 3 [default = "default"]; @@ -104,7 +101,8 @@ message CMEOptions optional bool join_snapshot_on_gap = 32 [default = false]; optional bool load_security_db = 33 [default = true]; - optional BookType book_type = 34 [default = BOOKTYPE_CONSOLIDATED]; + reserved 34; + // optional BookType book_type = 34 [default = BOOKTYPE_CONSOLIDATED]; } optional string onixs_license = 1 [default = "{default}"]; @@ -147,14 +145,6 @@ message CMETradingSchedule { optional double gen_open_hour = 1 [default = 8.5]; // 8:30 am CT } -message SimSource { - oneof source { - string replay_url = 1 [default = "http://mdreplay.bluetradesys.local/spark/futures.coconut"]; - string replay_file = 2 [default = "futures.coconut"]; - bool local_relay = 3; - } -} - message SecurityExpirationCutoff { optional uint32 max_days_after_expiration =1 [default = 7]; } diff --git a/src/lib/config/protos/orders_fills_cleanup.proto b/src/lib/config/protos/orders_fills_cleanup.proto index 074b51c52..9c0f5815a 100644 --- a/src/lib/config/protos/orders_fills_cleanup.proto +++ b/src/lib/config/protos/orders_fills_cleanup.proto @@ -7,6 +7,6 @@ message OrdersSoftCleanupCutoff{ message SODTime{ // utc time - optional uint32 sod_hh = 20 [default=21]; + optional uint32 sod_hh = 20 [default=15]; optional uint32 sod_mm = 30 [default=30]; } \ No newline at end of file diff --git a/src/lib/logging/logging.cpp b/src/lib/logging/logging.cpp index a415b896d..3c7e1bffd 100644 --- a/src/lib/logging/logging.cpp +++ b/src/lib/logging/logging.cpp @@ -213,7 +213,9 @@ const char* intern(const char* str) { extern "C" BTS_EXPORT void Logger_log (int level, int subsystem, const char *message, const char *file, int line) { - bts::log::_thread_logger.logger->enqueue(log::log_ts(),log::Subsystem{(uint8_t)subsystem}, log::Level{(uint8_t)level}, intern(file), line, "{}", std::string(message)); + if (bts::log::_thread_logger.logger != nullptr) { + bts::log::_thread_logger.logger->enqueue(log::log_ts(), log::Subsystem{ (uint8_t)subsystem }, log::Level{ (uint8_t)level }, intern(file), line, "{}", std::string(message)); + } } } #endif diff --git a/src/lib/md/Diag/MDPriceLantencyTester.cpp b/src/lib/md/Diag/MDPriceLantencyTester.cpp index 0067cfebe..c15319110 100644 --- a/src/lib/md/Diag/MDPriceLantencyTester.cpp +++ b/src/lib/md/Diag/MDPriceLantencyTester.cpp @@ -39,7 +39,7 @@ void run_md_price_latency_test(UserSymbol symbol) { while (true) { for(auto& entry : handles) { PriceSize bid, ask; - if (entry.handle.get_bbo(entry.time, bid, ask) == HandleReadResult::SUCCESS) { + if (entry.handle.get_direct_book(entry.time, 1, &bid, &ask) == HandleReadResult::SUCCESS) { auto bid_price = bid.price(); auto ask_price = ask.price(); if (bid_price != entry.bid || ask_price != entry.ask) { diff --git a/src/lib/md/Diag/ShmDumper.cpp b/src/lib/md/Diag/ShmDumper.cpp index 1caa319d0..914f58897 100644 --- a/src/lib/md/Diag/ShmDumper.cpp +++ b/src/lib/md/Diag/ShmDumper.cpp @@ -25,6 +25,11 @@ double dump(TimeStamp time) { return (TimeStamp::now() - time).seconds(); } +string dump(IntPrice price) { + stringstream res; + res << std::setprecision(11) << price.price(); + return res.str(); +} string dump(PriceSize ps) { stringstream res; res << "{"; @@ -144,9 +149,8 @@ string dump_entry(const ColdEntry& entry) { if (has_value(entry.last_trade)) res << "'last_trade': " << dump(entry.last_trade) << ", "; if (entry.total_volume > 0) res << "'total_volume': " << entry.total_volume << ", "; if (entry.last_trade_time > 0) res << "'last_trade_time_ll': " << entry.last_trade_time << ", "; - if (!os::is_nan(entry.misc.settlement_price)) res << "'settlement_price': " << entry.misc.settlement_price << ", "; if (entry.misc.settlement_date.year > 0) res << "'settlement_date': " << dump(entry.misc.settlement_date) << ", "; - if (!os::is_nan(entry.misc.previous_settlement_price)) res << "'previous_settlement_price': " << entry.misc.previous_settlement_price << ", "; + if (!(entry.misc.previous_settlement_price.is_nan())) res << "'previous_settlement_price': " << entry.misc.previous_settlement_price.price() << ", "; if (entry.misc.previous_settlement_date.year > 0) res << "'previous_settlement_date': " << dump(entry.misc.previous_settlement_date) << ", "; res << "'deleted': " << std::boolalpha << entry.deleted << " "; res << "}"; @@ -159,18 +163,31 @@ string dump_entry(const HotDepthEntry& entry) { res << "'timestamp': " << dump(entry.time()) << ", "; res << "'key': " << entry.key << ", "; - res << "'bids': ["; - for (uint32_t x = 0; x < COCONUT_MAX_DEPTH; ++x) { - res << dump(entry.at(x)->bid()) << ", "; + res << "\n'bid': ["; + for (const auto& level : entry.direct) { + res << dump(level.bid()) << ", "; } res << "], "; - res << "'asks': ["; - for (uint32_t x = 0; x < COCONUT_MAX_DEPTH; ++x) { - res << dump(entry.at(x)->ask()) << ", "; + res << "\n'ask': ["; + for (const auto& level : entry.direct) { + res << dump(level.ask()) << ", "; + } + res << "], "; + + res << "'\nimplied_bid': ["; + for (const auto& level : entry.implied) { + res << dump(level.bid()) << ", "; } res << "], "; + res << "'\nimplied_ask': ["; + for (const auto& level : entry.implied) { + res << dump(level.ask()) << ", "; + } + res << "], "; + + res << "\n'settlement': '" << dump(entry.settlement) << "', "; res << "'status': '" << CoconutSecurityStatus_Name((CoconutSecurityStatus)entry.status) << "', "; res << "'sequence': '" << entry.sequence << "', "; res << " }"; diff --git a/src/lib/md/IntPrice.h b/src/lib/md/IntPrice.h new file mode 100644 index 000000000..c3c6c3573 --- /dev/null +++ b/src/lib/md/IntPrice.h @@ -0,0 +1,107 @@ +#pragma once +#include "lib/utils/bts_assert.h" +#include "lib/utils/bts_array_size.h" + +namespace bts { +namespace md { + +static const double powers_10[] = { + 1E-20, 1E-19, 1E-18, 1E-17, 1E-16, 1E-15, 1E-14, 1E-13, 1E-12, 1E-11, 1E-10, 1E-9, 1E-8, 1E-7, 1E-6, 1E-5, 1E-4, 1E-3, 1E-2, 1E-1, + 1E0, + 1E1, 1E2, 1E3, 1E4, 1E5, 1E6, 1E7, 1E8, 1E9, 1E10, 1E11, 1E12, 1E13, 1E14, 1E15, 1E16, 1E17, 1E18, 1E19, 1E20 +}; +static const int max_abs_power_10 = BTS_ARRAYSIZE(powers_10) / 2; +inline double pow10(int power) { + const int offset = power + max_abs_power_10; + const int powers_10_sz = BTS_ARRAYSIZE(powers_10); + bts_assert_d(offset >= 0 && offset < powers_10_sz); + if (offset >= 0 && offset < powers_10_sz) { + return powers_10[offset]; + } + return std::numeric_limits().quiet_NaN(); +} + +class IntPrice { +#ifdef _WIN32 + int64_t _mantissa : 56; + int64_t _exponent : 8; +#else + int64_t _mantissa : 56; + int8_t _exponent : 8; +#endif + public: + IntPrice() : _mantissa(0), _exponent(127) {/*nan*/} + IntPrice(int64_t mantissa, int8_t exponent) : _mantissa(mantissa), _exponent(exponent) {} + IntPrice(const IntPrice& other) : _mantissa(other._mantissa), _exponent(other._exponent) {} + + bool operator==(const IntPrice& other) const { + // this requires both mantissa and exponent to be equal + // logically 100*pow10(1) == 10*pow10(2) + // HOWEVER, once converted to double there are subtelties that can make them not equal so + // this function is conservative + if(_exponent == other._exponent) + return _mantissa == other._mantissa; + else + return price()==other.price(); + } + bool operator!=(const IntPrice& other) const { + return !(*this == other); + } + bool operator<(const IntPrice& other) const { + if (_exponent == other._exponent) { + return _mantissa < other._mantissa; + } + else { + return price() < other.price(); + } + } + IntPrice operator - (const IntPrice& o) const { + if(_exponent==o._exponent) { + return IntPrice(_mantissa-o.mantissa(), _exponent); + } + else { + // not sure there's ever a cause for a positive exponent unless we get into scientific computing + if (is_nan() || o.is_nan()) return IntPrice(); + bts_assert_d(o.exponent() <= 0 && _exponent <= 0); + if (o.exponent() < _exponent) { + int8_t delta = _exponent - o.exponent(); + auto m = _mantissa * pow10(delta); + return IntPrice(m - o.mantissa(), o.exponent()); + } + else { + int8_t delta = o.exponent() - _exponent; + auto m = o.mantissa() * pow10(delta); + return IntPrice(_mantissa - m, _exponent); + } + } + } + int64_t mantissa() const { return _mantissa; } + int8_t exponent() const { return _exponent; } + double price() const { + if (is_nan()) return NO_PRICE; + return mantissa() * pow10(exponent()); + } + bool is_nan() const { return _exponent == 127; } +}; +static_assert(sizeof(IntPrice) == 8, "IntPrice should be this big |--------|"); +extern IntPrice INT_NAN; + +template +IntPrice IntPrice_from_double(double price) { + if (os::is_nan(price)) return INT_NAN; + double temp; + if (fabs(modf(price, &temp)) != 0.0) { + const double rounding = 0.5 * pow10(-PRECISION); + price += (price > 0) ? rounding : -rounding; + price *= pow10(PRECISION); + return {(int64_t)price, -PRECISION}; + } + return {(int64_t)price, 0}; +} + +inline std::ostream& operator<<(std::ostream& os, const IntPrice& p) { + os << p.price(); + return os; +} + +}} \ No newline at end of file diff --git a/src/lib/md/MDConsumer.cpp b/src/lib/md/MDConsumer.cpp index b534058ec..dba9eda76 100644 --- a/src/lib/md/MDConsumer.cpp +++ b/src/lib/md/MDConsumer.cpp @@ -419,6 +419,7 @@ private: auto iter = _data.find(m.security_key()); if (iter != _data.end()) { iter->second->strategy.CopyFrom(m.strategy()); + iter->second->updated = TimeStamp::now(); } } @@ -476,6 +477,7 @@ private: s->security.CopyFrom(*strategy.security); s->strategy.CopyFrom(strategy.strategy); s->legs = legs; + s->updated = TimeStamp::now(); result.insert( make_pair(strategy.security->id(),s)); } diff --git a/src/lib/md/MDConsumerHandle.cpp b/src/lib/md/MDConsumerHandle.cpp index 0b82746bf..a078dbef1 100644 --- a/src/lib/md/MDConsumerHandle.cpp +++ b/src/lib/md/MDConsumerHandle.cpp @@ -1,10 +1,13 @@ #ifndef NO_CPP0X #include "MDConsumerHandle.h" -#include "lib/utils/sane_algorithm.h" #include "lib/domain_model/Security.h" +#include #define never bts_assert(false) +template +using small_vector = boost::container::small_vector; + namespace bts { namespace md { @@ -122,8 +125,7 @@ struct DepthTraversal { } }; - -void copy_depth(size_t count, api::Side side, const api::ContractSpecification& spec, std::vector& leg_travs, Level* out_price) { +void copy_depth(size_t count, api::Side side, const api::ContractSpecification& spec, small_vector& leg_travs, Level* out_price) { size_t out_index = 0; bool first = true; while(out_index < count) { @@ -165,16 +167,40 @@ void copy_depth(size_t count, api::Side side, const api::ContractSpecification& } TimeStamp copy_depth(const SyntheticStrategy& synth, size_t count, PriceSize* bid, PriceSize* ask) { - std::vector bid_leg_travs; - std::vector ask_leg_travs; + small_vector bid_leg_travs; + small_vector ask_leg_travs; bid_leg_travs.reserve(synth.legs.size()); ask_leg_travs.reserve(synth.legs.size()); TimeStamp rv = std::numeric_limits::min(); for (size_t x = 0; x < synth.legs.size(); ++x) { std::array bids, asks; TimeStamp t; - HandleReadResult r = synth.legs[x].get_depth(t, count, &bids[0], &asks[0]); + 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; + + auto mdtype = leg_params.market_data_type(); + HandleReadResult r; + if (mdtype == spark::LegSpreaderParams::DIRECT) { + r = synth.legs[x].get_direct_book(t, count, &bids[0], &asks[0]); + } + else { + r = synth.legs[x].get_book(t, count, &bids[0], &asks[0]); + } if (r == HandleReadResult::BUSY || t == TimeStamp::invalid()) return TimeStamp::invalid(); + + // NET Change + if (leg_params.leg_md_calc() == spark::LegSpreaderParams::NET_CHANGE) { + auto settle = synth.legs[x].settlement_price(); + for (size_t d = 0; d < COCONUT_MAX_DEPTH; ++d) { + bids[d] = bids[d] - settle; + asks[d] = asks[d] - settle; + } + } + rv = std::max(rv, t); const auto& leg = synth.security.synthetic().legs(x); const auto& eff_bid = leg.side() == api::SIDE_BUY ? bids : asks; @@ -186,12 +212,12 @@ TimeStamp copy_depth(const SyntheticStrategy& synth, size_t count, PriceSize* bi const auto& spec = synth.security.contract_spec(); copy_depth(count, api::SIDE_BUY, spec, bid_leg_travs, scratch.data()); for (size_t x = 0; x < count; ++x) { - bid[x] = PriceSize::from_double<7>(scratch[x].price, scratch[x].size); + bid[x] = PriceSize_from_double<7>(scratch[x].price, scratch[x].size); } memset(scratch.data(), 0, sizeof(scratch)); copy_depth(count, api::SIDE_SELL, spec, ask_leg_travs, scratch.data()); for (size_t x = 0; x < count; ++x) { - ask[x] = PriceSize::from_double<7>(scratch[x].price, scratch[x].size); + ask[x] = PriceSize_from_double<7>(scratch[x].price, scratch[x].size); } return rv; } @@ -200,20 +226,6 @@ TimeStamp copy_depth(const SyntheticStrategy& synth, size_t count, PriceSize* bi // END SYNTHETIC DEPTH ///////////////////////////// -TimeStamp MDConsumerHandle::last_market_update() const { - switch(_type) { - case HANDLETYPE_VOID: - return TimeStamp::invalid(); - case HANDLETYPE_DEPTH: - return book.hot->time(); - case HANDLETYPE_BBO: - return bbo.hot->time(); - case HANDLETYPE_SYNTHETIC: - return sane::max(synthetic.def->legs, [](const MDConsumerHandle& h) { return h.last_market_update(); }); - } - never; -} - bool MDConsumerHandle::get_last_trade(TimeStamp& time, PriceSize& trade, datetime::LLDateTime& trade_timestamp, size_t* total_volume) const { switch(_type) { case HANDLETYPE_VOID: return false; diff --git a/src/lib/md/MDConsumerHandle.h b/src/lib/md/MDConsumerHandle.h index 1f8904f22..7f1097d73 100644 --- a/src/lib/md/MDConsumerHandle.h +++ b/src/lib/md/MDConsumerHandle.h @@ -5,6 +5,7 @@ #include "lib/md/Shm/MDStructure.h" #include "lib/sparkproto/SparkProtos.pb.h" #include "UserSymbol.h" +#include "lib/utils/sane_algorithm.h" #include #include @@ -16,6 +17,7 @@ struct SyntheticStrategy { spark::ExecutionStrategy strategy; api::Security security; std::vector legs; + TimeStamp updated; }; TimeStamp copy_bbo(const HotEntry& src, PriceSize& out_bid, PriceSize& out_ask); @@ -28,6 +30,117 @@ enum class HandleReadResult { SUCCESS }; +inline void read_direct( + const HotDepthEntry& hot, + size_t count, + PriceSize* bid, PriceSize* ask) { + + bts_assert_d(count <= COCONUT_MAX_DEPTH); + count = std::min(count, (size_t)COCONUT_MAX_DEPTH); + for (size_t x = 0; x < count; ++x) { + hot.read_direct(x, &bid[x], &ask[x]); + } +} +inline void read_implied( + const HotDepthEntry& hot, + size_t count, + PriceSize* bid, PriceSize* ask) { + + bts_assert_d(count <= COCONUT_MAX_IMPLIED_DEPTH); + count = std::min(count, (size_t)COCONUT_MAX_IMPLIED_DEPTH); + for (size_t x = 0; x < count; ++x) { + hot.read_implied(x, &bid[x], &ask[x]); + } +} +inline void read_combined( + const HotDepthEntry& hot, + size_t count, + PriceSize* bid, PriceSize* ask) { + bts_assert_d(count <= COCONUT_MAX_DEPTH); + size_t xda=0, xdb=0, xia=0, xib=0; + if (hot.implied[xib].bid().is_nan()) xib = COCONUT_MAX_IMPLIED_DEPTH; + if (hot.implied[xia].ask().is_nan()) xia = COCONUT_MAX_IMPLIED_DEPTH; + + for (size_t x = 0; x < count; ++x) { + if (xib < COCONUT_MAX_IMPLIED_DEPTH) { + auto direct = hot.direct[xdb].bid(); + auto implied = hot.implied[xib].bid(); + + // Compare bids + if (direct.int_price() == implied.int_price()) { + bid[x] = PriceSize( + direct.int_price(), + direct.size(), + ImpliedSize(implied.size())); + xdb += 1; + xib += 1; + } + // Bids grow down, take the higher price + else if (implied.int_price() < direct.int_price()) { + bid[x] = direct; + xdb += 1; + } + else { + bid[x] = implied; + xib += 1; + } + } + else { + bid[x] = hot.direct[xdb].bid(); + xdb += 1; + } + + if (xia < COCONUT_MAX_IMPLIED_DEPTH) { + auto direct = hot.direct[xda].ask(); + auto implied = hot.implied[xia].ask(); + + // Compare asks + if (direct.int_price() == implied.int_price()) { + ask[x] = PriceSize( + direct.int_price(), + direct.size(), + ImpliedSize(implied.size())); + xda += 1; + xia += 1; + } + // asks grow up, take the lower price + else if (direct.int_price() < implied.int_price()) { + ask[x] = direct; + xda += 1; + } + else { + ask[x] = implied; + xia += 1; + } + } + else { + ask[x] = hot.direct[xda].ask(); + xda += 1; + } + } +} + +template +TimeStamp optimistic_copy( + Read read, + const HotDepthEntry& hot, + size_t count, + PriceSize* bid, PriceSize* ask) +{ + TimeStamp t; + int attempts = 0; + const int max_attempts = 1619200; + do { + t = hot.time(); + read(hot, count, bid, ask); + ++attempts; + } while((t != hot.time() || t == TimeStamp::invalid()) && attempts < max_attempts); + if (attempts >= max_attempts) { + return TimeStamp::invalid(); + } + return t; +} + // Can be an invalid handle if you request a security which has not been discovered yet. class BTS_EXPORT MDConsumerHandle @@ -75,69 +188,44 @@ public: bts_assert(false); } - TimeStamp last_market_update() const; - - HandleReadResult get_bbo(TimeStamp& time, PriceSize& bid, PriceSize& ask) const - { - if (!is_initialized()) return HandleReadResult::NO_CHANGE; - + TimeStamp last_market_update() const { switch(_type) { - case HANDLETYPE_DEPTH: - if (book.hot->time() > time || book.hot->time() == TimeStamp::invalid()) { - TimeStamp temp = copy_depth(*book.hot, 1, &bid, &ask); - if (temp == TimeStamp::invalid()) { return HandleReadResult::BUSY; } - time = temp; - return HandleReadResult::SUCCESS; - } - return HandleReadResult::NO_CHANGE; - case HANDLETYPE_BBO: - if (bbo.hot->time() > time || bbo.hot->time() == TimeStamp::invalid()) { - TimeStamp temp = copy_bbo(*bbo.hot, bid, ask); - if (temp == TimeStamp::invalid()) { return HandleReadResult::BUSY; } - time = temp; - return HandleReadResult::SUCCESS; - } - return HandleReadResult::NO_CHANGE; - case HANDLETYPE_SYNTHETIC: - return get_depth(time, 1, &bid, &ask); case HANDLETYPE_VOID: - return HandleReadResult::NO_CHANGE; - } - bts_assert_d(false); - return HandleReadResult::NO_CHANGE; - } - - HandleReadResult get_depth(TimeStamp& time, uint32_t copydepth, PriceSize* bids, PriceSize* asks) const { - bts_assert_d(copydepth <= COCONUT_MAX_DEPTH); - if (!is_initialized() || copydepth == 0) return HandleReadResult::NO_CHANGE; - - switch(_type) { - case HANDLETYPE_VOID: return HandleReadResult::NO_CHANGE; + return TimeStamp::invalid(); case HANDLETYPE_DEPTH: - if (book.hot->time() > time || book.hot->time() == TimeStamp::invalid()) { - TimeStamp temp = copy_depth(*book.hot, std::min(copydepth, COCONUT_MAX_DEPTH), bids, asks); - if (temp == TimeStamp::invalid()) { return HandleReadResult::BUSY; } - time = temp; - return HandleReadResult::SUCCESS; - } - return HandleReadResult::NO_CHANGE; + return book.hot->time(); case HANDLETYPE_BBO: - return get_bbo(time, *bids, *asks); - case HANDLETYPE_SYNTHETIC: - auto t = last_market_update(); - if (t > time || t == TimeStamp::invalid()) { - TimeStamp temp = copy_depth(*synthetic.def, std::min(copydepth, COCONUT_MAX_DEPTH), bids, asks); - if (temp == TimeStamp::invalid()) { return HandleReadResult::BUSY; } - time = temp; - return HandleReadResult::SUCCESS; - } - return HandleReadResult::NO_CHANGE; + return bbo.hot->time(); + case HANDLETYPE_SYNTHETIC: + return std::max( + synthetic.def->updated, + sane::max(synthetic.def->legs, [](const MDConsumerHandle& h) { return h.last_market_update(); })); } bts_assert(false); } + + HandleReadResult get_direct_book(TimeStamp& time, uint32_t copydepth, PriceSize* bids, PriceSize* asks) const { + return get_book(read_direct, time, copydepth, bids, asks); + } + HandleReadResult get_implied_book(TimeStamp& time, uint32_t copydepth, PriceSize* bids, PriceSize* asks) const { + return get_book(read_implied, time, copydepth, bids, asks); + } + HandleReadResult get_book(TimeStamp& time, uint32_t copydepth, PriceSize* bids, PriceSize* asks) const { + return get_book(read_combined, time, copydepth, bids, asks); + } bool get_last_trade(TimeStamp& time, PriceSize& out_trade, datetime::LLDateTime& out_trade_timestamp, size_t* total_volume=nullptr) const; + IntPrice settlement_price() const { + switch(_type) { + case HANDLETYPE_DEPTH: return book.hot->settlement; + case HANDLETYPE_BBO: return bbo.hot->settlement; + default: + return INT_NAN; + } + } + datetime::Date settlement_date() const { return this->misc().settlement_date; } + TimeStamp misc_timestamp() const; MiscData misc() const; @@ -162,6 +250,37 @@ public: private: friend class MDConsumerInst; + + template + HandleReadResult get_book(Read read, TimeStamp& time, uint32_t copydepth, PriceSize* bids, PriceSize* asks) const { + bts_assert_d(copydepth <= COCONUT_MAX_DEPTH); + if (!is_initialized() || copydepth == 0) return HandleReadResult::NO_CHANGE; + switch(_type) { + case HANDLETYPE_VOID: return HandleReadResult::NO_CHANGE; + case HANDLETYPE_DEPTH: + if (book.hot->time() > time || book.hot->time() == TimeStamp::invalid()) { + TimeStamp temp = optimistic_copy(read, *book.hot, copydepth, bids, asks); + if (temp == TimeStamp::invalid()) { return HandleReadResult::BUSY; } + time = temp; + return HandleReadResult::SUCCESS; + } + return HandleReadResult::NO_CHANGE; + case HANDLETYPE_BBO: + bts_assert_msg(false,"Not impelmented"); + return HandleReadResult::NO_CHANGE; + case HANDLETYPE_SYNTHETIC: + auto t = last_market_update(); + if (t > time || t == TimeStamp::invalid()) { + TimeStamp temp = copy_depth(*synthetic.def, std::min(copydepth, COCONUT_MAX_DEPTH), bids, asks); + if (temp == TimeStamp::invalid()) { return HandleReadResult::BUSY; } + time = temp; + return HandleReadResult::SUCCESS; + } + return HandleReadResult::NO_CHANGE; + } + bts_assert(false); + } + void set_hot(const void* hot) { switch(_type) { case HANDLETYPE_DEPTH: diff --git a/src/lib/md/MDConsumerInterop.cpp b/src/lib/md/MDConsumerInterop.cpp index 2657235f9..b526ebc28 100644 --- a/src/lib/md/MDConsumerInterop.cpp +++ b/src/lib/md/MDConsumerInterop.cpp @@ -102,16 +102,6 @@ CAPI void md_consumer_set_on_security(MDConsumer* consumer, security_cb cb) { } #endif -typedef void(*just_security_cb)(security_key, const char*, size_t); -CAPI void md_consumer_get_single_security(const ColdEntry* entry, just_security_cb cb) { - bts_assert(entry); - bts::api::Security security; - convert_security(entry->security, &security); - std::string buffer; - security.SerializePartialToString(&buffer); - cb(security.id(), buffer.c_str(), buffer.size()); -} - bool is_heart_beat_ok(const ColdHeader &header); CAPI bool md_consumer_is_heart_beating(const ColdHeader* header) { if (header == nullptr) return false; diff --git a/src/lib/md/MDProducerHandle.cpp b/src/lib/md/MDProducerHandle.cpp index 99aec3d63..4e3cb476d 100644 --- a/src/lib/md/MDProducerHandle.cpp +++ b/src/lib/md/MDProducerHandle.cpp @@ -148,16 +148,21 @@ void MDProducerHandle::update_exchange_status(TimeStamp, TradingStatus status) { cheader().exchange_status_count++; } -void MDProducerHandle::update_settlement(TimeStamp time, Price price, Date date) { +void MDProducerHandle::update_settlement(TimeStamp time, IntPrice price, Date date) { if (date > cdata().misc.settlement_date) { - cdata().misc.previous_settlement_price = cdata().misc.settlement_price; + cdata().misc.previous_settlement_price = has_depth() ? hdepth().settlement : hdata().settlement; cdata().misc.previous_settlement_date = cdata().misc.settlement_date; } - cdata().misc.settlement_price = price; + if (has_depth()) { + hdepth().settlement = price; + } + else { + hdata().settlement = price; + } cdata().misc.settlement_date = date; cdata().misc.misc_timestamp = time; } -void MDProducerHandle::update_previous_settlement(TimeStamp time, Price price, Date date) { +void MDProducerHandle::update_previous_settlement(TimeStamp time, IntPrice price, Date date) { cdata().misc.previous_settlement_price = price; cdata().misc.previous_settlement_date = date; cdata().misc.misc_timestamp = time; @@ -206,9 +211,9 @@ void MDProducerHandle::mark_deleted() { std::array blank; std::array iblank; for (size_t x = 0; x < blank.size(); ++x) { - blank[x] = PriceSize::NO_MARKET; + blank[x] = NO_MARKET; if (x < COCONUT_MAX_IMPLIED_DEPTH) { - iblank[x] = PriceSize::NO_MARKET; + iblank[x] = NO_MARKET; } } update_direct(ts, blank, blank, 0); diff --git a/src/lib/md/MDProducerHandle.h b/src/lib/md/MDProducerHandle.h index b86dd941d..050dec46d 100644 --- a/src/lib/md/MDProducerHandle.h +++ b/src/lib/md/MDProducerHandle.h @@ -1,5 +1,5 @@ #pragma once - + #include "lib/utils/bts_no_copy.h" #include "lib/utils/bts_export.h" #include "Shm/MDStructure.h" @@ -33,9 +33,9 @@ public: return key; } void set_legs(const std::vector& legs); - + void update_bbo(TimeStamp time, const PriceSize bid_price, const PriceSize ask_price, uint16_t seq=0); - void update_direct(TimeStamp time, std::array& bids, std::array& asks, uint32_t seq=0) { + void update_direct(TimeStamp time, const std::array& bids, const std::array& asks, uint32_t seq=0) { if (!has_depth()) { update_bbo(time, bids[0], asks[0], seq); } @@ -51,11 +51,12 @@ public: hheader().direct_count += 1; } } - void update_implied(TimeStamp time, std::array& bids, std::array& asks, uint32_t seq=0) { + + void update_implied(TimeStamp time, const std::array& bids, const std::array& asks, uint32_t seq=0) { if (has_depth()) { auto& book = hdepth(); book.set_time(TimeStamp::invalid()); - for (size_t x = 0; x < COCONUT_MAX_DEPTH; ++x) { + for (size_t x = 0; x < COCONUT_MAX_IMPLIED_DEPTH; ++x) { book.write_implied(x, bids[x], asks[x]); } book.status = co2::COCONUTSECURITYSTATUS_OK; @@ -64,7 +65,31 @@ public: hheader().implied_count += 1; } } - + + void update_books(TimeStamp time, + const std::array& bid, + const std::array& ask, + const std::array& implied_bid, + const std::array& implied_ask, + uint32_t seq=0) { + + if (has_depth()) { + auto& book = hdepth(); + book.set_time(TimeStamp::invalid()); + for (size_t x = 0; x < COCONUT_MAX_DEPTH; ++x) { + if (x < COCONUT_MAX_IMPLIED_DEPTH) { + book.write_implied(x, implied_bid[x], implied_ask[x]); + } + book.write_direct(x, bid[x], ask[x]); + } + book.status = co2::COCONUTSECURITYSTATUS_OK; + book.sequence = seq; + book.set_time(time); + hheader().direct_count += 1; + hheader().implied_count += 1; + } + } + void set_status(co2::CoconutSecurityStatus status); // hot // Updates trade log and last trade @@ -72,12 +97,12 @@ public: // Update last_trade without updating trade log void set_last_trade(TimeStamp time, const PriceSize ps, datetime::LLDateTime date, uint32_t total_volume); void add_rfq(TimeStamp time, const co2::QuoteRequestMessage& rfq); // cold - + void update_exchange_status(TimeStamp time, TradingStatus status); // cold // Misc fields - void update_settlement(TimeStamp time, Price price, datetime::Date date); // cold - void update_previous_settlement(TimeStamp time, Price price, datetime::Date date); // cold + void update_settlement(TimeStamp time, IntPrice price, datetime::Date date); // cold + void update_previous_settlement(TimeStamp time, IntPrice price, datetime::Date date); // cold void update_opening(TimeStamp time, Price price, datetime::Date date); // cold void update_closing(TimeStamp time, Price price, datetime::Date date); // cold void update_session_high(TimeStamp time, Price price); // cold @@ -95,7 +120,7 @@ private: friend class tbb::concurrent_queue < MDProducerHandle > ; friend class SecurityIDRecorder; friend struct bts::co2::Work; - + HotEntry& hdata() const { bts_assert_d(_hdata && !has_depth()); return *static_cast(_hdata); @@ -110,7 +135,7 @@ private: bts_assert_d(_hheader); return *_hheader; } - + ColdEntry& cdata() const { bts_assert_d(_cdata); return *_cdata; diff --git a/src/lib/md/MemorySegmentUtils.cpp b/src/lib/md/MemorySegmentUtils.cpp index 1f216ce33..60b92b71e 100644 --- a/src/lib/md/MemorySegmentUtils.cpp +++ b/src/lib/md/MemorySegmentUtils.cpp @@ -8,8 +8,9 @@ using namespace std; namespace bts { namespace md { -bool enable_depth(const string&, SecurityType type) { - return type == api::SECURITYTYPE_FUTURE || type == api::SECURITYTYPE_SPREAD || type == api::SECURITYTYPE_SYNTHETIC; +bool enable_depth(const string&, SecurityType) { + return true; + // return type == api::SECURITYTYPE_FUTURE || type == api::SECURITYTYPE_SPREAD || type == api::SECURITYTYPE_SYNTHETIC; } string segment_name(const string& symbol, SecurityType type, Venue exchange, Platform platform) { diff --git a/src/lib/md/PriceSize.cpp b/src/lib/md/PriceSize.cpp index 490e132b6..46ea81030 100644 --- a/src/lib/md/PriceSize.cpp +++ b/src/lib/md/PriceSize.cpp @@ -4,7 +4,8 @@ namespace bts { namespace md { -BTS_EXPORT PriceSize PriceSize::NO_MARKET = PriceSize(); +IntPrice INT_NAN = IntPrice(); +PriceSize NO_MARKET = PriceSize(); int to_exponent(double value) { if (value == 0 && os::is_nan(value)) return 0; diff --git a/src/lib/md/PriceSize.h b/src/lib/md/PriceSize.h index 2b33551e3..c8b647d39 100644 --- a/src/lib/md/PriceSize.h +++ b/src/lib/md/PriceSize.h @@ -1,6 +1,9 @@ #pragma once #include "common.h" +#include "IntPrice.h" +#include "SizeTypes.h" + #include "lib/os/os.h" #include "lib/utils/warn_ctrl.h" #include "lib/utils/bts_array_size.h" @@ -8,6 +11,7 @@ #include "lib/utils/bts_no_copy.h" #include "lib/utils/Optimistic.h" + #include #include #include @@ -18,107 +22,62 @@ namespace md { typedef double Price; typedef uint32_t Size; -// #define LOAD(x) x.load(std::memory_order_acquire) -// #define STORE(x,value) x.store(value, std::memory_order_release) - -static const double powers_10[] = { - 1E-20, 1E-19, 1E-18, 1E-17, 1E-16, 1E-15, 1E-14, 1E-13, 1E-12, 1E-11, 1E-10, 1E-9, 1E-8, 1E-7, 1E-6, 1E-5, 1E-4, 1E-3, 1E-2, 1E-1, - 1E0, - 1E1, 1E2, 1E3, 1E4, 1E5, 1E6, 1E7, 1E8, 1E9, 1E10, 1E11, 1E12, 1E13, 1E14, 1E15, 1E16, 1E17, 1E18, 1E19, 1E20 -}; -static const int max_abs_power_10 = BTS_ARRAYSIZE(powers_10) / 2; -inline double pow10(int power) { - const int offset = power + max_abs_power_10; - const int powers_10_sz = BTS_ARRAYSIZE(powers_10); - bts_assert_d(offset >= 0 && offset < powers_10_sz); - if (offset >= 0 && offset < powers_10_sz) { - return powers_10[offset]; - } - return std::numeric_limits().quiet_NaN(); -} int to_exponent(double value); -template -inline bool compact_mantissa(Mantissa& mantissa, Exponent& exponent) { - bool result = false; - // If the mantissa is larger than 39 bits will allow attempt to shrink it - while (mantissa > 0x8000000000 || mantissa < -0x8000000000) { - mantissa /= 10; - exponent += 1; - result = true; - } - return result; -} class PriceSize { - int64_t _mantissa; - int8_t _exponent; + IntPrice _price; + // merged size uint32_t _size; + uint32_t _direct_size; public: - PriceSize(int64_t mantissa, int8_t exponent, Size size) : - _mantissa(mantissa), - _exponent(exponent), - _size(size) - { - // bts_assert_d(exponent != 0); - } - PriceSize() : _mantissa(0), _exponent(), _size(0) { } + PriceSize(IntPrice price, Size size) : + _price(size == 0 ? INT_NAN : price), + _size(size), + _direct_size(size) + { } + PriceSize(IntPrice price, Size direct_size, ImpliedSize implied_size) : + _price(price), + _size(direct_size + implied_size.value()), + _direct_size(direct_size) + { } + PriceSize() : _price(INT_NAN), _size(0), _direct_size(0) {} PriceSize(const PriceSize& other) = default; PriceSize& operator=(const PriceSize& other) = default; bool operator==(const PriceSize& other) const { - return (_size == other._size && _mantissa == other._mantissa && _exponent == other._exponent); + return (_size == other._size && _direct_size == other._direct_size && int_price() == other.int_price()); } bool operator!=(const PriceSize& other) const { return !(*this == other); } - - BTS_EXPORT static PriceSize NO_MARKET; - - template - static PriceSize from_double(double price, Size size) { - if (size == 0 || os::is_nan(price)) return NO_MARKET; - double temp; - if (fabs(modf(price, &temp)) != 0.0) { - const double rounding = 0.5 * pow10(-PRECISION); - price += (price > 0) ? rounding : -rounding; - price *= pow10(PRECISION); - return PriceSize((int64_t)price, -PRECISION, size); - } - return PriceSize((int64_t)price, 0, size); + PriceSize operator-(IntPrice price) const{ + PriceSize res=*this; + res._price=res._price-price; + return res; } - static PriceSize from_double(double price, int precision, Size size) { - if (size == 0 || os::is_nan(price)) return NO_MARKET; - double temp; - if (fabs(modf(price, &temp)) != 0.0) { - const double rounding = 0.5 * pow10(-precision); - price += (price > 0) ? rounding : -rounding; - price *= pow10(precision); - return PriceSize((int64_t)price, -precision, size); - } - return PriceSize((int64_t)price, 0, size); - } - // // Accessors - int64_t mantissa() const { - return _mantissa; - } - int8_t exponent() const { - return _exponent; - } - // This call is relatively slow so don't call it often - double price() const { - if (*this == NO_MARKET) return NO_PRICE; - return mantissa() * pow10(exponent()); - } - Size size() const { - return _size; - } + int64_t mantissa() const { return _price.mantissa(); } + int8_t exponent() const { return _price.exponent(); } + Size size() const { return _size; } + Size direct_size() const { return _direct_size; } + Size implied_size() const { return _size - _direct_size; } + double price() const { return _price.price(); } + IntPrice int_price() const { return _price; } + bool is_nan() const { return _price.is_nan(); } }; +extern PriceSize NO_MARKET; static_assert(sizeof(PriceSize) == 16, "PriceSize should be small"); + +template +PriceSize PriceSize_from_double(double price, Size size) { + if (size == 0 || os::is_nan(price)) return NO_MARKET; + return { IntPrice_from_double(price), size }; +} + typedef utils::Optimistic AtomicPriceSize; @@ -127,63 +86,34 @@ typedef utils::Optimistic AtomicPriceSize; // This structure can store the same data in 24 bytes so it's more efficient for shared memory struct PriceLevel { PriceLevel() : - _mantissa_b(0), _exponent_b(0), - _mantissa_a(0), _exponent_a(0), - _size_b(0), _size_a(0) {} - PriceLevel( - int64_t bid_mantissa, - int8_t bid_exponent, - uint32_t bid_size, - int64_t ask_mantissa, - int8_t ask_exponent, - uint32_t ask_size) : - _mantissa_b(bid_mantissa), _exponent_b(bid_exponent), - _mantissa_a(ask_mantissa), _exponent_a(ask_exponent), - _size_b(bid_size), - _size_a(ask_size) - {} + _bid(), _ask(), + _bid_size(0), _ask_size(0) {} PriceLevel(const PriceSize& bid, const PriceSize& ask): - _mantissa_b(bid.mantissa()), _exponent_b(bid.exponent()), - _mantissa_a(ask.mantissa()), _exponent_a(ask.exponent()), - _size_b(bid.size()), _size_a(ask.size()) + _bid(bid.int_price()), _ask(ask.int_price()), + _bid_size(bid.size()), _ask_size(ask.size()) {} -#ifdef _WIN32 - int64_t _mantissa_b : 56; - int64_t _exponent_b : 8; - int64_t _mantissa_a : 56; - int64_t _exponent_a : 8; -#else - int64_t _mantissa_b : 56; - int8_t _exponent_b : 8; - int64_t _mantissa_a : 56; - int8_t _exponent_a : 8; -#endif - uint32_t _size_b; - uint32_t _size_a; + IntPrice _bid; + IntPrice _ask; + uint32_t _bid_size; + uint32_t _ask_size; - uint64_t bid_mantissa() const { return _mantissa_b; } - uint64_t ask_mantissa() const { return _mantissa_a; } - int8_t bid_exponent() const { return _exponent_b; } - int8_t ask_exponent() const { return _exponent_a; } - uint32_t bid_size() const { return _size_b; } - uint32_t ask_size() const { return _size_a; } + const IntPrice& bid_price() const { return _bid; } + const IntPrice& ask_price() const { return _ask; } + uint32_t bid_size() const { return _bid_size; } + uint32_t ask_size() const { return _ask_size; } bool operator==(const PriceLevel& other) const { - return _size_a == other._size_a && - _size_b == other._size_b && - _mantissa_a == other._mantissa_a && - _mantissa_b == other._mantissa_b && - _exponent_a == other._exponent_a && - _exponent_b == other._exponent_b; + return _ask_size == other._ask_size && + _bid_size == other._bid_size && + _bid == other._bid && + _ask == other._ask; } bool operator!=(const PriceLevel& other) const { - return _size_a != other._size_a || - _size_b != other._size_b || - _mantissa_a != other._mantissa_a || - _mantissa_b != other._mantissa_b || - _exponent_a != other._exponent_a || - _exponent_b != other._exponent_b; + return _ask_size != other._ask_size || + _bid_size != other._bid_size || + _bid != other._bid || + _ask != other._ask; } bool update(const PriceLevel& temp) { @@ -195,14 +125,33 @@ struct PriceLevel { } PriceSize bid() const { - return PriceSize(_mantissa_b, _exponent_b, _size_b); + return PriceSize(_bid, _bid_size); } PriceSize ask() const { - return PriceSize(_mantissa_a, _exponent_a, _size_a); + return PriceSize(_ask, _ask_size); } }; static_assert(sizeof(PriceLevel) == 24, "PriceLevel should be small"); +struct ImpliedPriceLevel : private PriceLevel { + ImpliedPriceLevel() : PriceLevel() {} + ImpliedPriceLevel(const PriceSize& bid, const PriceSize& ask): + PriceLevel(bid,ask) {} + using PriceLevel::bid_price; + using PriceLevel::ask_price; + using PriceLevel::bid_size; + using PriceLevel::ask_size; + using PriceLevel::operator==; + using PriceLevel::operator!=; + bool operator=(const PriceLevel& other) { return update(other); } + PriceSize bid() const { + return PriceSize(_bid, 0, ImpliedSize(_bid_size)); + } + PriceSize ask() const { + return PriceSize(_ask, 0, ImpliedSize(_ask_size)); + } +}; +static_assert(sizeof(ImpliedPriceLevel) == sizeof(PriceLevel), "ImpliedPriceLevel should not be bigger than PriceLevel"); }}//namespace diff --git a/src/lib/md/SettlementRecorder.cpp b/src/lib/md/SettlementRecorder.cpp index cef22e6f7..dfa1eb549 100644 --- a/src/lib/md/SettlementRecorder.cpp +++ b/src/lib/md/SettlementRecorder.cpp @@ -26,7 +26,7 @@ enum WorkType { struct Work { WorkType type; MDProducerHandle handle; - Price price; + IntPrice price; Date date; }; @@ -43,10 +43,11 @@ struct SettlementRecorderImpl { sqlite3_stmt_ptr insert_stmt; }; -bool record_settle(sqlite3_stmt* insert, security_key key, Price price, Date date) { +bool record_settle(sqlite3_stmt* insert, security_key key, IntPrice price, Date date) { sqlite3_bind_int64(insert, 1, key); sqlite3_bind_int(insert, 2, date.to_short()); - sqlite3_bind_double(insert, 3, price); + sqlite3_bind_int64(insert, 3, price.mantissa()); + sqlite3_bind_int(insert, 4, price.exponent()); auto result = sqlite3_step(insert); sqlite3_reset(insert); return (result == SQLITE_DONE || result == SQLITE_ROW); @@ -56,14 +57,16 @@ void publish_settles(sqlite3_stmt* select, MDProducerHandle handle) { sqlite3_bind_int(select, 1, handle.key()); auto result = sqlite3_step(select); if (result == SQLITE_ROW) { - Price price = sqlite3_column_double(select, 0); - Date date = Date(sqlite3_column_int(select, 1)); - handle.update_settlement(TimeStamp::now(), price, date); + int64_t mantissa = sqlite3_column_int(select, 0); + int exponent = sqlite3_column_int(select, 1); + Date date = Date(sqlite3_column_int(select, 2)); + handle.update_settlement(TimeStamp::now(), {mantissa,(int8_t)exponent}, date); result = sqlite3_step(select); if (result == SQLITE_ROW) { - price = sqlite3_column_double(select, 0); - date = Date(sqlite3_column_int(select, 1)); - handle.update_previous_settlement(TimeStamp::now(), price, date); + int64_t mantissa = sqlite3_column_int(select, 0); + int exponent = sqlite3_column_int(select, 1); + date = Date(sqlite3_column_int(select, 2)); + handle.update_previous_settlement(TimeStamp::now(), {mantissa,(int8_t)exponent}, date); } } sqlite3_reset(select); @@ -97,28 +100,29 @@ SettlementRecorder::SettlementRecorder(Platform platform) : sqlite3_exec_nofail(db, "PRAGMA journal_mode=OFF"); sqlite3_exec_nofail(db, "PRAGMA syncronous=0"); // key=(security_key+date) - sqlite3_exec_nofail(db, "CREATE TABLE IF NOT EXISTS settlements (key INTEGER NOT NULL, date INTEGER NOT NULL, price REAL NOT NULL, PRIMARY KEY (key,date));"); + sqlite3_exec_nofail(db, + "CREATE TABLE IF NOT EXISTS settlements2 (key INTEGER NOT NULL, date INTEGER NOT NULL, mantissa INTEGER NOT NULL, exponent INTEGER NOT NULL, PRIMARY KEY (key,date));"); // Delete any records > 10 days old auto cutoff = datetime::local_now() - boost::gregorian::days(10); auto encoded = datetime::ll_to_date(datetime::ptime_to_ll(cutoff)).to_short(); char query[256]; - sprintf(query, "delete from settlements where date < %i", encoded); + sprintf(query, "delete from settlements2 where date < %i", encoded); sqlite3_exec_nofail(db, query); // statements - _self->select_stmt = prepare_query(db, "select price,date from settlements where key=? order by date desc limit 2"); - _self->insert_stmt = prepare_query(db, "insert or replace into settlements(key, date, price) values(?,?,?)"); + _self->select_stmt = prepare_query(db, "select mantissa,exponent,date from settlements2 where key=? order by date desc limit 2"); + _self->insert_stmt = prepare_query(db, "insert or replace into settlements2(key, date, mantissa,exponent) values(?,?,?,?)"); } SettlementRecorder::~SettlementRecorder() { } void SettlementRecorder::restore_settlement(MDProducerHandle handle) { - Work work = { SNAPSHOT, handle, 0, Date() }; + Work work = { SNAPSHOT, handle, INT_NAN, Date() }; _self->work_queue.push(work); } -void SettlementRecorder::add_settlement(MDProducerHandle handle, Price price, datetime::Date date) { +void SettlementRecorder::add_settlement(MDProducerHandle handle, IntPrice price, datetime::Date date) { Work work = { UPDATE, handle, price, date }; _self->work_queue.push(work); } diff --git a/src/lib/md/SettlementRecorder.h b/src/lib/md/SettlementRecorder.h index 536ca374f..2dfc9844b 100644 --- a/src/lib/md/SettlementRecorder.h +++ b/src/lib/md/SettlementRecorder.h @@ -19,7 +19,7 @@ public: // Put settlement update in a queue // in do_work() drain queue and update settlement 'depth' void restore_settlement(MDProducerHandle handle); - void add_settlement(MDProducerHandle handle, Price price, datetime::Date date); + void add_settlement(MDProducerHandle handle, IntPrice price, datetime::Date date); void do_work(); private: diff --git a/src/lib/md/Shm/MDStructure.h b/src/lib/md/Shm/MDStructure.h index 55e8501e8..67b88b7ea 100644 --- a/src/lib/md/Shm/MDStructure.h +++ b/src/lib/md/Shm/MDStructure.h @@ -26,7 +26,7 @@ namespace md { using utils::TimeStamp; -const uint32_t shm_version = 100; +const uint32_t shm_version = 101; struct ALIGNED_(CACHE_LINE) HotHeader { size_t count; @@ -45,6 +45,7 @@ class HotEntry { PriceLevel book; // 24 security_key key; // 4 uint32_t sequence; // 4 + IntPrice settlement; // 8 uint8_t status; // 1 void set_time(const TimeStamp& t) { timestamp.store(t, std::memory_order_release); } TimeStamp time() const { return timestamp.load(std::memory_order_acquire); } @@ -67,45 +68,34 @@ class HotDepthEntry { uint32_t size() const { return sizeof(HotDepthEntry); } security_key key; // 4 NOTE: key is at a different offset than it is in HotEntry uint32_t sequence; // 4 + IntPrice settlement; // 8 std::array direct; // 240 - std::array implied; // 48 + std::array implied; // 48 uint8_t status; // 1 - const PriceLevel* at(uint32_t depth) const { - if (depth < direct.size()) { - return &direct[depth]; - } - return nullptr; - } void read_direct(uint32_t depth, PriceSize* bid, PriceSize* ask) const { - if (depth < direct.size()) { - *bid = direct[depth].bid(); - *ask = direct[depth].ask(); - } + *bid = direct[depth].bid(); + *ask = direct[depth].ask(); } void read_implied(uint32_t depth, PriceSize* bid, PriceSize* ask) const { - if (depth < implied.size()) { - *bid = implied[depth].bid(); - *ask = implied[depth].ask(); - } + *bid = implied[depth].bid(); + *ask = implied[depth].ask(); } bool write_direct(uint32_t depth, const PriceSize& bid, const PriceSize& ask) { - if (depth < direct.size()) { - auto temp = PriceLevel(bid,ask); - if (direct[depth] != temp) { - direct[depth] = PriceLevel(bid,ask); - return true; - } + bts_assert_d(depth < direct.size()); + auto temp = PriceLevel(bid,ask); + if (direct[depth] != temp) { + direct[depth] = temp; + return true; } return false; } bool write_implied(uint32_t depth, const PriceSize& bid, const PriceSize& ask) { - if (depth < implied.size()) { - auto temp = PriceLevel(bid,ask); - if (implied[depth] != temp) { - implied[depth] = PriceLevel(bid,ask); - return true; - } + bts_assert_d(depth < implied.size()); + auto temp = PriceLevel(bid,ask); + if (implied[depth] != temp) { + implied[depth] = temp; + return true; } return false; } @@ -168,9 +158,8 @@ struct ALIGNED_(CACHE_LINE) ColdHeader { struct MiscData { MiscData(): misc_timestamp(), - settlement_price(NO_PRICE), settlement_date(), - previous_settlement_price(NO_PRICE), + previous_settlement_price(INT_NAN), previous_settlement_date(), open_price(NO_PRICE), open_date(), @@ -184,9 +173,8 @@ struct MiscData { { } TimeStamp misc_timestamp; // Settlement - Price settlement_price; datetime::Date settlement_date; - Price previous_settlement_price; + IntPrice previous_settlement_price; datetime::Date previous_settlement_date; // Opening/Closing @@ -204,7 +192,6 @@ struct MiscData { bool operator==(const MiscData& other) const { return - settlement_price == other.settlement_price && settlement_date == other.settlement_date && previous_settlement_price == other.previous_settlement_price && previous_settlement_date == other.previous_settlement_date && diff --git a/src/lib/md/SizeTypes.h b/src/lib/md/SizeTypes.h new file mode 100644 index 000000000..16484c8dd --- /dev/null +++ b/src/lib/md/SizeTypes.h @@ -0,0 +1,6 @@ +#pragma once +#include "lib/utils/ValueWrapper.h" +namespace bts { +namespace md { + using ImpliedSize = utils::ValueWrapper; +}} \ No newline at end of file diff --git a/src/lib/md/protos/co2.proto b/src/lib/md/protos/co2.proto index bfb145d5c..e9d4127ac 100644 --- a/src/lib/md/protos/co2.proto +++ b/src/lib/md/protos/co2.proto @@ -52,13 +52,13 @@ message MDRequest { repeated SecurityChannel channels = 1; enum Flags { SECURITY = 0; - BBO = 0x1; - DEPTH = 0x2; + BBO = 0x1; + DIRECT = 0x2; LAST_TRADE = 0x4; TRADE_LOG = 0x8; RFQ = 0x10; - // CUSTOMER_BBO = 0x20; - MISC = 0x40; + MISC = 0x30; + IMPLIED = 0x40; } string host = 2; string token = 3; @@ -67,7 +67,8 @@ message MDRequest { message ChannelUpdate { uint32 key = 1; bts.api.Security security = 2; - BookMessage book = 3; + BookMessage direct = 3; + BookMessage implied = 4; MiscMessage misc = 5; TradeMessage last_trade = 6; } @@ -84,6 +85,11 @@ message MDReply { repeated ChannelReply channels = 1; } +message PriceMessage { + int64 mantissa = 1; + sint32 exponent = 2; +} + message PriceSizeMessage { int64 mantissa = 1; sint32 exponent = 2; @@ -106,20 +112,22 @@ message BookMessage { message MiscMessage { // int32 key = 1; - double settlement_price = 2; - uint32 settlement_date = 3; - double previous_settlement_price = 20; - uint32 previous_settlement_date = 21; - double open_price = 4; - uint32 open_date = 5; - double close_price = 6; - uint32 close_date = 7; - double session_high = 8; - double session_low = 9; + reserved 1; + reserved 2; + PriceMessage settlement = 22; + uint32 settlement_date = 3; + PriceMessage previous_settlement_price = 23; + uint32 previous_settlement_date = 21; + double open_price = 4; + uint32 open_date = 5; + double close_price = 6; + uint32 close_date = 7; + double session_high = 8; + double session_low = 9; // double open_interest = 10; - double session_high_bid = 11; - double session_low_offer = 12; - double fixing_price = 13; + double session_high_bid = 11; + double session_low_offer = 12; + double fixing_price = 13; } enum AggressorSide { @@ -142,25 +150,25 @@ message TradeMessage { } message QuoteRequestMessage { enum Side { - UNDEFINED = 0; - BUY = 49; - SELL = 50; - CROSS = 99; - } - uint32 key = 1; - int64 time = 2; - string request_id = 3; - int32 size = 5; - Side side = 6; - bool tradable = 7; - bool gap = 8; + UNDEFINED = 0; + BUY = 49; + SELL = 50; + CROSS = 99; + } + uint32 key = 1; + int64 time = 2; + string request_id = 3; + int32 size = 5; + Side side = 6; + bool tradable = 7; + bool gap = 8; } message TradeList { - repeated TradeMessage items = 1; + repeated TradeMessage items = 1; } message RFQList { - repeated QuoteRequestMessage items = 1; + repeated QuoteRequestMessage items = 1; } message CoconutReplayHeader { diff --git a/src/lib/md/relay/QueryServer.cpp b/src/lib/md/relay/QueryServer.cpp index 51d61d6ed..6cbfcfc08 100644 --- a/src/lib/md/relay/QueryServer.cpp +++ b/src/lib/md/relay/QueryServer.cpp @@ -137,10 +137,15 @@ void set_price(PriceSize ps, co2::TradeMessage* ptrade) { ptrade->set_size(ps.size()); } -void copy_misc(MiscData misc, co2::MiscMessage* pmisc) { - pmisc->set_settlement_price(misc.settlement_price); +void set_price(const IntPrice& price, co2::PriceMessage* dest) { + dest->set_mantissa(price.mantissa()); + dest->set_exponent(price.exponent()); +} + +void copy_misc(IntPrice settlement, MiscData misc, co2::MiscMessage* pmisc) { + set_price(settlement, pmisc->mutable_settlement()); pmisc->set_settlement_date(misc.settlement_date.to_short()); - pmisc->set_previous_settlement_price(misc.previous_settlement_price); + set_price(misc.previous_settlement_price, pmisc->mutable_previous_settlement_price()); pmisc->set_previous_settlement_date(misc.previous_settlement_date.to_short()); pmisc->set_open_price(misc.open_price); pmisc->set_open_date(misc.open_date.to_short()); @@ -245,57 +250,80 @@ public: snapshot = true; } if (entitled) { - if (HASFLAG(flags, MDRequest::BBO) || HASFLAG(flags, MDRequest::DEPTH)) { + if (HASFLAG(flags, MDRequest::BBO) || HASFLAG(flags, MDRequest::DIRECT)) { std::array bids, asks; TimeStamp time(snapshot ? 0 : chan.time()); - size_t copy_depth = HASFLAG(flags, MDRequest::DEPTH) ? COCONUT_MAX_DEPTH : 1; - auto result = handle.get_depth(time, copy_depth, bids.data(), asks.data()); - if (result == HandleReadResult::SUCCESS) { - auto* p = pupd->mutable_book(); - for (size_t x = 0; x < COCONUT_MAX_DEPTH; ++x) { - PriceSize b = bids[x]; - if (b != PriceSize::NO_MARKET) { - convert(b, p->add_bids()); - } - PriceSize a = asks[x]; - if (a != PriceSize::NO_MARKET) { - convert(a, p->add_asks()); - } + size_t copy_depth = HASFLAG(flags, MDRequest::DIRECT) ? COCONUT_MAX_DEPTH : 1; + auto result = handle.get_direct_book(time, copy_depth, bids.data(), asks.data()); + if (result == HandleReadResult::SUCCESS) { + auto* p = pupd->mutable_direct(); + for (size_t x = 0; x < COCONUT_MAX_DEPTH; ++x) { + PriceSize b = bids[x]; + if (b != NO_MARKET) { + convert(b, p->add_bids()); + } + PriceSize a = asks[x]; + if (a != NO_MARKET) { + convert(a, p->add_asks()); } } - else if (result == HandleReadResult::BUSY) { - LOG(md,Info, "Busy read on handle {}. Retry on next request", instrument.handle.key()); - // If we get a busy reading BBO (which happens 1-2x per day for a single relay client) - // We move the timestamp back so the next request will try to read this value again. - // ALTERNATIVE: We could spin until the value was not busy but it could be that the writer - // was context switched out and we will have to wait multiple ms to unblock the request. - // This fix assumes it is better to send the update now with 1 stale value than to block the call - // and allow all the values to be stale until this one can be read. - reply->mutable_channel()->set_time(chan.time()); - } } - if (HASFLAG(flags, MDRequest::LAST_TRADE)) { - TimeStamp time(snapshot ? 0 : chan.time()); - PriceSize price; - datetime::LLDateTime trade_time; - size_t volume; - if (handle.get_last_trade(time, price, trade_time, &volume)) { - auto* trade = pupd->mutable_last_trade(); - trade->set_key(handle.key()); - trade->set_time(trade_time); - trade->set_total_volume(volume); - set_price(price, trade); - } - } - if (HASFLAG(flags, MDRequest::MISC)) { - TimeStamp time(snapshot ? 0 : chan.time()); - if (handle.misc_timestamp() > time) { - copy_misc(handle.misc(), pupd->mutable_misc()); - } + else if (result == HandleReadResult::BUSY) { + LOG(md,Info, "Busy read on handle {}. Retry on next request", instrument.handle.key()); + // If we get a busy reading BBO (which happens 1-2x per day for a single relay client) + // We move the timestamp back so the next request will try to read this value again. + // ALTERNATIVE: We could spin until the value was not busy but it could be that the writer + // was context switched out and we will have to wait multiple ms to unblock the request. + // This fix assumes it is better to send the update now with 1 stale value than to block the call + // and allow all the values to be stale until this one can be read. + reply->mutable_channel()->set_time(chan.time()); } } - - if (pupd->has_book() || + if ((HASFLAG(flags, MDRequest::IMPLIED) || HASFLAG(flags, MDRequest::BBO)) && !handle.is_synthetic()) { + std::array bids, asks; + TimeStamp time(snapshot ? 0 : chan.time()); + size_t copy_depth = HASFLAG(flags, MDRequest::IMPLIED) ? COCONUT_MAX_IMPLIED_DEPTH : 1; + auto result = handle.get_implied_book(time, copy_depth, bids.data(), asks.data()); + if (result == HandleReadResult::SUCCESS) { + auto* p = pupd->mutable_implied(); + for (size_t x = 0; x < COCONUT_MAX_IMPLIED_DEPTH; ++x) { + PriceSize b = bids[x]; + if (b != NO_MARKET) { + convert(b, p->add_bids()); + } + PriceSize a = asks[x]; + if (a != NO_MARKET) { + convert(a, p->add_asks()); + } + } + } + else if (result == HandleReadResult::BUSY) { + LOG(md,Info, "Busy read on handle (implied) {}. Retry on next request", instrument.handle.key()); + reply->mutable_channel()->set_time(chan.time()); + } + } + if (HASFLAG(flags, MDRequest::LAST_TRADE)) { + TimeStamp time(snapshot ? 0 : chan.time()); + PriceSize price; + datetime::LLDateTime trade_time; + size_t volume; + if (handle.get_last_trade(time, price, trade_time, &volume)) { + auto* trade = pupd->mutable_last_trade(); + trade->set_key(handle.key()); + trade->set_time(trade_time); + trade->set_total_volume(volume); + set_price(price, trade); + } + } + if (HASFLAG(flags, MDRequest::MISC)) { + TimeStamp time(snapshot ? 0 : chan.time()); + if (handle.misc_timestamp() > time) { + copy_misc(handle.settlement_price(), handle.misc(), pupd->mutable_misc()); + } + } + } + if (pupd->has_direct() || + pupd->has_implied() || pupd->has_last_trade() || pupd->has_misc() || pupd->has_security()) { diff --git a/src/lib/md/relay/RelayClient.cpp b/src/lib/md/relay/RelayClient.cpp index 482fa5b21..d63356f3e 100644 --- a/src/lib/md/relay/RelayClient.cpp +++ b/src/lib/md/relay/RelayClient.cpp @@ -34,17 +34,21 @@ namespace md { using namespace co2; PriceSize convert(const PriceSizeMessage& psm) { - return psm.size() > 0 ? PriceSize(psm.mantissa(), psm.exponent(), psm.size()) : PriceSize::NO_MARKET; + return psm.size() > 0 ? PriceSize({psm.mantissa(), (int8_t)psm.exponent()}, psm.size()) : NO_MARKET; } PriceSize trade_pricesize(const co2::TradeMessage& trade) { return trade.size() > 0 ? - PriceSize(trade.mantissa(), trade.exponent(), trade.size()) : PriceSize::NO_MARKET; + PriceSize({trade.mantissa(), (int8_t)trade.exponent()}, trade.size()) : NO_MARKET; } PriceSize book_at(const google::protobuf::RepeatedPtrField& side, int index) { if (side.size() > index) { return convert(side.Get(index)); } - return PriceSize::NO_MARKET; + return NO_MARKET; +} + +IntPrice convert(const PriceMessage& price) { + return { price.mantissa(), (int8_t)price.exponent() }; } class RelayProducer : public IRelayWriter { @@ -56,18 +60,23 @@ class RelayProducer : public IRelayWriter { utils::TimeStamp updated = utils::TimeStamp::now(); bool snapshot = update.has_security(); // If this is a snapshot an empty book() means "no market" rather than "no change" - if (update.has_book() || snapshot) { + if (update.has_direct() || update.has_implied() || snapshot) { std::array bids, asks; + std::array implied_bids, implied_asks; for (size_t x = 0; x < COCONUT_MAX_DEPTH; ++x) { - bids[x] = book_at(update.book().bids(), x); - asks[x] = book_at(update.book().asks(), x); + bids[x] = book_at(update.direct().bids(), x); + asks[x] = book_at(update.direct().asks(), x); } - handle.update_direct(updated, bids, asks); + for (size_t x = 0; x (7.2, 101), - PriceSize::from_double<2>(-9.7, 245)); + PriceSize_from_double<2>(7.2, 101), + PriceSize_from_double<2>(-9.7, 245)); entry.sequence = 777; entry.status = COCONUTSECURITYSTATUS_PACKET_LOSS; + entry.settlement = IntPrice_from_double<7>(98.6); entry.set_time(utils::TimeStamp::now()); } @@ -49,12 +50,18 @@ extern "C" BTS_EXPORT void co2_get_sample_depth(size_t& size, HotDepthEntry& ent entry.key = 1234; for (size_t x = 0; x < COCONUT_MAX_DEPTH; ++x) { entry.write_direct(x, - PriceSize::from_double<2>(x + 0.5, x + 1), - PriceSize::from_double<2>(x + 0.6, x + 2)); + PriceSize_from_double<2>(x + 0.5, x + 1), + PriceSize_from_double<2>(x + 0.6, x + 2)); + } + for (size_t x = 0; x < COCONUT_MAX_IMPLIED_DEPTH; ++x) { + entry.write_implied(x, + PriceSize_from_double<3>(77, x + 1), + PriceSize_from_double<3>(88, x + 2)); } entry.set_time(utils::TimeStamp::now()); entry.status = COCONUTSECURITYSTATUS_PACKET_LOSS; entry.sequence = 888; + entry.settlement = IntPrice_from_double<7>(98.6); } extern "C" BTS_EXPORT void co2_get_sample_cold(size_t& size, ColdEntry& entry) { @@ -62,13 +69,12 @@ extern "C" BTS_EXPORT void co2_get_sample_cold(size_t& size, ColdEntry& entry) { entry.exchange_status = TRADINGSTATUS_NORMAL; // Last trade - entry.last_trade.store(PriceSize::from_double<2>(4.2, 101)); + entry.last_trade.store(PriceSize_from_double<2>(4.2, 101)); entry.total_volume = 12345; entry.last_trade_time = datetime::ll_now(); // Settlement - entry.misc.settlement_price = 3.3; entry.misc.settlement_date = datetime::Date(2015, 9, 3); - entry.misc.previous_settlement_price = 3.3; + entry.misc.previous_settlement_price = IntPrice_from_double<7>(3.3); entry.misc.previous_settlement_date = datetime::Date(2015, 9, 2); // Open/close entry.misc.open_price = 4.4; @@ -101,21 +107,31 @@ extern "C" BTS_EXPORT void co2_get_sample_cold_header(ColdHeader& header) { // Do not write to trade_buffer or rfq_buffer } -extern "C" BTS_EXPORT MDProducer* co2_produce_test_md() { +struct TestProducer { + std::shared_ptr md; + std::vector handles; +}; + +extern "C" BTS_EXPORT TestProducer* co2_produce_test_md() { config::internal::initialize_for_test( "PlatformMap {" " value {" " glbx_platform: 'PLATFORM_SIM'" " }" "}"); - auto* p = new MDProducer(PLATFORM_SIM); - auto producers = create_test_securities(*p); + auto* tp = new TestProducer(); + tp->md = std::make_shared(PLATFORM_SIM, false); + tp->handles = create_test_securities(*(tp->md)); + auto& producers = tp->handles; assign_ids(producers.begin(), producers.end()); for (auto& p : producers) { - p.update_bbo(utils::TimeStamp::now(), PriceSize::from_double<3>(1.1, 101), PriceSize::from_double<3>(2.2,202)); - p.add_trade(utils::TimeStamp::now(), PriceSize::from_double<3>(5.5, 505), datetime::ll_from_date(datetime::Date(2015, 9, 22)), AGGRESSOR_SIDE_NONE, 4200); - p.update_settlement(utils::TimeStamp::now(), 6.6, datetime::Date(2000, 8, 8)); - p.update_settlement(utils::TimeStamp::now(), 6.6, datetime::Date(2000, 8, 9)); + p.update_bbo(utils::TimeStamp::now(), PriceSize_from_double<3>(1.1, 101), PriceSize_from_double<3>(2.2,202)); + p.add_trade(utils::TimeStamp::now(), + PriceSize_from_double<3>(5.5, 505), + datetime::ll_from_date(datetime::Date(2015, 9, 22)), + AGGRESSOR_SIDE_NONE, 4200); + p.update_settlement(utils::TimeStamp::now(), IntPrice_from_double<7>(6.6), datetime::Date(2000, 8, 8)); + p.update_settlement(utils::TimeStamp::now(), IntPrice_from_double<7>(6.6), datetime::Date(2000, 8, 9)); p.update_opening(utils::TimeStamp::now(), 7.7, datetime::Date(2001, 1, 2)); auto t = utils::TimeStamp::now(); @@ -127,16 +143,24 @@ extern "C" BTS_EXPORT MDProducer* co2_produce_test_md() { p.update_fixing_price(t, 14.14); } - return p; + return tp; } -extern "C" BTS_EXPORT void co2_destroy_test_md(MDProducer* producer) { +extern "C" BTS_EXPORT void co2_destroy_test_md(TestProducer* producer) { if (producer != nullptr) delete producer; config::internal::reset(); } extern "C" BTS_EXPORT void co2_get_zero_and_nan_book(PriceLevel& price) { - price = { PriceSize::NO_MARKET, PriceSize::from_double<7>(0,1) }; + price = { NO_MARKET, PriceSize_from_double<7>(0,1) }; +} +extern "C" BTS_EXPORT void co2_get_nan_price_size(PriceSize& ps) { + ps = NO_MARKET; +} +extern "C" BTS_EXPORT void co2_test_get_constants(size_t& max_depth, size_t& max_implied_depth, uint8_t& nan_exponent) { + max_depth = COCONUT_MAX_DEPTH; + max_implied_depth = COCONUT_MAX_IMPLIED_DEPTH; + nan_exponent = INT_NAN.exponent(); } extern "C" BTS_EXPORT void co2_get_sample_datetime(datetime::LLDateTime& now, datetime::LLDateTime& utc_now) { diff --git a/src/lib/md/test/md2_tests.cpp b/src/lib/md/test/md2_tests.cpp index ef7c9da50..9a3301311 100644 --- a/src/lib/md/test/md2_tests.cpp +++ b/src/lib/md/test/md2_tests.cpp @@ -55,8 +55,8 @@ Platform TEST_PLATFORM = PLATFORM_SIM; // Helper functions for Producer/Consumer tests // vector create_test_securities(MDProducer& producer, const char* postfix ="") { - FixedOptionSecurity fos; FixedFutureSecurity ffs; + FixedOptionSecurity fos; ffs.symbol("FOO"+string(postfix)); fos.symbol("FOOX"+string(postfix)); ffs.exchange = VENUE_BTS; @@ -109,18 +109,16 @@ Security mkoption(string symbol, security_key key, int year, int month, int day, result.mutable_option()->set_call_put(PUTORCALL_PUT); return result; } -unique_ptr mkfixedoption(string symbol, security_key key, int year, int month, int day, double strike, PutOrCall callput) { - unique_ptr fos(new FixedOptionSecurity()); - fos->type = SECURITYTYPE_OPTION; - fos->exchange = VENUE_BTS; - fos->key = key; - fos->strike = strike; - fos->expiration_day = day; - fos->expiration_month = month; - fos->expiration_year = year; - fos->call_put = callput; - fos->symbol(symbol); - return fos; +unique_ptr mkfixedfuture(string symbol, security_key key, int year, int month, int day) { + unique_ptr ffs(new FixedFutureSecurity()); + ffs->type = SECURITYTYPE_FUTURE; + ffs->exchange = VENUE_BTS; + ffs->key = key; + ffs->expiration_day = day; + ffs->expiration_month = month; + ffs->expiration_year = year; + ffs->symbol(symbol); + return ffs; } Security mkspread(string symbol, security_key key, const vector& legs) { Security result; @@ -198,8 +196,8 @@ public: // PriceSize violates some type punning rules // So it is important to have this test work correctly on the target platform PriceSize p2; - auto bid = PriceSize::from_double<2>(4.5, 14); - auto ask = PriceSize::from_double<9>(4.5, 14); + auto bid = PriceSize_from_double<2>(4.5, 14); + auto ask = PriceSize_from_double<9>(4.5, 14); PriceLevel memory; memory = PriceLevel(bid,ask); REQUIRE(bid.price() == memory.bid().price()); @@ -211,21 +209,21 @@ public: REQUIRE(p.size() == 14); } - auto p = PriceSize::from_double<2>(4.0, 5); + auto p = PriceSize_from_double<2>(4.0, 5); memory = PriceLevel(p,p); for (auto p2 : { memory.bid(), memory.ask() }) { REQUIRE(p2.price() == 4.0); REQUIRE(p2.size() == 5); } - p = PriceSize::from_double<2>(-4.2, 6); + p = PriceSize_from_double<2>(-4.2, 6); memory = PriceLevel(p,p); for (auto p2 : { memory.bid(), memory.ask() }) { REQUIRE(p2.price() == -4.2); REQUIRE(p2.size() == 6); } - p = PriceSize(4, -1, 2); + p = PriceSize({4, -1}, 2); memory = PriceLevel(p,p); for (auto p2 : { memory.bid(), memory.ask() }) { REQUIRE(p2.price() == 0.4); @@ -234,7 +232,7 @@ public: // Test requirement for more than 32 bit mantissa double bigval = -9999.9921875; - p = PriceSize::from_double<7>(bigval, 42); + p = PriceSize_from_double<7>(bigval, 42); memory = PriceLevel(p,p); for (auto p2 : { memory.bid(), memory.ask() }) { REQUIRE(p2.price() == bigval); @@ -244,14 +242,14 @@ public: void test_price_size_required_precision() { auto min_tick_2017 = 9999.0078125; - auto p17 = PriceSize::from_double<7>(min_tick_2017, 2017); + auto p17 = PriceSize_from_double<7>(min_tick_2017, 2017); PriceLevel m17(p17, p17); for (auto p : { m17.bid(), m17.ask() }) { REQUIRE(min_tick_2017 == p.price()); REQUIRE(p.size() == 2017); } auto min_tick_2018 = 1999.00390625; - auto p18 = PriceSize::from_double<8>(min_tick_2018, 2018); + auto p18 = PriceSize_from_double<8>(min_tick_2018, 2018); PriceLevel m18(p18, p18); for (auto p : { m18.bid(), m18.ask() }) { REQUIRE(min_tick_2018 == p.price()); @@ -285,29 +283,6 @@ public: // Equal if the delta is less than error return delta < acceptable_error; } - void test_compact_exponent() { - ExchangeDecimal test_values[] = { - { 2030000000000, -9 }, // mantissa > pow(2,40) - { -2030000000000, -9 }, // mantissa > pow(2,40) - { 2022750000000, -9 },// mantissa > pow(2,40) - { -2022750000000, -9 },// mantissa > pow(2,40) - { 20227500000000, -10 },// mantissa > pow(2,40) - { 202275000000000, -11 },// mantissa > pow(2,40) - { 2022750000000000, -12 },// mantissa > pow(2,40) - { 20227500000000000, -13 },// mantissa > pow(2,40) - { -20227500000000000, -13 },// mantissa > pow(2,40) - { 203, 1}, // control - { -14000000000, -8 }, // control - { -14, 1 }, // control - { 20227500, -4 }, // control - }; - for (auto p : test_values) { - double expected = p.mantissa * pow10(p.exponent); - compact_mantissa(p.mantissa, p.exponent); - double compacted = p.mantissa * pow10(p.exponent); - REQUIRE(close_enough(expected, compacted)); - } - } void test_producer_consumer() { MDProducer producer(TEST_PLATFORM); @@ -316,7 +291,7 @@ public: // Add bbo data for those expirations auto write_time = TimeStamp::now(); for (auto& p : producers) { - p.update_bbo(write_time, PriceSize::from_double<2>(5.0, 5), PriceSize::from_double<2>(5.0, 5)); + p.update_bbo(write_time, PriceSize_from_double<2>(5.0, 5), PriceSize_from_double<2>(5.0, 5)); } // Symbol list for the consumer @@ -349,7 +324,7 @@ public: PriceSize bid, ask; TimeStamp read_time; // get_boo returns true becuase this time is after the change - REQUIRE(c.get_bbo(read_time, bid, ask) == HandleReadResult::SUCCESS); + REQUIRE(c.get_direct_book(read_time, 1, &bid, &ask) == HandleReadResult::SUCCESS); REQUIRE((read_time - TimeStamp()).seconds() > 0); REQUIRE(bid.price() == 5.0); REQUIRE(ask.price() == 5.0); @@ -361,21 +336,21 @@ public: auto read_time = TimeStamp::now(); for (const auto& c : consumers) { PriceSize bid, ask; - REQUIRE(c.get_bbo(read_time, bid, ask) != HandleReadResult::SUCCESS); + REQUIRE(c.get_direct_book(read_time, 1, &bid, &ask) != HandleReadResult::SUCCESS); } // update everything once more write_time = TimeStamp::now(); for (auto& p : producers) { // Update bid/ask with one call - p.update_bbo(write_time, PriceSize::from_double<2>(6.55, 6), PriceSize::from_double<2>(6.55, 6)); + p.update_bbo(write_time, PriceSize_from_double<2>(6.55, 6), PriceSize_from_double<2>(6.55, 6)); } // Now read_time is < write_time for (const auto& c : consumers) { TimeStamp modified_time = read_time; PriceSize bid, ask; - // get_bbo returns true becuase this time is after the change - REQUIRE(c.get_bbo(modified_time, bid, ask) == HandleReadResult::SUCCESS); + // get_direct_book returns true becuase this time is after the change + REQUIRE(c.get_direct_book(modified_time, 1, &bid, &ask) == HandleReadResult::SUCCESS); REQUIRE( (modified_time - read_time).seconds() > 0); REQUIRE(bid.price() == 6.55); REQUIRE(ask.price() == 6.55); @@ -448,17 +423,17 @@ public: // Set bbo on the producers - still not public TimeStamp write_time = TimeStamp::now(); for (auto& p : producers) { - p.update_bbo(write_time, PriceSize::from_double<3>(4.5, 5), PriceSize::from_double<3>(6.75, 7)); + p.update_bbo(write_time, PriceSize_from_double<3>(4.5, 5), PriceSize_from_double<3>(6.75, 7)); } // Even though bbo is available it is not given out because the producing security does not have an id PriceSize bid, ask; for(auto& c : consumers) { TimeStamp zerotime; - REQUIRE(c.get_bbo(zerotime, bid, ask) != HandleReadResult::SUCCESS); + REQUIRE(c.get_direct_book(zerotime, 1, &bid, &ask) != HandleReadResult::SUCCESS); } for(auto& mc : managed_consumers) { TimeStamp zerotime; - REQUIRE(mc->get_bbo(zerotime, bid, ask) != HandleReadResult::SUCCESS); + REQUIRE(mc->get_direct_book(zerotime, 1, &bid, &ask) != HandleReadResult::SUCCESS); } assign_ids(producers.begin(), producers.end()); @@ -467,7 +442,7 @@ public: // Existing old handles remain invalid for(auto& c : consumers) { TimeStamp zerotime; - if (c.get_bbo(zerotime, bid, ask) == HandleReadResult::SUCCESS) { + if (c.get_direct_book(zerotime, 1, &bid, &ask) == HandleReadResult::SUCCESS) { bbo_count++; } } @@ -475,7 +450,7 @@ public: // Now, magically, the existing (managed) handles are valid for(auto& mc : managed_consumers) { TimeStamp zerotime; - if (mc->get_bbo(zerotime, bid, ask) == HandleReadResult::SUCCESS) { + if (mc->get_direct_book(zerotime, 1, &bid, &ask) == HandleReadResult::SUCCESS) { bbo_count++; } } @@ -506,11 +481,11 @@ public: PriceSize bid, ask; for(auto& c : consumers) { TimeStamp zerotime; - REQUIRE(c.get_bbo(zerotime, bid, ask) != HandleReadResult::SUCCESS); + REQUIRE(c.get_direct_book(zerotime, 1, &bid, &ask) != HandleReadResult::SUCCESS); } for(auto& mc : managed_consumers) { TimeStamp zerotime; - REQUIRE(mc->get_bbo(zerotime, bid, ask) != HandleReadResult::SUCCESS); + REQUIRE(mc->get_direct_book(zerotime, 1, &bid, &ask) != HandleReadResult::SUCCESS); } // Only NOW create the producers (and the shared memory) @@ -519,7 +494,7 @@ public: // Set bbo on the producers - still not public TimeStamp write_time = TimeStamp::now(); for (auto& p : producers) { - p.update_bbo(write_time, PriceSize::from_double<3>( 4.5, 5 ), PriceSize::from_double<3>(6.75, 7)); + p.update_bbo(write_time, PriceSize_from_double<3>( 4.5, 5 ), PriceSize_from_double<3>(6.75, 7)); } assign_ids(producers.begin(), producers.end()); @@ -528,7 +503,7 @@ public: // Existing old handles remain invalid for(auto& c : consumers) { TimeStamp zerotime; - if (c.get_bbo(zerotime, bid, ask) == HandleReadResult::SUCCESS) { + if (c.get_direct_book(zerotime, 1, &bid, &ask) == HandleReadResult::SUCCESS) { bbo_count++; } } @@ -536,7 +511,7 @@ public: // Now, magically, the existing (managed) handles are valid for(auto& mc : managed_consumers) { TimeStamp zerotime; - if (mc->get_bbo(zerotime, bid, ask) == HandleReadResult::SUCCESS) { + if (mc->get_direct_book(zerotime, 1, &bid, &ask) == HandleReadResult::SUCCESS) { bbo_count++; } } @@ -618,7 +593,7 @@ public: assign_ids(producers.begin(), producers.end()); producer1->get_channel("FOO", SECURITYTYPE_FUTURE, VENUE_BTS).heartbeat(); producer1->get_channel("FOOX", SECURITYTYPE_OPTION, VENUE_BTS).heartbeat(); - for (auto& pro : producers) { pro.update_bbo(TimeStamp::now(), PriceSize(1, 0, 1), PriceSize(2, 0, 2)); } + for (auto& pro : producers) { pro.update_bbo(TimeStamp::now(), PriceSize({1, 0}, 1), PriceSize({2, 0}, 2)); } // Symbol list for the consumer MDRecoverableConsumer consumer(get_test_symbols(), nullptr); @@ -680,7 +655,7 @@ public: // set bbo to a different value for (auto& pro : producers) { - pro.update_bbo(TimeStamp::now(), PriceSize(4, 0, 5), PriceSize(6, 0, 7)); + pro.update_bbo(TimeStamp::now(), PriceSize({4, 0}, 5), PriceSize({6, 0}, 7)); } // Attempt to recover the managed handles @@ -693,7 +668,7 @@ public: REQUIRE(handle->is_heart_beating()); TimeStamp tzero; PriceSize bid, ask; - REQUIRE(handle->get_bbo(tzero, bid, ask) == HandleReadResult::SUCCESS); + REQUIRE(handle->get_direct_book(tzero, 1, &bid, &ask) == HandleReadResult::SUCCESS); REQUIRE(bid.price() == 4.0); REQUIRE(bid.size() == 5); REQUIRE(ask.price() == 6.0); @@ -712,10 +687,10 @@ public: #pragma warning(pop) #endif TimeStamp read = TimeStamp::now(); - src->producer().update_bbo(TimeStamp::invalid(), PriceSize::from_double<3>(100.0, 100), PriceSize::from_double<3>(101.0, 101)); + src->producer().update_bbo(TimeStamp::invalid(), PriceSize_from_double<3>(100.0, 100), PriceSize_from_double<3>(101.0, 101)); PriceSize bid, ask; TimeStamp start = TimeStamp::now(); - auto result = src->consumer().get_bbo(read, bid, ask); + auto result = src->consumer().get_direct_book(read, 1, &bid, &ask); TimeStamp end = TimeStamp::now(); REQUIRE(result != HandleReadResult::SUCCESS); REQUIRE((end - start).seconds() < 5); @@ -728,7 +703,7 @@ public: vector p2_no_id = create_test_securities(producer, "2"); vector p3 = create_test_securities(producer, "3"); - for (auto& p : producers) { p.update_bbo(TimeStamp::now(), PriceSize::from_double<3>(5.0, 5), PriceSize::from_double<3>(5.0, 5)); } + for (auto& p : producers) { p.update_bbo(TimeStamp::now(), PriceSize_from_double<3>(5.0, 5), PriceSize_from_double<3>(5.0, 5)); } assign_ids(producers.begin(), producers.end()); assign_ids(p3.begin(), p3.end()); @@ -959,41 +934,41 @@ public: { co2::SettlementRecorder recorder(PLATFORM_UNSET); - recorder.add_settlement(ph1, 101, yesterday); - recorder.add_settlement(ph1, 102, today); - recorder.add_settlement(ph2, 199, yesterday); - recorder.add_settlement(ph2, 188, today); + recorder.add_settlement(ph1, {101,0}, yesterday); + recorder.add_settlement(ph1, {102,0}, today); + recorder.add_settlement(ph2, {199,0}, yesterday); + recorder.add_settlement(ph2, {188,0}, today); recorder.do_work(); recorder.do_work(); } - REQUIRE(h1.misc().settlement_price == 102); + REQUIRE(h1.settlement_price().price() == 102); REQUIRE(h1.misc().settlement_date == today); - REQUIRE(h1.misc().previous_settlement_price == 101); + REQUIRE(h1.misc().previous_settlement_price.price() == 101); REQUIRE(h1.misc().previous_settlement_date == yesterday); - REQUIRE(h2.misc().settlement_price == 188); + REQUIRE(h2.settlement_price().price() == 188); REQUIRE(h2.misc().settlement_date == today); - REQUIRE(h2.misc().previous_settlement_price == 199); + REQUIRE(h2.misc().previous_settlement_price.price() == 199); REQUIRE(h2.misc().previous_settlement_date == yesterday); // Update but skip persist auto tomorrow = datetime::add_days(today, 1); - ph1.update_settlement(TimeStamp::now(), 103, tomorrow); - REQUIRE(h1.misc().settlement_price == 103); + ph1.update_settlement(TimeStamp::now(), {103,0}, tomorrow); + REQUIRE(h1.settlement_price().price() == 103); REQUIRE(h1.misc().settlement_date == tomorrow); - REQUIRE(h1.misc().previous_settlement_price == 102); + REQUIRE(h1.misc().previous_settlement_price.price() == 102); REQUIRE(h1.misc().previous_settlement_date == today); - ph1.update_previous_settlement(TimeStamp::now(), 0, Date()); - REQUIRE(h1.misc().settlement_price == 103); + ph1.update_previous_settlement(TimeStamp::now(), {0,0}, Date()); + REQUIRE(h1.settlement_price().price() == 103); REQUIRE(h1.misc().settlement_date == tomorrow); - ph1.update_settlement(TimeStamp::now(), 0, Date()); - REQUIRE(h1.misc().settlement_price == 0); + ph1.update_settlement(TimeStamp::now(),{0,0}, Date()); + REQUIRE(h1.settlement_price().price() == 0); REQUIRE(h1.misc().settlement_date.day == 0); // pushing a new settlement for today doesn't invalidate yesterday - ph2.update_settlement(TimeStamp::now(), 201, today); - REQUIRE(h2.misc().settlement_price == 201); + ph2.update_settlement(TimeStamp::now(), {201,0}, today); + REQUIRE(h2.settlement_price().price() == 201); REQUIRE(h2.misc().settlement_date == today); - REQUIRE(h2.misc().previous_settlement_price == 199); + REQUIRE(h2.misc().previous_settlement_price.price() == 199); REQUIRE(h2.misc().previous_settlement_date == yesterday); // Restore persisted state @@ -1003,13 +978,13 @@ public: recorder.restore_settlement(ph2); recorder.do_work(); } - REQUIRE(h1.misc().settlement_price == 102); + REQUIRE(h1.settlement_price().price() == 102); REQUIRE(h1.misc().settlement_date == today); - REQUIRE(h1.misc().previous_settlement_price == 101); + REQUIRE(h1.misc().previous_settlement_price.price() == 101); REQUIRE(h1.misc().previous_settlement_date == yesterday); - REQUIRE(h2.misc().settlement_price == 188); + REQUIRE(h2.settlement_price().price() == 188); REQUIRE(h2.misc().settlement_date == today); - REQUIRE(h2.misc().previous_settlement_price == 199); + REQUIRE(h2.misc().previous_settlement_price.price() == 199); REQUIRE(h2.misc().previous_settlement_date == yesterday); } #endif @@ -1039,9 +1014,6 @@ TEST_CASE("CO2Tests","[md]") { SECTION("test_slow_double_conversion") { co2_tests->test_slow_double_conversion(); } - SECTION("test_compact_exponent") { - co2_tests->test_compact_exponent(); - } SECTION("test_producer_consumer") { co2_tests->test_producer_consumer(); } @@ -1214,35 +1186,8 @@ struct Co2IndexTests { } } ; - - -// TEST_CASE ("Co2IndexTests","[md]") { -// Co2IndexTests co2_index_tests; -// co2_index_tests.setup(); - -// SECTION("test_simple_index_search") { -// co2_index_tests.test_simple_index_search(); -// } - -// SECTION("test_simple_index_parallel_search") { -// co2_index_tests.test_simple_index_parallel_search(); -// } - -// SECTION("test_simple_index_iterate") { -// co2_index_tests.test_simple_index_iterate(); -// } -// SECTION("test_concurrent_index_search") { -// co2_index_tests.test_concurrent_index_search(); -// } -// SECTION("test_concurrent_index_iterate") { -// co2_index_tests.test_concurrent_index_iterate(); -// } -// SECTION("test_concurrent_index_parallel_search") { -// co2_index_tests.test_concurrent_index_parallel_search(); -// } -// } - struct Co2RelayTests { + Co2RelayTests() : _stats() {} void setup() { config::internal::initialize_for_test( "PlatformMap {" @@ -1250,6 +1195,7 @@ struct Co2RelayTests { " glbx_platform: 'PLATFORM_SIM'" " }" "}"); + } void teardown() {} @@ -1273,70 +1219,89 @@ struct Co2RelayTests { return temp; } + metrics::MetricsContainer _stats; + std::unique_ptr setup_query_server(MDProducer& producer, Entitlements* out_entitlements, vector* out_producers) { + // Create a bunch of securities + for (size_t x = 0; x < 100; ++x) { + // That's right. Expirations daily. + out_producers->push_back( + producer.add_security( + *mkfixedfuture("FOO", invalid_security_key, 2017, 1+(x/28), x%28))); + out_producers->push_back( + producer.add_security( + *mkfixedfuture("FOOF", invalid_security_key, 2017, 1+(x/28), x%28))); + } + assign_ids(out_producers->begin(), out_producers->end(), 1000); + + // set query server + set symbols; + symbols.insert({"FOO", VENUE_BTS, SECURITYTYPE_FUTURE}); + symbols.insert({"FOOF", VENUE_BTS, SECURITYTYPE_FUTURE}); + for (const auto& s : symbols) { + out_entitlements->insert(s.symbol()); + } + shared_ptr empty; + auto server = create_query_server(symbols, empty, _stats); + REQUIRE(server); + server->scan_securities(); + return server; + } + + void relay_nans() { MDProducer producer(TEST_PLATFORM); - UserSymbol symbol("FOO", VENUE_BTS, SECURITYTYPE_OPTION); + UserSymbol symbol("FOO", VENUE_BTS, SECURITYTYPE_FUTURE); Entitlements entitlements; - entitlements.insert(symbol.symbol()); // initialize data vector producers; - for (size_t x = 0; x < 100; ++x) { - auto strike = 1000 + (100 * x); - producers.push_back( - producer.add_security( - *mkfixedoption("FOO", invalid_security_key, 2017, 1, 28, strike, PUTORCALL_PUT))); - producers.push_back( - producer.add_security( - *mkfixedoption("FOO", invalid_security_key, 2017, 1, 28, strike, PUTORCALL_CALL))); - } - assign_ids(producers.begin(), producers.end(), 1000); - PriceSize price35 = PriceSize::from_double<2>(3.5, 5); - PriceSize price5 = PriceSize::from_double<2>(5, 5); + auto server = setup_query_server(producer, &entitlements, &producers); + + // assign prices + PriceSize price35 = PriceSize_from_double<2>(3.5, 5); + PriceSize price5 = PriceSize_from_double<2>(5, 5); for(auto p : producers) { p.update_bbo(TimeStamp::now(), price35, price35); } - // Read using QueryServer - set symbols; - symbols.insert(symbol); - shared_ptr empty; - metrics::MetricsContainer stats; - auto server = create_query_server(symbols, empty, stats); - REQUIRE(server); - server->scan_securities(); - + // Request via query server co2::MDRequest request; - auto* chan = request.add_channels(); - chan->set_type(symbol.type()); - chan->set_symbol(symbol.symbol()); - chan->set_exchange(symbol.exchange()); - chan->set_flags(co2::MDRequest::BBO | co2::MDRequest::SECURITY); + for (const auto& symbol : { "FOO", "FOOF" }) { + auto* chan = request.add_channels(); + chan->set_type(SECURITYTYPE_FUTURE); + chan->set_symbol(symbol); + chan->set_exchange(VENUE_BTS); + chan->set_flags(co2::MDRequest::BBO | co2::MDRequest::SECURITY); + } // This request should include everything co2::MDReply reply; server->query(request, &reply, &entitlements, false); reply = simulate_wire_transfer(reply); - REQUIRE((size_t)reply.channels().size() == symbols.size()); - REQUIRE((size_t)reply.channels(0).updates().size() == producers.size()); + REQUIRE((size_t)reply.channels().size() == 2); // update the request to match the server time - for (auto& update : reply.channels(0).updates()) { - REQUIRE(update.has_security()); - REQUIRE(update.has_book()); - REQUIRE(update.book().asks_size() > 0); - REQUIRE(update.book().bids_size() > 0); - REQUIRE(book_at(update.book().bids(), 0) == price35); - REQUIRE(book_at(update.book().asks(), 0) == price35); + for (int ch = 0; ch < 2; ++ch) { + REQUIRE((size_t)reply.channels(ch).updates().size() == producers.size()/2); + for (auto& update : reply.channels(ch).updates()) { + REQUIRE(update.has_security()); + REQUIRE(update.has_direct()); + REQUIRE(update.direct().asks_size() > 0); + REQUIRE(update.direct().bids_size() > 0); + REQUIRE(book_at(update.direct().bids(), 0) == price35); + REQUIRE(book_at(update.direct().asks(), 0) == price35); + } } // Second request with updated time should include nothing request.mutable_channels(0)->set_time(reply.channels(0).channel().time()); + request.mutable_channels(1)->set_time(reply.channels(1).channel().time()); reply.Clear(); server->query(request, &reply, &entitlements, false); reply = simulate_wire_transfer(reply); - REQUIRE(reply.channels().size() == 1); + REQUIRE(reply.channels().size() == 2); REQUIRE(reply.channels(0).updates().size() == 0); + REQUIRE(reply.channels(1).updates().size() == 0); // Update a single price and make sure we get that back producers[0].update_bbo(TimeStamp::now(), price5, price5, 1); @@ -1344,27 +1309,31 @@ struct Co2RelayTests { reply.Clear(); server->query(request, &reply, &entitlements, false); reply = simulate_wire_transfer(reply); - REQUIRE(reply.channels().size() == 1); + REQUIRE(reply.channels().size() == 2); REQUIRE(reply.channels(0).updates().size() == 1); - REQUIRE(reply.channels(0).updates(0).has_book()); - REQUIRE(book_at(reply.channels(0).updates(0).book().bids(), 0) == price5); - REQUIRE(book_at(reply.channels(0).updates(0).book().asks(), 0) == price5); + REQUIRE(reply.channels(1).updates().size() == 0); + REQUIRE(reply.channels(0).updates(0).has_direct()); + REQUIRE(book_at(reply.channels(0).updates(0).direct().bids(), 0) == price5); + REQUIRE(book_at(reply.channels(0).updates(0).direct().asks(), 0) == price5); // NaN out all the prices - expect to get a full set of updates for(auto p : producers) { - p.update_bbo(TimeStamp::now(), PriceSize::NO_MARKET, PriceSize::NO_MARKET, 2); + p.update_bbo(TimeStamp::now(), NO_MARKET, NO_MARKET, 2); } request.mutable_channels(0)->set_time(reply.channels(0).channel().time()); reply.Clear(); server->query(request, &reply, &entitlements, false); reply = simulate_wire_transfer(reply); - REQUIRE(reply.channels().size() == 1); - REQUIRE((size_t)reply.channels(0).updates().size() == producers.size()); - for (auto& update : reply.channels(0).updates()) { - REQUIRE(update.has_book()); - // We need to have a book to signal that it was updated but it can have no rows. This signifies an empty book and will be written as such by the relay client. - REQUIRE(book_at(update.book().bids(), 0) == PriceSize::NO_MARKET); - REQUIRE(book_at(update.book().asks(), 0) == PriceSize::NO_MARKET); + REQUIRE(reply.channels().size() == 2); + REQUIRE((size_t)reply.channels(0).updates().size() == producers.size()/2); + REQUIRE((size_t)reply.channels(1).updates().size() == producers.size()/2); + for (auto& channel : reply.channels()) { + for (auto& update : channel.updates()) { + REQUIRE(update.has_direct()); + // We need to have a book to signal that it was updated but it can have no rows. This signifies an empty book and will be written as such by the relay client. + REQUIRE(book_at(update.direct().bids(), 0) == NO_MARKET); + REQUIRE(book_at(update.direct().asks(), 0) == NO_MARKET); + } } // Fill up the markets again @@ -1372,33 +1341,188 @@ struct Co2RelayTests { p.update_bbo(TimeStamp::now(), price35, price35, 3); } request.mutable_channels(0)->set_time(reply.channels(0).channel().time()); + request.mutable_channels(1)->set_time(reply.channels(1).channel().time()); reply.Clear(); server->query(request, &reply, &entitlements, false); reply = simulate_wire_transfer(reply); - REQUIRE(reply.channels().size() == 1); - REQUIRE((size_t)reply.channels(0).updates().size() == producers.size()); - for (auto& update : reply.channels(0).updates()) { - REQUIRE(update.has_book()); - REQUIRE(update.book().bids_size() > 0); - REQUIRE(update.book().asks_size() > 0); - REQUIRE(book_at(update.book().bids(), 0) == price35); - REQUIRE(book_at(update.book().asks(), 0) == price35); + REQUIRE(reply.channels().size() == 2); + REQUIRE((size_t)reply.channels(0).updates().size() == producers.size()/2); + REQUIRE((size_t)reply.channels(1).updates().size() == producers.size()/2); + for (auto& channel : reply.channels()) { + for (auto& update : channel.updates()) { + REQUIRE(update.has_direct()); + REQUIRE(update.direct().bids_size() > 0); + REQUIRE(update.direct().asks_size() > 0); + REQUIRE(book_at(update.direct().bids(), 0) == price35); + REQUIRE(book_at(update.direct().asks(), 0) == price35); + } } // NaN out the bids for(auto p : producers) { - p.update_bbo(TimeStamp::now(), PriceSize::NO_MARKET, price35, 4); + p.update_bbo(TimeStamp::now(), NO_MARKET, price35, 4); } request.mutable_channels(0)->set_time(reply.channels(0).channel().time()); + request.mutable_channels(1)->set_time(reply.channels(1).channel().time()); reply.Clear(); server->query(request, &reply, &entitlements, false); reply = simulate_wire_transfer(reply); - REQUIRE(reply.channels().size() == 1); - REQUIRE((size_t)reply.channels(0).updates().size() == producers.size()); - for (auto& update : reply.channels(0).updates()) { - REQUIRE(update.has_book()); - REQUIRE(book_at(update.book().bids(), 0) == PriceSize::NO_MARKET); - REQUIRE(book_at(update.book().asks(), 0) == price35); + REQUIRE(reply.channels().size() == 2); + REQUIRE((size_t)reply.channels(0).updates().size() == producers.size()/2); + REQUIRE((size_t)reply.channels(1).updates().size() == producers.size()/2); + for (auto& channel : reply.channels()) { + for (auto& update : channel.updates()) { + REQUIRE(update.has_direct()); + REQUIRE(book_at(update.direct().bids(), 0) == NO_MARKET); + REQUIRE(book_at(update.direct().asks(), 0) == price35); + } + } + } + + void relay_implied() { + MDProducer producer(TEST_PLATFORM); + + // initialize data + vector producers; + Entitlements entitlements; + auto server = setup_query_server(producer, &entitlements, &producers); + + // assign prices to all levels + auto set_direct = [&](PriceSize bid, PriceSize ask) { + for(auto p : producers) { + std::array bids, asks; + for (auto& p : bids) p = bid; + for (auto& p : asks) p = ask; + p.update_direct(TimeStamp::now(), bids, asks); + } + }; + auto set_implied = [&](PriceSize bid, PriceSize ask) { + for(auto p : producers) { + std::array implied_bids, implied_asks; + for (auto& p : implied_bids) p = bid; + for (auto& p : implied_asks) p = ask; + p.update_implied(TimeStamp::now(), implied_bids, implied_asks); + } + }; + PriceSize price35 = PriceSize_from_double<7>(3.5, 5); + PriceSize price5 = PriceSize_from_double<7>(5, 5); + set_direct(price35, price5); + set_implied(price35, price5); + + // Form the request + co2::MDRequest request; + auto* chan = request.add_channels(); + chan->set_type(SECURITYTYPE_FUTURE); + chan->set_symbol("FOO"); + chan->set_exchange(VENUE_BTS); + chan->set_flags(co2::MDRequest::DIRECT | co2::MDRequest::IMPLIED); + + // Send the request + co2::MDReply reply; + server->query(request, &reply, &entitlements, false); + reply = simulate_wire_transfer(reply); + + // check price levels + REQUIRE(reply.channels_size() > 0); + for (auto& channel : reply.channels()) { + for (auto& update : channel.updates()) { + REQUIRE(update.has_direct()); + REQUIRE(update.has_implied()); + REQUIRE(update.direct().bids_size() == COCONUT_MAX_DEPTH); + REQUIRE(update.direct().asks_size() == COCONUT_MAX_DEPTH); + REQUIRE(update.implied().bids_size() == COCONUT_MAX_IMPLIED_DEPTH); + REQUIRE(update.implied().asks_size() == COCONUT_MAX_IMPLIED_DEPTH); + for (size_t x = 0; x < COCONUT_MAX_DEPTH; ++x) { + REQUIRE(book_at(update.direct().bids(), x) == price35); + REQUIRE(book_at(update.direct().asks(), x) == price5); + } + for (size_t x = 0; x < COCONUT_MAX_IMPLIED_DEPTH; ++x) { + REQUIRE(book_at(update.implied().bids(), x) == price35); + REQUIRE(book_at(update.implied().asks(), x) == price5); + } + } + } + + // + // Nan out implied + request.mutable_channels(0)->set_time(reply.channels(0).channel().time()); + set_implied(NO_MARKET, NO_MARKET); + reply.Clear(); + server->query(request, &reply, &entitlements, false); + reply = simulate_wire_transfer(reply); + + for (auto& channel : reply.channels()) { + for (auto& update : channel.updates()) { + REQUIRE(update.has_direct()); + REQUIRE(update.has_implied()); + REQUIRE(update.direct().bids_size() == COCONUT_MAX_DEPTH); + REQUIRE(update.direct().asks_size() == COCONUT_MAX_DEPTH); + REQUIRE(update.implied().bids_size() == 0); + REQUIRE(update.implied().asks_size() == 0); + for (size_t x = 0; x < COCONUT_MAX_DEPTH; ++x) { + REQUIRE(book_at(update.direct().bids(), x) == price35); + REQUIRE(book_at(update.direct().asks(), x) == price5); + } + for (size_t x = 0; x < COCONUT_MAX_IMPLIED_DEPTH; ++x) { + REQUIRE(book_at(update.implied().bids(), x) == NO_MARKET); + REQUIRE(book_at(update.implied().asks(), x) == NO_MARKET); + } + } + } + + // + // Nan out direct + request.mutable_channels(0)->set_time(reply.channels(0).channel().time()); + set_direct(NO_MARKET, NO_MARKET); + reply.Clear(); + server->query(request, &reply, &entitlements, false); + reply = simulate_wire_transfer(reply); + + for (auto& channel : reply.channels()) { + for (auto& update : channel.updates()) { + REQUIRE(update.has_direct()); + REQUIRE(update.has_implied()); + REQUIRE(update.direct().bids_size() == 0); + REQUIRE(update.direct().asks_size() == 0); + REQUIRE(update.implied().bids_size() == 0); + REQUIRE(update.implied().asks_size() == 0); + for (size_t x = 0; x < COCONUT_MAX_DEPTH; ++x) { + REQUIRE(book_at(update.direct().bids(), x) == NO_MARKET); + REQUIRE(book_at(update.direct().asks(), x) == NO_MARKET); + } + for (size_t x = 0; x < COCONUT_MAX_IMPLIED_DEPTH; ++x) { + REQUIRE(book_at(update.implied().bids(), x) == NO_MARKET); + REQUIRE(book_at(update.implied().asks(), x) == NO_MARKET); + } + } + } + + // + // restore values + request.mutable_channels(0)->set_time(reply.channels(0).channel().time()); + set_direct(price5, price35); + set_implied(price5, price35); + reply.Clear(); + server->query(request, &reply, &entitlements, false); + reply = simulate_wire_transfer(reply); + + for (auto& channel : reply.channels()) { + for (auto& update : channel.updates()) { + REQUIRE(update.has_direct()); + REQUIRE(update.has_implied()); + REQUIRE(update.direct().bids_size() == COCONUT_MAX_DEPTH); + REQUIRE(update.direct().asks_size() == COCONUT_MAX_DEPTH); + REQUIRE(update.implied().bids_size() == COCONUT_MAX_IMPLIED_DEPTH); + REQUIRE(update.implied().asks_size() == COCONUT_MAX_IMPLIED_DEPTH); + for (size_t x = 0; x < COCONUT_MAX_DEPTH; ++x) { + REQUIRE(book_at(update.direct().bids(), x) == price5); + REQUIRE(book_at(update.direct().asks(), x) == price35); + } + for (size_t x = 0; x < COCONUT_MAX_IMPLIED_DEPTH; ++x) { + REQUIRE(book_at(update.implied().bids(), x) == price5); + REQUIRE(book_at(update.implied().asks(), x) == price35); + } + } } } } ; @@ -1411,13 +1535,753 @@ TEST_CASE ("Co2RelayTests","[md]") SECTION("relay_nans") { co2_relay_tests.relay_nans(); } + SECTION("relay_implied") { + co2_relay_tests.relay_implied(); + } } +namespace ImpliedTests { + void test_merge(const std::array& direct_bid, + const std::array& implied_bid, + const std::array& direct_ask, + const std::array& implied_ask, + const std::function&)>& validate) { + MDProducer producer(TEST_PLATFORM); + vector producers = create_test_securities(producer); + auto write_time = TimeStamp::now(); + + // update both direct and implied + for (auto& p : producers) { + p.update_direct(write_time, direct_bid, direct_ask, 0); + p.update_implied(write_time, implied_bid, implied_ask, 1); + } + // read from both + set symbols; + UserSymbol future_symbol( "FOO", VENUE_BTS, SECURITYTYPE_FUTURE ); + symbols.insert(future_symbol); + UserSymbol option_symbol( "FOOX", VENUE_BTS, SECURITYTYPE_OPTION ); + symbols.insert(option_symbol); + MDConsumer consumer(symbols, nullptr); + vector consumer_securities; + // Poll for security list - none visible until they have keys + size_t count = consumer.walk_security_list([&](const Security& security, MDConsumerHandle) {consumer_securities.push_back(security);}); + // Since no security has an id none are "public" + REQUIRE(consumer_securities.size() == count); + REQUIRE(count == 0); + assign_ids(producers.begin(), producers.end()); + // Now securities can be enumerated by the client + count = consumer.walk_security_list([&](const Security& security, MDConsumerHandle) {consumer_securities.push_back(security);}); + REQUIRE(consumer_securities.size() == count); + REQUIRE(count == 24); + vector consumers; + for(const auto& security : consumer_securities) { + consumers.push_back(consumer.get_handle(security.id())); + } -TEST_CASE("Co2SyntheticsTests", "[md]") { - // test synthetics show up with other securities - // test synehtetics with legs of synthetics - // test adding a synthetic with synthetic legs later in the day + validate(consumers); + } +} + +TEST_CASE("MergeImpliedTests", "[md]") { + config::internal::initialize_for_test( + "PlatformMap {" + " value {" + " glbx_platform: 'PLATFORM_SIM'" + " }" + "}"); + // create a handle with implied and direct + std::array direct_bid, direct_ask; + std::array implied_bid, implied_ask; + + SECTION("equal_impiled_deep") { + for (size_t x = 0; x < direct_bid.size(); ++x) { + direct_bid[x] = PriceSize_from_double<7>(x * 100, 2); + if (x < implied_bid.size()) { + implied_bid[x] = PriceSize_from_double<7>(x * 100, 1); + } + } + direct_ask = direct_bid; + implied_ask = implied_bid; + ImpliedTests::test_merge(direct_bid,implied_bid, direct_ask, implied_ask, [](const vector& consumers) { + for (const auto& c : consumers) { + std::array bids, asks; + TimeStamp read_time; + // get_book returns true becuase this time is after the change + REQUIRE(c.get_direct_book(read_time, COCONUT_MAX_DEPTH, &bids[0], &asks[0]) == HandleReadResult::SUCCESS); + for (size_t x = 0; x < COCONUT_MAX_DEPTH; ++x) { + REQUIRE(bids[x].size() == 2); + REQUIRE(bids[x].direct_size() == 2); + REQUIRE(bids[x].implied_size() == 0); + REQUIRE(asks[x].size() == 2); + REQUIRE(asks[x].direct_size() == 2); + REQUIRE(asks[x].implied_size() == 0); + } + read_time = TimeStamp(); + REQUIRE(c.get_implied_book(read_time, COCONUT_MAX_IMPLIED_DEPTH, &bids[0], &asks[0]) == HandleReadResult::SUCCESS); + for (size_t x = 0; x < COCONUT_MAX_IMPLIED_DEPTH; ++x) { + REQUIRE(bids[x].size() == 1); + REQUIRE(asks[x].size() == 1); + + REQUIRE(bids[x].direct_size() == 0); + REQUIRE(bids[x].implied_size() == 1); + REQUIRE(asks[x].direct_size() == 0); + REQUIRE(asks[x].implied_size() == 1); + } + read_time = TimeStamp(); + REQUIRE(c.get_book(read_time, COCONUT_MAX_IMPLIED_DEPTH, &bids[0], &asks[0]) == HandleReadResult::SUCCESS); + for (size_t x = 0; x < COCONUT_MAX_DEPTH; ++x) { + if (x < COCONUT_MAX_IMPLIED_DEPTH) { + REQUIRE(bids[x].size() == 3); + REQUIRE(asks[x].size() == 3); + + REQUIRE(bids[x].direct_size() == 2); + REQUIRE(bids[x].implied_size() == 1); + REQUIRE(asks[x].direct_size() == 2); + REQUIRE(asks[x].implied_size() == 1); + } + else { + REQUIRE(bids[x].size() == 2); + REQUIRE(asks[x].size() == 2); + + REQUIRE(bids[x].direct_size() == 2); + REQUIRE(bids[x].implied_size() == 0); + REQUIRE(asks[x].direct_size() == 2); + REQUIRE(asks[x].implied_size() == 0); + } + } + } + }); + } + + SECTION("partial_Read") { + direct_bid[0] = { {700, 1}, 1 }; + direct_bid[1] = { {600, 1}, 1 }; + direct_bid[2] = { {500, 1}, 1 }; + direct_bid[3] = { {400, 1}, 1 }; + + direct_ask[0] = { {800, 1}, 1 }; + direct_ask[1] = { {900, 1}, 1 }; + direct_ask[2] = { {1000, 1}, 1 }; + direct_ask[3] = { {1100, 1}, 1 }; + + implied_bid[0] = { {700, 1}, 2 }; + implied_bid[1] = { {600, 1}, 2 }; + + implied_ask[0] = { {800, 1}, 2 }; + implied_ask[1] = { {900, 1}, 2 }; + + + ImpliedTests::test_merge(direct_bid,implied_bid,direct_ask,implied_ask, [](const vector& consumers) { + const size_t TEST_DEPTH = 4; + for (const auto& c : consumers) { + std::array bids, asks; + int canary = 0; + TimeStamp read_time; + // get_boo returns true becuase this time is after the change + REQUIRE(c.get_direct_book(read_time, TEST_DEPTH, &bids[0], &asks[0]) == HandleReadResult::SUCCESS); + for (size_t x = 0; x < TEST_DEPTH; ++x) { + REQUIRE(bids[x].size() == 1); + REQUIRE(asks[x].size() == 1); + REQUIRE(bids[x].direct_size() == 1); + REQUIRE(bids[x].implied_size() == 0); + REQUIRE(asks[x].direct_size() == 1); + REQUIRE(asks[x].implied_size() == 0); + } + read_time = TimeStamp(); + REQUIRE(c.get_implied_book(read_time, COCONUT_MAX_IMPLIED_DEPTH, &bids[0], &asks[0]) == HandleReadResult::SUCCESS); + for (size_t x = 0; x < COCONUT_MAX_IMPLIED_DEPTH; ++x) { + REQUIRE(bids[x].size() == 2); + REQUIRE(asks[x].size() == 2); + REQUIRE(bids[x].direct_size() == 0); + REQUIRE(bids[x].implied_size() == 2); + REQUIRE(asks[x].direct_size() == 0); + REQUIRE(asks[x].implied_size() == 2); + } + read_time = TimeStamp(); + REQUIRE(c.get_book(read_time, TEST_DEPTH, &bids[0], &asks[0]) == HandleReadResult::SUCCESS); + for (size_t x = 0; x < TEST_DEPTH; ++x) { + if (x < COCONUT_MAX_IMPLIED_DEPTH) { + REQUIRE(bids[x].size() == 3); + REQUIRE(asks[x].size() == 3); + REQUIRE(bids[x].direct_size() == 1); + REQUIRE(bids[x].implied_size() == 2); + REQUIRE(asks[x].direct_size() == 1); + REQUIRE(asks[x].implied_size() == 2); + } + else { + REQUIRE(bids[x].size() == 1); + REQUIRE(asks[x].size() == 1); + REQUIRE(bids[x].direct_size() == 1); + REQUIRE(bids[x].implied_size() == 0); + REQUIRE(asks[x].direct_size() == 1); + REQUIRE(asks[x].implied_size() == 0); + } + } + REQUIRE(canary == 0); + } + }); + } + + SECTION("better_implied") { + direct_bid[0] = { {500, 1}, 1 }; + direct_bid[1] = { {400, 1}, 1 }; + direct_bid[2] = { {300, 1}, 1 }; + direct_bid[3] = { {200, 1}, 1 }; + + direct_ask[0] = { {1000, 1}, 1 }; + direct_ask[1] = { {1100, 1}, 1 }; + direct_ask[2] = { {1200, 1}, 1 }; + direct_ask[3] = { {1300, 1}, 1 }; + + implied_bid[0] = { {600, 1}, 2 }; + implied_bid[1] = { {500, 1}, 2 }; + + implied_ask[0] = { {900, 1}, 2 }; + implied_ask[1] = { {1000, 1}, 2 }; + + + ImpliedTests::test_merge(direct_bid,implied_bid,direct_ask,implied_ask, [=](const vector& consumers) { + const size_t TEST_DEPTH = 4; + for (const auto& c : consumers) { + std::array bids, asks; + int canary = 0; + TimeStamp read_time; + // get_boo returns true becuase this time is after the change + REQUIRE(c.get_direct_book(read_time, TEST_DEPTH, &bids[0], &asks[0]) == HandleReadResult::SUCCESS); + for (size_t x = 0; x < TEST_DEPTH; ++x) { + REQUIRE(bids[x].size() == 1); + REQUIRE(asks[x].size() == 1); + REQUIRE(bids[x].direct_size() == 1); + REQUIRE(bids[x].implied_size() == 0); + REQUIRE(asks[x].direct_size() == 1); + REQUIRE(asks[x].implied_size() == 0); + } + read_time = TimeStamp(); + REQUIRE(c.get_implied_book(read_time, COCONUT_MAX_IMPLIED_DEPTH, &bids[0], &asks[0]) == HandleReadResult::SUCCESS); + for (size_t x = 0; x < COCONUT_MAX_IMPLIED_DEPTH; ++x) { + REQUIRE(bids[x].size() == 2); + REQUIRE(asks[x].size() == 2); + REQUIRE(bids[x].direct_size() == 0); + REQUIRE(bids[x].implied_size() == 2); + REQUIRE(asks[x].direct_size() == 0); + REQUIRE(asks[x].implied_size() == 2); + } + read_time = TimeStamp(); + REQUIRE(c.get_book(read_time, TEST_DEPTH, &bids[0], &asks[0]) == HandleReadResult::SUCCESS); + REQUIRE(bids[0].size() == 2); + REQUIRE(bids[0].mantissa() == 600); + REQUIRE(asks[0].size() == 2); + REQUIRE(asks[0].mantissa() == 900); + REQUIRE(bids[1].size() == 3); + REQUIRE(asks[1].size() == 3); + REQUIRE(bids[1].direct_size() == 1); + REQUIRE(bids[1].implied_size() == 2); + REQUIRE(asks[1].direct_size() == 1); + REQUIRE(asks[1].implied_size() == 2); + for (size_t x = 2; x < TEST_DEPTH; ++x) { + REQUIRE(bids[x].size() == 1); + REQUIRE(asks[x].size() == 1); + REQUIRE(bids[x].direct_size() == 1); + REQUIRE(bids[x].implied_size() == 0); + REQUIRE(asks[x].direct_size() == 1); + REQUIRE(asks[x].implied_size() == 0); + } + REQUIRE(canary == 0); + } + }); + } + + SECTION("worse_implied") { + direct_bid[0] = { {500, 1}, 1 }; + direct_bid[1] = { {400, 1}, 1 }; + direct_bid[2] = { {300, 1}, 1 }; + direct_bid[3] = { {200, 1}, 1 }; + + direct_ask[0] = { {1000, 1}, 1 }; + direct_ask[1] = { {1100, 1}, 1 }; + direct_ask[2] = { {1200, 1}, 1 }; + direct_ask[3] = { {1300, 1}, 1 }; + + implied_bid[0] = { {400, 1}, 2 }; + implied_bid[1] = { {300, 1}, 2 }; + + implied_ask[0] = { {1100, 1}, 2 }; + implied_ask[1] = { {1200, 1}, 2 }; + + + ImpliedTests::test_merge(direct_bid,implied_bid,direct_ask,implied_ask, [=](const vector& consumers) { + const size_t TEST_DEPTH = 4; + for (const auto& c : consumers) { + std::array bids, asks; + int canary = 0; + TimeStamp read_time; + // get_boo returns true becuase this time is after the change + REQUIRE(c.get_direct_book(read_time, TEST_DEPTH, &bids[0], &asks[0]) == HandleReadResult::SUCCESS); + for (size_t x = 0; x < TEST_DEPTH; ++x) { + REQUIRE(bids[x].size() == 1); + REQUIRE(asks[x].size() == 1); + REQUIRE(bids[x].direct_size() == 1); + REQUIRE(bids[x].implied_size() == 0); + REQUIRE(asks[x].direct_size() == 1); + REQUIRE(asks[x].implied_size() == 0); + } + read_time = TimeStamp(); + REQUIRE(c.get_implied_book(read_time, COCONUT_MAX_IMPLIED_DEPTH, &bids[0], &asks[0]) == HandleReadResult::SUCCESS); + for (size_t x = 0; x < COCONUT_MAX_IMPLIED_DEPTH; ++x) { + REQUIRE(bids[x].size() == 2); + REQUIRE(asks[x].size() == 2); + REQUIRE(bids[x].direct_size() == 0); + REQUIRE(bids[x].implied_size() == 2); + REQUIRE(asks[x].direct_size() == 0); + REQUIRE(asks[x].implied_size() == 2); + } + read_time = TimeStamp(); + REQUIRE(c.get_book(read_time, TEST_DEPTH, &bids[0], &asks[0]) == HandleReadResult::SUCCESS); + REQUIRE(bids[0].mantissa() == 500); + REQUIRE(bids[0].size() == 1); + REQUIRE(asks[0].mantissa() == 1000); + REQUIRE(asks[0].size() == 1); + REQUIRE(bids[0].direct_size() == 1); + REQUIRE(bids[0].implied_size() == 0); + REQUIRE(asks[0].direct_size() == 1); + REQUIRE(asks[0].implied_size() == 0); + + REQUIRE(bids[1].mantissa() == 400); + REQUIRE(bids[1].size() == 3); + REQUIRE(asks[1].mantissa() == 1100); + REQUIRE(asks[1].size() == 3); + REQUIRE(bids[1].direct_size() == 1); + REQUIRE(bids[1].implied_size() == 2); + REQUIRE(asks[1].direct_size() == 1); + REQUIRE(asks[1].implied_size() == 2); + + REQUIRE(bids[2].mantissa() == 300); + REQUIRE(bids[2].size() == 3); + REQUIRE(asks[2].mantissa() == 1200); + REQUIRE(asks[2].size() == 3); + REQUIRE(bids[2].direct_size() == 1); + REQUIRE(bids[2].implied_size() == 2); + REQUIRE(asks[2].direct_size() == 1); + REQUIRE(asks[2].implied_size() == 2); + + REQUIRE(bids[3].mantissa() == 200); + REQUIRE(bids[3].size() == 1); + REQUIRE(asks[3].mantissa() == 1300); + REQUIRE(asks[3].size() == 1); + REQUIRE(bids[3].direct_size() == 1); + REQUIRE(bids[3].implied_size() == 0); + REQUIRE(asks[3].direct_size() == 1); + REQUIRE(asks[3].implied_size() == 0); + + REQUIRE(canary == 0); + } + }); + } + + SECTION("worse_implied_bid") { + direct_bid[0] = { {500, 1}, 1 }; + direct_bid[1] = { {400, 1}, 1 }; + direct_bid[2] = { {300, 1}, 1 }; + direct_bid[3] = { {200, 1}, 1 }; + + direct_ask[0] = { {1000, 1}, 1 }; + direct_ask[1] = { {1100, 1}, 1 }; + direct_ask[2] = { {1200, 1}, 1 }; + direct_ask[3] = { {1300, 1}, 1 }; + + implied_bid[0] = { {400, 1}, 2 }; + implied_bid[1] = { {300, 1}, 2 }; + + implied_ask[0] = { {1000, 1}, 2 }; + implied_ask[1] = { {1100, 1}, 2 }; + + + ImpliedTests::test_merge(direct_bid,implied_bid,direct_ask,implied_ask, [=](const vector& consumers) { + const size_t TEST_DEPTH = 4; + for (const auto& c : consumers) { + std::array bids, asks; + int canary = 0; + TimeStamp read_time; + // get_boo returns true becuase this time is after the change + REQUIRE(c.get_direct_book(read_time, TEST_DEPTH, &bids[0], &asks[0]) == HandleReadResult::SUCCESS); + for (size_t x = 0; x < TEST_DEPTH; ++x) { + REQUIRE(bids[x].size() == 1); + REQUIRE(asks[x].size() == 1); + REQUIRE(bids[x].direct_size() == 1); + REQUIRE(bids[x].implied_size() == 0); + REQUIRE(asks[x].direct_size() == 1); + REQUIRE(asks[x].implied_size() == 0); + } + read_time = TimeStamp(); + REQUIRE(c.get_implied_book(read_time, COCONUT_MAX_IMPLIED_DEPTH, &bids[0], &asks[0]) == HandleReadResult::SUCCESS); + for (size_t x = 0; x < COCONUT_MAX_IMPLIED_DEPTH; ++x) { + REQUIRE(bids[x].size() == 2); + REQUIRE(asks[x].size() == 2); + REQUIRE(bids[x].direct_size() == 0); + REQUIRE(bids[x].implied_size() == 2); + REQUIRE(asks[x].direct_size() == 0); + REQUIRE(asks[x].implied_size() == 2); + } + read_time = TimeStamp(); + REQUIRE(c.get_book(read_time, TEST_DEPTH, &bids[0], &asks[0]) == HandleReadResult::SUCCESS); + REQUIRE(bids[0].mantissa() == 500); + REQUIRE(bids[0].size() == 1); + REQUIRE(asks[0].mantissa() == 1000); + REQUIRE(asks[0].size() == 3); + REQUIRE(bids[0].direct_size() == 1); + REQUIRE(bids[0].implied_size() == 0); + REQUIRE(asks[0].direct_size() == 1); + REQUIRE(asks[0].implied_size() == 2); + + REQUIRE(bids[1].mantissa() == 400); + REQUIRE(bids[1].size() == 3); + REQUIRE(asks[1].mantissa() == 1100); + REQUIRE(asks[1].size() == 3); + REQUIRE(bids[1].direct_size() == 1); + REQUIRE(bids[1].implied_size() == 2); + REQUIRE(asks[1].direct_size() == 1); + REQUIRE(asks[1].implied_size() == 2); + + REQUIRE(bids[2].mantissa() == 300); + REQUIRE(bids[2].size() == 3); + REQUIRE(asks[2].mantissa() == 1200); + REQUIRE(asks[2].size() == 1); + REQUIRE(bids[2].direct_size() == 1); + REQUIRE(bids[2].implied_size() == 2); + REQUIRE(asks[2].direct_size() == 1); + REQUIRE(asks[2].implied_size() == 0); + + REQUIRE(bids[3].mantissa() == 200); + REQUIRE(bids[3].size() == 1); + REQUIRE(asks[3].mantissa() == 1300); + REQUIRE(asks[3].size() == 1); + REQUIRE(bids[3].direct_size() == 1); + REQUIRE(bids[3].implied_size() == 0); + REQUIRE(asks[3].direct_size() == 1); + REQUIRE(asks[3].implied_size() == 0); + + REQUIRE(canary == 0); + } + }); + } + SECTION("worse_implied_ask") { + direct_bid[0] = { {500, 1}, 1 }; + direct_bid[1] = { {400, 1}, 1 }; + direct_bid[2] = { {300, 1}, 1 }; + direct_bid[3] = { {200, 1}, 1 }; + + direct_ask[0] = { {1000, 1}, 1 }; + direct_ask[1] = { {1100, 1}, 1 }; + direct_ask[2] = { {1200, 1}, 1 }; + direct_ask[3] = { {1300, 1}, 1 }; + + implied_bid[0] = { {500, 1}, 2 }; + implied_bid[1] = { {400, 1}, 2 }; + + implied_ask[0] = { {1300, 1}, 2 }; + implied_ask[1] = { {1400, 1}, 2 }; + + + ImpliedTests::test_merge(direct_bid,implied_bid,direct_ask,implied_ask, [=](const vector& consumers) { + const size_t TEST_DEPTH = 4; + for (const auto& c : consumers) { + std::array bids, asks; + int canary = 0; + TimeStamp read_time; + // get_boo returns true becuase this time is after the change + REQUIRE(c.get_direct_book(read_time, TEST_DEPTH, &bids[0], &asks[0]) == HandleReadResult::SUCCESS); + for (size_t x = 0; x < TEST_DEPTH; ++x) { + REQUIRE(bids[x].size() == 1); + REQUIRE(asks[x].size() == 1); + REQUIRE(bids[x].direct_size() == 1); + REQUIRE(bids[x].implied_size() == 0); + REQUIRE(asks[x].direct_size() == 1); + REQUIRE(asks[x].implied_size() == 0); + } + read_time = TimeStamp(); + REQUIRE(c.get_implied_book(read_time, COCONUT_MAX_IMPLIED_DEPTH, &bids[0], &asks[0]) == HandleReadResult::SUCCESS); + for (size_t x = 0; x < COCONUT_MAX_IMPLIED_DEPTH; ++x) { + REQUIRE(bids[x].size() == 2); + REQUIRE(asks[x].size() == 2); + REQUIRE(bids[x].direct_size() == 0); + REQUIRE(bids[x].implied_size() == 2); + REQUIRE(asks[x].direct_size() == 0); + REQUIRE(asks[x].implied_size() == 2); + } + read_time = TimeStamp(); + REQUIRE(c.get_book(read_time, TEST_DEPTH+1, &bids[0], &asks[0]) == HandleReadResult::SUCCESS); + REQUIRE(bids[0].mantissa() == 500); + REQUIRE(bids[0].size() == 3); + REQUIRE(asks[0].mantissa() == 1000); + REQUIRE(asks[0].size() == 1); + REQUIRE(bids[0].direct_size() == 1); + REQUIRE(bids[0].implied_size() == 2); + REQUIRE(asks[0].direct_size() == 1); + REQUIRE(asks[0].implied_size() == 0); + + REQUIRE(bids[1].mantissa() == 400); + REQUIRE(bids[1].size() == 3); + REQUIRE(asks[1].mantissa() == 1100); + REQUIRE(asks[1].size() == 1); + REQUIRE(bids[1].direct_size() == 1); + REQUIRE(bids[1].implied_size() == 2); + REQUIRE(asks[1].direct_size() == 1); + REQUIRE(asks[1].implied_size() == 0); + + REQUIRE(bids[2].mantissa() == 300); + REQUIRE(bids[2].size() == 1); + REQUIRE(asks[2].mantissa() == 1200); + REQUIRE(asks[2].size() == 1); + REQUIRE(bids[2].direct_size() == 1); + REQUIRE(bids[2].implied_size() == 0); + REQUIRE(asks[2].direct_size() == 1); + REQUIRE(asks[2].implied_size() == 0); + + REQUIRE(bids[3].mantissa() == 200); + REQUIRE(bids[3].size() == 1); + REQUIRE(asks[3].mantissa() == 1300); + REQUIRE(asks[3].size() == 3); + REQUIRE(bids[3].direct_size() == 1); + REQUIRE(bids[3].implied_size() == 0); + REQUIRE(asks[3].direct_size() == 1); + REQUIRE(asks[3].implied_size() == 2); + + REQUIRE(bids[4].mantissa() == 0); + REQUIRE(bids[4].size() == 0); + REQUIRE(asks[4].mantissa() == 1400); + REQUIRE(asks[4].size() == 2); + REQUIRE(bids[4].direct_size() == 0); + REQUIRE(bids[4].implied_size() == 0); + REQUIRE(asks[4].direct_size() == 0); + REQUIRE(asks[4].implied_size() == 2); + + REQUIRE(canary == 0); + } + }); + } + + SECTION("nan_implied") { + direct_bid[0] = { {500, 1}, 1 }; + direct_bid[1] = { {400, 1}, 1 }; + direct_bid[2] = { {300, 1}, 1 }; + direct_bid[3] = { {200, 1}, 1 }; + + direct_ask[0] = { {1000, 1}, 1 }; + direct_ask[1] = { {1100, 1}, 1 }; + direct_ask[2] = { {1200, 1}, 1 }; + direct_ask[3] = { {1300, 1}, 1 }; + + implied_bid[0] = { INT_NAN, 0 }; + implied_bid[1] = { INT_NAN, 0 }; + + implied_ask[0] = { INT_NAN, 0 }; + implied_ask[1] = { INT_NAN, 0 }; + + + ImpliedTests::test_merge(direct_bid,implied_bid,direct_ask,implied_ask, [=](const vector& consumers) { + const size_t TEST_DEPTH = 4; + for (const auto& c : consumers) { + std::array bids, asks; + int canary = 0; + TimeStamp read_time; + // get_boo returns true becuase this time is after the change + REQUIRE(c.get_direct_book(read_time, TEST_DEPTH, &bids[0], &asks[0]) == HandleReadResult::SUCCESS); + for (size_t x = 0; x < TEST_DEPTH; ++x) { + REQUIRE(bids[x].size() == 1); + REQUIRE(asks[x].size() == 1); + REQUIRE(bids[x].direct_size() == 1); + REQUIRE(bids[x].implied_size() == 0); + REQUIRE(asks[x].direct_size() == 1); + REQUIRE(asks[x].implied_size() == 0); + } + read_time = TimeStamp(); + REQUIRE(c.get_implied_book(read_time, COCONUT_MAX_IMPLIED_DEPTH, &bids[0], &asks[0]) == HandleReadResult::SUCCESS); + for (size_t x = 0; x < COCONUT_MAX_IMPLIED_DEPTH; ++x) { + REQUIRE(bids[x].size() == 0); + REQUIRE(asks[x].size() == 0); + REQUIRE(bids[x].direct_size() == 0); + REQUIRE(bids[x].implied_size() == 0); + REQUIRE(asks[x].direct_size() == 0); + REQUIRE(asks[x].implied_size() == 0); + } + read_time = TimeStamp(); + REQUIRE(c.get_book(read_time, TEST_DEPTH, &bids[0], &asks[0]) == HandleReadResult::SUCCESS); + REQUIRE(bids[0].mantissa() == 500); + REQUIRE(bids[0].size() == 1); + REQUIRE(asks[0].mantissa() == 1000); + REQUIRE(asks[0].size() == 1); + REQUIRE(bids[0].direct_size() == 1); + REQUIRE(bids[0].implied_size() == 0); + REQUIRE(asks[0].direct_size() == 1); + REQUIRE(asks[0].implied_size() == 0); + + REQUIRE(bids[1].mantissa() == 400); + REQUIRE(bids[1].size() == 1); + REQUIRE(asks[1].mantissa() == 1100); + REQUIRE(asks[1].size() == 1); + REQUIRE(bids[1].direct_size() == 1); + REQUIRE(bids[1].implied_size() == 0); + REQUIRE(asks[1].direct_size() == 1); + REQUIRE(asks[1].implied_size() == 0); + + REQUIRE(bids[2].mantissa() == 300); + REQUIRE(bids[2].size() == 1); + REQUIRE(asks[2].mantissa() == 1200); + REQUIRE(asks[2].size() == 1); + REQUIRE(bids[2].direct_size() == 1); + REQUIRE(bids[2].implied_size() == 0); + REQUIRE(asks[2].direct_size() == 1); + REQUIRE(asks[2].implied_size() == 0); + + REQUIRE(bids[3].mantissa() == 200); + REQUIRE(bids[3].size() == 1); + REQUIRE(asks[3].mantissa() == 1300); + REQUIRE(asks[3].size() == 1); + REQUIRE(bids[3].direct_size() == 1); + REQUIRE(bids[3].implied_size() == 0); + REQUIRE(asks[3].direct_size() == 1); + REQUIRE(asks[3].implied_size() == 0); + + REQUIRE(canary == 0); + } + }); + } + SECTION("nan_direct") { + direct_bid[0] = { INT_NAN, 0 }; + direct_bid[1] = { INT_NAN, 0 }; + direct_bid[2] = { INT_NAN, 0 }; + direct_bid[3] = { INT_NAN, 0 }; + + direct_ask[0] = { INT_NAN, 0 }; + direct_ask[1] = { INT_NAN, 0 }; + direct_ask[2] = { INT_NAN, 0 }; + direct_ask[3] = { INT_NAN, 0 }; + + implied_bid[0] = { {500, 1}, 2 }; + implied_bid[1] = { {400, 1}, 2 }; + + implied_ask[0] = { {1000, 1}, 2 }; + implied_ask[1] = { {1100, 1}, 2 }; + + + ImpliedTests::test_merge(direct_bid,implied_bid,direct_ask,implied_ask, [=](const vector& consumers) { + const size_t TEST_DEPTH = 4; + for (const auto& c : consumers) { + std::array bids, asks; + int canary = 0; + TimeStamp read_time; + // get_boo returns true becuase this time is after the change + REQUIRE(c.get_direct_book(read_time, TEST_DEPTH, &bids[0], &asks[0]) == HandleReadResult::SUCCESS); + for (size_t x = 0; x < TEST_DEPTH; ++x) { + REQUIRE(bids[x].size() == 0); + REQUIRE(asks[x].size() == 0); + REQUIRE(bids[x].direct_size() == 0); + REQUIRE(bids[x].implied_size() == 0); + REQUIRE(asks[x].direct_size() == 0); + REQUIRE(asks[x].implied_size() == 0); + } + read_time = TimeStamp(); + REQUIRE(c.get_implied_book(read_time, COCONUT_MAX_IMPLIED_DEPTH, &bids[0], &asks[0]) == HandleReadResult::SUCCESS); + for (size_t x = 0; x < COCONUT_MAX_IMPLIED_DEPTH; ++x) { + REQUIRE(bids[x].size() == 2); + REQUIRE(asks[x].size() == 2); + REQUIRE(bids[x].direct_size() == 0); + REQUIRE(bids[x].implied_size() == 2); + REQUIRE(asks[x].direct_size() == 0); + REQUIRE(asks[x].implied_size() == 2); + } + read_time = TimeStamp(); + REQUIRE(c.get_book(read_time, TEST_DEPTH, &bids[0], &asks[0]) == HandleReadResult::SUCCESS); + REQUIRE(bids[0].mantissa() == 500); + REQUIRE(bids[0].size() == 2); + REQUIRE(asks[0].mantissa() == 1000); + REQUIRE(asks[0].size() == 2); + REQUIRE(bids[0].direct_size() == 0); + REQUIRE(bids[0].implied_size() == 2); + REQUIRE(asks[0].direct_size() == 0); + REQUIRE(asks[0].implied_size() == 2); + + REQUIRE(bids[1].mantissa() == 400); + REQUIRE(bids[1].size() == 2); + REQUIRE(asks[1].mantissa() == 1100); + REQUIRE(asks[1].size() == 2); + REQUIRE(bids[1].direct_size() == 0); + REQUIRE(bids[1].implied_size() == 2); + REQUIRE(asks[1].direct_size() == 0); + REQUIRE(asks[1].implied_size() == 2); + + REQUIRE(bids[2].size() == 0); + REQUIRE(asks[2].size() == 0); + REQUIRE(bids[2].direct_size() == 0); + REQUIRE(bids[2].implied_size() == 0); + REQUIRE(asks[2].direct_size() == 0); + REQUIRE(asks[2].implied_size() == 0); + + REQUIRE(bids[3].size() == 0); + REQUIRE(asks[3].size() == 0); + REQUIRE(bids[3].direct_size() == 0); + REQUIRE(bids[3].implied_size() == 0); + REQUIRE(asks[3].direct_size() == 0); + REQUIRE(asks[3].implied_size() == 0); + + REQUIRE(canary == 0); + } + }); + } +} + +TEST_CASE("IntPrice", "[md]") { + SECTION("< equal exponent") { + auto a = IntPrice_from_double<4>(101.111); + auto b = IntPrice_from_double<4>(100.111); + REQUIRE((int)a.exponent() == (int)b.exponent()); + REQUIRE(b < a); + REQUIRE(!(a < b)); + } + + SECTION("< different exponent") { + auto a = IntPrice_from_double<4>(101.111); // -4 + auto b = IntPrice_from_double<0>(100); // 0 + REQUIRE((int)a.exponent() < (int)b.exponent()); + REQUIRE(b < a); + REQUIRE(!(a < b)); + auto c = std::max(a,b); + REQUIRE(c == a); + } + + // subtract with equal exponent + SECTION("subtract equal exponent") { + IntPrice a = IntPrice_from_double<4>(100.333); + IntPrice b = IntPrice_from_double<4>(100.222); + IntPrice delta = a - b; + REQUIRE(delta == IntPrice_from_double<4>(0.111)); + } + + // subtract smaller from greater + SECTION("subtract smaller from greater") { + IntPrice a = IntPrice_from_double<4>(100.333); + IntPrice b = IntPrice_from_double<5>(100.222); + IntPrice delta = a - b; + REQUIRE(delta == IntPrice_from_double<5>(0.111)); + } + // subtract greater from smaller + SECTION("subtract greater from smaller") { + IntPrice a = IntPrice_from_double<4>(100.333); + IntPrice b = IntPrice_from_double<3>(100.111); + IntPrice delta = a - b; + REQUIRE(delta == IntPrice_from_double<4>(0.222)); + } + + SECTION("subtract NAN from something") { + IntPrice a = IntPrice_from_double<4>(100.333); + IntPrice delta = a - INT_NAN; + REQUIRE(delta == INT_NAN); + } + SECTION("subtract something from NAN") { + IntPrice a = IntPrice_from_double<4>(100.333); + IntPrice delta = INT_NAN - a; + REQUIRE(delta == INT_NAN); + } } }}} diff --git a/src/lib/md/test/synthetic_book_merge_tests.cpp b/src/lib/md/test/synthetic_book_merge_tests.cpp index 215db620f..e6067a64e 100644 --- a/src/lib/md/test/synthetic_book_merge_tests.cpp +++ b/src/lib/md/test/synthetic_book_merge_tests.cpp @@ -1,3 +1,4 @@ +#ifndef _WIN32 #include "catch/catch.hpp" #include "../MDConsumerHandle.h" #include "Co2TestInstrument.h" @@ -9,6 +10,10 @@ #include #include #include +#define ENABLE_BENCHMARK 0 +#if ENABLE_BENCHMARK +#include +#endif using namespace std; @@ -35,8 +40,8 @@ void setup_market(MDProducerHandle producer, const vector& bids, const vecto auto ts = TimeStamp::now(); std::array bids_a = { }, asks_a = { }; - std::transform(bids.begin(), bids.end(), bids_a.begin(), [](const PS& ps) { return PriceSize::from_double<7>(ps.price, ps.size); }); - std::transform(asks.begin(), asks.end(), asks_a.begin(), [](const PS& ps) { return PriceSize::from_double<7>(ps.price, ps.size); }); + std::transform(bids.begin(), bids.end(), bids_a.begin(), [](const PS& ps) { return PriceSize_from_double<7>(ps.price, ps.size); }); + std::transform(asks.begin(), asks.end(), asks_a.begin(), [](const PS& ps) { return PriceSize_from_double<7>(ps.price, ps.size); }); producer.update_direct(ts, bids_a, asks_a); } @@ -44,7 +49,7 @@ void setup_market(MDProducerHandle producer, const vector& bids, const vecto void expect_market(const MDConsumerHandle& handle, const vector bids, const vector& asks) { array raw_bids,raw_asks; TimeStamp t; - handle.get_depth(t, COCONUT_MAX_DEPTH, raw_bids.data(), raw_asks.data()); + handle.get_direct_book(t, COCONUT_MAX_DEPTH, raw_bids.data(), raw_asks.data()); vector src_bids, src_asks; sane::map(raw_bids, src_bids, [](const PriceSize& ps) -> PS {return {ps.price(), ps.size()};}); sane::map(raw_asks, src_asks, [](const PriceSize& ps) -> PS {return {ps.price(), ps.size()}; }); @@ -70,11 +75,15 @@ void expect_market(const MDConsumerHandle& handle, const vector bids, const tuple,vector> make_securities(const vector>& leg_descr) { vector legs; auto synthetic = make_shared(); + uint32_t security_key = 100; for (const auto& ldesc : leg_descr) { double quantity, tick_size; api::Side side; std::tie(quantity, tick_size, side) = ldesc; Co2TestInstrument instr("ES", api::SECURITYTYPE_FUTURE); + instr.producer().set_key(security_key); + ++security_key; + auto& s = synthetic->security; s.mutable_contract_spec()->set_low_tick(tick_size); s.mutable_contract_spec()->set_high_tick(tick_size); @@ -86,10 +95,16 @@ tuple,vector> make_securities(c l->set_side(side); synthetic->legs.push_back(instr.consumer()); legs.push_back(instr); + } return { synthetic, legs }; } +void set_leg_params(SyntheticStrategy& strat, uint security_key, const spark::LegSpreaderParams& params) { + auto* map = strat.strategy.mutable_leg_params_map(); + map->insert({ security_key, params }); +} + TEST_CASE("SyntheticBookMerge", "[md][synthetic]") { shared_ptr synthetic; vector test_instr; @@ -221,6 +236,93 @@ TEST_CASE("SPARK-955", "[md][synthetic]") { { {5.00, 3}, {5.25, 1}, {6.00,3}, {6.50,6} }); } +TEST_CASE("Synthetic_net_change", "[md][synthetic]") { + shared_ptr synthetic; + vector test_instr; + tie(synthetic, test_instr) = make_securities({ {1.0, 0.01, api::SIDE_BUY}, {2.0, 0.01, api::SIDE_SELL }}); + auto es_buy1 = test_instr[0]; + auto es_sell2 = test_instr[1]; + MDConsumerHandle synthetic_handle{synthetic}; + + // Set legs to use net change + spark::LegSpreaderParams lparam; + lparam.set_leg_md_calc(spark::LegSpreaderParams::NET_CHANGE); + set_leg_params(*synthetic, es_buy1.producer().key(), lparam); + set_leg_params(*synthetic, es_sell2.producer().key(), lparam); + + // set settlement prices + es_buy1.producer().update_settlement(TimeStamp::now(), { 100, 0 }, datetime::today()); + es_sell2.producer().update_settlement(TimeStamp::now(), { 50, 0 }, datetime::today()); + + SECTION("buy 1 sell 2 Net") { + setup_market(es_buy1.producer(), + {{190,1}, {180,11}, {170,4}, {160,10}}, + {{200,2}, {210,6}, {220,8}, {230,12}}); + setup_market(es_sell2.producer(), + {{90,5}, {80,9}, {70,11}, {60,10}, {50,1}}, + {{100,16}, {110,8}, {120,12}, {130,5}}); + + // Validate merge + expect_market(synthetic_handle, + {{-10,1}, {-20,7}, {-40,4}, {-70,4}, {-80,2}, {-100,2}}, + {{20,2}, {40,1}, {50,4}, {70,1}, {80,4}, {90,1}, {100,3}, {110,1}, {120,1}}); + } +} -}}}//namespace \ No newline at end of file +// +// Benchmarks +// +#if ENABLE_BENCHMARK +void synthetic_get_book_speed2(benchmark::State& state) { + shared_ptr synthetic; + vector test_instr; + tie(synthetic, test_instr) = make_securities({ {1.0, 0.25, api::SIDE_BUY}, {2.0, 0.25, api::SIDE_SELL } }); + auto esm8 = test_instr[0]; + auto esu8 = test_instr[1]; + MDConsumerHandle synthetic_handle(synthetic); + + setup_market(esm8.producer(), + {{2594.50, 47}, {2594.25,63}, {2594.00,117}, {2593.75,137}}, + {{2594.75, 52}, {2595.00,138}, {2594.25,119}, {2595.50,128}}); + setup_market(esu8.producer(), + {{2598.50, 14}, {2598.25, 15}, {2598.00,16}, {2597.75, 15}}, + {{2599.75, 1}, {2600.00, 16}, {2600.25,15}, {2600.50, 75}}); + + for (auto _ : state) { + array raw_bids,raw_asks; + TimeStamp t; + synthetic_handle.get_book(t, COCONUT_MAX_DEPTH, raw_bids.data(), raw_asks.data()); + } +} +BENCHMARK(synthetic_get_book_speed2); +void synthetic_get_book_speed3(benchmark::State& state) { + shared_ptr synthetic; + vector test_instr; + tie(synthetic, test_instr) = make_securities({ {1.0, 0.25, api::SIDE_BUY}, {2.0, 0.25, api::SIDE_SELL }, { 1.0, 0.25, api::SIDE_BUY}}); + auto esm8 = test_instr[0]; + auto esu8 = test_instr[1]; + auto esz8 = test_instr[2]; + MDConsumerHandle synthetic_handle(synthetic); + + setup_market(esm8.producer(), + {{2594.50, 47}, {2594.25,63}, {2594.00,117}, {2593.75,137}}, + {{2594.75, 52}, {2595.00,138}, {2594.25,119}, {2595.50,128}}); + setup_market(esu8.producer(), + {{2598.50, 14}, {2598.25, 15}, {2598.00,16}, {2597.75, 15}}, + {{2599.75, 1}, {2600.00, 16}, {2600.25,15}, {2600.50, 75}}); + setup_market(esz8.producer(), + {{2601.25, 20}, {2600.50, 3}, {2600.25, 100}}, + {{2607.25, 3}, {2607.50, 1}, {2608.25, 900}}); + + for (auto _ : state) { + array raw_bids,raw_asks; + TimeStamp t; + synthetic_handle.get_book(t, COCONUT_MAX_DEPTH, raw_bids.data(), raw_asks.data()); + } +} +BENCHMARK(synthetic_get_book_speed3); +#endif + +}}}//namespace +#endif \ No newline at end of file diff --git a/src/lib/orderman/actors/MDSource.h b/src/lib/orderman/actors/MDSource.h index 8bf0110a2..26316ed7b 100644 --- a/src/lib/orderman/actors/MDSource.h +++ b/src/lib/orderman/actors/MDSource.h @@ -25,6 +25,7 @@ class MDSource : public mercury::Actor, public mercury::IPollableActor { : _handles(), _md_consumer(consumer), _book_update(), _stats(stats), _loop_timer() {} void init() override { LOG(OrderMan, Warn, "Starting MDSource actor"); + bts_assert_msg(_md_consumer.all_heartbeats_ok(), "Marketdata heartbeats are bad. MDConsumer will not recover from this condition. Please run 'sparkd md cleanup' and restart sparkd"); std::array buckets; auto i = 1; @@ -80,7 +81,7 @@ class MDSource : public mercury::Actor, public mercury::IPollableActor { auto start = Timer::now(); for (auto &h : _handles) { auto &handle = h.handle; - if (handle.get_depth(h.last, std::tuple_size(), + if (handle.get_book(h.last, std::tuple_size(), &book.bid()[0], &book.ask()[0]) == md::HandleReadResult::SUCCESS) { book._security_key = SecurityKey(handle.key()); diff --git a/src/lib/orderman/actors/MDSupplementarySource.cpp b/src/lib/orderman/actors/MDSupplementarySource.cpp new file mode 100644 index 000000000..a9630b987 --- /dev/null +++ b/src/lib/orderman/actors/MDSupplementarySource.cpp @@ -0,0 +1,81 @@ +#include "MDSupplementarySource.h" +#include "lib/orderman/events/SettlementRequestEvent.h" +#include "lib/orderman/events/SettlementUpdateEvent.h" +#include "lib/logging/logging.h" +#include "lib/md/MDConsumerHandle.h" +#include "lib/md/MDConsumer.h" +#include "lib/md/MDConsumer.h" +#include "lib/os/os.h" + +namespace bts { +namespace orderman { + +struct HandleRecord { + md::MDConsumerHandle handle; + md::IntPrice settlement_price; +}; + +struct MDSupplementarySource::PImpl +{ + PImpl(md::MDConsumer& md_consumer) + : + md_consumer(md_consumer), + handle_map(), + settlement_update(){ + } + md::MDConsumer md_consumer; + // list of sec subscriptions vs their handles + std::map handle_map; + mercury::Stream settlement_update; +}; + +MDSupplementarySource::MDSupplementarySource(md::MDConsumer& md_consumer) +: +me(new PImpl(md_consumer)){ +} + +MDSupplementarySource::~MDSupplementarySource(){ +} + +void MDSupplementarySource::init(){ + me->settlement_update=make_stream(); + subscribe([this](auto const &update) { + if (update.events) { + for (auto const &ev : *update.events) { + auto k = ev.security_key.value(); + auto it = me->handle_map.find(k); + if(it ==me->handle_map.end()){ + LOG(OrderMan, Info, "MDSupplementarySource::init key:{}", k); + me->handle_map[k]={me->md_consumer.get_handle(k),md::INT_NAN}; + } + else { + // late joiners + auto const &hr = it->second; + if (!hr.settlement_price.is_nan()) { + SettlementUpdateEvent ev(hr.settlement_price,SecurityKey(k)); + publish(me->settlement_update,ev); + } + } + } + } + }); +} + +void MDSupplementarySource::on_poll(){ + for(auto& kvp : me->handle_map){ + SecurityKey key(kvp.first); + HandleRecord& hr=kvp.second; + auto settlement_price = hr.handle.settlement_price(); + if(!settlement_price.is_nan() && + settlement_price!=hr.settlement_price){ + LOG(OrderMan, Info, + "MDSupplementarySource::on_poll new settle key:{} price:{}", + key, hr.settlement_price.price()); + hr.settlement_price=settlement_price; + SettlementUpdateEvent ev(settlement_price,key); + publish(me->settlement_update,ev); + } + } +} + +}} diff --git a/src/lib/orderman/actors/MDSupplementarySource.h b/src/lib/orderman/actors/MDSupplementarySource.h new file mode 100644 index 000000000..4ea5fa261 --- /dev/null +++ b/src/lib/orderman/actors/MDSupplementarySource.h @@ -0,0 +1,32 @@ +#pragma once + +#include +#include "lib/mercury/actors/Actor.h" +#include "lib/mercury/actors/IPollableActor.h" + +namespace bts { + + +namespace md {class MDConsumer;} +namespace orderman { + +// provides non hot-path market data such as settles, daily high-low etc +// (only settles for now) +// like MDSource, watches for MDRequestEvent's and hence constructs handles +// for requested contracts only. these handles are then polled and settles are +// published if changed. +class MDSupplementarySource : public mercury::Actor, public mercury::IPollableActor +{ +public: + MDSupplementarySource(md::MDConsumer& md_consumer); + ~MDSupplementarySource(); + + void init() override; + void on_poll() override; +private: + struct PImpl; + std::unique_ptr me; +}; + +}} + diff --git a/src/lib/orderman/actors/order_manager/OrderManager.cpp b/src/lib/orderman/actors/order_manager/OrderManager.cpp index f9ea2e284..94dcc73ed 100644 --- a/src/lib/orderman/actors/order_manager/OrderManager.cpp +++ b/src/lib/orderman/actors/order_manager/OrderManager.cpp @@ -371,6 +371,7 @@ void OrderManager::init_order_services() { me->services_data.create_fix_req_pub = make_stream(); me->services_data.md_request_pub = make_stream(); + me->services_data.settlement_request_pub= make_stream(); me->services_data.report_order_state_pub = make_stream(); me->pub_ready = make_stream(); @@ -383,8 +384,13 @@ void OrderManager::init_order_services() { std::function on_md_sub = [this](SecurityKey k) { publish(me->services_data.md_request_pub, k); }; + std::function on_settlement_sub = [this](SecurityKey k) { + publish(me->services_data.settlement_request_pub, k); + }; me->services_data.md_subs = std::make_unique>(*this, on_md_sub); + me->services_data.settlement_subs = + std::make_unique>(*this,on_settlement_sub); me->services_data.time_subs = std::make_unique>(*this); me->services_data.order_creation_subs = diff --git a/src/lib/orderman/actors/order_manager/OrderServices.h b/src/lib/orderman/actors/order_manager/OrderServices.h index 6fc014d54..4e50f804f 100644 --- a/src/lib/orderman/actors/order_manager/OrderServices.h +++ b/src/lib/orderman/actors/order_manager/OrderServices.h @@ -70,7 +70,9 @@ class SessionServices { class OrderServices { public: OrderServices(Order &owner, OrderServicesData &data) - : book_updates(*data.md_subs), child_updates(*data.child_order_subs), + : book_updates(*data.md_subs), + settlement_updates(*data.settlement_subs), + child_updates(*data.child_order_subs), time_change(*data.time_subs), order_creations(*data.order_creation_subs), _owner(owner), _data(data), _sessions() { @@ -79,6 +81,7 @@ class OrderServices { } EventStream book_updates; + EventStream settlement_updates; EventStream child_updates; EventStream time_change; diff --git a/src/lib/orderman/actors/order_manager/OrderServicesData.h b/src/lib/orderman/actors/order_manager/OrderServicesData.h index 46e2741e8..6fd2658e2 100644 --- a/src/lib/orderman/actors/order_manager/OrderServicesData.h +++ b/src/lib/orderman/actors/order_manager/OrderServicesData.h @@ -11,6 +11,7 @@ #include "lib/mercury/events/TimerEvent.h" #include "lib/orderman/events/ActivateOrderEvent.h" #include "lib/orderman/events/BookUpdateEvent.h" +#include "lib/orderman/events/SettlementUpdateEvent.h" #include "lib/orderman/events/CancelOrderEvent.h" #include "lib/orderman/events/CancelRejectEvent.h" #include "lib/orderman/events/CrackOrderEvent.h" @@ -20,6 +21,7 @@ #include "lib/orderman/events/CreateOrderRequestEvent.h" #include "lib/orderman/events/GatewayStatusChangeEvent.h" #include "lib/orderman/events/MDRequestEvent.h" +#include "lib/orderman/events/SettlementRequestEvent.h" #include "lib/orderman/events/NewExecutionReportEvent.h" #include "lib/orderman/events/NewSecuritiesEvent.h" #include "lib/orderman/events/OrderStateUpdateEvent.h" @@ -76,6 +78,7 @@ class OrderServicesData { OrderFactory order_factory; std::unique_ptr> md_subs; + std::unique_ptr> settlement_subs; std::unique_ptr< mercury::ContentSubscription> child_order_subs; @@ -94,6 +97,7 @@ class OrderServicesData { mercury::Stream create_order_req_pub; mercury::Stream create_fix_req_pub; mercury::Stream md_request_pub; + mercury::Stream settlement_request_pub; mercury::Stream report_order_state_pub; mercury::TimerEvent last_timer_ev; diff --git a/src/lib/orderman/events/SettlementRequestEvent.h b/src/lib/orderman/events/SettlementRequestEvent.h new file mode 100644 index 000000000..2d895d0a1 --- /dev/null +++ b/src/lib/orderman/events/SettlementRequestEvent.h @@ -0,0 +1,24 @@ +#pragma once + +#include + +#include "lib/orderman/orderman_utils/orderman_common.h" +#include "lib/mercury/events/Event.h" + +namespace bts { +namespace orderman { + +class SettlementRequestEvent : public mercury::Event { +public: + using SynopsisPolicy = mercury::CacheAll; + SettlementRequestEvent(SecurityKey k) : security_key(k) {} + SettlementRequestEvent &operator=(SettlementRequestEvent const&rhs) + { + security_key = rhs.security_key; + return *this; + } + SecurityKey security_key; +}; + +}} + diff --git a/src/lib/orderman/events/SettlementUpdateEvent.h b/src/lib/orderman/events/SettlementUpdateEvent.h new file mode 100644 index 000000000..9211c081b --- /dev/null +++ b/src/lib/orderman/events/SettlementUpdateEvent.h @@ -0,0 +1,26 @@ +#pragma once + +#include + +#include "lib/mercury/events/Event.h" +#include "lib/orderman/orderman_utils/Book.h" +#include "lib/orderman/orderman_utils/orderman_common.h" +#include "lib/mercury/synopsis/MergeByKey.h" + +namespace bts { +namespace orderman { + +class SettlementUpdateEvent : public mercury::Event { +public: + using SynopsisPolicy = mercury::NoSynopsis; + md::IntPrice settlement_price; + SecurityKey security_key; + SecurityKey key() const { return security_key; } + + SettlementUpdateEvent(const md::IntPrice& price, + const SecurityKey& key) + : + settlement_price(price), + security_key(key) {} +}; +}} diff --git a/src/lib/orderman/orderman_utils/Book.h b/src/lib/orderman/orderman_utils/Book.h index 34ef074be..36b6ae386 100644 --- a/src/lib/orderman/orderman_utils/Book.h +++ b/src/lib/orderman/orderman_utils/Book.h @@ -18,6 +18,11 @@ public: _book() {} + Book (SecurityKey key) : + _security_key(key), + _book() + {} + Book (SecurityKey key, BookSide const&bid, BookSide const&ask) : diff --git a/src/lib/orderman/orders/spreader_orders/SpreaderOrder.cpp b/src/lib/orderman/orders/spreader_orders/SpreaderOrder.cpp index 41eeebf06..63e2c8fae 100644 --- a/src/lib/orderman/orders/spreader_orders/SpreaderOrder.cpp +++ b/src/lib/orderman/orders/spreader_orders/SpreaderOrder.cpp @@ -6,6 +6,7 @@ #include "lib/orderman/orders/spreader_orders/spreader/LegPropertyData.h" #include "lib/orderman/orders/spreader_orders/spreader/SpreaderLegData.h" #include "lib/orderman/orders/spreader_orders/spreader/SpreaderBookkeepingData.h" +#include "lib/orderman/events/SettlementUpdateEvent.h" #include "lib/tracing/bts_trace.h" #include "lib/utils/helper_macros.h" @@ -100,6 +101,21 @@ void SpreaderOrder::_on_book_update(const BookUpdateEvent& book_event){ trace.spreader_on_book = utils::TimeStamp::now(); _state_worker->on_book (book_event.book,trace); } +void SpreaderOrder::_on_settlement_update(const SettlementUpdateEvent& + settlement_event){ + if (!_active) return; + LOG(OrderMan, Info, "SpreaderOrder::_on_settlement_update " + "key:{} sec_key:{} state:{} settlement:{}", + key(), settlement_event.key(), + static_cast(_state_worker->state()), + settlement_event.settlement_price.price()); + // this is a key->index lookup + SpreaderLegData& md_leg = + _bookkeeping_data().get_leg_data(settlement_event.key()); + md_leg.update_settlement_price(settlement_event.settlement_price); + bts::PerfTrace trace; + _state_worker->on_book (md_leg.book(),trace); +} void SpreaderOrder::_on_child_order_update(const OrderStateUpdateEvent& ord_ev){ auto trace = ord_ev.trace; trace.spreader_on_child_order_update = utils::TimeStamp::now(); @@ -114,7 +130,7 @@ void SpreaderOrder::_on_child_order_update(const OrderStateUpdateEvent& ord_ev){ if (!SpreaderUtils::order_acknowledged(status)){ LOG(OrderMan, Diag, "SpreaderOrder::_on_child_order_update, unack'ed " "callback for key:{} sec_key:{} order_key:{} " - "status:{} ignoring", + "status:{} ignoring", key(), sec_key, order_key, status); bts_assert(filled_qty.value() == 0); return; diff --git a/src/lib/orderman/orders/spreader_orders/SpreaderOrder.h b/src/lib/orderman/orders/spreader_orders/SpreaderOrder.h index 55a819528..8be458b9d 100644 --- a/src/lib/orderman/orders/spreader_orders/SpreaderOrder.h +++ b/src/lib/orderman/orders/spreader_orders/SpreaderOrder.h @@ -24,6 +24,8 @@ namespace bts { namespace api {namespace orderman {class OrderState; class CreateSpreaderOrder;}} namespace orderman { +class SettlementUpdateEvent; + class SpreaderOrder : public Order, public ISpreaderManager { friend class SpreaderStartStateWorker; @@ -124,6 +126,7 @@ private: ///////////////////////////////////////////////// void _on_time (const bts::mercury::TimerEvent&); void _on_book_update(const BookUpdateEvent&); + void _on_settlement_update(const SettlementUpdateEvent&); void _on_child_order_update(const OrderStateUpdateEvent&); // State Transformations //////////////////// ///////////////////////////////////////////// diff --git a/src/lib/orderman/orders/spreader_orders/spreader/LegPropertyData.h b/src/lib/orderman/orders/spreader_orders/spreader/LegPropertyData.h index 64e3fb66b..31d6457b5 100644 --- a/src/lib/orderman/orders/spreader_orders/spreader/LegPropertyData.h +++ b/src/lib/orderman/orders/spreader_orders/spreader/LegPropertyData.h @@ -40,6 +40,12 @@ public: bool is_active_quoting() const {return _leg_params.is_active_quoting();} const bts::api::orderman::OrderDefinition& quoter_order_definition() const {return _quoter_order_definition;} const bts::api::orderman::OrderDefinition& hedger_order_definition() const {return _hedger_order_definition;} + bts::spark::LegSpreaderParams::LegMarketDataCalculation leg_md_calc() const { + return _leg_params.leg_md_calc(); + } + bts::spark::LegSpreaderParams::MarketDataType md_type() const { + return _leg_params.market_data_type(); + } }; }} diff --git a/src/lib/orderman/orders/spreader_orders/spreader/SpreaderBookkeepingData.cpp b/src/lib/orderman/orders/spreader_orders/spreader/SpreaderBookkeepingData.cpp index 9c87cd6fb..160d6304a 100644 --- a/src/lib/orderman/orders/spreader_orders/spreader/SpreaderBookkeepingData.cpp +++ b/src/lib/orderman/orders/spreader_orders/spreader/SpreaderBookkeepingData.cpp @@ -28,7 +28,7 @@ _reported_state(){ for (const auto& lpd_iter : legs){ _leg_prop_data.push_back(lpd_iter); const LegPropertyData& leg_property_data = _leg_prop_data.back(); - _books.emplace_back(); + _books.emplace_back(lpd_iter.leg_security_key()); _quoter_order_builders.emplace_back(spreader_order_key, leg_property_data.quoter_order_definition(), spreader_pao_services, 10); _hedger_order_builders.emplace_back(spreader_order_key, leg_property_data.hedger_order_definition(), spreader_pao_services, 10); ChildOrder empty_order; diff --git a/src/lib/orderman/orders/spreader_orders/spreader/SpreaderLegData.cpp b/src/lib/orderman/orders/spreader_orders/spreader/SpreaderLegData.cpp index 57c4e776a..ceb595573 100644 --- a/src/lib/orderman/orders/spreader_orders/spreader/SpreaderLegData.cpp +++ b/src/lib/orderman/orders/spreader_orders/spreader/SpreaderLegData.cpp @@ -19,7 +19,8 @@ _hedger_order_builder (hedger_order_builder), _quoter_child_order_state (quoter_child_order_state), _hedger_data_vector (hedger_data_vector), _book(book), -_unhedged_delta(unhedged_delta) +_unhedged_delta(unhedged_delta), +_settlement_price(md::INT_NAN) {} SpreaderLegData::SpreaderLegData(SpreaderLegData&& other) diff --git a/src/lib/orderman/orders/spreader_orders/spreader/SpreaderLegData.h b/src/lib/orderman/orders/spreader_orders/spreader/SpreaderLegData.h index b3ad546f6..f96c8346e 100644 --- a/src/lib/orderman/orders/spreader_orders/spreader/SpreaderLegData.h +++ b/src/lib/orderman/orders/spreader_orders/spreader/SpreaderLegData.h @@ -6,6 +6,8 @@ #include "lib/orderman/actors/order_manager/OrderBuilder.h" #include "lib/orderman/orders/spreader_orders/spreader/external_interfacing/SpreaderOrderBuilder.h" #include "lib/orderman/orders/spreader_orders/spreader/hedger/UnhedgedDelta.h" +#include "lib/os/os.h" +#include "lib/logging/logging.h" namespace bts{ namespace orderman{ @@ -30,6 +32,7 @@ private: std::vector& _hedger_data_vector; Book& _book; UnhedgedDelta& _unhedged_delta; + md::IntPrice _settlement_price; public: SpreaderLegData (const LegPropertyData& leg_property_data, SingleLegQuoter& quoter, @@ -56,6 +59,7 @@ public: SingleLegQuoter& quoter() {return _quoter;} const SingleLegQuoter& quoter() const {return _quoter;} const bts::orderman::Book& book () const {return _book;} + //md::Price settlement_price() const {return _settlement_price;} SpreaderChildOrderState& order_update_message () {return _quoter_child_order_state;} bts::orderman::OrderKey key() {return _quoter_child_order_state.order_key();} bts::orderman::OrderKey order_key() {return _quoter_child_order_state.order_key();} @@ -99,6 +103,21 @@ public: // mutators //////////////////// //////////////////////////////// // called for every book update - void update_book(const bts::orderman::Book& book_arg) {_book = book_arg;} + void update_book(const Book& book) { + SpreaderUtils::copy_book(book, + _settlement_price, + _leg_property_data.md_type(), + _leg_property_data.leg_md_calc(), + _book); + } + void update_settlement_price(md::IntPrice price){ + _settlement_price=price; + SpreaderUtils::copy_book(_book, + _settlement_price, + _leg_property_data.md_type(), + _leg_property_data.leg_md_calc(), + _book); + + } }; }} diff --git a/src/lib/orderman/orders/spreader_orders/spreader/SpreaderUtils.h b/src/lib/orderman/orders/spreader_orders/spreader/SpreaderUtils.h index d0766cef5..792b7a4c6 100644 --- a/src/lib/orderman/orders/spreader_orders/spreader/SpreaderUtils.h +++ b/src/lib/orderman/orders/spreader_orders/spreader/SpreaderUtils.h @@ -136,6 +136,80 @@ public: } bts_assert(out_quantity.value() <= target_quantity.value()); } + // copies the price/qty content of the source book + // into the target, accounting for md_type and leg_calc. + // (note that the security_key in the target is left untouched + // and should be set by the callee) + // this is not very clean for the reason that the spreader + // uses the "size()" function in the book, so we copy the + // "effective size" to the size var in the Book, which + // for different values of md_type would mean different + // things. the better way to do it is to make a SpreaderBook + // that exposes effective_size() and routes it to the + // right size. + void static copy_book ( + const Book& source, + md::IntPrice settlement_price, + bts::spark::LegSpreaderParams::MarketDataType md_type, + bts::spark::LegSpreaderParams::LegMarketDataCalculation leg_calc, + Book& target){ + SecurityKey target_key=target.security_key(); + if(md_type==bts::spark::LegSpreaderParams::DIRECT){ + // since the "size()" in Book is the merged size + // we put the direct in the size + size_t target_i=0; + for(size_t source_i=0;source_i0){ + target.bid()[target_i]=md::PriceSize(b.int_price(),b.direct_size()); + target_i++; + } + } + empty_out_book(target.bid(),target_i,target.n_levels()); + target_i=0; + for(size_t source_i=0;source_i0){ + target.ask()[target_i]=md::PriceSize(a.int_price(),a.direct_size()); + target_i++; + } + } + empty_out_book(target.ask(),target_i,target.n_levels()); + } + else if(md_type==bts::spark::LegSpreaderParams::MERGED){ + target.bid()=source.bid(); + target.ask()=source.ask(); + } + if(leg_calc==bts::spark::LegSpreaderParams::NET_CHANGE){ + if(settlement_price.is_nan()){ + empty_out_book(target.bid(),0,target.n_levels()); + empty_out_book(target.ask(),0,target.n_levels()); + } + else{ + offset_book(target.bid(), target.n_levels(),settlement_price); + offset_book(target.ask(), target.n_levels(),settlement_price); + } + } + bts_assert(target_key==target.security_key()); + } + void static offset_book(BookSide& book_side, + size_t n_levels, + md::IntPrice offset){ + for(size_t i=0;i_bookkeeping_data().get_leg_data(l_idx).leg_property_data(); LOG(OrderMan, Info, "SpreaderStartStateWorker::on_activate subbing to book sec_key: {}", leg_prop.security_key()); order_services.order_services().book_updates.sub(leg_prop.leg_security_key(), - [this](const bts::orderman::BookUpdateEvent& bk_event) + [this](const BookUpdateEvent& bk_event) {_spreader->_on_book_update (bk_event);}); + order_services.order_services().settlement_updates.sub(leg_prop.leg_security_key(), + [this](const SettlementUpdateEvent& s_event) + {_spreader->_on_settlement_update (s_event);}); } // 6. sub to time order_services.order_services().time_change.sub([this](const bts::mercury::TimerEvent& t_event){_spreader->_on_time(t_event);}); diff --git a/src/lib/orderman/orders/spreader_orders/tests/spreader_tests.cpp b/src/lib/orderman/orders/spreader_orders/tests/spreader_tests.cpp index c3ff6ed8e..4d2ab8287 100644 --- a/src/lib/orderman/orders/spreader_orders/tests/spreader_tests.cpp +++ b/src/lib/orderman/orders/spreader_orders/tests/spreader_tests.cpp @@ -2868,6 +2868,289 @@ TEST_CASE ("spreader_perf_4legs", "[spreader]") std::cout << "4 leg hedges:" << n_hedges << " unit time(usecs):" << hedge_time << std::endl; } } +TEST_CASE ("simple book addition", "[spreader]"){ + SECTION ("direct, book"){ + SecurityKey key(12345); + Book in_book (key); + fill_book(in_book, + new double[5] {2000, 2000.01, 2000.03, 2000.04, 2000.1}, + new uint16_t[5] {10, 1, 15, 14, 20}, + new uint16_t[5] {1, 12, 0, 0, 0}, + api::Side::SIDE_BUY, + true); + Book out_book(key); + SpreaderUtils::copy_book(in_book, + md::IntPrice_from_double<7>(1999.0), + bts::spark::LegSpreaderParams::DIRECT, + bts::spark::LegSpreaderParams::BOOK, + out_book); + + validate_book(out_book, + key, + api::Side::SIDE_BUY, + 0.01, + new double[5] {2000, 2000.01, 2000.03, 2000.04, 2000.1}, + new uint16_t[5] {10, 1, 15, 14, 20}, + true); + } + SECTION ("direct, book_holes"){ + SecurityKey key(12345); + Book in_book (key); + fill_book(in_book, + new double[5] {2000, 2000.01, 2000.03, 2000.04, 2000.1}, + new uint16_t[5] {0, 1, 0, 14, 20}, //directs + new uint16_t[5] {1, 12, 0, 0, 0}, //implieds + api::Side::SIDE_BUY, + true); + Book out_book(key); + SpreaderUtils::copy_book(in_book, + md::IntPrice_from_double<7>(1999.0), + bts::spark::LegSpreaderParams::DIRECT, + bts::spark::LegSpreaderParams::BOOK, + out_book); + + validate_book(out_book, + key, + api::Side::SIDE_BUY, + 0.01, + new double[5] {2000.01, 2000.04, 2000.10, os::nan(), os::nan()}, + new uint16_t[5] {1, 14, 20, 0, 0}, + true); + } + SECTION ("direct, net_change"){ + SecurityKey key(13579); + Book in_book (key); + fill_book(in_book, + new double[5] {2000, 2000.01, 2000.03, 2000.04, 2000.1}, + new uint16_t[5] {10, 1, 15, 14, 20}, + new uint16_t[5] {1, 12, 0, 0, 0}, + api::Side::SIDE_BUY, + true); + Book out_book(key); + SpreaderUtils::copy_book(in_book, + md::IntPrice_from_double<7>(1999.0), + bts::spark::LegSpreaderParams::DIRECT, + bts::spark::LegSpreaderParams::NET_CHANGE, + out_book); + + validate_book(out_book, + key, + api::Side::SIDE_BUY, + 0.01, + new double[5] {1, 1.01, 1.03, 1.04, 1.1}, + new uint16_t[5] {10, 1, 15, 14, 20}, + true); + } + SECTION ("direct, net_change_nan"){ + SecurityKey key(13579); + Book in_book (key); + fill_book(in_book, + new double[5] {2000, 2000.01, 2000.03, 2000.04, 2000.1}, + new uint16_t[5] {10, 1, 15, 14, 20}, + new uint16_t[5] {1, 12, 0, 0, 0}, + api::Side::SIDE_BUY, + true); + Book out_book(key); + SpreaderUtils::copy_book(in_book, + md::INT_NAN, + bts::spark::LegSpreaderParams::DIRECT, + bts::spark::LegSpreaderParams::NET_CHANGE, + out_book); + + validate_book(out_book, + key, + api::Side::SIDE_BUY, + 0.01, + new double[5] {os::nan(), os::nan(), os::nan(), os::nan(), os::nan()}, + new uint16_t[5] {0, 0, 0, 0, 0}, + true); + } + SECTION ("merged, book"){ + SecurityKey key(24680); + Book in_book (key); + fill_book(in_book, + new double[5] {2000, 2000.01, 2000.03, 2000.04, 2000.1}, + new uint16_t[5] {10, 1, 15, 14, 20}, + new uint16_t[5] {1, 12, 0, 0, 0}, + api::Side::SIDE_BUY, + true); + Book out_book(key); + SpreaderUtils::copy_book(in_book, + md::IntPrice_from_double<7>(1999.0), + bts::spark::LegSpreaderParams::MERGED, + bts::spark::LegSpreaderParams::BOOK, + out_book); + + validate_book(out_book, + key, + api::Side::SIDE_BUY, + 0.01, + new double[5] {2000, 2000.01, 2000.03, 2000.04, 2000.1}, + new uint16_t[5] {11, 13, 15, 14, 20}, + true); + } + SECTION ("merged, net_change"){ + SecurityKey key(54321); + Book in_book (key); + fill_book(in_book, + new double[5] {2000, 2000.01, 2000.03, 2000.04, 2000.1}, + new uint16_t[5] {10, 1, 15, 14, 20}, + new uint16_t[5] {1, 12, 0, 0, 0}, + api::Side::SIDE_BUY, + true); + Book out_book(key); + SpreaderUtils::copy_book(in_book, + md::IntPrice_from_double<7>(1999.0), + bts::spark::LegSpreaderParams::MERGED, + bts::spark::LegSpreaderParams::NET_CHANGE, + out_book); + + validate_book(out_book, + key, + api::Side::SIDE_BUY, + 0.01, + new double[5] {1, 1.01, 1.03, 1.04, 1.1}, + new uint16_t[5] {11, 13, 15, 14, 20}, + true); + } +} +TEST_CASE ("random book addition", "[spreader]"){ + size_t n_runs=100; + // generates random prices, with tick size=0.01 + size_t n_levels=5; + double* bid_prices=new double[n_levels], + *ask_prices=new double[n_levels]; + uint16_t* direct_bid_sizes=new uint16_t[n_levels], + *implied_bid_sizes=new uint16_t[n_levels], + *direct_ask_sizes=new uint16_t[n_levels], + *implied_ask_sizes=new uint16_t[n_levels]; + double* exp_bid_prices=new double[n_levels], + *exp_ask_prices=new double[n_levels]; + uint16_t* exp_bid_sizes=new uint16_t[n_levels], + *exp_ask_sizes=new uint16_t[n_levels]; + for(size_t i=0;i(settlement_price), + bts::spark::LegSpreaderParams::DIRECT, + bts::spark::LegSpreaderParams::BOOK, + out_book); + validate_book(out_book, + key, + api::Side::SIDE_BUY, + tick_size, + bid_prices, + direct_bid_sizes, + false); + validate_book(out_book, + key, + api::Side::SIDE_SELL, + tick_size, + ask_prices, + direct_ask_sizes, + false); + } + // "direct, net_change" + { + for(size_t i=0;i(settlement_price), + bts::spark::LegSpreaderParams::DIRECT, + bts::spark::LegSpreaderParams::NET_CHANGE, + out_book); + validate_book(out_book, + key, + api::Side::SIDE_BUY, + tick_size, + exp_bid_prices, + direct_bid_sizes, + false); + validate_book(out_book, + key, + api::Side::SIDE_SELL, + tick_size, + exp_ask_prices, + direct_ask_sizes, + false); + } + // merged, book + { + for(size_t i=0;i(settlement_price), + bts::spark::LegSpreaderParams::MERGED, + bts::spark::LegSpreaderParams::BOOK, + out_book); + validate_book(out_book, + key, + api::Side::SIDE_BUY, + tick_size, + bid_prices, + exp_bid_sizes, + false); + validate_book(out_book, + key, + api::Side::SIDE_SELL, + tick_size, + ask_prices, + exp_ask_sizes, + false); + } + // merged, net change + { + for(size_t i=0;i(settlement_price), + bts::spark::LegSpreaderParams::MERGED, + bts::spark::LegSpreaderParams::NET_CHANGE, + out_book); + validate_book(out_book, + key, + api::Side::SIDE_BUY, + tick_size, + exp_bid_prices, + exp_bid_sizes, + false); + validate_book(out_book, + key, + api::Side::SIDE_SELL, + tick_size, + exp_ask_prices, + exp_ask_sizes, + false); + } + } +} class RV { public: diff --git a/src/lib/orderman/orders/spreader_orders/tests/tests_common.h b/src/lib/orderman/orders/spreader_orders/tests/tests_common.h index a927df1b4..6d60f8527 100644 --- a/src/lib/orderman/orders/spreader_orders/tests/tests_common.h +++ b/src/lib/orderman/orders/spreader_orders/tests/tests_common.h @@ -14,6 +14,7 @@ #include "lib/orderman/orders/spreader_orders/spreader/SpreaderChildOrderState.h" #include "lib/orderman/orders/spreader_orders/spreader/external_interfacing/SpreaderOrderBuilder.h" #include +#include "lib/os/os.h" namespace bts { namespace orderman { @@ -250,13 +251,32 @@ SpreaderPreActivationOrderServices::TestOnlySpreaderPreActivationOrderServices _ /**********************************************************************/ /******************************book************************************/ /**********************************************************************/ + +void fill_book(bts::orderman::Book& book, + double* prices, + uint16_t* direct_sizes, + uint16_t* implied_sizes, + bts::api::Side side, + bool delete_arrays){ + bts::orderman::BookSide& bookside = book.side(side); + for (size_t i = 0; i < book.n_levels(); i++) + bookside[i] = md::PriceSize(md::IntPrice_from_double<7>(prices[i]), + direct_sizes[i], + md::ImpliedSize(implied_sizes[i])); + if (delete_arrays){ + delete[] prices; + delete[] direct_sizes; + delete[] implied_sizes; + } +} + void fill_book(bts::orderman::Book& book, double* prices, uint16_t* sizes, bts::api::Side side){ bts::orderman::BookSide& bookside = book.side(side); for (size_t i = 0; i < book.n_levels(); i++) - bookside[i] = md::PriceSize::from_double<6>(prices[i], sizes[i]); + bookside[i] = md::PriceSize_from_double<6>(prices[i], sizes[i]); } void fill_book(bts::orderman::Book& book, double* bid_prices, @@ -266,8 +286,8 @@ void fill_book(bts::orderman::Book& book, bts::orderman::BookSide& bidside = book.side(api::Side::SIDE_BUY); bts::orderman::BookSide& askside = book.side(api::Side::SIDE_SELL); for (size_t i = 0; i < book.n_levels(); i++){ - bidside[i] = md::PriceSize::from_double<6>(bid_prices[i], bid_sizes[i]); - askside[i] = md::PriceSize::from_double<6>(ask_prices[i], ask_sizes[i]); + bidside[i] = md::PriceSize_from_double<6>(bid_prices[i], bid_sizes[i]); + askside[i] = md::PriceSize_from_double<6>(ask_prices[i], ask_sizes[i]); } } bts::orderman::Book get_book(SecurityKey key, @@ -280,11 +300,11 @@ bts::orderman::Book get_book(SecurityKey key, bool delete_arrays){ bts::orderman::BookSide bidside, askside; for (size_t i = 0; i < n_bid_levels; i++){ - md::PriceSize bid = md::PriceSize::from_double<6>(bid_prices[i], bid_sizes[i]); + md::PriceSize bid = md::PriceSize_from_double<6>(bid_prices[i], bid_sizes[i]); bidside[i] = bid; } for (size_t i = 0; i < n_ask_levels; i++){ - md::PriceSize ask = md::PriceSize::from_double<6>(ask_prices[i], ask_sizes[i]); + md::PriceSize ask = md::PriceSize_from_double<6>(ask_prices[i], ask_sizes[i]); askside[i] = ask; } if (delete_arrays){ @@ -465,6 +485,30 @@ void validate_book (const bts::orderman::Book& b1, const bts::orderman::Book& b2 REQUIRE (b1.ask()[i].size() == b2.ask()[i].size()); } } +void validate_book (const bts::orderman::Book& book, + SecurityKey key, + api::Side side, + double tick_size, + double* prices, + uint16_t* sizes, + bool delete_arrays){ + REQUIRE(book.security_key()==key); + const BookSide& md=side==api::Side::SIDE_BUY ? book.bid() : book.ask(); + for(size_t i=0; i& drivers, size_t n_legs){ drivers.push_back(d); } } +void generate_random_book(size_t n_levels, + double tick_size, + double top_book, + double top_ask, + // out vars + double* bid_prices, + uint16_t* direct_bid_sizes, + uint16_t* implied_bid_sizes, + double* ask_prices, + uint16_t* direct_ask_sizes, + uint16_t* implied_ask_sizes, + Book& book){ + double b=top_book, + a=top_ask; + for(size_t i=0;i &ss, DynamicOrderRegistry const&d, - metrics::MetricsContainer &stats, - PerfTraceLog &log) + metrics::MetricsContainer &stats, PerfTraceLog &log) : api::orderman::OrderServiceBase(ctx), me(new PImpl(ctx, ss, d,stats)) { me->publish_orders = make_orders_publisher(); @@ -191,6 +191,14 @@ OrderServiceServer::OrderServiceServer( "the OrdermanCfg key"); thread->executor.spawn(me->md_consumer); + // MDSupplementarySource + thread = find_thread_for_actor("MDSupplementarySource", me->actor_threads); + bts_assert_msg( + thread, + "Could not find a configured thread for MDSupplementarySource. See " + "the OrdermanCfg key"); + thread->executor.spawn(me->md_consumer); + // ServiceBridge thread = find_thread_for_actor("ServiceBridge", me->actor_threads); bts_assert_msg(thread, diff --git a/src/lib/sparkproto/SparkProtos.proto b/src/lib/sparkproto/SparkProtos.proto index e30e01b2e..77b71f857 100644 --- a/src/lib/sparkproto/SparkProtos.proto +++ b/src/lib/sparkproto/SparkProtos.proto @@ -28,8 +28,17 @@ message RulePriority int32 priority = 20; } + message LegSpreaderParams { + enum MarketDataType { + MERGED = 0; + DIRECT = 1; + } + enum LegMarketDataCalculation{ + BOOK=0; + NET_CHANGE=1; + } /// Legs can be quoted actively, or sent in reponse /// to fills bool is_active_quoting = 20; @@ -37,6 +46,8 @@ message LegSpreaderParams QuoteParams quote_params = 40; PreHedgeParams pre_hedge_params = 50; PostHedgeParams post_hedge_params = 60; + MarketDataType market_data_type = 70; + LegMarketDataCalculation leg_md_calc = 80; } // /************************************************************************************************* diff --git a/src/lib/utils/datetime.cpp b/src/lib/utils/datetime.cpp index 85fc9235e..5e858e6e9 100755 --- a/src/lib/utils/datetime.cpp +++ b/src/lib/utils/datetime.cpp @@ -1,446 +1,446 @@ -#include "datetime.h" - -#include "bts_assert.h" -#include "TimeStamp.h" - -#include -#include -#include - -namespace bts { -namespace datetime { - -const boost::gregorian::date anchor (1970, boost::gregorian::Jan, 1); -const uint64_t anchor_ticks = 621355968000000000; -const uint64_t micros_per_day = 86400000000; -const uint64_t ticks_per_micro = 10; +#include "datetime.h" + +#include "bts_assert.h" +#include "TimeStamp.h" + +#include +#include +#include + +namespace bts { +namespace datetime { + +const boost::gregorian::date anchor (1970, boost::gregorian::Jan, 1); +const uint64_t anchor_ticks = 621355968000000000; +const uint64_t micros_per_day = 86400000000; +const uint64_t ticks_per_micro = 10; const uint64_t ticks_per_day = micros_per_day * ticks_per_micro; const uint64_t ticks_mask = 0x3FFFFFFFFFFFFFFF; -// DateTimeKinds +// DateTimeKinds const uint64_t KindUnspecified = 0x0000000000000000; const uint64_t KindUtc = 0x4000000000000000; -const uint64_t KindLocal = 0x8000000000000000; - -unsigned long long ptime_to_ll(PTime const &t, uint64_t const &kind) { - uint64_t days = (t.date() - anchor).days(); - uint64_t usecs = t.time_of_day().total_microseconds(); - uint64_t ticks = anchor_ticks + usecs * ticks_per_micro + days * ticks_per_day; - return (ticks & ticks_mask) | kind; -} - -unsigned long long ptime_to_ll(PTime const &t) { - return ptime_to_ll(t, KindUnspecified); -} - -LLDateTime ll_now() { - return ptime_to_ll(boost::posix_time::microsec_clock::universal_time(), KindUtc); -} - -LLDateTime ll_local_now() { - return ptime_to_ll(boost::posix_time::microsec_clock::local_time (), KindLocal); -} - -long unix_now() { - return (boost::posix_time::microsec_clock::universal_time() - PTime(anchor)).total_microseconds() / (long)1E6; -} - -long long ll_diff_in_us (unsigned long long ll1, unsigned long long ll2) -{ -#ifndef NO_CPP0X - auto p1 = ll_to_ptime (ll1); - auto p2 = ll_to_ptime (ll2); // NOLINT - return (p2 - p1).total_microseconds (); -#else - return 0; -#endif -} - - -long long ptime_diff_in_us (PTime const &t1, PTime const &t2) -{ - return (t1 - t2).total_microseconds (); -} - -long long ptime_diff_in_ms (PTime const &t1, PTime const &t2) -{ - return (t1 - t2).total_milliseconds (); -} - -long long ptime_diff_in_secs (PTime const &t1, PTime const &t2) -{ - return (t1 - t2).total_seconds (); -} - -PTime timestamp_to_ptime(utils::TimeStamp const&t) -{ - auto now_ts = utils::TimeStamp::now(); - auto now = datetime::local_now(); - - auto delta = (now_ts - t).milliseconds(); - return now - boost::posix_time::milliseconds(delta); -} - -utils::TimeStamp ptime_to_timestamp(PTime const &t) -{ - auto now_ts = utils::TimeStamp::now(); - auto now = datetime::now(); - - auto delta = (now - t).total_milliseconds(); - return now_ts.shift_ms (delta); -} - -long long usecs_since (PTime const &t1) -{ - return (boost::posix_time::microsec_clock::universal_time () - t1).total_microseconds (); -} - -long long msecs_since (PTime const &t1) -{ - return (boost::posix_time::microsec_clock::universal_time () - t1).total_milliseconds (); -} - -PTime now () -{ - return boost::posix_time::microsec_clock::universal_time (); -} - -PTime local_now () -{ - return boost::posix_time::microsec_clock::local_time (); -} - -PTime make_ptime (int year, int month, int day) -{ - // January=1 - bts_assert_d(month > 0); - boost::gregorian::date dt (year, boost::gregorian::greg_month (month),day); - return PTime (dt); -} - -PTime make_ptime (int year, int month, int day, int hour, int minute, int second, int millisecond) -{ - boost::gregorian::date dt (year, boost::gregorian::greg_month (month),day); - return PTime (dt, boost::posix_time::time_duration(hour, minute, second) + boost::posix_time::milliseconds(millisecond)); -} - -PTime ll_to_ptime (LLDateTime ll) { - ll &= ticks_mask; - uint64_t ticks_part = ll % ticks_per_day; - uint64_t days_part = (ll - anchor_ticks) / ticks_per_day; - return PTime(anchor + boost::gregorian::date_duration(days_part), - boost::posix_time::time_duration(boost::posix_time::microseconds(ticks_part / ticks_per_micro))); -} - -extern "C" BTS_EXPORT int ll_to_cstr (LLDateTime ll, char*buffer, int buffer_sz) -{ - const int str_sz = sizeof ("20121204-14:44:02.000"); - bts_assert (buffer_sz >= str_sz); - auto str = FIX_timestamp (ll_to_ptime (ll)); - bts_assert (str.size () == str_sz-1); - memcpy (buffer,&str[0],str.size ()); - buffer[str.size ()] = '\0'; - return str.size (); -} - -std::string time_string (PTime const &t) -{ - int hours = t.time_of_day ().hours (); - int minutes = t.time_of_day ().minutes (); - int seconds = t.time_of_day ().seconds (); - int whole_ms = seconds * 1000 + minutes * 1000 * 60 + hours * 1000 * 60 * 60; - int frac_ms = t.time_of_day ().total_milliseconds () - whole_ms; - std::ostringstream out; - out << std::setw( 2 ) << std::setfill( '0' ) << hours << ":" - << std::setw( 2 ) << std::setfill( '0' ) << minutes << ":" - << std::setw( 2 ) << std::setfill( '0' ) << seconds << "." - << std::setw( 3 ) << std::setfill( '0' ) << frac_ms; - return out.str (); -} - -std::string date_string (PTime const &t) -{ - std::string result (10,' '); - sprintf (&result[0], "%02d/%02d/%04d", - t.date ().month ().as_number (), - t.date ().day ().as_number (), - (unsigned short) t.date ().year ()); - return result; -} - -std::string iso_date_time_string (PTime const &t) -{ - return boost::posix_time::to_iso_extended_string(t); -} - -std::string simple_date_time_string (PTime const &t) -{ - return boost::posix_time::to_simple_string(t); -} - -std::string FIX_date_time_string (PTime const &t) -{ - std::string result (strlen ("20121204-14:44:02"),' '); - sprintf (&result[0],"%d%02d%02d-%02d:%02d:%02d", - (unsigned short) t.date ().year (), - t.date ().month ().as_number (), - t.date ().day ().as_number (), - t.time_of_day ().hours (), - t.time_of_day ().minutes (), - t.time_of_day ().seconds ()); - return result; -} - -void FIX_timestamp (PTime const &t, std::string *out) -{ - int hours = t.time_of_day ().hours (); - int minutes = t.time_of_day ().minutes (); - int seconds = t.time_of_day ().seconds (); - int whole_ms = seconds * 1000 + minutes * 1000 * 60 + hours * 1000 * 60 * 60; - int frac_ms = t.time_of_day ().total_milliseconds () - whole_ms; - out->resize(sizeof ("20121204-14:44:02.000")); - -#ifdef WIN32 - _snprintf (&(*out)[0],out->size (), - "%04d%02d%02d-%02d:%02d:%02d.%03d", - (int) t.date ().year (), - t.date ().month ().as_number (), - t.date ().day ().as_number (), - hours, - minutes, - seconds, - frac_ms); -#else - snprintf (&(*out)[0],out->size (), - "%04d%02d%02d-%02d:%02d:%02d.%03d", - (int) t.date ().year (), - t.date ().month ().as_number (), - t.date ().day ().as_number (), - hours, - minutes, - seconds, - frac_ms); -#endif - out->resize (out->size ()-1); -} - -std::string FIX_timestamp (PTime const &t) -{ - std::string result; - FIX_timestamp(t,&result); - return result; -} - -bool is_from_today(PTime const &time) -{ - return (time.date() == boost::posix_time::microsec_clock::universal_time().date()); -} - -bool is_at_least_msecs_old (PTime const &time, uint64_t ms) -{ - PTime now = boost::posix_time::microsec_clock::universal_time (); - return (now - time).total_milliseconds () >= (int64_t) ms; -} - -bool is_at_least_secs_old (PTime const &time, uint64_t sec) -{ - PTime now = boost::posix_time::microsec_clock::universal_time (); - return (now - time).total_seconds () >= (int64_t) sec; -} - -bool is_at_least_mins_old (PTime const &time, uint64_t mins) -{ - PTime now = boost::posix_time::microsec_clock::universal_time (); - return ((now - time).total_seconds ()/60) >= (int64_t) mins; -} - -BTS_EXPORT bool is_at_least_hours_old (PTime const &time, uint64_t hours) -{ - PTime now = boost::posix_time::microsec_clock::universal_time (); - int64_t secs = (int64_t) ((now - time).total_seconds ()); - return (secs/60/60) >= (int64_t) hours; -} - -BTS_EXPORT bool is_at_least_days_old (PTime const &time, uint64_t days) -{ - return is_at_least_hours_old (time, days *24); -} - -BTS_EXPORT LLDateTime ll_from_unix_epoch(long seconds) -{ - PTime result(anchor, boost::posix_time::seconds(seconds)); - return ptime_to_ll(result, KindUnspecified); -} - -BTS_EXPORT LLDateTime ll_from_unix_epoch_ms(long long milliseconds) -{ - PTime result(anchor, boost::posix_time::millisec(milliseconds)); - return ptime_to_ll(result, KindUnspecified); -} - -BTS_EXPORT void ll_to_parts(LLDateTime ll_time, int& year, int& month, int& day, int& hour, int& minute, int& second, int& millisecond) -{ - PTime t = ll_to_ptime(ll_time); - year = t.date().year(); - month = t.date().month(); - day = t.date().day(); - hour = t.time_of_day().hours(); - minute = t.time_of_day().minutes(); - second = t.time_of_day().seconds(); - int whole_ms = second * 1000 + minute * 1000 * 60 + hour * 1000 * 60 * 60; - millisecond = t.time_of_day().total_milliseconds() - whole_ms; -} - -BTS_EXPORT LLDateTime ll_from_parts(int year, int month, int day, int hour, int minute, int second, int millisecond) -{ - return ptime_to_ll(make_ptime(year, month, day, hour, minute, second, millisecond)); // NOLINT -} - -BTS_EXPORT LLDateTime ll_from_date(const Date& date) { - if (date.year == 0 && date.day == 0 && date.month == 0) { - return 0; - } - return ll_from_parts(date.year, date.month, date.day, 0, 0, 0, 0); -} - -Date ll_to_date(LLDateTime time) { - Date date; - int year, month, day, unused; - ll_to_parts(time, year, month, day, unused, unused, unused, unused); - date.year = year; - date.month = month; - date.day = day; - return date; -} - -struct UtcTimeStamp { - int hour; - int min; - int sec; - int msec; - int day; - int month; - int year; -}; - -struct UtcTimeStampConvertor -{ - static UtcTimeStamp convert( const std::string& value) - { - bool haveMilliseconds = false; - - switch( value.size() ) - { - case 21: haveMilliseconds = true; - case 17: break; - default: bts_assert(false); - } - - int i = 0; - int c = 0; - for( c = 0; c < 8; ++c ) - if( !isdigit(value[i++]) ) bts_assert(false); - if (value[i++] != '-') bts_assert(false); - for( c = 0; c < 2; ++c ) - if( !isdigit(value[i++]) ) bts_assert(false); - if( value[i++] != ':' ) bts_assert(false); - for( c = 0; c < 2; ++c ) - if( !isdigit(value[i++]) ) bts_assert(false); - if( value[i++] != ':' ) bts_assert(false); - for( c = 0; c < 2; ++c ) - if( !isdigit(value[i++]) ) bts_assert(false); - - if( haveMilliseconds ) - { - if( value[i++] != '.' ) bts_assert(false); - for( c = 0; c < 3; ++c ) - if( !isdigit(value[i++]) ) bts_assert(false); - } - - int year, mon, mday, hour, min, sec, millis; - - i = 0; - - year = value[i++] - '0'; - year = 10 * year + value[i++] - '0'; - year = 10 * year + value[i++] - '0'; - year = 10 * year + value[i++] - '0'; - - mon = value[i++] - '0'; - mon = 10 * mon + value[i++] - '0'; - if( mon < 1 || 12 < mon ) bts_assert(false); - - mday = value[i++] - '0'; - mday = 10 * mday + value[i++] - '0'; - if( mday < 1 || 31 < mday ) bts_assert(false); - - ++i; // skip '-' - - hour = value[i++] - '0'; - hour = 10 * hour + value[i++] - '0'; - // No check for >= 0 as no '-' are converted here - if( 23 < hour ) bts_assert(false); - - ++i; // skip ':' - - min = value[i++] - '0'; - min = 10 * min + value[i++] - '0'; - // No check for >= 0 as no '-' are converted here - if( 59 < min ) bts_assert(false); - - ++i; // skip ':' - - sec = value[i++] - '0'; - sec = 10 * sec + value[i++] - '0'; - - // No check for >= 0 as no '-' are converted here - if( 60 < sec ) bts_assert(false); - - if( haveMilliseconds ) - { - millis = (100 * (value[i+1] - '0') - + 10 * (value[i+2] - '0') - + (value[i+3] - '0')); - } - else - millis = 0; - - return UtcTimeStamp {hour, min, sec, millis, mday, mon, year}; - } -}; - -PTime parse_FIX_timestamp (const std::string &ts_str) -{ - auto ts = UtcTimeStampConvertor::convert (ts_str); - boost::gregorian::date dt(ts.year, ts.month, ts.day); - // fucking boost. unbelieavable. - auto ticks_per_second = boost::posix_time::time_duration::rep_type::res_adjust(); - boost::posix_time::time_duration td (ts.hour, ts.min, ts.sec, ts.msec * (ticks_per_second / 1000)); - return PTime(dt, td); -} - -#ifndef NO_CPP0X -extern "C" BTS_EXPORT LLDateTime ll_today() { - return datetime::ll_from_date(today()); -} - -BTS_EXPORT Date today() { - auto now = datetime::now(); - Date date; - date.year = now.date().year(); - date.month = now.date().month(); - date.day = now.date().day(); - return date; -} -BTS_EXPORT Date add_days(Date indate, int days) { - boost::gregorian::date dt(indate.year, indate.month, indate.day); - auto adjusted_dt = dt + boost::gregorian::days(days); - Date date(adjusted_dt.year(), adjusted_dt.month(), adjusted_dt.day()); - return date; -} - -extern "C" BTS_EXPORT LLDateTime ll_yesterday() { - auto now = datetime::now() - boost::gregorian::days(1); - return datetime::ll_from_parts(now.date().year(), now.date().month(), now.date().day(), 0, 0, 0, 0); -} -#endif -}} +const uint64_t KindLocal = 0x8000000000000000; + +unsigned long long ptime_to_ll(PTime const &t, uint64_t const &kind) { + uint64_t days = (t.date() - anchor).days(); + uint64_t usecs = t.time_of_day().total_microseconds(); + uint64_t ticks = anchor_ticks + usecs * ticks_per_micro + days * ticks_per_day; + return (ticks & ticks_mask) | kind; +} + +unsigned long long ptime_to_ll(PTime const &t) { + return ptime_to_ll(t, KindUnspecified); +} + +LLDateTime ll_now() { + return ptime_to_ll(boost::posix_time::microsec_clock::universal_time(), KindUtc); +} + +LLDateTime ll_local_now() { + return ptime_to_ll(boost::posix_time::microsec_clock::local_time (), KindLocal); +} + +long unix_now() { + return (boost::posix_time::microsec_clock::universal_time() - PTime(anchor)).total_microseconds() / (long)1E6; +} + +long long ll_diff_in_us (unsigned long long ll1, unsigned long long ll2) +{ +#ifndef NO_CPP0X + auto p1 = ll_to_ptime (ll1); + auto p2 = ll_to_ptime (ll2); // NOLINT + return (p2 - p1).total_microseconds (); +#else + return 0; +#endif +} + + +long long ptime_diff_in_us (PTime const &t1, PTime const &t2) +{ + return (t1 - t2).total_microseconds (); +} + +long long ptime_diff_in_ms (PTime const &t1, PTime const &t2) +{ + return (t1 - t2).total_milliseconds (); +} + +long long ptime_diff_in_secs (PTime const &t1, PTime const &t2) +{ + return (t1 - t2).total_seconds (); +} + +PTime timestamp_to_ptime(utils::TimeStamp const&t) +{ + auto now_ts = utils::TimeStamp::now(); + auto now = datetime::local_now(); + + auto delta = (now_ts - t).milliseconds(); + return now - boost::posix_time::milliseconds(delta); +} + +utils::TimeStamp ptime_to_timestamp(PTime const &t) +{ + auto now_ts = utils::TimeStamp::now(); + auto now = datetime::now(); + + auto delta = (now - t).total_milliseconds(); + return now_ts.shift_ms (delta); +} + +long long usecs_since (PTime const &t1) +{ + return (boost::posix_time::microsec_clock::universal_time () - t1).total_microseconds (); +} + +long long msecs_since (PTime const &t1) +{ + return (boost::posix_time::microsec_clock::universal_time () - t1).total_milliseconds (); +} + +PTime now () +{ + return boost::posix_time::microsec_clock::universal_time (); +} + +PTime local_now () +{ + return boost::posix_time::microsec_clock::local_time (); +} + +PTime make_ptime (int year, int month, int day) +{ + // January=1 + bts_assert_d(month > 0); + boost::gregorian::date dt (year, boost::gregorian::greg_month (month),day); + return PTime (dt); +} + +PTime make_ptime (int year, int month, int day, int hour, int minute, int second, int millisecond) +{ + boost::gregorian::date dt (year, boost::gregorian::greg_month (month),day); + return PTime (dt, boost::posix_time::time_duration(hour, minute, second) + boost::posix_time::milliseconds(millisecond)); +} + +PTime ll_to_ptime (LLDateTime ll) { + ll &= ticks_mask; + uint64_t ticks_part = ll % ticks_per_day; + uint64_t days_part = (ll - anchor_ticks) / ticks_per_day; + return PTime(anchor + boost::gregorian::date_duration(days_part), + boost::posix_time::time_duration(boost::posix_time::microseconds(ticks_part / ticks_per_micro))); +} + +extern "C" BTS_EXPORT int ll_to_cstr (LLDateTime ll, char*buffer, int buffer_sz) +{ + const int str_sz = sizeof ("20121204-14:44:02.000"); + bts_assert (buffer_sz >= str_sz); + auto str = FIX_timestamp (ll_to_ptime (ll)); + bts_assert (str.size () == str_sz-1); + memcpy (buffer,&str[0],str.size ()); + buffer[str.size ()] = '\0'; + return str.size (); +} + +std::string time_string (PTime const &t) +{ + int hours = t.time_of_day ().hours (); + int minutes = t.time_of_day ().minutes (); + int seconds = t.time_of_day ().seconds (); + int whole_ms = seconds * 1000 + minutes * 1000 * 60 + hours * 1000 * 60 * 60; + int frac_ms = t.time_of_day ().total_milliseconds () - whole_ms; + std::ostringstream out; + out << std::setw( 2 ) << std::setfill( '0' ) << hours << ":" + << std::setw( 2 ) << std::setfill( '0' ) << minutes << ":" + << std::setw( 2 ) << std::setfill( '0' ) << seconds << "." + << std::setw( 3 ) << std::setfill( '0' ) << frac_ms; + return out.str (); +} + +std::string date_string (PTime const &t) +{ + std::string result (10,' '); + sprintf (&result[0], "%02d/%02d/%04d", + t.date ().month ().as_number (), + t.date ().day ().as_number (), + (unsigned short) t.date ().year ()); + return result; +} + +std::string iso_date_time_string (PTime const &t) +{ + return boost::posix_time::to_iso_extended_string(t); +} + +std::string simple_date_time_string (PTime const &t) +{ + return boost::posix_time::to_simple_string(t); +} + +std::string FIX_date_time_string (PTime const &t) +{ + std::string result (strlen ("20121204-14:44:02"),' '); + sprintf (&result[0],"%d%02d%02d-%02d:%02d:%02d", + (unsigned short) t.date ().year (), + t.date ().month ().as_number (), + t.date ().day ().as_number (), + t.time_of_day ().hours (), + t.time_of_day ().minutes (), + t.time_of_day ().seconds ()); + return result; +} + +void FIX_timestamp (PTime const &t, std::string *out) +{ + int hours = t.time_of_day ().hours (); + int minutes = t.time_of_day ().minutes (); + int seconds = t.time_of_day ().seconds (); + int whole_ms = seconds * 1000 + minutes * 1000 * 60 + hours * 1000 * 60 * 60; + int frac_ms = t.time_of_day ().total_milliseconds () - whole_ms; + out->resize(sizeof ("20121204-14:44:02.000")); + +#ifdef WIN32 + _snprintf (&(*out)[0],out->size (), + "%04d%02d%02d-%02d:%02d:%02d.%03d", + (int) t.date ().year (), + t.date ().month ().as_number (), + t.date ().day ().as_number (), + hours, + minutes, + seconds, + frac_ms); +#else + snprintf (&(*out)[0],out->size (), + "%04d%02d%02d-%02d:%02d:%02d.%03d", + (int) t.date ().year (), + t.date ().month ().as_number (), + t.date ().day ().as_number (), + hours, + minutes, + seconds, + frac_ms); +#endif + out->resize (out->size ()-1); +} + +std::string FIX_timestamp (PTime const &t) +{ + std::string result; + FIX_timestamp(t,&result); + return result; +} + +bool is_from_today(PTime const &time) +{ + return (time.date() == boost::posix_time::microsec_clock::universal_time().date()); +} + +bool is_at_least_msecs_old (PTime const &time, uint64_t ms) +{ + PTime now = boost::posix_time::microsec_clock::universal_time (); + return (now - time).total_milliseconds () >= (int64_t) ms; +} + +bool is_at_least_secs_old (PTime const &time, uint64_t sec) +{ + PTime now = boost::posix_time::microsec_clock::universal_time (); + return (now - time).total_seconds () >= (int64_t) sec; +} + +bool is_at_least_mins_old (PTime const &time, uint64_t mins) +{ + PTime now = boost::posix_time::microsec_clock::universal_time (); + return ((now - time).total_seconds ()/60) >= (int64_t) mins; +} + +BTS_EXPORT bool is_at_least_hours_old (PTime const &time, uint64_t hours) +{ + PTime now = boost::posix_time::microsec_clock::universal_time (); + int64_t secs = (int64_t) ((now - time).total_seconds ()); + return (secs/60/60) >= (int64_t) hours; +} + +BTS_EXPORT bool is_at_least_days_old (PTime const &time, uint64_t days) +{ + return is_at_least_hours_old (time, days *24); +} + +BTS_EXPORT LLDateTime ll_from_unix_epoch(long seconds) +{ + PTime result(anchor, boost::posix_time::seconds(seconds)); + return ptime_to_ll(result, KindUnspecified); +} + +BTS_EXPORT LLDateTime ll_from_unix_epoch_ms(long long milliseconds) +{ + PTime result(anchor, boost::posix_time::millisec(milliseconds)); + return ptime_to_ll(result, KindUnspecified); +} + +BTS_EXPORT void ll_to_parts(LLDateTime ll_time, int& year, int& month, int& day, int& hour, int& minute, int& second, int& millisecond) +{ + PTime t = ll_to_ptime(ll_time); + year = t.date().year(); + month = t.date().month(); + day = t.date().day(); + hour = t.time_of_day().hours(); + minute = t.time_of_day().minutes(); + second = t.time_of_day().seconds(); + int whole_ms = second * 1000 + minute * 1000 * 60 + hour * 1000 * 60 * 60; + millisecond = t.time_of_day().total_milliseconds() - whole_ms; +} + +BTS_EXPORT LLDateTime ll_from_parts(int year, int month, int day, int hour, int minute, int second, int millisecond) +{ + return ptime_to_ll(make_ptime(year, month, day, hour, minute, second, millisecond)); // NOLINT +} + +BTS_EXPORT LLDateTime ll_from_date(const Date& date) { + if (date.year == 0 && date.day == 0 && date.month == 0) { + return 0; + } + return ll_from_parts(date.year, date.month, date.day, 0, 0, 0, 0); +} + +Date ll_to_date(LLDateTime time) { + Date date; + int year, month, day, unused; + ll_to_parts(time, year, month, day, unused, unused, unused, unused); + date.year = year; + date.month = month; + date.day = day; + return date; +} + +struct UtcTimeStamp { + int hour; + int min; + int sec; + int msec; + int day; + int month; + int year; +}; + +struct UtcTimeStampConvertor +{ + static UtcTimeStamp convert( const std::string& value) + { + bool haveMilliseconds = false; + + switch( value.size() ) + { + case 21: haveMilliseconds = true; + case 17: break; + default: bts_assert(false); + } + + int i = 0; + int c = 0; + for( c = 0; c < 8; ++c ) + if( !isdigit(value[i++]) ) bts_assert(false); + if (value[i++] != '-') bts_assert(false); + for( c = 0; c < 2; ++c ) + if( !isdigit(value[i++]) ) bts_assert(false); + if( value[i++] != ':' ) bts_assert(false); + for( c = 0; c < 2; ++c ) + if( !isdigit(value[i++]) ) bts_assert(false); + if( value[i++] != ':' ) bts_assert(false); + for( c = 0; c < 2; ++c ) + if( !isdigit(value[i++]) ) bts_assert(false); + + if( haveMilliseconds ) + { + if( value[i++] != '.' ) bts_assert(false); + for( c = 0; c < 3; ++c ) + if( !isdigit(value[i++]) ) bts_assert(false); + } + + int year, mon, mday, hour, min, sec, millis; + + i = 0; + + year = value[i++] - '0'; + year = 10 * year + value[i++] - '0'; + year = 10 * year + value[i++] - '0'; + year = 10 * year + value[i++] - '0'; + + mon = value[i++] - '0'; + mon = 10 * mon + value[i++] - '0'; + if( mon < 1 || 12 < mon ) bts_assert(false); + + mday = value[i++] - '0'; + mday = 10 * mday + value[i++] - '0'; + if( mday < 1 || 31 < mday ) bts_assert(false); + + ++i; // skip '-' + + hour = value[i++] - '0'; + hour = 10 * hour + value[i++] - '0'; + // No check for >= 0 as no '-' are converted here + if( 23 < hour ) bts_assert(false); + + ++i; // skip ':' + + min = value[i++] - '0'; + min = 10 * min + value[i++] - '0'; + // No check for >= 0 as no '-' are converted here + if( 59 < min ) bts_assert(false); + + ++i; // skip ':' + + sec = value[i++] - '0'; + sec = 10 * sec + value[i++] - '0'; + + // No check for >= 0 as no '-' are converted here + if( 60 < sec ) bts_assert(false); + + if( haveMilliseconds ) + { + millis = (100 * (value[i+1] - '0') + + 10 * (value[i+2] - '0') + + (value[i+3] - '0')); + } + else + millis = 0; + + return UtcTimeStamp {hour, min, sec, millis, mday, mon, year}; + } +}; + +PTime parse_FIX_timestamp (const std::string &ts_str) +{ + auto ts = UtcTimeStampConvertor::convert (ts_str); + boost::gregorian::date dt(ts.year, ts.month, ts.day); + // fucking boost. unbelieavable. + auto ticks_per_second = boost::posix_time::time_duration::rep_type::res_adjust(); + boost::posix_time::time_duration td (ts.hour, ts.min, ts.sec, ts.msec * (ticks_per_second / 1000)); + return PTime(dt, td); +} + +#ifndef NO_CPP0X +extern "C" BTS_EXPORT LLDateTime ll_today() { + return datetime::ll_from_date(today()); +} + +BTS_EXPORT Date today() { + auto now = datetime::now(); + Date date; + date.year = now.date().year(); + date.month = now.date().month(); + date.day = now.date().day(); + return date; +} +BTS_EXPORT Date add_days(Date indate, int days) { + boost::gregorian::date dt(indate.year, indate.month, indate.day); + auto adjusted_dt = dt + boost::gregorian::days(days); + Date date(adjusted_dt.year(), adjusted_dt.month(), adjusted_dt.day()); + return date; +} + +extern "C" BTS_EXPORT LLDateTime ll_yesterday() { + auto now = datetime::now() - boost::gregorian::days(1); + return datetime::ll_from_parts(now.date().year(), now.date().month(), now.date().day(), 0, 0, 0, 0); +} +#endif +}} diff --git a/src/sdk/examples/md_publisher_example.cpp b/src/sdk/examples/md_publisher_example.cpp index d38cb424c..28b2c5d61 100644 --- a/src/sdk/examples/md_publisher_example.cpp +++ b/src/sdk/examples/md_publisher_example.cpp @@ -42,30 +42,30 @@ void produce() // initialized bid side book data std::array bids = { - md::PriceSize::from_double<7> (1.0,10), - md::PriceSize::from_double<7> (2.0,20), - md::PriceSize::from_double<7> (3.0,30), - md::PriceSize::from_double<7> (4.0,40), - md::PriceSize::from_double<7> (5.0,50), - md::PriceSize::from_double<7> (6.0,60), - md::PriceSize::from_double<7> (7.0,70), - md::PriceSize::from_double<7> (8.0,80), - md::PriceSize::from_double<7> (9.0,90), - md::PriceSize::from_double<7> (10.0,100) + md::PriceSize_from_double<7> (1.0,10), + md::PriceSize_from_double<7> (2.0,20), + md::PriceSize_from_double<7> (3.0,30), + md::PriceSize_from_double<7> (4.0,40), + md::PriceSize_from_double<7> (5.0,50), + md::PriceSize_from_double<7> (6.0,60), + md::PriceSize_from_double<7> (7.0,70), + md::PriceSize_from_double<7> (8.0,80), + md::PriceSize_from_double<7> (9.0,90), + md::PriceSize_from_double<7> (10.0,100) }; // initialized ask side book data std::array asks = { - md::PriceSize::from_double<7> (1.0,10), - md::PriceSize::from_double<7> (2.0,20), - md::PriceSize::from_double<7> (3.0,30), - md::PriceSize::from_double<7> (4.0,40), - md::PriceSize::from_double<7> (5.0,50), - md::PriceSize::from_double<7> (6.0,60), - md::PriceSize::from_double<7> (7.0,70), - md::PriceSize::from_double<7> (8.0,80), - md::PriceSize::from_double<7> (9.0,90), - md::PriceSize::from_double<7> (10.0,100) + md::PriceSize_from_double<7> (1.0,10), + md::PriceSize_from_double<7> (2.0,20), + md::PriceSize_from_double<7> (3.0,30), + md::PriceSize_from_double<7> (4.0,40), + md::PriceSize_from_double<7> (5.0,50), + md::PriceSize_from_double<7> (6.0,60), + md::PriceSize_from_double<7> (7.0,70), + md::PriceSize_from_double<7> (8.0,80), + md::PriceSize_from_double<7> (9.0,90), + md::PriceSize_from_double<7> (10.0,100) }; // Update book - this call does not use locks so should not be called concurrently diff --git a/src/server/sparkd/CMakeDeps.cmake b/src/server/sparkd/CMakeDeps.cmake index d739bee6c..36b86a8a3 100644 --- a/src/server/sparkd/CMakeDeps.cmake +++ b/src/server/sparkd/CMakeDeps.cmake @@ -1 +1 @@ -set(sources bts_trace.cpp;cmds/AutoCert.cpp;cmds/Benchmark.cpp;cmds/CmdRegistry.cpp;cmds/ConfigCheck.cpp;cmds/Help.cpp;cmds/Md.cpp;cmds/QuickTest.cpp;cmds/Run.cpp;cmds/RunSim.cpp;cmds/Span.cpp;cmds/Test.cpp;cmds/TestCeloxica.cpp;cmds/Web.cpp;dspark/mirage/quick_queue/quick_queue.cpp;exchange_sim/ExchangeSim.cpp;exchange_sim/ExchangeSimApplication.cpp;exchange_sim/book/Book.cpp;exchange_sim/book/ExchangeSim.proto;exchange_sim/book/Order.cpp;exchange_sim/book/OrderMatcher.cpp;exchange_sim/book/book_transform.cpp;exchange_sim/tests/test_book_transform.cpp;http/ApiGateway.cpp;http/WebServer.cpp;main.cpp;md/ChannelVolume.cpp;md/MarketDataAuxiliaryServiceServer.cpp;md/VolumeAtPriceState.cpp;md/platforms/celoxica/CXListener.cpp;md/platforms/celoxica/CXMain.cpp;md/platforms/celoxica/tests/cx_tests.cpp;md/platforms/mdp3_onixs/MDP3Listener.cpp;md/platforms/mdp3_onixs/MDP3Main.cpp;md/platforms/mdp3_onixs/tests/mdp3_tests.cpp;md/run_md.cpp;position/IMarginObserver.cpp;position/IPositionObserver.cpp;position/MarginServiceCore.cpp;position/PositionServiceCore.cpp;position/PositionServiceServer.cpp;position/tests/PositionTests.cpp;support/ErrorReportService.cpp;support/ShellLatencyServer.cpp;user/AdminServiceServer.cpp;user/UserServiceServer.cpp;user/tests/admin_service_tests.cpp) \ No newline at end of file +set(sources bts_trace.cpp;cmds/AutoCert.cpp;cmds/Benchmark.cpp;cmds/CmdRegistry.cpp;cmds/ConfigCheck.cpp;cmds/Help.cpp;cmds/Md.cpp;cmds/QuickTest.cpp;cmds/Run.cpp;cmds/RunSim.cpp;cmds/Span.cpp;cmds/Test.cpp;cmds/TestCeloxica.cpp;cmds/Web.cpp;dspark/mirage/quick_queue/quick_queue.cpp;exchange_sim/ExchangeSim.cpp;exchange_sim/ExchangeSimApplication.cpp;exchange_sim/book/Book.cpp;exchange_sim/book/ExchangeSim.proto;exchange_sim/book/Order.cpp;exchange_sim/book/OrderMatcher.cpp;exchange_sim/book/book_transform.cpp;exchange_sim/tests/test_book_transform.cpp;http/ApiGateway.cpp;http/WebServer.cpp;main.cpp;md/platforms/celoxica/CXListener.cpp;md/platforms/celoxica/CXMain.cpp;md/platforms/celoxica/tests/cx_tests.cpp;md/platforms/mdp3_onixs/MDP3Listener.cpp;md/platforms/mdp3_onixs/MDP3Main.cpp;md/platforms/mdp3_onixs/tests/mdp3_tests.cpp;md/run_md.cpp;position/IMarginObserver.cpp;position/IPositionObserver.cpp;position/MarginServiceCore.cpp;position/PositionServiceCore.cpp;position/PositionServiceServer.cpp;position/tests/PositionTests.cpp;support/ErrorReportService.cpp;support/ShellLatencyServer.cpp;user/AdminServiceServer.cpp;user/UserServiceServer.cpp;user/tests/admin_service_tests.cpp) \ No newline at end of file diff --git a/src/server/sparkd/cmds/Benchmark.cpp b/src/server/sparkd/cmds/Benchmark.cpp index 106d0cb58..76c83eab7 100644 --- a/src/server/sparkd/cmds/Benchmark.cpp +++ b/src/server/sparkd/cmds/Benchmark.cpp @@ -28,7 +28,9 @@ #include #include #include +#include #include + #include namespace bts { @@ -444,18 +446,6 @@ static void tbb_concurrent_queue(benchmark::State &state) { } BENCHMARK(tbb_concurrent_queue); -static void moody_camel_queue(benchmark::State &state) { - using Q = moodycamel::ConcurrentQueue; - Q queue(1000000); - for (auto _ : state) { - void *result; - void *arg = &queue; - queue.enqueue(arg); // NOLINT - benchmark::DoNotOptimize(queue.try_dequeue(result)); - } -} - -BENCHMARK(moody_camel_queue); static void boost_spsc(benchmark::State &state) { boost::lockfree::spsc_queue q(10000); @@ -658,6 +648,30 @@ static void same_thread_mpmc_spdlog(benchmark::State &state) { } BENCHMARK(same_thread_mpmc_spdlog); +static void moodycamel_concurrent_queue(benchmark::State &state) { + using Q = moodycamel::ConcurrentQueue; + Q queue(8); + int i = 0; + + for (auto _ : state) { + queue.enqueue(++i); // NOLINT + benchmark::DoNotOptimize(queue.try_dequeue(i)); + } +} +BENCHMARK(moodycamel_concurrent_queue); + +static void moodycamel_spsc_queue(benchmark::State &state) { + using Queue = moodycamel::ReaderWriterQueue; + Queue q1(8); + int i = 0; + + for (auto _ : state) { + q1.enqueue(++i); + benchmark::DoNotOptimize(q1.try_dequeue(i)); + } +} +BENCHMARK(moodycamel_spsc_queue); + std::string Benchmark::help() { return "Run micro-benchmarks. Use --benchmark_filter=regex to run only " "specific benchmarks."; diff --git a/src/server/sparkd/cmds/PrintHedgeOrders.cpp b/src/server/sparkd/cmds/PrintHedgeOrders.cpp new file mode 100644 index 000000000..3acf45c27 --- /dev/null +++ b/src/server/sparkd/cmds/PrintHedgeOrders.cpp @@ -0,0 +1,36 @@ +#include "server/sparkd/cmds/PrintHedgeOrders.h" +#include "lib/persistence/SqliteStore.h" +#include "lib/api/services/OrderService.pb.h" +#include "lib/utils/PBUtils.h" + +namespace bts { +namespace cmd { + +int PrintHedgeOrders::execute(std::vector const &) { + persistence::SqliteStore store("orders"); + auto all = store.retrieve_all()["orders"]; + + std::cout << "order_key,time_to_completed" << "\n"; + api::orderman::OrderRecord rec; + for (auto const &kvp : all) + if (utils::pb::from_string(kvp.second, &rec)) { + auto &def = rec.def(); + auto &state = rec.state(); + if (rec.parent_key() && + def.custom_def().has_limit() && + def.sub_type() == api::orderman::OrderSubType::ORDER_SUB_TYPE_HEDGING_LEG) { + auto created = state.created().seconds()* 1e9 + state.created().nanos(); + auto updated = state.last_change().seconds()* 1e9 + state.last_change().nanos(); + std::cout << kvp.first << "," << (updated-created) / 1000 / 1000 / 1000 << "\n"; + } + } + return 0; +} + +std::string PrintHedgeOrders::help(){ + return + "Print out order ids for all hedge orders" + "which were completed at least one second being created"; +} + +}} diff --git a/src/server/sparkd/cmds/PrintHedgeOrders.h b/src/server/sparkd/cmds/PrintHedgeOrders.h new file mode 100644 index 000000000..3ce5b1b6c --- /dev/null +++ b/src/server/sparkd/cmds/PrintHedgeOrders.h @@ -0,0 +1,17 @@ +#pragma once + +#include "ICmd.h" + +namespace bts { +namespace cmd { + +class PrintHedgeOrders: public ICmd +{ +public: + int execute(std::vector const &args); + std::string help (); +}; + +}} + + diff --git a/src/server/sparkd/cmds/Run.cpp b/src/server/sparkd/cmds/Run.cpp index fec89b31a..06f121f3f 100644 --- a/src/server/sparkd/cmds/Run.cpp +++ b/src/server/sparkd/cmds/Run.cpp @@ -18,7 +18,6 @@ #include "lib/orderman/server/OrderServiceServer.h" #include "lib/persistence/PersistenceServiceServer.h" #include "lib/utils/thread_manager.h" -#include "server/sparkd/exchange_sim/ExchangeSim.h" #include "server/sparkd/http/WebServer.h" #include "server/sparkd/md/run_md.h" #include "server/sparkd/position/PositionServiceServer.h" @@ -31,7 +30,6 @@ #include "lib/plugin/SparkServices.h" #include "lib/plugin/plugin.h" #include "lib/config/protos/toggles.pb.h" - #include namespace bts { @@ -72,7 +70,7 @@ int Run::execute(std::vector const &) { orderman::OrderServiceServer order_service_server(sc, synthetic_service,dyn_order_registry, stats, perf_log); support::ErrorReportServer error_report_service(sc); - admin::AdminServiceServer admin_service(sc); + admin::AdminServiceServer admin_service(sc); if(config::get().enabled()) md::auxiliary::MarketDataAuxiliaryServiceServer md_aux_server(sc); position::PositionServiceServer position_service(sc); @@ -91,9 +89,6 @@ int Run::execute(std::vector const &) { timer2.start(); stats.publish(bg_health); - threading::make_thread("ExchangeSim", []() { sim::start_exchange_sim(); }) - .detach(); - std::this_thread::sleep_until( std::chrono::time_point::max()); return 0; diff --git a/src/server/sparkd/exchange_sim/ExchangeSim.cpp b/src/server/sparkd/exchange_sim/ExchangeSim.cpp index bdba0e269..0dda72a44 100644 --- a/src/server/sparkd/exchange_sim/ExchangeSim.cpp +++ b/src/server/sparkd/exchange_sim/ExchangeSim.cpp @@ -11,6 +11,7 @@ #include "lib/os/os.h" #include "lib/config/protos/Ports.pb.h" +#include "lib/config/protos/ExchangeSimCfg.pb.h" #include "lib/config/config.h" #include @@ -18,6 +19,7 @@ #include "lib/md/relay/RelayClient.h" #include "lib/domain_model/Security.pb.h" #include "lib/md/relay/RelayRecorder.h" +#include "lib/md/IntPrice.h" #include @@ -41,12 +43,18 @@ auto cfg = "ValidateUserDefinedFields=N\n" "ValidateFieldsOutOfOrder=N\n"; +md::PriceSize coerce_precision(const co2::PriceSizeMessage& p) { + md::IntPrice raw = { p.mantissa(), (int8_t)p.exponent() }; + auto price = md::IntPrice_from_double(raw.price()); + return { price, p.size() }; +} struct MDPlayer : md::IRelayWriter { MDPlayer (IMDPlayerObserver &obs,std::map &known_securities) : obs (obs), - known_securities(known_securities) + known_securities(known_securities), + forward_implied(config::get().forward_implied()) {} @@ -65,21 +73,51 @@ struct MDPlayer : md::IRelayWriter else sec_ptr = &it->second; - auto const &book = cr.book(); + auto const &book = cr.direct(); md::PriceSize bids[md::COCONUT_MAX_DEPTH], asks[md::COCONUT_MAX_DEPTH]; size_t i = 0; for (auto &b : book.bids()) - bids[i++] = md::PriceSize(b.mantissa(),b.exponent(),b.size()); + bids[i++] = md::PriceSize({b.mantissa(),(int8_t)b.exponent()},b.size()); size_t n = 0; for (auto &a : book.asks()) - asks[n++] = md::PriceSize(a.mantissa(),a.exponent(),a.size()); - obs.on_depth(*sec_ptr, bids, asks); + asks[n++] = md::PriceSize({a.mantissa(),(int8_t)a.exponent()},a.size()); + + + std::array implied_bids, implied_asks; + bool has_implied = false; + {size_t x = 0; + for (const auto& b : cr.implied().bids()) { + implied_bids[x] = coerce_precision(b); + x += 1; + has_implied = true; + }} + {size_t x = 0; + for (const auto& b : cr.implied().asks()) { + implied_asks[x] = coerce_precision(b); + x += 1; + has_implied = true; + }} + + if (has_implied && forward_implied) { + obs.on_depth(*sec_ptr, bids, asks, &implied_bids, &implied_asks); + } + else { + obs.on_depth(*sec_ptr, bids, asks, nullptr, nullptr); + } + + if (cr.has_misc()) { + if (cr.misc().has_settlement()) { + const auto& s = cr.misc().settlement(); + obs.on_settlement(*sec_ptr, { s.mantissa(), (int8_t)s.exponent() }); + } + } } } IMDPlayerObserver &obs; std::map &known_securities; + bool forward_implied; }; @@ -104,22 +142,22 @@ int start_exchange_sim() acceptor.start(); std::map known_securities; - const auto& conf = config::get(); + const auto& conf = config::get(); while ( true ) { MDPlayer player(application,known_securities); switch(conf.source_case()) { - case md::SimSource::SOURCE_NOT_SET: - case md::SimSource::kReplayUrl: + case config::ExchangeSim::SOURCE_NOT_SET: + case config::ExchangeSim::kReplayUrl: md::replay_url(conf.replay_url(), player, config::get().naggle_ms()); break; - case md::SimSource::kReplayFile: + case config::ExchangeSim::kReplayFile: md::replay_file(conf.replay_file(), player, config::get().naggle_ms(), true); break; - case md::SimSource::kLocalRelay: { + case config::ExchangeSim::kLocalRelay: { md::local_relay(player); } break; diff --git a/src/server/sparkd/exchange_sim/ExchangeSimApplication.cpp b/src/server/sparkd/exchange_sim/ExchangeSimApplication.cpp index a214ff1b5..2a36f53f6 100644 --- a/src/server/sparkd/exchange_sim/ExchangeSimApplication.cpp +++ b/src/server/sparkd/exchange_sim/ExchangeSimApplication.cpp @@ -22,6 +22,7 @@ #include #include #include +#include namespace bts { namespace ex_sim { @@ -464,7 +465,7 @@ void ExchangeSimApplication::update_md(std::string const &sec_desc) { : book.bids.end(); std::transform(book.bids.begin(), bids_end, bids_temp.begin(), [&](const auto &bs) { - return md::PriceSize::from_double<7>( + return md::PriceSize_from_double( bs.first.value() * price_factor, bs.second.value()); }); auto asks_end = book.asks.size() > cmax @@ -472,16 +473,16 @@ void ExchangeSimApplication::update_md(std::string const &sec_desc) { : book.asks.end(); std::transform(book.asks.begin(), asks_end, asks_temp.begin(), [&](const auto &bs) { - return md::PriceSize::from_double<7>( + return md::PriceSize_from_double( bs.first.value() * price_factor, bs.second.value()); }); - handle.update_direct(utils::TimeStamp::now(), bids_temp, asks_temp,me->md_sn++); + handle.update_direct(utils::TimeStamp::now(), bids_temp, asks_temp, me->md_sn++); auto new_trades = matcher.get_new_trades(); for (auto &t : new_trades) handle.add_trade( utils::TimeStamp::now(), - md::PriceSize::from_double<7>(t.price.value() * price_factor, + md::PriceSize_from_double<7>(t.price.value() * price_factor, t.quantity.value()), datetime::ll_now(), co2::AGGRESSOR_SIDE_NONE); @@ -529,9 +530,21 @@ void ExchangeSimApplication::on_new_security(api::Security const &s) { } } +void ExchangeSimApplication::on_settlement(md::Security const& sec, md::IntPrice price) { + auto sec_desc = get_sim_sec_desc(sec); + auto iter = me->md_producer_handles.find(sec_desc); + if (iter != me->md_producer_handles.end()) { + auto &handle = iter->second; + handle.update_settlement(utils::TimeStamp::now(), price, datetime::today()); + } +} + void ExchangeSimApplication::on_depth( - md::Security const &sec, md::PriceSize bids[md::COCONUT_MAX_DEPTH], - md::PriceSize asks[md::COCONUT_MAX_DEPTH]) { + md::Security const &sec, + md::PriceSize bids[md::COCONUT_MAX_DEPTH], + md::PriceSize asks[md::COCONUT_MAX_DEPTH], + std::array* implied_bids, + std::array* implied_asks) { std::lock_guard l(me->lock); if (sec.has_synthetic()) @@ -566,6 +579,15 @@ void ExchangeSimApplication::on_depth( // we generate a set of orders that we apply to our order book merge_with_order_book(it->second.security.glbx().security_desc(), previous_md_book, current_md_book); + + // pass on implied market unchanged + if (implied_bids || implied_asks) { + auto iter = me->md_producer_handles.find(get_sim_sec_desc(sec)); + if (iter != me->md_producer_handles.end()) { + auto &handle = iter->second; + handle.update_implied(utils::TimeStamp::now(), *implied_bids, *implied_asks); + } + } } void ExchangeSimApplication::merge_with_order_book(std::string const &sec_desc, diff --git a/src/server/sparkd/exchange_sim/ExchangeSimApplication.h b/src/server/sparkd/exchange_sim/ExchangeSimApplication.h index 8f7cdc54e..90bfa7edf 100644 --- a/src/server/sparkd/exchange_sim/ExchangeSimApplication.h +++ b/src/server/sparkd/exchange_sim/ExchangeSimApplication.h @@ -45,6 +45,7 @@ namespace bts { namespace api {class Security;} namespace md {class MDConsumerHandle;} namespace ex_sim { +static const int precision = 9; class Order; struct Book; class OrderBook; @@ -99,9 +100,12 @@ public: void persist_book(OrderBook const &b); void on_new_security(md::Security const&) override; + void on_settlement(md::Security const&, md::IntPrice) override; void on_depth(md::Security const&s, md::PriceSize bids[md::COCONUT_MAX_DEPTH], - md::PriceSize asks[md::COCONUT_MAX_DEPTH]) override; + md::PriceSize asks[md::COCONUT_MAX_DEPTH], + std::array* implied_bids, + std::array* implied_asks) override; void merge_with_order_book(std::string const &sec_desc, Book const&previous,Book const¤t); }; }} diff --git a/src/server/sparkd/exchange_sim/IMDPlayerObserver.h b/src/server/sparkd/exchange_sim/IMDPlayerObserver.h index 6ba748133..b2e194b34 100644 --- a/src/server/sparkd/exchange_sim/IMDPlayerObserver.h +++ b/src/server/sparkd/exchange_sim/IMDPlayerObserver.h @@ -9,9 +9,12 @@ namespace sim { struct IMDPlayerObserver { virtual void on_new_security(md::Security const&) = 0; + virtual void on_settlement(const md::Security&, md::IntPrice) = 0; virtual void on_depth(md::Security const&s, md::PriceSize bids[md::COCONUT_MAX_DEPTH], - md::PriceSize asks[md::COCONUT_MAX_DEPTH]) = 0; + md::PriceSize asks[md::COCONUT_MAX_DEPTH], + std::array* implied_bids, + std::array* implied_asks) = 0; }; }} diff --git a/src/server/sparkd/exchange_sim/book/Book.h b/src/server/sparkd/exchange_sim/book/Book.h index b98439952..9ecf17621 100644 --- a/src/server/sparkd/exchange_sim/book/Book.h +++ b/src/server/sparkd/exchange_sim/book/Book.h @@ -1,7 +1,7 @@ #pragma once - #include "lib/domain_model/Security.pb.h" #include "lib/orderman/orderman_utils/orderman_common.h" +#include "server/sparkd/exchange_sim/book/Order.h" #include namespace bts { @@ -24,6 +24,9 @@ struct PriceCmp { PriceCmp(bool ascending) : ascending(ascending) {} bool operator()(Price p1, Price p2) const { + if (price_equals(p1.value(), p2.value())) + return false; + if (ascending) return p1 < p2; else @@ -44,7 +47,8 @@ struct Book { Quantity result(0); for (auto &kvp : bids) - if (level.value() <= kvp.first.value()) + if (price_equals(level.value(), kvp.first.value()) || + level.value() < kvp.first.value()) result.value() += kvp.second.value(); else break; @@ -53,7 +57,8 @@ struct Book { return BookOp{BookOp::ORDER, api::SIDE_BUY, level, result}; for (auto &kvp : asks) - if (level.value() >= kvp.first.value()) + if (price_equals(level.value(), kvp.first.value()) || + level.value() > kvp.first.value()) result.value() += kvp.second.value(); return BookOp{BookOp::ORDER, api::SIDE_SELL, level, result}; @@ -81,11 +86,11 @@ struct Book { return; BookSide *side; - if (op.side == api::SIDE_BUY) + if (op.side == api::SIDE_BUY) side = &bids; else side = &asks; - + Quantity *current_qty; current_qty = &(*side)[op.price]; @@ -93,9 +98,9 @@ struct Book { case BookOp::ORDER: current_qty->value() += op.quantity.value(); break; - case BookOp::CANCEL: + case BookOp::CANCEL: bts_assert(current_qty->value() >= op.quantity.value()); - current_qty->value()-=op.quantity.value(); + current_qty->value() -= op.quantity.value(); if (current_qty->value() == 0) side->erase(op.price); break; @@ -105,7 +110,8 @@ struct Book { void match() { if ((bids.size() && asks.size()) && - (bids.begin()->first.value() >= asks.begin()->first.value())) { + (price_equals(bids.begin()->first.value(),asks.begin()->first.value()) || + bids.begin()->first.value() > asks.begin()->first.value())) { auto bsz = bids.begin()->second.value(); auto asz = asks.begin()->second.value(); @@ -132,5 +138,5 @@ inline std::ostream &operator<<(std::ostream &out, const Book &book) { out << it->first.value() << "\t\t" << it->second.value() << "\n"; return out; } -} -} +} // namespace ex_sim +} // namespace bts diff --git a/src/server/sparkd/exchange_sim/book/Order.h b/src/server/sparkd/exchange_sim/book/Order.h index fd0ca0ce8..26821620c 100644 --- a/src/server/sparkd/exchange_sim/book/Order.h +++ b/src/server/sparkd/exchange_sim/book/Order.h @@ -14,18 +14,22 @@ namespace ex_sim { void execute(Order *order, Price price, Quantity quantity); +inline bool price_equals(double p1, double p2) { + return std::abs(p1-p2) <= 1e-9; +} + struct OrderCmp { OrderCmp(bool ascending) : ascending(ascending) {} bool operator()(Order const &lhs, Order const &rhs) const { if (ascending) { - if (lhs.price() != rhs.price()) + if (!price_equals(lhs.price(),rhs.price())) return lhs.price() < rhs.price(); else return utils::pb::smaller(lhs.create_time(),rhs.create_time()); } else { - if (lhs.price() != rhs.price()) + if (!price_equals(lhs.price(),rhs.price())) return lhs.price() > rhs.price(); else return utils::pb::smaller(lhs.create_time(),rhs.create_time()); diff --git a/src/server/sparkd/exchange_sim/book/OrderMatcher.cpp b/src/server/sparkd/exchange_sim/book/OrderMatcher.cpp index 5af642c97..a4a5c0977 100644 --- a/src/server/sparkd/exchange_sim/book/OrderMatcher.cpp +++ b/src/server/sparkd/exchange_sim/book/OrderMatcher.cpp @@ -60,10 +60,10 @@ OrderMatcher::find_level(OrdersSide &side, double level) for (auto it = side.begin() ; it != side.end(); it++) { - if (it->price() == level) { + if (price_equals(it->price(),level)) { result.first = it; for (;it != side.end(); it++) { - if (it->price() != level) + if (!price_equals(it->price(),level)) break; } result.second = it; diff --git a/src/server/sparkd/http/WebServer.cpp b/src/server/sparkd/http/WebServer.cpp index 3a30efa71..653c26ecf 100644 --- a/src/server/sparkd/http/WebServer.cpp +++ b/src/server/sparkd/http/WebServer.cpp @@ -117,7 +117,11 @@ namespace http { GatewayRequest request; if(request.ParseFromString(msg->get_payload())) { iter->second.handle(request, [this,hdl](const GatewayResponse& response) { - _server.send(hdl, response.SerializeAsString(), websocketpp::frame::opcode::binary); + websocketpp::lib::error_code ec; + _server.send(hdl, response.SerializeAsString(), websocketpp::frame::opcode::binary, ec); + if (ec) { + LOG(Api,Warn, "Error sending from websocket: '{}'", ec.message()); + } }); } else { diff --git a/src/server/sparkd/main.cpp b/src/server/sparkd/main.cpp index 20971e242..37d790c76 100644 --- a/src/server/sparkd/main.cpp +++ b/src/server/sparkd/main.cpp @@ -1,20 +1,21 @@ -#include "lib/utils/thread_manager.h" #include "lib/utils/init_spark_lib.h" +#include "lib/utils/thread_manager.h" #include "server/sparkd/cmds/CmdRegistry.h" // to add a command add the header here and the the REGISTER call below // don't touch this file except to add commands. +#include "server/sparkd/cmds/AutoCert.h" #include "server/sparkd/cmds/Benchmark.h" #include "server/sparkd/cmds/ConfigCheck.h" #include "server/sparkd/cmds/Help.h" #include "server/sparkd/cmds/Md.h" +#include "server/sparkd/cmds/PrintHedgeOrders.h" +#include "server/sparkd/cmds/QuickTest.h" #include "server/sparkd/cmds/Run.h" #include "server/sparkd/cmds/RunSim.h" +#include "server/sparkd/cmds/Span.h" #include "server/sparkd/cmds/Test.h" #include "server/sparkd/cmds/TestCeloxica.h" -#include "server/sparkd/cmds/AutoCert.h" -#include "server/sparkd/cmds/QuickTest.h" -#include "server/sparkd/cmds/Span.h" #include "server/sparkd/cmds/Web.h" using namespace bts; @@ -35,6 +36,7 @@ int main(int argc, char **argv) { REGISTER(AutoCert); REGISTER(Span); REGISTER(Web); + REGISTER(PrintHedgeOrders); END_CMD; utils::finit_spark_lib(); diff --git a/src/server/sparkd/md/ChannelVolume.cpp b/src/server/sparkd/md/ChannelVolume.cpp index c701687d1..89a6132a0 100644 --- a/src/server/sparkd/md/ChannelVolume.cpp +++ b/src/server/sparkd/md/ChannelVolume.cpp @@ -27,13 +27,19 @@ const VolumeAtPriceLevel& ChannelVolume::add_trade(const TradeMessage& m, VolumeAtPriceLevel vol; vol.set_record_id(record_id++); vol.set_security_id(m.key()); - auto p = md::PriceSize(m.mantissa(),m.exponent(),0); + IntPrice p = md::IntPrice (m.mantissa(),(int8_t)m.exponent()); vol.set_price(p.price()); price_map[m.mantissa()] = vol; price_it=price_map.find(m.mantissa()); } VolumeAtPriceLevel& vol=price_it->second; vol.set_volume(vol.volume()+m.size()); + if(m.aggressor_side()==AggressorSide::AGGRESSOR_SIDE_BUY) + vol.set_buy_volume(vol.buy_volume()+m.size()); + else if(m.aggressor_side()==AggressorSide::AGGRESSOR_SIDE_SELL) + vol.set_sell_volume(vol.sell_volume()+m.size()); + else if(m.aggressor_side()==AggressorSide::AGGRESSOR_SIDE_NONE) + vol.set_passive_volume(vol.passive_volume()+m.size()); return vol; } diff --git a/src/server/sparkd/md/MarketDataAuxiliaryServiceServer.cpp b/src/server/sparkd/md/MarketDataAuxiliaryServiceServer.cpp index 8c8d6c454..17886e386 100644 --- a/src/server/sparkd/md/MarketDataAuxiliaryServiceServer.cpp +++ b/src/server/sparkd/md/MarketDataAuxiliaryServiceServer.cpp @@ -8,6 +8,8 @@ #include "lib/md/get_symbols.h" #include "VolumeAtPriceState.h" #include "lib/utils/bts_chrono.h" +#include "lib/config/config.h" +#include "lib/config/protos/toggles.pb.h" namespace bts{ namespace md{ @@ -17,19 +19,26 @@ using namespace bts::co2; using namespace bts::api::md::auxiliary; struct MarketDataAuxiliaryServiceServer::PImpl{ + bool vap_enabled; + bts::api::ServiceContext& context; MarketDataRelay relay_service; MarketDataAuxiliaryServiceBase::Volume_at_priceStatefulPublisher publisher; std::unique_ptr trade_timer; int64_t poll_period_ms; VolumeAtPriceState state; + uint16_t log_counter, log_trunc; PImpl(bts::api::ServiceContext& context, const MarketDataAuxiliaryServiceBase::Volume_at_priceStatefulPublisher& publisher) - : + : + vap_enabled(config::get().enabled()), + context(context), relay_service(context.locate()), publisher(publisher), trade_timer(), poll_period_ms(100), - state(){ + state(), + log_counter(0), + log_trunc(300){ } }; @@ -39,19 +48,18 @@ MarketDataAuxiliaryServiceServer::MarketDataAuxiliaryServiceServer( MarketDataAuxiliaryServiceBase(context), me(new PImpl(context, make_volume_at_price_stateful_publisher())){ - auto& threadpool = utils::Dispatcher::get_named("TradesQuery"); - me->trade_timer = - std::make_unique(threadpool, - me->poll_period_ms, - [this]() { - me->relay_service.requestmd(me->state.md_req(), + if(me->vap_enabled) + me->context.post([this](){_poll_md();},me->poll_period_ms); +} + +void MarketDataAuxiliaryServiceServer::_poll_md(){ + me->relay_service.requestmd(me->state.md_req(), [this](const bts::api::Result& md_reply) - { - if(md_reply.result()!=nullptr) - _scan_trades(*md_reply.result());} - ); + { + if(md_reply.result()!=nullptr) + _scan_trades(*md_reply.result()); + me->context.post([this](){_poll_md();},me->poll_period_ms); }); - me->trade_timer->start(); } void MarketDataAuxiliaryServiceServer::_scan_trades(const MDReply& md_reply){ @@ -64,10 +72,13 @@ void MarketDataAuxiliaryServiceServer::_scan_trades(const MDReply& md_reply){ total_unique_levels); bts::TimeStamp t1=bts::Timer::now(); double usecs=static_cast(us_diff (t1,t0)); - LOG(OrderMan,Info,"MarketDataAuxiliaryServiceServer::_scan_trades " + if(me->log_counter++ >= me->log_trunc){ + LOG(OrderMan,Info,"MarketDataAuxiliaryServiceServer::_scan_trades " "trades:{} tot_unique:{} usecs:{}", n_trades,total_unique_levels,usecs); - me->publisher.flush(); + me->log_counter=0; + } + me->publisher.flush(); } MarketDataAuxiliaryServiceServer::~MarketDataAuxiliaryServiceServer(){ diff --git a/src/server/sparkd/md/MarketDataAuxiliaryServiceServer.h b/src/server/sparkd/md/MarketDataAuxiliaryServiceServer.h index c762545eb..04bbd3b3e 100644 --- a/src/server/sparkd/md/MarketDataAuxiliaryServiceServer.h +++ b/src/server/sparkd/md/MarketDataAuxiliaryServiceServer.h @@ -18,6 +18,7 @@ private: struct PImpl; std::unique_ptr me; private: + void _poll_md(); void _scan_trades(const bts::co2::MDReply&); public: MarketDataAuxiliaryServiceServer(bts::api::ServiceContext& context); diff --git a/src/server/sparkd/md/VolumeAtPriceState.cpp b/src/server/sparkd/md/VolumeAtPriceState.cpp index a42d5fc08..45a89f92b 100644 --- a/src/server/sparkd/md/VolumeAtPriceState.cpp +++ b/src/server/sparkd/md/VolumeAtPriceState.cpp @@ -43,7 +43,7 @@ ChannelVolume& VolumeAtPriceState::_update_or_add(const SecurityChannel& channel void VolumeAtPriceState::on_new_trades(const ChannelReply& reply, MarketDataAuxiliaryServiceBase::Volume_at_priceStatefulPublisher& publisher, uint32_t& n_trades, - uint32_t& total_unique_levels){ + uint32_t& total_unique_levels){ ChannelVolume& channel_vol=_update_or_add(reply.channel()); for(const TradeMessage& t : reply.trade_log()){ const VolumeAtPriceLevel& sec_level_vol=channel_vol.add_trade(t, diff --git a/src/server/sparkd/md/platforms/mdp3_onixs/MDP3Listener.cpp b/src/server/sparkd/md/platforms/mdp3_onixs/MDP3Listener.cpp index fa2b252cf..de7fd2d39 100644 --- a/src/server/sparkd/md/platforms/mdp3_onixs/MDP3Listener.cpp +++ b/src/server/sparkd/md/platforms/mdp3_onixs/MDP3Listener.cpp @@ -16,9 +16,8 @@ MDP3Listener::MDP3Listener( CMESecurityIndex& index, MDProducer& producer, SecurityIDRecorder& recorder, - co2::SettlementRecorder& , - const std::vector channels, - CMEOptions::BookType bt) : + // co2::SettlementRecorder& , + const std::vector channels) : _index(index), _producer(producer), _packet_arrived(), @@ -28,7 +27,6 @@ MDP3Listener::MDP3Listener( _symbol_filter_storage(), _symbol_filter(), _channels(channels), - _booktype(bt), _concurrency_monitor(0), _book_updates() { @@ -115,15 +113,11 @@ void MDP3Listener::onEndOfPacket(onixs::Handler&, const onixs::PacketArgs&) { // Security discovery void MDP3Listener::onBookUpdate(onixs::Handler&, const onixs::Security& sec, const onixs::DirectBook& book) { - if (_booktype == CMEOptions::BOOKTYPE_DIRECT) { - onBookUpdate(sec, book); - } + onBookUpdate(sec, book, book_type_direct); } -void MDP3Listener::onBookUpdate(onixs::Handler&, const onixs::Security& sec, const onixs::ConsolidatedBook& book) { - if (_booktype == CMEOptions::BOOKTYPE_CONSOLIDATED) { - onBookUpdate(sec, book); - } +void MDP3Listener::onBookUpdate(onixs::Handler&, const onixs::Security& sec, const onixs::ImpliedBook& book) { + onBookUpdate(sec, book, book_type_implied); } // @@ -245,8 +239,21 @@ void MDP3Listener::flush_books() { // This is called at the end of the packet // Write all book updates to shared memory and reset for the next packet for (auto& entry : _book_updates) { - uint16_t sequence = (_latest_sequence & 0xFFFF); - entry.handle.update_direct(_packet_arrived, entry.bids, entry.asks, sequence); + uint32_t sequence = _latest_sequence; + if (entry.type_flags == book_type_direct) { + entry.handle.update_direct(_packet_arrived, + entry.bids.first, entry.asks.first, sequence); + } + else if (entry.type_flags == book_type_implied) { + entry.handle.update_implied(_packet_arrived, + entry.bids.second, entry.asks.second, sequence); + } + else if (entry.type_flags == book_type_both) { + entry.handle.update_books(_packet_arrived, + entry.bids.first, entry.asks.first, + entry.bids.second, entry.asks.second, + sequence); + } } // Reset pending updates _book_updates.clear(); @@ -258,7 +265,7 @@ void MDP3Listener::update_all_status(CoconutSecurityStatus status) { }); } -md::MDP3Listener::BookData::BookData(MDProducerHandle & handle) : handle(handle), bids(), asks() { } +md::MDP3Listener::BookData::BookData(MDProducerHandle & handle) : handle(handle), type_flags(0), bids(), asks() { } }}//namespace diff --git a/src/server/sparkd/md/platforms/mdp3_onixs/MDP3Listener.h b/src/server/sparkd/md/platforms/mdp3_onixs/MDP3Listener.h index c960af0bf..b95f3ab5e 100644 --- a/src/server/sparkd/md/platforms/mdp3_onixs/MDP3Listener.h +++ b/src/server/sparkd/md/platforms/mdp3_onixs/MDP3Listener.h @@ -12,22 +12,28 @@ #include #include #include +#include namespace bts { namespace md { + class MDP3Listener : public onixs::HandlerListener, public onixs::MarketDataListener, public onixs::SecurityListener { + typedef uint32_t BookType; + static constexpr BookType book_type_direct=1; + static constexpr BookType book_type_implied=2; + static constexpr BookType book_type_both=(book_type_direct|book_type_implied); + public: MDP3Listener(CMESecurityIndex& index, MDProducer&, SecurityIDRecorder&, - co2::SettlementRecorder&, - const std::vector, - CMEOptions::BookType); + // co2::SettlementRecorder&, + const std::vector); virtual ~MDP3Listener(); // @@ -71,11 +77,11 @@ public: // Book // void onBookUpdate(onixs::Handler&, const onixs::Security&, const onixs::MboBook&) override; void onBookUpdate(onixs::Handler&, const onixs::Security&, const onixs::DirectBook&) override; - // void onBookUpdate(onixs::Handler&, const onixs::Security&, const onixs::ImpliedBook&) override; - void onBookUpdate(onixs::Handler&, const onixs::Security&, const onixs::ConsolidatedBook&) override; + void onBookUpdate(onixs::Handler&, const onixs::Security&, const onixs::ImpliedBook&) override; + // void onBookUpdate(onixs::Handler&, const onixs::Security&, const onixs::ConsolidatedBook&) override; - template - void onBookUpdate(const onixs::Security& sec, const TBook& book); + template + void onBookUpdate(const onixs::Security& sec, const TBook& book, BookType booktype); // // Trades @@ -105,8 +111,18 @@ public: // Misc template void onSettlementPrice(const onixs::Security& sec, const StatisticsArgs& args); - void onSettlementPrice(onixs::Handler&, const onixs::Security& s, const onixs::DailyStatistics49Args& arg) override { onSettlementPrice(s, arg); } - void onSettlementPrice(onixs::Handler&, const onixs::Security& s, const onixs::DailyStatistics33Args& arg) override { onSettlementPrice(s, arg); } + void onSettlementPrice(onixs::Handler&, const onixs::Security& s, const onixs::DailyStatistics49Args& arg) override { + auto type = arg.entry().settlPriceType(); + if (type.finalDaily() && type.actual()) { + onSettlementPrice(s, arg); + } + } + void onSettlementPrice(onixs::Handler&, const onixs::Security& s, const onixs::DailyStatistics33Args& arg) override { + auto type = arg.entry().settlPriceType(); + if (type.finalDaily() && type.actual()) { + onSettlementPrice(s, arg); + } + } void onSettlementPrice(onixs::Handler&, const onixs::Security& s, const onixs::Recovery52EntryArgs& arg) override { onSettlementPrice(s, arg); } void onSettlementPrice(onixs::Handler&, const onixs::Security& s, const onixs::Recovery38EntryArgs& arg) override { onSettlementPrice(s, arg); } @@ -174,17 +190,16 @@ private: std::vector _symbol_filter_storage; std::set _symbol_filter; std::vector _channels; - CMEOptions::BookType _booktype; std::atomic _concurrency_monitor; struct BookData { BookData(MDProducerHandle& handle); MDProducerHandle handle; - std::array bids; - std::array asks; + BookType type_flags; + std::pair, std::array> bids; + std::pair, std::array> asks; }; std::vector _book_updates; - }; ////////////////////////////////////////// @@ -334,9 +349,8 @@ void MDP3Listener::onSpread(onixs::Handler&, const onixs::Security&, const Instr _index.add_spread_legs(fsec.numeric_id, leg_exchange_ids); } - -template -void MDP3Listener::onBookUpdate(const onixs::Security& sec, const TBook& book) { +template +void MDP3Listener::onBookUpdate(const onixs::Security& sec, const TBook& onixs_book, BookType booktype) { tracepoint (bts_trace,trace_order,(char*) "onBookUpdate", _latest_sequence, (uint16_t) _latest_sequence, 0); auto data = _index.get_security(sec.id()); @@ -354,26 +368,31 @@ void MDP3Listener::onBookUpdate(const onixs::Security& sec, const TBook& book) { _book_updates.emplace_back(data->handle); } BookData& cache = found ? *find_iter : _book_updates.back(); + cache.type_flags |= booktype; + auto& cache_bids = std::get>(cache.bids); + auto& cache_asks = std::get>(cache.asks); // Overwrite all entries every time - size_t bids_size = book.bids().size(); - size_t asks_size = book.offers().size(); + size_t bids_size = onixs_book.bids().size(); + size_t asks_size = onixs_book.offers().size(); if (!data->handle.has_depth()) { bids_size = std::min(bids_size, size_t { 1 }); asks_size = std::min(asks_size, size_t { 1 }); } for (size_t x = 0; x < bids_size; ++x) { - cache.bids[x] = pricesize_of_price_size(book.bids()[x].price(), display_factor, book.bids()[x].quantity()); + cache_bids[x] = pricesize_of_price_size(onixs_book.bids()[x].price(), display_factor, onixs_book.bids()[x].quantity()); } for (size_t x = 0; x < asks_size; ++x) { - cache.asks[x] = pricesize_of_price_size(book.offers()[x].price(), display_factor, book.offers()[x].quantity()); + cache_asks[x] = pricesize_of_price_size(onixs_book.offers()[x].price(), display_factor, onixs_book.offers()[x].quantity()); } + if (!found) { // NaN out the remaining depth - for (size_t x = bids_size; x < COCONUT_MAX_DEPTH; ++x) { - cache.bids[x] = PriceSize::NO_MARKET; - } - for (size_t x = asks_size; x < COCONUT_MAX_DEPTH; ++x) { - cache.asks[x] = PriceSize::NO_MARKET; + for (size_t x = bids_size; x < MAX_DEPTH; ++x) { + cache_bids[x] = NO_MARKET; + } + for (size_t x = asks_size; x < MAX_DEPTH; ++x) { + cache_asks[x] = NO_MARKET; + } } } @@ -427,10 +446,11 @@ void MDP3Listener::onSettlementPrice(const onixs::Security& sec, const Statistic onixs::Timestamp time; args.entry().entryPx(price); args.entry().tradingReferenceDate(time); + // args.entry(). auto display_factor = data->handle.security().display_factor; data->handle.update_settlement( utils::TimeStamp::now(), - double_of_price(price, display_factor), + convert_price(price, display_factor), date_of_timestamp(time)); } } @@ -438,16 +458,19 @@ void MDP3Listener::onSettlementPrice(const onixs::Security& sec, const Statistic template void MDP3Listener::onSessionOpeningPrice(const onixs::Security& sec, const SessionArgs& args) { tracepoint (bts_trace,trace_order,(char*) "onOpeningPrice.session", _latest_sequence, (uint16_t) _latest_sequence, 0); - auto* data = _index.get_security(sec.id()); - if (data != nullptr) { - onixs::Decimal price = args.entry().entryPx(); - onixs::Timestamp time = args.message().transactTime(); - auto display_factor = data->handle.security().display_factor; - data->handle.update_opening( - utils::TimeStamp::now(), - double_of_price(price, display_factor), - date_of_timestamp(time)); - } + // auto type = args.entry().settlPriceType(); + // if (type.final() && type.actual()) { + auto* data = _index.get_security(sec.id()); + if (data != nullptr) { + onixs::Decimal price = args.entry().entryPx(); + onixs::Timestamp time = args.message().transactTime(); + auto display_factor = data->handle.security().display_factor; + data->handle.update_opening( + utils::TimeStamp::now(), + double_of_price(price, display_factor), + date_of_timestamp(time)); + } + // } template void MDP3Listener::onRecoveryOpeningPrice(const onixs::Security& sec, const RecoveryArgs& args) { diff --git a/src/server/sparkd/md/platforms/mdp3_onixs/MDP3ListenerHelpers.h b/src/server/sparkd/md/platforms/mdp3_onixs/MDP3ListenerHelpers.h index 4c3972592..5d323c5db 100644 --- a/src/server/sparkd/md/platforms/mdp3_onixs/MDP3ListenerHelpers.h +++ b/src/server/sparkd/md/platforms/mdp3_onixs/MDP3ListenerHelpers.h @@ -12,16 +12,22 @@ namespace bts { namespace md { inline PriceSize pricesize_of_price_size(const onixs::Decimal price, int8_t display_factor, Size size) { - PriceSize result(price.mantissa(), price.exponent() + display_factor, size); + PriceSize result({price.mantissa(), (int8_t)(price.exponent() + display_factor)}, size); return result; } -template -double double_of_price(const Price& price, int8_t display_factor) { +template +double double_of_price(const OnixPrice& price, int8_t display_factor) { // order of multiplication is important because of floating point error return (price.mantissa() * pow10(price.exponent())) * pow10(display_factor); } +template +IntPrice convert_price(const OnixsPrice& price, int8_t display_factor) { + return IntPrice(price.mantissa(), price.exponent() + display_factor); +} + + inline int floorlog10(int64_t m) { bts_assert(m >= 0); if (m < 10) return 0; diff --git a/src/server/sparkd/md/platforms/mdp3_onixs/MDP3Main.cpp b/src/server/sparkd/md/platforms/mdp3_onixs/MDP3Main.cpp index 115a23e64..a0e4bb3d5 100644 --- a/src/server/sparkd/md/platforms/mdp3_onixs/MDP3Main.cpp +++ b/src/server/sparkd/md/platforms/mdp3_onixs/MDP3Main.cpp @@ -28,7 +28,7 @@ namespace md { MDP3Main::MDP3Main() : _producer(PLATFORM_MDP3ONIXS), _security_recorder(), - _settlement_recorder(PLATFORM_MDP3ONIXS), + // _settlement_recorder(PLATFORM_MDP3ONIXS), _engines(), _handlers(), _listeners(), @@ -64,7 +64,7 @@ const onixs::FeedEngineSettings feed_engine_settings_from_config(const CMEOption result.socketBufferSize(settings.udp_socket_buffer_size()); - auto affinity = threading::get_affinity("mdp3feed"); + auto affinity = threading::get_affinity(settings.name()); for (auto& core : affinity) { result.threadAffinity().cpus().insert(core); } @@ -101,14 +101,6 @@ map create_feed_engine_settings(const std::vec } } - // If a channel asks for defaults ensure it exists. - if (settings.count("default") == 0) { - size_t channels_using_default = count_engine_uses(channels, "default"); - if (channels_using_default > 0) { - settings["default"] = feed_engine_settings_from_config(CMEOptions::FeedEngineSettings(), channels_using_default); - } - } - return settings; } @@ -171,10 +163,10 @@ void MDP3Main::run(std::vector& symbols) { }); spread_resolve_timer.start(); - utils::DispatcherTimer settlement_timer(disp, 100, [&]() { - _settlement_recorder.do_work(); - }); - settlement_timer.start(); + // utils::DispatcherTimer settlement_timer(disp, 100, [&]() { + // _settlement_recorder.do_work(); + // }); + // settlement_timer.start(); utils::DispatcherTimer heartbeat_timer(disp, 250, [&]() { for (const auto& listener: _listeners) { @@ -223,12 +215,9 @@ unique_ptr MDP3Main::prepare_handler(const string& channel_id) { settings.feeds().connectivityConfigurationFile(config_path()); // Market data options - if (options.book_type() == CMEOptions::BOOKTYPE_CONSOLIDATED) { - settings.bookManagement().consolidatedBooks().maintain(true); - } - else { - settings.bookManagement().directBooks().maintain(true); - } + settings.bookManagement().directBooks().maintain(true); + settings.bookManagement().impliedBooks().maintain(true); + settings.bookManagement().consolidatedBooks().maintain(false); // 10k default is *far* too low settings.recoveryQueueMaxSize(options.recovery_queue_size()); @@ -357,7 +346,7 @@ unique_ptr MDP3Main::prepare_listener(onixs::Handler& handler, con const auto& options = channel_settings_for(channel.id); - unique_ptr listener(new MDP3Listener(_index, _producer, _security_recorder, _settlement_recorder, producer_channels, options.book_type())); + unique_ptr listener(new MDP3Listener(_index, _producer, _security_recorder, /*_settlement_recorder,*/ producer_channels)); for (auto& symbol : channel.symbols) { listener->add_filter_symbol(symbol.symbol()); } diff --git a/src/server/sparkd/md/platforms/mdp3_onixs/MDP3Main.h b/src/server/sparkd/md/platforms/mdp3_onixs/MDP3Main.h index e6059c7fd..1ebfdb99b 100644 --- a/src/server/sparkd/md/platforms/mdp3_onixs/MDP3Main.h +++ b/src/server/sparkd/md/platforms/mdp3_onixs/MDP3Main.h @@ -40,7 +40,7 @@ private: CMESecurityIndex _index; MDProducer _producer; SecurityIDRecorder _security_recorder; - co2::SettlementRecorder _settlement_recorder; + // co2::SettlementRecorder _settlement_recorder; std::map> _engines; std::map> _handlers; std::vector> _listeners; diff --git a/src/server/sparkd/md/run_md.cpp b/src/server/sparkd/md/run_md.cpp index f048fb8c4..8a1ce07ca 100644 --- a/src/server/sparkd/md/run_md.cpp +++ b/src/server/sparkd/md/run_md.cpp @@ -10,6 +10,8 @@ #include "lib/md/Diag/ShmDumper.h" #include "platforms/mdp3_onixs/MDP3Main.h" #include "lib/md/SecurityDB.h" +#include "server/sparkd/exchange_sim/ExchangeSim.h" +#include "lib/utils/thread_manager.h" #include #include @@ -74,7 +76,6 @@ void run_cleanup() { std::thread* _md_thread; void run_market_data_publishers_forever() { os::set_thread_name("md publisher"); - run_cleanup(); if (config::get().use_replay()) { run_replay(); } @@ -85,13 +86,18 @@ void run_market_data_publishers_forever() { void run_market_datapublishers() { bts_assert(_md_thread == nullptr); - if (!config::get().use_md()) return; run_cleanup(); + if (!config::get().use_md()) return; // start a thread for the md _md_thread = new std::thread([]() { run_market_data_publishers_forever(); }); + if (config::get().use_sim()) { + threading::make_thread("ExchangeSim", []() { sim::start_exchange_sim(); }) + .detach(); + } + std::this_thread::sleep_for(2s); } diff --git a/src/server/sparkd/user/AdminServiceServer.cpp b/src/server/sparkd/user/AdminServiceServer.cpp index a04d6f034..ff1505cb9 100644 --- a/src/server/sparkd/user/AdminServiceServer.cpp +++ b/src/server/sparkd/user/AdminServiceServer.cpp @@ -6,6 +6,7 @@ #include "lib/utils/PBUtils.h" #include "lib/api/service_helpers/AdminServiceUtils.h" #include "lib/orderman/orderman_utils/orderman_utils.h" +#include "lib/utils/TimeStamp.h" #include #include @@ -135,6 +136,7 @@ void AdminServiceServer::delete_account( } void AdminServiceServer::_update_and_publish(bool is_insert){ std::string ser_state; + auto start = utils::TimeStamp::now(); if (_state.SerializeToString(&ser_state)) { LOG(Gen, Info, "Saving admin data"); @@ -145,7 +147,9 @@ void AdminServiceServer::_update_and_publish(bool is_insert){ _publisher.add_or_update(_state); _publisher.flush(); _publish_md_entitlements(); - _publish_md_permissions(); + // _publish_md_permissions(); + auto elapsed = (utils::TimeStamp::now() - start).milliseconds(); + LOG(Gen, Info, "Saved admin data ({}ms)", elapsed); } else LOG(Gen, Error, "Could not serialize admin data, saving failed"); diff --git a/src/web/CMakeDeps.cmake b/src/web/CMakeDeps.cmake index f2da73652..21fe04d3f 100644 --- a/src/web/CMakeDeps.cmake +++ b/src/web/CMakeDeps.cmake @@ -1 +1 @@ -set(sources ../../externals/linux/include/google/protobuf/descriptor.proto;../../externals/linux/include/google/protobuf/timestamp.proto;../lib/api/apicore.proto;../lib/api/apigateway.proto;../lib/api/apioptions.proto;../lib/api/services/AdminService.proto;../lib/api/services/ApiUnitTestService.proto;../lib/api/services/ErrorReportService.proto;../lib/api/services/HelloService.proto;../lib/api/services/MarketDataAuxiliaryService.proto;../lib/api/services/MarketDataRelay.proto;../lib/api/services/OrderService.proto;../lib/api/services/OutOfProcessURLListener.proto;../lib/api/services/PersistenceService.proto;../lib/api/services/PositionService.proto;../lib/api/services/ShellDiagnosticService.proto;../lib/api/services/ShellLatencyService.proto;../lib/api/services/SyntheticSecurityService.proto;../lib/api/services/UserService.proto;../lib/domain_model/Security.proto;../lib/md/protos/co2.proto;../lib/sparkproto/SparkProtos.proto;api/build.sh;api/quiet_build.sh;api/src/DynamicService.ts;api/src/ServiceContext.ts;api/src/bts_assert.ts;ui/src/Access.fs;ui/src/Accounts/ClipLimits.fs;ui/src/Accounts/ProductLimits.fs;ui/src/Accounts/SecurityLimits.fs;ui/src/Accounts/State.fs;ui/src/Accounts/Types.fs;ui/src/Accounts/View.fs;ui/src/App.fs;ui/src/Cmd.fs;ui/src/Controls/Button.fs;ui/src/Controls/DataGrid.fs;ui/src/Controls/Fields.fs;ui/src/Controls/Modal.fs;ui/src/Controls/ReactGrid.fs;ui/src/Controls/Table.fs;ui/src/Controls/nbsp.fs;ui/src/Global.fs;ui/src/Imports.fs;ui/src/Lens.fs;ui/src/Login/Login.fs;ui/src/Nav.fs;ui/src/State.fs;ui/src/Types.fs;ui/src/Users/State.fs;ui/src/Users/Types.fs;ui/src/Users/View.fs;ui/src/Util/EnumUtils.fs;ui/src/Util/JSMap.fs) \ No newline at end of file +set(sources ../../externals/linux/include/google/protobuf/descriptor.proto;../../externals/linux/include/google/protobuf/timestamp.proto;../lib/api/apicore.proto;../lib/api/apigateway.proto;../lib/api/apioptions.proto;../lib/api/services/AdminService.proto;../lib/api/services/ApiUnitTestService.proto;../lib/api/services/ErrorReportService.proto;../lib/api/services/HelloService.proto;../lib/api/services/MarketDataRelay.proto;../lib/api/services/OrderService.proto;../lib/api/services/OutOfProcessURLListener.proto;../lib/api/services/PersistenceService.proto;../lib/api/services/PositionService.proto;../lib/api/services/ShellDiagnosticService.proto;../lib/api/services/ShellLatencyService.proto;../lib/api/services/SyntheticSecurityService.proto;../lib/api/services/UserService.proto;../lib/domain_model/Security.proto;../lib/md/protos/co2.proto;../lib/sparkproto/SparkProtos.proto;api/build.sh;api/node_modules/cssstyle/scripts/run_tests.sh;api/node_modules/exit/test/fixtures/create-files.sh;api/node_modules/protobufjs/cli/node_modules/acorn-jsx/node_modules/acorn/bin/update_authors.sh;api/node_modules/watch/release.sh;api/quiet_build.sh;api/src/DynamicService.ts;api/src/ServiceContext.ts;api/src/bts_assert.ts;ui/src/Access.fs;ui/src/Accounts/ClipLimits.fs;ui/src/Accounts/ProductLimits.fs;ui/src/Accounts/SecurityLimits.fs;ui/src/Accounts/State.fs;ui/src/Accounts/Types.fs;ui/src/Accounts/View.fs;ui/src/App.fs;ui/src/Cmd.fs;ui/src/Controls/Button.fs;ui/src/Controls/DataGrid.fs;ui/src/Controls/Fields.fs;ui/src/Controls/Modal.fs;ui/src/Controls/ReactGrid.fs;ui/src/Controls/Table.fs;ui/src/Controls/nbsp.fs;ui/src/Global.fs;ui/src/Imports.fs;ui/src/Lens.fs;ui/src/Login/Login.fs;ui/src/Nav.fs;ui/src/State.fs;ui/src/Types.fs;ui/src/Users/State.fs;ui/src/Users/Types.fs;ui/src/Users/View.fs;ui/src/Util/EnumUtils.fs;ui/src/Util/JSMap.fs) \ No newline at end of file diff --git a/testing/manual/admin/user_account_permissions.txt b/testing/manual/admin/user_account_permissions.txt new file mode 100644 index 000000000..9b93c1345 --- /dev/null +++ b/testing/manual/admin/user_account_permissions.txt @@ -0,0 +1,18 @@ +This test verifies that users can only see trades that are in accounts for which they're permissioned + +1. Start a client and the admin gui, choose a user other than bts + +2. For your user, add permissions to see all accounts in the system in the admin gui + +3. Enter some orders in the client for those accounts (you may have to add limits, etc), make some orders fill, cancel some orders + +4. Now close the client, and in the admin UI pick another user (not bts) and remove trading access to the account you just traded into. Start the client again with the user you just de-permissioned. You should not be able to see any of the trades you just made + +5. Next re-permission your current user for the accounts with trades in them and the should show up in the UI. + +6. Start another client with -multiple with the original user and make sure you can still see all trades. + +7. Close that -multiple client and restart it with user bts. Make sure user bts can see all trades regardless of trading or admin permission status. + + + diff --git a/testing/manual/forms/implied_markets_ui.txt b/testing/manual/forms/implied_markets_ui.txt new file mode 100644 index 000000000..8970b116b --- /dev/null +++ b/testing/manual/forms/implied_markets_ui.txt @@ -0,0 +1,23 @@ +0. This test should run in sim market with merged turned on + +ExchangeSim {value {merge_with_md:true}} + +1. Bring up a market view for SI futures. There are 3 quantity columns +a. Implied : This is the quantity coming purely from implieds (ex for the spreads, this is +'implied' from the legs) +b. Direct : This comes purely from markets in the security itself +c. Qty (or Merged Qty) == Implied + Direct. + +2. Select a future with only implied markets (SI should have lots of them). Verify that the ladder +agrees with the market view (the ladder shows merged by default). + + +3. Enter buy and sell orders away from the market and make sure they show up as direct market in the ladder. + +4. In the ladder settings -> Market Data, select Direct Only. Make sure the ladder does the right thing. + +5. Go back to Merged and make sure the Ladder does the right thing. + +6. Make you direct bid better than the implied bid (by moving your buy order) and make sure MView does the right thing. + +7. Repeat 6 for sell/ask. \ No newline at end of file diff --git a/testing/manual/spreader_misc/implied_vs_direct.txt b/testing/manual/spreader_misc/implied_vs_direct.txt new file mode 100644 index 000000000..6acf12cb2 --- /dev/null +++ b/testing/manual/spreader_misc/implied_vs_direct.txt @@ -0,0 +1,29 @@ +This test makes sure the basic spreader functionality works for 2 legged synthetic spreads with one quoting leg, +such that he hedging leg can use either direct or merged market data. + +0. This test assumes recorded market data with the following sim md configuration + +ExchangeSim { + value { + merge_with_md: true + forward_implied: true + } +} + +1. Open up the mview and find a security with with both direct and implied markets, on the ask side. +Make sure those markets are stable and you can rely on them. + +2. Create a synthetic with the security in 1. being the hedge leg (buy) and choose a quoting +leg (sell) of your choice. Make sure there is only one quoting leg. For market data type +use "Merged" + +3. Disable all spreader params. + +4. Now enter a spreader order so that it doesnt get filled immediatly. Note the quoting order. +quoting leg price = hedge_price - spreader_price. +hedge_price is where it can buy the hedge order (which since its a buy will be on the ask side of +the hedging market). +Make sure thta its correct and its using the merged book in the hedging market. + +5. Cancel the order and change the spreader so its using Direct prices. +Repeat 4, and make sure its using direct price on the hedging market. \ No newline at end of file diff --git a/tools/runsparkd.cmd b/tools/runsparkd.cmd index 9a3168b01..00fedf7f3 100644 --- a/tools/runsparkd.cmd +++ b/tools/runsparkd.cmd @@ -1,2 +1,3 @@ @if "%SPARKVM_PORT%"=="" set SPARKVM_PORT=22 -@ssh bts@sparkvm -p %SPARKVM_PORT% "cd /home/bts/spark/build/ninja_debug/server/ && ./sparkd %*" +@if "%SPARKVM_RUNMODE%"=="" set SPARKVM_RUNMODE=debug +@ssh bts@sparkvm -p %SPARKVM_PORT% "cd /home/bts/spark/build/ninja_%SPARKVM_RUNMODE%/server/ && ./sparkd %*" diff --git a/tools/runsparktest.cmd b/tools/runsparktest.cmd new file mode 100644 index 000000000..903d77d38 --- /dev/null +++ b/tools/runsparktest.cmd @@ -0,0 +1,4 @@ +@echo off +pushd %~dp0\..\src\client\sparkcmd +dotnet watch run test %1 -t +popd diff --git a/tools/spark_vm_shell.sh b/tools/spark_vm_shell.sh index 4d3de68bf..b2bd64634 100755 --- a/tools/spark_vm_shell.sh +++ b/tools/spark_vm_shell.sh @@ -20,6 +20,8 @@ docker run \ -u $UID \ -v /home:/home \ -e DISPLAY=$DISPLAY \ + --name bts \ + -d \ -t bluetradingsystems/linux_docker \ /bin/bash -c "(sudo /usr/sbin/sshd) & (cd ~ && $DIR/../externals/linux/bin/unison -socket 8080) & /bin/bash --login" diff --git a/tools/tool.fsx b/tools/tool.fsx new file mode 100644 index 000000000..781fc03c2 --- /dev/null +++ b/tools/tool.fsx @@ -0,0 +1,45 @@ +#r "paket: +nuget Fake.IO.FileSystem +nuget Fake.Core.Target" +#load "./.fake/tool.fsx/intellisense.fsx" + +open Fake.Core +open Fake.IO +open Fake.IO.Globbing.Operators +open Fake + +open System.Diagnostics + +let replicate _ = + // unison.exe doesn't work without very specific parameters + printfn "Detected changes..." + let psi = ProcessStartInfo() + psi.FileName <- "python" + psi.Arguments <- __SOURCE_DIRECTORY__ + "/replicator/replicate_to_linux_vm.py" + psi.UseShellExecute <- false + psi.CreateNoWindow <- true + let p = Process.Start(psi) + p.WaitForExit() + printfn "Ok" + +let build _ = + replicate () + Process.directExec (fun config -> + { config with + FileName = "ssh" + Arguments = "bts@sparkvm -o StrictHostKeyChecking=no -t \"cd /home/bts/spark/build/ninja_debug/ && touch ../../CMakeLists.txt && time ninja\" " + }) |> ignore + + +Target.create "AutoBuild" (fun _ -> + use watcher = !!"src/**/*.cpp" ++ "src/**/*.h" ++ "src/**/*.proto" ++ "tools/*.sh" |> ChangeWatcher.run build + System.Console.ReadLine() |> ignore +) + +Target.create "Replicate" (fun _ -> + use watcher = !!"src/**/*.cpp" ++ "src/**/*.h" ++ "src/**/*.proto" |> ChangeWatcher.run replicate + System.Console.ReadLine() |> ignore +) + +Target.runOrDefault "Replicate" + diff --git a/tools/watch_tests.sh b/tools/watch_tests.sh index 29a41ffd6..6a5b9a871 100755 --- a/tools/watch_tests.sh +++ b/tools/watch_tests.sh @@ -5,6 +5,7 @@ if [ "$1" = "-b" ] cd $DIR/../build/ninja_debug/ ninja cd server + echo "Starting tests..." ./sparkd test $2 else cd $DIR/../src