diff --git a/.gitignore b/.gitignore index 10a357f8b..6896b131c 100644 --- a/.gitignore +++ b/.gitignore @@ -32,5 +32,4 @@ trace_data/ /src/lib/orderman/perf_analysis/last_pretty_log.csv /src/lib/orderman/perf_analysis/log.csv /doc/user/public/* -.ionide/* -src/web/ui/.ionide/symbolCache.db +.ionide/* diff --git a/doc/user/CMakeDeps.cmake b/doc/user/CMakeDeps.cmake index 636b0797c..59a6b66b9 100644 --- a/doc/user/CMakeDeps.cmake +++ b/doc/user/CMakeDeps.cmake @@ -1 +1 @@ -set(sources /home/ppinto/Dropbox/bts/spark/doc/user/build.sh;content/_index.md;content/bts_spark/Algos/_index.md;content/bts_spark/Glossary/index.md;content/bts_spark/Help/about_spark.png;content/bts_spark/Help/image39.png;content/bts_spark/Help/image58.png;content/bts_spark/Help/index.md;content/bts_spark/Launcher/image38.png;content/bts_spark/Launcher/image68.png;content/bts_spark/Launcher/index.md;content/bts_spark/Logging-In-to-the-System/image3.png;content/bts_spark/Logging-In-to-the-System/image64.png;content/bts_spark/Logging-In-to-the-System/index.md;content/bts_spark/Market/change_securities.png;content/bts_spark/Market/image11.png;content/bts_spark/Market/image14.png;content/bts_spark/Market/image20.png;content/bts_spark/Market/image21.png;content/bts_spark/Market/image23.png;content/bts_spark/Market/image24.png;content/bts_spark/Market/image34.png;content/bts_spark/Market/image49.png;content/bts_spark/Market/image53.png;content/bts_spark/Market/image66.png;content/bts_spark/Market/image69.png;content/bts_spark/Market/index.md;content/bts_spark/Market/market_view.png;content/bts_spark/Market/settings_icon.png;content/bts_spark/Market/time_and_sales.png;content/bts_spark/My-Windows/image15.png;content/bts_spark/My-Windows/image16.png;content/bts_spark/My-Windows/image37.png;content/bts_spark/My-Windows/index.md;content/bts_spark/Settings/image51.png;content/bts_spark/Settings/image9.png;content/bts_spark/Settings/index.md;content/bts_spark/Spreader/image10.png;content/bts_spark/Spreader/image13.png;content/bts_spark/Spreader/image18.png;content/bts_spark/Spreader/image19.png;content/bts_spark/Spreader/image28.png;content/bts_spark/Spreader/image29.png;content/bts_spark/Spreader/image31.png;content/bts_spark/Spreader/image35.png;content/bts_spark/Spreader/image40.png;content/bts_spark/Spreader/image43.png;content/bts_spark/Spreader/image54.png;content/bts_spark/Spreader/image55.png;content/bts_spark/Spreader/image56.png;content/bts_spark/Spreader/image6.png;content/bts_spark/Spreader/image7.png;content/bts_spark/Spreader/image70.png;content/bts_spark/Spreader/image72.png;content/bts_spark/Spreader/image8.png;content/bts_spark/Spreader/index.md;content/bts_spark/Trading/algo_studio.png;content/bts_spark/Trading/collapse_all_icon.png;content/bts_spark/Trading/cover_algo_control.png;content/bts_spark/Trading/expand_all_icon.png;content/bts_spark/Trading/image1.png;content/bts_spark/Trading/image17.png;content/bts_spark/Trading/image2.png;content/bts_spark/Trading/image25.png;content/bts_spark/Trading/image27.png;content/bts_spark/Trading/image33.png;content/bts_spark/Trading/image42.png;content/bts_spark/Trading/image44.png;content/bts_spark/Trading/image47.png;content/bts_spark/Trading/image48.png;content/bts_spark/Trading/image5.png;content/bts_spark/Trading/image50.png;content/bts_spark/Trading/image52.png;content/bts_spark/Trading/image57.png;content/bts_spark/Trading/image59.png;content/bts_spark/Trading/image60.png;content/bts_spark/Trading/image61.png;content/bts_spark/Trading/image62.png;content/bts_spark/Trading/image73.png;content/bts_spark/Trading/image74.png;content/bts_spark/Trading/index.md;content/bts_spark/Trading/join_algo_control.png;content/bts_spark/Trading/ladder.png;content/bts_spark/Trading/ladder_badges.png;content/bts_spark/Trading/ladder_main_part.png;content/bts_spark/Trading/lean_algo_control.png;content/bts_spark/Trading/mini_orderbook.png;content/bts_spark/Trading/oco_algo_control.png;content/bts_spark/Trading/offset_algo_control.png;content/bts_spark/Trading/pnl_calculation.png;content/bts_spark/Trading/pnl_window.png;content/bts_spark/Trading/recreate_icon.png;content/bts_spark/Trading/settings_icon.png;content/bts_spark/Trading/spark_menu_order_book.png;content/bts_spark/Trading/top_algo_control.png;content/bts_spark/Trading/trigger_algo_control.png;content/bts_spark/_index.md;content/release_notes/_index.md;content/system_requirements/_index.md;static/book.js;static/favicon.ico;static/image.js;templates/index.html;templates/page.html;templates/section.html;templates/shortcodes/back_button.html) \ No newline at end of file +set(sources /home/ppinto/Dropbox/bts/spark/doc/user/build.sh;content/_index.md;content/bts_spark/Glossary/_index.md;content/bts_spark/Help/_index.md;content/bts_spark/Help/about_spark.png;content/bts_spark/Help/image39.png;content/bts_spark/Help/image58.png;content/bts_spark/Launcher/_index.md;content/bts_spark/Launcher/image38.png;content/bts_spark/Launcher/image68.png;content/bts_spark/Logging-In-to-the-System/_index.md;content/bts_spark/Logging-In-to-the-System/image3.png;content/bts_spark/Logging-In-to-the-System/image64.png;content/bts_spark/Market/_index.md;content/bts_spark/Market/change_securities.png;content/bts_spark/Market/image11.png;content/bts_spark/Market/image14.png;content/bts_spark/Market/image20.png;content/bts_spark/Market/image21.png;content/bts_spark/Market/image23.png;content/bts_spark/Market/image24.png;content/bts_spark/Market/image34.png;content/bts_spark/Market/image49.png;content/bts_spark/Market/image53.png;content/bts_spark/Market/image66.png;content/bts_spark/Market/image69.png;content/bts_spark/Market/market_view.png;content/bts_spark/Market/settings_icon.png;content/bts_spark/Market/time_and_sales.png;content/bts_spark/My-Windows/_index.md;content/bts_spark/My-Windows/image15.png;content/bts_spark/My-Windows/image16.png;content/bts_spark/My-Windows/image37.png;content/bts_spark/Settings/_index.md;content/bts_spark/Settings/image51.png;content/bts_spark/Settings/image9.png;content/bts_spark/Spreader/_index.md;content/bts_spark/Spreader/image10.png;content/bts_spark/Spreader/image13.png;content/bts_spark/Spreader/image18.png;content/bts_spark/Spreader/image19.png;content/bts_spark/Spreader/image28.png;content/bts_spark/Spreader/image29.png;content/bts_spark/Spreader/image31.png;content/bts_spark/Spreader/image35.png;content/bts_spark/Spreader/image40.png;content/bts_spark/Spreader/image43.png;content/bts_spark/Spreader/image54.png;content/bts_spark/Spreader/image55.png;content/bts_spark/Spreader/image56.png;content/bts_spark/Spreader/image6.png;content/bts_spark/Spreader/image7.png;content/bts_spark/Spreader/image70.png;content/bts_spark/Spreader/image72.png;content/bts_spark/Spreader/image8.png;content/bts_spark/Trading/OrderTypes/index.md;content/bts_spark/Trading/_index.md;content/bts_spark/Trading/algo_studio.png;content/bts_spark/Trading/collapse_all_icon.png;content/bts_spark/Trading/cover_algo_control.png;content/bts_spark/Trading/expand_all_icon.png;content/bts_spark/Trading/image1.png;content/bts_spark/Trading/image17.png;content/bts_spark/Trading/image2.png;content/bts_spark/Trading/image25.png;content/bts_spark/Trading/image27.png;content/bts_spark/Trading/image33.png;content/bts_spark/Trading/image42.png;content/bts_spark/Trading/image44.png;content/bts_spark/Trading/image47.png;content/bts_spark/Trading/image48.png;content/bts_spark/Trading/image5.png;content/bts_spark/Trading/image50.png;content/bts_spark/Trading/image52.png;content/bts_spark/Trading/image57.png;content/bts_spark/Trading/image59.png;content/bts_spark/Trading/image60.png;content/bts_spark/Trading/image61.png;content/bts_spark/Trading/image62.png;content/bts_spark/Trading/image73.png;content/bts_spark/Trading/image74.png;content/bts_spark/Trading/join_algo_control.png;content/bts_spark/Trading/ladder.png;content/bts_spark/Trading/ladder_badges.png;content/bts_spark/Trading/ladder_main_part.png;content/bts_spark/Trading/lean_algo_control.png;content/bts_spark/Trading/mini_orderbook.png;content/bts_spark/Trading/oco_algo_control.png;content/bts_spark/Trading/offset_algo_control.png;content/bts_spark/Trading/pnl_calculation.png;content/bts_spark/Trading/pnl_window.png;content/bts_spark/Trading/recreate_icon.png;content/bts_spark/Trading/settings_icon.png;content/bts_spark/Trading/spark_menu_order_book.png;content/bts_spark/Trading/top_algo_control.png;content/bts_spark/Trading/trigger_algo_control.png;content/bts_spark/_index.md;content/release_notes/_index.md;content/system_requirements/_index.md;static/book.js;static/favicon.ico;static/image.js;templates/index.html;templates/page.html;templates/section.html;templates/shortcodes/back_button.html) \ No newline at end of file diff --git a/doc/user/content/release_notes/_index.md b/doc/user/content/release_notes/_index.md index 0d865e921..073897876 100644 --- a/doc/user/content/release_notes/_index.md +++ b/doc/user/content/release_notes/_index.md @@ -4,6 +4,37 @@ weight = 3 sort_by = "weight" insert_anchor_links = "right" +++ +## 2.9.0 +### User Visible Changes + - Improved Workspace management User Interface + - Added the ability to change grid background colors + - The Spark UI Window group now has a title in the Windows task bar + - Added sound alerts for trade events, see Launcher -> Settings -> Alerts + - Allowed composite algos that are attached to existing orders to identify fills that were already present prior to attachment + - Added import facilities for GMI position files + - Added the ability for risk managers to enter manual fills + - Added "raw" P&L columns, that is columns that *do not* include spread legs in their accounting +### Bug Fixes + - Fixed the settings action menu (Load/Save/Reset to Factory) + - Fixed the method used to determine if a security is expired to take into account the start of day time +### API Changes + - Fixed an issue in the Client SDK that could lead to occasional failures to connect to the spark server as well as excessive memory usage. + - Added the ability for algo orders to distinguish fills existing prior to launch +### Internal Changes + - Changed how we packaged certain third-party components + - Added logging of entitlement status + - Tagged outgoing orders with sequence number of marketdata or FIX event that triggered them +## 2.8.3 +### Bug Fixes + - Fixed a bug that could cause an OrderBook's Security column filter to be blank + - Fixed a bug that could cause a UI failure when clearing a filter selection on a filtering widget +## 2.8.2 +### Bug Fixes + - Added proper display factors for SOM spreads + - Fixed a bug that could cause a UI failure when trying to change settings in a synthetic ladder +## 2.8.1 +### Bug fixes + - Fixed an error in the UI grids filtering widget ## 2.8.0 ### User Visible Changes - Added ProRata and Scratch algos diff --git a/src/client/spark/App.xaml.cs b/src/client/spark/App.xaml.cs index ea2b1de78..0883bc861 100644 --- a/src/client/spark/App.xaml.cs +++ b/src/client/spark/App.xaml.cs @@ -70,7 +70,6 @@ namespace mbts.ui.shell { Log.Gen.info("Application.OnExit"); if (_runtime != null) _runtime.Dispose(); - utils.BTSNativeMethods.finit_spark_lib(); base.OnExit(e); Environment.Exit(0); } diff --git a/src/client/spark/Daemons/GlobalAlertsDaemon.cs b/src/client/spark/Daemons/GlobalAlertsDaemon.cs new file mode 100644 index 000000000..7cc191ea9 --- /dev/null +++ b/src/client/spark/Daemons/GlobalAlertsDaemon.cs @@ -0,0 +1,68 @@ +using bts.api; +using bts.shell; +using mbts; +using mbts.persist; +using mbts.spark; +using mbts.ui; +using mbts.ui.spark; +using mbts.ui.utilities; +using System; +using System.ComponentModel.Composition; +using System.Linq; +using System.Reactive.Disposables; +using System.Reactive.Linq; +using System.Windows; +using Disposable = mbts.Disposable; + +namespace bts { + [Export(typeof(IShellDaemon))] + class GlobalAlertsDaemon : IShellDaemon, IDisposable { + const string + // used for logging/diagnostic purposes + _window_name = "Global Alerts Dialog", + // actual window title + _window_title = "Alerts"; + + readonly ThreadedWindowLauncher _launcher; + readonly UserSettingsHub _hub; + readonly IBus _bus; + readonly INotificationService _notification_service; + IDisposable _dispose; + + [ImportingConstructor] + public GlobalAlertsDaemon(IBus bus, UserSettingsHub hub, INotificationService notification_service) { + _bus = bus; + _hub = hub; + _notification_service = notification_service; + _launcher = new ThreadedWindowLauncher(_window_name); + _dispose = Disposable.Create( + bus.subscribe(SparkTags.ShowGlobalAlertsTag) + .Subscribe(_ => _launcher.show(_view_factory)), + _hub.subscribe_default_settings() + .SubscribeEach(s => new CompositeDisposable( + s.alerts.trade_alerts.Where(t => t.is_enabled).Select( + a => _notification_service.link_to_trades( + a.predicate.security_criteria, a.predicate.create(), a.sound))))); + } + + Window _view_factory() { + var vm = new GlobalAlertsVM(); + vm.load_from(_hub.current.alerts); + vm.request_commit += commit; + var sec_sub = _bus.subscribe(SparkTags.SecuritiesTag).OnDispatcher().Subscribe(m => vm.security_map = m); + var win = new GlobalAlertsDialog { + Title = "Alerts", + DataContext = vm + }; + win.Closed += (s, e) => Disposable.dispose(ref sec_sub); + return win; + void commit() { + var s = _hub.current.with(alerts: vm.create()); + _hub.save_default(s).ignore(); + } + } + + public void Dispose() => + Disposable.dispose(ref _dispose); + } +} \ No newline at end of file diff --git a/src/client/spark/Images/Icons.cs b/src/client/spark/Images/Icons.cs index 3cc17164d..6e7087128 100644 --- a/src/client/spark/Images/Icons.cs +++ b/src/client/spark/Images/Icons.cs @@ -40,6 +40,7 @@ namespace mbts.ui { EditDocumentAltGeometryKey = nameof(EditDocumentAltGeometryKey), EditDocumentGeometryKey = nameof(EditDocumentGeometryKey), GearGeometryKey = nameof(GearGeometryKey), + HornGeometryKey = nameof(HornGeometryKey), LetterAKey = nameof(LetterAKey), LetterDKey = nameof(LetterDKey), OrdersGeometryKey = nameof(OrdersGeometryKey), @@ -48,6 +49,7 @@ namespace mbts.ui { PlayGeometryKey = nameof(PlayGeometryKey), ProhibitedGeometryKey = nameof(ProhibitedGeometryKey), RubberStampGeometryKey = nameof(RubberStampGeometryKey), + SpeakerGeometryKey = nameof(SpeakerGeometryKey), StarGeometryKey = nameof(StarGeometryKey), ToolsGeometryKey = nameof(ToolsGeometryKey), TrashGeometryKey = nameof(TrashGeometryKey), @@ -168,6 +170,7 @@ namespace mbts.ui { public static ImageSource FilterActive { get { return load_resource_and_freeze("FilterActive"); } } public static ImageSource FilterError { get { return load_resource_and_freeze("FilterError"); } } public static ImageSource Gear => create_icon(GearGeometryKey); + public static ImageSource Horn => create_icon(HornGeometryKey); public static ImageSource Import => load_resource_and_freeze("Import"); public static ImageSource Knight { get { return load_resource_and_freeze("Knight"); } } public static ImageSource Payment => create_icon(PaymentGeometryKey); @@ -176,6 +179,7 @@ namespace mbts.ui { public static ImageSource RubberStamp => create_icon(RubberStampGeometryKey); public static ImageSource RedoCircular => create_icon(UndoCircularGeometryKey, Brushes.Black, null, new ScaleTransform(-1, 1)); public static ImageSource Spawn { get { return load_resource_and_freeze("Spawn"); } } + public static ImageSource Speaker => create_icon(SpeakerGeometryKey); public static ImageSource Star => create_icon(StarGeometryKey); public static ImageSource Target { get { return load_resource_and_freeze("Target"); } } public static ImageSource Tetris { get { return load_resource_and_freeze("Tetris"); } } diff --git a/src/client/spark/Images/Vector/Icons.xaml b/src/client/spark/Images/Vector/Icons.xaml index a0ce61363..e44d4267a 100644 --- a/src/client/spark/Images/Vector/Icons.xaml +++ b/src/client/spark/Images/Vector/Icons.xaml @@ -274,6 +274,10 @@ M16.000015,9.6000061C12.500012,9.6000061 9.5999844,12.5 9.5999844,16 9.5999844,19.5 12.500012,22.399994 16.000015,22.399994 19.500019,22.399994 22.400015,19.5 22.400015,16 22.400015,12.5 19.500019,9.6000061 16.000015,9.6000061z M14.199995,0L17.800005,0C18.199999,0,18.599993,0.29998779,18.599993,0.79998779L18.599993,4.7999878C19.900013,5.1000061,21.000019,5.6000061,22.099997,6.2999878L25.000023,3.3999939C25.099999,3.2999878 25.300011,3.1999817 25.500025,3.1999817 25.700007,3.1999817 25.900019,3.2999878 26.000025,3.3999939L28.600033,6C28.900021,6.2999878,28.900021,6.7999878,28.600033,7.1000061L25.700007,10C26.400019,11.100006,26.900019,12.299988,27.200007,13.5L31.20001,13.5C31.600037,13.5,31.999998,13.799988,31.999998,14.299988L31.999998,17.899994C31.999998,18.299988,31.700012,18.699982,31.20001,18.699982L27.200007,18.699982C26.900019,20,26.400019,21.100006,25.700007,22.199982L28.600033,25.100006C28.700009,25.199982 28.799984,25.399994 28.799984,25.600006 28.799984,25.799988 28.700009,26 28.600033,26.100006L26.000025,28.600006C25.900019,28.699982 25.700007,28.799988 25.500025,28.799988 25.300011,28.799988 25.099999,28.699982 25.000023,28.600006L22.099997,25.699982C21.000019,26.399994,19.800007,26.899994,18.599993,27.199982L18.599993,31.199982C18.599993,31.600006,18.300005,32,17.800005,32L14.199995,32C13.8,32,13.400006,31.699982,13.400006,31.199982L13.400006,27.199982C12.099987,26.899994,11.000011,26.399994,9.9000027,25.699982L7.0000064,28.600006C6.9000003,28.699982 6.6999881,28.799988 6.5000059,28.799988 6.2999937,28.799988 6.099981,28.699982 6.0000054,28.600006L3.3999967,26C3.0999784,25.699982,3.0999784,25.199982,3.3999967,24.899994L6.2999937,22C5.5999805,20.899994,5.0999801,19.699982,4.7999923,18.5L0.79998828,18.5C0.39999409,18.5,-1.933513E-07,18.199982,0,17.699982L0,14.100006C-1.933513E-07,13.699982,0.2999879,13.299988,0.79998828,13.299988L4.7999923,13.299988C5.0999801,12,5.5999805,10.899994,6.2999937,9.7999878L3.3999967,7C3.2999908,6.8999939 3.1999845,6.6999817 3.1999845,6.5 3.1999845,6.2999878 3.2999908,6.1000061 3.3999967,6L6.0000054,3.3999939C6.099981,3.2999878 6.2999937,3.1999817 6.5000059,3.1999817 6.6999881,3.1999817 6.9000003,3.2999878 7.0000064,3.3999939L9.9000027,6.2999878C11.000011,5.6000061,12.199993,5.1000061,13.400006,4.7999878L13.400006,0.79998779C13.400006,0.29998779,13.8,0,14.199995,0z + + M4.0499969,7.0870913C2.9519978,7.0870913 2.0249984,8.4780906 2.0249987,10.125091 2.0249984,11.771091 2.9519978,13.162091 4.0499969,13.162091 5.1479961,13.162091 6.0749954,11.771091 6.0749955,10.125091 6.0749954,8.4780906 5.1479961,7.0870913 4.0499969,7.0870913z M29.975016,3.316155C23.883831,8.2016557,11.829888,8.9691842,8.1184387,9.0896781L8.0151367,9.0928462 8.0175586,9.1059306C8.0716033,9.4352315 8.0999939,9.7760909 8.0999942,10.125091 8.0999939,10.473966 8.0716033,10.814716 8.0175586,11.143921L8.0098858,11.185364 8.193316,11.195448C12.163719,11.420399,23.994581,12.458921,29.975016,17.005057z M31.176758,0.00019051835C31.280359,-0.0022594665 31.391857,0.01889403 31.514975,0.065146674 31.908983,0.21413228 31.999985,0.59114302 31.999985,1.0121596L31.999985,19.236969C31.999985,19.653958 31.913988,20.028956 31.525991,20.179955 31.405998,20.226952 31.450981,20.250023 31.32699,20.250023 31.050991,20.250023 30.778992,20.13601 30.583986,19.925928 25.229074,14.140572 9.6001122,13.269665 7.2865356,13.172559L7.2813506,13.172345 7.1739591,13.343056C6.4304952,14.468533 5.3060585,15.187091 4.0499969,15.187091 1.8169985,15.187091 0,12.916091 0,10.125091 0,7.3330911 1.8169985,5.0620912 4.0499969,5.0620912 5.3060585,5.0620912 6.4304952,5.78065 7.1739591,6.9063042L7.2852713,7.0832756 7.3087114,7.0828188C9.6077337,7.036146 25.141474,6.4939421 30.567997,0.34315929 30.75913,0.12521249 30.948835,0.005580858 31.176758,0.00019051835z + + M50.14375,33.389126C50.416863,33.394688,50.653496,33.587773,50.705998,33.864617L50.015995,45.114388C49.810295,46.291462,49.130394,45.097489,49.130394,45.097489L47.65659,42.640538C42.747776,49.635197 34.633152,54.221001 25.433924,54.221001 14.884193,54.221001 5.7437552,48.204125 1.2449617,39.419205 0.80087067,38.552021 0.21358696,37.705738 0.052086769,36.736958 -0.49735315,33.490925 3.4507786,32.901036 4.8049124,34.432404 5.3674143,35.067692 6.034036,36.16407 6.5079177,37.026051 10.106778,43.799416 17.2321,48.413819 25.433924,48.413819 32.656544,48.413819 39.026364,44.826695 42.903977,39.351506L40.229367,38.854115C40.229367,38.854115,38.677464,38.647118,39.742466,37.996033L50.024895,33.398526C50.064972,33.391365,50.104733,33.388333,50.14375,33.389126z M23.030291,9.0310004L27.889918,9.0310004 27.889918,13.028299C30.539786,13.205498,32.324932,13.958096,33.464161,14.528294L33.964176,14.775793 32.417435,20.038878 31.706417,19.68068C30.579987,19.11958 28.839241,18.244583 26.149073,18.244583 24.006916,18.244583 22.723183,19.122181 22.723183,20.590876 22.723183,21.894272 23.943314,22.804369 27.551308,24.187165 32.706444,26.124659 34.911,28.702853 34.911,32.817341 34.911,36.892929 32.090527,40.05172 27.673611,40.989117L27.673611,45.540001 22.767485,45.540001 22.767485,41.330414C20.252919,41.155815,17.695551,40.424118,16.101811,39.404721L15.683999,39.137619 17.221739,33.731438 17.969059,34.180634C19.088988,34.851235 21.402748,35.97873 24.340325,35.97873 26.734886,35.97873 28.276728,34.921635 28.276728,33.291336 28.276728,31.986641 27.547409,30.853846 23.986016,29.57255 20.083815,28.144253 16.095511,26.04406 16.095511,21.239174 16.095511,17.280985 18.737379,14.291395 23.030291,13.329098z M25.268765,0C35.819447,2.7347414E-07 44.961449,6.0155495 49.458798,14.804573 49.902695,15.669173 50.490094,16.516972 50.651287,17.48427 51.198281,20.734265 47.255726,21.318962 45.901341,19.789065 45.336346,19.153567 44.669552,18.051968 44.19566,17.19517 40.5957,10.421881 33.473275,5.8072398 25.268765,5.8072393 18.047545,5.8072398 11.678013,9.3918336 7.8003562,14.867073L10.474627,15.367072C10.474627,15.367072,12.026909,15.574172,10.962921,16.223872L0.67933281,20.826762C0.36037083,20.880062,0.057029126,20.669165,0.00099972012,20.354063L0.69093342,9.1118043C0.89552094,7.9296158,1.5738537,9.124924,1.5738539,9.124924L3.0452772,11.583379C7.9566038,4.584462,16.073666,2.7347414E-07,25.268765,0z @@ -327,6 +331,11 @@ M33.5,4h-6v10h6V4z M31.5,12h-2V6h2V12z + + + M18,12.839666L18,19.159448 18.029955,19.131576C18.848108,18.314159 19.332083,17.192702 19.332083,15.999873 19.332083,14.806357 18.848108,13.684685 18.029955,12.867527z M21.738388,4.748316C21.868353,4.7515661 21.999973,4.7804414 22.126221,4.8376924 26.511095,6.8177201 29.344013,11.199782 29.344013,15.999849 29.344013,20.799915 26.511095,25.181977 22.126221,27.162004 21.992224,27.223004 21.853228,27.251006 21.715233,27.251006 21.333244,27.251006 20.970255,27.032003 20.803259,26.662997 20.575266,26.15899 20.799259,25.566981 21.302244,25.339978 24.973139,23.681956 27.34407,20.015905 27.34407,15.999849 27.34407,11.983792 24.973139,8.3177406 21.302244,6.6597177 20.799259,6.4327146 20.575266,5.8407065 20.803259,5.3366992 20.973503,4.9594442 21.348494,4.7385656 21.738388,4.748316z M16,3.1854247L6.6530151,11.257323C6.4719848,11.414303,6.2399902,11.500301,6,11.500301L1.9999998,11.500301 1.9999998,21.500148 6,21.500148C6.2219848,21.500148,6.4370117,21.573144,6.6110229,21.709129L16,28.964023z M17.000252,1.186163E-08C17.141373,-2.1778678E-05 17.283264,0.02997845 17.416016,0.091477852 17.770996,0.25450047 18,0.60843792 18,1.0004608L18,10.368904 18.170259,10.465408C20.111607,11.612996 21.331976,13.716092 21.331976,15.999873 21.331976,18.282748 20.111607,20.38576 18.170259,21.53334L18,21.629842 18,31.000003C18,31.382016 17.78302,31.728995 17.440002,31.896962 17.301025,31.965991 17.150024,31.99999 17,31.99999 16.78302,31.99999 16.567017,31.929982 16.388977,31.791007L5.65802,23.500117 0.99999966,23.500117C0.44702145,23.500117,-3.9348969E-08,23.053104,3.5527137E-15,22.500133L3.5527137E-15,10.500316C-3.9348969E-08,9.9473459,0.44702145,9.5003311,0.99999966,9.5003311L5.6279907,9.5003311 16.346985,0.24345344C16.531998,0.083467316,16.765053,3.6392792E-05,17.000252,1.186163E-08z + + M1540.22,2082.07L1546.95,2102.78 1568.73,2102.78 1551.11,2115.58 1557.84,2136.29 1540.22,2123.49 1522.6,2136.29 1529.33,2115.58 1511.71,2102.78 1533.49,2102.78 1540.22,2082.07z diff --git a/src/client/spark/Modules/Market/MarketView/MarketViewRow.cs b/src/client/spark/Modules/Market/MarketView/MarketViewRow.cs index 50181fa6e..c214902cd 100644 --- a/src/client/spark/Modules/Market/MarketView/MarketViewRow.cs +++ b/src/client/spark/Modules/Market/MarketView/MarketViewRow.cs @@ -37,6 +37,7 @@ namespace mbts.spark { readonly MyOrderSummarizer _order_summarizer = new MyOrderSummarizer(); readonly MDDisplayHelper.SizeFormatter _size_formatter = MDDisplayHelper.get_formatter(MarketSizeDisplay.get_default()); Property _bid_direct_size, _bid_merged_size, _ask_direct_size, _ask_merged_size; + double _raw_day_position; string _bid_merged_size_text, _ask_merged_size_text; ulong _bbo_ref, _last_ref; @@ -91,6 +92,7 @@ namespace mbts.spark { _dispose = Disposable.Create( _orders_model.on_security_root_orders(security.Id).OnDispatcher().Subscribe(_on_orders), _position.OnDispatcher().Subscribe(v => _animation.animate_position(v.newval > v.oldval)), + _orders_model.on_security_positions(security.Id).OnDispatcher().Subscribe(t => Set(ref _raw_day_position, t.state.Values.Sum(p => p.Item2.raw_day_pos()), nameof(raw_day_position))), _inside_bids.OnDispatcher().Subscribe(v => _animation.animate_working_bids_inside(v.newval)), _inside_asks.OnDispatcher().Subscribe(v => _animation.animate_working_asks_inside(v.newval)), _day_change.OnDispatcher().Subscribe(v => _animation.animate_day_change(v.newval > v.oldval)), @@ -104,6 +106,7 @@ namespace mbts.spark { public bool inside_bids => _inside_bids.value; public bool inside_asks => _inside_asks.value; public int position => _position.value; + public double raw_day_position => _raw_day_position; public double last_price => _last_price.Value; public bool last_price_from_spread => _last_price_from_spread.Value; public uint last_size => _last_size.value; diff --git a/src/client/spark/Modules/Market/MarketView/MarketViewVM.cs b/src/client/spark/Modules/Market/MarketView/MarketViewVM.cs index 57a6793cf..d2c8ca2b5 100644 --- a/src/client/spark/Modules/Market/MarketView/MarketViewVM.cs +++ b/src/client/spark/Modules/Market/MarketView/MarketViewVM.cs @@ -10,7 +10,6 @@ using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Reactive.Linq; -using System.Reactive.Subjects; using System.Windows.Data; using System.Windows.Media; using static bts.shell.MarketViewWindowFactory; @@ -43,12 +42,13 @@ namespace mbts.spark { add_col(Columns.Last, "Last", r => r.format_price(r.last_price), nameof(Row.last_price), DefaultColumnWidths.Price, last_price_filter, DataGridFilterKnownTemplates.DoubleMinMax); add_col(Columns.LastSize, merged_size_col_header, r => r.last_size.ToString(), nameof(Row.last_size), DefaultColumnWidths.Size, last_size_filter, DataGridFilterKnownTemplates.UIntMinMax, h_alignment: System.Windows.HorizontalAlignment.Right); add_col(Columns.Position, "Pos", r => r.position.ToString(), nameof(Row.position), DefaultColumnWidths.Size, position_filter, DataGridFilterKnownTemplates.IntMinMax, true, h_alignment: System.Windows.HorizontalAlignment.Right); + add_col(Columns.RawDayPosition, "Raw Day Pos", r => SparkUtils.fmt_position(r.raw_day_position), nameof(Row.raw_day_position), DefaultColumnWidths.Size, raw_day_position_filter, DataGridFilterKnownTemplates.DoubleMinMax, true, h_alignment: System.Windows.HorizontalAlignment.Right); add_hl_working_orders_col(Columns.WorkingBids, working_col_header, nameof(Row.working_bids_text), nameof(Row.working_best_bid), nameof(Row.working_bids_click), working_bid_volume_filter, BTSColors.MarketData.bid, System.Windows.HorizontalAlignment.Right); add_col(Columns.BidImpliedSize, implied_size_col_header, r => r.bid_implied_size.ToString(), nameof(Row.bid_implied_size), DefaultColumnWidths.ImpliedDirectMergedSize, bid_implied_size_filter, DataGridFilterKnownTemplates.UIntMinMax, font_color: BTSColors.MarketData.bid, h_alignment: System.Windows.HorizontalAlignment.Right); add_col(Columns.BidDirectSize, direct_size_col_header, r => r.bid_direct_size.ToString(), nameof(Row.bid_direct_size), DefaultColumnWidths.ImpliedDirectMergedSize, bid_direct_size_filter, DataGridFilterKnownTemplates.UIntMinMax, font_color: BTSColors.MarketData.bid, h_alignment: System.Windows.HorizontalAlignment.Right); add_hl_merged_size_col(Columns.BidMergedSize, merged_size_col_header, nameof(Row.bid_merged_size), nameof(Row.bid_merged_size_text), nameof(Row.bid_merged_size_click), bid_merged_size_filter, BTSColors.MarketData.bid, h_alignment: System.Windows.HorizontalAlignment.Right); - add_hl_price_col(Columns.Bid, "Bid", nameof(Row.bid_price), nameof(Row.bid_price_click), bid_price_filter, BTSColors.header_text, BTSColors.MarketData.bid_brush, h_alignment: System.Windows.HorizontalAlignment.Right); - add_hl_price_col(Columns.Ask, "Ask", nameof(Row.ask_price), nameof(Row.ask_price_click), ask_price_filter, BTSColors.header_text, BTSColors.MarketData.ask_brush, h_alignment: System.Windows.HorizontalAlignment.Right); + add_hl_price_col(Columns.Bid, "Bid", nameof(Row.bid_price), nameof(Row.bid_price_click), bid_price_filter, BTSColors.header_text, BTSColors.MarketData.bid, h_alignment: System.Windows.HorizontalAlignment.Right); + add_hl_price_col(Columns.Ask, "Ask", nameof(Row.ask_price), nameof(Row.ask_price_click), ask_price_filter, BTSColors.header_text, BTSColors.MarketData.ask, h_alignment: System.Windows.HorizontalAlignment.Right); add_hl_merged_size_col(Columns.AskMergedSize, merged_size_col_header, nameof(Row.ask_merged_size), nameof(Row.ask_merged_size_text), nameof(Row.ask_merged_size_click), ask_merged_size_filter, BTSColors.MarketData.ask, h_alignment: System.Windows.HorizontalAlignment.Right); add_col(Columns.AskDirectSize, direct_size_col_header, r => r.ask_direct_size.ToString(), nameof(Row.ask_direct_size), DefaultColumnWidths.ImpliedDirectMergedSize, ask_direct_size_filter, DataGridFilterKnownTemplates.UIntMinMax, font_color: BTSColors.MarketData.ask, h_alignment: System.Windows.HorizontalAlignment.Right); add_col(Columns.AskImpliedSize, implied_size_col_header, r => r.ask_implied_size.ToString(), nameof(Row.ask_implied_size), DefaultColumnWidths.ImpliedDirectMergedSize, ask_implied_size_filter, DataGridFilterKnownTemplates.UIntMinMax, font_color: BTSColors.MarketData.ask, h_alignment: System.Windows.HorizontalAlignment.Right); @@ -78,18 +78,18 @@ namespace mbts.spark { horizontal_alignment = h_alignment }); - void add_hl_price_col(Columns col_key, string header, string prop, string command_prop, IDataGridColumnFilter filter, Color foreground, Brush background, System.Windows.HorizontalAlignment h_alignment) => - add_hl_col(col_key, header, price_binding(prop), prop, command_prop, filter, DataGridFilterKnownTemplates.DoubleMinMax, DefaultColumnWidths.MViewPrice, foreground, background, DefaultSelectionHighlight, h_alignment); + void add_hl_price_col(Columns col_key, string header, string prop, string command_prop, IDataGridColumnFilter filter, Color foreground, Color? background, System.Windows.HorizontalAlignment h_alignment) => + add_hl_col(col_key, header, price_binding(prop), prop, command_prop, filter, DataGridFilterKnownTemplates.DoubleMinMax, DefaultColumnWidths.MViewPrice, foreground, background, h_alignment); void add_hl_merged_size_col(Columns col_key, string header, string sort_prop, string text_prop, string command_prop, IDataGridColumnFilter filter, Color foreground, System.Windows.HorizontalAlignment h_alignment) => - add_hl_col(col_key, header, new Binding(text_prop), sort_prop, command_prop, filter, DataGridFilterKnownTemplates.UIntMinMax, DefaultColumnWidths.ImpliedDirectMergedSize, foreground, null, null, h_alignment); + add_hl_col(col_key, header, new Binding(text_prop), sort_prop, command_prop, filter, DataGridFilterKnownTemplates.UIntMinMax, DefaultColumnWidths.ImpliedDirectMergedSize, foreground, null, h_alignment); void add_hl_working_orders_col(Columns col_key, string header, string text_prop, string sort_prop, string command_prop, IDataGridColumnFilter filter, Color foreground, System.Windows.HorizontalAlignment h_alignment) => - add_hl_col(col_key, header, new Binding(text_prop), sort_prop, command_prop, filter, DataGridFilterKnownTemplates.IntMinMax, working_orders_col_width, foreground, null, null, h_alignment); + add_hl_col(col_key, header, new Binding(text_prop), sort_prop, command_prop, filter, DataGridFilterKnownTemplates.IntMinMax, working_orders_col_width, foreground, null, h_alignment); void add_hl_col( Columns col_key, string header, BindingBase binding, string prop, string command_prop, - IDataGridColumnFilter filter, string filter_template, double width, Color foreground, Brush background, Brush background_overlay, System.Windows.HorizontalAlignment h_alignment = System.Windows.HorizontalAlignment.Left) => + IDataGridColumnFilter filter, string filter_template, double width, Color foreground, Color? background, System.Windows.HorizontalAlignment h_alignment = System.Windows.HorizontalAlignment.Left) => columns.Add(new HyperlinkElementColumn( col_key, binding, new Binding(command_prop), filter, filter_template) { // it's necessary to have this id populated for grid settings @@ -97,8 +97,7 @@ namespace mbts.spark { Header = header, sort_member_path = prop, font_color_override = foreground, - background = background, - background_overlay = background_overlay, + background_color_override = background, Width = DefaultColumnWidths.Price, horizontal_alignment = h_alignment }); @@ -112,14 +111,8 @@ namespace mbts.spark { // // make font-color immutable for Side, Size, (the font-color rule is hard-coded: MarketData.bid for Buy orders, MarketData.ask for Sells) var col_settings_flags = new Dictionary { - { Columns.Bid.ToString(), GridColumnSettingsFeatureFlags.ImmutableFontColor }, - { Columns.Ask.ToString(), GridColumnSettingsFeatureFlags.ImmutableFontColor }, - { Columns.BidImpliedSize.ToString(), GridColumnSettingsFeatureFlags.ImmutableFontColor }, - { Columns.BidDirectSize.ToString(), GridColumnSettingsFeatureFlags.ImmutableFontColor }, - { Columns.BidMergedSize.ToString(), GridColumnSettingsFeatureFlags.ImmutableFontColor }, - { Columns.AskImpliedSize.ToString(), GridColumnSettingsFeatureFlags.ImmutableFontColor }, - { Columns.AskDirectSize.ToString(), GridColumnSettingsFeatureFlags.ImmutableFontColor }, - { Columns.AskMergedSize.ToString(), GridColumnSettingsFeatureFlags.ImmutableFontColor }, + { Columns.Bid.ToString(), GridColumnSettingsFeatureFlags.ImmutableFontColor | GridColumnSettingsFeatureFlags.ImmutableBackgroundColor }, + { Columns.Ask.ToString(), GridColumnSettingsFeatureFlags.ImmutableFontColor | GridColumnSettingsFeatureFlags.ImmutableBackgroundColor}, { Columns.WorkingAsks.ToString(), GridColumnSettingsFeatureFlags.ImmutableFontColor }, { Columns.WorkingBids.ToString(), GridColumnSettingsFeatureFlags.ImmutableFontColor } }; @@ -153,6 +146,7 @@ namespace mbts.spark { session_low_filter.load_state(filters.session_low); session_high_filter.load_state(filters.session_high); position_filter.load_state(filters.position); + raw_day_position_filter.load_state(filters.raw_day_position); working_bid_volume_filter.load_state(filters.working_bid_volume); working_ask_volume_filter.load_state(filters.working_ask_volume); volume_filter.load_state(filters.volume); @@ -164,7 +158,7 @@ namespace mbts.spark { bid_price_filter, bid_merged_size_filter, bid_direct_size_filter, bid_implied_size_filter, ask_price_filter, ask_merged_size_filter, ask_direct_size_filter, ask_implied_size_filter, last_price_filter, last_price_from_spread_filter, last_size_filter, day_change_filter, settlement_filter, - session_low_filter, session_high_filter, position_filter, working_bid_volume_filter, working_ask_volume_filter, volume_filter, security_key_filter), + session_low_filter, session_high_filter, position_filter, raw_day_position_filter, working_bid_volume_filter, working_ask_volume_filter, volume_filter, security_key_filter), settings_manager.subscribe_current().OnDispatcher().Subscribe(grid_settings_manager), WorkLoop.Start("MarketView", _refresh) ); @@ -233,6 +227,7 @@ namespace mbts.spark { public IDataGridColumnPersistentFilter session_low_filter { get; } = new DoubleMinMax(r => r.session_low, nameof(Row.session_low)); public IDataGridColumnPersistentFilter session_high_filter { get; } = new DoubleMinMax(r => r.session_high, nameof(Row.session_high)); public IDataGridColumnPersistentFilter position_filter { get; } = new IntMinMax(r => r.position, nameof(Row.position)); + public IDataGridColumnPersistentFilter raw_day_position_filter { get; } = new DoubleMinMax(r => r.raw_day_position, nameof(Row.raw_day_position)); public IDataGridColumnPersistentFilter working_bid_volume_filter { get; } = new IntMinMax(r => r.working_bid_volume, nameof(Row.working_bid_volume)); public IDataGridColumnPersistentFilter working_ask_volume_filter { get; } = new IntMinMax(r => r.working_ask_volume, nameof(Row.working_ask_volume)); public IDataGridColumnPersistentFilter volume_filter { get; } = new ULongMinMax(r => r.volume, nameof(Row.volume)); @@ -265,6 +260,7 @@ namespace mbts.spark { session_low = session_low_filter.get_state(), session_high = session_high_filter.get_state(), position = position_filter.get_state(), + raw_day_position = raw_day_position_filter.get_state(), working_bid_volume = working_bid_volume_filter.get_state(), working_ask_volume = working_ask_volume_filter.get_state(), volume = volume_filter.get_state(), diff --git a/src/client/spark/Modules/Market/MarketView/MarketViewView.xaml.cs b/src/client/spark/Modules/Market/MarketView/MarketViewView.xaml.cs index 14dfdb626..e42bf0e89 100644 --- a/src/client/spark/Modules/Market/MarketView/MarketViewView.xaml.cs +++ b/src/client/spark/Modules/Market/MarketView/MarketViewView.xaml.cs @@ -8,8 +8,35 @@ namespace mbts.ui.spark { /// Interaction logic for MarketSheetsView.xaml /// partial class MarketViewView : UserControl { - public enum Columns { Security, Volume, Last, LastSpd, LastSize, Position, WorkingBids, Bid, BidMergedSize, BidDirectSize, BidImpliedSize, Ask, AskMergedSize, AskDirectSize, AskImpliedSize, WorkingAsks, Settlement, DayChange, SessionLow, SessionHigh, SecurityKey, - MarketDescription, ClearedAlias, ProductDescription, HubAlias, StripName, MicCode + public enum Columns { + Security, + Volume, + Last, + LastSpd, + LastSize, + Position, + RawDayPosition, + WorkingBids, + Bid, + BidMergedSize, + BidDirectSize, + BidImpliedSize, + Ask, + AskMergedSize, + AskDirectSize, + AskImpliedSize, + WorkingAsks, + Settlement, + DayChange, + SessionLow, + SessionHigh, + SecurityKey, + MarketDescription, + ClearedAlias, + ProductDescription, + HubAlias, + StripName, + MicCode } public MarketViewView() { diff --git a/src/client/spark/Modules/Market/MarketView/MarketViewWindow.cs b/src/client/spark/Modules/Market/MarketView/MarketViewWindow.cs index 35f22e89c..dede41acc 100644 --- a/src/client/spark/Modules/Market/MarketView/MarketViewWindow.cs +++ b/src/client/spark/Modules/Market/MarketView/MarketViewWindow.cs @@ -33,7 +33,7 @@ namespace mbts.spark { _selected_security_criteria_backing, _sorted_selected_security_criteria_backing; - public MarketViewWindow(MarketViewWindowFactory factory, IBus bus, OrdersModel orders_model, MarketViewWindowFactory.StartupData state, WindowSettingsManager settings_manager) + public MarketViewWindow(MarketViewWindowFactory factory, IBus bus, OrdersModel orders_model, MarketViewWindowFactory.StartupData state, IWindowSettingsManager settings_manager) : base("Market View", bus, factory, SparkCommands.ShowMarketView.icon) { _orders_model = orders_model; _state = state; diff --git a/src/client/spark/Modules/Market/MarketView/MarketViewWindowFactory.cs b/src/client/spark/Modules/Market/MarketView/MarketViewWindowFactory.cs index da1b23358..9f99abcd7 100644 --- a/src/client/spark/Modules/Market/MarketView/MarketViewWindowFactory.cs +++ b/src/client/spark/Modules/Market/MarketView/MarketViewWindowFactory.cs @@ -1,4 +1,6 @@ using bts.api; +using bts.proto; +using bts.settings; using mbts.persist; using mbts.spark; using mbts.spark.filters; @@ -7,6 +9,7 @@ using mbts.ui.settings; using ProtoBuf; using System; using System.Collections.Generic; +using System.ComponentModel.Composition; using System.Linq; using System.Reactive.Linq; using System.Threading.Tasks; @@ -35,13 +38,8 @@ namespace bts.shell { void _show(SecurityCriteria criteria) => _bus.post(SparkTags.ShowWindow, _create(StartupData.Default(criteria))); - IShellWindow _create(StartupData state) { - var settings_manager = new WindowSettingsManager(_settings_hub, - state.grid, - get: us => us.market_view_grid, - set: (us, gs) => us.with(provided_market_view_gridsettings: gs)); - return new MarketViewWindow(this, _bus, _orders_model, state, settings_manager); - } + IShellWindow _create(StartupData state) => + new MarketViewWindow(this, _bus, _orders_model, state, _settings_hub.create_mview_settings_manager(state.grid)); [ProtoContract] public class StartupData { @@ -68,7 +66,29 @@ namespace bts.shell { _sorting_criteria = sort; } - public static GridSettings DefaultGridSettings => GridSettings.Default.with(DefaultColumns.all, DefaultColumns.default_sorting_criterion); + public static GridSettings sanitize(GridSettings s) { + if (s == null) { + return null; + } + + // column upgrade + var columns = s.columns.ToList(); + var bid_col_index = columns.FindIndex(x => x.id == col_id(Columns.Bid)); + var ask_col_index = columns.FindIndex(x => x.id == col_id(Columns.Ask)); + if (bid_col_index > -1 && ask_col_index > -1) { + if (!columns[bid_col_index].background_color_override.HasValue) { + columns[bid_col_index] = columns[bid_col_index].with(background_color_override: new NullableColorProto(BTSColors.MarketData.bid.ToUint())); + } + if (!columns[ask_col_index].background_color_override.HasValue) { + columns[ask_col_index] = columns[ask_col_index].with(background_color_override: new NullableColorProto(BTSColors.MarketData.ask.ToUint())); + } + } + // and enable background color upgrade + return s.with(columns, enable_background_color: true); + } + + + public static GridSettings DefaultGridSettings => GridSettings.Default.with(DefaultColumns.all, DefaultColumns.default_sorting_criterion, enable_background_color: true); public IReadOnlyList sorting_criterion => _sorting_criteria ?? (_sorting_criteria = DefaultColumns.default_sorting_criterion.ToList()); @@ -83,12 +103,13 @@ namespace bts.shell { public static GridColumnSettings last_from_spread => new GridColumnSettings(col_id(Columns.LastSpd), DefaultColumnHeaders.S, width: DefaultColumnWidths.Price, is_visible: false); public static GridColumnSettings last_size => new GridColumnSettings(col_id(Columns.LastSize), DefaultColumnHeaders.Size, width: DefaultColumnWidths.Size, horizontal_alignment: System.Windows.HorizontalAlignment.Right); public static GridColumnSettings position => new GridColumnSettings(col_id(Columns.Position), DefaultColumnHeaders.Pos, width: DefaultColumnWidths.Size, horizontal_alignment: System.Windows.HorizontalAlignment.Right); + public static GridColumnSettings raw_day_position => new GridColumnSettings(col_id(Columns.RawDayPosition), DefaultColumnHeaders.RawDayPos, width: DefaultColumnWidths.Size, horizontal_alignment: System.Windows.HorizontalAlignment.Right, is_visible: false); public static GridColumnSettings working_bids => new GridColumnSettings(col_id(Columns.WorkingBids), DefaultColumnHeaders.Working, font_color_override: BTSColors.MarketData.bid, width: DefaultColumnWidths.Working, horizontal_alignment: System.Windows.HorizontalAlignment.Right); public static GridColumnSettings bid_implied_size => new GridColumnSettings(col_id(Columns.BidImpliedSize), DefaultColumnHeaders.ImpliedSize, font_color_override: BTSColors.MarketData.bid, width: DefaultColumnWidths.ImpliedDirectMergedSize, horizontal_alignment: System.Windows.HorizontalAlignment.Right); public static GridColumnSettings bid_direct_size => new GridColumnSettings(col_id(Columns.BidDirectSize), DefaultColumnHeaders.DirectSize, font_color_override: BTSColors.MarketData.bid, width: DefaultColumnWidths.ImpliedDirectMergedSize, horizontal_alignment: System.Windows.HorizontalAlignment.Right); public static GridColumnSettings bid_merged_size => new GridColumnSettings(col_id(Columns.BidMergedSize), DefaultColumnHeaders.MergedSize, font_color_override: BTSColors.MarketData.bid, width: DefaultColumnWidths.ImpliedDirectMergedSize, horizontal_alignment: System.Windows.HorizontalAlignment.Right); - public static GridColumnSettings bid => new GridColumnSettings(col_id(Columns.Bid), DefaultColumnHeaders.Bid, font_color_override: BTSColors.header_text, width:DefaultColumnWidths.MViewPrice, horizontal_alignment: System.Windows.HorizontalAlignment.Right); - public static GridColumnSettings ask => new GridColumnSettings(col_id(Columns.Ask), DefaultColumnHeaders.Ask, font_color_override: BTSColors.header_text, width: DefaultColumnWidths.MViewPrice, horizontal_alignment: System.Windows.HorizontalAlignment.Right); + public static GridColumnSettings bid => new GridColumnSettings(col_id(Columns.Bid), DefaultColumnHeaders.Bid, font_color_override: BTSColors.header_text, width:DefaultColumnWidths.MViewPrice, horizontal_alignment: System.Windows.HorizontalAlignment.Right, background_color_override: BTSColors.MarketData.bid); + public static GridColumnSettings ask => new GridColumnSettings(col_id(Columns.Ask), DefaultColumnHeaders.Ask, font_color_override: BTSColors.header_text, width: DefaultColumnWidths.MViewPrice, horizontal_alignment: System.Windows.HorizontalAlignment.Right, background_color_override: BTSColors.MarketData.ask); public static GridColumnSettings ask_merged_size => new GridColumnSettings(col_id(Columns.AskMergedSize), DefaultColumnHeaders.MergedSize, font_color_override: BTSColors.MarketData.ask, width: DefaultColumnWidths.ImpliedDirectMergedSize, horizontal_alignment: System.Windows.HorizontalAlignment.Right); public static GridColumnSettings ask_direct_size => new GridColumnSettings(col_id(Columns.AskDirectSize), DefaultColumnHeaders.DirectSize, font_color_override: BTSColors.MarketData.ask, width: DefaultColumnWidths.ImpliedDirectMergedSize, horizontal_alignment: System.Windows.HorizontalAlignment.Right); public static GridColumnSettings ask_implied_size => new GridColumnSettings(col_id(Columns.AskImpliedSize), DefaultColumnHeaders.ImpliedSize, font_color_override: BTSColors.MarketData.ask, width: DefaultColumnWidths.ImpliedDirectMergedSize, horizontal_alignment: System.Windows.HorizontalAlignment.Right); @@ -121,6 +142,7 @@ namespace bts.shell { yield return last_from_spread; yield return last_size; yield return position; + yield return raw_day_position; yield return working_bids; yield return bid_implied_size; yield return bid_direct_size; @@ -175,6 +197,8 @@ namespace bts.shell { public DoubleMinMaxState session_high; [ProtoMember(12)] public IntMinMaxState position; + [ProtoMember(27)] + public DoubleMinMaxState raw_day_position; [ProtoMember(13)] public IntMinMaxState working_bid_volume; [ProtoMember(14)] @@ -206,5 +230,46 @@ namespace bts.shell { [ProtoMember(27)] public string mic_code; } + + // migration rules + [Export(typeof(IMarketViewGridSettingsMigrationRule))] + class AddMissingColumnsMigrationRule : IMarketViewGridSettingsMigrationRule { + public bool apply(GridSettings settings, out GridSettings new_settings) => + settings.add_missing_columns(StartupData.DefaultGridSettings.columns, out new_settings); + } + + [Export(typeof(IMarketViewGridSettingsMigrationRule))] + class InitBidAskBackgroundOverrideMigrationRule : IMarketViewGridSettingsMigrationRule { + public bool apply(GridSettings s, out GridSettings new_settings) { + var columns = s.columns.ToList(); + var bid_col_index = columns.FindIndex(x => x.id == col_id(Columns.Bid)); + var ask_col_index = columns.FindIndex(x => x.id == col_id(Columns.Ask)); + bool applied = false; + if (bid_col_index > -1 && ask_col_index > -1) { + if (!columns[bid_col_index].background_color_override.HasValue) { + columns[bid_col_index] = columns[bid_col_index].with(background_color_override: new NullableColorProto(BTSColors.MarketData.bid.ToUint())); + applied = true; + } + if (!columns[ask_col_index].background_color_override.HasValue) { + columns[ask_col_index] = columns[ask_col_index].with(background_color_override: new NullableColorProto(BTSColors.MarketData.ask.ToUint())); + applied = true; + } + } + new_settings = applied ? s.with(columns) : default; + return applied; + } + } + + [Export(typeof(IMarketViewGridSettingsMigrationRule))] + class InitBackgroundOverrideMigrationRule : IMarketViewGridSettingsMigrationRule { + public bool apply(GridSettings settings, out GridSettings new_settings) { + if (!settings.enable_background_color) { + new_settings = settings.with(enable_background_color: true); + return true; + } + new_settings = default; + return false; + } + } } -} \ No newline at end of file +} diff --git a/src/client/spark/Modules/Market/TimeAndSales/TimeAndSalesModel.cs b/src/client/spark/Modules/Market/TimeAndSales/TimeAndSalesModel.cs index 2b17345e6..b0121b166 100644 --- a/src/client/spark/Modules/Market/TimeAndSales/TimeAndSalesModel.cs +++ b/src/client/spark/Modules/Market/TimeAndSales/TimeAndSalesModel.cs @@ -7,16 +7,6 @@ using System.Linq; using System.Reactive.Linq; namespace mbts.spark { - struct TradeInfo { - public readonly SecurityImmutable security; - public readonly TradeMessageImmutable trade; - - public TradeInfo(SecurityImmutable security, TradeMessageImmutable trade) { - this.security = security; - this.trade = trade; - } - } - class TimeAndSalesModel : IDisposable { internal readonly SecurityCriteria security_criteria; diff --git a/src/client/spark/Modules/Market/TimeAndSales/TimeAndSalesVM.cs b/src/client/spark/Modules/Market/TimeAndSales/TimeAndSalesVM.cs index 11a3a44ee..e2b5b8139 100644 --- a/src/client/spark/Modules/Market/TimeAndSales/TimeAndSalesVM.cs +++ b/src/client/spark/Modules/Market/TimeAndSales/TimeAndSalesVM.cs @@ -137,7 +137,7 @@ namespace mbts.spark { public Row(TimeAndSalesVM parent, SecurityImmutable security, TradeMessageImmutable trade) : base(security) { _trade_key = trade.Key; time = ((ulong)trade.Time).lldatetime().ToLocalTime(); - price = trade.Mantissa * Math.Pow(10, trade.Exponent); + price = trade.price(); size = trade.Size; aggressor_side = trade.AggressorSide; show_ladder = new DelegateCommand(() => parent._show_ladder_async(this).ignore()); diff --git a/src/client/spark/Modules/Settings/SettingsShellModule.cs b/src/client/spark/Modules/Settings/SettingsShellModule.cs index 005dd648b..ce995d025 100644 --- a/src/client/spark/Modules/Settings/SettingsShellModule.cs +++ b/src/client/spark/Modules/Settings/SettingsShellModule.cs @@ -26,7 +26,10 @@ namespace bts.ui { public SettingsShellModule(IBus bus, UserSettingsHub settings_hub, WorkspacesModel ws) { // todo: settings dialog ws_model = ws; - var actions_list = new List(2) { SparkCommands.ShowWorkspaces }; + var actions_list = new List(2) { + SparkCommands.ShowGlobalAlerts, + SparkCommands.ShowWorkspaces + }; if (Configuration.get().enabled) { actions_list.Add(SparkCommands.ShowSettings); } diff --git a/src/client/spark/Modules/Trading/Ladder/LadderSettings.cs b/src/client/spark/Modules/Trading/Ladder/LadderSettings.cs index a26f626ae..e1222b5af 100644 --- a/src/client/spark/Modules/Trading/Ladder/LadderSettings.cs +++ b/src/client/spark/Modules/Trading/Ladder/LadderSettings.cs @@ -450,6 +450,8 @@ namespace mbts.spark { public bool account_position_visible { get; set; } [ProtoMember(50), DefaultValue(true)] public bool day_position_visible { get; set; } + [ProtoMember(56)] + public bool raw_day_position_visible { get; set; } [ProtoMember(37), DefaultValue(true)] public bool size_presets_visible { get; set; } [ProtoMember(38), DefaultValue(true)] @@ -570,6 +572,7 @@ namespace mbts.spark { order_type_visible == other.order_type_visible && account_position_visible == other.account_position_visible && day_position_visible == other.day_position_visible && + raw_day_position_visible == other.raw_day_position_visible && size_presets_visible == other.size_presets_visible && cancel_buys_visible == other.cancel_buys_visible && cancel_sells_visible == other.cancel_sells_visible && diff --git a/src/client/spark/Modules/Trading/Ladder/VM/LadderSettingsVM.cs b/src/client/spark/Modules/Trading/Ladder/VM/LadderSettingsVM.cs index 97db476d3..d8ed5a405 100644 --- a/src/client/spark/Modules/Trading/Ladder/VM/LadderSettingsVM.cs +++ b/src/client/spark/Modules/Trading/Ladder/VM/LadderSettingsVM.cs @@ -72,6 +72,7 @@ namespace mbts.spark { _order_type_visibility = new OrderEntryPanelItemVisibilityVM("Order Type"), _account_position_visibility = new OrderEntryPanelItemVisibilityVM("Account Position"), _day_position_visibility = new OrderEntryPanelItemVisibilityVM("Day Position"), + _raw_day_position_visibility = new OrderEntryPanelItemVisibilityVM("Raw Day Position"), _size_preset_visibility = new OrderEntryPanelItemVisibilityVM("Size Presets"), _auto_clear_visibility = new OrderEntryPanelItemVisibilityVM("Auto Clear"), _clear_visibility = new OrderEntryPanelItemVisibilityVM("Clear"), @@ -137,7 +138,7 @@ namespace mbts.spark { c.PropertyChanged -= on_prop_changed; } AccountTuple? account_factory(KeyValuePair kv) => new AccountTuple(kv.Key, kv.Value); - AlgoDefinitionVM algo_factory(AlgoData data) => AlgoDefinitionVM.create(_model, data); + AlgoDefinitionVM algo_factory(AlgoData data) => AlgoDefinitionVM.create(_model, data, is_synthetic ? AlgoFormats.Spreader : AlgoFormats.Exchange); } public event Action result_changed; @@ -239,6 +240,7 @@ namespace mbts.spark { yield return _order_type_visibility; yield return _account_position_visibility; yield return _day_position_visibility; + yield return _raw_day_position_visibility; yield return _size_preset_visibility; yield return _clear_visibility; yield return _auto_clear_visibility; @@ -281,6 +283,7 @@ namespace mbts.spark { yield return _order_type_visibility; yield return _account_position_visibility; yield return _day_position_visibility; + yield return _raw_day_position_visibility; yield return _size_preset_visibility; yield return _auto_clear_visibility; yield return _clear_visibility; @@ -343,6 +346,7 @@ namespace mbts.spark { _order_type_visibility.show = settings.order_type_visible; _account_position_visibility.show = settings.account_position_visible; _day_position_visibility.show = settings.day_position_visible; + _raw_day_position_visibility.show = settings.raw_day_position_visible; _size_preset_visibility.show = settings.size_presets_visible; _clear_visibility.show = settings.clear_visible; _auto_clear_visibility.show = settings.auto_clear_visible; @@ -415,6 +419,7 @@ namespace mbts.spark { clone.order_type_visible = _order_type_visibility.show; clone.account_position_visible = _account_position_visibility.show; clone.day_position_visible = _day_position_visibility.show; + clone.raw_day_position_visible = _raw_day_position_visibility.show; clone.size_presets_visible = _size_preset_visibility.show; clone.auto_clear_visible = _auto_clear_visibility.show; clone.clear_visible = _clear_visibility.show; diff --git a/src/client/spark/Modules/Trading/Ladder/VM/LadderVM.cs b/src/client/spark/Modules/Trading/Ladder/VM/LadderVM.cs index c985e6381..2bd48534a 100644 --- a/src/client/spark/Modules/Trading/Ladder/VM/LadderVM.cs +++ b/src/client/spark/Modules/Trading/Ladder/VM/LadderVM.cs @@ -35,8 +35,8 @@ namespace mbts.spark { _show_cancel_sells, _show_cancel_all, _show_zoom; readonly PropertyAutoWrapper _bid_background, _bid_foreground, _ask_background, _ask_foreground; readonly HashSet _sticky_algos = new HashSet(); - double _position, _day_position; - bool _show_account_position, _show_day_position; + double _position, _day_position, _raw_day_position; + bool _show_account_position, _show_day_position, _show_raw_day_position; ulong _misc_time = 0; ulong _last_trade_time = 0; IEqualityComparer _equality; @@ -109,7 +109,10 @@ namespace mbts.spark { (_show_cancel_all = bool_prop(nameof(show_cancel_all), s => s.cancel_all_visible)).set, (_show_zoom = bool_prop(nameof(show_zoom), s => s.zoom_slider_visible)).set, s => { - if (Set(ref _show_account_position, s.account_position_visible, nameof(show_account_position)) | Set(ref _show_day_position, s.day_position_visible, nameof(show_day_position))) { + if ( + Set(ref _show_account_position, s.account_position_visible, nameof(show_account_position)) | + Set(ref _show_day_position, s.day_position_visible, nameof(show_day_position)) | + Set(ref _show_raw_day_position, s.raw_day_position_visible, nameof(show_raw_day_position))) { RaisePropertyChanged(nameof(position_display_text)); } size_calculator.buttons = s.calculator_buttons; @@ -232,14 +235,19 @@ namespace mbts.spark { }), order_properties_vm, position_src.OnDispatcher().Subscribe(pos => { - double total, day; + double total, day, raw_day; if (pos.HasValue) { total = pos.Value.total_pos(); day = pos.Value.day_pos(); + raw_day = pos.Value.raw_day_pos(); } else { - total = day = double.NaN; + total = day = raw_day = double.NaN; } - if ((Set(ref _position, total, nameof(position)) | Set(ref _day_position, day, nameof(day_position))) && (show_day_position || show_account_position)) + if (( + Set(ref _position, total, nameof(position)) | + Set(ref _day_position, day, nameof(day_position)) | + Set(ref _raw_day_position, raw_day, nameof(raw_day_position)) + ) && (show_day_position || show_account_position || show_raw_day_position)) RaisePropertyChanged(nameof(position_display_text)); })); } @@ -290,6 +298,7 @@ namespace mbts.spark { public double settlement => _settlement.Value; public double position => _position; public double day_position => _day_position; + public double raw_day_position => _raw_day_position; public ulong volume => _volume.Value; public bool auto_clear { get => _auto_clear.Value; set => _auto_clear.set(value); } public CalculatorLogic size_calculator { get; } = new CalculatorLogic(0); @@ -316,6 +325,7 @@ namespace mbts.spark { public bool show_order_type => _show_order_type.view; public bool show_account_position => _show_account_position; public bool show_day_position => _show_day_position; + public bool show_raw_day_position => _show_raw_day_position; public bool show_size_preset => _show_size_preset.view; public bool show_auto_clear => _show_auto_clear.view; public bool show_clear => _show_clear.view; @@ -327,12 +337,18 @@ namespace mbts.spark { public double zoom_factor { get => _zoom_factor.Value; set => _zoom_factor.set(value); } public string position_display_text { get { - if (show_day_position) { - string day_pos_text = SparkUtils.fmt_position(day_position); - return show_account_position ? $"{day_pos_text} ({SparkUtils.fmt_position(position)})" : day_pos_text; - } else if (show_account_position) { - return SparkUtils.fmt_position(position); - } else return string.Empty; + string rv = null; + if (show_day_position) + rv = SparkUtils.fmt_position(day_position); + if (show_raw_day_position) { + var raw_day_text = SparkUtils.fmt_position(raw_day_position); + rv = rv == null ? raw_day_text : $"{rv} / {raw_day_text}"; + } + if (show_account_position) { + var acc_text = SparkUtils.fmt_position(position); + rv = rv == null ? SparkUtils.fmt_position(position) : $"{rv} ({acc_text})"; + } + return rv ?? ""; } } //public double window_height { get => _window_height.Value; set => _window_height.set(value); } diff --git a/src/client/spark/Modules/Trading/Ladder/View/LadderView.xaml b/src/client/spark/Modules/Trading/Ladder/View/LadderView.xaml index f6dd7a8d4..5e041034f 100644 --- a/src/client/spark/Modules/Trading/Ladder/View/LadderView.xaml +++ b/src/client/spark/Modules/Trading/Ladder/View/LadderView.xaml @@ -476,6 +476,7 @@ + diff --git a/src/client/spark/Modules/Trading/PAndLWindowFactory.cs b/src/client/spark/Modules/Trading/PAndLWindowFactory.cs index 07aba2002..dbd65eab6 100644 --- a/src/client/spark/Modules/Trading/PAndLWindowFactory.cs +++ b/src/client/spark/Modules/Trading/PAndLWindowFactory.cs @@ -1,15 +1,15 @@ using bts.api; -using bts.api.orderman; +using bts.settings; using bts.spark; using mbts.persist; using mbts.spark; using mbts.spark.filters; using mbts.ui; using mbts.ui.settings; -using mbts.ui.spark; using ProtoBuf; using System; using System.Collections.Generic; +using System.ComponentModel.Composition; using System.Linq; using System.Reactive.Linq; using System.Threading.Tasks; @@ -40,13 +40,14 @@ namespace bts.shell { _bus.post(SparkTags.ShowWindow, w); } - IShellWindow _create(PAndLStartupData state) { - var settings_manager = new WindowSettingsManager(_settings_hub, - state.grid, - get: us => us.pnl_grid, - set: (us, gs) => us.with(provided_pnl_gridsettings: gs)); - return new PAndLWindow(_orders_model, this, _bus, state, SparkCommands.ShowPAndL.icon, settings_manager); - } + IShellWindow _create(PAndLStartupData state) => + new PAndLWindow( + _orders_model, + this, + _bus, + state, + SparkCommands.ShowPAndL.icon, + _settings_hub.create_pandl_settings_manager(state.grid)); [ProtoContract] public class PAndLStartupData { @@ -81,7 +82,7 @@ namespace bts.shell { [ProtoMember(25)] public GridSettings grid; - static class DefaultColumns { + public static class DefaultColumns { public static GridColumnSettings account => new GridColumnSettings(col_id(Columns.Account), DefaultColumnHeaders.Account, width: DefaultColumnWidths.Account); public static GridColumnSettings exchange => new GridColumnSettings(col_id(Columns.Exchange), DefaultColumnHeaders.Exchange, false, width: DefaultColumnWidths.Exchange); @@ -92,10 +93,12 @@ namespace bts.shell { public static GridColumnSettings start_pnl => new GridColumnSettings(col_id(Columns.StartPNL), DefaultColumnHeaders.StartPnL, false, width: DefaultColumnWidths.PnL_Generic, horizontal_alignment: System.Windows.HorizontalAlignment.Right); public static GridColumnSettings day_long_pnl => new GridColumnSettings(col_id(Columns.DayLong), DefaultColumnHeaders.DayLong, width: DefaultColumnWidths.PnL_Generic, horizontal_alignment: System.Windows.HorizontalAlignment.Right); public static GridColumnSettings day_short_pnl => new GridColumnSettings(col_id(Columns.DayShort), DefaultColumnHeaders.DayShort, width: DefaultColumnWidths.PnL_Generic, horizontal_alignment: System.Windows.HorizontalAlignment.Right); + public static GridColumnSettings raw_day_net_position => new GridColumnSettings(col_id(Columns.RawDayPos), DefaultColumnHeaders.RawDayPos, width: DefaultColumnWidths.PnL_Generic, horizontal_alignment: System.Windows.HorizontalAlignment.Right, is_visible: false); public static GridColumnSettings day_pnl => new GridColumnSettings(col_id(Columns.DayPNL), DefaultColumnHeaders.DayPnL, width: DefaultColumnWidths.PnL_Generic, horizontal_alignment: System.Windows.HorizontalAlignment.Right); public static GridColumnSettings total_net_pos => new GridColumnSettings(col_id(Columns.TotalNetPos), DefaultColumnHeaders.TotalNetPos, false, width: DefaultColumnWidths.Total_Net_Pos, horizontal_alignment: System.Windows.HorizontalAlignment.Right); public static GridColumnSettings total_pnl => new GridColumnSettings(col_id(Columns.TotalPNL), DefaultColumnHeaders.TotalPnL, false, width: DefaultColumnWidths.PnL_Generic, horizontal_alignment: System.Windows.HorizontalAlignment.Right); public static GridColumnSettings pnl_price => new GridColumnSettings(col_id(Columns.PNLPrice), DefaultColumnHeaders.PnLPrice, width: DefaultColumnWidths.PnL_Generic, horizontal_alignment: System.Windows.HorizontalAlignment.Right); + public static GridColumnSettings has_day_trades => new GridColumnSettings(col_id(Columns.TradedToday), "Traded Today", width: 100, horizontal_alignment: System.Windows.HorizontalAlignment.Center); public static IEnumerable default_sorting_criterion { get { @@ -117,10 +120,12 @@ namespace bts.shell { yield return start_pnl; yield return day_long_pnl; yield return day_short_pnl; + yield return raw_day_net_position; yield return day_pnl; yield return total_net_pos; yield return total_pnl; yield return pnl_price; + yield return has_day_trades; } } @@ -145,6 +150,8 @@ namespace bts.shell { public UIntMinMaxState day_long_pos; [ProtoMember(8)] public UIntMinMaxState day_short_pos; + [ProtoMember(16)] + public DoubleMinMaxState raw_day_net_pos; [ProtoMember(9)] public DoubleMinMaxState day_pnl; [ProtoMember(10)] @@ -162,7 +169,11 @@ namespace bts.shell { public bool[] has_day_trades; } + // migration rules + [Export(typeof(IPAndLGridSettingsMigrationRule))] + class AddMissingColumnsMigrationRule : IPAndLGridSettingsMigrationRule { + public bool apply(GridSettings settings, out GridSettings new_settings) => + settings.add_missing_columns(PAndLStartupData.DefaultGridSettings.columns, out new_settings); + } } - - } \ No newline at end of file diff --git a/src/client/spark/Notifications/INotificationService.cs b/src/client/spark/Notifications/INotificationService.cs index bb9437610..47e61a63a 100644 --- a/src/client/spark/Notifications/INotificationService.cs +++ b/src/client/spark/Notifications/INotificationService.cs @@ -4,6 +4,7 @@ using bts.api.orderman; using bts.collections; using bts.spark; using mbts; +using mbts.co2; using mbts.collections; using mbts.spark; using mbts.ui; @@ -13,6 +14,7 @@ using System.Collections.Generic; using System.ComponentModel.Composition; using System.Linq; using System.Reactive.Linq; +using System.Reactive.Subjects; namespace bts.shell { interface INotificationService { @@ -22,25 +24,48 @@ namespace bts.shell { IDisposable link_to_activity(INotificationPredicate toast_notification_predicate); IDisposable link_to_order(IPredicate predicate, SoundNotification sound); IDisposable link_to_order(INotificationPredicate toast_notification_predicate); + // Note: security criteria should not be incorporated in the predicate's 'trigger' impl. + // It is easier/cleaner if the INotificationService incorporates the criteria internally. + IDisposable link_to_trades(SecurityCriteria security_criteria, IPredicate predicate, SoundNotification sound); } [Export(typeof(INotificationService))] partial class NotificationService : INotificationService, IDisposable { + // activity notifications readonly NotificationProcessor _activity_notifications; + // order notifications readonly NotificationProcessor _order_notifications; + // trade notifications + readonly NotificationProcessor _trade_notifications; + readonly MDConsumer _consumer; + readonly Subject> _trade_criteria_subject = new Subject>(); + readonly RefCountMap _trade_criteria_refs = new RefCountMap(); + readonly object _trade_lock = new object(); + // readonly DataCache _data_cache = new DataCache(); + readonly LoggerContext _log = new LoggerContext(Log.Ui, "Notification Service ", null); IDisposable _dispose; [ImportingConstructor] - public NotificationService(IBus bus, INotifier notifier, OrdersModel orders_model) { + public NotificationService(IBus bus, INotifier notifier, OrdersModel orders_model, MDConsumer consumer) { this.notifier = notifier; _activity_notifications = new NotificationProcessor(notifier); _order_notifications = new NotificationProcessor(notifier); + _trade_notifications = new NotificationProcessor(notifier); + _consumer = consumer; _dispose = Disposable.Create( bus.subscribe(SparkTags.LogActivity).Subscribe(_on_activity), - orders_model.on_orders.Subscribe(_on_orders) - ); + orders_model.on_orders.Subscribe(_on_orders), + TradesSource.create( + consumer, + bus.subscribe(SparkTags.SecuritiesTag).BufferLatest(TimeSpan.FromSeconds(2)) + .Table(s => s.security.Id) + .Where(t => t.has_changes()) + .Select(m => MappedDictionary.create(m.state, smd => smd.security)), + _trade_criteria_subject, + TimeSpan.FromSeconds(0.2)) + .Subscribe(_trade_notifications.process_items)); } [Export(typeof(INotificationServiceDataCache))] @@ -51,8 +76,42 @@ namespace bts.shell { public IDisposable link_to_activity(INotificationPredicate toast_notification_predicate) => _activity_notifications.link(toast_notification_predicate); public IDisposable link_to_order(IPredicate predicate, SoundNotification sound) => _order_notifications.link(predicate, sound); public IDisposable link_to_order(INotificationPredicate toast_notification_predicate) => _order_notifications.link(toast_notification_predicate); + + public IDisposable link_to_trades(SecurityCriteria security_criteria, IPredicate predicate, SoundNotification sound) { + if (predicate == null) { + _log.warn("cannot create trade alert sound because the specified predicate is null."); + return Disposable.empty; + } + if (!security_criteria.is_defined()) { + // there are simply too many trades to support universal-product predicates, therefor a product filter must be specified (aka SecrityCriteria). + _log.warn("cannot create trade alert sound because the specified security criteria is not defined."); + return Disposable.empty; + } + // This criteria predicate may be redundant, but it safe guards against a case + // where the criteria has not been incorporated into the predicate, + // as there doesn't seem to be a great way to require this in the interface. + // Note: verifying the criteria twice (instead of once) should not hurt on perf at all. + predicate = predicate.and(PredicateBuilder.create(t => ContractPickerModel.security_in_criteria(security_criteria, t.security))); + var link = _trade_notifications.link(predicate, sound); + lock (_trade_lock) { + if (_trade_criteria_refs.increment_adds_key(security_criteria)) + _reset_trade_readers(); + } + return Disposable.Create(() => { + Disposable.dispose(ref link); + lock (_trade_lock) { + if (_trade_criteria_refs.decrement_removes_key(security_criteria)) + _reset_trade_readers(); + } + }); + } + public void Dispose() => Disposable.dispose(ref _dispose); + /// should only call this within a locked scope () + void _reset_trade_readers() => + _trade_criteria_subject.OnNext(_trade_criteria_refs.Keys); + void _on_activity(Activity activity) => _activity_notifications.process_item(activity); @@ -128,6 +187,31 @@ namespace bts.shell { if (sounds.Count > 0 || toast.Count > 0) _notifier.notify(toast, sounds); } + + public void process_items(IEnumerable source) { + var sounds = new HashSet(); + var toast = new HashSet(); + var temp_sound_triggers = _sound_trigger_refs; + if (temp_sound_triggers.Count > 0) { + foreach (var v in source) { + foreach (var trigger_kv in temp_sound_triggers) { + if (trigger_kv.Key.trigger(v)) + sounds.AddRange(trigger_kv.Value.Keys); + } + } + } + var temp_toast_triggers = _toast_trigger_refs; + if (temp_toast_triggers.Count > 0) { + foreach (var v in source) { + foreach (var trigger_kv in temp_toast_triggers) { + if (trigger_kv.Key.trigger(v) is IToastNotification tn && tn.pass) + toast.Add(tn); + } + } + } + if (sounds.Count > 0 || toast.Count > 0) + _notifier.notify(toast, sounds); + } } } } \ No newline at end of file diff --git a/src/client/spark/Notifications/SoundNotification.cs b/src/client/spark/Notifications/SoundNotification.cs index 4125b3d95..b496317c8 100644 --- a/src/client/spark/Notifications/SoundNotification.cs +++ b/src/client/spark/Notifications/SoundNotification.cs @@ -1,9 +1,7 @@ -using mbts; -using mbts.ui.utility; +using bts.utils; using ProtoBuf; using System; using System.ComponentModel; -using System.Text; namespace bts.shell { enum LicenseType { @@ -33,7 +31,7 @@ namespace bts.shell { } [ProtoContract] - class SoundNotification { + class SoundNotification : IEquatable { SystemSound? _system_sound; string _uri_sound; @@ -81,6 +79,19 @@ namespace bts.shell { [ProtoMember(2)] public string uri_sound { get => _uri_sound; set => _set_ref_type(ref _uri_sound, value); } + public bool Equals(SoundNotification other) => + other != null && + _system_sound == other._system_sound && + _uri_sound == other._uri_sound; + + public override bool Equals(object obj) => + Equals(obj as SoundNotification); + + public override int GetHashCode() => + EqualityComparer.auto_phase( + _system_sound.GetHashCode(), + _uri_sound?.GetHashCode() ?? 0); + void _set(ref T? field, T? value) where T : struct { if (value != null) { _clear_values(); diff --git a/src/client/spark/Orders/OrderUtils.cs b/src/client/spark/Orders/OrderUtils.cs index 090939148..e109fcdd4 100644 --- a/src/client/spark/Orders/OrderUtils.cs +++ b/src/client/spark/Orders/OrderUtils.cs @@ -1,5 +1,6 @@ using bts.api; using bts.api.orderman; +using bts.co2; using bts.spark; using bts.utils; using mbts.spark.utilities; @@ -328,5 +329,13 @@ namespace mbts.spark { return string.Empty; } } + + public static AggressorSide to_aggressor(this Side side) { + switch (side) { + case Side.Buy: return AggressorSide.Buy; + case Side.Sell: return AggressorSide.Sell; + default: return AggressorSide.None; + } + } } } diff --git a/src/client/spark/Predicates/Data/Primitive/DoublePredicate.cs b/src/client/spark/Predicates/Data/Primitive/DoublePredicate.cs index 814ba0b54..997e45b85 100644 --- a/src/client/spark/Predicates/Data/Primitive/DoublePredicate.cs +++ b/src/client/spark/Predicates/Data/Primitive/DoublePredicate.cs @@ -61,6 +61,17 @@ namespace mbts { } } + public static DoublePredicate create(PredicateTypeOneofCase type, double min, double max) { + var rv = new DoublePredicate(); + switch (type) { + case PredicateTypeOneofCase.GreaterThan: rv.greater_than = min; break; + case PredicateTypeOneofCase.LessThan: rv.less_than = max; break; + case PredicateTypeOneofCase.InsideRange: rv.inside_range = new DoubleRange(min, max); break; + case PredicateTypeOneofCase.OutsideRange: rv.outside_range = new DoubleRange(min, max); break; + } + return rv; + } + public IPredicate create() { switch (predicate_type_case) { case PredicateTypeOneofCase.GreaterThan: return PredicateBuilder.greater_than(_greater_than.Value); diff --git a/src/client/spark/Predicates/IPredicate.cs b/src/client/spark/Predicates/IPredicate.cs index bd881c697..c5ae09899 100644 --- a/src/client/spark/Predicates/IPredicate.cs +++ b/src/client/spark/Predicates/IPredicate.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.ComponentModel; +using System.Linq; namespace mbts { interface IPredicate { @@ -34,9 +35,15 @@ namespace mbts { public static IPredicate greater_than(T value) => new GreaterThanTrigger(value, Comparer.Default); + public static IPredicate greater_than_or_equal_to(T value) => + new GreaterThanOrEqualToTrigger(value, Comparer.Default); + public static IPredicate less_than(T value) => greater_than(value).negate(); + public static IPredicate less_than_or_equal_to(T value) => + greater_than_or_equal_to(value).negate(); + public static IPredicate inside_range(T minimum, T maximum) => greater_than(minimum).and(less_than(maximum)); @@ -67,6 +74,14 @@ namespace mbts { public static IPredicate not_in_set(ISet set) => in_set(set).negate(); + public static IPredicate and_safe(this IPredicate first, IPredicate second) { + if (first == null) + return second; + if (second == null) + return first; + return and(first, second); + } + public static IPredicate and(this IPredicate first, IPredicate second) => new AndTrigger(first, second); @@ -88,7 +103,7 @@ namespace mbts { public static IPredicate state_transition(Func predicate) { // avoid false-positive in the first callback bool last_result = true; - return new AnonymousMutableTrigger(v => { + return new AnonymousTrigger(v => { bool curr_result = predicate(v); if (last_result == curr_result) { return false; @@ -97,7 +112,7 @@ namespace mbts { } #region Implementation - class EqualsTrigger : IPredicate { + class EqualsTrigger : IPredicate, IEquatable> { readonly T _value; readonly IEqualityComparer _eq_comparer; @@ -106,12 +121,26 @@ namespace mbts { _eq_comparer = eq_comparer; } - public bool trigger(T value) => _eq_comparer.Equals(value, _value); + public bool trigger(T value) => + _eq_comparer.Equals(value, _value); - public override string ToString() => $"EqualsTrigger ({typeof(T)})"; + public override string ToString() => + $"EqualsTrigger ({typeof(T)})"; + + public bool Equals(EqualsTrigger other) => + other != null && + // check equality with both IEqualityComparers + _eq_comparer.Equals(_value, other._value) && + other._eq_comparer.Equals(_value, other._value); + + public override bool Equals(object obj) => + Equals(obj as EqualsTrigger); + + public override int GetHashCode() => + _eq_comparer.GetHashCode(_value); } - class AndTrigger : IPredicate { + class AndTrigger : IPredicate, IEquatable> { readonly IPredicate _first, _second; public AndTrigger(IPredicate first, IPredicate second) { @@ -119,18 +148,28 @@ namespace mbts { _second = second; } - public bool trigger(T value) => _first.trigger(value) && _second.trigger(value); + public bool trigger(T value) => + _first.trigger(value) && + _second.trigger(value); - public override string ToString() => $"AndTrigger ({_first}, {_second})"; + public bool Equals(AndTrigger other) => + other != null && + _first.Equals(other._first) && + _second.Equals(other._second); + + public override string ToString() => + $"AndTrigger ({_first}, {_second})"; public override bool Equals(object obj) => - obj is AndTrigger t && _first.Equals(t._first) && _second.Equals(t._second); + Equals(obj as AndTrigger); public override int GetHashCode() => - EqualityComparer.aggregate_hash(_first.GetHashCode(), _second.GetHashCode()); + EqualityComparer.aggregate_hash( + _first.GetHashCode(), + _second.GetHashCode()); } - class AndTrigger : IPredicate<(T1, T2)> { + class AndTrigger : IPredicate<(T1, T2)>, IEquatable> { readonly IPredicate _first; readonly IPredicate _second; @@ -139,18 +178,28 @@ namespace mbts { _second = second; } - public bool trigger((T1, T2) value) => _first.trigger(value.Item1) && _second.trigger(value.Item2); + public bool trigger((T1, T2) value) => + _first.trigger(value.Item1) && + _second.trigger(value.Item2); + + public bool Equals(AndTrigger other) => + other != null && + _first.Equals(other._first) && + _second.Equals(other._second); public override bool Equals(object obj) => - obj is AndTrigger t && _first.Equals(t._first) && _second.Equals(t._second); + Equals(obj as AndTrigger); public override int GetHashCode() => - EqualityComparer.aggregate_hash(_first.GetHashCode(), _second.GetHashCode()); + EqualityComparer.aggregate_hash( + _first.GetHashCode(), + _second.GetHashCode()); - public override string ToString() => $"AndTrigger ({_first}, {_second})"; + public override string ToString() => + $"AndTrigger ({_first}, {_second})"; } - class OrTrigger : IPredicate { + class OrTrigger : IPredicate, IEquatable> { readonly IPredicate _first, _second; public OrTrigger(IPredicate first, IPredicate second) { @@ -160,16 +209,24 @@ namespace mbts { public bool trigger(T value) => _first.trigger(value) || _second.trigger(value); + public bool Equals(OrTrigger other) => + other != null && + _first.Equals(other._first) && + _second.Equals(other._second); + public override bool Equals(object obj) => - obj is OrTrigger t && _first.Equals(t._first) && _second.Equals(t._second); + Equals(obj is OrTrigger); public override int GetHashCode() => - EqualityComparer.aggregate_hash(_first.GetHashCode(), _second.GetHashCode()); + EqualityComparer.aggregate_hash( + _first.GetHashCode(), + _second.GetHashCode()); - public override string ToString() => $"OrTrigger ({_first}, {_second})"; + public override string ToString() => + $"OrTrigger ({_first}, {_second})"; } - class OrTrigger : IPredicate<(T1, T2)> { + class OrTrigger : IPredicate<(T1, T2)>, IEquatable> { readonly IPredicate _first; readonly IPredicate _second; @@ -178,15 +235,25 @@ namespace mbts { _second = second; } - public bool trigger((T1, T2) value) => _first.trigger(value.Item1) || _second.trigger(value.Item2); + public bool trigger((T1, T2) value) => + _first.trigger(value.Item1) || + _second.trigger(value.Item2); + + public bool Equals(OrTrigger other) => + other != null && + _first.Equals(other._first) && + _second.Equals(other._second); public override bool Equals(object obj) => - obj is OrTrigger t && _first.Equals(t._first) && _second.Equals(t._second); + Equals(obj as OrTrigger); public override int GetHashCode() => - EqualityComparer.aggregate_hash(_first.GetHashCode(), _second.GetHashCode()); + EqualityComparer.aggregate_hash( + _first.GetHashCode(), + _second.GetHashCode()); - public override string ToString() => $"OrTrigger ({_first}, {_second})"; + public override string ToString() => + $"OrTrigger ({_first}, {_second})"; } class GreaterThanTrigger : IPredicate { @@ -198,57 +265,109 @@ namespace mbts { _comparer = comparer; } - public bool trigger(T value) => _comparer.Compare(value, _value) > 0; + public bool trigger(T value) => + _comparer.Compare(value, _value) > 0; - public override string ToString() => $"GreaterThanTrigger ({_value})"; + public override string ToString() => + $"GreaterThanTrigger ({_value})"; } - class InSetTrigger: IPredicate { + class GreaterThanOrEqualToTrigger : IPredicate, IEquatable> { + readonly T _value; + readonly IComparer _comparer; + + public GreaterThanOrEqualToTrigger(T value, IComparer comparer) { + _value = value; + _comparer = comparer; + } + + public bool trigger(T value) => + _comparer.Compare(value, _value) >= 0; + + public bool Equals(GreaterThanOrEqualToTrigger other) => + other != null && + _comparer.Equals(other._comparer) && + _comparer.Compare(_value, other._value) == 0; + + public override bool Equals(object obj) => + Equals(obj as GreaterThanOrEqualToTrigger); + + public override int GetHashCode() => + _value.GetHashCode(); + + public override string ToString() => + $"GreaterThanTrigger ({_value})"; + } + + class InSetTrigger : IPredicate, IEquatable> { readonly ISet _set; public InSetTrigger(ISet set) => _set = set; - public bool trigger(T value) => _set.Contains(value); + public bool trigger(T value) => + _set.Contains(value); + + public bool Equals(InSetTrigger other) => + other != null && + // check SetEquals with both sets (IEqualityComparers may differ) + _set.SetEquals(other._set) && + other._set.SetEquals(_set); public override bool Equals(object obj) => obj is InSetTrigger t && _set.SetEquals(t._set); public override int GetHashCode() => - _set.GetHashCode(); + EqualityComparer.auto_phase(_set.Take(8)); - public override string ToString() => $"InSetTrigger (Count: {_set.Count}, type: {typeof(T)})"; + public override string ToString() => + $"InSetTrigger (Count: {_set.Count}, type: {typeof(T)})"; } - class WrapperTrigger : IPredicate { + class WrapperTrigger : IPredicate, IEquatable> { readonly IPredicate _inner_trigger; - readonly Func _selector; + readonly Func _selector; - public WrapperTrigger(IPredicate inner_trigger, Func selector) { + public WrapperTrigger(IPredicate inner_trigger, Func selector) { _inner_trigger = inner_trigger; _selector = selector; } - public bool trigger(TOutter value) => _inner_trigger.trigger(_selector(value)); + public bool trigger(TOuter value) => + _inner_trigger.trigger(_selector(value)); + + public bool Equals(WrapperTrigger other) => + other != null && + _selector.Equals(other._selector) && + _inner_trigger.Equals(other._inner_trigger); public override bool Equals(object obj) => - obj is WrapperTrigger t && _inner_trigger.Equals(t._inner_trigger) && _selector.Equals(t._selector); + Equals(obj as WrapperTrigger); public override int GetHashCode() => - EqualityComparer.aggregate_hash(_inner_trigger.GetHashCode(), _selector.GetHashCode()); + EqualityComparer.aggregate_hash( + _inner_trigger.GetHashCode(), + _selector.GetHashCode()); - public override string ToString() => $"WrapperTrigger (outter-type {typeof(TOutter)}, inner-trigger: {_inner_trigger})"; + public override string ToString() => + $"WrapperTrigger (outer-type {typeof(TOuter)}, inner-trigger: {_inner_trigger})"; } - class NegateTrigger : IPredicate { + class NegateTrigger : IPredicate, IEquatable> { readonly IPredicate _trigger; + public NegateTrigger(IPredicate trigger) => _trigger = trigger; - public bool trigger(T value) => !_trigger.trigger(value); + public bool trigger(T value) => + !_trigger.trigger(value); + + public bool Equals(NegateTrigger other) => + other != null && + _trigger.Equals(other._trigger); public override bool Equals(object obj) => - obj is NegateTrigger t && _trigger.Equals(t._trigger); + Equals(obj as NegateTrigger); public override int GetHashCode() => -_trigger.GetHashCode(); @@ -256,22 +375,22 @@ namespace mbts { public override string ToString() => $"NegateTrigger ({_trigger})"; } - class AnonymousTrigger : IPredicate { + class AnonymousTrigger : IPredicate, IEquatable> { readonly Func _trigger; public AnonymousTrigger(Func trigger) => _trigger = trigger; - public bool trigger(T value) => _trigger(value); + public bool Equals(AnonymousTrigger other) => + other != null && + _trigger.Equals(other._trigger); - public override string ToString() => $"AnonymousTrigger ({typeof(T)})"; - } + public bool trigger(T value) => + _trigger(value); - class AnonymousMutableTrigger : IPredicate { - public AnonymousMutableTrigger(Func predicate) => this.predicate = predicate; - public Func predicate { get; set; } - public bool trigger(T value) => predicate(value); + public override string ToString() => + $"AnonymousTrigger ({typeof(T)})"; } #endregion } -} \ No newline at end of file +} diff --git a/src/client/spark/Predicates/VM/DoublePredicateVM.cs b/src/client/spark/Predicates/VM/DoublePredicateVM.cs new file mode 100644 index 000000000..b386192e8 --- /dev/null +++ b/src/client/spark/Predicates/VM/DoublePredicateVM.cs @@ -0,0 +1,33 @@ +using bts.proto; +using mbts.ui.shell; + +namespace mbts.spark { + class DoublePredicateVM : ViewModelBase, IValueFactory { + double _minimum, _maximum; + DoublePredicate.PredicateTypeOneofCase _type; + + public DoublePredicate.PredicateTypeOneofCase type { get => _type; set => Set(ref _type, value); } + public double minimum { get => _minimum; set => Set(ref _minimum, value); } + public double maximum { get => _maximum; set => Set(ref _maximum, value); } + + public DoublePredicate create() => + DoublePredicate.create(type, minimum, maximum); + + public void load_from(DoublePredicate value) { + if (value == null) + return; + type = value.predicate_type_case; + switch (type) { + case DoublePredicate.PredicateTypeOneofCase.GreaterThan: minimum = value.greater_than.Value; break; + case DoublePredicate.PredicateTypeOneofCase.LessThan: maximum = value.less_than.Value; break; + case DoublePredicate.PredicateTypeOneofCase.InsideRange: _load_range(value.inside_range.Value); break; + case DoublePredicate.PredicateTypeOneofCase.OutsideRange: _load_range(value.outside_range.Value); break; + } + } + + void _load_range(DoubleRange range) { + minimum = range.minimum; + maximum = range.maximum; + } + } +} \ No newline at end of file diff --git a/src/client/spark/Themes/generic.xaml b/src/client/spark/Themes/generic.xaml index 52e66ff7d..8038e6b0b 100644 --- a/src/client/spark/Themes/generic.xaml +++ b/src/client/spark/Themes/generic.xaml @@ -338,7 +338,7 @@ - + diff --git a/src/client/spark/Trades/TradeInfo.cs b/src/client/spark/Trades/TradeInfo.cs new file mode 100644 index 000000000..e1ee23f7a --- /dev/null +++ b/src/client/spark/Trades/TradeInfo.cs @@ -0,0 +1,14 @@ +using bts.api; +using bts.co2; + +namespace mbts.spark { + struct TradeInfo { + public readonly SecurityImmutable security; + public readonly TradeMessageImmutable trade; + + public TradeInfo(SecurityImmutable security, TradeMessageImmutable trade) { + this.security = security; + this.trade = trade; + } + } +} \ No newline at end of file diff --git a/src/client/spark/Trades/TradeMessageExtensions.cs b/src/client/spark/Trades/TradeMessageExtensions.cs new file mode 100644 index 000000000..f01a273d9 --- /dev/null +++ b/src/client/spark/Trades/TradeMessageExtensions.cs @@ -0,0 +1,9 @@ +using bts.co2; +using System; + +namespace mbts.spark { + static class TradeMessageExtensions { + public static double price(this TradeMessageImmutable trade) => + trade.Mantissa * Math.Pow(10, trade.Exponent); + } +} \ No newline at end of file diff --git a/src/client/spark/Trades/TradesSource.cs b/src/client/spark/Trades/TradesSource.cs new file mode 100644 index 000000000..032fbecfc --- /dev/null +++ b/src/client/spark/Trades/TradesSource.cs @@ -0,0 +1,94 @@ +using bts.api; +using bts.api.core; +using bts.co2; +using bts.utils; +using mbts.co2; +using mbts.utils; +using System; +using System.Collections.Generic; +using System.Collections.Immutable; +using System.Linq; +using System.Reactive.Linq; + +namespace mbts.spark { + readonly struct Product : IEquatable { + public readonly string symbol; + public readonly SecurityType type; + public readonly Venue exchange; + + public Product(string symbol, SecurityType type, Venue exchange) { + this.symbol = symbol; + this.type = type; + this.exchange = exchange; + } + + public static Product from_security(SecurityImmutable security) => + new Product(security.Symbol, security.security_type(), security.Exchange); + + public bool Equals(Product other) => + symbol == other.symbol && + type == other.type && + exchange == other.exchange; + + public override bool Equals(object obj) => + obj is Product other && Equals(other); + + public override int GetHashCode() => + EqualityComparer.auto_phase( + symbol?.GetHashCode() ?? 0, + type.GetHashCode(), + exchange.GetHashCode()); + + public static bool operator ==(Product a, Product b) => a.Equals(b); + public static bool operator !=(Product a, Product b) => !a.Equals(b); + } + + static class TradesSource { + public static IEnumerable aggregate_security_criteria(IEnumerable securities, IEnumerable security_criterion) => + securities.Where(s => security_criterion.Any(sc => ContractPickerModel.security_in_criteria(sc, s))) + .Select(Product.from_security) + .Distinct(); + + public static IObservable> create(MDConsumer consumer, IObservable> security_map, IObservable> security_criterion, TimeSpan poll_interval) => + security_criterion.CombineLatest(security_map, (criterion, map) => (criterion, map)) + .Scan((channels: ImmutableDictionary.Empty, active: new List(0)), (channel_data, data) => { + var (criterion, map) = data; + var (channels, active) = channel_data; + var products = aggregate_security_criteria(map.Values, criterion); + active = new List(); + foreach (var p in products) { + if (!channels.TryGetValue(p, out var c)) { + c = new ChannelConsumer(consumer.get_channel(p.symbol, p.type, p.exchange), map.Values.Where(s => Product.from_security(s).Equals(p))); + channels = channels.Add(p, c); + } + active.Add(c); + } + return (channels, active); + }).SelectManyInSeries(channel_data => { + var (channels, active) = channel_data; + return Observable.Interval(poll_interval) + .Select(_ => active.SelectMany(c => c.read_trades()).ToList()); + }); + + class ChannelConsumer { + readonly MDConsumerChannel _channel; + readonly Dictionary _securities; + long _cursor = 0; + + public ChannelConsumer(MDConsumerChannel channel) => + _channel = channel; + + public ChannelConsumer(MDConsumerChannel channel, IEnumerable securities) { + _channel = channel; + _securities = securities.ToDictionaryCrushed(s => s.Id); + } + + public IEnumerable read_trades() { + foreach (var tm in _channel.read_trades(ref _cursor)) { + if (tm != null && _securities.TryGetValue(tm.Key, out SecurityImmutable sec)) + yield return new TradeInfo(sec, new TradeMessageImmutable(tm)); + } + } + } + } +} \ No newline at end of file diff --git a/src/client/spark/core/Bus/SparkCommands.cs b/src/client/spark/core/Bus/SparkCommands.cs index 726eff24a..9ed6fa67b 100644 --- a/src/client/spark/core/Bus/SparkCommands.cs +++ b/src/client/spark/core/Bus/SparkCommands.cs @@ -107,6 +107,9 @@ namespace bts.spark { public static SparkCommand ShowActivityLog => SparkCommand.create("Activity Log", Icons.Wrench, SparkTags.ShowActivityLog); + public static SparkCommand ShowGlobalAlerts => + SparkCommand.create("Alerts", Icons.Speaker, SparkTags.ShowGlobalAlertsTag); + public static PartialCommand ShowReportIssue => SparkCommand.create("Report a Problem", Icons.Support, SparkTags.Support); diff --git a/src/client/spark/core/Bus/SparkTags.cs b/src/client/spark/core/Bus/SparkTags.cs index eef9c6d4e..bf009631b 100644 --- a/src/client/spark/core/Bus/SparkTags.cs +++ b/src/client/spark/core/Bus/SparkTags.cs @@ -81,6 +81,8 @@ namespace mbts.spark public static ITag SyntheticDeleteTag { get; } = new SparkTag(nameof(SyntheticDeleteTag)); + public static ITag ShowGlobalAlertsTag { get; } = new SparkTag(nameof(ShowGlobalAlertsTag)); + public class Nothing { Nothing() { } } internal class SparkTag : ITag { diff --git a/src/client/spark/core/ShellInterface/UserSettings/Alerts/GlobalAlertSettings.cs b/src/client/spark/core/ShellInterface/UserSettings/Alerts/GlobalAlertSettings.cs new file mode 100644 index 000000000..839049584 --- /dev/null +++ b/src/client/spark/core/ShellInterface/UserSettings/Alerts/GlobalAlertSettings.cs @@ -0,0 +1,29 @@ +using bts.utils; +using ProtoBuf; +using System; +using System.Collections.Generic; +using System.Linq; + +namespace mbts.spark { + [ProtoContract] + class GlobalAlertSettings : IEquatable { + [ProtoMember(1)] + TradeAlert[] _trade_alerts; + + public GlobalAlertSettings() { } + public GlobalAlertSettings(IEnumerable trade_alerts) => + _trade_alerts = trade_alerts.ToArray(); + + public IReadOnlyList trade_alerts => _trade_alerts ?? Array.Empty(); + + public bool Equals(GlobalAlertSettings other) => + other != null && + trade_alerts.SequenceEqual(other.trade_alerts); + + public override bool Equals(object obj) => + Equals(obj as GlobalAlertSettings); + + public override int GetHashCode() => + EqualityComparer.auto_phase(trade_alerts); + } +} \ No newline at end of file diff --git a/src/client/spark/core/ShellInterface/UserSettings/Alerts/TradeAlertSettings.cs b/src/client/spark/core/ShellInterface/UserSettings/Alerts/TradeAlertSettings.cs new file mode 100644 index 000000000..6892fdfa0 --- /dev/null +++ b/src/client/spark/core/ShellInterface/UserSettings/Alerts/TradeAlertSettings.cs @@ -0,0 +1,121 @@ +using bts.api; +using bts.shell; +using bts.utils; +using ProtoBuf; +using System; +using System.Collections.Generic; + +namespace mbts.spark { + [ProtoContract] + readonly struct TradeAlert : IEquatable { + [ProtoMember(1)] + public readonly TradeAlertPredicate predicate; + [ProtoMember(2)] + public readonly SoundNotification sound; + [ProtoMember(3)] + public readonly bool is_enabled; + + public TradeAlert( + TradeAlertPredicate predicate, + SoundNotification sound = null, + bool is_enabled = false) { + this.predicate = predicate; + this.sound = sound; + this.is_enabled = is_enabled; + } + + public TradeAlert with( + TradeAlertPredicate? predicate = null, + SoundNotification sound = null, + bool? is_enabled = null) => + // + new TradeAlert( + predicate ?? this.predicate, + sound ?? this.sound, + is_enabled ?? this.is_enabled); + + public bool Equals(TradeAlert other) => + predicate.Equals(other.predicate) && + sound.safe_equals(other.sound) && + is_enabled == other.is_enabled; + + public override bool Equals(object obj) => + obj is TradeAlert other && Equals(other); + + public override int GetHashCode() => + EqualityComparer.auto_phase( + predicate.GetHashCode(), + sound?.GetHashCode() ?? 0, + is_enabled.GetHashCode()); + + public static bool operator ==(TradeAlert a, TradeAlert b) => a.Equals(b); + public static bool operator !=(TradeAlert a, TradeAlert b) => !a.Equals(b); + } + + [ProtoContract] + readonly struct TradeAlertPredicate : IEquatable { + [ProtoMember(1)] + public readonly SecurityCriteria security_criteria; + [ProtoMember(2)] + public readonly DoublePredicate quantity; + [ProtoMember(3)] + public readonly DoublePredicate price; + [ProtoMember(4)] + readonly Side[] _side; + + public TradeAlertPredicate( + SecurityCriteria security_criteria = null, + DoublePredicate quantity = null, + DoublePredicate price = null, + Side[] side = null) { + this.security_criteria = security_criteria; + this.quantity = quantity; + this.price = price; + this._side = side; + } + + public IReadOnlyList side => _side ?? Array.Empty(); + + public IPredicate create() { + IPredicate p = null; + // security criteria + var criteria = security_criteria; + if (criteria.is_defined()) { + p = p.and_safe(PredicateBuilder.create(t => ContractPickerModel.security_in_criteria(criteria, t.security))); + } + // quantity + var qpred = quantity?.create(); + if (qpred != null) + p = p.and_safe(PredicateBuilder.wrap(qpred, t => t.trade.Size)); + // price + var ppred = price?.create(); + if (ppred != null) + p = p.and_safe(PredicateBuilder.wrap(ppred, t => t.trade.price())); + // side + if (_side != null && _side.Length == 1) { + var agg_side = _side[0].to_aggressor(); + p = p.and_safe(PredicateBuilder.create(t => agg_side == t.trade.AggressorSide)); + } + return p; + } + + public bool Equals(TradeAlertPredicate other) => + security_criteria.safe_equals(other.security_criteria) && + quantity == other.quantity && + price == other.price && + EqualityComparer.safe_sequence_equals(_side, other._side); + + public override bool Equals(object obj) => + obj is TradeAlertPredicate other && Equals(other); + + public override int GetHashCode() => + EqualityComparer.auto_phase( + security_criteria?.GetHashCode() ?? 0, + quantity.GetHashCode(), + price.GetHashCode(), + _side != null && _side.Length == 1 ? (int)_side[0] : 0); + + public static bool operator ==(TradeAlertPredicate a, TradeAlertPredicate b) => a.Equals(b); + public static bool operator !=(TradeAlertPredicate a, TradeAlertPredicate b) => !a.Equals(b); + } +} \ No newline at end of file diff --git a/src/client/spark/core/ShellInterface/UserSettings/ISettingsMigrationRule.cs b/src/client/spark/core/ShellInterface/UserSettings/ISettingsMigrationRule.cs new file mode 100644 index 000000000..2091f8550 --- /dev/null +++ b/src/client/spark/core/ShellInterface/UserSettings/ISettingsMigrationRule.cs @@ -0,0 +1,82 @@ +using mbts.ui.settings; +using System; +using System.Collections.Generic; + +namespace bts.settings { + interface ISettingsMigrationRule { + bool apply(T settings, out T new_settings); + } + + // Migration rules of type ISettingsMigrationRule, where 'T' is a common type, such as GridSettings, + // should be exposed through explicitly named interfaces that inherit ISettingsMigrationRule. + // This avoids magic const names in the MEF import/export contracts, which would otherwise be needed to + // determine the various windows to which such rules apply. + interface IPAndLGridSettingsMigrationRule : ISettingsMigrationRule { } + interface IMarketViewGridSettingsMigrationRule : ISettingsMigrationRule { } + + static class SettingsMigrationHelper { + public static bool apply(T settings, IEnumerable> migration_rules, out T new_settings) { + bool applied = false; + foreach (var r in migration_rules) { + if (r.apply(settings, out var migrated)) { + settings = migrated; + applied = true; + } + } + new_settings = applied ? settings : default; + return applied; + } + + public static void apply_quiet(ref T settings, IEnumerable> migration_rules) { + if (apply(settings, migration_rules, out var new_settings)) + settings = new_settings; + } + + public static ISettingsMigrationRule merge(this IEnumerable> rules) => + new MergeWrapper(rules); + + public static ISettingsMigrationRule wrap(this ISettingsMigrationRule rule, Func get, Func set) => + new SelectorWrapper(rule, get, set); + + class MergeWrapper : ISettingsMigrationRule { + readonly IEnumerable> _rules; + + public MergeWrapper(IEnumerable> rules) => + _rules = rules; + + public bool apply(T settings, out T new_settings) { + bool applied = false; + foreach (var r in _rules) { + if (r.apply(settings, out var migrated)) { + settings = migrated; + applied = true; + } + } + new_settings = applied ? settings : default; + return applied; + } + } + + class SelectorWrapper : ISettingsMigrationRule { + readonly ISettingsMigrationRule _rule; + readonly Func _get; + readonly Func _set; + + public SelectorWrapper(ISettingsMigrationRule rule, Func get, Func set) { + _rule = rule; + _get = get; + _set = set; + } + + public bool apply(TOuter settings, out TOuter new_settings) { + var ival = _get(settings); + if (_rule.apply(ival, out var new_ival)) { + new_settings = _set(settings, new_ival); + return true; + } + new_settings = default; + return false; + } + } + } +} \ No newline at end of file diff --git a/src/client/spark/core/ShellInterface/UserSettings/UserSettings.cs b/src/client/spark/core/ShellInterface/UserSettings/UserSettings.cs index 6dbe4c73d..45f018859 100644 --- a/src/client/spark/core/ShellInterface/UserSettings/UserSettings.cs +++ b/src/client/spark/core/ShellInterface/UserSettings/UserSettings.cs @@ -56,6 +56,11 @@ namespace mbts.spark { GridSettings _pnl_grid; [ProtoMember(10)] bts.shell.orderbook.OrderBookGridSettings _order_book_grid; + // Placeholder for PNLSettings (will be merged to master soon) + // [ProtoMember(13)] + // PNLSettings _pnl; + [ProtoMember(14)] + GlobalAlertSettings _alerts; public UserSettings() { } @@ -73,6 +78,8 @@ namespace mbts.spark { public GridSettings pnl_grid => _pnl_grid ?? (_pnl_grid = bts.shell.PAndLWindowFactory.PAndLStartupData.DefaultGridSettings); public bts.shell.orderbook.OrderBookGridSettings order_book_grid => _order_book_grid ?? (_order_book_grid = bts.shell.orderbook.OrderBookStartupData.Default.grid_settings_wrapper()); + public GlobalAlertSettings alerts => _alerts ?? (_alerts = new GlobalAlertSettings()); + public UserSettings with(LadderSettings ladder = null, LadderSettingsProfile[] ladder_profiles = null, NotificationSettings notification = null, @@ -81,7 +88,8 @@ namespace mbts.spark { GridSettings provided_fills_gridsettings = null, GridSettings provided_synthetic_management_gridsettings = null, GridSettings provided_pnl_gridsettings = null, - bts.shell.orderbook.OrderBookGridSettings order_book_grid = null) { + bts.shell.orderbook.OrderBookGridSettings order_book_grid = null, + GlobalAlertSettings alerts = null) { var us = new UserSettings(ladder_profiles ?? _ladder_profiles) { _ladder = ladder ?? _ladder, _notification = notification ?? _notification, @@ -89,8 +97,9 @@ namespace mbts.spark { _time_and_sales_grid = time_and_sales ?? _time_and_sales_grid, _fills_grid = provided_fills_gridsettings ?? _fills_grid, _synthetic_management_grid = provided_synthetic_management_gridsettings ?? _synthetic_management_grid, - _pnl_grid = provided_pnl_gridsettings ?? _pnl_grid, + _pnl_grid = provided_pnl_gridsettings ?? _pnl_grid, _order_book_grid = order_book_grid ?? _order_book_grid, + _alerts = alerts ?? _alerts }; us._last_ladder_profile_id = Math.Max(_last_ladder_profile_id, us.ladder_profiles.Select(p => p.id).MaxOrDefault()); return us; @@ -98,8 +107,9 @@ namespace mbts.spark { public static class FileExtensions { public const string + GRID = "grid", LADDER = "ladder", - GRID = "grid"; + PNL = "pnl"; } } diff --git a/src/client/spark/core/ShellInterface/UserSettings/UserSettingsHub.cs b/src/client/spark/core/ShellInterface/UserSettings/UserSettingsHub.cs index b130f5949..698c4c5ec 100644 --- a/src/client/spark/core/ShellInterface/UserSettings/UserSettingsHub.cs +++ b/src/client/spark/core/ShellInterface/UserSettings/UserSettingsHub.cs @@ -1,6 +1,8 @@ using bts; +using bts.settings; using bts.utils; using mbts.spark; +using mbts.ui.settings; using System; using System.Collections.Generic; using System.ComponentModel.Composition; @@ -22,16 +24,30 @@ namespace mbts.persist { UserSettings _default_settings; readonly BehaviorSubject _default_settings_subject = new BehaviorSubject(UserSettings.factory_defaults); readonly object _default_settings_lock = new object(); + readonly IEnumerable _pandl_migration_rules; + readonly IEnumerable _mview_migration_rules; [ImportingConstructor] - public UserSettingsHub(IUserPersist persist) { + public UserSettingsHub( + IUserPersist persist, + [ImportMany]IEnumerable pandl_migration_rules, + [ImportMany]IEnumerable mview_migration_rules) { + // _persist = persist; + _pandl_migration_rules = pandl_migration_rules ?? Enumerable.Empty(); + _mview_migration_rules = mview_migration_rules ?? Enumerable.Empty(); init(); async void init() { UserSettings s; try { s = await _persist.get(NAME_SPACE, DEFAULT_KEY); if (s != null) { + var wrapped_rules = new[] { + _pandl_migration_rules.merge().wrap(us => us.pnl_grid, (us, gs) => us.with(provided_pnl_gridsettings: gs)), + _mview_migration_rules.merge().wrap(us => us.market_view_grid, (us, gs) => us.with(provided_market_view_gridsettings: gs)) + }; + SettingsMigrationHelper.apply_quiet(ref s, wrapped_rules); + // TODO: move ladder sanitization logic into ISettingsMigrationRule s.ladder.sanitize(); // ensure valid named ladder set (w/ distinct, non-zero ids) var ladder_profile_map = new Dictionary(); @@ -78,6 +94,20 @@ namespace mbts.persist { } } + public IWindowSettingsManager create_pandl_settings_manager(GridSettings instance_settings) => + _create_settings_manager( + instance_settings, + _pandl_migration_rules, + us => us.pnl_grid, + (us, gs) => us.with(provided_pnl_gridsettings: gs)); + + public IWindowSettingsManager create_mview_settings_manager(GridSettings instance_settings) => + _create_settings_manager( + instance_settings, + _mview_migration_rules, + us => us.market_view_grid, + (us, gs) => us.with(provided_market_view_gridsettings: gs)); + public async Task save_default(UserSettings value) { // notify observers before persisting so the changes are reflected immediately in the ui lock (_default_settings_lock) { @@ -86,6 +116,12 @@ namespace mbts.persist { await _persist_async(); } + IWindowSettingsManager _create_settings_manager(T instance_settings, IEnumerable> migration_rules, Func get, Func set) where T : class { + if (instance_settings != null && migration_rules.merge().apply(instance_settings, out var new_settings)) + instance_settings = new_settings; + return new WindowSettingsManager(this, instance_settings, get, set); + } + async Task _persist_async() { try { await _persist.set(NAME_SPACE, DEFAULT_KEY, _default_settings); diff --git a/src/client/spark/core/Utility/IPropertyWrapperVM.cs b/src/client/spark/core/Utility/IPropertyWrapperVM.cs index 7db97a542..b8cd8b8da 100644 --- a/src/client/spark/core/Utility/IPropertyWrapperVM.cs +++ b/src/client/spark/core/Utility/IPropertyWrapperVM.cs @@ -59,6 +59,22 @@ namespace mbts.spark { interface ITemplatedPropertyWrapperVM : IPropertyWrapperVM, ITemplateSelectorKey { } + static class ValueFactoryUtils { + public static void sync_with_models(this IList target, IEnumerable models) where TValueFactory : IValueFactory, new() { + target.sync_with_models( + models, + m => { + var vf = new TValueFactory(); + vf.load_from(m); + return vf; + }, + (vf, m) => { + vf.load_from(m); + return vf; + }); + } + } + static class PropertyWrapperVMUtils { public static T create_assigned(this IPropertyWrapperVM vm) where T : new() => vm.create_assigned(() => new T()); diff --git a/src/client/spark/core/data/Proto/ColorOpacityProto.cs b/src/client/spark/core/data/Proto/ColorOpacityProto.cs index c67e51d37..b5d69b2d6 100644 --- a/src/client/spark/core/data/Proto/ColorOpacityProto.cs +++ b/src/client/spark/core/data/Proto/ColorOpacityProto.cs @@ -22,6 +22,7 @@ namespace bts.proto { } public Color color => _color.rgb_color(); + public Color argb_color => Color.FromArgb((byte)(MathUtils.within_range(0, 1, _opacity) * 255.0), color.R, color.G, color.B); public double opacity => _opacity; public ColorOpacityProto with(uint? color = null, double? opacity = null) => diff --git a/src/client/spark/core/data/Proto/NullableColorProto.cs b/src/client/spark/core/data/Proto/NullableColorProto.cs index 3fa15eba3..2fb827d08 100644 --- a/src/client/spark/core/data/Proto/NullableColorProto.cs +++ b/src/client/spark/core/data/Proto/NullableColorProto.cs @@ -16,14 +16,8 @@ namespace bts.proto { public NullableColorProto(uint value) => _value = value; - - public Color? value { - get { - if (_value.HasValue) - return _value.Value.rgb_color(); - return null; - } - } + public Color? value => _value?.rgb_color(); + public Color? argb => _value?.argb_color(); public bool Equals(NullableColorProto other) => EqualityComparer.nullable_equals(_value, other._value); diff --git a/src/client/spark/view/Colors.xaml b/src/client/spark/view/Colors.xaml index 300d3b647..8843b2151 100644 --- a/src/client/spark/view/Colors.xaml +++ b/src/client/spark/view/Colors.xaml @@ -105,6 +105,9 @@ + + #FF4182F6 + #3D25A0DA #FF26A0DA diff --git a/src/client/spark/view/Controls/Adorners/RowSelectionAdorner.cs b/src/client/spark/view/Controls/Adorners/RowSelectionAdorner.cs new file mode 100644 index 000000000..e6079090d --- /dev/null +++ b/src/client/spark/view/Controls/Adorners/RowSelectionAdorner.cs @@ -0,0 +1,98 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Documents; +using System.Windows.Input; +using System.Windows.Media; +using bts.utils; +using mbts.ui.extensions; + +namespace mbts.ui.controls.adorners { + public class RowSelectionAdorner : Adorner, DIYGrid.ISelectionHighlightListener, IDisposable { + Brush _fill = DIYGrid.DefaultSelectionHighlight; + Pen _stroke = DIYGrid.DefaultSelectionStroke; + + DIYGridRowsPresenter _diy_rp; + ScrollViewer _diy_scroll; + ScrollContentPresenter _diy_scp; + List _disposables = new List(); + + #region Fill + public static readonly DependencyProperty FillProperty = + WPFHelper.create_dp( + nameof(Fill), + FrameworkPropertyMetadataOptions.AffectsRender, + (o, v) => o._fill = v, + DIYGrid.DefaultSelectionHighlight); + + public SolidColorBrush Fill { + get => GetValue(FillProperty) as SolidColorBrush; + set => SetValue(FillProperty, value); + } + #endregion + #region Stroke + public static readonly DependencyProperty StrokeProperty = + WPFHelper.create_dp( + nameof(Stroke), + FrameworkPropertyMetadataOptions.AffectsRender, + (o, v) => o._stroke = v, + DIYGrid.DefaultSelectionStroke); + + public Pen Stroke { + get => GetValue(StrokeProperty) as Pen; + set => SetValue(StrokeProperty, value); + } + #endregion + + internal RowSelectionAdorner(DIYGridRowsPresenter adorned_element, ScrollViewer scroll_viewer) : base(adorned_element) { + // we want our clicks to go through to the underlying grid + IsHitTestVisible = false; + + _diy_rp = adorned_element; + _diy_scroll = scroll_viewer; + _diy_scp = _diy_rp.descendants_of_type().empty_if_null().FirstOrDefault(); + + // We actually don't need mouse up/down or key up/down when we can just respond to selection changes + _disposables.Add(((System.Windows.Controls.Primitives.Selector)_diy_rp).SubscribeSelectionChanged((s, e) => InvalidateVisual())); + // so that we update the highlight as the grid scrolls + _disposables.Add(_diy_scroll.SubscribeScrollChanged((s, e) => InvalidateVisual())); + } + + + protected override void OnRender(DrawingContext drawingContext) { + base.OnRender(drawingContext); + if (AdornedElement is ListBox lb && lb.SelectedItems != null && lb.SelectedItems.Count > 0) { + foreach(var lbi in lb.SelectedItems) { + if (_diy_rp.ItemContainerGenerator.ContainerFromItem(lbi) is FrameworkElement row && row.IsVisible) { + var top_left = lb.GetChildTopLeft(row); + var top_is_within_bounds = top_left.Y >= 0; + + // Note that the top of the row can still technically be visible but we wont show + // the adorner rectangle b/c the bottom of the row will be obscured by the scrollbar + var bottom_is_within_bounds = lb.GetChildBounds(row).BottomRight.Y <= _diy_scroll.ActualHeight; + if (top_is_within_bounds && bottom_is_within_bounds) { + // we use the scroll content presenter in the X point calculation instead of the row + // because of the OB which has a right side panel that takes up some space over + // row width + drawingContext.DrawRectangle(_fill, + _stroke, + new Rect(top_left, new Point(top_left.X + _diy_scp.ActualWidth, top_left.Y + row.ActualHeight))); + } + } + } + } + } + + public void on_selection_fill(SolidColorBrush selection_fill) { + Fill = selection_fill; + Stroke = new Pen(selection_fill.Color.to_solid_brush(), 1); + InvalidateVisual(); + } + + public void Dispose() { + _disposables.DisposeAll(); + } + } +} \ No newline at end of file diff --git a/src/client/spark/view/Controls/ColorPicker/ColorPickerPopup.xaml.cs b/src/client/spark/view/Controls/ColorPicker/ColorPickerPopup.xaml.cs index e893b053c..a55734853 100644 --- a/src/client/spark/view/Controls/ColorPicker/ColorPickerPopup.xaml.cs +++ b/src/client/spark/view/Controls/ColorPicker/ColorPickerPopup.xaml.cs @@ -1,7 +1,9 @@ using bts.proto; +using bts.utils; using mbts.spark; using mbts.ui.controls.color_picker; using mbts.ui.shell; +using System; using System.Collections.ObjectModel; using System.Linq; using System.Windows; @@ -38,16 +40,42 @@ namespace mbts.ui.controls { class ColorPickerPopupVM : ViewModelBase, IValueFactory { readonly Property _color; readonly Property _opacity; + ObservableCollection _palettes; + bool _bind_opacity_to_alpha; + IDisposable _opacity_alpha_binding; public ColorPickerPopupVM() { _color = backing(nameof(color), Colors.Black); _opacity = backing(nameof(opacity), 1.0); - palettes = new ObservableCollection(ColorPalette.all_presets().Select(p => new ColorPaletteVM(p))); } public Color color { get => _color.Value; set => _color.set(value); } public double opacity { get => _opacity.Value; set => _opacity.set(value); } - public ObservableCollection palettes { get; } + public ObservableCollection palettes => _palettes ?? (_palettes = new ObservableCollection(ColorPalette.all_presets().Select(p => new ColorPaletteVM(p)))); + public bool bind_opacity_to_alpha { + get => _bind_opacity_to_alpha; + set { + if (_bind_opacity_to_alpha != value) { + Disposable.dispose(ref _opacity_alpha_binding); + _bind_opacity_to_alpha = value; + if (value) { + _opacity.Changed += opacity_changed; + _color.Changed += color_changed; + color_changed(default, color); + void opacity_changed(double oldval, double newval) { + _color.Changed -= color_changed; + _color.set(color.with((byte)(MathUtils.within_range(0, 1, newval) * 255.0))); + _color.Changed += color_changed; + } + void color_changed(Color oldval, Color newval) { + _opacity.Changed -= opacity_changed; + _opacity.set(newval.A / 255.0); + _opacity.Changed += opacity_changed; + } + } + } + } + } public SolidColorBrush create() => new SolidColorBrush(color) { Opacity = opacity }.frozen(); public void load_from(SolidColorBrush brush) { diff --git a/src/client/spark/view/Controls/DIYGrid/CellRender/HyperlinkCell.cs b/src/client/spark/view/Controls/DIYGrid/CellRender/HyperlinkCell.cs index 8d9e326cb..e9e5ae634 100644 --- a/src/client/spark/view/Controls/DIYGrid/CellRender/HyperlinkCell.cs +++ b/src/client/spark/view/Controls/DIYGrid/CellRender/HyperlinkCell.cs @@ -304,11 +304,6 @@ namespace mbts.ui.controls { } drawingContext.Pop(); } - if (bg != null) { - var bgo = GetBackgroundOverlay(this); - if (bgo != null) - drawingContext.DrawRectangle(bgo, null, rect); - } } bool _point_horizontally_over_text(double x) { diff --git a/src/client/spark/view/Controls/DIYGrid/Columns/ColumnBase.cs b/src/client/spark/view/Controls/DIYGrid/Columns/ColumnBase.cs index c3625c59f..50532299c 100644 --- a/src/client/spark/view/Controls/DIYGrid/Columns/ColumnBase.cs +++ b/src/client/spark/view/Controls/DIYGrid/Columns/ColumnBase.cs @@ -1,9 +1,8 @@ -using mbts.spark; +using bts; +using mbts.spark; using mbts.spark.filters; using mbts.ui.settings; -using mbts.ui.shell; using System; -using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Reflection; @@ -11,7 +10,7 @@ using System.Windows; using System.Windows.Controls; using System.Windows.Media; -namespace mbts.ui.controls { +namespace mbts.ui.controls { // Column types are nested inside the DIYGrid partial class DIYGrid { public static class DefaultColumnWidths { @@ -115,6 +114,7 @@ namespace mbts.ui.controls { StartPnL = "Start PnL", DayLong = "Day Long", DayShort = "Day Short", + RawDayPos = "Raw Day Pos", DayPnL = "Day PnL", TotalNetPos = "Total Net Pos", TotalPnL = "Total PnL", @@ -167,7 +167,7 @@ namespace mbts.ui.controls { } public interface ISelectionHighlightListener { - void on_selection_fill(Brush selection_fill); + void on_selection_fill(SolidColorBrush selection_fill); } // To include extra data with your column type you are encouraged to derive from DIYGridColumn @@ -175,16 +175,19 @@ namespace mbts.ui.controls { //readonly ExternalProperty _font_weigh FontWeight _fallback_font_weight; Color _fallback_font_color; + Color _fallback_background_color; double _fallback_font_size; FontFamily _fallback_font_family; FontWeight? _font_weight_override; Color? _font_color_override; + Color? _background_color_override; object _header_copy; protected ColumnBase() { var gs = GridSettings.Default; _fallback_font_weight = gs.font_weight; _fallback_font_color = gs.font_color.rgb_color(); + _fallback_background_color = gs.background_color.argb_color(); _fallback_font_size = gs.font_size; _fallback_font_family = new FontFamily(gs.font_family); original_width = double.NaN; @@ -217,6 +220,9 @@ namespace mbts.ui.controls { public FontWeight font_weight => font_weight_override ?? _fallback_font_weight; public Color font_color => font_color_override ?? _fallback_font_color; public Brush font_brush => new SolidColorBrush(font_color); + public Color? background_color_override { get => _background_color_override; set => _set_font_param(ref _background_color_override, value); } + public Color? background_color => background_color_override ?? _fallback_background_color; + public Brush background_brush => background_color?.to_brush(); // TODO: find a better way to store the header-property in a thread-safe manner - a value for GridSettings.display_name, more specifically. // Since save_state is called from a different thread in IShared/SlowWindows, it is not safe to access the DependencyProperty store from that method. // By updating a local _header_copy field in the Header setter, the DependencyProperty lookup may be bypassed when appropriate (see create method below). @@ -226,7 +232,7 @@ namespace mbts.ui.controls { } public GridColumnSettings create() => - new GridColumnSettings(id, _header_copy?.ToString(), is_visible, is_collapsed, font_weight_override, font_color_override, ActualWidth, horizontal_alignment, horizontal_offset); + new GridColumnSettings(id, _header_copy?.ToString(), is_visible, is_collapsed, font_weight_override, font_color_override, ActualWidth, horizontal_alignment, horizontal_offset, background_color_override); void _set_font_param(ref T field, T value) { field = value; @@ -237,7 +243,7 @@ namespace mbts.ui.controls { if (settings == null) return; using (ReflectionHelper.detect_property_changes(this, _raise_prop_changed, nameof(is_visible), nameof(is_collapsed), nameof(font_weight_override), nameof(font_weight), - nameof(font_color_override), nameof(font_color), nameof(font_brush), nameof(font_family), nameof(font_size), nameof(horizontal_alignment), nameof(horizontal_offset))) { + nameof(font_color_override), nameof(font_color), nameof(font_brush), nameof(font_family), nameof(font_size), nameof(horizontal_alignment), nameof(horizontal_offset), nameof(background_color_override), nameof(background_color), nameof(background_brush))) { // id = settings.id; if (!string.IsNullOrEmpty(settings.display_name)) @@ -248,6 +254,7 @@ namespace mbts.ui.controls { font_color_override = settings.font_color_override; horizontal_alignment = settings.horizontal_alignment; horizontal_offset = settings.horizontal_offset; + background_color_override = settings.background_color_override; } if (original_width != settings.width) { original_width = settings.width; @@ -257,11 +264,12 @@ namespace mbts.ui.controls { } // 'fallback' font-params (aka 'grid-level' font-params) - public void set_fallback_font_params(FontFamily font_family, double font_size, Color font_color, FontWeight font_weight) { - using (ReflectionHelper.detect_property_changes(this, _raise_prop_changed, nameof(font_family), nameof(font_size), nameof(font_color), nameof(font_weight))) { + public void set_fallback_font_params(FontFamily font_family, double font_size, Color font_color, FontWeight font_weight, Color background_color) { + using (ReflectionHelper.detect_property_changes(this, _raise_prop_changed, nameof(font_family), nameof(font_size), nameof(font_color), nameof(font_weight), nameof(background_color), nameof(background_brush))) { _fallback_font_family = font_family; _fallback_font_size = font_size; _fallback_font_color = font_color; + _fallback_background_color = background_color; _fallback_font_weight = font_weight; } font_params_changed?.Invoke(); @@ -275,9 +283,25 @@ namespace mbts.ui.controls { } internal abstract class ColumnBase : ColumnBase, IKeyedColumn, IGridColumn { + TColKey _key; + bool _key_initialized; + // need a default constructor to support declaration in xaml + protected ColumnBase() { } protected ColumnBase(TColKey key) => this.key = key; - public TColKey key { get; } + public TColKey key { + get => _key; + set { + if (Equals(_key, value)) + return; + Log.assert(!_key_initialized, "Column key already initialized."); + _key_initialized = true; + _key = value; + on_key_initialized(value); + } + } + + protected virtual void on_key_initialized(TColKey key) { } } } -} \ No newline at end of file +} diff --git a/src/client/spark/view/Controls/DIYGrid/Columns/FrameworkElementColumns/HyperlinkColumn.cs b/src/client/spark/view/Controls/DIYGrid/Columns/FrameworkElementColumns/HyperlinkColumn.cs index 258740e29..2b6b62bf0 100644 --- a/src/client/spark/view/Controls/DIYGrid/Columns/FrameworkElementColumns/HyperlinkColumn.cs +++ b/src/client/spark/view/Controls/DIYGrid/Columns/FrameworkElementColumns/HyperlinkColumn.cs @@ -7,7 +7,7 @@ using System.Windows.Media; namespace mbts.ui.controls { partial class DIYGrid { - internal class HyperlinkElementColumn : FrameworkElementColumnBase, IFrameworkElementColumn, IFilterableColumn, IKeyedColumn, ISelectionHighlightListener { + internal class HyperlinkElementColumn : FrameworkElementColumnBase, IFrameworkElementColumn, IFilterableColumn, IKeyedColumn { static readonly Thickness _default_padding = new Thickness(6, 0, 6, 0); readonly BindingBase _binding, _command_binding; @@ -22,27 +22,17 @@ namespace mbts.ui.controls { public IDataGridColumnFilter filter { get; } public string filter_template { get; } public Thickness padding { get; set; } = _default_padding; - public Brush background { get; set; } - public Brush background_overlay { get; set; } = DefaultSelectionHighlight; - - public void on_selection_fill(Brush selection_fill) => - background_overlay = selection_fill; - + public override FrameworkElement view_factory() { - var hl = new HyperlinkCell { Foreground = font_brush, Background = background, Padding = padding }; + var hl = new HyperlinkCell { Padding = padding }; + hl.SetBinding(HyperlinkCell.ForegroundProperty, new Binding(nameof(font_brush)) { Source = this }); hl.SetBinding(HyperlinkCell.TextProperty, _binding); hl.SetBinding(HyperlinkCell.FontFamilyProperty, new Binding(nameof(font_family)) { Source = this }); hl.SetBinding(HyperlinkCell.FontSizeProperty, new Binding(nameof(font_size)) { Source = this }); hl.SetBinding(HyperlinkCell.HorizontalAlignmentProperty, new Binding(nameof(horizontal_alignment)) { Source = this }); hl.SetBinding(HyperlinkCell.CommandProperty, _command_binding); hl.SetBinding(HyperlinkCell.WidthProperty, new Binding(nameof(ActualWidth)) { Source = this }); - if (background_overlay != null) { - hl.SetBinding(BackgroundOverlayProperty, new Binding(nameof(ListBoxItem.IsSelected)) { - Converter = new AnonymousValueConverter(val => val ? background_overlay : null), - Mode = BindingMode.OneWay, - RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(ListBoxItem), 1) - }); - } + hl.SetBinding(HyperlinkCell.BackgroundProperty, new Binding(nameof(background_brush)) { Source = this }); return hl; } } diff --git a/src/client/spark/view/Controls/DIYGrid/Columns/RenderColumn.cs b/src/client/spark/view/Controls/DIYGrid/Columns/RenderColumn.cs index 4c58bc40d..f5779e1e5 100644 --- a/src/client/spark/view/Controls/DIYGrid/Columns/RenderColumn.cs +++ b/src/client/spark/view/Controls/DIYGrid/Columns/RenderColumn.cs @@ -30,7 +30,7 @@ namespace mbts.ui.controls { Func get_brush = null) : base() { _get_text = get_text; - Func get_default_background_brush = (TRowVM _) => this.background; + Func get_default_background_brush = (TRowVM _) => this.background_brush; _get_background_brush = get_brush ?? get_default_background_brush; this.filter = filter; this.filter_template = filter_template; @@ -42,7 +42,6 @@ namespace mbts.ui.controls { public IDataGridColumnFilter filter { get; } public string filter_template { get; } - public Brush background { get; set; } public VerticalAlignment vertical_alignment { get; set; } = VerticalAlignment.Center; public Thickness padding { get; set; } = DefaultCellPadding; public GlyphContext glyph_context { get; private set; } diff --git a/src/client/spark/view/Controls/DIYGrid/DIYGrid.cs b/src/client/spark/view/Controls/DIYGrid/DIYGrid.cs index fda5d6cc6..ab0ae6d35 100644 --- a/src/client/spark/view/Controls/DIYGrid/DIYGrid.cs +++ b/src/client/spark/view/Controls/DIYGrid/DIYGrid.cs @@ -1,9 +1,9 @@ using bts; +using bts.utils; using mbts.spark.filters; using mbts.ui.controls.adorners; using mbts.ui.extensions; using mbts.ui.utility; -using bts.utils; using System; using System.Collections; using System.Collections.Generic; @@ -12,14 +12,13 @@ using System.Collections.Specialized; using System.ComponentModel; using System.Diagnostics; using System.Linq; +using System.Reactive.Linq; using System.Windows; using System.Windows.Controls; using System.Windows.Controls.Primitives; using System.Windows.Data; using System.Windows.Input; using System.Windows.Media; -using mbts.utils; -using System.Reactive.Linq; namespace mbts.ui.controls { interface IHandleGridSorting { @@ -126,6 +125,7 @@ namespace mbts.ui.controls { double _row_height = DIYGrid.DefaultRowHeight; DIYGrid _grid; DragRectAdorner _drag_rect_adorner; + RowSelectionAdorner _rsa; ScrollViewer _scroll_viewer; Point? _drag_start; double _drag_start_vertical_drift; @@ -148,12 +148,18 @@ namespace mbts.ui.controls { _drag_rect_adorner.SetResourceReference(DragRectAdorner.StrokeProperty, DIYGrid.SelectionStrokeKey); _drag_rect_adorner.attach(); } + _rsa = new RowSelectionAdorner(this, _scroll_viewer); + _rsa.attach(); }; Unloaded += (s, e) => { if (_drag_rect_adorner != null) { _drag_rect_adorner.detach(); Disposable.dispose(ref _drag_rect_adorner); } + if (_rsa != null) { + _rsa.detach(); + Disposable.dispose(ref _rsa); + } }; void on_row_height_changed(double v) => _row_height = v; } @@ -261,6 +267,12 @@ namespace mbts.ui.controls { o.Resources[SelectionStrokeKey] = new Pen(o.SelectionHighlight.Color.to_solid_brush().frozen(), 1).frozen(); foreach (var col in o.Columns.empty_if_null().OfType()) col.on_selection_fill(v); + var ad_layer = System.Windows.Documents.AdornerLayer.GetAdornerLayer(o); + if (ad_layer != null) { + foreach (var ad in ad_layer.descendants_of_type()) { + ad.on_selection_fill(v); + } + } }, (o, v) => v as SolidColorBrush ?? DefaultSelectionHighlight, DefaultSelectionHighlight); @@ -562,6 +574,11 @@ namespace mbts.ui.controls { public static readonly DependencyProperty HeaderTotalSizeHackProperty = DependencyProperty.Register("HeaderTotalSizeHack", typeof(double), typeof(DIYGrid), new FrameworkPropertyMetadata(0.0, dp_invalidate_column_layout)); + public double HeaderTotalSizeHack { + get => (double)GetValue(HeaderTotalSizeHackProperty); + set => SetValue(HeaderTotalSizeHackProperty, value); + } + private static void dp_invalidate_column_layout(DependencyObject d, DependencyPropertyChangedEventArgs unused) { var grid = d as DIYGrid; if (grid != null) { @@ -594,6 +611,61 @@ namespace mbts.ui.controls { // Public Properties #region Public Properties + #region ColumnKey + public static readonly DependencyProperty ColumnKeyProperty = + DependencyProperty.RegisterAttached("ColumnKey", + typeof(object), + typeof(DIYGrid), + new PropertyMetadata((d, e) => { + if (!(d is UIElement child)) + return; + if (VisualTreeHelper.GetParent(child) is Grid panel) { + if (GetSyncWithColumnGeometrySource(d) is DIYGrid diy_grid || panel.try_find_ancestor_of_type(out diy_grid)) + ColumnGeometrySyncHelper.reset_panel_child(child, diy_grid, e.NewValue); + } + })); + + public static object GetColumnKey(DependencyObject d) => d.GetValue(ColumnKeyProperty); + public static void SetColumnKey(DependencyObject d, object value) => d.SetValue(ColumnKeyProperty, value); + #endregion + + #region SyncWithColumnGeometrySource + public static readonly DependencyProperty SyncWithColumnGeometrySourceProperty = + DependencyProperty.RegisterAttached( + "SyncWithColumnGeometrySource", + typeof(DIYGrid), + typeof(DIYGrid), + new PropertyMetadata(ColumnGeometrySyncHelper.reset_sync)); + + public static DIYGrid GetSyncWithColumnGeometrySource(DependencyObject d) => d.GetValue(SyncWithColumnGeometrySourceProperty) as DIYGrid; + public static void SetSyncWithColumnGeometrySource(DependencyObject d, DIYGrid value) => d.SetValue(SyncWithColumnGeometrySourceProperty, value); + #endregion + + #region SyncWithColumnGeometry + public static readonly DependencyProperty SyncWithColumnGeometryProperty = + DependencyProperty.RegisterAttached( + "SyncWithColumnGeometry", + typeof(bool), + typeof(DIYGrid), + new PropertyMetadata(false, ColumnGeometrySyncHelper.reset_sync)); + + public static bool GetSyncWithColumnGeometry(DependencyObject d) => (bool)d.GetValue(SyncWithColumnGeometryProperty); + public static void SetSyncWithColumnGeometry(DependencyObject d, bool value) => d.SetValue(SyncWithColumnGeometryProperty, value); + + static readonly DependencyPropertyKey SyncWithColumnGeometryHookPropertyKey = + DependencyProperty.RegisterAttachedReadOnly( + "SyncWithColumnGeometryHook", + typeof(IDisposable), + typeof(DIYGrid), + new PropertyMetadata(FrameworkElementLifeDependencyHelper.DisposeOld)); + + internal static IDisposable GetSyncWithColumnGeometryHook(DependencyObject d) => + d.GetValue(SyncWithColumnGeometryHookPropertyKey.DependencyProperty) as IDisposable; + + static void SetSyncWithColumnGeometryHook(DependencyObject d, IDisposable value) => + d.SetValue(SyncWithColumnGeometryHookPropertyKey, value); + #endregion + public static readonly DependencyProperty BackgroundOverlayProperty = DependencyProperty.RegisterAttached( "BackgroundOverlay", @@ -783,7 +855,7 @@ namespace mbts.ui.controls { /// Compute the geometry of all visible (or partially visible columns) columns /// This effectively replaces the entire Measure/Arrange pass of the grid rows (and enables column virtualization) /// Mouse events will be hit tested against this as well - internal IEnumerable GetColumnGeometry() { + internal IEnumerable GetColumnGeometry(bool include_obscured_cols = false) { if (_header_scroll_viewer != null && ActualWidth > 0) { double frozen_offset = 0; int vis_frozen_col_count = _get_visible_frozen_column_count(); @@ -805,7 +877,7 @@ namespace mbts.ui.controls { double left = Math.Max(offset, frozen_offset); double right = offset + col.ActualWidth; // If right < frozen_offset column is completely obscured - if (right > frozen_offset && left <= ActualWidth) { + if (include_obscured_cols || (right > frozen_offset && left <= ActualWidth)) { // fully visible yield return new ColumnGeometry() { column = col, @@ -883,6 +955,98 @@ namespace mbts.ui.controls { } #endregion + static class ColumnGeometrySyncHelper { + public static void reset_panel_child(UIElement child, DIYGrid diy_grid) => + reset_panel_child(child, diy_grid, GetColumnKey(child)); + + public static void reset_panel_child(UIElement child, DIYGrid diy_grid, object col_key) { + if (col_key == null) + return; + var key_str = col_key.ToString(); + if (diy_grid._visible_columns.TryGetFirst(c => string.Equals(c.id, key_str, StringComparison.CurrentCultureIgnoreCase), out var target_col, out int target_pos)) { + // set clip to null in case this child was hidden before (see 'else' block). + child.Clip = null; + Grid.SetColumn(child, target_pos); + } else { + // hide element by setting clip to 0x0 rect, rather than visibility to collapsed, + // to avoid incidentally breaking visibility bindings, triggers, animations, etc. + child.Clip = new RectangleGeometry(new Rect(0, 0, 0, 0)); + } + } + + public static void reset_sync(DependencyObject d, DependencyPropertyChangedEventArgs e) { + if (!GetSyncWithColumnGeometry(d)) + SetSyncWithColumnGeometryHook(d, null); + if (!(d is Grid g)) + return; + var hook = new DisposableContainer(); + if (g.IsLoaded) + hook.replace(impl()); + SetSyncWithColumnGeometryHook(d, Disposable.Create(g.SubscribeLoaded(on_loaded), hook)); + // + void on_loaded(object sender, RoutedEventArgs loaded_e) { + if (GetSyncWithColumnGeometry(d)) + hook.replace(impl()); + } + IDisposable impl() { + // Avoid resetting children on every collection change, as that could be very noisy and hurt perf. + // In the visible columns CollectionChanged handler, call reset_children inside a Dispatcher.InvokeAsync request + // to effectively wait for potential batch-changes to finish. + bool resetting_children = false; + if (!(GetSyncWithColumnGeometrySource(d) is DIYGrid diy_grid) && !g.try_find_ancestor_of_type(out diy_grid)) + return null; + g.SetBinding(Grid.WidthProperty, new Binding(nameof(HeaderTotalSizeHack)) { Source = diy_grid }); + reset_children(); + on_layout_changed(diy_grid.GetColumnGeometry(true).ToList()); + diy_grid.ColumnLayoutChanged += on_layout_changed; + diy_grid._visible_columns.CollectionChanged += on_vis_columns_changed; + return Disposable.Create(() => { + diy_grid.ColumnLayoutChanged -= on_layout_changed; + diy_grid._visible_columns.CollectionChanged -= on_vis_columns_changed; + }); + // + void on_vis_columns_changed(object sender, NotifyCollectionChangedEventArgs vce) { + if (!resetting_children) { + // avoid resetting children on every collection change, as that could be very noisy and hurt perf. + resetting_children = true; + diy_grid.Dispatcher.InvokeAsync(() => { + on_layout_changed(diy_grid.GetColumnGeometry().ToList()); + reset_children(); + resetting_children = false; + }); + } + } + void on_layout_changed(List layout) { + // TODO: figure out how to make the ColumnLayoutChanged event lazy, or somehow avoid making 2 lists of columns + // (the layout param does not include obscured cols, so we have to create a new list of ColumnGeoemtry to satisfy our needs). + layout = (from col in diy_grid._visible_columns + join pos in diy_grid.GetColumnGeometry(true) + on col.id equals pos.column.id + select pos).ToList(); + + int n_removed = g.ColumnDefinitions.Count - layout.Count; + if (n_removed > 0) + g.ColumnDefinitions.RemoveRange(g.ColumnDefinitions.Count - n_removed, n_removed); + int i = 0; + for (; i < g.ColumnDefinitions.Count; i++) { + var col_pos = layout[i]; + var col = g.ColumnDefinitions[i]; + if (col.Width.GridUnitType == GridUnitType.Pixel && col.Width.Value == col_pos.visible_width) + continue; + col.Width = new GridLength(col_pos.visible_width, GridUnitType.Pixel); + } + for (; i < layout.Count; i++) { + g.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(layout[i].visible_width, GridUnitType.Pixel) }); + } + } + void reset_children() { + foreach (UIElement child in g.Children) + reset_panel_child(child, diy_grid); + } + } + } + } + class DataContextHooks : IDisposable { List _hooks = new List(4); diff --git a/src/client/spark/view/Controls/DIYGrid/DIYGridResources.xaml b/src/client/spark/view/Controls/DIYGrid/DIYGridResources.xaml index d066fbb96..b4a77bb97 100644 --- a/src/client/spark/view/Controls/DIYGrid/DIYGridResources.xaml +++ b/src/client/spark/view/Controls/DIYGrid/DIYGridResources.xaml @@ -29,22 +29,6 @@ VerticalAlignment="{TemplateBinding VerticalContentAlignment}" /> - - - - - - - - - - - - - - - - @@ -61,6 +45,9 @@ diff --git a/src/client/spark/view/Resources/Converters.xaml b/src/client/spark/view/Resources/Converters.xaml index 12ef41b11..19601cc6c 100644 --- a/src/client/spark/view/Resources/Converters.xaml +++ b/src/client/spark/view/Resources/Converters.xaml @@ -21,6 +21,7 @@ + diff --git a/src/client/spark/view/Resources/WindowStyle.xaml b/src/client/spark/view/Resources/WindowStyle.xaml index e5cfbeda1..a5b05d21e 100644 --- a/src/client/spark/view/Resources/WindowStyle.xaml +++ b/src/client/spark/view/Resources/WindowStyle.xaml @@ -143,6 +143,7 @@ + #FFAAD1FF + + + + + + + + + + + (N/A) + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/client/spark/view/Shell/Workspaces/WorkspacesDialogViewModel.cs b/src/client/spark/view/Shell/Workspaces/WorkspacesDialogViewModel.cs index 80282e25e..72c80dc7d 100644 --- a/src/client/spark/view/Shell/Workspaces/WorkspacesDialogViewModel.cs +++ b/src/client/spark/view/Shell/Workspaces/WorkspacesDialogViewModel.cs @@ -8,12 +8,15 @@ using System.Threading.Tasks; using mbts; using mbts.ui; using mbts.ui.shell; +using bts.spark; +using System.Windows; namespace mbts.ui.shell { public class WorkspacesDialogViewModel : ViewModelBaseWithValidation { private WorkspacesModel _work_model; + IRaiseCanExecuteChangedCommand _load_command, _delete_command, _save_command; public WorkspacesDialogViewModel(WorkspacesModel wm) { @@ -25,36 +28,74 @@ namespace mbts.ui.shell public bool IsPublicWorkspace { get; set; } public bool ShowWorkspaces { get; set; } - public ICommand SaveWorkspace + WorkspaceViewModel _selected_workspace; + public WorkspaceViewModel selected_workspace { + get => _selected_workspace; + set { + if (Set(ref _selected_workspace, value)) { + LoadCommand.TryRaiseCanExecuteChanged(); + DeleteCommand.TryRaiseCanExecuteChanged(); + SaveCommand.TryRaiseCanExecuteChanged(); + } + } + } + + private bool _command_can_execute() => selected_workspace != null; + private void _clear_error_state() => clear_error(nameof(SaveName)); + + public ICommand LoadCommand => _load_command ?? + (_load_command = new DelegateCommand( + () => { + _work_model.load_workspace(selected_workspace.Definition, selected_workspace.Name).ignore(); + _clear_error_state(); + }, _command_can_execute)); + public ICommand DeleteCommand => _delete_command ?? + (_delete_command = new DelegateCommand( + () => { + if (BTSMessageBox.Show( + $"Delete the \"{selected_workspace.Name}\" workspace?", + "Delete Existing Workspace Confirmation", + BTSMessageBoxButtons.Yes | BTSMessageBoxButtons.No, + MessageBoxImage.Question) == BTSMessageBoxButtons.Yes) { + _work_model.delete_workspace(selected_workspace.Name, selected_workspace.Definition).ignore(); + _clear_error_state(); + } + }, _command_can_execute)); + public ICommand SaveCommand => _save_command ?? + (_save_command = new DelegateCommand( + () => { + if (BTSMessageBox.Show( + $"Overwrite \"{selected_workspace.Name}\" with current layout?", + "Save Existing Workspace Confirmation", + BTSMessageBoxButtons.Yes | BTSMessageBoxButtons.No, + MessageBoxImage.Question) == BTSMessageBoxButtons.Yes) { + _work_model.save_workspace(selected_workspace.Name, ispublic:selected_workspace.Definition.is_shared); + _clear_error_state(); + } + }, _command_can_execute)); + + public ICommand SaveNewWorkspace { get { return new DelegateCommand(arg => { + _clear_error_state(); // Check for errors in the input - if (string.IsNullOrEmpty(SaveName)) - { - set_error("SaveName", "Workspace must be named"); + if (string.IsNullOrEmpty(SaveName)) { + set_error(nameof(SaveName), "Workspace must be named"); return; } - if (SaveName.Contains(WorkspacesModel.ACTIVE_WORKSPACE_PREFIX)) - { - set_error("SaveName", "Workspace name cannot contains '{0}'".fmt(WorkspacesModel.ACTIVE_WORKSPACE_PREFIX)); + if (SaveName.Contains(WorkspacesModel.ACTIVE_WORKSPACE_PREFIX)) { + set_error(nameof(SaveName), "Workspace name cannot contains '{0}'".fmt(WorkspacesModel.ACTIVE_WORKSPACE_PREFIX)); return; } - else - { - clear_error("SaveName"); + if (Workspaces.Any(x => x.Name.Replace(WorkspacesModel.SHARED_MONIKER, string.Empty) == SaveName && x.IsShared == IsPublicWorkspace)) { + set_error(nameof(SaveName), $"Workspace already exists '{SaveName}'"); + return; } - var save_candidate_already_exists = Workspaces.Any(x => x.Name.Replace(WorkspacesModel.SHARED_MONIKER, string.Empty) == SaveName && x.IsShared == IsPublicWorkspace); - // Save _work_model.save_workspace(SaveName, ispublic: IsPublicWorkspace); - - // if we just overwrote something, have the vm reflect it - if (save_candidate_already_exists) - Workspaces.Where(x => x.Name.Replace(WorkspacesModel.SHARED_MONIKER, string.Empty) == SaveName && x.IsShared == IsPublicWorkspace).ForEach(x => x.VisualState = "Overridden"); }); } } - } } diff --git a/src/client/spark/view/Shell/Workspaces/WorkspacesModel.cs b/src/client/spark/view/Shell/Workspaces/WorkspacesModel.cs index 867c34b18..7f55387d7 100644 --- a/src/client/spark/view/Shell/Workspaces/WorkspacesModel.cs +++ b/src/client/spark/view/Shell/Workspaces/WorkspacesModel.cs @@ -179,6 +179,7 @@ namespace mbts.ui.shell public void save_workspace(string name, bool persist=true, bool ispublic=false) { WorkspaceDefinition workspace = get_workspace(); workspace.is_shared = ispublic; + workspace.last_save_lldatetime = LLDateTime.now().value; if (ispublic) { name += SHARED_MONIKER; } // Remove the old one if a workspace by this name exists already @@ -282,5 +283,8 @@ namespace mbts.ui.shell [ProtoMember(10, IsRequired = false)] public bool is_shared { get; set; } + [ProtoMember(11, IsRequired = false)] + public ulong last_save_lldatetime { get; set; } + } } diff --git a/src/client/spark/view/Spark/Core/SparkUtils.cs b/src/client/spark/view/Spark/Core/SparkUtils.cs index 56d1c55d5..0c063761f 100644 --- a/src/client/spark/view/Spark/Core/SparkUtils.cs +++ b/src/client/spark/view/Spark/Core/SparkUtils.cs @@ -91,6 +91,10 @@ namespace mbts.spark { public static double sod_pos(this PositionUnitImmutable position_unit) => position_unit.SodLongPos - position_unit.SodShortPos; public static double day_pos(this PositionUnitImmutable position_unit) => position_unit.DayLongPos - position_unit.DayShortPos; public static double total_pos(this PositionUnitImmutable position_unit) => position_unit.sod_pos() + position_unit.day_pos(); + // raw day + public static double raw_day_long_pos(this PositionUnitImmutable position_unit) => position_unit.HasRawDay ? position_unit.RawDay.LongPos : double.NaN; + public static double raw_day_short_pos(this PositionUnitImmutable position_unit) => position_unit.HasRawDay ? position_unit.RawDay.ShortPos : double.NaN; + public static double raw_day_pos(this PositionUnitImmutable position_unit) => position_unit.HasRawDay ? position_unit.RawDay.LongPos - position_unit.RawDay.ShortPos : double.NaN; public static void lazy_assert(bool test, Func get_msg) { if (!test) diff --git a/src/client/spark/view/Spark/Core/TickUtils.cs b/src/client/spark/view/Spark/Core/TickUtils.cs index 8144dd74a..4cf1dcd37 100644 --- a/src/client/spark/view/Spark/Core/TickUtils.cs +++ b/src/client/spark/view/Spark/Core/TickUtils.cs @@ -197,6 +197,7 @@ namespace mbts.spark { static class TickStore { public static readonly RegionalTickFactors default_rtf = new RegionalTickFactors(new TickFactors(1000, 1), new TickFactors(1000, 1), double.MaxValue); static readonly ConcurrentDictionary _exchange_ticks = new ConcurrentDictionary(); + static readonly bool _som_ticks_fix_enabled = config.Configuration.get().enabled; public static RegionalTickFactors get(ContractSpecification spec) => _exchange_ticks.GetOrAdd(new SpecKey(spec), _create_rtf); @@ -249,8 +250,23 @@ namespace mbts.spark { var tf = _try_create_tf(tick_size, tick_base); if (tf != null && display_digits_override > 0) { int display_digit_diff = (int)display_digits_override - tf.display_digits(); - if (display_digit_diff != 0) + if (_som_ticks_fix_enabled) { + // we can always add display_digits (i.e. dislpay_digit_diff > 0), of course the ticks can be expressed precisely with extra 0s. + // but if we are going to remove display_digits (i.e display_digit_diff < 0), we must make sure the tick_unit is large enough to be expressed precisely with that many fewer digits + // - "-display_digit_diff <= Math.Ceiling(Math.Log10(tf.tick_unit))" will tell us if the digits can be removed without compromising precision + // - unfortunately, we cannot rely on the exchange's ContractSpec for this, there is 1 known case where the data they give us is incorrect + // SOM spreads ContractSpec (from the exchange): + // LowTick: 0.0025 (aka 1/400, or 2/800) + // MainFraction: 8 + // SubFraction: 0 + // PriceDisplayFormat: 1 + // - as you can see, this cannot possibly be formatted with 1 display digit, it ticks in increments of 1/400 + // - in practice, to maintain the '8' base, we format this in increments of 2/800 (e.g. 1-000, 1-002, 1-004, and so on up to 1-778, 2-000) + if (display_digit_diff > 0 || (display_digit_diff < 0 && -display_digit_diff <= Math.Ceiling(Math.Log10(tf.tick_unit)))) + tf = _inflate_to_base(tf, tf.tick_base * Math.Pow(10, display_digit_diff)); + } else if (display_digit_diff != 0) { tf = _inflate_to_base(tf, tf.tick_base * Math.Pow(10, display_digit_diff)); + } } return tf; } diff --git a/src/client/spark/view/Spark/PAndL/PAndLVM.cs b/src/client/spark/view/Spark/PAndL/PAndLVM.cs index c97d20f9a..76287417e 100644 --- a/src/client/spark/view/Spark/PAndL/PAndLVM.cs +++ b/src/client/spark/view/Spark/PAndL/PAndLVM.cs @@ -65,6 +65,8 @@ namespace mbts.spark { grouping.Where(y => y.fill.FillKind != Fill.Types.FillKind.Sod).Sum(y => (uint)y.sell_quantity), grouping.Sum(y => (int)(y.buy_quantity - y.sell_quantity)), grouping.Where(y => y.fill.FillKind == Fill.Types.FillKind.Sod).Sum(y => (int)(y.buy_quantity - y.sell_quantity)), + double.NaN, + double.NaN, grouping.weighted_average(y => y.fill.LastPrice, y => y.buy_quantity), grouping.weighted_average(y => y.fill.LastPrice, y => y.sell_quantity), grouping.Select(i => i.fill), @@ -95,6 +97,8 @@ namespace mbts.spark { (uint)pos.DayShortPos, (int)pos.total_pos(), (int)pos.sod_pos(), + pos.raw_day_long_pos(), + pos.raw_day_short_pos(), pos.DayLongAvgPrice, pos.DayShortAvgPrice, null, @@ -104,7 +108,7 @@ namespace mbts.spark { // remove rows for which there is no net pos or no day pos summary_position_rows = summary_position_rows.Where(r => - !(r.start_net_position == 0 && r.day_long_quantity == 0 && r.day_short_quantity == 0) + !(r.start_net_position == 0 && r.day_long_quantity == 0 && r.day_short_quantity == 0 && r.raw_day_long_position == 0 && r.raw_day_short_position == 0) ); _pnl_data.sync_with(summary_position_rows); _last_pnl_update_time = DateTime.Now; @@ -124,6 +128,7 @@ namespace mbts.spark { start_pnl_filter.load_state(fs.start_pnl); day_long_quantity_filter.load_state(fs.day_long_pos); day_short_quantity_filter.load_state(fs.day_short_pos); + raw_day_net_position_filter.load_state(fs.raw_day_net_pos); day_pnl_filter.load_state(fs.day_pnl); total_net_position_filter.load_state(fs.total_net_pos); total_pnl_filter.load_state(fs.total_pnl); @@ -146,6 +151,8 @@ namespace mbts.spark { add_col(Columns.StartPNL, DefaultColumnHeaders.StartPnL, r => r.start_pnl_text, nameof(PnLSummaryRow.start_pnl), DefaultColumnWidths.PnL_Generic, start_pnl_filter, DataGridFilterKnownTemplates.DoubleMinMax, HorizontalAlignment.Right, r => r.start_pnl_background); add_col(Columns.DayLong, DefaultColumnHeaders.DayLong, r => r.day_long_quantity.ToString(), nameof(PnLSummaryRow.day_long_quantity), DefaultColumnWidths.PnL_Generic, day_long_quantity_filter, DataGridFilterKnownTemplates.UIntMinMax, HorizontalAlignment.Right); add_col(Columns.DayShort, DefaultColumnHeaders.DayShort, r => r.day_short_quantity.ToString(), nameof(PnLSummaryRow.day_short_quantity), DefaultColumnWidths.PnL_Generic, day_short_quantity_filter, DataGridFilterKnownTemplates.UIntMinMax, HorizontalAlignment.Right); + // convert nan to 0 so text is consistent with other position columns + add_col(Columns.RawDayPos, DefaultColumnHeaders.RawDayPos, r => SparkUtils.fmt_position(r.raw_day_net_position), nameof(PnLSummaryRow.raw_day_net_position), DefaultColumnWidths.PnL_Generic, raw_day_net_position_filter, DataGridFilterKnownTemplates.DoubleMinMax, HorizontalAlignment.Right); add_col(Columns.DayPNL, DefaultColumnHeaders.DayPnL, r => r.day_pnl_text, nameof(PnLSummaryRow.day_pnl), 90, day_pnl_filter, DataGridFilterKnownTemplates.DoubleMinMax, HorizontalAlignment.Right, r => r.day_pnl_background); add_col(Columns.TradedToday, "Traded Today", r => r.has_day_trades_text, nameof(PnLSummaryRow.has_day_trades), 100, has_day_trades_filter, DataGridFilterKnownTemplates.YesNo, HorizontalAlignment.Center); add_col(Columns.TotalNetPos, DefaultColumnHeaders.TotalNetPos, r => r.total_net_position.ToString(), nameof(PnLSummaryRow.total_net_position), DefaultColumnWidths.Total_Net_Pos, total_net_position_filter, DataGridFilterKnownTemplates.DoubleMinMax, HorizontalAlignment.Right); @@ -188,6 +195,7 @@ namespace mbts.spark { start_pnl_filter, day_long_quantity_filter, day_short_quantity_filter, + raw_day_net_position_filter, day_pnl_filter, total_net_position_filter, total_pnl_filter, @@ -311,6 +319,7 @@ namespace mbts.spark { public IDataGridColumnPersistentFilter start_pnl_filter { get; } = new DoubleMinMax(r => r.start_pnl, nameof(PnLSummaryRow.start_pnl)); public IDataGridColumnPersistentFilter day_long_quantity_filter { get; } = new UIntMinMax(r => r.day_long_quantity, nameof(PnLSummaryRow.day_long_quantity)); public IDataGridColumnPersistentFilter day_short_quantity_filter { get; } = new UIntMinMax(r => r.day_short_quantity, nameof(PnLSummaryRow.day_short_quantity)); + public IDataGridColumnPersistentFilter raw_day_net_position_filter { get; } = new DoubleMinMax(r => r.raw_day_net_position, nameof(PnLSummaryRow.raw_day_net_position)); public IDataGridColumnPersistentFilter day_pnl_filter { get; } = new DoubleMinMax(r => r.day_pnl, nameof(PnLSummaryRow.day_pnl)); public IDataGridColumnPersistentFilter total_net_position_filter { get; } = new IntMinMax(r => r.total_net_position, nameof(PnLSummaryRow.total_net_position)); public IDataGridColumnPersistentFilter total_pnl_filter { get; } = new DoubleMinMax(r => r.total_pnl, nameof(PnLSummaryRow.total_pnl)); @@ -329,6 +338,7 @@ namespace mbts.spark { start_pnl = start_pnl_filter.get_state(), day_long_pos = day_long_quantity_filter.get_state(), day_short_pos = day_short_quantity_filter.get_state(), + raw_day_net_pos = raw_day_net_position_filter.get_state(), day_pnl = day_pnl_filter.get_state(), total_net_pos = total_net_position_filter.get_state(), total_pnl = total_pnl_filter.get_state(), diff --git a/src/client/spark/view/Spark/PAndL/PAndLView.xaml.cs b/src/client/spark/view/Spark/PAndL/PAndLView.xaml.cs index 04ceb59d0..d9dce989d 100644 --- a/src/client/spark/view/Spark/PAndL/PAndLView.xaml.cs +++ b/src/client/spark/view/Spark/PAndL/PAndLView.xaml.cs @@ -3,7 +3,7 @@ using System.Windows.Controls; namespace mbts.spark { internal partial class PAndLView : UserControl { - public enum Columns { Account, Exchange, Symbol, Security, SecurityType, StartNetPos, StartPNL, DayLong, DayShort, DayPNL, TotalNetPos, TotalPNL, PNLPrice, TradedToday } + public enum Columns { Account, Exchange, Symbol, Security, SecurityType, StartNetPos, StartPNL, DayLong, DayShort, DayPNL, TotalNetPos, TotalPNL, PNLPrice, TradedToday, RawDayPos } public PAndLView(PAndLVM vm) { InitializeComponent(); diff --git a/src/client/spark/view/Spark/PAndL/PAndLWindow.cs b/src/client/spark/view/Spark/PAndL/PAndLWindow.cs index 760e365b9..2f74f84a0 100644 --- a/src/client/spark/view/Spark/PAndL/PAndLWindow.cs +++ b/src/client/spark/view/Spark/PAndL/PAndLWindow.cs @@ -24,7 +24,8 @@ namespace mbts.spark { PAndLWindowFactory _factory; IReadOnlyList _commands; - public PAndLWindow(OrdersModel orders_model, PAndLWindowFactory factory, IBus bus, PAndLWindowFactory.PAndLStartupData startup_data, ImageSource Icon, WindowSettingsManager settings_manager) : base("P&L", bus, factory, SparkCommands.ShowPAndL.icon) { + public PAndLWindow(OrdersModel orders_model, PAndLWindowFactory factory, IBus bus, PAndLWindowFactory.PAndLStartupData startup_data, ImageSource Icon, IWindowSettingsManager settings_manager) + : base("P&L", bus, factory, SparkCommands.ShowPAndL.icon) { _orders_model = orders_model; _factory = factory; _startup_data = startup_data; diff --git a/src/client/spark/view/Spark/PAndL/PnLSummaryRow.cs b/src/client/spark/view/Spark/PAndL/PnLSummaryRow.cs index d919bcad1..10b29d470 100644 --- a/src/client/spark/view/Spark/PAndL/PnLSummaryRow.cs +++ b/src/client/spark/view/Spark/PAndL/PnLSummaryRow.cs @@ -30,7 +30,16 @@ namespace mbts.spark { public PnLSummaryRow(pnl_calc_type calc_type, (string, Venue?, string, SecurityImmutable?, SecurityMD?, SecurityType?) key, - uint day_buy_quant, uint day_sell_quant, int net_pos, int sod_pos, double wavg_buy, double wavg_sell, IEnumerable fills, PositionUnitImmutable? pu) { + uint day_buy_quant, + uint day_sell_quant, + int net_pos, + int sod_pos, + double raw_day_long, + double raw_day_short, + double wavg_buy, + double wavg_sell, + IEnumerable fills, + PositionUnitImmutable? pu) { this.key = key; _start_pnl = new PnLDoubleBrushCell(this, nameof(start_pnl)); _day_pnl = new PnLDoubleBrushCell(this, nameof(day_pnl)); @@ -39,6 +48,8 @@ namespace mbts.spark { day_short_quantity = day_sell_quant; total_net_position = net_pos; start_net_position = sod_pos; + raw_day_long_position = raw_day_long; + raw_day_short_position = raw_day_short; _pnl_price = new CellProperty(this, nameof(pnl_price), price => this.security.spec_or_default().format(price), 0); _weighted_average_buy = new CellProperty(this, nameof(weighted_average_buy), price => this.security.spec_or_default().format(price), wavg_buy); _weighted_average_sell = new CellProperty(this, nameof(weighted_average_sell), price => this.security.spec_or_default().format(price), wavg_sell); @@ -136,6 +147,8 @@ namespace mbts.spark { key.symbol == other.symbol && day_long_quantity == other.day_long_quantity && day_short_quantity == other.day_short_quantity && + raw_day_long_position.Equals(other.raw_day_long_position) && + raw_day_short_position.Equals(other.raw_day_short_position) && total_pnl == other.total_pnl; } @@ -166,6 +179,9 @@ namespace mbts.spark { public int total_net_position { get; } public uint day_long_quantity { get; } public uint day_short_quantity { get; } + public double raw_day_long_position { get; } + public double raw_day_short_position { get; } + public double raw_day_net_position => raw_day_long_position - raw_day_short_position; // start pnl public double start_pnl => _start_pnl.value; public string start_pnl_text => _start_pnl.text; diff --git a/src/client/spark/view/Utility/EnumHelper.cs b/src/client/spark/view/Utility/EnumHelper.cs index 3fc0c8926..ff87cca95 100644 --- a/src/client/spark/view/Utility/EnumHelper.cs +++ b/src/client/spark/view/Utility/EnumHelper.cs @@ -79,7 +79,8 @@ namespace mbts.ui.utility { long lflags = Convert.ToInt64(flags); foreach (TEnum e in get_values()) { long lval = Convert.ToInt64(e); - if ((lval & lflags)!= 0) + long common_bits = lval & lflags; + if (common_bits != 0 && common_bits == lval) yield return e; } } diff --git a/src/client/spark/view/Utility/ThreadedWindowLauncher.cs b/src/client/spark/view/Utility/ThreadedWindowLauncher.cs new file mode 100644 index 000000000..2af677603 --- /dev/null +++ b/src/client/spark/view/Utility/ThreadedWindowLauncher.cs @@ -0,0 +1,121 @@ +using bts; +using mbts.utils; +using System; +using System.Diagnostics; +using System.Threading; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Threading; + +namespace mbts.ui.utilities { + class ThreadedWindowLauncher { + const int _min_thread_count = 1; + static readonly int _max_thread_count = Environment.ProcessorCount; + readonly string _name; + readonly int _thread_count; + int _counter; + DispatcherContainer[] _cache; + readonly LoggerContext _log; + + public ThreadedWindowLauncher(string name) + : this(name, _max_thread_count) { } + + public ThreadedWindowLauncher(string name, int thread_count) { + ExceptionAssert.Argument.Between(thread_count, _min_thread_count, _max_thread_count, nameof(thread_count)); + _log = new LoggerContext(Log.Ui, $"{nameof(ThreadedWindowLauncher)} {name ?? "(anon)"} ", null); + _name = name; + _thread_count = thread_count; + _cache = new DispatcherContainer[thread_count]; + } + + public void show(Func factory) => + _show(factory, Interlocked.Increment(ref _counter) % _thread_count); + + void _show(Func factory, int thread_id) { + var dc = _cache[thread_id]; + if (dc != null) { + if (Interlocked.Increment(ref dc.window_count) == 1) { + // If window_count was 0 (before incrementing), the dispatcher must be shutting down. + // Since its initial count is 1, the only way it could have reached 0 is if all windows were closed, which triggers a shutdown. + _init_dispatcher(dc); + } + } else { + // the container may have been assigned since we last checked (if another thread is simultaneously executing this code block). + var late_dc = Interlocked.Exchange(ref _cache[thread_id], dc = new DispatcherContainer(thread_id)); + if (late_dc == null) + _init_dispatcher(dc); + } + _show_on_dispatcher(dc, factory); + } + + async void _show_on_dispatcher(DispatcherContainer dc, Func factory) { + const int max_loops = 50; + int count = 0; + var stopwatch = new Stopwatch(); + stopwatch.Start(); + // dc.dispatcher is only assigned once, so it is safe to access it after a null check (despite this being multithreaded code) + while (dc.dispatcher == null || dc.dispatcher.HasShutdownStarted) { + await Task.Delay(100); + if (++count == max_loops) { + stopwatch.Stop(); + // give up + _log.warn($"failed to open window: timed out waiting for dispatcher (looped {count} times over the course of {stopwatch.Elapsed:mm\\:ss\\.ff})."); + return; + } + } + await dc.dispatcher.InvokeAsync(() => { + var win = factory(); + win.Closed += closed; + win.Show(); + void closed(object sender, EventArgs e) { + if (Interlocked.Decrement(ref dc.window_count) == 0 && !dc.dispatcher.HasShutdownStarted) + dc.dispatcher.BeginInvokeShutdown(DispatcherPriority.Background); + } + }); + } + + void _init_dispatcher(DispatcherContainer dc) { + // kick off the new thread, and setup the dispatcher + var thread = new Thread(start) { + IsBackground = true + }; + thread.SetApartmentState(ApartmentState.STA); + thread.Start(); + // + void start() { + dc.dispatcher = Dispatcher.CurrentDispatcher; + try { + dc.dispatcher.UnhandledException += on_dispatcher_unhandled_exception; + Dispatcher.Run(); + } catch (Exception ex) { + _log.warn($"dispatcher caught an exception (shutting down dispatcher): {ex}"); + dispose_core(); + } + void on_dispatcher_unhandled_exception(object sender, DispatcherUnhandledExceptionEventArgs e) { + BTSApplicationBootstrapper.CurrentDispatcher_HandleClipboardError(sender, e); + if (!e.Handled) { + _log.error($"unhandled exception in dispatcher: {e.Exception}"); + if (!e.Dispatcher.HasShutdownStarted) + e.Dispatcher.BeginInvokeShutdown(DispatcherPriority.Send); + } + } + void dispose_core() { + if (!dc.dispatcher.HasShutdownStarted) + dc.dispatcher.BeginInvokeShutdown(DispatcherPriority.Background); + } + } + } + + // This simple wrapper allows dispatchers to be 'read' atomically via Interlocked. + // Since creation of a new Dispatcher is asynchronous, the wrapper acts as a placeholder until it is finally assigned. + class DispatcherContainer { + public readonly int id; + + public DispatcherContainer(int id) => + this.id = id; + + public Dispatcher dispatcher { get; set; } + public int window_count = 1; + } + } +} \ No newline at end of file diff --git a/src/client/spark_api/api/ServiceContext.cs b/src/client/spark_api/api/ServiceContext.cs index eb30f7726..be8bf6c0a 100644 --- a/src/client/spark_api/api/ServiceContext.cs +++ b/src/client/spark_api/api/ServiceContext.cs @@ -13,6 +13,7 @@ namespace bts.api.core { AuthToken _token; public ServiceContext() { + BTS.init(); _qbus = ApiNativeMethods.api_create_quickbus(null, 0); } diff --git a/src/client/spark_api/utils/BTS.cs b/src/client/spark_api/utils/BTS.cs new file mode 100644 index 000000000..856365559 --- /dev/null +++ b/src/client/spark_api/utils/BTS.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading; + +namespace bts +{ + public static class BTS + { + static int _needs_init_spark_lib = 0; + public static void init() { + var args = Environment.GetCommandLineArgs(); + utils.BTSNativeMethods.provide_command_line(args, args.Length); + + var buffer = new StringBuilder(4096); + bool config_ok = utils.BTSNativeMethods.initialize_nothrow(buffer, buffer.Capacity); + if (!config_ok) { + throw new ApplicationException(buffer.ToString()); + } + + if (Interlocked.CompareExchange(ref _needs_init_spark_lib,1,0) == 0) { + utils.BTSNativeMethods.init_spark_lib(); + } + + // This seems like a good idea but it crashes due to thread shutdown order + // AppDomain.CurrentDomain.ProcessExit += (o, e) => { + // utils.BTSNativeMethods.finit_spark_lib(); + // }; + } + + internal static IEnumerable remove_config_arg(string[] args) { + int skip = 0; + foreach (var arg in args) { + if (arg.ToLower() == "-configserver") { + skip = 2; + } + if (skip > 0) { skip -= 1; } + else { yield return arg; } + } + } + } +} diff --git a/src/client/spark_api/utils/BTSNativeMethods.cs b/src/client/spark_api/utils/BTSNativeMethods.cs index cef374bba..66a562c39 100644 --- a/src/client/spark_api/utils/BTSNativeMethods.cs +++ b/src/client/spark_api/utils/BTSNativeMethods.cs @@ -4,7 +4,7 @@ using System.Linq; using System.Runtime.InteropServices; using System.Text; -namespace mbts.utils +namespace bts.utils { internal static class BTSNativeMethods { @@ -25,5 +25,8 @@ namespace mbts.utils [DllImport("kernel32.dll")] public static extern bool SetConsoleTitle(string lpConsoleTitle); + + [DllImport("libspark")] + public static extern bool initialize_nothrow(StringBuilder buffer, int length); } } diff --git a/src/client/spark_api/utils/LLDateTime.cs b/src/client/spark_api/utils/LLDateTime.cs index 968a1a9eb..0ed577a58 100644 --- a/src/client/spark_api/utils/LLDateTime.cs +++ b/src/client/spark_api/utils/LLDateTime.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; +using bts.utils; namespace mbts.utils { diff --git a/src/client/spark_api/utils/Log.cs b/src/client/spark_api/utils/Log.cs index 6526cb32a..649f7d202 100644 --- a/src/client/spark_api/utils/Log.cs +++ b/src/client/spark_api/utils/Log.cs @@ -34,7 +34,8 @@ namespace bts { Latency, DataFlow, Plugin, - ClientSdk + ClientSdk, + Http, // Please keep this list in sync with logging.h/logging.cpp } @@ -64,6 +65,31 @@ namespace bts { static extern void Logger_log (Level level, Subsystem subsystem, string message, string file, int line); } + public readonly struct LoggerContext { + readonly string _prefix, _suffix; + readonly Logger _logger; + + public LoggerContext(Logger logger, string prefix, string suffix) { + _prefix = prefix; + _suffix = suffix; + _logger = logger; + } + + public void diag(string message) => _logger.diag(_fmt(message)); + public void info(string message) => _logger.info(_fmt(message)); + public void warn(string message) => _logger.warn(_fmt(message)); + public void error(string message) => _logger.error(_fmt(message)); + public void fatal(string message) => _logger.fatal(_fmt(message)); + + string _fmt(string message) { + if (_prefix != null) + message = _prefix + message; + if (_suffix != null) + message += _suffix; + return message; + } + } + public static class Log { internal static Logger Gen => new Logger(Subsystem.gen); internal static Logger Config => new Logger(Subsystem.Config); @@ -78,6 +104,7 @@ namespace bts { internal static Logger Testing => new Logger(Subsystem.Testing); internal static Logger DataFlow => new Logger(Subsystem.DataFlow); public static Logger ClientSdk => new Logger(Subsystem.ClientSdk); + public static Logger Http => new Logger(Subsystem.Http); internal static void log_at(this Logger logger, Level level, string message) { switch (level) { @@ -130,5 +157,5 @@ namespace bts { logger.warn(message, file, line); #endif } - } + } } diff --git a/src/client/spark_core/config/config.cs b/src/client/spark_core/config/config.cs index abf97b268..81d232b60 100644 --- a/src/client/spark_core/config/config.cs +++ b/src/client/spark_core/config/config.cs @@ -6,6 +6,7 @@ using System.Text; using System.Threading.Tasks; using System.Threading; using System.Linq; +using bts.utils; namespace mbts.config { @@ -24,38 +25,37 @@ namespace mbts.config return ProtoBuf.Serializer.Deserialize(stream); } - private static bool initialize(out string error) { - StringBuilder buffer = new StringBuilder(4096); - bool success = initialize_nothrow(buffer, buffer.Capacity); - error = buffer.ToString(); - return success; - } - public static void initialize() { if (_state == State.OK) return; - string[] args = Environment.GetCommandLineArgs(); - utils.BTSNativeMethods.provide_command_line(args, args.Length); // Considered doing this async on a background thread // but it wouldn't do any good. The next guy down the line to ask for config // will just block until this completes. - string error; - if (!initialize(out error)) { + try { + bts.BTS.init(); + } catch(ApplicationException) { _state = State.Error; - throw new ApplicationException(error); + throw; } _state = State.OK; - utils.BTSNativeMethods.init_spark_lib(); } - public static void initialize_for_test() { - reset(); - initialize(); + public static void initialize_for_test(string config="") { + raw_initialize_for_test(config); + // still need init_spark_lib + bts.BTS.init(); + _state = State.OK; } - public static bool try_initialize(out string error) - { + public static bool try_initialize(out string error) { reset(); - return initialize(out error); + try { + bts.BTS.init(); + }catch(ApplicationException e) { + error = e.Message; + return false; + } + error = string.Empty; + return true; } //////////// @@ -67,6 +67,10 @@ namespace mbts.config [DllImport("libspark")] private static extern bool initialize_nothrow(StringBuilder buffer, int length); + + [DllImport("libspark", EntryPoint="initialize_for_test")] + private static extern void raw_initialize_for_test(string buffer); + [DllImport("libspark")] private static extern void reset(); diff --git a/src/client/spark_core/utils/ConsoleEx.cs b/src/client/spark_core/utils/ConsoleEx.cs index 4d9f09da7..4abf7891a 100644 --- a/src/client/spark_core/utils/ConsoleEx.cs +++ b/src/client/spark_core/utils/ConsoleEx.cs @@ -25,19 +25,30 @@ namespace bts.test { public static T prompt(string prompt, params T[] options) { return ConsoleEx.prompt(prompt, (IEnumerable)options); } + private static (bool,int) read_small_number() { + var key = Console.ReadKey(); + return (Int32.TryParse(key.KeyChar.ToString(), out int value), value); + } + private static (bool,int) read_big_number() { + var line = Console.ReadLine(); + return (Int32.TryParse(line, out int value), value); + } + private static int read_number(int max) { + do { + (var success,var value) = max > 9 ? read_big_number() : read_small_number(); + if (success && value >= 0 && value <= max) { + return value; + } + }while(true); + } + public static T prompt(string prompt, IEnumerable options) { WriteLine(prompt, ConsoleColor.Yellow); for (int x = 0; x < options.Count(); ++x) { Write($"{x}. ", ConsoleColor.Yellow); WriteLine(options.ElementAt(x).ToString()); } - int response = 0; - bool success = false; - while(!success) { - var key = Console.ReadKey(); - success = Int32.TryParse(key.KeyChar.ToString(), out response); - if (response > options.Count()) success = false; - } + int response = read_number(options.Count()); var value = options.ElementAt(response); Console.WriteLine(" >> " + value.ToString()); Console.WriteLine(); diff --git a/src/client/spark_core/utils/Exceptions/ExceptionAssert.cs b/src/client/spark_core/utils/Exceptions/ExceptionAssert.cs index 05a86f42e..e61beb3fe 100644 --- a/src/client/spark_core/utils/Exceptions/ExceptionAssert.cs +++ b/src/client/spark_core/utils/Exceptions/ExceptionAssert.cs @@ -44,6 +44,11 @@ namespace mbts.utils { throw new ArgumentException($"Must equal {value}.", name); } + public static void Between(T argument, T minimum, T maximum, string name) where T : struct, IComparable { + if (argument.CompareTo(minimum) < 0 || argument.CompareTo(maximum) > 0) + throw new ArgumentException($"Must be between {minimum} and {maximum}, inclusive (actually {argument}).", name); + } + public static void GreaterThan(T argument, T value, string name) where T : IComparable { if (argument.CompareTo(value) <= 0) throw new ArgumentException($"Must be greater than {value}.", name); diff --git a/src/client/spark_core/utils/GlobalExtensions.cs b/src/client/spark_core/utils/GlobalExtensions.cs index 7433968b4..b8d93f6bc 100644 --- a/src/client/spark_core/utils/GlobalExtensions.cs +++ b/src/client/spark_core/utils/GlobalExtensions.cs @@ -67,6 +67,22 @@ namespace mbts { } } - + public static void sync_with_models(this IList target, IEnumerable models, Func add_factory, Func update_factory) { + var model_list = models as IList ?? models.ToList(); + if (model_list.Count == 0) { + target.Clear(); + } else { + int stop = Math.Min(target.Count, model_list.Count); + for (int i = 0; i < stop; i++) { + target[i] = update_factory(target[i], model_list[i]); + } + while (target.Count < model_list.Count) { + target.Add(add_factory(model_list[target.Count])); + } + while (model_list.Count < target.Count) { + target.RemoveAt(model_list.Count); + } + } + } } } \ No newline at end of file diff --git a/src/client/spark_sdk/samples/OrderSample.cs b/src/client/spark_sdk/samples/OrderSample.cs index d6df99265..aec01460e 100644 --- a/src/client/spark_sdk/samples/OrderSample.cs +++ b/src/client/spark_sdk/samples/OrderSample.cs @@ -89,6 +89,10 @@ namespace bts.sdk cr.NewOrder.SubType = OrderSubType.None; cr.NewOrder.TimeInForce = TimeInForce.Day; var admin = await _admin.Value.get_admin_data_async(new VoidMessage()); + if (!admin.success) { + Console.Error.WriteLine(admin.error_message); + return; + } BTSUser user = admin.value.Users.FirstOrDefault( x=> username != null ? x.Name.ToLowerInvariant() == username.ToLowerInvariant() : true ); if (user != null) { diff --git a/src/client/spark_sdk/samples/SDKMain.cs b/src/client/spark_sdk/samples/SDKMain.cs index d23ef3fbc..f0fb158d8 100644 --- a/src/client/spark_sdk/samples/SDKMain.cs +++ b/src/client/spark_sdk/samples/SDKMain.cs @@ -26,6 +26,7 @@ namespace bts.sdk } public async static Task Main(string[] args) { + BTS.init(); Log.ClientSdk.info("Welcome to the BTS Client Side SDK example"); string u = args.FirstOrDefault(x => x.Contains("--user=")); diff --git a/src/client/spark_test/api/ApiTests.cs b/src/client/spark_test/api/ApiTests.cs index 69b63f8f0..22be985fd 100644 --- a/src/client/spark_test/api/ApiTests.cs +++ b/src/client/spark_test/api/ApiTests.cs @@ -20,7 +20,6 @@ namespace mbts.api.Test { ApiTestService[] test_services() { return new[] { _mtest, _ntest }; } public ApiTests() { - config.Configuration.initialize(); _context = ServiceContextTestHelpers.create_private_context(); _native_server = ApiTestsNativeMethods.apitest_start_apitest_service(_context.native_context); _managed_server = new ApiTestServiceServer(_context); @@ -34,7 +33,7 @@ namespace mbts.api.Test { Disposable.dispose(ref _context); } - [Test, Ignore("Times out")] + [Test] public void test_empty_reply_is_not_null() { foreach (var svc in test_services()) { Assert.IsNotNull( @@ -43,7 +42,6 @@ namespace mbts.api.Test { } [Test] - [Ignore("Fixme")] public void hello_server_test() { _mtest.run_hello_service(null); _mtest.run_shout_service(null); diff --git a/src/client/sparkcmd/Cmd/TestCommand.cs b/src/client/sparkcmd/Cmd/TestCommand.cs index b5b919f0a..9b071eb7a 100644 --- a/src/client/sparkcmd/Cmd/TestCommand.cs +++ b/src/client/sparkcmd/Cmd/TestCommand.cs @@ -9,6 +9,7 @@ using mbts.config; using System.Linq; using CommandLine; using Newtonsoft.Json; +using bts; namespace sparkcmd { @@ -41,6 +42,12 @@ namespace sparkcmd { } public static int Run(TestCommandOptions o) { + if (o.type == TestType.Unit) { + // Skip loading any local config + Configuration.initialize_for_test(); + } + else Configuration.initialize(); + Console.WriteLine(JsonConvert.SerializeObject(o)); if (o.list) { diff --git a/src/client/sparkcmd/Program.cs b/src/client/sparkcmd/Program.cs index c35332f50..8999b8bcc 100644 --- a/src/client/sparkcmd/Program.cs +++ b/src/client/sparkcmd/Program.cs @@ -9,6 +9,7 @@ using bts; using System.Collections.Generic; using System.Linq.Expressions; using System.Reflection; +using bts.utils; namespace sparkcmd { class Program { @@ -23,7 +24,11 @@ namespace sparkcmd { // return 3; // } static int Main(string[] args) { - mbts.utils.BTSNativeMethods.provide_command_line(args, args.Length); + // Initializing config is up to each individual command + // We call provide_command_line here in case config is first + // initialized from c++ instead of c# + BTSNativeMethods.provide_command_line(args, args.Length); + // note: this can be made to infer the options type list from the list of delegates return Parser.Default .ParseArguments< @@ -35,7 +40,7 @@ namespace sparkcmd { TestCommandOptions, OrderStressOptions, MBOCommandOptions - >(args) + >(BTS.remove_config_arg(args)) .MapResult< ConfigcheckOptions, ScriptClientOptions, diff --git a/src/data_files/CMakeDeps.cmake b/src/data_files/CMakeDeps.cmake index c72de1cbb..de9a0184e 100644 --- a/src/data_files/CMakeDeps.cmake +++ b/src/data_files/CMakeDeps.cmake @@ -1 +1 @@ -set(sources files/Onixs.BTS.lic;files/cert_secure_logon.txt;files/futures.coconut.xz;files/onixs_ice/ConnectivityConfiguration.APITest.xml;files/onixs_ice/ConnectivityConfiguration.PerfTest.xml;files/onixs_ice/ConnectivityConfiguration.Production.xml;files/perf_trace/compiled.css;files/perf_trace/index.html;files/perf_trace/jquery-3.3.1.min.js;files/perf_trace/main.js;files/perf_trace/perf_log.sql;files/perf_trace/perf_trace_analysis.sql;files/perf_trace/plotly-latest.min.js) \ No newline at end of file +set(sources files/cert_secure_logon.txt;files/futures.coconut.xz;files/onixs_ice/ConnectivityConfiguration.APITest.xml;files/onixs_ice/ConnectivityConfiguration.PerfTest.xml;files/onixs_ice/ConnectivityConfiguration.Production.xml;files/perf_trace/compiled.css;files/perf_trace/index.html;files/perf_trace/jquery-3.3.1.min.js;files/perf_trace/main.js;files/perf_trace/perf_log.sql;files/perf_trace/perf_trace_analysis.sql;files/perf_trace/plotly-latest.min.js) \ No newline at end of file diff --git a/src/data_files/files/Onixs.BTS.lic b/src/data_files/files/Onixs.BTS.lic deleted file mode 100644 index 9ff33e7a2..000000000 --- a/src/data_files/files/Onixs.BTS.lic +++ /dev/null @@ -1,16 +0,0 @@ - - production - - Blue Trading Systems OF 9420-2 - - - 2019-05-02 - 2020-05-13 - - - - - - - 5E50A1DACD94D7C1BECD316EEAE5AB02 - \ No newline at end of file diff --git a/src/data_files/files/cert_secure_logon.txt b/src/data_files/files/cert_secure_logon.txt index cd0110d4f..213e01d68 100644 --- a/src/data_files/files/cert_secure_logon.txt +++ b/src/data_files/files/cert_secure_logon.txt @@ -1,6 +1,12 @@ -Session ID Access ID Secret Key Creation Date Expiration Date Environment -MC1 KTima1GzuuXgmAd5LWnN bAFTi2XyMDBxX5S2-BC-k8Ti-5GmENer8Vtv4zmyCXM 2019-04-09 2020-04-09 CERT -MC5 tnoschzeeYyQEcUFLYBp uA4xXSggo3-hwRJdD35v0CTKpx6_HmpAock0CHgmZ4A 2019-04-09 2020-04-09 CERT -MC7 8ArA8FSwuoeTzMhkoQlW Zi1egeOWG3phGx4qZYjhOTtptcmA3t7ty_7if7qwO4c 2019-04-09 2020-04-09 CERT -MC8 grWHZk3dCz8wtRPYTQR9 OPIIFk1orwBLu4IBJVcNAVvRb8Db1-VRD0qyQClYHl8 2019-04-09 2020-04-09 CERT -V06 jw4KHsh6FqXIGK7ejJKp s_Cqv1o1rPhWTUWgY8mvHXJudzBYBUYS1rlMzwjMMT8 2019-04-09 2020-04-09 NEWRELEASE +Session ID Access ID Secret Key Creation Date Expiration Date Environment + +M4D lLtjH74y1Kh5XUJTLyH2 f1K_tROQp2lTIplVp34WxKa7xDUgfFnIteolfik061o 2019-09-03 2020-09-03 CERT + +MC1 VEMlkDuLz74OVHSuacQB IuHyYT3b-9KUo3WsuXykt-fXsjzp7L-JOrdCSQZLNl0 2019-09-03 2020-09-03 CERT + +MC5 77lzSUwPLlM9obaXh2mK z6aA9qfASDKfZkYTVKFqJd-78i0HZ2Q9ec3CKCYIqHg 2019-09-03 2020-09-03 CERT + +MC7 RphzI3rn71oGUEiYnX9e y6OvvPPyoyj2S8YAnE1gFduXT8v29HIMjO5EngYxSNg 2019-09-03 2020-09-03 CERT + +MC8 PYBIm2TI9gFPIxmHbuYl 6o-ZolGH2DdeQOlGtJ5Zqoh_kg7VdbxkmjeOHmigKwU 2019-09-03 2020-09-03 CERT + diff --git a/src/lib/CMakeDeps.cmake b/src/lib/CMakeDeps.cmake index 42887ddaf..9cb9851f4 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/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 +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/service_helpers/PositionUtilities.cpp;api/services/AdminService.proto;api/services/AlgoDefinitionService.proto;api/services/AlgoService.proto;api/services/ApiUnitTestService.proto;api/services/AuthService.proto;api/services/BasePositionService.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/MoneyFileService.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/position_cfg.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/position_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/sod.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/SecurityMapper.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;md/vendor/OnixsUtils.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/FilledPositionEvent.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/DynamicHelpers.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/ProRataAlgo.cpp;orderman/orders/algo/algo_orders/RegisterDorman.cpp;orderman/orders/algo/algo_orders/ScratchAlgo.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/NLegsHedgeRecompData.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/sftp.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 9d75f6455..ecd7b1cc6 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/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 +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/SecurityMapper.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;md/vendor/OnixsUtils.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/SingleWriterConcurrentVectorTests.cpp;utils/thread_manager.cpp) \ No newline at end of file diff --git a/src/lib/CMakeLists.txt b/src/lib/CMakeLists.txt index 2bc9cd150..c255c0b57 100644 --- a/src/lib/CMakeLists.txt +++ b/src/lib/CMakeLists.txt @@ -130,6 +130,7 @@ else () cryptopp pugixml lttng-ust + ssh2 -lrt -ldl -static-libgcc diff --git a/src/lib/api/apigateway.proto b/src/lib/api/apigateway.proto index 47b44c838..1463f8354 100644 --- a/src/lib/api/apigateway.proto +++ b/src/lib/api/apigateway.proto @@ -13,6 +13,7 @@ message GatewayLoginResult { api.AuthToken token = 3; bool can_create = 4; } + message GatewayCallHeader { int32 request_id = 1; int32 method = 2; @@ -21,8 +22,37 @@ message GatewayCallHeader { string service_qualifier = 5; } -message GatewaySubscribe { } -message GatewayUnsubscribe {} +message GatewaySubscribe { + int32 request_id = 1; + int32 method = 2; + api.AuthToken token = 3; + string service_type = 4; + string service_qualifier = 5; + string stream_qualifier = 6; + string method_name = 7; +} +message GatewayUnsubscribe { + uint32 subscription_id = 1; + string service_type = 4; + string service_qualifier = 5; +} + +message StreamGWUpdate { + enum UpdateType { + REMOVE = 0; + REPLACE = 1; + CREATE= 2; + RESET = 3; + } + UpdateType update_type = 1; + int32 key = 2; + bytes buffer = 3; +} +message SubscriptionUpdate { + int32 id = 1; + repeated StreamGWUpdate updates = 2; +} + message GatewayRequest { oneof request { GatewayLogin login = 1; @@ -40,6 +70,7 @@ message GatewayResponse { oneof response { GatewayLoginResult logon = 1; bytes call_result = 2; + SubscriptionUpdate tick = 3; string error = 10; } } \ No newline at end of file diff --git a/src/lib/api/core/ApiClientInterop.cpp b/src/lib/api/core/ApiClientInterop.cpp index 487b2eabb..6c29be4b7 100644 --- a/src/lib/api/core/ApiClientInterop.cpp +++ b/src/lib/api/core/ApiClientInterop.cpp @@ -140,14 +140,23 @@ void reply_error(ApiError error, raw_response_callback cb, void* extra) { } -CAPI void api_client_send(ApiClient* pclient, const char* buffer, int buffer_length, raw_response_callback cb, void* extra) { +int32_t rpc_connect_timeout_ms() { + static int32_t t = config::get().rpc_connect_timeout_seconds() * 1000; + return t; +} +void api_client_send_internal(ApiClient* pclient, qbus::RPCRequest&& request, raw_response_callback cb, int32_t delay, void* extra) { bts_assert(pclient); - std::string strbuf(buffer, buffer_length); - qbus::RPCRequest request(move(strbuf)); - pclient->rpc.wait_until_connected(1); - LOG(Api,Diag,"send rpc {}",((int64_t)buffer)); + bool connected = pclient->rpc.wait_until_connected(0); + if (!connected && delay > 0) { + // This branch is the uncommon 'slow path' where connection is taking longer than we expect + LOG(Api,Diag,"rpc call delay waiting for connection {}",((int64_t)request.value().c_str())); + pclient->qbus.post([=, request{std::move(request)}]() mutable { + api_client_send_internal(pclient, std::move(request), cb, delay - 100, extra); + }, 100); + return; + } pclient->rpc.send(std::move(request), [=](qbus::RPCError e, const qbus::RPCReplyPtr& reply) { - LOG(Api,Diag,"rpc reply {}",((int64_t)buffer)); + LOG(Api,Diag,"rpc reply {}",((int64_t)request.value().c_str())); switch (e) { case qbus::RPCError::RPC_OK: safe_call_fn([&]() { @@ -165,6 +174,12 @@ CAPI void api_client_send(ApiClient* pclient, const char* buffer, int buffer_len } }); } +CAPI void api_client_send(ApiClient* pclient, const char* buffer, int buffer_length, raw_response_callback cb, void* extra) { + LOG(Api,Diag,"send rpc {}",((int64_t)buffer)); + std::string strbuf(buffer, buffer_length); + qbus::RPCRequest request(move(strbuf)); + api_client_send_internal(pclient, std::move(request), cb, rpc_connect_timeout_ms(), extra); +} CAPI bool api_client_is_on_qb_thread(ApiClient* pclient) { return pclient->qbus.on_qb_thread(); diff --git a/src/lib/api/core/ApiClientInterop.h b/src/lib/api/core/ApiClientInterop.h index b150e5463..b22b8049d 100644 --- a/src/lib/api/core/ApiClientInterop.h +++ b/src/lib/api/core/ApiClientInterop.h @@ -21,7 +21,11 @@ enum EventType { typedef void* TableSubscription; typedef void(*raw_subscription_callback)(EventType event_type, const StreamUpdate* changes, int count, void* extra); -CAPI TableSubscription api_client_subscribe(ApiClient* pclient, const char* method, const char* qualifier, raw_subscription_callback cb, void* extra); +CAPI TableSubscription api_client_subscribe( + ApiClient* pclient, + const char* method, + const char* qualifier, + raw_subscription_callback cb, void* extra); CAPI void api_client_unsubscribe(ApiClient* pclient, TableSubscription ptable); }}//namespace diff --git a/src/lib/api/core/ApiServerInterop.cpp b/src/lib/api/core/ApiServerInterop.cpp index 8b57c72dd..46a224b4e 100644 --- a/src/lib/api/core/ApiServerInterop.cpp +++ b/src/lib/api/core/ApiServerInterop.cpp @@ -59,18 +59,16 @@ CAPI TableHandle api_server_create_table(ApiServerCore* pserver, const char* met } CAPI void api_server_update_table(TableHandle htable, StreamUpdate* raw_changes, int change_count) { - if (change_count > 0) { - bts_assert(htable); - qbus::TablePublisher* ptable = reinterpret_cast(htable); - std::vector changes(change_count); - for (int x = 0; x < change_count; ++x) { - StreamUpdate& ch = raw_changes[x]; - std::string buffer(ch.buffer, ch.buffer_len); - qbus::RecordPtr record = std::make_shared(std::move(buffer)); - changes[x] = qbus::Change((qbus::Change::Type)ch.type, qbus::RecordKey(ch.key), record); - } - ptable->publish(std::move(changes)); + bts_assert(htable); + qbus::TablePublisher* ptable = reinterpret_cast(htable); + std::vector changes(change_count); + for (int x = 0; x < change_count; ++x) { + StreamUpdate& ch = raw_changes[x]; + std::string buffer(ch.buffer, ch.buffer_len); + qbus::RecordPtr record = std::make_shared(std::move(buffer)); + changes[x] = qbus::Change((qbus::Change::Type)ch.type, qbus::RecordKey(ch.key), record); } + ptable->publish(std::move(changes)); } CAPI void api_server_destroy_table(qbus::TablePublisher* ptable) { diff --git a/src/lib/api/service_compiler/templates/fs/fable.fs b/src/lib/api/service_compiler/templates/fs/fable.fs index 8cd5c3385..92f5b37e6 100644 --- a/src/lib/api/service_compiler/templates/fs/fable.fs +++ b/src/lib/api/service_compiler/templates/fs/fable.fs @@ -68,7 +68,8 @@ module {{name}} = type I{{name}} = {{#each method}} {{#if server_streaming}} - // todo: {{name}} + // {{comment}} + abstract member subscribe_{{name}} : qualifier:string -> callback:(StreamUtils.IUpdate<{{output_type_full}}>->unit) -> System.IDisposable {{else}} // {{comment}} abstract member {{name}}: {{input_type_full}} -> Fable.Import.JS.Promise<{{output_type_full}}> @@ -78,6 +79,7 @@ type I{{name}} = module {{name}} = let Create (context:ServiceContext.ServiceContext) (qualifier:string) : I{{name}} = Imports.DynamicService.DynamicService.Create(context, "{{name}}", qualifier, + // methods unbox [| {{#each method}} {{#if server_streaming}} @@ -85,7 +87,16 @@ module {{name}} = "{{name}}", {{@index}}., "{{input_type_full}}", "{{output_type_full}}" {{/if}} {{/each}} - |]) + |], + // streams + unbox [| + {{#each method}} + {{#if server_streaming}} + "{{name}}", {{@index}}., "{{output_type_full}}" + {{/if}} + {{/each}} + |] + ) |> unbox {{/each}} \ No newline at end of file diff --git a/src/lib/api/service_helpers/AdminServiceUtils.cpp b/src/lib/api/service_helpers/AdminServiceUtils.cpp index 5c627bd79..440ea9ee5 100644 --- a/src/lib/api/service_helpers/AdminServiceUtils.cpp +++ b/src/lib/api/service_helpers/AdminServiceUtils.cpp @@ -12,6 +12,10 @@ namespace bts{ namespace admin{ + +using namespace bts::api::admin; +using namespace bts::api::orderman; + //////////////////////////////////////////// // connection accessors //////////////////// //////////////////////////////////////////// @@ -58,31 +62,32 @@ bool AdminServiceUtils::has_exchange(const ExchangeConnection& conn, } return rv; } -const bts::api::admin::ExchangeConnection* AdminServiceUtils::find_default_account_connection(const bts::api::admin::AdminData& data, - const bts::api::admin::BTSAccount& account, - bts::api::Venue exchange){ +const ExchangeConnection* AdminServiceUtils::find_default_account_connection( + const AdminData& data, + const BTSAccount& account, + bts::api::Venue exchange){ //todo:get from parent accounts if not overridden if(account.has_connections()) for (const std::string& conn_name : account.connections().exchange_connection_names()){ - const bts::api::admin::ExchangeConnection* conn = find_connection(data, conn_name); + const ExchangeConnection* conn = find_connection(data, conn_name); if (conn!=nullptr && has_exchange(*conn, exchange)) return conn; } return nullptr; } -const bts::api::admin::ExchangeConnection* -AdminServiceUtils::resolve_cme_connection(const bts::api::admin::AdminData& data, - const bts::api::admin::BTSAccount& account, +const ExchangeConnection* AdminServiceUtils::resolve_cme_connection( + const AdminData& data, + const BTSAccount& account, const bts::api::Security& sec){ if(account.has_connections()) for (const std::string& conn_name : account.connections().exchange_connection_names()){ - const bts::api::admin::ExchangeConnection* conn = find_connection(data, conn_name); + const ExchangeConnection* conn = find_connection(data, conn_name); if (conn!=nullptr && conn->has_cme_connection()) if (conn->cme_connection().has_cgw_connection() || (sec.has_glbx() && conn->cme_connection().has_msgw_connection() && conn->cme_connection().msgw_connection().market_segment_id() == sec.glbx().market_segment_id())) - return conn; + return conn; } return nullptr; } @@ -201,45 +206,96 @@ void AdminServiceUtils::fill_connections_from_config(bts::api::admin::AdminData& //////////////////////////////////////////// // account accessors /////////////////////// //////////////////////////////////////////// -bts::api::admin::BTSAccount* AdminServiceUtils::find_account (bts::api::admin::AdminData& data, - const std::string& name, - bool include_deleted){ +BTSAccount* AdminServiceUtils::find_account (AdminData& data, + const std::string& name, + bool include_deleted){ auto account_ptrs = accounts_mutable(data, include_deleted); for (auto account_ptr : account_ptrs) if(name==account_ptr->name()) return account_ptr; return nullptr; } -const bts::api::admin::BTSAccount* AdminServiceUtils::find_account (const bts::api::admin::AdminData& data, - const std::string& name, - bool include_deleted){ +std::map +AdminServiceUtils::get_clearing_account_map(const AdminData& data){ + std::map rv; + auto all_accounts = accounts(data,true); + for (BTSAccount const* a : all_accounts){ + // only for CME, ICE, GLBX + AdminServiceOperationResult res; + // cme + { + auto cme_clr_acc = find_account_exchange_account(data, *a, + api::Venue::VENUE_GLBX, + res); + if(res.is_success()) + rv[cme_clr_acc] = a->id(); + } + // ice + { + auto ice_clr_acc = find_account_exchange_account(data, *a, + api::Venue::VENUE_ICE, + res); + if(res.is_success()) + rv[ice_clr_acc] = a->id(); + } + // sim + { + auto sim_clr_acc = find_account_exchange_account(data, *a, + api::Venue::VENUE_SIM, + res); + if(res.is_success()) + rv[sim_clr_acc] = a->id(); + } + } + return rv; +} +const BTSAccount* AdminServiceUtils::find_account (const AdminData& data, + const std::string& name, + bool include_deleted){ auto account_ptrs = accounts(data, include_deleted); for (auto account_ptr : account_ptrs) if(name==account_ptr->name()) return account_ptr; return nullptr; } -bts::api::admin::BTSAccount* AdminServiceUtils::find_account (bts::api::admin::AdminData& data, - uint32_t id, - bool include_deleted){ + +BTSAccount const* AdminServiceUtils::find_account ( + const AdminData& data, + const std::string& clearing_account, + bts::api::Venue exchange){ + auto all_accounts = accounts(data,true); + for (BTSAccount const* a : all_accounts){ + AdminServiceOperationResult res; + // returns the first account that matches + std::string s = find_account_exchange_account (data, *a, exchange, res); + if (res.is_success() && s == clearing_account) + return a; + } + return nullptr; +} + +BTSAccount* AdminServiceUtils::find_account (AdminData& data, + uint32_t id, + bool include_deleted){ auto account_ptrs = accounts_mutable(data, include_deleted); for (auto account_ptr : account_ptrs) if(id==account_ptr->id()) return account_ptr; return nullptr; } -const bts::api::admin::BTSAccount* AdminServiceUtils::find_account (const bts::api::admin::AdminData& data, - uint32_t id, - bool include_deleted){ +const BTSAccount* AdminServiceUtils::find_account (const AdminData& data, + uint32_t id, + bool include_deleted){ auto account_ptrs = accounts(data, include_deleted); for (auto account_ptr : account_ptrs) if(id==account_ptr->id()) return account_ptr; return nullptr; } -std::vector AdminServiceUtils::accounts (const bts::api::admin::AdminData& data, - bool include_deleted){ - std::vector rv; +std::vector AdminServiceUtils::accounts ( + const AdminData& data, + bool include_deleted){ + std::vector rv; for (const auto& account : data.accounts()) if (account.is_present() || include_deleted) rv.push_back(&account); @@ -262,24 +318,30 @@ uint32_t AdminServiceUtils::find_max_acct_id (const bts::api::admin::AdminData& rv = acct.id(); return rv; } -const bts::api::admin::CMEAccountData* AdminServiceUtils::get_account_cme_data(const bts::api::admin::BTSAccount& account, - const bts::api::admin::AdminData& data){ - if (account.has_exchange_account_data() && account.exchange_account_data().has_cme_account_data()) +const CMEAccountData* AdminServiceUtils::get_account_cme_data( + const BTSAccount& account, + const AdminData& data){ + if (account.has_exchange_account_data() && + account.exchange_account_data().has_cme_account_data()) return &(account.exchange_account_data().cme_account_data()); else if (has_id(account.parent_account_id())){ - const bts::api::admin::BTSAccount* parent_account = find_account(data, account.parent_account_id()); + const BTSAccount* parent_account = + find_account(data, account.parent_account_id()); if (parent_account != nullptr) return get_account_cme_data (*parent_account, data); } return nullptr; } -const bts::api::admin::ICEAccountData* AdminServiceUtils::get_account_ice_data(const bts::api::admin::BTSAccount& account, - const bts::api::admin::AdminData& data){ - if (account.has_exchange_account_data() && account.exchange_account_data().has_ice_account_data()) +const ICEAccountData* AdminServiceUtils::get_account_ice_data( + const BTSAccount& account, + const AdminData& data){ + if (account.has_exchange_account_data() && + account.exchange_account_data().has_ice_account_data()) return &(account.exchange_account_data().ice_account_data()); else if (has_id(account.parent_account_id())){ - const bts::api::admin::BTSAccount* parent_account = find_account(data, account.parent_account_id()); + const BTSAccount* parent_account = + find_account(data, account.parent_account_id()); if (parent_account != nullptr) return get_account_ice_data (*parent_account, data); } @@ -298,7 +360,7 @@ const bts::api::admin::SIMAccountData* AdminServiceUtils::get_account_sim_data(c return nullptr; } -bool AdminServiceUtils::account_has_connection(const bts::api::admin::BTSAccount& account, +bool AdminServiceUtils::account_has_connection(const BTSAccount& account, const std::string& connection_name){ if(account.has_connections()){ for(int i=0; iuser_data().has_exchange_user_data()) user->mutable_user_data()->mutable_exchange_user_data()->CopyFrom(exchange_user_data); } +void AdminServiceUtils::set_user_md_entitlement_data(bts::api::admin::BTSUser* user, + const bts::api::admin::ExchangeUserMarketDataEntitlementData& exchange_user_md_entitelemnt_data){ + if (!user->has_user_data()) + user->mutable_user_data(); + if (!user->user_data().has_exchange_md_entitlement_data()) + user->mutable_user_data()->mutable_exchange_md_entitlement_data()->CopyFrom(exchange_user_md_entitelemnt_data); +} void AdminServiceUtils::clear_user_accounts (bts::api::admin::BTSUser* user){ if(user->has_user_data() && user->user_data().has_account_permissions()) user->mutable_user_data()->clear_account_permissions(); @@ -1468,6 +1539,7 @@ void AdminServiceUtils::bootstrap_data (bts::api::admin::AdminData& data){ auto user_3 = create_user(data, def_group->id(), std::string("bts_3"), std::string("bts_3"), empty, empty, false, license_data, license_entitlements, user_result); user_3->set_is_active(true); //associate users bts::api::admin::UserAccountPermissionUnit permission_unit; + permission_unit.mutable_trading_permission()->set_submit_market_orders(true); permission_unit.mutable_trading_permission()->set_has_permissions(true); permission_unit.mutable_admin_permission()->set_has_permissions(true); bts::api::admin::UserAccountPermissions account_permissions; diff --git a/src/lib/api/service_helpers/AdminServiceUtils.h b/src/lib/api/service_helpers/AdminServiceUtils.h index 5821a735e..c72283cdb 100644 --- a/src/lib/api/service_helpers/AdminServiceUtils.h +++ b/src/lib/api/service_helpers/AdminServiceUtils.h @@ -64,6 +64,12 @@ public: static BTSAccount* find_account (AdminData& data, const std::string& acct_name, bool include_deleted=false); + static std::map get_clearing_account_map(const AdminData&); + + // finds the account from a clearing account (and its venue) + static BTSAccount const* find_account (const AdminData& data, + const std::string& clearing_account, + bts::api::Venue); // same as above for const pointer static const BTSAccount* find_account (const AdminData& data, const std::string& acct_name, @@ -89,6 +95,8 @@ public: // if needed) static const CMEAccountData* get_account_cme_data(const BTSAccount& account, const AdminData& data); + static const ICEAccountData* get_account_ice_data(const BTSAccount& account, + const AdminData& data); static const ICEAccountData* get_account_ice_data(const BTSAccount& account, const AdminData& data); static const SIMAccountData* get_account_sim_data(const BTSAccount& account, diff --git a/src/lib/api/service_helpers/PositionUtilities.cpp b/src/lib/api/service_helpers/PositionUtilities.cpp new file mode 100644 index 000000000..fb385896a --- /dev/null +++ b/src/lib/api/service_helpers/PositionUtilities.cpp @@ -0,0 +1,165 @@ +#include "PositionUtilities.h" +#include "lib/api/services/PositionService.server.pb.h" +#include "lib/api/services/OrderService.pb.h" +#include "lib/api/services/OrderService.client.pb.h" +#include "lib/logging/logging.h" + +namespace bts { +namespace position{ + +using namespace bts::api::position; +using namespace bts::api::orderman; + +void PositionUtilities::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){ + if(fill_q<0){ + LOG(OrderMan,Error,"avg_price_qty " + "fill_id:{} fill_q:{} is_add:{}", + fill_id, fill_q, is_add); + fill_q=0; + } + if(is_add){ + tot_qty=base_q+fill_q; + avg_price = (base_p*base_q+fill_p*fill_q)/tot_qty; + } + else{ + if(fill_q>=base_q){ + tot_qty=0; + avg_price=0; + if(fill_q>base_q) + LOG(OrderMan,Error,"avg_price_qty " + "fill_id:{} base_qty:{} fill_qty:{}", + fill_id, base_q, fill_q); + } + else{ + tot_qty=base_q-fill_q; + avg_price = (base_p*base_q-fill_p*fill_q)/tot_qty; + } + } +} + +void PositionUtilities::apply_fill (PositionEntry& pos, + api::Side side, + double price, + double qty, + const std::string& fill_id, + bool is_add){ + double tot_avg_price, tot_qty; + if(side==api::Side::SIDE_BUY){ + avg_price_qty(pos.long_avg_price(), + pos.long_pos(), + price, + qty, + is_add, + fill_id, + tot_avg_price, + tot_qty); + pos.set_long_avg_price(tot_avg_price); + pos.set_long_pos(tot_qty); + } + else{ + avg_price_qty(pos.short_avg_price(), + pos.short_pos(), + price, + qty, + is_add, + fill_id, + tot_avg_price, + tot_qty); + pos.set_short_avg_price(tot_avg_price); + pos.set_short_pos(tot_qty); + } +} + +void PositionUtilities::apply_fill (PositionEntry& pos, + const Fill& f, + bool is_add){ + PositionUtilities::apply_fill(pos, + f.side(), + f.last_price(), + f.last_shares(), + f.fill_key(), + is_add); +} + +void PositionUtilities::apply_manual_fill ( + PositionEntry& pos, + const ManualFill& mf, + bool is_add){ + PositionUtilities::apply_fill(pos, + mf.side(), + mf.price(), + mf.quantity(), + "", + is_add); +} + +void PositionUtilities::total_position(const PositionUnit& p, + uint32_t& total_long, + uint32_t& total_short){ + total_long = p.sod().long_pos() + p.day().long_pos() + + p.admin_day().long_pos() + p.admin_sod().long_pos(); + total_short = p.sod().short_pos() + p.day().short_pos() + + p.admin_day().short_pos() + p.admin_sod().short_pos(); +} + +PositionEntry PositionUtilities::add_positions ( + const std::vector& v){ + double long_pos = 0, long_price = 0, short_pos = 0, short_price = 0; + for (auto ptr : v){ + const auto& p = *ptr; + long_pos += p.long_pos(); + long_price += p.long_pos()*p.long_avg_price(); + short_pos += p.short_pos(); + short_price += p.short_pos()*p.short_avg_price(); + } + long_price = long_pos > 0 ? long_price/long_pos : 0.0; + short_price = short_pos > 0 ? short_price/short_pos : 0.0; + bts::api::position::PositionEntry rv; + rv.set_long_pos(long_pos); + rv.set_long_avg_price(long_price); + rv.set_short_pos(short_pos); + rv.set_short_avg_price(short_price); + return rv; +} + +// copies from the new PositionEntry fields +// to the doubles (this is a temporary function) +void PositionUtilities::backwards_compat(PositionUnit& p){ + std::vector v; + // day + v.push_back(&p.day()); + v.push_back(&p.admin_day()); + v.push_back(&p.local()); + bts::api::position::PositionEntry day = add_positions(v); + p.set_day_long_pos(day.long_pos()); + p.set_day_long_avg_price(day.long_avg_price()); + p.set_day_short_pos(day.short_pos()); + p.set_day_short_avg_price(day.short_avg_price()); + // sod + v.clear(); + v.push_back(&p.sod()); + v.push_back(&p.admin_sod()); + bts::api::position::PositionEntry sod = add_positions(v); + p.set_sod_long_pos(sod.long_pos()); + p.set_sod_long_avg_price(sod.long_avg_price()); + p.set_sod_short_pos(sod.short_pos()); + p.set_sod_short_avg_price(sod.short_avg_price()); +} + +double PositionUtilities::compute_pnl_entry (const PositionEntry& p, + double cpp, + double md){ + return std::abs(p.long_pos()) * cpp * + (md - p.long_avg_price()) + + std::abs(p.short_pos()) * cpp * + (p.short_avg_price() - md); +} + +}} \ No newline at end of file diff --git a/src/lib/api/service_helpers/PositionUtilities.h b/src/lib/api/service_helpers/PositionUtilities.h new file mode 100644 index 000000000..1ceab634e --- /dev/null +++ b/src/lib/api/service_helpers/PositionUtilities.h @@ -0,0 +1,54 @@ +#pragma once + +#include "lib/api/services/PositionService.server.pb.h" +#include "lib/api/services/OrderService.pb.h" +#include "lib/api/services/OrderService.client.pb.h" +#include "lib/logging/logging.h" + +namespace bts { +namespace position{ + +class PositionUtilities{ +public: + 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 void apply_fill (bts::api::position::PositionEntry& pos, + api::Side side, + double price, + double qty, + const std::string& fill_id, + bool is_add); + + static void apply_fill (bts::api::position::PositionEntry& pos, + const bts::api::orderman::Fill& fill, + bool is_add); + + static void apply_manual_fill (bts::api::position::PositionEntry& pos, + const bts::api::position::ManualFill& mf, + bool is_add); + + // subsumes p2 into p1 + static void total_position(const bts::api::position::PositionUnit& p, + uint32_t& total_long, + uint32_t& total_short); + + static bts::api::position::PositionEntry add_positions ( + const std::vector& v); + + // copies from the new PositionEntry fields + // to the doubles (this is a temporary function) + static void backwards_compat(bts::api::position::PositionUnit& p); + + static double compute_pnl_entry (const api::position::PositionEntry& p, + double cpp, + double md); +}; + +}} \ No newline at end of file diff --git a/src/lib/api/services/AdminService.proto b/src/lib/api/services/AdminService.proto index 994937757..1a1f57fff 100644 --- a/src/lib/api/services/AdminService.proto +++ b/src/lib/api/services/AdminService.proto @@ -156,6 +156,7 @@ message BTSAccount{ message RiskLimitEnforcement{ bool product_limit_in_net_terms = 10; + bool ignore_spread_position_limits = 20; } message AccountProductPositionLimitUnit{ diff --git a/src/lib/api/services/BasePositionService.proto b/src/lib/api/services/BasePositionService.proto new file mode 100644 index 000000000..e6dc77d2f --- /dev/null +++ b/src/lib/api/services/BasePositionService.proto @@ -0,0 +1,22 @@ +syntax="proto3"; +package bts.api.position; +option csharp_namespace="bts.api.position"; +import "lib/api/apioptions.proto"; +import "lib/api/services/PositionService.proto"; + +message GMI_ParseResult { + oneof result { + string failure = 1; + BasePositionBatch success = 2; + } +} + +message GMI_CSVBlock { + string text = 1; +} + +service BasePositionService { + option (bts.api.service_description) = + "Obtains an initial position from an external source and invokes the Position Service with it. Also provides on demand parsing of the GMI format"; + rpc parse_gmi_csv (GMI_CSVBlock) returns (GMI_ParseResult); +} \ No newline at end of file diff --git a/src/lib/api/services/MarginService.proto b/src/lib/api/services/MarginService.proto index abe7a343b..d2d9d67b5 100644 --- a/src/lib/api/services/MarginService.proto +++ b/src/lib/api/services/MarginService.proto @@ -48,6 +48,15 @@ message CashBalanceUpdate { double cash_balance = 20; } +message CashBalanceItem { + int32 account_id = 1; + double cash_balance = 2; +} + +message CashBalanceBatch { + repeated CashBalanceItem cash_balance = 1; +} + message EnforceUpdate { string account = 10; bool should_enforce_margin = 20; @@ -61,7 +70,8 @@ service MarginService { 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 set_cash_balance_batch(CashBalanceBatch) 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/MoneyFileService.proto b/src/lib/api/services/MoneyFileService.proto new file mode 100644 index 000000000..5418fc1f8 --- /dev/null +++ b/src/lib/api/services/MoneyFileService.proto @@ -0,0 +1,22 @@ +syntax="proto3"; +package bts.api.position; +option csharp_namespace="bts.api.position"; +import "lib/api/apioptions.proto"; +import "lib/api/services/MarginService.proto"; + +message Money_ParseResult { + oneof result { + string failure = 1; + CashBalanceBatch success = 2; + } +} + +message Money_CSVBlock { + string text = 1; +} + +service MoneyFileService { + option (bts.api.service_description) = + "Obtains cash balances from an external source and invokes the margin service with them. Also provides on demand parsing of the money file"; + rpc parse_money_csv (Money_CSVBlock) returns (Money_ParseResult); +} \ No newline at end of file diff --git a/src/lib/api/services/OrderService.proto b/src/lib/api/services/OrderService.proto index 77872a425..4a12cb0a6 100644 --- a/src/lib/api/services/OrderService.proto +++ b/src/lib/api/services/OrderService.proto @@ -223,8 +223,8 @@ message ProRataOrderDefinition { uint32 chase_ticks = 2; spark.QuantityMeasure fill_limit = 5; spark.QuantityMeasure stay_in_threshold = 6; - bool enable_scratch_on_overfill = 7; - bool enable_stay_in = 8; + bool scratch_on_overfill = 7; + bool stay_in = 8; CustomOrderDefinition child = 10; CustomOrderDefinition hedge = 20; @@ -250,6 +250,7 @@ message OrderState { double price = 2; uint32 quantity = 3; uint32 filled_quantity=4; + uint32 filled_at_attach_time_quantity = 34; double last_price = 5; double avg_price = 6; string last_msg = 7; diff --git a/src/lib/api/services/PositionService.proto b/src/lib/api/services/PositionService.proto index b8aceea6c..24e3630c6 100644 --- a/src/lib/api/services/PositionService.proto +++ b/src/lib/api/services/PositionService.proto @@ -4,12 +4,23 @@ option csharp_namespace="bts.api.position"; import "lib/api/apioptions.proto"; import "lib/api/apicore.proto"; import "lib/domain_model/Security.proto"; +import "google/protobuf/timestamp.proto"; -message PositionUnit{ +message PositionEntry{ + double long_pos = 10; + double long_avg_price = 20; + double short_pos = 30; + double short_avg_price = 40; +} + +message PositionUnit{ int32 record_id = 1 [(bts.api.key)=true]; string account = 10; uint32 account_id = 15; uint32 security_id = 20; + // all these doubles will be retired + // to be replaced by the 5 PositionEntry + // entries below double sod_long_pos = 30; double sod_long_avg_price = 40; double sod_short_pos = 50; @@ -17,7 +28,16 @@ message PositionUnit{ double day_long_pos = 70; double day_long_avg_price = 80; double day_short_pos = 90; - double day_short_avg_price = 100; + double day_short_avg_price = 100; + PositionEntry sod = 110; + PositionEntry day = 120; + // this is the position without the spread legs + // being incorporated into the outright position + // (for spreads, day == raw_day) + PositionEntry raw_day = 125; + PositionEntry local = 130; + PositionEntry admin_sod = 140; + PositionEntry admin_day = 150; } message PNLUnit{ @@ -37,6 +57,11 @@ message AccountPNL{ } message ManualFill { + enum FillType { + LOCAL = 0; + ADMIN_DAY = 1; + ADMIN_SOD = 2; + } int32 record_id = 1 [(bts.api.key)=true]; uint32 security_key = 2; uint32 account_id = 3; @@ -45,6 +70,8 @@ message ManualFill { api.Side side = 5; uint32 quantity = 6; double price = 7; + FillType fill_type = 9; + google.protobuf.Timestamp timestamp = 10; } message ManualFillBatch { @@ -60,16 +87,37 @@ message PositionServiceResponse { repeated PositionServiceOpResponse response = 1; } -service PositionService { - option (bts.api.service_description) = "Provides Position and PNL, per security, per account"; +message BasePosition { + uint32 security_id = 1; + uint32 account_id = 2; + double avg_price = 3; + api.Side side = 4; + uint32 quantity = 6; +} +message SetBasePositionResponse { + bool ok = 1; + string msg = 2; +} + +message BasePositionBatch { + repeated BasePosition positions = 1; + google.protobuf.Timestamp current_as_of = 10; // UTC +} + +service PositionService { + option (bts.api.service_description) = "Provides Position and PNL, per security, per account"; + rpc create_manual_fill (ManualFillBatch) returns (PositionServiceResponse); rpc update_manual_fill (ManualFillBatch) returns (PositionServiceResponse); rpc remove_manual_fill (ManualFillBatch) returns (PositionServiceResponse); + rpc set_base_position (BasePositionBatch) returns (SetBasePositionResponse); // 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/ConfigurationState.cpp b/src/lib/config/ConfigurationState.cpp index bd821e192..0678fbd61 100755 --- a/src/lib/config/ConfigurationState.cpp +++ b/src/lib/config/ConfigurationState.cpp @@ -11,6 +11,7 @@ #include "lib/utils/bts_assert.h" #include "lib/utils/is_unittest_environment.h" #include "lib/utils/bts_array_size.h" +#include "lib/utils/defer.h" #include "config.h" #include "lib/config/protos/system.pb.h" @@ -349,11 +350,13 @@ void ConfigurationState::initialize_settings() std::string settings = settings_path(); std::string fallback = get_command_argument("-ConfigFallback"); - load(settings, fallback, log); - initialize_builtins(); - initialize_macros(); - _deferred_queue->process(); + DEFER(// Do this even if load() fails + initialize_builtins(); + initialize_macros(); + _deferred_queue->process(); + ); + load(settings, fallback, log); } void ConfigurationState::reset() diff --git a/src/lib/config/config.cpp b/src/lib/config/config.cpp index be514ea26..38455c881 100755 --- a/src/lib/config/config.cpp +++ b/src/lib/config/config.cpp @@ -89,7 +89,10 @@ namespace internal { // Functions used in testing ////////////////////////////////////////// -extern "C" BTS_EXPORT void initialize_for_test(const std::string& values, bool print_errors) +extern "C" BTS_EXPORT void initialize_for_test(const char* config) { + initialize_for_test(std::string(config), false); +} +void initialize_for_test(const std::string& values, bool print_errors) { config_state.reset(); config_state.load_string(values, print_errors); diff --git a/src/lib/config/config.h b/src/lib/config/config.h index 4cf0dc3bb..46f388312 100755 --- a/src/lib/config/config.h +++ b/src/lib/config/config.h @@ -56,7 +56,8 @@ namespace config { { // Initialize configuration state from a string instead of from the filesystem. defaults.conf will still be loaded. // Calling this a second time clears all values from previous initialization - extern "C" BTS_EXPORT void initialize_for_test(const std::string& configuration, bool print_errors=true); + extern "C" BTS_EXPORT void initialize_for_test(const char* config); + void initialize_for_test(const std::string& configuration, bool print_errors=true); // Releases all resources and returns config to factory defaults extern "C" BTS_EXPORT void reset(); diff --git a/src/lib/config/defaults/MarketData.conf b/src/lib/config/defaults/MarketData.conf index d999851bb..9d63e760e 100644 --- a/src/lib/config/defaults/MarketData.conf +++ b/src/lib/config/defaults/MarketData.conf @@ -121,3 +121,14 @@ MDBootDelayWindow { } } } + +DisplayFactorOverride { + value { + override { + symbol: "SOM" + exchange: "GLBX" + type: "SPREAD" + price_multiplier: 1 + } + } +} diff --git a/src/lib/config/defaults/logging.conf b/src/lib/config/defaults/logging.conf index 44a2d58d6..a09a08e1b 100644 --- a/src/lib/config/defaults/logging.conf +++ b/src/lib/config/defaults/logging.conf @@ -1,24 +1,24 @@ -Logging { - rule: "System.build_type == 'debug' && !('testorderman' in System.cmd_args) && !('dump' in System.cmd_args) && !('sparkcmd' in System.cmd_args) && !('dotnet' in System.cmd_args)" - value - { - console { default_level: "Info" } - syslog { default_level: "Info" } - file { default_level: "Info" } - } -} - Logging { rule: "System.platform == 'windows'" value { console { default_level: "Error" } syslog { default_level: "Info" } file { default_level: "Info" } - initial_queue_size: 1024 + initial_queue_size: 128 max_queue_growth: 100 } } +Logging { + rule: "System.build_type = 'debug'" + value + { + console { default_level: "Info" } + syslog { default_level: "Info" } + file { default_level: "Info" } + } +} + Logging { value diff --git a/src/lib/config/defaults/position_cfg.conf b/src/lib/config/defaults/position_cfg.conf new file mode 100644 index 000000000..eb362017c --- /dev/null +++ b/src/lib/config/defaults/position_cfg.conf @@ -0,0 +1,9 @@ +GMI_SymbolMap { + value { + sym { gmi: "01" spark: "ZW" venue: VENUE_GLBX price_multiplier: 100.0} + sym { gmi: "02" spark: "ZC" venue: VENUE_GLBX price_multiplier: 100.0} + sym { gmi: "03" spark: "ZO" venue: VENUE_GLBX price_multiplier: 100.0} + sym { gmi: "05" spark: "ZS" venue: VENUE_GLBX price_multiplier: 100.0} + sym { gmi: "KW" spark: "KE" venue: VENUE_GLBX price_multiplier: 100.0} + } +} \ No newline at end of file diff --git a/src/lib/config/protos/MarketData.proto b/src/lib/config/protos/MarketData.proto index 9ef415adb..b20424632 100755 --- a/src/lib/config/protos/MarketData.proto +++ b/src/lib/config/protos/MarketData.proto @@ -127,7 +127,8 @@ message CMEOptions optional bool validate_mbo = 35 [default = false]; } - optional string onixs_license = 1 [default = "{default}"]; + // optional string onixs_license = 1 [default = "{default}"]; + reserved 1; repeated CMEChannel channel = 3; repeated FeedEngineSettings feed_engine = 5; @@ -175,11 +176,16 @@ message ICEOptions optional bool load_security_db = 41 [default = true]; } - optional string onixs_license = 1 [default = "{default}"]; + // optional string onixs_license = 1 [default = "{default}"]; + reserved 1; optional string ice_config_file = 2 [default = "ConnectivityConfiguration.Production.xml"]; repeated ICEChannel channel = 3; } +message OnixsOptions { + optional string onixs_license = 1 [default = "{default}"]; +} + message RecordOptions { // optional string output_path = 2 [default = "/home/bts/md/today"]; optional bool enable_depth = 3 [default = false]; @@ -217,3 +223,13 @@ message CMETradingSchedule { message SecurityExpirationCutoff { optional uint32 max_days_after_expiration =1 [default = 7]; } + +message DisplayFactorOverride { + message Override { + required string symbol = 1; + required string exchange = 2; + optional string type = 3; + optional double price_multiplier = 10; + } + repeated Override override = 1; +} \ No newline at end of file diff --git a/src/lib/config/protos/position_cfg.proto b/src/lib/config/protos/position_cfg.proto new file mode 100644 index 000000000..b2a4b5342 --- /dev/null +++ b/src/lib/config/protos/position_cfg.proto @@ -0,0 +1,37 @@ +syntax='proto2'; +import "lib/domain_model/Security.proto"; +package bts.config; + +message GMI_Import +{ + required string sftp_host = 10 [default = "10.96.18.41"]; + optional uint32 sftp_port = 11 [default = 22222]; + required string sftp_user = 12 [default = "sftp"]; + required string sftp_password = 2 [default = "sftp"]; + required int32 pause_between_downloads_seconds = 4 [default = 30]; + required int32 download_timeout_seconds = 5 [default = 35]; + optional string download_window_begin = 6 [default = "22:00"]; + optional string download_window_end = 7 [default = "23:00"]; + + // These are just in case one we need to turn one of these off while + // the other remains on + optional bool disable_money_download = 8 [default = false]; + optional bool disable_position_download = 9 [default = false]; +} + +message GMI_SymbolMap { + message SymbolMap { + required string gmi = 1; + required string spark = 2; + required bts.api.Venue venue = 3; + required double price_multiplier = 4; + } + repeated SymbolMap sym = 4; +} + +message GMI_Parser { + optional bool map_glbx_to_sim = 1 [default = false]; + optional bool ignore_unknown_securities = 5 [ default = false]; + optional bool ignore_unsupported_security_types = 6 [ default = true]; +} + diff --git a/src/lib/config/protos/quickbus_cfg.proto b/src/lib/config/protos/quickbus_cfg.proto index c779a2f77..bed2302ee 100644 --- a/src/lib/config/protos/quickbus_cfg.proto +++ b/src/lib/config/protos/quickbus_cfg.proto @@ -18,3 +18,6 @@ message QuickbusCfg { optional uint32 max_missed_hbs = 6 [default=3]; } +message RPCConfig { + optional uint32 rpc_connect_timeout_seconds = 1 [default = 10]; +} \ No newline at end of file diff --git a/src/lib/config/protos/toggles.proto b/src/lib/config/protos/toggles.proto index af0232f0b..f1733290f 100644 --- a/src/lib/config/protos/toggles.proto +++ b/src/lib/config/protos/toggles.proto @@ -48,6 +48,10 @@ message RejectStopAndStopLimitOrders { optional bool enabled = 1 [default = true]; } +message SOMTicksFix { + optional bool enabled = 1 [default = true]; +} + message EnsureOnScreenWithWorkAreaRect { // Defaulting to false bc this is near the end of the cycle (04/27/2019), // and the new ensure_onscreen code has only been tested with a single monitor arrangement. diff --git a/src/lib/domain_model/Security.cpp b/src/lib/domain_model/Security.cpp index 17964b68d..40b2a2534 100644 --- a/src/lib/domain_model/Security.cpp +++ b/src/lib/domain_model/Security.cpp @@ -1,10 +1,12 @@ #include "Security.h" #include "lib/domain_model/Security.pb.h" -#include - +#include "lib/config/config.h" +#include "lib/config/protos/orders_fills_cleanup.pb.h" #include "lib/utils/datetime.h" + #include +#include namespace bts { namespace securities { @@ -65,12 +67,25 @@ boost::gregorian::date expiration(api::Security const &security) { } } -bool is_expired(api::Security const &security) { +bool is_expired(api::Security const &security, datetime::PTime const &now, + uint32_t sod_hh, uint32_t sod_mm) { auto e = expiration(security); if (e == boost::gregorian::date()) return false; - else - return e < boost::posix_time::microsec_clock::universal_time().date(); + else { + auto etime = datetime::PTime{ + e, boost::posix_time::time_duration(sod_hh, sod_mm, 0)}; + return etime < now; + } +} + +bool is_expired(api::Security const &security, datetime::PTime const &now) { + const auto &sod = config::get(); + return is_expired(security,now,sod.sod_hh(),sod.sod_mm()); +} + +bool is_expired(api::Security const &security) { + return is_expired(security, datetime::local_now()); } } // namespace securities diff --git a/src/lib/domain_model/Security.h b/src/lib/domain_model/Security.h index 0d37a0e03..80444d860 100644 --- a/src/lib/domain_model/Security.h +++ b/src/lib/domain_model/Security.h @@ -1,5 +1,6 @@ #pragma once +#include "lib/utils/datetime.h" #include #include @@ -7,7 +8,11 @@ #include "lib/domain_model/common.h" #include "lib/utils/bts_assert.h" -namespace boost {namespace gregorian {class date;}} +namespace boost { +namespace gregorian { +class date; +} +} // namespace boost namespace bts { @@ -64,7 +69,7 @@ inline Price improve_price(api::Side side, Price p, Price offset) { } inline Price worsen_price(api::Side side, Price p, Price offset) { - return improve_price(other_side(side), p, offset); + return improve_price(other_side(side), p, offset); } inline bool is_same(Price p1, Price p2) { @@ -77,7 +82,7 @@ inline bool is_equal_or_better(api::Side side, Price p1, Price p2) { // returns the better price out of p1, p2, if p1==p2, // return p1 -inline Price better_price (api::Side side, Price p1, Price p2){ +inline Price better_price(api::Side side, Price p1, Price p2) { return is_equal_or_better(side, p1, p2) ? p1 : p2; } @@ -151,8 +156,12 @@ inline Price round_to_quote_price(api::Security const &sec, Price in, raw_round_to_quote_price(p, sec.contract_spec().low_tick(), side)); } -boost::gregorian::date expiration(api::Security const&sec); -bool is_expired(api::Security const &security) ; +boost::gregorian::date expiration(api::Security const &sec); +bool is_expired(api::Security const &security); +// for testing +bool is_expired(api::Security const &security, datetime::PTime const &now); +bool is_expired(api::Security const &security, datetime::PTime const &now, + uint32_t sod_hh, uint32_t sod_mm); inline Price tick_size(api::Security const &sec) { return Price(sec.contract_spec().low_tick()); @@ -180,7 +189,7 @@ inline bool gr_than_eq(double p1, double p2, double tick_size) { } inline bool gr_than(double p1, double p2, double tick_size) { return gr_than_eq(p1, p2, tick_size) && - !test_price_equality(p1,p2,tick_size); + !test_price_equality(p1, p2, tick_size); } diff --git a/src/lib/domain_model/sod.cpp b/src/lib/domain_model/sod.cpp new file mode 100644 index 000000000..6cbdfe419 --- /dev/null +++ b/src/lib/domain_model/sod.cpp @@ -0,0 +1,26 @@ +#include "sod.h" +#include "lib/config/config.h" +#include "lib/config/protos/orders_fills_cleanup.pb.h" + + +namespace bts { +namespace sod { + +datetime::PTime get_sod_for (datetime::PTime const&this_time) { + const auto &sod = config::get(); + auto sod_time = datetime::make_ptime (this_time.date().year(), + this_time.date().month().as_number(), + this_time.date().day().as_number(), + sod.sod_hh(), + sod.sod_mm(), + 0,0); + if (sod_time > this_time) + sod_time -= boost::gregorian::date_duration(1); + return sod_time; +} + +datetime::PTime get_current_sod() { + return get_sod_for(datetime::local_now()); +} + +}} diff --git a/src/lib/domain_model/sod.h b/src/lib/domain_model/sod.h new file mode 100644 index 000000000..1583c0255 --- /dev/null +++ b/src/lib/domain_model/sod.h @@ -0,0 +1,12 @@ +#pragma once + +#include "lib/utils/datetime.h" +#include + +namespace bts { +namespace sod { + +datetime::PTime get_sod_for (datetime::PTime const&this_time) ; +datetime::PTime get_current_sod(); + +}} diff --git a/src/lib/domain_model/tests/domain_tests.cpp b/src/lib/domain_model/tests/domain_tests.cpp index c2dd3d24d..82e131589 100644 --- a/src/lib/domain_model/tests/domain_tests.cpp +++ b/src/lib/domain_model/tests/domain_tests.cpp @@ -3,8 +3,9 @@ #include "catch/catch.hpp" #include "lib/domain_model/Security.h" #include "lib/utils/PBUtils.h" - +#include "lib/utils/datetime.h" #include +#include namespace bts { namespace test { @@ -127,6 +128,27 @@ auto spread_txt = "id: 17\n" " cme_symbol: \"ES\"\n" " market_segment_id: 64\n" "}"; +auto fut_text = " id: 6\n" + " symbol: \"ES\"\n" + " exchange: VENUE_GLBX\n" + " contract_spec {\n" + " low_tick: 0.25\n" + " high_tick: 0.25\n" + " cash_per_point: 50\n" + " price_multiplier: 0.01\n" + " }\n" + " future {\n" + " expiration_year: 2019\n" + " expiration_month: 1\n" + " expiration_day: 20\n" + " }\n" + " glbx {\n" + " exchange_id: 16145\n" + " security_desc: \"ESZ9\"\n" + " cfi_code: \"FFIXSX\"\n" + " cme_symbol: \"ES\"\n" + " market_segment_id: 64\n" + " }\n"; TEST_CASE("expiration_tests", "[security]") { SECTION("test_expiration_retrieval") { @@ -142,6 +164,24 @@ TEST_CASE("expiration_tests", "[security]") { REQUIRE(securities::is_expired(spread)); REQUIRE(!securities::is_expired(leg)); } + + SECTION("test_expiration_sod_boundary") { + + using namespace boost::gregorian; + + auto fut = utils::pb::from_string(fut_text); + REQUIRE(securities::is_expired( + fut, datetime::make_ptime(2019, 1, 21, 0, 0, 0, 0), 16, 30)); + + REQUIRE(!securities::is_expired( + fut, datetime::make_ptime(2019, 1, 19, 0, 0, 0, 0), 16, 30)); + + REQUIRE(!securities::is_expired( + fut, datetime::make_ptime(2019, 1, 20, 16, 29, 0, 0), 16, 30)); + + REQUIRE(securities::is_expired( + fut, datetime::make_ptime(2019, 1, 20, 16, 31, 0, 0), 16, 30)); + } } } // namespace test diff --git a/src/lib/logging/logging.cpp b/src/lib/logging/logging.cpp index a698279c5..46d575b2a 100644 --- a/src/lib/logging/logging.cpp +++ b/src/lib/logging/logging.cpp @@ -118,6 +118,8 @@ void configure_logging(bts::config::Logging const&cfg, bts::config::ThreadGroupC {log::Latency,"Latency"}, {log::DataFlow,"DataFlow"}, {log::Plugin,"Plugin"}, + {log::ClientSdk,"ClientSdk"}, + {log::Http,"Http"}, }; real_cfg.sinks["console"] = diff --git a/src/lib/logging/logging.h b/src/lib/logging/logging.h index 2678d8552..0bfea9935 100644 --- a/src/lib/logging/logging.h +++ b/src/lib/logging/logging.h @@ -45,6 +45,7 @@ constexpr log::Subsystem Latency {Testing.ordinal+1 }; constexpr log::Subsystem DataFlow {Latency.ordinal+1 }; constexpr log::Subsystem Plugin {DataFlow.ordinal+1 }; constexpr log::Subsystem ClientSdk {Plugin.ordinal+1 }; +constexpr log::Subsystem Http {ClientSdk.ordinal+1 }; struct LoggerHandle { LoggerHandle(LoggerHandle const&) = delete; diff --git a/src/lib/md/MDProducer.cpp b/src/lib/md/MDProducer.cpp index c8ba2cb3a..9e283af9c 100644 --- a/src/lib/md/MDProducer.cpp +++ b/src/lib/md/MDProducer.cpp @@ -16,12 +16,12 @@ namespace md { // Owns shared memory for a single symbol+type class MDProducerImpl { public: - MDProducerImpl(const string& symbol, SecurityType type, Venue exchange, Platform platform, bool shared) : + MDProducerImpl(const string& symbol, SecurityType type, Venue exchange, Platform platform, MDProducerFlags flags) : _symbol(symbol), _type(type), _exchange(exchange), - _hot(hot_segment_name(symbol, type, exchange, platform), 1024, 1024, shared), - _cold(cold_segment_name(symbol, type, exchange, platform), 8192, 2048, shared), + _hot(hot_segment_name(symbol, type, exchange, platform), 1024, 1024, flags & MDProducerFlags::Shared), + _cold(cold_segment_name(symbol, type, exchange, platform), 8192, 2048, flags & MDProducerFlags::Shared), _trades(_cold.header().trade_buffer, BTS_ARRAYSIZE(_cold.header().trade_buffer), true), _rfqs(_cold.header().rfq_buffer, BTS_ARRAYSIZE(_cold.header().rfq_buffer), true) { @@ -33,7 +33,7 @@ public: // static_assert(sizeof(HotEntry) <= 32,"HotEntry should be less than 32 bytes"); } - MDProducerHandle add_security(const FixedSecurity& security) { + MDProducerHandle add_security(const FixedSecurity& security, SecurityMapper& smapper) { void* h_entry = nullptr; if (has_depth()) { h_entry = new (_hot.add(sizeof(HotDepthEntry))) HotDepthEntry(); @@ -45,6 +45,7 @@ public: // Calculating the size for the variable sized entries is tricky ColdEntry* c_entry = new (_cold.add(ColdEntry::fixed_size() + security.size)) ColdEntry(security.size); memcpy(&c_entry->security, &security, security.size); + smapper.apply(c_entry->security); MDProducerHandle handle(_cold.header(), _hot.header(), *c_entry, h_entry, MDProducerChannel(*this)); @@ -102,27 +103,30 @@ private: // MDProducer // -MDProducer::MDProducer(Platform platform, bool shared) : +MDProducer::MDProducer(Platform platform, MDProducerFlags flags) : _mutex(new std::mutex()), _producers(), _id_recorder(), _platform(platform), - _shared(shared) + _flags(flags), + _smapper(flags & MDProducerFlags::SecurityMapper) { } #ifndef WIN32 -MDProducer::MDProducer(Platform platform, SecurityIDRecorder recorder, bool shared) : +MDProducer::MDProducer(Platform platform, SecurityIDRecorder recorder, MDProducerFlags flags) : _mutex(new std::mutex()), _producers(), _id_recorder(std::make_unique(recorder)), _platform(platform), - _shared(shared) + _flags(flags), + _smapper(flags & MDProducerFlags::SecurityMapper) { } #endif MDProducer::MDProducer(MDProducer&& other) : _mutex(std::move(other._mutex)), _producers(std::move(other._producers)), _platform(other._platform), - _shared(other._shared) + _flags(other._flags), + _smapper(_flags & MDProducerFlags::SecurityMapper) {} MDProducer::~MDProducer() {} @@ -141,10 +145,10 @@ MDProducerImpl& MDProducer::get_producer(const char* symbol, SecurityType type, unique_ptr newp; if (type == api::SECURITYTYPE_SYNTHETIC) { // synthetics are special. We don't want to allocate a separate section for each one so we put them all in one - newp.reset(new MDProducerImpl(SYNTHETIC_SYMBOL, api::SECURITYTYPE_SYNTHETIC, Venue::VENUE_BTS, _platform, _shared)); + newp.reset(new MDProducerImpl(SYNTHETIC_SYMBOL, api::SECURITYTYPE_SYNTHETIC, Venue::VENUE_BTS, _platform, _flags)); } else { - newp.reset(new MDProducerImpl(symbol, type, exchange, _platform, _shared)); + newp.reset(new MDProducerImpl(symbol, type, exchange, _platform, _flags)); } _producers.push_back(move(newp)); return *_producers.back(); @@ -155,7 +159,7 @@ MDProducerHandle MDProducer::add_security(const FixedSecurity& security) { bts_assert(security.size >= sizeof(FixedSecurity)); std::lock_guard lock(*_mutex); auto& impl = get_producer(security.symbol(), security.type, security.exchange); - auto result = impl.add_security(security); + auto result = impl.add_security(security, _smapper); if (security.key != invalid_security_key) { result.set_key(security.key); } else if(_id_recorder) { _id_recorder->ensure_key(result); diff --git a/src/lib/md/MDProducer.h b/src/lib/md/MDProducer.h index aaad1ee1a..4b08f0589 100644 --- a/src/lib/md/MDProducer.h +++ b/src/lib/md/MDProducer.h @@ -7,6 +7,7 @@ #include "MDProducerHandle.h" #include "MDProducerChannel.h" #include "Shm/FixedSecurity.h" +#include "SecurityMapper.h" #include #include #include @@ -17,11 +18,23 @@ namespace md { class MDProducerImpl; class SecurityIDRecorder; +enum class MDProducerFlags { + None = 0x0, + Shared = 0x1, + SecurityMapper = 0x2, +}; + +constexpr MDProducerFlags operator+(MDProducerFlags a, MDProducerFlags b) { return (MDProducerFlags)((uint32_t)a | (uint32_t)b); } +constexpr MDProducerFlags operator-(MDProducerFlags a, MDProducerFlags b) { return (MDProducerFlags)((uint32_t)a | (uint32_t)b); } +constexpr bool operator&(const MDProducerFlags a, const MDProducerFlags b) { return ((uint32_t)a & (uint32_t)b) == (uint32_t)b; } + +constexpr MDProducerFlags DefaultMDProducerFlags = MDProducerFlags::Shared + MDProducerFlags::SecurityMapper; + class BTS_EXPORT MDProducer { public: - MDProducer(Platform platform, bool shared=true); - MDProducer(Platform platform, SecurityIDRecorder recorder, bool shared=true); + MDProducer(Platform platform, MDProducerFlags flags=DefaultMDProducerFlags); + MDProducer(Platform platform, SecurityIDRecorder recorder, MDProducerFlags flags=DefaultMDProducerFlags); MDProducer(MDProducer&& other); ~MDProducer(); @@ -45,7 +58,8 @@ private: std::vector> _producers; std::unique_ptr _id_recorder; Platform _platform; - bool _shared; + MDProducerFlags _flags; + SecurityMapper _smapper; }; diff --git a/src/lib/md/SecurityMapper.cpp b/src/lib/md/SecurityMapper.cpp new file mode 100644 index 000000000..5d1f046f7 --- /dev/null +++ b/src/lib/md/SecurityMapper.cpp @@ -0,0 +1,92 @@ + +#include "SecurityMapper.h" +#include "UserSymbol.h" +#include "PriceSize.h" +#include "lib/config/config.h" +#include "lib/config/protos/MarketData.pb.h" +#include "lib/config/protos/cme_reference_data.pb.h" +#include "lib/domain_model/Security.h" +#include "lib/utils/parse_enum.h" +#ifndef _WIN32 +#include "google/btree/btree_map.h" + +namespace bts { +namespace md { +struct DisplayFactorMapper : public ISecurityMapper +{ + btree::btree_map _mapped; + DisplayFactorMapper() : _mapped() { + for (const auto& ov : config::get().override()) { + if (ov.has_price_multiplier()) { + auto venue = parse_enum(ov.exchange(), "VENUE", api::Venue_Parse); + bts_assert_msg(venue, fmt::format("Invalid venue `{}`", ov.exchange())); + auto type = parse_enum(ov.type(), "SECURITYTYPE", api::SecurityType_Parse); + + if (type) { + _mapped.insert( + std::make_pair( + UserSymbol { uppercase_copy(ov.symbol()), *venue, *type }, + -to_exponent(ov.price_multiplier()))); + } + else { + for (auto t : { + SecurityType::SECURITYTYPE_FUTURE, + SecurityType::SECURITYTYPE_SPREAD, + SecurityType::SECURITYTYPE_OPTION }) { + _mapped.insert( + std::make_pair( + UserSymbol { uppercase_copy(ov.symbol()), *venue, t }, + -to_exponent(ov.price_multiplier()))); + } + } + } + } + } + void apply(FixedSecurity& f) const override { + UserSymbol key { f.symbol(), f.exchange, f.type }; + auto iter = _mapped.find(key); + if (iter != _mapped.end()) { + f.display_factor = iter->second; + } + } +}; + +struct CppOverride : public ISecurityMapper +{ + btree::btree_map _map; + CppOverride() : _map() { + for (const auto& p : config::get().cpp_entry()) { + _map.insert(p); + } + } + + void apply(FixedSecurity& f) const override { + auto iter = _map.find(f.symbol()); + if (iter != _map.end()) { + f.cash_per_point = iter->second; + } + } +}; + + +SecurityMapper::SecurityMapper(bool enabled): _impl() { + if (enabled) { + _impl.push_back(std::make_unique()); + _impl.push_back(std::make_unique()); + // Register more mappers HERE + } +} + +#else +namespace bts { +namespace md { +SecurityMapper::SecurityMapper(bool): _impl() {} +#endif + +SecurityMapper::~SecurityMapper() { } +void SecurityMapper::apply(FixedSecurity& security) { + for (const auto& m : _impl) { + m->apply(security); + } +} +}} //namespace diff --git a/src/lib/md/SecurityMapper.h b/src/lib/md/SecurityMapper.h new file mode 100644 index 000000000..2ca2129fa --- /dev/null +++ b/src/lib/md/SecurityMapper.h @@ -0,0 +1,24 @@ +#pragma once +#include "Shm/FixedSecurity.h" +#include +#include + +namespace bts { +namespace md { + +struct ISecurityMapper { + virtual ~ISecurityMapper() {} + virtual void apply(FixedSecurity&) const = 0; +}; + +class SecurityMapper { + std::vector> _impl; + public: + SecurityMapper(bool enabled); + ~SecurityMapper(); + // apply various mappings to the security + void apply(FixedSecurity&); +}; + + +}} //namespace diff --git a/src/lib/md/Shm/FixedSecurity.cpp b/src/lib/md/Shm/FixedSecurity.cpp index 55c2a9df2..c3b5ce25c 100644 --- a/src/lib/md/Shm/FixedSecurity.cpp +++ b/src/lib/md/Shm/FixedSecurity.cpp @@ -144,7 +144,7 @@ std::string security_descriptor(const api::Security& security) { return result.str(); } -void get_contract_spec(const FixedSecurity& source, api::ContractSpecification& spec) { +void get_contract_spec(const FixedSecurity& source, /*out*/ api::ContractSpecification& spec) { spec.set_price_break(source.price_break); spec.set_low_tick(source.low_tick); spec.set_high_tick(source.high_tick); diff --git a/src/lib/md/cme/MDP3Utils.cpp b/src/lib/md/cme/MDP3Utils.cpp index 1f09eb931..f1a8d7b41 100644 --- a/src/lib/md/cme/MDP3Utils.cpp +++ b/src/lib/md/cme/MDP3Utils.cpp @@ -167,5 +167,6 @@ const vector parse_channels(const vector& symbols) { return result; } + }} //namespace #endif \ No newline at end of file diff --git a/src/lib/md/cme/MDP3Utils.h b/src/lib/md/cme/MDP3Utils.h index dab13ff8d..08caddeab 100644 --- a/src/lib/md/cme/MDP3Utils.h +++ b/src/lib/md/cme/MDP3Utils.h @@ -1,10 +1,12 @@ #pragma once #include "lib/md/coconut2.h" +#include "lib/utils/optional.h" #include #include #include + namespace bts { namespace md { @@ -99,5 +101,6 @@ inline int floorlog10(int64_t m) { } + }}//namespace diff --git a/src/lib/md/relay/RelayClient.cpp b/src/lib/md/relay/RelayClient.cpp index 518a76795..0cc92b7a4 100644 --- a/src/lib/md/relay/RelayClient.cpp +++ b/src/lib/md/relay/RelayClient.cpp @@ -100,7 +100,9 @@ public: MDProducer& ensure_producer(Platform platform) { auto find_result = _producers.find(platform); if (find_result == _producers.end()) { - auto result = _producers.insert(make_pair(platform, MDProducer(platform, _shared))); + // relay never wants to enable SecurityMapper. Securities should always match the relay source. + auto flags = _shared ? MDProducerFlags::Shared : MDProducerFlags::None; + auto result = _producers.insert(make_pair(platform, MDProducer(platform, flags))); if (result.second) { return result.first->second; } @@ -238,6 +240,9 @@ public: log << Venue_Name(ch.exchange()).substr(sizeof("venue")); log << " " << (uint32_t)ch.time(); log << " " << (uint16_t)ch.segment_id(); + if (!ch.entitled()) { + log << " " << "Ent=0"; + } } log << "\n}"; auto encoded = websocketpp::base64_encode(log.str()); diff --git a/src/lib/md/test/Co2ManagedTestHelper.cpp b/src/lib/md/test/Co2ManagedTestHelper.cpp index 90462dfcd..a59e9a618 100644 --- a/src/lib/md/test/Co2ManagedTestHelper.cpp +++ b/src/lib/md/test/Co2ManagedTestHelper.cpp @@ -121,7 +121,7 @@ extern "C" BTS_EXPORT TestProducer* co2_produce_test_md() { " }" "}"); auto* tp = new TestProducer(); - tp->md = std::make_shared(PLATFORM_SIM, false); + tp->md = std::make_shared(PLATFORM_SIM, MDProducerFlags::None); tp->handles = create_test_securities(*(tp->md)); auto& producers = tp->handles; assign_ids(producers.begin(), producers.end()); diff --git a/src/lib/md/vendor/OnixsUtils.cpp b/src/lib/md/vendor/OnixsUtils.cpp new file mode 100644 index 000000000..c76422f0e --- /dev/null +++ b/src/lib/md/vendor/OnixsUtils.cpp @@ -0,0 +1,68 @@ +#ifndef WIN32 +#include "OnixsUtils.h" +#include "lib/logging/logging.h" +#include "lib/config/config.h" +#include "lib/config/protos/MarketData.pb.h" + +namespace bts { +namespace md { + +// The onixs checksum includes the line endings as \r\n +// to avoid the possibility of git mangling the line endings we store +// an array of lines instead of a single string and restore the lines +// when we write it out in MDP3main.cpp +const char * onixs_license[] = { +"", +" production", +" ", +" Blue Trading Systems OF 9420-2", +" ", +" ", +" 2019-05-02", +" 2020-05-13", +" ", +" ", +" ", +" ", +" ", +" ", +" 5E50A1DACD94D7C1BECD316EEAE5AB02", +"", +}; + +OnixsTemplLicense onixs_create_temporary_license() { + const auto& options = config::get(); + OnixsTemplLicense linfo; + linfo.path = options.onixs_license(); + if (linfo.path == "{default}") { + linfo.path = boost::filesystem::path {os::bts_data_path()} / "licenses"; + auto ident = boost::filesystem::unique_path("%%%%-%%%%-%%%%-%%%%.lic"); + linfo.license = { linfo.path / ident }; + } + + if (linfo.license) { + boost::system::error_code ignore; + boost::filesystem::create_directories(linfo.path, ignore); + auto temp = linfo.license.value(); + std::ofstream out(temp.c_str(), std::ios::out | std::ios::binary); + if (out.good()) { + for (const char* line : onixs_license) { + if (line != onixs_license[0]) out << "\r\n"; + out << line; + } + } + else { + LOG(md,Warn, "Licensing error. Failed to write to '{}'. Please ensure it is writeable.", linfo.path); + } + } + return linfo; +} +void onixs_destroy_temporary_license(const OnixsTemplLicense& lic) { + if (lic.license) { + boost::system::error_code ignore; + boost::filesystem::remove(lic.license.value(), ignore); + } +} + +}} //namespace +#endif diff --git a/src/lib/md/vendor/OnixsUtils.h b/src/lib/md/vendor/OnixsUtils.h new file mode 100644 index 000000000..0d46fc357 --- /dev/null +++ b/src/lib/md/vendor/OnixsUtils.h @@ -0,0 +1,16 @@ +#pragma once +#include "boost/filesystem.hpp" +#include "lib/utils/optional.h" + +namespace bts { +namespace md { + +struct OnixsTemplLicense { + boost::filesystem::path path; + std::optional license; +}; + +OnixsTemplLicense onixs_create_temporary_license(); +void onixs_destroy_temporary_license(const OnixsTemplLicense&); + +}} //namespace diff --git a/src/lib/mercury/synopsis/MergeByKey.h b/src/lib/mercury/synopsis/MergeByKey.h index 2ef3d0be9..8de729cea 100644 --- a/src/lib/mercury/synopsis/MergeByKey.h +++ b/src/lib/mercury/synopsis/MergeByKey.h @@ -2,6 +2,7 @@ #include #include +#include "lib/mercury/synopsis/ISynopsisBuilder.h" namespace bts { namespace mercury { diff --git a/src/lib/orderman/actors/FillStore.cpp b/src/lib/orderman/actors/FillStore.cpp index ab0f2a781..5dbbd7536 100644 --- a/src/lib/orderman/actors/FillStore.cpp +++ b/src/lib/orderman/actors/FillStore.cpp @@ -3,14 +3,15 @@ #include "lib/config/config.h" #include "lib/config/protos/toggles.pb.h" #include "lib/domain_model/Security.h" -#include "lib/orderman/actors/SODPosition.h" #include "lib/orderman/events/NewExecutionReportEvent.h" #include "lib/orderman/events/OrderDefinitionUpdateEvent.h" #include "lib/orderman/events/OrderStateUpdateEvent.h" #include "lib/orderman/events/StoredFillEvent.h" #include "lib/persistence/SqliteStore.h" #include "lib/utils/PBUtils.h" +#include "lib/orderman/actors/SODPosition.h" +#include #include namespace bts { @@ -26,14 +27,13 @@ struct FillStore::UnresolvedFills { struct FillStore::PImpl { PImpl(md::SecurityOracle &oracle) : store(new persistence::SqliteStore("fills")), known_orders(), - fills_with_unresolved_accounts(), sod_pos_map(), oracle(oracle) {} + fills_with_unresolved_accounts(), oracle(oracle) {} std::unique_ptr store; std::unordered_set known_fills; std::unordered_map known_orders; std::unordered_map fills_with_unresolved_accounts; mercury::Stream fill_stream; - SODPosition sod_pos_map; md::SecurityOracle oracle; }; @@ -51,9 +51,11 @@ void FillStore::init() { LOG(persist, Info, "Loading load all fills"); auto all = me->store->retrieve_all()[fills_ns]; LOG(persist, Info, "Finished loading all {} fills", all.size()); + SODPosition sod_helper; for (auto const &kvp : all) if (utils::pb::from_string(kvp.second, &fill)) { - + if (sod_helper.is_sod(fill.timestamp())) + fill.set_fill_kind(Fill::FILL_KIND_SOD); auto sec = me->oracle.get_security(SecurityKey(fill.security_key())); if (sec) { if (config::get().enabled() && @@ -61,9 +63,8 @@ void FillStore::init() { continue; else { auto fk = kvp.first; - if (!me->sod_pos_map.add_fill_to_sod_pos(fill)) - stored_batch.emplace_back(fill, *sec); me->known_fills.insert(fk); + stored_batch.emplace_back(fill, *sec); } } else LOG(persist, Error, @@ -79,7 +80,6 @@ void FillStore::init() { "Fill Store published {} fills recoved from disk out of {} " "total fills", stored_batch.size(), all.size()); - me->sod_pos_map.add_sod_pos_as_fill_events(stored_batch, me->oracle); publish_batch(me->fill_stream, stored_batch); // subscribe to order definition updates so we can resolve @@ -121,10 +121,10 @@ void FillStore::on_execution_report( if(er.exec_type == api::orderman::EXEC_TYPE_TRADE_CORRECT) fill.set_fill_status(api::orderman::Fill::FILL_STATUS_CORRECTED); fill.set_fill_key(fk); - struct timeval tv; - gettimeofday(&tv, NULL); - fill.mutable_timestamp()->set_seconds(tv.tv_sec); - fill.mutable_timestamp()->set_nanos(tv.tv_usec * 1000); + + auto exchange_ts = utils::pb::to_timestamp( + datetime::parse_FIX_timestamp(to_str(er.transact_time))); + *fill.mutable_timestamp() = exchange_ts; auto it = me->known_orders.find(er.order_key); if (it != me->known_orders.end()) { diff --git a/src/lib/orderman/actors/SODPosition.cpp b/src/lib/orderman/actors/SODPosition.cpp index 69b846ded..f962b119e 100644 --- a/src/lib/orderman/actors/SODPosition.cpp +++ b/src/lib/orderman/actors/SODPosition.cpp @@ -6,6 +6,7 @@ #include "lib/logging/logging.h" #include "lib/utils/PBUtils.h" #include "lib/utils/bts_assert.h" +#include #include #include @@ -13,62 +14,70 @@ namespace bts { namespace orderman { SODPosition::SODPosition() - : _enabled(config::get().enabled()), - _sod_time(_get_fills_sod_time()) { - LOG(persist, Info, "SODPosition enabled:{} sec:{}", _enabled, - _sod_time.tv_sec); +: +SODPosition(sod_time()) { } -// returns the timeval for today's hh,mm,ss -// timeval is the microsecons from epoch) -timeval SODPosition::_to_timeval(uint16_t hh, uint16_t mm, uint16_t ss) { +SODPosition::SODPosition(timeval sod_time) +: +_sod_time(sod_time){ + LOG(persist, Info, "SODPosition::cons sod_time:{}", sod_time.tv_sec); +} + +// returns the timeval for today's hh,mm,ss (in local machine terms) +// (timeval is the microseconds from epoch) +timeval SODPosition::to_timeval(uint16_t hh, uint16_t mm, uint16_t ss) { + // function does the conversion the following way: + // 1. gets the now secs from epoch using time + // 2. converts it into local time (so we have now local time) + // 3. overrides the hh, mm, ss passed + // 4. converts it back into secs from epoch using mktime bts_assert(hh < 24); bts_assert(mm < 60); bts_assert(ss < 60); time_t secs; - time(&secs); // seconds since epoch (utc) - struct tm *ptm = gmtime(&secs); - ptm->tm_hour = hh; - ptm->tm_min = mm; - ptm->tm_sec = ss; - ptm->tm_isdst = -1; - secs = mktime(ptm); + time(&secs); // seconds since epoch (utc) (independent of machine timezone) + struct tm *ptm_local = localtime(&secs); // in local timezone + ptm_local->tm_hour = hh; + ptm_local->tm_min = mm; + ptm_local->tm_sec = ss; + secs = mktime(ptm_local); // converts local time to UTC epoch time timeval rv; rv.tv_sec = secs; rv.tv_usec = 0; return rv; } -timeval SODPosition::_subtract_days(const timeval &tv, uint32_t n_days) { +timeval SODPosition::subtract_days(const timeval &tv, uint32_t n_days) { uint32_t secs_in_day = 86400 * n_days; timeval rv = tv; rv.tv_sec = (rv.tv_sec > secs_in_day) ? rv.tv_sec - secs_in_day : 0; return rv; } -timeval SODPosition::_get_fills_sod_time() { +timeval SODPosition::sod_time() { const auto &sod_time = config::get(); - timeval sod = _to_timeval(sod_time.sod_hh(), sod_time.sod_mm(), 0); + timeval sod = to_timeval(sod_time.sod_hh(), sod_time.sod_mm(), 0); timeval now; gettimeofday(&now, NULL); // if the server started before the sod for today // we go back to yesterday if (sod.tv_sec >= now.tv_sec) - sod = _subtract_days(sod); + sod = subtract_days(sod); return sod; } timeval SODPosition::get_orders_start_time() { const auto &sod_time = config::get(); - timeval sod = _to_timeval(sod_time.sod_hh(), sod_time.sod_mm(), 0); + timeval sod = to_timeval(sod_time.sod_hh(), sod_time.sod_mm(), 0); const auto &orders_cleanup = config::get(); timeval now; gettimeofday(&now, NULL); // if the server started before the sod for today // we go back to yesterday if (sod.tv_sec >= now.tv_sec) - sod = _subtract_days(sod); - sod = _subtract_days(sod, orders_cleanup.extra_days()); + sod = subtract_days(sod); + sod = subtract_days(sod, orders_cleanup.extra_days()); return sod; } @@ -105,100 +114,24 @@ timeval SODPosition::get_order_cutoff_time( rv.tv_sec = max_ts.seconds(); rv.tv_usec = static_cast(max_ts.nanos()) / 1000; const auto &orders_cleanup = config::get(); - return _subtract_days(rv, orders_cleanup.extra_days()); + return subtract_days(rv, orders_cleanup.extra_days()); +} +// returns now in UTC +timeval SODPosition::now(){ + timeval tv; + gettimeofday(&tv, NULL); + return tv; } -std::string SODPosition::_sod_fill_id(const Fill &fill, api::Side side) { - // sod:acct:long/short:sec - std::ostringstream oss; - oss << "sod:" << fill.account() - << (side == api::Side::SIDE_BUY ? ":L:" : ":S:") << fill.security_key(); - return oss.str(); +google::protobuf::Timestamp SODPosition::to_timestamp (timeval tv){ + google::protobuf::Timestamp rv; + rv.set_seconds(tv.tv_sec); + rv.set_nanos(tv.tv_usec*1000); + return rv; } -void SODPosition::_merge_pos(Fill &pos, const Fill &fill) { - bts_assert(pos.security_key() == fill.security_key()); - bts_assert(pos.account() == fill.account()); - bts_assert(pos.side() == fill.side()); - double total_qty = pos.last_shares() + fill.last_shares(); - double avg_price = (pos.avg_price() * pos.last_shares() + - fill.last_price() * fill.last_shares()) / - total_qty; - pos.set_avg_price(avg_price); - pos.set_last_price(avg_price); - pos.set_last_shares(total_qty); -} - -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); - pos.set_fill_kind(Fill::FILL_KIND_SOD); - pos.set_order_key(0); - pos.set_fill_key(_sod_fill_id(fill, side)); - pos.set_last_shares(0); -} - -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) { - 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[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); - if (sec_it == sec_map.end()) { - Fill long_pos, short_pos; - _zero_pos(long_pos, fill, api::Side::SIDE_BUY); - _zero_pos(short_pos, fill, api::Side::SIDE_SELL); - sec_map[seckey] = std::pair(long_pos, short_pos); - } - sec_it = sec_map.find(seckey); - bts_assert(sec_it != sec_map.end()); - std::pair &pos = sec_it->second; - if (fill.side() == api::Side::SIDE_BUY) - _merge_pos(pos.first, fill); - else - _merge_pos(pos.second, fill); - return true; - } - else - return false; -} - -void SODPosition::add_sod_pos_as_fill_events( - std::vector &fe, - md::SecurityOracle& oracle) { - if (_enabled) - for (auto &acct_p : _sod_map) { - for (auto &sec_p : acct_p.second) { - Fill &long_pos = sec_p.second.first; - Fill &short_pos = sec_p.second.second; - bts_assert(long_pos.security_key() == short_pos.security_key()); - auto sec = oracle.get_security(SecurityKey(long_pos.security_key())); - if (!sec) - LOG(persist, Error, - "Could not recover fill for key:{} from storage. " - "This should be investigated", - long_pos.security_key()); - else { - fe.emplace_back(long_pos, *sec); - fe.emplace_back(short_pos, *sec); - } - } - } +bool SODPosition::is_sod(const google::protobuf::Timestamp& fill_time) const{ + return fill_time.seconds() < _sod_time.tv_sec; } } // namespace orderman diff --git a/src/lib/orderman/actors/SODPosition.h b/src/lib/orderman/actors/SODPosition.h index fd31b8b3d..523ab11aa 100644 --- a/src/lib/orderman/actors/SODPosition.h +++ b/src/lib/orderman/actors/SODPosition.h @@ -8,6 +8,8 @@ using namespace bts::api::orderman; +namespace google { namespace protobuf { class timestamp;}} + namespace bts{ namespace orderman{ @@ -16,25 +18,21 @@ SecPosMap; class SODPosition{ private: - std::map _sod_map; - const bool _enabled; - const timeval _sod_time; + const timeval _sod_time; private: - // converts today:hh:mm:ss to timeval - static timeval _to_timeval(uint16_t hh, uint16_t mm, uint16_t ss); - static timeval _subtract_days(const timeval& tv, uint32_t n_days=1); - static timeval _get_fills_sod_time(); - static std::string _sod_fill_id(const Fill& fill,api::Side side); - void _merge_pos(Fill& pos, const Fill& fill); - void _zero_pos(Fill& pos, const Fill& fill, api::Side side); + // converts today:hh:mm:ss to timeval public: SODPosition(); + SODPosition(timeval); + static timeval sod_time(); + static timeval to_timeval(uint16_t hh, uint16_t mm, uint16_t ss); + static timeval subtract_days(const timeval& tv, uint32_t n_days=1); // all inactive orders before this time are not published static timeval get_orders_start_time(); static timeval get_order_cutoff_time(const std::map& ordermap); - bool add_fill_to_sod_pos(const Fill& fill); - void add_sod_pos_as_fill_events(std::vector& fill_events, - md::SecurityOracle&); + static timeval now(); + static google::protobuf::Timestamp to_timestamp (timeval); + bool is_sod (const google::protobuf::Timestamp& fill_time) const; }; }} diff --git a/src/lib/orderman/actors/ServiceBridge.cpp b/src/lib/orderman/actors/ServiceBridge.cpp index c85e3bfbd..ad501ab98 100644 --- a/src/lib/orderman/actors/ServiceBridge.cpp +++ b/src/lib/orderman/actors/ServiceBridge.cpp @@ -8,6 +8,7 @@ #include "lib/orderman/events/ActivateOrderEvent.h" #include "lib/orderman/events/CancelOrderEvent.h" #include "lib/orderman/events/DetachOrderEvent.h" +#include "lib/orderman/events/FilledPositionEvent.h" #include "lib/orderman/events/ExcessLiquidityUpdateEvent.h" #include "lib/orderman/events/NewAdminDataEvent.h" #include "lib/orderman/events/OrderManagerReadyEvent.h" @@ -22,6 +23,7 @@ #include "lib/orderman/securities/MapSecurityDB.h" #include "lib/tracing/PerfLog.h" #include "lib/utils/PBUtils.h" +#include "lib/api/service_helpers/PositionUtilities.h" #include namespace bts { @@ -53,6 +55,7 @@ struct ServiceBridge::PImpl { mercury::Stream detach_order_pub; mercury::Stream new_admin_data_pub; mercury::Stream account_pnl_pub; + mercury::Stream filled_position_pub; mercury::Stream excess_liquidity_pub; std::unordered_set known_orders; std::vector> admin_data_updates; @@ -86,6 +89,7 @@ void ServiceBridge::init() { me->detach_order_pub = make_stream(); me->new_admin_data_pub = make_stream(); me->account_pnl_pub = make_stream(); + me->filled_position_pub = make_stream(); me->excess_liquidity_pub = make_stream(); me->ready.set_value(); } @@ -253,6 +257,56 @@ void ServiceBridge::update_excess_liquidity_report( }); } +std::optional +make_pos_event(api::position::PositionUnit const &pu, + securities::MapSecurityDB const &o) { + auto sec = o.resolve(SecurityKey(pu.security_id())); + if (sec) { + uint32_t total_long_pos, total_short_pos; + bts::position::PositionUtilities::total_position(pu, + total_long_pos, + total_short_pos); + FilledPositionEvent ev{ + *sec, AccountID(pu.account_id()), + Quantity(total_long_pos), + Quantity(total_short_pos) + }; + return {ev}; + } else + return {}; +} + +void ServiceBridge::update_position( + api::position::Position_dataUpdate const &pos) { + + std::vector evs; + for (auto const &pu : pos.added()) { + auto ev = make_pos_event(*pu, me->sec_db); + if (ev.has_value()) + evs.push_back(ev.value()); + else + LOG(OrderMan, Error, + "Could not update position limiter for unknown security {}. This " + "should not happen", + pu->security_id()); + } + + for (auto const &pu_change : pos.changed()) { + auto ev = make_pos_event(*pu_change.current(), me->sec_db); + if (ev.has_value()) + evs.push_back(ev.value()); + else + LOG(OrderMan, Error, + "Could not update position limiter for unknown security {}. This " + "should not happen", + pu_change.current()->security_id()); + } + + me->service_call_marshaller.schedule_action([this, evs]() { + publish_batch(me->filled_position_pub, evs); + }); +} + 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 ab68bf1ed..59d1af670 100644 --- a/src/lib/orderman/actors/ServiceBridge.h +++ b/src/lib/orderman/actors/ServiceBridge.h @@ -78,9 +78,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_position(api::position::Position_dataUpdate const&); void update_excess_liquidity_report( - api::position::Excess_liquidity_reportUpdate const &); - + api::position::Excess_liquidity_reportUpdate const &); private: struct PImpl; std::unique_ptr me; diff --git a/src/lib/orderman/actors/order_manager/OrderManager.cpp b/src/lib/orderman/actors/order_manager/OrderManager.cpp index 7746b7682..5c51317f2 100644 --- a/src/lib/orderman/actors/order_manager/OrderManager.cpp +++ b/src/lib/orderman/actors/order_manager/OrderManager.cpp @@ -5,11 +5,11 @@ #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/FilledPositionEvent.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" -#include "lib/orderman/events/StoredFillEvent.h" #include "lib/orderman/events/StoredOrderEvent.h" #include "lib/orderman/order_factory/OrderFactory.h" #include "lib/orderman/orders/Order.h" @@ -29,7 +29,7 @@ using namespace mercury; enum class OrderManager::State { WAIT_ADMIN, WAIT_SEC, - WAIT_FILLS, + WAIT_FILLED_POSITION, WAIT_ORDERS, READY }; @@ -42,7 +42,7 @@ struct OrderManager::PImpl { State state; std::unordered_map orders; mercury::Actor::SubHandle stored_order_sub; - mercury::Actor::SubHandle stored_fills_sub; + mercury::Actor::SubHandle filled_position_sub; OrderServicesData services_data; mercury::Stream pub_ready; std::thread background_thread; @@ -134,36 +134,32 @@ void OrderManager::on_securities( if (me->state == State::WAIT_SEC) { LOG(OrderMan, Info, "Order manager received initial set of {} " - "securities. Waiting for stored fills now", + "securities. Waiting for the filled position now", update.events->size()); - me->state = State::WAIT_FILLS; - me->stored_fills_sub = subscribe( - [this](auto const &fills) { this->on_fill_recovery(fills); }); + me->state = State::WAIT_FILLED_POSITION; + me->filled_position_sub = subscribe( + [this](auto const &pos) { this->on_filled_position(pos); }); } } } -void OrderManager::on_fill_recovery( - const mercury::Update &update) { +void OrderManager::on_filled_position( + const mercury::Update &update) { if (update.events) { - if (me->state == State::WAIT_FILLS) { + if (me->state == State::WAIT_FILLED_POSITION) { LOG(OrderMan, Info, "Order manager received initial set of {} " - "stored fills. Waiting for stored orders now", + "stored filled position records. Waiting for stored orders now", update.events->size()); me->state = State::WAIT_ORDERS; - - for (auto const &ev : *(update.events)) { - auto trimmed_account_name = - boost::algorithm::trim_copy(ev.fill.account()); - me->services_data.limiter.add( - *ev.security, trimmed_account_name, ev.fill.side(), - Quantity(ev.fill.last_shares()), false); - } - me->stored_order_sub = subscribe( [this](auto const &orders) { this->on_order_recovery(orders); }); } + + for (auto const &ev : *(update.events)) { + me->services_data.limiter.set_filled_position( + *ev.security, ev.account_id, ev.long_quantity, ev.short_quantity); + } } } diff --git a/src/lib/orderman/actors/order_manager/OrderManager.h b/src/lib/orderman/actors/order_manager/OrderManager.h index 218704ad2..9b004f031 100644 --- a/src/lib/orderman/actors/order_manager/OrderManager.h +++ b/src/lib/orderman/actors/order_manager/OrderManager.h @@ -29,7 +29,7 @@ class UpdateOrderEvent; class CancelOrderEvent; class DetachOrderEvent; class StoredOrderEvent; -class StoredFillEvent; +class FilledPositionEvent; class OrderServices; class CreateFIXBufferReplyEvent; class GatewayStatusChangeEvent; @@ -53,7 +53,7 @@ namespace orderman { // - a set of execution gateways // - subscriptions to interesting events such as: // - market data book updates -// - security discuovery events +// - security discovery events // - timer updates // // Additionally handles order maintenance events: @@ -74,7 +74,7 @@ class OrderManager : public mercury::Actor, public mercury::IPollableActor { // external events void on_securities(const mercury::Update &); - void on_fill_recovery(const mercury::Update &); + void on_filled_position(const mercury::Update &); void on_order_recovery(const mercury::Update &); void on_execution_report(const mercury::Update &); void on_activate_order(const mercury::Update &); diff --git a/src/lib/orderman/actors/order_manager/OrderServices.h b/src/lib/orderman/actors/order_manager/OrderServices.h index 1d64f554d..aeb344f8a 100644 --- a/src/lib/orderman/actors/order_manager/OrderServices.h +++ b/src/lib/orderman/actors/order_manager/OrderServices.h @@ -167,6 +167,7 @@ class OrderServices { // i.e. caller will attach to an existing order. void attach(OrderKey attach_target, AttachCallback const &); + trace::CorID corid() { return _data->cor_id; } private: friend class OrderBuilder; @@ -272,7 +273,6 @@ class OrderServices { void warm_up_activate_child(Order &o) { o.warm_up_activate(*_data, *this); } - trace::CorID corid() { return _data->cor_id; } OrderOpResult check_is_attachable(); bool has_reported_state() const; diff --git a/src/lib/orderman/events/FilledPositionEvent.cpp b/src/lib/orderman/events/FilledPositionEvent.cpp new file mode 100644 index 000000000..a71fc8689 --- /dev/null +++ b/src/lib/orderman/events/FilledPositionEvent.cpp @@ -0,0 +1 @@ +#include "FilledPositionEvent.h" diff --git a/src/lib/orderman/events/FilledPositionEvent.h b/src/lib/orderman/events/FilledPositionEvent.h new file mode 100644 index 000000000..98b94bd79 --- /dev/null +++ b/src/lib/orderman/events/FilledPositionEvent.h @@ -0,0 +1,35 @@ +#pragma once + +#include "lib/api/services/OrderService.pb.h" +#include "lib/domain_model/Security.h" +#include "lib/mercury/events/Event.h" +#include "lib/mercury/synopsis/MergeByKey.h" +#include + +namespace bts { +namespace orderman { + +class FilledPositionEvent : public mercury::Event { + public: + using SynopsisPolicy = mercury::MergeByKey; + + FilledPositionEvent(api::Security const &sec, AccountID id, + Quantity long_quantity, Quantity short_quantity) + : security(&sec), account_id(id), long_quantity(long_quantity), + short_quantity(short_quantity) {} + + uint64_t key() const { + uint64_t result = security->id(); + result <<= 32; + result += account_id.value(); + return result; + } + + api::Security const *security; + AccountID account_id; + Quantity long_quantity; + Quantity short_quantity; +}; + +} // namespace orderman +} // namespace bts diff --git a/src/lib/orderman/events/OrderStateUpdateEvent.h b/src/lib/orderman/events/OrderStateUpdateEvent.h index 7ffc83650..d50f637b5 100644 --- a/src/lib/orderman/events/OrderStateUpdateEvent.h +++ b/src/lib/orderman/events/OrderStateUpdateEvent.h @@ -15,12 +15,13 @@ class OrderStateUpdateEvent : public mercury::Event { using SynopsisPolicy = mercury::CacheAll; OrderStateUpdateEvent() - : key(null_order_key()), parent_key(null_order_key()) {} + : key(null_order_key()), parent_key(null_order_key()) {} - OrderStateUpdateEvent(OrderKey k, OrderKey pk, - std::shared_ptr const &def, - api::orderman::OrderState const &state) { - init(k, pk, def,state); + OrderStateUpdateEvent( + OrderKey k, OrderKey pk, + std::shared_ptr const &def, + api::orderman::OrderState const &state) { + init(k, pk, def, state); } void init(OrderKey k, OrderKey pk, @@ -35,6 +36,7 @@ class OrderStateUpdateEvent : public mercury::Event { filled_quantity = state.filled_quantity(); last_price = state.last_price(); avg_price = state.avg_price(); + filled_at_attach_time_quantity = state.filled_at_attach_time_quantity(); last_msg = state.last_msg(); display_status = state.display_status(); @@ -55,8 +57,8 @@ class OrderStateUpdateEvent : public mercury::Event { OrderKey key; OrderKey parent_key; - std::shared_ptr definition; - + std::shared_ptr definition; + // common state api::orderman::OrderStatus status; double price; @@ -64,9 +66,10 @@ class OrderStateUpdateEvent : public mercury::Event { uint32_t filled_quantity; double last_price; double avg_price; + uint32_t filled_at_attach_time_quantity; std::string last_msg; api::orderman::OrderState::DisplayStatus display_status; - + api::orderman::OrderState::CustomCase custom; // native order state @@ -86,11 +89,12 @@ class OrderStateUpdateEvent : public mercury::Event { result.set_filled_quantity(filled_quantity); result.set_last_price(last_price); result.set_avg_price(avg_price); + result.set_filled_at_attach_time_quantity(filled_at_attach_time_quantity); result.set_last_msg(last_msg); result.set_display_status(display_status); return result; } - + api::orderman::OrderState rebuild_state() const { api::orderman::OrderState result = quick_rebuild_state(); switch (custom) { @@ -143,7 +147,7 @@ class OrderStateUpdateEvent : public mercury::Event { break; case api::orderman::OrderState::kQueueHolder: result.mutable_queue_holder(); - break; + break; case api::orderman::OrderState::kOco: result.mutable_oco(); break; @@ -155,7 +159,7 @@ class OrderStateUpdateEvent : public mercury::Event { break; case api::orderman::OrderState::kProrata: result.mutable_prorata(); - break; + break; case api::orderman::OrderState::kDynamic: result.mutable_dynamic(); break; diff --git a/src/lib/orderman/limiter/LimitStore.cpp b/src/lib/orderman/limiter/LimitStore.cpp index 3f71be12b..65f57cebb 100644 --- a/src/lib/orderman/limiter/LimitStore.cpp +++ b/src/lib/orderman/limiter/LimitStore.cpp @@ -19,8 +19,9 @@ LimitStore::LimitStore(const bts::api::admin::AdminData &admin_data, ProductLimitTree *product_tree_ptr = _find_product_for_security(security); bts_assert(product_tree_ptr != nullptr); _sec_map.insert(std::pair( - security.id(), SecurityLimitTree(security, *product_tree_ptr, - _admin_data, _zero_security_limit))); + security.id(), SecurityLimitTree(security, + *product_tree_ptr, + _admin_data))); } _set_user_data(); } diff --git a/src/lib/orderman/limiter/LimitStore.h b/src/lib/orderman/limiter/LimitStore.h index f46ca7f0c..8e2b68f18 100644 --- a/src/lib/orderman/limiter/LimitStore.h +++ b/src/lib/orderman/limiter/LimitStore.h @@ -3,6 +3,7 @@ #include #include "lib/api/services/OrderService.pb.h" #include "lib/orderman/orderman_utils/orderman_common.h" +#include "lib/api/services/PositionService.server.pb.h" #include "lib/domain_model/Security.pb.h" #include "lib/api/services/AdminService.server.pb.h" #include "lib/api/service_helpers/AdminServiceUtils.h" @@ -21,6 +22,17 @@ namespace orderman { // AdminData (user account relationships, position // limits, clip sizes etc) are constant. The non const // data are the positions + +// the structure of the code is like this : +// Limiter -> LimitStore (admin data and securities are +// immutable for LimitStore) +// Limiter +// LimitStore +// SecurityLimitTree* (key:sec) +// SecurityLimitNode* (key:sec,account) +// ProductLimitNode (key:account,product multiple sec's point to it) +// ProductLimitTree (key:product contains ProductLimitNode) is not used +// in adding positions or limit checks) class LimitStore{ private: // for a single user, contains the trading accounts @@ -60,9 +72,7 @@ private: // this is passed over to the SecurityLimitTree // entried (above) std::vector _prod_map; - // these are kept here and references passes to each node const bts::api::admin::AccountProductPositionLimitUnit _zero_product_limit; - const bts::api::admin::AccountSecurityPositionLimitUnit _zero_security_limit; public: LimitStore(const bts::api::admin::AdminData& admin_data, const std::map& sec_map); @@ -83,34 +93,42 @@ public: // copy_data_from copies the position and // data from another source void copy_data_from(LimitStore& source); - void add_position(const api::Security& security, + void add_working_order(const api::Security& security, AccountID account_id, api::Side side, Quantity quantity, bool recurse){ SecurityLimitTree* sec_tree = _find_security(security.id()); bts_assert(sec_tree != nullptr); - sec_tree->add_position(account_id, side, quantity); + sec_tree->add_working_order(account_id, side, quantity); if (recurse && 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_qty = Quantity (quantity.value() * std::abs(leg.quantity())); - add_position (leg.security(), account_id, effective_leg_side, effective_leg_qty, recurse); + add_working_order (leg.security(), account_id, effective_leg_side, effective_leg_qty, recurse); } } } - void remove_position(const api::Security& security, + void remove_working_order(const api::Security& security, AccountID account_id, api::Side side, Quantity quantity, bool recurse){ SecurityLimitTree* sec_tree = _find_security(security.id()); bts_assert(sec_tree != nullptr); - sec_tree->remove_position(account_id, side, quantity); + sec_tree->remove_working_order(account_id, side, quantity); if (recurse && 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_qty = Quantity (quantity.value() * std::abs(leg.quantity())); - remove_position (leg.security(), account_id, effective_leg_side, effective_leg_qty,recurse); + remove_working_order (leg.security(), account_id, effective_leg_side, effective_leg_qty,recurse); } } } + void set_filled_position (const api::Security& security, + AccountID account_id, + Quantity long_pos, + Quantity short_pos){ + SecurityLimitTree* sec_tree = _find_security(security.id()); + bts_assert(sec_tree != nullptr); + sec_tree->update_position(account_id, long_pos, short_pos); + } void set_pnl (AccountID account_id, double pnl); void set_excess_liquidity (AccountID account_id, double excess_liquidity); // risk limit checks diff --git a/src/lib/orderman/limiter/Limiter.cpp b/src/lib/orderman/limiter/Limiter.cpp index cd1cfc9e5..4dee99c64 100644 --- a/src/lib/orderman/limiter/Limiter.cpp +++ b/src/lib/orderman/limiter/Limiter.cpp @@ -1,6 +1,7 @@ #include "Limiter.h" #include "LimitStore.h" #include "lib/api/services/AdminService.server.pb.h" +#include "lib/api/services/PositionService.server.pb.h" #include "lib/domain_model/Security.pb.h" namespace bts { @@ -16,6 +17,7 @@ struct Limiter::PImpl { Limiter::Limiter() : me(new PImpl()) {} Limiter::~Limiter() {} + void Limiter::update(api::admin::AdminData const &admin_data) { LOG(OrderMan, Info, "Limiter::update"); _reset(admin_data); @@ -32,47 +34,51 @@ void Limiter::add_security(api::Security const &security) { _add_security(&security); _reset(me->limit_store->admin_data()); } -void Limiter::add(api::Security const &security, AccountID account_id, - api::Side side, Quantity quantity, bool recurse) { +void Limiter::set_filled_position(api::Security const &security, + AccountID account_id, + Quantity long_pos, + Quantity short_pos){ + // this means a security was added and filled before + // add_securities was called or before add was called + // (we can put it in a queue and examine it on the + // add_securities call) + if (!_contains_security(&security)) + LOG(OrderMan, Error, "Limiter::set_filled_position could not find" + "sec_key:{} this should be investigated", + security.id()); + else{ + if (!me->limit_store->has_account(account_id)) + // add a temporary account, so trades can go in there + _add_account_and_reset(account_id); + me->limit_store->set_filled_position(security, account_id, + long_pos, short_pos); + } +} + +void Limiter::add_to_working_position(api::Security const &security, + AccountID account_id, + api::Side side, + Quantity quantity){ if (!_contains_security(&security)) add_security(security); if (!me->limit_store->has_account(account_id)) { // add a temporary account, so trades can go in there _add_account_and_reset(account_id); } - me->limit_store->add_position(security, account_id, side, quantity, recurse); + me->limit_store->add_working_order(security, account_id, side, quantity, true); } -void Limiter::remove(api::Security const &security, AccountID account_id, - api::Side side, Quantity quantity, bool recurse) { +void Limiter::remove_from_working_position(api::Security const &security, + AccountID account_id, + api::Side side, + Quantity quantity) { if (!_contains_security(&security)) add_security(security); if (!me->limit_store->has_account(account_id)) { _add_account_and_reset(account_id); } - me->limit_store->remove_position(security, account_id, side, quantity, recurse); -} -void Limiter::add(const api::Security &security, - const std::string &account_name, api::Side side, - Quantity quantity, bool recurse) { - const auto *acct_ptr = bts::admin::AdminServiceUtils::find_account( - me->limit_store->admin_data(), account_name); - if (acct_ptr == nullptr) - LOG(OrderMan, Error, "Limiter::add can not find account:{}", - account_name); - else - add(security, AccountID(acct_ptr->id()), side, quantity, recurse); -} -void Limiter::remove(const api::Security &security, - const std::string &account_name, api::Side side, - Quantity quantity, bool recurse) { - const auto *acct_ptr = bts::admin::AdminServiceUtils::find_account( - me->limit_store->admin_data(), account_name); - if (acct_ptr == nullptr) - LOG(OrderMan, Error, "Limiter::remove can not find account:{}", - account_name); - else - remove(security, AccountID(acct_ptr->id()), side, quantity, recurse); + me->limit_store->remove_working_order(security, account_id, side, quantity, true); } + void Limiter::set_day_pnl (AccountID account_id, double pnl){ if (!me->limit_store->has_account(account_id)) _add_account_and_reset(account_id); @@ -147,6 +153,10 @@ void Limiter::_add_security(const api::Security *sec) { } } +bool Limiter::_contains_security(uint32_t security_key) { + return me->sec_map.find(security_key) != me->sec_map.end(); +} + bool Limiter::_contains_security(const api::Security *security) { return me->sec_map.find(security->id()) != me->sec_map.end(); } diff --git a/src/lib/orderman/limiter/Limiter.h b/src/lib/orderman/limiter/Limiter.h index 8ad1f0929..e1cdfeeb7 100644 --- a/src/lib/orderman/limiter/Limiter.h +++ b/src/lib/orderman/limiter/Limiter.h @@ -12,6 +12,14 @@ class AdminData; } // namespace api } // namespace bts +namespace bts { +namespace api { +namespace position { +class PositionUnit; +} +} // namespace api +} // namespace bts + namespace bts { namespace orderman { @@ -24,16 +32,28 @@ class Limiter { void add_securities(std::vector const &secs); void add_security(api::Security const &new_sec); - void add(api::Security const &, AccountID, api::Side, Quantity, - bool recurse); - void remove(api::Security const &, AccountID, api::Side, Quantity, - bool recurse); + // Set the current *filled* position, i.e. actual number of + // contracts owned, for the specified sec and account. Spreads are + // not recursively deconstructed into their legs, i.e this sets the + // position only for the passed security. + void set_filled_position(api::Security const &, AccountID, + Quantity long_pos, + Quantity short_pos); - void add(api::Security const &, std::string const & /* account_name */, - 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); + // Increment/decrement the working position, i.e. the quantity open + // in the market, recursively updating leg positions in the case of + // a spread. + void add_to_working_position(api::Security const &, AccountID, api::Side, + Quantity); + void remove_from_working_position(api::Security const &, AccountID, + api::Side, Quantity); + + void update_position(const api::position::PositionUnit &); + // this can be a partial list + void update_position( + const std::vector &positions); + + void set_day_pnl(AccountID account_id, double pnl); void set_excess_liquidity(AccountID account_id, double excesss_liquidity); enum LimiterErrorCode { @@ -62,11 +82,10 @@ class Limiter { LimiterQueryResult can_trade(UserID user, AccountID into_account) const; LimiterQueryResult can_send_new_order(AccountID into_account, api::Security const &sec, - api::Side side, - Quantity quantity) ; + api::Side side, Quantity quantity); LimiterQueryResult can_update_order(AccountID account, api::Security const &sec, api::Side side, - Quantity from, Quantity to) ; + Quantity from, Quantity to); ~Limiter(); // for testing purposes only LimitStore &limit_store(); @@ -75,6 +94,7 @@ class Limiter { void _reset(const bts::api::admin::AdminData &); void _add_account_and_reset(AccountID account_id); void _add_security(const api::Security *security); + bool _contains_security(uint32_t security_key); bool _contains_security(const api::Security *security); private: diff --git a/src/lib/orderman/limiter/ProductLimitNode.cpp b/src/lib/orderman/limiter/ProductLimitNode.cpp index 5e78bed62..0fb9e1f9f 100644 --- a/src/lib/orderman/limiter/ProductLimitNode.cpp +++ b/src/lib/orderman/limiter/ProductLimitNode.cpp @@ -5,14 +5,17 @@ namespace orderman { ProductLimitNode::ProductLimitNode( uint32_t account_id, - uint32_t, // parent_account_id, const bts::api::admin::AccountProductPositionLimitUnit &limit, - bool enforce_net) - : _account_id(account_id), - //_parent_account_id(parent_account_id), - _limit(limit), _enforce_net(enforce_net), - _long_pos_limit(limit.long_position_limit()), - _short_pos_limit(limit.short_position_limit()), _long_pos(0), - _short_pos(0) {} + bool enforce_net, + bool ignore_position_enforcement) + : + _account_id(account_id), + _limit(limit), + _enforce_net(enforce_net), + _ignore_position_enforcement(ignore_position_enforcement), + _long_pos_limit(limit.long_position_limit()), + _short_pos_limit(limit.short_position_limit()), + _long_pos(0), + _short_pos(0) {} } // namespace orderman -} // namespace bts +} // namespace bts \ No newline at end of file diff --git a/src/lib/orderman/limiter/ProductLimitNode.h b/src/lib/orderman/limiter/ProductLimitNode.h index c6f675454..91bec6ea0 100644 --- a/src/lib/orderman/limiter/ProductLimitNode.h +++ b/src/lib/orderman/limiter/ProductLimitNode.h @@ -11,32 +11,37 @@ namespace bts{ namespace orderman{ -// Keeps the running position and risk limits -// for a single -// Its the equivelant to SecurityLimitNode -// however it does not need to check for -// clip size. +// Scope : +// All the level position limits are +// enforced here. +// Keeps the running long and short position (both working +// orders and positions - which is transparent to this class) +// and risk limits class ProductLimitNode{ private: - const uint32_t _account_id; // , _parent_account_id; + const uint32_t _account_id; + // this is only kept to faciliate testing const bts::api::admin::AccountProductPositionLimitUnit& _limit; - const bool _enforce_net; + const bool _enforce_net, _ignore_position_enforcement; const uint32_t _long_pos_limit, _short_pos_limit; // this is the only state here uint32_t _long_pos, _short_pos; public: - ProductLimitNode(uint32_t account_id, - uint32_t parent_account_id, - const bts::api::admin::AccountProductPositionLimitUnit& limit, - bool enforce_net); - // accessors - //////////// - uint32_t account_id() const {return _account_id;} + ProductLimitNode( + uint32_t account_id, + const bts::api::admin::AccountProductPositionLimitUnit& limit, + bool enforce_net, + bool ignore_position_enforcement); + // accessors /////////////////////////////////////////////////////////////// + //////////////////////////////////////////////////////////////////////////// + uint32_t account_id() const { + return _account_id; + } int net_pos() const { return static_cast(_long_pos)-static_cast(_short_pos); } - // position updates - /////////////////// + // position updates //////////////////////////////////////////////////////// + //////////////////////////////////////////////////////////////////////////// void copy_positions_from(const ProductLimitNode& source){ _long_pos=source._long_pos; _short_pos=source._short_pos; @@ -55,37 +60,50 @@ 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) const{ Limiter::LimiterQueryResult result; + if(_ignore_position_enforcement){ + result.result = Limiter::OK; + return result; + } if (_enforce_net) - LimiterUtils::can_send_new_order_net (side, - quantity, - false, - static_cast(_long_pos_limit), - -1 * static_cast(_short_pos_limit), - net_pos(), - result); + LimiterUtils::can_send_new_order_net ( + side, + quantity, + false, + static_cast(_long_pos_limit), + -1 * static_cast(_short_pos_limit), + net_pos(), + result); else - LimiterUtils::can_send_new_order_abs (side, + LimiterUtils::can_send_new_order_abs (side, quantity, - _long_pos_limit, + _long_pos_limit, _short_pos_limit, _long_pos, _short_pos, result); 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) const{ Limiter::LimiterQueryResult result; + if(_ignore_position_enforcement){ + result.result = Limiter::OK; + return result; + } if(_enforce_net) - LimiterUtils::can_update_order_net (side, - from, - to, - false, - static_cast(_long_pos_limit), - -1 * static_cast(_short_pos_limit), - net_pos(), - result); + LimiterUtils::can_update_order_net ( + side, + from, + to, + false, + static_cast(_long_pos_limit), + -1 * static_cast(_short_pos_limit), + net_pos(), + result); else LimiterUtils::can_update_order_abs (side, from, @@ -108,6 +126,9 @@ public: } // test only functions ////////////////////// - const bts::api::admin::AccountProductPositionLimitUnit& test_only_func_limit() const {return _limit;} + const bts::api::admin::AccountProductPositionLimitUnit& + test_only_func_limit() const { + return _limit; + } }; }} diff --git a/src/lib/orderman/limiter/ProductLimitTree.cpp b/src/lib/orderman/limiter/ProductLimitTree.cpp index 1df2aa4ea..185d8901f 100644 --- a/src/lib/orderman/limiter/ProductLimitTree.cpp +++ b/src/lib/orderman/limiter/ProductLimitTree.cpp @@ -1,26 +1,34 @@ #include "ProductLimitTree.h" +using namespace bts::api::admin; +using namespace bts::admin; + namespace bts { namespace orderman { ProductLimitTree::ProductLimitTree( - const api::Product &product, const bts::api::admin::AdminData &admin_data, - const bts::api::admin::AccountProductPositionLimitUnit &zero_limit) - : _product(product) { + const api::Product &product, + const AdminData &admin_data, + const AccountProductPositionLimitUnit &zero_limit) + : + _product(product) { for (const auto &account : admin_data.accounts()) { - bool enforce_net = true; // default is net - if (account.has_risk_limit_enforcement()) + bool enforce_net = true, // default is net + ignore_position_enforcement = false; + if (account.has_risk_limit_enforcement()){ enforce_net = account.risk_limit_enforcement().product_limit_in_net_terms(); - const bts::api::admin::AccountProductPositionLimitUnit *limit = - bts::admin::AdminServiceUtils::find_account_product_position_limit( - account, _product); - _tree.emplace_back( - account.id(), - bts::admin::AdminServiceUtils::has_id(account.parent_account_id()) - ? account.parent_account_id() - : 0, - limit != nullptr ? *limit : zero_limit, enforce_net); + ignore_position_enforcement = + _is_spread() && + account.risk_limit_enforcement().ignore_spread_position_limits(); + } + const AccountProductPositionLimitUnit *limit = + AdminServiceUtils::find_account_product_position_limit(account, + _product); + _tree.emplace_back(account.id(), + limit != nullptr ? *limit : zero_limit, + enforce_net, + ignore_position_enforcement); } } } // namespace orderman diff --git a/src/lib/orderman/limiter/ProductLimitTree.h b/src/lib/orderman/limiter/ProductLimitTree.h index 93f6ad5a6..e873ace87 100644 --- a/src/lib/orderman/limiter/ProductLimitTree.h +++ b/src/lib/orderman/limiter/ProductLimitTree.h @@ -12,6 +12,7 @@ namespace bts{ namespace orderman{ +// Scope : // this keeps the state for all the accounts // for a single product, via a vector of // ProductLimitNode, which is the only state @@ -32,5 +33,9 @@ public: return nullptr; } const api::Product& product() const {return _product;} +private: + bool _is_spread() const { + return _product.type() == api::SecurityType::SECURITYTYPE_SPREAD; + } }; }} diff --git a/src/lib/orderman/limiter/SecurityLimitNode.cpp b/src/lib/orderman/limiter/SecurityLimitNode.cpp index 9fbb209b0..63e12259a 100644 --- a/src/lib/orderman/limiter/SecurityLimitNode.cpp +++ b/src/lib/orderman/limiter/SecurityLimitNode.cpp @@ -5,16 +5,15 @@ namespace orderman { SecurityLimitNode::SecurityLimitNode( uint32_t account_id, - uint32_t, // parent_account_id, - const bts::api::admin::AccountSecurityPositionLimitUnit &limit, - bool apply_sec_position_limits, ProductLimitNode &product_node, + ProductLimitNode& product_node, uint32_t clip_size) - : _account_id(account_id), - //_parent_account_id(parent_account_id), - _limit(limit), _long_pos_limit(limit.long_position_limit()), - _short_pos_limit(-1 * limit.short_position_limit()), - _apply_sec_position_limits(apply_sec_position_limits), - _product_node(product_node), _clip_size(clip_size), _net_pos(0) {} + : + _account_id(account_id), + _clip_size(clip_size), + _product_node(product_node), + _long_pos(0), + _short_pos(0) + {} } // namespace orderman -} // namespace bts +} // namespace bts \ No newline at end of file diff --git a/src/lib/orderman/limiter/SecurityLimitNode.h b/src/lib/orderman/limiter/SecurityLimitNode.h index 44f8a53d7..2e0889337 100644 --- a/src/lib/orderman/limiter/SecurityLimitNode.h +++ b/src/lib/orderman/limiter/SecurityLimitNode.h @@ -3,101 +3,117 @@ #include #include "lib/api/services/OrderService.pb.h" +#include "lib/api/services/PositionService.pb.h" +#include "lib/api/services/PositionService.server.pb.h" #include "lib/orderman/orderman_utils/orderman_common.h" #include "lib/domain_model/Security.pb.h" #include "lib/api/services/AdminService.server.pb.h" #include "lib/api/service_helpers/AdminServiceUtils.h" +#include "lib/api/service_helpers/PositionUtilities.h" #include "Limiter.h" #include "LimiterUtils.h" #include "ProductLimitTree.h" namespace bts{ namespace orderman{ -// Keeps the running position and risk limits -// (both position limits and clip sizes) for a -// single -// It also keeps a reference to the product -// node, that is updated as fills come in. + +// Scope : +// Keeps the running position and clip limits +// for a single . +// Has a reference to the ProductLimitNode for the product +// that this security belongs to. +// Position limits are enforced through the ProductLimitNode class SecurityLimitNode{ private: - const uint32_t _account_id; // , _parent_account_id; - const bts::api::admin::AccountSecurityPositionLimitUnit& _limit; - const int _long_pos_limit, _short_pos_limit; - const bool _apply_sec_position_limits; - ProductLimitNode& _product_node; + const uint32_t _account_id; const uint32_t _clip_size; - int _net_pos; + ProductLimitNode& _product_node; + // this is the committed position. working order + // adds and removes are passed straight to the + // product node. Position updates are changed to + // increments and then passed + uint32_t _long_pos; + uint32_t _short_pos; public: - SecurityLimitNode(uint32_t account_id, uint32_t parent_account_id, - const bts::api::admin::AccountSecurityPositionLimitUnit& limit, - bool apply_sec_position_limits, + SecurityLimitNode(uint32_t account_id, ProductLimitNode& product_limit, uint32_t clip_size); - //accessors - /////////// - uint32_t account_id() const {return _account_id;} - int net_pos() const {return _net_pos;} - // position updates - /////////////////// + // accessors /////////////////////////////////////////////////////////////// + //////////////////////////////////////////////////////////////////////////// + uint32_t account_id() const { + return _account_id; + } + // position updates //////////////////////////////////////////////////////// + //////////////////////////////////////////////////////////////////////////// void copy_positions_from(const SecurityLimitNode& source){ - _net_pos = source.net_pos(); + _long_pos = source._long_pos; + _short_pos = source._short_pos; // this operation will be repeated since multiple // security nodes share the same product node, but // since its a copy (and not as addition) for // simplicity's sake we do this _product_node.copy_positions_from(source._product_node); } - void add_position(api::Side side, uint32_t qty){ + void add_working_order(api::Side side, uint32_t qty){ _product_node.add_position(side, qty); - if (side==api::Side::SIDE_BUY) - _net_pos += qty; - else - _net_pos -= qty; } - void remove_position(api::Side side, uint32_t qty){ + void remove_working_order(api::Side side, uint32_t qty){ _product_node.remove_position(side, qty); - if (side==api::Side::SIDE_BUY) - _net_pos -= qty; - else - _net_pos += qty; } - // risk limit checks - //////////////////// - Limiter::LimiterQueryResult can_send_new_order(api::Side side, + void update_position (AccountID account_id, + Quantity long_pos, + Quantity short_pos){ + bts_assert(_account_id == account_id.value()); + uint32_t new_long = long_pos.value(), + new_short = short_pos.value(); + // long + if(new_long > _long_pos) + _product_node.add_position(api::Side::SIDE_BUY, + new_long-_long_pos); + else if(new_long < _long_pos) + _product_node.remove_position(api::Side::SIDE_BUY, + _long_pos-new_long); + _long_pos = new_long; + // short + if(new_short > _short_pos) + _product_node.add_position(api::Side::SIDE_SELL, + new_short-_short_pos); + else if(new_short < _short_pos) + _product_node.remove_position(api::Side::SIDE_SELL, + _short_pos-new_short); + _short_pos = new_short; + } + // risk limit checks /////////////////////////////////////////////////////// + //////////////////////////////////////////////////////////////////////////// + Limiter::LimiterQueryResult can_send_new_order( + api::Side side, Quantity quantity, bool enforce_clip = true) const{ Limiter::LimiterQueryResult result;result.result= Limiter::OK; 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()); + result.error_msg = fmt::format("Security Clip Size (Max {} Qty {})", + _clip_size, quantity.value()); return result; } - if (_apply_sec_position_limits) - LimiterUtils::can_send_new_order_net (side, quantity, true, - _long_pos_limit, _short_pos_limit, - _net_pos, result); if (result.ok()) result = _product_node.can_send_new_order(side, quantity); return result; } - Limiter::LimiterQueryResult can_update_order(api::Side side, Quantity from, Quantity to, - bool enforce_clip = true) 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 (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()); + result.error_msg = fmt::format("Security Clip Size (Max {} Qty {})", + _clip_size, to.value()); return result; } - if (_apply_sec_position_limits) - LimiterUtils::can_update_order_net (side, from, to, true, - _long_pos_limit, _short_pos_limit, - _net_pos, result); if (result.ok()) result = _product_node.can_update_order(side, from, to); return result; } - // test only functions - ////////////////////// - const bts::api::admin::AccountSecurityPositionLimitUnit& test_only_func_limit () const {return _limit;} }; -}} +}} \ No newline at end of file diff --git a/src/lib/orderman/limiter/SecurityLimitTree.cpp b/src/lib/orderman/limiter/SecurityLimitTree.cpp index a0a150961..5cbb680bc 100644 --- a/src/lib/orderman/limiter/SecurityLimitTree.cpp +++ b/src/lib/orderman/limiter/SecurityLimitTree.cpp @@ -1,29 +1,26 @@ #include "lib/orderman/limiter/SecurityLimitTree.h" +using namespace bts::api::admin; +using namespace bts::admin; + namespace bts { namespace orderman { SecurityLimitTree::SecurityLimitTree( - const api::Security &security, ProductLimitTree &product_tree, - const bts::api::admin::AdminData &admin_data, - const bts::api::admin::AccountSecurityPositionLimitUnit &zero_limit) { + const api::Security &security, + ProductLimitTree &product_tree, + const AdminData &admin_data) { for (const auto &account : admin_data.accounts()) { - ProductLimitNode *product_node_ptr = - product_tree.find_account(account.id()); + ProductLimitNode *product_node_ptr = + product_tree.find_account(account.id()); bts_assert(product_node_ptr != nullptr); - const bts::api::admin::AccountSecurityPositionLimitUnit *limit = - bts::admin::AdminServiceUtils::find_account_security_position_limit( - account, security); uint32_t clip_size = - bts::admin::AdminServiceUtils::find_account_security_clip_size( - account, security); + AdminServiceUtils::find_account_security_clip_size(account, + security); _tree.emplace_back( account.id(), - bts::admin::AdminServiceUtils::has_id(account.parent_account_id()) - ? account.parent_account_id() - : 0, - limit != nullptr ? *limit : zero_limit, limit != nullptr, - *product_node_ptr, clip_size); + *product_node_ptr, + clip_size); } } } // namespace orderman diff --git a/src/lib/orderman/limiter/SecurityLimitTree.h b/src/lib/orderman/limiter/SecurityLimitTree.h index 37eba73f5..f02786689 100644 --- a/src/lib/orderman/limiter/SecurityLimitTree.h +++ b/src/lib/orderman/limiter/SecurityLimitTree.h @@ -13,59 +13,74 @@ namespace bts{ namespace orderman{ -// this keeps the state for all the accounts -// for a single security. the only state is a -// vector of SecurityLimitNode (which keeps +// Scope : +// this keeps the state a single security for all accounts +// The only state is a vector of SecurityLimitNode (which keeps // the actual data) class SecurityLimitTree{ private: std::vector _tree; public: - SecurityLimitTree(const api::Security& security, - ProductLimitTree& product_tree, - const bts::api::admin::AdminData& admin_data, - const bts::api::admin::AccountSecurityPositionLimitUnit& zero_limit); - // position updates - /////////////////// - void add_position(AccountID account_id, api::Side side, - Quantity quantity){ + SecurityLimitTree( + const api::Security& security, + ProductLimitTree& product_tree, + const bts::api::admin::AdminData& admin_data); + // position updates //////////////////////////////////////////////////////// + //////////////////////////////////////////////////////////////////////////// + void add_working_order(AccountID account_id, + api::Side side, + Quantity quantity){ SecurityLimitNode* node_ptr = _find_account(account_id.value()); bts_assert(node_ptr != nullptr); - node_ptr->add_position(side, quantity.value()); + node_ptr->add_working_order(side, quantity.value()); } - void remove_position(AccountID account_id, - api::Side side, Quantity quantity){ + void remove_working_order(AccountID account_id, + api::Side side, + Quantity quantity){ SecurityLimitNode* account_ptr = _find_account(account_id.value()); bts_assert(account_ptr != nullptr); - account_ptr->remove_position(side, quantity.value()); + account_ptr->remove_working_order(side, quantity.value()); } - void copy_positions_from(SecurityLimitTree& source){ + void update_position (AccountID account_id, + Quantity long_pos, + Quantity short_pos){ + SecurityLimitNode* account_ptr = _find_account(account_id.value()); + bts_assert(account_ptr != nullptr); + account_ptr->update_position(account_id, long_pos, short_pos); + } + void copy_positions_from(const SecurityLimitTree& source){ for (SecurityLimitNode& node : _tree){ - SecurityLimitNode* source_node_ptr = source._find_account(node.account_id()); + SecurityLimitNode const* source_node_ptr = + source._find_account(node.account_id()); + // this object is the updated one, so a new account wont + // be found in the old object if(source_node_ptr != nullptr) node.copy_positions_from(*source_node_ptr); } } - // risk limit checks - //////////////////// + // risk limit checks /////////////////////////////////////////////////////// + //////////////////////////////////////////////////////////////////////////// Limiter::LimiterQueryResult can_send_new_order(AccountID account_id, api::Side side, Quantity quantity, - bool enforce_clip = true) const{ + 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, enforce_clip); } Limiter::LimiterQueryResult can_update_order(AccountID account_id, api::Side side, - Quantity from, Quantity to, - bool enforce_clip = true) 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, enforce_clip); } - // accessors - //////////// + // accessors /////////////////////////////////////////////////////////////// + //////////////////////////////////////////////////////////////////////////// const SecurityLimitNode* find_account (uint32_t account_id) const{ return _find_account(account_id); } diff --git a/src/lib/orderman/limiter/tests/limit_tests.cpp b/src/lib/orderman/limiter/tests/limit_tests.cpp index a6b89a7f2..8cc48e701 100644 --- a/src/lib/orderman/limiter/tests/limit_tests.cpp +++ b/src/lib/orderman/limiter/tests/limit_tests.cpp @@ -107,18 +107,6 @@ void fill_future_security(api::Security &sec, uint32_t key, sec.mutable_contract_spec()->MergeFrom(c_spec); } -void verify_security_limit_data(SecurityLimitNode const *acc_sec_state, - uint32_t expected_long_limit, - uint32_t expected_short_limit, - int expected_net_pos) { - REQUIRE(acc_sec_state != nullptr); - REQUIRE(acc_sec_state->test_only_func_limit().long_position_limit() == - expected_long_limit); - REQUIRE(acc_sec_state->test_only_func_limit().short_position_limit() == - expected_short_limit); - REQUIRE(acc_sec_state->net_pos() == expected_net_pos); -} - void verify_product_limit_data(ProductLimitNode const *acc_prod_state, uint32_t expected_long_limit, uint32_t expected_short_limit, diff --git a/src/lib/orderman/order_factory/OrderFactory.cpp b/src/lib/orderman/order_factory/OrderFactory.cpp index b6f75a0b2..8ffc67968 100644 --- a/src/lib/orderman/order_factory/OrderFactory.cpp +++ b/src/lib/orderman/order_factory/OrderFactory.cpp @@ -89,7 +89,7 @@ Order &OrderFactory::create(OrderKey parent, // field correctly. Algos will just propagate the value of this // field down so eventually native orders launched by algos will // get the right value. - if (dynamic_cast(&order) == nullptr && + if (dynamic_cast(&order) == nullptr && def.manual_order_indicator() == api::orderman::MANUAL_ORDER_INDICATOR_YES) { // An algo order incorrectly marked as a manual order @@ -114,7 +114,7 @@ Order &OrderFactory::recover(api::orderman::OrderRecord const &rec) { return order; } -NativeOrder *OrderFactory::make_order(const api::Venue exchange) { +NativeOrderBase *OrderFactory::make_order(const api::Venue exchange) { if (exchange == api::Venue::VENUE_ICE || exchange == api::Venue::VENUE_SIMICE) return new ICENativeOrder(); @@ -122,7 +122,7 @@ NativeOrder *OrderFactory::make_order(const api::Venue exchange) { return new GLBXNativeOrder(); } -NativeOrder * +NativeOrderBase * OrderFactory::make_native_order(const api::orderman::OrderDefinition &def) { auto secptr = me->sec_db.resolve(SecurityKey(def.security_key())); if (secptr == nullptr) @@ -140,7 +140,7 @@ Order &OrderFactory::recreate_from_exe_report(ExecutionReport const &r) { case NATIVE_ORDER_TYPE_LIMIT: case NATIVE_ORDER_TYPE_STOP: case NATIVE_ORDER_TYPE_STOP_LIMIT: { - std::unique_ptr native_order(make_order(r.exchange)); + std::unique_ptr native_order(make_order(r.exchange)); native_order->_key = r.order_key; native_order->_parent_key = null_order_key(); auto def = native_order->on_init_from_exe_report( diff --git a/src/lib/orderman/order_factory/OrderFactory.h b/src/lib/orderman/order_factory/OrderFactory.h index 2f4fbbdcc..06410463c 100644 --- a/src/lib/orderman/order_factory/OrderFactory.h +++ b/src/lib/orderman/order_factory/OrderFactory.h @@ -11,7 +11,7 @@ class ISecurityDB; } namespace orderman { class Order; -class NativeOrder; +class NativeOrderBase; class DynamicOrderRegistry; } // namespace orderman namespace orderman { @@ -46,8 +46,8 @@ class OrderFactory : public IOrderFactory { private: Order &allocate(OrderKey k, OrderKey pk, api::orderman::OrderDefinition const &); - NativeOrder* make_order(const api::Venue exchange); - NativeOrder* make_native_order(const api::orderman::OrderDefinition& def); + NativeOrderBase* make_order(const api::Venue exchange); + NativeOrderBase* make_native_order(const api::orderman::OrderDefinition& def); struct PImpl; std::unique_ptr me; diff --git a/src/lib/orderman/orderman_utils/fix_order_codec.h b/src/lib/orderman/orderman_utils/fix_order_codec.h index 074dc9c56..14cceb2ce 100644 --- a/src/lib/orderman/orderman_utils/fix_order_codec.h +++ b/src/lib/orderman/orderman_utils/fix_order_codec.h @@ -1,51 +1,55 @@ #pragma once -#include #include +#include -#include "orderman_common.h" -#include "lib/jetfix/codec/i32_toa.h" #include "lib/jetfix/codec/MessageDefinitions.h" +#include "lib/jetfix/codec/i32_toa.h" #include "lib/utils/bts_assert.h" +#include "orderman_common.h" #include "lib/utils/base62.h" namespace bts { -namespace api {class Security;} +namespace api { +class Security; +} namespace orderman { -constexpr int clordid_sz() {return 20;} -constexpr int price_sz() {return 17;} -constexpr int quantity_sz() {return 6;} -constexpr int max_show_sz() {return 6;} +constexpr int clordid_sz() { return 20; } +constexpr int price_sz() { return 17; } +constexpr int quantity_sz() { return 6; } +constexpr int max_show_sz() { return 6; } constexpr int memo_sz() {return 12;} constexpr int ice_orderid_sz() {return 12;} +constexpr uint32_t corid_sz() { + return 16; +} // 5 for channel, 1 separator, 10 sn -inline void encode_clorid(OrderKey ok, SecurityKey sk,std::string *out) { - u32_toa(sk.value(),&((*out)[0]),clordid_sz()/2); - u32_toa(ok.value(),&((*out)[clordid_sz()/2]),clordid_sz()/2); +inline void encode_clorid(OrderKey ok, SecurityKey sk, std::string *out) { + u32_toa(sk.value(), &((*out)[0]), clordid_sz() / 2); + u32_toa(ok.value(), &((*out)[clordid_sz() / 2]), clordid_sz() / 2); } -inline void decode_clorid(std::string const&in, OrderKey *ok, SecurityKey *sk) -{ +inline void decode_clorid(std::string const &in, OrderKey *ok, + SecurityKey *sk) { bts_assert(in.size() == clordid_sz()); - char buffer[clordid_sz()/2+1]; - buffer[clordid_sz()/2] = 0; + char buffer[clordid_sz() / 2 + 1]; + buffer[clordid_sz() / 2] = 0; - std::memcpy(buffer,&in[0],clordid_sz()/2); + std::memcpy(buffer, &in[0], clordid_sz() / 2); *sk = SecurityKey(std::atoi(buffer)); - std::memcpy(buffer,&in[clordid_sz()/2],clordid_sz()/2); + std::memcpy(buffer, &in[clordid_sz() / 2], clordid_sz() / 2); *ok = OrderKey(std::atoi(buffer)); } -template -OrderKey get_order_key(MSG const &msg) -{ - bts_assert(msg.template has ()); +template OrderKey get_order_key(MSG const &msg) { + + bts_assert(msg.template has()); OrderKey result; SecurityKey dummy; - decode_clorid(msg.template get(),&result,&dummy); + decode_clorid(msg.template get(), &result, &dummy); return result; } @@ -92,4 +96,14 @@ std::string get_ice_clord_id_string(const MSG& msg) } -}} +inline void encode_corid(const char *channel, uint32_t sn, std::array &out) { + memset(&out[0], ' ', out.size()); + auto ch_sz = std::min(strlen(channel), (size_t)5); + strncpy(&out[0], channel, ch_sz); + out[ch_sz] = ':'; + auto sz_written = u32_toa(sn, &out[ch_sz + 1]); + out[ch_sz+1+sz_written]=' '; +} + +} // namespace orderman +} // namespace bts diff --git a/src/lib/orderman/orders/GLBXNativeOrder.cpp b/src/lib/orderman/orders/GLBXNativeOrder.cpp index 1d6240992..c5d26a72f 100644 --- a/src/lib/orderman/orders/GLBXNativeOrder.cpp +++ b/src/lib/orderman/orders/GLBXNativeOrder.cpp @@ -28,10 +28,10 @@ using namespace api::orderman; OrderStatus map_external_status(NativeOrderStatus); -struct GLBXNativeOrder::PImpl { +struct NativeOrder::PImpl { PImpl() : services(nullptr), session_services(), sec(nullptr), - previous_position_contribution(0), definition(), state(), + previous_working_position(0), definition(), state(), price_encoding_precision(7), exchange_order_id() { exchange_order_id.reserve(100); } @@ -40,7 +40,7 @@ struct GLBXNativeOrder::PImpl { SessionServices *session_services; const api::Security *sec; - Quantity previous_position_contribution; + Quantity previous_working_position; OrderDefinition definition; OrderState state; @@ -54,12 +54,14 @@ struct GLBXNativeOrder::PImpl { jetfix::FIXFragment::Bookmark update_stop_px_bm; jetfix::FIXFragment::Bookmark update_transact_time_bm; + jetfix::FIXFragment::Bookmark update_corid_bm; jetfix::FIXFragment create_encoding; jetfix::FIXFragment::Bookmark create_price_bm; jetfix::FIXFragment::Bookmark create_quantity_bm; jetfix::FIXFragment::Bookmark create_transact_time_bm; + jetfix::FIXFragment::Bookmark create_corid_bm; jetfix::FIXFragment cancel_encoding; jetfix::FIXFragment::Bookmark @@ -72,14 +74,22 @@ struct GLBXNativeOrder::PImpl { std::optional> buffered_update; }; -GLBXNativeOrder::GLBXNativeOrder() : me(new PImpl()) {} +jetfix::FIXFragment::Bookmark +update_corid(jetfix::FIXFragment::Bookmark const &bm, + jetfix::FIXFragment &encoding, trace::CorID const &cid) { + std::array buffer; + encode_corid(cid.channel, cid.sn, buffer); + return encoding.overwrite(bm, &buffer[0], corid_sz()); +} -OrderDefinition const &GLBXNativeOrder::definition() const { +NativeOrder::NativeOrder() : me(new PImpl()) {} + +OrderDefinition const &NativeOrder::definition() const { return me->definition; } -void GLBXNativeOrder::on_init_new(OrderDefinition const &def, - const PreActivationOrderServices &serv) { +void NativeOrder::on_init_new(OrderDefinition const &def, + const PreActivationOrderServices &serv) { // order was just created. me->definition = def; @@ -161,8 +171,8 @@ void GLBXNativeOrder::on_init_new(OrderDefinition const &def, init_fix(); } -void GLBXNativeOrder::on_init_recovered(OrderRecord const &rec, - const PreActivationOrderServices &serv) { +void NativeOrder::on_init_recovered(OrderRecord const &rec, + const PreActivationOrderServices &serv) { // order was recovered from our orders database or from an execution // report @@ -171,7 +181,7 @@ void GLBXNativeOrder::on_init_recovered(OrderRecord const &rec, me->definition = def; me->state = rec.state(); me->sec = serv.sec_db.resolve(SecurityKey(def.security_key())); - me->previous_position_contribution = Quantity(rec.state().filled_quantity()); + me->previous_working_position = Quantity(0); auto const &native_state = get_native_state(me->state); switch (me->state.status()) { @@ -231,8 +241,8 @@ void GLBXNativeOrder::on_init_recovered(OrderRecord const &rec, } api::orderman::OrderDefinition -GLBXNativeOrder::on_init_from_exe_report(ExecutionReport const &er, - const PreActivationOrderServices &os) { +NativeOrder::on_init_from_exe_report(ExecutionReport const &er, + const PreActivationOrderServices &os) { auto k = er.order_key; OrderRecord orecord; @@ -330,10 +340,10 @@ GLBXNativeOrder::on_init_from_exe_report(ExecutionReport const &er, return orecord.def(); } -GLBXNativeOrder::~GLBXNativeOrder() {} +NativeOrder::~NativeOrder() {} -void GLBXNativeOrder::warm_up_activate(OrderServicesData &data, - OrderServices &services) { +void NativeOrder::warm_up_activate(OrderServicesData &data, + OrderServices &services) { bts_assert(!me->services); me->services = &services; @@ -358,9 +368,9 @@ void GLBXNativeOrder::warm_up_activate(OrderServicesData &data, } OrderOpResult -GLBXNativeOrder::_on_activate(log::Level reject_level, log::Level transition_level, - Price p, Quantity q, - bts::orderman::OrderServices &services) { +NativeOrder::_on_activate(log::Level reject_level, log::Level transition_level, + Price p, Quantity q, + bts::orderman::OrderServices &services) { me->state.set_price(p.value()); me->state.set_quantity(q.value()); me->state.set_filled_quantity(0); @@ -464,11 +474,15 @@ GLBXNativeOrder::_on_activate(log::Level reject_level, log::Level transition_lev auto const &ts = services.get_time_str(); me->create_transact_time_bm = encoding.overwrite( me->create_transact_time_bm, &ts.value()[0], ts.value().size()); + + me->create_corid_bm = + update_corid(me->create_corid_bm,encoding, services.corid()); + return OrderOpResult{}; } -OrderOpResult GLBXNativeOrder::on_activate(Price p, Quantity q, - bts::orderman::OrderServices &services) { +OrderOpResult NativeOrder::on_activate(Price p, Quantity q, + bts::orderman::OrderServices &services) { bts_assert(!me->services); me->services = &services; auto result = OrderOpResult{}; @@ -476,7 +490,7 @@ OrderOpResult GLBXNativeOrder::on_activate(Price p, Quantity q, case ORDER_STATUS_INACTIVE: { auto scoped_log = - services.log_scope("GLBXNativeOrder::on_activate", key().value()); + services.log_scope("NativeOrder::on_activate", key().value()); // this is a brand new order result = _on_activate(Warn, Info, p, q, services); @@ -570,19 +584,19 @@ OrderOpResult GLBXNativeOrder::on_activate(Price p, Quantity q, return result; } -OrderOpResult GLBXNativeOrder::on_update(Price p, Quantity q) { +OrderOpResult NativeOrder::on_update(Price p, Quantity q) { api::orderman::Update update; update.set_price(p.value()); update.set_quantity(q.value()); return on_update(update); } -OrderOpResult GLBXNativeOrder::on_update(api::orderman::Update const &update) { +OrderOpResult NativeOrder::on_update(api::orderman::Update const &update) { auto p = Price(update.price()); auto q = Quantity(update.quantity()); auto scoped_log = - me->services->log_scope("GLBXNativeOrder::on_update", key().value()); + me->services->log_scope("NativeOrder::on_update", key().value()); auto result = OrderOpResult{}; switch (me->state.status()) { @@ -670,6 +684,9 @@ OrderOpResult GLBXNativeOrder::on_update(api::orderman::Update const &update) { me->update_transact_time_bm = encoding.overwrite( me->update_transact_time_bm, &ts.value()[0], ts.value().size()); + me->update_corid_bm = + update_corid(me->update_corid_bm,encoding, me->services->corid()); + me->services->queue_fix(*me->session_services, jetfix::MessageTypes::CancelReplace, me->update_encoding); @@ -707,9 +724,9 @@ OrderOpResult GLBXNativeOrder::on_update(api::orderman::Update const &update) { return result; } -OrderOpResult GLBXNativeOrder::on_detach() { return OrderOpResult{}; } +OrderOpResult NativeOrder::on_detach() { return OrderOpResult{}; } -void GLBXNativeOrder::on_attached( +void NativeOrder::on_attached( api::orderman::OrderDefinition const &new_parent) { switch (me->state.status()) { @@ -764,9 +781,9 @@ void GLBXNativeOrder::on_attached( } } -OrderOpResult GLBXNativeOrder::on_cancel() { +OrderOpResult NativeOrder::on_cancel() { auto scoped_log = - me->services->log_scope("GLBXNativeOrder::on_cancel", key().value()); + me->services->log_scope("NativeOrder::on_cancel", key().value()); auto result = OrderOpResult{}; switch (me->state.status()) { @@ -822,7 +839,7 @@ OrderOpResult GLBXNativeOrder::on_cancel() { return result; } -bool GLBXNativeOrder::is_old_exe_report( +bool NativeOrder::is_old_exe_report( api::orderman::NativeOrderState &native_state, const NewExecutionReportEvent &er) const { if (native_state.last_transact_time().size() != @@ -844,9 +861,9 @@ bool GLBXNativeOrder::is_old_exe_report( return true; } -void GLBXNativeOrder::on_exe_report(const NewExecutionReportEvent &er) { +void NativeOrder::on_exe_report(const NewExecutionReportEvent &er) { auto scoped_log = - me->services->log_scope("GLBXNativeOrder::on_exe_report", key().value()); + me->services->log_scope("NativeOrder::on_exe_report", key().value()); auto &state = me->state; auto &native_state = get_native_state(state); @@ -909,7 +926,7 @@ void GLBXNativeOrder::on_exe_report(const NewExecutionReportEvent &er) { state.set_price(er.data->price.value()); state.set_quantity(er.data->quantity.value()); } - + switch (me->state.status()) { // terminal states, nothing can change them case ORDER_STATUS_COMPLETED: @@ -969,13 +986,13 @@ void GLBXNativeOrder::on_exe_report(const NewExecutionReportEvent &er) { } } -void GLBXNativeOrder::on_cancel_reject(const orderman::CancelRejectEvent &cr) { +void NativeOrder::on_cancel_reject(const orderman::CancelRejectEvent &cr) { LOG(OrderMan, Warn, "Cancel reject received for order {}", key().value()); switch (me->state.status()) { case ORDER_STATUS_COMPLETED: - case ORDER_STATUS_CANCELED: + case ORDER_STATUS_CANCELED: break; case ORDER_STATUS_DISCONNECTED: @@ -997,7 +1014,7 @@ void GLBXNativeOrder::on_cancel_reject(const orderman::CancelRejectEvent &cr) { } } -void GLBXNativeOrder::on_gateway_status(const GatewayStatusChangeEvent &ev) { +void NativeOrder::on_gateway_status(const GatewayStatusChangeEvent &ev) { if (ev.is_connected) { switch (me->state.status()) { case ORDER_STATUS_COMPLETED: @@ -1057,24 +1074,22 @@ void GLBXNativeOrder::on_gateway_status(const GatewayStatusChangeEvent &ev) { report_state(); } -void GLBXNativeOrder::update_working_position() { +void NativeOrder::update_working_position() { if (me->sec) { - auto current_position = position_contribution(); - auto previous_position = me->previous_position_contribution; - me->previous_position_contribution = current_position; + auto current_position = working_position(); + auto previous_position = me->previous_working_position; + me->previous_working_position = current_position; if (current_position.value() > previous_position.value()) - me->services->limiter().add( + me->services->limiter().add_to_working_position( *me->sec, AccountID(me->definition.account_id()), me->definition.side(), - Quantity(current_position.value() - previous_position.value()), - true); + Quantity(current_position.value() - previous_position.value())); else if (previous_position.value() > current_position.value()) - me->services->limiter().remove( + me->services->limiter().remove_from_working_position( *me->sec, AccountID(me->definition.account_id()), me->definition.side(), - Quantity(previous_position.value() - current_position.value()), - true); + Quantity(previous_position.value() - current_position.value())); } else LOG(OrderMan, Error, "Order {} ignored for purposes of limit enforcing " @@ -1082,12 +1097,12 @@ void GLBXNativeOrder::update_working_position() { key().value(), me->definition.security_key()); } -void GLBXNativeOrder::report_state() { +void NativeOrder::report_state() { update_working_position(); me->services->report_state(me->state); } -void GLBXNativeOrder::init_fix() { +void NativeOrder::init_fix() { bts_assert(me->sec); using namespace jetfix; @@ -1125,6 +1140,9 @@ void GLBXNativeOrder::init_fix() { datetime::FIX_timestamp(datetime::now())); me->create_quantity_bm = create_encoding.add(0, quantity_sz()); + me->create_corid_bm = + create_encoding.add(std::string(corid_sz(), ' ')); + // initialize update FIX auto &update_encoding = me->update_encoding; update_encoding.add(rr.operator_id()); @@ -1151,6 +1169,8 @@ void GLBXNativeOrder::init_fix() { datetime::FIX_timestamp(datetime::now())); me->update_quantity_bm = update_encoding.add(0, quantity_sz()); + me->update_corid_bm = + update_encoding.add(std::string(corid_sz(), ' ')); // initialize cancel FIX auto &cancel_encoding = me->cancel_encoding; @@ -1253,7 +1273,7 @@ void GLBXNativeOrder::init_fix() { } } -void GLBXNativeOrder::add_exchange_order_id_to_fix() { +void NativeOrder::add_exchange_order_id_to_fix() { bts_assert(has_exchange_order_id()); me->update_encoding.add(me->exchange_order_id); me->cancel_encoding.add(me->exchange_order_id); @@ -1296,16 +1316,16 @@ OrderStatus map_external_status(NativeOrderStatus status) { return ORDER_STATUS_ERROR; } -bool GLBXNativeOrder::has_exchange_order_id() { +bool NativeOrder::has_exchange_order_id() { return me->exchange_order_id.size(); } -void GLBXNativeOrder::transition(api::orderman::OrderStatus new_status, +void NativeOrder::transition(api::orderman::OrderStatus new_status, std::string const &msg) { transition(Info, new_status, msg); } -void GLBXNativeOrder::transition(log::Level level, +void NativeOrder::transition(log::Level level, api::orderman::OrderStatus new_status, std::string const &msg) { auto const &from = OrderStatus_Name(me->state.status()); @@ -1317,7 +1337,7 @@ void GLBXNativeOrder::transition(log::Level level, me->state.set_last_msg(msg); } -void GLBXNativeOrder::transition(api::orderman::OrderStatus new_status) { +void NativeOrder::transition(api::orderman::OrderStatus new_status) { auto const &from = OrderStatus_Name(me->state.status()); auto const &to = OrderStatus_Name(new_status); if (from != to) @@ -1330,7 +1350,7 @@ void GLBXNativeOrder::transition(api::orderman::OrderStatus new_status) { me->state.set_status(new_status); } -void GLBXNativeOrder::transition_to_error( +void NativeOrder::transition_to_error( api::orderman::NativeOrderState::SubError sub_error, std::string const &msg) { me->state.mutable_limit()->mutable_native_state()->set_sub_error_code( @@ -1338,20 +1358,22 @@ void GLBXNativeOrder::transition_to_error( transition(ORDER_STATUS_ERROR, msg); } -Quantity GLBXNativeOrder::position_contribution() { +Quantity NativeOrder::working_position() { switch (me->state.status()) { case ORDER_STATUS_WORKING: case ORDER_STATUS_PENDING_WORKING: case ORDER_STATUS_DISCONNECTED: - return Quantity( - std::max(me->state.quantity(), me->state.filled_quantity())); + if (me->state.quantity() > me->state.filled_quantity()) + return Quantity(me->state.quantity() - me->state.filled_quantity()); + else + return Quantity(0); case ORDER_STATUS_REJECTED_BY_SPARK: case ORDER_STATUS_ERROR: case ORDER_STATUS_COMPLETED: case ORDER_STATUS_CANCELED: - return Quantity(me->state.filled_quantity()); + return Quantity(0); case ORDER_STATUS_INACTIVE: case OrderStatus_INT_MIN_SENTINEL_DO_NOT_USE_: @@ -1363,7 +1385,7 @@ Quantity GLBXNativeOrder::position_contribution() { return Quantity(0); } -bool GLBXNativeOrder::terminal_state(api::orderman::OrderStatus status) const { +bool NativeOrder::terminal_state(api::orderman::OrderStatus status) const { switch (status) { case ORDER_STATUS_COMPLETED: @@ -1389,7 +1411,7 @@ bool GLBXNativeOrder::terminal_state(api::orderman::OrderStatus status) const { } api::orderman::NativeOrderState & -GLBXNativeOrder::get_native_state(api::orderman::OrderState &st) { +NativeOrder::get_native_state(api::orderman::OrderState &st) { switch (st.custom_case()) { case OrderState::kMarket: return *st.mutable_market()->mutable_native_state(); @@ -1410,7 +1432,7 @@ GLBXNativeOrder::get_native_state(api::orderman::OrderState &st) { return *st.mutable_stop_limit()->mutable_native_state(); } -void GLBXNativeOrder::ignore_or_cancel(std::string const &msg, +void NativeOrder::ignore_or_cancel(std::string const &msg, api::orderman::OrderError error, OrderOpResult *out) { if (parent_key() == null_order_key()) diff --git a/src/lib/orderman/orders/GLBXNativeOrder.h b/src/lib/orderman/orders/GLBXNativeOrder.h index 17490d8db..ee2a8b001 100644 --- a/src/lib/orderman/orders/GLBXNativeOrder.h +++ b/src/lib/orderman/orders/GLBXNativeOrder.h @@ -1,6 +1,7 @@ #pragma once -#include "lib/orderman/orders/NativeOrder.h" +#include "lib/api/services/OrderService.pb.h" +#include "lib/orderman/orders/NativeOrderBase.h" #include "lib/tracing/PerfLog.h" #include @@ -12,10 +13,10 @@ class CancelRejectEvent; class GatewayStatusChangeEvent; class ExecutionReport; -class GLBXNativeOrder : public NativeOrder { +class NativeOrder : public NativeOrderBase { public: - GLBXNativeOrder(); - ~GLBXNativeOrder() override; + NativeOrder(); + ~NativeOrder() override; api::orderman::OrderDefinition const &definition() const override; void on_init_new(api::orderman::OrderDefinition const &, @@ -32,10 +33,7 @@ class GLBXNativeOrder : public NativeOrder { api::orderman::OrderDefinition on_init_from_exe_report(ExecutionReport const &, - const PreActivationOrderServices &) override; - void ignore_or_cancel(std::string const &, - api::orderman::OrderError, - OrderOpResult *out) ; + const PreActivationOrderServices &) override protected: void warm_up_activate(OrderServicesData &, OrderServices &) override; @@ -64,13 +62,18 @@ class GLBXNativeOrder : public NativeOrder { void update_working_position(); bool has_exchange_order_id(); - Quantity position_contribution(); + Quantity working_position(); bool is_old_exe_report(api::orderman::NativeOrderState &, const NewExecutionReportEvent &) const; bool terminal_state(api::orderman::OrderStatus status) const; api::orderman::NativeOrderState & get_native_state(api::orderman::OrderState &); + void ignore_or_cancel(std::string const &, + api::orderman::OrderError, + OrderOpResult *out) ; }; +typedef NativeOrder GLBXNativeOrder; + } // namespace orderman } // namespace bts diff --git a/src/lib/orderman/orders/ICENativeOrder.h b/src/lib/orderman/orders/ICENativeOrder.h index 2ab985d58..91a53f9c8 100644 --- a/src/lib/orderman/orders/ICENativeOrder.h +++ b/src/lib/orderman/orders/ICENativeOrder.h @@ -1,6 +1,6 @@ #pragma once -#include "lib/orderman/orders/NativeOrder.h" +#include "lib/orderman/orders/NativeOrderBase.h" #include "lib/tracing/PerfLog.h" #include @@ -12,7 +12,7 @@ class CancelRejectEvent; class GatewayStatusChangeEvent; class ExecutionReport; -class ICENativeOrder : public NativeOrder { +class ICENativeOrder : public NativeOrderBase { public: ICENativeOrder(); ~ICENativeOrder() override; diff --git a/src/lib/orderman/orders/NativeOrder.h b/src/lib/orderman/orders/NativeOrderBase.h similarity index 76% rename from src/lib/orderman/orders/NativeOrder.h rename to src/lib/orderman/orders/NativeOrderBase.h index cbc7f5582..158fb5424 100644 --- a/src/lib/orderman/orders/NativeOrder.h +++ b/src/lib/orderman/orders/NativeOrderBase.h @@ -1,21 +1,22 @@ -#pragma once - -#include "lib/api/services/OrderService.pb.h" -#include "lib/orderman/orders/Order.h" - -namespace bts { -namespace orderman { -class ExecutionReport; - -class NativeOrder : public Order { - public: - NativeOrder() = default; - ~NativeOrder() override {}; - - virtual api::orderman::OrderDefinition const &definition() const = 0; - api::orderman::OrderDefinition - virtual on_init_from_exe_report(ExecutionReport const &, const PreActivationOrderServices &) = 0; -}; - -} // namespace orderman -} // namespace bts +#pragma once + +#include "lib/api/services/OrderService.pb.h" +#include "lib/orderman/orders/Order.h" + +namespace bts { +namespace orderman { +class ExecutionReport; + +class NativeOrderBase : public Order { + public: + NativeOrderBase() = default; + ~NativeOrderBase() override {}; + + virtual api::orderman::OrderDefinition const &definition() const = 0; + api::orderman::OrderDefinition + virtual on_init_from_exe_report(ExecutionReport const &, const PreActivationOrderServices &) = 0; +}; + +} // namespace orderman +} // namespace bts + diff --git a/src/lib/orderman/orders/algo/AlgoWrapperOrder.cpp b/src/lib/orderman/orders/algo/AlgoWrapperOrder.cpp index 820fc771e..f7a41e77c 100644 --- a/src/lib/orderman/orders/algo/AlgoWrapperOrder.cpp +++ b/src/lib/orderman/orders/algo/AlgoWrapperOrder.cpp @@ -45,7 +45,8 @@ struct AlgoWrapperOrder::PImpl { using Edge = std::pair; using Action = std::function; - PImpl(std::unique_ptr &&algo) : algo(std::move(algo)) {} + PImpl(std::unique_ptr &&algo) + : algo(std::move(algo)), _my_key(null_order_key()) {} std::unique_ptr algo; Status status = Status::INACTIVE; @@ -71,6 +72,7 @@ struct AlgoWrapperOrder::PImpl { std::optional pending_update; std::optional> pending_terminate; OrderStatus external_status() const { return _state.status(); } + bool in_do_launch_attached = false; void set_external_status(OrderStatus s) { is_state_dirty = true; @@ -94,11 +96,19 @@ struct AlgoWrapperOrder::PImpl { } void set_fill_quantities(Quantity total_fills, Price last_price, - Price avg_price) { + Price avg_price, bool set_attach_time_fills) { is_state_dirty = true; _state.set_filled_quantity(total_fills.value()); _state.set_last_price(last_price.value()); _state.set_avg_price(avg_price.value()); + if (set_attach_time_fills) { + LOG(OrderMan, Diag, + "AlgoWrapperOrder {} setting fill at attach time to {}", + _my_key.value(), total_fills.value()); + _state.set_filled_at_attach_time_quantity(total_fills.value()); + } else + LOG(OrderMan, Diag, "AlgoWrapperOrder {} settings fills to {}", + _my_key.value(), total_fills.value()); } void set_ladder_visibility(bool is_visible) { @@ -107,8 +117,11 @@ struct AlgoWrapperOrder::PImpl { : OrderState::HIDDEN); } + void set_key(OrderKey k) { _my_key = k; } + private: OrderState _state; + OrderKey _my_key; }; struct AlgoWrapperOrder::ReportStateGuard { @@ -116,6 +129,11 @@ struct AlgoWrapperOrder::ReportStateGuard { ~ReportStateGuard() { if (owner.me->is_state_dirty) { owner.me->is_state_dirty = false; + LOG(OrderMan, Diag, + "AlgoWrapperOrder::ReportStateGuard. k: {} fills: fills at " + "attach: {}", + owner.key().value(), owner.me->state().filled_quantity(), + owner.me->state().filled_at_attach_time_quantity()); owner.me->services->report_state(owner.me->state()); } } @@ -142,6 +160,9 @@ void AlgoWrapperOrder::on_init_new(OrderDefinition const &def, // Save our order definition me->definition = def; + // Save our key + me->set_key(key()); + // If we are consuming the attachment we remove it from our definition so // that derived orders can use our definition to create their children // without triggering an attachment. @@ -169,6 +190,7 @@ void AlgoWrapperOrder::on_init_recovered( OrderRecord const &rec, const PreActivationOrderServices &serv) { auto const &def = rec.def(); me->definition = def; + me->set_key(key()); me->set_state(rec.state()); me->security_db = &serv.sec_db; me->order_factory = &serv.order_factory; @@ -201,7 +223,6 @@ void AlgoWrapperOrder::on_init_recovered( OrderOpResult AlgoWrapperOrder::on_activate(Price p, Quantity q, OrderServices &services) { - ReportStateGuard _(*this); bts_assert(!me->services); me->services = &services; @@ -218,16 +239,25 @@ OrderOpResult AlgoWrapperOrder::on_activate(Price p, Quantity q, me->set_price_quantity(p, q); for (auto &ct : me->child_templates) ct->builder.activate(*me->services); - if (q.value() == 0) + if (q.value() == 0) { + ReportStateGuard _(*this); terminate(ORDER_STATUS_COMPLETED); + } else if (is_attaching()) + // Do not report state until the attachment is complete, so + // that we can correctly populate the + // filled_at_attach_time_quantity field transition(Status::W_ATTACH); - else + else { + ReportStateGuard _(*this); transition(Status::WORKING); + } return {}; } - case Status::COMPLETED: + case Status::COMPLETED: { + ReportStateGuard _(*this); return {}; + } case Status::W_ATTACH: case Status::SHUTTING_DOWN: @@ -240,16 +270,20 @@ OrderOpResult AlgoWrapperOrder::on_activate(Price p, Quantity q, } OrderOpResult AlgoWrapperOrder::on_update(Update const &update) { - ReportStateGuard _(*this); switch (me->status) { case Status::W_ATTACH: + // We do not want to report state until the attach is complete + // so there is no report guard here me->pending_update = update; return {}; case Status::COMPLETED: - case Status::SHUTTING_DOWN: + case Status::SHUTTING_DOWN: { + ReportStateGuard _(*this); return {}; + } case Status::WORKING: { + ReportStateGuard _(*this); me->set_price_quantity(Price(update.price()), Quantity(update.quantity())); if (update.has_custom()) { @@ -268,8 +302,6 @@ OrderOpResult AlgoWrapperOrder::on_update(Update const &update) { } OrderOpResult AlgoWrapperOrder::on_update(Price p, Quantity q) { - ReportStateGuard _(*this); - Update up; up.set_price(p.value()); up.set_quantity(q.value()); @@ -368,8 +400,8 @@ void AlgoWrapperOrder::subscribe_book(SecurityKey k) { 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->book_subs[k] = + me->services->subscribe_to_book(k, [this, k](auto const &ev) { ReportStateGuard _(*this); me->algo->on_book(k, ev.book); }); @@ -384,18 +416,20 @@ void AlgoWrapperOrder::unsubscribe_book(SecurityKey k) { } void AlgoWrapperOrder::subscribe_ticker(SecurityKey k) { - if (!is_working()) return; + 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); - }); + 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; + 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); @@ -428,11 +462,10 @@ void AlgoWrapperOrder::subscribe_timer() { return; bts_assert(!me->has_timer_sub); me->has_timer_sub = true; - me->services->subscribe_to_time( - [this](auto const &) { - ReportStateGuard _(*this); - me->algo->on_timer(); - }); + me->services->subscribe_to_time([this](auto const &) { + ReportStateGuard _(*this); + me->algo->on_timer(); + }); } void AlgoWrapperOrder::unsubscribe_timer() { @@ -542,7 +575,11 @@ void AlgoWrapperOrder::update_fill_state(Quantity q, Price p) { (me->state().filled_quantity() * me->state().avg_price() + (q.value() - previous_qty) * p.value()) / q.value(); - me->set_fill_quantities(q, p, new_avg_price); + LOG(OrderMan, Diag, + "AlgoWrapperOrder updated its fill state. Total fills: {} Is " + "attaching: {}", + q.value(), me->in_do_launch_attached); + me->set_fill_quantities(q, p, new_avg_price, me->in_do_launch_attached); } } @@ -612,8 +649,9 @@ void AlgoWrapperOrder::on_attach_complete( key().value(), me->attach_target.value(), r.error_msg)); } break; - case Status::COMPLETED: + case Status::COMPLETED: { break; + } case Status::SHUTTING_DOWN: case Status::INACTIVE: case Status::WORKING: @@ -729,8 +767,17 @@ void AlgoWrapperOrder::setup_transitions() { _transition(Status::WORKING); auto &maybe_state = me->children[me->attach_target].state; bts_assert(maybe_state.has_value()); + + // This is ugly but its needed to make the API as simple as possible but + // still be able to distinguish fills that were present at the attachment + // time from fills that came later. update_fill_state() will look at this + // boolean to decide how it should populate fills_at_attach_time + me->in_do_launch_attached = true; me->algo->do_launch_attached(*this, me->attach_target, maybe_state.value()); + // Reset in_do_launch_attached back to false + me->in_do_launch_attached = false; + if (me->status == Status::WORKING && me->pending_update.has_value()) { auto update = me->pending_update.value(); me->pending_update.reset(); @@ -876,7 +923,8 @@ void AlgoWrapperOrder::update_child_record( Price(new_state.avg_price()), Quantity(new_state.filled_quantity() - old_state.total_fills.value()), Price(new_state.last_price()), - map_child_status(new_state.status())}; + map_child_status(new_state.status()), + Quantity(new_state.filled_at_attach_time_quantity())}; } OrderKey AlgoWrapperOrder::index_and_launch_child(ChildOrder &&child, Price p, diff --git a/src/lib/orderman/orders/algo/IAlgoOrder.h b/src/lib/orderman/orders/algo/IAlgoOrder.h index e73466444..b1a871034 100644 --- a/src/lib/orderman/orders/algo/IAlgoOrder.h +++ b/src/lib/orderman/orders/algo/IAlgoOrder.h @@ -17,6 +17,7 @@ struct ChildState { Quantity last_fills; Price last_price; ChildStatus status; + Quantity fills_at_attach_time; }; class Book; @@ -61,7 +62,9 @@ class IAlgoOrder { // Override this method to activate your algo using an existing // working order as a child. Attached child orders are // automatically subscribed to, so there is no need to subscribe to - // changes for the attached order. + // changes for the attached order. Note that any fills reported + // from this call through IAlgoServices::update_fill_state() will + // be included in the fills_at_attach_time count field. virtual void do_launch_attached(IAlgoServices &, OrderKey child, ChildState) ; // Implement this method to respond to updates to your algo. The diff --git a/src/lib/orderman/orders/algo/IAlgoServices.h b/src/lib/orderman/orders/algo/IAlgoServices.h index ef7e14845..e6e2acf3f 100644 --- a/src/lib/orderman/orders/algo/IAlgoServices.h +++ b/src/lib/orderman/orders/algo/IAlgoServices.h @@ -22,8 +22,8 @@ class IAlgoServices : public IBeforeLaunchAlgoServices { // 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. + // an algo is shutting down any requests to create, update or + // cancel child orders will be ignored. virtual bool is_shutting_down() const = 0; // -------------------------------------------------------------- @@ -40,7 +40,7 @@ class IAlgoServices : public IBeforeLaunchAlgoServices { virtual void subscribe_child_change(OrderKey) = 0; virtual void unsubscribe_child_change(OrderKey) = 0; - // Subscribe to a periodic timer + // Subscribe to a periodic 10ms timer virtual void subscribe_timer() = 0; virtual void unsubscribe_timer() = 0; @@ -63,7 +63,8 @@ class IAlgoServices : public IBeforeLaunchAlgoServices { // and quantity. By default launching a child results in an // automatic subscription to its state changes. Order creation is // relatively costly so in performance sensitive code it is better - // to create ChildTemplate before the algo is launched. + // to create a ChildTemplate before the algo is launched and use + // the launch_child(ChildTemplate, ...) call virtual OrderKey launch_child(api::orderman::OrderDefinition const &, Price, Quantity, bool subscribe = true) = 0; diff --git a/src/lib/orderman/orders/algo/algo_orders/CoverAlgo.cpp b/src/lib/orderman/orders/algo/algo_orders/CoverAlgo.cpp index 80f85d7d1..4e90fe2cc 100644 --- a/src/lib/orderman/orders/algo/algo_orders/CoverAlgo.cpp +++ b/src/lib/orderman/orders/algo/algo_orders/CoverAlgo.cpp @@ -87,6 +87,12 @@ void CoverAlgo::on_covered_change(ChildState const &st) { switch (me->status) { case Status::WORKING: case Status::CANCELING: + LOG(OrderMan, Diag, + "Cover received child update. Child fills: {} Pre-Attach child " + "fills: {}", + st.total_fills.value(), st.fills_at_attach_time.value()); + if (me->hedged_fills.value() == 0) + me->hedged_fills = st.fills_at_attach_time; if (st.total_fills.value() > me->hedged_fills.value()) { auto hedge_qty = Quantity(st.total_fills.value() - me->hedged_fills.value()); @@ -130,7 +136,8 @@ void CoverAlgo::on_covered_change(ChildState const &st) { terminate_if_hedges_complete(); break; case ChildStatus::CANCELED: - LOG(OrderMan, Info, "CoverAlgo {}. Child was unexpectadly canceled, terminating", + LOG(OrderMan, Info, + "CoverAlgo {}. Child was unexpectadly canceled, terminating", algo().order_key().value()); on_done(); algo().terminate_error("Unexpected child cancel"); @@ -283,13 +290,11 @@ IAlgoOrder::OnCancelResponse CoverAlgo::on_cancel() { return IAlgoOrder::OnCancelResponse::HONOR; } -void CoverAlgo::on_done() -{ +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) diff --git a/src/lib/orderman/orders/algo/algo_orders/DormanAlgo.cpp b/src/lib/orderman/orders/algo/algo_orders/DormanAlgo.cpp index ca4c9acee..eccc65f87 100644 --- a/src/lib/orderman/orders/algo/algo_orders/DormanAlgo.cpp +++ b/src/lib/orderman/orders/algo/algo_orders/DormanAlgo.cpp @@ -194,10 +194,12 @@ void DormanAlgo::on_book(SecurityKey key, const Book& book) { 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; } + else { + target = Quantity { std::get<1>(entry) }; + } } } target = Quantity { std::min(target.value(), me->params.steps.maximum) }; diff --git a/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.cpp b/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.cpp index bebd90879..3e244c699 100644 --- a/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.cpp +++ b/src/lib/orderman/orders/algo/algo_orders/ProRataAlgo.cpp @@ -42,8 +42,8 @@ struct ProRataAlgo::PImpl Price chase_diff; Quantity fill_limit; Quantity stay_in_threshold; - bool enable_scratch_on_overfill; - bool enable_stay_in; + bool scratch_on_overfill; + bool stay_in; ChildTemplate *child_template = nullptr; ChildTemplate *hedge_template = nullptr; @@ -162,7 +162,7 @@ void ProRataAlgo::check_overfill() { if(me->is_overfilled && me->status != Status::GETTING_OUT) { - if(me->enable_stay_in) + if(me->stay_in) { auto my_side = algo().definition().side(); auto other_side = securities::other_side(my_side); @@ -202,7 +202,7 @@ void ProRataAlgo::get_out() { me->status = Status::GETTING_OUT; algo().cancel_child(me->child_key); - if(me->enable_scratch_on_overfill) + if(me->scratch_on_overfill) { for(auto& h : me->overfill_hedges) scratch(h); @@ -369,8 +369,8 @@ void ProRataAlgo::set_parameters(IBeforeLaunchAlgoServices &serv, const api::ord return; } me->stay_in_threshold = SpreaderUtils::_quantity_threshold(prorata.stay_in_threshold(), algo().quantity()); - me->enable_scratch_on_overfill = prorata.enable_scratch_on_overfill(); - me->enable_stay_in = prorata.enable_stay_in(); + me->scratch_on_overfill = prorata.scratch_on_overfill(); + me->stay_in = prorata.stay_in(); } void ProRataAlgo::check_terminate() diff --git a/src/lib/orderman/orders/algo/algo_orders/TopAlgo.cpp b/src/lib/orderman/orders/algo/algo_orders/TopAlgo.cpp index acb3f3f29..adfa72a0f 100644 --- a/src/lib/orderman/orders/algo/algo_orders/TopAlgo.cpp +++ b/src/lib/orderman/orders/algo/algo_orders/TopAlgo.cpp @@ -67,6 +67,9 @@ void TopAlgo::do_launch_attached(IAlgoServices &algo, OrderKey k, me->algo = &algo; me->child_key = k; algo.subscribe_book(SecurityKey(algo.security().id())); + LOG(OrderMan, Diag, + "Top order attached to order {}. Child currently has {} fills", + k.value(), st.total_fills); on_child_change(k, st); } 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 index 556f43b21..5a5a91f4a 100644 --- a/src/lib/orderman/orders/algo/algo_orders/tests/dorman_tests.cpp +++ b/src/lib/orderman/orders/algo/algo_orders/tests/dorman_tests.cpp @@ -189,7 +189,6 @@ size_t count_status(const std::vector& child_orders, ChildStatus sta return match; } - TEST_CASE("dorman_order_size", "[algo][dorman]") { // create a table of various sizes // send market state to the algo @@ -211,13 +210,14 @@ TEST_CASE("dorman_order_size", "[algo][dorman]") { SECTION("initial size vs market") { std::vector test_sizes { - { 10, 250 }, - { 200, 250 }, - { 250, 250 }, - { 500, 400 }, - { 999, 400 }, - { 2500, 750 }, - { 9999, 800 } + { 10, 0 }, + { 200, 0 }, + { 250, 0 }, + { 500, 250 }, + { 999, 250 }, + { 2500, 600 }, + { 9999, 750 }, + { 20002, 800 }, }; for (auto& s : test_sizes) @@ -244,10 +244,10 @@ TEST_CASE("dorman_order_size", "[algo][dorman]") { state.update_book(book); REQUIRE(state.before_launch().ok()); state.launch(); - REQUIRE(state.algo().exposed_qty() == Quantity(400)); + REQUIRE(state.algo().exposed_qty() == Quantity(250)); // Check that orders were created - REQUIRE(400 == count_exposed(state.child_orders())); + REQUIRE(250 == count_exposed(state.child_orders())); for(const auto& ch : state.child_orders()) { REQUIRE(ch.cstate.quantity == state.quantity()); } @@ -255,9 +255,9 @@ TEST_CASE("dorman_order_size", "[algo][dorman]") { // Reduce market book.bid()[0] = { some_price, 405 }; state.update_book(book); - REQUIRE(state.algo().exposed_qty() == Quantity(250)); + REQUIRE(state.algo().exposed_qty() == Quantity(0)); REQUIRE( - 250 == count_exposed(state.child_orders())); + 0 == count_exposed(state.child_orders())); } SECTION("Increase qty w/ delay when market grows") { @@ -268,28 +268,28 @@ TEST_CASE("dorman_order_size", "[algo][dorman]") { state.update_book(book); REQUIRE(state.before_launch().ok()); state.launch(); - REQUIRE(state.algo().exposed_qty() == Quantity(400)); + REQUIRE(state.algo().exposed_qty() == Quantity(250)); // Check that orders were created REQUIRE( - 400 == count_exposed(state.child_orders())); + 250 == 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 + // so this is still 250 REQUIRE( - 400 == count_exposed(state.child_orders())); + 250 == count_exposed(state.child_orders())); - // Still 400 after the timer ticks but not enough time has passed + // Still 250 after the timer ticks but not enough time has passed state.advance_time(std::chrono::seconds()); REQUIRE( - 400 == count_exposed(state.child_orders())); + 250 == count_exposed(state.child_orders())); state.advance_time(std::chrono::seconds(input.steps.trail_seconds)); REQUIRE( - 600 == count_exposed(state.child_orders())); + 400 == count_exposed(state.child_orders())); } SECTION("Respect maximum") { @@ -336,7 +336,7 @@ TEST_CASE("Dorman fills", "[algo][dorman]") { state.update_book(book); REQUIRE(state.before_launch().ok()); state.launch(); - REQUIRE(state.algo().exposed_qty() == Quantity(450)); + REQUIRE(state.algo().exposed_qty() == Quantity(250)); for (const auto& order : state.child_orders()) { state.fill(order.key, price, order.cstate.quantity); @@ -360,7 +360,7 @@ TEST_CASE("Dorman fills", "[algo][dorman]") { state.update_book(book); REQUIRE(state.before_launch().ok()); state.launch(); - REQUIRE(state.algo().exposed_qty() == Quantity(650)); + REQUIRE(state.algo().exposed_qty() == Quantity(450)); REQUIRE(state.quantity() == Quantity {500}); // Fill 400 @@ -405,7 +405,7 @@ TEST_CASE("Dorman overfill", "[algo][dorman]") { state.update_book(book); REQUIRE(state.before_launch().ok()); state.launch(); - REQUIRE(state.algo().exposed_qty() == Quantity(650)); + REQUIRE(state.algo().exposed_qty() == Quantity(450)); REQUIRE(state.quantity() == Quantity {100}); REQUIRE(state.child_orders().size() == 1); @@ -467,7 +467,7 @@ TEST_CASE("Dorman profit-orders", "[algo][dorman]") { state.update_book(book); REQUIRE(state.before_launch().ok()); state.launch(); - REQUIRE(state.algo().exposed_qty() == Quantity(650)); + REQUIRE(state.algo().exposed_qty() == Quantity(450)); REQUIRE(state.quantity() == Quantity {100}); REQUIRE(state.child_orders().size() == 1); @@ -714,26 +714,26 @@ TEST_CASE("Dorman market move up then back down", "[algo][dorman]") { auto secid = SecurityKey(state.security().id()); Book book(secid); - book.bid()[0] = { some_price, 350 }; + book.bid()[0] = { some_price, 1350 }; state.update_book(book); REQUIRE(state.before_launch().ok()); state.launch(); - REQUIRE(state.algo().exposed_qty() == Quantity(250)); + REQUIRE(state.algo().exposed_qty() == Quantity(450)); 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 }; + // Update book sets future quantity = 650 + book.bid()[0] = { some_price, 2219 }; 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)); + REQUIRE(state.algo().exposed_qty() == Quantity(450)); - book.bid()[0] = { some_price, 425 }; + book.bid()[0] = { some_price, 1425 }; 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)); + REQUIRE(state.algo().exposed_qty() == Quantity(450)); } diff --git a/src/lib/orderman/orders/spreader_orders/tests/spreader_tests.cpp b/src/lib/orderman/orders/spreader_orders/tests/spreader_tests.cpp index 84d619544..a3e76866c 100644 --- a/src/lib/orderman/orders/spreader_orders/tests/spreader_tests.cpp +++ b/src/lib/orderman/orders/spreader_orders/tests/spreader_tests.cpp @@ -3065,7 +3065,7 @@ TEST_CASE ("spreader_perf_2legs", "[spreader]") } TEST_CASE ("spreader_perf_4legs", "[spreader]") { - bool recomp = gen_rand(0,10) > 5; + bool recomp = false;//gen_rand(0,10) > 5; bts::orderman::OrderKey order_key(0); Price spreader_price = Price (40); Quantity spreader_quantity = Quantity (250); diff --git a/src/lib/orderman/server/OrderServiceServer.cpp b/src/lib/orderman/server/OrderServiceServer.cpp index 381673dac..e16768468 100644 --- a/src/lib/orderman/server/OrderServiceServer.cpp +++ b/src/lib/orderman/server/OrderServiceServer.cpp @@ -241,11 +241,18 @@ OrderServiceServer::OrderServiceServer(bts::api::ServiceContext &ctx, me->service_bridge->update_admin_data(update.all()[0]); }); - // subscribe to position updates and foward them to the service bridge + // 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); + }); + + // Subscribe + me->position_service.subscribe_position_data() | + rxcpp::operators::subscribe( + [this](auto const &update) { + me->service_bridge->update_position(update); }); me->margin_service.subscribe_excess_liquidity_report() | diff --git a/src/lib/utils/AuthSessionManager.cpp b/src/lib/utils/AuthSessionManager.cpp index dddb415fd..8b1b74438 100644 --- a/src/lib/utils/AuthSessionManager.cpp +++ b/src/lib/utils/AuthSessionManager.cpp @@ -80,7 +80,7 @@ bool AuthSessionManager::validate_ip_port(const bts::config::KeyCode& kc) return portgood && ipgood; } -bool AuthSessionManager::add_or_replace_session(const api::user::TokenPayload& tok, std::string* msg) +bool AuthSessionManager::add_or_replace_session(const api::user::TokenPayload& tok, const admin::BTSUser& user, std::string* msg) { if(tok.wheel()) return true; @@ -105,18 +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()); + LOG(User, Warn, "KEYCODE: Replacing session {} user:{}:{} ", tok.id(), user.id(), user.name()); return true; } else if(_map.size() < _nusers) { _map[tok.id()] = Session{tok}; - LOG(User, Warn, "KEYCODE: Creating session {} ", tok.id()); + LOG(User, Warn, "KEYCODE: Creating session {} user:{}:{}", tok.id(), user.id(), user.name()); return true; } if(msg) *msg = "KEYCODES: Not Enough Logins available for session"; - LOG(User, Error, "KEYCODES: Not Enough Logins available for session {}", tok.id()); + LOG(User, Error, "KEYCODES: Not Enough Logins available for session {} user:{}:{}", tok.id(), user.id(), user.name()); return false; } diff --git a/src/lib/utils/AuthSessionManager.h b/src/lib/utils/AuthSessionManager.h index 08e9d03cd..fa68d097e 100644 --- a/src/lib/utils/AuthSessionManager.h +++ b/src/lib/utils/AuthSessionManager.h @@ -8,7 +8,7 @@ namespace bts { namespace config { class KeyCode; } namespace api { namespace auth { - + struct Session { api::user::TokenPayload token; @@ -18,8 +18,8 @@ class AuthSessionManager { public: AuthSessionManager(); - - bool add_or_replace_session(const api::user::TokenPayload& tok, std::string* msg = nullptr); + + bool add_or_replace_session(const api::user::TokenPayload& tok, const admin::BTSUser& user, std::string* msg = nullptr); bool check_session(const api::user::TokenPayload& tok); private: @@ -33,6 +33,6 @@ private: bool _concurrent; }; - - + + }}} \ No newline at end of file diff --git a/src/lib/utils/AuthToken.cpp b/src/lib/utils/AuthToken.cpp index 2423638a8..9c062c592 100644 --- a/src/lib/utils/AuthToken.cpp +++ b/src/lib/utils/AuthToken.cpp @@ -89,7 +89,7 @@ AuthToken generate_token(const admin::BTSUser& user, std::string* msg) payload.set_wheel(true); if(requires_auth_session(user)) - session_manager().add_or_replace_session(payload, msg); + session_manager().add_or_replace_session(payload, user, msg); auto token = encrypt(payload.SerializeAsString()); bts_assert_d(token.size() < 255-8); AuthToken result; diff --git a/src/lib/utils/PBUtils.cpp b/src/lib/utils/PBUtils.cpp index 237c1c021..908b84712 100755 --- a/src/lib/utils/PBUtils.cpp +++ b/src/lib/utils/PBUtils.cpp @@ -16,11 +16,17 @@ std::string to_string (google::protobuf::Message const &msg) return result; } -google::protobuf::Timestamp to_timestamp(datetime::PTime const &ptime) +google::protobuf::Timestamp to_timestamp(datetime::PTime const &t) { google::protobuf::Timestamp gts; - gts.set_seconds(boost::posix_time::to_time_t(ptime)); - gts.set_nanos(0); + + using namespace boost::posix_time; + using namespace boost::gregorian; + ptime timet_start(date(1970,1,1)); + time_duration diff = t - timet_start; + + gts.set_seconds(diff.total_seconds()); + gts.set_nanos(diff.total_nanoseconds() - (diff.total_seconds() * 1000 * 1000 * 1000)); return gts; } diff --git a/src/lib/utils/SecureLogon.cpp b/src/lib/utils/SecureLogon.cpp index 4dece1128..62a24e00f 100644 --- a/src/lib/utils/SecureLogon.cpp +++ b/src/lib/utils/SecureLogon.cpp @@ -52,11 +52,14 @@ std::tuple load_session_keys(const string& session) { // we try to make it easier to run cert by copying the cert secure // logon file to the right place if in dev - if (!is_directory(path) && config::get().type()=="dev") { + if (config::get().type()=="dev") { auto cert_file=os::sparkd_wd() + "/data_files/cert_secure_logon.txt" ; if (exists(cert_file)) { create_directories(path); - copy_file(cert_file, path+"/cert_secure_logon.txt"); + auto target = path+"cert_secure_logon.txt"; + if (exists(target)) + remove(target); + copy_file(cert_file,target); } } diff --git a/src/lib/utils/optional.h b/src/lib/utils/optional.h new file mode 100644 index 000000000..b5466b79d --- /dev/null +++ b/src/lib/utils/optional.h @@ -0,0 +1,56 @@ +#pragma once +// An incomplete version of std::optional which should work well enough on Windows until it catch up +#ifdef _WIN32 +#include +namespace std { + class bad_optional_access : exception { }; + + template + class optional { + bool _has_value; + union { + T _value; + int _unused; + }; + public: + optional(const T& value) : _has_value(true), _value(value) { } + optional(T&& value) : _has_value(true), _value(value) { } + optional() : _has_value(false) + ~optional() { reset(); } + bool has_value() const { return _has_value; } + operator bool() { return b.has_value(); } + T& operator *() { + if (has_value()) { + return _value; + } + else throw std::bad_optional_access(); + } + const T& operator*() const { return **this; } + T& operator->() { return **this; } + const T& operator->() const { return **this; } + bool operator==(const std::optional& other) const { + if (!_has_value && !other.has_value()) return true; + return _has_value == other._has_value && _value == other.value(); + } + bool operator!=(const std::optional& other) const { return !(*this == other); } + T& value() { return **this; } + const T& value() const { return **this; } + T value_or(T default_value) { + if (has_value()) return _value; + else return default_value; + } + void reset() { + if (has_value()) { + _value.~T(); + _has_value = false; + } + } + // This class is incomplete. Add methods as necessary + }; + + template + constexpr optional nullopt = {}; +} +#else +#include +#endif \ No newline at end of file diff --git a/src/lib/utils/parse_enum.h b/src/lib/utils/parse_enum.h new file mode 100644 index 000000000..3b8447bae --- /dev/null +++ b/src/lib/utils/parse_enum.h @@ -0,0 +1,27 @@ +#pragma once + +#include +#include "lib/utils/optional.h" +namespace bts { + + inline std::string uppercase_copy(const std::string& cs) { + std::string s = cs; + std::transform(s.begin(), s.end(), s.begin(), ::toupper); + return s; + } + + template + std::optional parse_enum(const std::string& value, const std::string& prefix, T2 parse) { + auto temp = value; + T result; + if (temp.find(prefix) == std::string::npos) { + temp = prefix + "_" + temp; + } + if(parse(temp, &result)) { + return {result}; + } + else { + return std::nullopt; + } + } +} \ No newline at end of file diff --git a/src/lib/utils/sftp.cpp b/src/lib/utils/sftp.cpp new file mode 100644 index 000000000..849ab97f3 --- /dev/null +++ b/src/lib/utils/sftp.cpp @@ -0,0 +1,168 @@ +#ifndef _WIN32 +#include "lib/utils/sftp.h" +#include "lib/utils/Dispatcher.h" +#include "lib/logging/logging.h" +#include "lib/utils/defer.h" +#include "lib/utils/base64.hpp" + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include +#include + + +namespace bts { +namespace utils { + +void begin_sftp_download( + const std::string host, + uint32_t port, + const std::string& user, + const std::string& password, + uint32_t /*time_out_secs*/, + const FilePicker& on_choose, + const std::function on_done) { + + utils::Dispatcher::background().post([=]() { + on_done(sftp_download(host, port, user, password, on_choose)); + }); +} + +DownloadResult sftp_download( + const std::string host, + uint32_t port, + const std::string& user, + const std::string& password, + const FilePicker& picker) { + + // resolve host + struct hostent* he; + he = gethostbyname((char*)host.c_str()); + if (he == 0) { + return (DownloadFailure {"Hostname lookup failure"}); + } + + auto addr_list = (in_addr**)he->h_addr_list; + char ip[100]; + strcpy(ip, inet_ntoa(*addr_list[0])); + + in_addr addr; + if (inet_aton(ip,&addr) == 0) { + return (DownloadFailure {"Invalid address"}); + } + + // connect over sftp + auto rc = libssh2_init(0); + if (rc != 0) { + return (DownloadFailure {fmt::format("Failed to initialize libssh2. rc {}", rc)}); + } + DEFER(libssh2_exit()); + auto sock = socket(AF_INET, SOCK_STREAM,0); + sockaddr_in sin; + sin.sin_family = AF_INET; + sin.sin_port = htons(port); + sin.sin_addr = addr; + rc = connect(sock, (sockaddr*)&sin, sizeof(sockaddr_in)); + if (rc != 0) { + return (DownloadFailure {fmt::format("Failed to connect to {}:{}. rc {}",host,port, rc)}); + } + DEFER(close(sock)); + + auto session = libssh2_session_init(); + if (!session) { + return (DownloadFailure {"Failed to init libssh2 session"}); + } + DEFER( + libssh2_session_disconnect(session, "Normal Shutdown"); + libssh2_session_free(session) + ); + + libssh2_session_set_blocking(session, true); + rc = libssh2_session_handshake(session, sock); + if (rc != 0) { + return (DownloadFailure {fmt::format("ssh2 handshake failed. rc {}", rc)}); + } + + std::string fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1); + fingerprint = websocketpp::base64_encode(fingerprint); + LOG(Gen,Info,"sftp session fingerprint for {}:{} = {}", host, port, fingerprint); + + // Authenticate + if (libssh2_userauth_password(session, user.c_str(), password.c_str())) { + return (DownloadFailure {fmt::format("Password authentiation failed for user {} password {}",user, password)}); + } + + auto sftp_session = libssh2_sftp_init(session); + if (!sftp_session) { + return (DownloadFailure {"Failed to create sftp session"}); + } + DEFER(libssh2_sftp_shutdown(sftp_session)); + + // get a directory listing + std::vector candidates; + { + auto sftp_dir_handle = libssh2_sftp_opendir(sftp_session, "."); + if (!sftp_dir_handle) { + return (DownloadFailure {"Failed to open sftp directory '.'"}); + } + DEFER(libssh2_sftp_closedir(sftp_dir_handle)); + + do { + char mem[1024]; + LIBSSH2_SFTP_ATTRIBUTES attrs; + rc = libssh2_sftp_readdir(sftp_dir_handle, mem, sizeof(mem), &attrs); + if (rc > 0) { + if (LIBSSH2_SFTP_S_ISREG(attrs.permissions)) { + candidates.push_back({mem,(size_t)rc}); + } + } + else break; + }while(rc > 0); } + + auto to_download = picker(candidates); + if (!to_download.has_value()) { + return DownloadFailure { "No file selected" }; + } + + auto sftp_handle = libssh2_sftp_open(sftp_session, to_download.value().c_str(), LIBSSH2_FXF_READ, 0); + if (!sftp_handle) { + return DownloadFailure { fmt::format("Unable to open file '{}' with sftp", to_download.value()) }; + } + DEFER(libssh2_sftp_close(sftp_handle)); + + // Get a temporary file + auto base_name = fmt::format("bts-sftp-%%%%-%%%%-%%%%-%%%%-{}",to_download.value()); + auto temp = boost::filesystem::temp_directory_path() / boost::filesystem::unique_path(base_name); + if (temp.empty()) { + return (DownloadFailure {"Failed to create temporary file"}); + } + + std::ofstream out { temp.string() }; + if (out.bad()) { + return DownloadFailure { fmt::format("Failed to open temp file '{}'", temp) }; + } + + do { + char buffer[1024]; + rc = libssh2_sftp_read(sftp_handle, buffer, sizeof(buffer)); + if (rc > 0) { + out.write(buffer, rc); + } + else break; + } while(true); + + return DownloadSuccess { to_download.value(),temp.string() }; +} + +}} +#endif diff --git a/src/lib/utils/sftp.h b/src/lib/utils/sftp.h new file mode 100644 index 000000000..478f1c43c --- /dev/null +++ b/src/lib/utils/sftp.h @@ -0,0 +1,39 @@ +#pragma once + +#include +#include +#include +#include + +namespace bts { +namespace utils { + +struct DownloadSuccess { + std::string const sftp_file_path; + std::string const local_file_path; +}; +struct DownloadFailure { + std::string const error_msg; +}; +using DownloadResult = std::variant; +using FilePicker = std::function(std::vector const&)>; + +void begin_sftp_download( + const std::string host, + uint32_t port, + const std::string& user, + const std::string& password, + uint32_t time_out_secs, + const FilePicker& picker, + std::function const); + +DownloadResult sftp_download( + const std::string host, + uint32_t port, + const std::string& user, + const std::string& password, + const FilePicker& on_choose); + + +}}//ns + diff --git a/src/sdk/make_sdk.sh b/src/sdk/make_sdk.sh index 53112d13e..945b69c2e 100755 --- a/src/sdk/make_sdk.sh +++ b/src/sdk/make_sdk.sh @@ -25,7 +25,6 @@ ln -sf $1/server $1/sdk/lib mkdir $1/sdk/examples cp $2/examples/* $1/sdk/examples -cp $2/doc/* $1/sdk/doc cp $2/sdk_shell.sh $1/sdk/sdk_shell.sh cp $2/update_github.sh $1/sdk/update_github.sh cp $2/README.md $1/sdk/README.md diff --git a/src/server/sparkd/CMakeDeps.cmake b/src/server/sparkd/CMakeDeps.cmake index d570ae6a8..adae3e3c8 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/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 +set(sources algo/AlgoDefinitionServiceServer.cpp;cmds/AdminReport.cpp;cmds/AutoCert.cpp;cmds/Benchmark.cpp;cmds/CmdRegistry.cpp;cmds/ConfigCheck.cpp;cmds/FIXSim.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/MultipartFormDecoder.cpp;http/WebServer.cpp;http/tests/MultiPartFormTests.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/PositionMap.cpp;position/PositionServicePNLHelper.cpp;position/PositionServiceServer.cpp;position/import/BasePositionServiceServer.cpp;position/import/DownloadCommonStateMachine.cpp;position/import/DownloadWindow.cpp;position/import/MoneyFileServiceServer.cpp;position/import/parsing/gmi_parser.cpp;position/import/parsing/money_parser.cpp;position/import/parsing/parser_common.cpp;position/tests/PositionTests.cpp;position/tests/position_tests_internal.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/FIXSim.cpp b/src/server/sparkd/cmds/FIXSim.cpp new file mode 100644 index 000000000..413719a14 --- /dev/null +++ b/src/server/sparkd/cmds/FIXSim.cpp @@ -0,0 +1,18 @@ +#include "server/sparkd/cmds/FIXSim.h" +#include "server/sparkd/exchange_sim/ExchangeSim.h" + +namespace bts { +namespace cmd { + +int FIXSim::execute(std::vector const &) { + bts::sim::start_exchange_sim(); + return 0; +} + + +std::string FIXSim::help() { + return "FIXSim - Starts a standalone FIX simulator "; +} + +} // namespace cmd +} // namespace bts diff --git a/src/server/sparkd/cmds/FIXSim.h b/src/server/sparkd/cmds/FIXSim.h new file mode 100644 index 000000000..3d3cbf985 --- /dev/null +++ b/src/server/sparkd/cmds/FIXSim.h @@ -0,0 +1,17 @@ +#pragma once + +#include "ICmd.h" + +namespace bts { +namespace cmd { + +class FIXSim : public ICmd +{ +public: + int execute(std::vector const &args); + std::string help (); +}; + +}} + + diff --git a/src/server/sparkd/cmds/QuickTest.cpp b/src/server/sparkd/cmds/QuickTest.cpp index f9765553c..53aefdadc 100644 --- a/src/server/sparkd/cmds/QuickTest.cpp +++ b/src/server/sparkd/cmds/QuickTest.cpp @@ -1,14 +1,37 @@ #include "QuickTest.h" #include "ICmd.h" +#include "lib/config/config.h" +#include "lib/config/protos/orders_fills_cleanup.pb.h" +#include "lib/orderman/orders/algo/IAlgoServices.h" #include "lib/tracing/PerfLog.h" +#include "lib/utils/datetime.h" +#include "lib/domain_model/sod.h" +#include "lib/utils/datetime.h" +#include +#include +#include #include +#include namespace bts { namespace cmd { +using namespace bts::orderman; int QuickTest::execute(std::vector const &) { - return 0; + + auto sftp_file_name = std::string("2019-08-05.adatmny.csv"); + std::regex rx("(\\d\\d\\d\\d)"); + std::smatch matches; + if (!std::regex_match(sftp_file_name, matches, rx)) + return {}; + auto y = std::stoi(matches[0]); + auto m = std::stoi(matches[1]); + auto d = std::stoi(matches[2]); + + const auto &sod = config::get(); + std::cout << datetime::make_ptime(y, m, d, sod.sod_hh(), sod.has_sod_mm(), 0, 0) << "\n"; + return 1; } std::string QuickTest::help() { diff --git a/src/server/sparkd/cmds/Run.cpp b/src/server/sparkd/cmds/Run.cpp index c75503975..1bbcbfa49 100644 --- a/src/server/sparkd/cmds/Run.cpp +++ b/src/server/sparkd/cmds/Run.cpp @@ -11,11 +11,11 @@ #include "lib/utils/Dispatcher.h" #include "lib/config/protos/toggles.pb.h" +#include "lib/md/MBOProcessor.h" +#include "lib/md/MBOServiceServer.h" #include "lib/md/MDClient.h" #include "lib/md/SyntheticSecurityService.h" #include "lib/md/relay/QueryServer.h" -#include "lib/md/MBOProcessor.h" -#include "lib/md/MBOServiceServer.h" #include "lib/metrics/metrics.h" #include "lib/metrics/system_metrics.h" #include "lib/orderman/order_factory/DynamicOrderRegistry.h" @@ -25,19 +25,21 @@ #include "lib/plugin/plugin.h" #include "lib/tracing/PerfLog.h" #include "lib/utils/thread_manager.h" +#include "server/sparkd/algo/AlgoDefinitionServiceServer.h" #include "server/sparkd/http/WebServer.h" #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/position/PositionServiceServer.h" +#include "server/sparkd/position/import/BasePositionServiceServer.h" +#include "server/sparkd/position/import/MoneyFileServiceServer.h" #include "server/sparkd/support/ErrorReportService.h" #include "server/sparkd/support/ShellLatencyServer.h" #include "server/sparkd/user/AdminServiceServer.h" #include "server/sparkd/user/AuthServiceServer.h" -#include "server/sparkd/algo/AlgoDefinitionServiceServer.h" -#include "lib/orderman/orders/algo/algo_orders/ScratchAlgo.h" #include "lib/orderman/orders/algo/algo_orders/ProRataAlgo.h" +#include "lib/orderman/orders/algo/algo_orders/ScratchAlgo.h" #include @@ -53,7 +55,8 @@ int Run::execute(std::vector const &) { config::QuickbusNodeCfg qb_cfg; auto &ports = config::get(); qb_cfg.set_my_port(ports.qbus_port()); - qbus::Quickbus quickbus(qb_cfg, qbus::Quickbus::ThreadOptions::deferred_start); + qbus::Quickbus quickbus(qb_cfg, + qbus::Quickbus::ThreadOptions::deferred_start); api::ServiceContext sc(&quickbus); trace::PerfLog perf_log; @@ -65,33 +68,37 @@ int Run::execute(std::vector const &) { user::AuthServiceServer login_server(sc); md::MBOProcessor mbo; - auto* mbowriter = config::get().enabled() ? &mbo.writer() : nullptr; + auto *mbowriter = config::get().enabled() + ? &mbo.writer() + : nullptr; md::run_market_datapublishers(mbowriter); api::synthetic::SyntheticSecurityService synthetic_service(sc); - md::MDClient mdclient(&utils::Dispatcher::background(), &synthetic_service, &mbo.reader()); + md::MDClient mdclient(&utils::Dispatcher::background(), &synthetic_service, + &mbo.reader()); md::QueryServerPtr md_query_server( md::run_query_server(sc, mdclient, stats)); - md::MBOServiceServer mboserver(sc, utils::Dispatcher::background(), mdclient); + md::MBOServiceServer mboserver(sc, utils::Dispatcher::background(), + mdclient); // initialize order plugins orderman::DynamicOrderRegistry dyn_order_registry; SparkServices plugin_services(dyn_order_registry); plugin::init_plugins(plugin_services); // finished initializing order plugins - + orderman::OrderServiceServer order_service_server( sc, mdclient, dyn_order_registry, stats, perf_log); support::ErrorReportServer error_report_service(sc); admin::AdminServiceServer admin_service(sc); 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,mdclient); + bts::api::orderman::algo::AlgoDefinitionServiceServer algo_definition_server( + sc); + position::BasePositionServiceServer base_pos_server(sc); + position::MoneyFileServiceServer money_file_server(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/exchange_sim/ExchangeSim.h b/src/server/sparkd/exchange_sim/ExchangeSim.h index 2d65cc9e6..7ae259e84 100644 --- a/src/server/sparkd/exchange_sim/ExchangeSim.h +++ b/src/server/sparkd/exchange_sim/ExchangeSim.h @@ -7,6 +7,6 @@ namespace bts { namespace md { class MBOWriter; } namespace sim { -int start_exchange_sim(bts::api::Venue, md::MBOWriter*); +int start_exchange_sim(bts::api::Venue, md::MBOWriter* = nullptr); }} diff --git a/src/server/sparkd/http/ApiGateway.cpp b/src/server/sparkd/http/ApiGateway.cpp index a242fe3c1..42e88cd32 100644 --- a/src/server/sparkd/http/ApiGateway.cpp +++ b/src/server/sparkd/http/ApiGateway.cpp @@ -16,13 +16,19 @@ using namespace bts::api::gateway; namespace bts { namespace http { +struct SubscriptionContext { + int id; + Responder send; + api::TableSubscription table; +}; + struct ApiGatewayImpl { std::mutex _m; const api::ServiceContext& _sc; api::user::AuthService _logon; std::unordered_map _apiclient; - std::vector _subscriptions; + std::map _subscriptions; ApiGatewayImpl(const api::ServiceContext& ctx) : @@ -66,23 +72,30 @@ struct ApiGatewayImpl { r(response); } - void handle_call(const GatewayCallHeader& header, const std::string& body, const Responder& r) { - std::lock_guard lock(_m); - if (header.service_type().empty()) { - error(r, "Invalid service name"); - return; - } - auto key = header.service_type() + header.service_qualifier(); - // create the api_client if needed + // Create or return the client proxy + api::ApiClient* get_api_client(const std::string& service_type, const std::string& qualifier, bool create=true) { + auto key = service_type + qualifier; auto iter = _apiclient.find(key); - api::ApiClient* pclient = nullptr; if (iter == _apiclient.end()) { - pclient = api::api_client_create(&_sc.bus(), header.service_type().c_str(), header.service_qualifier().c_str()); - _apiclient.insert(std::make_pair(key, pclient)); + if (create) { + auto* pclient = api::api_client_create(&_sc.bus(), service_type.c_str(), qualifier.c_str()); + _apiclient.insert(std::make_pair(key, pclient)); + return pclient; + } + else return nullptr; } else { - pclient = iter->second; + return iter->second; } + } + + void handle_call(const GatewayCallHeader& header, const std::string& body, const Responder& reply) { + std::lock_guard lock(_m); + if (header.service_type().empty()) { + error(reply, "Invalid service name"); + return; + } + api::ApiClient* pclient = get_api_client(header.service_type(), header.service_qualifier()); // Form the api request buffer api::RequestHeader api_header; api_header.set_method(header.method()); @@ -96,7 +109,7 @@ struct ApiGatewayImpl { api_header.AppendToString(&buffer); buffer += body; // call send - Responder* presponder = new Responder(move(r)); + Responder* presponder = new Responder(move(reply)); api_client_send(pclient, buffer.c_str(), buffer.size(), [](const char* buffer, size_t length, void* extra) { // buffer = [size][ResponseHeader][Payload]. GatewayResponse response; @@ -111,8 +124,65 @@ struct ApiGatewayImpl { }, presponder); } - // void handle_subscribe() - // void handle_unsubscribe() + void handle_subscribe(const GatewaySubscribe& request, const Responder& reply) { + std::lock_guard lock(_m); + api::ApiClient* pclient = get_api_client(request.service_type(), request.service_qualifier()); + if (pclient == nullptr) { + error(reply, "Invalid service"); + return; + } + // call api_client_subscribe() store the TableSubscription and the id and the reply channel + // When a tableupate fires attach the id to the reply and send it over the channel + // Responder* preply = new Responder(move(reply)); + auto id = request.request_id(); + SubscriptionContext* pcontext = new SubscriptionContext { id, move(reply), nullptr }; + + auto table = api_client_subscribe(pclient, request.method_name().c_str(), request.stream_qualifier().c_str(), + [](api::EventType type, const api::StreamUpdate* changes, int count, void* extra) { + SubscriptionContext* pcontext = (SubscriptionContext*)extra; + GatewayResponse update; + update.mutable_tick()->set_id(pcontext->id); + if (type == api::EventType::TABLE_UPDATED) { + for (int x=0; x < count; ++x) { + const api::StreamUpdate& current = changes[x]; + auto* row = update.mutable_tick()->add_updates(); + row->set_update_type((StreamGWUpdate::UpdateType)current.type); + row->set_key(current.key); + if (current.type == api::StreamUpdate::CREATE || current.type == api::StreamUpdate::REPLACE) { + row->set_buffer(current.buffer, current.buffer_len); + } + } + } + else { + update.mutable_tick()->add_updates()->set_update_type(StreamGWUpdate::RESET); + } + pcontext->send(update); + }, pcontext); + pcontext->table = table; + + _subscriptions[id] = pcontext; + // reply with empty response so we don't time out + reply({}); + } + void handle_unsubscribe(const GatewayUnsubscribe& request, const Responder& reply) { + auto id = request.subscription_id(); + api::ApiClient* pclient = get_api_client(request.service_type(), request.service_qualifier(), false); + bts_assert_d(pclient); + auto iter = _subscriptions.find(id); + if (iter != _subscriptions.end()) { + SubscriptionContext* pcontext = iter->second; + _subscriptions.erase(iter); + if(pcontext && pclient) { + api_client_unsubscribe(pclient, pcontext->table); + } + // Note: not safe to delete context yet + api::api_post(_sc.bus(), [pcontext]() { + delete pcontext; + }, 5000); + } + // empty response makes quickbus happy + reply({}); + } }; @@ -125,7 +195,6 @@ ApiGatewaySession::~ApiGatewaySession(){} void ApiGatewaySession::handle(const api::gateway::GatewayRequest& request, const Responder& reply) { - GatewayResponse response; LOG(Api,Diag, "Gateway Request: {}", api::toJSON(request)); switch(request.request_case()) { case GatewayRequest::kLogin: @@ -135,8 +204,14 @@ void ApiGatewaySession::handle(const api::gateway::GatewayRequest& request, cons _me->handle_call(request.call(), request.addendum(), reply); break; case GatewayRequest::kSubscribe: + _me->handle_subscribe(request.subscribe(), reply); + break; case GatewayRequest::kUnsubscribe: - case GatewayRequest::REQUEST_NOT_SET: + _me->handle_unsubscribe(request.unsubscribe(), reply); + break; + case GatewayRequest::REQUEST_NOT_SET: { + _me->error(reply, "Request type not set"); + } break; } } diff --git a/src/server/sparkd/http/MultipartFormDecoder.cpp b/src/server/sparkd/http/MultipartFormDecoder.cpp new file mode 100644 index 000000000..8914df4e4 --- /dev/null +++ b/src/server/sparkd/http/MultipartFormDecoder.cpp @@ -0,0 +1,71 @@ +#include "MultipartFormDecoder.h" +#include "lib/logging/logging.h" +#include +#include + +namespace bts { +namespace http { + +using namespace std; + +vector split(const string& contents, const string& deliniator) { + vector result; + boost::iter_split(result, contents, boost::first_finder(deliniator)); + return result; +} + +std::tuple> decode_header(const std::string& header) { + auto tokens = split(header, ";"); + std::string header_name = *tokens.begin(); + map props; + for (const auto& token : tokens) { + auto equals = token.find('='); + if (equals != std::string::npos) { + auto key = token.substr(1,equals-1); + auto value = token.substr(equals+1); + boost::algorithm::trim_if(value, + [](char c) { return c == ' ' || c == '"' || c == '\r' || c == '\n'; }); + props[key] = value; + } + } + return { header_name, props }; +} + +std::optional decode_form_data(const string content_type, const string& body) { + auto [_,props] = decode_header(content_type); + (void)_; + auto boundary = props["boundary"]; + if (boundary.empty()) { + LOG(Http,Warn,"Failed to decode boundary in content type '{}'", content_type); + return std::nullopt; + } + + MultiPartFormData result; + auto parts = split(body, "--" + boundary); + for (const auto& part : parts) { + auto end_headers = part.find("\r\n\r\n"); + if (end_headers == std::string::npos) continue; + if (part == "--\r\n") continue; + auto headers = split(part.substr(0,end_headers), "\r\n"); + + FormPart form; + form.content_type = part.substr(end_headers+2); + for (const auto& header : headers) { + const char* content_type = "Content-Type: "; + if (boost::starts_with(header, content_type)) { + form.content_type = header.substr(sizeof(content_type)); + } + else { + auto [_, props] = decode_header(header); (void)_; + if (!props["filename"].empty()) { + form.filename = props["filename"]; + } + } + } + result.parts.push_back(form); + } + + return {result}; +} + +}} //namespace diff --git a/src/server/sparkd/http/MultipartFormDecoder.h b/src/server/sparkd/http/MultipartFormDecoder.h new file mode 100644 index 000000000..4b85ff557 --- /dev/null +++ b/src/server/sparkd/http/MultipartFormDecoder.h @@ -0,0 +1,22 @@ +#pragma once +#include +#include +#include +#include + +namespace bts { +namespace http { + +struct FormPart { + std::string content_type; + std::string filename; + std::string contents; +}; +struct MultiPartFormData { + std::vector parts; +}; + +std::tuple> decode_header(const std::string& header); +std::optional decode_form_data(const std::string content_type, const std::string& body); + +}} //namespace diff --git a/src/server/sparkd/http/WebServer.cpp b/src/server/sparkd/http/WebServer.cpp index e2c34cd17..11a6cea27 100644 --- a/src/server/sparkd/http/WebServer.cpp +++ b/src/server/sparkd/http/WebServer.cpp @@ -1,5 +1,6 @@ #include "WebServer.h" #include "ApiGateway.h" +#include "MultipartFormDecoder.h" #include "lib/utils/Dispatcher.h" #include "lib/api/core/ApiClientInterop.h" #include "lib/api/apigateway.pb.h" @@ -13,6 +14,7 @@ #include #include +#include #include #include @@ -95,7 +97,7 @@ namespace http { void on_open(connection_hdl hdl) { auto con = _server.get_con_from_hdl(hdl); if (con) { - LOG(Api,Info, "Web client connection from {}", con->get_remote_endpoint()); + LOG(Http,Info, "Web client connection from {}", con->get_remote_endpoint()); std::lock_guard lock(_m); _session[hdl] = ApiGatewaySession(_ctx); } @@ -104,7 +106,7 @@ namespace http { void on_close(connection_hdl hdl) { auto con = _server.get_con_from_hdl(hdl); if (con) { - LOG(Api,Info, "Web client disconnected: {}", con->get_remote_endpoint()); + LOG(Http,Info, "Web client disconnected: {}", con->get_remote_endpoint()); std::lock_guard lock(_m); _session.erase(hdl); } @@ -120,12 +122,12 @@ namespace http { websocketpp::lib::error_code ec; _server.send(hdl, response.SerializeAsString(), websocketpp::frame::opcode::binary, ec); if (ec) { - LOG(Api,Warn, "Error sending from websocket: '{}'", ec.message()); + LOG(Http,Warn, "Error sending from websocket: '{}'", ec.message()); } }); } else { - LOG(Api,Info, "Could not parse request as GatewayRequest: {}", msg->get_payload()); + LOG(Http,Info, "Could not parse request as GatewayRequest: {}", msg->get_payload()); } } } @@ -174,6 +176,40 @@ namespace http { _mtimer->observe((utils::TimeStamp::now() - start).microseconds() / 1000.0); } + void bad_request(connection_ptr& con, const std::string& message) { + con->set_body(message); + con->set_status(websocketpp::http::status_code::bad_request); + } + + std::vector split_string(std::string body, std::string& deliniator) { + std::vector result; + size_t i = body.find(deliniator); + while(i != std::string::npos) { + result.push_back(body.substr(0,i)); + body = body.substr(i + deliniator.size()); + i = body.find(deliniator); + } + return result; + } + + void handle_file_upload(connection_ptr& con) { + auto content_type = con->get_request_header("Content-Type"); + auto data = decode_form_data(content_type, con->get_request_body()); + if (data) { + for(const auto& part : data.value().parts) { + if (!part.filename.empty()) { + LOG(Http,Info,"Received file upload: {}", part.filename); + } + } + } + else { + LOG(Http,Warn, "Failed to decode form data. Ensure you are using 'enctype=\"multipart/form-data\"'"); + } + + con->set_body("got it"); + con->set_status(websocketpp::http::status_code::ok); + } + bool require_authentication(connection_ptr& con, const std::string& username, const std::string& password) { auto auth = con->get_request_header("Authorization"); if (!auth.empty() && auth.size() >= 7 && boost::iequals(auth.substr(0, 6), "Basic ")) { @@ -303,7 +339,7 @@ namespace http { void on_http(connection_hdl hdl) { auto con = _server.get_con_from_hdl(hdl); auto resource = strip_query_str(con->get_resource()); - LOG(Api,Diag, "HTTP GET: {}", resource); + LOG(Http,Diag, "HTTP GET: {}", resource); if (boost::iequals(resource, "/config/.all")) { return_dotall(con); } @@ -334,6 +370,9 @@ namespace http { std::string body = "User-agent: *\nDisallow: /"; con->set_body(body); } + else if (boost::iequals(resource, "/upload")) { + handle_file_upload(con); + } else { auto path = map_path_to_filesystem(resource); if (boost::filesystem::is_directory(path)) { diff --git a/src/server/sparkd/http/tests/MultiPartFormTests.cpp b/src/server/sparkd/http/tests/MultiPartFormTests.cpp new file mode 100644 index 000000000..784241280 --- /dev/null +++ b/src/server/sparkd/http/tests/MultiPartFormTests.cpp @@ -0,0 +1,45 @@ +#include "catch/catch.hpp" +#include "../MultipartFormDecoder.h" + +#include + +namespace bts { +namespace http { +namespace test { + +TEST_CASE("Form header decoder", "[http]") { + auto header1 = decode_header("multipart/form-data; boundary=---------------------------123631036720284"); + REQUIRE(std::get(header1) == "multipart/form-data"); + REQUIRE(std::get<1>(header1)["boundary"] == "---------------------------123631036720284"); + auto header2 = decode_header( "Content-Disposition: form-data; name=\"file\"; filename=\"file_a.txt\"\n"); + REQUIRE(std::get(header2) == "Content-Disposition: form-data"); + REQUIRE(std::get<1>(header2)["name"] == "file"); + REQUIRE(std::get<1>(header2)["filename"] == "file_a.txt"); +} + +TEST_CASE("MultiPart form decoder", "[http]") { + std::string header = "multipart/form-data; boundary=---------------------------123631036720284"; + std::string body = + "-----------------------------123631036720284\r\n" + "Content-Disposition: form-data; name=\"file\"; filename=\"file_a.txt\"\r\n" + "Content-Type: text/plain\r\n" + "\r\n" + "\r\n" + " a\r\n" + " aaa\r\n" + " aaaaa\r\n" + "aaaaaaaa\r\n" + "\r\n" + "\r\n" + "-----------------------------123631036720284--\r\n"; + + auto maybe_data = decode_form_data(header, body); + REQUIRE((bool)maybe_data); + const auto& data = maybe_data.value(); + REQUIRE(data.parts.size() == 1); + auto& part = data.parts[0]; + REQUIRE(part.filename == "file_a.txt"); + +} + +}}} // ns \ No newline at end of file diff --git a/src/server/sparkd/main.cpp b/src/server/sparkd/main.cpp index 6796c3a01..4e50a071c 100644 --- a/src/server/sparkd/main.cpp +++ b/src/server/sparkd/main.cpp @@ -4,11 +4,13 @@ // to add a command add the header here and the the REGISTER call below // don't touch this file except to add commands. +#include "server/sparkd/cmds/AdminReport.h" #include "server/sparkd/cmds/AutoCert.h" #include "server/sparkd/cmds/Benchmark.h" #include "server/sparkd/cmds/ConfigCheck.h" -#include "server/sparkd/cmds/KeyCode.h" +#include "server/sparkd/cmds/FIXSim.h" #include "server/sparkd/cmds/Help.h" +#include "server/sparkd/cmds/KeyCode.h" #include "server/sparkd/cmds/Md.h" #include "server/sparkd/cmds/PrintHedgeOrders.h" #include "server/sparkd/cmds/QuickTest.h" @@ -17,7 +19,6 @@ #include "server/sparkd/cmds/Span.h" #include "server/sparkd/cmds/Test.h" #include "server/sparkd/cmds/TestCeloxica.h" -#include "server/sparkd/cmds/AdminReport.h" #include "server/sparkd/cmds/Web.h" using namespace bts; @@ -37,6 +38,7 @@ int main(int argc, char **argv) { REGISTER(Benchmark); REGISTER(QuickTest); REGISTER(AutoCert); + REGISTER(FIXSim); REGISTER(Span); REGISTER(Web); REGISTER(PrintHedgeOrders); diff --git a/src/server/sparkd/md/platforms/ice_onixs/ICEMain.cpp b/src/server/sparkd/md/platforms/ice_onixs/ICEMain.cpp index 035e05352..c9cb6a79b 100644 --- a/src/server/sparkd/md/platforms/ice_onixs/ICEMain.cpp +++ b/src/server/sparkd/md/platforms/ice_onixs/ICEMain.cpp @@ -1,6 +1,7 @@ #include "ICEMain.h" #include "lib/md/ice/ICEUtils.h" +#include "lib/md/vendor/OnixsUtils.h" #include "lib/config/config.h" #include "lib/md/coconut2.h" #include "lib/md/SecurityDB.h" @@ -24,16 +25,16 @@ void recursive_copy(const boost::filesystem::path &src, const boost::filesystem: { if (boost::filesystem::exists(dst)) throw std::runtime_error(dst.generic_string() + " exists"); - if (boost::filesystem::is_directory(src)) + if (boost::filesystem::is_directory(src)) { boost::filesystem::create_directories(dst); - for (boost::filesystem::directory_entry& item : boost::filesystem::directory_iterator(src)) - recursive_copy(item.path(), dst/item.path().filename()); - } - else if (boost::filesystem::is_regular_file(src)) - boost::filesystem::copy(src, dst); - else - throw std::runtime_error(dst.generic_string() + " not dir or file"); + for (boost::filesystem::directory_entry& item : boost::filesystem::directory_iterator(src)) + recursive_copy(item.path(), dst/item.path().filename()); + } + else if (boost::filesystem::is_regular_file(src)) + boost::filesystem::copy(src, dst); + else + throw std::runtime_error(dst.generic_string() + " not dir or file"); } using namespace std; @@ -55,10 +56,10 @@ ICEMain::ICEMain() : ICEMain::~ICEMain() {} -const ICEOptions::ICEChannel channel_settings_for(const ICEChannel& channel) +const ICEOptions::ICEChannel channel_settings_for(const ICEChannel& channel) { const auto& config = config::get(); - for (const auto& ch : config.channel()) + for (const auto& ch : config.channel()) { if(ch.name() == channel.name) return ch; @@ -67,7 +68,7 @@ const ICEOptions::ICEChannel channel_settings_for(const ICEChannel& channel) } -onixs_ice::MarketSubscriptions market_subcription_for_channel(const ICEChannel& chan) +onixs_ice::MarketSubscriptions market_subcription_for_channel(const ICEChannel& chan) { onixs_ice::MarketSubscriptions subs; for(const auto& x : chan.market_types) @@ -101,42 +102,36 @@ onixs_ice::MarketSubscriptions market_subcription_for_channel(const ICEChannel& } -void ICEMain::start(std::vector& symbols) +void ICEMain::start(std::vector& symbols) { - // License path and config file - auto license_location = os::bts_data_path() + "/licenses/"; - if (!boost::filesystem::is_directory(license_location)) - { - auto license_path =os::sparkd_wd() + "/data_files/Onixs.BTS.lic" ; - boost::filesystem::create_directories(license_location); - boost::filesystem::copy_file(license_path,license_location+"/Onixs.BTS.lic"); - } auto ice_conf_location = os::bts_data_path() + "/onixs_ice/"; - if (!boost::filesystem::is_directory(ice_conf_location)) + if (!boost::filesystem::is_directory(ice_conf_location)) { auto conf_path = os::sparkd_wd() + "/data_files/onixs_ice/" ; recursive_copy(boost::filesystem::path(conf_path), boost::filesystem::path(ice_conf_location)); } - + await_ice_config(); + auto license = onixs_create_temporary_license(); + const auto& channels = parse_ice_channels(symbols); - for (const auto& channel : channels) + for (const auto& channel : channels) { - unique_ptr phandler = prepare_handler(channel); + unique_ptr phandler = prepare_handler(channel, license.path.string()); _listeners.push_back(prepare_listener(*phandler, channel)); - _handlers.push_back(std::make_pair(channel, move(phandler))); - - std::stringstream symstr; - for (const auto& sym : channel.symbols) - symstr << sym.symbol() << "-" << SecurityType_Name(sym.type()) << " "; + _handlers.push_back(std::make_pair(channel, move(phandler))); + + std::stringstream symstr; + for (const auto& sym : channel.symbols) + symstr << sym.symbol() << "-" << SecurityType_Name(sym.type()) << " "; LOG(md,Info, "ICE: Initialized channel {} ({})", channel.to_string(), symstr.str()); } - for (auto& pair : _handlers) - { + for (auto& pair : _handlers) + { LOG(md,Info, "ICE: Init Handler {}", pair.first.to_string()); - sleep_for(16s); - onixs_ice::MarketSubscriptions subs = market_subcription_for_channel(pair.first); + sleep_for(16s); + onixs_ice::MarketSubscriptions subs = market_subcription_for_channel(pair.first); try { pair.second->start(subs); @@ -148,68 +143,64 @@ void ICEMain::start(std::vector& symbols) } } + onixs_destroy_temporary_license(license); LOG(md,Info, "** ICE Publisher started **"); } void ICEMain::send_heartbeats() { - for (const auto& listener: _listeners) + for (const auto& listener: _listeners) listener->heartbeat(); } // // // Initialize HandlerSettings and allocate the channel Handler -unique_ptr ICEMain::prepare_handler(const ICEChannel& channel) +unique_ptr ICEMain::prepare_handler(const ICEChannel& channel, const std::string& license) { - const auto& ice_options = config::get(); - auto license = ice_options.onixs_license(); - if (license == "{default}") - license = os::bts_data_path() + "/licenses/"; - const auto& options = channel_settings_for(channel); LOG(md,Info,"Loaded channel options for channel {}:\n{}", channel.to_string(), options.DebugString()); - onixs_ice::HandlerSettings settings; - settings.licenseDirectory = license; + onixs_ice::HandlerSettings settings; + settings.licenseDirectory = license; // // Logging settings.logLevel = (onixs_ice::LogLevels::Enum) options.onixs_log_mode(); settings.logDirectory = ice_log_path(); - + // - // Config file + // Config file settings.connectivityConfiguration = ice_config_path(); - // + // // Network settings const auto& pub_options = config::get(); - if (pub_options.has_network_interface()) + if (pub_options.has_network_interface()) settings.networkInterface = pub_options.network_interface(); - else + else { std::string inter; - if (pub_options.has_network_interface_a() && !pub_options.has_network_interface_b()) + if (pub_options.has_network_interface_a() && !pub_options.has_network_interface_b()) inter = pub_options.network_interface_a(); - if (!pub_options.has_network_interface_a() && pub_options.has_network_interface_b()) + if (!pub_options.has_network_interface_a() && pub_options.has_network_interface_b()) inter = pub_options.network_interface_b(); - if (pub_options.has_network_interface_a() && pub_options.has_network_interface_b()) + if (pub_options.has_network_interface_a() && pub_options.has_network_interface_b()) inter = pub_options.network_interface_b() + ":" + pub_options.network_interface_b(); settings.networkInterface = inter; - } + } settings.udpBufferSize = options.udp_buffer_size(); settings.heartbeatInterval = options.heartbeat_interval(); settings.sequenceGapMargin = options.sequence_gap_margin(); settings.gapTimeoutInMilliseconds = options.gap_timeout_in_ms(); - + // // Snapshot recovery options settings.snapshotRecoveryOptions.snapshotRecoveryTimeoutInMilliseconds = options.snapshot_recovery_timeout_in_ms(); settings.snapshotRecoveryOptions.maximumNumberOfSnaphotRecoveryAttempts = options.maximum_number_of_snapshot_recovery_attempts(); settings.snapshotRecoveryOptions.maximumSizeOfSnapshotMessageQueue = options.maximum_size_of_snapshot_message_queue(); - settings.snapshotRecoveryOptions.maximumSizeOfLiveMessageQueue = options.maximum_size_of_live_message_queue(); + settings.snapshotRecoveryOptions.maximumSizeOfLiveMessageQueue = options.maximum_size_of_live_message_queue(); // - // Misc settings + // Misc settings if(options.processing_thread_affinity() != "" ) settings.processingThreadAffinity = onixs_ice::ThreadAffinity::deserialize(options.processing_thread_affinity()); // if(options.has_track_top_of_the_book_only() @@ -220,10 +211,10 @@ unique_ptr ICEMain::prepare_handler(const ICEChannel& channe settings.maxMulticastMessageBlockSize = options.max_multicast_message_block_size(); if(options.product_definition_cache_directory() != "" ) settings.productDefinitionCacheDirectory = options.product_definition_cache_directory(); - + // // return feed handler - try + try { auto handler = unique_ptr(new onixs_ice::Handler(settings)); return handler; @@ -237,18 +228,18 @@ unique_ptr ICEMain::prepare_handler(const ICEChannel& channe // // Create a listener for this channel and register callbacks -unique_ptr ICEMain::prepare_listener(onixs_ice::Handler& handler, const ICEChannel& channel) +unique_ptr ICEMain::prepare_listener(onixs_ice::Handler& handler, const ICEChannel& channel) { // We need to get a handler to the producer channels so we can produce heartbeats vector producer_channels; - for (auto& symbol : channel.symbols) + for (auto& symbol : channel.symbols) { producer_channels.push_back(_producer.get_channel(symbol.symbol().c_str(), symbol.type(), symbol.exchange())); } unique_ptr listener(new ICEListener(_index, _producer, /*_settlement_recorder,*/ producer_channels)); - for (auto& symbol : channel.symbols) - listener->add_filter_symbol(symbol.symbol()); + for (auto& symbol : channel.symbols) + listener->add_filter_symbol(symbol.symbol()); try { diff --git a/src/server/sparkd/md/platforms/ice_onixs/ICEMain.h b/src/server/sparkd/md/platforms/ice_onixs/ICEMain.h index ec6f1dfb8..5c43220c5 100644 --- a/src/server/sparkd/md/platforms/ice_onixs/ICEMain.h +++ b/src/server/sparkd/md/platforms/ice_onixs/ICEMain.h @@ -34,7 +34,7 @@ public: void start(std::vector&); void send_heartbeats() override; - std::unique_ptr prepare_handler(const ICEChannel& channel); + std::unique_ptr prepare_handler(const ICEChannel& channel, const std::string& license); std::unique_ptr prepare_listener(onixs_ice::Handler& handler, const ICEChannel& channel); private: diff --git a/src/server/sparkd/md/platforms/mdp3_onixs/MDP3Main.cpp b/src/server/sparkd/md/platforms/mdp3_onixs/MDP3Main.cpp index af427f286..78547f12a 100644 --- a/src/server/sparkd/md/platforms/mdp3_onixs/MDP3Main.cpp +++ b/src/server/sparkd/md/platforms/mdp3_onixs/MDP3Main.cpp @@ -1,6 +1,7 @@ #include "MDP3Main.h" #include "lib/md/cme/MDP3Utils.h" +#include "lib/md/vendor/OnixsUtils.h" #include "lib/md/MBOProcessor.h" #include "lib/config/config.h" #include "lib/md/coconut2.h" @@ -17,6 +18,7 @@ #include #include #include +#include using namespace std; using namespace bts::co2; @@ -144,7 +146,8 @@ onixs::SessionSettings session_settings_for_channel(const string& channel_id, MB void MDP3Main::start(std::vector& symbols, MBOWriter* mbo) { await_cme_config(); - initialize(); + auto license = onixs_create_temporary_license(); + initialize(license.path.string()); const auto& channels = parse_channels(symbols); _engines = create_feed_engines(channels); for (const auto& channel : channels) { @@ -160,37 +163,19 @@ void MDP3Main::start(std::vector& symbols, MBOWriter* mbo) { onixs::SessionSettings settings = session_settings_for_channel(pair.first, mbo); pair.second->start(settings); } + onixs_destroy_temporary_license(license); LOG(md,Info, "** CME MDP3 Publisher started **"); } void MDP3Main::send_heartbeats() { - for (const auto& listener: _listeners) - listener->heartbeat(); - - // utils::DispatcherTimer settlement_timer(disp, 100, [&]() { - // _settlement_recorder.do_work(); - // }); - // settlement_timer.start(); + for (const auto& listener: _listeners) { + listener->heartbeat(); + } } -void MDP3Main::initialize() { - const auto& options = config::get(); - auto license_location = os::bts_data_path() + "/licenses/"; - - if (!boost::filesystem::is_directory(license_location)) { - auto license_path =os::sparkd_wd() + "/data_files/Onixs.BTS.lic" ; - boost::filesystem::create_directories(license_location); - boost::filesystem::copy_file(license_path,license_location+"/Onixs.BTS.lic"); - } - - // - // License path +void MDP3Main::initialize(const std::string& license_store) { onixs::InitializationSettings init_settings; - auto license = options.onixs_license(); - init_settings.licenseStore(license); - if (license == "{default}") { - init_settings.licenseStore(license_location); - } + init_settings.licenseStore(license_store); LOG(md,Info,"Loading onixs license from {}", init_settings.licenseStore()); onixs::initialize(init_settings); } diff --git a/src/server/sparkd/md/platforms/mdp3_onixs/MDP3Main.h b/src/server/sparkd/md/platforms/mdp3_onixs/MDP3Main.h index bda70e4c6..baacdab35 100644 --- a/src/server/sparkd/md/platforms/mdp3_onixs/MDP3Main.h +++ b/src/server/sparkd/md/platforms/mdp3_onixs/MDP3Main.h @@ -35,7 +35,7 @@ public: void start(std::vector&, MBOWriter*); void send_heartbeats() override; - void initialize(); + void initialize(const std::string& license_store); std::unique_ptr prepare_handler(const std::string& channel, MBOWriter*); std::unique_ptr prepare_listener(onixs::Handler& handler, const CMEChannel& channel, MBOWriter*); diff --git a/src/server/sparkd/md/run_md.cpp b/src/server/sparkd/md/run_md.cpp index 1c99a8d63..a63c5f7c4 100644 --- a/src/server/sparkd/md/run_md.cpp +++ b/src/server/sparkd/md/run_md.cpp @@ -16,6 +16,7 @@ #include "lib/md/SecurityDB.h" #include "server/sparkd/exchange_sim/ExchangeSim.h" #include "lib/utils/thread_manager.h" +#include "lib/utils/parse_enum.h" #include @@ -170,16 +171,14 @@ string& uppercase(string& s) { return s; } template -bool parse_enum(const string& value, const string& prefix, T2 parse, T default_value, T* presult) { - string temp = value; - if (temp.find(prefix) == string::npos) { - temp = prefix + "_" + temp; - } - if(parse(temp, presult)) { +bool parse_enum(const string& value, const string& prefix, T2 parse, T default_value, T* result) { + auto opt = ::bts::parse_enum(value, prefix, parse); + if (opt) { + *result = opt.value(); return true; } else { - *presult = default_value; + *result = default_value; return false; } } @@ -209,13 +208,14 @@ struct MDArgs { MDArgs parse_md_args(const vector& args) { using namespace api; using namespace co2; - auto venue = VENUE_UNSET; // Default to glbx. If you change this change below (used) also + auto venue = VENUE_UNSET; auto platform = PLATFORM_UNSET; auto type = SECURITYTYPE_UNSET; bool hot = false; long number = 0; std::string symbol; for (auto str : args) { + // Iterate through each argument and see if it parses to one of these enums uppercase(str); bool used = (venue == VENUE_UNSET && parse_enum(str, "VENUE", Venue_Parse, venue, &venue)) || diff --git a/src/server/sparkd/position/MarginServiceServer.cpp b/src/server/sparkd/position/MarginServiceServer.cpp index 723c95e7b..e499062b1 100644 --- a/src/server/sparkd/position/MarginServiceServer.cpp +++ b/src/server/sparkd/position/MarginServiceServer.cpp @@ -230,6 +230,49 @@ void MarginServiceServer::set_cash_factor(const ::bts::api::position::CashFactor }); } } + +void MarginServiceServer::set_cash_balance_batch(const ::bts::api::position::CashBalanceBatch& 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); + + std::shared_ptr resp(new api::ResultMessage); + resp->set_success(true); + for(const auto& r : request.cash_balance()) + { + auto acct = AdminServiceUtils::find_account(_admin_data, r.account_id(), false); + if(!acct) + { + resp->set_success(false); + resp->set_error_message("Account ID" + std::to_string(r.account_id()) + " not found"); + } + else + { + acct->mutable_margin_limit()->set_cash_balance(r.cash_balance()); + AccountUpdateAPIArgument update; + update.mutable_complete_data()->CopyFrom(*acct); + + _admin_service.update_account(update, + [this, update, resp](const api::Result& admin_result) + { + resp->set_success(admin_result.success()); + resp->set_error_message(admin_result.error_message()); + incremental_update_report(update.complete_data().name(), update.complete_data().id()); + }); + } + if(!resp->success()) + break; + } + + reply(*resp); +} + void MarginServiceServer::set_cash_balance(const ::bts::api::position::CashBalanceUpdate& request, const api::Response<::bts::api::ResultMessage>& reply) diff --git a/src/server/sparkd/position/MarginServiceServer.h b/src/server/sparkd/position/MarginServiceServer.h index aa0c5cd01..6b5cf978e 100644 --- a/src/server/sparkd/position/MarginServiceServer.h +++ b/src/server/sparkd/position/MarginServiceServer.h @@ -40,6 +40,8 @@ public: void set_cash_balance(const ::bts::api::position::CashBalanceUpdate& request, const api::Response<::bts::api::ResultMessage>& reply) override; + void set_cash_balance_batch(const ::bts::api::position::CashBalanceBatch& , + const api::Response<::bts::api::ResultMessage>& ) override; void set_should_enforce_margin(const ::bts::api::position::EnforceUpdate& request, const api::Response<::bts::api::ResultMessage>& reply) override; diff --git a/src/server/sparkd/position/PositionMap.cpp b/src/server/sparkd/position/PositionMap.cpp new file mode 100644 index 000000000..2f4c6b809 --- /dev/null +++ b/src/server/sparkd/position/PositionMap.cpp @@ -0,0 +1,273 @@ +#include "PositionMap.h" +#include "lib/config/config.h" +#include "lib/config/protos/toggles.pb.h" +#include "lib/logging/logging.h" +#include "lib/utils/PBUtils.h" +#include "lib/utils/bts_assert.h" +#include "lib/api/service_helpers/PositionUtilities.h" +#include "lib/orderman/actors/SODPosition.h" +#include +#include "lib/api/services/AdminService.client.pb.h" +#include "lib/api/services/AdminService.pb.h" +#include +#include + +namespace bts { +namespace orderman { + +using namespace bts::position; +using namespace bts::api::orderman; +using namespace bts::api::position; + +PositionMap::PositionMap() +: +PositionMap (SODPosition::sod_time()){ +} + +PositionMap::PositionMap(timeval sod_time) +: +_pos_id(0), +_sod_helper(sod_time), +_account_id_map(), +_fill_map(), +_manual_fill_map(), +_pos_map(), +_base_pos_ts(), +_base_pos_map(){ +} + +const PositionUnit& PositionMap::on_fill (const Fill& f, + bool is_add){ + if(!is_add) + _fill_map.erase(f.fill_key()); + else + _fill_map[f.fill_key()] = f; + PositionUnit &pu = _get_or_add(f.internal_account_id(), + f.security_key()); + auto fill_type = _fill_type(f.timestamp()); + switch (fill_type){ + case FillType::BASE: + break; + case FillType::SOD: + PositionUtilities::apply_fill(*(pu.mutable_sod()),f,is_add); + break; + case FillType::DAY: + PositionUtilities::apply_fill(*(pu.mutable_day()),f,is_add); + if (f.fill_kind() == Fill::FILL_KIND_STANDARD) + PositionUtilities::apply_fill(*(pu.mutable_raw_day()),f,is_add); + break; + } + PositionUtilities::backwards_compat(pu); + return pu; + } + + const PositionUnit& PositionMap::on_manual_fill ( + const ManualFill& mf, + bool is_add){ + if(!is_add) + _manual_fill_map.erase(mf.record_id()); + else + _manual_fill_map[mf.record_id()] = mf; + PositionUnit &pu =_get_or_add(mf.account_id(), + mf.security_key()); + switch (mf.fill_type()) { + case ManualFill::LOCAL: + _on_manual_local_fill(mf, is_add); + break; + case ManualFill::ADMIN_DAY: + _on_manual_admin_fill(mf, is_add); + break; + default: + break; + } + PositionUtilities::backwards_compat(pu); + return pu; +} + +void PositionMap::set_base_position(const BasePositionBatch& request){ + _base_pos_ts = request.current_as_of(); + _base_pos_map.clear(); + for (const auto& bp : request.positions()){ + PositionEntry& e = _base_pos_map[bp.account_id()][bp.security_id()]; + PositionUtilities::apply_fill(e, + bp.side(), + bp.avg_price(), + bp.quantity(), + "", + true); + LOG(Gen, Info, "PositionMap::set_base_position acct_id:{} sec:key:{} " + "price:{} qty:{} side:{}", + bp.account_id(), bp.security_id(), + bp.avg_price(), bp.quantity(),bp.side()); + } + _reset_sod(); +} + +void PositionMap::set_admin_data ( + const api::admin::AdminData& admin_data){ + _account_id_map.clear(); + for (const auto &a : admin_data.accounts()) + _account_id_map[a.id()] = a.name(); + for (auto& acct_p : _pos_map) + for (auto& sec_p : acct_p.second) + _fill_account(sec_p.second); +} + +void PositionMap::get_pos (std::map& out_pos_map) const{ + for (auto &acct_p : _pos_map) { + for (auto &sec_p : acct_p.second) { + const PositionUnit& pu = sec_p.second; + out_pos_map[pu.record_id()] = &pu; + } + } +} + +ManualFill const* PositionMap::get_manual_fill (int rec_id) const{ + const auto& it = _manual_fill_map.find(rec_id); + if (it == _manual_fill_map.end()) + return nullptr; + else + return &(it->second); +} + +bool PositionMap::is_sod (google::protobuf::Timestamp fill_time) const{ + return _sod_helper.is_sod(fill_time); +} +/////////////////////////////////////////////////////////////////////////////// +// helpers //////////////////////////////////////////////////////////////////// +PositionUnit& PositionMap::_get_or_add(uint32_t account_id, uint32_t seckey){ + auto &pu = _pos_map[account_id][seckey]; + if (pu.record_id() == 0) { + // new PU + pu.set_record_id(++_pos_id); + pu.set_account_id(account_id); + pu.set_security_id(seckey); + // zeroing out all pos's + _zero_out_pos_unit(pu); + } + _fill_account(pu); + return pu; +} + +PositionMap::FillType PositionMap::_fill_type( + google::protobuf::Timestamp fill_time) const{ + if(fill_time.seconds() <= _base_pos_ts.seconds()) + return FillType::BASE; + else + return _sod_helper.is_sod(fill_time) ? FillType::SOD : FillType::DAY; +} + +void PositionMap::_reset_sod(){ + // 1. + // zero'ing out all pos + // important : once an acct_id,sec_key entry is made + // it is never deleted, only 0'd out + for (auto &acct_p : _pos_map) + for (auto &sec_p : acct_p.second) + _zero_out_pos_unit(sec_p.second); + // 2. + // entering the base pos as SOD + // important : base pos is always entered as current SOD + for (auto& acct_p : _base_pos_map) + for (auto& sec_p : acct_p.second) { + PositionUnit& pu = _get_or_add(acct_p.first, sec_p.first); + pu.mutable_sod()->CopyFrom(sec_p.second); + } + // 3. + // rentering all the fills + for (auto& fill_p : _fill_map) + on_fill(fill_p.second,true); + // 4. + // rentering all the manual fills + for (auto& mfill_p : _manual_fill_map) + on_manual_fill(mfill_p.second,true); + // 5. + // for backwards compat, copy it to the doubles + for (auto &acct_p : _pos_map) + for (auto &sec_p : acct_p.second) + PositionUtilities::backwards_compat(sec_p.second); + +} +// for backwards compatibility sake +void PositionMap::_fill_account (PositionUnit& u){ + if (u.account().empty()){ + const auto& it = _account_id_map.find(u.account_id()); + if (it != _account_id_map.end()) + u.set_account(it->second); + } +} + +void PositionMap::_zero_out_pos_unit (bts::api::position::PositionUnit& pu){ + _zero_out_pos_entry(*pu.mutable_day()); + _zero_out_pos_entry(*pu.mutable_raw_day()); + _zero_out_pos_entry(*pu.mutable_sod()); + _zero_out_pos_entry(*pu.mutable_local()); + _zero_out_pos_entry(*pu.mutable_admin_day()); + _zero_out_pos_entry(*pu.mutable_admin_sod()); + pu.set_sod_long_pos(0.0);pu.set_sod_long_pos(0.0); + pu.set_sod_short_pos(0.0);pu.set_sod_short_avg_price(0.0); + pu.set_day_long_pos(0.0);pu.set_day_long_avg_price(0.0); + pu.set_day_short_pos(0.0);pu.set_day_short_avg_price(0.0); +} + +void PositionMap::_zero_out_pos_entry (bts::api::position::PositionEntry& p){ + p.set_long_pos(0); + p.set_long_avg_price(0); + p.set_short_pos(0); + p.set_short_avg_price(0); +} + +void PositionMap::_on_manual_admin_fill (const ManualFill& mf, + bool is_add){ + PositionUnit &pu =_get_or_add(mf.account_id(), + mf.security_key()); + auto fill_type = _fill_type(mf.timestamp()); + switch (fill_type){ + case FillType::BASE: + break; + case FillType::SOD: + PositionUtilities::apply_manual_fill(*(pu.mutable_admin_sod()),mf,is_add); + break; + case FillType::DAY: + PositionUtilities::apply_manual_fill(*(pu.mutable_admin_day()),mf,is_add); + PositionUtilities::apply_manual_fill(*(pu.mutable_raw_day()),mf,is_add); + break; + } +} + +void PositionMap::_on_manual_local_fill (const ManualFill& mf, + bool is_add){ + PositionUnit &pu =_get_or_add(mf.account_id(), + mf.security_key()); + auto fill_type = _fill_type(mf.timestamp()); + switch (fill_type){ + case FillType::BASE: + case FillType::SOD: + // local fills only apply to DAY + break; + case FillType::DAY: + PositionUtilities::apply_manual_fill(*(pu.mutable_local()),mf,is_add); + PositionUtilities::apply_manual_fill(*(pu.mutable_raw_day()),mf,is_add); + break; + } +} + +// test only ////////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////// +PositionUnit const* PositionMap::test_only_pos (uint32_t account_id, + uint32_t sec_key) const{ + auto acct_it = _pos_map.find(account_id); + if (acct_it != _pos_map.end()){ + auto sec_it = acct_it->second.find(sec_key); + if(sec_it != acct_it->second.end()){ + return &(sec_it->second); + } + } + return nullptr; +} + +const std::map& PositionMap::test_only_fills() const{ + return _fill_map; +} + +}} \ No newline at end of file diff --git a/src/server/sparkd/position/PositionMap.h b/src/server/sparkd/position/PositionMap.h new file mode 100644 index 000000000..f98392888 --- /dev/null +++ b/src/server/sparkd/position/PositionMap.h @@ -0,0 +1,74 @@ + +#pragma once + +#include "lib/api/services/OrderService.pb.h" +#include "lib/api/services/PositionService.server.pb.h" +#include "lib/api/services/OrderService.client.pb.h" +#include +#include "lib/orderman/actors/SODPosition.h" +#include +#include + +namespace bts{ namespace api { namespace admin { class AdminData;}}} + +namespace bts{ +namespace orderman{ + +// Used by the PositionService (only) for access to all +// the position data +class PositionMap{ +private: + enum FillType{ + BASE, + SOD, + DAY + }; + + int _pos_id; + bts::orderman::SODPosition _sod_helper; + // this is kept for backwards compat, to put in + // the account names in PositionUnit + std::map _account_id_map; + // electronic fills + std::map _fill_map; + // all the manual fills (local or admin) + std::map _manual_fill_map; + // total position state + std::map> _pos_map; + // base position data + google::protobuf::Timestamp _base_pos_ts; + std::map> _base_pos_map; +private: + FillType _fill_type(google::protobuf::Timestamp fill_time) const; + FillType _fill_type(const bts::api::orderman::Fill &fill) const; + bts::api::position::PositionUnit& _get_or_add(uint32_t account_id, + uint32_t seckey); + void _reset_sod(); + void _fill_account (bts::api::position::PositionUnit&); + void _zero_out_pos_unit (bts::api::position::PositionUnit&); + void _zero_out_pos_entry (bts::api::position::PositionEntry&); + void _on_manual_admin_fill (const bts::api::position::ManualFill& mf, + bool is_add); + void _on_manual_local_fill (const bts::api::position::ManualFill& mf, + bool is_add); +public: + PositionMap(); + PositionMap(timeval sod_time); + const bts::api::position::PositionUnit& on_fill ( + const bts::api::orderman::Fill& f, + bool is_add); + const bts::api::position::PositionUnit& on_manual_fill ( + const bts::api::position::ManualFill& mf, + bool is_add); + void set_base_position(const bts::api::position::BasePositionBatch& request); + void set_admin_data (const bts::api::admin::AdminData&); + void get_pos (std::map&) const; + bts::api::position::ManualFill const* get_manual_fill (int rec_id) const; + bool is_sod (google::protobuf::Timestamp fill_time) const; +public: + bts::api::position::PositionUnit const* test_only_pos (uint32_t account_id, + uint32_t sec_key) const; + const std::map& test_only_fills() const; +}; + +}} diff --git a/src/server/sparkd/position/PositionServicePNLHelper.cpp b/src/server/sparkd/position/PositionServicePNLHelper.cpp index e7687c87b..403c9b52b 100644 --- a/src/server/sparkd/position/PositionServicePNLHelper.cpp +++ b/src/server/sparkd/position/PositionServicePNLHelper.cpp @@ -5,6 +5,7 @@ #include "PositionServicePNLHelper.h" #include "lib/config/protos/cme_reference_data.pb.h" #include "lib/config/config.h" +#include "lib/utils/PBUtils.h" #include "lib/utils/LockBox.h" #include @@ -67,9 +68,13 @@ public: else return PNLType::UNRESOLVED; } - std::string get_symbol (SecurityKey key) const{ - auto handle = _get_handle(key); - return handle.get_usersymbol().symbol(); + double get_cpp (SecurityKey key) const{ + auto h = _get_handle(key); + if (h.is_initialized()){ + const auto& sec = h.get_normalized_security(); + return sec.contract_spec().cash_per_point(); + } + return os::nan(); } void get_md(SecurityKey key, double& bid, double& ask, double& last, double& settle){ @@ -120,35 +125,25 @@ private: struct PositionServicePNLHelper::PImpl { HandleIndex handle_index; - std::map symbol_cpp_map; - std::map sec_cpp_map; + std::map missing_data_log_count; 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); - } + missing_data_log_count(){ } }; void PositionServicePNLHelper::compute_pnl( - const std::vector& in_pos, + const std::map& 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; + for (const auto& p : in_pos) { + const PositionUnit& pu = *(p.second); uint32_t sec_key = pu.security_id(); - double cpp = _get_cpp(sec_key); + double cpp = me->handle_index.get_cpp(SecurityKey(sec_key)); double bid, ask, last, settle; me->handle_index.get_md(SecurityKey(sec_key),bid,ask,last,settle); PNLUnit pnlu; @@ -160,6 +155,7 @@ void PositionServicePNLHelper::compute_pnl( pnlu.set_last_price(last); pnlu.set_settlement_price(settle); out_pnl.push_back(pnlu); + _log_missing_data(pnlu); } _aggregate_account_pnl(out_pnl,account_map,out_account_pnl); } @@ -233,25 +229,24 @@ double PositionServicePNLHelper::_compute_day_pnl ( } 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; - } +void PositionServicePNLHelper::_log_missing_data(const PNLUnit& u){ + auto sec_key = SecurityKey(u.pos().security_id()); + bool missing_cpp = os::is_nan(u.cash_value()), + missing_md = os::is_nan(u.bid_price()) && + os::is_nan(u.ask_price()) && + os::is_nan(u.last_price()) && + os::is_nan(u.settlement_price()); + if(missing_cpp || missing_md){ + const auto it = me->missing_data_log_count.find(sec_key); + uint16_t count = it == me->missing_data_log_count.end() ? 0 : + it->second; + if (count <= 50) + LOG(md, Warn, "Missing pnl data sec:{} cpp:{} md:{}", + sec_key, missing_cpp, missing_md); + me->missing_data_log_count[sec_key] = count+1; } else - return it->second; + me->missing_data_log_count[sec_key] = 0; } PositionServicePNLHelper::PositionServicePNLHelper(md::MDClient& md_client) : diff --git a/src/server/sparkd/position/PositionServicePNLHelper.h b/src/server/sparkd/position/PositionServicePNLHelper.h index 32190d471..1d9cc29e2 100644 --- a/src/server/sparkd/position/PositionServicePNLHelper.h +++ b/src/server/sparkd/position/PositionServicePNLHelper.h @@ -23,10 +23,11 @@ private: const std::vector& in_pnl, const std::map& account_map, std::vector& out_account_pnl); + void _log_missing_data(const bts::api::position::PNLUnit&); public: PositionServicePNLHelper(md::MDClient& client); ~PositionServicePNLHelper(); - void compute_pnl(const std::vector& in_pos, + void compute_pnl(const std::map& in_pos, const std::map& account_map, std::vector& out_pnl, std::vector& out_account_pnl); diff --git a/src/server/sparkd/position/PositionServiceServer.cpp b/src/server/sparkd/position/PositionServiceServer.cpp index 7c3fdf022..f622e2e5a 100644 --- a/src/server/sparkd/position/PositionServiceServer.cpp +++ b/src/server/sparkd/position/PositionServiceServer.cpp @@ -1,14 +1,18 @@ #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 "lib/api/public/ServiceContext.h" +#include "lib/api/service_helpers/AdminServiceUtils.h" +#include "lib/api/service_helpers/PositionUtilities.h" +#include "lib/api/services/AdminService.client.pb.h" +#include "lib/api/services/AdminService.pb.h" +#include "lib/api/services/OrderService.client.pb.h" +#include "lib/api/services/OrderService.pb.h" +#include "lib/logging/logging.h" +#include "lib/orderman/actors/SODPosition.h" +#include "lib/persistence/SqliteStore.h" +#include "lib/utils/PBUtils.h" +#include "lib/utils/Puid.h" +#include "PositionMap.h" #include #include @@ -18,346 +22,299 @@ #include namespace bts { -namespace position{ +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; + bts::orderman::PositionMap state; + bts::api::ServiceContext &context; + bts::api::orderman::OrderService order_service; + PositionServiceBase::Position_dataStatefulPublisher pos_publisher; + int id; + // manual fills management + std::unique_ptr manual_fills_store; + PositionServiceBase::Manual_fillsStatefulPublisher manual_fills_publisher; + // pnl PositionServiceBase::Pnl_dataStatefulPublisher pnl_publisher; PositionServiceBase::Pnl_account_dataStatefulPublisher pnl_account_publisher; - bts::api::orderman::OrderService order_service; - 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; + // admin data needed for pnl + bts::api::admin::AdminService admin_service; api::admin::AdminData admin_data; - std::map account_map; + std::map account_map; - PImpl(bts::api::ServiceContext& context, - const PositionServiceBase::Position_dataStatefulPublisher& 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), - admin_data(), - account_map() - { - } + PImpl( + bts::api::ServiceContext &context, + const PositionServiceBase::Position_dataStatefulPublisher &pos_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) + : // pos + context(context), + order_service(context.locate()), + pos_publisher(pos_publisher), + // manual fills + manual_fills_store( + std::make_unique("manual_fills")), + manual_fills_publisher(manual_fills_publisher), + // pnl + pnl_publisher(pnl_publisher), + pnl_account_publisher(pnl_account_publisher), + pnl_helper(md_client), + poll_period_ms(300), + // admin + admin_service(context.locate()), + admin_data(), + account_map() {} }; -PositionServiceServer::PositionServiceServer(bts::api::ServiceContext& context, - bts::md::MDClient& md_client) -: -PositionServiceBase (context), -me(new PImpl(context, - make_position_data_stateful_publisher(), - make_pnl_data_stateful_publisher(), - make_pnl_account_data_stateful_publisher(), - make_manual_fills_stateful_publisher(), - md_client)) -{ +PositionServiceServer::PositionServiceServer(bts::api::ServiceContext &context, + bts::md::MDClient &md_client) + : PositionServiceBase(context), + me(new PImpl(context, make_position_data_stateful_publisher(), + make_pnl_data_stateful_publisher(), + make_pnl_account_data_stateful_publisher(), + make_manual_fills_stateful_publisher(), md_client)) { + _delete_presod_local_on_start(); + // incremental fill update 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)); - 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(); - }); - + [this](auto const &update) { + for (const auto &f : update.removed()) + me->pos_publisher.add_or_update(_on_fill(*f, false)); + for (const auto &f : update.added()) + me->pos_publisher.add_or_update(_on_fill(*f, true)); + for (const auto &f : update.changed()) { + me->pos_publisher.add_or_update(_on_fill(*f.previous(), false)); + me->pos_publisher.add_or_update(_on_fill(*f.current(), true)); + } + me->pos_publisher.flush(); + _compute_pnl_and_publish(); + } + ); + // admin data 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());} + 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); + // manual fills auto all = me->manual_fills_store->retrieve_all()[manual_fills_ns]; LOG(persist, Info, "Finished loading all {} manual fills", all.size()); ManualFill rec; for (auto const &kvp : all) if (utils::pb::from_string(kvp.second, &rec)) { - me->manual_fills.insert({rec.record_id(), rec}); me->manual_fills_publisher.add_or_update(rec); - me->publisher.add_or_update(_on_fill(to_fill(rec),true)); - } - else { + me->pos_publisher.add_or_update(_on_manual_fill(rec, true)); + } else { LOG(OrderMan, Error, "Could not recover manual fill {} from storage. " "This should be investigated", kvp.first); } me->manual_fills_publisher.flush(); - me->publisher.flush(); + me->pos_publisher.flush(); + me->context.post([this]() { _periodic_poll(); }, me->poll_period_ms); } -bts::api::orderman::Fill PositionServiceServer::to_fill(bts::api::position::ManualFill const&src) -{ - using namespace api::orderman; - bts::api::orderman::Fill result; - result.set_account(src.account_name()); - result.set_security_key(src.security_key()); - result.set_last_price(src.price()); - result.set_side(src.side()); - result.set_last_shares(src.quantity()); - result.set_fill_kind(Fill::FILL_KIND_STANDARD); - result.set_fill_key(std::string("manual:")+std::to_string(src.record_id())); - return result; +void PositionServiceServer::_delete_presod_local_on_start() const{ + std::set rm_ids; + auto all = me->manual_fills_store->retrieve_all()[manual_fills_ns]; + ManualFill rec; + for (auto const &kvp : all){ + if (utils::pb::from_string(kvp.second, &rec)) { + if(rec.fill_type() == ManualFill::LOCAL && + me->state.is_sod(rec.timestamp())){ + LOG(OrderMan, Info, + "PositionServiceServer::_delete_presod_local_on_start " + "fill: {}", kvp.second); + auto id = std::to_string(rec.record_id()); + rm_ids.insert(id); + } + } + else + LOG(OrderMan, Error, + "Could not recover manual fill {} from storage. " + "This should be investigated", + kvp.first); + } + me->manual_fills_store->remove(manual_fills_ns, rm_ids); } -const PositionUnit& PositionServiceServer::_on_fill (const Fill& f, - bool is_add){ - 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){ - _avg_price_qty(pu.sod_long_avg_price(),pu.sod_long_pos(), - f.last_price(),f.last_shares(), - is_add,f.fill_key(), - avg_price, tot_qty); - pu.set_sod_long_avg_price(avg_price); - pu.set_sod_long_pos(tot_qty); - } - else{ - _avg_price_qty(pu.sod_short_avg_price(),pu.sod_short_pos(), - f.last_price(),f.last_shares(), - is_add,f.fill_key(), - avg_price, tot_qty); - pu.set_sod_short_avg_price(avg_price); - pu.set_sod_short_pos(tot_qty); - } - } - else{ - if(f.side()==api::Side::SIDE_BUY){ - _avg_price_qty(pu.day_long_avg_price(),pu.day_long_pos(), - f.last_price(),f.last_shares(), - is_add,f.fill_key(), - avg_price, tot_qty); - pu.set_day_long_avg_price(avg_price); - pu.set_day_long_pos(tot_qty); - } - else{ - _avg_price_qty(pu.day_short_avg_price(),pu.day_short_pos(), - f.last_price(),f.last_shares(), - is_add,f.fill_key(), - avg_price, tot_qty); - pu.set_day_short_avg_price(avg_price); - pu.set_day_short_pos(tot_qty); - } - } - return pu; +const PositionUnit &PositionServiceServer::_on_fill(const Fill &f, + bool is_add) { + return me->state.on_fill(f,is_add); } -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_security_id(seckey); - pu.set_account_id(account_id); - pu.set_account(f.account()); - } - return pu; +const PositionUnit &PositionServiceServer::_on_manual_fill(const ManualFill &mf, + bool is_add) { + return me->state.on_manual_fill(mf,is_add); } -void PositionServiceServer::_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){ - if(fill_q<0){ - LOG(OrderMan,Error,"PositionServiceServer::_avg_price_qty " - "fill_id:{} fill_q:{} is_add:{}", - fill_id, fill_q, is_add); - fill_q=0; - } - if(is_add){ - tot_qty=base_q+fill_q; - avg_price = (base_p*base_q+fill_p*fill_q)/tot_qty; - } - else{ - if(fill_q>=base_q){ - tot_qty=0; - avg_price=0; - if(fill_q>base_q) - LOG(OrderMan,Error,"PositionServiceServer::_avg_price_qty " - "fill_id:{} base_qty:{} fill_qty:{}", - fill_id, base_q, fill_q); - } - else{ - tot_qty=base_q-fill_q; - avg_price = (base_p*base_q-fill_p*fill_q)/tot_qty; - } - } -} -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){ +void PositionServiceServer::_set_admin_data( + const api::admin::AdminData &admin_data) { me->admin_data = admin_data; - for (const auto& a : admin_data.accounts()) + for (const auto &a : admin_data.accounts()) me->account_map[a.name()] = a.id(); -} -PositionServiceServer::~PositionServiceServer(){ + me->state.set_admin_data(admin_data); + _clear_and_publish_pos(); } +template +void _diff (const std::map& new_state, + const std::map& prev_state, + std::vector& added, + std::vector& updated, + std::vector& deleted){ + for (const auto& p : new_state){ + if(prev_state.find(p.first)==prev_state.end()) + added.push_back(p.second); + else + updated.push_back(p.second); + } + for (const auto& p : prev_state) + if(new_state.find(p.first)==new_state.end()) + deleted.push_back(p.second); +} + +void PositionServiceServer::_clear_and_publish_pos(){ + std::map new_state; + me->state.get_pos (new_state); + // entries are never deleted from new_state + for (const auto& p : new_state) + me->pos_publisher.add_or_update(*p.second); + me->pos_publisher.flush(); +} +/////////////////////////////////////////////////////////////////////////////// +/// Manual Fills API ////////////////////////////////////////////////////////// void PositionServiceServer::create_manual_fill( - const ManualFillBatch &batch, - const api::Response &r) -{ + const ManualFillBatch &batch, + const api::Response &r) { PositionServiceResponse resp; for (auto mf : batch.manual_fill()) { - auto id = utils::gen_id ("manual_fills"); + auto id = utils::gen_id("manual_fills"); mf.set_record_id(id); + struct timeval tv; + gettimeofday(&tv, NULL); + mf.mutable_timestamp()->set_seconds(tv.tv_sec); + mf.mutable_timestamp()->set_nanos(tv.tv_usec * 1000); - me->manual_fills.insert({mf.record_id(), mf}); - me->manual_fills_store->insert(manual_fills_ns, - std::to_string(id), + me->manual_fills_store->insert(manual_fills_ns, std::to_string(id), utils::pb::to_string(mf)); resp.add_response()->set_ok(true); me->manual_fills_publisher.add_or_update(mf); - me->publisher.add_or_update(_on_fill(to_fill(mf),true)); + me->pos_publisher.add_or_update(_on_manual_fill(mf, true)); } r(resp); me->manual_fills_publisher.flush(); - me->publisher.flush(); + me->pos_publisher.flush(); } void PositionServiceServer::update_manual_fill( - const ManualFillBatch &batch, - const api::Response &r) -{ + const ManualFillBatch &batch, + const api::Response &r) { PositionServiceResponse resp; for (auto mf : batch.manual_fill()) { auto *this_resp = resp.add_response(); auto id = std::to_string(mf.record_id()); - auto p = me->manual_fills_store->retrieve_one(manual_fills_ns,id); + auto p = me->manual_fills_store->retrieve_one(manual_fills_ns, id); auto found = p.first; if (!found) { this_resp->set_ok(false); this_resp->set_msg("Could not find record"); - } - else { - me->manual_fills_store->update(manual_fills_ns, - id, + } else { + me->manual_fills_store->update(manual_fills_ns, id, utils::pb::to_string(mf)); this_resp->set_ok(true); // Retrieve old record so we can remove it from position - auto it = me->manual_fills.find(mf.record_id()); - bts_assert(it!= me->manual_fills.end()); - auto &old = it->second; + ManualFill const* old_ptr = me->state.get_manual_fill(mf.record_id()); + bts_assert(old_ptr != nullptr); // remove old - me->publisher.add_or_update(_on_fill(to_fill(old),false)); + me->pos_publisher.add_or_update(_on_manual_fill(*old_ptr, false)); // add new - me->manual_fills.insert({mf.record_id(), mf}); - me->publisher.add_or_update(_on_fill(to_fill(mf),true)); + me->pos_publisher.add_or_update(_on_manual_fill(mf, true)); me->manual_fills_publisher.add_or_update(mf); } } r(resp); - me->publisher.flush(); + me->pos_publisher.flush(); me->manual_fills_publisher.flush(); } void PositionServiceServer::remove_manual_fill( - const ManualFillBatch &batch, - const api::Response &r) -{ + const ManualFillBatch &batch, + const api::Response &r) { PositionServiceResponse resp; for (auto mf : batch.manual_fill()) { auto *this_resp = resp.add_response(); auto id = std::to_string(mf.record_id()); - auto p = me->manual_fills_store->retrieve_one(manual_fills_ns,id); + auto p = me->manual_fills_store->retrieve_one(manual_fills_ns, id); auto found = p.first; if (!found) { this_resp->set_ok(false); this_resp->set_msg("Could not find record"); - } - else { + } else { // Retrieve old record so we can remove it from position - auto it = me->manual_fills.find(mf.record_id()); - bts_assert(it != me->manual_fills.end()); - auto &old = it->second; - me->publisher.add_or_update(_on_fill(to_fill(old),false)); - me->manual_fills.erase(it); + ManualFill const* old_ptr = me->state.get_manual_fill(mf.record_id()); + bts_assert(old_ptr != nullptr); + me->pos_publisher.add_or_update(_on_manual_fill(*old_ptr, false)); - std::set to_r ; + std::set to_r; to_r.insert(id); - me->manual_fills_store->remove(manual_fills_ns,to_r); + me->manual_fills_store->remove(manual_fills_ns, to_r); this_resp->set_ok(true); me->manual_fills_publisher.erase(mf.record_id()); } } r(resp); - me->publisher.flush(); + me->pos_publisher.flush(); 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); - } - } +void PositionServiceServer::set_base_position( + const BasePositionBatch& req, + const api::Response&) { + LOG(persist, Info, "PositionServiceServer::set_base_position bp count:{}", + req.positions_size()); + me->state.set_base_position(req); + _clear_and_publish_pos(); +} + +/////////////////////////////////////////////////////////////////////////////// +/// PNL helpers /////////////////////////////////////////////////////////////// +void PositionServiceServer::_compute_pnl_and_publish() { + std::map in_pos; + me->state.get_pos(in_pos); 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_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) + 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(){ +void PositionServiceServer::_periodic_poll() { _compute_pnl_and_publish(); - me->context.post([this](){_periodic_poll();},me->poll_period_ms); + me->context.post([this]() { _periodic_poll(); }, me->poll_period_ms); } -}} + +PositionServiceServer::~PositionServiceServer() {} +} // namespace position +} // namespace bts diff --git a/src/server/sparkd/position/PositionServiceServer.h b/src/server/sparkd/position/PositionServiceServer.h index dac6f2996..769c66e6d 100644 --- a/src/server/sparkd/position/PositionServiceServer.h +++ b/src/server/sparkd/position/PositionServiceServer.h @@ -5,10 +5,27 @@ #include namespace bts { -namespace md { class MDClient;} -namespace api { class ServiceContext; } -namespace api { namespace orderman { class Fill;}} -namespace api { namespace admin { class AdminData;}} +namespace md { +class MDClient; +} +namespace api { +class ServiceContext; +} +namespace api { +namespace orderman { +class Fill; +} +} // namespace api +namespace api { +namespace admin { +class AdminData; +} +} // namespace api +namespace api { +namespace position { +class ManualFill; +} +} // namespace api namespace position { @@ -18,29 +35,25 @@ class PositionServiceServer : public bts::api::position::PositionServiceBase { private: struct PImpl; std::unique_ptr me; - - private: + // this function deleted all local fills with + // a timestamp less than SOD + void _delete_presod_local_on_start() const; // this is the function that aggrgates the // position. is_add is to indicate if a row // is added or removed const bts::api::position::PositionUnit & - _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 bts::api::orderman::Fill& fill); + _on_fill(const bts::api::orderman::Fill &fill, bool is_add); + // analogous to the above, for manual fills (this inclused + // both client(local) and server(admin) fills) + const bts::api::position::PositionUnit & + _on_manual_fill(const api::position::ManualFill &fill, bool is_add); void _periodic_poll(); + // recomputes the whole pnl (no increments) and publishes it 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); - + void _set_admin_data(const api::admin::AdminData &admin_data); + void _clear_and_publish_pos(); public: - PositionServiceServer(bts::api::ServiceContext &context, - md::MDClient&); + PositionServiceServer(bts::api::ServiceContext &context, md::MDClient &); void create_manual_fill( const ::bts::api::position::ManualFillBatch &, @@ -57,7 +70,11 @@ class PositionServiceServer : public bts::api::position::PositionServiceBase { const api::Response<::bts::api::position::PositionServiceResponse> &) override; - bts::api::orderman::Fill to_fill(bts::api::position::ManualFill const&); + void set_base_position( + const ::bts::api::position::BasePositionBatch &request, + const api::Response<::bts::api::position::SetBasePositionResponse> + &reply) override; + ~PositionServiceServer(); }; } // namespace position diff --git a/src/server/sparkd/position/import/BasePositionServiceServer.cpp b/src/server/sparkd/position/import/BasePositionServiceServer.cpp new file mode 100644 index 000000000..58ecf78d0 --- /dev/null +++ b/src/server/sparkd/position/import/BasePositionServiceServer.cpp @@ -0,0 +1,219 @@ +#include "BasePositionServiceServer.h" +#include "lib/api/public/ServiceContext.h" +#include "lib/api/services/AdminService.client.pb.h" +#include "lib/api/services/PositionService.client.pb.h" +#include "lib/config/config.h" +#include "lib/config/protos/position_cfg.pb.h" +#include "lib/config/protos/orders_fills_cleanup.pb.h" +#include "lib/logging/logging.h" +#include "lib/utils/PBUtils.h" +#include "lib/utils/bts_assert.h" +#include "lib/utils/datetime.h" +#include "server/sparkd/position/import/parsing/gmi_parser.h" +#include "server/sparkd/position/import/DownloadCommonStateMachine.h" + +#include +#include +#include +#include + +namespace bts { +namespace position { + +struct BasePositionServiceServer::PImpl { + PImpl(BasePositionServiceServer &owner, + bts::api::ServiceContext &ctx) + : ctx(ctx), + admin_service(ctx.locate()), + pos_service(ctx.locate()), + download_sm("Position downloader", + + // File picker + [&owner] (auto const &candidates) { + return owner.pick_most_recent_pos_file(candidates); + }, + + // Download complete handler + [&owner] (auto const&sftp_file, auto const&local_file) { + return owner.on_download_complete(sftp_file,local_file); + }, + + // Thread Marshaller + [&ctx] (auto const&action) { + ctx.post(action); + }), + admin_data() + {} + + bts::api::ServiceContext &ctx; + api::admin::AdminService admin_service; + api::position::PositionService pos_service; + DownloadCommonStateMachine download_sm; + std::shared_ptr admin_data; +}; + +BasePositionServiceServer::BasePositionServiceServer( + bts::api::ServiceContext &ctx) + : BasePositionServiceBase(ctx), me(new PImpl(*this,ctx)) { + auto const &c = config::get(); + + if (c.sftp_host().size() && !c.disable_position_download()) { + LOG(OrderMan, Info, "BasePositionServiceServer is active."); + + on_timer(); + me->admin_service.subscribe_admin_data() | + rxcpp::operators::subscribe( + [this](auto const &update) { + bts_assert(update.all().size() == 1); + me->admin_data = update.all()[0]; + me->download_sm.on_admin(me->admin_data); + }); + } else + LOG(OrderMan, Warn, + "GMI import is disabled, since no host is specified in the " + "GMI_Import.sftp_host config key"); +} + +BasePositionServiceServer::~BasePositionServiceServer() {} + +void BasePositionServiceServer::parse_gmi_csv( + const api::position::GMI_CSVBlock &b, + const api::Response &reply) { + + api::position::GMI_ParseResult result; + if (!me->admin_data) { + *result.mutable_failure() = "Server is not ready"; + reply(result); + return; + } + + std::istringstream in{b.text()}; + auto parse_result = parse_gmi(*me->admin_data, in); + + if (!std::holds_alternative( + parse_result)) { + *result.mutable_failure() = std::get(parse_result); + reply(result); + return; + } + + auto base_position = + std::get(parse_result); + *result.mutable_success() = reduce_parsed_gmi(base_position); + reply(result); + return; +} + +void BasePositionServiceServer::on_timer() { + me->ctx.post([this]() { on_timer(); }, 10000); + me->download_sm.on_timer(); +} + +std::optional BasePositionServiceServer::on_download_complete( + std::string const &sftp_file_path, std::string const &local_file_path) { + + LOG(OrderMan, Warn, + "Successfully downloaded GMI file {} to local file " + "{}. Will parse next.", + sftp_file_path, local_file_path); + + auto as_of = get_as_of_date(sftp_file_path); + if (!as_of.has_value()) { + auto error_msg = fmt::format( + "Could not extract date from sftp file name {}", sftp_file_path); + return error_msg; + } + + auto parse_result = parse_gmi_file(local_file_path); + if (!std::holds_alternative( + parse_result)) { + auto error_msg = + fmt::format("Failed to parse GMI file at {}. Error msg: {}", + local_file_path, std::get(parse_result)); + return error_msg; + } + + auto base_position = + std::get(parse_result); + + LOG(OrderMan, Warn, + "Successfully parsed GMI file at {}. Extracted {} records for " + "the " + "base position", + local_file_path, base_position.positions_size()); + + *base_position.mutable_current_as_of() = + utils::pb::to_timestamp(as_of.value()); + + auto reduced_base_pos = reduce_parsed_gmi(base_position); + + LOG(OrderMan, Warn, "Reduced parsed GMI file at {} from {} to {} records", + local_file_path, base_position.positions_size(), + reduced_base_pos.positions_size()); + + for (auto const &bp : reduced_base_pos.positions()) + LOG(OrderMan, Info, "Base position for Account: {} Sec: {} {} {} @ {} ", + bp.account_id(), bp.security_id(), api::Side_Name(bp.side()), + bp.quantity(), bp.avg_price()); + + me->pos_service.set_base_position( + reduced_base_pos, + [this](auto const &rr) { + if (!rr.success()) + on_position_set_failure(rr.error_message()); + else if (!rr.value()->ok()) + on_position_set_failure(rr.value()->msg()); + }, + 5000); + return {}; +} + +void BasePositionServiceServer::on_position_set_failure( + std::string const &error) { + LOG(OrderMan, Error, + "Failed to set base position through the position service. The " + "following information is available: {}", + error); + me->download_sm.on_post_handle_failure(error); +} + +std::optional +BasePositionServiceServer::get_as_of_date(std::string const &sftp_file_name) { + std::regex rx(".*btspos_(\\d\\d\\d\\d)(\\d\\d)(\\d\\d)\\.csv.*"); + std::smatch matches; + if (!std::regex_match(sftp_file_name, matches, rx)) + return {}; + auto y = std::stoi(matches[1]); + auto m = std::stoi(matches[2]); + auto d = std::stoi(matches[3]); + + const auto &sod = config::get(); + try { + return datetime::make_ptime(y, m, d, sod.sod_hh(), sod.has_sod_mm(), 0, + 0); + } catch (std::out_of_range const &) { + return {}; + } +} + +std::optional BasePositionServiceServer::pick_most_recent_pos_file( + std::vector const &candidates) { + + std::vector pos_files; + std::copy_if(candidates.begin(), candidates.end(), + std::back_inserter(pos_files), [](auto const &file_name) { + return boost::algorithm::starts_with(file_name, "btspos_"); + }); + if (pos_files.empty()) + return {}; + std::sort(pos_files.begin(), pos_files.end()); + return pos_files.back(); +} + +std::variant +BasePositionServiceServer::parse_gmi_file(std::string const &f) { + return position::parse_gmi(*me->admin_data, f); +} + +} // namespace position +} // namespace bts diff --git a/src/server/sparkd/position/import/BasePositionServiceServer.h b/src/server/sparkd/position/import/BasePositionServiceServer.h new file mode 100644 index 000000000..dbcfcf8e0 --- /dev/null +++ b/src/server/sparkd/position/import/BasePositionServiceServer.h @@ -0,0 +1,54 @@ +#pragma once + +#include "lib/api/services/BasePositionService.server.pb.h" +#include "lib/utils/datetime.h" +#include +#include + +namespace bts { +namespace api { +namespace position { +class BasePositionBatch; +} +namespace admin { +class AdminData; +} +} // namespace api +} // namespace bts + +namespace bts { +namespace position { + +class DownloadWindow; + +class BasePositionServiceServer + : public bts::api::position::BasePositionServiceBase { + public: + BasePositionServiceServer(bts::api::ServiceContext &ctx); + ~BasePositionServiceServer(); + + void parse_gmi_csv( + const api::position::GMI_CSVBlock &, + const api::Response &) override; + + private: + void on_timer(); + void on_admin(std::shared_ptr const &); + std::optional on_download_complete(std::string const &sftp_file_path, + std::string const &local_file_path); + void on_position_set_failure(std::string const &); + + std::optional + pick_most_recent_pos_file(std::vector const &); + + std::optional + get_as_of_date(std::string const &sftp_file_name); + + std::variant + parse_gmi_file(std::string const &file_path); + + struct PImpl; + std::unique_ptr me; +}; +} // namespace position +} // namespace bts diff --git a/src/server/sparkd/position/import/DownloadCommonStateMachine.cpp b/src/server/sparkd/position/import/DownloadCommonStateMachine.cpp new file mode 100644 index 000000000..e51f0fce9 --- /dev/null +++ b/src/server/sparkd/position/import/DownloadCommonStateMachine.cpp @@ -0,0 +1,246 @@ +#include "DownloadCommonStateMachine.h" + +#include "lib/config/config.h" +#include "lib/config/protos/position_cfg.pb.h" +#include "lib/utils/bts_assert.h" +#include "lib/utils/datetime.h" +#include "lib/utils/sftp.h" +#include "server/sparkd/position/import/DownloadWindow.h" + +namespace bts { +namespace position { + +using namespace boost::posix_time; + +enum class DownloadCommonStateMachine::State { + W_ADMIN, // Waiting for the initial admin object + DOWNLOADING, // In the process of downloading the newest file + W_WINDOW, // Waiting for the download window to open + PAUSED, // Waiting for a few seconds before starting a download +}; + +struct DownloadCommonStateMachine::PImpl { + PImpl(std::string const &state_machine_descriptor, FilePicker const &p, + FileHandler const &h, ThreadMarshaller const &m) + : descriptor(state_machine_descriptor), file_picker(p), file_handler(h), + post(m), state(State::W_ADMIN), last_download_attempt_ts(), + admin_data(), + download_window( + now(), + duration_from_string( + config::get().download_window_begin()), + duration_from_string( + config::get().download_window_end())) {} + + std::string descriptor; + FilePicker file_picker; + FileHandler file_handler; + ThreadMarshaller post; + State state = State::W_ADMIN; + datetime::PTime last_download_attempt_ts; + std::shared_ptr admin_data; + DownloadWindow download_window; +}; + +DownloadCommonStateMachine::DownloadCommonStateMachine( + std::string const &desc, FilePicker const &p, FileHandler const &h, + ThreadMarshaller const &m) + : me(new PImpl(desc, p, h, m)) { + + LOG(OrderMan, Info, "{} current download window is {}", me->descriptor, + me->download_window); +} + +DownloadCommonStateMachine::~DownloadCommonStateMachine() {} + +datetime::PTime DownloadCommonStateMachine::now() { + return datetime::local_now(); +} + +void DownloadCommonStateMachine::on_admin( + std::shared_ptr const &a) { + LOG(OrderMan, Info, "Received new admin data in {}", me->descriptor); + + me->admin_data = a; + + switch (me->state) { + case State::W_ADMIN: + case State::W_WINDOW: + LOG(OrderMan, Info, + "{} received admistrative data in state {}. " + "Beginning " + "download", + me->descriptor, state_name(me->state)); + pause(); + return; + case State::DOWNLOADING: + case State::PAUSED: + LOG(OrderMan, Info, + "{} received new administrative data while in " + "state {}. " + "The new data will be used when decoding the next position file", + me->descriptor, state_name(me->state)); + return; + } +} + +void DownloadCommonStateMachine::on_timer() { + switch (me->state) { + case State::W_ADMIN: + case State::DOWNLOADING: + break; + case State::PAUSED: + if (pause_over()) { + LOG(OrderMan, Info, "{} pause period over. Beginning download", + me->descriptor); + start_download(); + } else + LOG(OrderMan, Diag, "{} is paused", me->descriptor); + break; + case State::W_WINDOW: + if (me->download_window.is_inside(now())) { + LOG(OrderMan, Info, + "{} download window {} began. Beginning " + "download", + me->descriptor, me->download_window); + start_download(); + } else + LOG(OrderMan, Diag, + "{} is waiting for the download window {} to begin", + me->descriptor, me->download_window); + break; + } +} + +void DownloadCommonStateMachine::start_download() { + LOG(OrderMan, Info, "{} attempting download", me->descriptor); + transition(State::DOWNLOADING); + + using namespace bts::utils; + const auto &conf = config::get(); + + begin_sftp_download( + conf.sftp_host(), conf.sftp_port(), conf.sftp_user(), + conf.sftp_password(), conf.download_timeout_seconds() * 1000, + [this](const auto &v) { return me->file_picker(v); }, + [this](auto const &result) { + if (std::holds_alternative(result)) { + auto error_msg = fmt::format( + "{} failed to download file. Error={}", me->descriptor, + std::get(result).error_msg); + me->post([this, error_msg]() { on_download_failure(error_msg); }); + } else { + auto sftp_file_path = + std::get(result).sftp_file_path; + auto local_file_path = + std::get(result).local_file_path; + me->post([this, sftp_file_path, local_file_path]() { + on_download_complete(sftp_file_path, local_file_path); + }); + } + }); +} + +void DownloadCommonStateMachine::on_download_failure( + std::string const &error_msg) { + LOG(OrderMan, Error, + "{} failed to obtain or parse the target file. The following " + "information is " + "available: {}. ", + me->descriptor, error_msg); + + if (me->download_window.is_inside(now())) + pause(); + else { + if (now() > me->download_window.stop()) + advance_download_window(); + wait_download_window(); + } +} + +void DownloadCommonStateMachine::on_download_complete( + std::string const &sftp_file_path, std::string const &local_file_path) { + + LOG(OrderMan, Warn, + "{} successfully downloaded target file {} to local file " + "{}. Will parse next.", + me->descriptor, sftp_file_path, local_file_path); + + bts_assert(me->state == State::DOWNLOADING); + auto error = me->file_handler(sftp_file_path, local_file_path); + if (error.has_value()) + on_download_failure(error.value()); + else { + if (now() >= me->download_window.start()) + advance_download_window(); + wait_download_window(); + } +} + +void DownloadCommonStateMachine::on_post_handle_failure( + std::string const &error) { + LOG(OrderMan, Error, + "A post file handlong service downsream from {} failed. The " + "following information is available: {}", + me->descriptor, error); + + switch (me->state) { + case State::W_ADMIN: + bts_assert(false); + return; + case State::W_WINDOW: + LOG(OrderMan, Info, "{} will force retry of download", me->descriptor); + pause(); + return; + case State::DOWNLOADING: + case State::PAUSED: + return; + } +} + +void DownloadCommonStateMachine::advance_download_window() { + auto previous = me->download_window; + me->download_window = me->download_window.next(); + LOG(OrderMan, Info, "{} advancing download window from {} to {}", + me->descriptor, previous, me->download_window); +} + +void DownloadCommonStateMachine::wait_download_window() { + transition(State::W_WINDOW); +} + +void DownloadCommonStateMachine::transition(State new_state) { + LOG(OrderMan, Info, "Transitioning {} from {} to {}", me->descriptor, + state_name(me->state), state_name(new_state)); + me->state = new_state; +} + +std::string DownloadCommonStateMachine::state_name(State st) const { + switch (st) { + case State::W_ADMIN: + return "W_ADMIN"; + case State::DOWNLOADING: + return "DOWNLOADING"; + case State::PAUSED: + return "PAUSED"; + case State::W_WINDOW: + return "W_WINDOW"; + } + bts_assert(false); + return ""; +} + +void DownloadCommonStateMachine::pause() { + me->last_download_attempt_ts = now(); + transition(State::PAUSED); +} + +bool DownloadCommonStateMachine::pause_over() const { + auto secs_to_pause = + config::get().pause_between_downloads_seconds(); + return (now() - me->last_download_attempt_ts).total_seconds() >= + (int64_t)secs_to_pause; +} + +} // namespace position +} // namespace bts diff --git a/src/server/sparkd/position/import/DownloadCommonStateMachine.h b/src/server/sparkd/position/import/DownloadCommonStateMachine.h new file mode 100644 index 000000000..d6df9cc81 --- /dev/null +++ b/src/server/sparkd/position/import/DownloadCommonStateMachine.h @@ -0,0 +1,56 @@ +#pragma once + +#include +#include +#include +#include +#include +#include "lib/utils/datetime.h" + +namespace bts { +namespace api { +namespace admin { +class AdminData; +} +} // namespace api +namespace position { + +using FilePicker = + std::function(std::vector const &)>; +using FileHandler = std::function( + std::string const &sftp_file_path, std::string const &local_file_path)>; +using ThreadMarshaller = std::function)>; + +class DownloadCommonStateMachine { + public: + DownloadCommonStateMachine( + std::string const&state_machine_descriptor, + FilePicker const &, FileHandler const &, + ThreadMarshaller const&); + + ~DownloadCommonStateMachine(); + void on_admin(std::shared_ptr const &); + void on_timer(); + void on_post_handle_failure(std::string const &msg); + + private: + enum class State; + struct PImpl; + static datetime::PTime now() ; + std::unique_ptr me; + + void start_download(); + + void on_download_failure(std::string const &); + void on_download_complete(std::string const &, std::string const &); + void pause(); + bool pause_over() const ; + void wait_download_window(); + void advance_download_window(); + + void transition(State new_state); + std::string state_name(State) const; +}; + +} // namespace position +} // namespace bts diff --git a/src/server/sparkd/position/import/DownloadWindow.cpp b/src/server/sparkd/position/import/DownloadWindow.cpp new file mode 100644 index 000000000..64d963fed --- /dev/null +++ b/src/server/sparkd/position/import/DownloadWindow.cpp @@ -0,0 +1,80 @@ +#include "server/sparkd/position/import/DownloadWindow.h" +#include "lib/config/config.h" +#include "lib/config/protos/position_cfg.pb.h" +#include "lib/utils/bts_assert.h" + +#include + +namespace bts { +namespace position { + +using namespace boost::posix_time; + +DownloadWindow::DownloadWindow(const DownloadWindow &src) + : _start(src.start()), _stop(src.stop()) {} + +DownloadWindow ::DownloadWindow(datetime::PTime now, time_duration start_time, + time_duration stop_time) { + bts_assert(start_time != stop_time); + + auto now_time = now.time_of_day(); + auto today = now.date(); + auto tomorrow = today + boost::gregorian::date_duration(1); + auto yesterday = today - boost::gregorian::date_duration(1); + + if (start_time < stop_time) { + // The window opens and closes in the same day + + // are we past the end of the window? + if (now_time > stop_time) { + // Today's window has closed and will only open up again tomorrow + _start = datetime::PTime(tomorrow, start_time); + _stop = datetime::PTime(tomorrow, stop_time); + } else { + // Today's window is either open now or will open later + // today + _start = datetime::PTime(today, start_time); + _stop = datetime::PTime(today, stop_time); + } + } else { + // The window opens one day and closes the next + + if (now_time < stop_time) { + // Yesterdays window is still open, it close today + _start = datetime::PTime(yesterday, start_time); + _stop = datetime::PTime(now.date(), stop_time); + } else { + // Today's window is either open now or will open later + // today (and close tomorrow) + _start = datetime::PTime(today, start_time); + _stop = datetime::PTime(tomorrow, stop_time); + } + } + bts_assert(_start < _stop); +} + +DownloadWindow::DownloadWindow(datetime::PTime const &start, + datetime::PTime const &stop) + : _start(start), _stop(stop) { + bts_assert(_start < _stop); +} + +DownloadWindow DownloadWindow::next() const { + return DownloadWindow(_start + boost::gregorian::date_duration(1), + _stop + boost::gregorian::date_duration(1)); +} + +bool DownloadWindow::is_inside(datetime::PTime const &time) const { + return time >= _start and time <= _stop; +} + +datetime::PTime DownloadWindow::start() const { return _start; } +datetime::PTime DownloadWindow::stop() const { return _stop; } + +std::ostream &operator<<(std::ostream &out, DownloadWindow const &w) { + out << "[ " << w.start() << " ... " << w.stop() << " ]"; + return out; +} + +} // namespace position +} // namespace bts diff --git a/src/server/sparkd/position/import/DownloadWindow.h b/src/server/sparkd/position/import/DownloadWindow.h new file mode 100644 index 000000000..2faa5648d --- /dev/null +++ b/src/server/sparkd/position/import/DownloadWindow.h @@ -0,0 +1,27 @@ +#pragma once + +#include "lib/utils/datetime.h" +#include + +namespace bts { +namespace position { + +class DownloadWindow { +public: + DownloadWindow (const DownloadWindow&); + DownloadWindow(datetime::PTime now, + boost::posix_time::time_duration window_begin, + boost::posix_time::time_duration window_end); + DownloadWindow(datetime::PTime const &, datetime::PTime const &); + DownloadWindow next() const ; + bool is_inside(datetime::PTime const &time) const ; + datetime::PTime start() const; + datetime::PTime stop() const; +private: + datetime::PTime _start; + datetime::PTime _stop; +}; + +std::ostream& operator<<(std::ostream &,DownloadWindow const&); + +}} diff --git a/src/server/sparkd/position/import/MoneyFileServiceServer.cpp b/src/server/sparkd/position/import/MoneyFileServiceServer.cpp new file mode 100644 index 000000000..3b1d96465 --- /dev/null +++ b/src/server/sparkd/position/import/MoneyFileServiceServer.cpp @@ -0,0 +1,192 @@ +#include "MoneyFileServiceServer.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/config/config.h" +#include "lib/config/protos/position_cfg.pb.h" +#include "lib/config/protos/orders_fills_cleanup.pb.h" +#include "lib/logging/logging.h" +#include "lib/utils/PBUtils.h" +#include "lib/utils/bts_assert.h" +#include "lib/utils/datetime.h" +#include "server/sparkd/position/import/parsing/money_parser.h" +#include "server/sparkd/position/import/DownloadCommonStateMachine.h" +#include "lib/api/service_helpers/AdminServiceUtils.h" + +#include +#include +#include +#include + +namespace bts { +namespace position { + +struct MoneyFileServiceServer::PImpl { + PImpl(MoneyFileServiceServer &owner, + bts::api::ServiceContext &ctx) + : ctx(ctx), + admin_service(ctx.locate()), + margin_service(ctx.locate()), + download_sm("Money file downloader", + + // File picker + [&owner] (auto const &candidates) { + return owner.pick_most_recent_money_file(candidates); + }, + + // Download complete handler + [&owner] (auto const&sftp_file, auto const&local_file) { + return owner.on_download_complete(sftp_file,local_file); + }, + + // Thread Marshaller + [&ctx] (auto const&action) { + ctx.post(action); + }), + admin_data() + {} + + bts::api::ServiceContext &ctx; + api::admin::AdminService admin_service; + api::position::MarginService margin_service; + DownloadCommonStateMachine download_sm; + std::shared_ptr admin_data; + std::map all_clearing_accounts; +}; + +MoneyFileServiceServer::MoneyFileServiceServer( + bts::api::ServiceContext &ctx) + : MoneyFileServiceBase(ctx), me(new PImpl(*this,ctx)) { + auto const &c = config::get(); + + if (c.sftp_host().size() && !c.disable_money_download()) { + LOG(OrderMan, Info, "MoneyFileServiceServer is active."); + + on_timer(); + me->admin_service.subscribe_admin_data() | + rxcpp::operators::subscribe( + [this](auto const &update) { + bts_assert(update.all().size() == 1); + + auto first_update = !me->admin_data; + me->admin_data = update.all()[0]; + auto old_clearing_accounts = std::move(me->all_clearing_accounts); + me->all_clearing_accounts = + admin::AdminServiceUtils::get_clearing_account_map(*me->admin_data); + + if (first_update || + // this is necessary to break the cycle money + // file change -> margin -> admin -> money file + // change + old_clearing_accounts != me->all_clearing_accounts) + me->download_sm.on_admin(me->admin_data); + }); + } else + LOG(OrderMan, Warn, + "Money file import is disabled, since no host is specified in the " + "GMI_Import.sftp_host config key"); +} + +MoneyFileServiceServer::~MoneyFileServiceServer() {} + +void MoneyFileServiceServer::parse_money_csv( + const api::position::Money_CSVBlock &b, + const api::Response &reply) { + + api::position::Money_ParseResult result; + if (!me->admin_data) { + *result.mutable_failure() = "Server is not ready"; + reply(result); + return; + } + + std::istringstream in{b.text()}; + auto parse_result = parse_money(*me->admin_data, in); + + if (!std::holds_alternative( + parse_result)) { + *result.mutable_failure() = std::get(parse_result); + reply(result); + return; + } + + auto cash_balance = + std::get(parse_result); + *result.mutable_success() = cash_balance; + reply(result); + return; +} + +void MoneyFileServiceServer::on_timer() { + me->ctx.post([this]() { on_timer(); }, 7000); + me->download_sm.on_timer(); +} + +std::optional MoneyFileServiceServer::on_download_complete( + std::string const &sftp_file_path, std::string const &local_file_path) { + + LOG(OrderMan, Warn, + "Successfully downloaded money file {} to local file " + "{}. Will parse next.", + sftp_file_path, local_file_path); + + auto parse_result = parse_money_file(local_file_path); + if (!std::holds_alternative( + parse_result)) { + auto error_msg = + fmt::format("Failed to parse money file at {}. Error msg: {}", + local_file_path, std::get(parse_result)); + return error_msg; + } + + auto cash_balance = + std::get(parse_result); + + LOG(OrderMan, Warn, + "Successfully parsed money file at {}. Extracted {} records", + local_file_path, cash_balance.cash_balance_size()); + + for (auto const &cb : cash_balance.cash_balance()) + LOG(OrderMan, Info, "Cash balance for account {} is {}", + cb.account_id(), cb.cash_balance()); + + me->margin_service.set_cash_balance_batch( + cash_balance, + [this](auto const &rr) { + if (!rr.success()) + on_money_set_failure(rr.error_message()); + }, + 5000); + return {}; +} + +void MoneyFileServiceServer::on_money_set_failure( + std::string const &error) { + LOG(OrderMan, Error, + "Failed to set cash balance through the margin service. The " + "following information is available: {}", + error); + me->download_sm.on_post_handle_failure(error); +} + +std::optional MoneyFileServiceServer::pick_most_recent_money_file( + std::vector const &candidates) { + + std::vector pos_files; + std::copy_if(candidates.begin(), candidates.end(), + std::back_inserter(pos_files), [](auto const &file_name) { + return boost::algorithm::starts_with(file_name, "btsmny_"); + }); + if (pos_files.empty()) + return {}; + std::sort(pos_files.begin(), pos_files.end()); + return pos_files.back(); +} + +std::variant +MoneyFileServiceServer::parse_money_file(std::string const &f) { + return position::parse_money(*me->admin_data, f); +} + +} // namespace position +} // namespace bts diff --git a/src/server/sparkd/position/import/MoneyFileServiceServer.h b/src/server/sparkd/position/import/MoneyFileServiceServer.h new file mode 100644 index 000000000..c21b6870c --- /dev/null +++ b/src/server/sparkd/position/import/MoneyFileServiceServer.h @@ -0,0 +1,51 @@ +#pragma once + +#include "lib/api/services/MoneyFileService.server.pb.h" +#include "lib/utils/datetime.h" +#include +#include + +namespace bts { +namespace api { +namespace position { +class MoneyFileBatch; +} +namespace admin { +class AdminData; +} +} // namespace api +} // namespace bts + +namespace bts { +namespace position { + +class DownloadWindow; + +class MoneyFileServiceServer + : public bts::api::position::MoneyFileServiceBase { + public: + MoneyFileServiceServer(bts::api::ServiceContext &ctx); + ~MoneyFileServiceServer(); + + void parse_money_csv( + const api::position::Money_CSVBlock &, + const api::Response &) override; + + private: + void on_timer(); + void on_admin(std::shared_ptr const &); + std::optional on_download_complete(std::string const &sftp_file_path, + std::string const &local_file_path); + void on_money_set_failure(std::string const &); + + std::optional + pick_most_recent_money_file(std::vector const &); + + std::variant + parse_money_file(std::string const &file_path); + + struct PImpl; + std::unique_ptr me; +}; +} // namespace position +} // namespace bts diff --git a/src/server/sparkd/position/import/docs/data/04/a77600-afc9-4929-ae2c-c68f32e137e7/sftp_credentials_encrypted.txt.gpg b/src/server/sparkd/position/import/docs/data/04/a77600-afc9-4929-ae2c-c68f32e137e7/sftp_credentials_encrypted.txt.gpg new file mode 100644 index 000000000..67c4ccc51 --- /dev/null +++ b/src/server/sparkd/position/import/docs/data/04/a77600-afc9-4929-ae2c-c68f32e137e7/sftp_credentials_encrypted.txt.gpg @@ -0,0 +1,2 @@ +Œ  JÐ>ͬŒpÿÒ¢¼à¾ßadߣ÷Cm•AVs4ÄÝCr\OÜ +輦%*(,W(DŠ–ùrúŠËZÎq^?~=jÉ=üÁ±&f M@T¶ÔXq—W¾ÑÆM«ÎÈ"OÂéà DOWNLOADING [label=admin] + DOWNLOADING -> W_WINDOW [label="download_success ||\n[out_w]download_failure"] + DOWNLOADING -> PAUSED [label="[in_w]download_failure"] + PAUSED -> DOWNLOADING [label="[timeout]time"] + W_WINDOW -> PAUSED [label="set_pos_failure ||\n admin ||\n[in_w]time"] + } +#+END_SRC + +#+RESULTS: +[[file:download_state_machine.pdf]] diff --git a/src/server/sparkd/position/import/docs/reference/2019-07-08.File_Layout_Money.pdf b/src/server/sparkd/position/import/docs/reference/2019-07-08.File_Layout_Money.pdf new file mode 100644 index 000000000..ea50b1431 Binary files /dev/null and b/src/server/sparkd/position/import/docs/reference/2019-07-08.File_Layout_Money.pdf differ diff --git a/src/server/sparkd/position/import/docs/reference/2019-07-08.File_Layout_Positions.pdf b/src/server/sparkd/position/import/docs/reference/2019-07-08.File_Layout_Positions.pdf new file mode 100644 index 000000000..d004bd287 Binary files /dev/null and b/src/server/sparkd/position/import/docs/reference/2019-07-08.File_Layout_Positions.pdf differ diff --git a/src/server/sparkd/position/import/docs/reference/GMI Mapping spreadsheet.xlsx b/src/server/sparkd/position/import/docs/reference/GMI Mapping spreadsheet.xlsx new file mode 100644 index 000000000..640b22809 Binary files /dev/null and b/src/server/sparkd/position/import/docs/reference/GMI Mapping spreadsheet.xlsx differ diff --git a/src/server/sparkd/position/import/docs/reference/Gmail - Money and Position files.pdf b/src/server/sparkd/position/import/docs/reference/Gmail - Money and Position files.pdf new file mode 100644 index 000000000..f39bdb98f Binary files /dev/null and b/src/server/sparkd/position/import/docs/reference/Gmail - Money and Position files.pdf differ diff --git a/src/server/sparkd/position/import/docs/reference/adatmny.docx b/src/server/sparkd/position/import/docs/reference/adatmny.docx new file mode 100644 index 000000000..a3fce38dd Binary files /dev/null and b/src/server/sparkd/position/import/docs/reference/adatmny.docx differ diff --git a/src/server/sparkd/position/import/docs/reference/adatpos.docx b/src/server/sparkd/position/import/docs/reference/adatpos.docx new file mode 100644 index 000000000..69bd33ee6 Binary files /dev/null and b/src/server/sparkd/position/import/docs/reference/adatpos.docx differ diff --git a/src/server/sparkd/position/import/docs/reference/btsmny_20190813.csv b/src/server/sparkd/position/import/docs/reference/btsmny_20190813.csv new file mode 100755 index 000000000..5cf9a086c Binary files /dev/null and b/src/server/sparkd/position/import/docs/reference/btsmny_20190813.csv differ diff --git a/src/server/sparkd/position/import/docs/reference/btspos_20190814.csv b/src/server/sparkd/position/import/docs/reference/btspos_20190814.csv new file mode 100755 index 000000000..c441e5bb9 --- /dev/null +++ b/src/server/sparkd/position/import/docs/reference/btspos_20190814.csv @@ -0,0 +1,36 @@ +CRECID,CFIRM,COFFIC,CACCT,CATYPE,CCUSIP,CCUSP2,CCTYM,CSBCUS,CSTYPE,CSUBTY,CSTYP2,CSTRIK,CEXPDT,CSUBAC,CDEAL,CTDATE,CTPRIC,CBS,CLOC,CBROKR,CSPRED,CTRACE,CICODE,CREFNO,CSWAC1,CSWAP1,CSWAS1,CSWAC2,CSWAP2,CSWAS2,CZONE,CACNTY,CCLASS,CSUBCL,CRR,CRISKG,CMEMBR,CSNAME,CSRCAC,CREFOF,CREFRR,COFFFN,COFFOF,COFFAC,COFFAT,CMPGMN,CQTEDT,CQTY,CPRTQ,CBUSTQ,CMAKEQ,CLEAVQ,CSEGQ,CTFRQ,CDLVYQ,CDLVYA,CSDSC#,CSDSC1,CSDSC2,CSDSC3,CTDSC#,CTDSC1,CTDSC2,CTDSC3,CMKVAL,CYSTMV,CMULTF,CSDATE,CEXRAT,CRXDAT,CIAD,CYIELD,CDCTCO,CDELTA,CLTDAT,CCTLNO,CPCNTY,CEXCH,CFC,CTYPE,CSYMBL,CPTYPE,CSUBEX,CTRDEX,CMATDT,CPRTPR,CBASIS,CCLOSE,CPRCDT,CPRTCP,CUNDCP,CPRTUC,CPRVCP,CTRPRC,CEXPR,CPRICE,CSPEDT,CTPEDT,CCURSY,CCURCD,CCALC,CCMNT1,CCMNT2,CCMNT3,CLEGND,COMNCD,CRTHT,CSCHDL,COC,CCOD,CUCUSP,CCVTBL,CEXBKR,COPBKR,COPFRM,CGIVIO,CGIVF#,CIEXNG,CIEFRM,CCARD,CORDER,CCHIT,CFEECL,CFEEEX,CCTI,CORDTY,CORDOR,CTIME,CTBKT,CSESSN,CSTINS,CTTYPE,CBARG1,CBARG2,CBARG3,CBARG4,CBARG5,CMTABL,CMINIT,CMVARN,CMUNTL,CMGNCV,CREL1F,CREL1O,CREL1A,CREL1T,CREL2F,CREL2O,CREL2A,CREL2T,CREL3F,CREL3O,CREL3A,CREL3T,CCOMM,CFEE1,CFEE2,CFEE3,CFEE4,CFEE5,CFEE6,CFEE7,CFEE8,CFEE9,CGICHG,CBKCHG,COTHER,CGROSS,CNET,CMBKOF,CMFLOR,CMORDR,CMWIRE,CMOTHR,CATCOM,CATFE1,CATFE2,CATFE3,CATFE4,CATFE5,CATFE6,CATFE7,CATFE8,CATFE9,CATGIV,CATBKG,CATOTH,CATBKO,CATFLR,CATORD,CATWIR,CATALO,CFDCOM,CFDFE1,CFDFE2,CFDFE3,CFDFE4,CFDFE5,CFDFE6,CFDFE7,CFDFE8,CFDFE9,CFDGIV,CFDBKG,CFDOTH,CFDBKO,CFDFLR,CFDORD,CFDWIR,CFDALO,CFUFE1,CFUFE2,CFUFE3,CFUFE4,CFUFE5,CFUFE6,CFUFE7,CFUFE8,CFUFE9,CFUGIV,CFUBKG,CFUOTH,CFUBKO,CFUFLR,CFUORD,CFUWIR,CFUALO,COVNQ,CDAYQ,CSCRQ,CSPDQ,COVNC,CDAYC,CSCRC,CSPDC,CLEVEL,CBATCH,CRECNO,CCLRID,CINPMO,CINPYR,CFBACC,CORIG,CCABCD,CSCHOV,CPGMID,CSCRID,CUSER,CWSID,CWSTIM,CDEST,CDELET,CXTRCT,CBQTY,CSQTY,CFDAM1,CAMT1,CFDAM2,CAMT2,CROUT1,CROUT2,CROUT3,CCURAC,CCURAT,CCCNTY +"P","Z","XYZ","12345","F1",".AKW0000019L"," ","201912"," ","F"," "," ",.00000000 ,0 ," "," ",20190813 ,4.02000000 ,"1"," "," ","S","9H13 DU6W0","100"," "," "," "," "," "," "," "," ","US","C"," ","ABCDE"," ","M"," "," "," "," "," "," "," "," "," ","0",2.0000 ," 2 ",.0000 ,.0000 ,.0000 ,.0000 ,.0000 ,.0000 ,.00 ,1 ,"DEC 19 KCBT RED WHEAT "," "," ",0 ," "," "," ",-125.00 ,.00 ,5000.0000000 ,20190813 ,1.00000000 ,0 ,.000 ,.00000000 ,.00000000 ,.0000 ,20191213 ," ","US","01","KW"," "," "," "," ","1A",0 ," 4.02 ",.00000000 ,4.00750000 ,20190814 ," 4.00 3/4",.00000000 ," ",4.00500000 ,.00000000 ," "," 0004020"," ","4","USD","US"," "," J"," "," "," "," ","H","FD"," "," "," F1",.00000000 ,"2H0L ","PKELL"," "," "," "," "," ","130L"," "," ","D","D","1"," "," ","130624"," "," "," ","6"," "," "," "," "," "," ",.00 ,.00 ,0 ,.00 ,"Z","XYZ","12345","M"," "," "," "," "," "," "," "," ",-.10 ,.00 ,-.70 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,"F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","M","C","E","N","X","Z"," "," "," "," "," ","B","T","Y","F","D","W","K","C","C","C","M","M","M","M","M","M"," ","C","C","M","M","M","M","M",.0000 ,.0000 ,.0000 ,2.0000 ,.00 ,.00 ,.00 ,-.10 ,"ATO","BAV",558632 ,"0165522483","&","19"," "," "," "," ","01","31","PKELLEY ","ATESDGMIMC",130637 ,"B"," ",0 ,2.0000 ,.0000 ," ",.00 ," ",.00 ,"A","A","A","USD","USD","US" +"P","Z","XYZ","12345","F1",".AKW0000019L"," ","201912"," ","F"," "," ",.00000000 ,0 ," "," ",20190813 ,4.02250000 ,"1"," "," ","S","9H13 DGVI0","100"," "," "," "," "," "," "," "," ","US","C"," ","ABCDE"," ","M"," "," "," "," "," "," "," "," "," ","0",2.0000 ," 2 ",.0000 ,.0000 ,.0000 ,.0000 ,.0000 ,.0000 ,.00 ,1 ,"DEC 19 KCBT RED WHEAT "," "," ",0 ," "," "," ",-150.00 ,.00 ,5000.0000000 ,20190813 ,1.00000000 ,0 ,.000 ,.00000000 ,.00000000 ,.0000 ,20191213 ," ","US","01","KW"," "," "," "," ","1A",0 ," 4.02 1/4",.00000000 ,4.00750000 ,20190814 ," 4.00 3/4",.00000000 ," ",4.00500000 ,.00000000 ," "," 0004022"," ","4","USD","US"," "," J"," "," "," "," ","H","FD"," "," "," F1",.00000000 ,"2H0L ","PKELL"," "," "," "," "," ","176R"," "," ","D","D","1"," "," ","112900"," "," "," ","6"," "," "," "," "," "," ",.00 ,.00 ,0 ,.00 ,"Z","XYZ","12345","M"," "," "," "," "," "," "," "," ",-.10 ,.00 ,-.70 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,"F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","M","C","E","N","X","Z"," "," "," "," "," ","B","T","Y","F","D","W","K","C","C","C","M","M","M","M","M","M"," ","C","C","M","M","M","M","M",.0000 ,.0000 ,.0000 ,2.0000 ,.00 ,.00 ,.00 ,-.10 ,"ATO","BAQ",558633 ,"0165506378","&","19"," "," "," "," ","01","31","PKELLEY ","ATESDGMIMC",112911 ,"B"," ",0 ,2.0000 ,.0000 ," ",.00 ," ",.00 ,"A","A","A","USD","USD","US" +"P","Z","XYZ","12345","F1",".AKW0000019L"," ","201912"," ","F"," "," ",.00000000 ,0 ," "," ",20190813 ,4.02500000 ,"1"," "," ","S","9H13 DGVR0","100"," "," "," "," "," "," "," "," ","US","C"," ","ABCDE"," ","M"," "," "," "," "," "," "," "," "," ","0",3.0000 ," 3 ",.0000 ,.0000 ,.0000 ,.0000 ,.0000 ,.0000 ,.00 ,1 ,"DEC 19 KCBT RED WHEAT "," "," ",0 ," "," "," ",-262.50 ,.00 ,5000.0000000 ,20190813 ,1.00000000 ,0 ,.000 ,.00000000 ,.00000000 ,.0000 ,20191213 ," ","US","01","KW"," "," "," "," ","1A",0 ," 4.02 1/2",.00000000 ,4.00750000 ,20190814 ," 4.00 3/4",.00000000 ," ",4.00500000 ,.00000000 ," "," 0004025"," ","4","USD","US"," "," J"," "," "," "," ","H","FD"," "," "," F1",.00000000 ,"2H0L ","PKELL"," "," "," "," "," ","176R"," "," ","D","D","1"," "," ","112907"," "," "," ","6"," "," "," "," "," "," ",.00 ,.00 ,0 ,.00 ,"Z","XYZ","12345","M"," "," "," "," "," "," "," "," ",-.15 ,.00 ,-1.05 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,"F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","M","C","E","N","X","Z"," "," "," "," "," ","B","T","Y","F","D","W","K","C","C","C","M","M","M","M","M","M"," ","C","C","M","M","M","M","M",.0000 ,.0000 ,.0000 ,3.0000 ,.00 ,.00 ,.00 ,-.15 ,"ATO","BAQ",558634 ,"0165506426","&","19"," "," "," "," ","01","31","PKELLEY ","ATESDGMIMC",112921 ,"B"," ",0 ,3.0000 ,.0000 ," ",.00 ," ",.00 ,"A","A","A","USD","USD","US" +"P","Z","XYZ","12345","F1",".AKW0000019L"," ","201912"," ","F"," "," ",.00000000 ,0 ," "," ",20190813 ,4.04500000 ,"1"," "," ","S","9H13 C1730","100"," "," "," "," "," "," "," "," ","US","C"," ","ABCDE"," ","M"," "," "," "," "," "," "," "," "," ","0",2.0000 ," 2 ",.0000 ,.0000 ,.0000 ,.0000 ,.0000 ,.0000 ,.00 ,1 ,"DEC 19 KCBT RED WHEAT "," "," ",0 ," "," "," ",-375.00 ,.00 ,5000.0000000 ,20190813 ,1.00000000 ,0 ,.000 ,.00000000 ,.00000000 ,.0000 ,20191213 ," ","US","01","KW"," "," "," "," ","1A",0 ," 4.04 1/2",.00000000 ,4.00750000 ,20190814 ," 4.00 3/4",.00000000 ," ",4.00500000 ,.00000000 ," "," 0004045"," ","4","USD","US"," "," J"," "," "," "," ","H","FD"," "," "," F1",.00000000 ,"2H0L ","PKELL"," "," "," "," "," ","12RX"," "," ","D","D","1"," "," ","102311"," "," "," ","6"," "," "," "," "," "," ",.00 ,.00 ,0 ,.00 ,"Z","XYZ","12345","M"," "," "," "," "," "," "," "," ",-.10 ,.00 ,-.70 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,"F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","M","C","E","N","X","Z"," "," "," "," "," ","B","T","Y","F","D","W","K","C","C","C","M","M","M","M","M","M"," ","C","C","M","M","M","M","M",.0000 ,.0000 ,.0000 ,2.0000 ,.00 ,.00 ,.00 ,-.10 ,"ATO","BAN",558635 ,"0165490446","&","19"," "," "," "," ","01","31","PKELLEY ","ATESDGMIMC",102327 ,"B"," ",0 ,2.0000 ,.0000 ," ",.00 ," ",.00 ,"A","A","A","USD","USD","US" +"P","Z","XYZ","12345","F1",".AKW0000019L"," ","201912"," ","F"," "," ",.00000000 ,0 ," "," ",20190813 ,4.05000000 ,"1"," "," ","S","9H13 C8IC0","100"," "," "," "," "," "," "," "," ","US","C"," ","ABCDE"," ","M"," "," "," "," "," "," "," "," "," ","0",10.0000 ," 10 ",.0000 ,.0000 ,.0000 ,.0000 ,.0000 ,.0000 ,.00 ,1 ,"DEC 19 KCBT RED WHEAT "," "," ",0 ," "," "," ",-2125.00 ,.00 ,5000.0000000 ,20190813 ,1.00000000 ,0 ,.000 ,.00000000 ,.00000000 ,.0000 ,20191213 ," ","US","01","KW"," "," "," "," ","1A",0 ," 4.05 ",.00000000 ,4.00750000 ,20190814 ," 4.00 3/4",.00000000 ," ",4.00500000 ,.00000000 ," "," 0004050"," ","4","USD","US"," "," J"," "," "," "," ","H","FD"," "," "," F1",.00000000 ,"2H0L ","PKELL"," "," "," "," "," ","170F"," "," ","D","D","1"," "," ","104804"," "," "," ","6"," "," "," "," "," "," ",.00 ,.00 ,0 ,.00 ,"Z","XYZ","12345","M"," "," "," "," "," "," "," "," ",-.50 ,.00 ,-3.50 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,"F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","M","C","E","N","X","Z"," "," "," "," "," ","B","T","Y","F","D","W","K","C","C","C","M","M","M","M","M","M"," ","C","C","M","M","M","M","M",.0000 ,.0000 ,.0000 ,10.0000 ,.00 ,.00 ,.00 ,-.50 ,"ATO","BAQ",558636 ,"0165497557","&","19"," "," "," "," ","01","31","PKELLEY ","ATESDGMIMC",104817 ,"B"," ",0 ,10.0000 ,.0000 ," ",.00 ," ",.00 ,"A","A","A","USD","USD","US" +"P","Z","XYZ","12345","F1",".AKW0000019L"," ","201912"," ","F"," "," ",.00000000 ,0 ," "," ",20190813 ,4.05250000 ,"1"," "," ","S","9H13 CWT60","100"," "," "," "," "," "," "," "," ","US","C"," ","ABCDE"," ","M"," "," "," "," "," "," "," "," "," ","0",2.0000 ," 2 ",.0000 ,.0000 ,.0000 ,.0000 ,.0000 ,.0000 ,.00 ,1 ,"DEC 19 KCBT RED WHEAT "," "," ",0 ," "," "," ",-450.00 ,.00 ,5000.0000000 ,20190813 ,1.00000000 ,0 ,.000 ,.00000000 ,.00000000 ,.0000 ,20191213 ," ","US","01","KW"," "," "," "," ","1A",0 ," 4.05 1/4",.00000000 ,4.00750000 ,20190814 ," 4.00 3/4",.00000000 ," ",4.00500000 ,.00000000 ," "," 0004052"," ","4","USD","US"," "," J"," "," "," "," ","H","FD"," "," "," F1",.00000000 ,"2H0L ","PKELL"," "," "," "," "," ","16TB"," "," ","D","D","1"," "," ","100946"," "," "," ","6"," "," "," "," "," "," ",.00 ,.00 ,0 ,.00 ,"Z","XYZ","12345","M"," "," "," "," "," "," "," "," ",-.10 ,.00 ,-.70 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,"F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","M","C","E","N","X","Z"," "," "," "," "," ","B","T","Y","F","D","W","K","C","C","C","M","M","M","M","M","M"," ","C","C","M","M","M","M","M",.0000 ,.0000 ,.0000 ,2.0000 ,.00 ,.00 ,.00 ,-.10 ,"ATO","BAN",558637 ,"0165484943","&","19"," "," "," "," ","01","31","PKELLEY ","ATESDGMIMC",101000 ,"B"," ",0 ,2.0000 ,.0000 ," ",.00 ," ",.00 ,"A","A","A","USD","USD","US" +"P","Z","XYZ","12345","F1",".AKW0000019L"," ","201912"," ","F"," "," ",.00000000 ,0 ," "," ",20190813 ,4.05500000 ,"1"," "," ","S","9H13 CYSQ0","100"," "," "," "," "," "," "," "," ","US","C"," ","ABCDE"," ","M"," "," "," "," "," "," "," "," "," ","0",2.0000 ," 2 ",.0000 ,.0000 ,.0000 ,.0000 ,.0000 ,.0000 ,.00 ,1 ,"DEC 19 KCBT RED WHEAT "," "," ",0 ," "," "," ",-475.00 ,.00 ,5000.0000000 ,20190813 ,1.00000000 ,0 ,.000 ,.00000000 ,.00000000 ,.0000 ,20191213 ," ","US","01","KW"," "," "," "," ","1A",0 ," 4.05 1/2",.00000000 ,4.00750000 ,20190814 ," 4.00 3/4",.00000000 ," ",4.00500000 ,.00000000 ," "," 0004055"," ","4","USD","US"," "," J"," "," "," "," ","H","FD"," "," "," F1",.00000000 ,"2H0L ","PKELL"," "," "," "," "," ","12RV"," "," ","D","D","1"," "," ","101311"," "," "," ","6"," "," "," "," "," "," ",.00 ,.00 ,0 ,.00 ,"Z","XYZ","12345","M"," "," "," "," "," "," "," "," ",-.10 ,.00 ,-.70 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,"F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","M","C","E","N","X","Z"," "," "," "," "," ","B","T","Y","F","D","W","K","C","C","C","M","M","M","M","M","M"," ","C","C","M","M","M","M","M",.0000 ,.0000 ,.0000 ,2.0000 ,.00 ,.00 ,.00 ,-.10 ,"ATO","BAN",558638 ,"0165486540","&","19"," "," "," "," ","01","31","PKELLEY ","ATESDGMIMC",101326 ,"B"," ",0 ,2.0000 ,.0000 ," ",.00 ," ",.00 ,"A","A","A","USD","USD","US" +"P","Z","XYZ","12345","F1",".AKW0000019L"," ","201912"," ","F"," "," ",.00000000 ,0 ," "," ",20190813 ,4.06750000 ,"1"," "," ","S","9H13 CVV00","100"," "," "," "," "," "," "," "," ","US","C"," ","ABCDE"," ","M"," "," "," "," "," "," "," "," "," ","0",4.0000 ," 4 ",.0000 ,.0000 ,.0000 ,.0000 ,.0000 ,.0000 ,.00 ,1 ,"DEC 19 KCBT RED WHEAT "," "," ",0 ," "," "," ",-1200.00 ,.00 ,5000.0000000 ,20190813 ,1.00000000 ,0 ,.000 ,.00000000 ,.00000000 ,.0000 ,20191213 ," ","US","01","KW"," "," "," "," ","1A",0 ," 4.06 3/4",.00000000 ,4.00750000 ,20190814 ," 4.00 3/4",.00000000 ," ",4.00500000 ,.00000000 ," "," 0004067"," ","4","USD","US"," "," J"," "," "," "," ","H","FD"," "," "," F1",.00000000 ,"2H0L ","PKELL"," "," "," "," "," ","16TJ"," "," ","D","D","1"," "," ","100710"," "," "," ","6"," "," "," "," "," "," ",.00 ,.00 ,0 ,.00 ,"Z","XYZ","12345","M"," "," "," "," "," "," "," "," ",-.20 ,.00 ,-1.40 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,"F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","M","C","E","N","X","Z"," "," "," "," "," ","B","T","Y","F","D","W","K","C","C","C","M","M","M","M","M","M"," ","C","C","M","M","M","M","M",.0000 ,.0000 ,.0000 ,4.0000 ,.00 ,.00 ,.00 ,-.20 ,"ATO","BAN",558639 ,"0165483857","&","19"," "," "," "," ","01","31","PKELLEY ","ATESDGMIMC",100721 ,"B"," ",0 ,4.0000 ,.0000 ," ",.00 ," ",.00 ,"A","A","A","USD","USD","US" +"P","Z","XYZ","12345","F1",".AKW0000019L"," ","201912"," ","F"," "," ",.00000000 ,0 ," "," ",20190814 ,3.97750000 ,"1"," "," ","S","9H14 BPLJ0","100"," "," "," "," "," "," "," "," ","US","C"," ","ABCDE"," ","M"," "," "," "," "," "," "," "," "," ","0",2.0000 ," 2 ",.0000 ,.0000 ,.0000 ,.0000 ,.0000 ,.0000 ,.00 ,1 ,"DEC 19 KCBT RED WHEAT "," "," ",0 ," "," "," ",300.00 ,.00 ,5000.0000000 ,20190814 ,1.00000000 ,0 ,.000 ,.00000000 ,.00000000 ,.0000 ,20191213 ," ","US","01","KW"," "," "," "," ","1A",0 ," 3.97 3/4",.00000000 ,4.00750000 ,20190814 ," 4.00 3/4",.00000000 ," ",3.97750000 ,.00000000 ," "," 0003977"," ","4","USD","US"," "," J"," "," "," "," ","H","FD"," "," "," F1",.00000000 ,"2H0L ","PKELL"," "," "," "," "," ","1986"," "," ","D","D","1"," "," ","084311"," "," "," ","6"," "," "," "," "," "," ",.00 ,.00 ,0 ,.00 ,"Z","XYZ","12345","M"," "," "," "," "," "," "," "," ",-.10 ,.00 ,-.70 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,"F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","M","C","E","N","X","Z"," "," "," "," "," ","B","T","Y","F","D","W","K","C","C","C","M","M","M","M","M","M"," ","C","C","M","M","M","M","M",.0000 ,2.0000 ,.0000 ,.0000 ,.00 ,-.10 ,.00 ,.00 ,"ATO","BAL",558640 ,"0165600515","&","19"," "," "," "," ","01","31","PKELLEY ","ATESDGMIMC",84326 ,"B"," ",0 ,2.0000 ,.0000 ," ",.00 ," ",.00 ,"A","A","A","USD","USD","US" +"P","Z","XYZ","12345","F1",".AKW0000019L"," ","201912"," ","F"," "," ",.00000000 ,0 ," "," ",20190814 ,3.98750000 ,"1"," "," ","S","9H14 BNKF0","100"," "," "," "," "," "," "," "," ","US","C"," ","ABCDE"," ","M"," "," "," "," "," "," "," "," "," ","0",3.0000 ," 3 ",.0000 ,.0000 ,.0000 ,.0000 ,.0000 ,.0000 ,.00 ,1 ,"DEC 19 KCBT RED WHEAT "," "," ",0 ," "," "," ",300.00 ,.00 ,5000.0000000 ,20190814 ,1.00000000 ,0 ,.000 ,.00000000 ,.00000000 ,.0000 ,20191213 ," ","US","01","KW"," "," "," "," ","1A",0 ," 3.98 3/4",.00000000 ,4.00750000 ,20190814 ," 4.00 3/4",.00000000 ," ",3.98750000 ,.00000000 ," "," 0003987"," ","4","USD","US"," "," J"," "," "," "," ","H","FD"," "," "," F1",.00000000 ,"2H0L ","PKELL"," "," "," "," "," ","1968"," "," ","D","D","1"," "," ","083703"," "," "," ","6"," "," "," "," "," "," ",.00 ,.00 ,0 ,.00 ,"Z","XYZ","12345","M"," "," "," "," "," "," "," "," ",-.15 ,.00 ,-1.05 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,"F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","M","C","E","N","X","Z"," "," "," "," "," ","B","T","Y","F","D","W","K","C","C","C","M","M","M","M","M","M"," ","C","C","M","M","M","M","M",.0000 ,3.0000 ,.0000 ,.0000 ,.00 ,-.15 ,.00 ,.00 ,"ATO","BAK",558641 ,"0165598119","&","19"," "," "," "," ","01","31","PKELLEY ","ATESDGMIMC",83718 ,"B"," ",0 ,3.0000 ,.0000 ," ",.00 ," ",.00 ,"A","A","A","USD","USD","US" +"P","Z","XYZ","12345","F1",".AKW0000019L"," ","201912"," ","F"," "," ",.00000000 ,0 ," "," ",20190814 ,4.03250000 ,"1"," "," ","S","9H14 C6PO0","100"," "," "," "," "," "," "," "," ","US","C"," ","ABCDE"," ","M"," "," "," "," "," "," "," "," "," ","0",3.0000 ," 3 ",.0000 ,.0000 ,.0000 ,.0000 ,.0000 ,.0000 ,.00 ,1 ,"DEC 19 KCBT RED WHEAT "," "," ",0 ," "," "," ",-375.00 ,.00 ,5000.0000000 ,20190814 ,1.00000000 ,0 ,.000 ,.00000000 ,.00000000 ,.0000 ,20191213 ," ","US","01","KW"," "," "," "," ","1A",0 ," 4.03 1/4",.00000000 ,4.00750000 ,20190814 ," 4.00 3/4",.00000000 ," ",4.03250000 ,.00000000 ," "," 0004032"," ","4","USD","US"," "," J"," "," "," "," ","H","FD"," "," "," F1",.00000000 ,"2H0L ","PKELL"," "," "," "," "," ","1AH0"," "," ","D","D","1"," "," ","123926"," "," "," ","6"," "," "," "," "," "," ",.00 ,.00 ,0 ,.00 ,"Z","XYZ","12345","M"," "," "," "," "," "," "," "," ",-.15 ,.00 ,-1.05 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,"F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","M","C","E","N","X","Z"," "," "," "," "," ","B","T","Y","F","D","W","K","C","C","C","M","M","M","M","M","M"," ","C","C","M","M","M","M","M",.0000 ,3.0000 ,.0000 ,.0000 ,.00 ,-.15 ,.00 ,.00 ,"ATO","BAU",558642 ,"0165659478","&","19"," "," "," "," ","01","31","PKELLEY ","ATESDGMIMC",123940 ,"B"," ",0 ,3.0000 ,.0000 ," ",.00 ," ",.00 ,"A","A","A","USD","USD","US" +"P","Z","XYZ","12345","F1",".A010000019L"," ","201912"," ","F"," "," ",.00000000 ,0 ," "," ",20190814 ,4.77000000 ,"2"," "," ","S","9H14 BLRQ0","100"," "," 0.0 "," "," "," "," "," "," ","US","C"," ","ABCDE"," ","M"," "," "," "," "," "," "," "," "," ","0",32.0000 ," 32-",.0000 ,.0000 ,.0000 ,.0000 ,.0000 ,.0000 ,.00 ,1 ,"DEC 19 WHEAT "," "," ",0 ," "," "," ",-2000.00 ,.00 ,5000.0000000 ,20190814 ,1.00000000 ,0 ,.000 ,.00000000 ,.00000000 ,.0000 ,20191213 ," ","US","01","01"," "," "," "," ","1A",0 ," 4.77 ",.00000000 ,4.78250000 ,20190814 ," 4.78 1/4",.00000000 ," ",4.77000000 ,.00000000 ," "," 0004770"," ","4","USD","US"," "," J"," "," "," "," ","H","FD"," "," "," F1",.00000000 ,"2H0L ","PKELL"," "," "," "," "," ","13GG"," "," ","D","D","1"," "," ","083241"," "," "," ","D"," "," "," "," "," "," ",.00 ,.00 ,0 ,.00 ,"Z","XYZ","12345","M"," "," "," "," "," "," "," "," ",-1.60 ,.00 ,-11.20 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,"F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","M","C","E","N","X","Z"," "," "," "," "," ","B","T","Y","F","D","W","K","C","C","C","M","M","M","M","M","M"," ","C","C","M","M","M","M","M",.0000 ,.0000 ,32.0000 ,.0000 ,.00 ,.00 ,-1.60 ,.00 ,"ATO","BAK",558643 ,"0165596192","&","19"," "," "," "," ","01","31","PKELLEY ","ATESDGMIMC",83306 ,"B"," ",0 ,.0000 ,32.0000 ," ",.00 ," ",.00 ,"A","A","A","USD","USD","US" +"P","Z","XYZ","12345","F1",".A010000019L"," ","201912"," ","F"," "," ",.00000000 ,0 ," "," ",20190814 ,4.78250000 ,"2"," "," ","S","9H14 CUH20","100"," "," -0.0625"," "," "," "," "," "," ","US","C"," ","ABCDE"," ","M"," "," "," "," "," "," "," "," "," ","0",25.0000 ," 25-",.0000 ,.0000 ,.0000 ,.0000 ,.0000 ,.0000 ,.00 ,1 ,"DEC 19 WHEAT "," "," ",0 ," "," "," ",.00 ,.00 ,5000.0000000 ,20190814 ,1.00000000 ,0 ,.000 ,.00000000 ,.00000000 ,.0000 ,20191213 ," ","US","01","01"," "," "," "," ","1A",0 ," 4.78 1/4",.00000000 ,4.78250000 ,20190814 ," 4.78 1/4",.00000000 ," ",4.78250000 ,.00000000 ," "," 0004782"," ","4","USD","US"," "," J"," "," "," "," ","H","FD"," "," "," F1",.00000000 ,"2H0L ","PKELL"," "," "," "," "," ","1A06"," "," ","D","D","1"," "," ","112640"," "," "," ","D"," "," "," "," "," "," ",.00 ,.00 ,0 ,.00 ,"Z","XYZ","12345","M"," "," "," "," "," "," "," "," ",-1.25 ,.00 ,-8.75 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,"F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","M","C","E","N","X","Z"," "," "," "," "," ","B","T","Y","F","D","W","K","C","C","C","M","M","M","M","M","M"," ","C","C","M","M","M","M","M",.0000 ,.0000 ,2.0000 ,23.0000 ,.00 ,.00 ,-.10 ,-1.15 ,"ATO","BAR",558644 ,"0165645052","&","19"," "," "," "," ","01","31","PKELLEY ","ATESDGMIMC",112652 ,"B"," ",0 ,.0000 ,25.0000 ," ",.00 ," ",.00 ,"A","A","A","USD","USD","US" +"P","Z","XYZ","12345","F1",".A010000019L"," ","201912"," ","F"," "," ",.00000000 ,0 ," "," ",20190814 ,4.79000000 ,"2"," "," ","S","9H14 CZK60","100"," "," -0.0575"," "," "," "," "," "," ","US","C"," ","ABCDE"," ","M"," "," "," "," "," "," "," "," "," ","0",1.0000 ," 1-",.0000 ,.0000 ,.0000 ,.0000 ,.0000 ,.0000 ,.00 ,1 ,"DEC 19 WHEAT "," "," ",0 ," "," "," ",37.50 ,.00 ,5000.0000000 ,20190814 ,1.00000000 ,0 ,.000 ,.00000000 ,.00000000 ,.0000 ,20191213 ," ","US","01","01"," "," "," "," ","1A",0 ," 4.79 ",.00000000 ,4.78250000 ,20190814 ," 4.78 1/4",.00000000 ," ",4.79000000 ,.00000000 ," "," 0004790"," ","4","USD","US"," "," J"," "," "," "," ","H","FD"," "," "," F1",.00000000 ,"2H0L ","PKELL"," "," "," "," "," ","1A8Q"," "," ","D","D","1"," "," ","115617"," "," "," ","D"," "," "," "," "," "," ",.00 ,.00 ,0 ,.00 ,"Z","XYZ","12345","M"," "," "," "," "," "," "," "," ",-.05 ,.00 ,-.35 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,"F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","M","C","E","N","X","Z"," "," "," "," "," ","B","T","Y","F","D","W","K","C","C","C","M","M","M","M","M","M"," ","C","C","M","M","M","M","M",.0000 ,.0000 ,1.0000 ,.0000 ,.00 ,.00 ,-.05 ,.00 ,"ATO","BAU",558645 ,"0165651408","&","19"," "," "," "," ","01","31","PKELLEY ","ATESDGMIMC",115628 ,"B"," ",0 ,.0000 ,1.0000 ," ",.00 ," ",.00 ,"A","A","A","USD","USD","US" +"P","Z","XYZ","12345","F1",".A010000019L"," ","201912"," ","F"," "," ",.00000000 ,0 ," "," ",20190814 ,4.79250000 ,"2"," "," ","S","9H14 C71H0","100"," "," -0.06 "," "," "," "," "," "," ","US","C"," ","ABCDE"," ","M"," "," "," "," "," "," "," "," "," ","0",117.0000 ," 117-",.0000 ,.0000 ,.0000 ,.0000 ,.0000 ,.0000 ,.00 ,1 ,"DEC 19 WHEAT "," "," ",0 ," "," "," ",5850.00 ,.00 ,5000.0000000 ,20190814 ,1.00000000 ,0 ,.000 ,.00000000 ,.00000000 ,.0000 ,20191213 ," ","US","01","01"," "," "," "," ","1A",0 ," 4.79 1/4",.00000000 ,4.78250000 ,20190814 ," 4.78 1/4",.00000000 ," ",4.79250000 ,.00000000 ," "," 0004792"," ","4","USD","US"," "," J"," "," "," "," ","H","FD"," "," "," F1",.00000000 ,"2H0L ","PKELL"," "," "," "," "," ","1AJY"," "," ","D","D","1"," "," ","124928"," "," "," ","D"," "," "," "," "," "," ",.00 ,.00 ,0 ,.00 ,"Z","XYZ","12345","M"," "," "," "," "," "," "," "," ",-5.85 ,.00 ,-40.95 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,"F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","M","C","E","N","X","Z"," "," "," "," "," ","B","T","Y","F","D","W","K","C","C","C","M","M","M","M","M","M"," ","C","C","M","M","M","M","M",.0000 ,.0000 ,.0000 ,117.0000 ,.00 ,.00 ,.00 ,-5.85 ,"ATO","BAW",558646 ,"0165661097","&","19"," "," "," "," ","01","31","PKELLEY ","ATESDGMIMC",124941 ,"B"," ",0 ,.0000 ,117.0000 ," ",.00 ," ",.00 ,"A","A","A","USD","USD","US" +"P","Z","XYZ","12345","F1",".A010000019L"," ","201912"," ","F"," "," ",.00000000 ,0 ," "," ",20190814 ,4.79500000 ,"2"," "," ","S","9H14 C0E70","100"," "," -0.0575"," "," "," "," "," "," ","US","C"," ","ABCDE"," ","M"," "," "," "," "," "," "," "," "," ","0",7.0000 ," 7-",.0000 ,.0000 ,.0000 ,.0000 ,.0000 ,.0000 ,.00 ,1 ,"DEC 19 WHEAT "," "," ",0 ," "," "," ",437.50 ,.00 ,5000.0000000 ,20190814 ,1.00000000 ,0 ,.000 ,.00000000 ,.00000000 ,.0000 ,20191213 ," ","US","01","01"," "," "," "," ","1A",0 ," 4.79 1/2",.00000000 ,4.78250000 ,20190814 ," 4.78 1/4",.00000000 ," ",4.79500000 ,.00000000 ," "," 0004795"," ","4","USD","US"," "," J"," "," "," "," ","H","FD"," "," "," F1",.00000000 ,"2H0L ","PKELL"," "," "," "," "," ","1A8Q"," "," ","D","D","1"," "," ","120016"," "," "," ","D"," "," "," "," "," "," ",.00 ,.00 ,0 ,.00 ,"Z","XYZ","12345","M"," "," "," "," "," "," "," "," ",-.35 ,.00 ,-2.45 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,"F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","M","C","E","N","X","Z"," "," "," "," "," ","B","T","Y","F","D","W","K","C","C","C","M","M","M","M","M","M"," ","C","C","M","M","M","M","M",.0000 ,.0000 ,.0000 ,7.0000 ,.00 ,.00 ,.00 ,-.35 ,"ATO","BAU",558647 ,"0165652292","&","19"," "," "," "," ","01","31","PKELLEY ","ATESDGMIMC",120031 ,"B"," ",0 ,.0000 ,7.0000 ," ",.00 ," ",.00 ,"A","A","A","USD","USD","US" +"P","Z","XYZ","12345","F1",".A010000019L"," ","201912"," ","F"," "," ",.00000000 ,0 ," "," ",20190814 ,4.80250000 ,"2"," "," ","S","9H14 CII60","100"," "," 0.0 "," "," "," "," "," "," ","US","C"," ","ABCDE"," ","M"," "," "," "," "," "," "," "," "," ","0",50.0000 ," 50-",.0000 ,.0000 ,.0000 ,.0000 ,.0000 ,.0000 ,.00 ,1 ,"DEC 19 WHEAT "," "," ",0 ," "," "," ",5000.00 ,.00 ,5000.0000000 ,20190814 ,1.00000000 ,0 ,.000 ,.00000000 ,.00000000 ,.0000 ,20191213 ," ","US","01","01"," "," "," "," ","1A",0 ," 4.80 1/4",.00000000 ,4.78250000 ,20190814 ," 4.78 1/4",.00000000 ," ",4.80250000 ,.00000000 ," "," 0004802"," ","4","USD","US"," "," J"," "," "," "," ","H","FD"," "," "," F1",.00000000 ,"2H0L ","PKELL"," "," "," "," "," ","19RF"," "," ","D","D","1"," "," ","102110"," "," "," ","D"," "," "," "," "," "," ",.00 ,.00 ,0 ,.00 ,"Z","XYZ","12345","M"," "," "," "," "," "," "," "," ",-2.50 ,.00 ,-17.50 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,"F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","M","C","E","N","X","Z"," "," "," "," "," ","B","T","Y","F","D","W","K","C","C","C","M","M","M","M","M","M"," ","C","C","M","M","M","M","M",.0000 ,.0000 ,.0000 ,50.0000 ,.00 ,.00 ,.00 ,-2.50 ,"ATO","BAO",558648 ,"0165631592","&","19"," "," "," "," ","01","31","PKELLEY ","ATESDGMIMC",102124 ,"B"," ",0 ,.0000 ,50.0000 ," ",.00 ," ",.00 ,"A","A","A","USD","USD","US" +"P","Z","XYZ","12345","F1",".A010000019L"," ","201912"," ","F"," "," ",.00000000 ,0 ," "," ",20190814 ,4.80500000 ,"2"," "," ","S","9H14 C5NQ0","100"," "," "," "," "," "," "," "," ","US","C"," ","ABCDE"," ","M"," "," "," "," "," "," "," "," "," ","0",2.0000 ," 2-",.0000 ,.0000 ,.0000 ,.0000 ,.0000 ,.0000 ,.00 ,1 ,"DEC 19 WHEAT "," "," ",0 ," "," "," ",225.00 ,.00 ,5000.0000000 ,20190814 ,1.00000000 ,0 ,.000 ,.00000000 ,.00000000 ,.0000 ,20191213 ," ","US","01","01"," "," "," "," ","1A",0 ," 4.80 1/2",.00000000 ,4.78250000 ,20190814 ," 4.78 1/4",.00000000 ," ",4.80500000 ,.00000000 ," "," 0004805"," ","4","USD","US"," "," J"," "," "," "," ","H","FD"," "," "," F1",.00000000 ,"2H0L ","PKELL"," "," "," "," "," ","1AGZ"," "," ","D","D","1"," "," ","123215"," "," "," ","6"," "," "," "," "," "," ",.00 ,.00 ,0 ,.00 ,"Z","XYZ","12345","M"," "," "," "," "," "," "," "," ",-.10 ,.00 ,-.70 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,"F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","M","C","E","N","X","Z"," "," "," "," "," ","B","T","Y","F","D","W","K","C","C","C","M","M","M","M","M","M"," ","C","C","M","M","M","M","M",.0000 ,.0000 ,.0000 ,2.0000 ,.00 ,.00 ,.00 ,-.10 ,"ATO","BAU",558649 ,"0165658281","&","19"," "," "," "," ","01","31","PKELLEY ","ATESDGMIMC",123228 ,"B"," ",0 ,.0000 ,2.0000 ," ",.00 ," ",.00 ,"A","A","A","USD","USD","US" +"P","Z","XYZ","12345","F1",".A010000019L"," ","201912"," ","F"," "," ",.00000000 ,0 ," "," ",20190814 ,4.81000000 ,"2"," "," ","S","9H14 C6PP0","100"," "," "," "," "," "," "," "," ","US","C"," ","ABCDE"," ","M"," "," "," "," "," "," "," "," "," ","0",1.0000 ," 1-",.0000 ,.0000 ,.0000 ,.0000 ,.0000 ,.0000 ,.00 ,1 ,"DEC 19 WHEAT "," "," ",0 ," "," "," ",137.50 ,.00 ,5000.0000000 ,20190814 ,1.00000000 ,0 ,.000 ,.00000000 ,.00000000 ,.0000 ,20191213 ," ","US","01","01"," "," "," "," ","1A",0 ," 4.81 ",.00000000 ,4.78250000 ,20190814 ," 4.78 1/4",.00000000 ," ",4.81000000 ,.00000000 ," "," 0004810"," ","4","USD","US"," "," J"," "," "," "," ","H","FD"," "," "," F1",.00000000 ,"2H0L ","PKELL"," "," "," "," "," ","1AH0"," "," ","D","D","1"," "," ","123926"," "," "," ","6"," "," "," "," "," "," ",.00 ,.00 ,0 ,.00 ,"Z","XYZ","12345","M"," "," "," "," "," "," "," "," ",-.05 ,.00 ,-.35 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,"F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","M","C","E","N","X","Z"," "," "," "," "," ","B","T","Y","F","D","W","K","C","C","C","M","M","M","M","M","M"," ","C","C","M","M","M","M","M",.0000 ,.0000 ,.0000 ,1.0000 ,.00 ,.00 ,.00 ,-.05 ,"ATO","BAU",558650 ,"0165659479","&","19"," "," "," "," ","01","31","PKELLEY ","ATESDGMIMC",123940 ,"B"," ",0 ,.0000 ,1.0000 ," ",.00 ," ",.00 ,"A","A","A","USD","USD","US" +"P","Z","XYZ","12345","F1",".A010000020C"," ","202003"," ","F"," "," ",.00000000 ,0 ," "," ",20190814 ,4.84500000 ,"1"," "," ","S","9H14 CUH30","100"," "," -0.0625"," "," "," "," "," "," ","US","C"," ","ABCDE"," ","M"," "," "," "," "," "," "," "," "," ","0",25.0000 ," 25 ",.0000 ,.0000 ,.0000 ,.0000 ,.0000 ,.0000 ,.00 ,1 ,"MAR 20 WHEAT "," "," ",0 ," "," "," ",-625.00 ,.00 ,5000.0000000 ,20190814 ,1.00000000 ,0 ,.000 ,.00000000 ,.00000000 ,.0000 ,20200313 ," ","US","01","01"," "," "," "," ","1A",0 ," 4.84 1/2",.00000000 ,4.84000000 ,20190814 ," 4.84 ",.00000000 ," ",4.84500000 ,.00000000 ," "," 0004845"," ","4","USD","US"," "," J"," "," "," "," ","H","FD"," "," "," F1",.00000000 ,"2H0L ","PKELL"," "," "," "," "," ","1A06"," "," ","D","D","1"," "," ","112640"," "," "," ","D"," "," "," "," "," "," ",.00 ,.00 ,0 ,.00 ,"Z","XYZ","12345","M"," "," "," "," "," "," "," "," ",-1.25 ,.00 ,-8.75 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,"F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","M","C","E","N","X","Z"," "," "," "," "," ","B","T","Y","F","D","W","K","C","C","C","M","M","M","M","M","M"," ","C","C","M","M","M","M","M",.0000 ,.0000 ,.0000 ,25.0000 ,.00 ,.00 ,.00 ,-1.25 ,"ATO","BAR",558651 ,"0165645053","3","20"," "," "," "," ","01","31","PKELLEY ","ATESDGMIMC",112652 ,"B"," ",0 ,25.0000 ,.0000 ," ",.00 ," ",.00 ,"A","A","A","USD","USD","US" +"P","Z","XYZ","12345","F1",".A010000020C"," ","202003"," ","F"," "," ",.00000000 ,0 ," "," ",20190814 ,4.84750000 ,"1"," "," ","S","9H14 CZK70","100"," "," -0.0575"," "," "," "," "," "," ","US","C"," ","ABCDE"," ","M"," "," "," "," "," "," "," "," "," ","0",1.0000 ," 1 ",.0000 ,.0000 ,.0000 ,.0000 ,.0000 ,.0000 ,.00 ,1 ,"MAR 20 WHEAT "," "," ",0 ," "," "," ",-37.50 ,.00 ,5000.0000000 ,20190814 ,1.00000000 ,0 ,.000 ,.00000000 ,.00000000 ,.0000 ,20200313 ," ","US","01","01"," "," "," "," ","1A",0 ," 4.84 3/4",.00000000 ,4.84000000 ,20190814 ," 4.84 ",.00000000 ," ",4.84750000 ,.00000000 ," "," 0004847"," ","4","USD","US"," "," J"," "," "," "," ","H","FD"," "," "," F1",.00000000 ,"2H0L ","PKELL"," "," "," "," "," ","1A8Q"," "," ","D","D","1"," "," ","115617"," "," "," ","D"," "," "," "," "," "," ",.00 ,.00 ,0 ,.00 ,"Z","XYZ","12345","M"," "," "," "," "," "," "," "," ",-.05 ,.00 ,-.35 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,"F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","M","C","E","N","X","Z"," "," "," "," "," ","B","T","Y","F","D","W","K","C","C","C","M","M","M","M","M","M"," ","C","C","M","M","M","M","M",.0000 ,.0000 ,.0000 ,1.0000 ,.00 ,.00 ,.00 ,-.05 ,"ATO","BAU",558652 ,"0165651409","3","20"," "," "," "," ","01","31","PKELLEY ","ATESDGMIMC",115628 ,"B"," ",0 ,1.0000 ,.0000 ," ",.00 ," ",.00 ,"A","A","A","USD","USD","US" +"P","Z","XYZ","12345","F1",".A010000020C"," ","202003"," ","F"," "," ",.00000000 ,0 ," "," ",20190814 ,4.85000000 ,"1"," "," ","S","9H14 C0F80","100"," "," 0.0 "," "," "," "," "," "," ","US","C"," ","ABCDE"," ","M"," "," "," "," "," "," "," "," "," ","0",17.0000 ," 17 ",.0000 ,.0000 ,.0000 ,.0000 ,.0000 ,.0000 ,.00 ,1 ,"MAR 20 WHEAT "," "," ",0 ," "," "," ",-850.00 ,.00 ,5000.0000000 ,20190814 ,1.00000000 ,0 ,.000 ,.00000000 ,.00000000 ,.0000 ,20200313 ," ","US","01","01"," "," "," "," ","1A",0 ," 4.85 ",.00000000 ,4.84000000 ,20190814 ," 4.84 ",.00000000 ," ",4.85000000 ,.00000000 ," "," 0004850"," ","4","USD","US"," "," J"," "," "," "," ","H","FD"," "," "," F1",.00000000 ,"2H0L ","PKELL"," "," "," "," "," ","1A8Q"," "," ","D","D","1"," "," ","120032"," "," "," ","D"," "," "," "," "," "," ",.00 ,.00 ,0 ,.00 ,"Z","XYZ","12345","M"," "," "," "," "," "," "," "," ",-.85 ,.00 ,-5.95 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,"F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","M","C","E","N","X","Z"," "," "," "," "," ","B","T","Y","F","D","W","K","C","C","C","M","M","M","M","M","M"," ","C","C","M","M","M","M","M",.0000 ,.0000 ,.0000 ,17.0000 ,.00 ,.00 ,.00 ,-.85 ,"ATO","BAU",558653 ,"0165652330","3","20"," "," "," "," ","01","31","PKELLEY ","ATESDGMIMC",120047 ,"B"," ",0 ,17.0000 ,.0000 ," ",.00 ," ",.00 ,"A","A","A","USD","USD","US" +"P","Z","XYZ","12345","F1",".A010000020C"," ","202003"," ","F"," "," ",.00000000 ,0 ," "," ",20190814 ,4.85250000 ,"1"," "," ","S","9H14 C71I0","100"," "," -0.06 "," "," "," "," "," "," ","US","C"," ","ABCDE"," ","M"," "," "," "," "," "," "," "," "," ","0",57.0000 ," 57 ",.0000 ,.0000 ,.0000 ,.0000 ,.0000 ,.0000 ,.00 ,1 ,"MAR 20 WHEAT "," "," ",0 ," "," "," ",-3562.50 ,.00 ,5000.0000000 ,20190814 ,1.00000000 ,0 ,.000 ,.00000000 ,.00000000 ,.0000 ,20200313 ," ","US","01","01"," "," "," "," ","1A",0 ," 4.85 1/4",.00000000 ,4.84000000 ,20190814 ," 4.84 ",.00000000 ," ",4.85250000 ,.00000000 ," "," 0004852"," ","4","USD","US"," "," J"," "," "," "," ","H","FD"," "," "," F1",.00000000 ,"2H0L ","PKELL"," "," "," "," "," ","1AJY"," "," ","D","D","1"," "," ","124928"," "," "," ","D"," "," "," "," "," "," ",.00 ,.00 ,0 ,.00 ,"Z","XYZ","12345","M"," "," "," "," "," "," "," "," ",-2.85 ,.00 ,-19.95 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,"F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","M","C","E","N","X","Z"," "," "," "," "," ","B","T","Y","F","D","W","K","C","C","C","M","M","M","M","M","M"," ","C","C","M","M","M","M","M",.0000 ,.0000 ,.0000 ,57.0000 ,.00 ,.00 ,.00 ,-2.85 ,"ATO","BAW",558654 ,"0165661098","3","20"," "," "," "," ","01","31","PKELLEY ","ATESDGMIMC",124941 ,"B"," ",0 ,57.0000 ,.0000 ," ",.00 ," ",.00 ,"A","A","A","USD","USD","US" +"P","Z","XYZ","12345","F1",".A010000020C"," ","202003"," ","F"," "," ",.00000000 ,0 ," "," ",20190814 ,4.85750000 ,"1"," "," ","S","9H14 CMCO0","100"," "," -0.065 "," "," "," "," "," "," ","US","C"," ","ABCDE"," ","M"," "," "," "," "," "," "," "," "," ","0",50.0000 ," 50 ",.0000 ,.0000 ,.0000 ,.0000 ,.0000 ,.0000 ,.00 ,1 ,"MAR 20 WHEAT "," "," ",0 ," "," "," ",-4375.00 ,.00 ,5000.0000000 ,20190814 ,1.00000000 ,0 ,.000 ,.00000000 ,.00000000 ,.0000 ,20200313 ," ","US","01","01"," "," "," "," ","1A",0 ," 4.85 3/4",.00000000 ,4.84000000 ,20190814 ," 4.84 ",.00000000 ," ",4.85750000 ,.00000000 ," "," 0004857"," ","4","USD","US"," "," J"," "," "," "," ","H","FD"," "," "," F1",.00000000 ,"2H0L ","PKELL"," "," "," "," "," ","19UY"," "," ","D","D","1"," "," ","103928"," "," "," ","D"," "," "," "," "," "," ",.00 ,.00 ,0 ,.00 ,"Z","XYZ","12345","M"," "," "," "," "," "," "," "," ",-2.50 ,.00 ,-17.50 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,"F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","M","C","E","N","X","Z"," "," "," "," "," ","B","T","Y","F","D","W","K","C","C","C","M","M","M","M","M","M"," ","C","C","M","M","M","M","M",.0000 ,.0000 ,.0000 ,50.0000 ,.00 ,.00 ,.00 ,-2.50 ,"ATO","BAQ",558655 ,"0165635896","3","20"," "," "," "," ","01","31","PKELLEY ","ATESDGMIMC",103945 ,"B"," ",0 ,50.0000 ,.0000 ," ",.00 ," ",.00 ,"A","A","A","USD","USD","US" +"P","Z","XYZ","12345","F1",".A010000020C"," ","202003"," ","F"," "," ",.00000000 ,0 ," "," ",20190814 ,4.86750000 ,"1"," "," ","S","9H14 CII50","100"," "," 0.0 "," "," "," "," "," "," ","US","C"," ","ABCDE"," ","M"," "," "," "," "," "," "," "," "," ","0",50.0000 ," 50 ",.0000 ,.0000 ,.0000 ,.0000 ,.0000 ,.0000 ,.00 ,1 ,"MAR 20 WHEAT "," "," ",0 ," "," "," ",-6875.00 ,.00 ,5000.0000000 ,20190814 ,1.00000000 ,0 ,.000 ,.00000000 ,.00000000 ,.0000 ,20200313 ," ","US","01","01"," "," "," "," ","1A",0 ," 4.86 3/4",.00000000 ,4.84000000 ,20190814 ," 4.84 ",.00000000 ," ",4.86750000 ,.00000000 ," "," 0004867"," ","4","USD","US"," "," J"," "," "," "," ","H","FD"," "," "," F1",.00000000 ,"2H0L ","PKELL"," "," "," "," "," ","19RF"," "," ","D","D","1"," "," ","102110"," "," "," ","D"," "," "," "," "," "," ",.00 ,.00 ,0 ,.00 ,"Z","XYZ","12345","M"," "," "," "," "," "," "," "," ",-2.50 ,.00 ,-17.50 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,"F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","M","C","E","N","X","Z"," "," "," "," "," ","B","T","Y","F","D","W","K","C","C","C","M","M","M","M","M","M"," ","C","C","M","M","M","M","M",.0000 ,.0000 ,.0000 ,50.0000 ,.00 ,.00 ,.00 ,-2.50 ,"ATO","BAO",558656 ,"0165631591","3","20"," "," "," "," ","01","31","PKELLEY ","ATESDGMIMC",102124 ,"B"," ",0 ,50.0000 ,.0000 ," ",.00 ," ",.00 ,"A","A","A","USD","USD","US" +"P","Z","XYZ","12345","F1",".A050000019K"," ","201911"," ","F"," "," ",.00000000 ,0 ," "," ",20190813 ,8.88000000 ,"2"," "," ","S","9H13 DWKV0","100"," "," "," "," "," "," "," "," ","US","C"," ","ABCDE"," ","M"," "," "," "," "," "," "," "," "," ","0",3.0000 ," 3-",.0000 ,.0000 ,.0000 ,.0000 ,.0000 ,.0000 ,.00 ,1 ,"NOV 19 SOYBEANS "," "," ",0 ," "," "," ",1500.00 ,.00 ,5000.0000000 ,20190813 ,1.00000000 ,0 ,.000 ,.00000000 ,.00000000 ,.0000 ,20191114 ," ","US","01","05"," "," "," "," ","1A",0 ," 8.88 ",.00000000 ,8.78000000 ,20190814 ," 8.78 ",.00000000 ," ",8.89000000 ,.00000000 ," "," 0008880"," ","4","USD","US"," "," J"," "," "," "," ","H","FD"," "," "," F1",.00000000 ,"2H0L ","PKELL"," "," "," "," "," ","17PD"," "," ","D","D","1"," "," ","131423"," "," "," ","6"," "," "," "," "," "," ",.00 ,.00 ,0 ,.00 ,"Z","XYZ","12345","M"," "," "," "," "," "," "," "," ",-.15 ,.00 ,-1.05 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,"F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","M","C","E","N","X","Z"," "," "," "," "," ","B","T","Y","F","D","W","K","C","C","C","M","M","M","M","M","M"," ","C","C","M","M","M","M","M",.0000 ,3.0000 ,.0000 ,.0000 ,.00 ,-.15 ,.00 ,.00 ,"ATO","BAV",558657 ,"0165524028","-","19"," "," "," "," ","01","31","PKELLEY ","ATESDGMIMC",131441 ,"B"," ",0 ,.0000 ,3.0000 ," ",.00 ," ",.00 ,"A","A","A","USD","USD","US" +"P","Z","XYZ","12345","F1",".A050000019K"," ","201911"," ","F"," "," ",.00000000 ,0 ," "," ",20190813 ,8.89000000 ,"2"," "," ","S","9H13 DVII0","100"," "," "," "," "," "," "," "," ","US","C"," ","ABCDE"," ","M"," "," "," "," "," "," "," "," "," ","0",4.0000 ," 4-",.0000 ,.0000 ,.0000 ,.0000 ,.0000 ,.0000 ,.00 ,1 ,"NOV 19 SOYBEANS "," "," ",0 ," "," "," ",2200.00 ,.00 ,5000.0000000 ,20190813 ,1.00000000 ,0 ,.000 ,.00000000 ,.00000000 ,.0000 ,20191114 ," ","US","01","05"," "," "," "," ","1A",0 ," 8.89 ",.00000000 ,8.78000000 ,20190814 ," 8.78 ",.00000000 ," ",8.89000000 ,.00000000 ," "," 0008890"," ","4","USD","US"," "," J"," "," "," "," ","H","FD"," "," "," F1",.00000000 ,"2H0L ","PKELL"," "," "," "," "," ","17PD"," "," ","D","D","1"," "," ","130917"," "," "," ","6"," "," "," "," "," "," ",.00 ,.00 ,0 ,.00 ,"Z","XYZ","12345","M"," "," "," "," "," "," "," "," ",-.20 ,.00 ,-1.40 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,"F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","M","C","E","N","X","Z"," "," "," "," "," ","B","T","Y","F","D","W","K","C","C","C","M","M","M","M","M","M"," ","C","C","M","M","M","M","M",.0000 ,3.0000 ,1.0000 ,.0000 ,.00 ,-.15 ,-.05 ,.00 ,"ATO","BAV",558658 ,"0165522847","-","19"," "," "," "," ","01","31","PKELLEY ","ATESDGMIMC",130929 ,"B"," ",0 ,.0000 ,4.0000 ," ",.00 ," ",.00 ,"A","A","A","USD","USD","US" +"P","Z","XYZ","12345","F1",".A050000019K"," ","201911"," ","F"," "," ",.00000000 ,0 ," "," ",20190814 ,8.82250000 ,"2"," "," ","S","9H14 BJ4P0","100"," "," "," "," "," "," "," "," ","US","C"," ","ABCDE"," ","M"," "," "," "," "," "," "," "," "," ","0",1.0000 ," 1-",.0000 ,.0000 ,.0000 ,.0000 ,.0000 ,.0000 ,.00 ,1 ,"NOV 19 SOYBEANS "," "," ",0 ," "," "," ",212.50 ,.00 ,5000.0000000 ,20190814 ,1.00000000 ,0 ,.000 ,.00000000 ,.00000000 ,.0000 ,20191114 ," ","US","01","05"," "," "," "," ","1A",0 ," 8.82 1/4",.00000000 ,8.78000000 ,20190814 ," 8.78 ",.00000000 ," ",8.82250000 ,.00000000 ," "," 0008822"," ","4","USD","US"," "," J"," "," "," "," ","H","FD"," "," "," F1",.00000000 ,"2H0L ","PKELL"," "," "," "," "," ","17PD"," "," ","D","D","1"," "," ","083016"," "," "," ","6"," "," "," "," "," "," ",.00 ,.00 ,0 ,.00 ,"Z","XYZ","12345","M"," "," "," "," "," "," "," "," ",-.05 ,.00 ,-.35 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,"F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","M","C","E","N","X","Z"," "," "," "," "," ","B","T","Y","F","D","W","K","C","C","C","M","M","M","M","M","M"," ","C","C","M","M","M","M","M",.0000 ,1.0000 ,.0000 ,.0000 ,.00 ,-.05 ,.00 ,.00 ,"ATO","BAK",558659 ,"0165594516","-","19"," "," "," "," ","01","31","PKELLEY ","ATESDGMIMC",83040 ,"B"," ",0 ,.0000 ,1.0000 ," ",.00 ," ",.00 ,"A","A","A","USD","USD","US" +"P","Z","XYZ","12345","F1",".A050000020A"," ","202001"," ","F"," "," ",.00000000 ,0 ," "," ",20190813 ,9.01250000 ,"1"," "," ","S","9H13 DWKT0","100"," "," "," "," "," "," "," "," ","US","C"," ","ABCDE"," ","M"," "," "," "," "," "," "," "," "," ","0",6.0000 ," 6 ",.0000 ,.0000 ,.0000 ,.0000 ,.0000 ,.0000 ,.00 ,1 ,"JAN 20 SOYBEANS "," "," ",0 ," "," "," ",-2925.00 ,.00 ,5000.0000000 ,20190813 ,1.00000000 ,0 ,.000 ,.00000000 ,.00000000 ,.0000 ,20200114 ," ","US","01","05"," "," "," "," ","1A",0 ," 9.01 1/4",.00000000 ,8.91500000 ,20190814 ," 8.91 1/2",.00000000 ," ",9.02000000 ,.00000000 ," "," 0009012"," ","4","USD","US"," "," J"," "," "," "," ","H","FD"," "," "," F1",.00000000 ,"2H0L ","PKELL"," "," "," "," "," ","17PD"," "," ","D","D","1"," "," ","131423"," "," "," ","6"," "," "," "," "," "," ",.00 ,.00 ,0 ,.00 ,"Z","XYZ","12345","M"," "," "," "," "," "," "," "," ",-.30 ,.00 ,-2.10 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,"F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","M","C","E","N","X","Z"," "," "," "," "," ","B","T","Y","F","D","W","K","C","C","C","M","M","M","M","M","M"," ","C","C","M","M","M","M","M",.0000 ,6.0000 ,.0000 ,.0000 ,.00 ,-.30 ,.00 ,.00 ,"ATO","BAV",558660 ,"0165524026","1","20"," "," "," "," ","01","31","PKELLEY ","ATESDGMIMC",131441 ,"B"," ",0 ,6.0000 ,.0000 ," ",.00 ," ",.00 ,"A","A","A","USD","USD","US" +"P","Z","XYZ","12345","F1",".A050000020A"," ","202001"," ","F"," "," ",.00000000 ,0 ," "," ",20190813 ,9.02000000 ,"1"," "," ","S","9H13 DVIG0","100"," "," "," "," "," "," "," "," ","US","C"," ","ABCDE"," ","M"," "," "," "," "," "," "," "," "," ","0",8.0000 ," 8 ",.0000 ,.0000 ,.0000 ,.0000 ,.0000 ,.0000 ,.00 ,1 ,"JAN 20 SOYBEANS "," "," ",0 ," "," "," ",-4200.00 ,.00 ,5000.0000000 ,20190813 ,1.00000000 ,0 ,.000 ,.00000000 ,.00000000 ,.0000 ,20200114 ," ","US","01","05"," "," "," "," ","1A",0 ," 9.02 ",.00000000 ,8.91500000 ,20190814 ," 8.91 1/2",.00000000 ," ",9.02000000 ,.00000000 ," "," 0009020"," ","4","USD","US"," "," J"," "," "," "," ","H","FD"," "," "," F1",.00000000 ,"2H0L ","PKELL"," "," "," "," "," ","17PD"," "," ","D","D","1"," "," ","130917"," "," "," ","6"," "," "," "," "," "," ",.00 ,.00 ,0 ,.00 ,"Z","XYZ","12345","M"," "," "," "," "," "," "," "," ",-.40 ,.00 ,-2.80 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,"F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","M","C","E","N","X","Z"," "," "," "," "," ","B","T","Y","F","D","W","K","C","C","C","M","M","M","M","M","M"," ","C","C","M","M","M","M","M",.0000 ,8.0000 ,.0000 ,.0000 ,.00 ,-.40 ,.00 ,.00 ,"ATO","BAV",558661 ,"0165522845","1","20"," "," "," "," ","01","31","PKELLEY ","ATESDGMIMC",130929 ,"B"," ",0 ,8.0000 ,.0000 ," ",.00 ," ",.00 ,"A","A","A","USD","USD","US" +"P","Z","XYZ","12345","F1",".A050000020A"," ","202001"," ","F"," "," ",.00000000 ,0 ," "," ",20190814 ,8.95250000 ,"1"," "," ","S","9H14 BJ4Q0","100"," "," "," "," "," "," "," "," ","US","C"," ","ABCDE"," ","M"," "," "," "," "," "," "," "," "," ","0",1.0000 ," 1 ",.0000 ,.0000 ,.0000 ,.0000 ,.0000 ,.0000 ,.00 ,1 ,"JAN 20 SOYBEANS "," "," ",0 ," "," "," ",-187.50 ,.00 ,5000.0000000 ,20190814 ,1.00000000 ,0 ,.000 ,.00000000 ,.00000000 ,.0000 ,20200114 ," ","US","01","05"," "," "," "," ","1A",0 ," 8.95 1/4",.00000000 ,8.91500000 ,20190814 ," 8.91 1/2",.00000000 ," ",8.95250000 ,.00000000 ," "," 0008952"," ","4","USD","US"," "," J"," "," "," "," ","H","FD"," "," "," F1",.00000000 ,"2H0L ","PKELL"," "," "," "," "," ","17PD"," "," ","D","D","1"," "," ","083016"," "," "," ","6"," "," "," "," "," "," ",.00 ,.00 ,0 ,.00 ,"Z","XYZ","12345","M"," "," "," "," "," "," "," "," ",-.05 ,.00 ,-.35 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,"F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","M","C","E","N","X","Z"," "," "," "," "," ","B","T","Y","F","D","W","K","C","C","C","M","M","M","M","M","M"," ","C","C","M","M","M","M","M",.0000 ,1.0000 ,.0000 ,.0000 ,.00 ,-.05 ,.00 ,.00 ,"ATO","BAK",558662 ,"0165594517","1","20"," "," "," "," ","01","31","PKELLEY ","ATESDGMIMC",83040 ,"B"," ",0 ,1.0000 ,.0000 ," ",.00 ," ",.00 ,"A","A","A","USD","USD","US" +"P","Z","XYZ","12345","F1",".A050000020A"," ","202001"," ","F"," "," ",.00000000 ,0 ," "," ",20190814 ,8.95500000 ,"1"," "," ","S","9H14 BJ4N0","100"," "," "," "," "," "," "," "," ","US","C"," ","ABCDE"," ","M"," "," "," "," "," "," "," "," "," ","0",1.0000 ," 1 ",.0000 ,.0000 ,.0000 ,.0000 ,.0000 ,.0000 ,.00 ,1 ,"JAN 20 SOYBEANS "," "," ",0 ," "," "," ",-200.00 ,.00 ,5000.0000000 ,20190814 ,1.00000000 ,0 ,.000 ,.00000000 ,.00000000 ,.0000 ,20200114 ," ","US","01","05"," "," "," "," ","1A",0 ," 8.95 1/2",.00000000 ,8.91500000 ,20190814 ," 8.91 1/2",.00000000 ," ",8.95500000 ,.00000000 ," "," 0008955"," ","4","USD","US"," "," J"," "," "," "," ","H","FD"," "," "," F1",.00000000 ,"2H0L ","PKELL"," "," "," "," "," ","17PD"," "," ","D","D","1"," "," ","083016"," "," "," ","6"," "," "," "," "," "," ",.00 ,.00 ,0 ,.00 ,"Z","XYZ","12345","M"," "," "," "," "," "," "," "," ",-.05 ,.00 ,-.35 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,"F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","M","C","E","N","X","Z"," "," "," "," "," ","B","T","Y","F","D","W","K","C","C","C","M","M","M","M","M","M"," ","C","C","M","M","M","M","M",.0000 ,1.0000 ,.0000 ,.0000 ,.00 ,-.05 ,.00 ,.00 ,"ATO","BAK",558663 ,"0165594515","1","20"," "," "," "," ","01","31","PKELLEY ","ATESDGMIMC",83040 ,"B"," ",0 ,1.0000 ,.0000 ," ",.00 ," ",.00 ,"A","A","A","USD","USD","US" +"P","Z","XYZ","12345","F1",".A050000020C"," ","202003"," ","F"," "," ",.00000000 ,0 ," "," ",20190813 ,9.13500000 ,"2"," "," ","S","9H13 DWKU0","100"," "," "," "," "," "," "," "," ","US","C"," ","ABCDE"," ","M"," "," "," "," "," "," "," "," "," ","0",3.0000 ," 3-",.0000 ,.0000 ,.0000 ,.0000 ,.0000 ,.0000 ,.00 ,1 ,"MAR 20 SOYBEANS "," "," ",0 ," "," "," ",1462.50 ,.00 ,5000.0000000 ,20190813 ,1.00000000 ,0 ,.000 ,.00000000 ,.00000000 ,.0000 ,20200313 ," ","US","01","05"," "," "," "," ","1A",0 ," 9.13 1/2",.00000000 ,9.03750000 ,20190814 ," 9.03 3/4",.00000000 ," ",9.14000000 ,.00000000 ," "," 0009135"," ","4","USD","US"," "," J"," "," "," "," ","H","FD"," "," "," F1",.00000000 ,"2H0L ","PKELL"," "," "," "," "," ","17PD"," "," ","D","D","1"," "," ","131423"," "," "," ","6"," "," "," "," "," "," ",.00 ,.00 ,0 ,.00 ,"Z","XYZ","12345","M"," "," "," "," "," "," "," "," ",-.15 ,.00 ,-1.05 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,"F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","M","C","E","N","X","Z"," "," "," "," "," ","B","T","Y","F","D","W","K","C","C","C","M","M","M","M","M","M"," ","C","C","M","M","M","M","M",.0000 ,3.0000 ,.0000 ,.0000 ,.00 ,-.15 ,.00 ,.00 ,"ATO","BAV",558664 ,"0165524027","3","20"," "," "," "," ","01","31","PKELLEY ","ATESDGMIMC",131441 ,"B"," ",0 ,.0000 ,3.0000 ," ",.00 ," ",.00 ,"A","A","A","USD","USD","US" +"P","Z","XYZ","12345","F1",".A050000020C"," ","202003"," ","F"," "," ",.00000000 ,0 ," "," ",20190813 ,9.14000000 ,"2"," "," ","S","9H13 DVIH0","100"," "," "," "," "," "," "," "," ","US","C"," ","ABCDE"," ","M"," "," "," "," "," "," "," "," "," ","0",4.0000 ," 4-",.0000 ,.0000 ,.0000 ,.0000 ,.0000 ,.0000 ,.00 ,1 ,"MAR 20 SOYBEANS "," "," ",0 ," "," "," ",2050.00 ,.00 ,5000.0000000 ,20190813 ,1.00000000 ,0 ,.000 ,.00000000 ,.00000000 ,.0000 ,20200313 ," ","US","01","05"," "," "," "," ","1A",0 ," 9.14 ",.00000000 ,9.03750000 ,20190814 ," 9.03 3/4",.00000000 ," ",9.14000000 ,.00000000 ," "," 0009140"," ","4","USD","US"," "," J"," "," "," "," ","H","FD"," "," "," F1",.00000000 ,"2H0L ","PKELL"," "," "," "," "," ","17PD"," "," ","D","D","1"," "," ","130917"," "," "," ","6"," "," "," "," "," "," ",.00 ,.00 ,0 ,.00 ,"Z","XYZ","12345","M"," "," "," "," "," "," "," "," ",-.20 ,.00 ,-1.40 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,"F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","M","C","E","N","X","Z"," "," "," "," "," ","B","T","Y","F","D","W","K","C","C","C","M","M","M","M","M","M"," ","C","C","M","M","M","M","M",.0000 ,4.0000 ,.0000 ,.0000 ,.00 ,-.20 ,.00 ,.00 ,"ATO","BAV",558665 ,"0165522846","3","20"," "," "," "," ","01","31","PKELLEY ","ATESDGMIMC",130929 ,"B"," ",0 ,.0000 ,4.0000 ," ",.00 ," ",.00 ,"A","A","A","USD","USD","US" +"P","Z","XYZ","12345","F1",".A050000020C"," ","202003"," ","F"," "," ",.00000000 ,0 ," "," ",20190814 ,9.07500000 ,"2"," "," ","S","9H14 BJ4S0","100"," "," "," "," "," "," "," "," ","US","C"," ","ABCDE"," ","M"," "," "," "," "," "," "," "," "," ","0",1.0000 ," 1-",.0000 ,.0000 ,.0000 ,.0000 ,.0000 ,.0000 ,.00 ,1 ,"MAR 20 SOYBEANS "," "," ",0 ," "," "," ",187.50 ,.00 ,5000.0000000 ,20190814 ,1.00000000 ,0 ,.000 ,.00000000 ,.00000000 ,.0000 ,20200313 ," ","US","01","05"," "," "," "," ","1A",0 ," 9.07 1/2",.00000000 ,9.03750000 ,20190814 ," 9.03 3/4",.00000000 ," ",9.07500000 ,.00000000 ," "," 0009075"," ","4","USD","US"," "," J"," "," "," "," ","H","FD"," "," "," F1",.00000000 ,"2H0L ","PKELL"," "," "," "," "," ","17PD"," "," ","D","D","1"," "," ","083016"," "," "," ","6"," "," "," "," "," "," ",.00 ,.00 ,0 ,.00 ,"Z","XYZ","12345","M"," "," "," "," "," "," "," "," ",-.05 ,.00 ,-.35 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,.00 ,"F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","F1","M","C","E","N","X","Z"," "," "," "," "," ","B","T","Y","F","D","W","K","C","C","C","M","M","M","M","M","M"," ","C","C","M","M","M","M","M",.0000 ,1.0000 ,.0000 ,.0000 ,.00 ,-.05 ,.00 ,.00 ,"ATO","BAK",558666 ,"0165594518","3","20"," "," "," "," ","01","31","PKELLEY ","ATESDGMIMC",83040 ,"B"," ",0 ,.0000 ,1.0000 ," ",.00 ," ",.00 ,"A","A","A","USD","USD","US" diff --git a/src/server/sparkd/position/import/docs/reference/sftp_credentials_encrypted.txt.gpg b/src/server/sparkd/position/import/docs/reference/sftp_credentials_encrypted.txt.gpg new file mode 100644 index 000000000..67c4ccc51 --- /dev/null +++ b/src/server/sparkd/position/import/docs/reference/sftp_credentials_encrypted.txt.gpg @@ -0,0 +1,2 @@ +Œ  JÐ>ͬŒpÿÒ¢¼à¾ßadߣ÷Cm•AVs4ÄÝCr\OÜ +輦%*(,W(DŠ–ùrúŠËZÎq^?~=jÉ=üÁ±&f M@T¶ÔXq—W¾ÑÆM«ÎÈ"OÂéà +#include +#include + +namespace bts { +namespace position { + +std::pair map_gmi_future_symbol( + std::string const &cfc, api::Venue venue) { + auto map_glbx_to_sim = config::get().map_glbx_to_sim(); + + for (auto const &m : config::get().sym()) + if (m.gmi() == cfc && m.venue() == venue) + return std::pair( + {m.spark(), venue, api::SECURITYTYPE_FUTURE}, + m.price_multiplier()); + else if (map_glbx_to_sim && m.gmi() == cfc && + venue == api::Venue::VENUE_SIM && + m.venue() == api::Venue::VENUE_GLBX) + return std::pair( + {m.spark(), venue, api::SECURITYTYPE_FUTURE}, + m.price_multiplier()); + return std::pair({},os::nan()); +} + +api::Venue map_gmi_venue(std::string const &gmi_venue) { + if (gmi_venue == "1" || gmi_venue == "2" || gmi_venue == "01" || + gmi_venue == "02") { + if (config::get().map_glbx_to_sim()) + return api::Venue::VENUE_SIM; + else + return api::Venue::VENUE_GLBX; + } else if (gmi_venue == "5") + return api::Venue::VENUE_ICE; + else + return api::Venue::VENUE_UNSET; +} + +bool is_gmi_security_a_future(std::string const &cstype, + std::string const &csubty) { + return cstype == "F" && csubty.empty(); +} + +api::Side map_gmi_side(std::string const &cbs) { + if (cbs == "1") + return api::Side::SIDE_BUY; + else if (cbs == "2") + return api::Side::SIDE_SELL; + else + return api::Side::SIDE_UNDEFINED; +} + +std::optional map_gmi_quantity(std::string const &cqty) { + try { + return std::stoi(cqty); + } catch (std::exception const &) { + return {}; + } +} + +std::optional +map_gmi_expiration(std::string const &exp_date) { + if (exp_date.size() != 8) + return {}; + + auto year = map_gmi_quantity(exp_date.substr(0, 4)); + if (!year.has_value()) + return {}; + auto month = map_gmi_quantity(exp_date.substr(4, 2)); + if (!month.has_value()) + return {}; + auto day = map_gmi_quantity(exp_date.substr(6, 2)); + if (!day.has_value()) + return {}; + + api::FutureContract result; + result.set_expiration_year(year.value()); + result.set_expiration_month(month.value()); + result.set_expiration_day(day.value()); + return result; +} + +api::Security const * +find_security(md::UserSymbol const &s, std::string const &cltdat, + std::map> &cached) { + + auto exp_date = map_gmi_expiration(cltdat); + if (!exp_date.has_value()) + return nullptr; + + auto it = cached.find(s); + if (it == cached.end()) { + auto &secs = cached[s]; + + std::set to_load; + to_load.insert(s); + bts::md::db::load_securities( + to_load, [&secs](auto const &s) { secs.push_back(s); }); + } + auto &secs = cached[s]; + + for (auto const &s : secs) { + bts_assert(s.has_future()); + auto &f = s.future(); + if (f.expiration_year() == exp_date.value().expiration_year() && + f.expiration_month() == exp_date.value().expiration_month() && + f.expiration_day() == exp_date.value().expiration_day()) + return &s; + } + return nullptr; +} + +std::variant +parse_gmi(api::admin::AdminData const &admin_data, + std::string const &file_path) { + std::ifstream in{file_path}; + return parse_gmi(admin_data, in); +} + +std::variant +parse_gmi(api::admin::AdminData const &admin_data, std::istream &in) { + if (in.bad()) + return fmt::format("Unable to open GMI file"); + + std::string line; + std::getline(in, line); + + if (in.bad()) + return fmt::format("Could not read first line of GMI file "); + + std::vector column_names = parse_csv(line); + std::map column_idx; + for (size_t i = 0; i < column_names.size(); i++) + column_idx[column_names[i]] = i; + + auto FC = std::string("CFC"); // Symbol + auto EXCH = std::string("CEXCH"); // Exchange + auto STYPE = std::string("CSTYPE"); // Symbol type (options are marked as F) + auto SUBTY = std::string("CSUBTY"); // Symbol sub-type (for futures blank) + auto LTDAT = std::string("CLTDAT"); // Expiration date + auto BS = std::string("CBS"); // Buy/Sell + auto QTY = std::string("CQTY"); // Quantity, + auto OFFIC = std::string("COFFIC"); // First 3 chars of clearing account + auto ACCT = std::string("CACCT"); // Last 5 chars of clearing account + auto TPRIC = std::string("CTPRIC"); // Trade price + + auto required_columns = { + FC, EXCH, STYPE, SUBTY, LTDAT, BS, QTY, OFFIC, ACCT, TPRIC, + }; + + for (auto c : required_columns) + if (column_idx.count(c) == 0) + return fmt::format("Could not find column {} in GMI file", c); + + api::position::BasePositionBatch result; + auto line_number = 1; + std::map> cached_securities; + + while (std::getline(in, line)) { + line_number++; + std::vector values = parse_csv(line); + if (values.size() != column_names.size()) { + auto msg = + fmt::format("Aborted parsing on line {}. Expected {} columns, but " + "found {}", + line_number, column_names.size(), values.size()); + LOG(OrderMan, Error, "{}", msg); + return msg; + } + + auto gmi_exchange_code = values[column_idx[EXCH]]; + auto venue = map_gmi_venue(gmi_exchange_code); + if (venue == api::Venue::VENUE_UNSET) { + LOG(OrderMan, Info, + "Skipping line {} file because we could not resolve " + "the GMI " + "exchange code on colum {} {}", + line_number, EXCH, gmi_exchange_code); + continue; + } + + auto clearing_account = + values[column_idx[OFFIC]] + values[column_idx[ACCT]]; + + auto bts_account = admin::AdminServiceUtils::find_account( + admin_data, clearing_account, venue); + + if (bts_account == nullptr) { + LOG(OrderMan, Info, + "Skipping line {} because we could not map the " + "GMI clearing account {} to a BTS account", + line_number, clearing_account); + continue; + } + + api::position::BasePosition this_line_position; + + // ACCOUNT ID + this_line_position.set_account_id(bts_account->id()); + // ACCOUNT ID + + auto ignore_unknown_sec = + config::get().ignore_unknown_securities(); + + bool ignore_unsupported_security_types = + config::get().ignore_unsupported_security_types(); + + if (!is_gmi_security_a_future(values[column_idx[STYPE]], + values[column_idx[SUBTY]])) { + if (ignore_unknown_sec || ignore_unsupported_security_types) { + LOG(OrderMan, Info, + "Skipping line {} because it does not refer to a future", + line_number); + continue; + } else { + auto msg = fmt::format("Aborted parsing on line {}. " + "{} and {} do not refer to a " + "future", + line_number, STYPE, SUBTY); + LOG(OrderMan, Error, "{}", msg); + return msg; + } + } + + auto gmi_cfc = values[column_idx[FC]]; + auto symbol_map = map_gmi_future_symbol(gmi_cfc, venue); + auto spark_symbol = symbol_map.first; + double price_multiplier = symbol_map.second; + if (spark_symbol == md::UserSymbol()) { + if (ignore_unknown_sec) { + LOG(OrderMan, Info, + "Skipping line {} because the {} column with value {} could " + "not be " + "resolved " + "to a spark future symbol at venue {}", + line_number, FC, gmi_cfc, api::Venue_Name(venue)); + continue; + } else { + auto msg = + fmt::format("Aborted parsing on line {} because " + "the {} column with value {} could " + "not be resolved " + "to a spark future symbol at venue {}", + line_number, FC, gmi_cfc, api::Venue_Name(venue)); + LOG(OrderMan, Error, "{}", msg); + return msg; + } + } + + auto gmi_exp = values[column_idx[LTDAT]]; + auto security = find_security(spark_symbol, gmi_exp, cached_securities); + + if (security == nullptr) { + if (ignore_unknown_sec) { + LOG(OrderMan, Info, + "Skipping line {} because no security with " + "symbol {} and expiration {} was found in the spark security " + "database", + line_number, spark_symbol.symbol(), gmi_exp); + continue; + } else { + auto msg = fmt::format( + "Aborted parsing on line {} because no security with " + "symbol {} and expiration {} was found in the spark security " + "database", + line_number, spark_symbol.symbol(), gmi_exp); + LOG(OrderMan, Error, "{}", msg); + return msg; + } + } + + // SECURITY KEY + this_line_position.set_security_id(security->id()); + // SECURITY KEY + + auto gmi_side = values[column_idx[BS]]; + auto side = map_gmi_side(gmi_side); + if (side == api::Side::SIDE_UNDEFINED) { + auto msg = + fmt::format("Aborted parsing on line {} because we " + "could not interpret " + "column {} with value {} to either a buy or a sell", + line_number, BS, gmi_side); + LOG(OrderMan, Error, "{}", msg); + return msg; + } + + // SIDE + this_line_position.set_side(side); + // SIDE + + auto gmi_qty = values[column_idx[QTY]]; + auto qty = map_gmi_quantity(gmi_qty); + if (!qty.has_value()) { + auto msg = fmt::format("Aborted parsing on line {} because " + "{} {} is not a number", + line_number, QTY, gmi_qty); + LOG(OrderMan, Error, "{}", msg); + return msg; + } + + // Quantity + this_line_position.set_quantity(qty.value()); + // Quantity + + auto gmi_price = values[column_idx[TPRIC]]; + auto price = parse_price(gmi_price); + if (!price.has_value()) { + auto msg = fmt::format("Aborted parsing on line {} because " + "{} {} is not a number", + line_number, TPRIC, gmi_price); + LOG(OrderMan, Error, "{}", msg); + return msg; + } + + // Price + auto avg_price = price.value() * price_multiplier; + this_line_position.set_avg_price(avg_price); + // Price + + LOG(OrderMan, Info, + "Successfully parsed line {}. Account: {} (id: {}) Sec: {} (id: {}) " + "{} {} @ {} ", + line_number, bts_account->name(), bts_account->id(), + securities::descriptor(*security), security->id(), + api::Side_Name(side), qty.value(), this_line_position.avg_price()); + + *result.add_positions() = this_line_position; + } + + if (result.positions_size() == 0) + LOG(OrderMan, Error, + "There were no positions in the GMI file. This is suspicious " + "but not necessarily wrong."); + + return result; +} + +api::position::BasePositionBatch +reduce_parsed_gmi(api::position::BasePositionBatch const &in) { + using namespace api::position; + + BasePositionBatch out; + *out.mutable_current_as_of() = in.current_as_of(); + + using Key = std::tuple; + std::map> index; + + for (auto const &bp : in.positions()) { + auto k = std::make_tuple(bp.security_id(), bp.side(), bp.account_id()); + index[k].push_back(bp); + } + + auto reducer = [](std::vector const &in) -> BasePosition { + bts_assert(in.size()); + + uint32_t total_qty = 0; + double total_price = 0; + + for (auto const &bp : in) { + total_qty += bp.quantity(); + total_price += (bp.quantity() * bp.avg_price()); + } + BasePosition result = in[0]; + result.set_quantity(total_qty); + if (total_qty != 0) + result.set_avg_price(total_price / total_qty); + else + result.set_avg_price(0); + return result; + }; + + for (auto const &p : index) { + auto reduced_pos = out.add_positions(); + *reduced_pos = reducer(p.second); + } + return out; +} + +} // namespace position +} // namespace bts diff --git a/src/server/sparkd/position/import/parsing/gmi_parser.h b/src/server/sparkd/position/import/parsing/gmi_parser.h new file mode 100644 index 000000000..ce578433b --- /dev/null +++ b/src/server/sparkd/position/import/parsing/gmi_parser.h @@ -0,0 +1,31 @@ +#pragma once + +#include "lib/api/services/PositionService.pb.h" +#include +#include +#include + +namespace bts { +namespace api { +namespace admin { +class AdminData; +} +} // namespace api +} // namespace bts + +namespace bts { +namespace position { + +std::variant +parse_gmi(api::admin::AdminData const &admin_data, + std::string const &file_path); + +std::variant +parse_gmi(api::admin::AdminData const &admin_data, std::istream &); + +// Accumulate records by account, security, side +api::position::BasePositionBatch +reduce_parsed_gmi(api::position::BasePositionBatch const &); + +} // namespace position +} // namespace bts diff --git a/src/server/sparkd/position/import/parsing/money_parser.cpp b/src/server/sparkd/position/import/parsing/money_parser.cpp new file mode 100644 index 000000000..861f86077 --- /dev/null +++ b/src/server/sparkd/position/import/parsing/money_parser.cpp @@ -0,0 +1,119 @@ +#include "server/sparkd/position/import/parsing/money_parser.h" +#include "lib/api/service_helpers/AdminServiceUtils.h" +#include "lib/logging/logging.h" +#include "server/sparkd/position/import/parsing/parser_common.h" +#include +#include + +namespace bts { +namespace position { + +std::variant +parse_money(api::admin::AdminData const &admin_data, + std::string const &file_path) { + std::ifstream in{file_path}; + return parse_money(admin_data, in); +} + +std::variant +parse_money(api::admin::AdminData const &admin_data, std::istream &in) { + if (in.bad()) + return fmt::format("Unable to open money file"); + + std::string line; + std::getline(in, line); + + if (in.bad()) + return fmt::format("Could not read first line of money file "); + + std::vector column_names = parse_csv(line); + std::map column_idx; + for (size_t i = 0; i < column_names.size(); i++) + column_idx[column_names[i]] = i; + + auto RECID = std::string("CRECID"); // G, M or R, (we want M) + auto OFFIC = std::string("COFFIC"); // First 3 chars of clearing account + auto ACCT = std::string("CACCT"); // Last 5 digits of clearing account + auto BAL = std::string("CBAL"); // Blance + + auto required_columns = { + RECID, + OFFIC, + ACCT, + BAL, + }; + + for (auto c : required_columns) + if (column_idx.count(c) == 0) + return fmt::format("Could not find column {} in money file", c); + + api::position::CashBalanceBatch result; + auto line_number = 1; + + while (std::getline(in, line)) { + line_number++; + std::vector values = parse_csv(line); + if (values.size() != column_names.size()) { + auto msg = + fmt::format("Aborted parsing of money file on line {}. Expected " + "{} columns, but " + "found {}", + line_number, column_names.size(), values.size()); + LOG(OrderMan, Error, "{}", msg); + return msg; + } + + auto mrecid = values[column_idx[RECID]]; + if (mrecid != "M") { + LOG(OrderMan, Info, "Skipping line {} because {} is {}, not M", + line_number, RECID, mrecid); + continue; + } + + auto clearing_account = + values[column_idx[OFFIC]] + values[column_idx[ACCT]]; + + auto bts_account = admin::AdminServiceUtils::find_account( + admin_data, clearing_account, api::Venue::VENUE_GLBX); + + if (bts_account == nullptr) { + LOG(OrderMan, Info, + "Skipping line {} because we could not map the " + "GMI clearing account {} to a BTS account", + line_number, clearing_account); + continue; + } + + api::position::CashBalanceItem this_line_balance; + + // ACCOUNT ID + this_line_balance.set_account_id(bts_account->id()); + // ACCOUNT ID + + // Balance + auto balance_s = values[column_idx[BAL]]; + auto balance = parse_price(balance_s); + if (!balance.has_value()) { + auto msg = fmt::format("Aborted parsing on line {} because " + "{} {} is not a number", + BAL, line_number, balance_s); + LOG(OrderMan, Error, "{}", msg); + return msg; + } + + // Balance + this_line_balance.set_cash_balance(balance.value()); + // Balance + + LOG(OrderMan, Info, + "Successfully parsed line {}. Account: {} (id: {}) Cash Balance: {}", + line_number, bts_account->name(), bts_account->id(), + this_line_balance.cash_balance()); + + *result.add_cash_balance() = this_line_balance; + } + + return result; +} +} // namespace position +} // namespace bts diff --git a/src/server/sparkd/position/import/parsing/money_parser.h b/src/server/sparkd/position/import/parsing/money_parser.h new file mode 100644 index 000000000..8d49adc08 --- /dev/null +++ b/src/server/sparkd/position/import/parsing/money_parser.h @@ -0,0 +1,27 @@ +#pragma once + +#include "lib/api/services/MarginService.server.pb.h" +#include +#include +#include + +namespace bts { +namespace api { +namespace admin { +class AdminData; +} +} // namespace api +} // namespace bts + +namespace bts { +namespace position { + +std::variant +parse_money(api::admin::AdminData const &, + std::string const &file_path); + +std::variant +parse_money(api::admin::AdminData const &, std::istream &); + +} // namespace position +} // namespace bts diff --git a/src/server/sparkd/position/import/parsing/parser_common.cpp b/src/server/sparkd/position/import/parsing/parser_common.cpp new file mode 100644 index 000000000..d2e423dff --- /dev/null +++ b/src/server/sparkd/position/import/parsing/parser_common.cpp @@ -0,0 +1,35 @@ +#include "server/sparkd/position/import/parsing/parser_common.h" + +#include + +namespace bts { +namespace position { + +std::vector sanitize_csv_values(std::vector const &values) { + std::vector out; + out = values; + + for (auto &s : out) { + boost::algorithm::replace_all(s, "\"", ""); + boost::algorithm::trim(s); + } + return out; +} + +std::vector parse_csv(std::string const&line) { + std::vector out; + boost::algorithm::split(out, line, + boost::algorithm::is_any_of(",")); + return sanitize_csv_values(out); +} + +std::optional parse_price(std::string const &cprice) { + try { + return std::stod(cprice); + } catch (std::exception const &) { + return {}; + } +} + + +}} diff --git a/src/server/sparkd/position/import/parsing/parser_common.h b/src/server/sparkd/position/import/parsing/parser_common.h new file mode 100644 index 000000000..bbc73ecd5 --- /dev/null +++ b/src/server/sparkd/position/import/parsing/parser_common.h @@ -0,0 +1,13 @@ +#pragma once + +#include +#include +#include + +namespace bts { +namespace position { +std::vector parse_csv(std::string const&); +std::optional parse_price(std::string const &); +}} + + diff --git a/src/server/sparkd/position/tests/PositionTests.cpp b/src/server/sparkd/position/tests/PositionTests.cpp index 51d1f5086..3c8dbc372 100644 --- a/src/server/sparkd/position/tests/PositionTests.cpp +++ b/src/server/sparkd/position/tests/PositionTests.cpp @@ -1,69 +1,126 @@ #include "catch/catch.hpp" -#include "../Position.h" -#include "lib/orderman/orderman_utils/orderman_common.h" - -#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/datetime.h" +#include +#include #include #include #include -namespace bts -{ -namespace position -{ +#include "server/sparkd/position/import/DownloadWindow.h" -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); - // } - -}; +namespace bts { +namespace test { -// TEST_CASE ("Margin API", "[margin]") -// { -// securities::SecurityDB _db; -// MarginServiceCore _margin(_db); +TEST_CASE("download_window", "[position]") { + SECTION("single_day_before") { + // now = 9:00am + auto now = datetime::make_ptime(2019, 8, 14, 9, 0, 0, 0); -// SECTION("test1") -// { -// bts::securities::ISecurityDB::SecurityMap secs = _db.retrieve_all(); -// Position p; -// for(const auto& i : secs) -// { -// if(i.second->has_future() && i.second->symbol() == "ES") -// p.security = i.second; -// } -// auto acct = orderman::to("TAG1"); -// p.account = acct; -// p.avg_price = Price(2500.0); -// p.position = Quantity(1); -// p.side = api::SIDE_BUY; -// std::vector pos; -// pos.push_back(p); -// p.position = Quantity(3); -// p.side = api::SIDE_SELL; -// pos.push_back(p); - -// MarginResult res; -// PositionTests::query_margin_webapi(_margin, pos, res); -// REQUIRE(res.valid); -// } + // start window at 22:00, end at 23:00 + auto w_start = boost::posix_time::duration_from_string("22:00"); + auto w_stop = boost::posix_time::duration_from_string("23:00"); -// } + auto w = position::DownloadWindow { now, w_start, w_stop}; -}} - + REQUIRE(w.start() == datetime::make_ptime(2019, 8, 14, 22, 0, 0, 0)); + REQUIRE(w.stop() == datetime::make_ptime(2019, 8, 14, 23, 0, 0, 0)); + REQUIRE(!w.is_inside(datetime::make_ptime(2019,8,14,21,0,0,0))); + REQUIRE(w.is_inside(datetime::make_ptime(2019,8,14,22,30,0,0))); + REQUIRE(!w.is_inside(datetime::make_ptime(2019,8,14,23,30,0,0))); + REQUIRE(!w.is_inside(datetime::make_ptime(2018,8,14,22,30,0,0))); + REQUIRE(!w.is_inside(datetime::make_ptime(2020,8,14,22,30,0,0))); + } + SECTION("single_day_inside") { + // now = 9:00am + auto now = datetime::make_ptime(2019, 8, 14, 9, 0, 0, 0); + + // start window at 8:00, end at 10:00 + auto w_start = boost::posix_time::duration_from_string("08:00"); + auto w_stop = boost::posix_time::duration_from_string("10:00"); + + auto w = position::DownloadWindow { now, w_start, w_stop}; + + REQUIRE(w.start() == datetime::make_ptime(2019, 8, 14, 8, 0, 0, 0)); + REQUIRE(w.stop() == datetime::make_ptime(2019, 8, 14, 10, 0, 0, 0)); + REQUIRE(!w.is_inside(datetime::make_ptime(2019,8,14,7,0,0,0))); + REQUIRE(w.is_inside(datetime::make_ptime(2019,8,14,8,30,0,0))); + REQUIRE(!w.is_inside(datetime::make_ptime(2019,8,14,23,30,0,0))); + REQUIRE(!w.is_inside(datetime::make_ptime(2018,8,14,22,30,0,0))); + REQUIRE(!w.is_inside(datetime::make_ptime(2020,8,14,22,30,0,0))); + } + + SECTION("single_day_after") { + // now = 9:00am + auto now = datetime::make_ptime(2019, 8, 14, 9, 0, 0, 0); + + // start window at 8:00, end at 8:30 + auto w_start = boost::posix_time::duration_from_string("08:00"); + auto w_stop = boost::posix_time::duration_from_string("08:30"); + + auto w = position::DownloadWindow { now, w_start, w_stop}; + + REQUIRE(w.start() == datetime::make_ptime(2019, 8, 15, 8, 0, 0, 0)); + REQUIRE(w.stop() == datetime::make_ptime(2019, 8, 15, 8, 30, 0, 0)); + REQUIRE(!w.is_inside(datetime::make_ptime(2019,8,14,7,0,0,0))); + REQUIRE(w.is_inside(datetime::make_ptime(2019,8,15,8,20,0,0))); + REQUIRE(!w.is_inside(datetime::make_ptime(2019,8,15,23,30,0,0))); + REQUIRE(!w.is_inside(datetime::make_ptime(2018,8,14,22,30,0,0))); + REQUIRE(!w.is_inside(datetime::make_ptime(2020,8,14,22,30,0,0))); + } + + + SECTION("multi_day_before") { + // now = 9:00am + auto now = datetime::make_ptime(2019, 8, 14, 9, 0, 0, 0); + + // start window at 23:00, end at 01:00 + auto w_start = boost::posix_time::duration_from_string("23:00"); + auto w_stop = boost::posix_time::duration_from_string("01:00"); + + auto w = position::DownloadWindow { now, w_start, w_stop}; + + REQUIRE(w.start() == datetime::make_ptime(2019, 8, 14, 23, 0, 0, 0)); + REQUIRE(w.stop() == datetime::make_ptime(2019, 8, 15, 1, 0, 0, 0)); + REQUIRE(!w.is_inside(datetime::make_ptime(2019,8,14,21,0,0,0))); + REQUIRE(w.is_inside(datetime::make_ptime(2019,8,14,23,30,0,0))); + REQUIRE(!w.is_inside(datetime::make_ptime(2019,8,14,13,30,0,0))); + REQUIRE(!w.is_inside(datetime::make_ptime(2018,8,14,22,30,0,0))); + REQUIRE(!w.is_inside(datetime::make_ptime(2020,8,14,22,30,0,0))); + } + + SECTION("multi_day_inside") { + // now = 9:00am + auto now = datetime::make_ptime(2019, 8, 14, 9, 0, 0, 0); + + // start window at 23:00, end at 10:00 + auto w_start = boost::posix_time::duration_from_string("23:00"); + auto w_stop = boost::posix_time::duration_from_string("10:00"); + + auto w = position::DownloadWindow { now, w_start, w_stop}; + + REQUIRE(w.start() == datetime::make_ptime(2019, 8, 13, 23, 0, 0, 0)); + REQUIRE(w.stop() == datetime::make_ptime(2019, 8, 14, 10, 0, 0, 0)); + REQUIRE(!w.is_inside(datetime::make_ptime(2019,8,14,21,0,0,0))); + REQUIRE(w.is_inside(datetime::make_ptime(2019,8,14,9,30,0,0))); + REQUIRE(!w.is_inside(datetime::make_ptime(2019,8,14,13,30,0,0))); + } + + SECTION("multi_day_after") { + // now = 9:00am + auto now = datetime::make_ptime(2019, 8, 14, 9, 0, 0, 0); + + // start window at 23:00, end at 8:00 + auto w_start = boost::posix_time::duration_from_string("23:00"); + auto w_stop = boost::posix_time::duration_from_string("8:00"); + + auto w = position::DownloadWindow { now, w_start, w_stop}; + + REQUIRE(w.start() == datetime::make_ptime(2019, 8, 14, 23, 0, 0, 0)); + REQUIRE(w.stop() == datetime::make_ptime(2019, 8, 15, 8, 0, 0, 0)); + } +} + +} // namespace test +} // namespace bts diff --git a/src/server/sparkd/position/tests/position_tests_internal.cpp b/src/server/sparkd/position/tests/position_tests_internal.cpp new file mode 100644 index 000000000..8f83ab5bc --- /dev/null +++ b/src/server/sparkd/position/tests/position_tests_internal.cpp @@ -0,0 +1,308 @@ +#include "catch/catch.hpp" + +#include "lib/utils/datetime.h" +#include "server/sparkd/position/PositionMap.h" +#include "lib/orderman/actors/SODPosition.h" +#include +#include + +namespace bts { +namespace test { + +using namespace bts::api::orderman; +using namespace bts::api::position; +using namespace bts::orderman; + +int fill_id = 0; +Fill create_fill (uint32_t account_id, + uint32_t sec_key, + double price, + uint32_t qty, + api::Side side, + Fill::FillKind fkind){ + Fill f; + f.set_security_key(sec_key); + f.set_internal_account_id(account_id); + f.set_last_price(price); + f.set_last_shares(qty); + f.set_side(side); + f.set_fill_kind(fkind); + f.mutable_timestamp()->CopyFrom(SODPosition::to_timestamp(SODPosition::now())); + f.set_fill_key(std::to_string(++fill_id)); + return f; +} + +BasePosition create_base_pos(uint32_t account_id, + uint32_t sec_key, + double price, + uint32_t qty, + api::Side side){ + BasePosition bp; + bp.set_security_id(sec_key); + bp.set_account_id(account_id); + bp.set_avg_price(price); + bp.set_quantity(qty); + bp.set_side(side); + return bp; +} + +void compare (double d1, double d2){ + REQUIRE(std::abs(d1-d2) < 0.000001); +} + +void compare_pos (const PositionEntry& p1, + const PositionEntry& p2){ + compare(p1.long_pos(), p2.long_pos()); + compare(p1.long_avg_price(), p2.long_avg_price()); + compare(p1.short_pos(), p2.short_pos()); + compare(p1.short_avg_price(), p2.short_avg_price()); +} + +void compare_pos (const PositionEntry& p1, + double long_pos, + double long_price, + double short_pos, + double short_price){ + compare(p1.long_pos(), long_pos); + compare(p1.long_avg_price(), long_price); + compare(p1.short_pos(), short_pos); + compare(p1.short_avg_price(), short_price); +} + +double gen_rand (double min, double max){ + double f = (double)rand() / RAND_MAX; + return min + f * (max - min); +} + +void add_pos (PositionEntry& pe, double p, double q, bool is_long){ + if (is_long){ + uint32_t tot_q = pe.long_pos() + q; + pe.set_long_avg_price((pe.long_pos()*pe.long_avg_price() + p*q)/tot_q); + pe.set_long_pos(tot_q); + } + else{ + uint32_t tot_q = pe.short_pos() + q; + pe.set_short_avg_price((pe.short_pos()*pe.short_avg_price() + p*q)/tot_q); + pe.set_short_pos(tot_q); + } +} + +TEST_CASE("basic", "[position_internal]") { + SECTION("day_standard") { + // just sends some day position to PositionMap + // and verifies their correctness + PositionMap pmap; + Fill f = create_fill(1, 1, 10.0, 10, api::Side::SIDE_BUY, Fill::FILL_KIND_STANDARD); + pmap.on_fill(f,true); + PositionUnit const* p = pmap.test_only_pos(1,1); + REQUIRE (p!=nullptr); + compare_pos(p->day(),10,10,0,0); + compare_pos(p->raw_day(),10,10,0,0); + compare_pos(p->sod(),0,0,0,0); + // remove it + pmap.on_fill(f,false); + p = pmap.test_only_pos(1,1); + REQUIRE (p!=nullptr); + compare_pos(p->day(),0,0,0,0); + compare_pos(p->raw_day(),0,0,0,0); + compare_pos(p->sod(),0,0,0,0); + } + SECTION("day_leg") { + // just sends some day position to PositionMap + // and verifies their correctness + PositionMap pmap; + Fill f = create_fill(1, 1, 10.0, 10, api::Side::SIDE_BUY, Fill::FILL_KIND_LEG); + pmap.on_fill(f,true); + PositionUnit const* p = pmap.test_only_pos(1,1); + REQUIRE (p!=nullptr); + compare_pos(p->day(),10,10,0,0); + compare_pos(p->raw_day(),0,0,0,0); + compare_pos(p->sod(),0,0,0,0); + // remove it + pmap.on_fill(f,false); + p = pmap.test_only_pos(1,1); + REQUIRE (p!=nullptr); + compare_pos(p->day(),0,0,0,0); + compare_pos(p->raw_day(),0,0,0,0); + compare_pos(p->sod(),0,0,0,0); + } + SECTION("sod") { + // just sends some day position to PositionMap + // and verifies their correctness + PositionMap pmap; + Fill f = create_fill(1, 1, 10.0, 10, api::Side::SIDE_BUY, Fill::FILL_KIND_STANDARD); + f.mutable_timestamp()->set_seconds(100); + pmap.on_fill(f,true); + PositionUnit const* p = pmap.test_only_pos(1,1); + REQUIRE (p!=nullptr); + compare_pos(p->day(),0,0,0,0); + compare_pos(p->raw_day(),0,0,0,0); + compare_pos(p->sod(),10,10,0,0); + // remove it + pmap.on_fill(f,false); + p = pmap.test_only_pos(1,1); + REQUIRE (p!=nullptr); + compare_pos(p->day(),0,0,0,0); + compare_pos(p->raw_day(),0,0,0,0); + compare_pos(p->sod(),0,0,0,0); + } +} + +TEST_CASE("basic_badge", "[position_internal]") { + SECTION("day_standard") { + PositionMap pmap; + std::map> pos_map; + uint32_t n_accounts = 5, + n_secs = 20, + n_day_fills = 100, + n_sod_fills = 200; + for (uint32_t a=1; a<= n_accounts; a++) + for(uint32_t s=1;s<=n_secs;s++){ + for(uint32_t i=0; i< n_day_fills; i++){ + // day standard + PositionUnit& pu = pos_map[a][s]; + double p = gen_rand(-100,100); + uint32_t q = gen_rand(1,100); + Fill f = create_fill(a, s, p, q, api::Side::SIDE_BUY, Fill::FILL_KIND_STANDARD); + add_pos(*pu.mutable_day(),p,q,true); + pmap.on_fill(f,true); + p = gen_rand(-100,100); + q = gen_rand(1,100); + f = create_fill(a, s, p, q, api::Side::SIDE_SELL, Fill::FILL_KIND_STANDARD); + add_pos(*pu.mutable_day(),p,q,false); + pmap.on_fill(f,true); + } + // sod + for(uint32_t i=0; i< n_sod_fills; i++){ + // day standard + PositionUnit& pu = pos_map[a][s]; + double p = gen_rand(-100,100); + uint32_t q = gen_rand(1,100); + Fill f = create_fill(a, s, p, q, api::Side::SIDE_BUY, Fill::FILL_KIND_STANDARD); + f.mutable_timestamp()->set_seconds(100); + add_pos(*pu.mutable_sod(),p,q,true); + pmap.on_fill(f,true); + p = gen_rand(-100,100); + q = gen_rand(1,100); + f = create_fill(a, s, p, q, api::Side::SIDE_SELL, Fill::FILL_KIND_STANDARD); + f.mutable_timestamp()->set_seconds(100); + add_pos(*pu.mutable_sod(),p,q,false); + pmap.on_fill(f,true); + } + } + + for (auto a : pos_map){ + for (auto s : a.second){ + uint32_t acc_id = a.first, + sec_key = s.first; + PositionUnit& pu_exp = s.second; + PositionUnit const* pu_calc = pmap.test_only_pos(acc_id, sec_key); + REQUIRE(pu_calc != nullptr); + compare_pos (pu_exp.day(),pu_calc->day()); + compare_pos (pu_exp.day(),pu_calc->raw_day()); + compare_pos (pu_exp.sod(),pu_calc->sod()); + } + } + const auto& fm = pmap.test_only_fills(); + REQUIRE(fm.size() == 2*n_accounts*n_secs*(n_day_fills + n_sod_fills)); + } +} + +TEST_CASE("sod", "[position_internal]") { + // makes sure fills on either side of the SOD + // go the right place + timeval sod = SODPosition::now(); + PositionMap pmap(sod); + timeval pre_sod = sod, post_sod = sod; + pre_sod.tv_sec = pre_sod.tv_sec - 60*60; + post_sod.tv_sec = post_sod.tv_sec + 60*60; + // presod fill + Fill presod_f = create_fill(1, 1, 10, 50, api::Side::SIDE_BUY, Fill::FILL_KIND_STANDARD); + presod_f.mutable_timestamp()->CopyFrom(SODPosition::to_timestamp(pre_sod)); + pmap.on_fill(presod_f,true); + // postsod fill + Fill postsod_f = create_fill(1, 1, 20, 100, api::Side::SIDE_BUY, Fill::FILL_KIND_STANDARD); + postsod_f.mutable_timestamp()->CopyFrom(SODPosition::to_timestamp(post_sod)); + pmap.on_fill(postsod_f,true); + PositionUnit const* pu = pmap.test_only_pos(1,1); + REQUIRE(pu!=nullptr); + compare_pos(pu->sod(),50,10,0,0); + compare_pos(pu->day(),100,20,0,0); +} + +TEST_CASE("base_position", "[position_internal]"){ + SECTION("base_only"){ + // just with base, no sod, no day + PositionMap pmap; + + BasePositionBatch bp_batch; + bp_batch.add_positions()->CopyFrom(create_base_pos(1,1,10,50,api::Side::SIDE_BUY)); + bp_batch.add_positions()->CopyFrom(create_base_pos(1,1,20,100,api::Side::SIDE_SELL)); + bp_batch.add_positions()->CopyFrom(create_base_pos(1,2,11,51,api::Side::SIDE_BUY)); + bp_batch.add_positions()->CopyFrom(create_base_pos(1,2,21,101,api::Side::SIDE_SELL)); + bp_batch.add_positions()->CopyFrom(create_base_pos(2,1,0.05,5000,api::Side::SIDE_BUY)); + bp_batch.mutable_current_as_of()->CopyFrom(SODPosition::to_timestamp(SODPosition::now())); + pmap.set_base_position(bp_batch); + + PositionUnit const* pu = pmap.test_only_pos(1,1); + REQUIRE(pu!=nullptr); + compare_pos(pu->sod(),50,10,100,20); + compare_pos(pu->day(),0,0,0,0); + + pu = pmap.test_only_pos(1,2); + REQUIRE(pu!=nullptr); + compare_pos(pu->sod(),51,11,101,21); + compare_pos(pu->day(),0,0,0,0); + + pu = pmap.test_only_pos(2,1); + REQUIRE(pu!=nullptr); + compare_pos(pu->sod(),5000,0.05,0,0); + compare_pos(pu->day(),0,0,0,0); + } + SECTION("base+sod+day"){ + // fills on either side of SOD, then a base position + // comes in and overrides the SOD + timeval sod = SODPosition::now(); + PositionMap pmap (sod); + timeval pre_sod = sod, post_sod = sod; + pre_sod.tv_sec = pre_sod.tv_sec - 60*60; + post_sod.tv_sec = post_sod.tv_sec + 60*60; + // presod fill + Fill presod_f = create_fill(1, 1, 10, 50, api::Side::SIDE_BUY, Fill::FILL_KIND_STANDARD); + presod_f.mutable_timestamp()->CopyFrom(SODPosition::to_timestamp(pre_sod)); + pmap.on_fill(presod_f,true); + presod_f = create_fill(1, 2, 30, 50, api::Side::SIDE_BUY, Fill::FILL_KIND_STANDARD); + presod_f.mutable_timestamp()->CopyFrom(SODPosition::to_timestamp(pre_sod)); + pmap.on_fill(presod_f,true); + // postsod fill + Fill postsod_f = create_fill(1, 1, 20, 100, api::Side::SIDE_BUY, Fill::FILL_KIND_STANDARD); + postsod_f.mutable_timestamp()->CopyFrom(SODPosition::to_timestamp(post_sod)); + pmap.on_fill(postsod_f,true); + + PositionUnit const* pu = pmap.test_only_pos(1,1); + REQUIRE(pu!=nullptr); + compare_pos(pu->sod(),50,10,0,0); + compare_pos(pu->day(),100,20,0,0); + pu = pmap.test_only_pos(1,2); + compare_pos(pu->sod(),50,30,0,0); + compare_pos(pu->day(),0,0,0,0); + // now enter a base position, it should override the SOD + BasePositionBatch bp_batch; + bp_batch.add_positions()->CopyFrom(create_base_pos(1,1,11,51,api::Side::SIDE_SELL)); + bp_batch.mutable_current_as_of()->CopyFrom(SODPosition::to_timestamp(sod)); + pmap.set_base_position(bp_batch); + + pu = pmap.test_only_pos(1,1); + REQUIRE(pu!=nullptr); + compare_pos(pu->sod(),0,0,51,11); // sod is overriden by bp + compare_pos(pu->day(),100,20,0,0); // day remains the same + // sec 2 gets wiped out + pu = pmap.test_only_pos(1,2); + REQUIRE(pu!=nullptr); + compare_pos(pu->sod(),0,0,0,0); // sod is overriden by bp + compare_pos(pu->day(),0,0,0,0); + } +} + +} // namespace test +} // namespace bts diff --git a/src/web/CMakeDeps.cmake b/src/web/CMakeDeps.cmake index bc74cb4cf..19fda82cb 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/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 +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/BasePositionService.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/MoneyFileService.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/Util/StreamUtils.fs;ui/src/obj/Debug/netstandard2.0/ui.AssemblyInfo.fs) \ No newline at end of file diff --git a/src/web/api/src/DynamicService.ts b/src/web/api/src/DynamicService.ts index fa2745e3a..363f618fa 100644 --- a/src/web/api/src/DynamicService.ts +++ b/src/web/api/src/DynamicService.ts @@ -1,13 +1,28 @@ import {root} from 'proto' -import { ServiceContext } from 'ServiceContext' +import { ServiceContext, RawUpdate, RawUpdateType } from 'ServiceContext' import { assertValid } from 'bts_assert'; -export interface IService { +export interface Change { + old : T + new : T +} +export interface Update { + all : { [key:number]: T } + added : { [key:number]: T } + changed : { [key:number]: Change } + removed : { [key:number]: T } +} +interface IService { context : ServiceContext name: string qualifier : string } +function decode_buffer(decoder : any, buffer : Uint8Array) { + const temp = decoder.decode(buffer); + return fillDefaults(decoder.name, decoder.toObject(temp)); +} + function gen_method(service:IService, method_id : number, request_type : string, output_type : string) { const request_proto = root.lookupType(request_type) const result_proto = root.lookupType(output_type) @@ -20,25 +35,78 @@ function gen_method(service:IService, method_id : number, request_type : string, // There is a built in library function do do this but it doesn't work recursively // https://github.com/dcodeIO/protobuf.js/issues/1041 // result_proto.toObject(temp, { enums: String, default: true }) - return fillDefaults(result_proto.name, result_proto.toObject(temp)) + //return fillDefaults(result_proto.name, result_proto.toObject(temp)) + return decode_buffer(result_proto, result); + } +} + +function gen_subscribe(service:IService, method_id : number, methodName : string, output_type : string) { + const resultProto = root.lookupType(output_type) + let state = {}; + return (qualifier, callback) => { + console.log(`--subscribe-- ${service.name}:${method_id}`) + const id = service.context.subscribe(service.name, service.qualifier, method_id, methodName, qualifier, (updates) => { + // iterate and decode each update + let result : Update = { all: {}, added: {}, changed: {}, removed: {} }; + updates.forEach((value:RawUpdate) => { + const key = value.key; + switch(value.updateType) { + case RawUpdateType.CREATE: { + var obj = decode_buffer(resultProto, value.buffer) // resultProto.decode(value.buffer) + state[key] = obj + result.added[key] = obj + } + break; + case RawUpdateType.REMOVE: { + var obj = state[key] + delete state[key] + result.removed[key] = obj + } + break; + case RawUpdateType.REPLACE: { + var oldItem = state[key] + var newItem = decode_buffer(resultProto, value.buffer)//resultProto.decode(value.buffer); + state[key] = newItem; + result.changed[key] = { new : newItem, old : oldItem }; + } + break; + case RawUpdateType.RESET: { + state = {}; + } + break; + } + }); + result.all = { ...state }; + callback(result); + }); + return { + Dispose : () => { + service.context.unsubscribe(id, service.name, service.qualifier); + } + } } } export class DynamicService { - constructor(context : ServiceContext, name : string, qualifier:string, methods : Array<[string,number,string,string]>) { + constructor( + context : ServiceContext, + name : string, qualifier:string, + methods : Array<[string,number,string,string]>, + streams : Array<[string,number,string]>) { + const svc = { context, name, qualifier } - for (var m of methods) { - // m[0] = method_name - // m[1] = method id - // m[2] = input type name - // m[3] = output type name - this[m[0]] = gen_method(svc, m[1], m[2], m[3]) + for (var [methodName, id, inType, outType] of methods) { + this[methodName] = gen_method(svc, id, inType, outType) + } + for (var [streamName,id,typeName] of streams) { + const fnName = "subscribe_" + streamName; + this[fnName] = gen_subscribe(svc, id, streamName, typeName) } } } export function fillDefaults(ptype:string, value:any) { - // This function could be replaced with toObject(value, + // This function could be replaced with toObject(value, switch(ptype) { case "double": case "float": @@ -54,7 +122,7 @@ export function fillDefaults(ptype:string, value:any) { case "string": return value||"" default: const type = root.lookupTypeOrEnum(ptype) - if (isEnum(type)) { + if (isEnum(type)) { const intval = value||0; return type.valuesById[intval] } @@ -74,8 +142,8 @@ export function fillDefaults(ptype:string, value:any) { } } -function isEnum(mtype:any) { return mtype && ('valuesById' in mtype); } +function isEnum(mtype:any) { return mtype && ('valuesById' in mtype); } export function merge(a:T, b:object) : T { return { ... (a as any), ... b } -} \ No newline at end of file +} diff --git a/src/web/api/src/ServiceContext.ts b/src/web/api/src/ServiceContext.ts index c0efa340d..cd448ec86 100644 --- a/src/web/api/src/ServiceContext.ts +++ b/src/web/api/src/ServiceContext.ts @@ -1,5 +1,5 @@ import {root} from 'proto' -import { assertValid } from 'bts_assert' +import { assert, assertValid } from 'bts_assert' function default_ws_uri() { if (typeof window == 'undefined') return 'ws://sparkvm:8000/' @@ -21,6 +21,8 @@ const GatewayLogin = root.lookupType("bts.api.gateway.GatewayLogin") const GatewayRequest = root.lookupType("bts.api.gateway.GatewayRequest") const GatewayResponse = root.lookupType("bts.api.gateway.GatewayResponse") const GatewayCallHeader = root.lookupType("bts.api.gateway.GatewayCallHeader") +const GatewaySubscribe = root.lookupType("bts.api.gateway.GatewaySubscribe") +const GatewayUnsubscribe = root.lookupType("bts.api.gateway.GatewaySubscribe") const ResponseHeader = root.lookupType("bts.api.ResponseHeader") class PrivateContext { @@ -105,7 +107,7 @@ class PrivateContext { onmessage(e){ const raw_buffer = new Uint8Array(e.data) const response = GatewayResponse.decode(raw_buffer) - if (response && response.callResult) { + if (response && response.callResult && response.callResult.length > 0) { const call_buffer = response.callResult const header_size = call_buffer[0] const sub = call_buffer.subarray(1, header_size) @@ -114,11 +116,28 @@ class PrivateContext { // console.log(`onmessage: [${call_buffer}] h:[${sub}] b:[${body}]`) this.sc.on_msg(header, body) } + else if (response && response.tick) { + this.sc.on_tick(response.tick); + } else if (response && response.error) { console.log(response.error); } } } +// matches apigateway.proto :: StreamUpdate::UpdateType +export enum RawUpdateType { + REMOVE = 0, REPLACE = 1, CREATE = 2, RESET = 3 +} +export interface RawUpdate { + updateType : RawUpdateType + key : number + buffer : Uint8Array +} + +interface RawSubscription { + id: number + callback : (u:RawUpdate[]) => void +} export class ServiceContext { next_id : number @@ -127,11 +146,13 @@ export class ServiceContext { token: string can_create: boolean outstanding : { [request:number]:{ resolve : (buffer)=>void, reject: (msg)=>void } } + private subscriptions : { [id:number]:RawSubscription } constructor(send : (msg)=>void) { this.next_id = 1000 this.send = send this.outstanding = {} + this.subscriptions = {} } call(service_type : string, qualifier : string, method_id : number, body : Uint8Array) : Promise { @@ -154,7 +175,44 @@ export class ServiceContext { this.send(message) }); } - subscribe() {/*not impl*/} + + subscribe(serviceType: string, serviceQualifier:string, + methodId: number, methodName : string, + streamQualifier : string, callback: (x:RawUpdate[])=>void) : number { + // Send a subscription request to the server + // Record the id of this subscription in the subscriptions table + // When a subscription message arrives call the callback + const id = ++this.next_id + let subReq = GatewaySubscribe.create({ + requestId: id, + method: methodId, + token: this.token, + serviceType : serviceType, + serviceQualifier: serviceQualifier, + streamQualifier: streamQualifier, + methodName : methodName + }); + let message = GatewayRequest.create({ + subscribe : subReq + }); + assertValid(GatewayRequest, message) + this.subscriptions[id] = { + id, + callback + }; + this.send(message); + console.log(`Subscribe ${serviceType}:${methodId}`); + console.log(this.subscriptions); + return id; + } + + unsubscribe(subscriptionId : number, serviceType : string, serviceQualifier : string) { + let req = GatewayUnsubscribe.create({subscriptionId, serviceType, serviceQualifier}); + let message = GatewayRequest.create({ + unsubscribe: req + }); + this.send(message); + } on_msg(header: {requestId:number, code: number}, body : Uint8Array) { // console.log("message received: " + JSON.stringify(header) + " => " + (header.requestId in this.outstanding)); @@ -172,6 +230,22 @@ export class ServiceContext { console.log("unexpected response") } } + + + on_tick(update : {id:number, updates : [any]}) { + // updates type is defined in apigateway.proto + // I could make an interface here but nothing would keep it in sync with the proto + // so we may as well do it live + if (update.id && this.subscriptions) { + const s = this.subscriptions[update.id]; + if (s) { + console.log("on tick"); + console.log(update); + assert(update.id == s.id); + s.callback(update.updates); + } + } + } } diff --git a/src/web/ui/sass/main.scss b/src/web/ui/sass/main.scss index dbf86783d..5cad50f26 100644 --- a/src/web/ui/sass/main.scss +++ b/src/web/ui/sass/main.scss @@ -214,7 +214,11 @@ body { } } - +.tabs { + .is-danger, .is-danger a { + color:red; + } +} .is-tab-content { $tab-content-left-margin: 3rem; @@ -289,6 +293,13 @@ body { padding: 1px; vertical-align: middle; } + td.border-row { + border-width:0; + padding:0; + border-top-width: 3px; + border-top-style: solid; + height: 0; + } thead th { padding: 0.4rem 2rem 0.4rem 0.625em; background: $primary; @@ -308,6 +319,11 @@ body { background-color: $table-row-hover-background-color; } } + tr.is-invalid, td.is-invalid { + border:1px solid red; + border-width: 2px 1px 2px 1px; + height:2.7rem; + } .control, .select { width:100%; height: 2.5rem; @@ -355,4 +371,8 @@ body { .disabled-label { color: $grey; -} \ No newline at end of file +} + +.is-100-width { + width: 100px +} diff --git a/src/web/ui/src/Access.fs b/src/web/ui/src/Access.fs index 5795ef920..883319c04 100644 --- a/src/web/ui/src/Access.fs +++ b/src/web/ui/src/Access.fs @@ -101,13 +101,28 @@ module Accounts = } } riskLimitEnforcement = - { productLimitInNetTerms = true } + { productLimitInNetTerms = true + ignoreSpreadPositionLimits = false } accountLossLimit = { enforce = false totalLossLimit = 0u } } - let emptyMargin : ExcessLiquidityReport = { +let emptyManualFill:position.ManualFill = + ({ + recordId = int 0 + securityKey = uint32 0u + accountId = uint32 0u + accountName = "" + userId = uint32 1 + side = Side.BUY + quantity = uint32 0u + price = double 0.0 + fillType = bts.api.position.ManualFill.FillType.AdminSod + timestamp = { seconds = int64 0; nanos = 0 } + }:bts.api.position.ManualFill) + +let emptyMargin : ExcessLiquidityReport = { account = "ERROR NO DATA" accountId = 0u marginResult = diff --git a/src/web/ui/src/Accounts/State.fs b/src/web/ui/src/Accounts/State.fs index c1f3f8261..48a628c19 100644 --- a/src/web/ui/src/Accounts/State.fs +++ b/src/web/ui/src/Accounts/State.fs @@ -1,13 +1,16 @@ module Accounts.State +open Fable.PowerPack.Fetch open Accounts.Types open bts.api.admin +open bts.api.position open Global open Elmish open Fable.PowerPack.PromiseImpl open Fable.Import open bts.co2 open Fable +open Fable.Core open Fable.PowerPack open bts.api.position @@ -16,12 +19,88 @@ let isNewAccount (account:BTSAccount) = let currentAccountIsDefault (account:BTSAccount) = (account.id = 0u) +let userToId userName model = + match model.adminData with + | Some ad -> let u = Array.tryFind (fun (i:BTSUser) -> i.name = userName) ad.users + match u with | Some btsu -> btsu.id | None -> uint32 0 + | None -> uint32 0 + +let idToUser userID model = + match model.adminData with + | Some ad -> let u = Array.tryFind (fun (i:BTSUser) -> i.id = userID) ad.users + match u with | Some btsu -> btsu.name | None -> "" + | None -> "" + +let stringToSide side = + match side with + | "Buy" -> bts.api.Side.BUY + | "Sell" -> bts.api.Side.SELL + | _ -> bts.api.Side.BUY + +let sideToString side = + match side with + | bts.api.Side.BUY -> "Buy" + | bts.api.Side.SELL -> "Sell" + | _ -> "Buy" + + +let emptyTempManualFill = {id = 0u; newId = 0u; qty = "0"; price = "0"; side="Buy"; user=""; venue=""; symbol=""; expiration=""; SecurityId="0"; fillType = ManualFill.FillType.AdminDay } + +let validateManualFill (fill:TempFillData) = + let parsedQty = System.Int32.TryParse fill.qty + fill.SecurityId <> "0" + && fill.user <> "" + && parsedQty |> fst + && snd parsedQty >= 1 + && System.Double.TryParse fill.price |> fst + +let convertTempManualFIll (tempFill:TempFillData) (model: Model) = + let dto = System.DateTimeOffset (System.DateTime.UtcNow) + { emptyManualFill with + recordId = int tempFill.id + securityKey = uint32 tempFill.SecurityId + accountId = uint32 model.scratch.id + accountName = model.scratch.name + userId = (userToId tempFill.user model) + side = (stringToSide tempFill.side) + quantity = uint32 tempFill.qty + price = double tempFill.price + fillType = tempFill.fillType + timestamp = { seconds = dto.ToUnixTimeSeconds(); nanos = 0 } + } + +let convertTempFillList (fills:TempFillData list) (model:Model) = + let lst = + fills + |> List.filter (fun mf -> mf |> validateManualFill) + |> List.map (fun mf -> convertTempManualFIll mf model) + { + manualFill = Array.ofList lst + } + +let convertManualFIll (fill:ManualFill) (model: Model) = + { emptyTempManualFill with + id = uint32 fill.recordId + SecurityId = string fill.securityKey + user = (idToUser fill.userId model) + side = (sideToString fill.side) + qty = string fill.quantity + price = string fill.price + fillType = fill.fillType + } + let init (context:ServiceContext.ServiceContext) = let admin = AdminService.Create context "" + let posServ = PositionService.Create context "" + let basePosServ = BasePositionService.Create context "" let mdr = MarketDataRelay.Create context "" let span = MarginService.Create context "" { tab = General admin = admin + context = Some context + unsub = None + basePositionService = basePosServ + positionService = posServ marginService = span marginData = emptyMargin marginScratch = emptyMargin @@ -43,8 +122,70 @@ let init (context:ServiceContext.ServiceContext) = can_create = context.can_create this_username = context.username ShowLoading = false + ManualFillData = { + fills = { manualFill = (Array.empty: bts.api.position.ManualFill [])} + newFills = list.Empty + updatedFills = list.Empty + deletedFills = { manualFill = (Array.empty: bts.api.position.ManualFill [])} + tempSODFillData = emptyTempManualFill + tempDayFillData = emptyTempManualFill + } }, Access.getAdminData admin UpdateAdminData NoAdminData +let positionSub model dispatch = + match model.unsub with + | Some (unsub) -> unsub.Dispose() + | None -> () + let svc = model.positionService + svc.subscribe_manual_fills "" ( + fun (subUpdate:StreamUtils.IUpdate) -> + subUpdate.all + |> JSMap.toArray + |> Array.map snd + |> SetPositionData + |> dispatch + ) + + |> SetUnsub + |> dispatch + +let marginSub model dispatch = + match model.unsub with + | Some (unsub) -> unsub.Dispose() + | None -> () + let svc = model.marginService + svc.subscribe_excess_liquidity_report "" ( + fun (subUpdate:StreamUtils.IUpdate) -> + (* + The update does not seem to contain any data + and there is no way to specify the account. + this should be revisited + *) + + //subUpdate.all + //|> JSMap.toArray + //|> Array.map snd + //|> UpdateMarginData + //|> dispatch + + GetMarginData |> dispatch + ) + |> SetUnsub + |> dispatch + + +let selectSub (tab:Tab option) model = + match tab, model.tab with + | Some PositionManagementTab, _ | None, PositionManagementTab -> Cmd.ofSub (positionSub model) + | Some MarginTab, _ | None, MarginTab -> Cmd.ofMsg GetMarginData //Cmd.ofSub (marginSub model) + | _ -> + match model.unsub with + | Some (unsub) -> unsub.Dispose() + | None -> + System.Console.WriteLine "...?" + () + Cmd.none + let existingSelection (scratch:BTSAccount) (previousScratch:BTSAccount) (data:AdminData) = List.choose id [ data.accounts |> Array.tryFind (fun a -> a.id = scratch.id || (scratch.id = 0u && a.name = scratch.name)) @@ -80,33 +221,50 @@ let applyICEUpdate (data:ICEAccountData) = function let applySIMUpdate (data:SIMAccountData) = function | CTI v -> { data with cti = v } + //| SIMExchangeField.Origin v -> { data with origin = v } | Origin v -> { data with origin = v } | ClearingAccount v -> { data with clearingAccount = v } -let saveAccount (admin:IAdminService) (account:BTSAccount) (model:Model) = promise { +let saveAccount (admin:IAdminService) (account:BTSAccount) (manualFillData:ManualFillData) (model:Model) = promise { let! result = admin.update_account { completeData = account } 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 } + let! result5 = model.positionService.create_manual_fill (convertTempFillList manualFillData.newFills model) + let! result6 = model.positionService.update_manual_fill (convertTempFillList manualFillData.updatedFills model) + let! result7 = model.positionService.remove_manual_fill manualFillData.deletedFills - if (result.isSuccess && result2.success && result3.success && result4.success) then + let result5Msg = result5.response |> Array.tryFind (fun i -> (not i.ok)) + let result6Msg = result6.response |> Array.tryFind (fun i -> (not i.ok)) + let result7Msg = result7.response |> Array.tryFind (fun i -> (not i.ok)) + + match result.isSuccess, result2.success, result3.success, result4.success, result5Msg, result6Msg, result7Msg with + //match result.isSuccess, true, true, true, result5Msg, result6Msg, result7Msg with + | false, _, _, _, _, _, _ -> + return ErrorOccured (sprintf "Error saving account: %s" result.errorText) + | _, false, _, _, _, _, _ -> + return ErrorOccured (sprintf "Error leverage factor: %s" result.errorText) + | _, _, false, _, _, _, _ -> + return ErrorOccured (sprintf "Error Enforce Margin: %s" result.errorText) + | _, _, _, false, _, _, _ -> + return ErrorOccured (sprintf "Error cash balance: %s" result.errorText) + | _, _, _, _, Some r, _, _ -> + return ErrorOccured (sprintf "Error saving positions: %s" r.msg) + | _, _, _, _, _, Some r, _ -> + return ErrorOccured (sprintf "Error updating positions: %s" r.msg) + | _, _, _, _, _, _, Some r -> + return ErrorOccured (sprintf "Error deleting positions: %s" r.msg) + | _ -> 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 cash balence: %s" result3.errorMessage) + } let createAccount (admin:IAdminService) (account:BTSAccount) (model:Model) = promise { let args = { parentAccountId = 0u; accountName = account.name; description = account.description; creatorUser = model.this_username } let! result = admin.create_account args if result.isSuccess then - return! saveAccount admin { account with id = result.newId } model + return! saveAccount admin { account with id = result.newId } model.ManualFillData model else return ErrorOccured (sprintf "Error creating account: %s" result.errorText) } @@ -129,6 +287,26 @@ let cancelOrders (admin:IAdminService) (accountName:string) (orders:OrdersSummar return UpdateAdminData data } +let parseGMIFile (data:string) (model:Model) = + promise { + let! result = model.basePositionService.parse_gmi_csv { text = data } + + match result.success.positions.Length with + | 0 -> return ErrorOccured result.failure + | _ -> return SavePositionBatch result.success + + } + +let setBasePosition basePosBatch (model:Model) = + promise { + let! result = model.positionService.set_base_position basePosBatch + + match result.ok with + | true -> Browser.console.log result.msg + | false -> Browser.console.log result.msg + + return (Msg.SelectTab model.tab) + } let requestProducts (mdRelay:IMarketDataRelay) = promise { let! result = mdRelay.request_products() @@ -153,6 +331,7 @@ let clearSelection model = ShowLoading = false } + let orderIsEqual (order1:OrdersSummaryUnit) (order2:OrdersSummaryUnit) = (order1.account = order2.account) && (order1.user = order2.user) @@ -183,6 +362,19 @@ let modifyProductPositionRow limits (oldRow:AccountProductPositionLimitUnit) (ne let right = limits.[1+ind..] Array.concat [left; right] +let clearValidNewFills model = + model.ManualFillData.newFills |> List.filter (validateManualFill >> not) + +let clearManualFill model = + {model with ManualFillData = {model.ManualFillData with newFills = clearValidNewFills model; updatedFills = []; deletedFills = {manualFill = Array.empty}}} + +let validateManualFillQty value = + let nv = System.Int32.TryParse value + match value.Length, fst nv, snd nv with + | 0, _, _ -> true + | _, true, v when v > 0 -> true + | _, _, _ -> false + let update msg (model:Model) : Model * Cmd = match msg with | UpdateAdminData data -> @@ -193,6 +385,7 @@ let update msg (model:Model) : Model * Cmd = | NoAdminData msg -> Global.alert Global.Info msg model, Cmd.Empty + | SetUnsub u -> { model with unsub = Some u }, Cmd.none | GetOrdersData -> { model with ShowLoading = false}, (Access.getAccountOrdersData model.admin UpdateOrdersData (box>>ErrorOccured)) | UpdateOrdersData summary -> @@ -214,11 +407,24 @@ let update msg (model:Model) : Model * Cmd = | _ -> [] {model with SelectedOrders = so}, Cmd.Empty | SelectAccount account -> - { (clearSelection model) with previousScratch = model.scratch; scratch = account; marginScratch = emptyMargin }, Cmd.ofMsg <| GetMarginData + { (clearSelection model) with previousScratch = model.scratch + scratch = account + ManualFillData = { + fills = { manualFill = (Array.empty: bts.api.position.ManualFill [])} + newFills = list.Empty + updatedFills = list.Empty + deletedFills = { manualFill = (Array.empty: bts.api.position.ManualFill [])} + tempSODFillData = emptyTempManualFill + tempDayFillData = emptyTempManualFill + } + marginScratch = emptyMargin + }, selectSub None model | 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) + let cmd = match model.tab with + | MarginTab -> Cmd.ofPromise (fun _ -> promise { do! Promise.sleep 10000 } ) () (fun _ -> GetMarginData) (fun _ -> GetMarginData) + | _ -> Cmd.none 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), @@ -239,6 +445,12 @@ let update msg (model:Model) : Model * Cmd = { model with marginScratch = { model.marginScratch with leverageFactor = newFactor}},Cmd.Empty | SetEnforceMargin enf -> { model with marginScratch = { model.marginScratch with shouldEnforceMargin = enf}},Cmd.Empty + + | ParseCSV (t, data) -> + model, Cmd.ofExistingPromise (parseGMIFile data model) id (box>>ErrorOccured) + | SavePositionBatch bpb -> + model, Cmd.ofExistingPromise (setBasePosition bpb model) id (box>>ErrorOccured) + | 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 @@ -252,7 +464,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 with marginScratch = model.marginData }, 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.ManualFillData model) id (box>>ErrorOccured) | ErrorOccured msg -> Global.alert Global.Error (string msg) Browser.console.log msg @@ -260,11 +472,17 @@ let update msg (model:Model) : Model * Cmd = | SelectTab tab -> match tab with | OrdersTab -> - { (clearSelection model) with tab = tab }, Cmd.ofMsg GetOrdersData + { (clearSelection model) with tab = tab }, Cmd.batch [ + Cmd.ofMsg GetOrdersData + selectSub (Some OrdersTab) model + ] + | PositionManagementTab -> + { (clearSelection model) with tab = tab }, selectSub (Some PositionManagementTab) model | MarginTab -> - { (clearSelection model) with tab = tab }, Cmd.ofMsg GetMarginData + { (clearSelection model) with tab = tab }, selectSub (Some MarginTab) model | _ -> - { (clearSelection model) with tab = tab }, Cmd.Empty + { (clearSelection model) with tab = tab }, selectSub (Some tab) model + | Update field -> { model with scratch = applyUpdate model.scratch field }, Cmd.Empty | ClipLimits (oldLimit, newLimit) -> @@ -331,8 +549,9 @@ let update msg (model:Model) : Model * Cmd = let limits = erase model.scratch.productPositionLimits i { model with scratch = { model.scratch with productPositionLimits = limits } }, Cmd.Empty | EnforceProductLimitInNetTerms value -> - {model with scratch = {model.scratch with riskLimitEnforcement = {productLimitInNetTerms = value}}}, Cmd.none - + {model with scratch = {model.scratch with riskLimitEnforcement = { model.scratch.riskLimitEnforcement with productLimitInNetTerms = value } } }, Cmd.none + | IgnoreSpreadPositionLimits ig -> + {model with scratch = {model.scratch with riskLimitEnforcement = { model.scratch.riskLimitEnforcement with ignoreSpreadPositionLimits = ig } } }, Cmd.none | EnforceLossLimit e -> { model with scratch = { model.scratch with accountLossLimit = { model.scratch.accountLossLimit with enforce = e} } }, Cmd.Empty | TotalLossLimit tll -> @@ -376,8 +595,88 @@ 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.ofMsg GetMarginData + { (clearManualFill model) with mdRelayExpNameData = data }, selectSub None model | OpenOptionsMenu acc -> {model with OptionsMenuOpen = acc}, Cmd.Empty | OpenConfirmModal (title, okMessage) -> {model with ConfirmModal = title, okMessage}, Cmd.Empty + | SetPositionData data-> + { model with ManualFillData = {model.ManualFillData with fills = { manualFill = data }}}, Cmd.none + | RemoveManualFill (badFillId, isNew) -> + match isNew with + | false -> + let badFill = Array.filter (fun i -> i.recordId = badFillId) model.ManualFillData.fills.manualFill + let newDeletedFills = { manualFill = Array.append model.ManualFillData.deletedFills.manualFill [|badFill.[0]|]} + { model with ManualFillData = {model.ManualFillData with deletedFills = newDeletedFills} }, Cmd.Empty + | true -> + let newAdds = List.filter (fun (i:TempFillData) -> i.newId <> uint32 badFillId) model.ManualFillData.newFills + { model with ManualFillData = {model.ManualFillData with newFills = newAdds} }, Cmd.Empty + + | AddManualFill isSOD -> + let fType = match isSOD with true -> ManualFill.FillType.AdminSod | false -> ManualFill.FillType.AdminDay + let nID = match model.ManualFillData.newFills.Length with 0 -> 0u | _ -> ((model.ManualFillData.newFills |> List.maxBy (fun i -> i.newId)).newId + 1u) + let newFills = model.ManualFillData.newFills @ [{ emptyTempManualFill with newId = nID; fillType = fType }] + { model with ManualFillData = {model.ManualFillData with newFills = newFills; tempDayFillData = {model.ManualFillData.tempDayFillData with qty = ""; price = ""} }}, Cmd.Empty + + | UpdateManualFill (id, input, place, isNew) -> + match isNew with + | false -> + let fill = match List.tryFind (fun (t:TempFillData) -> t.id = id) model.ManualFillData.updatedFills with + | None -> + let f = Array.find (fun (o:ManualFill) -> o.recordId = int id) model.ManualFillData.fills.manualFill + convertManualFIll f model + | Some f -> f + + let updatedFill = match place with + | "qty" -> if (validateManualFillQty input) then { fill with qty = input} else fill + | "price" -> { fill with price = input} + | "side" -> { fill with side = input} + | "user" -> { fill with user = input} + | "venue" -> { fill with venue = input; symbol = ""; SecurityId = "0" } + | "symbol" -> { fill with symbol = input; SecurityId = "0" } + | "expiration" -> { fill with expiration = input} + | "SecurityId" -> { fill with SecurityId = input} + | _ -> fill + + let filteredList = List.filter (fun (g:TempFillData) -> g.id <> id) model.ManualFillData.updatedFills + let finalList = filteredList @ [updatedFill] + match Array.tryFind (fun i -> i.recordId = int id) model.ManualFillData.deletedFills.manualFill with + | Some _ -> model, Cmd.Empty + | None -> { model with ManualFillData = {model.ManualFillData with updatedFills = finalList}}, Cmd.Empty + | true -> + let fill = match List.tryFind (fun (t:TempFillData) -> t.newId = id) model.ManualFillData.newFills with + | None -> emptyTempManualFill + | Some f -> f + + let updatedFill = match place with + | "qty" -> if (validateManualFillQty input) then { fill with qty = input} else fill + | "price" -> { fill with price = input} + | "side" -> { fill with side = input} + | "user" -> { fill with user = input} + | "venue" -> { fill with venue = input; symbol = ""; SecurityId = "0" } + | "symbol" -> { fill with symbol = input; SecurityId = "0" } + | "expiration" -> { fill with expiration = input} + | "SecurityId" -> { fill with SecurityId = input} + | _ -> fill + + let filteredList = List.filter (fun (g:TempFillData) -> g.newId <> id) model.ManualFillData.newFills + let finalList = filteredList @ [updatedFill] + let sortedList = finalList |> List.sortBy (fun i -> i.newId) + + { model with ManualFillData = {model.ManualFillData with newFills = sortedList}}, Cmd.Empty + + | CreateManualFillUpdate (id, venue, symbol) -> + let fill = match List.tryFind (fun (t:TempFillData) -> t.id = id) model.ManualFillData.updatedFills with + | None -> + let f = Array.find (fun (o:ManualFill) -> o.recordId = int id) model.ManualFillData.fills.manualFill + convertManualFIll f model + | Some f -> f + + let updatedFill = { fill with venue = venue; symbol = symbol} + + let finalList = model.ManualFillData.updatedFills @ [updatedFill] + match Array.tryFind (fun i -> i.recordId = int id) model.ManualFillData.deletedFills.manualFill with + | Some _ -> model, Cmd.Empty + | None -> { model with ManualFillData = {model.ManualFillData with updatedFills = finalList}}, Cmd.Empty + + diff --git a/src/web/ui/src/Accounts/Types.fs b/src/web/ui/src/Accounts/Types.fs index 98fdfd77e..fa7daafcc 100644 --- a/src/web/ui/src/Accounts/Types.fs +++ b/src/web/ui/src/Accounts/Types.fs @@ -14,6 +14,7 @@ type Tab = | ExchangeSettingsTab | ConnectionsTab | OrdersTab + | PositionManagementTab | MarginTab let venues = [| Venue.GLBX; Venue.ICE; Venue.SIM |] @@ -48,9 +49,21 @@ type SIMExchangeField = | Origin of CustomerOrFirm | ClearingAccount of string +type PositionSecurity = + { + securityName: string + venue: bts.api.Venue + symbol: string + } + +type FileType = + | GMI + | Money + type Msg = | UpdateAdminData of AdminData | NoAdminData of string + | SetUnsub of System.IDisposable // Orders | GetOrdersData | UpdateOrdersData of OrdersSummary @@ -63,6 +76,9 @@ type Msg = | SetMarginCashFactor of double | SetEnforceMargin of bool + | ParseCSV of FileType * string + | SavePositionBatch of BasePositionBatch + | SelectAccount of BTSAccount | DeleteAccount of BTSAccount | AddAccount @@ -89,6 +105,13 @@ type Msg = | EnforceLossLimit of bool | TotalLossLimit of uint32 + | IgnoreSpreadPositionLimits of bool + + | SetPositionData of ManualFill [] + | RemoveManualFill of int * bool + | AddManualFill of bool + | UpdateManualFill of uint32 * string * string * bool + | CreateManualFillUpdate of uint32 * string * string | SecurityLimits of AccountSecurityPositionLimitUnit[] | DeleteSecurityLimit of int @@ -105,10 +128,35 @@ type Msg = | OpenOptionsMenu of string | OpenConfirmModal of string * Msg +type TempFillData = { id: uint32 + newId: uint32 + qty:string + price:string + side:string + user:string + venue:string + symbol:string + expiration:string + SecurityId:string + fillType: ManualFill.FillType + } + +type ManualFillData = { + fills: ManualFillBatch + newFills: TempFillData list + updatedFills: TempFillData list + tempSODFillData: TempFillData + tempDayFillData: TempFillData + deletedFills: ManualFillBatch + } type Model = { tab : Tab admin : IAdminService + context : ServiceContext.ServiceContext option + unsub : System.IDisposable option + basePositionService: IBasePositionService + positionService: IPositionService marginService: IMarginService marginData: ExcessLiquidityReport marginScratch: ExcessLiquidityReport @@ -130,4 +178,5 @@ type Model = { can_create: bool this_username: string ShowLoading: bool -} \ No newline at end of file + ManualFillData: ManualFillData +} diff --git a/src/web/ui/src/Accounts/View.fs b/src/web/ui/src/Accounts/View.fs index 0371ebd80..5a7f68640 100644 --- a/src/web/ui/src/Accounts/View.fs +++ b/src/web/ui/src/Accounts/View.fs @@ -11,6 +11,7 @@ open Fable open System open bts.co2 open Elmish.React.Common +open System.Collections.Generic open Fable.Core.JsInterop open Fable.Core @@ -35,16 +36,29 @@ let isDirty model = model.marginData.shouldEnforceMargin <> model.marginScratch.shouldEnforceMargin match selectedAccount model with | Some account -> - account <> model.scratch || - marginDirty + account <> model.scratch + || model.ManualFillData.newFills.Length > 0 + || model.ManualFillData.updatedFills.Length > 0 + || model.ManualFillData.deletedFills.manualFill.Length > 0 + || marginDirty | None -> false +let validatePositionManagementTab filldata = + List.fold (fun a e -> a && validateManualFill e) true filldata.newFills + +let validate model = validatePositionManagementTab model.ManualFillData + let saveButton (model:Model) dispatch = let isNew = isNewAccount model.scratch let buttonText = if isNew then "Create" else "Save" let dirty = isDirty model || isNew - let enabled = if dirty then Button.Enabled else Button.Disabled - let flavor = if dirty then "is-success" else "" + let valid = validate model + let enabled = if dirty && valid then Button.Enabled else Button.Disabled + let flavor = match dirty, valid with + | true, true -> "is-success" + | true, false -> "is-danger" + | false, _ -> "" + Button.button buttonText flavor "" enabled (fun _ -> dispatch SaveScratch) let accountControls model (dispatch:Msg->unit) = @@ -111,6 +125,7 @@ let marginView model dispatch = ] className = "" header = None + onClick = None } let rws:TableControl.row = @@ -130,13 +145,14 @@ let marginView model dispatch = ] className = "" header = None + onClick = 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) + Fields.number "Leverage Factor:" (string model.marginScratch.leverageFactor) [Disabled disabled] (fun _ -> true) (double >> SetMarginCashFactor >> dispatch) + Fields.intNumber "Cash Balance:" (string model.marginScratch.cashBalance) [Disabled disabled] (double >> SetMarginCashBalance >> dispatch) accountControls model dispatch ] @@ -169,6 +185,7 @@ let ordersView (model:Model) dispatch = ] className = "" header = None + onClick = None } let rws:TableControl.row list = @@ -185,6 +202,7 @@ let ordersView (model:Model) dispatch = ] className = "" header = None + onClick = None } ] @@ -258,6 +276,7 @@ let clipLimitsView model dispatch = ] className = "" header = None + onClick = None } let footerRow:TableControl.row = { @@ -282,6 +301,7 @@ let clipLimitsView model dispatch = | true -> "" | false -> "is-hidden-touch is-hidden-desktop" header = None + onClick = None } let changeVenue (oldRow:AccountSecurityClipLimitUnit) (value:string) = @@ -382,6 +402,7 @@ let clipLimitsView model dispatch = | false -> "" | true -> "is-selected" header = None + onClick = None } yield footerRow ] @@ -434,6 +455,7 @@ let productLimits model dispatch = ] className = "" header = None + onClick = None } let footerRow:TableControl.row = @@ -468,6 +490,7 @@ let productLimits model dispatch = | true -> "" | false -> "is-hidden-touch is-hidden-desktop" header = None + onClick = None } let changeVenue (oldRow:AccountProductPositionLimitUnit) (value:string) = @@ -567,6 +590,7 @@ let productLimits model dispatch = | false -> "" | true -> "is-selected" header = None + onClick = None } ] @ [footerRow] @@ -598,6 +622,9 @@ let productLimits model dispatch = div [][ Fields.radioButton "Use Net Contracts" "" model.scratch.riskLimitEnforcement.productLimitInNetTerms [] (EnforceProductLimitInNetTerms >> dispatch) Fields.radioButton "Use Absolute Contracts" "" (not model.scratch.riskLimitEnforcement.productLimitInNetTerms) [] (not >> EnforceProductLimitInNetTerms >> dispatch) + div [] [ + Fields.checkbox "Ignore Spread Position Limits" "" model.scratch.riskLimitEnforcement.ignoreSpreadPositionLimits [] (IgnoreSpreadPositionLimits >> dispatch) + ] ] TableControl.createDataTable (Some headerRow) rws (Button.button "Add" "" "plus-circle" Button.Enabled addRow) br [] @@ -639,6 +666,7 @@ let securityLimitsView (model:Model) dispatch = ] className = "" header = None + onClick = None } let modifyRow (oldRow:AccountSecurityPositionLimitUnit) (newRow:AccountSecurityPositionLimitUnit) = @@ -720,6 +748,7 @@ let securityLimitsView (model:Model) dispatch = ] className = "" header = None + onClick = None } ] let emptyLimit = { @@ -751,6 +780,268 @@ let LimitsView (model:Model) dispatch = accountControls model dispatch ] +let venueToString (venue:bts.api.Venue) = + match venue with + | bts.api.Venue.UNSET -> "Unset" + | bts.api.Venue.BTS -> "BTS" + | bts.api.Venue.SIM -> "SIM" + | bts.api.Venue.CBOE -> "CBOE" + | bts.api.Venue.CBOECFE -> "CBOECFE" + | bts.api.Venue.GLBX -> "GLBX" + | bts.api.Venue.ICE -> "ICE" + +let exchange_display expirationNameList = + let s = sprintf "%O" expirationNameList.product.exchange + s.[6..s.Length] + +let getVenueList model = + (Array.toList model.mdRelayExpNameData) + |> List.map (exchange_display) + |> List.distinct + +let getSymbolList model venue = + (Array.toList model.mdRelayExpNameData) + |> List.filter (fun (i:ExpirationNameList) -> exchange_display i = venue) + |> List.map (fun (i:ExpirationNameList) -> sprintf "%O" i.product.symbol) + |> List.distinct + +let getPositionSecMap (sList:(uint32 * PositionSecurity) list) venue symbol = + sList + |> List.filter (fun i -> + (venueToString (snd i).venue = venue) && + (snd i).symbol = symbol + ) + |> List.map (fun i -> (sprintf "%O" (fst i)), (snd i).securityName) + |> List.distinct + |> List.sortBy (fun (y:string * string) -> Global.toSortableDate (snd y).[(snd y).Length - 2..]) + + +let SODPositionView model dispatch = + div [] + [ + div [ ClassName "columns" ][ + h1 [ ClassName "is-size-3 column is-narrow" ][str "SOD" ] + div [ ClassName "column is-size-7 has-text-grey-mid is-help-text"; ] + [str "Manually add fills for the Start of Day position"] + ] + ] + +let DayPositionView model dispatch = + let headerRow:TableControl.row = + { + cells = + [ + TableControl.controlCell (str "") + // TableControl.Cell (str "ID") + TableControl.Cell (str "Exchange") + TableControl.Cell (str "Symbol") + TableControl.Cell (str "Security") + TableControl.Cell (str "User") + TableControl.Cell (str "Side") + { content = (str "Qty"); className = "is-100-width"; isHeader = false; colSpan = 1 } + { content = (str "Price"); className = "is-100-width"; isHeader = false; colSpan = 1 } + TableControl.Cell (str "Time") + ] + className = "" + header = None + onClick = None + } + + let usersMap = + match model.adminData with + | Some ad -> List.map (fun (i:BTSUser) -> i.name) (List.ofArray ad.users) + | None -> List.Empty + + let securityList:(uint32 * PositionSecurity) list = + model.mdRelayExpNameData + |> List.ofArray + |> List.filter (fun i -> i.product.``type`` = bts.api.SecurityType.FUTURE) + |> List.collect (fun i -> + JSMap.toList i.names + |> List.map (fun x -> + (uint32 <| fst x), + ({ securityName = (snd x) + venue = i.product.exchange + symbol = i.product.symbol + }: PositionSecurity) + ) + ) + + + let securityMap = securityList |> Map.ofList + + let venueList = (getVenueList model) + + let extantUsersMap = + usersMap |> List.filter (fun (u:string) -> u.[..6] <> "DELETED") + + let filtered = Array.filter (fun (i:bts.api.position.ManualFill) -> i.fillType = bts.api.position.ManualFill.FillType.AdminDay && i.accountId = model.scratch.id) model.ManualFillData.fills.manualFill + + let fillRows:TableControl.row list = + [ for limit in filtered do + match (List.tryFind (fun (l:TempFillData) -> l.id = uint32 limit.recordId) model.ManualFillData.updatedFills), + (Array.tryFind (fun (l:bts.api.position.ManualFill) -> l.recordId = limit.recordId) model.ManualFillData.deletedFills.manualFill), + (securityMap |> Map.exists (fun i q -> i = limit.securityKey)) with + | _, _, false -> + yield { + cells = [] + className = "" + header = None + onClick = None + } + | _, Some _, _ -> + yield { + cells = [] + className = "" + header = None + onClick = None + } + | Some ulimit, None, _ -> + yield { + cells = + [ + TableControl.Cell (Button.deleteButton Button.IsDisabled.Enabled (fun _ -> ((ulimit.id |> int), false)|> RemoveManualFill |> dispatch)) + // TableControl.Cell (span [Style [Color "green"]][str (sprintf "%i" ulimit.id)]) + TableControl.Cell (Fields.simpleSelect "" venueList (ulimit.venue) (fun v -> (ulimit.id, v, "venue", false) |> UpdateManualFill |> dispatch)) + TableControl.Cell (Fields.simpleSelect "" (getSymbolList model ulimit.venue) (ulimit.symbol) (fun v -> (ulimit.id, v, "symbol", false) |> UpdateManualFill |> dispatch)) + TableControl.Cell (Fields.tupleMapSelect + "" + (getPositionSecMap + securityList + (ulimit.venue) + (ulimit.symbol) + ) + (match ulimit.SecurityId with + | "0" -> None + | _ -> Some securityMap.[uint32 ulimit.SecurityId].securityName) + (Some ulimit.SecurityId) + (fun v -> (ulimit.id, v, "SecurityId", false) |> UpdateManualFill |> dispatch) + None + ) + TableControl.Cell (Fields.simpleSelect "" extantUsersMap ulimit.user (fun v -> (ulimit.id, v, "user", false) |> UpdateManualFill |> dispatch)) + TableControl.Cell (Fields.simpleSelect "" ["Buy"; "Sell"] ulimit.side (fun v -> (ulimit.id, v, "side", false) |> UpdateManualFill |> dispatch)) + TableControl.Cell (Fields.positiveIntNumber + "" + ulimit.qty + [ ] + (fun v -> (ulimit.id, v, "qty", false) |> UpdateManualFill |> dispatch) + ) + TableControl.Cell (Fields.number + "" + ulimit.price + [ ] + (fun _ -> true) + (fun v -> (ulimit.id, v, "price", false) |> UpdateManualFill |> dispatch) + ) + TableControl.Cell (str (limit.timestamp.seconds |> uint64 |> int64 |> Global.epochToDateTimeString)) + ] + className = "" + header = None + onClick = None + } + | None, None, _ -> + yield { + cells = + [ + TableControl.Cell (Button.deleteButton Button.IsDisabled.Enabled (fun _ -> ((limit.recordId |> int), false) |> RemoveManualFill |> dispatch)) + // TableControl.Cell (str (sprintf "%i" limit.recordId)) + TableControl.Cell (str (venueToString securityMap.[limit.securityKey].venue)) + TableControl.Cell (str securityMap.[limit.securityKey].symbol) + TableControl.Cell (str securityMap.[limit.securityKey].securityName) + TableControl.Cell (str (idToUser limit.userId model)) + TableControl.Cell (str (match limit.side with | bts.api.Side.BUY -> "Buy" | bts.api.Side.SELL -> "Sell" | _ -> "")) + TableControl.Cell (str (sprintf "%i" limit.quantity)) + TableControl.Cell (str (sprintf "%f" limit.price)) + TableControl.Cell (str (limit.timestamp.seconds |> uint64 |> int64 |> Global.epochToDateTimeString)) + ] + className = "" + header = None + onClick = Some (fun _ -> (uint32 limit.recordId, (venueToString securityMap.[limit.securityKey].venue), securityMap.[limit.securityKey].symbol) |> CreateManualFillUpdate |> dispatch) + } + + ] + let newRows:TableControl.row list = + [ for limit in (List.filter (fun (i:TempFillData) -> i.fillType = bts.api.position.ManualFill.FillType.AdminDay) model.ManualFillData.newFills) do yield { + cells = + [ + TableControl.Cell (Button.deleteButton Button.IsDisabled.Enabled (fun _ -> ((limit.newId |> int), true) |> RemoveManualFill |> dispatch)) + // TableControl.Cell (span [Style [Color "green"]][str "NEW"]) + TableControl.Cell (Fields.simpleSelect "" venueList (limit.venue) (fun v -> (limit.newId, v, "venue", true) |> UpdateManualFill |> dispatch)) + TableControl.Cell (Fields.simpleSelect "" (getSymbolList model limit.venue) (limit.symbol) (fun v -> (limit.newId, v, "symbol", true) |> UpdateManualFill |> dispatch)) + TableControl.Cell (Fields.tupleMapSelect + "" + (getPositionSecMap + securityList + (limit.venue) + (limit.symbol) + ) + (match limit.SecurityId with + | "0" -> None + | _ -> Some securityMap.[uint32 limit.SecurityId].securityName + ) + (Some limit.SecurityId) + (fun v -> (limit.newId, v, "SecurityId", true) |> UpdateManualFill |> dispatch) + None + ) + TableControl.Cell (Fields.simpleSelect "" extantUsersMap limit.user (fun v -> (limit.newId, v, "user", true) |> UpdateManualFill |> dispatch)) + TableControl.Cell (Fields.simpleSelect "" ["Buy"; "Sell"] limit.side (fun v -> (limit.newId, v, "side", true) |> UpdateManualFill |> dispatch)) + TableControl.Cell (Fields.positiveIntNumber + "" + limit.qty + [ ] + (fun v -> (limit.newId, v, "qty", true) |> UpdateManualFill |> dispatch) + ) + TableControl.Cell (Fields.number + "" + limit.price + [ ] + (fun _ -> true) + (fun v -> (limit.newId, v, "price", true) |> UpdateManualFill |> dispatch) + ) + TableControl.Cell (span [Style [Color "green"]][str "NEW"]) + ] + className = match validateManualFill limit with true -> "" | false -> "is-invalid" + header = None + onClick = None + }] + + let dividerRow = match newRows.Length with 0 -> [] | _ -> [({ cells = [{ content = str ""; className = "border-row"; isHeader = false; colSpan = 9 }]; className = ""; header = None; onClick = None }:TableControl.row)] + let rws:TableControl.row list = fillRows @ dividerRow @ newRows + + div [] + [ + div [ ClassName "columns" ][ + h1 [ ClassName "is-size-3 column is-narrow" ][str "Day Fills" ] + div [ ClassName "column is-size-7 has-text-grey-mid is-help-text"; ] + [str "Manually add fills for the Day position"] + ] + TableControl.createDataTableWithStyle (Some headerRow) rws "" [MinWidth 824] ( + Button.button "Add" "" "plus-circle" Button.Enabled (fun _ -> AddManualFill <| false |> dispatch) + ) + + ] + +let PositionUploadView model dispatch = + div [] + [ + div [ ClassName "columns" ][ + h1 [ ClassName "is-size-3 column is-narrow" ][str "Upload" ] + div [ ClassName "column is-size-7 has-text-grey-mid is-help-text"; ] + [str "Add fills from a file for the Day position"] + ] + Fields.fileUpload false (fun f -> (ParseCSV (FileType.GMI, f)) |> dispatch) + br [] + ] + + +let PositionManagementView model dispatch = + div [] [ + //PositionUploadView model dispatch + //SODPositionView model dispatch + DayPositionView model dispatch + accountControls model dispatch + ] + let ConnectionsView (model:Accounts.Types.Model) dispatch = let conNames = model.scratch.connections.exchangeConnectionNames @@ -791,6 +1082,7 @@ let ConnectionsView (model:Accounts.Types.Model) dispatch = ] className="" header = None + onClick = None } ] @@ -802,6 +1094,7 @@ let ConnectionsView (model:Accounts.Types.Model) dispatch = ] className = "" header = None + onClick = None } div [ ] @@ -851,14 +1144,15 @@ let tabContent (model:Model) dispatch = div [Key (string model.scratch.id); ClassName "is-tab-content"] [ // (match model.tab with (match model.tab, (accountIsSelected model.scratch Access.Accounts.empty) with - | _ , false -> p [Style [ Color "Red" ]] [ str "You are not authorized to view this data" ] - | General, _ -> generalView model dispatch - | LimitsTab, _ -> LimitsView model dispatch - | ConnectionsTab, _ -> ConnectionsView model dispatch - | CreditLimitsTab, _ -> creditLimitsView model dispatch - | ExchangeSettingsTab, _ -> exchangeSettings model dispatch - | OrdersTab, _ -> ordersView model dispatch - | marginTab, _ -> marginView model dispatch) + | _ , false -> p [Style [ Color "Red" ]] [ str "You are not authorized to view this data" ] + | General, _ -> generalView model dispatch + | LimitsTab, _ -> LimitsView model dispatch + | ConnectionsTab, _ -> ConnectionsView model dispatch + | CreditLimitsTab, _ -> creditLimitsView model dispatch + | ExchangeSettingsTab, _ -> exchangeSettings model dispatch + | OrdersTab, _ -> ordersView model dispatch + | PositionManagementTab, _ -> PositionManagementView model dispatch + | marginTab, _ -> marginView model dispatch) ] let isActive account = function @@ -872,6 +1166,11 @@ let isOptionsMenuOpen accountName openName = let isActiveItem a b = if a = b then "is-active" else "" +let isValidTab tab model = + match tab with + | PositionManagementTab -> if not (validatePositionManagementTab model.ManualFillData) then "is-danger" else "" + | _ -> "" + let accountsMenu (accounts:BTSAccount[]) selectedAccount dispatch = div [ClassName "column is-narrow"] [ aside [ ClassName "menu box" ] [ @@ -904,6 +1203,7 @@ let accountSelectionGrid (accountList:BTSAccount list) (currentAccount:BTSAccoun ] className = "" header = None + onClick = None } let rws:TableControl.row list = [ for acc in activeAccounts do yield { @@ -917,6 +1217,7 @@ let accountSelectionGrid (accountList:BTSAccount list) (currentAccount:BTSAccoun ] className = "" header = None + onClick = None } ] div[][ @@ -992,11 +1293,12 @@ let tabName = function | ConnectionsTab -> "Connections" | ExchangeSettingsTab -> "Exchange Settings" | OrdersTab -> "Orders" + | PositionManagementTab -> "Position Management" | MarginTab -> "Margin" | other -> string other -let tabHeader tab currentTab dispatch = - li [ ClassName (isActiveItem tab currentTab) ] [ +let tabHeader tab model dispatch = + li [ ClassName ( sprintf "%s %s" (isActiveItem tab model.tab) (isValidTab tab model)) ] [ a [ OnClick (fun _ -> dispatch (SelectTab tab)) ] [ str <| tabName tab ] ] @@ -1021,13 +1323,14 @@ let root (model:Model) dispatch = div [ ClassName "tabs is-boxed" ] [ ul [] [ - (if (Array.length data.accounts > 0) then (tabHeader General model.tab dispatch) else span [][] ) - (if (Array.length data.accounts > 0) then (tabHeader LimitsTab model.tab dispatch) else span [][] ) - (if (Array.length data.accounts > 0) then (tabHeader ConnectionsTab model.tab dispatch) else span [][] ) + (if (Array.length data.accounts > 0) then (tabHeader General model dispatch) else span [][] ) + (if (Array.length data.accounts > 0) then (tabHeader LimitsTab model dispatch) else span [][] ) + (if (Array.length data.accounts > 0) then (tabHeader ConnectionsTab model dispatch) else span [][] ) // 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 [][] ) + (if (Array.length data.accounts > 0) then (tabHeader ExchangeSettingsTab model dispatch) else span [][] ) + (if (Array.length data.accounts > 0) then (tabHeader OrdersTab model dispatch) else span [][] ) + (if (Array.length data.accounts > 0) then (tabHeader PositionManagementTab model dispatch) else span [][] ) + (if (Array.length data.accounts > 0) then (tabHeader MarginTab model dispatch) else span [][] ) ] ] tabContent model dispatch diff --git a/src/web/ui/src/Controls/Button.fs b/src/web/ui/src/Controls/Button.fs index 3352611a6..e0cb985f9 100644 --- a/src/web/ui/src/Controls/Button.fs +++ b/src/web/ui/src/Controls/Button.fs @@ -11,14 +11,14 @@ type IsDisabled = let isDisabled = function | Disabled -> true | Enabled -> false - + type IsHidden = | Hidden | Visible - + let isHidden = function | Hidden -> true - | Visible -> false + | Visible -> false let boolToDisabledConverter d = match d with @@ -27,10 +27,11 @@ let boolToDisabledConverter d = let addButton disabled click = + let oclk = match isDisabled disabled with false -> click | true -> ignore a [ ClassName "button fabutton " HTMLAttr.Disabled (isDisabled disabled) Title "Add" - OnClick click ] [ + OnClick oclk ] [ span [ ClassName "icon is-small has-text-success" ] [ i [ ClassName "fa fa-plus"] [ ] ] ] diff --git a/src/web/ui/src/Controls/Fields.fs b/src/web/ui/src/Controls/Fields.fs index 4e4c4b0cc..08b53953e 100644 --- a/src/web/ui/src/Controls/Fields.fs +++ b/src/web/ui/src/Controls/Fields.fs @@ -151,9 +151,38 @@ let password (lbl:string) (dv:string) (attrs:IHTMLProp list) onchange = ] ] +let number (lbl:string) (dv:string) (attrs:IHTMLProp list) (validate: string -> bool) onchange = + let errorClass = match dv |> validate with + | true -> "" + | false -> "is-danger" + + div [ ClassName "field" ] + [ + Label lbl + div [ ClassName "control" ] + [ input ([ + ClassName (sprintf "input %s" errorClass) + Type "number" + Value (string dv) + OnChange (fun ev -> !!ev.target?value |> onchange) + ] @ attrs) + ] + ] + + + +let intNumber (lbl:string) (dv:string) (attrs:IHTMLProp list) onchange = + let validate value = fst <| System.Int32.TryParse value + number lbl dv attrs validate onchange + +let positiveIntNumber (lbl:string) (dv:string) (attrs:IHTMLProp list) onchange = + let validate value = + let itp = System.Int32.TryParse value + fst itp && snd itp >= 0 + + number lbl dv attrs validate onchange + -let number (lbl:string) (dv:int) (attrs:IHTMLProp list) onchange = - text lbl (string dv) attrs (int>>onchange) let number64 (lbl:string) (dv:int64) (attrs:IHTMLProp list) onchange = text lbl (string dv) attrs (int64>>onchange) @@ -221,6 +250,39 @@ let radioButton (lbl:string) (description:string) (dv:bool) (attrs:IHTMLProp lis ] +let tupleMapSelect (lbl:string) (options:(string * string) List) (defaultText:string option) (defaultValue:string option) (onchange:'a->unit) (is_disabled:bool option) = + let dis = + match is_disabled with + | None -> false + | Some a -> a + + let optList:ReactElement list = + match defaultText with + | None -> (option [] [str ""]) :: ([ for opt in options do yield option [ Value (fst opt) ] [ str (snd opt) ] ]) + | Some dt -> (option [] [str dt]) :: ([ for opt in options do yield option [ Value (fst opt) ] [ str (snd opt) ] ]) + + let dv = match defaultValue with + | Some d -> d + | None -> "" + + div [ ClassName "field" ] + [ + Label lbl + div [ ClassName "control" ] + [ + div [ ClassName "select"] + [ + select [ + Value dv + OnChange (fun ev -> !!ev.target?value |> onchange) + Disabled dis + ] + optList + ] + ] + ] + + let mapSelectEx (lbl:string) (options:Map) (defaultText:string option) (defaultValue:string option) (onchange:'a->unit) (is_disabled:bool option) = let dis = match is_disabled with @@ -229,14 +291,13 @@ let mapSelectEx (lbl:string) (options:Map) (defaultText:string op let optList:ReactElement list = match defaultText with - | None -> ([ for opt in options do yield option [ Value opt.Key ] [ str (opt.Value) ] ]) + | None -> (option [] [str ""]) :: ([ for opt in options do yield option [ Value opt.Key ] [ str (opt.Value) ] ]) | Some dt -> (option [] [str dt]) :: ([ for opt in options do yield option [ Value opt.Key ] [ str (opt.Value) ] ]) let dv = match defaultValue with | Some d -> d | None -> "" - div [ ClassName "field" ] [ Label lbl @@ -258,6 +319,10 @@ let mapSelect (lbl:string) (options:Map) (defaultText:string opti mapSelectEx lbl options defaultText defaultValue onchange (Some false) let simpleSelect (lbl:string) (options:string list) (defaultValue:string) (onchange:'a->unit) = + let opts = + match List.exists (fun i -> i = defaultValue) options with + | false -> options @ [defaultValue] + | _ -> options div [ ClassName "field" ] [ @@ -270,7 +335,7 @@ let simpleSelect (lbl:string) (options:string list) (defaultValue:string) (oncha Value defaultValue OnChange (fun ev -> !!ev.target?value |> onchange) ] - [ for opt in options do yield option [ Value opt ] [ str (opt) ] ] + [ for opt in opts do yield option [ Value opt ] [ str (opt) ] ] ] ] ] @@ -290,4 +355,39 @@ let menuSelect (options:string list) (onchange:'a->unit) = [ for opt in options do yield option [ Value opt ] [ str (opt) ] ] ] ] - ] \ No newline at end of file + ] + +let getFileData next (file:Import.Browser.File) = + let reader = Fable.Import.Browser.FileReader.Create () + reader.onload <- (fun _ -> + reader.result |> string |> next + ) + reader.readAsText file + +let fileUpload (multiple:bool) (onInput:string -> unit) = + div [ ClassName "field" ] + [ + div [ ClassName "control" ] + [ + div [ Class "file is-boxed" ] + [ label [ Class "file-label" ] + [ input [ Class "file-input" + Type "file" + Name "file-upload" + OnInput (fun (v:FormEvent) -> + v.target?files + |> unbox + |> Array.head + |> getFileData onInput + ) + Multiple multiple + ] + span [ Class "file-cta" ] + [ span [ Class "file-icon" ] + [ i [ Class "fa fa-upload" ] + [ ] ] + span [ Class "file-label" ] + [ str "Choose a file…" ] ] ] ] + ] + ] + diff --git a/src/web/ui/src/Controls/Table.fs b/src/web/ui/src/Controls/Table.fs index b4b55433b..6fcbd91cf 100644 --- a/src/web/ui/src/Controls/Table.fs +++ b/src/web/ui/src/Controls/Table.fs @@ -26,6 +26,7 @@ type row = { cells:cell list className:string header:string option + onClick: (MouseEvent -> unit) option } let createCell (inHeaderRow:bool) (c:cell) = @@ -34,11 +35,15 @@ let createCell (inHeaderRow:bool) (c:cell) = | true -> th [ ClassName c.className; ColSpan (float c.colSpan) ] [ c.content ] let createRow (isHeaderRow:bool) (r:row) = - match r.header, isHeaderRow with - | None, false -> tr [ ClassName r.className ] (List.map (createCell isHeaderRow) r.cells) - | None, true -> tr [ ClassName r.className ] (List.map (createCell isHeaderRow) r.cells) - | Some h, true -> tr [ ClassName r.className ] (th [] [str h] :: (List.map (createCell isHeaderRow) r.cells )) - | Some h, false -> tr [ ClassName r.className ] (th [] [str h] :: (List.map (createCell isHeaderRow) r.cells )) + match r.header, isHeaderRow, r.onClick with + | None, false, None -> tr [ ClassName r.className ] (List.map (createCell isHeaderRow) r.cells) + | None, true, None -> tr [ ClassName r.className ] (List.map (createCell isHeaderRow) r.cells) + | Some h, true, None -> tr [ ClassName r.className ] (th [] [str h] :: (List.map (createCell isHeaderRow) r.cells )) + | Some h, false, None -> tr [ ClassName r.className ] (th [] [str h] :: (List.map (createCell isHeaderRow) r.cells )) + | None, false, Some onC -> tr [ ClassName r.className; OnClick onC ] (List.map (createCell isHeaderRow) r.cells) + | None, true, Some onC -> tr [ ClassName r.className; OnClick onC ] (List.map (createCell isHeaderRow) r.cells) + | Some h, true, Some onC -> tr [ ClassName r.className; OnClick onC ] (th [] [str h] :: (List.map (createCell isHeaderRow) r.cells )) + | Some h, false, Some onC -> tr [ ClassName r.className; OnClick onC ] (th [] [str h] :: (List.map (createCell isHeaderRow) r.cells )) let createTableWithStyle (headers:row option) (rl:row list) className styles = let rws = List.map (createRow false) rl @@ -71,6 +76,23 @@ let createDataTable (headers:row option) (rl:row list) (buttons:ReactElement) = div [Class "is-clearfix "] [] ] +let createDataTableWithStyle (headers:row option) (rl:row list) (className:string) (style:CSSProp list) (buttons:ReactElement) = + let rws = List.map (createRow false) rl + let tbl = match headers with + | Some h -> + [ + thead [] [ createRow true h] + tbody [] rws + ] + | None -> + [ tbody [] rws ] + div [ Class "bts-data-table-container" ][ + table [ ClassName (sprintf "table bts-data-table %s" className); Style style ] tbl + div [Class "is-pulled-right table-buttons"] [ buttons ] + div [Class "is-clearfix "] [] + ] + + let createSelectorTableWithStyle (headers:row option) (rl:row list) className styles = let rws = List.map (createRow false) rl let tbl = match headers with diff --git a/src/web/ui/src/Global.fs b/src/web/ui/src/Global.fs index 41c76cca2..fb9b2c5b5 100644 --- a/src/web/ui/src/Global.fs +++ b/src/web/ui/src/Global.fs @@ -1,5 +1,6 @@ module Global open Fable.Core +open Fable.PowerPack let Class = Fable.Helpers.React.Props.HTMLAttr.ClassName @@ -13,6 +14,35 @@ let alert (_:AlertLevel) (_:string) = Fable.Core.Exceptions.jsNative let compareDouble (d1:double) (d2:double) (precision: uint32) = (int (d1 * (double precision))) = (int (d2 * (double precision))) +let epochToDateTimeString (time:int64) = + let dto = time |> System.DateTimeOffset.FromUnixTimeSeconds + dto.DateTime |> (Date.Format.localFormat Date.Local.englishUS "M/d/yyyy hh:mm tt") + + +let toSortableDate (badDate:string) = + let month = badDate.[0] + let year = badDate.[1] + + let curYear = System.DateTime.Now.Year + + let yearMap = Map.empty. + Add((string curYear).[3], curYear). + Add((string (curYear + 1 )).[3], curYear + 1). + Add((string (curYear + 2 )).[3], curYear + 2). + Add((string (curYear + 3 )).[3], curYear + 3). + Add((string (curYear + 4 )).[3], curYear + 4). + Add((string (curYear + 5 )).[3], curYear + 5). + Add((string (curYear + 6 )).[3], curYear + 6). + Add((string (curYear + 7 )).[3], curYear + 7). + Add((string (curYear + 8 )).[3], curYear + 8). + Add((string (curYear + 9 )).[3], curYear + 9) + + let fullYear = string yearMap.[year] + + sprintf "%s%c" fullYear month + + + module Reflection = open Fable.Core.JsInterop @@ -33,3 +63,4 @@ module Reflection = let pairsToObj<'a> (kv: (string*'a) seq) = createObj (unbox kv) + diff --git a/src/web/ui/src/Users/View.fs b/src/web/ui/src/Users/View.fs index eec262678..722554310 100644 --- a/src/web/ui/src/Users/View.fs +++ b/src/web/ui/src/Users/View.fs @@ -154,6 +154,7 @@ let accountsTabView (model:Model) (dispatch:Msg->Unit) (readonly:bool) = ] className = "" header = None + onClick = None } let rows:TableControl.row list = @@ -186,6 +187,7 @@ let accountsTabView (model:Model) (dispatch:Msg->Unit) (readonly:bool) = ] className="" header = None + onClick = None } ] @@ -376,6 +378,7 @@ let userSelectionGrid (usersList:BTSUser list) (currentUser:BTSUser) (dispatch:M ] className = "" header = None + onClick = None } let rws:TableControl.row list = [ for u in activeUsers do yield { @@ -389,6 +392,7 @@ let userSelectionGrid (usersList:BTSUser list) (currentUser:BTSUser) (dispatch:M ] className = "" header = None + onClick = None } ] div[][ diff --git a/src/web/ui/src/Util/StreamUtils.fs b/src/web/ui/src/Util/StreamUtils.fs new file mode 100644 index 000000000..b5c149169 --- /dev/null +++ b/src/web/ui/src/Util/StreamUtils.fs @@ -0,0 +1,14 @@ +module StreamUtils +open JSMap +open Fable.Core + +type Change<'a> = { + newItem : 'a + oldItem : 'a +} + +type IUpdate<'a> = + abstract member all : JSMap<'a> with get + abstract member added : JSMap<'a> with get + abstract member removed : JSMap<'a> with get + abstract member changed : JSMap> with get diff --git a/src/web/ui/src/ui.fsproj b/src/web/ui/src/ui.fsproj index fc520834a..6ff9f4c53 100644 --- a/src/web/ui/src/ui.fsproj +++ b/src/web/ui/src/ui.fsproj @@ -19,6 +19,7 @@ + @@ -29,6 +30,7 @@ + diff --git a/testing/manual/admin/product_position_limits.txt b/testing/manual/admin/product_position_limits.txt index c47774106..b43baabc1 100644 --- a/testing/manual/admin/product_position_limits.txt +++ b/testing/manual/admin/product_position_limits.txt @@ -1,6 +1,4 @@ -This test verifies that product position limits are enforced correctly. (Position limits are -enforced at both the security and account level) - +This test verifies that product position limits are enforced correctly. Use SIM for all steps of the test 0. Clear up your orders, fills and admin db so we don't have any lingering positions @@ -17,11 +15,16 @@ and enter a big clip, say 500000, so that clip limits don't interfere with this 5. Enter a rule for SIM, Future, SI, 60, 120. -6. Send 3 10 lot buys for both the futures. Make sure they go through. +6. Send 3 20 lot buys for both the futures. Make sure they go through. 7. Send a 1 lot on SIU8. The limit of 60 should not allow it. Make sure the rejection message is clear. +8. Cross 2 of the 20 lots enterred in 6 so that they fill. + +9. Send a 1 lot on SIU8. The limit of 60 should not allow it. Make sure the rejection +message is clear. + 8. Change the max to 70, and try a 10 lot again. This order should be allowed. Try another 10 lot and it should be stopped. diff --git a/testing/manual/admin/spread_position_limits.txt b/testing/manual/admin/spread_position_limits.txt index 994f71c19..ee5a65bc8 100644 --- a/testing/manual/admin/spread_position_limits.txt +++ b/testing/manual/admin/spread_position_limits.txt @@ -4,17 +4,19 @@ ii. spread legs fills (and working orders) are applied toward enforcing the position limits for the legs iii. leg limits are checked when entering spread orders -i: - 1. Pick a 2 legged spread (lets say SIU8-SIZ8). Set the clip limits high for both spreads and futures so that they don't interfere. 2. For the Position Limits, set Max Long and Max Short of 100 and 110 for the SI futures and 20 and 30 for the SI spreads respectively. -3. Verify that the limits set in 2 work for futures(remember working orders count toward limit enforcement so you dont need -fills). Cancel all the orders. +3. Make sure "Ignore Spread Position Limits" is not checked -4. Verify that the limits set in 2. work for spreads. Cancel all the orders. +4. Verify that the limits set in 2 work for futures. Accrue future "positions" through a +combination of working orders and fills. Cancel all the orders. -5. Make these position limits 1,000,000 to make them irrelevant +5. Verify that the limits set in 2. work for spreads. Cancel all the orders. + +6. Check "gnore Spread Position Limits" and make sure this disables spread limits. + +7. Make these position limits 1,000,000 to make them irrelevant diff --git a/testing/manual/algos/pro_rata_controlled.txt b/testing/manual/algos/pro_rata_controlled.txt index b9ea169e6..d78366657 100644 --- a/testing/manual/algos/pro_rata_controlled.txt +++ b/testing/manual/algos/pro_rata_controlled.txt @@ -10,14 +10,14 @@ It will: The prorata order works the child until the Fill Limit is breached. Once the fill limit is breached, the algo will begin the 'get out process' Unless: - - If "Enable Stay In" is checked, AND + - If "Stay In" is checked, AND a) The opposite inside market (at inception) doesn't move against us AND b) The same side quantity remains ABOVE the "Stay in Threshold" -If "Enable Stay In" is not checked, 'get out process' begins right away +If "Stay In" is not checked, 'get out process' begins right away "Get Out Process" means 1) Cancel the child order - 2) Scratch the overfill hedges if and only if the "Enable Scratch on Overfill" is checked + 2) Scratch the overfill hedges if and only if the "Scratch on Overfill" is checked -Scratches are done "Chase Ticks" through the original opposite market @@ -37,7 +37,7 @@ ExchangeSim { (if there are no markets use the M View and the ticket to enter, if you already had left over orders cancel them) -2. Create the bid side of the market by sending 50 lot buy limit orders at 2830.75 through 2831.50. +2. Create the bid side of the market by sending 50 lot buy limit orders at 2830.75 through 2831.25. You should have no orders at 2831.50, 2831.75 and 2832. Basics: @@ -47,8 +47,8 @@ Basics: Chase Ticks = 1 Fill Limit = Absolute:5 Stay in Threshold = Absolute:21 - Enable Scratch on Overfill = true - Enable Stay In = true + Scratch on Overfill = true + Stay In = true Verify that the Prorata order along with the Child order shows up. @@ -62,22 +62,22 @@ Next put a 50 lot buy limit order on top of your prorata at 2831.50 6) Fill one more 1 lot. 7) Again cancel the 50 lot limit at 2831.50. You should see: - - The remainder of the 20 lot should cancel + - The remainder of the 20 lot limit order child should cancel (the remainder will be 14, with 6 already filled) - 1 lot of your hedges should scratch to sell at 2831.25 - - The parent order should stay in + - The parent algo order should stay in (the badge will still be there) - You should still be working 5 sales for hedges 8) Fill the 5 sells, it should cancel the parent. -9) Repeat 3-8, but this time set Enable Scratch on Overfill = false, Enable Stay In = true +9) Repeat 3-8, but this time set Scratch on Overfill = false, Stay In = true - You should see the same results in 7, except the 1 lot scratch won't happen so you'll still have 6 working hedges -9) Repeat 3-6, but this time set Enable Scratch on Overfill = false, Enable Stay In = false +9) Repeat 3-6, but this time set Scratch on Overfill = false, Stay In = false - After filling your last lot in 1 you should see your working order cancel, with 6 hedges working - The ladder may deceive you here, check the OB - Cancel your hedges, this should cancel the whole chain -9) Repeat 3-6, but this time set Enable Scratch on Overfill = true, Enable Stay In = false +9) Repeat 3-6, but this time set Scratch on Overfill = true, Stay In = false - After filling your last lot in 1 you should see your working order cancel, one hedge scratch (at 2831.50), and 5 hedges working - The ladder may deceive you here, check the OB - Cancel your hedges, this should cancel the whole chain diff --git a/testing/manual/algos/scratch_controlled.txt b/testing/manual/algos/scratch_controlled.txt index e711374a2..78d91387c 100644 --- a/testing/manual/algos/scratch_controlled.txt +++ b/testing/manual/algos/scratch_controlled.txt @@ -33,14 +33,15 @@ Basics: Verify that the Scratch order along with the Child order shows up -4) Now cancel the 50 lot sell at 2832.25, you should see the 20 lot scratch pay 2832.50 on 20 +4) Now cancel the 50 lot sell at 2832.25, you should see the 20 lot scratch pay 2832.50 on 20 +(here you'll be filled at 2832.5 but the child limit order will have a price of 2832.75) 5) Repeat 3, now cancel the 50 lot buy at 2831.50, you should again see scratch pay 2832.50 on 20. -Put the 50 lot buy back in. +Put the 50 lot buy at 2831.50 back in (make sure it's a limit, not a Scratch). 6) Repeat 3, put an offer in at 2832.25, cancel it; nothing should happen. Cancel the Scratch Algo -7) This time enter the algo with Max Chase = 0, cancel the 50 lot offer at 2832.75 and 2833. Nothing should happen. Now cancel the 2832.5 offer, you should see the algo pay up to 2832.5, but not fill. +7) This time repeat 3 but enter the algo with Max Chase = 0, cancel the 50 lot offer at 2832.75 and 2833. Nothing should happen. Now cancel the 2832.5 offer, you should see the algo pay up to 2832.5, but not fill. 8) Cancel the last offer at 2833.25. Nothing should happen. diff --git a/testing/manual/ladder/badges.txt b/testing/manual/ladder/badges.txt index 7ebcc32a5..f97b5e066 100644 --- a/testing/manual/ladder/badges.txt +++ b/testing/manual/ladder/badges.txt @@ -5,13 +5,10 @@ The badges coming out in 1.2 have had their functionality greatly enhanced. Plea 1. Qty Filled text 2. Total Qty text 3. Badge will use buy/sell side color as fill -4. On child orders, a rectangle will be displayed with the first character of the root algo, along with the color of the root algo +4. On child orders, the Sub type will be displayed on the right side 5. On single order badges (no parent-child relationship), we'll put a border around the badge representing the order type 6. not pictured / tba -7. A dot will be painted in the upper right region of the badge on exchange orders: Limit, Market, Stop, StopLimit types -8. Not pictured, but some orders will indicate they should not appear in a badge depending on their state. Trigger orders that have been triggered will not be shown. -9. A triagle in the lower right corner of the badge will mean there are multiple orders at this level. In this multiple order state, all other badge widgets will be hidden. -10. A vertical line will be drawn on the left side of the badge from top to bottom to indicate this is a hedging order +9. A triangle in the lower right corner of the badge will mean there are multiple orders at this level. In this multiple order state, all other badge widgets will be hidden. Test Steps @@ -19,54 +16,39 @@ Test Steps Test 1 ------- -This will test characteristics 1,2,3,5,7,9 +This will test characteristics 1,2,3,5,9 1. Pull up your favorite product a work a limit order above the ask, you should see 0/qty working (1,2) the badge should be filled in with a lighter ask background color (red) (3) the badge will have a green border, depicting the single-order limit order type (5) -the badge will have a dot in the upper right hand corner (7) 2. Now place another limit order at the same level: the badge now has a triangle in the lower right, indicating we have multiple working orders (9) -the green border and dot have gone away (5, 7) +the green border has gone away (5) -Now place another limit below the bid: +Now place another limit order below the bid: 3. Outcome similar to 1, except the background color is lighter bid background color (blue) -Now place an a Buy Iceberg at the same level with a sufficiently large qty, say 200, as your working ask +Now place a Buy Iceberg at the same level with a sufficiently large qty, say 200, as your working ask -4. Outcome similar to 1, except the background color is purple to indicate a buy and sell are at the same level +4. Outcome similar to 1 Test 2 ------ -This will test characteristics 4,10 +This will test characteristics 4 -1. Create a spreader comprised 1 quoting leg and 1 hedging leg of your favorite instruments and place a spread order -2. Pull up the spreader ladder and place a sell order above the market -3. Pull up the quoting leg's ladder and observe the badge; it may be helpful to pull up the Orderbook to find the price at which the leg is being quoted. - The badge should have a rectangle filled in with the Spreader algo's color - there should be an S in the rectangle denoting it's root parent is Spreader algo - it should have a dot in the upper right denoting it's an exchange order (limit) +1. Create a cover order (set hedge ticks to 1) +2. The badge should have the algo's border color and the letters CV on the left to indicate it is a cover order. +3. Fill the order, hedge orders should be placed one tick above and below the cover order's price +4. The badges should display H on the right to indicate they are hedge orders. it should be surrounded by a green border (limit) -4. Work a sell limit at the same level of this order +4. Work a sell limit at the same level as the sell hedge The multi-order triangle will appear and all other badge widgets will disappear. -5. Cancel that limit. -6. I'm not sure how to get a Hedge order working w/o filling immediately; assume it just works for now - -Test 3 ------- -This will test characteristic 8 - -1. Put in a Price Type Bid trigger order with Trigger Action Limit at a fixed price somewhere way outside of the market so it will work. - When your trigger is hit, the trigger should disappear from the ladder but the triggered order should be visible and working. (Trigger orders may not behave this way in sim) - The badge for the triggered order should show a rectangle filled in w/ the Trigger order color and letter - The badge for the triggered order should have a green border (limit) - - \ No newline at end of file + diff --git a/testing/manual/ladder/ladder.txt b/testing/manual/ladder/ladder.txt index abc4164ee..51c50ef2e 100644 --- a/testing/manual/ladder/ladder.txt +++ b/testing/manual/ladder/ladder.txt @@ -1,7 +1,7 @@ Ladder UI Test -Start a ladder (Trading->Ladder) +Start a ladder (Trading->Ladder) Select a security and launch Test basic ladder functionality @@ -22,8 +22,5 @@ Test a market order, do a buy and a sell and make sure they fill at market price Test Stop and Stop limit orders. Put a Stop Buy above the market and a Stop Sell below Test ICEBerg Orders -Select ICEBerg and then specify limit as subtype via the left arrow. Set show quantity = 1 +Select ICEBerg; use the left arrow to Set show quantity = 1 Now select a size greater than 1 in the main ladder, place buys and sells in the ladder.' - -Test TimeBox -Enter any time in the future and a reasonable end time. Enter two orders. Cross one of the orders during the specified time window with a limit order and verify it was filled correctly. Verify the other order cancels after the end time. diff --git a/testing/manual/ladder/piq_mbo.txt b/testing/manual/ladder/piq_mbo.txt index 3cce313fa..029f611a2 100644 --- a/testing/manual/ladder/piq_mbo.txt +++ b/testing/manual/ladder/piq_mbo.txt @@ -10,7 +10,7 @@ General Stuff to Look For: - The MBO popup is 'live' and should update as the market moves. - The MBO popup should increase in size if more orders are added while it is open. - The MBO popup should not shrink when orders are removed. It only grows. -- The scale of the bars is based on the largest order seen by the popup. +- The scale of the bars is based on the largest order seen by the popup. - If a larger order shows up it will scale down the other orders. - When the large order goes away it should *not* expand the other bars. The scale only increases while the popup is open. - For example: @@ -26,9 +26,9 @@ General Stuff to Look For: Part 1: ------- - Find a security with a non-empty market that isn't moving too fast. Cert can be temperamental so open the market view and filter for Qty > 0. -- Open settings and turn on the PIQ column (settings -> column settings) -- Choose a price near the top of the book and middle click the price column to show the MBO popup. -- Add up the sizes in the popup and make sure they agree with the total size in the bid or ask column on the ladder. +- Open settings and turn on the PIQ column (settings -> Ladder Columns) +- Choose a price near the top of the book and shift + middle click the price column to show the MBO popup. (There must be a size for the popup to show) +- Add up the sizes in the popup and make sure they agree with the total size in the bid or ask column on the ladder. - Test both sides of the market near the top and away from the top. - Enter an order. - The PIQ column should reflect the number of orders ahead of you in the queue. The size in the bid/ask column is how many orders were there when you entered your order so you should expect the PIQ to be the size from before you entered your order. @@ -52,6 +52,6 @@ Part 3: - Find a security with enough orders to cause scrolling by either - Connect to a demo system with production md and look at a ZN spread (i.e. ZNM9 - ZNU9) - Enter a bunch of orders at the level on cert. - - If you try this on sim (you're not a very good listener, are you?) It will cap out at ExchangeSim.orders_per_level (default:10) + - If you try this on sim (you're not a very good listener, are you?) It will cap out at ExchangeSim.orders_per_level (default:10) - Make sure that scrolling works. diff --git a/testing/manual/orders/mini_ob.txt b/testing/manual/orders/mini_ob.txt index c679bb769..f423d234a 100644 --- a/testing/manual/orders/mini_ob.txt +++ b/testing/manual/orders/mini_ob.txt @@ -38,17 +38,17 @@ Row edit mode behavior Repeat for price Repeat for both Verify that pressing enter and/or clicking the check button only commits the current row - Clicking the update button will put the row in edit mode and bring up an additonal popup that will also allow for price and size modifications (for now) + Clicking the update button will put the row in edit mode and bring up an additonal popup that will also allow for price and size modifications (update is only enabled for algo orders) Change the qty in the update popup and verify the row's qty textbox updates to reflect the changed-to value and that the the check button is still disabled (i.e. the Update popup has committed the change already, so there is *not* any additional pending change) Change the price in the update popup and the row will disappear from the miniob since the order is now at a different price A(attach)/D(etach) buttons Click on the A button brings and bring up the attach popup Attach a limit to an IB - Verify the limit row's A button has changed to a D + Verify the limit row's attach button has changed to a detach button Verify the limit row is now a child to an IB order row - Click on D button on the limit order - Verify the limit row's D button has now changed to an A + Click on detach button on the limit order + Verify the limit row's detach button has now changed to an attach Verify the limit row is no longer a child to the IB Verify the IB order is now canceled diff --git a/testing/manual/positions_and_fills/PAndL.txt b/testing/manual/positions_and_fills/PAndL.txt index ba51139aa..da7d267e7 100644 --- a/testing/manual/positions_and_fills/PAndL.txt +++ b/testing/manual/positions_and_fills/PAndL.txt @@ -85,7 +85,7 @@ SODTime{ Day Short 0 Day PnL 0 Total Net Pos -1 - Total PnL -12.5 + Total PnL -37.5 PnL Price 14.0125 (displayed as 14.015) ACC A1: Start Net Pos 1 @@ -133,8 +133,8 @@ Buy a 1 lot at the current bid in ACCA via Trading -> Manual Fills ACC A: Start Net Pos -1 (no change) Start PnL -37.5 - Day Long 0 - Day Short 0 + Day Long 1 + Day Short 1 Day PnL 0 Total Net Pos -1 Total PnL -37.5 diff --git a/testing/manual/positions_and_fills/SOD.txt b/testing/manual/positions_and_fills/SOD.txt index 90526c240..cd5c62572 100644 --- a/testing/manual/positions_and_fills/SOD.txt +++ b/testing/manual/positions_and_fills/SOD.txt @@ -16,6 +16,9 @@ Open up the PNL and make sure the Day Long and Day Short positions match what yo The Start Net Pos should be 0. (The Total Net Pos in the PNL should match the pos button in the ladder) +2. Enter admin fills (from risk admin) and local fills. Make sure they reflect correctly in the PNL +(local fills apply to the PNL, but are not used to enforce limits) + 2. Now comes the SOD part. The SOD is what demarcates a day. Find the most recent time on your fills, select 1 minute past that override the SOD time in your spark.conf Here's an example of what the key looks like, assuming your last fill was say at 10:29 and its now 10:35 @@ -27,10 +30,16 @@ SODTime{ } } -3. Now restart the server. Verify that all rows in the fills have a Fill Kind 'Sod' and the time matches the SODTime (converted to your local time zone) +(Enter this in the timezone on your server, which you can check with "date") -4. Verify that the ladder is displaying the correct position (it should be the same as before the server start) +3. Now restart the server. Verify that all fill rows in the fills have a Fill Kind 'Sod' -5. Verify that the PNL is displaying the correct Start Net Pos and the Day part is 0. +4. The pre SOD local fills should be all gone (they are deleted by the system at startup) + +5 Verify that the ladder is displaying the correct position (it should be the same as before the server start) + +6. Verify that the PNL is displaying the correct Start Net Pos and the Day part is 0. + +7. Now force a few more fills (trade fills as well as admin/local). Verify that the ladder position box and the PNL is +correctly counting them. -6. Now force a few more fills. Verify that the ladder position box and the PNL is correctly counting them. diff --git a/testing/manual/settings/TradeAlertsManualTest.md b/testing/manual/settings/TradeAlertsManualTest.md new file mode 100644 index 000000000..015c6c3c7 --- /dev/null +++ b/testing/manual/settings/TradeAlertsManualTest.md @@ -0,0 +1 @@ +# Trade Alerts Test (20 min) ------------------------------ ###Preparation - Put the following in spark.conf on the server (we need complete control over the system trades, i.e., those in the trade ticker): `ExchangeSim { value { merged_with_md: false } }` - Make sure your volume is turned up. - Open the Global Alerts Dialog (Settings -> Alerts). - [Optional] Open the trade ticker (with all securities), to serve as a visual aid to identify expected/unexpected sound-alerts, or lack thereof. --- ###How To #####Add an Alert: 1. In the Global Alerts Dialog, select the 'Trade Alerts' tab, if not already selected. - Use the 'Click to Add..' hyperlink to add an alert. - Expectations: - a new alert row should be added to the grid - it should have the following defaults (not all columns have explicit defaults): - On/Off switch: 'On' - Security: 'None' - Price Condition: 'None' - Size Condition: 'None' - Side: 'Buy & Sell' - Use the controls inside of each cell to define the alert conditions/criteria. - In the Sound cell, click the play button to hear a preview (perhaps it would be better if the play button were removed, and the sound played upon selection). #####Delete an Alert: 1. In the Global Alerts Dialog, select the 'Trade Alerts' tab, if not already selected. - Click the 'X' button on the left side the [alert]row you wish to delete. #####Force a Trade: 1. Enter an order for the appropriate security (use a 1 lot unless otherwise specified, as this eliminates the potential for orders to continue working after a trade is triggered). - Enter an order for the same security on the opposite side, at the same price to cause a trade (use the same size as the first order unless otherwise specified). ## - Note: the aggressor side is the side that triggered the trade. If both orders are placed at the same price, the second order is the aggressor. The alert's 'Side' condition refers to the aggressor side. --- ###Test Steps #####PART I: Security Condition (3 min) 1. Delete all alerts. 2. Add an alert with the following conditions: - Security: a specific security. This security will be referred to as 'A' - Click 'Apply' to save the new alert. - Force a trade to happen for 'A': - Expectation: the appropriate sound is played. - Return to the Global Alerts Dialog, remove the 'A,' and replace it with a different one. This new security will be referred to as 'B.' - Force a trade to happen for the 'A.' - Expectation: no sound is played. - Force a trade to happen for 'B.' - Expectation: the appropriate sound is played. - Return to the Global Alerts Dialog, select a different sound, then click 'Apply.' - Force a trade to happen for 'B.' - Expectation: the appropriate sound is played. ##### PART II: Symbol Condition (3 min) 1. Delete all alerts. 2. Add an alert with the following conditions: - Security: a specific symbol with 2 or more securities. This symbol will be referred to as 'A.' 3. Force a trade to happen for 2 different securities of this symbol. - Expectations: the appropriate sound is played for both trades 4. Force a trade to happen for a security that is not of this symbol. - Expectations: no sound is played. #####PART III: Price Condition (4 min) 1. Execute the following steps for each price condition mode: - Greater Than - Less Than - Between - Not Between - Delete all alerts. - Add an alert with the following conditions: - Security: all (add each exchange to enable for all securities) - Price: a condition with the mode you aim to test. - Any real number will suffice for Greater/Less Than modes. - For the 'Between/Not Between' modes, the first number is the exclusive minimum, and must be less or equal to the second number, which is the exclusive maximum. - Note: for certain combinations of security-ticks and min/max values, the min/max may to behave inclusively due to rounding errors. We should address this soon, but is not a showstopper at the moment. - Force a trade to happen that satisfies the price condition (remember to use a security with the appropriate symbol). - Expectations: the appropriate sound is played. - Force a trade to happen that does NOT satisfy the price condition - Expectations: no sound is played. #####PART IV: Size Condition (4 min) 1. Execute the following steps for each size condition mode: - Greater Than - Less Than - Between - Not Between - Delete all alerts. - Add an alert with the following conditions: - Security: all (add each exchange to enable for all securities) - Size: a condition with the mode you aim to test. - Any real number will suffice for Greater/Less Than modes. - For the 'Between/Not Between' modes, the first number is the inclusive minimum, and must be less or equal to the second number, which is the inclusive maximum. - Force a trade to happen that satisfies the size condition (remember to use a security with the appropriate symbol). - Expectations: the appropriate sound is played. - Force a trade to happen that does NOT satisfy the size condition - Expectations: no sound is played. #####PART V: Side Condition (4 min) 1. Execute the following steps for each Side mode: - Buy - Sell - Buy & Sell - Delete all alerts. - Add an alert with the following conditions: - Security: all (add each exchange to enable for all securities) - Side: choose the side(s) you aim to test. - Force a 'buy aggressor' trade to happen. - Expectations: a sound is played if Side is either 'Buy' is included in the side condition, otherwise no sound is heard. - Force a 'sell aggressor' trade to happen. - Expectations: a sound is played if Side is either 'Sell' is included in the side condition, otherwise no sound is heard. #####PART VI: On/Off (2 min) 1. Delete all alerts. - Add an alert. Assign the condition(s) and sound of your choosing (remember to set the security field, as 'None' effectively turns the alert off). - Enter a trade that satisfies the condition - Expectations: the appropriate sound is played - Return to the Global Alerts Dialog, turn the alert off (different from delete), then click 'Apply.' - Enter a trade that satisfies the condition - Expectations: no sound is played - For good measure, return to the Global Alerts Dialog, turn the alert back on, and click 'Apply.' - Enter a trade that satisfies the condition - Expectations: the appropriate sound is played \ No newline at end of file diff --git a/testing/manual/span/span.txt b/testing/manual/span/span.txt index 22a815661..7540946b5 100644 --- a/testing/manual/span/span.txt +++ b/testing/manual/span/span.txt @@ -1,5 +1,5 @@ Test Author: Kevin -If it breaks: Kevin +If it breaks: Kevin This will test our ability to compute and enforce span margin at CME @@ -20,10 +20,10 @@ SpanMargin { 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. +4) If not already zero, set your cash balance to zero and set leverage factor to 1 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 +6) Increase your cash balance to 10,000 (or higher, your Excess Liquidity should be positive) 7) Try to trade again, you should be able to. \ No newline at end of file diff --git a/testing/manual/spreader_misc/fractional_hedging.txt b/testing/manual/spreader_misc/fractional_hedging.txt index c0f218383..fce9925bb 100644 --- a/testing/manual/spreader_misc/fractional_hedging.txt +++ b/testing/manual/spreader_misc/fractional_hedging.txt @@ -2,20 +2,22 @@ This test verifies that fractional hedges work. That is, when a fill in a quotin not equate to one full contract in the hedging leg, we accumulate the unhedged delta, until we can send one or more hedging contracts. -1. Create a new synthetic spread. For the legs choose SIF9 and SIH9 (if you don't +0. This needs sim with replay turned off (see step 0 in common_setup_1), so we can create our own markets. + +1. Create a new synthetic spread. For the legs choose SIF1 and SIH1 (if you don't have these contracts, other contracts can be chosen too) and for the quantities choose 1 and -1.5 respectively (and same for the Price multiplier). -Enable quoting for SIH9, and disable all quoting, pre-hedge and post-hedge parameters. -Name the synthetic SIF9-1.5SIH9. +Enable quoting for SIH1, and disable all quoting, pre-hedge and post-hedge parameters. +Name the synthetic SIF1-1.5SIH1. 1. Bring up the ladder for the synthetic and for the 2 legs. -2. Enter a 10 lot sell order at 20.0 for SIF9 (If the ladder is showing blanks use the market view +2. Enter a 10 lot sell order at 20.0 for SIF1 (If the ladder is showing blanks use the market view to enter orders). The quoting order will lean against this. 3. Enter a 10 lot buy order for the synthetic at -9.900. -4. Verify that a 15 lot quoting order is sent for SIH9 at 19.935. +4. Verify that a 15 lot quoting order is sent for SIH1 at 19.935. 5. Now we will start filling the quoting order and verify the hedges. A 1 lot fill in the quoting leg means a 2/3 contract in the hedging leg. Since we can not send 2/3 contracts, @@ -32,14 +34,14 @@ happening on the order sent in step 2 (or look at the hedges in the orderbook). ------------------------------------------------------------------------------ quoting fill unhedged_delta hedge_order_sent remaining_unhedged_delta ------------------------------------------------------------------------------ -1 2/3 0 2/3 -1 4/3 1 1/3 -1 1 1 0 -4 8/3 2 2/3 -5 4 4 0 -2 4/3 1 1/3 -1 1 1 0 +1 2/3 0 2/3 +1 4/3 1 1/3 +1 1 1 0 +4 8/3 2 2/3 +5 4 4 0 +2 4/3 1 1/3 +1 1 1 0 6. At the end of this, the whole order should be filled. Check the order book -to make sure the fill price is -9.900, there are 10 total fills in SIF9 and -15 fills in SIH9. \ No newline at end of file +to make sure the fill price is -9.900, there are 10 total fills in SIF1 and +15 fills in SIH1. \ No newline at end of file diff --git a/testing/manual/spreader_misc/implied_vs_direct.txt b/testing/manual/spreader_misc/implied_vs_direct.txt index 74fbef376..669f9ac8a 100644 --- a/testing/manual/spreader_misc/implied_vs_direct.txt +++ b/testing/manual/spreader_misc/implied_vs_direct.txt @@ -10,7 +10,7 @@ ExchangeSim { } } -1. Open up the mview and find a security with with both direct and implied markets, on the ask side. +1. Open up the market view and find a security with with both direct and implied markets, on the ask side. Make sure those markets are stable and you can rely on them. 2. Create a synthetic with the security in 1. being the hedge leg (buy) and choose a quoting diff --git a/testing/manual/spreader_misc/multi_quoting_legs.txt b/testing/manual/spreader_misc/multi_quoting_legs.txt index c5c1806be..b4b2187aa 100644 --- a/testing/manual/spreader_misc/multi_quoting_legs.txt +++ b/testing/manual/spreader_misc/multi_quoting_legs.txt @@ -1,4 +1,4 @@ -This test verifies the pecularities of the spreader with > 1 quoting leg. +This test verifies the peculiarities of the spreader with > 1 quoting leg. 0. This needs sim with replay turned off (see step 0 in common_setup_1), so we can create our own markets. @@ -9,14 +9,14 @@ well. Disable all Quote, Pre-Hedge and Post-Hedge parameters. market of 20 at 1.00 should show up. 3. Now enter a 5 lot synthetic buy order at 0.70. Since both legs are quoting GCM2 uses GCZ2 (19.50 bid) -to quote its bid at 19.50+0.7=20.2, and GCZ2 uses GCM2 (20.05) to quote its offer at 20.5-0.70=19.8. Verify -these quoting prices +to quote its bid at 19.50+0.7=20.2, and GCZ2 uses GCM2 (20.05) to quote its offer at 20.5-0.70=19.8. Verify +these quoting prices (remember, for quoting prices: leg_1=leg_2_bid_market+spreader_price leg_2=leg_2_bid_market-spreader_price ) -4. Move the markets a few times (without crossing and causing any fills) and make sure the quoting prices +4. Move the markets a few times (without crossing and causing any fills) and make sure the quoting prices are correct, and bring them back to where they were in 2. 5. Now force a 1 lot fill on the GCZ2 quoting order, by sending a 1 lot buy against it. This should send diff --git a/tools/buildspark.cmd b/tools/buildspark.cmd index 25ddc7079..afa7df151 100644 --- a/tools/buildspark.cmd +++ b/tools/buildspark.cmd @@ -4,4 +4,5 @@ setlocal cd /d %~dp0/../ setlocal set path=%spark_build_python_path%;%path% +touch src\lib\CMakeLists.txt msbuild /m /nologo /p:debugtype=portable /p:platform=x64 /p:GenerateFullPaths=true /clp:verbosity=minimal /clp:summary %* spark_client.sln diff --git a/tools/linux_control/build_in_docker.sh b/tools/linux_control/build_in_docker.sh old mode 100644 new mode 100755 index c111b9ad0..4ddc96cda --- a/tools/linux_control/build_in_docker.sh +++ b/tools/linux_control/build_in_docker.sh @@ -2,6 +2,6 @@ if [ -f /.dockerenv ]; then ninja else - cid=`docker ps | grep bluetradingsystems/linux_docker | awk '{print $1}'` + cid=`docker ps | grep -P "emacs_ide|bluetradingsystems/linux_docker" | awk '{print $1}'` docker exec $cid /bin/bash -c "cd build/clang_debug/ && ninja" fi