diff --git a/.gitignore b/.gitignore index 7c976336d..d7ea25bb8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,34 +1,35 @@ -nohup.out -build/ -cmake-build-debug/ -.idea/ -.dir-locals.el -*.pyc -.vscode/* -!.vscode/*.json -.vs -obj/ -*.csproj.user -*.vspx -*.psess -msbuild.log -*.orig -vcxproj.user -src/client/spark/Properties/launchSettings.json -compile_commands.json -.cquery_cached_index/ -src/client/sparkcmd/Properties/launchSettings.json -node_modules -paket-files -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 -*.code-workspace -src/lib/orderman/perf_analysis/logs/latency_data/ -trace_data/ -/src/lib/orderman/perf_analysis/last_pretty_log.csv -/src/lib/orderman/perf_analysis/log.csv -/doc/user/public/* +nohup.out +build/ +cmake-build-debug/ +.idea/ +.dir-locals.el +*.pyc +.vscode/* +!.vscode/*.json +.vs +obj/ +*.csproj.user +*.vspx +*.psess +msbuild.log +*.orig +vcxproj.user +src/client/spark/Properties/launchSettings.json +compile_commands.json +.cquery_cached_index/ +src/client/sparkcmd/Properties/launchSettings.json +node_modules +paket-files +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 +*.code-workspace +src/lib/orderman/perf_analysis/logs/latency_data/ +trace_data/ +/src/lib/orderman/perf_analysis/last_pretty_log.csv +/src/lib/orderman/perf_analysis/log.csv +/doc/user/public/* +.ionide/* diff --git a/.vscode/launch.json b/.vscode/launch.json index fac8a7668..52aa628ff 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -249,7 +249,7 @@ "program": "/home/bts/spark/build/ninja_debug/server/sparkd", "args": [ "test", - "SingleWriterConcurrentVector" + "[algo]" ], "stopAtEntry": false, "cwd": "/home/bts/spark/build/ninja_debug/server/", diff --git a/doc/user/content/bts_spark/Glossary/index.md b/doc/user/content/bts_spark/Glossary/_index.md similarity index 100% rename from doc/user/content/bts_spark/Glossary/index.md rename to doc/user/content/bts_spark/Glossary/_index.md diff --git a/doc/user/content/bts_spark/Help/index.md b/doc/user/content/bts_spark/Help/_index.md similarity index 100% rename from doc/user/content/bts_spark/Help/index.md rename to doc/user/content/bts_spark/Help/_index.md diff --git a/doc/user/content/bts_spark/Launcher/index.md b/doc/user/content/bts_spark/Launcher/_index.md similarity index 100% rename from doc/user/content/bts_spark/Launcher/index.md rename to doc/user/content/bts_spark/Launcher/_index.md diff --git a/doc/user/content/bts_spark/Logging-In-to-the-System/index.md b/doc/user/content/bts_spark/Logging-In-to-the-System/_index.md similarity index 100% rename from doc/user/content/bts_spark/Logging-In-to-the-System/index.md rename to doc/user/content/bts_spark/Logging-In-to-the-System/_index.md diff --git a/doc/user/content/bts_spark/Market/index.md b/doc/user/content/bts_spark/Market/_index.md similarity index 100% rename from doc/user/content/bts_spark/Market/index.md rename to doc/user/content/bts_spark/Market/_index.md diff --git a/doc/user/content/bts_spark/My-Windows/index.md b/doc/user/content/bts_spark/My-Windows/_index.md similarity index 100% rename from doc/user/content/bts_spark/My-Windows/index.md rename to doc/user/content/bts_spark/My-Windows/_index.md diff --git a/doc/user/content/bts_spark/Settings/index.md b/doc/user/content/bts_spark/Settings/_index.md similarity index 100% rename from doc/user/content/bts_spark/Settings/index.md rename to doc/user/content/bts_spark/Settings/_index.md diff --git a/doc/user/content/bts_spark/Spreader/index.md b/doc/user/content/bts_spark/Spreader/_index.md similarity index 100% rename from doc/user/content/bts_spark/Spreader/index.md rename to doc/user/content/bts_spark/Spreader/_index.md diff --git a/doc/user/content/bts_spark/Algos/_index.md b/doc/user/content/bts_spark/Trading/OrderTypes/index.md similarity index 67% rename from doc/user/content/bts_spark/Algos/_index.md rename to doc/user/content/bts_spark/Trading/OrderTypes/index.md index f3955e979..1b423a99f 100644 --- a/doc/user/content/bts_spark/Algos/_index.md +++ b/doc/user/content/bts_spark/Trading/OrderTypes/index.md @@ -1,11 +1,11 @@ +++ -title = "BTS Basic Algos" -weight = 10 +title = "Basic Algos" +weight = 1 sort_by = "weight" insert_anchor_links = "right" +aliases = ["./bts_spark/Algos/"] +++ - ## Lean The goal of the lean order is to watch the opposite-inside market size and cross when that size goes below the threshold (this also implies a cross if that size goes to zero and the market trades away from you) If the market trades toward you, you can opt into several different behaviors @@ -43,4 +43,13 @@ An OCO order creates a relationship between a child order and a 'stop' or 'other Composing this order with Cover simulates scalping functionality ##### Parameters - * Stop Ticks - ticks below or above main order to place stop / other order \ No newline at end of file + * Stop Ticks - ticks below or above main order to place stop / other order + +## Splitter +The splitter is useful for pro-rata markets where you want to vary your exposure independent of the order quantity that you want to fill. The splitter will complete when the number of fills reaches or exceeds the order quantity but it will put exposed_qty total size on the market. Splitter favors order size over queue position. + +If exposed_qty is set to 0 the splitter will continue to exist in the Order Book but will cancel all of its child orders. This makes it a useful building block for custom algos that need to implement a 'paused' state. + +##### Parameters + * exposed_qty - The total quantity to expose on the market. + * max_order_qty - The maximum size of a single order. diff --git a/doc/user/content/bts_spark/Trading/index.md b/doc/user/content/bts_spark/Trading/_index.md similarity index 100% rename from doc/user/content/bts_spark/Trading/index.md rename to doc/user/content/bts_spark/Trading/_index.md diff --git a/doc/user/content/release_notes/_index.md b/doc/user/content/release_notes/_index.md index f9a04d11e..8b5adec5b 100644 --- a/doc/user/content/release_notes/_index.md +++ b/doc/user/content/release_notes/_index.md @@ -4,6 +4,67 @@ weight = 3 sort_by = "weight" insert_anchor_links = "right" +++ +## 2.7.2 +### User Visible Changes + - Changed the login dialog to avoid leaking workspace names from other users. + - Added new Splitter algo. From the manual: The splitter is useful + for pro-rata markets where you want to vary your exposure + independent of the order quantity that you want to fill. The + splitter will complete when the number of fills reaches or exceeds + the order quantity but it will put exposed_qty total size on the + market. Splitter favors order size over queue position. +### Internal Changes + - Made spark more resilient to lack of connectivity to the CME web site for downloading of templates. + +## 2.7.1 +### User Visible Changes + - Fixed some issues with the Cover algo, where in some circumstances + it would erroneously cancel launched hedges on completion. + +## 2.7.0 +### User Visible Changes + - Allowed algos to process "in-flight" fills while shutting down + - Changed the cover algo to continue to hedge while shutting down +### API Changes + - Changed the API so that algos do not fully terminate until all + their children are terminated. Algos that are in the process of + shutting down in response to a complete or cancel call will still + receive child updates, which they can use to calculate and report + an accurate fill count (during this shutdown stage algos cannot + create, cancel or update children). A new IAlgoServices method, + is_shutting_down(), can be used to determine if an algo is + shutting down. + +## 2.6.2 +### User Visible Changes + - Fixed an issue in the ladder where in some cases the mini order book could not be brought up from a badge + - Fixed a rare race condition that could lead to erroneous limit enforcement + - Fixed an issue with the margin limits where an error could occur if an account name with more than 12 chars was used + +## 2.6.1 +### User Visible Changes + - Fixed an issue in the ladder where an error could occur when trying to change ladder settings for the first time + +## 2.6.0 +### User Visible Changes + - In the ladder made last trade more distinctive when overlayed on the volumn column + - In the ladder, corrected display of volume tool tip + - In the ladder, changed the consolidation of last trade to consider only the price ignoring aggressor changes + - Added an option for the Top Algo to delete itself if within N ticks from top of book + - Address a rare issue that could lead to an error report on UI exit + - Added margin risk limits + - Added the "Offset" algo. Offset prices a child algo at a parameterizable distance from the top of the book + - Avoided switching the current account and order type on open ladders when limits are changed +### Internal Changes + - Ensure that admin logins are always allowed to login, regardless of keycode status. + +## 2.5.0 +### User Visible Changes + - Added loss limit risk check + - Made algos, synthetics and user names private so that multiple users can share a single system privately. + - Allowed the mini order book defaults for column widths to be changed from the mini order book + - Allowed the mini order book to shrink to the minimum size that will be needed to display the selected columns + - Improved accuracy of the directional arrow when displaying last trades in the volumn column ## 2.4.0 ### User Visible Settings - In the ladder, changed the last trade column to accumulate successive trades at the same price @@ -163,7 +224,7 @@ insert_anchor_links = "right" - Integrated stunnel into UI to make VPN setup unnecessary in unsecured environments. ## 1.5.7 ### User Visible Changes - - New videos have been added to the manual describing the [Algo Studio](./bts_spark/Trading/index.md#algo-studio) and [Spreader](./bts_spark/Spreader/index.md) + - New videos have been added to the manual describing the [Algo Studio](./bts_spark/Trading/_index.md#algo-studio) and [Spreader](./bts_spark/Spreader/_index.md) - Fixed a bug that occasionally would cause windows to be position outside the visible screen on startup - Fixed a bug in the order book form that would occasionally cause the Recreate Order option to be unavailable - Added a confirmation step to Cancel All, Cancel Buys and Cancel Sells commands @@ -286,7 +347,7 @@ insert_anchor_links = "right" - The zoom slider can be enabled from Ladder Form -> Settings button -> Left Pane tab -> check Zoom Slider checkbox - The zoom functionality can also be performed via hotkey and mouse wheel combinations - Zoom In = CTRL + Mouse wheel up - - Zoom Out = CTRL + Mouse wheel down + - Zoom Out = CTRL + Mouse wheel down - Reset zoom = CTRL + Mouse wheel click or double click on the zoom slider ### Exchange simulator - Improved the exchange simulator to track real market data more closely @@ -391,7 +452,8 @@ insert_anchor_links = "right" - This is configurable from a ladder's settings as well ### Customized settings in all windows - All windows now contain a settings page where grid settings (font color, size, etc) are applied - - These settings can be saved on a per-window basis or as a Default for newly-launched windows + - These settings can be saved on a per-window basis or as a Default for newly-launched windows + ## 1.3.1 Through 1.3.4 diff --git a/src/client/spark/Admin/AdminModel.cs b/src/client/spark/Admin/AdminModel.cs index af7cf9499..9e8508b22 100644 --- a/src/client/spark/Admin/AdminModel.cs +++ b/src/client/spark/Admin/AdminModel.cs @@ -7,12 +7,12 @@ using System.Linq; using System.Reactive.Linq; using System.Reactive.Subjects; using bts.api.core; +using mbts.ui.shell; namespace mbts.spark { [Export] class AdminModel : IDisposable { - readonly string _user_name; - public string user_name => _user_name; + public readonly UserTuple user; readonly BehaviorSubject> _accounts_subject = new BehaviorSubject>(new Dictionary()), @@ -24,14 +24,14 @@ namespace mbts.spark { IDisposable _dispose; [ImportingConstructor] - AdminModel(AdminService service, [Import("username")]string user_name) { - _user_name = user_name; + AdminModel(AdminService service, [Import]UserTuple user) { + this.user = user; _dispose = service.subscribei_admin_data().Select(update => { - _wrapper_subject.OnNext(new AdminModelWrapper(update.all.TryFirst(), _user_name)); + _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 - where bts_user.Name == user_name && bts_user.HasUserData && bts_user.UserData.HasAccountPermissions + where bts_user.Name == user.name && bts_user.HasUserData && bts_user.UserData.HasAccountPermissions select bts_user; IEnumerable active_accounts = @@ -62,14 +62,6 @@ namespace mbts.spark { 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) { - // just for good measure, there should never be > 1 element in _user_map, and it should always match _user_name - if (kv.Value == _user_name) - return new UserTuple(kv.Key, kv.Value); - } - return null; - } public AccountTuple? get_default_account() { foreach (var kv in _account_map) { return new AccountTuple(kv.Key, kv.Value); diff --git a/src/client/spark/Daemons/AboutDaemon.cs b/src/client/spark/Daemons/AboutDaemon.cs index 8abe6cc66..8a8ad2f06 100644 --- a/src/client/spark/Daemons/AboutDaemon.cs +++ b/src/client/spark/Daemons/AboutDaemon.cs @@ -3,6 +3,7 @@ using mbts; using mbts.config; using mbts.spark; using mbts.ui; +using mbts.ui.shell; using System; using System.Collections.Generic; using System.ComponentModel.Composition; @@ -22,7 +23,7 @@ namespace bts.ui { IDisposable _dispose; [ImportingConstructor] - public AboutDaemon(IBus bus, [Import("username")]string username) { + public AboutDaemon(IBus bus, [Import(LoginContainer.Exports.UserName)]string username) { _user = username; _bus = bus; _dispose = bus.subscribe(SparkTags.About).OnDispatcher().Subscribe(_ => show_about()); diff --git a/src/client/spark/Daemons/ErrorReporter.cs b/src/client/spark/Daemons/ErrorReporter.cs index d19f7ef04..fa8ccc6ad 100644 --- a/src/client/spark/Daemons/ErrorReporter.cs +++ b/src/client/spark/Daemons/ErrorReporter.cs @@ -10,6 +10,7 @@ using System.Reactive.Linq; using System.Windows.Threading; using System.Windows; using System.Threading.Tasks; +using mbts.ui.shell; namespace bts.spark { @@ -20,7 +21,7 @@ namespace bts.spark IDisposable _dispose; [ImportingConstructor] - public ErrorReporter(ErrorReportService svc, IBus bus, [Import("username")] string username) { + public ErrorReporter(ErrorReportService svc, IBus bus, [Import(LoginContainer.Exports.UserName)] string username) { _svc = svc; _username = username; _dispose = bus.subscribe(SparkTags.Support).OnDispatcher().Subscribe(msg => show_support_window(msg, "Report a problem")); @@ -82,7 +83,6 @@ namespace bts.spark } } - //public static async Task> public static async Task<(T value, bool success)> safe_execute(this ErrorReporter error_reporter, Func>> execute, Func get_success, Func get_error_msg, string desc) { try { var result = await execute(); @@ -101,5 +101,31 @@ namespace bts.spark void _report_error(string msg) => error_reporter.report_error($"Error {desc}: {msg}", AlgoStudioWindow.NAME, MessageBoxImage.Warning); } + + public static async Task safe_execute( + this ErrorReporter error_reporter, + Func>> execute, + Func get_success, + Func get_error_msg, + Func result_selector, + string desc) { + // + try { + var result = await execute(); + if (!result.success) { + _report_error(result.error_message); + } else if (!get_success(result.value)) { + _report_error(get_error_msg(result.value)); + } else { + return result_selector(result.value, true); + } + } catch (Exception ex) { + _report_error(ex.ToString()); + } + return result_selector(default, false); + // + void _report_error(string msg) => + error_reporter.report_error($"Error {desc}: {msg}", AlgoStudioWindow.NAME, MessageBoxImage.Warning); + } } } \ No newline at end of file diff --git a/src/client/spark/Daemons/MDDaemon.cs b/src/client/spark/Daemons/MDDaemon.cs index 70361557b..a2f866421 100644 --- a/src/client/spark/Daemons/MDDaemon.cs +++ b/src/client/spark/Daemons/MDDaemon.cs @@ -15,6 +15,7 @@ using System.Collections.Concurrent; using System.Threading; using bts.api.user; using bts.md; +using mbts.ui.shell; namespace bts { [Export(typeof(IInitializable))] @@ -27,8 +28,8 @@ namespace bts { static uint get_naggle() => mbts.config.Configuration.get().naggle_ms; [ImportingConstructor] - public MDDaemon(Lazy bus, Lazy token, Lazy ss) : - base(get_naggle(), MDSourceOptions.DeferInit) { + public MDDaemon(Lazy bus, Lazy token, Lazy ss, [Import(LoginContainer.Exports.UserID)]uint user_id) : + base(get_naggle(), MDSourceOptions.DeferInit, user_id) { _bus = bus; _token = token; _ss = ss; diff --git a/src/client/spark/Daemons/ShellLatencyDaemon.cs b/src/client/spark/Daemons/ShellLatencyDaemon.cs index 34e0f1065..0f4e10fd3 100644 --- a/src/client/spark/Daemons/ShellLatencyDaemon.cs +++ b/src/client/spark/Daemons/ShellLatencyDaemon.cs @@ -3,6 +3,7 @@ using bts.metrics; using mbts; using mbts.config; using mbts.ui; +using mbts.ui.shell; using System; using System.Collections.Generic; using System.ComponentModel.Composition; @@ -23,7 +24,7 @@ namespace bts.perf { Ping _pinger = new Ping(); [ImportingConstructor] - public ShellLatencyDaemon(ShellLatencyService service, [Import("username")] string user) { + public ShellLatencyDaemon(ShellLatencyService service, [Import(LoginContainer.Exports.UserName)] string user) { _service = service; _version = Configuration.get().version; _username = user; diff --git a/src/client/spark/Modules/Market/MarketShellModule.cs b/src/client/spark/Modules/Market/MarketShellModule.cs index ad95c0472..f2db00136 100644 --- a/src/client/spark/Modules/Market/MarketShellModule.cs +++ b/src/client/spark/Modules/Market/MarketShellModule.cs @@ -5,6 +5,7 @@ using mbts.co2; using mbts.persist; using mbts.spark; using mbts.ui; +using mbts.ui.shell; using System.Collections.Generic; using System.ComponentModel.Composition; using System.IO; @@ -18,7 +19,7 @@ namespace bts.shell class MarketShellModule : IShellModule { [ImportingConstructor] - public MarketShellModule(IBus bus, MDConsumer consumer, UserSettingsHub settings_hub, OrdersModel orders_model, [Import("username")]string user_name) { + public MarketShellModule(IBus bus, MDConsumer consumer, UserSettingsHub settings_hub, OrdersModel orders_model, [Import(LoginContainer.Exports.UserName)]string user_name) { _tsf = new TimeAndSalesWindowFactory(this, bus, consumer, settings_hub); _mvf = new MarketViewWindowFactory(this, bus, settings_hub, orders_model); } diff --git a/src/client/spark/Modules/Spreader/SpreaderModel.cs b/src/client/spark/Modules/Spreader/SpreaderModel.cs index 346f74e22..f34af26ab 100644 --- a/src/client/spark/Modules/Spreader/SpreaderModel.cs +++ b/src/client/spark/Modules/Spreader/SpreaderModel.cs @@ -1,6 +1,7 @@ using bts.api; using bts.api.synthetic; using bts.spark; +using mbts.ui.shell; using System; using System.Collections.Generic; using System.ComponentModel.Composition; @@ -14,12 +15,14 @@ namespace mbts.spark { readonly IBus _bus; readonly SyntheticSecurityService _service; readonly ErrorReporter _error; + readonly uint _user_id; [ImportingConstructor] - public SpreaderModel(IBus bus, SyntheticSecurityService service, ErrorReporter error) { + public SpreaderModel(IBus bus, SyntheticSecurityService service, ErrorReporter error, [Import(LoginContainer.Exports.UserID)]uint user_id) { _bus = bus; _service = service; _error = error; + _user_id = user_id; } public IObservable> strategy => _bus.subscribe(SparkTags.SyntheticstrategiesTag); @@ -39,29 +42,40 @@ namespace mbts.spark { return (result, success); }); - public async Task lookup_strategy_async(SecurityImmutable security) => - await lookup_strategy_async(security.Id); + public Task lookup_strategy_async(SecurityImmutable security) => + lookup_strategy_async(security.Id); - public async Task lookup_strategy_async(uint id) { - var result = await _service.query_security_metadata_async(new MetadataQuery { SecurityKey = { id } }); - if (result.success) { - var metadata = result.value.Data.FirstOrDefault(i => i.SecurityKey == id); - if (metadata != null) { - return metadata.Strategy; - } + public Task lookup_strategy_async(uint id) => + _error.safe_execute( + () => _service.query_security_metadata_async(new MetadataQuery { SecurityKey = { id } }), + mdc => mdc.Data.Count == 1 && mdc.Data[0].SecurityKey == id, + mdc => $"did not find strategy for security id '{id}.'", + (mdc, success) => success ? mdc.Data[0].Strategy : (ExecutionStrategyImmutable?)null, + "lookup strategy"); + + public Task create_synthetic(SecurityImmutable synthetic) { + if (synthetic.ContractCase != Security.ContractOneofCase.Synthetic) + return null; + if (synthetic.Synthetic.UserId != _user_id) { + var msynth = synthetic.CloneToMutable(); + msynth.Synthetic.UserId = _user_id; + synthetic = new SecurityImmutable(msynth); } - return null; + return _error.safe_execute( + () => _service.create_synthetic_async(synthetic), + r => r.Error == CreateResult.Types.Error.Ok, + r => r.Error.ToString(), + (r, success) => success ? r.Key : (uint?)null, + "creating synthetic"); } - public async Task create_synthetic(SecurityImmutable? synthetic) { - var result = await _service.create_synthetic_async(synthetic.Value); - return _error.handle_error(result, "Error creating synthetic: {0}", r => r.Error, e => e == CreateResult.Types.Error.Ok, r => new uint?(r.Key), () => null).result; - } - - public async Task save_strategy(uint synthetic, ExecutionStrategyImmutable strategy) { - var result = await _service.update_security_metadata_async(new Metadata { SecurityKey = synthetic, Strategy = strategy.CloneToMutable() }); - return _error.handle_error(result, "Error saving execution strategy: {0}", r => r.Error, e => e == UpdateResult.Types.Error.Ok); - } + public Task save_strategy(uint synthetic, ExecutionStrategyImmutable strategy) => + _error.safe_execute( + () => _service.update_security_metadata_async(new Metadata { SecurityKey = synthetic, Strategy = strategy.CloneToMutable() }), + r => r.Error == UpdateResult.Types.Error.Ok, + r => r.Error.ToString(), + (r, success) => success, + "saving strategy"); public async Task verify_name_is_unique(string name) { var (result, success) = await strategy_tuple.FirstAsyncTask(SparkUtils.DefaultTimeOut); diff --git a/src/client/spark/Modules/Spreader/VM/SyntheticEditorVM.cs b/src/client/spark/Modules/Spreader/VM/SyntheticEditorVM.cs index 6e1ddca65..017eb24c0 100644 --- a/src/client/spark/Modules/Spreader/VM/SyntheticEditorVM.cs +++ b/src/client/spark/Modules/Spreader/VM/SyntheticEditorVM.cs @@ -134,7 +134,7 @@ namespace mbts.spark { _synthetic.ContractSpec.PriceBreak = double.MaxValue; _synthetic.ContractSpec.MainFraction = (uint)MathUtils.factor_out(_tick_factors.tick_base, 10); _synthetic.ContractSpec.PriceDisplayFormat = (uint)_tick_factors.display_digits(); - uint? id = await model.create_synthetic(_synthetic); + uint? id = await model.create_synthetic(new SecurityImmutable(_synthetic)); if (id.HasValue && id != 0) { if (await model.save_strategy(id.Value, get_strategy())) { var (smd, success) = await SparkUtils.get_security_md_async(bus, id.Value); diff --git a/src/client/spark/Modules/Trading/AlgoStudio/Model/AlgoStudioDefinitionTemplateModel.cs b/src/client/spark/Modules/Trading/AlgoStudio/Model/AlgoStudioDefinitionTemplateModel.cs index 1e0f3b8b9..b036f0671 100644 --- a/src/client/spark/Modules/Trading/AlgoStudio/Model/AlgoStudioDefinitionTemplateModel.cs +++ b/src/client/spark/Modules/Trading/AlgoStudio/Model/AlgoStudioDefinitionTemplateModel.cs @@ -1,5 +1,4 @@ using bts; -using bts.api; using bts.api.core; using bts.api.orderman; using bts.persist.algo; @@ -110,38 +109,24 @@ namespace mbts.spark { public void Dispose() => Disposable.dispose(ref _dispose); - async Task _create_definition(CompositeAlgoDefinitionImmutable definition) { - if (_try_get_user(out uint user_id)) { - return await _create_definition(definition, user_id); - } else { - _report_no_user_id(ActionMetadata.CreateDefinition); - } - return null; - } + async Task _create_definition(CompositeAlgoDefinitionImmutable definition) => + await _create_definition(definition, _admin_model.user.id); async Task _create_template(AlgoTemplateImmutable template) { - if (_try_get_user(out uint user_id)) { - var (value, success) = await _error_reporter.safe_execute(() => _service.create_template_async( - new TemplateCreateRequest { UserId = user_id, NewTemplate = template.CloneToMutable() }), - r => r.Success, r => r.ErrMsg, ActionMetadata.CreateTemplate); - if (success) - return _Utils.template_data(value.Record); - } else { - _report_no_user_id(ActionMetadata.CreateTemplate); - } + var (value, success) = await _error_reporter.safe_execute(() => _service.create_template_async( + new TemplateCreateRequest { UserId = _admin_model.user.id, NewTemplate = template.CloneToMutable() }), + r => r.Success, r => r.ErrMsg, ActionMetadata.CreateTemplate); + if (success) + return _Utils.template_data(value.Record); return null; } async Task _update_template(AlgoTemplateRecordImmutable template) { - if (_try_get_user(out uint user_id)) { - var (value, success) = await _error_reporter.safe_execute(() => _service.update_template_async( - new TemplateUpdateRequest { Key = template.Key, UpdatedTemplate = template.AlgoTemplate.CloneToMutable() }), - r => r.Success, r => r.ErrMsg.ToString(), ActionMetadata.UpdateTemplate); - if (success) - return _Utils.template_data(template); - } else { - _report_no_user_id(ActionMetadata.UpdateTemplate); - } + var (value, success) = await _error_reporter.safe_execute(() => _service.update_template_async( + new TemplateUpdateRequest { Key = template.Key, UpdatedTemplate = template.AlgoTemplate.CloneToMutable() }), + r => r.Success, r => r.ErrMsg.ToString(), ActionMetadata.UpdateTemplate); + if (success) + return _Utils.template_data(template); return null; } @@ -175,17 +160,6 @@ namespace mbts.spark { void _report_no_user_id(string desc) => _error_reporter.report_error($"{desc} failed: did not find current user id"); - bool _try_get_user(out uint id) { - var user = _admin_model.get_current_user(); - if (user.HasValue) { - id = user.Value.id; - return true; - } else { - id = 0; - return false; - } - } - // used for error handling/reporting interface IActionMetadata { string desc { get; } diff --git a/src/client/spark/Modules/Trading/AlgoStudio/Model/AlgoStudioFlatDefinitionModel.cs b/src/client/spark/Modules/Trading/AlgoStudio/Model/AlgoStudioFlatDefinitionModel.cs index d71801748..57518ae9f 100644 --- a/src/client/spark/Modules/Trading/AlgoStudio/Model/AlgoStudioFlatDefinitionModel.cs +++ b/src/client/spark/Modules/Trading/AlgoStudio/Model/AlgoStudioFlatDefinitionModel.cs @@ -1,14 +1,11 @@ using bts; -using bts.api; using bts.api.core; using bts.api.orderman.algo; using bts.persist.algo; using bts.spark; -using mbts.spark.utilities; using mbts.utils; using System; using System.Collections.Generic; -using System.Linq; using System.Reactive.Linq; using System.Threading.Tasks; @@ -27,7 +24,12 @@ namespace mbts.spark { _admin_model = admin_model; _error_reporter = error_reporter; this.ui_configuration = ui_configuration; - definitions = service.subscribei_algo_definitions().Filter(r => r.Metadata.IsActive).SelectKeyValue(_definition_data, d => d.key).Table().Cache(); + definitions = + service.subscribei_algo_definitions() + .Filter(r => r.Metadata.IsActive && r.Metadata.CreatorUserId == _admin_model.user.id) + .SelectKeyValue(_definition_data, d => d.key) + .Table() + .Cache(); } public IObservable> definitions { get; } @@ -39,15 +41,11 @@ namespace mbts.spark { Observable.Return(TableUpdate.mk_empty(t => t.key)); public async Task save_definition(AlgoData definition_data) { - if (_try_get_user(out uint user_id)) { - var (value, success) = await _error_reporter.safe_execute(() => _service.save_algo_definition_async( - _with_key(new AlgoDefinitionUpdateRequest { UserId = user_id, Algo = _template(definition_data) }, definition_data.key)), - r => r.Success, r => r.ErrorMessage, "saving template"); - if (success) - return _definition_data(new AlgoDefinitionRecordImmutable(value.Record)); - } else { - _report_no_user_id("save template"); - } + var (value, success) = await _error_reporter.safe_execute(() => _service.save_algo_definition_async( + _with_key(new AlgoDefinitionUpdateRequest { UserId = _admin_model.user.id, Algo = _template(definition_data) }, definition_data.key)), + r => r.Success, r => r.ErrorMessage, "saving template"); + if (success) + return _definition_data(new AlgoDefinitionRecordImmutable(value.Record)); return null; } @@ -105,16 +103,5 @@ namespace mbts.spark { void _report_no_user_id(string desc) => _error_reporter.report_error($"{desc} failed: did not find current user id"); - - bool _try_get_user(out uint id) { - var user = _admin_model.get_current_user(); - if (user.HasValue) { - id = user.Value.id; - return true; - } else { - id = 0; - return false; - } - } } } \ No newline at end of file diff --git a/src/client/spark/Modules/Trading/Ladder/LadderSettings.cs b/src/client/spark/Modules/Trading/Ladder/LadderSettings.cs index 7fbb2fb55..636d901c4 100644 --- a/src/client/spark/Modules/Trading/Ladder/LadderSettings.cs +++ b/src/client/spark/Modules/Trading/Ladder/LadderSettings.cs @@ -339,6 +339,7 @@ namespace mbts.spark { zoom_slider_visible = false, zoom_factor = _default_zoom_factor, net_change = NetChangeTerms.None, + mini_ob = new LadderMiniOrderBookSettings(bts.shell.orderbook.OrderBookStartupData.default_mini_grid.general), }; rv.row_height = LadderRender.measure_row_height(rv); return rv; diff --git a/src/client/spark/Modules/Trading/Ladder/LadderWindowFactory.cs b/src/client/spark/Modules/Trading/Ladder/LadderWindowFactory.cs index faab2022d..e0548210f 100644 --- a/src/client/spark/Modules/Trading/Ladder/LadderWindowFactory.cs +++ b/src/client/spark/Modules/Trading/Ladder/LadderWindowFactory.cs @@ -96,14 +96,28 @@ namespace bts.shell { lp => new SettingsProfile(lp.id, lp.name, LadderSettings.sanitize(lp.value), lp.security_criteria))), (us, lps) => us.with(ladder_profiles: lps.Values.Select(lp => new LadderSettingsProfile(lp.id, lp.name, lp.value, lp.security_criteria)).ToArray())); state_settings?.sanitize(); + /// When settings are adjusted outside of the settings dialog, the vm should raise the event. + /// In response to that event, 'needs_resync' is set to true. This causes 'last_value' to be refreshed when the next caller requests the latest settings + /// (i.e., an IObserver subscribing to the settings_source, or the settings-dialog viewmodel). + bool initialized = false, needs_resync = false; LadderSettings last_value = fit_to_sec(state_settings); - var settings_subject = new BehaviorSubject(last_value); + LadderVM vm = null; + var settings_subject = new Subject(); + var settings_source = Observable.Create(o => { + ensure_synced(); + if (last_value != null) + o.OnNext(last_value); + return settings_subject.Subscribe(s => { + if (s != null) + o.OnNext(s); + }); + }); // vm - var vm = LadderVM.create(_bus, model, last_value ?? _settings_hub.current.ladder, settings_subject.Where(s => s != null)); + vm = LadderVM.create(_bus, model, LadderSettings.sanitize(last_value ?? _settings_hub.current.ladder), settings_source); + vm.settings_invalidated += on_settings_invalidated; vm.order_properties_vm.open_algo_popup_on_select = _ladder_conf.open_algo_popup_on_select; vm.order_properties_vm.use_algo_param_grid = _ladder_conf.use_algo_param_grid; vm.request_settings_dialog += open_settings_dialog; - bool initialized = false; var settings_subscription = _settings_hub.subscribe_default_settings().Subscribe(on_user_settings); vm.OnDisposed += disposing => { Disposable.dispose(ref settings_subscription); @@ -119,8 +133,25 @@ namespace bts.shell { LadderSettings fit_to_sec(LadderSettings s) => _fit_to_sec(s, md.security.ContractCase); + void on_settings_invalidated() => + needs_resync = true; + + void ensure_synced() { + if (!needs_resync) + return; + var curr = vm.get_save_state(); + if (settings_id == 0) { + last_value = curr; + } else if (!curr.Equals(last_value)) { + settings_id = 0; + last_value = curr; + } + needs_resync = false; + } + void on_user_settings(UserSettings us) { if (initialized) { + ensure_synced(); if (settings_id == 0) { // not on a profile, don't respond to default settings changes return; @@ -290,4 +321,4 @@ namespace bts.shell { [ProtoMember(3)] public uint settings_id; } -} \ No newline at end of file +} diff --git a/src/client/spark/Modules/Trading/Ladder/VM/LadderSettingsVM.cs b/src/client/spark/Modules/Trading/Ladder/VM/LadderSettingsVM.cs index 21fb574cb..82e95beb8 100644 --- a/src/client/spark/Modules/Trading/Ladder/VM/LadderSettingsVM.cs +++ b/src/client/spark/Modules/Trading/Ladder/VM/LadderSettingsVM.cs @@ -355,8 +355,19 @@ namespace mbts.spark { } public LadderSettings get_result() { - var clone = SparkUtils.clone_proto(_current); - clone.mini_ob = new LadderMiniOrderBookSettings(mini_ob.create()); + LadderSettings clone; + if (_current == null) { + clone = new LadderSettings { + mini_ob = new LadderMiniOrderBookSettings(mini_ob.create()) + }; + } else { + clone = SparkUtils.clone_proto(_current); +#pragma warning disable CS0612 // Type or member is obsolete + clone.mini_ob = new LadderMiniOrderBookSettings(_current.mini_ob.value != null ? + mini_ob.create().with(sorting_criterion: _current.mini_ob.value.sorting_criterion) : + mini_ob.create()); +#pragma warning restore CS0612 // Type or member is obsolete + } clone.badge_style = badge_style; clone.alternate_order_size = alternate_order_size; clone.favorite_algos = favorite_algos_vm.create(); diff --git a/src/client/spark/Modules/Trading/Ladder/VM/LadderVM.cs b/src/client/spark/Modules/Trading/Ladder/VM/LadderVM.cs index 809fdb0b9..006fc8f67 100644 --- a/src/client/spark/Modules/Trading/Ladder/VM/LadderVM.cs +++ b/src/client/spark/Modules/Trading/Ladder/VM/LadderVM.cs @@ -241,7 +241,7 @@ namespace mbts.spark { public IBus bus => _bus; - public event Action on_tick, request_settings_dialog; + public event Action on_tick, request_settings_dialog, settings_invalidated; public MDConsumerHandle md => _model.md; // we return only entries who have piqs and whose piqs are not uint.Max @@ -394,6 +394,12 @@ namespace mbts.spark { } } + public void on_mini_orderbook_closed(bts.shell.orderbook.OrderBookStartupData ob_state) { + current_settings = SparkUtils.clone_proto(current_settings); + current_settings.mini_ob = new LadderMiniOrderBookSettings(ob_state.orderbook_grid.general); + settings_invalidated?.Invoke(); + } + public void send_order((double price, Side? side, MouseButton button) args) { if (order_properties_vm == null || args.button == MouseButton.Middle) return; diff --git a/src/client/spark/Modules/Trading/Ladder/VM/LadderVMContainer.cs b/src/client/spark/Modules/Trading/Ladder/VM/LadderVMContainer.cs index dadd8123d..b50b9a890 100644 --- a/src/client/spark/Modules/Trading/Ladder/VM/LadderVMContainer.cs +++ b/src/client/spark/Modules/Trading/Ladder/VM/LadderVMContainer.cs @@ -51,7 +51,7 @@ namespace mbts.spark { LadderVM.create_readonly(_bus, new LadderModel(security_md.Value, _model, _model.mbo_service, new Func(osec => _selected_order_tree.Contains(osec.order.Key))), - Observable.Return(LadderSettings.Default)) : null); + Observable.Return(LadderSettings.sanitize(LadderSettings.Default))) : null); RaisePropertyChanged(nameof(value)); } } diff --git a/src/client/spark/Modules/Trading/Ladder/View/LadderGridHost.cs b/src/client/spark/Modules/Trading/Ladder/View/LadderGridHost.cs index 8b5460ca7..b78519f62 100644 --- a/src/client/spark/Modules/Trading/Ladder/View/LadderGridHost.cs +++ b/src/client/spark/Modules/Trading/Ladder/View/LadderGridHost.cs @@ -67,7 +67,8 @@ namespace mbts.spark { NativePriceSize _last_trade; Func _fmt_size = MDDisplayHelper.get_formatter_nps(MarketSizeDisplay.get_default()); int _old_tooltip_row = int.MaxValue; - double _old_last_trade_price = 0.0; + NativePriceSize? _old_last_trade; + string _direction_arrow = string.Empty; bool _tooltips_are_hooked_up = false; ToolTip tooltip_view = new ToolTip(); IDisposable _parameterized_order_popup; @@ -693,12 +694,14 @@ namespace mbts.spark { // At any rate, we need this part hiding the tooltip so that we don't leave random tooltips around // and we have to reset to old row so that when we go back to what could be our preivous row, the tooltip gets drawn again //System.Diagnostics.Debug.WriteLine(" Tooltip - Volume is 0"); + _old_tooltip_row = int.MaxValue; _hide_tool_tip(); //_old_tooltip_row = Int32.MaxValue; } } } else { //System.Diagnostics.Debug.WriteLine(" Tooltip - not over Volume column"); + _old_tooltip_row = int.MaxValue; _hide_tool_tip(); } } @@ -861,21 +864,43 @@ namespace mbts.spark { void _refresh_volume_at_price() { if (!_settings.volume_column.general.show) return; + + // if the last trade has a new size then we need to explicitly + // mark the volume container as dirty because there is a chance + // we have arrived here off a size change and not a volume change + // or a random change, like applying new settings + bool last_trade_size_changed = _old_last_trade.HasValue ? _last_trade.size != _old_last_trade.Value.size : true; + if (last_trade_size_changed && _settings.volume_column.show_ltq) { + _volume_container.mark_dirty(); + } var vapls = _volume_container.read_new_value(); if (vapls != null) { Log.assert_d(_price_list != null); - var ltp = _last_trade.price; - var last_index = LadderRender.index_of(_price_list, ltp, _price_comparer); - string direction_arrow = string.Empty; - if (MathUtils.epsilon_equals(ltp, _old_last_trade_price)) { - // do nothing if in here b/c direction_arrow already string.empty - } else if (ltp > _old_last_trade_price) { - direction_arrow = "▲"; - } else { - direction_arrow = "▼"; - } - LadderRender.render_volumes(_volume_vis_tup.visual, _volume_vis_tup.glyph_context, _price_list, vapls, _settings.row_height, security.ContractSpec, _settings.volume_column, last_index, _last_trade, direction_arrow); - _old_last_trade_price = ltp; + var last_index = LadderRender.index_of(_price_list, _last_trade.price, _price_comparer); + LadderRender.render_volumes(_volume_vis_tup.visual, _volume_vis_tup.glyph_context, _price_list, vapls, _settings.row_height, security.ContractSpec, _settings.volume_column, last_index, _get_last_arrow_string()); + _old_last_trade = _last_trade; + } + + string _get_last_arrow_string() { + // if _old_last_trade doesn't have a value, this is the first time the ladder has been launched + // and so the direction should be drawn without an arrow since there is no basis for comparison + if (!_old_last_trade.HasValue) { + _direction_arrow = string.Empty; + // if the Lasts are different then we need to reformat the direction arrow in some fashion + } else if (_old_last_trade != _last_trade) { + var ltp = _last_trade.price; + if (MathUtils.epsilon_equals(ltp, _old_last_trade.Value.price)) { + // update on the same price, so size must be different + _direction_arrow = string.Empty; + } else if (ltp > _old_last_trade.Value.price) { + _direction_arrow = "▲"; + } else { + _direction_arrow = "▼"; + } + } + // regardless of if we've arrived here via a new Last (and new direction arrow) + // or we're reusing the old one, this format bit is the same + return $"{_direction_arrow} {_last_trade.size} "; } } diff --git a/src/client/spark/Modules/Trading/Ladder/View/LadderOrdersHost.cs b/src/client/spark/Modules/Trading/Ladder/View/LadderOrdersHost.cs index 6ccb3d81c..903e97ab3 100644 --- a/src/client/spark/Modules/Trading/Ladder/View/LadderOrdersHost.cs +++ b/src/client/spark/Modules/Trading/Ladder/View/LadderOrdersHost.cs @@ -9,9 +9,7 @@ using System.Linq; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; -using System.Windows.Data; using System.Windows.Input; -using System.Windows.Interactivity; using System.Windows.Media; namespace mbts.spark { @@ -156,7 +154,10 @@ namespace mbts.spark { popup.SetBinding(System.Windows.Controls.Primitives.Popup.IsOpenProperty, nameof(OrderBookVM.is_row_collection_populated) ); - popup.Closed += (sender, event_args) => (popup.DataContext as IDisposable)?.Dispose(); + popup.Closed += (sender, event_args) => { + (_parent.DataContext as LadderVM)?.on_mini_orderbook_closed(obvm.create()); + (popup.DataContext as IDisposable)?.Dispose(); + }; popup.MouseUp += (sender, event_args) => { if (event_args.ChangedButton == MouseButton.Middle) { popup.IsOpen = false; diff --git a/src/client/spark/Modules/Trading/Ladder/View/LadderRender.cs b/src/client/spark/Modules/Trading/Ladder/View/LadderRender.cs index 0c7511bcd..d12c79a6c 100644 --- a/src/client/spark/Modules/Trading/Ladder/View/LadderRender.cs +++ b/src/client/spark/Modules/Trading/Ladder/View/LadderRender.cs @@ -196,8 +196,7 @@ namespace mbts.spark { ContractSpecificationImmutable spec, VolumeColumnSettings settings, int last_index, - NativePriceSize last_trade, - string direction_arrow + string last_size_and_direction ) { if (!data.Any()) { _clear_visual(dv); @@ -216,16 +215,16 @@ namespace mbts.spark { Brush total_brush = null; if (settings.total.bars.result.HasValue) { total_brush = new SolidColorBrush(settings.total.bars.result.Value); - } else if (components.Count == 0) { + } else if (components.Count == 0 && !settings.show_ltq) { _clear_visual(dv); return; } // inflate the max so that the graph never fills the available space double max = data.Max(v => v.Volume) * 1.1; - 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) { + Brush text_brush = settings.general.foreground.rgb_brush(); double y = row_height * r; var last_trade_here = r == last_index; @@ -259,7 +258,8 @@ namespace mbts.spark { } if (settings.show_ltq && last_trade_here) { numbers_text.Clear(); - numbers_text.Add($"{direction_arrow} {last_trade.size} "); + numbers_text.Add(last_size_and_direction); + text_brush = BTSCoreColors.white; } if (numbers_text.Count > 0) { string str = string.Join("/", numbers_text); diff --git a/src/client/spark/Modules/Trading/OrderBook/MiniOrderBook/MiniOrderBookView.xaml b/src/client/spark/Modules/Trading/OrderBook/MiniOrderBook/MiniOrderBookView.xaml index a3c025aa5..70dc3b094 100644 --- a/src/client/spark/Modules/Trading/OrderBook/MiniOrderBook/MiniOrderBookView.xaml +++ b/src/client/spark/Modules/Trading/OrderBook/MiniOrderBook/MiniOrderBookView.xaml @@ -14,7 +14,6 @@ xmlns:order="clr-namespace:bts.api.orderman;assembly=spark_api" mc:Ignorable="d" MaxHeight="240" - MinWidth="875" d:DesignHeight="600" d:DesignWidth="1080" FocusManager.IsFocusScope="True" Background="{StaticResource CommonBackground}"> diff --git a/src/client/spark/Modules/Trading/OrderBook/OrderBookColumns.cs b/src/client/spark/Modules/Trading/OrderBook/OrderBookColumns.cs index 6ae3e6f72..d917adaac 100644 --- a/src/client/spark/Modules/Trading/OrderBook/OrderBookColumns.cs +++ b/src/client/spark/Modules/Trading/OrderBook/OrderBookColumns.cs @@ -1177,7 +1177,6 @@ namespace mbts.spark { public class CommitColumn : FrameworkElementColumnBase { public CommitColumn() : base(Columns.Commit) { id = key.ToString(); - Header = ""; Width = 20; } diff --git a/src/client/spark/Modules/Trading/OrderBook/OrderBookVM.cs b/src/client/spark/Modules/Trading/OrderBook/OrderBookVM.cs index 77ecffeb4..7dc1fb12a 100644 --- a/src/client/spark/Modules/Trading/OrderBook/OrderBookVM.cs +++ b/src/client/spark/Modules/Trading/OrderBook/OrderBookVM.cs @@ -331,8 +331,8 @@ namespace mbts.spark { IDisposable connect_order_mod_vm() => Disposable.Create( - _selected_params_mon.subscribe_algos().Subscribe(order_mod_vm.algo_vm.load_many), - order_mod_vm.subscribe_is_dirty().Subscribe(_ => { + _selected_params_mon.subscribe_algos().OnDispatcher().Subscribe(order_mod_vm.algo_vm.load_many), + order_mod_vm.subscribe_is_dirty().OnDispatcher().Subscribe(_ => { modify_selected_orders_async_cmd.RaiseCanExecuteChanged(); revert_selected_orders_cmd.RaiseCanExecuteChanged(); }), @@ -340,10 +340,10 @@ namespace mbts.spark { IDisposable connect_common_selected_params(OrderParamsVM vm) => Disposable.Create( - _selected_params_mon.subscribe_security().to_security_md(bus).OnDispatcher().Subscribe(newval => vm.security_md = newval), - _selected_params_mon.subscribe_price().Subscribe(vm.on_common_price), - _selected_params_mon.subscribe_size().Subscribe(vm.on_common_size), - _selected_params_mon.subscribe_account().Subscribe(newval => vm.selected_account = newval)); + _selected_params_mon.subscribe_security().OnDispatcher().to_security_md(bus).OnDispatcher().Subscribe(newval => vm.security_md = newval), + _selected_params_mon.subscribe_price().OnDispatcher().Subscribe(vm.on_common_price), + _selected_params_mon.subscribe_size().OnDispatcher().Subscribe(vm.on_common_size), + _selected_params_mon.subscribe_account().OnDispatcher().Subscribe(newval => vm.selected_account = newval)); } private void _row_collection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { @@ -994,11 +994,13 @@ namespace mbts.spark { var join = config.Configuration.get(); var lean = config.Configuration.get(); var top = config.Configuration.get(); + var offset = config.Configuration.get(); _algo_type_conf = new Dictionary { { CustomOrderDefinition.CustomOneofCase.Cover, (cover.show_price, cover.show_quantity) }, { CustomOrderDefinition.CustomOneofCase.Join, (join.show_price, join.show_quantity) }, { CustomOrderDefinition.CustomOneofCase.Lean, (lean.show_price, lean.show_quantity) }, - { CustomOrderDefinition.CustomOneofCase.Top, (top.show_price, top.show_quantity) } + { CustomOrderDefinition.CustomOneofCase.Top, (top.show_price, top.show_quantity) }, + { CustomOrderDefinition.CustomOneofCase.Offset, (offset.show_price, offset.show_quantity) } }; } diff --git a/src/client/spark/Modules/Trading/OrderBook/Settings/OrderBookFilterStates.cs b/src/client/spark/Modules/Trading/OrderBook/Settings/OrderBookFilterStates.cs index 52c09c565..5545761d4 100644 --- a/src/client/spark/Modules/Trading/OrderBook/Settings/OrderBookFilterStates.cs +++ b/src/client/spark/Modules/Trading/OrderBook/Settings/OrderBookFilterStates.cs @@ -144,7 +144,7 @@ namespace bts.shell.orderbook { pb.add_discrete_values(fs.symbols, o => o.security?.Symbol); // numeric pb.add_numeric(fs.qty, o => o.order.quantity()); - pb.add_numeric(fs.price, o => o.order.price()); + pb.add_price(fs.price, o => o.order.price(), o => o.security); pb.add_numeric(fs.qty_filled, o => o.order.filled()); pb.add_numeric(fs.avg_price, o => o.order.avg_price()); pb.add_numeric(fs.last_price, o => o.order.last_price()); diff --git a/src/client/spark/Orders/OrderDataHelper.cs b/src/client/spark/Orders/OrderDataHelper.cs index 511dc6711..15abf0209 100644 --- a/src/client/spark/Orders/OrderDataHelper.cs +++ b/src/client/spark/Orders/OrderDataHelper.cs @@ -168,17 +168,14 @@ namespace mbts.spark.utilities { // this will be the tag 50 on CME orders // current user might be okay, but we might also want to get it // from the store that serves the risk admin gui - ManualOrderIndicator = ManualOrderIndicator.Yes + ManualOrderIndicator = ManualOrderIndicator.Yes, + User = _admin_model.user.name, + UserId = _admin_model.user.id }; if (account.HasValue) { rv.Account = account.Value.name; rv.AccountId = account.Value.id; } - var user = _admin_model.get_current_user(); - if (user.HasValue) { - rv.User = user.Value.name; - rv.UserId = user.Value.id; - } return rv; } @@ -215,6 +212,7 @@ namespace mbts.spark.utilities { public TriggerOrderDefinition trigger_order() => OrderDataHelper.Defaults.TriggerOrderDefinition(); public DynamicOrderDefinition dynamic_order() => OrderDataHelper.Defaults.DynamicOrderDefinition(); public SpreaderOrderDefinition spreader_order() => OrderDataHelper.Defaults.SpreaderOrderDefinition(); + public SplitterOrderDefinition splitter_order() => OrderDataHelper.Defaults.SplitterOrderDefinition(); public SpreaderOrderDefinition spreader_order(SecurityImmutable security, ExecutionStrategyImmutable strategy) => new SpreaderOrderDefinition { Security = security.CloneToMutable(), @@ -245,6 +243,7 @@ namespace mbts.spark.utilities { case CustomOrderDefinition.CustomOneofCase.Offset: rv.Offset = offset_order(); break; case CustomOrderDefinition.CustomOneofCase.Trigger: rv.Trigger = trigger_order(); break; case CustomOrderDefinition.CustomOneofCase.Spreader: rv.Spreader = spreader_order(); break; + case CustomOrderDefinition.CustomOneofCase.Splitter: rv.Splitter = splitter_order(); break; case CustomOrderDefinition.CustomOneofCase.Dynamic: rv.Dynamic = dynamic_order(); break; //case CustomOrderDefinition.CustomOneofCase.QueueHolder: rv.QueueHolder = queue_holder_order(); break; } @@ -272,6 +271,7 @@ namespace mbts.spark.utilities { case CustomOrderDefinition.CustomOneofCase.Offset: return new OrderMetadata { KeyBinding = "O", Color = BTSColors.Orders.off_order }; case CustomOrderDefinition.CustomOneofCase.Trigger: return new OrderMetadata { KeyBinding = "T", Color = BTSColors.Orders.trigger_order }; case CustomOrderDefinition.CustomOneofCase.Spreader: return new OrderMetadata { KeyBinding = "", Color = BTSColors.Orders.spreader_order }; + // case CustomOrderDefinition.CustomOneofCase.Splitter: return new OrderMetadata { KeyBinding = "", Color = BTSColors.orange.rgb_color() }; case CustomOrderDefinition.CustomOneofCase.Dynamic: return new OrderMetadata { KeyBinding = "", Color = BTSColors.Orders.top_order }; case CustomOrderDefinition.CustomOneofCase.None: goto default; //case CustomOrderDefinition.CustomOneofCase.QueueHolder: return new OrderMetadata { KeyBinding = "Q", Color = BTSColors.purple.rgb_color() }; @@ -595,6 +595,8 @@ namespace mbts.spark.utilities { // The following standard types are not yet ready for production use: // CustomOrderDefinition.CustomOneofCase.Tick, // CustomOrderDefinition.CustomOneofCase.Dynamic, + + // exchange_order_types are the types which can be submitted on an exchange security exchange_order_types = new[] { CustomOrderDefinition.CustomOneofCase.Market, CustomOrderDefinition.CustomOneofCase.Limit, @@ -605,7 +607,9 @@ namespace mbts.spark.utilities { CustomOrderDefinition.CustomOneofCase.Lean, CustomOrderDefinition.CustomOneofCase.Join, CustomOrderDefinition.CustomOneofCase.Cover, - CustomOrderDefinition.CustomOneofCase.Top + CustomOrderDefinition.CustomOneofCase.Top, + CustomOrderDefinition.CustomOneofCase.Offset, + CustomOrderDefinition.CustomOneofCase.Splitter, }, // The following spreader types are not yet ready for production use: // CustomOrderDefinition.CustomOneofCase.Dynamic, @@ -615,14 +619,16 @@ namespace mbts.spark.utilities { CustomOrderDefinition.CustomOneofCase.Lean, CustomOrderDefinition.CustomOneofCase.Join, CustomOrderDefinition.CustomOneofCase.Cover, - CustomOrderDefinition.CustomOneofCase.Top + CustomOrderDefinition.CustomOneofCase.Top, + CustomOrderDefinition.CustomOneofCase.Splitter, }, attachable_order_types = new[] { CustomOrderDefinition.CustomOneofCase.Iceberg, CustomOrderDefinition.CustomOneofCase.Lean, CustomOrderDefinition.CustomOneofCase.Join, CustomOrderDefinition.CustomOneofCase.Cover, - CustomOrderDefinition.CustomOneofCase.Top + CustomOrderDefinition.CustomOneofCase.Top, + CustomOrderDefinition.CustomOneofCase.Offset }, outright_order_types = new[] { CustomOrderDefinition.CustomOneofCase.Market, @@ -718,7 +724,7 @@ namespace mbts.spark.utilities { !b.HasChild); public static bool top_equals(TopOrderDefinitionImmutable a, TopOrderDefinitionImmutable b) => - a.WatchLevelTickOffset == b.WatchLevelTickOffset && + a.TicksOff == b.TicksOff && (a.HasTopThreshold ? b.HasTopThreshold && quantity_measure_equals(a.TopThreshold, b.TopThreshold) : !b.HasTopThreshold) && @@ -909,7 +915,7 @@ namespace mbts.spark.utilities { add_component(_get_hash(_get_tick_child(tick))); break; case CustomOrderDefinition.CustomOneofCase.Top: - add_component(def.Top.WatchLevelTickOffset.GetHashCode()); + add_component(def.Top.TicksOff.GetHashCode()); add_component(def.Top.HasTopThreshold ? def.Top.TopThreshold.GetHashCode() : 0); add_component(_get_top_child(def.Top).GetHashCode()); break; @@ -1033,6 +1039,8 @@ namespace mbts.spark.utilities { public static TriggerOrderDefinition TriggerOrderDefinition() => new TriggerOrderDefinition { Child = CustomOrderDefinition(), TriggerCondition = new TriggerCondition(), TriggerAction = new TriggerAction(), }; public static DynamicOrderDefinition DynamicOrderDefinition() => new DynamicOrderDefinition(); public static SpreaderOrderDefinition SpreaderOrderDefinition() => new SpreaderOrderDefinition { ExecutionStrategy = new ExecutionStrategy() }; + + public static SplitterOrderDefinition SplitterOrderDefinition() => new SplitterOrderDefinition(); } } } diff --git a/src/client/spark/Orders/OrderPropertiesEditorVM.cs b/src/client/spark/Orders/OrderPropertiesEditorVM.cs index bcf276bec..81354eae0 100644 --- a/src/client/spark/Orders/OrderPropertiesEditorVM.cs +++ b/src/client/spark/Orders/OrderPropertiesEditorVM.cs @@ -105,13 +105,18 @@ namespace mbts.spark { var inline_account_src = order_properties.AggregateLatest( model.factory.subscribe_accounts(), (last, change, cache) => convert_inline_account(change.New.account, cache), - (last, props, change) => convert_inline_account(props.account, change.New), + (last, props, change) => convert_inline_account(props.account, change.New, false), () => new InlineUserProperty(), new InlineOrderProperties(), DictionaryHelper.empty()); + // Note that with the .Where on the account_tuples, we will ignore any + // updates for which all accounts have been removed. + // This was a safer change than changing SelectorProperty's IEnumberable.OnNext + // so that it would not reset the selected item on server reboots; i.e. this + // change only affects this IObservable. _accounts = backing( nameof(selected_account), - model.factory.subscribe_account_tuples(), + model.factory.subscribe_account_tuples().Where(items => items.Any()), inline_account_src, a => new AccountTuple?(a), () => accounts.Cast().FirstOrDefault(), @@ -263,9 +268,18 @@ namespace mbts.spark { break; } } - InlineUserProperty convert_inline_account(InlineUserProperty account_id, IReadOnlyDictionary account_map) => + // + // Regarding the 3rd parameter, force_override_from_inline_property: + // When using an InlineUserProperty, in the context of assigning a default vs selected item, + // there is somewhat of a conflict between the IObserver> and the + // IObserver> OnNext methods. This 3rd paramter provides yet another override + // to say if the force_override should come from the inline property or if the force_override should + // be disbled, by way of the immediate return in ViewModelBase._try_auto_select when force_override + // is false. Our intent is then accomplished because we are no longer attempting to set a default/ + // selected item when the account_tuples potentially changes + InlineUserProperty convert_inline_account(InlineUserProperty account_id, IReadOnlyDictionary account_map, bool force_override_from_inline_property = true) => account_id.has_value && account_map.TryGetValue(account_id.value, out string name) ? - new InlineUserProperty(new AccountTuple(account_id.value, name), account_id.force_override) : + new InlineUserProperty(new AccountTuple(account_id.value, name), force_override_from_inline_property ? account_id.force_override : false) : new InlineUserProperty(); } diff --git a/src/client/spark/Orders/OrderTypeVMs/OrderTypeVMFactory.cs b/src/client/spark/Orders/OrderTypeVMs/OrderTypeVMFactory.cs index 5a4ae67d8..06e14f5c2 100644 --- a/src/client/spark/Orders/OrderTypeVMs/OrderTypeVMFactory.cs +++ b/src/client/spark/Orders/OrderTypeVMs/OrderTypeVMFactory.cs @@ -57,6 +57,8 @@ namespace mbts.spark { public static IAlgoVM iceberg(AlgoKey key, string display_name, AlgoEditingPermissions editing_permissions) => new IcebergOrderVM(key, display_name, editing_permissions); public static IAlgoVM spreader(AlgoKey key, string display_name, AlgoEditingPermissions editing_permissions) => new SpreaderOrderVM(key, display_name, editing_permissions); public static IAlgoVM dynamic(AlgoKey key, string display_name, string dynamic_type, AlgoEditingPermissions editing_permissions) => new DynamicOrderVM(key, dynamic_type, display_name, editing_permissions); + public static IAlgoVM splitter(AlgoKey key, string display_name, AlgoEditingPermissions permissions) => + new SplitterAlgoVM(key, display_name, permissions); public static IAlgoVM create(AlgoData def_data, AlgoEditingPermissions editing_permissions = default_editing_permissions, bool is_expanded = false) { if (def_data.value.CustomCase == AlgoType.Composite) { @@ -88,6 +90,7 @@ namespace mbts.spark { case AlgoType.Iceberg: vm = iceberg(key, name, editing_permissions); break; case AlgoType.Spreader: vm = spreader(key, name, editing_permissions); break; case AlgoType.Dynamic: vm = dynamic(key, name, name, editing_permissions); break; + case AlgoType.Splitter: vm = splitter(key,name,editing_permissions); break; case AlgoType.None: vm = new DefaultAlgoVM(); break; default: throw new NotImplementedException(); } diff --git a/src/client/spark/Orders/OrderTypeVMs/SplitterAlgoVM.cs b/src/client/spark/Orders/OrderTypeVMs/SplitterAlgoVM.cs new file mode 100644 index 000000000..bac206336 --- /dev/null +++ b/src/client/spark/Orders/OrderTypeVMs/SplitterAlgoVM.cs @@ -0,0 +1,24 @@ +using bts.api.orderman; +using mbts.ui.shell; + +namespace mbts.spark +{ + static partial class OrderTypeVMFactory { + class SplitterAlgoVM : OrderTypeVMBase + { + readonly Property _clip, _exposed; + + public SplitterAlgoVM(AlgoKey id, string display_name, AlgoEditingPermissions editing_permissions) + : base(id, display_name, editing_permissions) + { + _clip = add_property(nameof(max_order_qty), SplitterOrderDefinition.MaxOrderQtyFieldNumber); + _exposed = add_property(nameof(exposed), SplitterOrderDefinition.ExposedQtyFieldNumber); + + } + + public uint max_order_qty { get => _clip.Value; set => _clip.set(value); } + public uint exposed { get => _exposed.Value; set => _exposed.set(value); } + } + } + +} \ No newline at end of file diff --git a/src/client/spark/Orders/OrderTypeVMs/TopOrderVM.cs b/src/client/spark/Orders/OrderTypeVMs/TopOrderVM.cs index 984303599..8f41cbbaf 100644 --- a/src/client/spark/Orders/OrderTypeVMs/TopOrderVM.cs +++ b/src/client/spark/Orders/OrderTypeVMs/TopOrderVM.cs @@ -5,13 +5,16 @@ using TopDefi = bts.api.orderman.TopOrderDefinitionImmutable; namespace mbts.spark { static partial class OrderTypeVMFactory { class TopOrderVM : OrderTypeVMBase { + readonly Property _ticks_off; readonly Property _top_threshold; public TopOrderVM(AlgoKey key, string display_name, AlgoEditingPermissions editing_permissions) : base(key, display_name, editing_permissions) { + _ticks_off = add_nullable_property(nameof(ticks_off), TopDef.TicksOffFieldNumber); _top_threshold = add_property(nameof(top_threshold), TopDef.TopThresholdFieldNumber, new QuantityMeasureImmutable(new QuantityMeasure { Ratio = 1.0, Absolute = 1 })); } + public uint? ticks_off { get => _ticks_off.Value; set => _ticks_off.set(value); } public QuantityMeasureImmutable? top_threshold { get => _top_threshold.Value; set => _top_threshold.set(value); } } } diff --git a/src/client/spark/core/Utility/ViewModelBase.cs b/src/client/spark/core/Utility/ViewModelBase.cs index 3800e98bf..8fa702aaf 100644 --- a/src/client/spark/core/Utility/ViewModelBase.cs +++ b/src/client/spark/core/Utility/ViewModelBase.cs @@ -820,6 +820,11 @@ namespace mbts.ui.shell { void _try_auto_select() { if (_is_user_selected_item) { + // if we have a user-selected item and the _desired_item for which we might want to set as the selected item + // isn't force_override, then no change + if (!_desired_item.force_override) { + return; + } if (desired_item_valid() && _desired_item.force_override && _try_match_item(_desired_item.value, out TValue match)) { _set_selected_item(match, SelectionReason.SettingsMandatedSelection); return; diff --git a/src/client/spark/view/Controls/DIYGrid/Columns/ColumnPredicateBuilder.cs b/src/client/spark/view/Controls/DIYGrid/Columns/ColumnPredicateBuilder.cs index 03ee224f3..7d8833c00 100644 --- a/src/client/spark/view/Controls/DIYGrid/Columns/ColumnPredicateBuilder.cs +++ b/src/client/spark/view/Controls/DIYGrid/Columns/ColumnPredicateBuilder.cs @@ -37,6 +37,51 @@ namespace mbts.spark.filters { _predicates.Add(impl); bool impl(TRow o) => max.CompareTo(selector(o)) >= 0; } + } + + // this is a copy of add_numeric except we do a tick_equals comparsion + // if the ticks are not equals, then it falls back to a regular double comparison + // + // if the SecurityImmutable? doesn't have a value + // then we fallback to double comparison + public void add_price(DoubleMinMaxState value, Func selector, Func si_selector) { + if (value == null) + return; + if (value.minimum.active) { + var min = value.minimum.value.Value; + if (value.maximum.active) { + var max = value.maximum.value.Value; + _predicates.Add(impl); + bool impl(TRow o) { + var v = selector(o); + var si = si_selector(o); + if (si.HasValue) { + return si.Value.ContractSpec.compare_tick_fallback_to_double(min, v) <= 0 && + si.Value.ContractSpec.compare_tick_fallback_to_double(max, v) >= 0; + } + return min.CompareTo(v) <= 0 && max.CompareTo(v) >= 0; + } + } else { + _predicates.Add(impl); + bool impl(TRow o) { + var si = si_selector(o); + if (si.HasValue) { + return si.Value.ContractSpec.compare_tick_fallback_to_double(min, selector(o)) <= 0; + } + return min.CompareTo(selector(o)) <= 0; + } + } + } else if (value.maximum.active) { + var max = value.maximum.value.Value; + _predicates.Add(impl); + bool impl(TRow o) { + var si = si_selector(o); + if (si.HasValue) { + return si.Value.ContractSpec.compare_tick_fallback_to_double(max, selector(o)) >= 0; + } + return max.CompareTo(selector(o)) >= 0; + } + } } public void add_regex(string str, Func selector) { diff --git a/src/client/spark/view/Controls/Order/AlgoContentTemplateSelector.xaml b/src/client/spark/view/Controls/Order/AlgoContentTemplateSelector.xaml index c64d29f52..eba06c5bb 100644 --- a/src/client/spark/view/Controls/Order/AlgoContentTemplateSelector.xaml +++ b/src/client/spark/view/Controls/Order/AlgoContentTemplateSelector.xaml @@ -489,6 +489,8 @@ + + @@ -503,9 +505,14 @@ + + Ticks Off + + + + + + + + + + + + + + + + + + Expose Quantity + + Max Order Quantity + + + + + diff --git a/src/client/spark/view/Controls/Order/AlgoContentTemplateSelector.xaml.cs b/src/client/spark/view/Controls/Order/AlgoContentTemplateSelector.xaml.cs index a7af0cd77..9ad12396a 100644 --- a/src/client/spark/view/Controls/Order/AlgoContentTemplateSelector.xaml.cs +++ b/src/client/spark/view/Controls/Order/AlgoContentTemplateSelector.xaml.cs @@ -43,6 +43,7 @@ namespace mbts.ui.spark { public DataTemplate GLBXIcebergOrderTemplate { get; set; } public DataTemplate IcebergOrderTemplate { get; set; } public DataTemplate SpreaderTemplate { get; set; } + public DataTemplate SplitterTemplate { get; set; } public DataTemplate DynamicOrderTemplate { get; set; } public DataTemplate DefaultOrderTemplate { get; set; } @@ -66,6 +67,7 @@ namespace mbts.ui.spark { case CustomOrderDefinition.CustomOneofCase.GlbxIceberg: return GLBXIcebergOrderTemplate; case CustomOrderDefinition.CustomOneofCase.Iceberg: return IcebergOrderTemplate; case CustomOrderDefinition.CustomOneofCase.Spreader: return SpreaderTemplate; + case CustomOrderDefinition.CustomOneofCase.Splitter: return SplitterTemplate; case CustomOrderDefinition.CustomOneofCase.Dynamic: return DynamicOrderTemplate; } } diff --git a/src/client/spark/view/Shell/Login/LoginModel.cs b/src/client/spark/view/Shell/Login/LoginModel.cs index 8f8095007..35f666503 100644 --- a/src/client/spark/view/Shell/Login/LoginModel.cs +++ b/src/client/spark/view/Shell/Login/LoginModel.cs @@ -28,16 +28,27 @@ namespace mbts.ui.shell { model.Dispose(); } - [Export("username")] + [Export(Exports.UserName)] public string UserName { get; private set; } - [Export("userid")] + [Export(Exports.UserID)] public uint UserId { get; private set; } + + [Export] + public UserTuple UserTuple => new UserTuple(UserId, UserName); + [Export] public AuthTokenImmutable Token { get; private set; } [Export("workspace")] public string Workspace { get; private set; } + + public static class Exports { + public const string + UserName = "username", + UserID = "userid", + UserTuple = "usertuple"; + } } public struct User { @@ -49,8 +60,7 @@ namespace mbts.ui.shell { public readonly UInt32 id; } - public class LoginModel : IDisposable - { + public class LoginModel : IDisposable { // Singleton to help bootstrap the system private static LoginModel _instance = new LoginModel(); public static LoginModel instance { get { return _instance; } } @@ -70,6 +80,11 @@ namespace mbts.ui.shell { public bool HasFatalError { get; set; } public User[] Users { get; private set; } + public bool try_get_user(string name, out User user) => + Users.TryGetFirst(u => u.name == name, out user); + + public bool user_exists(string name) => + Users.Any(u => u.name == name); // The final username public (string, uint, AuthTokenImmutable) get_user() { diff --git a/src/client/spark/view/Shell/Login/LoginView.xaml b/src/client/spark/view/Shell/Login/LoginView.xaml index 4778ea279..741efc3fb 100644 --- a/src/client/spark/view/Shell/Login/LoginView.xaml +++ b/src/client/spark/view/Shell/Login/LoginView.xaml @@ -1,136 +1,265 @@ - - - - - - - - + + + + + + + + + + + + 40 + 10 + 22 + + + + + - - - + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - + + + + + + + + + + - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + - - + + + + + + + + + + + + + - - - - - - - - - - + + Load last workspace + - + + + + + + + \ No newline at end of file diff --git a/src/client/spark/view/Shell/Login/LoginView.xaml.cs b/src/client/spark/view/Shell/Login/LoginView.xaml.cs index 8a763e43a..974f3c5ba 100644 --- a/src/client/spark/view/Shell/Login/LoginView.xaml.cs +++ b/src/client/spark/view/Shell/Login/LoginView.xaml.cs @@ -1,15 +1,43 @@ -using System; +using bts; +using mbts.ui.converters; +using mbts.ui.native; +using mbts.utils; +using System; using System.Linq; using System.Windows; +using System.Windows.Data; using System.Windows.Input; using System.Windows.Interop; using System.Windows.Media.Imaging; -using mbts.ui.native; -using bts; namespace mbts.ui.shell { - public partial class LoginView : Window - { + public partial class LoginView : Window { + public enum LoginStatus { + Initializing, + Ready, + Error, + Rejected, + LoggingIn + } + + public static IValueConverter WorkspaceVisualStateConverter => + ValueConverter.create(workspace => + !string.IsNullOrEmpty(workspace) && workspace != LoginViewModel.NEW_WORKSPACE_NAME ? + VisualStateNames.Workspace.Active : + VisualStateNames.Workspace.Inactive); + + public static IValueConverter LoginStatusVisualStateConverter => + ValueConverter.create(status => { + switch (status) { + case LoginStatus.Initializing: return VisualStateNames.LoginStatus.Initializing; + case LoginStatus.Ready: return VisualStateNames.LoginStatus.Ready; + case LoginStatus.LoggingIn: return VisualStateNames.LoginStatus.Loading; + case LoginStatus.Rejected: return VisualStateNames.LoginStatus.Rejected; + case LoginStatus.Error: return VisualStateNames.LoginStatus.Error; + default: throw ExceptionBuilder.Argument.unexpected_case(status, nameof(status)); + } + }); + public LoginView(LoginViewModel model) { this.DataContext = model; if (model is IRequestViewClose) { @@ -65,6 +93,24 @@ namespace mbts.ui.shell { return IntPtr.Zero; } - } + public static class VisualStateNames { + public static class LoginStatus { + public const string GroupName = nameof(LoginStatus); + public static readonly string + Initializing = $"{GroupName}{nameof(Initializing)}", + Ready = $"{GroupName}{nameof(Ready)}", + Loading = $"{GroupName}{nameof(Loading)}", + Rejected = $"{GroupName}{nameof(Rejected)}", + Error = $"{GroupName}{nameof(Error)}"; + } -} + public static class Workspace { + public const string GroupName = nameof(Workspace); + + public static readonly string + Active = $"{GroupName}{VisualStates.Active}", + Inactive = $"{GroupName}{VisualStates.Inactive}"; + } + } + } +} \ No newline at end of file diff --git a/src/client/spark/view/Shell/Login/LoginViewModel.cs b/src/client/spark/view/Shell/Login/LoginViewModel.cs index b9994863d..608b79009 100644 --- a/src/client/spark/view/Shell/Login/LoginViewModel.cs +++ b/src/client/spark/view/Shell/Login/LoginViewModel.cs @@ -2,23 +2,22 @@ using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; -using System.Text; -using System.Threading.Tasks; using System.Windows; using System.Windows.Input; using System.Windows.Threading; +using LoginStatus = mbts.ui.shell.LoginView.LoginStatus; -namespace mbts.ui.shell -{ +namespace mbts.ui.shell { public class LoginViewModel : ViewModelBase, IRequestViewClose { + public const string NEW_WORKSPACE_NAME = "New workspace..."; private LoginModel _model; private Dispatcher _dispatcher; public LoginViewModel(LoginModel model, Dispatcher disp) { _dispatcher = disp; _model = model; Workspaces = new ObservableCollection(); - LoginCommand = new DelegateCommand(OnLogin, _ => UserNames.Count > 0 && SelectedWorkspace != null); + LoginCommand = new DelegateCommand(OnLogin, _ => !string.IsNullOrEmpty(_name) && SelectedWorkspace != null); CancelCommand = new DelegateCommand(OnCancel); EmailCommand = new DelegateCommand(OnSendMail); if (_model.HasFatalError) { @@ -42,23 +41,29 @@ namespace mbts.ui.shell void _model_Ready() { _dispatcher.BeginInvoke(() => { if (!_model.HasFatalError) { - this.UserNames.AddRange(_model.Users.Select(u => u.name)); - this.VisualState = "LoginState"; + login_status = LoginStatus.Ready; load_username(); + if (!string.IsNullOrEmpty(_name)) + KnownUsers.Add(_name); LoginCommand.RaiseCanExecuteChanged(); } else { ErrorMessage = _model.ErrorMessage; - VisualState = "ErrorState"; + login_status = LoginStatus.Error; } }); } - private string _vstate = "Normal"; - public string VisualState { get { return _vstate; } set { _vstate = value; RaisePropertyChanged("VisualState"); } } + LoginStatus _login_status; + public LoginStatus login_status { get => _login_status; private set => Set(ref _login_status, value); } - private ObservableCollection _user_names = new ObservableCollection(); - public ObservableCollection UserNames { get { return _user_names; } } + /// should only contain the user-name of the last login (if none exists, it should be empty). + public ObservableCollection KnownUsers { get; } = new ObservableCollection(); + bool _resume_workspace = true; + public bool resume_workspace { + get => _resume_workspace; + set => Set(ref _resume_workspace, value); + } private string _name; public string Name { get { return _name; } @@ -81,13 +86,14 @@ namespace mbts.ui.shell public string SelectedWorkspace { get; set; } private async void update_workspaces(string name) { Workspaces.Clear(); - Workspaces.Add("New workspace..."); - var user = _model.Users.First(u => u.name == name); - var to_add = await _model.workspaces_for(user); - this.SelectedWorkspace = Workspaces.First(); - if (to_add != null && name == _name && Workspaces.Count == 1) { - Workspaces.AddRange(to_add); - load_last_workspace(name); + Workspaces.Add(NEW_WORKSPACE_NAME); + SelectedWorkspace = Workspaces.First(); + if (_model.try_get_user(name, out var user)) { + var to_add = await _model.workspaces_for(user); + if (to_add != null && name == _name && Workspaces.Count == 1) { + Workspaces.AddRange(to_add); + load_last_workspace(name); + } } RaisePropertyChanged("SelectedWorkspace"); LoginCommand.RaiseCanExecuteChanged(); @@ -111,7 +117,7 @@ namespace mbts.ui.shell var value = key.GetValue("login"); if (value != null) { var n = value.ToString(); - if (UserNames.Contains(n)) { + if (_model.user_exists(n)) { Name = n; } } @@ -134,15 +140,13 @@ namespace mbts.ui.shell private async void OnLogin(object arg) { save_selection(); - string selected_workspace = SelectedWorkspace; - if (selected_workspace == "New workspace...") { - selected_workspace = null; - } + string selected_workspace = _resume_workspace && SelectedWorkspace != NEW_WORKSPACE_NAME ? + SelectedWorkspace : null; var result = await _model.do_login(Name, selected_workspace, Password); - if (result.success) { VisualState = "Loading"; } + if (result.success) { login_status = LoginStatus.LoggingIn; } if (!result.success && !string.IsNullOrEmpty(result.reason)) { WarningMessage = result.reason; - VisualState = "BadPassword"; + login_status = LoginStatus.Rejected; } else if (!string.IsNullOrEmpty(result.reason)) { // Login was successful but we have a message for the user diff --git a/src/client/spark/view/Shell/ViewModels/LauncherVM.cs b/src/client/spark/view/Shell/ViewModels/LauncherVM.cs index c870865ec..63b760725 100644 --- a/src/client/spark/view/Shell/ViewModels/LauncherVM.cs +++ b/src/client/spark/view/Shell/ViewModels/LauncherVM.cs @@ -1,14 +1,14 @@ -using System; +using bts; +using bts.api; +using bts.spark; +using mbts.ui.native; +using mbts.ui.utility; +using System; using System.Collections.ObjectModel; using System.ComponentModel.Composition; using System.Windows; using System.Windows.Controls; using System.Windows.Input; -using mbts.ui.native; -using mbts.ui.utility; -using bts.api; -using bts.spark; -using bts; namespace mbts.ui.shell { [Export(typeof(ILauncher))] @@ -23,7 +23,7 @@ namespace mbts.ui.shell { [ImportingConstructor] public LauncherVM( ObservableCollection modules, - [Import("username")]string username, + [Import(LoginContainer.Exports.UserName)]string username, ErrorReporter errors, IBus bus) { _user = username; _modules = new ObservableAutoWrapper(modules, m => new ShellModuleVM(m, bus)); diff --git a/src/client/spark/view/Shell/Workspaces/WorkspacesDialogView.xaml b/src/client/spark/view/Shell/Workspaces/WorkspacesDialogView.xaml index d573ac3dd..38f388d91 100644 --- a/src/client/spark/view/Shell/Workspaces/WorkspacesDialogView.xaml +++ b/src/client/spark/view/Shell/Workspaces/WorkspacesDialogView.xaml @@ -45,12 +45,7 @@ - - - - - Shared diff --git a/src/client/spark/view/Spark/Core/TickUtils.cs b/src/client/spark/view/Spark/Core/TickUtils.cs index 5a2a7d5e6..8144dd74a 100644 --- a/src/client/spark/view/Spark/Core/TickUtils.cs +++ b/src/client/spark/view/Spark/Core/TickUtils.cs @@ -128,6 +128,9 @@ namespace mbts.spark { public static IEqualityComparer tick_equality_comparer(this ContractSpecificationImmutable contract) => EqualityComparer.Create((double a, double b) => tick_equals(contract, a, b), x => MathUtils.RoundFractionalUnits(x, contract.LowTick)); + public static int compare_tick_fallback_to_double(this ContractSpecificationImmutable contract, double x, double y) => + tick_equals(contract, x, y) ? 0 : x.CompareTo(y); + public static double round_to_tick(this ContractSpecificationImmutable spec, double value) { double intpart = (long)value; double floatpart = value % 1; diff --git a/src/client/spark/view/Utility/StateManager.cs b/src/client/spark/view/Utility/StateManager.cs index ddcd63094..3537c894e 100644 --- a/src/client/spark/view/Utility/StateManager.cs +++ b/src/client/spark/view/Utility/StateManager.cs @@ -253,15 +253,18 @@ namespace mbts.ui { sd.StateChanged += on_state_changed; return Disposable.Create(() => sd.StateChanged -= on_state_changed); void on_state_changed(string oldval, string newval) => transition(newval); - async void transition(string state) { + void transition(string state) { version++; if (target.IsLoaded) StateManager.GoToState(target, state); else { int v = version; - await target.LoadedAsync(); - if (v == version) - StateManager.GoToState(target, state); + target.Loaded += on_loaded; + void on_loaded(object sender, RoutedEventArgs e) { + target.Loaded -= on_loaded; + if (v == version) + StateManager.GoToState(target, state); + } } } }); diff --git a/src/client/spark_api/Co2/MDConsumerHandle.cs b/src/client/spark_api/Co2/MDConsumerHandle.cs index 7788ce771..36b560c1a 100644 --- a/src/client/spark_api/Co2/MDConsumerHandle.cs +++ b/src/client/spark_api/Co2/MDConsumerHandle.cs @@ -259,14 +259,13 @@ namespace mbts.co2 { } } - [StructLayout(LayoutKind.Sequential, Size = 712)] + [StructLayout(LayoutKind.Sequential, Size = 704)] internal struct ColdEntry { public int exchange_status; // Last trade public AtomicPriceSize last_trade; public ulong total_volume; public utils.LLDateTime last_trade_time; - private AggressorSide last_side; //// Settlement public UInt64 misc_timestamp; diff --git a/src/client/spark_api/md/MDSource.cs b/src/client/spark_api/md/MDSource.cs index 392fc6844..7281aba04 100644 --- a/src/client/spark_api/md/MDSource.cs +++ b/src/client/spark_api/md/MDSource.cs @@ -1,36 +1,33 @@ using bts.api; -using bts.api.synthetic; using mbts; using mbts.co2; using System; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Threading.Tasks; -using System.Linq; -using SecurityKey = System.UInt32; using System.Collections.Concurrent; -using System.Threading; -using bts.api.user; +using System.Collections.Generic; using System.Reactive.Subjects; +using System.Threading.Tasks; +using SecurityKey = System.UInt32; -namespace bts.md -{ +namespace bts.md { [Flags] enum MDSourceOptions { DeferInit = 1, } class MDSource : IObserver>, IDisposable - { + { + const uint _no_user_id = 0; protected readonly MDConsumer _consumer = new MDConsumer(); protected readonly Co2RelayControl _relay; protected DateTime _version = DateTime.MinValue; protected readonly ConcurrentDictionary _exchange_sec_map = new ConcurrentDictionary(); protected List _dispose = new List(); protected readonly MDSourceOptions _options; + readonly uint _user_id; - public MDSource(uint naggle_ms, MDSourceOptions options = 0) + public MDSource(uint naggle_ms, MDSourceOptions options = 0, uint user_id = _no_user_id) { _options = options; + _user_id = user_id; _relay = new Co2RelayControl(_consumer, naggle_ms); _relay.preload_securities((co2.MDRequest.Types.Flags)0xff).ignore(); _dispose.Add(_consumer.on_security.Subscribe(this)); @@ -54,12 +51,15 @@ namespace bts.md public void OnCompleted() { } public void OnError(Exception error) => Log.Md.error(error.ToString()); - public void OnNext(List values) - { - if (values.Count > 0) - { - foreach (var v in values) - { + public void OnNext(List values) { + if (values.Count > 0) { + foreach (var v in values) { + if (_user_id != _no_user_id && + v.security.ContractCase == Security.ContractOneofCase.Synthetic && + v.security.Synthetic.UserId != _user_id && + v.security.Synthetic.UserId != _no_user_id) { + continue; + } var smd = new SecurityMD(new SecurityImmutable(v.security), v.handle); _exchange_sec_map.AddOrUpdate(smd.security.Id, smd, (_, __) => smd); } @@ -93,4 +93,4 @@ namespace bts.md } } -} \ No newline at end of file +} diff --git a/src/client/spark_excel/margin_data_example.xlsm b/src/client/spark_excel/margin_data_example.xlsm new file mode 100644 index 000000000..58d601e8c Binary files /dev/null and b/src/client/spark_excel/margin_data_example.xlsm differ diff --git a/src/client/spark_excel/rtd/formatting/JsonFormatter.cs b/src/client/spark_excel/rtd/formatting/JsonFormatter.cs index 06dddf859..0d8b658ee 100644 --- a/src/client/spark_excel/rtd/formatting/JsonFormatter.cs +++ b/src/client/spark_excel/rtd/formatting/JsonFormatter.cs @@ -13,6 +13,8 @@ namespace bts.excel.rtd class JsonFormatter : IExcelFormatter { private object _data; + static Google.Protobuf.JsonFormatter _protoformatter = + new Google.Protobuf.JsonFormatter(Google.Protobuf.JsonFormatter.Settings.Default.WithFormatDefaultValues(true)); public JsonFormatter(object data) { _data = data; @@ -81,7 +83,7 @@ namespace bts.excel.rtd { string json = null; if(data is IMessage imsg) - json = Google.Protobuf.JsonFormatter.Default.Format(imsg); + json = _protoformatter.Format(imsg); else json = JsonConvert.SerializeObject(data); diff --git a/src/client/spark_sdk/README.md b/src/client/spark_sdk/README.md index a5367bce8..15e1ed463 100644 --- a/src/client/spark_sdk/README.md +++ b/src/client/spark_sdk/README.md @@ -73,6 +73,10 @@ Commands: order [] Arguments: --pump= + +Example: + To print simulated market data to the screen: + spark_sdk.exe md SIM FUTURE ES SIM:ESU9 --user=bts ``` * Check out sample code diff --git a/src/client/spark_test/System/LimitsTests.cs b/src/client/spark_test/System/LimitsTests.cs index 53465d4d6..158e4af5f 100644 --- a/src/client/spark_test/System/LimitsTests.cs +++ b/src/client/spark_test/System/LimitsTests.cs @@ -169,7 +169,7 @@ namespace bts.tests var second_short_order_def = create_order_def(second_sec.Id, Side.Sell, test_account, test_user); uint short_no_limit_order_key; - + await Task.Delay(10000); Result short_no_limit_order_result = await OrderUtilities.send_order(_order_service, first_short_order_def, 50000, 7); short_no_limit_order_key = short_no_limit_order_result.value.OrderKey; diff --git a/src/lib/CMakeDeps.cmake b/src/lib/CMakeDeps.cmake index bdd16cb35..42887ddaf 100644 --- a/src/lib/CMakeDeps.cmake +++ b/src/lib/CMakeDeps.cmake @@ -1 +1 @@ -set(sources api/apicore.proto;api/apigateway.proto;api/apioptions.proto;api/core/ApiClientInterop.cpp;api/core/ApiServerInterop.cpp;api/public/ApiServer.cpp;api/public/ClientChannel.cpp;api/public/QuickbusClientChannel.cpp;api/public/ServerChannel.cpp;api/public/ServiceContext.cpp;api/service_helpers/AdminServiceUtils.cpp;api/service_helpers/AlgoServiceUtils.cpp;api/services/AdminService.proto;api/services/AlgoDefinitionService.proto;api/services/AlgoService.proto;api/services/ApiUnitTestService.proto;api/services/AuthService.proto;api/services/ErrorReportService.proto;api/services/HelloService.proto;api/services/MBOService.proto;api/services/MarketDataAuxiliaryService.proto;api/services/MarketDataRelay.proto;api/services/OrderService.proto;api/services/OutOfProcessURLListener.proto;api/services/PersistenceService.proto;api/services/PositionService.proto;api/services/ShellDiagnosticService.proto;api/services/ShellLatencyService.proto;api/services/SyntheticSecurityService.proto;api/test/ApiTests.cpp;api/test/HelloServiceServer.cpp;config/ConfigCheck.cpp;config/ConfigDownloader.cpp;config/ConfigErrorCollector.cpp;config/ConfigLog.cpp;config/ConfigPropertyEvaluator.cpp;config/ConfigValueQueue.cpp;config/ConfigurationException.cpp;config/ConfigurationState.cpp;config/PreprocessInputStream.cpp;config/config.cpp;config/defaults/MarketData.conf;config/defaults/Ports.conf;config/defaults/Symbols.conf;config/defaults/cme_reference_data.conf;config/defaults/edge.conf;config/defaults/jetfix.conf;config/defaults/keycode.conf;config/defaults/logging.conf;config/defaults/orderman_cfg.conf;config/defaults/orders_fills_cleanup.conf;config/defaults/spreader.conf;config/defaults/testkeys.conf;config/defaults/threads.conf;config/defaults/toggles.conf;config/extensions/extensions.proto;config/invalid/invalid.proto;config/protos/ExchangeSimCfg.proto;config/protos/MarketData.proto;config/protos/Ports.proto;config/protos/Symbols.proto;config/protos/UI.proto;config/protos/cme_reference_data.proto;config/protos/jetfix.proto;config/protos/keycode.proto;config/protos/logging.proto;config/protos/mercury_cfg.proto;config/protos/orderman_cfg.proto;config/protos/orders_fills_cleanup.proto;config/protos/perflog_cfg.proto;config/protos/quickbus_cfg.proto;config/protos/spreader.proto;config/protos/system.proto;config/protos/testkeys.proto;config/protos/threads.proto;config/protos/toggles.proto;config/tests/HTTPServer.cpp;config/tests/config_ref_tests.cpp;config/tests/config_tests.cpp;config/text_format/text_format.cpp;domain_model/Security.cpp;domain_model/Security.proto;domain_model/SecurityTests.cpp;domain_model/tests/domain_tests.cpp;expression/BooleanGrammar.cpp;expression/ExpressionEvaluator.cpp;expression/ParserExceptions.cpp;expression/Value.cpp;expression/tests/expression_parser_tests.cpp;jetfix/codec/FIXArray.cpp;jetfix/common/OutFIX.cpp;jetfix/connection/AsyncReader.cpp;jetfix/connection/IConnection.cpp;jetfix/connection/SocketConnection.cpp;jetfix/gateway/CoreGateway.cpp;jetfix/gateway/JetFIXGateway.cpp;jetfix/gateway/Session.cpp;jetfix/store/FIXFileStore.cpp;jetfix/store/session_name.cpp;jetfix/tests/async_reader_tests.cpp;jetfix/tests/decoding_tests.cpp;jetfix/tests/fix_encoding_stream_tests.cpp;jetfix/tests/fix_fragment_tests.cpp;jetfix/tests/jetfix_performance_tests.cpp;jetfix/tests/sample_fix.cpp;jetfix/tests/store_tests.cpp;logging/LoggerHub.cpp;logging/Sink.cpp;logging/logging.cpp;logging/tests/logging_tests.cpp;md/Diag/MDPeriodicDumper.cpp;md/Diag/ShmDumper.cpp;md/MBOProcessor.cpp;md/MBOServiceServer.cpp;md/MDClient.cpp;md/MDConsumer.cpp;md/MDConsumerHandle.cpp;md/MDConsumerInterop.cpp;md/MDProducer.cpp;md/MDProducerHandle.cpp;md/MemorySegmentUtils.cpp;md/PlatformLoader.cpp;md/PriceSize.cpp;md/SecurityCache.cpp;md/SecurityDB.cpp;md/SecurityIDRecorder.cpp;md/SecurityOracle.cpp;md/SettlementRecorder.cpp;md/Shm/FixedSecurity.cpp;md/SyntheticSecurityService.cpp;md/cme/MDP3Utils.cpp;md/common.cpp;md/ice/ICEUtils.cpp;md/protos/co2.proto;md/relay/QueryServer.cpp;md/relay/RelayClient.cpp;md/relay/RelayRecorder.cpp;md/test/Co2ManagedTestHelper.cpp;md/test/Co2TestInstrument.cpp;md/test/mbo_tests.cpp;md/test/md2_tests.cpp;md/test/md_test_utils.cpp;md/test/synthetic_book_merge_tests.cpp;mercury/actors/IPollableActor.cpp;mercury/broker/Broker.cpp;mercury/broker/BrokerProxy.cpp;mercury/broker/ILocalBroker.cpp;mercury/broker/IRemoteBroker.cpp;mercury/common/mercury_common.cpp;mercury/events/TimerEvent.cpp;mercury/executor/Executor.cpp;mercury/topics/Topic.cpp;metrics/metrics.cpp;metrics/system_metrics.cpp;orderman/actors/AsyncFactory.cpp;orderman/actors/FillStore.cpp;orderman/actors/GLBXExecutionGateway.cpp;orderman/actors/ICEExecutionGateway.cpp;orderman/actors/IExecutionGateway.cpp;orderman/actors/MDSupplementarySource.cpp;orderman/actors/OrderStore.cpp;orderman/actors/SODPosition.cpp;orderman/actors/SecuritySrc.cpp;orderman/actors/ServiceBridge.cpp;orderman/actors/order_manager/OrderBuilder.cpp;orderman/actors/order_manager/OrderManager.cpp;orderman/actors/order_manager/OrderServices.cpp;orderman/actors/order_manager/OrderServicesData.cpp;orderman/core/IOrderServices.cpp;orderman/events/CancelRejectEvent.cpp;orderman/events/NewExecutionReportEvent.cpp;orderman/limiter/LimitStore.cpp;orderman/limiter/Limiter.cpp;orderman/limiter/ProductLimitNode.cpp;orderman/limiter/ProductLimitTree.cpp;orderman/limiter/SecurityLimitNode.cpp;orderman/limiter/SecurityLimitTree.cpp;orderman/limiter/tests/limit_tests.cpp;orderman/order_factory/DynamicOrderRegistry.cpp;orderman/order_factory/OrderFactory.cpp;orderman/order_router/OrderRouter.cpp;orderman/orderman_utils/fix_order_codec.cpp;orderman/orderman_utils/orderman_utils.cpp;orderman/orders/CompositeOrder.cpp;orderman/orders/DynamicOrder.cpp;orderman/orders/GLBXNativeOrder.cpp;orderman/orders/ICENativeOrder.cpp;orderman/orders/Order.cpp;orderman/orders/TimeBoxOrder.cpp;orderman/orders/TriggerOrder.cpp;orderman/orders/algo/AlgoWrapperOrder.cpp;orderman/orders/algo/IAlgoOrder.cpp;orderman/orders/algo/IAlgoServices.cpp;orderman/orders/algo/IBeforeLaunchAlgoServices.cpp;orderman/orders/algo/algo_orders/CoverAlgo.cpp;orderman/orders/algo/algo_orders/IcebergAlgo.cpp;orderman/orders/algo/algo_orders/JoinAlgo.cpp;orderman/orders/algo/algo_orders/LeanAlgo.cpp;orderman/orders/algo/algo_orders/TopAlgo.cpp;orderman/orders/book_algos/BookAlgo.cpp;orderman/orders/book_algos/BookOrderBase.cpp;orderman/orders/book_algos/OCOOrder.cpp;orderman/orders/book_algos/OffsetOrder.cpp;orderman/orders/book_algos/PricerOrder.cpp;orderman/orders/queue_holder/QueueHolderOrder.cpp;orderman/orders/queue_holder/state_workers/QHOEndStateWorker.cpp;orderman/orders/queue_holder/state_workers/QHOStartStateWorker.cpp;orderman/orders/queue_holder/state_workers/QHOWaitingForAckStateWorker.cpp;orderman/orders/queue_holder/state_workers/QHOWaitingForCancelAllStateWorker.cpp;orderman/orders/queue_holder/state_workers/QHOWorkingStateWorker.cpp;orderman/orders/spreader_orders/SpreaderOrder.cpp;orderman/orders/spreader_orders/spreader/LegFillState.cpp;orderman/orders/spreader_orders/spreader/LegPropertyData.cpp;orderman/orders/spreader_orders/spreader/QuoteLegPriceQuantityState.cpp;orderman/orders/spreader_orders/spreader/SingleFillHedger.cpp;orderman/orders/spreader_orders/spreader/SingleLegQuoter.cpp;orderman/orders/spreader_orders/spreader/SpreaderBookkeepingData.cpp;orderman/orders/spreader_orders/spreader/SpreaderChildOrderState.cpp;orderman/orders/spreader_orders/spreader/SpreaderLegData.cpp;orderman/orders/spreader_orders/spreader/SpreaderUtils.cpp;orderman/orders/spreader_orders/spreader/external_interfacing/SpreaderOrderBuilder.cpp;orderman/orders/spreader_orders/spreader/external_interfacing/SpreaderOrderServices.cpp;orderman/orders/spreader_orders/spreader/external_interfacing/SpreaderPreActivationOrderServices.cpp;orderman/orders/spreader_orders/spreader/hedger/HedgerManager.cpp;orderman/orders/spreader_orders/spreader/hedger/UnhedgedDelta.cpp;orderman/orders/spreader_orders/spreader/hedger/hedger_states/HedgerEndStateWorker.cpp;orderman/orders/spreader_orders/spreader/hedger/hedger_states/HedgerStartStateWorker.cpp;orderman/orders/spreader_orders/spreader/hedger/hedger_states/HedgerWaitingForCancelAckStateWorker.cpp;orderman/orders/spreader_orders/spreader/hedger/hedger_states/HedgerWaitingForFirstAckCancelledStateWorker.cpp;orderman/orders/spreader_orders/spreader/hedger/hedger_states/HedgerWaitingForFirstAckStateWorker.cpp;orderman/orders/spreader_orders/spreader/hedger/hedger_states/HedgerWorkingStateWorker.cpp;orderman/orders/spreader_orders/spreader/quoter/QuoteDriverSet.cpp;orderman/orders/spreader_orders/spreader/quoter/QuoteDriverSetHistory.cpp;orderman/orders/spreader_orders/spreader/quoter/QuoteLegDriver.cpp;orderman/orders/spreader_orders/spreader/quoter/quoter_states/QuoterEndStateWorker.cpp;orderman/orders/spreader_orders/spreader/quoter/quoter_states/QuoterStartStateWorker.cpp;orderman/orders/spreader_orders/spreader/quoter/quoter_states/QuoterWaitingForCancelAckStateWorker.cpp;orderman/orders/spreader_orders/spreader/quoter/quoter_states/QuoterWaitingForFirstAckCancelledWorker.cpp;orderman/orders/spreader_orders/spreader/quoter/quoter_states/QuoterWaitingForFirstAckWorker.cpp;orderman/orders/spreader_orders/spreader/quoter/quoter_states/QuoterWaitingForIOCWorker.cpp;orderman/orders/spreader_orders/spreader/quoter/quoter_states/QuoterWaitingForSizeStateWorker.cpp;orderman/orders/spreader_orders/spreader/quoter/quoter_states/QuoterWorkingStateWorker.cpp;orderman/orders/spreader_orders/spreader_states/SpreaderEndStateWorker.cpp;orderman/orders/spreader_orders/spreader_states/SpreaderStartStateWorker.cpp;orderman/orders/spreader_orders/spreader_states/SpreaderWaitingForCancelAllStateWorker.cpp;orderman/orders/spreader_orders/spreader_states/SpreaderWaitingForInitialDataStateWorker.cpp;orderman/orders/spreader_orders/spreader_states/SpreaderWorkingStateWorker.cpp;orderman/orders/spreader_orders/tests/spreader_tests.cpp;orderman/server/OrderServiceServer.cpp;orderman/tests/fat_wrapper_tests.cpp;os/os.cpp;os/tests/os_tests.cpp;persistence/IStore.cpp;persistence/PersistenceServiceServer.cpp;persistence/SqliteStore.cpp;persistence/persistence_utils.cpp;persistence/tests/PersistenceServiceTests.cpp;plugin/SparkServices.cpp;plugin/plugin.cpp;quickbus/core/LocalNode.cpp;quickbus/core/Quickbus.cpp;quickbus/core/SubscriptionID.cpp;quickbus/core/TableID.cpp;quickbus/message_model/Update.cpp;quickbus/message_model/merge_updates.cpp;quickbus/network/QBConnection.cpp;quickbus/network/QBConnectionManager.cpp;quickbus/network/endpoint_description.cpp;quickbus/network/quickbus_msgs.proto;quickbus/pubsub/TablePublisher.cpp;quickbus/pubsub/TableSubscriber.cpp;quickbus/pubsub/tests/qb_pubsub_tests.cpp;quickbus/routing/INode.cpp;quickbus/routing/NodeID.cpp;quickbus/routing/Router.cpp;quickbus/rpc/RPCClient.cpp;quickbus/rpc/RPCServer.cpp;quickbus/rpc/RPCSessionID.cpp;quickbus/rpc/rpc_utils.cpp;quickbus/rpc/tests/qb_rpc_tests.cpp;quickbus/tests/quickbus_tests.cpp;sparkproto/SparkProtos.proto;testing/catch_test_runner.cpp;tracing/PerfLog.cpp;tracing/bts_trace.cpp;utils/AuthSessionManager.cpp;utils/AuthToken.cpp;utils/Dispatcher.cpp;utils/GrowableSPSCQueue.cpp;utils/HTTPStream.cpp;utils/PBUtils.cpp;utils/Puid.cpp;utils/SecureLogon.cpp;utils/SharedMemory.cpp;utils/Sqlite_utils.cpp;utils/Uuid.cpp;utils/crypto/CipherType.cpp;utils/crypto/CryptoInit.cpp;utils/crypto/Digest.cpp;utils/crypto/KeyCodeHelper.cpp;utils/crypto/PasswordCallback.cpp;utils/crypto/PrivateKey.cpp;utils/crypto/PublicKey.cpp;utils/crypto/iostreambio.cpp;utils/datetime.cpp;utils/download.cpp;utils/file_utils.cpp;utils/init_spark_lib.cpp;utils/is_unittest_environment.cpp;utils/stats.cpp;utils/tests/AuthTokenTests.cpp;utils/tests/IDGeneratorTests.cpp;utils/tests/SingleWriterConcurrentVectorTests.cpp;utils/thread_manager.cpp) \ No newline at end of file +set(sources api/apicore.proto;api/apigateway.proto;api/apioptions.proto;api/core/ApiClientInterop.cpp;api/core/ApiServerInterop.cpp;api/public/ApiServer.cpp;api/public/ClientChannel.cpp;api/public/QuickbusClientChannel.cpp;api/public/ServerChannel.cpp;api/public/ServiceContext.cpp;api/service_helpers/AdminServiceUtils.cpp;api/service_helpers/AlgoServiceUtils.cpp;api/services/AdminService.proto;api/services/AlgoDefinitionService.proto;api/services/AlgoService.proto;api/services/ApiUnitTestService.proto;api/services/AuthService.proto;api/services/ErrorReportService.proto;api/services/HelloService.proto;api/services/MBOService.proto;api/services/MarginService.proto;api/services/MarketDataAuxiliaryService.proto;api/services/MarketDataRelay.proto;api/services/OrderService.proto;api/services/OutOfProcessURLListener.proto;api/services/PersistenceService.proto;api/services/PositionService.proto;api/services/ShellDiagnosticService.proto;api/services/ShellLatencyService.proto;api/services/SyntheticSecurityService.proto;api/test/ApiTests.cpp;api/test/HelloServiceServer.cpp;config/ConfigCheck.cpp;config/ConfigDownloader.cpp;config/ConfigErrorCollector.cpp;config/ConfigLog.cpp;config/ConfigPropertyEvaluator.cpp;config/ConfigValueQueue.cpp;config/ConfigurationException.cpp;config/ConfigurationState.cpp;config/PreprocessInputStream.cpp;config/config.cpp;config/defaults/MarketData.conf;config/defaults/Ports.conf;config/defaults/Symbols.conf;config/defaults/cme_reference_data.conf;config/defaults/edge.conf;config/defaults/jetfix.conf;config/defaults/keycode.conf;config/defaults/logging.conf;config/defaults/orderman_cfg.conf;config/defaults/orders_fills_cleanup.conf;config/defaults/span.conf;config/defaults/spreader.conf;config/defaults/testkeys.conf;config/defaults/threads.conf;config/defaults/toggles.conf;config/extensions/extensions.proto;config/invalid/invalid.proto;config/protos/ExchangeSimCfg.proto;config/protos/MarketData.proto;config/protos/Ports.proto;config/protos/Symbols.proto;config/protos/UI.proto;config/protos/cme_reference_data.proto;config/protos/jetfix.proto;config/protos/keycode.proto;config/protos/logging.proto;config/protos/mercury_cfg.proto;config/protos/orderman_cfg.proto;config/protos/orders_fills_cleanup.proto;config/protos/perflog_cfg.proto;config/protos/quickbus_cfg.proto;config/protos/span.proto;config/protos/spreader.proto;config/protos/system.proto;config/protos/testkeys.proto;config/protos/threads.proto;config/protos/toggles.proto;config/tests/HTTPServer.cpp;config/tests/config_ref_tests.cpp;config/tests/config_tests.cpp;config/text_format/text_format.cpp;domain_model/Security.cpp;domain_model/Security.proto;domain_model/SecurityTests.cpp;domain_model/tests/domain_tests.cpp;expression/BooleanGrammar.cpp;expression/ExpressionEvaluator.cpp;expression/ParserExceptions.cpp;expression/Value.cpp;expression/tests/expression_parser_tests.cpp;jetfix/codec/FIXArray.cpp;jetfix/common/OutFIX.cpp;jetfix/connection/AsyncReader.cpp;jetfix/connection/IConnection.cpp;jetfix/connection/SocketConnection.cpp;jetfix/gateway/CoreGateway.cpp;jetfix/gateway/JetFIXGateway.cpp;jetfix/gateway/Session.cpp;jetfix/store/FIXFileStore.cpp;jetfix/store/session_name.cpp;jetfix/tests/async_reader_tests.cpp;jetfix/tests/decoding_tests.cpp;jetfix/tests/fix_encoding_stream_tests.cpp;jetfix/tests/fix_fragment_tests.cpp;jetfix/tests/jetfix_performance_tests.cpp;jetfix/tests/sample_fix.cpp;jetfix/tests/store_tests.cpp;logging/LoggerHub.cpp;logging/Sink.cpp;logging/logging.cpp;logging/tests/logging_tests.cpp;md/Diag/MDPeriodicDumper.cpp;md/Diag/ShmDumper.cpp;md/MBOProcessor.cpp;md/MBOServiceServer.cpp;md/MDClient.cpp;md/MDConsumer.cpp;md/MDConsumerHandle.cpp;md/MDConsumerInterop.cpp;md/MDProducer.cpp;md/MDProducerHandle.cpp;md/MemorySegmentUtils.cpp;md/PlatformLoader.cpp;md/PriceSize.cpp;md/SecurityCache.cpp;md/SecurityDB.cpp;md/SecurityIDRecorder.cpp;md/SecurityOracle.cpp;md/SettlementRecorder.cpp;md/Shm/FixedSecurity.cpp;md/SyntheticSecurityService.cpp;md/cme/MDP3Utils.cpp;md/common.cpp;md/ice/ICEUtils.cpp;md/protos/co2.proto;md/relay/QueryServer.cpp;md/relay/RelayClient.cpp;md/relay/RelayRecorder.cpp;md/test/Co2ManagedTestHelper.cpp;md/test/Co2TestInstrument.cpp;md/test/mbo_tests.cpp;md/test/md2_tests.cpp;md/test/md_test_utils.cpp;md/test/synthetic_book_merge_tests.cpp;mercury/actors/IPollableActor.cpp;mercury/broker/Broker.cpp;mercury/broker/BrokerProxy.cpp;mercury/broker/ILocalBroker.cpp;mercury/broker/IRemoteBroker.cpp;mercury/common/mercury_common.cpp;mercury/events/TimerEvent.cpp;mercury/executor/Executor.cpp;mercury/topics/Topic.cpp;metrics/metrics.cpp;metrics/system_metrics.cpp;orderman/actors/AsyncFactory.cpp;orderman/actors/ExecutionGateway.cpp;orderman/actors/FillStore.cpp;orderman/actors/MDSupplementarySource.cpp;orderman/actors/OrderStore.cpp;orderman/actors/SODPosition.cpp;orderman/actors/SecuritySrc.cpp;orderman/actors/ServiceBridge.cpp;orderman/actors/order_manager/OrderBuilder.cpp;orderman/actors/order_manager/OrderManager.cpp;orderman/actors/order_manager/OrderServices.cpp;orderman/actors/order_manager/OrderServicesData.cpp;orderman/core/IOrderServices.cpp;orderman/events/CancelRejectEvent.cpp;orderman/events/NewExecutionReportEvent.cpp;orderman/limiter/LimitStore.cpp;orderman/limiter/Limiter.cpp;orderman/limiter/ProductLimitNode.cpp;orderman/limiter/ProductLimitTree.cpp;orderman/limiter/SecurityLimitNode.cpp;orderman/limiter/SecurityLimitTree.cpp;orderman/limiter/tests/limit_tests.cpp;orderman/order_factory/DynamicOrderRegistry.cpp;orderman/order_factory/OrderFactory.cpp;orderman/order_router/OrderRouter.cpp;orderman/orderman_utils/fix_order_codec.cpp;orderman/orderman_utils/orderman_utils.cpp;orderman/orders/CompositeOrder.cpp;orderman/orders/DynamicOrder.cpp;orderman/orders/NativeOrder.cpp;orderman/orders/Order.cpp;orderman/orders/TimeBoxOrder.cpp;orderman/orders/TriggerOrder.cpp;orderman/orders/algo/AlgoWrapperOrder.cpp;orderman/orders/algo/IAlgoOrder.cpp;orderman/orders/algo/IAlgoServices.cpp;orderman/orders/algo/IBeforeLaunchAlgoServices.cpp;orderman/orders/algo/algo_orders/CoverAlgo.cpp;orderman/orders/algo/algo_orders/DormanAlgo.cpp;orderman/orders/algo/algo_orders/IcebergAlgo.cpp;orderman/orders/algo/algo_orders/JoinAlgo.cpp;orderman/orders/algo/algo_orders/LeanAlgo.cpp;orderman/orders/algo/algo_orders/OffsetAlgo.cpp;orderman/orders/algo/algo_orders/RegisterDorman.cpp;orderman/orders/algo/algo_orders/SplitterAlgo.cpp;orderman/orders/algo/algo_orders/TopAlgo.cpp;orderman/orders/algo/algo_orders/tests/AlgoTestDriver.cpp;orderman/orders/algo/algo_orders/tests/dorman_tests.cpp;orderman/orders/algo/algo_orders/tests/splitter_tests.cpp;orderman/orders/book_algos/BookAlgo.cpp;orderman/orders/book_algos/BookOrderBase.cpp;orderman/orders/book_algos/OCOOrder.cpp;orderman/orders/book_algos/PricerOrder.cpp;orderman/orders/queue_holder/QueueHolderOrder.cpp;orderman/orders/queue_holder/state_workers/QHOEndStateWorker.cpp;orderman/orders/queue_holder/state_workers/QHOStartStateWorker.cpp;orderman/orders/queue_holder/state_workers/QHOWaitingForAckStateWorker.cpp;orderman/orders/queue_holder/state_workers/QHOWaitingForCancelAllStateWorker.cpp;orderman/orders/queue_holder/state_workers/QHOWorkingStateWorker.cpp;orderman/orders/spreader_orders/SpreaderOrder.cpp;orderman/orders/spreader_orders/spreader/LegFillState.cpp;orderman/orders/spreader_orders/spreader/LegPropertyData.cpp;orderman/orders/spreader_orders/spreader/QuoteLegPriceQuantityState.cpp;orderman/orders/spreader_orders/spreader/SingleFillHedger.cpp;orderman/orders/spreader_orders/spreader/SingleLegQuoter.cpp;orderman/orders/spreader_orders/spreader/SpreaderBookkeepingData.cpp;orderman/orders/spreader_orders/spreader/SpreaderChildOrderState.cpp;orderman/orders/spreader_orders/spreader/SpreaderLegData.cpp;orderman/orders/spreader_orders/spreader/SpreaderUtils.cpp;orderman/orders/spreader_orders/spreader/external_interfacing/SpreaderOrderBuilder.cpp;orderman/orders/spreader_orders/spreader/external_interfacing/SpreaderOrderServices.cpp;orderman/orders/spreader_orders/spreader/external_interfacing/SpreaderPreActivationOrderServices.cpp;orderman/orders/spreader_orders/spreader/hedger/HedgerManager.cpp;orderman/orders/spreader_orders/spreader/hedger/UnhedgedDelta.cpp;orderman/orders/spreader_orders/spreader/hedger/hedger_states/HedgerEndStateWorker.cpp;orderman/orders/spreader_orders/spreader/hedger/hedger_states/HedgerStartStateWorker.cpp;orderman/orders/spreader_orders/spreader/hedger/hedger_states/HedgerWaitingForCancelAckStateWorker.cpp;orderman/orders/spreader_orders/spreader/hedger/hedger_states/HedgerWaitingForFirstAckCancelledStateWorker.cpp;orderman/orders/spreader_orders/spreader/hedger/hedger_states/HedgerWaitingForFirstAckStateWorker.cpp;orderman/orders/spreader_orders/spreader/hedger/hedger_states/HedgerWorkingStateWorker.cpp;orderman/orders/spreader_orders/spreader/quoter/QuoteDriverSet.cpp;orderman/orders/spreader_orders/spreader/quoter/QuoteDriverSetHistory.cpp;orderman/orders/spreader_orders/spreader/quoter/QuoteLegDriver.cpp;orderman/orders/spreader_orders/spreader/quoter/quoter_states/QuoterEndStateWorker.cpp;orderman/orders/spreader_orders/spreader/quoter/quoter_states/QuoterStartStateWorker.cpp;orderman/orders/spreader_orders/spreader/quoter/quoter_states/QuoterWaitingForCancelAckStateWorker.cpp;orderman/orders/spreader_orders/spreader/quoter/quoter_states/QuoterWaitingForFirstAckCancelledWorker.cpp;orderman/orders/spreader_orders/spreader/quoter/quoter_states/QuoterWaitingForFirstAckWorker.cpp;orderman/orders/spreader_orders/spreader/quoter/quoter_states/QuoterWaitingForIOCWorker.cpp;orderman/orders/spreader_orders/spreader/quoter/quoter_states/QuoterWaitingForSizeStateWorker.cpp;orderman/orders/spreader_orders/spreader/quoter/quoter_states/QuoterWorkingStateWorker.cpp;orderman/orders/spreader_orders/spreader_states/SpreaderEndStateWorker.cpp;orderman/orders/spreader_orders/spreader_states/SpreaderStartStateWorker.cpp;orderman/orders/spreader_orders/spreader_states/SpreaderWaitingForCancelAllStateWorker.cpp;orderman/orders/spreader_orders/spreader_states/SpreaderWaitingForInitialDataStateWorker.cpp;orderman/orders/spreader_orders/spreader_states/SpreaderWorkingStateWorker.cpp;orderman/orders/spreader_orders/tests/spreader_tests.cpp;orderman/server/OrderServiceServer.cpp;orderman/tests/fat_wrapper_tests.cpp;os/os.cpp;os/tests/os_tests.cpp;persistence/IStore.cpp;persistence/PersistenceServiceServer.cpp;persistence/SqliteStore.cpp;persistence/persistence_utils.cpp;persistence/tests/PersistenceServiceTests.cpp;plugin/SparkServices.cpp;plugin/plugin.cpp;quickbus/core/LocalNode.cpp;quickbus/core/Quickbus.cpp;quickbus/core/SubscriptionID.cpp;quickbus/core/TableID.cpp;quickbus/message_model/Update.cpp;quickbus/message_model/merge_updates.cpp;quickbus/network/QBConnection.cpp;quickbus/network/QBConnectionManager.cpp;quickbus/network/endpoint_description.cpp;quickbus/network/quickbus_msgs.proto;quickbus/pubsub/TablePublisher.cpp;quickbus/pubsub/TableSubscriber.cpp;quickbus/pubsub/tests/qb_pubsub_tests.cpp;quickbus/routing/INode.cpp;quickbus/routing/NodeID.cpp;quickbus/routing/Router.cpp;quickbus/rpc/RPCClient.cpp;quickbus/rpc/RPCServer.cpp;quickbus/rpc/RPCSessionID.cpp;quickbus/rpc/rpc_utils.cpp;quickbus/rpc/tests/qb_rpc_tests.cpp;quickbus/tests/quickbus_tests.cpp;sparkproto/SparkProtos.proto;testing/catch_test_runner.cpp;tracing/PerfLog.cpp;tracing/bts_trace.cpp;utils/AuthSessionManager.cpp;utils/AuthToken.cpp;utils/Dispatcher.cpp;utils/GrowableSPSCQueue.cpp;utils/HTTPStream.cpp;utils/PBUtils.cpp;utils/Puid.cpp;utils/SecureLogon.cpp;utils/SharedMemory.cpp;utils/Sqlite_utils.cpp;utils/Uuid.cpp;utils/crypto/CipherType.cpp;utils/crypto/CryptoInit.cpp;utils/crypto/Digest.cpp;utils/crypto/KeyCodeHelper.cpp;utils/crypto/PasswordCallback.cpp;utils/crypto/PrivateKey.cpp;utils/crypto/PublicKey.cpp;utils/crypto/iostreambio.cpp;utils/datetime.cpp;utils/download.cpp;utils/file_utils.cpp;utils/init_spark_lib.cpp;utils/is_unittest_environment.cpp;utils/stats.cpp;utils/tests/AuthTokenTests.cpp;utils/tests/SingleWriterConcurrentVectorTests.cpp;utils/thread_manager.cpp) \ No newline at end of file diff --git a/src/lib/CMakeDeps_win.cmake b/src/lib/CMakeDeps_win.cmake index cf0fdd4ec..9d75f6455 100644 --- a/src/lib/CMakeDeps_win.cmake +++ b/src/lib/CMakeDeps_win.cmake @@ -1 +1 @@ -set(sources api/apicore.proto;api/apigateway.proto;api/apioptions.proto;api/core/ApiClientInterop.cpp;api/core/ApiServerInterop.cpp;api/public/ApiServer.cpp;api/public/ClientChannel.cpp;api/public/QuickbusClientChannel.cpp;api/public/ServerChannel.cpp;api/public/ServiceContext.cpp;api/service_helpers/AdminServiceUtils.cpp;api/service_helpers/AlgoServiceUtils.cpp;api/services/AdminService.proto;api/services/AlgoDefinitionService.proto;api/services/AlgoService.proto;api/services/ApiUnitTestService.proto;api/services/AuthService.proto;api/services/ErrorReportService.proto;api/services/HelloService.proto;api/services/MBOService.proto;api/services/MarketDataAuxiliaryService.proto;api/services/MarketDataRelay.proto;api/services/OrderService.proto;api/services/OutOfProcessURLListener.proto;api/services/PersistenceService.proto;api/services/PositionService.proto;api/services/ShellDiagnosticService.proto;api/services/ShellLatencyService.proto;api/services/SyntheticSecurityService.proto;api/test/ApiTests.cpp;api/test/HelloServiceServer.cpp;config/ConfigCheck.cpp;config/ConfigDownloader.cpp;config/ConfigErrorCollector.cpp;config/ConfigLog.cpp;config/ConfigPropertyEvaluator.cpp;config/ConfigValueQueue.cpp;config/ConfigurationException.cpp;config/ConfigurationState.cpp;config/PreprocessInputStream.cpp;config/config.cpp;config/defaults/MarketData.conf;config/defaults/Ports.conf;config/defaults/Symbols.conf;config/defaults/cme_reference_data.conf;config/defaults/edge.conf;config/defaults/jetfix.conf;config/defaults/keycode.conf;config/defaults/logging.conf;config/defaults/orderman_cfg.conf;config/defaults/orders_fills_cleanup.conf;config/defaults/spreader.conf;config/defaults/testkeys.conf;config/defaults/threads.conf;config/defaults/toggles.conf;config/extensions/extensions.proto;config/invalid/invalid.proto;config/protos/ExchangeSimCfg.proto;config/protos/MarketData.proto;config/protos/Ports.proto;config/protos/Symbols.proto;config/protos/UI.proto;config/protos/cme_reference_data.proto;config/protos/jetfix.proto;config/protos/keycode.proto;config/protos/logging.proto;config/protos/mercury_cfg.proto;config/protos/orderman_cfg.proto;config/protos/orders_fills_cleanup.proto;config/protos/perflog_cfg.proto;config/protos/quickbus_cfg.proto;config/protos/spreader.proto;config/protos/system.proto;config/protos/testkeys.proto;config/protos/threads.proto;config/protos/toggles.proto;config/tests/HTTPServer.cpp;config/tests/config_ref_tests.cpp;config/tests/config_tests.cpp;config/text_format/text_format.cpp;domain_model/Security.cpp;domain_model/Security.proto;domain_model/SecurityTests.cpp;domain_model/tests/domain_tests.cpp;expression/BooleanGrammar.cpp;expression/ExpressionEvaluator.cpp;expression/ParserExceptions.cpp;expression/Value.cpp;expression/tests/expression_parser_tests.cpp;logging/LoggerHub.cpp;logging/Sink.cpp;logging/logging.cpp;logging/tests/logging_tests.cpp;md/Diag/MDPeriodicDumper.cpp;md/Diag/ShmDumper.cpp;md/MBOProcessor.cpp;md/MBOServiceServer.cpp;md/MDClient.cpp;md/MDConsumer.cpp;md/MDConsumerHandle.cpp;md/MDConsumerInterop.cpp;md/MDProducer.cpp;md/MDProducerHandle.cpp;md/MemorySegmentUtils.cpp;md/PlatformLoader.cpp;md/PriceSize.cpp;md/SecurityCache.cpp;md/SecurityDB.cpp;md/SecurityIDRecorder.cpp;md/SecurityOracle.cpp;md/SettlementRecorder.cpp;md/Shm/FixedSecurity.cpp;md/SyntheticSecurityService.cpp;md/cme/MDP3Utils.cpp;md/common.cpp;md/ice/ICEUtils.cpp;md/protos/co2.proto;md/relay/QueryServer.cpp;md/relay/RelayClient.cpp;md/relay/RelayRecorder.cpp;md/test/Co2ManagedTestHelper.cpp;md/test/Co2TestInstrument.cpp;md/test/mbo_tests.cpp;md/test/md2_tests.cpp;md/test/md_test_utils.cpp;md/test/synthetic_book_merge_tests.cpp;os/os.cpp;os/tests/os_tests.cpp;quickbus/core/LocalNode.cpp;quickbus/core/Quickbus.cpp;quickbus/core/SubscriptionID.cpp;quickbus/core/TableID.cpp;quickbus/message_model/Update.cpp;quickbus/message_model/merge_updates.cpp;quickbus/network/QBConnection.cpp;quickbus/network/QBConnectionManager.cpp;quickbus/network/endpoint_description.cpp;quickbus/network/quickbus_msgs.proto;quickbus/pubsub/TablePublisher.cpp;quickbus/pubsub/TableSubscriber.cpp;quickbus/pubsub/tests/qb_pubsub_tests.cpp;quickbus/routing/INode.cpp;quickbus/routing/NodeID.cpp;quickbus/routing/Router.cpp;quickbus/rpc/RPCClient.cpp;quickbus/rpc/RPCServer.cpp;quickbus/rpc/RPCSessionID.cpp;quickbus/rpc/rpc_utils.cpp;quickbus/rpc/tests/qb_rpc_tests.cpp;quickbus/tests/quickbus_tests.cpp;sparkproto/SparkProtos.proto;testing/catch_test_runner.cpp;utils/AuthSessionManager.cpp;utils/AuthToken.cpp;utils/Dispatcher.cpp;utils/GrowableSPSCQueue.cpp;utils/HTTPStream.cpp;utils/PBUtils.cpp;utils/Puid.cpp;utils/SecureLogon.cpp;utils/SharedMemory.cpp;utils/Sqlite_utils.cpp;utils/Uuid.cpp;utils/crypto/CipherType.cpp;utils/crypto/CryptoInit.cpp;utils/crypto/Digest.cpp;utils/crypto/KeyCodeHelper.cpp;utils/crypto/PasswordCallback.cpp;utils/crypto/PrivateKey.cpp;utils/crypto/PublicKey.cpp;utils/crypto/iostreambio.cpp;utils/datetime.cpp;utils/download.cpp;utils/file_utils.cpp;utils/init_spark_lib.cpp;utils/is_unittest_environment.cpp;utils/stats.cpp;utils/tests/AuthTokenTests.cpp;utils/tests/IDGeneratorTests.cpp;utils/tests/SingleWriterConcurrentVectorTests.cpp;utils/thread_manager.cpp) \ No newline at end of file +set(sources api/apicore.proto;api/apigateway.proto;api/apioptions.proto;api/core/ApiClientInterop.cpp;api/core/ApiServerInterop.cpp;api/public/ApiServer.cpp;api/public/ClientChannel.cpp;api/public/QuickbusClientChannel.cpp;api/public/ServerChannel.cpp;api/public/ServiceContext.cpp;api/service_helpers/AdminServiceUtils.cpp;api/service_helpers/AlgoServiceUtils.cpp;api/services/AdminService.proto;api/services/AlgoDefinitionService.proto;api/services/AlgoService.proto;api/services/ApiUnitTestService.proto;api/services/AuthService.proto;api/services/ErrorReportService.proto;api/services/HelloService.proto;api/services/MBOService.proto;api/services/MarginService.proto;api/services/MarketDataAuxiliaryService.proto;api/services/MarketDataRelay.proto;api/services/OrderService.proto;api/services/OutOfProcessURLListener.proto;api/services/PersistenceService.proto;api/services/PositionService.proto;api/services/ShellDiagnosticService.proto;api/services/ShellLatencyService.proto;api/services/SyntheticSecurityService.proto;api/test/ApiTests.cpp;api/test/HelloServiceServer.cpp;config/ConfigCheck.cpp;config/ConfigDownloader.cpp;config/ConfigErrorCollector.cpp;config/ConfigLog.cpp;config/ConfigPropertyEvaluator.cpp;config/ConfigValueQueue.cpp;config/ConfigurationException.cpp;config/ConfigurationState.cpp;config/PreprocessInputStream.cpp;config/config.cpp;config/defaults/MarketData.conf;config/defaults/Ports.conf;config/defaults/Symbols.conf;config/defaults/cme_reference_data.conf;config/defaults/edge.conf;config/defaults/jetfix.conf;config/defaults/keycode.conf;config/defaults/logging.conf;config/defaults/orderman_cfg.conf;config/defaults/orders_fills_cleanup.conf;config/defaults/span.conf;config/defaults/spreader.conf;config/defaults/testkeys.conf;config/defaults/threads.conf;config/defaults/toggles.conf;config/extensions/extensions.proto;config/invalid/invalid.proto;config/protos/ExchangeSimCfg.proto;config/protos/MarketData.proto;config/protos/Ports.proto;config/protos/Symbols.proto;config/protos/UI.proto;config/protos/cme_reference_data.proto;config/protos/jetfix.proto;config/protos/keycode.proto;config/protos/logging.proto;config/protos/mercury_cfg.proto;config/protos/orderman_cfg.proto;config/protos/orders_fills_cleanup.proto;config/protos/perflog_cfg.proto;config/protos/quickbus_cfg.proto;config/protos/span.proto;config/protos/spreader.proto;config/protos/system.proto;config/protos/testkeys.proto;config/protos/threads.proto;config/protos/toggles.proto;config/tests/HTTPServer.cpp;config/tests/config_ref_tests.cpp;config/tests/config_tests.cpp;config/text_format/text_format.cpp;domain_model/Security.cpp;domain_model/Security.proto;domain_model/SecurityTests.cpp;domain_model/tests/domain_tests.cpp;expression/BooleanGrammar.cpp;expression/ExpressionEvaluator.cpp;expression/ParserExceptions.cpp;expression/Value.cpp;expression/tests/expression_parser_tests.cpp;logging/LoggerHub.cpp;logging/Sink.cpp;logging/logging.cpp;logging/tests/logging_tests.cpp;md/Diag/MDPeriodicDumper.cpp;md/Diag/ShmDumper.cpp;md/MBOProcessor.cpp;md/MBOServiceServer.cpp;md/MDClient.cpp;md/MDConsumer.cpp;md/MDConsumerHandle.cpp;md/MDConsumerInterop.cpp;md/MDProducer.cpp;md/MDProducerHandle.cpp;md/MemorySegmentUtils.cpp;md/PlatformLoader.cpp;md/PriceSize.cpp;md/SecurityCache.cpp;md/SecurityDB.cpp;md/SecurityIDRecorder.cpp;md/SecurityOracle.cpp;md/SettlementRecorder.cpp;md/Shm/FixedSecurity.cpp;md/SyntheticSecurityService.cpp;md/cme/MDP3Utils.cpp;md/common.cpp;md/ice/ICEUtils.cpp;md/protos/co2.proto;md/relay/QueryServer.cpp;md/relay/RelayClient.cpp;md/relay/RelayRecorder.cpp;md/test/Co2ManagedTestHelper.cpp;md/test/Co2TestInstrument.cpp;md/test/mbo_tests.cpp;md/test/md2_tests.cpp;md/test/md_test_utils.cpp;md/test/synthetic_book_merge_tests.cpp;os/os.cpp;os/tests/os_tests.cpp;quickbus/core/LocalNode.cpp;quickbus/core/Quickbus.cpp;quickbus/core/SubscriptionID.cpp;quickbus/core/TableID.cpp;quickbus/message_model/Update.cpp;quickbus/message_model/merge_updates.cpp;quickbus/network/QBConnection.cpp;quickbus/network/QBConnectionManager.cpp;quickbus/network/endpoint_description.cpp;quickbus/network/quickbus_msgs.proto;quickbus/pubsub/TablePublisher.cpp;quickbus/pubsub/TableSubscriber.cpp;quickbus/pubsub/tests/qb_pubsub_tests.cpp;quickbus/routing/INode.cpp;quickbus/routing/NodeID.cpp;quickbus/routing/Router.cpp;quickbus/rpc/RPCClient.cpp;quickbus/rpc/RPCServer.cpp;quickbus/rpc/RPCSessionID.cpp;quickbus/rpc/rpc_utils.cpp;quickbus/rpc/tests/qb_rpc_tests.cpp;quickbus/tests/quickbus_tests.cpp;sparkproto/SparkProtos.proto;testing/catch_test_runner.cpp;utils/AuthSessionManager.cpp;utils/AuthToken.cpp;utils/Dispatcher.cpp;utils/GrowableSPSCQueue.cpp;utils/HTTPStream.cpp;utils/PBUtils.cpp;utils/Puid.cpp;utils/SecureLogon.cpp;utils/SharedMemory.cpp;utils/Sqlite_utils.cpp;utils/Uuid.cpp;utils/crypto/CipherType.cpp;utils/crypto/CryptoInit.cpp;utils/crypto/Digest.cpp;utils/crypto/KeyCodeHelper.cpp;utils/crypto/PasswordCallback.cpp;utils/crypto/PrivateKey.cpp;utils/crypto/PublicKey.cpp;utils/crypto/iostreambio.cpp;utils/datetime.cpp;utils/download.cpp;utils/file_utils.cpp;utils/init_spark_lib.cpp;utils/is_unittest_environment.cpp;utils/stats.cpp;utils/tests/AuthTokenTests.cpp;utils/tests/IDGeneratorTests.cpp;utils/tests/SingleWriterConcurrentVectorTests.cpp;utils/thread_manager.cpp) \ No newline at end of file diff --git a/src/lib/api/apicore.proto b/src/lib/api/apicore.proto index 4da2a780d..6de2eab0d 100644 --- a/src/lib/api/apicore.proto +++ b/src/lib/api/apicore.proto @@ -50,6 +50,7 @@ message VoidMessage {} message BytesMessage { bytes buffer = 1; } -message BoolResultMessage { - bool success = 1; +message ResultMessage { + bool success = 1; + string error_message = 2; } diff --git a/src/lib/api/public/Update.h b/src/lib/api/public/Update.h index bc2885e2f..3d18c5d6d 100644 --- a/src/lib/api/public/Update.h +++ b/src/lib/api/public/Update.h @@ -27,7 +27,12 @@ namespace api { _removed_items(std::move(removed)) {} - Update(const Update& other) = delete; + Update(const Update& other) : _all_items(other._all_items), + _added_items(other._added_items), + _changed_items(other._changed_items), + _removed_items(other._removed_items) + {} + Update(Update&& other) : _all_items(std::move(other._all_items)), _added_items(std::move(other._added_items)), @@ -104,4 +109,4 @@ namespace api { } -}}//namespace \ No newline at end of file +}}//namespace diff --git a/src/lib/api/services/AdminService.proto b/src/lib/api/services/AdminService.proto index 911ab2d2b..994937757 100644 --- a/src/lib/api/services/AdminService.proto +++ b/src/lib/api/services/AdminService.proto @@ -151,6 +151,7 @@ message BTSAccount{ ExchangeAccountData exchange_account_data = 110; // extra parameters for risk enforcement RiskLimitEnforcement risk_limit_enforcement=120; + AccountLossLimit account_loss_limit = 130; } message RiskLimitEnforcement{ @@ -192,6 +193,11 @@ message AccountExchangeConnections{ repeated string exchange_connection_names = 20; } +message AccountLossLimit{ + bool enforce = 10; + uint32 total_loss_limit = 20; + // if we need per product we can add it here +} /************************************************************************/ /***************************Exchange Data********************************/ /************************************************************************/ diff --git a/src/lib/api/services/MarginService.proto b/src/lib/api/services/MarginService.proto new file mode 100644 index 000000000..abe7a343b --- /dev/null +++ b/src/lib/api/services/MarginService.proto @@ -0,0 +1,67 @@ +syntax="proto3"; +package bts.api.position; +option csharp_namespace="bts.api.position"; +import "lib/api/apioptions.proto"; +import "lib/api/apicore.proto"; +import "lib/api/services/PositionService.proto"; + +message MarginResult { + enum ResultStatus { NewValid = 0; CachedValid = 1; Error = 2; } + + string account = 10 [(bts.api.key)=true]; + ResultStatus status = 12; + string portfolio_id = 15; + string settle_indicator = 20; + string settle_qualifier = 30; + string as_of_time = 40; + string calculation_id = 50; + + double opt_value = 100; + double nonopt_value = 110; + double maint_margin = 120; + double initial_margin = 130; +} + +message ExcessLiquidityReport { + string account = 10 [(bts.api.key)=true]; + uint32 account_id = 11; + double leverage_factor = 20; + double cash_balance = 30; + MarginResult margin_result = 40; + AccountPNL account_pnl = 50; + bool should_enforce_margin = 60; + double excess_liquidity = 100; +} + + +message MarginRequest { + string account = 10; +} + +message CashFactorUpdate { + string account = 10; + double leverage_factor = 20; +} + +message CashBalanceUpdate { + string account = 10; + double cash_balance = 20; +} + +message EnforceUpdate { + string account = 10; + bool should_enforce_margin = 20; +} + +service MarginService { + option (bts.api.service_description) = "Provides CME Margin Information per account"; + + rpc get_margin(MarginRequest) returns (MarginResult); + rpc get_excess_liquidity_report(MarginRequest) returns (ExcessLiquidityReport); + rpc set_should_enforce_margin(EnforceUpdate) returns (bts.api.ResultMessage); + rpc set_cash_factor(CashFactorUpdate) returns (bts.api.ResultMessage); + rpc set_cash_balance(CashBalanceUpdate) returns (bts.api.ResultMessage); + + rpc margin_data(bts.api.VoidMessage) returns (stream MarginResult); + rpc excess_liquidity_report(bts.api.VoidMessage) returns (stream ExcessLiquidityReport); +} \ No newline at end of file diff --git a/src/lib/api/services/OrderService.proto b/src/lib/api/services/OrderService.proto index 572584e12..babe98533 100644 --- a/src/lib/api/services/OrderService.proto +++ b/src/lib/api/services/OrderService.proto @@ -101,6 +101,7 @@ message CustomOrderDefinition OCOOrderDefinition oco = 25; PricerOrderDefinition pricer = 26; CompositeOrderDefinition composite = 27; + SplitterOrderDefinition splitter = 29; DynamicOrderDefinition dynamic = 1000; } @@ -160,8 +161,8 @@ message JoinOrderDefinition { CustomOrderDefinition child = 6; } message TopOrderDefinition { - uint32 watch_level_tick_offset = 1; spark.QuantityMeasure top_threshold = 2; + uint32 ticks_off = 3; CustomOrderDefinition child = 6; } message TriggerOrderDefinition { @@ -204,6 +205,12 @@ message CompositeOrderDefinition { uint32 algo_template_key = 1; CustomOrderDefinition definition = 2; } +message SplitterOrderDefinition { + uint32 max_order_qty = 1; + uint32 exposed_qty = 2; + // uint32 target_qty = 3; + CustomOrderDefinition child = 10; +} message DynamicOrderDefinition { string dynamic_order_type = 1; map order_params = 2; @@ -253,9 +260,10 @@ message OrderState { TriggerOrderState trigger = 21; CoverOrderState cover = 22; OffsetOrderState offset = 24; - QueueHolderState queue_holder = 30; OCOOrderState oco = 25; PricerOrderState pricer = 26; + QueueHolderState queue_holder = 30; + SplitterOrderState splitter = 31; DynamicOrderState dynamic = 255; // do not use anything bigger than 255, or things will break } @@ -351,6 +359,7 @@ message OCOOrderState {} message PricerOrderState {} message DynamicOrderState {} message QueueHolderState {} +message SplitterOrderState {} enum OrderStatus { diff --git a/src/lib/api/services/PositionService.proto b/src/lib/api/services/PositionService.proto index fa14b930c..b8aceea6c 100644 --- a/src/lib/api/services/PositionService.proto +++ b/src/lib/api/services/PositionService.proto @@ -8,6 +8,7 @@ import "lib/domain_model/Security.proto"; message PositionUnit{ int32 record_id = 1 [(bts.api.key)=true]; string account = 10; + uint32 account_id = 15; uint32 security_id = 20; double sod_long_pos = 30; double sod_long_avg_price = 40; @@ -19,6 +20,22 @@ message PositionUnit{ double day_short_avg_price = 100; } +message PNLUnit{ + int32 record_id = 1 [(bts.api.key)=true]; + PositionUnit pos = 10; + double cash_value = 20; + double bid_price = 30; + double ask_price = 40; + double last_price = 50; + double settlement_price = 60; +} + +message AccountPNL{ + uint32 account_id = 1 [(bts.api.key)=true]; + string account_name = 10; + double day_pnl = 20; +} + message ManualFill { int32 record_id = 1 [(bts.api.key)=true]; uint32 security_key = 2; @@ -53,5 +70,6 @@ service PositionService { // subscription rpc manual_fills(bts.api.VoidMessage) returns (stream ManualFill); rpc position_data(bts.api.VoidMessage) returns (stream PositionUnit); - + rpc pnl_data(bts.api.VoidMessage) returns (stream PNLUnit); + rpc pnl_account_data(bts.api.VoidMessage) returns (stream AccountPNL); } \ No newline at end of file diff --git a/src/lib/config/defaults/MarketData.conf b/src/lib/config/defaults/MarketData.conf index 70a91715c..d999851bb 100644 --- a/src/lib/config/defaults/MarketData.conf +++ b/src/lib/config/defaults/MarketData.conf @@ -9,6 +9,13 @@ MarketDataConfig { } } +PublisherOptions { + rule: "DeploymentType.type == 'dev'" + value { + disable_heartbeat: true + } +} + PlatformMap { value { glbx_platform: "PLATFORM_MDP3ONIXS" diff --git a/src/lib/config/defaults/span.conf b/src/lib/config/defaults/span.conf new file mode 100644 index 000000000..96ea56a5a --- /dev/null +++ b/src/lib/config/defaults/span.conf @@ -0,0 +1,9 @@ + +SpanMargin { + value { + enabled: false + login: 'api_bts1' + password: 'Summer123' + url: 'https://cmecorenr.cmegroup.com/MarginServiceApi/1.4/' + } +} diff --git a/src/lib/config/defaults/toggles.conf b/src/lib/config/defaults/toggles.conf index 939c2e230..52b63f712 100644 --- a/src/lib/config/defaults/toggles.conf +++ b/src/lib/config/defaults/toggles.conf @@ -8,4 +8,4 @@ OrdersSoftCleanup { value { enabled: true } -} \ No newline at end of file +} diff --git a/src/lib/config/protos/UI.proto b/src/lib/config/protos/UI.proto index 5731f5170..6d51c635d 100644 --- a/src/lib/config/protos/UI.proto +++ b/src/lib/config/protos/UI.proto @@ -141,6 +141,11 @@ message TopAttachUIOptions { optional bool show_quantity = 2 [default = false]; } +message OffsetAttachUIOptions { + optional bool show_price = 1 [default = false]; + optional bool show_quantity = 2 [default = false]; +} + message ManualFillsOptions { optional bool modal_transactions = 1 [default = true]; } \ No newline at end of file diff --git a/src/lib/config/protos/span.proto b/src/lib/config/protos/span.proto new file mode 100644 index 000000000..6d207757f --- /dev/null +++ b/src/lib/config/protos/span.proto @@ -0,0 +1,10 @@ +syntax='proto2'; +package bts.config; + +message SpanMargin +{ + optional bool enabled = 10; + optional string login = 20; + optional string password = 30; + optional string url = 40; +} diff --git a/src/lib/config/protos/toggles.proto b/src/lib/config/protos/toggles.proto index 8886b4ecc..af0232f0b 100644 --- a/src/lib/config/protos/toggles.proto +++ b/src/lib/config/protos/toggles.proto @@ -53,4 +53,4 @@ message EnsureOnScreenWithWorkAreaRect { // and the new ensure_onscreen code has only been tested with a single monitor arrangement. // Should verify that the code works with every monitor arrangement before enabling by default. optional bool enabled = 1 [default = false]; -} \ No newline at end of file +} diff --git a/src/lib/domain_model/Security.proto b/src/lib/domain_model/Security.proto index 3e4996f90..d0ee67220 100644 --- a/src/lib/domain_model/Security.proto +++ b/src/lib/domain_model/Security.proto @@ -146,6 +146,7 @@ message SpreadContract { message SyntheticContract { string name = 1; + uint32 user_id = 2; repeated ContractLeg legs = 20; PriceCalc price_calc = 30; enum PriceCalc { diff --git a/src/lib/md/IntPrice.h b/src/lib/md/IntPrice.h index dfca4e73f..22822745b 100644 --- a/src/lib/md/IntPrice.h +++ b/src/lib/md/IntPrice.h @@ -34,9 +34,9 @@ class IntPrice { #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) {} + constexpr IntPrice() : _mantissa(0), _exponent(127) {/*nan*/} + constexpr IntPrice(int64_t mantissa, int8_t exponent) : _mantissa(mantissa), _exponent(exponent) {} + constexpr IntPrice(const IntPrice& other) : _mantissa(other._mantissa), _exponent(other._exponent) {} bool operator==(const IntPrice& other) const { if(_exponent == other._exponent) { diff --git a/src/lib/md/MDProducerHandle.cpp b/src/lib/md/MDProducerHandle.cpp index 4efa289bd..8025e604b 100644 --- a/src/lib/md/MDProducerHandle.cpp +++ b/src/lib/md/MDProducerHandle.cpp @@ -105,12 +105,11 @@ void MDProducerHandle::add_trade(TimeStamp time, const PriceSize price, LLDateTi co2::AggressorSide agside, uint32_t volume, bool from_spread) { // Add this trade to the last trade if price/side match auto current = std::get(cdata().last_trade.load()); - if (current.int_price() == price.int_price() && agside == cdata().last_side) { + if (current.int_price() == price.int_price()) { cdata().last_trade.store(PriceSize { price.int_price(), price.size() + current.size() }, time); } else { cdata().last_trade.store(price,time); - cdata().last_side = agside; } cdata().last_trade_time = tradetime; cdata().from_spread = (from_spread ? 1 : 0); diff --git a/src/lib/md/PriceSize.h b/src/lib/md/PriceSize.h index e10a90365..3a72de6b1 100644 --- a/src/lib/md/PriceSize.h +++ b/src/lib/md/PriceSize.h @@ -77,7 +77,7 @@ public: extern PriceSize NO_MARKET; static_assert(sizeof(PriceSize) == 16, "PriceSize should be small"); -template +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 }; diff --git a/src/lib/md/SecurityCache.cpp b/src/lib/md/SecurityCache.cpp index 160633145..63a181a5d 100644 --- a/src/lib/md/SecurityCache.cpp +++ b/src/lib/md/SecurityCache.cpp @@ -22,6 +22,7 @@ void SecurityCacheImpl::enqueue_update_db(const SecurityPtr& security) { } void SecurityCacheImpl::flush() { + #ifndef _WIN32 if (!update_queue.empty()) { std::vector work; SecurityPtr temp; @@ -33,6 +34,7 @@ void SecurityCacheImpl::flush() { } } } + #endif } ////////////////// diff --git a/src/lib/md/Shm/MDStructure.h b/src/lib/md/Shm/MDStructure.h index 58ba91feb..2bad7f6ee 100644 --- a/src/lib/md/Shm/MDStructure.h +++ b/src/lib/md/Shm/MDStructure.h @@ -214,7 +214,6 @@ struct ColdEntry { last_trade(), total_volume(0), last_trade_time(), - last_side(co2::AggressorSide::AGGRESSOR_SIDE_NONE), misc(), from_spread(0), deleted(0) @@ -231,7 +230,6 @@ struct ColdEntry { AtomicPriceSize last_trade; size_t total_volume; datetime::LLDateTime last_trade_time; - co2::AggressorSide last_side; MiscData misc; diff --git a/src/lib/md/cme/MDP3Utils.cpp b/src/lib/md/cme/MDP3Utils.cpp index ae3ac82cd..1f09eb931 100644 --- a/src/lib/md/cme/MDP3Utils.cpp +++ b/src/lib/md/cme/MDP3Utils.cpp @@ -13,6 +13,7 @@ #include #include +#include using namespace std; @@ -56,6 +57,8 @@ bool check_file_exists_with_instructions(const string& filename, const string& u bool is_pow_two_or_0(uint32_t x) { return (x & (~x + 1)) == x; } void await_cme_config() { + static std::mutex config_mutex; + std::lock_guard guard(config_mutex); std::string config_url; std::string templates_url; @@ -112,6 +115,7 @@ const std::vector get_configured_cme_channels() { return parse_channels(symbols); } const vector parse_channels(const vector& symbols) { + await_cme_config(); vector result; bool skip_options = config::get().skip_options(); diff --git a/src/lib/md/relay/RelayRecorder.cpp b/src/lib/md/relay/RelayRecorder.cpp index d2a094b52..f443c0926 100644 --- a/src/lib/md/relay/RelayRecorder.cpp +++ b/src/lib/md/relay/RelayRecorder.cpp @@ -11,6 +11,7 @@ #include "lib/md/protos/co2.pb.h" #include "lib/md/SecurityIDRecorder.h" #include "lib/config/protos/system.pb.h" +#include "lib/config/protos/MarketData.pb.h" #include "lib/utils/bts_chrono.h" #include @@ -173,7 +174,7 @@ void replay_file(const std::string& filename, IRelayWriter& writer, int32_t nagg } while (loop); } -void replay_url(const std::string& url, IRelayWriter& writer, int32_t naggle_ms) { +bool replay_url(const std::string& url, IRelayWriter& writer, int32_t naggle_ms, bool loop) { // split up url parts size_t http_len = BTS_ARRAYSIZE("http://") - 1; auto after_host = url.find('/', http_len); @@ -184,21 +185,53 @@ void replay_url(const std::string& url, IRelayWriter& writer, int32_t naggle_ms) filename = os::sparkdata_path("data/replay/") + filename; utils::download_file_if_newer(host, path, filename); if (boost::filesystem::exists(filename)) { - replay_file(filename, writer, naggle_ms, true); + replay_file(filename, writer, naggle_ms, loop); + return true; + } + return false; +} + +void replay_url_with_fallback(const std::string& url, IRelayWriter& writer, int32_t naggle_ms, bool loop) { + if (!replay_url(url, writer, naggle_ms, loop)) { + const auto fallback = config::get().fallback(); + if (boost::filesystem::exists(fallback)) { + replay_file(fallback, writer, naggle_ms, loop); + } } } -void replay_url_with_fallback(const std::string& url, IRelayWriter& writer, int32_t naggle_ms) { - // loops forever - replay_url(url, writer, naggle_ms); - // If it returns it means it failed to download or play cached - const auto fallback = config::get().fallback(); - if (boost::filesystem::exists(fallback)) { - replay_file(fallback, writer, naggle_ms); +struct SecurityExtractorDone : public std::bad_exception {}; +struct SecurityExtractor : public IRelayWriter { + size_t _count; + std::function _cb; + std::set _keys; + SecurityExtractor(std::function& cb, size_t count) + : _count(count), _cb(cb), _keys() { } + ~SecurityExtractor() override {}; + void write(const co2::MDReply& data, bool*) override { + for (auto const &ch : data.channels()) { + for (auto const &cr : ch.updates()) { + if (cr.has_security() && _keys.find(cr.security().id()) == _keys.end()) { + _keys.insert(cr.security().id()); + _cb(cr.security()); + } + } + } + if (_keys.size() >= _count) { + throw SecurityExtractorDone(); + } } +}; +void get_securities_from_replay(std::function cb) { + SecurityExtractor extractor(cb, 100); + auto url = config::get().url(); + try { + replay_url_with_fallback(url, extractor, 0, false); + } catch(const SecurityExtractorDone&) { } } + shared_ptr mkrelayproducer(bool shared); /*this is the public one*/ diff --git a/src/lib/md/relay/RelayRecorder.h b/src/lib/md/relay/RelayRecorder.h index d70dd4757..da6286340 100644 --- a/src/lib/md/relay/RelayRecorder.h +++ b/src/lib/md/relay/RelayRecorder.h @@ -29,9 +29,10 @@ void print_recording(const std::string&); void unprint_recording(const std::string& src, const std::string& dest); void trace_recording(const std::string& file, security_key key); void replay_recording(const std::string&, int32_t delay_ms, const std::string fallback=""); -void replay_url(const std::string& url, IRelayWriter& writer, int32_t naggle_ms); -void replay_url_with_fallback(const std::string& url, IRelayWriter& writer, int32_t naggle_ms); +bool replay_url(const std::string& url, IRelayWriter& writer, int32_t naggle_ms, bool loop=true); +void replay_url_with_fallback(const std::string& url, IRelayWriter& writer, int32_t naggle_ms, bool loop=true); void replay_file(const std::string& filename, IRelayWriter& writer, int32_t naggle_ms, bool loop=false); +void get_securities_from_replay(std::function); }}//namespace diff --git a/src/lib/md/test/synthetic_book_merge_tests.cpp b/src/lib/md/test/synthetic_book_merge_tests.cpp index 25d97cfab..63d1914c9 100644 --- a/src/lib/md/test/synthetic_book_merge_tests.cpp +++ b/src/lib/md/test/synthetic_book_merge_tests.cpp @@ -111,7 +111,7 @@ tuple,vector> make_securities(c l->mutable_security()->mutable_contract_spec()->set_low_tick(tick_size); l->mutable_security()->mutable_contract_spec()->set_high_tick(tick_size); l->set_quantity(quantity); - l->set_price_multiplier(quantity * (side == api::SIDE_BUY ? 1.0 : -1)); + l->set_price_multiplier(mult); l->set_side(side); synthetic->legs.push_back(instr.consumer()); legs.push_back(instr); @@ -126,7 +126,8 @@ tuple,vector> make_securities(c double quantity, tick_size; api::Side side; std::tie(quantity, tick_size, side) = t; - legs.push_back({ quantity, quantity, tick_size, side }); + double mult = quantity * (side == api::SIDE_BUY ? 1.0 : -1); + legs.push_back({ quantity, mult, tick_size, side }); } return make_securities(legs); } @@ -457,6 +458,113 @@ TEST_CASE("Synthetic_net_change", "[md][synthetic]") { } } +TEST_CASE("HCL_SP_104", "[md][synthetic]") { + shared_ptr synthetic; + vector test_instr; + tie(synthetic, test_instr) = make_securities({ + // (quantity, multiplier, tick_size, side) + {1.0, 1.0, 0.01, api::SIDE_BUY}, + {1.0, 1.0, 0.01, api::SIDE_BUY}, + {1.0, -1.0, 0.01, api::SIDE_SELL}, + {1.0, -1.0, 0.01, api::SIDE_SELL } + }); + auto leg1 = test_instr[0]; // CLQ9-CLU9 + auto leg2 = test_instr[1]; // HCLQ9-CLQ9 + auto leg3 = test_instr[2]; // HCLQ9-HCLU9 + auto leg4 = test_instr[3]; // HCLU9-CLU9 + + setup_market(leg1.producer(), + { + { -0.10, 3299 }, + { -0.11, 2885 }, + { -0.12, 2558 }, + { -0.13, 2447 }, + { -0.14, 1540 }, + { -0.15, 2334 }, + { -0.16, 1396 }, + { -0.17, 1190 }, + { -0.18, 1221 }, + { -0.19, 812 } + }, + { + { -0.09, 1318 }, + { -0.08, 2174 }, + { -0.07, 3057 }, + { -0.06, 2331 }, + { -0.05, 2543 }, + { -0.04, 2638 }, + { -0.03, 2232 }, + { -0.02, 3221 }, + { -0.01, 2863 }, + { 0.00, 3327 } + }); + + setup_market(leg2.producer(), + { + { 4.06, 1 }, + { 4.05, 2 }, + { 4.04, 3 }, + { 4.03, 12 }, + { 4.02, 6 }, + { 4.01, 1 } + }, + { + { 4.07, 1 }, + { 4.08, 2 }, + { 4.10, 8 }, + { 4.11, 10 }, + { 4.12, 2 }, + { 4.14, 1 } + }); + + setup_market(leg3.producer(), + { + { 0.20, 2 }, + { 0.19, 6 }, + { 0.18, 5 }, + { 0.17, 18 }, + { 0.16, 2 }, + { 0.15, 3 }, + { 0.14, 2 } + }, + { + { 0.22, 2 }, + { 0.23, 4 }, + { 0.24, 9 }, + { 0.25, 17}, + { 0.26, 5 }, + { 0.27, 5 }, + { 0.28, 4 }, + { 0.29, 2 }, + { 0.30, 2 } + }); + + setup_market(leg4.producer(), + { + { 3.75, 1 }, + { 3.74, 3 }, + { 3.73, 4 }, + { 3.72, 12 }, + { 3.71, 1 }, + { 3.70, 3 } + }, + { + { 3.78, 1 }, + { 3.79, 2 }, + { 3.80, 6 }, + { 3.81, 1 }, + { 3.82, 10 }, + { 3.83, 1 } + }); + + MDConsumerHandle synthetic_handle(synthetic); + // dump_market(synthetic_handle); + expect_market(synthetic_handle, + { { -0.04,1 },{ -0.06,1 },{ -0.07,1 },{ -0.09,3 },{ -0.11,3 },{ -0.12,1 },{ -0.13,5 },{ -0.14,3 },{ -0.15,2 },{ -0.16,1 } }, + { { 0.03,1 },{ 0.05,1 },{ 0.06,1 },{ 0.08,1 },{ 0.09,4 },{ 0.11,3 },{ 0.12,2 },{ 0.13,7 },{ 0.14,1 },{ 0.16,2 } } + ); +} + // // Benchmarks diff --git a/src/lib/orderman/actors/FillStore.cpp b/src/lib/orderman/actors/FillStore.cpp index 72cb22a03..ab0f2a781 100644 --- a/src/lib/orderman/actors/FillStore.cpp +++ b/src/lib/orderman/actors/FillStore.cpp @@ -130,6 +130,7 @@ void FillStore::on_execution_report( if (it != me->known_orders.end()) { auto &order_def = it->second; fill.set_account(order_def.account()); + fill.set_internal_account_id(order_def.account_id()); } else me->fills_with_unresolved_accounts[er.order_key] .fills.push_back(fill); @@ -208,6 +209,7 @@ void FillStore::on_order_definition_update( auto &unresolved_fills = me->fills_with_unresolved_accounts[ev.key]; for (auto &f : unresolved_fills.fills) { f.set_account(ev.def->account()); + f.set_internal_account_id(ev.def->account_id()); auto fv = utils::pb::to_string(f); me->store->update(fills_ns, f.fill_key(), fv); publish(me->fill_stream, f, *unresolved_fills.security); diff --git a/src/lib/orderman/actors/SODPosition.cpp b/src/lib/orderman/actors/SODPosition.cpp index a9560741e..69b846ded 100644 --- a/src/lib/orderman/actors/SODPosition.cpp +++ b/src/lib/orderman/actors/SODPosition.cpp @@ -1,6 +1,7 @@ #include "lib/orderman/actors/SODPosition.h" #include "lib/config/config.h" #include "lib/config/protos/orders_fills_cleanup.pb.h" +#include "lib/api/service_helpers/AdminServiceUtils.h" #include "lib/config/protos/toggles.pb.h" #include "lib/logging/logging.h" #include "lib/utils/PBUtils.h" @@ -131,6 +132,7 @@ void SODPosition::_merge_pos(Fill &pos, const Fill &fill) { void SODPosition::_zero_pos(Fill &pos, const Fill &fill, api::Side side) { pos.set_security_key(fill.security_key()); pos.set_account(fill.account()); + pos.set_internal_account_id(fill.internal_account_id()); pos.set_side(side); pos.mutable_timestamp()->set_seconds(_sod_time.tv_sec); pos.mutable_timestamp()->set_nanos(0); @@ -140,14 +142,20 @@ void SODPosition::_zero_pos(Fill &pos, const Fill &fill, api::Side side) { pos.set_last_shares(0); } -bool SODPosition::add_fill_to_sod_pos(const Fill &fill) { +bool SODPosition::add_fill_to_sod_pos(const Fill &fill) { if (_enabled && fill.fill_status() != api::orderman::Fill::FILL_STATUS_BUSTED && fill.timestamp().seconds() < _sod_time.tv_sec) { - uint32_t seckey = fill.security_key(); - const std::string &acct = fill.account(); - auto acct_it = _sod_map.find(acct); + if (!bts::admin::AdminServiceUtils::has_id(fill.internal_account_id())){ + LOG(persist, Error, "SODPosition::add_fill_to_sod_pos Could not find " + "account_id for fill:{} account:{}", + fill.fill_key(), fill.account()); + return false; + } + uint32_t seckey = fill.security_key(), + account_id = fill.internal_account_id(); + auto acct_it = _sod_map.find(account_id); if (acct_it == _sod_map.end()) - _sod_map[acct] = SecPosMap(); - acct_it = _sod_map.find(fill.account()); + _sod_map[account_id] = SecPosMap(); + acct_it = _sod_map.find(account_id); bts_assert(acct_it != _sod_map.end()); SecPosMap &sec_map = acct_it->second; auto sec_it = sec_map.find(seckey); @@ -165,7 +173,8 @@ bool SODPosition::add_fill_to_sod_pos(const Fill &fill) { else _merge_pos(pos.second, fill); return true; - } else + } + else return false; } diff --git a/src/lib/orderman/actors/SODPosition.h b/src/lib/orderman/actors/SODPosition.h index 860e0cf2d..fd31b8b3d 100644 --- a/src/lib/orderman/actors/SODPosition.h +++ b/src/lib/orderman/actors/SODPosition.h @@ -16,8 +16,7 @@ SecPosMap; class SODPosition{ private: - std::map _sod_map; - std::map _sec_ptr_map; + std::map _sod_map; const bool _enabled; const timeval _sod_time; private: diff --git a/src/lib/orderman/actors/ServiceBridge.cpp b/src/lib/orderman/actors/ServiceBridge.cpp index 4d104dd5f..c85e3bfbd 100644 --- a/src/lib/orderman/actors/ServiceBridge.cpp +++ b/src/lib/orderman/actors/ServiceBridge.cpp @@ -1,10 +1,14 @@ #include "ServiceBridge.h" #include "lib/api/public/ServiceContext.h" +#include "lib/api/services/OrderService.pb.h" +#include "lib/api/services/PositionService.pb.h" #include "lib/mercury/broker/BrokerProxy.h" +#include "lib/orderman/events/AccountPnLUpdateEvent.h" #include "lib/orderman/events/ActivateOrderEvent.h" #include "lib/orderman/events/CancelOrderEvent.h" #include "lib/orderman/events/DetachOrderEvent.h" +#include "lib/orderman/events/ExcessLiquidityUpdateEvent.h" #include "lib/orderman/events/NewAdminDataEvent.h" #include "lib/orderman/events/OrderManagerReadyEvent.h" #include "lib/orderman/events/OrderOpResultEvent.h" @@ -13,13 +17,11 @@ #include "lib/orderman/events/UpdateOrderEvent.h" #include "lib/orderman/order_factory/OrderFactory.h" #include "lib/orderman/order_router/OrderRouter.h" +#include "lib/orderman/orderman_utils/orderman_common.h" +#include "lib/orderman/orders/Order.h" #include "lib/orderman/securities/MapSecurityDB.h" #include "lib/tracing/PerfLog.h" #include "lib/utils/PBUtils.h" - -#include "lib/api/services/OrderService.pb.h" -#include "lib/orderman/orderman_utils/orderman_common.h" -#include "lib/orderman/orders/Order.h" #include namespace bts { @@ -50,6 +52,8 @@ struct ServiceBridge::PImpl { mercury::Stream cancel_order_pub; mercury::Stream detach_order_pub; mercury::Stream new_admin_data_pub; + mercury::Stream account_pnl_pub; + mercury::Stream excess_liquidity_pub; std::unordered_set known_orders; std::vector> admin_data_updates; mercury::BrokerProxy &service_call_marshaller; @@ -81,6 +85,8 @@ void ServiceBridge::init() { me->cancel_order_pub = make_stream(); me->detach_order_pub = make_stream(); me->new_admin_data_pub = make_stream(); + me->account_pnl_pub = make_stream(); + me->excess_liquidity_pub = make_stream(); me->ready.set_value(); } @@ -217,6 +223,36 @@ void ServiceBridge::update_admin_data( }); } +void ServiceBridge::update_account_pnl( + api::position::Pnl_account_dataUpdate const &pnl) { + me->service_call_marshaller.schedule_action([this, pnl]() { + for (auto const &account_pnl : pnl.added()) { + publish(me->account_pnl_pub, AccountID(account_pnl->account_id()), + account_pnl->day_pnl()); + } + for (auto const &account_pnl : pnl.changed()) { + publish(me->account_pnl_pub, + AccountID(account_pnl.current()->account_id()), + account_pnl.current()->day_pnl()); + } + }); +} + +void ServiceBridge::update_excess_liquidity_report( + api::position::Excess_liquidity_reportUpdate const &elr) { + me->service_call_marshaller.schedule_action([this, elr]() { + for (auto const &elr : elr.added()) { + publish(me->excess_liquidity_pub, AccountID(elr->account_id()), + elr->excess_liquidity()); + } + for (auto const &elr : elr.changed()) { + publish(me->excess_liquidity_pub, + AccountID(elr.current()->account_id()), + elr.current()->excess_liquidity()); + } + }); +} + bool ServiceBridge::is_ready() const { return me->have_orders && me->order_manager_ready; } diff --git a/src/lib/orderman/actors/ServiceBridge.h b/src/lib/orderman/actors/ServiceBridge.h index bcf225db1..ab68bf1ed 100644 --- a/src/lib/orderman/actors/ServiceBridge.h +++ b/src/lib/orderman/actors/ServiceBridge.h @@ -1,18 +1,26 @@ #pragma once +#include "lib/api/services/MarginService.client.pb.h" +#include "lib/api/services/PositionService.client.pb.h" #include "lib/config/protos/jetfix.pb.h" -#include "lib/mercury/actors/Actor.h" #include "lib/md/SecurityOracle.h" +#include "lib/mercury/actors/Actor.h" + #include // fwds namespace bts { -namespace mercury {class BrokerProxy;} +namespace mercury { +class BrokerProxy; +} namespace api { class ServiceContext; namespace admin { class AdminData; } +namespace position { +class AccountPNL; +} namespace orderman { class OrderRecord; class Fill; @@ -45,16 +53,14 @@ class ServiceBridge : public mercury::Actor { using OnReady = std::function; using OnOrders = - std::function const &)>; + std::function const &)>; using OnFills = - std::function const &)>; + std::function const &)>; + + using OnOrderOpResult = std::function; - using OnOrderOpResult = - std::function; - ServiceBridge(OnReady const &, OnOrders const &, OnFills const &, - OnOrderOpResult const&, - DynamicOrderRegistry const &, + OnOrderOpResult const &, DynamicOrderRegistry const &, mercury::BrokerProxy &service_call_marshaller, md::SecurityOracle o); void init() override; @@ -71,6 +77,9 @@ class ServiceBridge : public mercury::Actor { void update_admin_data(std::shared_ptr const &admin); + void update_account_pnl(api::position::Pnl_account_dataUpdate const &); + void update_excess_liquidity_report( + api::position::Excess_liquidity_reportUpdate const &); private: struct PImpl; diff --git a/src/lib/orderman/actors/order_manager/OrderManager.cpp b/src/lib/orderman/actors/order_manager/OrderManager.cpp index 6be6c2146..7746b7682 100644 --- a/src/lib/orderman/actors/order_manager/OrderManager.cpp +++ b/src/lib/orderman/actors/order_manager/OrderManager.cpp @@ -4,6 +4,8 @@ #include "lib/mercury/events/TimerEvent.h" #include "lib/orderman/actors/order_manager/OrderServices.h" #include "lib/orderman/actors/order_manager/OrderServicesData.h" +#include "lib/orderman/events/AccountPnLUpdateEvent.h" +#include "lib/orderman/events/ExcessLiquidityUpdateEvent.h" #include "lib/orderman/events/NewAdminDataEvent.h" #include "lib/orderman/events/NewSecuritiesEvent.h" #include "lib/orderman/events/OrderManagerReadyEvent.h" @@ -69,6 +71,11 @@ OrderManager::~OrderManager() { void OrderManager::init() { subscribe( [this](auto const &admin_update) { this->on_admin_data(admin_update); }); + subscribe( + [this](auto const &pnl_update) { this->on_pnl_update(pnl_update); }); + subscribe([this](auto const &liq_update) { + this->on_excess_liquidity_update(liq_update); + }); } void OrderManager::on_admin_data(const mercury::Update &m) { @@ -93,6 +100,20 @@ void OrderManager::on_admin_data(const mercury::Update &m) { } } +void OrderManager::on_pnl_update( + const mercury::Update &u) { + if (u.events) + for (auto ev : *u.events) + me->services_data.limiter.set_day_pnl(ev.account_id, ev.day_pnl); +} + +void OrderManager::on_excess_liquidity_update( + const mercury::Update &u) { + if (u.events) + for (auto ev : *u.events) + me->services_data.limiter.set_excess_liquidity(ev.account_id, ev.excess_liquidity); +} + void OrderManager::on_securities( const mercury::Update &update) { diff --git a/src/lib/orderman/actors/order_manager/OrderManager.h b/src/lib/orderman/actors/order_manager/OrderManager.h index bc702c5a5..218704ad2 100644 --- a/src/lib/orderman/actors/order_manager/OrderManager.h +++ b/src/lib/orderman/actors/order_manager/OrderManager.h @@ -34,6 +34,8 @@ class OrderServices; class CreateFIXBufferReplyEvent; class GatewayStatusChangeEvent; class NewAdminDataEvent; +class AccountPnLUpdateEvent; +class ExcessLiquidityUpdateEvent; struct SessionServicesData; class DynamicOrderRegistry; } // namespace orderman @@ -83,6 +85,8 @@ class OrderManager : public mercury::Actor, public mercury::IPollableActor { void on_gateway_state(const mercury::Update &); void on_timer(const mercury::Update &); void on_admin_data(const mercury::Update &); + void on_pnl_update(const mercury::Update &); + void on_excess_liquidity_update(const mercury::Update &); void on_poll(trace::PerfLog& log) override; // end external events diff --git a/src/lib/orderman/events/AccountPnLUpdateEvent.h b/src/lib/orderman/events/AccountPnLUpdateEvent.h new file mode 100644 index 000000000..e6882df8e --- /dev/null +++ b/src/lib/orderman/events/AccountPnLUpdateEvent.h @@ -0,0 +1,27 @@ +#pragma once + +#include "lib/api/services/AdminService.pb.h" + +#include "lib/mercury/common/mercury_common.h" +#include "lib/mercury/events/Event.h" +#include "lib/mercury/synopsis/MergeByKey.h" + +#include + +namespace bts { +namespace orderman { + +class AccountPnLUpdateEvent : public mercury::Event { + public: + using SynopsisPolicy = mercury::MergeByKey; + AccountID key() const { return account_id; } + + AccountPnLUpdateEvent(AccountID id, double pnl) + : account_id(id), day_pnl(pnl) {} + + AccountID account_id; + double day_pnl; +}; + +} // namespace orderman +} // namespace bts diff --git a/src/lib/orderman/events/ExcessLiquidityUpdateEvent.h b/src/lib/orderman/events/ExcessLiquidityUpdateEvent.h new file mode 100644 index 000000000..3174c787a --- /dev/null +++ b/src/lib/orderman/events/ExcessLiquidityUpdateEvent.h @@ -0,0 +1,25 @@ +#pragma once + +#include "lib/mercury/common/mercury_common.h" +#include "lib/mercury/events/Event.h" +#include "lib/mercury/synopsis/MergeByKey.h" + +#include + +namespace bts { +namespace orderman { + +class ExcessLiquidityUpdateEvent : public mercury::Event { + public: + using SynopsisPolicy = mercury::MergeByKey; + AccountID key() const { return account_id; } + + ExcessLiquidityUpdateEvent(AccountID id, double excess_liquidity) + : account_id(id), excess_liquidity(excess_liquidity) {} + + AccountID account_id; + double excess_liquidity; +}; + +} // namespace orderman +} // namespace bts diff --git a/src/lib/orderman/events/OrderStateUpdateEvent.h b/src/lib/orderman/events/OrderStateUpdateEvent.h index b639c4549..9aeb47d81 100644 --- a/src/lib/orderman/events/OrderStateUpdateEvent.h +++ b/src/lib/orderman/events/OrderStateUpdateEvent.h @@ -153,6 +153,9 @@ class OrderStateUpdateEvent : public mercury::Event { case api::orderman::OrderState::kDynamic: result.mutable_dynamic(); break; + case api::orderman::OrderState::kSplitter: + result.mutable_splitter(); + break; case api::orderman::OrderState::CUSTOM_NOT_SET: break; } diff --git a/src/lib/orderman/events/TickerUpdateEvent.h b/src/lib/orderman/events/TickerUpdateEvent.h index d34522d48..029d0fc04 100644 --- a/src/lib/orderman/events/TickerUpdateEvent.h +++ b/src/lib/orderman/events/TickerUpdateEvent.h @@ -6,6 +6,7 @@ #include "lib/orderman/orderman_utils/orderman_common.h" #include "lib/utils/bts_chrono.h" #include "lib/md/protos/co2.pb.h" +#include "boost/container/small_vector.hpp" namespace bts { namespace orderman { @@ -27,9 +28,10 @@ class TickerUpdateEvent : public mercury::Event { co2::AggressorSide aggressor_side; }; - boost::container::small_vector trades; + boost::container::small_vector trades; TickerUpdateEvent(SecurityKey key) : _key(key), trades() {} }; +typedef boost::container::small_vector TradeVector; } } diff --git a/src/lib/orderman/limiter/LimitStore.cpp b/src/lib/orderman/limiter/LimitStore.cpp index e25807d7c..3f71be12b 100644 --- a/src/lib/orderman/limiter/LimitStore.cpp +++ b/src/lib/orderman/limiter/LimitStore.cpp @@ -5,8 +5,10 @@ namespace orderman { LimitStore::LimitStore(const bts::api::admin::AdminData &admin_data, const std::map &sec_map) - : _admin_data(bts::admin::AdminServiceUtils::sort_limits(admin_data)), - _account_map(bts::admin::AdminServiceUtils::accounts_map(_admin_data)) { + : _admin_data(bts::admin::AdminServiceUtils::sort_limits(admin_data)) { + for (const auto& acct : _admin_data.accounts()) + _account_data_map.insert(std::pair( + acct.id(),AccountData(acct))); for (auto it = sec_map.begin(); it != sec_map.end(); it++) { const api::Security &security = *(it->second); _add_product_entry(security, _admin_data); @@ -22,7 +24,7 @@ LimitStore::LimitStore(const bts::api::admin::AdminData &admin_data, } _set_user_data(); } -void LimitStore::copy_positions_from(LimitStore &source) { +void LimitStore::copy_data_from(LimitStore &source) { // copy securities for (auto it = _sec_map.begin(); it != _sec_map.end(); it++) { SecurityLimitTree *source_security_limit_tree = @@ -30,6 +32,25 @@ void LimitStore::copy_positions_from(LimitStore &source) { if (source_security_limit_tree != nullptr) it->second.copy_positions_from(*source_security_limit_tree); } + // copy pnl's and liqs + for (auto it=_account_data_map.begin();it!=_account_data_map.end();it++){ + auto source_it = source._account_data_map.find(it->first); + if(source_it != source._account_data_map.end()) { + it->second.curr_day_pnl = source_it->second.curr_day_pnl; + it->second.excess_liquidity = source_it->second.excess_liquidity; + } + } +} +void LimitStore::set_pnl (AccountID account_id, double pnl){ + auto it = _account_data_map.find(account_id.value()); + bts_assert(it != _account_data_map.end()); + it->second.curr_day_pnl = pnl; +} +void LimitStore::set_excess_liquidity (AccountID account_id, + double excess_liquidity){ + auto it = _account_data_map.find(account_id.value()); + bts_assert(it != _account_data_map.end()); + it->second.excess_liquidity = excess_liquidity; } void LimitStore::_add_product_entry( const api::Security &security, diff --git a/src/lib/orderman/limiter/LimitStore.h b/src/lib/orderman/limiter/LimitStore.h index 90a3a7888..f46ca7f0c 100644 --- a/src/lib/orderman/limiter/LimitStore.h +++ b/src/lib/orderman/limiter/LimitStore.h @@ -9,6 +9,10 @@ #include "Limiter.h" #include "ProductLimitTree.h" #include "SecurityLimitTree.h" +#include "lib/os/os.h" +#include "lib/config/config.h" +#include "lib/config/protos/span.pb.h" + namespace bts { namespace orderman { @@ -33,9 +37,22 @@ private: return nullptr; } }; + + class AccountData{ + public: + const bts::api::admin::BTSAccount& account; + double curr_day_pnl; + double excess_liquidity; + AccountData (const bts::api::admin::BTSAccount& account) + : + account(account), + curr_day_pnl (os::nan()), + excess_liquidity (os::nan()) + {} + }; const bts::api::admin::AdminData _admin_data; - // just an easy way to find accounts - const std::map _account_map; + // easy account access (uses for loss limit) + std::map _account_data_map; // map of user id's vs the user pointer and acccounts std::map _user_data_map; // map of securities vs their account limits and positions @@ -52,14 +69,20 @@ public: // accessors /////////////// //////////////////////////// - bool has_account (AccountID account_id) const - {return _account_map.find(account_id.value()) != _account_map.end();} + bool has_account (AccountID account_id) const{ + return _account_data_map.find(account_id.value()) != + _account_data_map.end(); + } bool has_security (uint32_t sec_id) const {return _sec_map.find(sec_id) != _sec_map.end();} const bts::api::admin::AdminData& admin_data () const {return _admin_data;} - // position updates //////// - //////////////////////////// - void copy_positions_from(LimitStore& source); + // data updates //////////// + // once the LimitStore is created its position and + // pnl is updated from outside. + //////////////////////////// + // copy_data_from copies the position and + // data from another source + void copy_data_from(LimitStore& source); void add_position(const api::Security& security, AccountID account_id, api::Side side, Quantity quantity, bool recurse){ @@ -88,6 +111,8 @@ public: } } } + void set_pnl (AccountID account_id, double pnl); + void set_excess_liquidity (AccountID account_id, double excess_liquidity); // risk limit checks //////////////////// // checks to see if the user is allowed to trade @@ -130,11 +155,21 @@ public: Limiter::LimiterQueryResult can_send_new_order(AccountID account_id, const api::Security& security, api::Side side, - Quantity quantity) const{ + Quantity quantity, + bool enforce_clip = true) const{ Limiter::LimiterQueryResult rv; rv.result = Limiter::OK; + const auto acct_it = _account_data_map.find(account_id.value()); + AccountData const* account_ptr = (acct_it == _account_data_map.end()) ? + nullptr : &(acct_it->second); + _loss_limit(account_ptr, rv); + if(!rv.ok()) + return rv; + _margin_limit(account_ptr,rv); + if(!rv.ok()) + return rv; const SecurityLimitTree* sec_tree = _find_security(security.id()); bts_assert(sec_tree != nullptr); - rv = sec_tree->can_send_new_order(account_id, side, quantity); + rv = sec_tree->can_send_new_order(account_id, side, quantity, enforce_clip); if (rv.ok() && security.has_spread()){ // if its rejected we dont need to validate further, // however if its ok and its a spread we need to check @@ -142,7 +177,12 @@ public: for (const api::ContractLeg& leg : security.spread().legs()){ api::Side effective_leg_side = side == api::Side::SIDE_BUY ? leg.side() : LimiterUtils::invert_side(leg.side()); Quantity effective_leg_qty = Quantity (quantity.value() * std::abs(leg.quantity())); - rv = can_send_new_order (account_id, leg.security(), effective_leg_side, effective_leg_qty); + // in the case of a spread with future legs the clip limits + // for the legs are not checked. if the legs are are spreads + // themselves then they are checked (eventually it will recuse + // down to a non spread) + rv = can_send_new_order (account_id, leg.security(), effective_leg_side, effective_leg_qty, + leg.security().has_spread()); if (!rv.ok()) return rv; } @@ -152,18 +192,29 @@ public: Limiter::LimiterQueryResult can_update_order(AccountID account_id, const api::Security& security, api::Side side, - Quantity from, Quantity to) const{ + Quantity from, Quantity to, + bool enforce_clip = true) const{ Limiter::LimiterQueryResult rv; rv.result = Limiter::OK; + const auto acct_it = _account_data_map.find(account_id.value()); + AccountData const* account_ptr = (acct_it == _account_data_map.end()) ? + nullptr : &(acct_it->second); + // in case of order modifications, loss limits are only checked + // for size increases + if (to.value() > from.value()) + _loss_limit(account_ptr, rv); + if(!rv.ok()) + return rv; const SecurityLimitTree* sec_tree = _find_security(security.id()); bts_assert(sec_tree != nullptr); - rv = sec_tree->can_update_order(account_id, side, from, to); + rv = sec_tree->can_update_order(account_id, side, from, to, enforce_clip); // same logic for the spread legs as above if (rv.ok() && security.has_spread()){ for (const api::ContractLeg& leg : security.spread().legs()){ api::Side effective_leg_side = side == api::Side::SIDE_BUY ? leg.side() : LimiterUtils::invert_side(leg.side()); Quantity effective_leg_from_qty = Quantity (from.value() * std::abs(leg.quantity())); Quantity effective_leg_to_qty = Quantity (to.value() * std::abs(leg.quantity())); - rv = can_update_order (account_id, leg.security(), effective_leg_side, effective_leg_from_qty, effective_leg_to_qty); + rv = can_update_order (account_id, leg.security(), effective_leg_side, effective_leg_from_qty, effective_leg_to_qty, + leg.security().has_spread()); if (!rv.ok()) return rv; } @@ -190,5 +241,66 @@ private: void _add_product_entry(const api::Security& security, const bts::api::admin::AdminData& admin_data); void _set_user_data(); + void _loss_limit(AccountData const* account_ptr, + Limiter::LimiterQueryResult& out_res) const{ + // the usual flow should not alter out_res. + // its set to an error state iff : + // i. account is not found + // ii. limit check is enabled but pnl is nan + // iii. limit check is enable and it actually fails + if(account_ptr != nullptr){ + const auto& account_data = *account_ptr; + const auto& account = account_data.account; + if (account.has_account_loss_limit() && + account.account_loss_limit().enforce()){ + double pnl = account_data.curr_day_pnl; + if(os::is_nan(pnl)){ + out_res.result = Limiter::LimiterErrorCode::LOSS_LIMIT_BREACHED; + out_res.error_msg = "Could not find account PNL"; + } + else{ + double loss_limit = -1.0 * static_cast( + account.account_loss_limit().total_loss_limit()); + if(pnl().enabled()) { + if (account.has_margin_limit() && account.margin_limit().enforce_margin()){ + double ex_liq = account_data.excess_liquidity; + if(os::is_nan(ex_liq)){ + out_res.result = Limiter::LimiterErrorCode::MARGIN_LIMIT_BREACHED; + out_res.error_msg = "Could not find account Excess Liquidity"; + } + else{ + if(ex_liq < 0){ + out_res.result = Limiter::LimiterErrorCode::MARGIN_LIMIT_BREACHED; + out_res.error_msg = fmt::format("Margin Limit Excess Liquidity : {}", + ex_liq); + } + } + } + } + } + else{ + out_res.result = Limiter::LimiterErrorCode::NO_SUCH_ACCOUNT; + out_res.error_msg = "Could not find account"; + } + } }; }} diff --git a/src/lib/orderman/limiter/Limiter.cpp b/src/lib/orderman/limiter/Limiter.cpp index 8f5fbe1c7..cd1cfc9e5 100644 --- a/src/lib/orderman/limiter/Limiter.cpp +++ b/src/lib/orderman/limiter/Limiter.cpp @@ -73,6 +73,20 @@ void Limiter::remove(const api::Security &security, else remove(security, AccountID(acct_ptr->id()), side, quantity, recurse); } +void Limiter::set_day_pnl (AccountID account_id, double pnl){ + if (!me->limit_store->has_account(account_id)) + _add_account_and_reset(account_id); + me->limit_store->set_pnl(account_id, pnl); +} +void Limiter::set_excess_liquidity (AccountID account_id, + double excess_liquidity) { + LOG(OrderMan, Info, + "Limiter::set_excess_liquidity account:{} excess_liquidity: {}", + account_id, excess_liquidity); + if (!me->limit_store->has_account(account_id)) + _add_account_and_reset(account_id); + me->limit_store->set_excess_liquidity(account_id, excess_liquidity); +} Limiter::LimiterQueryResult Limiter::can_trade(UserID user_id, AccountID account_id) const { LimiterQueryResult rv = me->limit_store->can_trade(user_id, account_id); @@ -119,7 +133,7 @@ void Limiter::_add_account_and_reset(AccountID account_id) { } void Limiter::_reset(const bts::api::admin::AdminData &admin_data) { LimitStore *new_store = new LimitStore(admin_data, me->sec_map); - new_store->copy_positions_from(*me->limit_store); + new_store->copy_data_from(*me->limit_store); me->limit_store.reset(new_store); } diff --git a/src/lib/orderman/limiter/Limiter.h b/src/lib/orderman/limiter/Limiter.h index ff6e94c1d..8ad1f0929 100644 --- a/src/lib/orderman/limiter/Limiter.h +++ b/src/lib/orderman/limiter/Limiter.h @@ -33,7 +33,9 @@ class Limiter { api::Side, Quantity, bool recurse); void remove(api::Security const &, std::string const & /* account_name */, api::Side, Quantity, bool recurse); - + void set_day_pnl (AccountID account_id, double pnl); + void set_excess_liquidity(AccountID account_id, double excesss_liquidity); + enum LimiterErrorCode { OK, NO_SUCH_SECURITY, @@ -45,7 +47,9 @@ class Limiter { ACCOUNT_DEACTIVATED, NO_SUCH_ACCOUNT, USER_DEACTIVATED, - NO_SUCH_USER + NO_SUCH_USER, + LOSS_LIMIT_BREACHED, + MARGIN_LIMIT_BREACHED }; struct LimiterQueryResult { diff --git a/src/lib/orderman/limiter/SecurityLimitNode.h b/src/lib/orderman/limiter/SecurityLimitNode.h index be1aa8a90..44f8a53d7 100644 --- a/src/lib/orderman/limiter/SecurityLimitNode.h +++ b/src/lib/orderman/limiter/SecurityLimitNode.h @@ -63,9 +63,11 @@ public: } // risk limit checks //////////////////// - Limiter::LimiterQueryResult can_send_new_order(api::Side side, Quantity quantity) const{ + Limiter::LimiterQueryResult can_send_new_order(api::Side side, + Quantity quantity, + bool enforce_clip = true) const{ Limiter::LimiterQueryResult result;result.result= Limiter::OK; - if (quantity.value() > _clip_size){ + if (enforce_clip && quantity.value() > _clip_size){ result.result = Limiter::CLIP_SIZE_BREACHED; result.error_msg = fmt::format("Security Clip Size (Max {} Qty {})", _clip_size, quantity.value()); return result; @@ -78,9 +80,10 @@ public: result = _product_node.can_send_new_order(side, quantity); return result; } - Limiter::LimiterQueryResult can_update_order(api::Side side, Quantity from, Quantity to) const{ + Limiter::LimiterQueryResult can_update_order(api::Side side, Quantity from, Quantity to, + bool enforce_clip = true) const{ Limiter::LimiterQueryResult result;result.result=Limiter::OK; - if (to.value() > _clip_size){ + if (enforce_clip && to.value() > _clip_size){ result.result = Limiter::CLIP_SIZE_BREACHED; result.error_msg = fmt::format("Security Clip Size (Max {} Qty {})", _clip_size, to.value()); return result; diff --git a/src/lib/orderman/limiter/SecurityLimitTree.h b/src/lib/orderman/limiter/SecurityLimitTree.h index 61115ad2f..37eba73f5 100644 --- a/src/lib/orderman/limiter/SecurityLimitTree.h +++ b/src/lib/orderman/limiter/SecurityLimitTree.h @@ -50,17 +50,19 @@ public: //////////////////// Limiter::LimiterQueryResult can_send_new_order(AccountID account_id, api::Side side, - Quantity quantity) const{ + Quantity quantity, + bool enforce_clip = true) const{ const SecurityLimitNode* node_ptr = _find_account(account_id.value()); bts_assert(node_ptr!=nullptr); - return node_ptr->can_send_new_order(side, quantity); + return node_ptr->can_send_new_order(side, quantity, enforce_clip); } Limiter::LimiterQueryResult can_update_order(AccountID account_id, api::Side side, - Quantity from, Quantity to) const{ + Quantity from, Quantity to, + bool enforce_clip = true) const{ const SecurityLimitNode* node_ptr = _find_account(account_id.value()); bts_assert(node_ptr!=nullptr); - return node_ptr->can_update_order(side, from, to); + return node_ptr->can_update_order(side, from, to, enforce_clip); } // accessors //////////// diff --git a/src/lib/orderman/order_factory/OrderFactory.cpp b/src/lib/orderman/order_factory/OrderFactory.cpp index 611a71c24..41d7ed745 100644 --- a/src/lib/orderman/order_factory/OrderFactory.cpp +++ b/src/lib/orderman/order_factory/OrderFactory.cpp @@ -9,7 +9,6 @@ // order types #include "lib/orderman/order_factory/DynamicOrderRegistry.h" -#include "lib/orderman/orders/CompositeOrder.h" #include "lib/orderman/orders/DynamicOrder.h" #include "lib/orderman/orders/GLBXNativeOrder.h" #include "lib/orderman/orders/ICENativeOrder.h" @@ -17,15 +16,17 @@ #include "lib/orderman/orders/TriggerOrder.h" #include "lib/orderman/orders/algo/AlgoWrapperOrder.h" #include "lib/orderman/orders/algo/algo_orders/CoverAlgo.h" -#include "lib/orderman/orders/algo/algo_orders/IcebergAlgo.h" #include "lib/orderman/orders/algo/algo_orders/JoinAlgo.h" -#include "lib/orderman/orders/algo/algo_orders/LeanAlgo.h" #include "lib/orderman/orders/algo/algo_orders/TopAlgo.h" +#include "lib/orderman/orders/algo/algo_orders/IcebergAlgo.h" +#include "lib/orderman/orders/algo/algo_orders/LeanAlgo.h" +#include "lib/orderman/orders/algo/algo_orders/SplitterAlgo.h" +#include "lib/orderman/orders/algo/algo_orders/OffsetAlgo.h" #include "lib/orderman/orders/book_algos/OCOOrder.h" -#include "lib/orderman/orders/book_algos/OffsetOrder.h" #include "lib/orderman/orders/book_algos/PricerOrder.h" -#include "lib/orderman/orders/queue_holder/QueueHolderOrder.h" #include "lib/orderman/orders/spreader_orders/SpreaderOrder.h" +#include "lib/orderman/orders/CompositeOrder.h" +#include "lib/orderman/orders/queue_holder/QueueHolderOrder.h" #include #include @@ -202,7 +203,7 @@ Order &OrderFactory::allocate(OrderKey k, OrderKey pk, new orderman::AlgoWrapperOrder(std::make_unique())); break; case CustomOrderDefinition::kOffset: - order_ptr.reset(new orderman::OffsetOrder()); + order_ptr.reset(new orderman::AlgoWrapperOrder(std::make_unique())); break; case CustomOrderDefinition::kQueueHolder: order_ptr.reset(new orderman::QueueHolderOrder()); @@ -227,8 +228,12 @@ Order &OrderFactory::allocate(OrderKey k, OrderKey pk, case CustomOrderDefinition::kComposite: order_ptr.reset(new orderman::CompositeOrder()); break; + case CustomOrderDefinition::kSplitter: + order_ptr.reset( + new orderman::AlgoWrapperOrder(std::make_unique())); + break; case CustomOrderDefinition::CUSTOM_NOT_SET: - bts_assert(false); + bts_assert_msg(false, "Cannot create order with unknown type"); break; } order_ptr->_key = k; diff --git a/src/lib/orderman/orderman_utils/orderman_utils.cpp b/src/lib/orderman/orderman_utils/orderman_utils.cpp index 8099a0b5a..ce9d4af61 100644 --- a/src/lib/orderman/orderman_utils/orderman_utils.cpp +++ b/src/lib/orderman/orderman_utils/orderman_utils.cpp @@ -14,6 +14,7 @@ bool can_consume_attachment(api::orderman::CustomOrderDefinition::CustomCase ord case CustomOrderDefinition::kTop: case CustomOrderDefinition::kCover: case CustomOrderDefinition::kLean: + case CustomOrderDefinition::kOffset: return true; default: return false; @@ -44,10 +45,136 @@ bool should_forward_attachment( return cdef.lean().has_child() && can_consume_attachment(cdef.lean().child().custom_case()); + case CustomOrderDefinition::kOffset: + return + cdef.offset().has_child() && + can_consume_attachment(cdef.offset().child().custom_case()); default: return false; } } +api::orderman::DynamicOrderParam dynamic_parameter(int32_t v) { + api::orderman::DynamicOrderParam res; + res.set_as_int32(v); + return res; +} +api::orderman::DynamicOrderParam dynamic_parameter(uint32_t v) { + api::orderman::DynamicOrderParam res; + res.set_as_uint32(v); + return res; +} +api::orderman::DynamicOrderParam dynamic_parameter(double v) { + api::orderman::DynamicOrderParam res; + res.set_as_double(v); + return res; +} +api::orderman::DynamicOrderParam dynamic_parameter(std::string v) { + api::orderman::DynamicOrderParam res; + res.set_as_string(v); + return res; +} +api::orderman::DynamicOrderParam dynamic_parameter(bool v) { + api::orderman::DynamicOrderParam res; + res.set_as_bool(v); + return res; +} +api::orderman::DynamicOrderParam dynamic_parameter(const api::orderman::CustomOrderDefinition& v) { + api::orderman::DynamicOrderParam res; + res.mutable_as_order_def()->CopyFrom(v); + return res; +} +api::orderman::DynamicOrderParam dynamic_parameter(const api::orderman::DynamicOrderArray& v) { + api::orderman::DynamicOrderParam res; + res.mutable_as_array()->CopyFrom(v); + return res; +} +api::orderman::DynamicOrderParam dynamic_parameter(const api::orderman::DynamicOrderStruct& v) { + api::orderman::DynamicOrderParam res; + res.mutable_as_struct()->CopyFrom(v); + return res; +} +api::orderman::DynamicOrderParam dynamic_parameter(const api::orderman::DynamicOrderEnumerated& v) { + api::orderman::DynamicOrderParam res; + res.mutable_as_enumerated()->CopyFrom(v); + return res; +} +api::orderman::DynamicOrderParam dynamic_array(const std::vector& v) { + api::orderman::DynamicOrderArray a; + for (const auto& item : v) { + a.add_element()->CopyFrom(item); + } + return dynamic_parameter(a); +} +api::orderman::DynamicOrderParam dynamic_map( + const std::vector>& v) { + DynamicOrderStruct res; + auto& map = *res.mutable_field(); + for (const auto& tup : v) { + map[std::get(tup)] = std::get<1>(tup); + } + return dynamic_parameter(res); +} + +// +// Registration + +api::orderman::DynamicOrderParamDescriptor param_int32(const std::string& name) { + api::orderman::DynamicOrderParamDescriptor doap; + doap.set_param_name(name); + doap.mutable_param_type()->set_primitive(api::orderman::DYNAMIC_ORDER_PRIMITIVE_INT); + return doap; +} + +api::orderman::DynamicOrderParamDescriptor param_uint32(const std::string& name) { + api::orderman::DynamicOrderParamDescriptor doap; + doap.set_param_name(name); + doap.mutable_param_type()->set_primitive(api::orderman::DYNAMIC_ORDER_PRIMITIVE_UINT); + return doap; +} + +api::orderman::DynamicOrderParamDescriptor param_double(const std::string& name) { + api::orderman::DynamicOrderParamDescriptor doap; + doap.set_param_name(name); + doap.mutable_param_type()->set_primitive(api::orderman::DYNAMIC_ORDER_PRIMITIVE_DOUBLE); + return doap; +} + +api::orderman::DynamicOrderParamDescriptor param_string(const std::string& name) { + api::orderman::DynamicOrderParamDescriptor doap; + doap.set_param_name(name); + doap.mutable_param_type()->set_primitive(api::orderman::DYNAMIC_ORDER_PRIMITIVE_STRING); + return doap; +} + +api::orderman::DynamicOrderParamDescriptor param_bool(const std::string& name) { + api::orderman::DynamicOrderParamDescriptor doap; + doap.set_param_name(name); + doap.mutable_param_type()->set_primitive(api::orderman::DYNAMIC_ORDER_PRIMITIVE_BOOL); + return doap; +} + +api::orderman::DynamicOrderParamDescriptor param_order(const std::string& name) { + api::orderman::DynamicOrderParamDescriptor doap; + doap.set_param_name(name); + doap.mutable_param_type()->set_primitive(api::orderman::DYNAMIC_ORDER_PRIMITIVE_ORDER_DEF); + return doap; +} +api::orderman::DynamicOrderParamDescriptor param_array(const std::string& name, const DynamicOrderTypeDescriptor& entry) { + api::orderman::DynamicOrderParamDescriptor doap; + doap.set_param_name(name); + doap.mutable_param_type()->mutable_array()->mutable_element_type()->CopyFrom(entry); + return doap; +} +api::orderman::DynamicOrderParamDescriptor param_struct(const std::string& name, const std::vector& entries) { + api::orderman::DynamicOrderParamDescriptor doap; + doap.set_param_name(name); + api::orderman::DynamicOrderStructDescriptor& dstruct = *doap.mutable_param_type()->mutable_structure(); + for (const auto& field : entries) { + *dstruct.add_field() = field; + } + return doap; +} + } // namespace orderman } // namespace bts diff --git a/src/lib/orderman/orderman_utils/orderman_utils.h b/src/lib/orderman/orderman_utils/orderman_utils.h index 50b97ce43..4deda32e9 100644 --- a/src/lib/orderman/orderman_utils/orderman_utils.h +++ b/src/lib/orderman/orderman_utils/orderman_utils.h @@ -1,4 +1,6 @@ #include "lib/api/services/OrderService.pb.h" +#include +#include namespace bts { namespace orderman { @@ -30,5 +32,74 @@ inline bool is_parent_order(const bts::api::orderman::OrderRecord &ord) { bool should_forward_attachment(const api::orderman::CustomOrderDefinition &def); bool can_consume_attachment(api::orderman::CustomOrderDefinition::CustomCase) ; + +api::orderman::DynamicOrderParam dynamic_parameter(int32_t); +api::orderman::DynamicOrderParam dynamic_parameter(uint32_t); +api::orderman::DynamicOrderParam dynamic_parameter(double); +api::orderman::DynamicOrderParam dynamic_parameter(std::string); +api::orderman::DynamicOrderParam dynamic_parameter(bool); +api::orderman::DynamicOrderParam dynamic_parameter(const api::orderman::CustomOrderDefinition&); +api::orderman::DynamicOrderParam dynamic_parameter(const api::orderman::DynamicOrderArray&); +api::orderman::DynamicOrderParam dynamic_parameter(const api::orderman::DynamicOrderStruct&); +api::orderman::DynamicOrderParam dynamic_parameter(const api::orderman::DynamicOrderEnumerated&); +api::orderman::DynamicOrderParam dynamic_array(const std::vector&); +api::orderman::DynamicOrderParam dynamic_map( const std::vector>&); + +template +std::optional get_if(const api::orderman::DynamicOrderParam&) { return nullptr; } + +template <> +inline std::optional get_if(const api::orderman::DynamicOrderParam& p) { + return p.value_case() == api::orderman::DynamicOrderParam::kAsInt32 ? std::optional{p.as_int32()} : std::nullopt; +} +template <> +inline std::optional get_if(const api::orderman::DynamicOrderParam& p) { + return p.value_case() == api::orderman::DynamicOrderParam::kAsUint32 ? std::optional{p.as_uint32()} : std::nullopt; +} +template <> +inline std::optional get_if(const api::orderman::DynamicOrderParam& p) { + return p.value_case() == api::orderman::DynamicOrderParam::kAsDouble ? std::optional{p.as_double()} : std::nullopt; +} +template <> +inline std::optional get_if(const api::orderman::DynamicOrderParam& p) { + return p.value_case() == api::orderman::DynamicOrderParam::kAsBool ? std::optional{p.as_bool()} : std::nullopt; +} +template <> +inline std::optional get_if(const api::orderman::DynamicOrderParam& p) { + return p.value_case() == api::orderman::DynamicOrderParam::kAsOrderDef ? std::optional{p.as_order_def()} : std::nullopt; +} +template <> +inline std::optional get_if(const api::orderman::DynamicOrderParam& p) { + return p.value_case() == api::orderman::DynamicOrderParam::kAsArray ? std::optional{p.as_array()} : std::nullopt; +} +template <> +inline std::optional get_if(const api::orderman::DynamicOrderParam& p) { + return p.value_case() == api::orderman::DynamicOrderParam::kAsStruct ? std::optional{p.as_struct()} : std::nullopt; +} +template <> +inline std::optional get_if(const api::orderman::DynamicOrderParam& p) { + return p.value_case() == api::orderman::DynamicOrderParam::kAsEnumerated ? std::optional{p.as_enumerated()} : std::nullopt; +} + + +template +std::optional get_if(const google::protobuf::Map& map, const std::string& key) { + auto c = map.find(key); + if (c != map.end()) { + return get_if(c->second); + } + return std::nullopt; +} + +api::orderman::DynamicOrderParamDescriptor param_int32(const std::string& name); +api::orderman::DynamicOrderParamDescriptor param_uint32(const std::string& name); +api::orderman::DynamicOrderParamDescriptor param_double(const std::string& name); +api::orderman::DynamicOrderParamDescriptor param_string(const std::string& name); +api::orderman::DynamicOrderParamDescriptor param_bool(const std::string& name); +api::orderman::DynamicOrderParamDescriptor param_order(const std::string& name); +api::orderman::DynamicOrderParamDescriptor param_array(const std::string& name, const api::orderman::DynamicOrderTypeDescriptor&); +api::orderman::DynamicOrderParamDescriptor param_struct(const std::string& name, const std::vector&); + + } // namespace orderman } // namespace bts diff --git a/src/lib/orderman/orders/algo/AlgoWrapperOrder.cpp b/src/lib/orderman/orders/algo/AlgoWrapperOrder.cpp index c3a483969..820fc771e 100644 --- a/src/lib/orderman/orders/algo/AlgoWrapperOrder.cpp +++ b/src/lib/orderman/orders/algo/AlgoWrapperOrder.cpp @@ -16,7 +16,13 @@ namespace orderman { using namespace api::orderman; template using Map = boost::container::flat_map; -enum class AlgoWrapperOrder::Status { INACTIVE, W_ATTACH, WORKING, COMPLETED }; +enum class AlgoWrapperOrder::Status { + INACTIVE, + W_ATTACH, + WORKING, + SHUTTING_DOWN, + COMPLETED +}; class ChildTemplate { public: @@ -49,6 +55,7 @@ struct AlgoWrapperOrder::PImpl { OrderState const &state() const { return _state; } Map book_subs; + Map ticker_subs; Map children; bool has_timer_sub = false; @@ -62,6 +69,7 @@ struct AlgoWrapperOrder::PImpl { OrderKey attach_target = null_order_key(); std::optional pending_update; + std::optional> pending_terminate; OrderStatus external_status() const { return _state.status(); } void set_external_status(OrderStatus s) { @@ -222,6 +230,7 @@ OrderOpResult AlgoWrapperOrder::on_activate(Price p, Quantity q, return {}; case Status::W_ATTACH: + case Status::SHUTTING_DOWN: case Status::WORKING: bts_assert(false); } @@ -238,6 +247,7 @@ OrderOpResult AlgoWrapperOrder::on_update(Update const &update) { me->pending_update = update; return {}; case Status::COMPLETED: + case Status::SHUTTING_DOWN: return {}; case Status::WORKING: { me->set_price_quantity(Price(update.price()), @@ -275,6 +285,7 @@ orderman::OrderOpResult AlgoWrapperOrder::on_detach() { switch (me->status) { case Status::W_ATTACH: case Status::WORKING: + case Status::SHUTTING_DOWN: for (auto &[_, c] : me->children) { (void)_; c.order.mark_detached_from_parent(); @@ -309,6 +320,7 @@ orderman::OrderOpResult AlgoWrapperOrder::on_cancel() { break; } break; + case Status::SHUTTING_DOWN: case Status::COMPLETED: break; case Status::INACTIVE: @@ -347,13 +359,20 @@ Quantity AlgoWrapperOrder::total_fills() const { return Quantity(me->state().filled_quantity()); } +bool AlgoWrapperOrder::is_shutting_down() const { + return me->status == Status::SHUTTING_DOWN; +} + void AlgoWrapperOrder::subscribe_book(SecurityKey k) { if (!is_working()) return; bts_assert(me->book_subs.find(k) == me->book_subs.end()); me->book_subs[k] = me->services->subscribe_to_book( - k, [this, k](auto const &ev) { me->algo->on_book(k, ev.book); }); + k, [this, k](auto const &ev) { + ReportStateGuard _(*this); + me->algo->on_book(k, ev.book); + }); } void AlgoWrapperOrder::unsubscribe_book(SecurityKey k) { @@ -364,6 +383,24 @@ void AlgoWrapperOrder::unsubscribe_book(SecurityKey k) { me->services->unsubscribe_from_book(it->second); } +void AlgoWrapperOrder::subscribe_ticker(SecurityKey k) { + if (!is_working()) return; + bts_assert(me->ticker_subs.find(k) == me->ticker_subs.end()); + + me->ticker_subs[k] = me->services->subscribe_to_ticker( + k, [this,k](const auto& ev) { + ReportStateGuard _(*this); + me->algo->on_ticker(k, ev.trades); + }); +} + +void AlgoWrapperOrder::unsubscribe_ticker(SecurityKey k) { + if (!is_working()) return; + bts_assert(me->ticker_subs.find(k) != me->ticker_subs.end()); + if (auto it = me->ticker_subs.find(k); it != me->ticker_subs.end()) + me->services->unsubscribe_from_ticker(it->second); +} + void AlgoWrapperOrder::subscribe_child_change(OrderKey k) { if (!is_working()) return; @@ -392,7 +429,10 @@ void AlgoWrapperOrder::subscribe_timer() { bts_assert(!me->has_timer_sub); me->has_timer_sub = true; me->services->subscribe_to_time( - [this](auto const &) { me->algo->on_timer(); }); + [this](auto const &) { + ReportStateGuard _(*this); + me->algo->on_timer(); + }); } void AlgoWrapperOrder::unsubscribe_timer() { @@ -485,10 +525,9 @@ void AlgoWrapperOrder::detach_all_children() { } void AlgoWrapperOrder::update_fill_state(Quantity q, Price p) { - if (!is_working()) + if (!is_working() && !is_shutting_down()) return; - ReportStateGuard _(*this); auto previous_qty = me->state().filled_quantity(); if (previous_qty > q.value()) { LOG(OrderMan, Error, @@ -510,35 +549,37 @@ void AlgoWrapperOrder::update_fill_state(Quantity q, Price p) { void AlgoWrapperOrder::set_ladder_visibility(bool is_visible) { if (!is_working()) return; - - ReportStateGuard _(*this); me->set_ladder_visibility(is_visible); } void AlgoWrapperOrder::cancel(std::string const &msg) { if (!is_working()) return; - ReportStateGuard _(*this); - terminate(ORDER_STATUS_CANCELED, msg); + if (working_children_count() == 0) + terminate(ORDER_STATUS_CANCELED, msg); + else + shutdown(ORDER_STATUS_CANCELED, msg); } void AlgoWrapperOrder::terminate_error(std::string const &msg) { - if (!is_working() && me->status != Status::W_ATTACH) + if (!is_working() && !is_shutting_down() && me->status != Status::W_ATTACH) return; - ReportStateGuard _(*this); // If we attached and an error was signaled we do not want to // cancel our child because that child might be a long running // order with a good queue position that we want to preserve if (is_attaching()) me->children.erase(me->attach_target); + terminate(ORDER_STATUS_ERROR, msg); } void AlgoWrapperOrder::complete() { if (!is_working()) return; - ReportStateGuard _(*this); - terminate(ORDER_STATUS_COMPLETED); + if (working_children_count() == 0) + terminate(ORDER_STATUS_COMPLETED); + else + shutdown(ORDER_STATUS_COMPLETED); } // -------------------------------------------------------------------------------- @@ -573,6 +614,7 @@ void AlgoWrapperOrder::on_attach_complete( break; case Status::COMPLETED: break; + case Status::SHUTTING_DOWN: case Status::INACTIVE: case Status::WORKING: bts_assert(false); @@ -587,6 +629,7 @@ void AlgoWrapperOrder::on_child_order_update( switch (me->status) { case Status::WORKING: case Status::W_ATTACH: + case Status::SHUTTING_DOWN: if (auto it = me->children.find(child_key); it != me->children.end()) { auto &child_record = it->second; switch (new_child_state.status()) { @@ -625,16 +668,38 @@ void AlgoWrapperOrder::on_child_order_update( break; } } else - terminate_error( - fmt::format("Unknown child order update: {}", child_key.value())); - return; + LOG(OrderMan, Info, + "AlgoOrder {} received update for presumably detached order {}. " + "Ignoring it", + key().value(), child_key.value()); + break; case Status::COMPLETED: return; case Status::INACTIVE: bts_assert(false); return; } - bts_assert(false); + + if (me->status == Status::SHUTTING_DOWN) + try_complete_shutdown(); +} + +void AlgoWrapperOrder::try_complete_shutdown() { + bts_assert(me->status == Status::SHUTTING_DOWN); + bts_assert(me->children.size()); + bts_assert(me->pending_terminate.has_value()); + + if (working_children_count() == 0) { + LOG(OrderMan, Info, "AlgoOrder {} completed shutdown. Terminated", + key().value()); + terminate(me->pending_terminate.value().first, + me->pending_terminate.value().second); + } else { + LOG(OrderMan, Info, + "AlgoOrder {} still has {} orders working. Shutdown still in " + "progress", + key().value(), working_children_count()); + } } void AlgoWrapperOrder::setup_transitions() { @@ -676,8 +741,21 @@ void AlgoWrapperOrder::setup_transitions() { // From WORKING me->transition_actions[{Status::WORKING, Status::COMPLETED}] = [this]() { cleanup(); + me->children.clear(); _transition(Status::COMPLETED); }; + me->transition_actions[{Status::WORKING, Status::SHUTTING_DOWN}] = [this]() { + cleanup(); + _transition(Status::SHUTTING_DOWN); + }; + + // From SHUTTING_DOWN + me->transition_actions[{Status::SHUTTING_DOWN, Status::COMPLETED}] = + [this]() { + me->children.clear(); + me->pending_terminate.reset(); + _transition(Status::COMPLETED); + }; } void AlgoWrapperOrder::check_invariants() { @@ -697,6 +775,7 @@ void AlgoWrapperOrder::check_invariants() { bts_assert(me->book_subs.empty()); bts_assert(me->children.empty()); bts_assert(!me->has_timer_sub); + bts_assert(!me->pending_terminate.has_value()); break; case Status::WORKING: @@ -704,10 +783,23 @@ void AlgoWrapperOrder::check_invariants() { bts_assert(me->security); bts_assert(me->external_status() == ORDER_STATUS_WORKING); bts_assert(!me->pending_update.has_value()); + bts_assert(!me->pending_terminate.has_value()); + break; + + case Status::SHUTTING_DOWN: + bts_assert(working_children_count()); + bts_assert(me->services); + bts_assert(me->security); + bts_assert(me->external_status() == ORDER_STATUS_WORKING); + bts_assert(me->book_subs.empty()); + bts_assert(!me->has_timer_sub); + bts_assert(!me->pending_update.has_value()); + bts_assert(me->pending_terminate.has_value()); break; case Status::COMPLETED: bts_assert(me->children.empty()); + bts_assert(!me->pending_update.has_value()); bts_assert(me->external_status() == ORDER_STATUS_CANCELED || me->external_status() == ORDER_STATUS_COMPLETED || me->external_status() == ORDER_STATUS_ERROR || @@ -715,6 +807,7 @@ void AlgoWrapperOrder::check_invariants() { bts_assert(me->book_subs.empty()); bts_assert(!me->has_timer_sub); bts_assert(!me->pending_update.has_value()); + bts_assert(!me->pending_terminate.has_value()); break; } } @@ -745,15 +838,25 @@ void AlgoWrapperOrder::terminate(api::orderman::OrderStatus st, transition(Status::COMPLETED); } +void AlgoWrapperOrder::shutdown(api::orderman::OrderStatus st, + std::string const &error) { + bts_assert(!me->pending_terminate.has_value()); + bts_assert(working_children_count()); + me->pending_terminate = std::make_pair(st, error); + LOG(OrderMan, Info, + "AlgoOrder {} is shutting down. Waiting for {} child orders to complete " + "before terminating", + key().value(), working_children_count()); + transition(Status::SHUTTING_DOWN); +} + void AlgoWrapperOrder::cleanup() { for (auto const &s : me->book_subs) { me->services->unsubscribe_from_book(s.second); me->book_subs.clear(); } - for (auto &c : me->children) { + for (auto &c : me->children) c.second.order.cancel(); - me->children.clear(); - } if (me->has_timer_sub) unsubscribe_timer(); me->pending_update.reset(); @@ -785,6 +888,16 @@ OrderKey AlgoWrapperOrder::index_and_launch_child(ChildOrder &&child, Price p, return k; } +size_t AlgoWrapperOrder::working_children_count() const { + auto working_children_count = 0; + for (auto &c : me->children) + if (!c.second.state.has_value() || + c.second.state.value().status == ChildStatus::WORKING) { + working_children_count++; + } + return working_children_count; +} + bool AlgoWrapperOrder::is_working() const { return me->status == Status::WORKING; } @@ -799,6 +912,8 @@ log::CharPtrWrapper AlgoWrapperOrder::state_name(Status st) { return {"INACTIVE"}; case Status::WORKING: return {"WORKING"}; + case Status::SHUTTING_DOWN: + return {"SHUTTING_DOWN"}; } bts_assert(false); return {""}; diff --git a/src/lib/orderman/orders/algo/AlgoWrapperOrder.h b/src/lib/orderman/orders/algo/AlgoWrapperOrder.h index f11a84a23..a2b2b1daa 100644 --- a/src/lib/orderman/orders/algo/AlgoWrapperOrder.h +++ b/src/lib/orderman/orders/algo/AlgoWrapperOrder.h @@ -40,12 +40,15 @@ class AlgoWrapperOrder : public Order, Price price() const override; Quantity quantity() const override; Quantity total_fills() const override; + bool is_shutting_down() const override; void subscribe_book(SecurityKey) override; void unsubscribe_book(SecurityKey) override; void subscribe_child_change(OrderKey) override; void unsubscribe_child_change(OrderKey) override; void subscribe_timer() override; void unsubscribe_timer() override; + void subscribe_ticker(SecurityKey) override; + void unsubscribe_ticker(SecurityKey) override; OrderKey launch_child(ChildTemplate &, Price, Quantity, bool subscribe = true) override; OrderKey launch_child(api::orderman::OrderDefinition const &, Price, @@ -75,10 +78,13 @@ class AlgoWrapperOrder : public Order, void transition(Status); void _transition(Status); void terminate(api::orderman::OrderStatus, std::string const &error = ""); + void shutdown(api::orderman::OrderStatus, std::string const &error = ""); + void try_complete_shutdown(); void cleanup(); void update_child_record(api::orderman::OrderState const &, ChildRecord *); OrderKey index_and_launch_child(ChildOrder &&, Price, Quantity, bool); bool is_working() const ; + size_t working_children_count() const; log::CharPtrWrapper state_name(Status) ; ChildStatus map_child_status(api::orderman::OrderStatus) ; bool _is_attaching(api::orderman::OrderDefinition const &) const ; diff --git a/src/lib/orderman/orders/algo/IAlgoOrder.h b/src/lib/orderman/orders/algo/IAlgoOrder.h index 3b882b49d..e73466444 100644 --- a/src/lib/orderman/orders/algo/IAlgoOrder.h +++ b/src/lib/orderman/orders/algo/IAlgoOrder.h @@ -1,6 +1,7 @@ #pragma once #include "lib/orderman/orders/Order.h" +#include "lib/orderman/events/TickerUpdateEvent.h" #include namespace bts { @@ -36,7 +37,10 @@ class IAlgoOrder { // The book for the specified security has changed virtual void on_book(SecurityKey, Book const &) {} - // A period timer has run out + // Trades stream for the specified security + virtual void on_ticker(SecurityKey, TradeVector const&) {}; + + // A periodic timer has fired virtual void on_timer() {} // -------------------------------------------------------------- diff --git a/src/lib/orderman/orders/algo/IAlgoServices.h b/src/lib/orderman/orders/algo/IAlgoServices.h index 4d9cd2f2f..ef7e14845 100644 --- a/src/lib/orderman/orders/algo/IAlgoServices.h +++ b/src/lib/orderman/orders/algo/IAlgoServices.h @@ -20,6 +20,12 @@ class IAlgoServices : public IBeforeLaunchAlgoServices { // Returns the filled quantity for this algo virtual Quantity total_fills() const = 0; + // Returns wheter the algo is in the process of shutting down (due + // to a cancel, an error, or just having completed its work). When + // algo is shutting down any requests to create, update or cancel + // child orders will be ignored. + virtual bool is_shutting_down() const = 0; + // -------------------------------------------------------------- // Event Subscriptions // -------------------------------------------------------------- @@ -38,6 +44,10 @@ class IAlgoServices : public IBeforeLaunchAlgoServices { virtual void subscribe_timer() = 0; virtual void unsubscribe_timer() = 0; + // Subscribe to trade ticker + virtual void subscribe_ticker(SecurityKey) = 0; + virtual void unsubscribe_ticker(SecurityKey) = 0; + // -------------------------------------------------------------- // Child management // -------------------------------------------------------------- @@ -86,7 +96,7 @@ class IAlgoServices : public IBeforeLaunchAlgoServices { // Set wheter this algo instance should be displayed in the Spark UI ladder virtual void set_ladder_visibility(bool is_visible) = 0; - + // Update this algos's filled quantity and last price virtual void update_fill_state(Quantity total_fills, Price last_price) = 0; }; diff --git a/src/lib/orderman/orders/algo/IBeforeLaunchAlgoServices.cpp b/src/lib/orderman/orders/algo/IBeforeLaunchAlgoServices.cpp index 87f753e91..426290ef4 100644 --- a/src/lib/orderman/orders/algo/IBeforeLaunchAlgoServices.cpp +++ b/src/lib/orderman/orders/algo/IBeforeLaunchAlgoServices.cpp @@ -1 +1,8 @@ #include "lib/orderman/orders/algo/IBeforeLaunchAlgoServices.h" +namespace bts { +namespace orderman { + +TimeStamp IBeforeLaunchAlgoServices::get_time() const { return Timer::now(); } + +} // namespace orderman +} // namespace bts diff --git a/src/lib/orderman/orders/algo/IBeforeLaunchAlgoServices.h b/src/lib/orderman/orders/algo/IBeforeLaunchAlgoServices.h index 733f60d7c..3756971db 100644 --- a/src/lib/orderman/orders/algo/IBeforeLaunchAlgoServices.h +++ b/src/lib/orderman/orders/algo/IBeforeLaunchAlgoServices.h @@ -3,6 +3,7 @@ #include "lib/api/services/OrderService.pb.h" #include "lib/domain_model/SecurityKey.h" #include "lib/orderman/orderman_utils/orderman_common.h" +#include "lib/utils/bts_chrono.h" namespace bts { namespace orderman { @@ -42,6 +43,8 @@ class IBeforeLaunchAlgoServices { virtual ChildTemplate & make_child_template(api::orderman::OrderDefinition const &child_def, size_t reserve = 1) = 0; + + virtual TimeStamp get_time() const; }; } // namespace orderman diff --git a/src/lib/orderman/orders/algo/algo_orders/CoverAlgo.cpp b/src/lib/orderman/orders/algo/algo_orders/CoverAlgo.cpp index 783bcd960..80f85d7d1 100644 --- a/src/lib/orderman/orders/algo/algo_orders/CoverAlgo.cpp +++ b/src/lib/orderman/orders/algo/algo_orders/CoverAlgo.cpp @@ -10,18 +10,8 @@ namespace orderman { using namespace api::orderman; -enum class CoverAlgo::Status { WORKING, WAITING_FOR_HEDGES, DONE }; -const char *CoverAlgo::status_name(Status st) { - switch (st) { - case Status::WORKING: - return "WORKING"; - case Status::WAITING_FOR_HEDGES: - return "WAITING_FOR_HEDGES"; - case Status::DONE: - return "Done"; - } - return nullptr; -} +enum class CoverAlgo::Status { WORKING, WAITING_FOR_HEDGES, DONE, CANCELING }; + struct CoverAlgo::PImpl { IAlgoServices *algo; OrderKey child_key = null_order_key(); @@ -92,8 +82,11 @@ void CoverAlgo::do_launch_attached(IAlgoServices &algo, OrderKey k, on_child_change(k, st); } -void CoverAlgo::on_child_change(OrderKey k, ChildState const &st) { - if (k == me->child_key) { +void CoverAlgo::on_covered_change(ChildState const &st) { + // First launch hedge if appropriate + switch (me->status) { + case Status::WORKING: + case Status::CANCELING: if (st.total_fills.value() > me->hedged_fills.value()) { auto hedge_qty = Quantity(st.total_fills.value() - me->hedged_fills.value()); @@ -116,6 +109,15 @@ void CoverAlgo::on_child_change(OrderKey k, ChildState const &st) { hedge_qty.value(), hedge_price.value()); } algo().update_fill_state(st.total_fills, st.last_price); + break; + case Status::WAITING_FOR_HEDGES: + case Status::DONE: + break; + } + + // Now analyze child to figure out wheter we should terminate + switch (me->status) { + case Status::WORKING: switch (st.status) { case ChildStatus::WORKING: break; @@ -125,36 +127,67 @@ void CoverAlgo::on_child_change(OrderKey k, ChildState const &st) { algo().order_key().value()); algo().set_ladder_visibility(false); me->status = Status::WAITING_FOR_HEDGES; + terminate_if_hedges_complete(); break; case ChildStatus::CANCELED: - LOG(OrderMan, Info, "CoverAlgo {}. Child was canceled, terminating", + LOG(OrderMan, Info, "CoverAlgo {}. Child was unexpectadly canceled, terminating", algo().order_key().value()); - me->status = Status::DONE; + on_done(); algo().terminate_error("Unexpected child cancel"); break; } - } else { + break; + case Status::CANCELING: switch (st.status) { case ChildStatus::WORKING: break; case ChildStatus::CANCELED: - if (me->cancel_on_external_hedge_cancel) { - LOG(OrderMan, Info, "CoverAlgo {}. Hedge was canceled, terminating", - algo().order_key().value()); - me->status = Status::DONE; - algo().terminate_error("Unexpected child cancel"); - } else - me->hedges.erase( - std::remove(me->hedges.begin(), me->hedges.end(), k), - me->hedges.end()); - break; case ChildStatus::COMPLETED: - me->hedges.erase(std::remove(me->hedges.begin(), me->hedges.end(), k), - me->hedges.end()); + LOG(OrderMan, Info, "CoverAlgo {}. Shutdown complete", + algo().order_key().value()); + on_done(); + algo().cancel(); break; } + break; + case Status::WAITING_FOR_HEDGES: + case Status::DONE: + break; } - check_terminate(); +} + +void CoverAlgo::on_hedge_change(OrderKey k, ChildState const &st) { + + // First adjust our hedge count or cancel if something went wrong + switch (st.status) { + case ChildStatus::WORKING: + break; + case ChildStatus::COMPLETED: + me->hedges.erase(std::remove(me->hedges.begin(), me->hedges.end(), k), + me->hedges.end()); + break; + case ChildStatus::CANCELED: + if (me->cancel_on_external_hedge_cancel) { + LOG(OrderMan, Info, "CoverAlgo {}. Hedge was canceled, terminating", + algo().order_key().value()); + on_done(); + algo().terminate_error("Unexpected child cancel"); + return; + } else + me->hedges.erase(std::remove(me->hedges.begin(), me->hedges.end(), k), + me->hedges.end()); + break; + } + + // Now if we are waiting for hedges to complete, bail + terminate_if_hedges_complete(); +} + +void CoverAlgo::on_child_change(OrderKey k, ChildState const &st) { + if (k == me->child_key) + on_covered_change(st); + else + on_hedge_change(k, st); } void CoverAlgo::do_update(api::orderman::Update const &update) { @@ -211,7 +244,7 @@ void CoverAlgo::update_definition( cover.cancel_on_external_hedge_cancel(); } -void CoverAlgo::check_terminate() { +void CoverAlgo::terminate_if_hedges_complete() { LOG(OrderMan, Diag, "CoverAlgo {} check_terminate. State: {} Hedge count: {}", algo().order_key(), log::CharPtrWrapper{status_name(me->status)}, @@ -220,15 +253,61 @@ void CoverAlgo::check_terminate() { if (me->status == Status::WAITING_FOR_HEDGES && me->hedges.empty()) { LOG(OrderMan, Info, "CoverAlgo {}. Last hedge completed, terminating", algo().order_key().value()); + on_done(); algo().complete(); } } IAlgoOrder::OnCancelResponse CoverAlgo::on_cancel() { + switch (me->status) { + case Status::WORKING: { + algo().cancel_child(me->child_key); + LOG(OrderMan, Info, + "CoverAlgo {} received cancel. Canceling child and waiting for an " + "ack", + algo().order_key()); + me->status = Status::CANCELING; + return IAlgoOrder::OnCancelResponse::IGNORE; + } + case Status::CANCELING: + return IAlgoOrder::OnCancelResponse::IGNORE; + case Status::WAITING_FOR_HEDGES: + maybe_detach_hedges(); + return IAlgoOrder::OnCancelResponse::HONOR; + case Status::DONE: + bts_assert(false); + return IAlgoOrder::OnCancelResponse::HONOR; + } + + bts_assert(false); + return IAlgoOrder::OnCancelResponse::HONOR; +} + +void CoverAlgo::on_done() +{ + me->status = Status::DONE; + maybe_detach_hedges(); +} + + +void CoverAlgo::maybe_detach_hedges() { if (!me->cancel_hedges_on_cancel) for (auto &k : me->hedges) algo().detach_child(k); - return IAlgoOrder::OnCancelResponse::HONOR; +} + +const char *CoverAlgo::status_name(Status st) { + switch (st) { + case Status::WORKING: + return "WORKING"; + case Status::WAITING_FOR_HEDGES: + return "WAITING_FOR_HEDGES"; + case Status::DONE: + return "DONE"; + case Status::CANCELING: + return "CANCELING"; + } + return nullptr; } } // namespace orderman diff --git a/src/lib/orderman/orders/algo/algo_orders/CoverAlgo.h b/src/lib/orderman/orders/algo/algo_orders/CoverAlgo.h index 24fb06cf5..7514ae5cd 100644 --- a/src/lib/orderman/orders/algo/algo_orders/CoverAlgo.h +++ b/src/lib/orderman/orders/algo/algo_orders/CoverAlgo.h @@ -30,8 +30,13 @@ private: api::orderman::CoverOrderDefinition const &); void init_hedge_template(IBeforeLaunchAlgoServices &, api::orderman::CustomOrderDefinition const &); - void check_terminate(); + void terminate_if_hedges_complete(); + + void on_hedge_change(OrderKey hedge, ChildState const&) ; + void on_covered_change(ChildState const&); const char *status_name(Status st); + void maybe_detach_hedges(); + void on_done(); }; } // namespace orderman diff --git a/src/lib/orderman/orders/algo/algo_orders/DormanAlgo.cpp b/src/lib/orderman/orders/algo/algo_orders/DormanAlgo.cpp new file mode 100644 index 000000000..ca4c9acee --- /dev/null +++ b/src/lib/orderman/orders/algo/algo_orders/DormanAlgo.cpp @@ -0,0 +1,374 @@ +#include "DormanAlgo.h" +#include "lib/api/services/OrderService.pb.h" +#include "lib/domain_model/Security.h" +#include "lib/logging/logging.h" +#include "lib/orderman/order_factory/IOrderFactory.h" +#include "lib/orderman/orderman_utils/Book.h" +#include "lib/orderman/orders/algo/IAlgoServices.h" +#include "lib/utils/PBUtils.h" +#include "lib/orderman/orderman_utils/orderman_utils.h" +#include "lib/orderman/orders/algo/utils/Accumulator.h" + +namespace bts { +namespace orderman { +namespace dorman { + +using namespace api::orderman; + +struct FutureQty { + Quantity size; + TimeStamp when; +}; + +struct DormanAlgo::Impl { + IAlgoServices *algo = nullptr; + AlgoParams params; + api::Side side; + Quantity exposed_qty = {}; + std::optional next_size; + + OrderDefinition splitter_definition; + OrderKey child_id; + ChildState child_state; + + uint32_t fills ={}; + + std::vector profit_orders; + std::optional trade_accumulator; + + std::optional pause_until; +}; + +DormanAlgo::DormanAlgo() : + me(new Impl()) +{ } +DormanAlgo::~DormanAlgo() { } + +OrderOpResult DormanAlgo::do_before_launch(IBeforeLaunchAlgoServices& ibl) { + const auto& def = ibl.definition(); + // Validation + bts_assert(def.has_custom_def()); + bts_assert(def.custom_def().has_dynamic()); + + auto params = to_dorman_params(def.custom_def().dynamic()); + if (auto error = std::get_if(¶ms)) { + return *error; + } + me->params = std::get(params); + me->side = def.side(); + if (me->side != api::Side::SIDE_BUY && me->side != api::Side::SIDE_SELL) { + return OrderOpResult { ORDER_ERROR_INVALID_ORDER_DEFINITON, "Invalid side" }; + } + + if (me->params.trade_accumulation) { + auto& t = me->params.trade_accumulation.value(); + me->trade_accumulator = {Accumulator {t.contracts, t.seconds, ibl.get_time()}}; + } + + CustomOrderDefinition default_order; + default_order.mutable_limit(); + auto child_def = + get_if(def.custom_def().dynamic().order_params(), "Child") + .value_or(default_order); + + update_child_template(ibl, child_def); + + if (ibl.is_attaching()) { + return OrderOpResult{ORDER_ERROR_OP_NOT_SUPPORTED, + "This algo does not support attach."}; + } + + return {/*ok*/}; +} + + +void DormanAlgo::do_launch(IAlgoServices& algo) { + me->algo = &algo; + auto skey = SecurityKey(algo.security().id()); + algo.subscribe_book(skey); + algo.subscribe_timer(); + if (me->trade_accumulator) { + algo.subscribe_ticker(skey); + } +} + +void DormanAlgo::do_launch_attached(IAlgoServices&, OrderKey, ChildState) { + bts_assert(false); +} +void DormanAlgo::do_update(const api::orderman::Update& update) { + LOG(OrderMan, Diag, "Dorman order {} receiving update: {}", + algo().order_key().value(), utils::pb::to_string(update)); + + if (update.has_custom()) { + if (!update.custom().has_dynamic()) { + algo().cancel("Update cannot change order type"); + return; + } + auto params = to_dorman_params(update.custom().dynamic()); + + if (auto error = std::get_if(¶ms)) { + algo().cancel(error->error_msg); + } + + me->params = std::get(params); + auto skey = SecurityKey(algo().security().id()); + if (me->params.trade_accumulation && !me->trade_accumulator) { + algo().subscribe_ticker(skey); + auto& t = me->params.trade_accumulation.value(); + me->trade_accumulator = {Accumulator {t.contracts, t.seconds, algo().get_time()}}; + } + else if (!me->params.trade_accumulation && me->trade_accumulator) { + algo().unsubscribe_ticker(skey); + me->trade_accumulator = std::nullopt; + } + else if (me->params.trade_accumulation && + me->trade_accumulator && + (me->trade_accumulator.value().threshold != me->params.trade_accumulation.value().contracts || + me->trade_accumulator.value().bucket_interval != std::chrono::seconds(me->params.trade_accumulation.value().seconds)) + ) { + // accumulator params have changed + auto& t = me->params.trade_accumulation.value(); + me->trade_accumulator = {Accumulator {t.contracts, t.seconds, algo().get_time()}}; + } + + // Check for child + auto child = get_if(update.custom().dynamic().order_params(), "Child"); + if (child) { + update_child_template(algo(), child.value()); + } + } + + if (me->fills >= update.quantity()) { + complete(); + } + else { + // In case of price/qty change always update the child + // NOTE: it might be useful to cache the available market data size + // and update based on that if the update contains a size table change + update_child(me->exposed_qty); + } +} + +void DormanAlgo::on_child_change(OrderKey key, const ChildState& state) { + if (key != me->child_id) return; + + if (state.status == ChildStatus::CANCELED && !is_paused()) { + algo().cancel("Unexpected child cancel"); + return; + } + + auto delta = state.total_fills.value() - me->child_state.total_fills.value(); + if (delta != 0) { + me->fills += delta; + algo().update_fill_state(Quantity {me->fills}, state.last_price); + send_profit_order(state.last_price, Quantity {delta}); + } + me->child_state = state; + + if (me->params.scratch && + me->fills >= me->params.scratch.value().max_overfill) { + do_scratch(); + } + + if (me->fills >= algo().quantity().value() || state.status == ChildStatus::COMPLETED) { + complete(); + } +} + +void DormanAlgo::on_book(SecurityKey key, const Book& book) { + if (key.value() != me->algo->security().id()) { return; } + auto price = me->algo->price(); + auto tick = me->algo->security().contract_spec().low_tick(); + + // Determine available size on the market + auto& side = book.side(me->side); + uint32_t available = 0; + for (const auto& level : side) { + if (securities::test_price_equality(level.price(), price.value(), tick)) { + available += level.size(); + break; + } + } + + // Check the step_table + auto target = Quantity {0}; + if (available > 0) { + for (const auto& entry : me->params.steps.step_table) { + target = Quantity { std::get<1>(entry) }; + if (std::get<0>(entry) > available) { + break; + } + } + } + target = Quantity { std::min(target.value(), me->params.steps.maximum) }; + + // First on_book + if (!child_active()) { + update_child(target); + } + // See if we need to adjust + else { + if (target == me->exposed_qty) { + me->next_size = std::nullopt; + } + // If target < size reduce immediately + else if (target < me->exposed_qty) { + update_child(target); + me->next_size = std::nullopt; + } + // if target > size schedule increase + else if (target > me->exposed_qty) { + if (me->next_size) { + // already planning to increase, update the target + me->next_size.value().size = target; + } + else { + // no existing scheduled increase, create one now + me->next_size = { FutureQty { target, me->algo->get_time() + std::chrono::seconds(me->params.steps.trail_seconds) } }; + } + } + } +} +void DormanAlgo::on_timer() { + // Check for pending size changes + auto now = algo().get_time(); + if (me->pause_until && + now >= me->pause_until.value()) { + + resume(); + } + if (me->next_size && + now >= me->next_size.value().when) { + auto size = me->next_size.value().size; + update_child(size); + me->next_size = std::nullopt; + } +} +void DormanAlgo::on_ticker(SecurityKey, const TradeVector& vec) { + if (me->trade_accumulator) { + size_t contracts = 0; + for (const auto& t : vec) { + contracts += t.quantity.value(); + } + auto& acc = me->trade_accumulator.value(); + acc.push_back(algo().get_time(), contracts); + if (acc.exceeded()) { + auto delay = std::chrono::seconds(me->params.trade_accumulation.value().sleep_seconds); + pause_for(delay); + } + } +} + +IAlgoOrder::OnCancelResponse DormanAlgo::on_cancel() { + // detach any remaining profit orders so they keep running + for(const auto& key : me->profit_orders) { + algo().detach_child(key); + } + return OnCancelResponse::HONOR; +} + +IAlgoServices& DormanAlgo::algo() const { return *me->algo; } + +// +// private + +void DormanAlgo::update_child(Quantity exposed) { + auto& splitter = *me->splitter_definition.mutable_custom_def()->mutable_splitter(); + splitter.set_max_order_qty(me->params.clip_limit); + me->exposed_qty = exposed; + splitter.set_exposed_qty(is_paused() ? 0 : exposed.value()); + + if (!child_active()) { + // launch child + me->child_id = algo().launch_child(me->splitter_definition, algo().price(), algo().quantity()); + } + else { + Update update; + update.set_price(algo().price().value()); + update.set_quantity(algo().quantity().value()); + *update.mutable_custom()->mutable_splitter() = splitter; + algo().update_child(me->child_id, update); + } +} + +void DormanAlgo::update_child_template(IBeforeLaunchAlgoServices& ibl, const CustomOrderDefinition& child) { + auto child_def = ibl.definition(); + // Insert a spliter between this and the configured child definition. + api::orderman::SplitterOrderDefinition& splitter_def = *child_def.mutable_custom_def()->mutable_splitter(); + *splitter_def.mutable_child() = child; + me->splitter_definition = child_def; +} + +Quantity DormanAlgo::exposed_qty() const { return me->exposed_qty; } + +bool DormanAlgo::child_active() const { return me->child_id.value() != 0; } + +void DormanAlgo::do_scratch() { + // When the # of fills exceeds the max overfill quantity + // We enter fills opposite fills at the fill price and terminate + // Use a splitter to bypass clip limit if necessary + + OrderDefinition def = me->splitter_definition; + def.set_side(securities::other_side(me->side)); + def.mutable_custom_def()->mutable_splitter()->set_exposed_qty(me->fills); + + auto price = me->child_state.last_price; + auto scratch_id = algo().launch_child(def, price, Quantity { me->fills }, false); + + algo().detach_child(scratch_id); + complete(); +} + +void DormanAlgo::send_profit_order(Price p, Quantity q) { + if (me->params.profit_orders) { + auto side = securities::other_side(me->side); + Price offset = + Price { algo().security().contract_spec().low_tick() * me->params.profit_orders.value().ticks_away }; + auto price = + securities::worsen_price(side, p, offset); + + OrderDefinition def = me->splitter_definition; + // BTS Simulator does not currently support GTC + // Note: This could be made a configurable order type in the algo parameters + def.set_time_in_force(TimeInForce::TIME_IN_FORCE_DAY); + def.set_side(side); + def.mutable_custom_def()->mutable_limit(); + me->profit_orders.push_back( + algo().launch_child(def, price, q, false)); + } +} + +void DormanAlgo::complete() { + // Allow profit orders to keep running + if (me->params.profit_orders) { + for(const auto& key : me->profit_orders) { + algo().detach_child(key); + } + } + algo().complete(); +} + + +void DormanAlgo::pause_for(Duration d) { + if (me->params.trade_accumulation) { + if (is_paused()) { + me->pause_until = std::max(me->pause_until.value(), { algo().get_time() + d }); + } + else { + me->pause_until = { algo().get_time() + d }; + // When paused child update will always expose 0 + update_child(me->exposed_qty); + } + } +} +bool DormanAlgo::is_paused() const { + return (bool)(me->pause_until); +} +void DormanAlgo::resume() { + if (is_paused()) { + me->pause_until = std::nullopt; + update_child(me->exposed_qty); + } +} + +}}} //namespace diff --git a/src/lib/orderman/orders/algo/algo_orders/DormanAlgo.h b/src/lib/orderman/orders/algo/algo_orders/DormanAlgo.h new file mode 100644 index 000000000..64111a5d8 --- /dev/null +++ b/src/lib/orderman/orders/algo/algo_orders/DormanAlgo.h @@ -0,0 +1,96 @@ +#pragma once + +#include "lib/orderman/orders/algo/IAlgoOrder.h" +#include +#include +#include +#include + +namespace bts { +namespace orderman { +namespace dorman { + +// +// Algo parameters +// + +// This defines the market quantity vs the order quantity +struct StepParams { + std::vector> step_table; + // This is the cap on the maximum quantity, + // even though the step table can allow for higher quantities (depending on the + // clip size this could be divided across multiple exchange orders) + uint32_t maximum; + // If the market increases don't follow until it has stayed high for trail_seconds; + uint32_t trail_seconds; +}; + +// If the number of accumulated fills (net of profit orders) touches or goes beyond +// this, the fills will be scratched by entering opposite orders at the fill price +struct ScratchParams { + uint32_t max_overfill; +}; +// If this is enabled, then as fills accumulate, profit GTC orders are +// immediately sent N ticks away on the opposite side. +struct ProfitOrderParams { + uint32_t ticks_away; +}; +// If N contracts trade in T seconds, then the algo is paused (ie child exchange order +// is/are cancelled) for X seconds. Once the condition clears order is reentered. +struct TradeAccumulationParams { + uint32_t contracts; + uint32_t seconds; + uint32_t sleep_seconds; +}; +struct AlgoParams { + StepParams steps; + uint32_t clip_limit; + std::optional scratch; + std::optional profit_orders; + std::optional trade_accumulation; +}; + + +class DormanAlgo : public IAlgoOrder { + struct Impl; + std::unique_ptr me; + IAlgoServices& algo() const; + +public: + DormanAlgo(); + ~DormanAlgo(); + + OrderOpResult do_before_launch(IBeforeLaunchAlgoServices &) override; + void do_launch(IAlgoServices &) override; + void do_launch_attached(IAlgoServices &, OrderKey, ChildState) override; + void do_update(api::orderman::Update const &) override; + void on_child_change(OrderKey, ChildState const &) override; + void on_book(SecurityKey, const Book&) override; + void on_timer() override; + void on_ticker(SecurityKey, const TradeVector&) override; + IAlgoOrder::OnCancelResponse on_cancel() override; + + Quantity exposed_qty() const; + +private: + // + // Child management + void update_child_template(IBeforeLaunchAlgoServices&, const api::orderman::CustomOrderDefinition&); + void update_child(Quantity exposed); + bool child_active() const; + + void do_scratch(); + void send_profit_order(Price, Quantity); + void complete(); + + void pause_for(Duration); + bool is_paused() const; + void resume(); + +}; + +template +using Result = std::variant; +Result to_dorman_params(const api::orderman::DynamicOrderDefinition&); + +}}} //namespace diff --git a/src/lib/orderman/orders/algo/algo_orders/IcebergAlgo.cpp b/src/lib/orderman/orders/algo/algo_orders/IcebergAlgo.cpp index f93231c57..897a366ff 100644 --- a/src/lib/orderman/orders/algo/algo_orders/IcebergAlgo.cpp +++ b/src/lib/orderman/orders/algo/algo_orders/IcebergAlgo.cpp @@ -145,7 +145,7 @@ void IcebergAlgo::on_child_change(OrderKey k, ChildState const &st) { on_timer(); return; case ChildStatus::CANCELED: - algo().terminate_error("Unexpected child cancel"); + algo().cancel("Unexpected child cancel"); return; } } diff --git a/src/lib/orderman/orders/algo/algo_orders/OffsetAlgo.cpp b/src/lib/orderman/orders/algo/algo_orders/OffsetAlgo.cpp new file mode 100644 index 000000000..3b09525cb --- /dev/null +++ b/src/lib/orderman/orders/algo/algo_orders/OffsetAlgo.cpp @@ -0,0 +1,190 @@ +#include "lib/orderman/orders/algo/IAlgoServices.h" +#include "lib/orderman/orders/algo/algo_orders/OffsetAlgo.h" +#include "lib/orderman/order_factory/IOrderFactory.h" +#include "lib/orderman/orders/spreader_orders/spreader/SpreaderUtils.h" +#include "lib/os/os.h" +#include "lib/utils/PBUtils.h" + +namespace bts { +namespace orderman { + +using namespace api::orderman; +using namespace std; + +enum class OffsetAlgo::Status { WAITING_FOR_MD, DEPLOYED }; + +struct OffsetAlgo::PImpl { + IAlgoServices *algo; + Status status = Status::WAITING_FOR_MD; + OrderKey child_key = null_order_key(); + ChildTemplate *child_template = nullptr; + Book last_book; + uint32_t tick_offset_on_deploy; + double child_price = os::nan(); +}; + +OffsetAlgo::OffsetAlgo() : me(new PImpl()) {} +OffsetAlgo::~OffsetAlgo() {} +IAlgoServices &OffsetAlgo::algo() const { return *me->algo; } + +OrderOpResult OffsetAlgo::do_before_launch(IBeforeLaunchAlgoServices &serv) { + auto &def = serv.definition(); + bts_assert(def.has_custom_def()); + bts_assert(def.custom_def().has_offset()); + // Validate the offset part of the definition + auto &offset = def.custom_def().offset(); + if (!offset.has_child() && + OrderKey(def.attach_target_order_key()) == null_order_key()) { + return OrderOpResult{ + ORDER_ERROR_INVALID_ORDER_DEFINITON, + "Offset order must have either a child order definition or an " + "order to attach to"}; + } + me->tick_offset_on_deploy = offset.tick_offset_on_deploy(); + if (!serv.is_attaching()) { + // if we are not attaching create a child template so we can + // quickly launch our child when the time comes. + auto child_def = def; + *child_def.mutable_custom_def() = def.custom_def().offset().child(); + me->child_template = &(serv.make_child_template(child_def)); + } + return {}; +} + +void OffsetAlgo::do_launch(IAlgoServices &algo) { + me->algo = &algo; + bts_assert(me->child_template); + algo.subscribe_book(SecurityKey(algo.security().id())); +} + +void OffsetAlgo::do_launch_attached(IAlgoServices &algo, OrderKey k, + ChildState st) { + me->algo = &algo; + me->child_key = k; + me->child_price = st.price.value(); + algo.subscribe_book(SecurityKey(algo.security().id())); +} + +void OffsetAlgo::do_update(api::orderman::Update const &update) { + LOG(OrderMan, Diag, "OffsetAlgo order {} receiving update: {}", + algo().order_key().value(), utils::pb::to_string(update)); + if (update.has_custom()) { + if (!update.custom().has_offset()) { + algo().terminate_error("Invalid update"); + return; + } + me->tick_offset_on_deploy = + update.custom().offset().tick_offset_on_deploy(); + } + switch (me->status) { + case Status::WAITING_FOR_MD: + if (update.has_custom()) { + if (algo().is_attaching()) { + // don't change the child's price + // (need md to determine initial deploy price) + bts_assert(!os::is_nan(me->child_price)); + algo().update_child(me->child_key, mk_child_update(update, me->child_price)); + } else { + auto &def = algo().definition(); + auto child_def = def; + *child_def.mutable_custom_def() = update.custom().offset().child(); + me->child_template = &(algo().make_child_template(child_def)); + } + } + break; + case Status::DEPLOYED: + algo().update_child(me->child_key, mk_child_update(update)); + break; + } +} + +void OffsetAlgo::on_book(SecurityKey, Book const &b) { + me->last_book = b; + // log top of book + LOG(OrderMan, Diag, "OffsetAlgo order {} on book. BID: {}@{} ASK: {}@{}", + algo().order_key().value(), b.side(api::SIDE_BUY)[0].size(), + b.side(api::SIDE_BUY)[0].price(), b.side(api::SIDE_SELL)[0].size(), + b.side(api::SIDE_SELL)[0].price()); + // attempt deploy + if (me->status == Status::WAITING_FOR_MD) + try_deploy(); +} + +Update OffsetAlgo::mk_child_update(const Update &source, const double &price_override) { + Update target; + if (source.has_custom()) { + bts_assert(source.custom().has_offset()); + *target.mutable_custom() = source.custom().offset().child(); + } + if (!os::is_nan(price_override)) + target.set_price(price_override); + else + target.set_price(source.price()); + target.set_quantity(source.quantity()); + return target; +} + +void OffsetAlgo::try_deploy() { + bts_assert(me->status == Status::WAITING_FOR_MD); + auto side = algo().definition().side(); + auto &top_psz = me->last_book.side(side)[0]; + if (!top_psz.size()) { + // log state (still waiting for md) + LOG(OrderMan, Diag, + "OffsetAlgo order {} still waiting for md. Order: {} {}@ {} ticks " + "away.", + algo().order_key().value(), algo().definition().side(), + algo().quantity().value(), me->tick_offset_on_deploy); + + } else { + const double &offset = securities::tick_size(algo().security()).value() * + me->tick_offset_on_deploy; + me->child_price = + securities::worsen_price(side, Price(top_psz.price()), Price(offset)).value(); + if (algo().is_attaching()) { + // log imminent update + LOG(OrderMan, Diag, + "OffsetAlgo order {} attaching child {}. Order: {} {}@ {} ({} ticks " + "away).", + algo().order_key().value(), + me->child_key.value(), side, + algo().quantity().value(), + me->child_price, me->tick_offset_on_deploy); + // update + Update child_update; + child_update.set_quantity(algo().quantity().value()); + child_update.set_price(me->child_price); + algo().update_child(me->child_key, child_update); + } else { + // log imminent deploy + LOG(OrderMan, Diag, + "OffsetAlgo order {} deploying child. Order: {} {}@ {} ({} ticks " + "away).", + algo().order_key().value(), side, + algo().quantity().value(), + me->child_price, me->tick_offset_on_deploy); + // deploy + me->child_key = + algo().launch_child(*me->child_template, Price(me->child_price), algo().quantity()); + } + algo().set_ladder_visibility(false); + me->status = Status::DEPLOYED; + } +} + +void OffsetAlgo::on_child_change(OrderKey, ChildState const &st) { + me->child_price = st.price.value(); + algo().update_fill_state(st.total_fills, st.last_price); + switch (st.status) { + case ChildStatus::WORKING: + return; + case ChildStatus::COMPLETED: + algo().complete(); + return; + case ChildStatus::CANCELED: + algo().cancel("Unexpected child cancel"); + return; + } +} +} // namespace orderman +} // namespace bts diff --git a/src/lib/orderman/orders/algo/algo_orders/OffsetAlgo.h b/src/lib/orderman/orders/algo/algo_orders/OffsetAlgo.h new file mode 100644 index 000000000..4e73e2f23 --- /dev/null +++ b/src/lib/orderman/orders/algo/algo_orders/OffsetAlgo.h @@ -0,0 +1,38 @@ +#pragma once + +#include "lib/orderman/orders/algo/IAlgoOrder.h" + +using Update = bts::api::orderman::Update; + +namespace bts { +namespace orderman { + +// Offset sends a child-order/algo N ticks away from the top of the book. +// If necessary, it will wait for the market to arrive before shooting the +// child. + +class OffsetAlgo : public IAlgoOrder { + public: + OffsetAlgo(); + ~OffsetAlgo(); + + OrderOpResult do_before_launch(IBeforeLaunchAlgoServices &) override; + void do_launch(IAlgoServices &) override; + void do_launch_attached(IAlgoServices &, OrderKey, ChildState) override; + void do_update(Update const &) override; + void on_book(SecurityKey, Book const &) override; + void on_child_change(OrderKey, ChildState const &) override; + + private: + enum class Status; + struct PImpl; + std::unique_ptr me; + + static Update mk_child_update(const Update& source_update, const double& price_override = os::nan()); + + IAlgoServices &algo() const; + void try_deploy(); +}; + +} // namespace orderman +} // namespace bts \ No newline at end of file diff --git a/src/lib/orderman/orders/algo/algo_orders/RegisterDorman.cpp b/src/lib/orderman/orders/algo/algo_orders/RegisterDorman.cpp new file mode 100644 index 000000000..0125d4e25 --- /dev/null +++ b/src/lib/orderman/orders/algo/algo_orders/RegisterDorman.cpp @@ -0,0 +1,118 @@ + +#include "lib/plugin/SparkServices.h" +#include "lib/api/services/OrderService.pb.h" +#include "lib/logging/logging.h" +#include "lib/orderman/orderman_utils/orderman_utils.h" +#include "DormanAlgo.h" + +using namespace bts; +using namespace api::orderman; +using namespace orderman::dorman; +using namespace orderman; + +namespace bts { +namespace orderman { +namespace dorman { + +template +using Result = std::variant; +Result bad_definition(const char* msg) { return {OrderOpResult{ORDER_ERROR_INVALID_ORDER_DEFINITON, msg}}; } + +// Validate DynamicOrderDefinition and construct parameter object +Result to_dorman_params(const api::orderman::DynamicOrderDefinition& def) { + const auto& params = def.order_params(); + AlgoParams res; + + // + // Step Table + for (size_t x = 0; x < 5; ++x) { + auto xs = std::to_string(x+1); + auto mq = get_if(params, "Market Quantity " + xs).value_or(0); + auto oq = get_if(params, "Order Quantity " + xs).value_or(0); + if (mq && oq) { + res.steps.step_table.push_back({ mq, oq }); + } + } + std::sort(res.steps.step_table.begin(), res.steps.step_table.end()); + res.steps.maximum = get_if(params, "Max Size").value_or(-1); + res.steps.trail_seconds = get_if(params, "Trail Seconds").value_or(0); + + // + // Clip limit + res.clip_limit = get_if(params, "Clip Size").value_or(0); + + // + // scratch + auto enable_scratch = get_if(params, "Enable Scratch"); + if (enable_scratch && enable_scratch.value()) { + auto overfill = get_if(params, "Max Overfill"); + if (!overfill) return bad_definition("Missing 'Max Overfill'"); + res.scratch = { overfill.value() }; + } + + // + // profit orders + auto enable_profit_orders = get_if(params, "Profit Orders"); + if (enable_profit_orders && enable_profit_orders.value()) { + auto ticks = get_if(params, "Ticks Away"); + if (!ticks) return bad_definition("Missing 'Ticks Away'"); + res.profit_orders = { ticks.value() }; + } + + // + // trade accumulation + auto trade_acc = get_if(params, "Trade Accumulation").value_or(false); + if (trade_acc) { + TradeAccumulationParams tap; + + auto contracts = get_if(params, "Trade Contracts"); + if (!contracts) return bad_definition("Missing 'Trade Accumulation # Contracts'"); + tap.contracts = contracts.value(); + + auto interval = get_if(params, "Trade Interval (seconds)"); + if (!interval) return bad_definition("Missing 'Trade Accumulation Interval'"); + tap.seconds = interval.value(); + + auto sleep = get_if(params, "Trade Sleep (seconds)"); + if (!sleep) return bad_definition("Missing 'Trade Accumulation Sleep'"); + tap.sleep_seconds = sleep.value(); + + res.trade_accumulation = {tap}; + } + + return {res}; +} + +extern "C" void init_spark_plugin(bts::SparkServices &srv) { + DynamicOrderDescriptor join_desc; + + join_desc.set_dynamic_order_type("Dorman"); + + *join_desc.add_param_desc() = param_uint32("Market Quantity 1"); + *join_desc.add_param_desc() = param_uint32("Order Quantity 1"); + *join_desc.add_param_desc() = param_uint32("Market Quantity 2"); + *join_desc.add_param_desc() = param_uint32("Order Quantity 2"); + *join_desc.add_param_desc() = param_uint32("Market Quantity 3"); + *join_desc.add_param_desc() = param_uint32("Order Quantity 3"); + *join_desc.add_param_desc() = param_uint32("Market Quantity 4"); + *join_desc.add_param_desc() = param_uint32("Order Quantity 4"); + *join_desc.add_param_desc() = param_uint32("Market Quantity 5"); + *join_desc.add_param_desc() = param_uint32("Order Quantity 5"); + + *join_desc.add_param_desc() = param_uint32("Max Size"); + *join_desc.add_param_desc() = param_uint32("Trail Seconds"); + *join_desc.add_param_desc() = param_uint32("Clip Size"); + *join_desc.add_param_desc() = param_bool("Enable Scratch"); + *join_desc.add_param_desc() = param_uint32("Max Overfill"); + *join_desc.add_param_desc() = param_bool("Profit Orders"); + *join_desc.add_param_desc() = param_uint32("Ticks Away"); + + *join_desc.add_param_desc() = param_bool("Trade Accumulation"); + *join_desc.add_param_desc() = param_uint32("Trade Contracts"); + *join_desc.add_param_desc() = param_uint32("Trade Interval (seconds)"); + *join_desc.add_param_desc() = param_uint32("Trade Sleep (seconds)"); + + srv.register_dynamic_algo_order(join_desc, []() { return std::make_unique(); }); +} + +}}} \ No newline at end of file diff --git a/src/lib/orderman/orders/algo/algo_orders/SplitterAlgo.cpp b/src/lib/orderman/orders/algo/algo_orders/SplitterAlgo.cpp new file mode 100644 index 000000000..55d62777e --- /dev/null +++ b/src/lib/orderman/orders/algo/algo_orders/SplitterAlgo.cpp @@ -0,0 +1,268 @@ +#include "SplitterAlgo.h" +#include "lib/api/services/OrderService.pb.h" +#include "lib/domain_model/Security.h" +#include "lib/logging/logging.h" +#include "lib/orderman/order_factory/IOrderFactory.h" +#include "lib/orderman/orderman_utils/orderman_utils.h" +#include "lib/orderman/orders/algo/IAlgoServices.h" + +#include +#include + +namespace bts { +namespace orderman { +namespace splitter { + +using namespace api::orderman; + +struct ChildOrderState { + OrderKey key; + ChildStatus status; + Price price; + Quantity quantity; + Quantity fills; + uint32_t min_qty() const { return fills.value(); } +}; + +struct SplitterAlgo::Impl { + IAlgoServices* algo = nullptr; + ChildTemplate* child_template = nullptr; + + uint32_t clip = 0; + uint32_t exposed_qty = 0; + uint32_t target_qty = 0; + + bool is_terminated = false; + Quantity total_fills = {}; + Price last_fill_price = {}; + + std::vector orders; +}; + +SplitterAlgo::SplitterAlgo() : me(new Impl()) { } +SplitterAlgo::~SplitterAlgo() {} + +OrderOpResult SplitterAlgo::do_before_launch(IBeforeLaunchAlgoServices& ibl) { + // Validate and store parameters + + auto& def = ibl.definition(); + bts_assert(def.has_custom_def()); + bts_assert(def.custom_def().has_splitter()); + auto& splitter = def.custom_def().splitter(); + + me->clip = splitter.max_order_qty(); + me->exposed_qty = splitter.exposed_qty(); + if (me->clip == 0) { + return OrderOpResult{ORDER_ERROR_INVALID_ORDER_DEFINITON, + "max_order_qty must be greater than 0"}; + } + + update_child_template(ibl); + + me->orders.reserve(me->exposed_qty / me->clip + 1); + + if (ibl.is_attaching()) { + return OrderOpResult{ORDER_ERROR_OP_NOT_SUPPORTED, + "Splitter does not support attach."}; + } + + return {}; +} + +void SplitterAlgo::do_launch(IAlgoServices& ias) { + me->algo = &ias; + me->target_qty = ias.quantity().value(); + // launches initial child orders + sync_orders(); +} +void SplitterAlgo::do_launch_attached(IAlgoServices &, OrderKey, ChildState) { + // rejected in before_launch + bts_assert(false); +} +void SplitterAlgo::do_update(const api::orderman::Update& update) { + const CustomOrderDefinition* new_child_def = nullptr; + if (update.has_custom() && update.custom().has_splitter()) { + auto& splitter = update.custom().splitter(); + if (splitter.max_order_qty() > 0) { + me->clip = splitter.max_order_qty(); + } + me->exposed_qty = splitter.exposed_qty(); + + if (splitter.has_child()) { + update_child_template(algo()); + new_child_def = &splitter.child(); + } + } + me->target_qty = update.quantity(); + + sync_orders(new_child_def); +} +void SplitterAlgo::on_child_change(OrderKey key, ChildState const & state) { + for (auto& child : me->orders) { + if (child.key == key) { + + // If we got an error or cancel that we did not expect we need to cancel + if (state.status == ChildStatus::CANCELED && + child.status != ChildStatus::CANCELED) { + algo().cancel("Unexpected child cancel"); + } + if (state.status != ChildStatus::WORKING) { + child.status = state.status; + } + + auto delta = state.total_fills.value() - child.fills.value(); + if (delta != 0) { + child.fills = state.total_fills; + me->total_fills = Quantity { me->total_fills.value() + delta }; + me->last_fill_price = state.last_price; + algo().update_fill_state(me->total_fills, me->last_fill_price); + } + sync_orders(); + + break; + } + } +} + +// TEST case: +// total = 300, clip = 100 +// constraints: 50,50,50,50 +void compute_desired_sizes(uint32_t total, uint32_t clip, const std::vector& constraints, std::vector* results) { + bts_assert(results); + results->clear(); + + uint32_t irreducible = 0; + for (const auto& mm : constraints) irreducible += mm.min; + + // total > overfills ? + + auto citer = constraints.begin(); + while(total > 0) { + auto current = std::min(total, clip); + // fit to constraint + if (citer != constraints.end()) { + irreducible -= citer->min; + if (current > citer->max) current = citer->max; + if (current < citer->min) current = citer->min; + + // I can't be higher than (total - irreducible) + if (current > (total - irreducible)) current = (total-irreducible); + citer++; + } + results->push_back(Quantity{current}); + total -= current; + } +} + +void get_constraints(const std::vector& orders, std::vector* constraints) { + bts_assert(constraints); + for (const auto& order : orders) { + //Note: to maintain queue position set max to current quantity. This will cause more orders to be added to the end if size increases + constraints->push_back(MinMax { + order.fills.value(), + order.status == ChildStatus::WORKING ? + std::numeric_limits::max() : order.fills.value() + }); + } +} + +bool SplitterAlgo::is_complete() const { + uint32_t fills = 0; + for (const auto& o : me->orders) { fills += o.fills.value(); } + return fills >= me->target_qty; +} + +void SplitterAlgo::terminate() const { + algo().complete(); + for (auto& o : me->orders) { + if (o.status == ChildStatus::WORKING) { + o.status = ChildStatus::CANCELED; + algo().cancel_child(o.key); + } + } + me->is_terminated = true; +} + +void SplitterAlgo::update_child_template(IBeforeLaunchAlgoServices& ibl) { + // Create child template + auto child_def = ibl.definition(); + bts_assert(child_def.has_custom_def()); + bts_assert(child_def.custom_def().has_splitter()); + auto& splitter = child_def.custom_def().splitter(); + if (splitter.has_child()) { + // Copy child before mutabing child_def because splitter is a reference + auto child = splitter.child(); + *child_def.mutable_custom_def() = child; + } + else { + child_def.mutable_custom_def()->mutable_limit(); + } + me->child_template = &ibl.make_child_template(child_def); +} + +void SplitterAlgo::sync_orders(const api::orderman::CustomOrderDefinition* new_child_def) { + if (me->is_terminated) return; + if (is_complete()) { + terminate(); + return; + } + + std::sort(me->orders.begin(), me->orders.end(), [](auto& a, auto b) { return b.min_qty() < a.min_qty(); }); + std::vector constraints; + get_constraints(me->orders, &constraints); + + std::vector desired; + compute_desired_sizes(me->exposed_qty, me->clip, constraints, &desired); + + auto oiter = me->orders.begin(); + // iterate to the current number of orders, don't consider orders that we're adding + auto order_count = me->orders.size(); + auto price = algo().price(); + for (const auto& qty : desired) { + if (order_count == 0) { + // Ran out of existing orders, time to add more + auto key = algo().launch_child(*me->child_template, price, qty); + // Assume the order begins successfully. Hopefully we get back some sort of NACK otherwise + me->orders.push_back({}); + me->orders.back().key = key; + me->orders.back().price = price; + me->orders.back().quantity = Quantity { qty }; + me->orders.back().status = ChildStatus::WORKING; + } + else { + // Check the current order's quantity and modify it to match desired qty + if (oiter->status == ChildStatus::WORKING) { + if (oiter->price != price || + oiter->quantity != qty || + new_child_def != nullptr) { + Update update; + update.set_quantity(qty.value()); + oiter->quantity = qty; + update.set_price(price.value()); + oiter->price = price; + if (new_child_def != nullptr) { + *update.mutable_custom() = *new_child_def; + } + algo().update_child(oiter->key, update); + } + } + oiter++; + order_count -= 1; + } + } + // cancel any orders past desired + if (desired.size() < me->orders.size()) { + for (size_t x = desired.size(); x < me->orders.size(); ++x) { + auto& order= me->orders[x]; + if (order.status == ChildStatus::WORKING) { + order.status = ChildStatus::CANCELED; + algo().cancel_child(order.key); + } + } + } + +} + +IAlgoServices& SplitterAlgo::algo() const { bts_assert(me->algo); return *me->algo; } + +}}} //namespace diff --git a/src/lib/orderman/orders/algo/algo_orders/SplitterAlgo.h b/src/lib/orderman/orders/algo/algo_orders/SplitterAlgo.h new file mode 100644 index 000000000..92850aa7d --- /dev/null +++ b/src/lib/orderman/orders/algo/algo_orders/SplitterAlgo.h @@ -0,0 +1,60 @@ +#pragma once +#include "lib/orderman/orders/algo/IAlgoOrder.h" + +namespace bts { +namespace orderman { +namespace splitter { + +/* + SplitterAlgo + + Parameters: + max_order_qty + -- Clip size. No single order can exceed this quantity. + exposed_qty + -- The total quantity we want visible on the exchange. + -- This will typically be higher than the quantity which helps capture fills in pro-rata algorithms + price, quantity + -- Common to all orders. + -- Splitter is filled when it reaches (or exceeds) quantity not exposed_qty + + SplitterAlgo acts as a single order to its parent but actually divides the total exposure + by the clip size and working several orders at once. + - It completes when the quantity is matched or exceeded. + - It tries to maximize the size of the orders and it may sacrifice queue position to do that. + - It reacts to fills and parameter changes and adjusts it's children appropriately. + - See splitter_tests.cpp for examples + - Future work: By constraining maximum size we can force this algorithm to maintain queue position. + +*/ +class SplitterAlgo : public IAlgoOrder +{ + struct Impl; + std::unique_ptr me; + +public: + SplitterAlgo(); + ~SplitterAlgo(); + + OrderOpResult do_before_launch(IBeforeLaunchAlgoServices &) override; + void do_launch(IAlgoServices &) override; + void do_launch_attached(IAlgoServices &, OrderKey, ChildState) override; + void do_update(api::orderman::Update const &) override; + void on_child_change(OrderKey, ChildState const &) override; + +private: + IAlgoServices& algo() const; + + void sync_orders(const api::orderman::CustomOrderDefinition* new_child_def = nullptr); + bool is_complete() const; + void terminate() const; + + void update_child_template(IBeforeLaunchAlgoServices&); +}; + +// exposed for unit testing purposes. +struct MinMax {uint32_t min, max;}; +void compute_desired_sizes(uint32_t , uint32_t , + const std::vector& , std::vector* ); + +}}} //namespace diff --git a/src/lib/orderman/orders/algo/algo_orders/TopAlgo.cpp b/src/lib/orderman/orders/algo/algo_orders/TopAlgo.cpp index e16aaa5d8..acb3f3f29 100644 --- a/src/lib/orderman/orders/algo/algo_orders/TopAlgo.cpp +++ b/src/lib/orderman/orders/algo/algo_orders/TopAlgo.cpp @@ -16,6 +16,7 @@ using namespace api::orderman; struct TopAlgo::PImpl { IAlgoServices *algo; spark::QuantityMeasure top_threshold_measure; + uint32_t ticks_off = 0; OrderKey child_key = null_order_key(); ChildTemplate *child_template = nullptr; Book last_book; @@ -41,7 +42,7 @@ OrderOpResult TopAlgo::do_before_launch(IBeforeLaunchAlgoServices &serv) { } me->top_threshold_measure = top.top_threshold(); - + me->ticks_off = top.ticks_off(); if (!serv.is_attaching()) { // if we are not attaching create a child template so we can // quickly launch our child when the time comes. @@ -87,13 +88,14 @@ void TopAlgo::do_update(api::orderman::Update const &update) { auto &top_update = update.custom().top(); *child_update.mutable_custom() = top_update.child(); me->top_threshold_measure = top_update.top_threshold(); + me->ticks_off = top_update.ticks_off(); } algo().update_child(me->child_key, child_update); } void TopAlgo::on_book(SecurityKey, Book const &b) { me->last_book = b; - ensure_min_quantity(); + ensure_order_meets_constraints(); } void TopAlgo::on_child_change(OrderKey, ChildState const &st) { @@ -111,14 +113,14 @@ void TopAlgo::on_child_change(OrderKey, ChildState const &st) { } } -bool TopAlgo::ensure_min_quantity() { +bool TopAlgo::ensure_order_meets_constraints() { LOG(OrderMan, Diag, "Top order {} received book update. Current top of book: {}@{}", algo().order_key().value(), top_of_book_quantity().value(), top_of_book_price().value()); if (better_than_top()) { - LOG(OrderMan, Diag, + LOG(OrderMan, Info, "Top order {} became better than the top of the book, canceling", algo().order_key().value()); algo().cancel("Order would become BBO"); @@ -135,7 +137,7 @@ bool TopAlgo::ensure_min_quantity() { algo().order_key().value(), total_min_quantity, top_of_book_quantity().value()); if (top_of_book_quantity() < Quantity(total_min_quantity)) { - LOG(OrderMan, Diag, + LOG(OrderMan, Info, "Top order {} is canceling because min quantiy is not met", algo().order_key().value()); algo().cancel("Top of book is below threshold"); @@ -147,6 +149,26 @@ bool TopAlgo::ensure_min_quantity() { algo().order_key().value(), algo().price().value(), top_of_book_price().value()); + if (me->ticks_off > 0) { + auto tick_diff = securities::price_diff_to_ticks( + algo().price().value(), top_of_book_price().value(), + securities::tick_size(algo().security()).value()); + if (std::abs(tick_diff) <= (int)me->ticks_off) { + LOG(OrderMan, Info, + "Top order {} price {} is less than {} ticks away from top ({}). " + "Canceling", + algo().order_key().value(), algo().price().value(), + me->ticks_off + 1, top_of_book_price().value()); + algo().cancel("Order is too close to top of the book"); + return false; + } else + LOG(OrderMan, Diag, + "Top order {} price {} is {} ticks away from top. Top: {} Ticks " + "off: {}", + algo().order_key().value(), algo().price().value(), + std::abs(tick_diff), top_of_book_price().value(), me->ticks_off); + } + return true; } diff --git a/src/lib/orderman/orders/algo/algo_orders/TopAlgo.h b/src/lib/orderman/orders/algo/algo_orders/TopAlgo.h index 3648dc50c..857f12f88 100644 --- a/src/lib/orderman/orders/algo/algo_orders/TopAlgo.h +++ b/src/lib/orderman/orders/algo/algo_orders/TopAlgo.h @@ -27,7 +27,7 @@ class TopAlgo : public IAlgoOrder { bool at_top_of_book() const; bool better_than_top() const; Quantity min_quantity_at_top() const; - bool ensure_min_quantity(); + bool ensure_order_meets_constraints(); bool has_running_child() const; Price top_of_book_price() const; Quantity top_of_book_quantity() const; diff --git a/src/lib/orderman/orders/algo/algo_orders/tests/AlgoTestDriver.cpp b/src/lib/orderman/orders/algo/algo_orders/tests/AlgoTestDriver.cpp new file mode 100644 index 000000000..5ca3f517b --- /dev/null +++ b/src/lib/orderman/orders/algo/algo_orders/tests/AlgoTestDriver.cpp @@ -0,0 +1,441 @@ +#include "AlgoTestDriver.h" +#include "lib/md/relay/RelayRecorder.h" +#include "catch/catch.hpp" +#include "lib/api/services/OrderService.pb.h" +#include "lib/orderman/orderman_utils/Book.h" +#include "lib/logging/logging.h" + +#include +#include + +namespace bts { +namespace orderman { +namespace test { +using namespace api::orderman; + +constexpr bool has_flag(ATDFlags prop, ATDFlags flag) { + return ((uint32_t)prop & (uint32_t)flag) == (uint32_t)flag; +} + +const std::map& get_some_securities() { + static std::map results; + if (results.empty()) { + md::get_securities_from_replay([&](const api::Security& s) { + results.insert(std::make_pair(SecurityKey(s.id()), s)); + }); + } + return results; +} + + +struct RecursionCounter { + size_t depth = 0; + virtual void flush() = 0; + virtual ~RecursionCounter() {} +}; +struct RecursionGuard { + RecursionGuard(RecursionCounter& c) : counter(c) { + counter.depth += 1; + REQUIRE(counter.depth < 100); + } + ~RecursionGuard() { + counter.depth -= 1; + if (counter.depth == 0) { counter.flush(); } + } +private: + RecursionCounter& counter; +}; + +struct AlgoTestDriverBase::Impl : public RecursionCounter { + IAlgoOrder& algo; + api::orderman::OrderDefinition definition; + Price price; + Quantity size; + ATDFlags flags; + TimeStamp now; + api::Security security; + OrderKey order_key, parent_key; + std::tuple fill_state; + + AlgoState state; + std::vector orders; + std::vector detached_orders; + + std::map books; + std::map book_subs; + std::map ticker_subs; + bool sub_timer = false; + + // Callbacks + std::vector> on_child_launching; + std::vector> on_child_changing; + std::vector> on_child_changed; + bool allow_overfill = false; + + Impl(IAlgoOrder& o) : algo(o) {} + + OrderKey next_order() { + _next_order.value() += 1; + return _next_order; + } + RecursionGuard recusion_guard() { return {*this}; } + + void flush() override; + bool flushing = false; + // child changes must be asyncronous + std::vector> deferred; + void defer_child_change(OrderKey k, ChildState cs) { deferred.push_back(std::make_tuple(k,cs)); } + +private: + OrderKey _next_order = OrderKey(99); +}; + +void AlgoTestDriverBase::Impl::flush() { + if(flushing) return; + flushing = true; + for (const auto& update : deferred) { + algo.on_child_change(std::get(update), std::get(update)); + } + deferred.clear(); + flushing = false; +} + +AlgoTestDriverBase::AlgoTestDriverBase( + IAlgoOrder& algo, + const api::orderman::OrderDefinition& def, + Price price, Quantity size, + ATDFlags flags) + : me(new Impl(algo)) { + + me->definition = def; + me->price = price; + me->size = size; + me->flags = flags; + me->now = Timer::now(); + me->state = AlgoState::CREATED; + + auto securities = get_some_securities(); + REQUIRE(securities.size() > 0); + me->security = securities.begin()->second; + + me->order_key = me->next_order(); + if (has_flag(flags, ATDFlags::HAS_PARENT)) { + me->parent_key = me->next_order(); + } +} + +AlgoTestDriverBase::~AlgoTestDriverBase() {} + +OrderOpResult AlgoTestDriverBase::before_launch() { + REQUIRE(me->state == AlgoState::CREATED); + me->state = AlgoState::READY; + return me->algo.do_before_launch(*this); +} + +void AlgoTestDriverBase::launch() { + REQUIRE(me->state == AlgoState::READY); + me->algo.do_launch(*this); + me->state = AlgoState::RUNNING; +} + +void AlgoTestDriverBase::launch_attached(OrderKey key, ChildState state) { + REQUIRE(me->state == AlgoState::READY); + me->algo.do_launch_attached(*this, key, state); + me->state = AlgoState::RUNNING; +} +void AlgoTestDriverBase::do_update(const api::orderman::Update& update) { + auto guard = me->recusion_guard(); + REQUIRE(me->state == AlgoState::RUNNING); + me->algo.do_update(update); +} + +void AlgoTestDriverBase::update_book(const Book& book) { + auto key = book.security_key(); + me->books[key] = book; + auto iter = me->book_subs.find(key); + if (iter != me->book_subs.end()) { + me->algo.on_book(key, book); + } +} +void AlgoTestDriverBase::update_ticker(SecurityKey key, const TradeVector& data) { + auto iter = me->ticker_subs.find(key); + if (iter != me->ticker_subs.end() && iter->second) { + me->algo.on_ticker(key, data); + } +} +void AlgoTestDriverBase::advance_time(Duration d, uint32_t steps) { + Duration dprime = d / steps; + for (uint32_t s = 0; s < steps; ++s) { + me->now += dprime; + if (me->sub_timer) { + me->algo.on_timer(); + } + } +} +IAlgoOrder::OnCancelResponse AlgoTestDriverBase::cancel() { + auto r = me->algo.on_cancel(); + if (r == IAlgoOrder::OnCancelResponse::HONOR) { + me->state = AlgoState::CANCELED; + for (auto& o : me->orders) { + o.cstate.status = ChildStatus::CANCELED; + } + } + return r; +} + +const std::vector& AlgoTestDriverBase::child_orders() const { + return me->orders; +} + +const std::vector& AlgoTestDriverBase::detached_orders() const { + return me->detached_orders; +} + +AlgoState AlgoTestDriverBase::state() const { + return me->state; +} + +void AlgoTestDriverBase::fill(OrderKey k, Price price, Quantity qty) { + auto guard = me->recusion_guard(); + for (auto& child : me->orders) { + if (child.key == k) { + if (!me->allow_overfill) REQUIRE(child.cstate.quantity.value() - child.cstate.total_fills.value() >= qty.value()); + for (const auto& cb : me->on_child_changing) cb(child); + auto old = child; + + auto total_price = child.cstate.avg_price.value() * child.cstate.total_fills.value(); + total_price += price.value() * qty.value(); + + child.cstate.last_fills = qty; + child.cstate.total_fills.value() += qty.value(); + child.cstate.last_price = price; + child.cstate.avg_price = Price { total_price / child.cstate.total_fills.value() }; + + if (child.cstate.quantity.value() <= child.cstate.total_fills.value()) { + child.cstate.status = ChildStatus::COMPLETED; + } + + for (const auto& cb : me->on_child_changed) cb(old, child); + // No defer because this is called from outside + if (child.subscribed) me->algo.on_child_change(k, child.cstate); + break; + } + } +} + +const ChildOrder* largest_order(const std::vector& orders) { + uint32_t sz = 0; + const ChildOrder* result = nullptr; + for (const auto& order : orders) { + auto available = + order.cstate.quantity.value() - order.cstate.total_fills.value(); + if (order.cstate.status == ChildStatus::WORKING && + available > sz) { + sz = available; + result = ℴ + } + } + return result; +} +void AlgoTestDriverBase::fill(Price p, Quantity q) { + auto to_fill = q.value(); + while(to_fill > 0) { + auto order = largest_order(this->child_orders()); + REQUIRE(order); + auto available = (order->cstate.quantity.value() - order->cstate.total_fills.value()); + if (available >= to_fill) { + this->fill(order->key, p, Quantity {to_fill}); + return; + } + else { + this->fill(order->key, p, Quantity {available}); + to_fill -= available; + } + } +} + +void AlgoTestDriverBase::on_child_launching(std::function&& cb) { + me->on_child_launching.push_back(cb); +} +void AlgoTestDriverBase::on_child_changing(std::function&& cb) { + me->on_child_changing.push_back(cb); +} +void AlgoTestDriverBase::on_child_changed(std::function&& cb) { + me->on_child_changed.push_back(cb); +} + +bool AlgoTestDriverBase::allow_overfill() const { return me->allow_overfill; } +void AlgoTestDriverBase::allow_overfill(bool value) { me->allow_overfill = value; } + +// +// Utility + +ChildOrder mkchild(OrderKey key, Price price, Quantity qty, bool sub) { + ChildOrder result = {}; + result.key = key; + result.cstate.price = price; + result.cstate.quantity = qty; + result.subscribed = sub; + return result; +} + +// +// IBeforeLaunch + +const api::orderman::OrderDefinition& AlgoTestDriverBase::definition() const { return me->definition; } +const api::Security& AlgoTestDriverBase::security() const { return me->security; } +OrderKey AlgoTestDriverBase::order_key() const { return me->order_key; } +OrderKey AlgoTestDriverBase::parent_order_key() const { return me->parent_key; } +const api::Security* AlgoTestDriverBase::lookup_security(SecurityKey key) const { + auto s = get_some_securities(); + auto iter = s.find(key); + if (iter != s.end()) { + return &iter->second; + } + return nullptr; +} +bool AlgoTestDriverBase::is_attaching() const { return has_flag(me->flags, ATDFlags::IS_ATTACHING); } +ChildTemplate& AlgoTestDriverBase::make_child_template(const api::orderman::OrderDefinition& def, size_t) { + REQUIRE(def.custom_def().custom_case() != CustomOrderDefinition::CUSTOM_NOT_SET); + ChildTemplate* t = nullptr; + return *t; +} +TimeStamp AlgoTestDriverBase::get_time() const { return me->now; } + +// +// IAlgoServices + +Price AlgoTestDriverBase::price() const { return me->price; } +Quantity AlgoTestDriverBase::quantity() const { return me->size; } +Quantity AlgoTestDriverBase::total_fills() const { + return std::get(me->fill_state); +} +bool AlgoTestDriverBase::is_shutting_down() const { + return false; +} + +void AlgoTestDriverBase::subscribe_book(SecurityKey key){ + me->book_subs[key] = true; + auto iter = me->books.find(key); + if (iter != me->books.end()) { + me->algo.on_book(key, iter->second); + } +} +void AlgoTestDriverBase::unsubscribe_book(SecurityKey key) { + me->book_subs.erase(key); +} + +void AlgoTestDriverBase::subscribe_ticker(SecurityKey key) { + me->ticker_subs[key] = true; +} +void AlgoTestDriverBase::unsubscribe_ticker(SecurityKey key) { + me->ticker_subs.erase(key); +} + +void AlgoTestDriverBase::subscribe_child_change(OrderKey k) { + for (auto& child : me->orders) { + if (child.key == k) { + child.subscribed = true; + } + } +} +void AlgoTestDriverBase::unsubscribe_child_change(OrderKey k) { + for (auto& child : me->orders) { + if (child.key == k) { + child.subscribed = false; + } + } +} +void AlgoTestDriverBase::subscribe_timer() { me->sub_timer = true; } +void AlgoTestDriverBase::unsubscribe_timer() { me->sub_timer = false; } + + +OrderKey AlgoTestDriverBase::launch_child(ChildTemplate&, Price price, Quantity qty, bool sub) { + OrderKey key = me->next_order(); + me->orders.push_back( + mkchild(key, price, qty, sub)); + for(const auto& cb : me->on_child_launching) cb(key); + return key; +} +OrderKey AlgoTestDriverBase::launch_child(api::orderman::OrderDefinition const & def, Price price, Quantity qty, bool sub) { + OrderKey key = me->next_order(); + me->orders.push_back( + mkchild(key, price, qty, sub)); + me->orders.back().def = {def}; + for(const auto& cb : me->on_child_launching) cb(key); + return key; +} +void AlgoTestDriverBase::update_child(OrderKey key, api::orderman::Update const & update) { + auto guard = me->recusion_guard(); + for (auto& child : me->orders) { + if (child.key == key) { + // Note: Algo may have sent this update before it was notified that the child was filled/canceled + // REQUIRE(child.cstate.status == ChildStatus::WORKING); + for (const auto& cb : me->on_child_changing) cb(child); + auto old = child; + if (update.price() != 0) { + child.cstate.price = Price(update.price()); + } + if (update.quantity() != 0) { + child.cstate.quantity = Quantity(update.quantity()); + } + if (child.cstate.quantity.value() <= child.cstate.total_fills.value()) { + child.cstate.status = ChildStatus::COMPLETED; + } + if (update.has_custom()) { + if (!child.def) { + child.def = {api::orderman::OrderDefinition()}; + } + *child.def.value().mutable_custom_def() = update.custom(); + } + for (const auto& cb : me->on_child_changed) cb(old, child); + + if (child.subscribed) { + me->defer_child_change(key, child.cstate); + } + } + } +} +void AlgoTestDriverBase::cancel_child(OrderKey key) { + auto guard = me->recusion_guard(); + for (auto& child : me->orders) { + if (child.key == key) { + REQUIRE(child.cstate.status != ChildStatus::CANCELED); + for (const auto& cb : me->on_child_changing) cb(child); + auto before = child; + child.cstate.status = ChildStatus::CANCELED; + for (const auto& cb : me->on_child_changed) cb(before, child); + if(child.subscribed) { + me->defer_child_change(key, child.cstate); + } + break; + } + } +} +void AlgoTestDriverBase::detach_child(OrderKey key) { + for (auto iter = me->orders.begin(); iter != me->orders.end(); ++iter) { + auto order = *iter; + if (order.key == key) { + me->detached_orders.push_back(order); + me->orders.erase(iter); + break; + } + } +} +void AlgoTestDriverBase::detach_all_children() { + for (const auto& order : me->orders) me->detached_orders.push_back(order); + me->orders.clear(); +} +void AlgoTestDriverBase::cancel(std::string const &) { + cancel(); +} +void AlgoTestDriverBase::terminate_error(std::string const &) { + me->state = AlgoState::TERMINATED; +} +void AlgoTestDriverBase::complete() { + me->state = AlgoState::COMPLETED; +} +void AlgoTestDriverBase::set_ladder_visibility(bool) { /* whatever, ok */ } +void AlgoTestDriverBase::update_fill_state(Quantity q, Price p) { me->fill_state = { q, p }; } + +}}}//namespace \ No newline at end of file diff --git a/src/lib/orderman/orders/algo/algo_orders/tests/AlgoTestDriver.h b/src/lib/orderman/orders/algo/algo_orders/tests/AlgoTestDriver.h new file mode 100644 index 000000000..c4ad79b17 --- /dev/null +++ b/src/lib/orderman/orders/algo/algo_orders/tests/AlgoTestDriver.h @@ -0,0 +1,151 @@ +#pragma once +#include "lib/orderman/orders/algo/IAlgoOrder.h" +#include "lib/orderman/orders/algo/IAlgoServices.h" +#include "lib/orderman/events/TickerUpdateEvent.h" +#include "lib/md/IntPrice.h" +#include +#include +#include + +namespace bts { +namespace orderman { +namespace test { + +const std::map& get_some_securities(); + +enum class ATDFlags { + NONE = 0, + IS_ATTACHING = 1, + HAS_PARENT = 2, +}; + +enum class AlgoState { + CREATED, + // before_Launch() -> + READY, + // launch() -> + RUNNING, + CANCELED, + TERMINATED, + COMPLETED +}; + +struct ChildOrder { + OrderKey key; + bool subscribed; + ChildState cstate; + std::optional def; + uint32_t active_qty() const { + return cstate.status == ChildStatus::WORKING ? + cstate.quantity.value() - cstate.total_fills.value() : 0; + } +}; + +class AlgoTestDriverBase : private IAlgoServices { + struct Impl; + std::unique_ptr me; + + public: + AlgoTestDriverBase( + IAlgoOrder& algo, + const api::orderman::OrderDefinition& def, + Price price, Quantity size, + ATDFlags flags=ATDFlags::NONE); + ~AlgoTestDriverBase(); + + // + // Expose some things from IAlgoServices + const api::Security& security() const override; + Price price() const override; + Quantity quantity() const override; + Quantity total_fills() const override; + bool is_shutting_down() const override; + + // + // Change state + + OrderOpResult before_launch(); + void launch(); + void launch_attached(OrderKey, ChildState); + void do_update(const api::orderman::Update&); + + void update_book(const Book&); + void update_ticker(SecurityKey, const TradeVector&); + void advance_time(Duration, uint32_t steps=1); + IAlgoOrder::OnCancelResponse cancel(); + + // + // Inspection methods + const std::vector& child_orders() const; + const std::vector& detached_orders() const; + AlgoState state() const; + + // Fills for this specific order + void fill(OrderKey, Price, Quantity); + // Fill this quantity, start with the largest child order + void fill(Price, Quantity); + + void on_child_launching(std::function&&); + void on_child_changing(std::function&&); + void on_child_changed(std::function&&); + + // + // Tweak internal REQUIRE + bool allow_overfill() const; + void allow_overfill(bool=true); + +private: + + // + // IBeforeLaunchAlgoServices + const api::orderman::OrderDefinition& definition() const override; + // const api::Security& security() const override; + OrderKey order_key() const override; + OrderKey parent_order_key() const override; + const api::Security* lookup_security(SecurityKey) const override; + bool is_attaching() const override; + ChildTemplate& make_child_template(const api::orderman::OrderDefinition& child_def, size_t reserve = 1) override; + TimeStamp get_time() const override; + + // + // IAlgoServices + void subscribe_book(SecurityKey)override; + void unsubscribe_book(SecurityKey) override; + void subscribe_child_change(OrderKey) override; + void unsubscribe_child_change(OrderKey) override; + void subscribe_timer() override; + void unsubscribe_timer() override; + void subscribe_ticker(SecurityKey) override; + void unsubscribe_ticker(SecurityKey) override; + + OrderKey launch_child(ChildTemplate &, Price, Quantity, bool) override; + OrderKey launch_child(api::orderman::OrderDefinition const &, Price, + Quantity, bool) override; + void update_child(OrderKey , api::orderman::Update const &) override; + void cancel_child(OrderKey) override; + void detach_child(OrderKey) override; + void detach_all_children() override; + + void cancel(std::string const &) override; + void terminate_error(std::string const &) override; + void complete() override; + void set_ladder_visibility(bool) override; + void update_fill_state(Quantity , Price) override; + +}; + +template +struct AlgoTestDriver : public AlgoTestDriverBase +{ + // Avoids mutual ownership problem + Algo _algo; + AlgoTestDriver( + const api::orderman::OrderDefinition& def, + const md::IntPrice& price, Quantity size, + ATDFlags flags=ATDFlags::NONE) + : AlgoTestDriverBase(_algo, def, Price {price.price()}, size, flags), _algo() + { } + const Algo& algo() const { return _algo; } +}; + +}}}//namespace \ No newline at end of file diff --git a/src/lib/orderman/orders/algo/algo_orders/tests/dorman_tests.cpp b/src/lib/orderman/orders/algo/algo_orders/tests/dorman_tests.cpp new file mode 100644 index 000000000..556f43b21 --- /dev/null +++ b/src/lib/orderman/orders/algo/algo_orders/tests/dorman_tests.cpp @@ -0,0 +1,743 @@ +#include "catch/catch.hpp" +#include "../DormanAlgo.h" +#include "AlgoTestDriver.h" +#include "lib/orderman/orderman_utils/Book.h" +#include "lib/domain_model/Security.h" +#include "lib/orderman/orderman_utils/orderman_utils.h" +#include +#include +#include + +using namespace std; +using namespace bts::orderman::dorman; +using namespace bts::api::orderman; + +namespace bts { +namespace orderman { +namespace test { + +/* STATE + +struct OrderState { + Price desired_price; + Quantity exposed_qty; +} + +*/ + +/* TEST ASPECTS + +order_qty vs market quantity +order_qty reduce when market changes +order_qty increase (w/ delay) when market changes +order_qty maximum + +overfill [under/over] max with auto-scratch [on/off] +Profit orders + - test that orders are sent + - test that away order are at the right price + +Trade Accumulation + - accumulator goes [on/off] + - Dorman goes to sleep when it goes 'on' and wakes up when it goes 'off' + +Market Moves + - Available size increases + - Available size decreases + +clip size pseudo-order + - change clip size + - immidiate effect? + - don't have to cancel if clip size goes down? + - change order size [up/down] + - suspend/resume + - cancels all + + + */ + +api::orderman::DynamicOrderDefinition to_dynamic_order_params(const AlgoParams& input) { + api::orderman::DynamicOrderDefinition result; + result.set_dynamic_order_type("Dorman"); + auto& map = *result.mutable_order_params(); + + for (size_t x = 0; x < input.steps.step_table.size(); ++x) { + std::string xs = std::to_string(x+1); + map["Market Quantity " + xs] = dynamic_parameter( std::get<0>(input.steps.step_table[x])); + map["Order Quantity " + xs] = dynamic_parameter(std::get<1>(input.steps.step_table[x])); + } + + map["Max Size"] = dynamic_parameter(input.steps.maximum); + map["Trail Seconds"] = dynamic_parameter(input.steps.trail_seconds); + map["Clip Size"] = dynamic_parameter(input.clip_limit); + + // scratch + map["Enable Scratch"] = dynamic_parameter((bool)(input.scratch)); + if (input.scratch) { + map["Max Overfill"] = dynamic_parameter(input.scratch.value().max_overfill); + } + // + // profit orders + map["Profit Orders"] = dynamic_parameter((bool)input.profit_orders); + if (input.profit_orders) { + map["Ticks Away"] = dynamic_parameter(input.profit_orders.value().ticks_away); + } + + // + // trade accumulator + map["Trade Accumulation"] = dynamic_parameter((bool)input.trade_accumulation); + if (input.trade_accumulation) { + auto& a = input.trade_accumulation.value(); + map[("Trade Contracts")] = dynamic_parameter(a.contracts); + map["Trade Interval (seconds)"] = dynamic_parameter(a.seconds); + map["Trade Sleep (seconds)"] = dynamic_parameter(a.sleep_seconds); + } + + return result; +} + +void check_round_trip(const AlgoParams& input) { + auto d1 = to_dynamic_order_params(input); + auto ap1 = to_dorman_params(d1); + if (std::holds_alternative(ap1)) { + std::cout << std::get(ap1).error_msg; + } + REQUIRE(std::holds_alternative(ap1)); + auto d2 = to_dynamic_order_params(std::get(ap1)); + + REQUIRE(d2.DebugString() == d1.DebugString()); +} + +TEST_CASE("dorman_round_trip_params", "[algo][dorman]") { + + SECTION("a") { + AlgoParams input; + input.steps = { + { {500, 250}, + {1000, 400} }, + 1024, 5 + }; + check_round_trip(input); + } + + SECTION("b") { + AlgoParams input; + input.steps = { + { {500, 250}, + {1000, 400} + }, + 1024, 5 + }; + input.scratch = { 500 }; + input.profit_orders = { 42 }; + input.clip_limit = 220; + check_round_trip(input); + } + + SECTION("c") { + AlgoParams input; + input.steps = { + { {500, 250}, + {1000, 400} + }, + 1024, 5 + }; + input.profit_orders = { 42 }; + input.trade_accumulation = { + 100, 200, 300 + }; + check_round_trip(input); + } + +} + +struct ExpectedSize { + uint32_t input; + uint32_t expected; +}; +constexpr auto some_price = md::IntPrice { 128000, -3 }; + + +template +size_t count_x(const std::vector& child_orders, CB fn) { + return std::accumulate( + child_orders.begin(), + child_orders.end(), 0, + [&](uint32_t acc, const auto& child) { + return acc + fn(child); + }); +} + +size_t count_exposed(const std::vector& child_orders) { + return count_x(child_orders, [](const ChildOrder& child) { + REQUIRE(child.def); + if (child.cstate.status == ChildStatus::CANCELED) return 0u; + else return child.def.value().custom_def().splitter().exposed_qty() + child.cstate.total_fills.value(); + }); +} + +size_t count_fills(const std::vector& child_orders) { + return count_x(child_orders, [](const ChildOrder& child) { + return child.def.value().custom_def().splitter().exposed_qty() + child.cstate.total_fills.value(); + }); +} +size_t count_status(const std::vector& child_orders, ChildStatus status) { + size_t match = 0; + for (const auto& child : child_orders) { + if (child.cstate.status == status) match += 1; + } + return match; +} + + +TEST_CASE("dorman_order_size", "[algo][dorman]") { + // create a table of various sizes + // send market state to the algo + AlgoParams input; + input.steps.step_table = { + {500, 250}, + {1000, 400}, + {2000, 600}, + {3500, 750}, + {10000, 999} + }; + input.steps.maximum = 800; + input.steps.trail_seconds = 5; + input.clip_limit = 100; + + api::orderman::OrderDefinition def; + def.set_side(api::SIDE_BUY); + def.mutable_custom_def()->mutable_dynamic()->CopyFrom(to_dynamic_order_params(input)); + + SECTION("initial size vs market") { + std::vector test_sizes { + { 10, 250 }, + { 200, 250 }, + { 250, 250 }, + { 500, 400 }, + { 999, 400 }, + { 2500, 750 }, + { 9999, 800 } + }; + + for (auto& s : test_sizes) + { + AlgoTestDriver state(def, some_price, Quantity {10}); + auto secid = SecurityKey(state.security().id()); + Book book(secid); + + // order size is driven by the available market size + book.bid()[0] = { some_price, s.input }; + state.update_book(book); + + REQUIRE(state.before_launch().ok()); + state.launch(); + REQUIRE(state.algo().exposed_qty().value() == s.expected); + REQUIRE(s.expected == count_exposed(state.child_orders())); + } + } + SECTION("Reduce qty on market shrink (immidiate)") { + AlgoTestDriver state(def, some_price, Quantity {10}); + auto secid = SecurityKey(state.security().id()); + Book book(secid); + book.bid()[0] = { some_price, 505 }; + state.update_book(book); + REQUIRE(state.before_launch().ok()); + state.launch(); + REQUIRE(state.algo().exposed_qty() == Quantity(400)); + + // Check that orders were created + REQUIRE(400 == count_exposed(state.child_orders())); + for(const auto& ch : state.child_orders()) { + REQUIRE(ch.cstate.quantity == state.quantity()); + } + + // Reduce market + book.bid()[0] = { some_price, 405 }; + state.update_book(book); + REQUIRE(state.algo().exposed_qty() == Quantity(250)); + REQUIRE( + 250 == count_exposed(state.child_orders())); + } + + SECTION("Increase qty w/ delay when market grows") { + AlgoTestDriver state(def, some_price, Quantity {10}); + auto secid = SecurityKey(state.security().id()); + Book book(secid); + book.bid()[0] = { some_price, 505 }; + state.update_book(book); + REQUIRE(state.before_launch().ok()); + state.launch(); + REQUIRE(state.algo().exposed_qty() == Quantity(400)); + + // Check that orders were created + REQUIRE( + 400 == count_exposed(state.child_orders())); + + // INCREASE market + book.bid()[0] = { some_price, 1001 }; + state.update_book(book); + // NOTE: that the orders do not follow the market instantly + // so this is still 400 + REQUIRE( + 400 == count_exposed(state.child_orders())); + + // Still 400 after the timer ticks but not enough time has passed + state.advance_time(std::chrono::seconds()); + REQUIRE( + 400 == count_exposed(state.child_orders())); + + state.advance_time(std::chrono::seconds(input.steps.trail_seconds)); + REQUIRE( + 600 == count_exposed(state.child_orders())); + + } + SECTION("Respect maximum") { + AlgoTestDriver state(def, some_price, Quantity {10}); + auto secid = SecurityKey(state.security().id()); + Book book(secid); + book.bid()[0] = { some_price, 10505 }; + state.update_book(book); + REQUIRE(state.before_launch().ok()); + state.launch(); + + REQUIRE(state.algo().exposed_qty() == Quantity(input.steps.maximum)); + + // Check that orders were created + REQUIRE( + input.steps.maximum == count_exposed(state.child_orders())); + } +} + +TEST_CASE("Dorman fills", "[algo][dorman]") { + AlgoParams input; + input.steps.step_table = { + {500, 250}, + {1000, 450}, + {2000, 650}, + {3500, 750}, + }; + input.steps.maximum = 750; + input.steps.trail_seconds = 2; + input.clip_limit = 100; + api::orderman::OrderDefinition def; + def.set_side(api::SIDE_BUY); + def.mutable_custom_def()->mutable_dynamic()->CopyFrom(to_dynamic_order_params(input)); + auto price = Price { some_price.price() }; + + SECTION("complete when filled") { + // Send out some orders + // Fill them + // Expect order state to become finished + AlgoTestDriver state(def, some_price, Quantity {10}); + auto secid = SecurityKey(state.security().id()); + Book book(secid); + book.bid()[0] = { some_price, 501 }; + state.update_book(book); + REQUIRE(state.before_launch().ok()); + state.launch(); + REQUIRE(state.algo().exposed_qty() == Quantity(450)); + + for (const auto& order : state.child_orders()) { + state.fill(order.key, price, order.cstate.quantity); + } + + REQUIRE(state.state() == AlgoState::COMPLETED); + } + + // Test cases for transitions w/ fills + // + + SECTION("Complete by update") { + // target = 500, exposed = 650 + // Fill 400 + // Change target to 350 + // expect completion + AlgoTestDriver state(def, some_price, Quantity {500}); + auto secid = SecurityKey(state.security().id()); + Book book(secid); + book.bid()[0] = { some_price, 1999 }; + state.update_book(book); + REQUIRE(state.before_launch().ok()); + state.launch(); + REQUIRE(state.algo().exposed_qty() == Quantity(650)); + REQUIRE(state.quantity() == Quantity {500}); + + // Fill 400 + state.fill(price, Quantity { 400 }); + Update update; + update.set_quantity(350); + state.do_update(update); + REQUIRE(state.state() == AlgoState::COMPLETED); + } +} + +TEST_CASE("Dorman overfill", "[algo][dorman]") { + AlgoParams input; + input.steps.step_table = { + {500, 250}, + {1000, 450}, + {2000, 650}, + {3500, 750}, + }; + input.steps.maximum = 750; + input.steps.trail_seconds = 2; + input.clip_limit = 100; + + SECTION("auto-scratch ON") { + input.scratch = {{ 500 }}; + } + SECTION("auto-scratch OFF") { + // both sections will run + input.scratch = std::nullopt; + } + + api::orderman::OrderDefinition def; + def.set_side(api::SIDE_BUY); + def.mutable_custom_def()->mutable_dynamic()->CopyFrom(to_dynamic_order_params(input)); + auto price = Price { some_price.price() }; + + AlgoTestDriver state(def, some_price, Quantity {100}); + state.allow_overfill(); + auto secid = SecurityKey(state.security().id()); + Book book(secid); + book.bid()[0] = { some_price, 1999 }; + state.update_book(book); + REQUIRE(state.before_launch().ok()); + state.launch(); + REQUIRE(state.algo().exposed_qty() == Quantity(650)); + REQUIRE(state.quantity() == Quantity {100}); + REQUIRE(state.child_orders().size() == 1); + + state.fill(state.child_orders().begin()->key, price, Quantity {500}); + REQUIRE(state.state() == AlgoState::COMPLETED); + + if (input.scratch) { + // check that we scratched + REQUIRE(state.detached_orders().size() > 0); + } + else { + // check that we *did not* scratch + REQUIRE(state.detached_orders().size() == 0); + } + +} + +TEST_CASE("Dorman profit-orders", "[algo][dorman]") { + AlgoParams input; + input.steps.step_table = { + {500, 250}, + {1000, 450}, + {2000, 650}, + {3500, 750}, + }; + input.steps.maximum = 750; + input.steps.trail_seconds = 2; + input.clip_limit = 100; + + bool test_cancel = true; + + SECTION("Profit Orders ON - Cancel") { + // Send profit orders 3 ticks away + input.profit_orders = {{ 3 }}; + } + SECTION("Profit Orders ON - Complete") { + // Send profit orders 3 ticks away + input.profit_orders = {{ 3 }}; + test_cancel = false; + } + SECTION("Profit Orders OFF") { + input.profit_orders = std::nullopt; + } + SECTION("Profit Orders OFF - complete") { + input.profit_orders = std::nullopt; + test_cancel = false; + } + + api::orderman::OrderDefinition def; + def.set_side(api::SIDE_BUY); + def.mutable_custom_def()->mutable_dynamic()->CopyFrom(to_dynamic_order_params(input)); + auto price = Price { some_price.price() }; + + AlgoTestDriver state(def, some_price, Quantity {100}); + auto secid = SecurityKey(state.security().id()); + auto tick = state.security().contract_spec().low_tick(); + Book book(secid); + book.bid()[0] = { some_price, 1999 }; + state.update_book(book); + REQUIRE(state.before_launch().ok()); + state.launch(); + REQUIRE(state.algo().exposed_qty() == Quantity(650)); + REQUIRE(state.quantity() == Quantity {100}); + REQUIRE(state.child_orders().size() == 1); + + state.fill(state.child_orders().begin()->key, price, Quantity {20}); + + if (input.profit_orders) { + // We're testing the bid side so we expect 3 ticks up + auto expected_price = Price { + price.value() + (tick * input.profit_orders.value().ticks_away) }; + REQUIRE(state.child_orders().size() > 1); + REQUIRE(state.detached_orders().empty()); + size_t profit_order_count = 0; + for (const auto& order : state.child_orders()) { + if (order.def.value().side() == api::SIDE_SELL) { + profit_order_count += 1; + REQUIRE(order.cstate.quantity.value() == 20); + REQUIRE(securities::is_same(order.cstate.price, expected_price)); + } + } + REQUIRE(profit_order_count == 1); + } + else { + REQUIRE(state.child_orders().size() == 1); + REQUIRE(state.detached_orders().empty()); + } + + state.fill(state.child_orders().begin()->key, price, Quantity {42}); + + if (input.profit_orders) { + auto expected_price = Price { + price.value() + (tick * input.profit_orders.value().ticks_away) }; + REQUIRE(state.child_orders().size() > 1); + REQUIRE(state.detached_orders().empty()); + bool got_20 = false; + bool got_42 = false; + for (const auto& order : state.child_orders()) { + if (order.def.value().side() == api::SIDE_SELL) { + if (order.cstate.quantity.value() == 20) got_20 = true; + if (order.cstate.quantity.value() == 42) got_42 = true; + REQUIRE(securities::is_same(order.cstate.price, expected_price)); + } + } + REQUIRE(got_42); + REQUIRE(got_20); + } + else { + REQUIRE(state.child_orders().size() == 1); + REQUIRE(state.detached_orders().empty()); + } + + if (test_cancel) { + // Profit orders should detach on cancel + state.cancel(); + REQUIRE(state.state() == AlgoState::CANCELED); + if (input.profit_orders) { + REQUIRE(state.detached_orders().size() > 0); + } + } + else { + state.fill(state.child_orders().begin()->key, price, Quantity {38}); + REQUIRE(state.state() == AlgoState::COMPLETED); + if (input.profit_orders) { + REQUIRE(state.detached_orders().size() > 0); + } + } +} + +// TEST_CASE("Dorman detaches profit orders when completed by scratch", "[algo][dorman][!hide]") { +// Idea for future test +// } + +TickerUpdateEvent::Trade mkTrade(Price p, Quantity q) { + return { + Timer::now(), + p, + q, + Quantity { q.value() + 9999 }, + co2::AGGRESSOR_SIDE_NONE + }; +} + +TradeVector mkTrades(Price p, Quantity total) { + std::random_device rd; + std::mt19937 gen(rd()); //Standard mersenne_twister_engine seeded with rd() + + uint32_t remaining = total.value(); + TradeVector result; + while(remaining > 0) { + std::uniform_real_distribution<> dis(1, remaining); + auto qty = (uint32_t)dis(gen); + remaining -= qty; + result.push_back(mkTrade(p, Quantity {qty})); + } + return result; +} + +TEST_CASE("Dorman trade accumulation", "[algo][dorman]") { + // Start algo with trade accumulation enabled + // If 100 trades in 5 seconds pause for 6 seconds + // Exceed Limits + // Check that child was canceled and re-created + // Disable accumulator + // attempt to exceed limits + // Check that algo does not turn off + // re-enable accumulator + // Check that algo turns off + // Change parameters + // Check that the algo turns off based on the *new* params + + AlgoParams input; + input.steps.step_table = { + {225, 32}, + {362, 64}, + {589, 128}, + {697, 5000}, + }; + input.steps.maximum = 9999; + input.clip_limit = 8; + // If 100 trades in 5 seconds pause for 6 seconds + input.trade_accumulation = {TradeAccumulationParams { 100, 5, 6 } }; + + api::orderman::OrderDefinition def; + def.set_side(api::SIDE_BUY); + def.mutable_custom_def()->mutable_dynamic()->CopyFrom(to_dynamic_order_params(input)); + auto price = Price { some_price.price() }; + + AlgoTestDriver state(def, some_price, Quantity {2500}); + auto secid = SecurityKey(state.security().id()); + Book book(secid); + book.bid()[0] = { some_price, 1999 }; + state.update_book(book); + REQUIRE(state.before_launch().ok()); + state.launch(); + REQUIRE(state.algo().exposed_qty() == Quantity(5000)); + REQUIRE(state.child_orders().size() == 1); + + state.advance_time(std::chrono::seconds(1)); + + // Need 100 trades in 5 seconds, + // The algorithm we're using has a margin of error so + // lets do 25 trades per second for 5 seconds which should be comfortably over + // send_trades(state, Quantity { 120 }, std::chrono::seconds(5)); + for (auto t = 0; t < 5; ++t) { + state.update_ticker(secid, mkTrades(price, Quantity { 25 })); + state.advance_time(std::chrono::milliseconds(1001), 3); + } + REQUIRE(count_exposed(state.child_orders()) == 0); + + state.advance_time(std::chrono::seconds(4)); + // Check that it hasn't jumped the gun + REQUIRE(count_exposed(state.child_orders()) == 0); + state.advance_time(std::chrono::seconds(3)); + + // Sleep time has expired so we should re-join the market + REQUIRE(count_exposed(state.child_orders()) == 5000); + + + // Disable the accumulator + Update update; + input.trade_accumulation = std::nullopt; + update.mutable_custom()->mutable_dynamic()->CopyFrom(to_dynamic_order_params(input)); + update.set_quantity(5000); + state.do_update(update); + + // Attempt to exceed again + for (auto t = 0; t < 5; ++t) { + state.update_ticker(secid, mkTrades(price, Quantity { 25 })); + state.advance_time(std::chrono::milliseconds(1001), 9); + } + // Still working + REQUIRE(count_exposed(state.child_orders()) == 5000); + + // re-enable accumulator + input.trade_accumulation = {TradeAccumulationParams { 100, 5, 6 } }; + update.mutable_custom()->mutable_dynamic()->CopyFrom(to_dynamic_order_params(input)); + state.do_update(update); + + for (auto t = 0; t < 5; ++t) { + state.update_ticker(secid, mkTrades(price, Quantity { 25 })); + state.advance_time(std::chrono::milliseconds(1001), 4); + } + // turned off? + REQUIRE(count_exposed(state.child_orders()) == 0); + state.advance_time(std::chrono::seconds(10)); + // back on! + REQUIRE(count_exposed(state.child_orders()) == 5000); + + // Change the accumulator params without disabling it + input.trade_accumulation = {TradeAccumulationParams { 1000, 5, 6 } }; + update.mutable_custom()->mutable_dynamic()->CopyFrom(to_dynamic_order_params(input)); + state.do_update(update); + + // Attempt to trigger based on the old settings. + for (auto t = 0; t < 5; ++t) { + state.update_ticker(secid, mkTrades(price, Quantity { 35 })); + state.advance_time(std::chrono::milliseconds(1001), 7); + } + // Still working? + REQUIRE(count_exposed(state.child_orders()) == 5000); + + // Attempt to trigger based on new settings + for (auto t = 0; t < 5; ++t) { + state.update_ticker(secid, mkTrades(price, Quantity { 500 })); + state.advance_time(std::chrono::milliseconds(1001), 5); + } + REQUIRE(count_exposed(state.child_orders()) == 0); + + // complete while paused (fills can arrive after requested cancel) + ChildOrder latest_order = {}; + for (auto& order : state.child_orders()) { + if (order.key > latest_order.key) latest_order = order; + } + state.fill(latest_order.key, price, latest_order.cstate.quantity); + REQUIRE(state.state() == AlgoState::COMPLETED); + +} + +TEST_CASE("Dorman updates child", "[algo][dorman]") { + // When updating the Dorman price the price of the child should also update +} + +TEST_CASE("Dorman market move up then back down", "[algo][dorman]") { + // Start dorman algo + // Move market up so we plan to increase the exposed qty after trail_seconds + // Move market back down before trail_seconds elapses + // check that algo does *not* increase exposure + + AlgoParams input; + input.steps.step_table = { + {500, 250}, + {1000, 450}, + {2000, 650}, + {3500, 750}, + }; + input.steps.maximum = 750; + input.steps.trail_seconds = 30; + input.clip_limit = 100; + api::orderman::OrderDefinition def; + def.set_side(api::SIDE_BUY); + def.mutable_custom_def()->mutable_dynamic()->CopyFrom(to_dynamic_order_params(input)); + // auto price = Price { some_price.price() }; + + AlgoTestDriver state(def, some_price, Quantity {1337}); + auto secid = SecurityKey(state.security().id()); + + Book book(secid); + book.bid()[0] = { some_price, 350 }; + state.update_book(book); + REQUIRE(state.before_launch().ok()); + state.launch(); + REQUIRE(state.algo().exposed_qty() == Quantity(250)); + REQUIRE(state.child_orders().size() == 1); + state.advance_time(std::chrono::seconds(1)); + + // Update book sets future quantity = 450 + book.bid()[0] = { some_price, 999 }; + state.update_book(book); + state.advance_time(std::chrono::seconds(10)); + // Algo does not increase exposure after 10 second + REQUIRE(state.algo().exposed_qty() == Quantity(250)); + + book.bid()[0] = { some_price, 425 }; + state.update_book(book); + state.advance_time(std::chrono::seconds(40)); + // Algo does not increase exposure after trail_seconds + REQUIRE(state.algo().exposed_qty() == Quantity(250)); + +} + + + +}}}//namespace + diff --git a/src/lib/orderman/orders/algo/algo_orders/tests/splitter_tests.cpp b/src/lib/orderman/orders/algo/algo_orders/tests/splitter_tests.cpp new file mode 100644 index 000000000..50b2ee503 --- /dev/null +++ b/src/lib/orderman/orders/algo/algo_orders/tests/splitter_tests.cpp @@ -0,0 +1,727 @@ + +#include "../SplitterAlgo.h" +#include "AlgoTestDriver.h" +#include "catch/catch.hpp" +#include "lib/orderman/orderman_utils/Book.h" +#include "lib/utils/bts_chrono.h" +#include +#include +#include +#include +#include + +using namespace std; +using namespace bts::orderman::splitter; +using namespace bts::api::orderman; + +namespace bts { +namespace orderman { +namespace test { + +/* + +- max/exposed, assert correct # of orders on the market +- completes on target_qty reached +- completes on target_qty exceeded +- send fills, check order state +- send fills, then change target_qty + + */ + +constexpr auto some_price = md::IntPrice{128000, -3}; + +using ExpectedCount = utils::ValueWrapper; +struct SimpleCase { + ExpectedCount expected_orders; + Quantity target; + SplitterOrderDefinition sod; +}; + +SimpleCase mkSimple(ExpectedCount expected, uint32_t max_size, uint32_t exposed, + uint32_t target) { + SimpleCase result; + result.expected_orders = expected; + result.target = Quantity{target}; + result.sod.set_max_order_qty(max_size); + result.sod.set_exposed_qty(exposed); + return result; +} + +TEST_CASE("compute_desired_sizes fuzzing", "[algo][splitter]") { + srand((unsigned)time(0)); + + auto start = Timer::now(); + + while (sec_diff(Timer::now(),start) < 5) { + uint32_t total = rand() % 100 + 1; + uint32_t clip = rand() % 4 + 1; + std::vector constraints(rand() % 10); + std::vector result; + + uint32_t all_fills = 0; + for (auto &mm : constraints) { + uint32_t order_working = rand() % 2; + uint32_t fills = rand() % 2 * clip; + all_fills += fills; + mm = {fills, order_working ? fills + rand() % clip + 1 : fills}; + } + if (all_fills < total) { + compute_desired_sizes(total, clip, constraints, &result); + uint32_t exposed_qty = 0; + for (auto q : result) + exposed_qty += q.value(); + + bool clip_size_honored = true; + for (auto q : result) + clip_size_honored = clip_size_honored && (q.value() <= clip); + + if (exposed_qty != total || !clip_size_honored) { + std::cout << "total: " << total << "\n" + << "clip: " << clip << "\n{ "; + + for (auto mm : constraints) + std::cout << "{" << mm.min << "," << mm.max << "},"; + std::cout << " }\n"; + } + REQUIRE(clip_size_honored); + REQUIRE(exposed_qty == total); + } + } +} + +TEST_CASE("initial orders", "[algo][splitter]") { + std::vector cases; + cases.push_back(mkSimple(ExpectedCount{10}, 10, 100, 50)); + cases.push_back(mkSimple(ExpectedCount{11}, 10, 105, 50)); + cases.push_back(mkSimple(ExpectedCount{5}, 10, 50, 50)); + cases.push_back(mkSimple(ExpectedCount{5}, 10, 50, 150)); + + for (const auto &tcase : cases) { + auto expected = tcase.expected_orders.value(); + api::orderman::OrderDefinition def; + def.set_side(api::SIDE_BUY); + *def.mutable_custom_def()->mutable_splitter() = tcase.sod; + AlgoTestDriver state(def, some_price, tcase.target); + + REQUIRE(state.before_launch().ok()); + state.launch(); + REQUIRE(state.child_orders().size() == expected); + REQUIRE(state.state() == AlgoState::RUNNING); + } +} + +TEST_CASE("completes on target", "[algo][splitter]") { + std::vector cases; + cases.push_back(mkSimple(ExpectedCount{3}, 30, 90, 50)); + cases.push_back(mkSimple(ExpectedCount{10}, 10, 100, 50)); + cases.push_back(mkSimple(ExpectedCount{11}, 10, 105, 50)); + cases.push_back(mkSimple(ExpectedCount{5}, 10, 50, 50)); + cases.push_back(mkSimple(ExpectedCount{5}, 10, 50, 150)); + cases.push_back(mkSimple(ExpectedCount{4}, 13, 50, 150)); + cases.push_back(mkSimple(ExpectedCount{12}, 13, 150, 50)); + + for (const auto &tcase : cases) { + auto expected = tcase.expected_orders.value(); + api::orderman::OrderDefinition def; + def.set_side(api::SIDE_BUY); + *def.mutable_custom_def()->mutable_splitter() = tcase.sod; + AlgoTestDriver state(def, some_price, tcase.target); + + REQUIRE(state.before_launch().ok()); + state.launch(); + REQUIRE(state.child_orders().size() == expected); + REQUIRE(state.state() == AlgoState::RUNNING); + + SECTION("Fill completely") { + auto orders = state.child_orders(); + // Fill the order + size_t filled = 0; + size_t iterations = 0; + while (state.state() == AlgoState::RUNNING) { + // loop to allow more orders to be added + ++iterations; + REQUIRE(iterations < 999); + for (const auto &order : orders) { + state.fill(order.key, Price(some_price.price()), + order.cstate.quantity); + filled += order.cstate.quantity.value(); + REQUIRE(filled == state.total_fills().value()); + if (filled >= tcase.target.value()) { + REQUIRE(state.state() == AlgoState::COMPLETED); + break; + } else { + REQUIRE(state.state() == AlgoState::RUNNING); + } + } + } + } + SECTION("fill one by one") { + const auto &orders = state.child_orders(); + // Fill 1 per iteration. + // Algo should add more orders if not enough are visible initially + size_t filled = 0; + size_t iterations = 0; + while (state.state() == AlgoState::RUNNING) { + ++iterations; + REQUIRE(iterations < 999); // not making progress + for (const auto &order : orders) { + if (order.cstate.status == ChildStatus::WORKING) { + auto price = Price{some_price.price()}; + state.fill(order.key, price, Quantity{1}); + filled += 1; + REQUIRE(filled == state.total_fills().value()); + // Fills should never cause new orders to get added + REQUIRE(state.child_orders().size() == expected); + if (filled >= tcase.target.value()) { + REQUIRE(state.state() == AlgoState::COMPLETED); + break; + } else { + REQUIRE(state.state() == AlgoState::RUNNING); + } + } + } + } + } + } +} + +TEST_CASE("updates", "[algo][splitter]") { + // Start in state 0, update to state 1 + std::vector, SimpleCase>> cases; + + // reduce max order size, increase # orders + cases.push_back({mkSimple(ExpectedCount{10} /*order count*/, 10 /*max*/, + 100 /*exposed*/, 50 /*target*/), + {}, + mkSimple(ExpectedCount{20}, 5, 100, 50)}); + /// reduce max order size, keep # orders constant + cases.push_back({mkSimple(ExpectedCount{10}, 10, 100, 50), + {}, + mkSimple(ExpectedCount{10}, 5, 50, 50)}); + cases.push_back({mkSimple(ExpectedCount{10}, 10, 100, 50), + {5, 5}, + mkSimple(ExpectedCount{8}, 5, 50, 40)}); + cases.push_back({mkSimple(ExpectedCount{10}, 10, 100, 50), + {5, 5}, + mkSimple(ExpectedCount{18}, 5, 100, 50)}); + + for (const auto &tcase : cases) { + auto case_init = std::get<0>(tcase); + api::orderman::OrderDefinition def; + def.set_side(api::SIDE_BUY); + *def.mutable_custom_def()->mutable_splitter() = case_init.sod; + AlgoTestDriver state(def, some_price, case_init.target); + + REQUIRE(state.before_launch().ok()); + state.launch(); + REQUIRE(state.state() == AlgoState::RUNNING); + REQUIRE(state.child_orders().size() == case_init.expected_orders.value()); + for (const auto &o : state.child_orders()) { + if (o.cstate.status == ChildStatus::WORKING) { + REQUIRE(o.cstate.quantity.value() <= case_init.sod.exposed_qty()); + } + } + + uint32_t fills = 0; + for (auto q : std::get<1>(tcase)) { + state.fill({}, Quantity{q}); + fills += q; + } + + REQUIRE(state.total_fills().value() == fills); + + Update update; + auto case_next = std::get<2>(tcase); + update.set_price(some_price.price()); + update.set_quantity(case_next.target.value()); + *def.mutable_custom_def()->mutable_splitter() = case_next.sod; + *update.mutable_custom() = def.custom_def(); + + state.do_update(update); + REQUIRE(state.state() == AlgoState::RUNNING); + size_t working = 0; + for (const auto &c : state.child_orders()) + if (c.cstate.status == ChildStatus::WORKING) + working += 1; + REQUIRE(working == case_next.expected_orders.value()); + for (const auto &o : state.child_orders()) { + if (o.cstate.status == ChildStatus::WORKING) { + REQUIRE(o.cstate.quantity.value() <= case_next.sod.exposed_qty()); + } + } + } +} +TEST_CASE("Reduction with fills", "[algo][splitter]") { + // Fills represent a floor on how much a trade can be reduced + // IF we have expose:400, clip: 100 we get + // 100,100,100,100 + // Receive fills + // 100/50, 100/50, 100/50, 100/50 + // Change expose to 300 + // naive: 100, 100, 100 but we *can't* reduce the last to 0 + // expected: 100, 100, 50, 50 + api::orderman::OrderDefinition def; + def.set_side(api::SIDE_BUY); + auto *splitter = def.mutable_custom_def()->mutable_splitter(); + splitter->set_max_order_qty(100); + splitter->set_exposed_qty(400); + AlgoTestDriver state(def, some_price, Quantity{300}); + + REQUIRE(state.before_launch().ok()); + state.launch(); + // check initial state + REQUIRE(state.state() == AlgoState::RUNNING); + REQUIRE(state.child_orders().size() == 4); + for (const auto &c : state.child_orders()) { + REQUIRE(c.cstate.quantity.value() == 100); + } + + // Add fills + for (const auto &order : state.child_orders()) { + state.fill(order.key, {}, Quantity{50}); + } + + // Reduce exposed quantity + Update update; + update.set_quantity(300); + update.mutable_custom()->mutable_splitter()->set_exposed_qty(300); + state.do_update(update); + + REQUIRE(state.state() == AlgoState::RUNNING); + REQUIRE(state.child_orders().size() == 4); + size_t total = 0; + for (const auto &order : state.child_orders()) { + total += order.cstate.quantity.value(); + } + REQUIRE(total == 300); + + // need this to be 100,100,50,50 not 100,100,100 + std::set actual, expected{100, 100, 50, 50}; + for (const auto &order : state.child_orders()) { + actual.insert(order.cstate.quantity.value()); + } + REQUIRE(actual == expected); + + REQUIRE(state.total_fills().value() == 200); +} + +TEST_CASE("Reduction with fills 2", "[algo][splitter]") { + // Fills represent a floor on how much a trade can be reduced + // IF we have expose:400, clip: 100 we get + // 100,100,100,100 + // Receive fills + // 100/35, 100/45, 100/55, 100/65 + // Change expose to 300 + // naive: 100, 100, 100 but we *can't* reduce the last to 0 + // expected: 100, 100, 65, 35 + api::orderman::OrderDefinition def; + def.set_side(api::SIDE_BUY); + auto *splitter = def.mutable_custom_def()->mutable_splitter(); + splitter->set_max_order_qty(100); + splitter->set_exposed_qty(400); + AlgoTestDriver state(def, some_price, Quantity{300}); + + REQUIRE(state.before_launch().ok()); + state.launch(); + // check initial state + REQUIRE(state.state() == AlgoState::RUNNING); + REQUIRE(state.child_orders().size() == 4); + for (const auto &c : state.child_orders()) { + REQUIRE(c.cstate.quantity.value() == 100); + } + + // Add fills + state.fill({}, Quantity{35}); + state.fill({}, Quantity{45}); + state.fill({}, Quantity{55}); + state.fill({}, Quantity{65}); + + // Reduce exposed quantity + Update update; + update.set_quantity(300); + update.mutable_custom()->mutable_splitter()->set_exposed_qty(300); + state.do_update(update); + + REQUIRE(state.state() == AlgoState::RUNNING); + REQUIRE(state.child_orders().size() == 4); + size_t total = 0; + for (const auto &order : state.child_orders()) { + total += order.cstate.quantity.value(); + } + REQUIRE(total == 300); + + // need this to be 100,100,50,50 not 100,100,100 + std::set actual, expected{100, 100, 65, 35}; + for (const auto &order : state.child_orders()) { + actual.insert(order.cstate.quantity.value()); + } + REQUIRE(actual == expected); +} + +TEST_CASE("Reduction with fills 3", "[algo][splitter]") { + // Fills represent a floor on how much a trade can be reduced + // IF we have expose:400, clip: 100 we get + // 100,100,100,100 + // Receive fills + // 100/35, 100/45, 100/55, 100/65 + // Change expose to 201 + // naive: 100, 100, 1 but we *can't* reduce the last to 0 + // expected: 66, 55, 45, 35 + api::orderman::OrderDefinition def; + def.set_side(api::SIDE_BUY); + auto *splitter = def.mutable_custom_def()->mutable_splitter(); + splitter->set_max_order_qty(100); + splitter->set_exposed_qty(400); + AlgoTestDriver state(def, some_price, Quantity{300}); + + REQUIRE(state.before_launch().ok()); + state.launch(); + // check initial state + REQUIRE(state.state() == AlgoState::RUNNING); + REQUIRE(state.child_orders().size() == 4); + for (const auto &c : state.child_orders()) { + REQUIRE(c.cstate.quantity.value() == 100); + } + + // Add fills + state.fill({}, Quantity{35}); + state.fill({}, Quantity{45}); + state.fill({}, Quantity{55}); + state.fill({}, Quantity{65}); + + // Reduce exposed quantity + Update update; + update.set_quantity(201); + update.mutable_custom()->mutable_splitter()->set_exposed_qty(201); + state.do_update(update); + + REQUIRE(state.state() == AlgoState::RUNNING); + REQUIRE(state.child_orders().size() == 4); + size_t total = 0; + for (const auto &order : state.child_orders()) { + total += order.cstate.quantity.value(); + } + REQUIRE(total == 201); + + std::set actual, expected{66, 55, 45, 35}; + for (const auto &order : state.child_orders()) { + actual.insert(order.cstate.quantity.value()); + } + REQUIRE(actual == expected); +} + +TEST_CASE("reduce to completion", "[algo][splitter]") { + // Fills represent a floor on how much a trade can be reduced + // IF we have expose:400, clip: 100 we get + // 100,100,100,100 + // Receive fills + // 100/100, 100/100, 100/0, 100/00 + // Change expose to 200 + // expected: algo transitions to copmleted state canceling 2 orders + api::orderman::OrderDefinition def; + def.set_side(api::SIDE_BUY); + auto *splitter = def.mutable_custom_def()->mutable_splitter(); + splitter->set_max_order_qty(100); + splitter->set_exposed_qty(400); + AlgoTestDriver state(def, some_price, Quantity{300}); + + REQUIRE(state.before_launch().ok()); + state.launch(); + // check initial state + REQUIRE(state.state() == AlgoState::RUNNING); + REQUIRE(state.child_orders().size() == 4); + for (const auto &c : state.child_orders()) { + REQUIRE(c.cstate.quantity.value() == 100); + } + + // Add fills + state.fill({}, Quantity{100}); + state.fill({}, Quantity{100}); + + // Reduce exposed quantity + Update update; + update.set_quantity(200); + def.mutable_custom_def()->mutable_splitter()->set_exposed_qty(200); + state.do_update(update); + + REQUIRE(state.state() == AlgoState::COMPLETED); + size_t total = 0; + for (const auto &order : state.child_orders()) { + if (order.cstate.status == ChildStatus::WORKING) { + total += order.cstate.quantity.value(); + } else { + total += order.cstate.total_fills.value(); + } + } + REQUIRE(total == 200); +} + +TEST_CASE("reduce to completion 2", "[algo][splitter]") { + // Fills represent a floor on how much a trade can be reduced + // IF we have expose:400, clip: 100 we get + // 100,100,100,100 + // Receive fills + // 100/100, 100/100, 100/0, 100/00 + // Change expose to 200 + // expected: algo transitions to copmleted state canceling 2 orders + api::orderman::OrderDefinition def; + def.set_side(api::SIDE_BUY); + auto *splitter = def.mutable_custom_def()->mutable_splitter(); + splitter->set_max_order_qty(100); + splitter->set_exposed_qty(400); + AlgoTestDriver state(def, some_price, Quantity{300}); + + REQUIRE(state.before_launch().ok()); + state.launch(); + // check initial state + REQUIRE(state.state() == AlgoState::RUNNING); + REQUIRE(state.child_orders().size() == 4); + for (const auto &c : state.child_orders()) { + REQUIRE(c.cstate.quantity.value() == 100); + } + + // Add fills + state.fill({}, Quantity{100}); + state.fill({}, Quantity{50}); + state.fill({}, Quantity{50}); + + // Reduce exposed quantity + Update update; + update.set_quantity(200); + state.do_update(update); + + REQUIRE(state.state() == AlgoState::COMPLETED); +} + +TEST_CASE("Async Fills", "[algo][splitter]") { + // OK, NOW it gets tricky because we're going to test the asyncronous stuff + + // Start with 4 orders size 20 outstanding (80 total exposed) + // Change exposure to 64 so one of the four will be reduced to 5 + // Before the change is confirmed fill the one that is reducing by 7 + // which would constitute an overfill + // Algo must reduce a second order to compensate + + api::orderman::OrderDefinition def; + def.set_side(api::SIDE_BUY); + auto *splitter = def.mutable_custom_def()->mutable_splitter(); + splitter->set_max_order_qty(20); + splitter->set_exposed_qty(80); + AlgoTestDriver state(def, some_price, Quantity{80}); + + REQUIRE(state.before_launch().ok()); + state.launch(); + // check initial state + REQUIRE(state.state() == AlgoState::RUNNING); + REQUIRE(state.child_orders().size() == 4); + for (const auto &c : state.child_orders()) { + REQUIRE(c.cstate.quantity.value() == 20); + } + + // Set up some instant fills + bool applied_fills = false; + state.on_child_changing([&](const auto &) { + if (applied_fills) return; + applied_fills = true; + // 7 fills on the last order which is has a size reduction in flight + state.fill(state.child_orders().rbegin()->key, {}, Quantity{7}); + }); + + Update update; + update.set_price(state.price().value()); + update.set_quantity(65); + update.mutable_custom()->mutable_splitter()->set_exposed_qty(65); + // This should trigger 1 order to be reduced to size 5 from 20 + // in the meantime we'll trigger fills of size 7 + // This results in an overfill for the 5 lot order + // And the + state.do_update(update); + + // expected result: + // 7 fills, exposed_qty 65 = 58 active + uint32_t active = 0; + for (const auto &o : state.child_orders()) + active += o.active_qty(); + REQUIRE(active == 58); +} + +TEST_CASE("Increase then decrease", "[algo][splitter]") { + // Start with 3 orders of size 100 + // Increase the size to 400 so we launch a 4th order + // Then instantly send another update to 350 + + // NOTE: this test is not very good due to limitations in the test driver + // I'm leaving it in because it passes but if it starts failing in the future + // it's probably OK to disable it + + api::orderman::OrderDefinition def; + def.set_side(api::SIDE_BUY); + auto *splitter = def.mutable_custom_def()->mutable_splitter(); + splitter->set_max_order_qty(100); + splitter->set_exposed_qty(300); + AlgoTestDriver state(def, some_price, Quantity{300}); + + REQUIRE(state.before_launch().ok()); + state.launch(); + // check initial state + REQUIRE(state.state() == AlgoState::RUNNING); + REQUIRE(state.child_orders().size() == 3); + for (const auto &c : state.child_orders()) { + REQUIRE(c.cstate.quantity.value() == 100); + } + + Update update; + update.set_price(state.price().value()); + update.set_quantity(state.quantity().value()); + update.mutable_custom()->mutable_splitter()->set_exposed_qty(400); + + // Set up some instant fills + bool applied_second_update = false; + state.on_child_launching([&, update](const auto &) mutable { + if (!applied_second_update) { + applied_second_update = true; + update.mutable_custom()->mutable_splitter()->set_exposed_qty(350); + state.do_update(update); + } + }); + + state.do_update(update); + + // expected result: + // 350 exposed, NOT 450 + uint32_t quantity = 0; + for (const auto &o : state.child_orders()) + quantity += o.active_qty(); + REQUIRE(quantity == 350); +} + +std::multiset order_set(const std::vector& orders, ChildStatus status, const uint32_t* clip) { + std::multiset res; + for (const auto &c : orders) { + if (c.cstate.status == status) { + res.insert(c.cstate.quantity.value()); + } + if (clip) { + REQUIRE(c.cstate.quantity.value() <= *clip); + } + } + return res; +} + +TEST_CASE("exposed < target", "[algo][splitter]") { + // Test various scenarios where exposed < target + + api::orderman::OrderDefinition def; + def.set_side(api::SIDE_BUY); + auto *splitter = def.mutable_custom_def()->mutable_splitter(); + + uint32_t clip = 100, exposed = 200, target = 300; + + splitter->set_max_order_qty(clip); + splitter->set_exposed_qty(exposed); + AlgoTestDriver state(def, some_price, Quantity{target}); + + REQUIRE(state.before_launch().ok()); + state.launch(); + // check initial state + REQUIRE(state.state() == AlgoState::RUNNING); + REQUIRE(state.child_orders().size() == exposed/clip); + + { + std::multiset + expected { 100, 100 }, + actual = order_set(state.child_orders(), ChildStatus::WORKING, &clip); + REQUIRE(expected == actual); + } + + auto update = [&state](uint32_t clip, uint32_t exposed, uint32_t target) { + Update update; + update.set_price(state.price().value()); + update.set_quantity(target); + update.mutable_custom()->mutable_splitter()->set_exposed_qty(exposed); + update.mutable_custom()->mutable_splitter()->set_max_order_qty(clip); + state.do_update(update); + }; + + // update exposure to 100 + // expect 1 order @ 100 + { + update(100u,100u,100u); + std::multiset + expected { 100 }, + actual = order_set(state.child_orders(), ChildStatus::WORKING, &clip); + REQUIRE(expected == actual); + REQUIRE(state.state() == AlgoState::RUNNING); + } + + // update exposed to 400 (exposed > target) and clip to 50 + // expect 8 orders + { + update(50u,400u,300u); + std::multiset + expected { 50, 50, 50, 50, + 50, 50, 50, 50}, + actual = order_set(state.child_orders(), ChildStatus::WORKING, &clip); + REQUIRE(expected == actual); + REQUIRE(state.state() == AlgoState::RUNNING); + } + + // update target to 450 (exposed < target) + // expect 4 order + { + update(100,400,450u); + std::multiset + expected { 100, 100, 100, 100 }, + actual = order_set(state.child_orders(), ChildStatus::WORKING, &clip); + REQUIRE(expected == actual); + REQUIRE(state.state() == AlgoState::RUNNING); + } + + // fill 400 + // expect 0 working children but order not complete + state.fill(Price {some_price.price()}, Quantity {400}); + REQUIRE(state.state() == AlgoState::RUNNING); + // Expect there to be *NO* working orders at this point, we're still running though + std::multiset + expected {}, + actual = order_set(state.child_orders(), ChildStatus::WORKING, &clip); + REQUIRE(expected == actual); + + // Raise the exposed qty to 450 + // So that we can get the last 50 filled + // and complete the order + { + update(100,450,450u); + std::multiset + expected { 50 }, + actual = order_set(state.child_orders(), ChildStatus::WORKING, &clip); + REQUIRE(expected == actual); + REQUIRE(state.state() == AlgoState::RUNNING); + state.fill(Price {some_price.price()}, Quantity {50}); + REQUIRE(state.state() == AlgoState::COMPLETED); + } + +} + +TEST_CASE("Initial exposed = 0", "[algo][splitter]") { + // Test various scenarios where exposed < target + + api::orderman::OrderDefinition def; + def.set_side(api::SIDE_BUY); + auto *splitter = def.mutable_custom_def()->mutable_splitter(); + + splitter->set_max_order_qty(100); + splitter->set_exposed_qty(0); + AlgoTestDriver state(def, some_price, Quantity{300}); + + REQUIRE(state.before_launch().ok()); + state.launch(); + // check initial state + REQUIRE(state.state() == AlgoState::RUNNING); + REQUIRE(state.child_orders().empty()); +} + + +} // namespace test +} // namespace orderman +} // namespace bts diff --git a/src/lib/orderman/orders/algo/doc/algo_wrapper_sm.png b/src/lib/orderman/orders/algo/doc/algo_wrapper_sm.png new file mode 100644 index 000000000..4e49734d6 Binary files /dev/null and b/src/lib/orderman/orders/algo/doc/algo_wrapper_sm.png differ diff --git a/src/lib/orderman/orders/algo/doc/state_machine.org b/src/lib/orderman/orders/algo/doc/state_machine.org new file mode 100644 index 000000000..5eaf1b7bf --- /dev/null +++ b/src/lib/orderman/orders/algo/doc/state_machine.org @@ -0,0 +1,39 @@ +#+TITLE: Algo Wrapper State Machien +#+OPTIONS: ^:nil +#+OPTIONS: H:2 +#+OPTIONS: ^:nil H:2 author:nil creator:nil timestamp:nil toc:nil +#+OPTIONS: html-postamble:nil + + +* State Machine +#+begin_src dot :file algo_wrapper_sm.png :cmdline -Kdot -Tpng +digraph G { + bgcolor="transparent"; + edge [ fontname=Helvetica, fontsize=12 ]; + + start [style=invisible]; + COMPLETED [style=filled] ; + start -> INACTIVE; + INACTIVE -> COMPLETED [label="qty=0|error"]; + INACTIVE -> W_ATTACH [label="ok & attach"]; + INACTIVE -> WORKING [label="ok & !attach"]; + W_ATTACH -> COMPLETED [label=attach_fail]; + W_ATTACH -> WORKING [label=attach_ok]; + WORKING -> COMPLETED [label="[wchild=0]cancel|complete"]; + WORKING -> COMPLETED [label=error]; + WORKING -> SHUTTING_DOWN [label="[wchild>0]cancel|complete"]; + SHUTTING_DOWN -> COMPLETED [label="wchild==0|error|on_detach"]; +} +#+END_SRC + +#+RESULTS: +[[file:algo_wrapper_sm.png]] + +* Invariants +| | attach_target | exernal_status | pending_update | pending_terminate | working_children | subs | +|---------------+---------------+--------------------------------------+----------------+-------------------+------------------+------| +| INACTIVE | | | | | | 0 | +| W_ATTACH | 1 | WORKING | | 0 | 0 | 0 | +| WORKING | | WORKING | 0 | 0 | | | +| SHUTTING_DOWN | | WORKING | 0 | 1 | >0 | 0 | +| COMPLETED | | CANCELED, COMPLETED, ERROR, REJECTED | 0 | 0 | 0 | 0 | diff --git a/src/lib/orderman/orders/algo/utils/Accumulator.h b/src/lib/orderman/orders/algo/utils/Accumulator.h new file mode 100644 index 000000000..f4e3f5ebc --- /dev/null +++ b/src/lib/orderman/orders/algo/utils/Accumulator.h @@ -0,0 +1,48 @@ +#pragma once + +#include "lib/utils/bts_chrono.h" +#include + +namespace bts { +namespace orderman { + +struct Accumulator { + uint32_t threshold; + TimeStamp top; + Duration bucket_interval = std::chrono::seconds(1); + std::vector buckets; + uint32_t current_bucket = 0; + + Accumulator(uint32_t threshold, uint32_t window_seconds, TimeStamp start) : threshold(threshold), top(start) { + buckets.resize(window_seconds); + } + + void push_back(TimeStamp now, size_t contracts) { + // If current time is within bucket increment the count + // Otherwise advance to the next bucket and clear it + // by the number of 'bucket_intervals' that have passed and set that bucket + auto delta = (now - top); + while(delta > bucket_interval) { + delta -= bucket_interval; + current_bucket = (current_bucket+1) % buckets.size(); + buckets[current_bucket] = 0; + top += bucket_interval; + } + buckets[current_bucket] += contracts; + } + + size_t total() const { + size_t count= 0; + for (const auto& b : buckets) { + count += b; + } + return count; + } + + bool exceeded() const { + return total() >= threshold; + } + +}; + +}} //namespace diff --git a/src/lib/orderman/orders/book_algos/OffsetOrder.cpp b/src/lib/orderman/orders/book_algos/OffsetOrder.cpp deleted file mode 100644 index 6a1a6972d..000000000 --- a/src/lib/orderman/orders/book_algos/OffsetOrder.cpp +++ /dev/null @@ -1,102 +0,0 @@ -#include "OffsetOrder.h" - -#include "lib/orderman/actors/order_manager/OrderBuilder.h" -#include "lib/orderman/orders/spreader_orders/spreader/SpreaderUtils.h" -#include "lib/utils/PBUtils.h" -#include - -namespace bts { -namespace orderman { - -using namespace api::orderman; - -struct OffsetOrder::PImpl -{ - PImpl() {} - - uint32_t tick_offset_on_deploy; -}; - - -OffsetOrder::OffsetOrder() : me(new PImpl()) {} - -OffsetOrder::~OffsetOrder() {} - -void OffsetOrder::setup_child(const api::orderman::OrderDefinition& this_def, const PreActivationOrderServices&, - api::orderman::OrderState& initial_state, api::orderman::OrderDefinition* out_child) -{ - initial_state.mutable_offset(); - auto const &offset_def = this_def.custom_def().offset(); - *out_child->mutable_custom_def() = offset_def.child(); - - me->tick_offset_on_deploy = offset_def.tick_offset_on_deploy(); -} - - -void OffsetOrder::on_book_update(const bts::orderman::BookUpdateEvent& book) -{ - switch (this_state().status()) - { - case ORDER_STATUS_WORKING: - switch (child_state().status()) - { - case ORDER_STATUS_INACTIVE: - { - if(definition().side() == api::SIDE_BUY) - { - const auto& same = book.book.side(api::Side::SIDE_BUY)[0]; - double price = same.price() - tick_size() * me->tick_offset_on_deploy; - activate_child(Price(price), Quantity(this_state().quantity())); - transition(ORDER_STATUS_WORKING); - } - else - { - const auto& same = book.book.side(api::Side::SIDE_SELL)[0]; - double price = same.price() + tick_size() * me->tick_offset_on_deploy; - activate_child(Price(price), Quantity(this_state().quantity())); - transition(ORDER_STATUS_WORKING); - } - report_state(); - } - break; - case ORDER_STATUS_WORKING: - { - detach(); - } - break; - - case ORDER_STATUS_PENDING_WORKING: - break; - - // child cannot be in any of these states if we are in the working state - case ORDER_STATUS_DISCONNECTED: - case ORDER_STATUS_ERROR: - case ORDER_STATUS_REJECTED_BY_SPARK: - case ORDER_STATUS_COMPLETED: - case ORDER_STATUS_CANCELED: - case OrderStatus_INT_MIN_SENTINEL_DO_NOT_USE_: - case OrderStatus_INT_MAX_SENTINEL_DO_NOT_USE_: - bts_assert(false); - break; - } - break; - - case ORDER_STATUS_COMPLETED: - case ORDER_STATUS_CANCELED: - case ORDER_STATUS_ERROR: - break; - - case ORDER_STATUS_INACTIVE: - case ORDER_STATUS_REJECTED_BY_SPARK: - case ORDER_STATUS_PENDING_WORKING: - case ORDER_STATUS_DISCONNECTED: - case OrderStatus_INT_MIN_SENTINEL_DO_NOT_USE_: - case OrderStatus_INT_MAX_SENTINEL_DO_NOT_USE_: - bts_assert(false); - break; - } -} - -} -} - diff --git a/src/lib/orderman/orders/book_algos/OffsetOrder.h b/src/lib/orderman/orders/book_algos/OffsetOrder.h deleted file mode 100644 index 7b9ad70fb..000000000 --- a/src/lib/orderman/orders/book_algos/OffsetOrder.h +++ /dev/null @@ -1,29 +0,0 @@ -#pragma once - -#include "lib/orderman/orders/book_algos/BookAlgo.h" - -namespace bts { -namespace orderman { - -class OrderStateUpdateEvent; -class BookUpdateEvent; - -//OffsetOrder deploys N ticks away from the initial same-side inside market it's mainly for composition / fat finger - -class OffsetOrder : public BookAlgo -{ -public: - OffsetOrder(); - virtual ~OffsetOrder(); - -protected: - void setup_child(const api::orderman::OrderDefinition& this_def, const PreActivationOrderServices&, - api::orderman::OrderState&, api::orderman::OrderDefinition*) override; - void on_book_update(const bts::orderman::BookUpdateEvent&) override; - -private: - struct PImpl; - std::unique_ptr me; -}; - -}} diff --git a/src/lib/orderman/server/OrderServiceServer.cpp b/src/lib/orderman/server/OrderServiceServer.cpp index af49ce401..381673dac 100644 --- a/src/lib/orderman/server/OrderServiceServer.cpp +++ b/src/lib/orderman/server/OrderServiceServer.cpp @@ -1,11 +1,13 @@ #include "OrderServiceServer.h" #include "lib/api/public/ServiceContext.h" #include "lib/api/services/AdminService.client.pb.h" +#include "lib/api/services/MarginService.client.pb.h" +#include "lib/api/services/PositionService.client.pb.h" #include "lib/config/config.h" #include "lib/config/protos/jetfix.pb.h" #include "lib/config/protos/orderman_cfg.pb.h" -#include "lib/md/MDConsumer.h" #include "lib/md/MDClient.h" +#include "lib/md/MDConsumer.h" #include "lib/mercury/actors/Timer.h" #include "lib/mercury/executor/Executor.h" #include "lib/metrics/metrics.h" @@ -45,14 +47,14 @@ struct ActorThread { stats.publish(loop_metric); } - void start(trace::PerfLog& log) { + void start(trace::PerfLog &log) { if (cfg.poll()) - thread = threading::make_thread(cfg.thread_name(), [this,&log]() { + thread = threading::make_thread(cfg.thread_name(), [this, &log]() { for (;;) executor.do_work(log); }); else { - thread = threading::make_thread(cfg.thread_name(), [this,&log]() { + thread = threading::make_thread(cfg.thread_name(), [this, &log]() { const auto inner_iter = 1000; while (!done) { auto start = Timer::now(); @@ -90,14 +92,16 @@ ActorThread *find_thread_for_actor( } struct OrderServiceServer::PImpl { - PImpl(bts::api::ServiceContext &ctx, - md::MDClient& mdc, + PImpl(bts::api::ServiceContext &ctx, md::MDClient &mdc, DynamicOrderRegistry const &d, metrics::MetricsContainer &stats, trace::PerfLog &log) : service_bridge(), service_ready(false), known_orders(), known_fills(), publish_orders(), publish_fills(), - admin_service(ctx.locate()), md_consumer(mdc.current_consumer()), - stats(stats), dyn_order_registry(d), log(log), in_flight_order_ops(), + admin_service(ctx.locate()), + position_service(ctx.locate()), + margin_service(ctx.locate()), + md_consumer(mdc.current_consumer()), stats(stats), + dyn_order_registry(d), log(log), in_flight_order_ops(), current_req_op_id(0) {} ServiceBridge *service_bridge; @@ -111,7 +115,9 @@ struct OrderServiceServer::PImpl { OrderServiceServer::OrdersPublisher publish_orders; OrderServiceServer::FillsPublisher publish_fills; api::admin::AdminService admin_service; - bts::md::MDConsumer& md_consumer; + api::position::PositionService position_service; + api::position::MarginService margin_service; + bts::md::MDConsumer &md_consumer; metrics::MetricsContainer &stats; DynamicOrderRegistry const &dyn_order_registry; trace::PerfLog &log; @@ -120,12 +126,13 @@ struct OrderServiceServer::PImpl { uint32_t current_req_op_id; }; -OrderServiceServer::OrderServiceServer( - bts::api::ServiceContext &ctx, - md::MDClient& mdc, - DynamicOrderRegistry const &d, metrics::MetricsContainer &stats, - trace::PerfLog &log) - : api::orderman::OrderServiceBase(ctx), me(new PImpl(ctx, mdc, d, stats,log)) { +OrderServiceServer::OrderServiceServer(bts::api::ServiceContext &ctx, + md::MDClient &mdc, + DynamicOrderRegistry const &d, + metrics::MetricsContainer &stats, + trace::PerfLog &log) + : api::orderman::OrderServiceBase(ctx), + me(new PImpl(ctx, mdc, d, stats, log)) { me->publish_orders = make_orders_publisher(); me->publish_fills = make_fills_publisher(); @@ -152,7 +159,7 @@ OrderServiceServer::OrderServiceServer( bts_assert_msg(thread, "Could not find a configured thread for OrderManager. See " "the OrdermanCfg key"); - thread->executor.spawn(d,me->log, mdc.oracle()); + thread->executor.spawn(d, me->log, mdc.oracle()); // Timer thread = find_thread_for_actor("Timer", me->actor_threads); @@ -212,12 +219,10 @@ OrderServiceServer::OrderServiceServer( }, [this, &ctx](auto const &order_op_result) { ctx.post([this, order_op_result] { - this->on_order_op_result(order_op_result); + this->on_order_op_result(order_op_result); }); }, - me->dyn_order_registry, - thread->executor.make_proxy(),mdc.oracle()); - + me->dyn_order_registry, thread->executor.make_proxy(), mdc.oracle()); thread->executor.activate(me->service_bridge); @@ -235,6 +240,19 @@ OrderServiceServer::OrderServiceServer( bts_assert(update.all().size() == 1); me->service_bridge->update_admin_data(update.all()[0]); }); + + // subscribe to position updates and foward them to the service bridge + me->position_service.subscribe_pnl_account_data() | + rxcpp::operators::subscribe( + [this](auto const &update) { + me->service_bridge->update_account_pnl(update); + }); + + me->margin_service.subscribe_excess_liquidity_report() | + rxcpp::operators::subscribe( + [this](auto const &update) { + me->service_bridge->update_excess_liquidity_report(update); + }); } OrderServiceServer::~OrderServiceServer() { @@ -358,7 +376,7 @@ void OrderServiceServer::get_registered_dynamic_order_types( void OrderServiceServer::on_order_op_result( OrderOpResultEvent const &op_result) { auto it = me->in_flight_order_ops.find(op_result.req_id); - bts_assert(it!=me->in_flight_order_ops.end()); + bts_assert(it != me->in_flight_order_ops.end()); auto &responder = it->second; api::orderman::Response r; r.set_order_key(op_result.ok.value()); diff --git a/src/lib/plugin/SparkServices.cpp b/src/lib/plugin/SparkServices.cpp index b3c74fae3..6d75d7959 100644 --- a/src/lib/plugin/SparkServices.cpp +++ b/src/lib/plugin/SparkServices.cpp @@ -1,5 +1,6 @@ #include "SparkServices.h" #include "lib/orderman/order_factory/DynamicOrderRegistry.h" +#include "lib/logging/logging.h" namespace bts { @@ -21,10 +22,67 @@ void SparkServices::register_dynamic_order( me->dyn_order_registry.register_dynamic_order(desc, builder); } -void SparkServices::register_dynamic_algo_order( +bool validate_param(const api::orderman::DynamicOrderParamDescriptor& param); +bool validate_type(const std::string name, const api::orderman::DynamicOrderTypeDescriptor& type) { + switch(type.descriptor_case()) { + case api::orderman::DynamicOrderTypeDescriptor::DescriptorCase::kPrimitive: break; + case api::orderman::DynamicOrderTypeDescriptor::DescriptorCase::kArray: + if (!type.array().has_element_type()) { + LOG(OrderMan, Error, "Array parameter {} missing element type", name); + return false; + } + if (!validate_type(name + "::array", type.array().element_type())) return false; + break; + case api::orderman::DynamicOrderTypeDescriptor::DescriptorCase::kStructure: + for(const auto& field : type.structure().field()) { + if (!validate_param(field)) return false; + } + break; + case api::orderman::DynamicOrderTypeDescriptor::DescriptorCase::kEnumerated: + if (type.enumerated().element_size() == 0) { + LOG(OrderMan, Error, "Enumerated parameter {} missing options list", name); + return false; + } + case api::orderman::DynamicOrderTypeDescriptor::DescriptorCase::DESCRIPTOR_NOT_SET: + break; + } + return true; +} +bool validate_param(const api::orderman::DynamicOrderParamDescriptor& param) { + if (param.param_name().empty()) { + LOG(OrderMan, Error, "Parameter missing param_name"); + return false; + } + if (!param.has_param_type()) { + LOG(OrderMan, Error, "Parameter {} missing type", param.param_name()); + return false; + } + return validate_type(param.param_name(), param.param_type()); + return true; +} + +bool validate_descriptor(const api::orderman::DynamicOrderDescriptor& desc) { + if (desc.dynamic_order_type().empty()) { + LOG(OrderMan, Error, "Algo missing 'dynamic_order_type'"); + return false; + } + for (const auto& param : desc.param_desc()) { + if (!validate_param(param)) { + return false; + } + } + return true; +} + +AlgoRegistrationResult SparkServices::register_dynamic_algo_order( api::orderman::DynamicOrderDescriptor const &desc, std::function()> const &builder) { - me->dyn_order_registry.register_dynamic_algo_order(desc, builder); + + if (validate_descriptor(desc)) { + me->dyn_order_registry.register_dynamic_algo_order(desc, builder); + return AlgoRegistrationResult::SUCCESS; + } + return AlgoRegistrationResult::ERROR; } extern "C" void initialize_spark_plugin(SparkServices &) {} diff --git a/src/lib/plugin/SparkServices.h b/src/lib/plugin/SparkServices.h index 70de0c9cf..d4ef29221 100644 --- a/src/lib/plugin/SparkServices.h +++ b/src/lib/plugin/SparkServices.h @@ -13,6 +13,11 @@ class IAlgoOrder; class DynamicOrderRegistry; } // namespace orderman +enum class AlgoRegistrationResult { + SUCCESS, + ERROR +}; + class SparkServices { public: SparkServices(orderman::DynamicOrderRegistry &); @@ -22,7 +27,7 @@ class SparkServices { api::orderman::DynamicOrderDescriptor const &desc, std::function()> const &builder); - void register_dynamic_algo_order( + AlgoRegistrationResult register_dynamic_algo_order( api::orderman::DynamicOrderDescriptor const &desc, std::function()> const &builder); private: diff --git a/src/lib/utils/AuthSessionManager.cpp b/src/lib/utils/AuthSessionManager.cpp index 11d620282..dddb415fd 100644 --- a/src/lib/utils/AuthSessionManager.cpp +++ b/src/lib/utils/AuthSessionManager.cpp @@ -86,7 +86,7 @@ bool AuthSessionManager::add_or_replace_session(const api::user::TokenPayload& t return true; if(datetime::ll_diff_in_us(datetime::ll_now(), _expiration) < 0) { - LOG(Gen,Error, "Your BTS Keycode is expired!"); + LOG(User,Error, "Your BTS Keycode is expired!"); if(msg) *msg = "Your BTS Keycode is EXPIRED! Please contact sales@bluetradesys.com"; return false; @@ -105,13 +105,18 @@ bool AuthSessionManager::add_or_replace_session(const api::user::TokenPayload& t if(it != _map.end()) { _map[tok.id()] = Session{tok}; + LOG(User, Warn, "KEYCODE: Replacing session {} ", tok.id()); return true; } else if(_map.size() < _nusers) { _map[tok.id()] = Session{tok}; + LOG(User, Warn, "KEYCODE: Creating session {} ", tok.id()); return true; } + if(msg) + *msg = "KEYCODES: Not Enough Logins available for session"; + LOG(User, Error, "KEYCODES: Not Enough Logins available for session {}", tok.id()); return false; } diff --git a/src/lib/utils/AuthToken.cpp b/src/lib/utils/AuthToken.cpp index e1292b595..2423638a8 100644 --- a/src/lib/utils/AuthToken.cpp +++ b/src/lib/utils/AuthToken.cpp @@ -51,6 +51,35 @@ AuthSessionManager& session_manager() return mgr; } +bool requires_auth_session(const admin::BTSUser& user) +{ + //if the user can trade in any account or see market data, it needs a session + if(user.has_user_data() && + user.user_data().has_account_permissions() && + user.user_data().account_permissions().account_permissions_size()) + { + for(const auto& i: user.user_data().account_permissions().account_permissions()) + { + if(i.second.has_trading_permission() && i.second.trading_permission().has_permissions()) + return true; + } + } + + if(user.has_user_data() && + user.user_data().has_exchange_user_data()) + { + if(user.user_data().exchange_user_data().has_cme_data()) + { + for(const auto& i: user.user_data().exchange_user_data().cme_data().md_permissions()) + { + if(i.enabled()) + return true; + } + } + } + return false; +} + AuthToken generate_token(const admin::BTSUser& user, std::string* msg) { api::user::TokenPayload payload; @@ -58,7 +87,9 @@ AuthToken generate_token(const admin::BTSUser& user, std::string* msg) payload.set_id(user.id()); if(user.name() == "bts") payload.set_wheel(true); - session_manager().add_or_replace_session(payload, msg); + + if(requires_auth_session(user)) + session_manager().add_or_replace_session(payload, msg); auto token = encrypt(payload.SerializeAsString()); bts_assert_d(token.size() < 255-8); AuthToken result; diff --git a/src/lib/utils/Dispatcher.cpp b/src/lib/utils/Dispatcher.cpp index 1d7ab64d3..c45d28c54 100755 --- a/src/lib/utils/Dispatcher.cpp +++ b/src/lib/utils/Dispatcher.cpp @@ -214,6 +214,20 @@ void Dispatcher::post(Action&& action, int64_t delay_ms) } } +void Dispatcher::post_copy(Action action, int64_t delay_ms) +{ + if (delay_ms > 0) + { + shared_ptr timer = make_shared(_me->service); + timer->expires_from_now(boost::posix_time::milliseconds(delay_ms)); + timer->async_wait([timer, action] (const boost::system::error_code& err) { if (!err) action(); }); + } + else + { + _me->service.post(action); + } +} + io_service* Dispatcher::io_service() { return &_me->service; } // diff --git a/src/lib/utils/Dispatcher.h b/src/lib/utils/Dispatcher.h index 2adc656af..997ab1220 100755 --- a/src/lib/utils/Dispatcher.h +++ b/src/lib/utils/Dispatcher.h @@ -88,6 +88,7 @@ public: void invoke(Action action); // Queue an action on the dispatcher thread and return immediately. void post(Action&& action, int64_t delay_ms=0); + void post_copy(Action action, int64_t delay_ms=0); boost::asio::io_service* io_service(); diff --git a/src/lib/utils/ValueWrapper.h b/src/lib/utils/ValueWrapper.h index 6c8b8692b..5df5e2ace 100644 --- a/src/lib/utils/ValueWrapper.h +++ b/src/lib/utils/ValueWrapper.h @@ -24,6 +24,7 @@ public: bool operator==(ValueWrapper const&v) const {return _v == v._v;} bool operator!=(ValueWrapper const&v) const {return _v != v._v;} bool operator<(ValueWrapper const&v) const {return _v < v._v;} + bool operator>(ValueWrapper const&v) const {return _v > v._v;} ValueT const& value() const {return _v;} ValueT & value() {return _v;} private: diff --git a/src/lib/utils/tests/AuthTokenTests.cpp b/src/lib/utils/tests/AuthTokenTests.cpp index 51de507f7..6bcbe52d8 100644 --- a/src/lib/utils/tests/AuthTokenTests.cpp +++ b/src/lib/utils/tests/AuthTokenTests.cpp @@ -5,19 +5,34 @@ namespace bts { namespace test { TEST_CASE("AuthToken", "[security]") { - api::admin::BTSUser user; - user.set_id(42); - user.set_name("user1"); + api::admin::BTSUser admin_user; + admin_user.set_id(42); + admin_user.set_name("user1"); + + api::admin::BTSUser trader_user; + trader_user.set_id(43); + trader_user.set_name("trader1"); + trader_user.mutable_user_data()->mutable_exchange_user_data()->mutable_cme_data()->add_md_permissions()->set_enabled(true); - SECTION("round trip") { - auto token = api::auth::generate_token(user); + SECTION("round trip admin") { + auto token = api::auth::generate_token(admin_user); + // We use 1 byte for api header size + REQUIRE(token.ByteSize() <= 240); + api::user::TokenPayload payload; + REQUIRE(!api::auth::decode_token(token, &payload)); + REQUIRE(payload.id() == admin_user.id()); + REQUIRE(payload.wheel() == false); + } + + SECTION("round trip trader") { + auto token = api::auth::generate_token(trader_user); // We use 1 byte for api header size REQUIRE(token.ByteSize() <= 240); api::user::TokenPayload payload; REQUIRE(api::auth::decode_token(token, &payload)); - REQUIRE(payload.id() == user.id()); + REQUIRE(payload.id() == trader_user.id()); REQUIRE(payload.wheel() == false); - } + } SECTION("empty token") { api::user::TokenPayload payload; @@ -28,11 +43,11 @@ TEST_CASE("AuthToken", "[security]") { } SECTION("bts is the big wheel") { - user.set_name("bts"); - auto token = api::auth::generate_token(user); + admin_user.set_name("bts"); + auto token = api::auth::generate_token(admin_user); api::user::TokenPayload payload; REQUIRE(api::auth::decode_token(token, &payload)); - REQUIRE(payload.id() == user.id()); + REQUIRE(payload.id() == admin_user.id()); REQUIRE(payload.wheel()); } } diff --git a/src/sdk/examples/order_plugin_example.cpp b/src/sdk/examples/order_plugin_example.cpp index 69a715c5c..4f62067e1 100644 --- a/src/sdk/examples/order_plugin_example.cpp +++ b/src/sdk/examples/order_plugin_example.cpp @@ -324,8 +324,14 @@ extern "C" void init_spark_plugin(bts::SparkServices &serv) { // Associate the descriptor with a builder function and register // that with the Spark server. - serv.register_dynamic_algo_order( + auto result = + serv.register_dynamic_algo_order( join_desc, []() { return std::make_unique(); }); + + if (result != bts::AlgoRegistrationResult::SUCCESS) { + // The join_desc has errors + // Check the error log for more details + } } } // namespace order_example diff --git a/src/server/sparkd/CMakeDeps.cmake b/src/server/sparkd/CMakeDeps.cmake index 7bc50d309..d570ae6a8 100644 --- a/src/server/sparkd/CMakeDeps.cmake +++ b/src/server/sparkd/CMakeDeps.cmake @@ -1 +1 @@ -set(sources algo/AlgoDefinitionServiceServer.cpp;cmds/AdminReport.cpp;cmds/AutoCert.cpp;cmds/Benchmark.cpp;cmds/CmdRegistry.cpp;cmds/ConfigCheck.cpp;cmds/Help.cpp;cmds/KeyCode.cpp;cmds/Md.cpp;cmds/PrintHedgeOrders.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/IMDPlatform.cpp;md/platforms/celoxica/CXListener.cpp;md/platforms/celoxica/CXMain.cpp;md/platforms/celoxica/tests/cx_tests.cpp;md/platforms/ice_onixs/ICEListener.cpp;md/platforms/ice_onixs/ICEMain.cpp;md/platforms/ice_onixs/tests/ice_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/AuthServiceServer.cpp;user/tests/admin_service_tests.cpp) \ No newline at end of file +set(sources algo/AlgoDefinitionServiceServer.cpp;cmds/AdminReport.cpp;cmds/AutoCert.cpp;cmds/Benchmark.cpp;cmds/CmdRegistry.cpp;cmds/ConfigCheck.cpp;cmds/Help.cpp;cmds/KeyCode.cpp;cmds/Md.cpp;cmds/PrintHedgeOrders.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/IMDPlatform.cpp;md/platforms/celoxica/CXListener.cpp;md/platforms/celoxica/CXMain.cpp;md/platforms/celoxica/tests/cx_tests.cpp;md/platforms/ice_onixs/ICEListener.cpp;md/platforms/ice_onixs/ICEMain.cpp;md/platforms/ice_onixs/tests/ice_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/IPositionObserver.cpp;position/MarginServiceCore.cpp;position/MarginServiceServer.cpp;position/PositionAggregator.cpp;position/PositionServicePNLHelper.cpp;position/PositionServiceServer.cpp;position/tests/PositionTests.cpp;support/ErrorReportService.cpp;support/ShellLatencyServer.cpp;user/AdminServiceServer.cpp;user/AuthServiceServer.cpp;user/tests/admin_service_tests.cpp) \ No newline at end of file diff --git a/src/server/sparkd/cmds/QuickTest.cpp b/src/server/sparkd/cmds/QuickTest.cpp index f6f86be5a..f9765553c 100644 --- a/src/server/sparkd/cmds/QuickTest.cpp +++ b/src/server/sparkd/cmds/QuickTest.cpp @@ -1,59 +1,13 @@ #include "QuickTest.h" #include "ICmd.h" #include "lib/tracing/PerfLog.h" -#include "lib/orderman/orders/algo/IAlgoServices.h" #include namespace bts { namespace cmd { -using namespace bts::orderman; - -struct Proxy : public IAlgoServices { - api::orderman::OrderDefinition def; - api::Security sec; - - api::orderman::OrderDefinition const &definition() const { return def; } - api::Security const &security() const { return sec; } - OrderKey order_key() const { return OrderKey(0); } - OrderKey parent_order_key() const { return OrderKey(0); } - api::Security const *lookup_security(SecurityKey) const { return nullptr; } - bool is_attaching() const { return false; } - ChildTemplate &make_child_template(api::orderman::OrderDefinition const &, - size_t) { - ChildTemplate *p = nullptr; - return *p; - } - Price price() const { return Price(0); } - Quantity quantity() const { return Quantity(0); } - Quantity total_fills() const { return Quantity(0); } - void subscribe_book(SecurityKey){}; - void unsubscribe_book(SecurityKey) {} - void subscribe_child_change(OrderKey) {} - void unsubscribe_child_change(OrderKey) {} - void subscribe_timer() {} - void unsubscribe_timer() {} - OrderKey launch_child(ChildTemplate &, Price, Quantity, bool) { return {}; } - OrderKey launch_child(api::orderman::OrderDefinition const &, Price, - Quantity, bool) { - return {}; - } - void update_child(OrderKey , api::orderman::Update const &) {} - void cancel_child(OrderKey) {} - void detach_child(OrderKey) {} - void detach_all_children() {} - void cancel(std::string const &) {} - void terminate_error(std::string const &) {} - void complete() {} - void update_fill_state(Quantity , Price ) {} - void set_ladder_visibility(bool) {} -}; int QuickTest::execute(std::vector const &) { - Proxy p; - auto t = &(p.make_child_template(api::orderman::OrderDefinition(),2)); - auto k = p.launch_child(*t,Price(1),Quantity(2),false); - std::cout << k.value() << "\n"; return 0; } diff --git a/src/server/sparkd/cmds/Run.cpp b/src/server/sparkd/cmds/Run.cpp index 6b3b7bbef..bb0bd9ffe 100644 --- a/src/server/sparkd/cmds/Run.cpp +++ b/src/server/sparkd/cmds/Run.cpp @@ -29,6 +29,7 @@ #include "server/sparkd/md/MarketDataAuxiliaryServiceServer.h" #include "server/sparkd/md/run_md.h" #include "server/sparkd/position/PositionServiceServer.h" +#include "server/sparkd/position/MarginServiceServer.h" #include "server/sparkd/support/ErrorReportService.h" #include "server/sparkd/support/ShellLatencyServer.h" #include "server/sparkd/user/AdminServiceServer.h" @@ -84,11 +85,10 @@ int Run::execute(std::vector const &) { md::auxiliary::MarketDataAuxiliaryServiceServer md_aux_server(sc); //orderman::AlgoServiceServer algo_server(sc); bts::api::orderman::algo::AlgoDefinitionServiceServer algo_definition_server(sc); + - - - - position::PositionServiceServer position_service(sc); + position::PositionServiceServer position_service(sc,mdclient); + position::MarginServiceServer margin_service(sc, mdclient); metrics::ShellLatencyServer shell_latency_service( sc, stats, utils::Dispatcher::background()); diff --git a/src/server/sparkd/cmds/Span.cpp b/src/server/sparkd/cmds/Span.cpp index 8499a465e..1bbe3c362 100644 --- a/src/server/sparkd/cmds/Span.cpp +++ b/src/server/sparkd/cmds/Span.cpp @@ -1,15 +1,61 @@ #include "Span.h" +#include "server/sparkd/position/PositionAggregator.h" #include "server/sparkd/position/MarginServiceCore.h" +#include "server/sparkd/position/Position.h" +#include "lib/orderman/orderman_utils/orderman_common.h" -namespace bts -{ -namespace cmd -{ +#include "lib/config/config.h" +#include "lib/api/public/ServiceContext.h" +#include "lib/api/services/AuthService.client.pb.h" +#include "lib/domain_model/Security.pb.h" +#include "lib/utils/helper_macros.h" +#include "lib/utils/Dispatcher.h" + +#include +#include +#include +#include +#include +#include +#include + +using namespace bts::position; +using namespace bts::api::position; + +namespace bts { +namespace cmd { int Span::execute(const std::vector&) { + srand(time(NULL)); + auto& disp = utils::Dispatcher::current(); + MarginServiceCore core(disp); + + auto acct = std::string("ACC A"); + Position p; + p.security_desc = "ESU9"; + p.account = acct; + p.position = Quantity(rand() % 100); + p.side = api::SIDE_BUY; + std::vector pos; + pos.push_back(p); + p.position = Quantity(rand() % 200); + p.side = api::SIDE_SELL; + pos.push_back(p); + + //core.set_verbose_debugging(true); + core.compute_margin(acct, pos, [](const api::position::MarginResult& m) + { + std::string str; + google::protobuf::TextFormat::PrintToString(m, &str); + + std::cout << std::endl << str << std::endl << std::endl; + }); + + std::cout << "Waiting for margin results, press any key to exit" << std::endl; + disp.run(); return 0; } @@ -18,7 +64,6 @@ std::string Span::help() return "\t test CME CORE Span integration \n"; } -} -} +}} diff --git a/src/server/sparkd/position/IMarginObserver.cpp b/src/server/sparkd/position/IMarginObserver.cpp deleted file mode 100644 index 6536da462..000000000 --- a/src/server/sparkd/position/IMarginObserver.cpp +++ /dev/null @@ -1,9 +0,0 @@ -#include "IMarginObserver.h" - -namespace bts -{ -namespace position -{ - IMarginObserver::~IMarginObserver() {} -} -} diff --git a/src/server/sparkd/position/IMarginObserver.h b/src/server/sparkd/position/IMarginObserver.h deleted file mode 100644 index de4a0e278..000000000 --- a/src/server/sparkd/position/IMarginObserver.h +++ /dev/null @@ -1,18 +0,0 @@ -#pragma once - -#include "lib/orderman/orderman_utils/orderman_common.h" - -namespace bts -{ -namespace position -{ -struct MarginResult; -class -IMarginObserver -{ -public: - virtual ~IMarginObserver(); - virtual void on_margin_change(const orderman::AccountName&, const MarginResult&) = 0; -}; -} -} diff --git a/src/server/sparkd/position/MarginResult.h b/src/server/sparkd/position/MarginResult.h deleted file mode 100644 index 53cd29332..000000000 --- a/src/server/sparkd/position/MarginResult.h +++ /dev/null @@ -1,25 +0,0 @@ -#pragma once - -#include "lib/orderman/orderman_utils/orderman_common.h" -#include "lib/domain_model/Security.pb.h" - -namespace bts -{ -namespace position -{ -struct MarginResult -{ - bool valid; - std::string settle_indicator; - std::string settle_qualifier; - std::string portfolio_id; - std::string as_of_time; - std::string calculation_id; - - double opt_value; - double nonopt_value; - double maint_margin; - double initial_margin; -}; -} -} diff --git a/src/server/sparkd/position/MarginServiceCore.cpp b/src/server/sparkd/position/MarginServiceCore.cpp index 3bc45e612..79faf20df 100644 --- a/src/server/sparkd/position/MarginServiceCore.cpp +++ b/src/server/sparkd/position/MarginServiceCore.cpp @@ -1,18 +1,16 @@ #include "MarginServiceCore.h" -#include "PositionServiceCore.h" #include "Position.h" -#include "MarginResult.h" -#include "IMarginObserver.h" - #include "lib/config/config.h" #include "lib/api/services/AuthService.pb.h" #include "lib/utils/bts_assert.h" #include "lib/utils/Uuid.h" +#include "lib/utils/Dispatcher.h" #include "lib/utils/bts_chrono.h" #include "lib/logging/logging.h" #include "lib/utils/helper_macros.h" #include "lib/utils/datetime.h" +#include "lib/config/protos/span.pb.h" #include "curl/curl.h" @@ -23,46 +21,42 @@ #include #include -#define CONSOLE_LOG(...) std::cout , __VA_ARGS__ , std::endl - -template std::ostream& operator,(std::ostream& out, const T& t) { out << t; return out; } -std::ostream& operator,(std::ostream& out, std::ostream&(*f)(std::ostream&)) { out << f; return out; } -#define MARGIN_LOG(subsystem,level,...) \ - bts::log::_thread_logger.logger->enqueue(log::log_ts(),bts::log::subsystem,level,__FILE__,__LINE__,__VA_ARGS__); \ -CONSOLE_LOG(__VA_ARGS__) +using bts::api::position::MarginResult; namespace bts { namespace position { -using MarginMap = boost::container::flat_map; +using MarginMap = boost::container::flat_map; + struct MarginServiceCore::PImpl { - securities::ISecurityDB& db; - PositionServiceCore position_service; - IMarginObserver* observer; - MarginMap last_results, last_valid_results; - + utils::Dispatcher& dispatcher; + MarginMap last_results, last_valid_results; std::string url_base, core_user, core_password, post_margin_path, get_margin_path; - size_t max_iterations; bool verbose; int throttle; - bts::datetime::PTime last_update; - PImpl(securities::ISecurityDB& d, IMarginObserver* obs) : - db(d), position_service(d), observer(obs), last_results(), last_valid_results(), - url_base("https://cmecorenr.cmegroup.com/MarginServiceApi/1.4/"), core_user("api_bts1"), core_password("BT5u53r!"), + PImpl(utils::Dispatcher& disp) : + dispatcher(disp), + last_results(), last_valid_results(), //api_bts_span BT5Margin! + url_base("https://cmecorenr.cmegroup.com/MarginServiceApi/1.4/"), core_user("api_bts1"), core_password("Summer123"), post_margin_path("margins/?complete=true"), get_margin_path("margins/"), - max_iterations(20), verbose(false), - throttle(30000), - last_update() + throttle(30000) { + if(config::get().login() != "") + core_user = config::get().login(); + if(config::get().password() != "") + core_password = config::get().password(); + if(config::get().url() != "") + url_base = config::get().url(); + auto res = curl_global_init(CURL_GLOBAL_DEFAULT); if(res != CURLE_OK) { - MARGIN_LOG(Gen,Error,"curl_global_init() failed: ", std::string(curl_easy_strerror(res))); + LOG(Gen,Error,"curl_global_init() failed: {}", std::string(curl_easy_strerror(res))); } } ~PImpl() @@ -151,7 +145,7 @@ struct MarginServiceCore::PImpl struct curl_slist* setup_url_and_headers(CURL* curl, const std::string& xpath) { std::string full_url = url_base + xpath; - MARGIN_LOG(Gen,Info,"CORE Margin URL: ",full_url); + LOG(Gen,Info,"CORE Margin URL: {}",full_url); curl_easy_setopt(curl, CURLOPT_URL, full_url.c_str()); //curl_easy_setopt(curl, CURLOPT_URL, "https://cmecorenr.cmegroup.com/MarginServiceApi/1.4/margins/"); curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, debug_callback); @@ -182,19 +176,19 @@ struct MarginServiceCore::PImpl curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)&reply); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, payload.c_str()); - MARGIN_LOG(Gen,Info,"CORE Margin Payload: ", payload); + LOG(Gen,Info,"CORE Margin Payload: {}", payload); auto res = curl_easy_perform(curl); if(res == CURLE_OK) { std::string resp(reply.data.begin(), reply.data.end()); - MARGIN_LOG(Gen,Info,"CORE Margin Post Response: ", resp); + LOG(Gen,Info,"CORE Margin Post Response: {}", resp); succ = true; } else { std::string error(curl_easy_strerror(res)); - MARGIN_LOG(Gen,Error,"POST curl_easy_perform() failed: ", error); - std::cout << fmt::format("CORE Margin CURL Error: ", error); + LOG(Gen,Error,"POST curl_easy_perform() failed: {}", error); + //std::cout << fmt::format("CORE Margin CURL Error: ", error); } curl_easy_cleanup(curl); if(header) @@ -202,7 +196,7 @@ struct MarginServiceCore::PImpl } else { - MARGIN_LOG(Gen,Error,"curl_easy_init() failed\n"); + LOG(Gen,Error,"curl_easy_init() failed\n"); } return succ; } @@ -221,12 +215,12 @@ struct MarginServiceCore::PImpl if(res == CURLE_OK) { std::string resp(reply.data.begin(), reply.data.end()); - MARGIN_LOG(Gen,Info,"CORE Margin GET Response: ", resp); + LOG(Gen,Info,"CORE Margin GET Response: {}", resp); succ = true; } else { - MARGIN_LOG(Gen,Error,"GET curl_easy_perform() failed: ", std::string(curl_easy_strerror(res))); + LOG(Gen,Error,"GET curl_easy_perform() failed: {}", std::string(curl_easy_strerror(res))); } curl_easy_cleanup(curl); if(header) @@ -234,7 +228,7 @@ struct MarginServiceCore::PImpl } else { - MARGIN_LOG(Gen,Error,"curl_easy_init() failed\n"); + LOG(Gen,Error,"curl_easy_init() failed\n"); } return succ; } @@ -284,7 +278,7 @@ struct MarginServiceCore::PImpl void parse_margin_calculation(const CurlMsg& msg, MarginResult& result) { - result.valid = false; + result.set_status(MarginResult::Error); std::string str(msg.data.begin(), msg.data.end()); pugi::xml_document doc; doc.load_string(str.c_str()); @@ -293,19 +287,19 @@ struct MarginServiceCore::PImpl child = child.child("margin"); if(child) { - result.settle_indicator = child.attribute("settleInd").as_string(); - result.settle_qualifier = child.attribute("settleQual").as_string(); - result.portfolio_id = child.attribute("portfolioId").as_string(); - result.as_of_time = child.attribute("asOfTime").as_string(); - result.calculation_id = child.attribute("id").as_string(); + result.set_settle_indicator(child.attribute("settleInd").as_string()); + result.set_settle_qualifier(child.attribute("settleQual").as_string()); + result.set_portfolio_id(child.attribute("portfolioId").as_string()); + result.set_as_of_time(child.attribute("asOfTime").as_string()); + result.set_calculation_id(child.attribute("id").as_string()); auto val = child.child("amounts"); - if(val && result.portfolio_id != "") + if(val && result.portfolio_id() != "") { - result.opt_value = child.attribute("optVal").as_double(); - result.nonopt_value = child.attribute("nonOptVal").as_double(); - result.maint_margin = child.attribute("maint").as_double(); - result.initial_margin = child.attribute("init").as_double(); - result.valid = true; + result.set_opt_value(val.attribute("optVal").as_double()); + result.set_nonopt_value(val.attribute("nonOptVal").as_double()); + result.set_maint_margin(val.attribute("maint").as_double()); + result.set_initial_margin(val.attribute("init").as_double()); + result.set_status(MarginResult::NewValid); } } } @@ -321,9 +315,8 @@ struct MarginServiceCore::PImpl }; bool compute_margin(const std::string& account_name, const std::string& portfolid_id, - const std::vector& pos, MarginResult& margin) - { - margin.valid = false; + const std::vector& pos, std::function cb) + { pugi::xml_document doc; pugi::xml_node trades_node; create_margin_request(portfolid_id, doc, trades_node); @@ -331,12 +324,9 @@ struct MarginServiceCore::PImpl size_t count = 0; for(const auto& i : pos) { - if(i.security && i.security->has_glbx()) - { - int p = i.position.value() * (i.side == api::SIDE_SELL ? -1 : 1); - request += fmt::format("BTS, {}, {}, {} {}", account_name, i.security->glbx().security_desc(), p, "\n"); - count++; - } + int p = i.position.value() * (i.side == api::SIDE_SELL ? -1 : 1); + request += fmt::format("BTS, {}, {}, {}, {}", account_name, i.security_desc, p, "\n"); + count++; } if(count > 0) { @@ -345,78 +335,88 @@ struct MarginServiceCore::PImpl xml_string_writer writer(xml); doc.save(writer); - size_t iters = 0; - while(iters++ < max_iterations) - { - CurlMsg reply; - sleep_for(5s); - post(post_margin_path, xml, reply); - std::string portid_resp, calcid_resp; - if(parse_margin_response(reply, portid_resp, calcid_resp)) - { - CurlMsg reply2; - sleep_for(2s); - if(get(get_margin_path + calcid_resp, reply2)) + CurlMsg reply; + post(post_margin_path, xml, reply); + std::string portid_resp, calcid_resp; + if(parse_margin_response(reply, portid_resp, calcid_resp)) + { + std::function do_web; + do_web = + [=]() { - parse_margin_calculation(reply2, margin); - return true; - } - } + MarginResult margin; + margin.set_status(MarginResult::Error); + margin.set_account(account_name); + CurlMsg reply2; bool done = false; + try + { + if(get(get_margin_path + calcid_resp, reply2)) + { + parse_margin_calculation(reply2, margin); + cb(margin); + done = true; + } + } + catch(const std::exception& ex) + { + LOG(Gen, Error, "Exception in parse_margin_calculation {}", std::string(ex.what())); + } + catch(...) + { + LOG(Gen, Error, "Unknown Exception in parse_margin_calculation"); + } + if(!done) + dispatcher.post_copy(do_web, 5000); + }; + dispatcher.post_copy(do_web, 5000); + return true; } } return false; } }; -MarginServiceCore::MarginServiceCore(securities::ISecurityDB& db, IMarginObserver* obs) - : me(new PImpl(db, obs)) +MarginServiceCore::MarginServiceCore(utils::Dispatcher& disp) : me(new PImpl(disp)) { } MarginServiceCore::~MarginServiceCore() {} -bool MarginServiceCore::pump(const orderman::AccountName& acct) -{ - MarginResult result; - return compute_margin(acct, result); -} -bool MarginServiceCore::compute_margin(const orderman::AccountName& acct, MarginResult& result) +void MarginServiceCore::compute_margin(const std::string& acct, const std::vector& pos, + std::function cb) { - auto now = bts::datetime::now(); - auto diff = bts::datetime::ptime_diff_in_ms(now, me->last_update); - if(me->last_update.is_not_a_date_time() || abs(diff) > me->throttle) + me->dispatcher.post([=] () { - query_margin_webapi(acct, result); - me->last_update = now; - return true; - } - else - { - get_last_valid_result(acct, result); - return false; //false, we did not get new data - } + query_margin_webapi(acct, pos, cb); + }, me->throttle); } -bool MarginServiceCore::query_margin_webapi(const orderman::AccountName& acct, MarginResult& margin) -{ - std::vector pos; - me->position_service.query_all_positions(acct, pos); - utils::Uuid guid; - bool succ = me->compute_margin(orderman::to_str(acct), guid.to_text_string(), pos, margin); - if(me->observer) - me->observer->on_margin_change(acct, margin); - me->last_results.insert_or_assign(acct, margin); - if(succ && margin.valid) - me->last_valid_results.insert_or_assign(acct, margin); - return succ; -} - -bool MarginServiceCore::query_margin_webapi(const std::vector& pos, MarginResult& margin) +bool MarginServiceCore::query_margin_webapi(const std::string& acct, const std::vector& pos, + std::function cb) { utils::Uuid guid; - std::string str = guid.to_text_string(); - return me->compute_margin(str.substr(0, 3), str, pos, margin); + try + { + return me->compute_margin(acct, guid.to_text_string(), pos, + [=] (const MarginResult& m) + { + me->last_results.insert_or_assign(acct, m); + if(m.status() == MarginResult::NewValid) + me->last_valid_results.insert_or_assign(acct, m); + cb(m); + }); + } + catch(const std::exception& ex) + { + LOG(Gen, Error, "Exception in query_margin_webapi {}", std::string(ex.what())); + return false; + } + catch(...) + { + LOG(Gen, Error, "Unknown Exception in query_margin_webapi"); + return false; + } } void MarginServiceCore::set_verbose_debugging(bool verb) @@ -424,7 +424,7 @@ void MarginServiceCore::set_verbose_debugging(bool verb) me->verbose = verb; } -bool MarginServiceCore::get_last_valid_result(const orderman::AccountName& acct, MarginResult& result) +bool MarginServiceCore::get_last_valid_result(const std::string& acct, MarginResult& result) { auto it = me->last_valid_results.find(acct); if(it != me->last_valid_results.end()) @@ -435,7 +435,7 @@ bool MarginServiceCore::get_last_valid_result(const orderman::AccountName& acct, return false; } -bool MarginServiceCore::get_last_result(const orderman::AccountName& acct, MarginResult& result) +bool MarginServiceCore::get_last_result(const std::string& acct, MarginResult& result) { auto it = me->last_results.find(acct); if(it != me->last_results.end()) diff --git a/src/server/sparkd/position/MarginServiceCore.h b/src/server/sparkd/position/MarginServiceCore.h index 51ab6321e..c4b7d7f8e 100644 --- a/src/server/sparkd/position/MarginServiceCore.h +++ b/src/server/sparkd/position/MarginServiceCore.h @@ -1,6 +1,7 @@ #pragma once #include "lib/orderman/orderman_utils/orderman_common.h" +#include "lib/api/services/MarginService.pb.h" #include "server/sparkd/position/Position.h" #include "lib/domain_model/Security.pb.h" @@ -8,33 +9,33 @@ namespace bts { +namespace utils { class Dispatcher; } namespace securities { class ISecurityDB; } namespace position { -struct MarginResult; class IMarginObserver; + class MarginServiceCore { BTS_NO_COPY(MarginServiceCore); public: - MarginServiceCore(securities::ISecurityDB&, IMarginObserver* obs = nullptr); + + MarginServiceCore(utils::Dispatcher&); ~MarginServiceCore(); - - //Call as much as you want; it will update IMarginObserver at most once every 30s - bool pump(const orderman::AccountName&); - //Call as much as you want; it will query the webapi at most once every 30s - bool compute_margin(const orderman::AccountName&, MarginResult&); + void compute_margin(const std::string&, const std::vector&, + std::function); + void set_verbose_debugging(bool); + bool get_last_valid_result(const std::string&, api::position::MarginResult&); + bool get_last_result(const std::string&, api::position::MarginResult&); + private: friend class PositionTests; - bool get_last_valid_result(const orderman::AccountName&, MarginResult&); - bool get_last_result(const orderman::AccountName&, MarginResult&); - //This is a blocking call to a WebAPI. Very Slow. - bool query_margin_webapi(const orderman::AccountName&, MarginResult&); - //This is a blocking call to a WebAPI. Very Slow. - bool query_margin_webapi(const std::vector&, MarginResult&); - void set_verbose_debugging(bool); - + + //This is a blocking call to a WebAPI. Very Slow. + bool query_margin_webapi(const std::string&, const std::vector&, + std::function); + private: struct PImpl; std::unique_ptr me; diff --git a/src/server/sparkd/position/MarginServiceServer.cpp b/src/server/sparkd/position/MarginServiceServer.cpp new file mode 100644 index 000000000..723c95e7b --- /dev/null +++ b/src/server/sparkd/position/MarginServiceServer.cpp @@ -0,0 +1,312 @@ +#include "MarginServiceServer.h" +#include "MarginServiceCore.h" +#include "PositionAggregator.h" +#include "Position.h" + +#include "lib/api/service_helpers/AdminServiceUtils.h" +#include "lib/orderman/orderman_utils/orderman_utils.h" +#include "lib/persistence/SqliteStore.h" +#include "lib/config/config.h" +#include "lib/config/protos/span.pb.h" +#include "lib/domain_model/Security.h" +#include "lib/logging/logging.h" +#include "lib/utils/bts_assert.h" +#include "lib/utils/TimeStamp.h" +#include "lib/utils/Dispatcher.h" +#include "lib/os/os.h" + +#include +#include + +using namespace bts::api::orderman; +using namespace bts::api::position; +using namespace bts::admin; +using namespace std; +using bts::api::position::MarginResult; + +namespace bts { +namespace position { + +MarginServiceServer::MarginServiceServer(bts::api::ServiceContext& context, md::MDClient& mdclient) + : bts::api::position::MarginServiceBase (context), + _admin_service(context.locate()), + _publisher(make_margin_data_stateful_publisher()), + _report_publisher(make_excess_liquidity_report_stateful_publisher()), + _dispatcher(utils::Dispatcher::get_named("MarginServiceServer")), + _timer(_dispatcher, 30000, [=]() { publish_all();} ), + _position(new PositionAggregator(context.locate(), mdclient)), + _margin(new MarginServiceCore(_dispatcher)), + //state below + _lock(), + _last_update(), + _admin_data(), + _last_report(), + _first_time(true) +{ + if(!config::get().enabled()) + return; + + LOG(Gen, Info, "MarginServiceServer: service started"); + + _admin_service.subscribe_admin_data() | + rxcpp::operators::subscribe( + [this](auto const &update) + { + std::scoped_lock l(_lock); + bts_assert(update.all().size() == 1); + _admin_data = *update.all()[0]; + + if(_first_time) + { + auto accts = AdminServiceUtils::accounts(_admin_data, false); + for(const auto& i : accts) + { + incremental_update_report(i->name(), i->id()); + } + _first_time = false; + } + }); + + _timer.start(); +} + +void MarginServiceServer::publish_all() +{ + if(config::get().enabled()) + { + std::scoped_lock l(_lock); + auto accts = AdminServiceUtils::accounts(_admin_data, false); + for(const auto& i : accts) + { + datetime::LLDateTime since = 0; + std::string acct = i->name(); + auto it = _last_update.find(acct); + if(it != _last_update.end()) + since = it->second; + std::vector pos; AccountPNL pnl; + if(_position->query_positions(acct, pos, pnl, &since) && pos.size()) + { + LOG(Gen, Info, "MarginServiceServer: requesting margin for {} ", acct); + _margin->compute_margin(acct, pos, [this, since, acct, pnl](const MarginResult& m) + { + std::scoped_lock l(_lock); + publish_margin(m); + publish_excess_liquidity_report(m, pnl); + _last_update[acct] = since; + }); + } + } + } +} + +MarginServiceServer::~MarginServiceServer() {} + +void MarginServiceServer::get_margin(const api::position::MarginRequest& request, + const api::Response& reply) +{ + if(!config::get().enabled()) + { + MarginResult res; + res.set_account(request.account()); + reply(res); + } + else + { + std::vector pos; AccountPNL pnl; + _position->query_positions(request.account(), pos, pnl); + _margin->compute_margin(request.account(), pos, [=](const MarginResult& m) + { + reply(m); + }); + } +} + +void MarginServiceServer::get_excess_liquidity_report(const api::position::MarginRequest& request, + const api::Response& reply) +{ + std::scoped_lock l(_lock); + ExcessLiquidityReport rep; + rep.set_account(request.account()); + auto it = _last_report.find(request.account()); + if(it != _last_report.end()) + rep.CopyFrom(it->second); + reply(rep); +} + + + +void MarginServiceServer::publish_margin(const api::position::MarginResult& result) +{ + std::scoped_lock l(_lock); + _publisher.add_or_update(result); + _publisher.flush(); +} + +void MarginServiceServer::publish_excess_liquidity_report(const api::position::MarginResult& result, const api::position::AccountPNL& pnl) +{ + std::scoped_lock l(_lock); + ExcessLiquidityReport report; + report.set_account(result.account()); + report.set_account_id(pnl.account_id()); + report.mutable_account_pnl()->CopyFrom(pnl); + report.mutable_margin_result()->CopyFrom(result); + auto acct = AdminServiceUtils::find_account(_admin_data, result.account(), false); + if(acct && acct->has_margin_limit()) + { + report.set_leverage_factor( acct->margin_limit().leverage_factor_percentage() ); + report.set_cash_balance(acct->margin_limit().cash_balance()); + report.set_should_enforce_margin(acct->margin_limit().enforce_margin() && + config::get().enabled()); + } + + double netliq = report.leverage_factor() * report.cash_balance(); + if(!bts::os::is_nan(result.maint_margin())) + netliq -= result.maint_margin(); + if(!bts::os::is_nan(pnl.day_pnl())) + netliq += pnl.day_pnl(); + report.set_excess_liquidity(netliq); + _last_report[result.account()] = report; + _report_publisher.add_or_update(report); + _report_publisher.flush(); +} + +//so the admin ui knows what to do +void MarginServiceServer::incremental_update_report(const std::string& account, uint32_t account_id) +{ + std::scoped_lock l(_lock); + + MarginResult margin; + if(!_margin->get_last_valid_result(account, margin)) + { + margin.Clear(); + margin.set_account(account); + } + AccountPNL pnl; + if(!_position->query_pnl(account, pnl)) + { + pnl.Clear(); + pnl.set_account_name(account); + pnl.set_account_id(account_id); + } + publish_excess_liquidity_report(margin, pnl); + _last_update[account] = 0; +} + + +void MarginServiceServer::set_cash_factor(const ::bts::api::position::CashFactorUpdate& request, + const api::Response<::bts::api::ResultMessage>& reply) +{ + if(!config::get().enabled()) + { + api::ResultMessage resp; resp.set_success(true); + reply(resp); + return; + } + + std::scoped_lock l(_lock); + auto acct = AdminServiceUtils::find_account(_admin_data, request.account(), false); + if(!acct) + { + auto msg = "Account " + request.account() +"not found"; + api::ResultMessage resp; + resp.set_success(false); + resp.set_error_message(msg); + reply(resp); + } + else + { + acct->mutable_margin_limit()->set_leverage_factor_percentage(request.leverage_factor()); + AccountUpdateAPIArgument update; + update.mutable_complete_data()->CopyFrom(*acct); + + _admin_service.update_account(update, + [this, request, reply, update](const api::Result& admin_result) + { + api::ResultMessage resp; + resp.set_success(admin_result.success()); + resp.set_error_message(admin_result.error_message()); + reply(resp); + incremental_update_report(request.account(), update.complete_data().id()); + }); + } +} + +void MarginServiceServer::set_cash_balance(const ::bts::api::position::CashBalanceUpdate& request, + const api::Response<::bts::api::ResultMessage>& reply) +{ + if(!config::get().enabled()) + { + api::ResultMessage resp; resp.set_success(true); + reply(resp); + return; + } + + std::scoped_lock l(_lock); + auto acct = AdminServiceUtils::find_account(_admin_data, request.account(), false); + if(!acct) + { + auto msg = "Account " + request.account() +"not found"; + api::ResultMessage resp; + resp.set_success(false); + resp.set_error_message(msg); + reply(resp); + } + else + { + acct->mutable_margin_limit()->set_cash_balance(request.cash_balance()); + AccountUpdateAPIArgument update; + update.mutable_complete_data()->CopyFrom(*acct); + + _admin_service.update_account(update, + [this, request, reply, update](const api::Result& admin_result) + { + api::ResultMessage resp; + resp.set_success(admin_result.success()); + resp.set_error_message(admin_result.error_message()); + reply(resp); + incremental_update_report(request.account(), update.complete_data().id()); + }); + } +} + +void MarginServiceServer::set_should_enforce_margin(const ::bts::api::position::EnforceUpdate& request, + const api::Response<::bts::api::ResultMessage>& reply) +{ + if(!config::get().enabled()) + { + api::ResultMessage resp; resp.set_success(true); + reply(resp); + return; + } + + std::scoped_lock l(_lock); + auto acct = AdminServiceUtils::find_account(_admin_data, request.account(), false); + if(!acct) + { + auto msg = "Account " + request.account() +"not found"; + api::ResultMessage resp; + resp.set_success(false); + resp.set_error_message(msg); + reply(resp); + } + else + { + acct->mutable_margin_limit()->set_enforce_margin(request.should_enforce_margin()); + AccountUpdateAPIArgument update; + update.mutable_complete_data()->CopyFrom(*acct); + + _admin_service.update_account(update, + [this, request, reply, update](const api::Result& admin_result) + { + api::ResultMessage resp; + resp.set_success(admin_result.success()); + resp.set_error_message(admin_result.error_message()); + reply(resp); + incremental_update_report(request.account(), update.complete_data().id()); + }); + } +} + + +}} + diff --git a/src/server/sparkd/position/MarginServiceServer.h b/src/server/sparkd/position/MarginServiceServer.h new file mode 100644 index 000000000..aa0c5cd01 --- /dev/null +++ b/src/server/sparkd/position/MarginServiceServer.h @@ -0,0 +1,72 @@ +#pragma once + +#include "lib/orderman/orderman_utils/orderman_common.h" +#include "lib/md/SecurityOracle.h" +#include "lib/persistence/IStore.h" +#include "lib/api/public/ServiceContext.h" +#include "lib/api/services/MarginService.server.pb.h" +#include "lib/api/services/AdminService.pb.h" +#include "lib/api/services/AdminService.client.pb.h" +#include "lib/api/services/PositionService.pb.h" +#include "lib/api/services/PositionService.client.pb.h" +#include "lib/utils/bts_no_copy.h" +#include "lib/utils/datetime.h" +#include +#include +#include + +namespace bts { +namespace utils { class Dispatcher; } +namespace position { + +struct Position; +class PositionAggregator; +class MarginServiceCore; + +class MarginServiceServer : public api::position::MarginServiceBase +{ +public: + MarginServiceServer(bts::api::ServiceContext&, md::MDClient&); + ~MarginServiceServer(); + + void get_margin(const api::position::MarginRequest& request, + const api::Response& reply) override; + + void get_excess_liquidity_report(const api::position::MarginRequest& request, + const api::Response& reply) override; + + void set_cash_factor(const ::bts::api::position::CashFactorUpdate& request, + const api::Response<::bts::api::ResultMessage>& reply) override; + + void set_cash_balance(const ::bts::api::position::CashBalanceUpdate& request, + const api::Response<::bts::api::ResultMessage>& reply) override; + + void set_should_enforce_margin(const ::bts::api::position::EnforceUpdate& request, + const api::Response<::bts::api::ResultMessage>& reply) override; + + +private: + void publish_margin(const api::position::MarginResult&); + void publish_excess_liquidity_report(const api::position::MarginResult&, const api::position::AccountPNL&); + void publish_all(); + void incremental_update_report(const std::string& account, uint32_t account_id); + +private: + api::admin::AdminService _admin_service; + MarginServiceBase::Margin_dataStatefulPublisher _publisher; + MarginServiceBase::Excess_liquidity_reportStatefulPublisher _report_publisher; + utils::Dispatcher& _dispatcher; + utils::DispatcherTimer _timer; + std::unique_ptr _position; + std::unique_ptr _margin; + +private: + //state + std::recursive_mutex _lock; + std::map _last_update; + bts::api::admin::AdminData _admin_data; + std::map _last_report; + bool _first_time; + +}; +}} diff --git a/src/server/sparkd/position/Position.h b/src/server/sparkd/position/Position.h index 715f27724..721da10c3 100644 --- a/src/server/sparkd/position/Position.h +++ b/src/server/sparkd/position/Position.h @@ -1,19 +1,14 @@ #pragma once #include "lib/orderman/orderman_utils/orderman_common.h" -#include "lib/domain_model/Security.pb.h" -namespace bts -{ -namespace position -{ +namespace bts { +namespace position { struct Position { - orderman::AccountName account; - const api::Security* security; + std::string account; + std::string security_desc; Quantity position; api::Side side; - Price avg_price; }; -} -} +}} diff --git a/src/server/sparkd/position/PositionAggregator.cpp b/src/server/sparkd/position/PositionAggregator.cpp new file mode 100644 index 000000000..af18e4417 --- /dev/null +++ b/src/server/sparkd/position/PositionAggregator.cpp @@ -0,0 +1,132 @@ + +#include "PositionAggregator.h" +#include "Position.h" + +#include "lib/domain_model/Security.h" +#include "lib/logging/logging.h" +#include "lib/utils/bts_assert.h" +#include "lib/orderman/orderman_utils/orderman_utils.h" +#include "lib/utils/TimeStamp.h" +#include "lib/utils/Dispatcher.h" + +#include +#include + +using namespace bts::api::orderman; +using namespace bts::api::position; +using namespace std; + +namespace bts { +namespace position { + +PositionAggregator::PositionAggregator(api::position::PositionService&& pos, md::MDClient& mdclient) + : _position_service(std::move(pos)), + _position_map(), + _pnl_map(), + _accounts(), + _oracle(mdclient), + _lock() +{ + _position_service.subscribe_position_data() | + rxcpp::operators::subscribe( + [this](auto const& update) + { + std::scoped_lock l(_lock); + for (const auto& o : update.removed()) + { + const auto& it = _position_map.find(o->record_id()); + if(it != _position_map.end()) + _position_map.erase(it); + } + for (const auto& ch : update.changed()) + { + const auto& o = ch.current(); + _position_map[o->record_id()] = *o; + _accounts[o->account()] = bts::datetime::ll_now(); + } + for (const auto& o : update.added()) + { + _position_map[o->record_id()] = *o; + _accounts[o->account()] = bts::datetime::ll_now(); + } + }); + _position_service.subscribe_pnl_account_data() | + rxcpp::operators::subscribe( + [this](auto const& update) + { + std::scoped_lock l(_lock); + for (const auto& o : update.removed()) + { + const auto& it = _pnl_map.find(o->account_name()); + if(it != _pnl_map.end()) + _pnl_map.erase(it); + } + for (const auto& ch : update.changed()) + { + const auto& o = ch.current(); + _pnl_map[o->account_name()] = *o; + } + for (const auto& o : update.added()) + { + _pnl_map[o->account_name()] = *o; + } + }); + +} + +PositionAggregator::~PositionAggregator() {} + + +void eraseSubStr(std::string & mainStr, const std::string & toErase) +{ + size_t pos = mainStr.find(toErase); + if (pos != std::string::npos) + mainStr.erase(pos, toErase.length()); +} + +bool PositionAggregator::query_pnl(const std::string& account, api::position::AccountPNL& pnl) +{ + auto pit = _pnl_map.find(account); + if(pit != _pnl_map.end()) + { + pnl = pit->second; + return true; + } + return false; +} + +bool PositionAggregator::query_positions(const std::string& account, std::vector& result, + api::position::AccountPNL& pnl, + datetime::LLDateTime* since) +{ + auto last_update = _accounts[account]; + if(!since || *since == 0 || bts::datetime::ll_diff_in_us(*since, last_update) > 0) + { + std::scoped_lock l(_lock); + for(auto& entry : _position_map) + { + const PositionUnit& unit = entry.second; + if(unit.account() != account) + continue; + auto sec = _oracle.get_security( SecurityKey(unit.security_id()) ); + if(sec && sec->has_glbx() && sec->has_future()) + { + Position pos; + pos.account = account; + pos.security_desc = securities::descriptor(*sec); + eraseSubStr(pos.security_desc, "SIM:"); + int32_t totalpos = unit.sod_long_pos() + unit.day_long_pos() - unit.sod_short_pos() - unit.day_short_pos(); + pos.position = Quantity(abs(totalpos)); + pos.side = (totalpos < 0.0 ? api::SIDE_SELL : api::SIDE_BUY); + result.push_back(pos); + } + } + if(since) + *since = last_update; + return query_pnl(account, pnl); + } + return false; +} + +}} + diff --git a/src/server/sparkd/position/PositionAggregator.h b/src/server/sparkd/position/PositionAggregator.h new file mode 100644 index 000000000..3553b17d5 --- /dev/null +++ b/src/server/sparkd/position/PositionAggregator.h @@ -0,0 +1,37 @@ +#pragma once + +#include "lib/orderman/orderman_utils/orderman_common.h" +#include "lib/md/SecurityOracle.h" +#include "lib/api/services/PositionService.pb.h" +#include "lib/api/services/PositionService.client.pb.h" +#include "lib/utils/bts_no_copy.h" +#include "lib/utils/datetime.h" + +#include +#include +#include + +namespace bts { +namespace position { +struct Position; + +class PositionAggregator +{ +public: + PositionAggregator(api::position::PositionService&&, md::MDClient&); + ~PositionAggregator(); + + bool query_positions(const std::string& account, std::vector& result, api::position::AccountPNL& pnl, + datetime::LLDateTime* since = nullptr); + + bool query_pnl(const std::string& account, api::position::AccountPNL& pnl); + +private: + api::position::PositionService _position_service; + std::map _position_map; + std::map _pnl_map; + std::map _accounts; + md::SecurityOracle _oracle; + std::recursive_mutex _lock; +}; +}} diff --git a/src/server/sparkd/position/PositionServiceCore.cpp b/src/server/sparkd/position/PositionServiceCore.cpp deleted file mode 100644 index 8fca5da02..000000000 --- a/src/server/sparkd/position/PositionServiceCore.cpp +++ /dev/null @@ -1,141 +0,0 @@ -#include "PositionServiceCore.h" - -#include "Position.h" -#include "IPositionObserver.h" -#include "lib/orderman/orderman_utils/orderman_common.h" -#include "lib/orderman/securities/ISecurityDB.h" - -#include "lib/utils/bts_assert.h" -#include "lib/logging/logging.h" - -#include -#include - -namespace bts -{ -namespace position -{ - -template int sgn(T val) { - return (T(0) < val) - (val < T(0)); -} - -class BookEntry -{ -public: - orderman::AccountName account; - SecurityKey security_key; - double position; - double avg_price; -public: - BookEntry(const orderman::AccountName& acc, SecurityKey k) : - account(acc), security_key(k), position(0.0), avg_price(0.0) - { - } - void add_fill(double last_size, double last_price, api::Side side) - { - if(side == api::SIDE_SELL) - last_size = -last_size; - if(position == 0.0 || sgn(position) == sgn(last_size)) - { - double prev = position * avg_price; - position += last_size; - avg_price = prev + (last_size*last_price) / position; - } - else - position += last_size; - } -}; - -struct PositionServiceCore::PImpl -{ - typedef std::pair KeyType; - boost::container::flat_map> book; - IPositionObserver* observer; - PImpl(IPositionObserver* obs) : book(), observer(obs) - { - } - - BookEntry& find_entry(const orderman::AccountName& account, SecurityKey skey) - { - KeyType key(account, skey); - auto& entry = book[key]; - if(!entry) - entry = std::make_shared(account, skey); - return *entry; - } - - void book_raw_fill(const orderman::AccountName& account, SecurityKey sec, - double last_size, double last_price, api::Side side) - { - auto& entry = find_entry(account, sec); - entry.add_fill(last_size, last_price, side); - if(observer) - observer->on_position_change(account, sec, entry.position, entry.avg_price); - } -}; - -PositionServiceCore::PositionServiceCore(securities::ISecurityDB& db, IPositionObserver* obs) - : me(new PImpl(obs)), _db(db) -{ -} - -PositionServiceCore::~PositionServiceCore() {} - -bool PositionServiceCore::query_position(const orderman::AccountName& account, SecurityKey seckey, Position& result) -{ - auto& entry = me->find_entry(account, seckey); - auto sec = _db.resolve(entry.security_key); - if(sec) - { - result.account = entry.account; - result.avg_price = Price(entry.avg_price); - result.position = Quantity(abs(entry.position)); - result.side = (entry.position < 0.0 ? api::SIDE_SELL : api::SIDE_BUY); - result.security = sec; - return true; - } - else - return false; -} - -void PositionServiceCore::query_all_positions(const orderman::AccountName& account, std::vector& result) -{ - result.reserve(me->book.size()); - for(auto& entry : me->book) - { - if(entry.first.first != account) - continue; - auto sec = _db.resolve(entry.second->security_key); - if(sec) - { - Position pos; - pos.account = entry.second->account; - pos.avg_price = Price(entry.second->avg_price); - pos.position = Quantity(abs(entry.second->position)); - pos.side = (entry.second->position < 0.0 ? api::SIDE_SELL : api::SIDE_BUY); - pos.security = sec; - result.push_back(pos); - } - } -} - -void PositionServiceCore::consume_raw_fill(const std::string& account, uint32_t security_key, - double last_size, double last_price, api::Side side) -{ - orderman::AccountName acc = orderman::to(account); - SecurityKey skey(security_key); - auto sec = _db.resolve(skey); - if(sec->Contract_case() == api::Security::ContractCase::kSpread) - { - //TODO this is wrong; need the leg fills - for(int i = 0; i < sec->spread().legs_size(); i++) - me->book_raw_fill(acc, SecurityKey(sec->spread().legs(i).security().id()), - 0.0, 0.0, side); - } - else - me->book_raw_fill(acc, skey, last_size, last_price, side); -} - -} -} diff --git a/src/server/sparkd/position/PositionServiceCore.h b/src/server/sparkd/position/PositionServiceCore.h deleted file mode 100644 index b6e2482f7..000000000 --- a/src/server/sparkd/position/PositionServiceCore.h +++ /dev/null @@ -1,36 +0,0 @@ -#pragma once - -#include "lib/orderman/orderman_utils/orderman_common.h" -#include "lib/utils/bts_no_copy.h" -#include "lib/domain_model/Security.pb.h" - -#include - -namespace bts -{ -namespace securities { class ISecurityDB; } -namespace position -{ -struct Position; -class IPositionObserver; -class PositionServiceCore -{ - BTS_NO_COPY(PositionServiceCore); -public: - PositionServiceCore(securities::ISecurityDB&, IPositionObserver* obs = nullptr); - ~PositionServiceCore(); - - bool query_position(const orderman::AccountName&, SecurityKey, Position&); - void query_all_positions(const orderman::AccountName&, std::vector&); - -private: - void consume_raw_fill(const std::string& account, uint32_t security_key, - double last_size, double last_price, api::Side side); - -private: - struct PImpl; - std::unique_ptr me; - securities::ISecurityDB& _db; -}; -} -} diff --git a/src/server/sparkd/position/PositionServicePNLHelper.cpp b/src/server/sparkd/position/PositionServicePNLHelper.cpp new file mode 100644 index 000000000..e7687c87b --- /dev/null +++ b/src/server/sparkd/position/PositionServicePNLHelper.cpp @@ -0,0 +1,263 @@ +#include "lib/api/services/OrderService.client.pb.h" +#include "lib/logging/logging.h" +#include "lib/md/MDClient.h" +#include "lib/os/os.h" +#include "PositionServicePNLHelper.h" +#include "lib/config/protos/cme_reference_data.pb.h" +#include "lib/config/config.h" + +#include "lib/utils/LockBox.h" +#include +#include + +#include +#include +#include +#include +#include + +namespace bts { +namespace position{ + +using namespace bts::api::position; +using namespace bts::api::orderman; +using namespace bts::md; + + +class HandleIndex { +public: + enum PNLType{ + UNRESOLVED = 0, + SPREAD, + OUTRIGHT + }; + bts::md::MDClient& _client; + int _sub; + std::optional _consumer; + +public: + utils::LockBox> index; + + HandleIndex(md::MDClient& client): + _client(client), + _sub(0), + _consumer(std::nullopt), + index() { + _sub = _client.on_consumer().subscribe([=](const MDConsumer& c) { on_consumer(c); }); + on_consumer(_client.current_consumer()); + } + ~HandleIndex() { _client.on_consumer().unsubscribe(_sub); } + + MDConsumerHandle _get_handle(SecurityKey key) const{ + auto handle = index.lock([id=key](const auto& index) { + auto iter = index.find(id); + return iter != index.end() ? iter->second : MDConsumerHandle {}; + }); + return handle; + } + PNLType get_pnl_type (SecurityKey key) const{ + auto h = _get_handle(key); + if (h.is_initialized()){ + const auto& sec = h.get_normalized_security(); + if (sec.has_future() || sec.has_option()) + return PNLType::OUTRIGHT; + else + return PNLType::SPREAD; + } + else + return PNLType::UNRESOLVED; + } + std::string get_symbol (SecurityKey key) const{ + auto handle = _get_handle(key); + return handle.get_usersymbol().symbol(); + } + void get_md(SecurityKey key, + double& bid, double& ask, double& last, double& settle){ + auto handle = _get_handle(key); + bid = bts::os::nan(); + ask = bid; + last = bid; + settle = bid; + if(handle.is_initialized()){ + TimeStamp time; + PriceSize bid_ps,ask_ps; + handle.get_direct_book(time, 1, &bid_ps, &ask_ps); + bid = bid_ps.price(); + ask = ask_ps.price(); + PriceSize last_ps; + datetime::LLDateTime trade_time; + handle.get_last_trade(time, last_ps, trade_time, nullptr); + last = last_ps.price(); + settle = handle.settlement_price().price(); + } + } + +private: + void on_consumer(const MDConsumer& c) { + MDConsumer temp(c); + if (temp.all_heartbeats_ok()) { + auto existing = _consumer ? _consumer->id() : 0; + LOG(md,Info, "Updating MBOService MDConsumer. Old {} new {}", existing, temp.id()); + // clear the index + index.lock([](auto& index) { index.clear(); }); + // Swap to new consumer + if (_consumer) { + _consumer->swap(temp); + } + else { _consumer = temp; } + // Subscribe to on_security + auto id = _consumer->id(); + _consumer->on_security([this,id](const SecurityPtr&, MDConsumerHandle handle) { + if (_consumer->id() == id) { + index.lock([handle](auto& index) { + index[SecurityKey(handle.key())] = handle; + }); + } + }); + } + } +}; + +struct PositionServicePNLHelper::PImpl { + HandleIndex handle_index; + std::map symbol_cpp_map; + std::map sec_cpp_map; + PImpl(bts::md::MDClient& md_client) + : + handle_index(md_client), + symbol_cpp_map(), + sec_cpp_map() + { + const auto& cpp_map = config::get().cpp_entry(); + LOG(md,Info,"PositionServicePNLHelper::cons loaded {} cpps",cpp_map.size()); + for (const auto& p : cpp_map){ + symbol_cpp_map[std::string(p.first)]=p.second; + LOG(md,Info,"PositionServicePNLHelper loading cpp's Sym:{} cpp:{}", + p.first,p.second); + } + } +}; + + +void PositionServicePNLHelper::compute_pnl( + const std::vector& in_pos, + const std::map& account_map, + std::vector& out_pnl, + std::vector& out_account_pnl){ + out_account_pnl.clear(); + for (const PositionUnit* pu_ptr : in_pos) { + const PositionUnit& pu = *pu_ptr; + uint32_t sec_key = pu.security_id(); + double cpp = _get_cpp(sec_key); + double bid, ask, last, settle; + me->handle_index.get_md(SecurityKey(sec_key),bid,ask,last,settle); + PNLUnit pnlu; + pnlu.set_record_id(pu.record_id()); + pnlu.mutable_pos()->CopyFrom(pu); + pnlu.set_cash_value(cpp); + pnlu.set_bid_price(bid); + pnlu.set_ask_price(ask); + pnlu.set_last_price(last); + pnlu.set_settlement_price(settle); + out_pnl.push_back(pnlu); + } + _aggregate_account_pnl(out_pnl,account_map,out_account_pnl); +} + +void PositionServicePNLHelper::_aggregate_account_pnl( + const std::vector& in_pnl, + const std::map& account_map, + std::vector& out_account_pnl){ + out_account_pnl.clear(); + if (account_map.size() == 0) + return; + std::map acc_pnl; + for (const auto& pnlu : in_pnl){ + uint32_t acct_id = pnlu.pos().account_id(); + const std::string& acct_name = pnlu.pos().account(); + auto it=acc_pnl.find(acct_id); + if(it==acc_pnl.end()){ + AccountPNL new_acc; + new_acc.set_account_id(acct_id); + new_acc.set_account_name(acct_name); + new_acc.set_day_pnl(0.0); + acc_pnl[acct_id]=new_acc; + it=acc_pnl.find(acct_id); + } + AccountPNL& acc_pnl = it->second; + double pnl_dollars = _compute_day_pnl(pnlu); + acc_pnl.set_day_pnl(acc_pnl.day_pnl()+pnl_dollars); + } + // for accounts with no positions add 0 + for (const auto& p : account_map){ + const std::string& acct_name = p.first; + uint32_t acct_id = p.second; + if(acc_pnl.find(acct_id) == acc_pnl.end()){ + AccountPNL new_acc; + new_acc.set_account_id(acct_id); + new_acc.set_account_name(acct_name); + new_acc.set_day_pnl(0.0); + acc_pnl[acct_id]=new_acc; + } + } + for(auto& u : acc_pnl) + out_account_pnl.push_back(u.second); +} + +double PositionServicePNLHelper::_compute_day_pnl ( + const bts::api::position::PNLUnit& u){ + // long + double pnl; + auto pnl_type = me->handle_index.get_pnl_type( + SecurityKey(u.pos().security_id())); + switch(pnl_type){ + case HandleIndex::OUTRIGHT:{ + double md = (u.bid_price() + u.ask_price())*0.5; + md = bts::os::is_nan(md) ? u.bid_price() : md; + md = bts::os::is_nan(md) ? u.ask_price() : md; + md = bts::os::is_nan(md) ? u.settlement_price() : md; + + double long_pnl = std::abs(u.pos().day_long_pos()) * u.cash_value() * + (md - u.pos().day_long_avg_price()), + short_pnl = std::abs(u.pos().day_short_pos()) * u.cash_value() * + (u.pos().day_short_avg_price() - md); + pnl = long_pnl + short_pnl; + break; + } + case HandleIndex::SPREAD: + pnl = 0.0; + break; + case HandleIndex::UNRESOLVED: + pnl = os::nan(); + break; + } + return pnl; +} +double PositionServicePNLHelper::_get_cpp(uint32_t sec_key){ + auto it = me->sec_cpp_map.find(sec_key); + if(it == me->sec_cpp_map.end()){ + // try to find it and put the value there + std::string symbol = me->handle_index.get_symbol(SecurityKey(sec_key)); + auto cppit = me->symbol_cpp_map.find(symbol); + if(cppit == me->symbol_cpp_map.end()){ + LOG(md,Warn,"Could not find cpp for key:{} symbol:{}", + sec_key,symbol); + return bts::os::nan(); + } + else{ + double cpp = cppit->second; + me->sec_cpp_map[sec_key]=cpp; + return cpp; + } + } + else + return it->second; +} +PositionServicePNLHelper::PositionServicePNLHelper(md::MDClient& md_client) +: +me(new PImpl(md_client)){ +} +PositionServicePNLHelper::~PositionServicePNLHelper(){ +} + +}} \ No newline at end of file diff --git a/src/server/sparkd/position/PositionServicePNLHelper.h b/src/server/sparkd/position/PositionServicePNLHelper.h new file mode 100644 index 000000000..32190d471 --- /dev/null +++ b/src/server/sparkd/position/PositionServicePNLHelper.h @@ -0,0 +1,35 @@ +#pragma once + +#include "lib/api/services/PositionService.server.pb.h" +#include "lib/utils/bts_no_copy.h" +#include + +namespace bts { +namespace md { class MDClient;} + +namespace position { + +class MDClient; + +class PositionServicePNLHelper { +private: + struct PImpl; + std::unique_ptr me; +private: + double _compute_day_pnl (const bts::api::position::PNLUnit&); + double _get_cpp (uint32_t); + // aggregates [sec, account]->pnl tp [account]->pnl + void _aggregate_account_pnl( + const std::vector& in_pnl, + const std::map& account_map, + std::vector& out_account_pnl); + public: + PositionServicePNLHelper(md::MDClient& client); + ~PositionServicePNLHelper(); + void compute_pnl(const std::vector& in_pos, + const std::map& account_map, + std::vector& out_pnl, + std::vector& out_account_pnl); +}; +} // namespace position +} // namespace bts diff --git a/src/server/sparkd/position/PositionServiceServer.cpp b/src/server/sparkd/position/PositionServiceServer.cpp index 116938572..7c3fdf022 100644 --- a/src/server/sparkd/position/PositionServiceServer.cpp +++ b/src/server/sparkd/position/PositionServiceServer.cpp @@ -1,11 +1,14 @@ #include "PositionServiceServer.h" #include "lib/api/services/OrderService.pb.h" #include "lib/api/services/OrderService.client.pb.h" +#include "lib/api/services/AdminService.pb.h" +#include "lib/api/services/AdminService.client.pb.h" #include "lib/api/public/ServiceContext.h" #include "lib/logging/logging.h" #include "lib/utils/PBUtils.h" #include "lib/persistence/SqliteStore.h" #include "lib/utils/Puid.h" +#include "PositionServicePNLHelper.h" #include #include @@ -19,56 +22,89 @@ namespace position{ using namespace bts::api::position; using namespace bts::api::orderman; +using namespace bts::md; static auto const manual_fills_ns = "manual_fills"; struct PositionServiceServer::PImpl { + bts::api::ServiceContext& context; PositionServiceBase::Position_dataStatefulPublisher publisher; + PositionServiceBase::Pnl_dataStatefulPublisher pnl_publisher; + PositionServiceBase::Pnl_account_dataStatefulPublisher pnl_account_publisher; bts::api::orderman::OrderService order_service; - std::map> state; + bts::api::admin::AdminService admin_service; + std::map> state; std::map manual_fills; int id; - + PositionServicePNLHelper pnl_helper; + int64_t poll_period_ms; // manual fills management std::unique_ptr manual_fills_store ; PositionServiceBase::Manual_fillsStatefulPublisher manual_fills_publisher; - + api::admin::AdminData admin_data; + std::map account_map; + PImpl(bts::api::ServiceContext& context, const PositionServiceBase::Position_dataStatefulPublisher& publisher, - const PositionServiceBase::Manual_fillsStatefulPublisher &manual_fills_publisher) + const PositionServiceBase::Pnl_dataStatefulPublisher& pnl_publisher, + const PositionServiceBase::Pnl_account_dataStatefulPublisher& pnl_account_publisher, + const PositionServiceBase::Manual_fillsStatefulPublisher &manual_fills_publisher, + bts::md::MDClient& md_client) : + context(context), publisher(publisher), + pnl_publisher(pnl_publisher), + pnl_account_publisher(pnl_account_publisher), order_service(context.locate()), + admin_service(context.locate()), state(), id(0), + pnl_helper(md_client), + poll_period_ms(500), manual_fills_store(std::make_unique ("manual_fills")), - manual_fills_publisher(manual_fills_publisher) + manual_fills_publisher(manual_fills_publisher), + admin_data(), + account_map() { } }; -PositionServiceServer::PositionServiceServer(bts::api::ServiceContext& context) +PositionServiceServer::PositionServiceServer(bts::api::ServiceContext& context, + bts::md::MDClient& md_client) : PositionServiceBase (context), me(new PImpl(context, make_position_data_stateful_publisher(), - make_manual_fills_stateful_publisher())) + make_pnl_data_stateful_publisher(), + make_pnl_account_data_stateful_publisher(), + make_manual_fills_stateful_publisher(), + md_client)) { me->order_service.subscribe_fills() | rxcpp::operators::subscribe( [this](auto const& update) { - for (const auto& f : update.removed()) me->publisher.add_or_update(_on_fill(*f,false)); for (const auto& f : update.added()) - me->publisher.add_or_update(_on_fill(*f,true)); + me->publisher.add_or_update(_on_fill(*f,true)); for (const auto& f : update.changed()) { me->publisher.add_or_update(_on_fill(*f.previous(),false)); me->publisher.add_or_update(_on_fill(*f.current(),true)); } me->publisher.flush(); + _compute_pnl_and_publish(); + }); + + me->admin_service.subscribe_admin_data() | + rxcpp::operators::subscribe( + [this](auto const& update) { + for (const auto& r : update.added()) + {_set_admin_data(*r);} + for (const auto& r : update.changed()) + {_set_admin_data(*r.current());} }); + me->context.post([this](){_periodic_poll();},me->poll_period_ms); auto all = me->manual_fills_store->retrieve_all()[manual_fills_ns]; LOG(persist, Info, "Finished loading all {} manual fills", all.size()); ManualFill rec; @@ -102,11 +138,9 @@ bts::api::orderman::Fill PositionServiceServer::to_fill(bts::api::position::Manu return result; } - const PositionUnit& PositionServiceServer::_on_fill (const Fill& f, bool is_add){ - std::string fill_account=_fill_account(f); - PositionUnit& pu=_get_or_add(fill_account,f.security_key()); + PositionUnit& pu=_get_or_add(f); double avg_price, tot_qty; if(f.fill_kind()==bts::api::orderman::Fill::FILL_KIND_SOD){ if(f.side()==api::Side::SIDE_BUY){ @@ -147,14 +181,16 @@ const PositionUnit& PositionServiceServer::_on_fill (const Fill& f, return pu; } -PositionUnit& PositionServiceServer::_get_or_add (const std::string& acct, - uint32_t seckey){ - auto &pu = me->state[acct][seckey]; +PositionUnit& PositionServiceServer::_get_or_add (const Fill& f){ + uint32_t account_id = f.internal_account_id(); + uint32_t seckey = f.security_key(); + auto &pu = me->state[account_id][seckey]; if (pu.record_id() == 0) { // new PU - pu.set_record_id(++me->id); - pu.set_account(acct); - pu.set_security_id(seckey); + pu.set_record_id(++me->id); + pu.set_security_id(seckey); + pu.set_account_id(account_id); + pu.set_account(f.account()); } return pu; } @@ -195,6 +231,14 @@ void PositionServiceServer::_avg_price_qty(double base_p, std::string PositionServiceServer::_fill_account(const Fill& fill){ return boost::trim_right_copy(fill.account()); } +bool PositionServiceServer::_valid_account_id (const Fill& fill){ + return fill.internal_account_id() != 0; +} +void PositionServiceServer::_set_admin_data(const api::admin::AdminData& admin_data){ + me->admin_data = admin_data; + for (const auto& a : admin_data.accounts()) + me->account_map[a.name()] = a.id(); +} PositionServiceServer::~PositionServiceServer(){ } @@ -293,4 +337,27 @@ void PositionServiceServer::remove_manual_fill( me->manual_fills_publisher.flush(); } +void PositionServiceServer::_compute_pnl_and_publish(){ + std::vector in_pos; + for (auto& acct_p : me->state) { + for (auto& sec_p : acct_p.second) { + const PositionUnit& pu = sec_p.second; + in_pos.push_back(&pu); + } + } + std::vector pnl; + std::vector account_pnl; + me->pnl_helper.compute_pnl(in_pos,me->account_map,pnl,account_pnl); + for (const auto& u : pnl) + me->pnl_publisher.add_or_update(u); + for (const auto& u : account_pnl) + me->pnl_account_publisher.add_or_update(u); + me->pnl_publisher.flush(); + me->pnl_account_publisher.flush(); +} + +void PositionServiceServer::_periodic_poll(){ + _compute_pnl_and_publish(); + me->context.post([this](){_periodic_poll();},me->poll_period_ms); +} }} diff --git a/src/server/sparkd/position/PositionServiceServer.h b/src/server/sparkd/position/PositionServiceServer.h index cd983e8c6..dac6f2996 100644 --- a/src/server/sparkd/position/PositionServiceServer.h +++ b/src/server/sparkd/position/PositionServiceServer.h @@ -5,16 +5,15 @@ #include namespace bts { -namespace api { -class ServiceContext; -} -namespace api { -namespace orderman { -class Fill; -} -} // namespace api +namespace md { class MDClient;} +namespace api { class ServiceContext; } +namespace api { namespace orderman { class Fill;}} +namespace api { namespace admin { class AdminData;}} + namespace position { +class MDClient; + class PositionServiceServer : public bts::api::position::PositionServiceBase { private: struct PImpl; @@ -28,16 +27,20 @@ class PositionServiceServer : public bts::api::position::PositionServiceBase { _on_fill(const bts::api::orderman::Fill &fill, bool is_add); // given an account and key, finds (or adds if absent) // the PositionUnit entry - bts::api::position::PositionUnit &_get_or_add(const std::string &acct, - uint32_t sec_id); + bts::api::position::PositionUnit &_get_or_add(const bts::api::orderman::Fill& fill); + void _periodic_poll(); + void _compute_pnl_and_publish(); static void _avg_price_qty(double base_p, double base_q, double fill_p, double fill_q, bool is_add, const std::string &fill_id, double &avg_price, double &tot_qty); static std::string _fill_account(const bts::api::orderman::Fill &fill); + static bool _valid_account_id (const bts::api::orderman::Fill& fill); + void _set_admin_data (const api::admin::AdminData& admin_data); public: - PositionServiceServer(bts::api::ServiceContext &context); + PositionServiceServer(bts::api::ServiceContext &context, + md::MDClient&); void create_manual_fill( const ::bts::api::position::ManualFillBatch &, diff --git a/src/server/sparkd/position/tests/PositionTests.cpp b/src/server/sparkd/position/tests/PositionTests.cpp index 62c93aec6..51d1f5086 100644 --- a/src/server/sparkd/position/tests/PositionTests.cpp +++ b/src/server/sparkd/position/tests/PositionTests.cpp @@ -1,11 +1,6 @@ #include "catch/catch.hpp" -#include "../MarginServiceCore.h" -#include "../PositionServiceCore.h" #include "../Position.h" -#include "../MarginResult.h" -#include "../IMarginObserver.h" - #include "lib/orderman/orderman_utils/orderman_common.h" #include "lib/config/config.h" @@ -26,14 +21,14 @@ namespace position class PositionTests { public: - static bool query_margin_webapi(MarginServiceCore& svc, const std::vector& pos, MarginResult& res) - { - return svc.query_margin_webapi(pos, res); - } - static void set_verbose_debugging(MarginServiceCore& svc, bool verb) - { - return svc.set_verbose_debugging(verb); - } + // static bool query_margin_webapi(MarginServiceCore& svc, const std::vector& pos, MarginResult& res) + // { + // return svc.query_margin_webapi(pos, res); + // } + // static void set_verbose_debugging(MarginServiceCore& svc, bool verb) + // { + // return svc.set_verbose_debugging(verb); + // } }; diff --git a/src/server/sparkd/user/AdminServiceServer.cpp b/src/server/sparkd/user/AdminServiceServer.cpp index bb30092a7..db3a0c7ad 100644 --- a/src/server/sparkd/user/AdminServiceServer.cpp +++ b/src/server/sparkd/user/AdminServiceServer.cpp @@ -155,7 +155,7 @@ void AdminServiceServer::get_filtered_admin_data( const api::Response& reply) { bts::api::user::TokenPayload toke; - if(bts::api::auth::decode_token(token, &toke)) + bts::api::auth::decode_token(token, &toke); { if(toke.wheel()) reply(_state); diff --git a/src/web/CMakeDeps.cmake b/src/web/CMakeDeps.cmake index 40156daa9..bc74cb4cf 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/AlgoDefinitionService.proto;../lib/api/services/AlgoService.proto;../lib/api/services/ApiUnitTestService.proto;../lib/api/services/AuthService.proto;../lib/api/services/ErrorReportService.proto;../lib/api/services/HelloService.proto;../lib/api/services/MBOService.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/domain_model/Security.proto;../lib/md/protos/co2.proto;../lib/sparkproto/SparkProtos.proto;api/build.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;ui/src/obj/Debug/netstandard2.0/ui.AssemblyInfo.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/AlgoDefinitionService.proto;../lib/api/services/AlgoService.proto;../lib/api/services/ApiUnitTestService.proto;../lib/api/services/AuthService.proto;../lib/api/services/ErrorReportService.proto;../lib/api/services/HelloService.proto;../lib/api/services/MBOService.proto;../lib/api/services/MarginService.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/domain_model/Security.proto;../lib/md/protos/co2.proto;../lib/sparkproto/SparkProtos.proto;api/build.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/psl/scripts/ci-build.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;ui/src/obj/Debug/netstandard2.0/ui.AssemblyInfo.fs) \ No newline at end of file diff --git a/src/web/ui/sass/main.scss b/src/web/ui/sass/main.scss index c37815561..dbf86783d 100644 --- a/src/web/ui/sass/main.scss +++ b/src/web/ui/sass/main.scss @@ -340,3 +340,19 @@ body { } } +.inline-label { + line-height: 1.5; + padding-bottom: 0.6em; + padding-left: 0.625em; + padding-right: 0.625em; + padding-top: 0.6em; + display: inline-flex; +} + +.inline-label:not(:last-child) { + margin-bottom: 0; +} + +.disabled-label { + color: $grey; +} \ No newline at end of file diff --git a/src/web/ui/src/Access.fs b/src/web/ui/src/Access.fs index a54bb0c46..66e19466a 100644 --- a/src/web/ui/src/Access.fs +++ b/src/web/ui/src/Access.fs @@ -1,6 +1,7 @@ [] module Access open bts.api.admin +open bts.api.position open bts.api open Elmish open bts.api.orderman @@ -20,6 +21,12 @@ let getAccountOrdersData (admin:IAdminService) (success) (fail) = success (fun e -> fail e.Message) +let getMarginData (span:IMarginService) (account_name: string) (success) (fail) = + Cmd.ofPromise + span.get_excess_liquidity_report + { account = account_name } + success + (fun e -> fail e.Message) module Lens = // type Setter<'a,'b> = { @@ -96,9 +103,40 @@ module Accounts = riskLimitEnforcement = { productLimitInNetTerms = true } + accountLossLimit = + { + enforce = false + totalLossLimit = 0u + } + } + + let emptyMargin : ExcessLiquidityReport = { + account = "ERROR NO DATA" + accountId = 0u + marginResult = { + account = "ERROR NO DATA" + status = bts.api.position.MarginResult.ResultStatus.Error + portfolioId = "" + settleIndicator = "" + settleQualifier = "" + asOfTime = "" + calculationId = "" + optValue = 0.0 + nonoptValue = 0.0 + maintMargin = 0.0 + initialMargin = 0.0 + } + shouldEnforceMargin = false + leverageFactor = 0.0 + cashBalance = 0.0 + accountPnl = { + accountId = 0u + accountName = "" + dayPnl = 0.0 + } + excessLiquidity = 0.0 } - diff --git a/src/web/ui/src/Accounts/State.fs b/src/web/ui/src/Accounts/State.fs index 6efe5f43f..c1f3f8261 100644 --- a/src/web/ui/src/Accounts/State.fs +++ b/src/web/ui/src/Accounts/State.fs @@ -2,12 +2,14 @@ module Accounts.State open Accounts.Types open bts.api.admin +open Global open Elmish open Fable.PowerPack.PromiseImpl open Fable.Import open bts.co2 open Fable open Fable.PowerPack +open bts.api.position let isNewAccount (account:BTSAccount) = account.id = 0u && account.isPresent @@ -17,8 +19,12 @@ let currentAccountIsDefault (account:BTSAccount) = (account.id = 0u) let init (context:ServiceContext.ServiceContext) = let admin = AdminService.Create context "" let mdr = MarketDataRelay.Create context "" + let span = MarginService.Create context "" { tab = General admin = admin + marginService = span + marginData = emptyMargin + marginScratch = emptyMargin adminData = None ordersData = [] SelectedOrders = [] @@ -79,11 +85,21 @@ let applySIMUpdate (data:SIMAccountData) = function let saveAccount (admin:IAdminService) (account:BTSAccount) (model:Model) = promise { let! result = admin.update_account { completeData = account } - if result.isSuccess then + let! result2 = model.marginService.set_cash_factor { account = model.scratch.name; leverageFactor = model.marginScratch.leverageFactor } + let! result3 = model.marginService.set_cash_balance { account = model.scratch.name; cashBalance = model.marginScratch.cashBalance } + let! result4 = model.marginService.set_should_enforce_margin { account = model.scratch.name; shouldEnforceMargin = model.marginScratch.shouldEnforceMargin } + + if (result.isSuccess && result2.success && result3.success && result4.success) then let! data = admin.get_filtered_admin_data () return UpdateAdminData data + elif (not result.isSuccess) then + return (ErrorOccured (sprintf "Error saving account: %s" result.errorText)) + elif (not result2.success) then + return ErrorOccured (sprintf "Error saving leverage factor: %s" result2.errorMessage) + elif (not result4.success) then + return ErrorOccured (sprintf "Error saving Enfrce Margin: %s" result4.errorMessage) else - return ErrorOccured (sprintf "Error saving account: %s" result.errorText) + return ErrorOccured (sprintf "Error saving cash balence: %s" result3.errorMessage) } let createAccount (admin:IAdminService) (account:BTSAccount) (model:Model) = promise { @@ -198,7 +214,31 @@ let update msg (model:Model) : Model * Cmd = | _ -> [] {model with SelectedOrders = so}, Cmd.Empty | SelectAccount account -> - { (clearSelection model) with previousScratch = model.scratch; scratch = account }, Cmd.Empty + { (clearSelection model) with previousScratch = model.scratch; scratch = account; marginScratch = emptyMargin }, Cmd.ofMsg <| GetMarginData + | GetMarginData -> + { model with ShowLoading = false}, (getMarginData model.marginService model.scratch.name UpdateMarginData (box>>ErrorOccured)) + | UpdateMarginData newData -> + let cmd = Cmd.ofPromise (fun _ -> promise { do! Promise.sleep 10000 } ) () (fun _ -> GetMarginData) (fun _ -> GetMarginData) + match (model.marginScratch = emptyMargin || model.marginScratch.account <> model.scratch.name), + (compareDouble model.marginData.leverageFactor model.marginScratch.leverageFactor 10000u), + (compareDouble model.marginData.cashBalance model.marginScratch.cashBalance 10000u), + (model.marginData.shouldEnforceMargin = model.marginScratch.shouldEnforceMargin) with + | true, _, _, _ -> {model with marginData = newData; marginScratch = newData}, cmd + | false, true, true, true -> {model with marginData = newData; marginScratch = newData}, cmd + | false, false, true, true -> {model with marginData = newData; marginScratch = {model.marginScratch with cashBalance = newData.cashBalance; shouldEnforceMargin = newData.shouldEnforceMargin}}, cmd + | false, true, false, true -> {model with marginData = newData; marginScratch = {model.marginScratch with leverageFactor = newData.leverageFactor; shouldEnforceMargin = newData.shouldEnforceMargin}}, cmd + | false, false, false, true -> {model with marginData = newData; marginScratch = { model.marginScratch with shouldEnforceMargin = newData.shouldEnforceMargin}}, cmd + | false, true, true, false -> {model with marginData = newData; marginScratch = {model.marginScratch with cashBalance = newData.cashBalance; leverageFactor = newData.leverageFactor}}, cmd + | false, false, true, false -> {model with marginData = newData; marginScratch = {model.marginScratch with cashBalance = newData.cashBalance}}, cmd + | false, true, false, false -> {model with marginData = newData; marginScratch = {model.marginScratch with leverageFactor = newData.leverageFactor}}, cmd + | false, false, false, false -> {model with marginData = newData}, cmd + + | SetMarginCashBalance newBalance -> + { model with marginScratch = { model.marginScratch with cashBalance = newBalance}},Cmd.Empty + | SetMarginCashFactor newFactor -> + { model with marginScratch = { model.marginScratch with leverageFactor = newFactor}},Cmd.Empty + | SetEnforceMargin enf -> + { model with marginScratch = { model.marginScratch with shouldEnforceMargin = enf}},Cmd.Empty | DeleteAccount account -> let mdl = { model with OptionsMenuOpen = ""} match Fable.Import.Browser.window.confirm (sprintf "Are you sure you want to delete '%s'" account.name) with @@ -212,7 +252,7 @@ let update msg (model:Model) : Model * Cmd = | SaveScratch -> match isNewAccount model.scratch with | true -> model, Cmd.ofExistingPromise (createAccount model.admin model.scratch model ) id (box>>ErrorOccured) - | false -> model, Cmd.ofExistingPromise (saveAccount model.admin model.scratch model ) id (box>>ErrorOccured) + | false -> { model with marginScratch = model.marginData }, Cmd.ofExistingPromise (saveAccount model.admin model.scratch model ) id (box>>ErrorOccured) | ErrorOccured msg -> Global.alert Global.Error (string msg) Browser.console.log msg @@ -221,6 +261,8 @@ let update msg (model:Model) : Model * Cmd = match tab with | OrdersTab -> { (clearSelection model) with tab = tab }, Cmd.ofMsg GetOrdersData + | MarginTab -> + { (clearSelection model) with tab = tab }, Cmd.ofMsg GetMarginData | _ -> { (clearSelection model) with tab = tab }, Cmd.Empty | Update field -> @@ -231,8 +273,8 @@ let update msg (model:Model) : Model * Cmd = { model with scratch = { model.scratch with securityClipLimits = { limit = limits } }; SelectedClipLimits = selectedlimits }, Cmd.Empty | AddClipLimit limit -> let limits = match (Array.exists (fun (elm:AccountSecurityClipLimitUnit) -> elm.security.securitySymbol = "") model.scratch.securityClipLimits.limit) with - | true -> model.scratch.securityClipLimits.limit - | false -> Array.concat [ model.scratch.securityClipLimits.limit; [| limit |] ] + | true -> model.scratch.securityClipLimits.limit + | false -> Array.concat [ model.scratch.securityClipLimits.limit; [| limit |] ] { model with scratch = { model.scratch with securityClipLimits = { limit = limits } }}, Cmd.Empty | ChangeMultipleClipLimits (value, selectedLimits) -> @@ -290,6 +332,12 @@ let update msg (model:Model) : Model * Cmd = { model with scratch = { model.scratch with productPositionLimits = limits } }, Cmd.Empty | EnforceProductLimitInNetTerms value -> {model with scratch = {model.scratch with riskLimitEnforcement = {productLimitInNetTerms = value}}}, Cmd.none + + | EnforceLossLimit e -> + { model with scratch = { model.scratch with accountLossLimit = { model.scratch.accountLossLimit with enforce = e} } }, Cmd.Empty + | TotalLossLimit tll -> + { model with scratch = { model.scratch with accountLossLimit = { model.scratch.accountLossLimit with totalLossLimit = tll} } }, Cmd.Empty + | SecurityLimits limits -> { model with scratch = { model.scratch with securityPositionLimits = limits } }, Cmd.Empty | DeleteSecurityLimit i -> @@ -328,7 +376,7 @@ let update msg (model:Model) : Model * Cmd = | RequestExpirationNames -> model, Cmd.ofExistingPromise (requestExpNames model.mdRelay) id (box>>ErrorOccured) | UpdateExpirationNames (data:ExpirationNameList[]) -> - { model with mdRelayExpNameData = data }, Cmd.Empty + { model with mdRelayExpNameData = data }, Cmd.ofMsg GetMarginData | OpenOptionsMenu acc -> {model with OptionsMenuOpen = acc}, Cmd.Empty | OpenConfirmModal (title, okMessage) -> diff --git a/src/web/ui/src/Accounts/Types.fs b/src/web/ui/src/Accounts/Types.fs index 73b4c4316..7ce054b59 100644 --- a/src/web/ui/src/Accounts/Types.fs +++ b/src/web/ui/src/Accounts/Types.fs @@ -5,6 +5,7 @@ open bts.api open bts.api.orderman open JSMap open bts.co2 +open bts.api.position type Tab = | General @@ -13,6 +14,7 @@ type Tab = | ExchangeSettingsTab | ConnectionsTab | OrdersTab + | MarginTab let venues = [| Venue.GLBX; Venue.ICE; Venue.SIM |] @@ -54,10 +56,20 @@ type Msg = | UpdateOrdersData of OrdersSummary | SelectOrder of bool * OrdersSummaryUnit list | CancelOrders of OrdersSummaryUnit list + // Margin / Span + | GetMarginData + | UpdateMarginData of ExcessLiquidityReport + | SetMarginCashBalance of double + | SetMarginCashFactor of double + | SetEnforceMargin of bool + | SelectAccount of BTSAccount | DeleteAccount of BTSAccount | AddAccount + | SaveLeverageFactor + | SaveCashBalance | SaveScratch + | ErrorOccured of obj // Tabs | SelectTab of Tab @@ -77,6 +89,9 @@ type Msg = | DeleteProductLimit of int | EnforceProductLimitInNetTerms of bool + | EnforceLossLimit of bool + | TotalLossLimit of uint32 + | SecurityLimits of AccountSecurityPositionLimitUnit[] | DeleteSecurityLimit of int // ExchangeData @@ -96,6 +111,9 @@ type Msg = type Model = { tab : Tab admin : IAdminService + marginService: IMarginService + marginData: ExcessLiquidityReport + marginScratch: ExcessLiquidityReport adminData : AdminData option ordersData : OrdersSummaryUnit list SelectedOrders: OrdersSummaryUnit list diff --git a/src/web/ui/src/Accounts/View.fs b/src/web/ui/src/Accounts/View.fs index 8a1ccad08..9002756df 100644 --- a/src/web/ui/src/Accounts/View.fs +++ b/src/web/ui/src/Accounts/View.fs @@ -11,10 +11,17 @@ open Fable open System open bts.co2 open Elmish.React.Common - +open Fable.Core.JsInterop +open Fable.Core let accountIsSelected (initialState:BTSAccount) (scratch:BTSAccount) = scratch <> initialState +[] +let withCommas (n:float) : string = jsNative + +[] +let withCommasAndTwoDigits (n:float) : string = jsNative + let selectedAccount model = match model.adminData with | Some data -> @@ -23,9 +30,13 @@ let selectedAccount model = | None -> None let isDirty model = + let marginDirty = model.marginData.cashBalance <> model.marginScratch.cashBalance || + model.marginData.leverageFactor <> model.marginScratch.leverageFactor || + model.marginData.shouldEnforceMargin <> model.marginScratch.shouldEnforceMargin match selectedAccount model with | Some account -> - account <> model.scratch + account <> model.scratch || + marginDirty | None -> false let saveButton model dispatch = @@ -80,6 +91,56 @@ let generalView model dispatch = accountControls model dispatch ] +let marginView model dispatch = + let disabled = not model.marginScratch.shouldEnforceMargin + + let headerRow:TableControl.row = + { + cells = + [ + TableControl.Cell (div [Style [MinWidth "3em"]][str "Time"]) + TableControl.Cell (str "Account") + TableControl.Cell (str "Leverage Factor") + TableControl.Cell (str "Cash Balance") + TableControl.Cell (str "Non Opt Value") + TableControl.Cell (str "Opt Value") + TableControl.Cell (str "Initial Margin") + TableControl.Cell (str "Maint. Margin") + TableControl.Cell (str "Day PnL") + TableControl.Cell (str "Excess Liquidity") + ] + className = "" + header = None + } + + let rws:TableControl.row = + { + cells = + [ + TableControl.Cell (str model.marginData.marginResult.asOfTime.[0..9]) + TableControl.Cell (str model.marginData.account) + TableControl.Cell (Fields.disabledSpan (withCommas model.marginData.leverageFactor) disabled) + TableControl.Cell (Fields.disabledSpan (withCommas model.marginData.cashBalance) disabled) + TableControl.Cell (str (withCommasAndTwoDigits model.marginData.marginResult.nonoptValue)) + TableControl.Cell (str (withCommasAndTwoDigits model.marginData.marginResult.optValue)) + TableControl.Cell (str (withCommasAndTwoDigits model.marginData.marginResult.initialMargin)) + TableControl.Cell (str (withCommasAndTwoDigits model.marginData.marginResult.maintMargin)) + TableControl.Cell (str (withCommasAndTwoDigits model.marginData.accountPnl.dayPnl)) + TableControl.Cell (str (withCommasAndTwoDigits model.marginData.excessLiquidity)) + ] + className = "" + header = None + } + + div [][ + TableControl.createDataTable (Some headerRow) [rws] (span [][]) + Fields.checkbox "Enforce Margin Limit" "" (model.marginScratch.shouldEnforceMargin) [] (SetEnforceMargin >> dispatch) + Fields.double "Leverage Factor:" (model.marginScratch.leverageFactor) [Disabled disabled] (double >> SetMarginCashFactor >> dispatch) + Fields.number "Cash Balance:" (int model.marginScratch.cashBalance) [Disabled disabled] (double >> SetMarginCashBalance >> dispatch) + accountControls model dispatch + ] + + let GetSymbolArray (model:Model) = Array.map (fun a -> a.symbol ) model.mdRelayProductData |> Array.distinct @@ -550,6 +611,19 @@ let lazyProductLimitsView model dispatch = productLimits model dispatch +let lossLimitView model dispatch = + div [] [ + div [ ClassName "columns" ][ + h1 [ ClassName "is-size-3 column is-narrow" ][str "Loss Limit" ] + div [ ClassName "column is-size-7 has-text-grey-mid is-help-text" ] + [str "If Enabled, this limits the account's loss."] + ] + div [][ + Fields.checkbox "Enable Loss Limit" "" model.scratch.accountLossLimit.enforce [] (EnforceLossLimit >> dispatch) + Fields.inlineText "Total Loss Limit" (string model.scratch.accountLossLimit.totalLossLimit) [] (not model.scratch.accountLossLimit.enforce) (TotalLossLimit >> dispatch) + ] + ] + let securityLimitsView model dispatch = let headerRow:TableControl.row = { @@ -668,9 +742,10 @@ let securityLimitsView model dispatch = let LimitsView model dispatch = div [ Class "limits-container"] [ + lossLimitView model dispatch + br [] lazyClipLimitsView model dispatch lazyProductLimitsView model dispatch - //securityLimitsView model dispatch accountControls model dispatch @@ -782,7 +857,8 @@ let tabContent model dispatch = | ConnectionsTab, _ -> ConnectionsView model dispatch | CreditLimitsTab, _ -> creditLimitsView model dispatch | ExchangeSettingsTab, _ -> exchangeSettings model dispatch - | OrdersTab, _ -> ordersView model dispatch) + | OrdersTab, _ -> ordersView model dispatch + | marginTab, _ -> marginView model dispatch) ] let isActive account = function @@ -916,6 +992,7 @@ let tabName = function | ConnectionsTab -> "Connections" | ExchangeSettingsTab -> "Exchange Settings" | OrdersTab -> "Orders" + | MarginTab -> "Margin" | other -> string other let tabHeader tab currentTab dispatch = @@ -950,6 +1027,7 @@ let root (model:Model) dispatch = // tabHeader CreditLimits model.tab dispatch (if (Array.length data.accounts > 0) then (tabHeader ExchangeSettingsTab model.tab dispatch) else span [][] ) (if (Array.length data.accounts > 0) then (tabHeader OrdersTab model.tab dispatch) else span [][] ) + (if (Array.length data.accounts > 0) then (tabHeader MarginTab model.tab dispatch) else span [][] ) ] ] tabContent model dispatch diff --git a/src/web/ui/src/Controls/Fields.fs b/src/web/ui/src/Controls/Fields.fs index e0b3b002e..4e4c4b0cc 100644 --- a/src/web/ui/src/Controls/Fields.fs +++ b/src/web/ui/src/Controls/Fields.fs @@ -16,6 +16,11 @@ let Label (lbl:string) = label [ ClassName "label" ] [ str lbl ] +let disabledSpan (text:string) (disabled: bool) = + match disabled with + | true -> span [Class "disabled-label"] [str text] + | false -> span [] [str text] + let text (lbl:string) (dv:string) (attrs:IHTMLProp list) onchange = div [ ClassName "field" ] @@ -31,6 +36,22 @@ let text (lbl:string) (dv:string) (attrs:IHTMLProp list) onchange = ] ] +let inlineText (lbl:string) (dv:string) (attrs:IHTMLProp list) (disabled:bool) onchange = + let disabledLabel = if disabled then "disabled-label" else "" + span [ ClassName "field" ] + [ + label [ ClassName ("label inline-label " + disabledLabel)][str lbl] + span [ ClassName "control" ] + [ input ([ + ClassName "input" + Type "text" + Value dv + OnChange (fun ev -> !!ev.target?value |> onchange) + Disabled disabled + ] @ attrs) + ] + ] + let textPair (v:string*string) (keyAttrs:IHTMLProp list) (valueAttrs:IHTMLProp list) (thirdCol:ReactElement list) (onchange: (string*string) -> string -> string -> unit) = div [ ClassName "columns" ] [ @@ -138,6 +159,9 @@ let number64 (lbl:string) (dv:int64) (attrs:IHTMLProp list) onchange = text lbl (string dv) attrs (int64>>onchange) +let double (lbl:string) (dv:double) (attrs:IHTMLProp list) onchange = + text lbl (string dv) attrs (double>>onchange) + let enumSelect<'a> (lbl:string) (dv:'a) (values:'a[]) (attrs:IHTMLProp list) (onchange:'a->unit) = let vals = match (Array.tryFindIndex (fun elm -> unbox elm = unbox dv) values) with | None -> Array.concat [[| dv |]; values] @@ -198,7 +222,7 @@ let radioButton (lbl:string) (description:string) (dv:bool) (attrs:IHTMLProp lis let mapSelectEx (lbl:string) (options:Map) (defaultText:string option) (defaultValue:string option) (onchange:'a->unit) (is_disabled:bool option) = - let dis = + let dis = match is_disabled with | None -> false | Some a -> a @@ -218,7 +242,7 @@ let mapSelectEx (lbl:string) (options:Map) (defaultText:string op Label lbl div [ ClassName "control" ] [ - div [ ClassName "select"] + div [ ClassName "select"] [ select [ Value dv diff --git a/src/web/ui/src/Global.fs b/src/web/ui/src/Global.fs index 4b6dfe9df..41c76cca2 100644 --- a/src/web/ui/src/Global.fs +++ b/src/web/ui/src/Global.fs @@ -11,6 +11,8 @@ type AlertLevel = [] let alert (_:AlertLevel) (_:string) = Fable.Core.Exceptions.jsNative +let compareDouble (d1:double) (d2:double) (precision: uint32) = (int (d1 * (double precision))) = (int (d2 * (double precision))) + module Reflection = open Fable.Core.JsInterop @@ -21,7 +23,7 @@ module Reflection = [] let values<'a> x : 'a[] = jsNative - let iterate<'a> x : (string*'a)[] = + let iterate<'a> x : (string*'a)[] = Array.zip (keys x) (values x) let spread<'a> (a:'a) (b:obj) : 'a = Imports.DynamicService.merge (a,b) diff --git a/src/web/ui/src/Users/Types.fs b/src/web/ui/src/Users/Types.fs index ae2ad5d9b..130711a57 100644 --- a/src/web/ui/src/Users/Types.fs +++ b/src/web/ui/src/Users/Types.fs @@ -86,12 +86,12 @@ type Model = { } -let CMEMicMap:Map = [ - ("XCME", bts.api.CMEMics.CME); - ("XNYM", bts.api.CMEMics.NYMEX); - ("XCBT", bts.api.CMEMics.CBT); - ("XCEC", bts.api.CMEMics.COMEX) - ] |> Map.ofList +let CMEMicMap:Map = Map.empty + .Add("XCME", bts.api.CMEMics.CME) + .Add("XNYM", bts.api.CMEMics.NYMEX) + .Add("XCBT", bts.api.CMEMics.CBT) + .Add("XCEC", bts.api.CMEMics.COMEX) + .Add("DME", bts.api.CMEMics.DME) let ICEMicMap:Map = [ ("IFCA", bts.api.ICEMics.IFC); diff --git a/src/web/ui/src/ui.fsproj b/src/web/ui/src/ui.fsproj index 7538454d3..fc520834a 100644 --- a/src/web/ui/src/ui.fsproj +++ b/src/web/ui/src/ui.fsproj @@ -28,6 +28,8 @@ + + diff --git a/testing/manual/admin/clip_limits.txt b/testing/manual/admin/clip_limits.txt index 97883bd46..98d0751ca 100644 --- a/testing/manual/admin/clip_limits.txt +++ b/testing/manual/admin/clip_limits.txt @@ -19,3 +19,8 @@ Enter a Clip Size of 10. Make sure orders > 10 are rejected and < 10 are allowed 7. Delete the BZ spread clip limits row 8. Verify you can no longer enter BZ spread trades, but you can still enter BZ future trades + +9. Delete all rows for clip limits. Make sure you can not enter a future or spread order. Now +enter a row for Spreads (All) with clip limit 100. Make sure that works correctly, ie allows +spreads <= 100 but does not allow futures. Now enter a row for Futures (All) with clip limit 50. +Verify that this works correctly too. \ No newline at end of file diff --git a/testing/manual/admin/loss_limit.txt b/testing/manual/admin/loss_limit.txt new file mode 100644 index 000000000..c0531a5c2 --- /dev/null +++ b/testing/manual/admin/loss_limit.txt @@ -0,0 +1,29 @@ +This test verifies that account loss limits work. + +-1. This needs sim market data **not** to be merged with replay (or cert), so we can create our own markets. + +0. Blow away the orders and fills databases (/home/bts/sparkdata/data/databases/orders,fills, +ex_sim_orders) + +Pick a future and 2 accounts (lets call them A and B). + +In the risk admin +a. make sure the clip limits and position limits are high enough for the 2 accounts so they +dont interfare and stop orders +b. for the loss limit enter 1000 for B. + +1. Bring up 2 ladders for the contract you selected, each set to one of the accounts above. Bring up the +PNL window and set it to Mid. Now from account A enter a 100 lot bid and 100 lot ask (so now we have markets). + +2. From Account B cross the sell with a 1 lot buy. Now you should see a negative PNL for B, and a positive for A +(because we bought at higher than the mid in B, and sold for higher than the mid in A) + +3. Go to the risk admin and enter a loss limit for account B (~4 times the PNL for B) + +4. Repeat 2 and continue monitoring the PNL. Sell a few more at the market bid. As soon as the PNL goes below +the loss limits, orders should be rejected with a message saying exactly why. + +5. Increase the loss limit, and hit it again with a few loser trades. + +6. Move the market mid (by moving the 100 lots orders in 1) and try to get B's PNL within the loss limit range. +Now enter more loser trades in B and make it hit the limit again. diff --git a/testing/manual/algo_operations/Top_cover_TEST.txt b/testing/manual/algo_operations/Top_cover_TEST.txt index 57f6f2afa..a603a2c3f 100644 --- a/testing/manual/algo_operations/Top_cover_TEST.txt +++ b/testing/manual/algo_operations/Top_cover_TEST.txt @@ -1,3 +1,14 @@ +This needs sim market data **not** to be merged with replay (or +cert), so we can create our own markets. Use the following key to set +market data up: + +ExchangeSim { + value { + merge_with_md: false + forward_implied: false + } +} + Algo Studio lets you combine algos together and save it as an order type: To test this, we will create a common algo combination and test to make sure it's working as desired. @@ -6,42 +17,32 @@ To test this, we will create a common algo combination and test to make sure it' Let's start by creating the Algo: Launch Algo Studio -Click on top right icon to "add algo" +Click on top left icon to "add algo" Give it a name, call it T_C and give the abbrev a title of T_C as well. Create a Top with Cover, so the very top algos with be a "top" (top thresh 10) and it's child will be a cover (Hedge Ticks 2) Leave both Hedge and Child as limits of the child -Use a very liquid market with recorded data to test, I suggest something like ES or CL. - On the ladder, go to settings, then algo favorites and click add and choose your T_C algo. This will display it on the ladder for quick deployment -click on algo, then enter an order on the best bid +1.) Create a 20 lot bid/ask market 1 tick apart. +2.) Place a 5 lot T_C (top cover algo) buy order on the best bid market. +3.) Place another 5 lot buy limit order on the best bid +4.) Place a 25 lot sell limit order on the best bid, filling the orders placed in 1 and 2 +5.) Verify that the cover order was deployed. -wait until it fills, once fills, it should submit the cover order 2 ticks above the price you got filled at. - -try this with a sell order following the same procedure. +Repeat steps 1 through 5 with a sell order. Additionally, we will add a "lean" into the mix as the child of the cover. -go back to your algo studio, and change the hedge of the cover to be that of lean. - -What will happen is once the cover order is submitted, it will include the lean logic on that cover order. Meaning, it'll lean into the market and cross once the cross thresh is reach. - -Set the parameters to a cross thresh of 10, max chase ticks 0 and on toward no change - -save the algo, and send an order and wait for it to be filled. - -Once filled, it will remain working until the market flips, which means it ticks up a level. Once it flips and there is 1 remaining, the order should cross. - - - - - - +Go back to your algo studio, and change the hedge of the cover to be that of lean. +What will happen is once the cover order is submitted, it will include the lean logic on that cover order. +Set the parameters to a cross thresh of 10, max chase ticks 0 and on toward no change. +Save the algo, and repeat steps 1 through 5 again. +This time the hedge should get filled immediately, as it will lean into the market that we just reduced below it's threshold size. \ No newline at end of file diff --git a/testing/manual/keycodes/keycode.txt b/testing/manual/keycodes/keycode.txt index ffd573a26..ed9535602 100644 --- a/testing/manual/keycodes/keycode.txt +++ b/testing/manual/keycodes/keycode.txt @@ -15,7 +15,7 @@ This will test our ability to generate and enforce keycodes. You should see keycode details printed to screen -4) Now test concurrent logins. Start a sparkd. +4) Now test concurrent logins. Start a sparkd. Make sure bts_2 and bts_3 have market data permission a) Start a spark -multiple login as bts and go to where you would see some market data b) Start another spark -multiple but log in as bts_2 and display some data c) Start a third spark -multiple, log in as bts_2 and display some data. @@ -37,5 +37,19 @@ You should see keycode details printed to screen d) remove the expired keycode you just generated by: rm ~/sparkdata/conf/keycode.conf e) restart sparkd, you should see data as normal in bts_2 + +6) Test admin vs trading logins +Note that an admin login is one that does not have trading permission in any account +AND does not have the ability to see any market data + + a) start sparkd again. make sure bts_2 and bts_3 have market data and trading permissions in webui + b) Start a spark -multiple login as bts_2 and go to where you would see some market data. You should see data + c) Start a webui and log in as bts_3, you should see data + d) close your shell and log out of web ui, now log in bts_3 into the webui and log in bts_2 in a shell ( + this is the reverse of the previous order), you should see a message in the shell about keycodes, but it will let you log in, just not see market data + e) from the webui, de-permission bts_3 from trading AND market data, + f) exit shell, log out of webui and kill sparkd, restart sparkd + + g)now try d again-- you should not get the same shell error diff --git a/testing/manual/span/span.txt b/testing/manual/span/span.txt new file mode 100644 index 000000000..22a815661 --- /dev/null +++ b/testing/manual/span/span.txt @@ -0,0 +1,29 @@ +Test Author: Kevin +If it breaks: Kevin + +This will test our ability to compute and enforce span margin at CME + +1) Make sure span.conf looks like: + +SpanMargin { + value { + enabled: true + login: 'api_bts1' + password: 'Summer123' + url: 'https://cmecorenr.cmegroup.com/MarginServiceApi/1.4/' + } +} + +2) Fire up a shell and trade 1 ES into ACC A + +3) Open a webui, look at Margin Tab in ACC A + +You should see a value of around 6300 in maintenance margin after a minute or two + +4) If not already zero, set your cash balance to zero. + +5) Try to trade an ES in ACC A, you should be blocked by a limit breach + +6) Increase your cash balance to 10,000 and set leverage factor to 1.0 + +7) Try to trade again, you should be able to. \ No newline at end of file