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..c44b8cd56 100644 --- a/doc/user/content/release_notes/_index.md +++ b/doc/user/content/release_notes/_index.md @@ -4,6 +4,36 @@ 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 +### 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/docker/linux b/docker/linux index e13bb5d8d..b09d4c035 160000 --- a/docker/linux +++ b/docker/linux @@ -1 +1 @@ -Subproject commit e13bb5d8df14171a9a5abfdb8b6b4ecbc519032a +Subproject commit b09d4c035b4c0d0e600c7639cd9cb5edaee98d2e diff --git a/externals b/externals index 67a79263b..6b8c745fb 160000 --- a/externals +++ b/externals @@ -1 +1 @@ -Subproject commit 67a79263ba116a7646beb8368cef015530bea027 +Subproject commit 6b8c745fb2725ad92fc0292b7836e0838e7e1679 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 4efaeaf41..016e8022d 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), KeyGeometryKey = nameof(KeyGeometryKey), LetterAKey = nameof(LetterAKey), LetterDKey = nameof(LetterDKey), @@ -50,6 +51,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), @@ -170,6 +172,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 Key => create_icon(KeyGeometryKey); public static ImageSource Knight { get { return load_resource_and_freeze("Knight"); } } @@ -181,6 +184,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 d4edbe407..4becbc715 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 + + M22.399981,2.4000006C22.099982,2.4000006 21.899982,2.5 21.599982,2.6999998 21.199983,3.1000004 21.199983,3.8000002 21.599982,4.2000003L26.199979,8.9000001C26.599978,9.3000002 27.299977,9.3000002 27.699977,8.9000001 28.099977,8.5000002 28.099977,7.8000002 27.699977,7.4000006L23.099981,2.6999998C22.899981,2.5,22.699981,2.4000006,22.399981,2.4000006z M21.499982,0C22.299982,0,23.199981,0.30000019,23.79998,0.90000057L29.299976,6.4000001C30.599975,7.7000003,30.599975,9.7000003,29.299976,11L22.599981,17.7C21.299983,19,19.299984,19,17.999985,17.7L17.299985,17 12.699989,21.6 10.399991,20.900001 10.899991,23.400001 8.4999928,22.8 8.9999924,25.5 6.2999945,25.1 6.7999945,27.6 4.2999964,27 4.7999954,29.6 2.4999971,32 0,26.6 13.499989,13.1 16.999986,16.6 12.699989,12.3C11.399991,11,11.399991,9.0000002,12.699989,7.7000003L19.399984,1C19.899983,0.30000019,20.699983,0,21.499982,0z @@ -335,6 +339,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/MarketViewVM.cs b/src/client/spark/Modules/Market/MarketView/MarketViewVM.cs index 73581518c..b8257083e 100644 --- a/src/client/spark/Modules/Market/MarketView/MarketViewVM.cs +++ b/src/client/spark/Modules/Market/MarketView/MarketViewVM.cs @@ -1,5 +1,6 @@ using bts.api; using bts.collections; +using bts.proto; using bts.utils; using mbts.persist; using mbts.spark.filters; @@ -46,8 +47,8 @@ namespace mbts.spark { 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); @@ -77,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 @@ -96,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 }); @@ -111,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 } }; diff --git a/src/client/spark/Modules/Market/MarketView/MarketViewWindow.cs b/src/client/spark/Modules/Market/MarketView/MarketViewWindow.cs index 35f22e89c..da46e6c8d 100644 --- a/src/client/spark/Modules/Market/MarketView/MarketViewWindow.cs +++ b/src/client/spark/Modules/Market/MarketView/MarketViewWindow.cs @@ -153,7 +153,7 @@ namespace mbts.spark { s => _vm.grid_settings_manager.create_vm( // we need to get the most up to date GridSettings when launching b/c users may // have changed some settings on the grid directly, like column widths. - s.with(_vm.columns.Select(c => c.create())) + MarketViewWindowFactory.StartupData.sanitize(s).with(_vm.columns.Select(c => c.create())) ) ); vm.SubscribeDisposed(literal_title_source.OnDispatcher().Subscribe(t => vm.title = t)); diff --git a/src/client/spark/Modules/Market/MarketView/MarketViewWindowFactory.cs b/src/client/spark/Modules/Market/MarketView/MarketViewWindowFactory.cs index 38931a3eb..c28013d6b 100644 --- a/src/client/spark/Modules/Market/MarketView/MarketViewWindowFactory.cs +++ b/src/client/spark/Modules/Market/MarketView/MarketViewWindowFactory.cs @@ -1,4 +1,5 @@ using bts.api; +using bts.proto; using mbts.persist; using mbts.spark; using mbts.spark.filters; @@ -36,9 +37,9 @@ namespace bts.shell { _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, + var settings_manager = new WindowSettingsManager(_settings_hub, + StartupData.sanitize(state.grid), + get: us => StartupData.sanitize(us.market_view_grid), set: (us, gs) => us.with(provided_market_view_gridsettings: gs)); return new MarketViewWindow(this, _bus, _orders_model, state, settings_manager); } @@ -68,7 +69,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()); @@ -86,8 +109,8 @@ namespace bts.shell { 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); 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/VM/LadderSettingsVM.cs b/src/client/spark/Modules/Trading/Ladder/VM/LadderSettingsVM.cs index 7a5684d48..8522cedcc 100644 --- a/src/client/spark/Modules/Trading/Ladder/VM/LadderSettingsVM.cs +++ b/src/client/spark/Modules/Trading/Ladder/VM/LadderSettingsVM.cs @@ -137,7 +137,7 @@ namespace mbts.spark { c.PropertyChanged -= on_prop_changed; } AccountTuple? account_factory(uint key, string name) => new AccountTuple(key, name); - 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; diff --git a/src/client/spark/Modules/Trading/PNL/Settings/PNLColumnVM.cs b/src/client/spark/Modules/Trading/PNL/Settings/PNLColumnVM.cs index 1f755e862..5cf3749c4 100644 --- a/src/client/spark/Modules/Trading/PNL/Settings/PNLColumnVM.cs +++ b/src/client/spark/Modules/Trading/PNL/Settings/PNLColumnVM.cs @@ -79,11 +79,11 @@ namespace bts.shell.pnl { target.with( font_weight_override: new NullableFontWeightProto(is_font_weight_overridden ? font_weight_override : (FontWeight?)null), font_color_override: new NullableColorProto(is_font_color_overridden ? font_color_override.color : (Color?)null), - background_override: new NullableColorProto(is_background_overridden ? background_override.color : (Color?)null)); + background_color_override: new NullableColorProto(is_background_overridden ? background_override.color : (Color?)null)); public void load(GridColumnSettings settings) { if (settings != null) - load(settings.font_weight_override, settings.font_color_override, settings.background_override); + load(settings.font_weight_override, settings.font_color_override, settings.background_color_override); } public void load(FontParamsProto font_params) { diff --git a/src/client/spark/Modules/Trading/PNL/Settings/PNLSettings.cs b/src/client/spark/Modules/Trading/PNL/Settings/PNLSettings.cs index 2d94e0645..0a5fcd5fb 100644 --- a/src/client/spark/Modules/Trading/PNL/Settings/PNLSettings.cs +++ b/src/client/spark/Modules/Trading/PNL/Settings/PNLSettings.cs @@ -100,7 +100,11 @@ namespace bts.shell.pnl { } }; - public static GridSettings DefaultGridSettings => GridSettings.Default.with(DefaultColumns.all, DefaultColumns.default_sorting_criterion); + public static GridSettings DefaultGridSettings => + GridSettings.Default.with( + DefaultColumns.all, + DefaultColumns.default_sorting_criterion, + enable_background_color: true); public static fills.FillsSettings DefaultAuxiliaryFills { get { diff --git a/src/client/spark/Modules/Trading/PNL/Settings/PNLSettingsVM.cs b/src/client/spark/Modules/Trading/PNL/Settings/PNLSettingsVM.cs index 62bd9f770..eec72cab8 100644 --- a/src/client/spark/Modules/Trading/PNL/Settings/PNLSettingsVM.cs +++ b/src/client/spark/Modules/Trading/PNL/Settings/PNLSettingsVM.cs @@ -48,9 +48,7 @@ namespace bts.shell.pnl { var col_vm = new PNLColumnVM(core_settings_vm, EnumHelper.parse(col_settings.id)); col_vm.load_from(col_settings); return col_vm; - }) { - can_edit_background = true - }; + }); core_settings_vm.selection_highlight.bind_opacity_to_alpha = true; _dispose = Disposable.Create( anchor_items.Subscribe((s, e) => _sync_anchors.execute())); diff --git a/src/client/spark/Modules/Trading/PNL/VM/PNLVM.cs b/src/client/spark/Modules/Trading/PNL/VM/PNLVM.cs index aa30d61f6..f445a7b92 100644 --- a/src/client/spark/Modules/Trading/PNL/VM/PNLVM.cs +++ b/src/client/spark/Modules/Trading/PNL/VM/PNLVM.cs @@ -352,7 +352,7 @@ namespace bts.shell.pnl { var dabd = from.ToBuilder(); foreach (var k in EnumHelper.get_values()) { if (GridSettingsHelper.try_find_column(col_settings, k, out var match)) - dabd[k] = new PNLCellAppearance(match.font_color_override, match.background_override, match.font_weight_override).merge(fallback_appearance); + dabd[k] = new PNLCellAppearance(match.font_color_override, match.background_color_override, match.font_weight_override).merge(fallback_appearance); } from = dabd.ToImmutable(); } 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 36d2d7e1c..6bc4cc7c6 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; @@ -339,5 +340,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 b6e396a42..78caf3cbf 100644 --- a/src/client/spark/Themes/generic.xaml +++ b/src/client/spark/Themes/generic.xaml @@ -339,7 +339,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 23c625221..1c2e0cea5 100644 --- a/src/client/spark/core/Bus/SparkCommands.cs +++ b/src/client/spark/core/Bus/SparkCommands.cs @@ -110,6 +110,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 71deddc26..a05477e7f 100644 --- a/src/client/spark/core/Bus/SparkTags.cs +++ b/src/client/spark/core/Bus/SparkTags.cs @@ -79,6 +79,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/UserSettings.cs b/src/client/spark/core/ShellInterface/UserSettings/UserSettings.cs index 3415cc6da..4d08ae5db 100644 --- a/src/client/spark/core/ShellInterface/UserSettings/UserSettings.cs +++ b/src/client/spark/core/ShellInterface/UserSettings/UserSettings.cs @@ -59,6 +59,8 @@ namespace mbts.spark { bts.shell.orderbook.OrderBookGridSettings _order_book_grid; [ProtoMember(13)] PNLSettings _pnl; + [ProtoMember(14)] + GlobalAlertSettings _alerts; public UserSettings() { } @@ -77,6 +79,8 @@ namespace mbts.spark { public PNLSettings pnl => _pnl ?? (_pnl = PNLSettings.Default); 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, @@ -86,7 +90,8 @@ namespace mbts.spark { GridSettings provided_synthetic_management_gridsettings = null, GridSettings provided_pnl_gridsettings = null, PNLSettings pnl = 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, @@ -97,6 +102,7 @@ namespace mbts.spark { _pnl_grid = provided_pnl_gridsettings ?? _pnl_grid, _pnl = pnl ?? _pnl, _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; @@ -181,4 +187,4 @@ namespace mbts.spark { public TimeSpan? duration => _mode == Mode.Flash ? _duration?.ToTimeSpan() ?? TimeSpan.FromSeconds(5) : new TimeSpan?(); public double max_height => _max_height; } -} \ No newline at end of file +} 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 5ffb50953..958f2ebca 100644 --- a/src/client/spark/core/data/Proto/NullableColorProto.cs +++ b/src/client/spark/core/data/Proto/NullableColorProto.cs @@ -31,4 +31,4 @@ namespace bts.proto { public static bool operator ==(NullableColorProto a, NullableColorProto b) => a.Equals(b); public static bool operator !=(NullableColorProto a, NullableColorProto b) => !a.Equals(b); } -} \ No newline at end of file +} diff --git a/src/client/spark/view/Colors.xaml b/src/client/spark/view/Colors.xaml index e7ebf4f93..c70363e60 100644 --- a/src/client/spark/view/Colors.xaml +++ b/src/client/spark/view/Colors.xaml @@ -113,6 +113,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/DIYGrid/CellRender/HyperlinkCell.cs b/src/client/spark/view/Controls/DIYGrid/CellRender/HyperlinkCell.cs index 4e65114a9..0e8fb5abf 100644 --- a/src/client/spark/view/Controls/DIYGrid/CellRender/HyperlinkCell.cs +++ b/src/client/spark/view/Controls/DIYGrid/CellRender/HyperlinkCell.cs @@ -361,11 +361,6 @@ namespace mbts.ui.controls { } drawingContext.Pop(); } - if (bg != null) { - var bgo = DIYGrid.GetBackgroundOverlay(this); - if (bgo != null) - drawingContext.DrawRectangle(bgo, null, rect); - } } static Thickness _underline_padding(Thickness padding) => @@ -393,4 +388,4 @@ namespace mbts.ui.controls { InvalidateVisual(); } } -} \ No newline at end of file +} diff --git a/src/client/spark/view/Controls/DIYGrid/Columns/ColumnBase.cs b/src/client/spark/view/Controls/DIYGrid/Columns/ColumnBase.cs index 409540b85..eb06ac295 100644 --- a/src/client/spark/view/Controls/DIYGrid/Columns/ColumnBase.cs +++ b/src/client/spark/view/Controls/DIYGrid/Columns/ColumnBase.cs @@ -217,7 +217,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 @@ -226,10 +226,12 @@ namespace mbts.ui.controls { GridColumnSettings _settings; 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; bool _is_visible; @@ -237,6 +239,7 @@ namespace mbts.ui.controls { 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; @@ -279,6 +282,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(); public GridColumnSettings settings => _settings ?? (_settings = create()); // 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. @@ -291,7 +297,7 @@ namespace mbts.ui.controls { /// important: don't access any dependency props in here. /// this may be called from a thread other than that of this dispatcher (i.e., when is called). 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; @@ -303,7 +309,7 @@ namespace mbts.ui.controls { return; _settings = settings; 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)) @@ -314,21 +320,22 @@ 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; Width = original_width; } // - raise_prop_changed(nameof(ColumnBase.settings)); } // '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(); @@ -375,4 +382,4 @@ namespace mbts.ui.controls { 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 6f40ba7e0..8bba5af3e 100644 --- a/src/client/spark/view/Controls/DIYGrid/DIYGrid.cs +++ b/src/client/spark/view/Controls/DIYGrid/DIYGrid.cs @@ -143,6 +143,7 @@ namespace mbts.ui.controls { ScrollContentPresenter _content_presenter_backing; DIYGrid _grid; DragRectAdorner _drag_rect_adorner; + RowSelectionAdorner _rsa; ScrollViewer _scroll_viewer; Point? _drag_start; double _drag_start_vertical_drift; @@ -169,12 +170,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; void on_support_group_items_changed(bool v) => _support_group_items = v; @@ -355,6 +362,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); diff --git a/src/client/spark/view/Controls/DIYGrid/DIYGridResources.xaml b/src/client/spark/view/Controls/DIYGrid/DIYGridResources.xaml index 8100030d9..866cfc7c6 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}" /> - - - - - - - - - - - - - - - - @@ -66,6 +50,9 @@ - \ No newline at end of file + diff --git a/src/client/spark/view/Controls/DIYGrid/Settings/GridColumnSettings.cs b/src/client/spark/view/Controls/DIYGrid/Settings/GridColumnSettings.cs index 3276b30a7..49b1667d5 100644 --- a/src/client/spark/view/Controls/DIYGrid/Settings/GridColumnSettings.cs +++ b/src/client/spark/view/Controls/DIYGrid/Settings/GridColumnSettings.cs @@ -61,7 +61,7 @@ namespace mbts.ui.settings { readonly double _horizontal_offset; [ProtoMember(10)] - readonly NullableColorProto _background_override; + readonly NullableColorProto _background_color_override; public GridColumnSettings() { _is_visible = _default_is_visible; @@ -72,7 +72,7 @@ namespace mbts.ui.settings { } public GridColumnSettings(string id, string display_name = null, bool is_visible = _default_is_visible, bool is_collapsed = _default_is_collapsed, FontWeight? font_weight_override = null, Color? font_color_override = null, - double width = _default_width, HorizontalAlignment horizontal_alignment = _default_horizontal_alignment, double horizontal_offset = _default_horizontal_offset, Color? background_override = null) { + double width = _default_width, HorizontalAlignment horizontal_alignment = _default_horizontal_alignment, double horizontal_offset = _default_horizontal_offset, Color? background_color_override = null) { _id = id; _display_name = display_name; _is_visible = is_visible; @@ -82,7 +82,7 @@ namespace mbts.ui.settings { _width = width; _horizontal_alignment = horizontal_alignment; _horizontal_offset = horizontal_offset; - _background_override = new NullableColorProto(background_override); + _background_color_override = new NullableColorProto(background_color_override); } public string id => _id; @@ -90,7 +90,7 @@ namespace mbts.ui.settings { public bool is_collapsed => _is_collapsed; public FontWeight? font_weight_override => _font_weight_override.value; public Color? font_color_override => _font_color_override.value; - public Color? background_override => _background_override.argb; + public Color? background_color_override => _background_color_override.argb; public double width => _width; public HorizontalAlignment horizontal_alignment => _horizontal_alignment; public double horizontal_offset => _horizontal_offset; @@ -98,7 +98,7 @@ namespace mbts.ui.settings { public GridColumnSettings with( string display_name = null, bool? is_visible = null, bool? is_collapsed = null, NullableFontWeightProto? font_weight_override = null, NullableColorProto? font_color_override = null, - double? width = null, HorizontalAlignment? horizontal_alignment = null, double? horizontal_offset = null, NullableColorProto? background_override = null) => + double? width = null, HorizontalAlignment? horizontal_alignment = null, double? horizontal_offset = null, NullableColorProto? background_color_override = null) => // new GridColumnSettings(_id, display_name ?? this.display_name, @@ -109,7 +109,7 @@ namespace mbts.ui.settings { width ?? this.width, horizontal_alignment ?? this.horizontal_alignment, horizontal_offset ?? this.horizontal_offset, - (background_override ?? _background_override).argb); + (background_color_override ?? _background_color_override).argb); public bool Equals(GridColumnSettings other) => other != null && @@ -119,6 +119,7 @@ namespace mbts.ui.settings { is_collapsed == other.is_collapsed && font_weight_override.Equals(other.font_weight_override) && font_color_override.Equals(other.font_color_override) && + background_color_override.Equals(other.background_color_override) && width == other.width && horizontal_alignment == other.horizontal_alignment && horizontal_offset == other.horizontal_offset && @@ -135,6 +136,7 @@ namespace mbts.ui.settings { is_collapsed.GetHashCode(), font_weight_override.GetHashCode(), font_color_override.GetHashCode(), + background_color_override.GetHashCode(), width.GetHashCode(), horizontal_alignment.GetHashCode(), horizontal_offset.GetHashCode(), @@ -145,4 +147,4 @@ namespace mbts.ui.settings { return $"id: {id} display_name: {display_name}"; } } -} \ No newline at end of file +} diff --git a/src/client/spark/view/Controls/DIYGrid/Settings/GridColumnSettingsVM.cs b/src/client/spark/view/Controls/DIYGrid/Settings/GridColumnSettingsVM.cs index a99ed6612..ea34cf594 100644 --- a/src/client/spark/view/Controls/DIYGrid/Settings/GridColumnSettingsVM.cs +++ b/src/client/spark/view/Controls/DIYGrid/Settings/GridColumnSettingsVM.cs @@ -22,11 +22,13 @@ namespace mbts.spark { /// /// For flag behavior implementation, see /// + [Flags] enum GridColumnSettingsFeatureFlags { None = 0, ImmutableFontColor = 1, ImmutableFontWeight = 1 << 1, - ImmutableHeader = 1 << 2 + ImmutableHeader = 1 << 2, + ImmutableBackgroundColor = 1 << 3 } partial class GridSettingsVM { @@ -50,7 +52,9 @@ namespace mbts.spark { _can_override_font_color = true, _is_font_color_overridden, _can_override_font_weight = true, - _is_font_weight_overridden; + _is_font_weight_overridden, + _can_override_background_color = true, + _is_background_color_overridden; FontWeight _font_weight_override; GridColumnSettings _settings; @@ -78,6 +82,10 @@ namespace mbts.spark { public bool can_override_font_weight { get => _can_override_font_weight; private set => Set(ref _can_override_font_weight, value); } public bool is_font_weight_overridden { get => _is_font_weight_overridden; set => Set(ref _is_font_weight_overridden, value); } public FontWeight font_weight_override { get => _font_weight_override; set => Set(ref _font_weight_override, value); } + // background color override + public bool can_override_background_color { get => _can_override_background_color; private set => Set(ref _can_override_background_color, value); } + public bool is_background_color_overridden { get => _is_background_color_overridden; set => Set(ref _is_background_color_overridden, value); } + public ColorPickerPopupVM background_color_override { get; } = new ColorPickerPopupVM() { bind_opacity_to_alpha = true }; // animations public ObservableEditableCollection predicate_animations { get; } @@ -89,7 +97,9 @@ namespace mbts.spark { is_font_weight_overridden ? new NullableFontWeightProto(font_weight_override) : new NullableFontWeightProto(null), is_font_color_overridden ? new NullableColorProto(font_color_override.color) : new NullableColorProto(null), width, - horizontal_alignment); + horizontal_alignment, + background_color_override: is_background_color_overridden ? + new NullableColorProto(background_color_override.color) : default); var animations_lookup = predicate_animations.ToFlatLookup(pa => pa.predicate.create_assigned(owner._predicate_data_factory), pa => pa.animations.Select(a => a.create())); return (settings, animations_lookup); } @@ -111,6 +121,11 @@ namespace mbts.spark { if (is_font_weight_overridden) { _font_weight_override = _settings.font_weight_override.Value; } + // background color + is_background_color_overridden = _settings.background_color_override.HasValue; + if (is_background_color_overridden) { + background_color_override.color = _settings.background_color_override.Value; + } predicate_animations.SetItems(value.animations.empty_if_null().Select(g => { var vm = new PredicateCellAnimationVM(id, owner._create_column_predicate_vm(id), owner._predicate_data_factory); vm.load_from((g.Key, g)); @@ -129,6 +144,9 @@ namespace mbts.spark { // font weight can_override_font_weight = (flags & GridColumnSettingsFeatureFlags.ImmutableFontWeight) == 0; is_font_weight_overridden &= can_override_font_weight; + // background color + can_override_background_color = (flags & GridColumnSettingsFeatureFlags.ImmutableBackgroundColor) == 0; + is_background_color_overridden &= can_override_background_color; } public override string ToString() => $"{display_name} ({id})"; diff --git a/src/client/spark/view/Controls/DIYGrid/Settings/GridSettings.cs b/src/client/spark/view/Controls/DIYGrid/Settings/GridSettings.cs index d32065edc..ceb9c92ea 100644 --- a/src/client/spark/view/Controls/DIYGrid/Settings/GridSettings.cs +++ b/src/client/spark/view/Controls/DIYGrid/Settings/GridSettings.cs @@ -17,11 +17,16 @@ namespace mbts.ui.settings { const double _default_font_size = 11; const uint _default_font_color = 0, - _default_background = 0; + _default_background = 0x00FFFFFF; const ushort _default_font_weight = 400; + const uint _default_background_color = 0x00FFFFFF; // Colors.Transparent + const bool _default_enable_background_color = false; public static GridSettings Default => - new GridSettings(selection_highlight: new ColorOpacityProto(FallbackValues.selection_highlight.Color, FallbackValues.selection_highlight.Opacity)); + new GridSettings( + selection_highlight: new ColorOpacityProto(FallbackValues.selection_highlight.Color, FallbackValues.selection_highlight.Opacity), + background: _default_background_color, //new ColorOpacityProto(FallbackValues.background_color.Color, FallbackValues.background_color.Opacity)); + enable_background_color: _default_enable_background_color); // corrects any inappropriate values that would render the grid unusable (usually nulls) // NOTE: if you add a property to GridSettings that could have such a value, please add a conditional-correction for it below) @@ -41,6 +46,7 @@ namespace mbts.ui.settings { public static Color font_color => _default_font_color.rgb_color(); public static FontWeight font_weight => FontWeight.FromOpenTypeWeight(_default_font_weight); public static SolidColorBrush selection_highlight => DIYGrid.DefaultSelectionHighlight; + public static uint background_color => _default_background; } public static IValueConverter selection_highlight_converter => ValueConverter.create(gs => gs.selection_highlight.to_brush().frozen()); @@ -77,6 +83,9 @@ namespace mbts.ui.settings { [ProtoMember(8)] [DefaultValue(_default_background)] uint _background; + [ProtoMember(9)] + [DefaultValue(_default_enable_background_color)] + readonly bool _enable_background_color; public GridSettings() { _font_family = _default_font_family; @@ -94,7 +103,8 @@ namespace mbts.ui.settings { ColorOpacityProto? selection_highlight = null, IEnumerable columns = null, IEnumerable sorting_criterion = null, - uint background = _default_background) { + uint background = _default_background, + bool enable_background_color = _default_enable_background_color) { // _font_family = font_family; _font_size = font_size; @@ -104,6 +114,7 @@ namespace mbts.ui.settings { _columns = new List(columns.empty_if_null()); _sorting_criterion = new List(sorting_criterion.empty_if_null()); _background = background; + _enable_background_color = enable_background_color; } public string font_family => _font_family; @@ -112,6 +123,8 @@ namespace mbts.ui.settings { public FontWeight font_weight => FontWeight.FromOpenTypeWeight(_font_weight); public Color background => _background.argb_color(); public ColorOpacityProto selection_highlight => _selection_highlight; + public uint background_color => _background; + public bool enable_background_color => _enable_background_color; public IReadOnlyList columns => _columns ?? (_columns = new List(0)); [Obsolete] // TODO: remove sorting_criterion from GridSettings entirely. This should be part of IShellWindow's StartupData, since it is an instance-specific setting, and prob not part of the shared defaults public IReadOnlyList sorting_criterion => _sorting_criterion ?? (_sorting_criterion = new List(0)); @@ -124,8 +137,8 @@ namespace mbts.ui.settings { IEnumerable sorting_criterion = null, string font_family = null, double font_size = double.NaN, - uint? background = null) => - // + uint? background = null, + bool? enable_background_color = null) => new GridSettings( font_family ?? _font_family, DoubleHelper.IsNaNOrInfinityOr(font_size, 0) ? _font_size : font_size, @@ -134,7 +147,8 @@ namespace mbts.ui.settings { _selection_highlight, columns ?? _columns, sorting_criterion ?? _sorting_criterion, - background ?? _background); + background ?? _background, + enable_background_color ?? _enable_background_color); public GridSettings clone() => with(); @@ -146,6 +160,7 @@ namespace mbts.ui.settings { font_color == other.font_color && selection_highlight == other.selection_highlight && _background == other._background && + _enable_background_color == other._enable_background_color && EqualityComparer.safe_sequence_equals(columns, other.columns) && #pragma warning disable CS0612 // Type or member is obsolete EqualityComparer.safe_sequence_equals(sorting_criterion, other.sorting_criterion); @@ -162,6 +177,7 @@ namespace mbts.ui.settings { font_color.GetHashCode(), selection_highlight.GetHashCode(), _background.GetHashCode(), + _enable_background_color.GetHashCode(), EqualityComparer.safe_aggregate_hash(columns), #pragma warning disable CS0612 // Type or member is obsolete EqualityComparer.safe_aggregate_hash(sorting_criterion)); diff --git a/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsHelper.cs b/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsHelper.cs index bee5cba63..bab9fc9a5 100644 --- a/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsHelper.cs +++ b/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsHelper.cs @@ -31,14 +31,15 @@ namespace mbts.ui.settings { } public static void load_column_settings(IEnumerable columns, GridSettings grid_settings) => - load_column_settings(columns, grid_settings.columns, new FontFamily(grid_settings.font_family), grid_settings.font_size, grid_settings.font_color.rgb_color(), grid_settings.font_weight); + load_column_settings(columns, grid_settings.columns, grid_settings.background, new FontFamily(grid_settings.font_family), grid_settings.font_size, grid_settings.font_color.rgb_color(), grid_settings.font_weight); - public static void load_column_settings(IEnumerable columns, IEnumerable settings, FontFamily grid_font_family, double grid_font_size, Color grid_font_color, FontWeight grid_font_weight) => - load_column_settings(columns, settings, grid_font_family, grid_font_size, grid_font_color, grid_font_weight, (c, s) => c.load_from(s)); + public static void load_column_settings(IEnumerable columns, IEnumerable settings, Color grid_background, FontFamily grid_font_family, double grid_font_size, Color grid_font_color, FontWeight grid_font_weight) => + load_column_settings(columns, settings, grid_background, grid_font_family, grid_font_size, grid_font_color, grid_font_weight, (c, s) => c.load_from(s)); public static void load_column_settings( IEnumerable columns, IEnumerable settings, + Color grid_background, FontFamily grid_font_family, double grid_font_size, Color grid_font_color, @@ -52,7 +53,7 @@ namespace mbts.ui.settings { select new { col, stg }; foreach (var tup in join) { load_impl(tup.col, tup.stg); - tup.col.set_fallback_font_params(grid_font_family, grid_font_size, grid_font_color, grid_font_weight); + tup.col.set_fallback_font_params(grid_font_family, grid_font_size, grid_font_color, grid_font_weight, grid_background); } } diff --git a/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsManager.cs b/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsManager.cs index 11dc2d487..a006995f0 100644 --- a/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsManager.cs +++ b/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsManager.cs @@ -30,7 +30,7 @@ namespace mbts.ui { readonly DelegateProperty _font_family; readonly DelegateProperty _font_weight; readonly DelegateProperty _font_size; - readonly DelegateProperty _font_color, _selection_highlight; + readonly DelegateProperty _font_color, _selection_highlight, _background_color; readonly SetMap, AnimationDefinitionWrapper> _animation_defs = new SetMap, AnimationDefinitionWrapper>(); readonly Func _predicate_data_factory; readonly Func> _predicate_factory; @@ -77,6 +77,7 @@ namespace mbts.ui { _font_size = backing(nameof(font_size), () => grid_settings.font_size, nameof(font_size)); _font_color = backing(nameof(font_color), () => grid_settings.font_color.rgb_solid_brush().frozen(), nameof(grid_settings)); _selection_highlight = backing(nameof(selection_highlight), () => grid_settings.selection_highlight.to_brush().frozen(), nameof(grid_settings)); + _background_color = backing(nameof(background_color), () => grid_settings.background_color.argb_color().to_solid_brush().frozen(), nameof(grid_settings)); } public void Dispose() => @@ -88,6 +89,7 @@ namespace mbts.ui { public double font_size => _font_size.Value; public SolidColorBrush font_color => _font_color.Value; public SolidColorBrush selection_highlight => _selection_highlight.Value; + public SolidColorBrush background_color => _background_color.Value; public GridSettingsVM create_vm(GridSettings settings) { var vm = new GridSettingsVM(_bus, _orders_model, _predicate_data_factory, _notification_predicate_factory, _create_predicate_vm, _create_column_predicate_vm, _create_settings_obj, _column_flags.ToDictionary(kv => kv.Key, kv => kv.Value.flags)); @@ -112,7 +114,7 @@ namespace mbts.ui { var grid = value.general ?? GridSettings.Default; _grid_settings.set(grid); foreach (ColumnBase col in _columns) - col.set_fallback_font_params(new FontFamily(grid.font_family), grid.font_size, grid.font_color.rgb_color(), grid.font_weight); + col.set_fallback_font_params(new FontFamily(grid.font_family), grid.font_size, grid.font_color.rgb_color(), grid.font_weight, grid.background_color.argb_color()); _predicate_animations_data = value.predicate_animations; _animation_defs.clear(); foreach (var pa in _predicate_animations_data) { diff --git a/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsVM.cs b/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsVM.cs index 175940d56..6df1bfd9f 100644 --- a/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsVM.cs +++ b/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsVM.cs @@ -1,5 +1,6 @@ using bts.api; using bts.collections; +using bts.proto; using bts.shell; using bts.utils; using mbts.collections; @@ -56,7 +57,7 @@ namespace mbts.spark { readonly Property _font_weight; readonly Property _font_display_name; readonly Property _font_size; - readonly Property _show_predicate_animations, _show_cell_predicate_animations; + readonly Property _show_predicate_animations, _show_cell_predicate_animations, _show_background_color; readonly OrdersModel _orders_model; readonly IReadOnlyDictionary _column_flags; @@ -83,6 +84,7 @@ namespace mbts.spark { _font_display_name = backing(nameof(font_display_name), () => TextUtils.display_name(font_family, font_size), new[] { nameof(font_family), nameof(font_size) }); _show_predicate_animations = backing(nameof(show_predicate_animations), true); _show_cell_predicate_animations = backing(nameof(show_cell_predicate_animations), true); + _show_background_color = backing(nameof(show_background_color), false); _current_column = backing(nameof(current_column)); predicate_animations = new ObservableEditableCollection(() => new PredicateRowAnimationVM(_create_predicate_vm(), _predicate_data_factory), true); @@ -100,6 +102,8 @@ namespace mbts.spark { public FontWeight font_weight { get => _font_weight.Value; set => _font_weight.set(value); } public ColorPickerPopupVM font_color { get; } = new ColorPickerPopupVM(); public ColorPickerPopupVM selection_highlight { get; } = new ColorPickerPopupVM(); + public ColorPickerPopupVM background_color { get; } = new ColorPickerPopupVM(); + public bool show_background_color => _show_background_color.Value; public double font_size { get => _font_size.Value; set => _font_size.set(value); } public bool enable_sound_triggers => true; public bool show_predicate_animations { get => _show_predicate_animations.Value; set => _show_predicate_animations.set(value); } @@ -130,7 +134,8 @@ namespace mbts.spark { foreach (IGrouping g in animations) animation_map.AddOrUpdate(g.Key, p => animation_tuple(c: g), (p, t) => t.cell_animations.AddRange(g)); } - return _create_settings_obj(new GridSettings(font_family.name(), font_size, font_weight, font_color.color.rgb_color(), selection_highlight.to_proto(), col_settings), + return _create_settings_obj(new GridSettings(font_family.name(), font_size, font_weight, font_color.color.rgb_color(), selection_highlight.to_proto(), col_settings, + background: background_color.color.with((byte)(MathUtils.within_range(0, 1, background_color.opacity) * 255.0)).rgb_color()), animation_map.Select(kv => PredicateAnimations.create_data(kv.Key, kv.Value.cell_animations, kv.Value.row_animations, kv.Value.sounds))); // (List row_animations, List sounds, List cell_animations) animation_tuple(IEnumerable r = null, IEnumerable s = null, IGrouping c = null) => @@ -142,6 +147,14 @@ namespace mbts.spark { _font_weight.set(sd.general.font_weight); font_color.load_from(sd.general.font_color.rgb_solid_brush()); selection_highlight.load_from(sd.general.selection_highlight.to_brush()); + _show_background_color.set(sd.general.enable_background_color); + if (sd.general.enable_background_color) { + // note that when the OB is moved to allow background colors + // we can just background_color.color instead of load_from + // so that we get all the alpha goodness similar to SimpleGridSettingsVM + // assuming bind_opacity_to_alpha on the picker. + background_color.load_from(sd.general.background_color.argb_color().to_solid_brush()); + } _font_size.set(sd.general.font_size); var col_animations = new Dictionary>>(); predicate_animations.Clear(); diff --git a/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsView.xaml b/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsView.xaml index 1c232e041..ebed749cc 100644 --- a/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsView.xaml +++ b/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsView.xaml @@ -350,6 +350,8 @@ + + @@ -387,6 +389,16 @@ Visibility="{Binding is_font_color_overridden, Converter={StaticResource FalseVisibilityConverter}}" Style="{StaticResource DisabledPlaceholderComboBox}" /> + Override Background Color + + + + diff --git a/src/client/spark/view/Controls/DIYGrid/Settings/ImmutableColumnSettingsStore.cs b/src/client/spark/view/Controls/DIYGrid/Settings/ImmutableColumnSettingsStore.cs index 4ab50812d..5b6926814 100644 --- a/src/client/spark/view/Controls/DIYGrid/Settings/ImmutableColumnSettingsStore.cs +++ b/src/client/spark/view/Controls/DIYGrid/Settings/ImmutableColumnSettingsStore.cs @@ -21,6 +21,9 @@ namespace mbts.ui { if ((flags & GridColumnSettingsFeatureFlags.ImmutableFontWeight) != 0) { stores_per_flag.Add(new ImmutableFontWeightOverrideStore()); } + if ((flags & GridColumnSettingsFeatureFlags.ImmutableBackgroundColor) != 0) { + stores_per_flag.Add(new ImmutableBackgroundColorOverrideStore()); + } return new CompositeImmutableSettingsStore(stores_per_flag, flags); } @@ -66,6 +69,19 @@ namespace mbts.ui { _value = new NullableFontWeightProto(settings.font_weight_override); } + class ImmutableBackgroundColorOverrideStore : ImmutableColumnSettingStore { + NullableColorProto _value; + + public ImmutableBackgroundColorOverrideStore() + : base(GridColumnSettingsFeatureFlags.ImmutableBackgroundColor) { } + + public override GridColumnSettings apply(GridColumnSettings settings) => + settings.with(background_color_override: _value); + + public override void store(GridColumnSettings settings) => + _value = new NullableColorProto(settings.background_color_override); + } + class CompositeImmutableSettingsStore : ImmutableColumnSettingStore { readonly IReadOnlyList _stores; public CompositeImmutableSettingsStore(IReadOnlyList stores, GridColumnSettingsFeatureFlags flags) diff --git a/src/client/spark/view/Controls/DIYGrid/Settings/SimpleGridColumnSettingsVM.cs b/src/client/spark/view/Controls/DIYGrid/Settings/SimpleGridColumnSettingsVM.cs index 7ac3633ae..a4028ceb6 100644 --- a/src/client/spark/view/Controls/DIYGrid/Settings/SimpleGridColumnSettingsVM.cs +++ b/src/client/spark/view/Controls/DIYGrid/Settings/SimpleGridColumnSettingsVM.cs @@ -21,7 +21,9 @@ namespace mbts.spark { _can_override_font_color = true, _is_font_color_overridden, _can_override_font_weight = true, - _is_font_weight_overridden; + _is_font_weight_overridden, + _can_override_background_color = true, + _is_background_color_overridden; FontWeight _font_weight_override; GridColumnSettings _settings; @@ -43,16 +45,23 @@ namespace mbts.spark { public bool can_override_font_weight { get => _can_override_font_weight; private set => Set(ref _can_override_font_weight, value); } public bool is_font_weight_overridden { get => _is_font_weight_overridden; set => Set(ref _is_font_weight_overridden, value); } public FontWeight font_weight_override { get => _font_weight_override; set => Set(ref _font_weight_override, value); } + // background color override + public bool can_override_background_color { get => _can_override_background_color; private set => Set(ref _can_override_background_color, value); } + public bool is_background_color_overridden { get => _is_background_color_overridden; set => Set(ref _is_background_color_overridden, value); } + public ColorPickerPopupVM background_color_override { get; } = new ColorPickerPopupVM(); public GridColumnSettings create() => _settings.with( can_override_display_name ? display_name : null, is_visible, null, - is_font_weight_overridden ? new NullableFontWeightProto(font_weight_override) : new NullableFontWeightProto(null), - is_font_color_overridden ? new NullableColorProto(font_color_override.color) : new NullableColorProto(null), + can_override_font_weight ? (is_font_weight_overridden ? new NullableFontWeightProto(font_weight_override) : new NullableFontWeightProto()) : (NullableFontWeightProto?)null, + can_override_font_color ? (is_font_color_overridden ? new NullableColorProto(font_color_override.color) : new NullableColorProto()) : (NullableColorProto?)null, width, - horizontal_alignment); + horizontal_alignment, + background_color_override: + can_override_background_color ? (is_background_color_overridden ? new NullableColorProto(background_color_override.color) : new NullableColorProto()) : (NullableColorProto ?)null + ); public void load_from(GridColumnSettings value) { _settings = value; @@ -71,6 +80,13 @@ namespace mbts.spark { if (is_font_weight_overridden) { _font_weight_override = _settings.font_weight_override.Value; } + // background color + is_background_color_overridden = _settings.background_color_override.HasValue; + if (is_background_color_overridden) { + background_color_override.load_from( + // don't set opacity (the color's Alpha bit is stored in the setting) + new SolidColorBrush(_settings.background_color_override.Value)); + } } public void apply_flags(GridColumnSettingsFeatureFlags flags) { @@ -84,6 +100,9 @@ namespace mbts.spark { // font weight can_override_font_weight = (flags & GridColumnSettingsFeatureFlags.ImmutableFontWeight) == 0; is_font_weight_overridden &= can_override_font_weight; + // background color + can_override_background_color = (flags & GridColumnSettingsFeatureFlags.ImmutableBackgroundColor) == 0; + is_background_color_overridden &= can_override_background_color; } public override string ToString() => $"{display_name} ({id})"; diff --git a/src/client/spark/view/Controls/DIYGrid/Settings/SimpleGridSettingsManager.cs b/src/client/spark/view/Controls/DIYGrid/Settings/SimpleGridSettingsManager.cs index 76823094e..d29df539d 100644 --- a/src/client/spark/view/Controls/DIYGrid/Settings/SimpleGridSettingsManager.cs +++ b/src/client/spark/view/Controls/DIYGrid/Settings/SimpleGridSettingsManager.cs @@ -20,7 +20,7 @@ namespace mbts.ui { readonly DelegateProperty _font_family; readonly DelegateProperty _font_weight; readonly DelegateProperty _font_size; - readonly DelegateProperty _font_color, _selection_highlight; + readonly DelegateProperty _font_color, _selection_highlight, _background_color; readonly IReadOnlyDictionary _column_flags; public SimpleGridSettingsManager(ObservableCollection columns) @@ -35,6 +35,7 @@ namespace mbts.ui { _font_size = backing(nameof(font_size), () => grid_settings.font_size, nameof(font_size)); _font_color = backing(nameof(font_color), () => grid_settings.font_color.rgb_solid_brush().frozen(), nameof(grid_settings)); _selection_highlight = backing(nameof(selection_highlight), () => grid_settings.selection_highlight.to_brush().frozen(), nameof(grid_settings)); + _background_color = backing(nameof(background_color), () => grid_settings.background_color.argb_color().to_solid_brush().frozen(), nameof(grid_settings)); _row_vms = row_vms; } @@ -44,6 +45,7 @@ namespace mbts.ui { public double font_size => _font_size.Value; public SolidColorBrush font_color => _font_color.Value; public SolidColorBrush selection_highlight => _selection_highlight.Value; + public SolidColorBrush background_color => _background_color.Value; public SimpleGridSettingsVM create_vm(GridSettings settings) { var vm = new SimpleGridSettingsVM(_column_flags.ToDictionary(kv => kv.Key, kv => kv.Value.flags)); @@ -86,7 +88,7 @@ namespace mbts.ui { grid = grid ?? GridSettings.Default; _grid_settings.set(grid); foreach (ColumnBase col in _columns) - col.set_fallback_font_params(new FontFamily(grid.font_family), grid.font_size, grid.font_color.rgb_color(), grid.font_weight); + col.set_fallback_font_params(new FontFamily(grid.font_family), grid.font_size, grid.font_color.rgb_color(), grid.font_weight, grid.background_color.argb_color()); // ** in the unfortunate event that we botch backwards compatability, and the columns don't bear a 1:1 relationship with the column-settings // don't add insult to injury by crashing ** if (grid_settings.columns != null) { diff --git a/src/client/spark/view/Controls/DIYGrid/Settings/SimpleGridSettingsVM.cs b/src/client/spark/view/Controls/DIYGrid/Settings/SimpleGridSettingsVM.cs index d3c9c3b94..ee00e328c 100644 --- a/src/client/spark/view/Controls/DIYGrid/Settings/SimpleGridSettingsVM.cs +++ b/src/client/spark/view/Controls/DIYGrid/Settings/SimpleGridSettingsVM.cs @@ -36,12 +36,10 @@ namespace mbts.spark { readonly Property _font_weight; readonly Property _font_display_name; readonly Property _font_size; - readonly Property _show_predicate_animations, _show_cell_predicate_animations; + readonly Property _show_predicate_animations, _show_cell_predicate_animations, _show_background_color; readonly DeferrableActionWrapper _update; readonly IReadOnlyDictionary _column_flags; readonly Func _column_factory; - bool _can_edit_background; - ColorPickerPopupVM _background; public SimpleGridSettingsVM(IReadOnlyDictionary column_flags = null, Func column_factory = null) { _column_flags = column_flags ?? new Dictionary(); @@ -52,6 +50,7 @@ namespace mbts.spark { _font_display_name = backing(nameof(font_display_name), () => TextUtils.display_name(font_family, font_size), new[] { nameof(font_family), nameof(font_size) }); _show_predicate_animations = backing(nameof(show_predicate_animations), true); _show_cell_predicate_animations = backing(nameof(show_cell_predicate_animations), true); + _show_background_color = backing(nameof(show_background_color), false); _current_column = backing(nameof(current_column)); // update _update = new DeferrableActionWrapper(() => result_changed?.Invoke()); @@ -82,11 +81,13 @@ namespace mbts.spark { public FontFamily font_family { get => _font_family.Value; set => _font_family.set(value); } public FontWeight font_weight { get => _font_weight.Value; set => _font_weight.set(value); } public ColorPickerPopupVM font_color { get; } = new ColorPickerPopupVM(); - public ColorPickerPopupVM selection_highlight { get; } = new ColorPickerPopupVM(); - public ColorPickerPopupVM background => _background ?? (_background = new ColorPickerPopupVM { bind_opacity_to_alpha = true, opacity = 1 }); + public ColorPickerPopupVM selection_highlight { get; } = new ColorPickerPopupVM() { bind_opacity_to_alpha = true }; + public ColorPickerPopupVM background_color { get; } = new ColorPickerPopupVM() { bind_opacity_to_alpha = true }; + // TODO: consolidate background/background_color into 1 property (tricky merge yielded 2 properties with the same purpose) + public ColorPickerPopupVM background => background_color; + public bool show_background_color => _show_background_color.Value; public double font_size { get => _font_size.Value; set => _font_size.set(value); } public bool enable_sound_triggers => false; - public bool can_edit_background { get => _can_edit_background; set => Set(ref _can_edit_background, value); } public bool show_predicate_animations { get => _show_predicate_animations.Value; set => _show_predicate_animations.set(value); } public bool show_cell_predicate_animations { get => _show_cell_predicate_animations.Value; set => _show_cell_predicate_animations.set(value); } public IReadOnlyList dialog_skirt_items => new[] { @@ -104,10 +105,15 @@ namespace mbts.spark { get { yield return font_color; yield return selection_highlight; + yield return background_color; yield return this; } } + // Note that one of the responsibilites of create() is via instance_vm.get_result() in the ShellSettingsDialogVM.export_to_custom_cmd, + // which is what handles saving .grid files. This means if you have introduced any optional args, say for example enable_background_color + // that is not yet a feature of all windows at this point in time, 8/28/2019, you need to be sure it's explicity set if the proto + // constructor parameter default has been initialized to different default value. public GridSettings create() { var rv = new GridSettings( font_family.name(), @@ -116,8 +122,8 @@ namespace mbts.spark { font_color.color.rgb_color(), selection_highlight.to_proto(), columns.Select(c => c.create())); - if (can_edit_background) - rv = rv.with(background: background.color.rgb_color()); + if (show_background_color) + rv = rv.with(background: background_color.color.rgb_color()); return rv; } @@ -128,15 +134,17 @@ namespace mbts.spark { _font_family.set(new FontFamily(value.font_family)); _font_weight.set(value.font_weight); font_color.load_from(value.font_color.rgb_solid_brush()); - selection_highlight.load_from(value.selection_highlight.to_brush()); + selection_highlight.color = value.selection_highlight.argb_color; + _show_background_color.set(value.enable_background_color); + if (value.enable_background_color) { + background_color.color = value.background; + } _font_size.set(value.font_size); var curr_col_id = current_column?.id; columns.SetItems(value.columns.Select(_column_factory)); if (curr_col_id == null || !columns.TryGetFirst(c => c.id == curr_col_id, out ISimpleColumnVM curr_column)) curr_column = columns.FirstOrDefault(); _current_column.set(curr_column); - if (can_edit_background) - background.color = value.background; } } @@ -154,4 +162,4 @@ namespace mbts.spark { void IWindowSettingsVM.load(GridSettings value) => load_from(value); GridSettings IWindowSettingsVM.get_result() => create(); } -} \ No newline at end of file +} diff --git a/src/client/spark/view/Controls/DataGridEx/Filters/VM/DiscreteValuesFilterVM.cs b/src/client/spark/view/Controls/DataGridEx/Filters/VM/DiscreteValuesFilterVM.cs index bec6942cd..cae2aa8a5 100644 --- a/src/client/spark/view/Controls/DataGridEx/Filters/VM/DiscreteValuesFilterVM.cs +++ b/src/client/spark/view/Controls/DataGridEx/Filters/VM/DiscreteValuesFilterVM.cs @@ -112,10 +112,11 @@ namespace mbts.spark.filters { class DynamicItemsFilter : FilterVMBase, IDisposable { readonly IEqualityComparer _equality_comparer; + readonly IComparer _comparer; readonly Func, TState> _get_state; readonly Func>> _load_state; readonly HashSet _items_set, _selected_items_set; - readonly ObservableCollection _unsorted_items; + readonly ObservableCollection _items = new ObservableCollection(); bool _override_desired_selection; IDisposable _desired_items_sub, _items_sub; @@ -131,7 +132,7 @@ namespace mbts.spark.filters { // _items_sub = items.OnDispatcher().Subscribe(new_items => { _items_set.SetItems(new_items.Concat(_selected_items_set)); - _unsorted_items.sync_with(_items_set); + _sync_items(); }); } @@ -150,7 +151,7 @@ namespace mbts.spark.filters { _items_set.Remove(v); foreach (var v in update.added) _items_set.Add(v); - _unsorted_items.sync_with(_items_set); + _sync_items(); }); } @@ -166,11 +167,12 @@ namespace mbts.spark.filters { _get_state = get_state; _load_state = load_state; _equality_comparer = equality_comparer; + _comparer = comparer; _selected_items_set = new HashSet(_equality_comparer); _items_set = new HashSet(_equality_comparer); - _unsorted_items = new ObservableCollection(); - items = comparer != null ? ObservableAutoWrapper.create_sorted_wrapper(_unsorted_items, comparer) : ObservableAutoWrapper.create_read_only_wrapper(_unsorted_items); + items = ObservableAutoWrapper.create_read_only_wrapper(_items); selected_items.CollectionChanged += _on_selected_items_CollectionChanged; + _sync_items(); } public IReadOnlyObservableCollection items { get; } @@ -240,9 +242,11 @@ namespace mbts.spark.filters { var copy = new HashSet(_selected_items_set, _equality_comparer); _selected_items_set.SetItems(dsi); using (selected_items.SuspendHandler(_on_selected_items_CollectionChanged)) { - foreach (var item in _selected_items_set) { - if (_items_set.Add(item)) - _unsorted_items.Add(item); + var to_add = _selected_items_set.Where(_items_set.Add); + if (_comparer != null) { + _items.InsertSorted(to_add, _comparer); + } else { + _items.AddRange(to_add); } selected_items.sync_with(_selected_items_set); } @@ -250,6 +254,14 @@ namespace mbts.spark.filters { _raise_filter_changed(); } } + + void _sync_items() { + if (_comparer != null) { + _items.sync_with(_items_set.Ordered(_comparer)); + } else { + _items.sync_with(_items_set); + } + } } class TableUpdateItemsFilter : FilterVMBase, IObserver> where TKey : struct, IEquatable { diff --git a/src/client/spark/view/Controls/ListBoxEx/ListBoxEx.xaml.cs b/src/client/spark/view/Controls/ListBoxEx/ListBoxEx.xaml.cs index 5fbdcd504..3b663b650 100644 --- a/src/client/spark/view/Controls/ListBoxEx/ListBoxEx.xaml.cs +++ b/src/client/spark/view/Controls/ListBoxEx/ListBoxEx.xaml.cs @@ -101,7 +101,8 @@ namespace mbts.ui.controls case NotifyCollectionChangedAction.Replace: if (e.OldItems != null) foreach (var item in e.OldItems) _set_is_selected(item, false); if (e.NewItems != null) foreach (var item in e.NewItems) _set_is_selected(item, true); break; - case NotifyCollectionChangedAction.Reset: foreach (var item in ItemsSource) _set_is_selected(item, false); break; + case NotifyCollectionChangedAction.Reset: + if (ItemsSource != null) foreach (var item in ItemsSource) _set_is_selected(item, false); break; } _update_is_all_selected(); } diff --git a/src/client/spark/view/Converters/AppendTextConverter.cs b/src/client/spark/view/Converters/AppendTextConverter.cs new file mode 100644 index 000000000..c9de27145 --- /dev/null +++ b/src/client/spark/view/Converters/AppendTextConverter.cs @@ -0,0 +1,22 @@ +using System; +using System.Globalization; +using System.Text; +using System.Windows.Data; + +namespace mbts.ui.converters { + class AppendTextConverter : IValueConverter { + public string Prefix { get; set; } + public string Suffix { get; set; } + + public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { + var sb = new StringBuilder(value?.ToString()); + // don't need to check for null (Insert/Append support it) + sb.Insert(0, Prefix); + sb.Append(Suffix); + return sb.ToString(); + } + + public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => + throw new NotImplementedException(); + } +} \ No newline at end of file diff --git a/src/client/spark/view/Extensions/UIElementExtensions.cs b/src/client/spark/view/Extensions/UIElementExtensions.cs index d9951973f..0e5924fd2 100644 --- a/src/client/spark/view/Extensions/UIElementExtensions.cs +++ b/src/client/spark/view/Extensions/UIElementExtensions.cs @@ -170,6 +170,19 @@ namespace mbts.ui.extensions { } } + public static IDisposable SubscribePreviewKeyUp(this UIElement target, KeyEventHandler callback) { + target.PreviewKeyUp += callback; + return Disposable.Create(() => target.PreviewKeyUp -= callback); + } + + public static IDisposable SubscribePreviewKeyUp(this UIElement target, Key key, Action callback) { + return SubscribePreviewKeyUp(target, on_preview_key_up); + void on_preview_key_up(object sender, KeyEventArgs e) { + if (e.Key == key) + callback(); + } + } + public static IObservable SubscribePreviewKeyDown(this UIElement target) => Observable.Create(o => { target.PreviewKeyDown += on_preview_key_down; @@ -211,6 +224,13 @@ namespace mbts.ui.extensions { } } + public static class SelectorExtensions { + public static IDisposable SubscribeSelectionChanged(this System.Windows.Controls.Primitives.Selector target, SelectionChangedEventHandler callback) { + target.SelectionChanged += callback; + return Disposable.Create(() => target.SelectionChanged -= callback); + } + } + public static class ScrollViewerExtensions { public static IDisposable SubscribeScrollChanged(this ScrollViewer target, ScrollChangedEventHandler callback) { target.ScrollChanged += callback; diff --git a/src/client/spark/view/Resources/Buttons.xaml b/src/client/spark/view/Resources/Buttons.xaml index 847b4a715..c7e524120 100644 --- a/src/client/spark/view/Resources/Buttons.xaml +++ b/src/client/spark/view/Resources/Buttons.xaml @@ -628,47 +628,47 @@ 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 fe1d0a6f4..51884b83c 100644 --- a/src/client/spark/view/Resources/WindowStyle.xaml +++ b/src/client/spark/view/Resources/WindowStyle.xaml @@ -148,6 +148,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/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 d3b86f5d0..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 } @@ -103,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) { 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 b98ed6d8b..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/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 0c86f2a7f..022542544 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 //////////////////// //////////////////////////////////////////// @@ -55,31 +59,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; } @@ -167,45 +172,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); @@ -228,18 +284,36 @@ 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 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 BTSAccount* parent_account = + find_account(data, account.parent_account_id()); + if (parent_account != nullptr) + return get_account_ice_data (*parent_account, data); + } + return nullptr; +} + const bts::api::admin::SIMAccountData* AdminServiceUtils::get_account_sim_data(const bts::api::admin::BTSAccount& account, const bts::api::admin::AdminData& data){ if (account.has_exchange_account_data() && account.exchange_account_data().has_sim_account_data()) @@ -252,7 +326,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; iclearing_account(); break; } + case api::Venue::VENUE_ICE:{ + auto ice_account_data = get_account_ice_data(account, data); + if (ice_account_data != nullptr) + rv = ice_account_data->clearing_account_override(); + break; + } case api::Venue::VENUE_SIM: { auto sim_account_data = get_account_sim_data(account, data); if (sim_account_data != nullptr) @@ -284,11 +365,11 @@ std::string AdminServiceUtils::find_account_exchange_account (const bts::api::ad case api::Venue::VENUE_UNSET: case api::Venue::VENUE_BTS: case api::Venue::VENUE_CBOE: - case api::Venue::VENUE_CBOECFE: - case api::Venue::VENUE_ICE: + case api::Venue::VENUE_CBOECFE: case api::Venue::Venue_INT_MIN_SENTINEL_DO_NOT_USE_: case api::Venue::Venue_INT_MAX_SENTINEL_DO_NOT_USE_: - out_result.set_is_success(false); out_result.set_error_text("Exchange not supported"); + out_result.set_is_success(false); + out_result.set_error_text("Exchange not supported"); break; } return rv; diff --git a/src/lib/api/service_helpers/AdminServiceUtils.h b/src/lib/api/service_helpers/AdminServiceUtils.h index 781a82362..ebd941cf6 100644 --- a/src/lib/api/service_helpers/AdminServiceUtils.h +++ b/src/lib/api/service_helpers/AdminServiceUtils.h @@ -60,6 +60,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, @@ -85,6 +91,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 SIMAccountData* get_account_sim_data(const BTSAccount& account, const AdminData& data); // returns true if an account has a connection 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 f79cc5b1c..a274c3cb6 100644 --- a/src/lib/api/services/AdminService.proto +++ b/src/lib/api/services/AdminService.proto @@ -154,6 +154,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 40bfa150a..091487e66 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 dce50e8c6..b7eabbb88 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" @@ -348,11 +349,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 f22836227..a8dd25828 100644 --- a/src/lib/config/defaults/MarketData.conf +++ b/src/lib/config/defaults/MarketData.conf @@ -94,3 +94,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 0055809d5..18c08950a 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; @@ -174,11 +175,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]; @@ -216,3 +222,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/domain_model/Security.cpp b/src/lib/domain_model/Security.cpp index 5a8dbd611..94df17488 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 { @@ -64,12 +66,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 54a35a324..722e6670b 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); } } // namespace securities } // namespace bts 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 a159e291a..fd5f7bfec 100644 --- a/src/lib/md/Shm/FixedSecurity.cpp +++ b/src/lib/md/Shm/FixedSecurity.cpp @@ -140,7 +140,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 54c3a9066..b7d4da066 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 30caf3bde..f38ca3325 100644 --- a/src/lib/md/test/Co2ManagedTestHelper.cpp +++ b/src/lib/md/test/Co2ManagedTestHelper.cpp @@ -120,7 +120,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 13c99fda3..c5a492fa7 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 @@ -117,10 +117,10 @@ void FillStore::on_execution_report( fill.set_side(er.side); fill.set_last_shares(er.last_shares.value()); 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 0612e881f..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.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 76bcabfb4..3e39108bf 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" @@ -28,7 +28,7 @@ using namespace mercury; enum class OrderManager::State { WAIT_ADMIN, WAIT_SEC, - WAIT_FILLS, + WAIT_FILLED_POSITION, WAIT_ORDERS, READY }; @@ -41,7 +41,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; }; @@ -115,36 +115,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 322b2a7a8..d00459f35 100644 --- a/src/lib/orderman/actors/order_manager/OrderServices.h +++ b/src/lib/orderman/actors/order_manager/OrderServices.h @@ -164,6 +164,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; @@ -269,7 +270,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/orderman_utils/fix_order_codec.h b/src/lib/orderman/orderman_utils/fix_order_codec.h index 5b69c7d1b..6b83b8794 100644 --- a/src/lib/orderman/orderman_utils/fix_order_codec.h +++ b/src/lib/orderman/orderman_utils/fix_order_codec.h @@ -1,50 +1,63 @@ #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" 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 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; } -}} +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/NativeOrder.cpp b/src/lib/orderman/orders/NativeOrder.cpp index 0fbf80394..c32eca3e9 100644 --- a/src/lib/orderman/orders/NativeOrder.cpp +++ b/src/lib/orderman/orders/NativeOrder.cpp @@ -31,7 +31,7 @@ OrderStatus map_external_status(NativeOrderStatus); 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 NativeOrder::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 NativeOrder::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,6 +74,14 @@ struct NativeOrder::PImpl { std::optional> buffered_update; }; +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()); +} + NativeOrder::NativeOrder() : me(new PImpl()) {} OrderDefinition const &NativeOrder::definition() const { @@ -171,7 +181,7 @@ void NativeOrder::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()) { @@ -464,6 +474,10 @@ NativeOrder::_on_activate(log::Level reject_level, log::Level transition_level, 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{}; } @@ -670,6 +684,9 @@ OrderOpResult NativeOrder::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); @@ -909,7 +926,7 @@ void NativeOrder::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: @@ -975,7 +992,7 @@ void NativeOrder::on_cancel_reject(const orderman::CancelRejectEvent &cr) { switch (me->state.status()) { case ORDER_STATUS_COMPLETED: - case ORDER_STATUS_CANCELED: + case ORDER_STATUS_CANCELED: break; case ORDER_STATUS_DISCONNECTED: @@ -1059,22 +1076,20 @@ void NativeOrder::on_gateway_status(const GatewayStatusChangeEvent &ev) { 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 " @@ -1125,6 +1140,9 @@ void NativeOrder::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 NativeOrder::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; @@ -1338,20 +1358,22 @@ void NativeOrder::transition_to_error( transition(ORDER_STATUS_ERROR, msg); } -Quantity NativeOrder::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_: diff --git a/src/lib/orderman/orders/NativeOrder.h b/src/lib/orderman/orders/NativeOrder.h index 472c1627a..e3b7d6fa4 100644 --- a/src/lib/orderman/orders/NativeOrder.h +++ b/src/lib/orderman/orders/NativeOrder.h @@ -62,7 +62,7 @@ class NativeOrder : public Order { 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; 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/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 a37e4937a..819fb5bdb 100644 --- a/src/server/sparkd/exchange_sim/ExchangeSim.h +++ b/src/server/sparkd/exchange_sim/ExchangeSim.h @@ -6,6 +6,6 @@ namespace bts { namespace md { class MBOWriter; } namespace sim { -int start_exchange_sim(md::MBOWriter*); +int start_exchange_sim(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 f36be139d..ea2b3ef9b 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) @@ -82,106 +83,96 @@ 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); pair.second->start(subs); } + 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() @@ -192,10 +183,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; @@ -208,18 +199,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()); handler.registerExchangeListener(listener.get()); handler.registerFeedListener(listener.get()); 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 42f4da101..35ded097d 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 @@ -169,16 +170,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; } } @@ -208,13 +207,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 7cce85068..45d1fb9b4 100644 --- a/src/web/ui/src/Access.fs +++ b/src/web/ui/src/Access.fs @@ -111,13 +111,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 dabaa22ca..4647425ee 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)) @@ -76,33 +217,50 @@ let applyCMEUpdate (data:CMEAccountData) = 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) } @@ -125,6 +283,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() @@ -149,6 +327,7 @@ let clearSelection model = ShowLoading = false } + let orderIsEqual (order1:OrdersSummaryUnit) (order2:OrdersSummaryUnit) = (order1.account = order2.account) && (order1.user = order2.user) @@ -179,6 +358,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 -> @@ -189,6 +381,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 -> @@ -210,11 +403,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), @@ -235,6 +441,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 @@ -248,7 +460,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 @@ -256,11 +468,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) -> @@ -327,8 +545,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 -> @@ -368,8 +587,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 21d38d90b..a0bf2442f 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.SIM |] @@ -44,9 +45,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 @@ -59,6 +72,9 @@ type Msg = | SetMarginCashFactor of double | SetEnforceMargin of bool + | ParseCSV of FileType * string + | SavePositionBatch of BasePositionBatch + | SelectAccount of BTSAccount | DeleteAccount of BTSAccount | AddAccount @@ -85,6 +101,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 @@ -100,10 +123,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 @@ -125,4 +173,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 2f0b5db66..1c624e10e 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 [ ] @@ -840,14 +1133,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 @@ -861,6 +1155,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" ] [ @@ -893,6 +1192,7 @@ let accountSelectionGrid (accountList:BTSAccount list) (currentAccount:BTSAccoun ] className = "" header = None + onClick = None } let rws:TableControl.row list = [ for acc in activeAccounts do yield { @@ -906,6 +1206,7 @@ let accountSelectionGrid (accountList:BTSAccount list) (currentAccount:BTSAccoun ] className = "" header = None + onClick = None } ] div[][ @@ -981,11 +1282,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 ] ] @@ -1010,13 +1312,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 e359a4eef..6e8908f76 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 double (lbl:string) (dv:double) (attrs:IHTMLProp list) onchange = text lbl (string dv) attrs (double>>onchange) @@ -217,6 +246,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 @@ -225,14 +287,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 @@ -254,6 +315,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" ] [ @@ -266,7 +331,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) ] ] ] ] ] @@ -286,4 +351,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 ba2c57828..f1159f84e 100644 --- a/src/web/ui/src/Users/View.fs +++ b/src/web/ui/src/Users/View.fs @@ -39,13 +39,13 @@ let saveButton (model:Users.Types.Model) dispatch = let enabled = if dirty then Button.Enabled else Button.Disabled let flavor = if dirty then "is-success" else "" Button.button buttonText flavor "" enabled (fun _ -> dispatch SaveUser) - + let accountIDLookup (accountsList:BTSAccount list) (id:string) = match (List.tryFind (fun a -> a.id.ToString() = id) accountsList) with | Some acnt -> acnt.id.ToString() | None -> "" - + let generalTabView (model:Model) (dispatch:Msg->Unit) (readonly: bool)= let accMap = @@ -53,43 +53,43 @@ let generalTabView (model:Model) (dispatch:Msg->Unit) (readonly: bool)= |> List.filter (fun a -> a.isPresent) |> List.map (fun a -> a.id.ToString(), a.name) |> Map.ofList - - let hasPermission (acctid) = + + let hasPermission (acctid) = let found = JSMap.tryFind (acctid.ToString()) model.scratchUser.userData.accountPermissions.accountPermissions - match found with + match found with | None -> false | Some a -> a.tradingPermission.hasPermissions - - let acctlist = model.accountsList |> List.filter (fun a -> a.isPresent) - - let hasOnePermission = - let found = + + let acctlist = model.accountsList |> List.filter (fun a -> a.isPresent) + + let hasOnePermission = + let found = [ for entry in acctlist do yield JSMap.tryFind (entry.id.ToString()) model.scratchUser.userData.accountPermissions.accountPermissions ] |> List.choose id - - let trues = - List.choose (fun x -> - match x.tradingPermission.hasPermissions with + + let trues = + List.choose (fun x -> + match x.tradingPermission.hasPermissions with | true -> Some x | false -> None ) found - + match List.length (trues) with | 0 -> false - | _ -> true - - - let checkboxes = - [ - for acc in acctlist do - yield span [] [ - Fields.checkbox acc.name "" (hasPermission acc.id) [] ( fun x-> (acc.id.ToString(), x) |> TradingPerm |> UpdateField |> dispatch ) - ] - ] + | _ -> true - let can_create = model.can_create || (model.this_username = Access.superuser) + + let checkboxes = + [ + for acc in acctlist do + yield span [] [ + Fields.checkbox acc.name "" (hasPermission acc.id) [] ( fun x-> (acc.id.ToString(), x) |> TradingPerm |> UpdateField |> dispatch ) + ] + ] + + let can_create = model.can_create || (model.this_username = Access.superuser) let can_login_disabled = readonly || (model.this_username = model.scratchUser.name) let modal = Modal.showCreate "Create User" @@ -100,12 +100,12 @@ let generalTabView (model:Model) (dispatch:Msg->Unit) (readonly: bool)= Fields.text "Company" model.scratchUser.company [] (Company >> UpdateField >> dispatch) Fields.checkbox "Can Login?" "Unchecking this stops the user from logging in" model.scratchUser.isActive [] (IsActive >> UpdateField >> dispatch) Fields.checkbox "Can Create?" "Checking this allows the user to create other users and accounts" model.scratchUser.canCreate [Disabled (not can_create)] (CanCreate >> UpdateField >> dispatch) - + p [ Style [FontWeight "Bold"] ] [ str ("Trading Account")] - div [ Style [Padding " .5em 1em"]] checkboxes - //Fields.mapSelect "Trading Account" accMap (Some "Add Accounts") None ( fun x-> (x,true) |> TradingPerm |> UpdateField |> dispatch ) + div [ Style [Padding " .5em 1em"]] checkboxes + //Fields.mapSelect "Trading Account" accMap (Some "Add Accounts") None ( fun x-> (x,true) |> TradingPerm |> UpdateField |> dispatch ) ]) - (fun _ -> + (fun _ -> match hasOnePermission with | false -> dispatch (ErrorOccured "New Users must have at least one trading account") | true -> dispatch SaveUser) @@ -140,7 +140,7 @@ let accountsTabView (model:Model) (dispatch:Msg->Unit) (readonly:bool) = |> List.filter (fun a -> a.isPresent) |> List.map (fun a -> a.id.ToString(), a.name) |> Map.ofList - + let realAccList = model.accountsList |> List.filter (fun a -> a.isPresent) let headerRow:TableControl.row = @@ -154,12 +154,13 @@ let accountsTabView (model:Model) (dispatch:Msg->Unit) (readonly:bool) = ] className = "" header = None + onClick = None } - + let rows:TableControl.row list = - [for acc in realAccList do + [for acc in realAccList do let string_id = acc.id.ToString() - let perm = JSMap.tryFind string_id accountPermissions + let perm = JSMap.tryFind string_id accountPermissions let is_bts = model.scratchUser.name = Access.superuser let hasTradeAdmin = (if perm.IsNone then false else perm.Value.tradingPermission.hasPermissions) let hasRiskAdmin = is_bts || (if perm.IsNone then false else perm.Value.adminPermission.hasPermissions) @@ -168,7 +169,7 @@ let accountsTabView (model:Model) (dispatch:Msg->Unit) (readonly:bool) = yield { cells = [ TableControl.Cell(str acc.name) - { content = Fields.checkbox "" "" hasTradeAdmin [Disabled readonly] (update TradingPerm hasTradeAdmin) + { content = Fields.checkbox "" "" hasTradeAdmin [Disabled readonly] (update TradingPerm hasTradeAdmin) className = ""; isHeader = false; colSpan = 0 @@ -186,6 +187,7 @@ let accountsTabView (model:Model) (dispatch:Msg->Unit) (readonly:bool) = ] className="" header = None + onClick = None } ] @@ -202,7 +204,7 @@ let MarketDataTabView (model:Model) (dispatch:Msg->Unit) (readonly:bool) = match checkedValue with | Some t -> true | None -> false - + let micList = [ for micPair in CMEMicMap do yield ( @@ -212,9 +214,9 @@ let MarketDataTabView (model:Model) (dispatch:Msg->Unit) (readonly:bool) = (findCheckedValue micPair.Value) [Disabled readonly] (fun chk -> (({enabled = chk; mic = micPair.Value}:CMEMicData) |> (CMEMic >> UpdateField >> dispatch))) - ) + ) ] - + let send = UpdateField>>dispatch div [] @@ -227,12 +229,12 @@ let MarketDataTabView (model:Model) (dispatch:Msg->Unit) (readonly:bool) = //Pico recommends we hide this for the ICE //Fields.enumSelect "Pro / Non-Pro" (toHuman false ProfessionalDesignation.values cmeData.proNonpro) (allToHuman false ProfessionalDesignation.values) [Disabled readonly] (fromHuman ProfessionalDesignation.values>>UserField.CMEProNonPro>>send) Fields.enumSelect "Display / Non-Display" (toHuman false DisplayDesignation.values cmeData.displayNondisplay) (allToHuman false DisplayDesignation.values) [Disabled readonly] (fromHuman DisplayDesignation.values>>UserField.CMEDisplayNonDisplay>>send) - ] + ] -let orderRoutingTabView (model:Model) (dispatch:Msg->Unit) (readonly:bool) = +let orderRoutingTabView (model:Model) (dispatch:Msg->Unit) (readonly:bool) = let cmeData = model.scratchUser.userData.exchangeUserData.cmeData - let simData = model.scratchUser.userData.exchangeUserData.simData + let simData = model.scratchUser.userData.exchangeUserData.simData div [] [ @@ -255,10 +257,10 @@ let orderRoutingTabView (model:Model) (dispatch:Msg->Unit) (readonly:bool) = Fields.text "Location" simData.location [Disabled readonly] (SimLocation >> UpdateField >> dispatch) ] - ] - + ] + + - let userControls (model:Model) (dispatch:Msg->Unit) = let currentUser = State.getCurrentUser model @@ -273,9 +275,9 @@ let userControls (model:Model) (dispatch:Msg->Unit) = let tabContent (model:Model) (dispatch:Msg->Unit) display_md display_risk = let mod1 = { - model with currentTab = (if (not display_risk && display_md) then Tab.MarketData else model.currentTab) + model with currentTab = (if (not display_risk && display_md) then Tab.MarketData else model.currentTab) } - + div [Key (string model.scratchUser.id); ClassName "is-tab-content"] [ (match mod1.currentTab, ( State.userIsSelected model.scratchUser State.defaultScratchUser) with | _, false -> p [Style [ Color "Red" ]] [ str "You are not authorized to view this data" ] @@ -328,6 +330,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 { @@ -341,6 +344,7 @@ let userSelectionGrid (usersList:BTSUser list) (currentUser:BTSUser) (dispatch:M ] className = "" header = None + onClick = None } ] div[][ @@ -401,16 +405,16 @@ let NavRoot (usersList:BTSUser list) (currentUser:BTSUser) model (dispatch:Msg-> div [ Class "column is-narrow is-sidebar-menu" ] [ aside [ Class "menu" ] - [ p [ Class "menu-label" ] [ + [ p [ Class "menu-label" ] [ span [Class "is-size-4"] [str "Users"] - span [ Class "is-pulled-right circle-icon" ] + span [ Class "is-pulled-right circle-icon" ] (if model.can_create then - [ Button.fabutton + [ Button.fabutton "Add User" "plus-circle" - "" - Button.Enabled - (fun _ -> dispatch AddUser) + "" + Button.Enabled + (fun _ -> dispatch AddUser) ] else []) ] @@ -435,11 +439,11 @@ let root (model:Model) (dispatch:Msg->Unit) = | _, _ -> () ) *) - let adminDisplay = model.scratchUser.userData.exchangeUserData.cmeData.adminDisplayFlag + let adminDisplay = model.scratchUser.userData.exchangeUserData.cmeData.adminDisplayFlag let is_bts = model.this_username = Access.superuser let display_md = if is_bts then true else (adminDisplay = bts.api.admin.CMEUserData.AdminDisplay.All || adminDisplay = bts.api.admin.CMEUserData.AdminDisplay.MD) - let display_risk = if is_bts then true else (adminDisplay = bts.api.admin.CMEUserData.AdminDisplay.All || adminDisplay = bts.api.admin.CMEUserData.AdminDisplay.Routing) - + let display_risk = if is_bts then true else (adminDisplay = bts.api.admin.CMEUserData.AdminDisplay.All || adminDisplay = bts.api.admin.CMEUserData.AdminDisplay.Routing) + let modal = match (fst model.ConfirmModal) with | "" -> span [] [] | _ -> Modal.showConfirm (fst model.ConfirmModal) (fun () -> (snd model.ConfirmModal) |> dispatch) (fun _ -> OpenConfirmModal ("", Msg.SwitchTab model.currentTab ) |> dispatch) @@ -460,30 +464,30 @@ let root (model:Model) (dispatch:Msg->Unit) = div [ ClassName "tabs is-boxed" ] [ ul [ ] [ - (if display_risk then + (if display_risk then li [ ClassName (isActiveTab Tab.General model.currentTab) ] [ a [ OnClick (fun _ -> dispatch (SwitchTab Tab.General)) ] [ str "General" ] ] else span [] []) - (if display_risk then + (if display_risk then li [ ClassName (isActiveTab Tab.Accounts model.currentTab) ] [ a [ OnClick (fun _ -> dispatch (SwitchTab Tab.Accounts)) ] [ str "Account Permissions" ] ] else - span [] []) - (if display_md then + span [] []) + (if display_md then li [ ClassName (isActiveTab Tab.MarketData model.currentTab) ] [ a [ OnClick (fun _ -> dispatch (SwitchTab Tab.MarketData)) ] [ str "Market Data" ] ] else - span [] [] ) - (if display_risk then + span [] [] ) + (if display_risk then li [ ClassName (isActiveTab Tab.OrderRouting model.currentTab) ] - [ a [ OnClick (fun _ -> dispatch (SwitchTab Tab.OrderRouting)) ] + [ a [ OnClick (fun _ -> dispatch (SwitchTab Tab.OrderRouting)) ] [ str "Order Routing" ] ] else - span [] [] ) + span [] [] ) ] ] tabContent model dispatch display_md display_risk 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/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/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