diff --git a/CMakeLists.txt b/CMakeLists.txt index be98723f2..ab7b9d481 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -58,7 +58,6 @@ else () COMMAND ln ARGS -sf ${EXTERNALS_ROOT}/lib/Release/libOnixS.IceImpactMulticastPriceFeedHandlerCpp.so.3.13.0.0 ${CMAKE_BINARY_DIR}/server COMMAND ln ARGS -sf ${EXTERNALS_ROOT}/lib/Release/libACE-5.7.5.so ${CMAKE_BINARY_DIR}/server COMMAND mkdir ARGS -p ${CMAKE_BINARY_DIR}/server/plugins - COMMAND ln ARGS -sf ${CMAKE_BINARY_DIR}/sdk/examples/order_plugin_example.so ${CMAKE_BINARY_DIR}/server/plugins/ COMMENT "Creating server runtime layout" DEPENDS sparkd data_files_dir) diff --git a/doc/api/.gitignore b/doc/api/.gitignore new file mode 100644 index 000000000..ab5e793bc --- /dev/null +++ b/doc/api/.gitignore @@ -0,0 +1 @@ +public diff --git a/doc/api/config.toml b/doc/api/config.toml new file mode 100644 index 000000000..7c49216a4 --- /dev/null +++ b/doc/api/config.toml @@ -0,0 +1,15 @@ +# The URL the site will be built for +base_url = "https://api.bluetradesys.com" + +title = "BTS Spark API" +description = "Documentation for the BTS Spark API" + +compile_sass = true + +highlight_code = true +highlight_theme = "kronuz" +insert_anchor_links = true +build_search_index = true + +[extra] +author = "Blue Trading Systems" diff --git a/doc/api/content/_index.md b/doc/api/content/_index.md new file mode 100644 index 000000000..2af9c9fad --- /dev/null +++ b/doc/api/content/_index.md @@ -0,0 +1,5 @@ ++++ +title = "BTS Spark API" ++++ + +This is index \ No newline at end of file diff --git a/doc/api/content/client/_index.md b/doc/api/content/client/_index.md new file mode 100644 index 000000000..d00ac6373 --- /dev/null +++ b/doc/api/content/client/_index.md @@ -0,0 +1,7 @@ ++++ +title="Client API" +template="client.html" +sort_by="weight" ++++ + + diff --git a/doc/api/content/client/getting-started/_index.md b/doc/api/content/client/getting-started/_index.md new file mode 100644 index 000000000..afbb6f79f --- /dev/null +++ b/doc/api/content/client/getting-started/_index.md @@ -0,0 +1,7 @@ ++++ +title="Getting Started" +template="client.html" +sort_by="weight" +weight = 1 ++++ + diff --git a/doc/api/content/client/getting-started/install.md b/doc/api/content/client/getting-started/install.md new file mode 100644 index 000000000..2cd9902aa --- /dev/null +++ b/doc/api/content/client/getting-started/install.md @@ -0,0 +1,9 @@ ++++ +title = "Install Prerequisites" +template="client.html" +weight = 1 ++++ + +## Coming Soon + +* Install .net core diff --git a/doc/api/content/server/_index.md b/doc/api/content/server/_index.md new file mode 100644 index 000000000..2ea19d687 --- /dev/null +++ b/doc/api/content/server/_index.md @@ -0,0 +1,25 @@ ++++ +title="Algo API" +template="documentation.html" +redirect_to = "server/getting-started/overview/" +sort_by="weight" ++++ + +Getting Started +- Install (docker, editor) +- Downloading +- Write your first algo +- Testing + +Reference +- Subscriptions +- Child Orders +- Detaching +- Attaching +- ... + +Samples +- Iceberg +- Pro-rata + + diff --git a/doc/api/content/server/getting-started/_index.md b/doc/api/content/server/getting-started/_index.md new file mode 100644 index 000000000..d0e0e72e1 --- /dev/null +++ b/doc/api/content/server/getting-started/_index.md @@ -0,0 +1,7 @@ ++++ +title="Getting Started" +template="documentation.html" +sort_by="weight" +weight = 1 ++++ + diff --git a/doc/api/content/server/getting-started/algostudio.png b/doc/api/content/server/getting-started/algostudio.png new file mode 100644 index 000000000..df322e108 Binary files /dev/null and b/doc/api/content/server/getting-started/algostudio.png differ diff --git a/doc/api/content/server/getting-started/compiled.png b/doc/api/content/server/getting-started/compiled.png new file mode 100644 index 000000000..d81c5b1af Binary files /dev/null and b/doc/api/content/server/getting-started/compiled.png differ diff --git a/doc/api/content/server/getting-started/development.md b/doc/api/content/server/getting-started/development.md new file mode 100644 index 000000000..386ba96dc --- /dev/null +++ b/doc/api/content/server/getting-started/development.md @@ -0,0 +1,138 @@ ++++ +title = "Development" +weight = 3 ++++ + +# Visual Studio Code + +In this section we're going to use the Remote - Containers feature of Visual Studio Code to simplify the development experience. If you are using a different editor skip to the other editor section below. + +## Remote - Containers + +The Visual Studio Code Remote containers extension allows it to run a complete development environment inside the Spark SDK Docker container. The editor UI runs as a thin client on your desktop and connects to a headless instance running inside the Spark SDK Docker environment. This configuration allows us to ship a pre-configured set of [Tasks](https://code.visualstudio.com/Docs/editor/tasks) for building and testing your code. + +Start Visual Studio Code and click on the puzzle piece icon on the left-hand side to open the extensions panel. Use the search box at the top to find the "Remote - Containers" extension and install it. + + + +## Into the Code + +Open the examples/algo_template directory using the File->Open Folder menu option. If you opted to add it to your path during install, you could run `code .` from your command prompt. + +When the workspace opens, you should see a notification offering to let you re-open the workspace inside the container. + + + +The first time you open the workspace inside the container it will take a few seconds to configure the spark_sdk docker container. While the workspace is open in Visual Studio Code the docker image will also be running in the background. The container will stop automatically when you close the editor. + +Open algo_template.cpp from the file browser on the left or by pressing ctrl-p and typing the name. Each Algo is a standard C++ class which implements **IAlgoOrder**. Placing your cursor on IAlgoOrder and pressing F12 will take you to the IAlgoOrder.h header file where you can browse the API. To learn more about how an Algo is instantiated see the [Algo Initialization](/server/ref/initialization). + +## Compiling + +To build press "Ctrl+Shift+B". You should see the build output at the bottom of your screen. If the build was successful the resulting algo_template.so will be automatically copied to the plugins directory where it will be loaded by the Spark server process on start. + + + +## Debugging + +To debug your Algo set a breakpoint (F9) and open the Debug panel (Ctrl+Shift+D) and choose "(gdb) Launch". This will launch the Spark Server inside the container with the debugger attached. + + + +With sparkd running you can launch the Spark Client (spark_client.cmd) and it will connect to the running server. Log in with the user "bts" and no password and open a ladder. You can now select your Algo on the order type dropdown and activate it by clicking on a price on the ladder. + + + +While sparkd is running you can also access the Spark Server Admin interface on [http://localhost:9000](http://localhost:9000). Use the same "bts" login to access the admin interface. Here you can manage user and account permissions and risk limits. + +## Starting Spark Server + +To start the Spark Server without attaching a debugger press "Ctrl+Shift+P" and select "Tasks: Run Task" and then "Start Sparkd" and "Never scan task output" This will open a new terminal where you can see any messages logged with the **Sdk** subsystem as well as any warnings or errors. To stop the Spark server simply kill the terminal (trash can icon) or run the "Stop Sparkd" task. + +## Next Steps + +If you would like to understand more about how Docker is working behind the scenes continue to the [next section](#other-editors). Otherwise, continue to [Algo Registration](/server/ref/registration) to learn how Algos are registered with the Spark. + +
+ +# Other Editors + +In this section you will be more exposed to the details of how Docker works and how to compile and test your Algo using the command line. + +## Docker + +Spark Server is built and runs only on Linux so you need a Linux development environment to effectively develop plug-ins for it. Docker lets you take the spark_sdk.img disk image and run it inside a virtual Linux machine running on your Windows desktop. + +First, some terminology. A Docker "image" is a disk image containing a complete filesystem. The spark_sdk.img file you imported earlier is an image. A Docker "container" is what you get when you get when you attach this image to a running Linux system. You can connect to a running container and use it to run commands like the C++ compiler or the Spark server. Directories from your local filesystem can be mapped into the Docker container when it starts so you can easily share files. + +### Start + +The first step is to start the spark_sdk Docker container with the examples directory mapped so we can compile the examples inside the container. In your command prompt cd to the *examples* directory that you unzipped from the spark_sdk.zip file. Then run the following command to start the container. + +Windows Command Prompt: +{% clipable() %} +docker run --rm -it -v %CD%:/examples -u bts -w /examples --shm-size 1024m --name spark_sdk -h spark_sdk --cap-add=SYS_PTRACE --security-opt seccomp=unconfined -p 9000:9000 -p 9001:9001 spark_sdk bash +{% end %} + +**Powershell** / Linux / Mac: +{% clipable() %} +docker run --rm -it -v ${PWD}:/examples -u bts -w /examples --shm-size 1024m --name spark_sdk -h spark_sdk --cap-add=SYS_PTRACE --security-opt seccomp=unconfined -p 9000:9000 -p 9001:9001 spark_sdk bash +{% end %} + +### Attach + +The container will remain as long as this prompt is open. You can attach an additional to the running container with the following command: +{% clipable() %} +docker exec -it spark_sdk bash +{% end %} + +## Compiling + +Let's look at the algo_template sample first. All of the samples are laid out similarly with a `build.sh` script for compiling. As long as you are running build.sh inside the spark Docker container, it automatically copies the build output to /sparkbin/plugins/ where it needs to be to get loaded by the Spark server. + +``` +bts@spark_sdk:/examples$ cd algo_template/ +bts@spark_sdk:/examples/algo_template$ ./build.sh +algo_template.so copied to /sparkbin/plugins/ +Restart sparkd to see changes. +``` + +## Starting Spark Server + +To start the Spark server `cd /sparkbin/` and run `./sparkd run`. Press Ctrl+C to stop it. +``` +bts@spark_sdk:/sparkbin$ ./sparkd run +2019-10-25 16:21:09.383626|262552152803600|Gen|Warn |1 |main|132 |Run.cpp:53 |dev| +------------------------------------ Sparkd launched -------------------------------- +``` + +You will see any Sdk log messages as well as any log messages at or above Warning level from other parts of the system. If you need additional diagnostic information more details logs are stored in /sparkdata/data/logging/log. These logs will be automatically deleted when you stop the Docker container. + +With sparkd running you can launch the Spark Client (spark_client.cmd) and it will connect to the running server. Log in with the user "bts" and no password and open a ladder. You can now select your Algo on the order type dropdown and activate it by clicking on a price on the ladder. + + + +While sparkd is running you can also access the Spark Server Admin interface on [http://localhost:9000](http://localhost:9000). Use the same "bts" login to access the admin interface. Here you can manage user and account permissions and risk limits. + +## Debugging + +You can debug your algo using gdb from the bash command prompt. + +```bts@spark_sdk:/sparkbin$ gdb --args ./sparkd run +GNU gdb (Ubuntu 8.1-0ubuntu3) 8.1.0.20180409-git +Copyright (C) 2018 Free Software Foundation, Inc. +... +Reading symbols from ./sparkd...done. +(gdb) break algo_template.cpp:45 +No source file named algo_template.cpp. +Make breakpoint pending on future shared library load? (y or [n]) y +Breakpoint 1 (algo_template.cpp:45) pending. +(gdb) run +Starting program: /sparkbin/sparkd run +``` + +If your editor supports debugging over the ssh protocol you can start sshd from inside the container. + +## Next + +Continue to [Algo Registration](/server/ref/registration) to learn how Algos are registered with the Spark. diff --git a/doc/api/content/server/getting-started/docker_config.png b/doc/api/content/server/getting-started/docker_config.png new file mode 100644 index 000000000..d8fb57ac1 Binary files /dev/null and b/doc/api/content/server/getting-started/docker_config.png differ diff --git a/doc/api/content/server/getting-started/docker_tray.png b/doc/api/content/server/getting-started/docker_tray.png new file mode 100644 index 000000000..f180c7a94 Binary files /dev/null and b/doc/api/content/server/getting-started/docker_tray.png differ diff --git a/doc/api/content/server/getting-started/download.md b/doc/api/content/server/getting-started/download.md new file mode 100644 index 000000000..be3c054a2 --- /dev/null +++ b/doc/api/content/server/getting-started/download.md @@ -0,0 +1,79 @@ ++++ +title = "Download SDK" +weight = 2 ++++ + +## Request Access 🔑 + +To start using the Server SDK you will need to request access though our sales department. Please email info@bluetradesys.com or call (919) 913-0850 and we will provide download credentials. + +## Download + +Download the latest SDK image here: [spark_sdk.zip](https://api.bluetradesys.com/download/spark_sdk.zip) + +Unzip that folder into a working directory. Inside you should see something like this: + +``` +spark_sdk.img +client/ +spark_client.cmd +examples/ +``` + +## Import + +Import the spark_sdk image into docker using the `docker load` command. +{% clipable() %} +docker load < spark_sdk.img +{% end %} + +
+ +If you see this error it means Docker Desktop is not running: + + +error during connect: Get http://%2F%2F.%2Fpipe%2Fdocker_engine/v1.40/images/json: open //./pipe/docker_engine: The system cannot find the file specified. In the default daemon configuration on Windows, the docker client must be run elevated to connect. This error may also indicate that the docker daemon is not running. +
+ +Confirm that the import worked by running `docker images` which should list the new image. +``` +c:\bts> docker images +REPOSITORY TAG IMAGE ID CREATED SIZE +bluetradingsystems/spark_sdk latest e2be89436e73 4 hours ago 3.89GB +``` + + + +## Update + +To update your Spark SDK simply download an unzip a new [spark_sdk.zip](https://api.bluetradesys.com/download/spark_sdk.zip) and use `docker load < spark_sdk.img` to overwrite the old image with the new one. + +To preserve the old image so you can go back to it later run `docker tag spark_sdk spark_sdk:your_custom_tag` before loading the new SDK image. + + + diff --git a/doc/api/content/server/getting-started/gdb_launch.png b/doc/api/content/server/getting-started/gdb_launch.png new file mode 100644 index 000000000..878d96820 Binary files /dev/null and b/doc/api/content/server/getting-started/gdb_launch.png differ diff --git a/doc/api/content/server/getting-started/iced_algo.png b/doc/api/content/server/getting-started/iced_algo.png new file mode 100644 index 000000000..bf1ca5b50 Binary files /dev/null and b/doc/api/content/server/getting-started/iced_algo.png differ diff --git a/doc/api/content/server/getting-started/install.md b/doc/api/content/server/getting-started/install.md new file mode 100644 index 000000000..01075e6af --- /dev/null +++ b/doc/api/content/server/getting-started/install.md @@ -0,0 +1,57 @@ ++++ +title = "Install Prerequisites" +weight = 1 ++++ + +These instructions assume you are using Microsoft Windows but you should be able to follow them on a Linux or Mac with minor modifications. + +{% linux() %} +When there is a significant difference on non-Windows platforms it will be called out in a box like this one. +{% end %} +{% info() %} +Tips or solutions to common problems will be highlighted in a box like this. +{% end %} + +The minimum hardware requirements for using the SDK are: +- **12 GB** RAM (preferably 16 GB or more) +- 4 CPU cores +- 20 GB free hard disk space + +The Spark SDK uses a tool called Docker to distribute all of the tools you need to build and test your trading algorithm ("Algo"). Once you have Docker installed you can download and import the Spark SDK image and be ready to start developing your Algo in a few minutes. + +## Docker + +Download and install Docker Desktop for Windows/Mac. + +
+ +If you are using Linux follow the official install instructions for Docker for your distribution. +
+ +During the install Docker may ask you to enable virtualization support and reboot. + +Once Docker Desktop is installed and running it will create an icon in your system tray area that looks like the Docker logo. +![](../docker_tray.png) + +Right click on the tray icon and choose 'Settings' and choose the 'Advanced' tab. + +Ensure that the docker image has 8192 MB of memory and at least 2 cores. Memory that you assign to the docker image will not be available to Windows/Mac while Docker is running. CPU cores are shared with the host OS. Since you are going to use this image to compile your Algo it makes sense to allow Docker to use all available cores. + + + +## Sharing + +You need to give Docker access to the Windows drive where your source code is kept. + +Open Settings -> Shared Drives and check the drive where you intend to keep your Algo source code. + +
+ +If you run into trouble with drive sharing, see Docker Desktop for Windows tips on avoiding common problems. +
+ +## Editor + +The Server SDK includes a complete configuration for C++ development with the Visual Studio Code editor. If you are a new to C++ development on a Linux system we recommend you give this a try as it's the easiest way to get up and running quickly. Of course, If you have a different editor that you prefer we fully support that as well. + + diff --git a/doc/api/content/server/getting-started/myalgo.png b/doc/api/content/server/getting-started/myalgo.png new file mode 100644 index 000000000..331f204df Binary files /dev/null and b/doc/api/content/server/getting-started/myalgo.png differ diff --git a/doc/api/content/server/getting-started/orderbook_truncated.png b/doc/api/content/server/getting-started/orderbook_truncated.png new file mode 100644 index 000000000..0b99b937e Binary files /dev/null and b/doc/api/content/server/getting-started/orderbook_truncated.png differ diff --git a/doc/api/content/server/getting-started/overview.md b/doc/api/content/server/getting-started/overview.md new file mode 100644 index 000000000..1d7fd9a4a --- /dev/null +++ b/doc/api/content/server/getting-started/overview.md @@ -0,0 +1,44 @@ ++++ +title = "Overview" +weight = 0 ++++ + +Using the **Spark Server SDK**, users can create new order types ("Algos"). In this case, users implement the new functionality in C++, producing a shared library that is loaded at run time by our Spark Server. + +If you are interested in accessing Spark server remotely, please see the [Spark Client SDK](/client/). + +## Speed + +The Spark platform offers a sub-10 microsecond socket to socket response times for algorithmic trading. With the **Spark Server SDK**, you can create your order types in high-performance C++ and load them into the Spark server. + +Since your order is running inside the Spark Server, there is virtually no overhead allowing your algorithm to take full advantage of the speed Spark offers. Hosting a high-performance language like C++ directly in the Spark server process allows it to respond to the market thousands of times faster than an Algo built using a proprietary drag and drop tool. + + +## Easy to Get Started + +If you are not actively developing with C++, it can be time-consuming to set up a complete development environment from scratch. That is why we provide a full development environment out of the box with [Docker](https://docker.io) so you can download and try the Server SDK quickly and easily. + +Docker works across Windows, macOS, and Linux, so you don't need to change your development environment to try the Spark API. Docker also ensures that all of the dependencies you need are packaged together in one image, so you know it will work out of the box, no matter the platform. + +## Implementation + +Creating a new Algo begins with implementing a standard C++ class derived from IAlgoOrder. Start from the [algo_template](/server/samples/template) example to get going quickly. + +The first call your new Algo receives is [do_before_launch](/server/ref/initialization#before-launch), which is an opportunity to validate parameters and allocate any long-lived resources needed during execution. Next, it receives either [do_launch](/server/ref/initialization#launch-launch-attached) in the case of a new order or [do_launch_attached](/server/ref/initialization#launch-launch-attached) if the order is attaching to an existing order. + +The Algo receives a reference to [IAlgoServices](/server/ref/subscriptions/), which is how it calls out to the Spark system. It will use [IAlgoServices](/server/ref/subscriptions/) to subscribe to market events, launch and control child orders, and communicate its state to the larger Spark system. + +## Fully Integrated + +When a Spark Server plugin is loaded, it registers its exposed Algos with the Spark Server though the [init_spark_plugin](/server/ref/registration) callback. This callback allows the Algo to describe the parameters it expects so they can be automatically entered in the [Spark Client](https://www.bluetradingsystems.com/bts-spark/). + + + + +Compose your Algo with out of the box Algos in the [Spark Algo Studio](https://www.bluetradingsystems.com/news/155-bts-algo-studio-provides-algo-library-with-customization-functionality) to create new functionality on the fly. + +
+ + + + diff --git a/doc/api/content/server/getting-started/paramsui.png b/doc/api/content/server/getting-started/paramsui.png new file mode 100644 index 000000000..d81b39c35 Binary files /dev/null and b/doc/api/content/server/getting-started/paramsui.png differ diff --git a/doc/api/content/server/getting-started/vscode_notification.png b/doc/api/content/server/getting-started/vscode_notification.png new file mode 100644 index 000000000..38674d111 Binary files /dev/null and b/doc/api/content/server/getting-started/vscode_notification.png differ diff --git a/doc/api/content/server/getting-started/vscode_remote_containers.png b/doc/api/content/server/getting-started/vscode_remote_containers.png new file mode 100644 index 000000000..185f16223 Binary files /dev/null and b/doc/api/content/server/getting-started/vscode_remote_containers.png differ diff --git a/doc/api/content/server/ref/_index.md b/doc/api/content/server/ref/_index.md new file mode 100644 index 000000000..ac7238434 --- /dev/null +++ b/doc/api/content/server/ref/_index.md @@ -0,0 +1,7 @@ ++++ +title="Reference" +template="documentation.html" +sort_by="weight" +weight = 2 ++++ + diff --git a/doc/api/content/server/ref/faq.md b/doc/api/content/server/ref/faq.md new file mode 100644 index 000000000..7c738c0a0 --- /dev/null +++ b/doc/api/content/server/ref/faq.md @@ -0,0 +1,8 @@ ++++ +title = "Questions" +weight = 40 ++++ + +## I need to make a call that might take a few seconds. What is the best way to do this? + +Create a background thread in [do_before_launch](/server/ref/initialization#before-launch) or [do_launch](/server/ref/initialization#launch-launch-attached) to do the work. Use [on_timer](/server/ref/subscriptions#timer) to poll until the result is available. diff --git a/doc/api/content/server/ref/initialization.md b/doc/api/content/server/ref/initialization.md new file mode 100644 index 000000000..6bc243567 --- /dev/null +++ b/doc/api/content/server/ref/initialization.md @@ -0,0 +1,97 @@ ++++ +title = "Initialization" +weight = 10 ++++ + +Each Algo derives from IAlgoOrder and overrides virtual methods to receive callbacks from the Spark system. + + +## Before Launch + +When an order is activated by a user or by another Algo, the first call it receives is do_before_launch. This call happens outside of the latency-sensitive path, so it's the place to validate parameters and set up data structures that the Algo might need. + +This call returns an OrderOpResult, which allows it to fail and return an error message to its creator. If a user created this order, this message is displayed in the [Activity Log](https://www.bluetradingsystems.com/docs/bts_spark/Help/#activity-log) and across the bottom of the ladder. + +Every SDK Algo is a "Dynamic" Algo since it's parameters are defined during [registration](/server/ref/registration/). Use the `get_if` helper to retrieve a named parameter from the `order_params` collection as a `std::option`. If a parameter has a valid default value you can use `std::option::value_or(...)` to set default values for missing parameters. If a parameter *must* exist or must be within a certain valid range, return an error to the caller. + +```cpp +OrderOpResult MyAlgo::do_before_launch(IBeforeLaunchAlgoServices& serv) { + auto& def = serv.definition(); + auto& params = def.custom_def().dynamic().order_params(); + auto mqi = get_if(params, "Min Quantity"); + if (!mqi || mqi.value() == 0) + return OrderOpResult{ + ORDER_ERROR_INVALID_ORDER_DEFINITON, + "Min Quantity parameter must be defined and non zero"}; +} +``` + +
+ +The parameters are defined during registration by the DynamicOrderDescriptor. +
+ +### IBeforeLaunchAlgoServices + +IBeforeLaunchAlgoServices is passed to `do_before_launch` and allows the Algo to query its environment. + +```cpp +class IBeforeLaunchAlgoServices { + public: + // The OrderDefinition for this algo + virtual api::orderman::OrderDefinition const &definition() const = 0; + + // The security this algo is paramterized with (also available from + // definition()) + virtual api::Security const &security() const = 0; + + // The unique order key for this algo + virtual OrderKey order_key() const = 0; + + // The parent key for this algo's parent or null_order_key() if the + // algo does not have a parent + virtual OrderKey parent_order_key() const = 0; + + // The security identified by k or nullptr if no such security exists + virtual api::Security const *lookup_security(SecurityKey k) const = 0; + + // Wheter this algo is being attached to an existing order. + // If an algo does not support attaching it can return an error + // if is_attaching() == true from do_before_launch. + virtual bool is_attaching() const = 0; + + // Build a child template that can later can be used to quickly + // launch child orders. The reserve parameter indicates how many + // orders should be created ahead of time, before they are + // launched. This is usefull to remove the relatively expensive + // order creation operation to a background thread. + virtual ChildTemplate & + make_child_template(api::orderman::OrderDefinition const &child_def, + size_t reserve = 1) = 0; + + // Get the current time + virtual TimeStamp get_time() const; +}; +``` + + +## Launch / Launch Attached + +Depending on the state of `is_attaching()` the next call will either be `do_launch` or `do_launch_attached`. The only difference is that in `do_launch_attached` the existing OrderKey and ChildState are provided. + +`do_launch` and `do_launch_attached` are in the latency-sensitive path and should not do more work than needed. These functions get a reference to `IAlgoServices`, which is a superset of the functionality available on `IBeforeLaunchAlgoServices`. Most Algos should store a pointer or reference to this object for managing child orders (it has a stable address). + +Now is the time to use `IAlgoServices` to subscribe to market events that are relevant to your Algo and create any child orders that need to exist at the start of your Algo. You can safely call IAlgoServices from any `IAlgoOrder` callback. + +To learn more about subscribing to events see [subscriptions](/server/ref/subscriptions). + +### Things to do in the launch callback +☑ Store a reference to IAlgoServices +☑ Subscribe to relevant market events +☑ Launch a child order? + + +## Update + +If the user or parent Algo changes the order parameters (price, size, or the custom parameters defined during registration), Spark calls the `do_update` callback with the new parameters. It is up to the Algo to decide how to apply the new parameters to update the running state. It can do this by canceling, changing, or adding new child orders. + diff --git a/doc/api/content/server/ref/orders.md b/doc/api/content/server/ref/orders.md new file mode 100644 index 000000000..5a5a342a5 --- /dev/null +++ b/doc/api/content/server/ref/orders.md @@ -0,0 +1,88 @@ ++++ +title = "Child Orders" +weight = 30 ++++ + +Every Algo must, at some point, launch a child order to be useful. Each child order can be another Algo or regular exchange order. When the child order is updated or filled, those changes are also sent to the parent. + + +# Templates + +Since launching a child order is one of the most latency-sensitive things an Algo can do, the Server SDK enables you to create a child template. An order template prepares an order based on a known OrderDefinition and gets it ready to send on a background thread. When the Algo determines it is time to send the order, it only needs to provide the price and quantity to complete and send the order. If an Algo expects to send multiple child orders, it can reserve more than one. Preparing a child template is typically done in [do_before_launch](http://127.0.0.1:1111/server/ref/initialization/#before-launch) callback. + +```cpp +virtual ChildTemplate& + IBeforeLaunchAlgoServices::make_child_template( + api::orderman::OrderDefinition const &child_def, + size_t reserve = 1); +``` + +# Launch + +IAlgoServices provides two overloads for launching child orders. By default, launching a child order automatically subscribes the Algo to its state changes. + +Launch a child order from a template if you have one prepared. This is the fastest option. +```cpp +OrderKey IAlgoServices::launch_child(ChildTemplate&, Price, Quantity, bool subscribe = true); +``` +Launch a child order by constructing an OrderDefinition as needed. +```cpp +OrderKey launch_child(OrderDefinition const &, Price, Quantity, bool subscribe = true); +``` + +When a child is launched its `OrderKey` is returned to the caller. This ID can be used to update, cancel, or detach the child order. + +## Update + +To update an existing child order, call update_child and pass an Update. The Update type lets you change the price, size or the entire OrderDefinition. You only need to specify the fields that you intend to update so only set the price() field if that is what you are changing. + +```cpp +void IAlgoServices::update_child(OrderKey child, api::orderman::Update const &); +``` + +## Cancel + +To cancel an existing child order call `cancel_child`. When the order is canceled you will receive an update though the `on_child_change` callback. + +```cpp +void IAlgoServices::cancel_child(OrderKey child); +``` + +## Detach + +The lifetime of a child order is tied to its parent. If a parent is canceled or completed child orders are automatically stopped as well. If the Algo needs to launch an order that will continue after the Algo ends use `detach_child` to sever the connection. This order hierarchy can be viewed in the Spark [Order Book](https://www.bluetradingsystems.com/docs/bts_spark/Trading/#order-book). + +```cpp +void IAlgoServices::detach_child(OrderKey child); +``` +To detach all children at once use `detach_all_children`. + +```cpp +void IAlgoServices::detach_all_children(); +``` + +# Child Updated + +When a child order is updated the parent receives the `on_child_change` callback with an updated ChildState object. + +```cpp +void on_child_change(OrderKey, ChildState const &) override; + +struct ChildState { + Price price; + Quantity quantity; + Quantity total_fills; + Price avg_price; + Quantity last_fills; + Price last_price; + ChildStatus status; + Quantity fills_at_attach_time; +}; +``` + + + diff --git a/doc/api/content/server/ref/paramsui.png b/doc/api/content/server/ref/paramsui.png new file mode 100644 index 000000000..d81b39c35 Binary files /dev/null and b/doc/api/content/server/ref/paramsui.png differ diff --git a/doc/api/content/server/ref/registration.md b/doc/api/content/server/ref/registration.md new file mode 100644 index 000000000..c909a42ac --- /dev/null +++ b/doc/api/content/server/ref/registration.md @@ -0,0 +1,92 @@ ++++ +title = "Registration" +weight = 1 ++++ + +When Spark Server starts, it loads all plugins listed in the /sparkbin/plugins directory and calls an exported function named `init_spark_plugin` which must have the following signature. + +```cpp +extern "C" void init_spark_plugin(bts::SparkServices &serv) { } +``` + +The `init_spark_plugin` function registers the Algo with a DynamicOrderDescriptor. Once registered, the Algo appears in the [Spark ladder](https://www.bluetradingsystems.com/docs/bts_spark/Trading/#ladder) and the [Algo Studio](https://www.bluetradingsystems.com/docs/bts_spark/Trading/#algo-studio). + +To register an Algo call `register_dynamic_algo_order` on the provided bts::SparkServices instance. This function takes the DynamicOrderDescriptor and a function that is called to construct the IAlgoOrder. A single init_spark_plugin can call register_dynamic_algo_order multiple times to register multiple Algos. + +The DynamicOrderDescriptor must have a dynamic_order_type (name), or the registration will fail. + +```cpp + DynamicOrderDescriptor desc; + desc.set_dynamic_order_type("My Algo"); + auto result = + serv.register_dynamic_algo_order( + desc, []() { return std::make_unique(); }); + if (result != bts::AlgoRegistrationResult::SUCCESS) { + // Algo failed to register + } +``` + + +## DynamicOrderDescriptor + +The DynamicOrderDescriptor describes the type of parameters that the Algo accepts. These inputs can come either from the user manually entering them or from a parent Algo. Spark Algos are composable, so one Algo can activate another. See [Child Orders](/server/ref/orders) for more on how to activate custom order types as child orders. + +Add parameters to the DynamicOrderDescriptor by calling `add_param_desc()` and setting the name and parameter type. + +```cpp + *desc.add_param_desc() = param_bool("Enable Feature"); + *desc.add_param_desc() = param_uint32("Feature Quantity"); +``` + +The above parameters look like this when rendered in the Spark ladder. + + + +And like this when viewed in the Spark order book. + + + +Each descriptor can be one of the following types: +``` +// Primitive +int32 +uint32 +double +string +bool +// Complex +Order +Array +Struct +``` + +**Currently the Spark UI only renders parameters with primitive types.** The complex types can only be provided by a parent order. + +### Order + +This parameter type indicates that another order type parameterizes this Algo. When the Algo needs to create a child order, it can use this parameter type to create a custom order type instead of defaulting to a limit order. + +For example, an Algo might be designed to wait for a specific market condition before putting in an order. Instead of placing a limit order when the condition is met, the user can specify that it should an IceBerg order. The Algo doesn't need special logic to handle different child order types. + +### Array + +An array parameter is a collection of the same type of parameter. If your Algo needed to behave differently depending on a table of prices, you could send that list in though an array parameter. +```cpp + *desc.add_param_desc() = + param_array("Price Points", + param_double("unused").param_type()); +``` + +### Struct + +A struct parameter is a set of named parameter types similar to the entire DynamicOrderDescriptor. The struct parameter type can be combined with the array type to handle tabular data. + +```cpp + *desc.add_param_desc() = + param_array("Step Table", + param_struct("", { + param_uint32("Market Quantity"), + param_uint32("Order Quantity") + }).param_type()); +``` + diff --git a/doc/api/content/server/ref/subscriptions.md b/doc/api/content/server/ref/subscriptions.md new file mode 100644 index 000000000..42432634f --- /dev/null +++ b/doc/api/content/server/ref/subscriptions.md @@ -0,0 +1,69 @@ ++++ +title = "Subscriptions" +weight = 20 ++++ + +An Algo exists to observe changes in the market and respond to them. When the Algo is [launched](1/server/ref/initialization/#launch-launch-attached) it uses IAlgoServices to subscribe to the events it needs to operate. Each subscribe call is paired with a matching unsubscribe call, which takes the same argument to unsubscribe. + +# Book + +Subscribe to book updates for the specified security call use subscribe_book and unsubscribe book. +```cpp +// Subscribe to book updates for the specified security +void IAlgoServices::subscribe_book(SecurityKey); +void IAlgoServices::unsubscribe_book(SecurityKey); + +// You implement +virtual void on_book(SecurityKey, Book const &) override; +``` +Get the `SecurityKey` of the current security using: +```cpp +services.subscribe_book(SecurityKey(algo_services().security().id())); +``` +If your Algo needs to subscribe to other securities, their keys must be passed in as parameters. + + +To get the details of a security use +```cpp +api::Security const* IBeforeLaunchAlgoServices::lookup_security(SecurityKey k) const = 0; +``` + +# Timer +To handle time-based logic, an Algo can subscribe to a common timer event that is invoked once every 10 milliseconds. Use the TimeStamp class from `bts_chrono.h` to efficiently handle time calculations. +```cpp +// Subscribe to a periodic 10ms timer +void IAlgoServices::subscribe_timer(); +// You implement +virtual void on_timer() override; +``` + +# Ticker +The status callback is called when the market for this security opens, closes, or has some other state transition. +```cpp +// Subscribe to trade ticker +void IAlgoServices::subscribe_ticker(SecurityKey); +// You implement +void on_ticker(SecurityKey, TradeVector const&) override; +``` + +# Trading Status + +The status callback is called when the market for this security opens, closes, or has some other state transition. +```cpp +// Subscribe to security trading status +void IAlgoServices::subscribe_status(SecurityKey); +// You implement +virtual void on_status(SecurityKey, co2::TradingStatus) override; +``` + +# Child Orders + +When an Algo launches a child order, it is subscribed to the changes on that child automatically. If the Algo unsubscribes from those changes, it can re-subscribe using this method. + +```cpp +// Subscribe to child order updates for the specified child order. +void IAlgoServices::subscribe_child_change(OrderKey); +// You implement +virtual void on_child_change(OrderKey, ChildState const &) override; +``` + diff --git a/doc/api/content/server/samples/_index.md b/doc/api/content/server/samples/_index.md new file mode 100644 index 000000000..b02ad8516 --- /dev/null +++ b/doc/api/content/server/samples/_index.md @@ -0,0 +1,7 @@ ++++ +title="Samples" +template="documentation.html" +sort_by="weight" +weight = 3 ++++ + diff --git a/doc/api/content/server/samples/join.md b/doc/api/content/server/samples/join.md new file mode 100644 index 000000000..0a1b7dbd3 --- /dev/null +++ b/doc/api/content/server/samples/join.md @@ -0,0 +1,354 @@ ++++ +title = "Join" +weight = 10 ++++ + + +Join starts working N ticks away from the market (usually) and then watches for the opposite inside to become same side inside market; if there is then enough size on that market, it will join. + +
+ +This sample is part of the Server SDK examples included in the spark_sdk.zip download. +
+ +```cpp +#include "spark/lib/config/config.h" +#include "spark/lib/config/protos/system.pb.h" +#include "spark/lib/domain_model/Security.h" +#include "spark/lib/orderman/orders/server/IAlgoOrder.h" +#include "spark/lib/orderman/orders/server/IAlgoServices.h" +#include "spark/lib/plugin/SparkServices.h" + +#include "spark/lib/orderman/order_factory/IOrderFactory.h" +#include "spark/lib/orderman/orderman_utils/Book.h" +#include "spark/lib/orderman/orderman_utils/orderman_utils.h" +#include "spark/lib/utils/PBUtils.h" + +using namespace bts::api::orderman; +using namespace bts::orderman; + +namespace bts { +namespace order_example { + +// Join starts working N ticks away from the market (usually) and then +// watches for the opposite inside to become same side inside market; +// if there is then enough size on that market, it will join. +class DynamicJoin : public IAlgoOrder { + public: + DynamicJoin(); + ~DynamicJoin(); + + OrderOpResult do_before_launch(IBeforeLaunchAlgoServices &) override; + void do_launch(IAlgoServices &) override; + void do_launch_attached(IAlgoServices &, OrderKey, ChildState) override; + void do_update(api::orderman::Update const &) override; + void on_book(SecurityKey, Book const &) override; + void on_child_change(OrderKey, ChildState const &) override; + + private: + enum class Status; + struct PImpl; + std::unique_ptr me; + + IAlgoServices &algo_services() const; + void try_to_join(); +}; + +// The three stages our algo goes through. We start by waiting to +// observe the book for our security (WAITING_FOR_MD). Once we have a +// book we use it to derive our target price and we continue to +// monitor the book to see if we should join the market +// (WAITING_TO_JOIN). Once the size and price constraints are met we +// jump to the target price (JOINED). We remain in the JOINED state +// until our order is filled or canceled. +enum class DynamicJoin::Status { WAITING_FOR_MD, WAITING_TO_JOIN, JOINED }; + +// All state information needed for this algo +struct DynamicJoin::PImpl { + IAlgoServices *algo_services; + Quantity min_quantity_to_join; + Status status = Status::WAITING_FOR_MD; + OrderKey child_key = null_order_key(); + ChildTemplate *child_template = nullptr; + Book book_at_inception; + Book last_book; +}; + +DynamicJoin::DynamicJoin() : me(new PImpl()) {} +DynamicJoin::~DynamicJoin() {} +IAlgoServices &DynamicJoin::algo_services() const { return *me->algo_services; } + +// This is called outside of the latency sensitive path +// once. Expensive initialization should be done here, rather than +// in do_launch() +OrderOpResult DynamicJoin::do_before_launch(IBeforeLaunchAlgoServices &serv) { + auto &def = serv.definition(); + bts_assert(def.has_custom_def()); + bts_assert(def.custom_def().has_dynamic()); + + // Validate our parameters, returning an error if we find a + // problem. If an error is returned here the order will be + // abandoned. + auto ¶ms = def.custom_def().dynamic().order_params(); + auto mqi = get_if(params, "Min Quantity"); + if (!mqi || mqi.value() == 0) { + return OrderOpResult{ + ORDER_ERROR_INVALID_ORDER_DEFINITON, + "Min Quantity parameter must be defined and non zero"}; + } + + me->min_quantity_to_join = Quantity(mqi.value()); + + if (!serv.is_attaching()) { + // If we are not attaching, create a child template so we can + // quickly launch our child when the time comes. + + // We will be sending a limit order with the same side, + // security, trader, account, etc, as this algo order, so just + // copy our definition to the child definition to start with. + auto child_def = def; + // Now add a limit order component to mark our child as a limit + // order. Limit orders do not require any extra parameters + // beyond those already inherited from this algo order + // (i.e. beyond what we obtained through serv.definition()) so + // we can just use a default constructed LimitOrderDefinition. + *child_def.mutable_custom_def()->mutable_limit() = LimitOrderDefinition{}; + // Now just build the child template and cache it for future use + me->child_template = &(serv.make_child_template(child_def)); + } else { + // If we are attaching to an existing order then we do not need + // to spawn a new child, so there is no need to setup a child + // template + } + + // All is good, return a default constructed OrderOpResult which + // indicates success. + return {}; +} + +// Called in the latency sensitive path, once, when the algo is first +// launched. +void DynamicJoin::do_launch(IAlgoServices &services) { + // Cache our algo services + me->algo_services = &services; + + LOG(OrderMan, Diag, "Lauching DynamicJoin k: {} ", + algo_services().order_key().value()); + + // Launch our child order + me->child_key = + services.launch_child(*me->child_template, algo_services().price(), + algo_services().quantity()); + + // Subscribe to market data + services.subscribe_book(SecurityKey(algo_services().security().id())); +} + +// Called in the latency sensitive path, once, when the algo is first +// launched. In this case the algo is provided with an existing order +// it is now responsible for managing. +void DynamicJoin::do_launch_attached(IAlgoServices &services, + OrderKey attached_order, ChildState st) { + // The order was launched and attached to an existing order, with + // key 'attached_order' and current state 'st' + + // Cache our algo services + me->algo_services = &services; + + LOG(OrderMan, Diag, + "Lauching DynamicJoin k: {} Attached to existing order: {}", + algo_services().order_key().value(), attached_order.value()); + + // Cache the key of the attached order we will be managing + me->child_key = attached_order; + + // Subscribe to market data + services.subscribe_book(SecurityKey(services.security().id())); + + // Simulate a child change so we can update our own state to + // reflect that of the child order we attached to. From this point + // forward there is no distinction between an algo instance that + // spawned a new child order and one that attached to an existing + // order. + on_child_change(attached_order, st); +} + +// Called when a human (or a supervising algo) updates a parameter for +// this algo. The implementation validates the update and depending on +// the validation outcome either applies the update or terminates the +// algo. +void DynamicJoin::do_update(api::orderman::Update const &update) { + LOG(OrderMan, Diag, "DynamicJoin k: {} receiving update: {}", + algo_services().order_key().value(), utils::pb::to_string(update)); + + // Validate update + if (update.quantity() == 0) { + // An invalid update has been detected so we terminate the + // algo, signaling an error. Any child orders launched by + // this algo will also be terminated. + algo_services().terminate_error("Invalid update"); + return; + } + + // Updates have two parts, a standard part consisting of price and + // size and present in all updates, and an optional part that is + // specific to the algo in question. Below we check for the + // existance of this optional part and validate if present. + bool has_new_min_qty = false; + if (update.has_custom()) { + auto ¶ms = update.custom().dynamic().order_params(); + auto mqi = params.find("Min Quantity"); + + if (mqi == params.end() || mqi->second.as_uint32() == 0) { + algo_services().terminate_error( + "Minimum quantity to join must be greater than 0"); + return; + } + has_new_min_qty = true; + me->min_quantity_to_join = Quantity(mqi->second.as_uint32()); + } + + // Update is good, forward it to our child + Update child_update; + child_update.set_price(update.price()); + child_update.set_quantity(update.quantity()); + algo_services().update_child(me->child_key, child_update); + + // Try to join again if conditions changed + if (me->status == Status::WAITING_TO_JOIN && has_new_min_qty) + try_to_join(); +} + +// Called when the book for the specified security has changed +void DynamicJoin::on_book(SecurityKey, Book const &b) { + + // Cache the book, so we can reffer to it in the event of a + // parameter change that might allow us to join + me->last_book = b; + + LOG(OrderMan, Diag, "DynamicJoin order {} on book. BID: {}@{} ASK: {}@{}", + algo_services().order_key().value(), b.side(api::SIDE_BUY)[0].size(), + b.side(api::SIDE_BUY)[0].price(), b.side(api::SIDE_SELL)[0].size(), + b.side(api::SIDE_SELL)[0].price()); + + switch (me->status) { + case Status::WAITING_FOR_MD: { + + auto other_side = + securities::other_side(algo_services().definition().side()); + auto &other_side_top = me->last_book.side(other_side)[0]; + + if (other_side_top.size() > 0) { + // First time we get the opposite side market, we will use + // this price as the target price for the join + me->status = Status::WAITING_TO_JOIN; + me->book_at_inception = b; + LOG(OrderMan, Diag, + "JoinOrder order {} receiving first book: {} Target: {}", + algo_services().order_key().value(), + me->min_quantity_to_join.value(), other_side_top.price()); + try_to_join(); + } + } break; + case Status::JOINED: + // Nothing to do at this point + break; + case Status::WAITING_TO_JOIN: + // Check if we reached the target and join if so + try_to_join(); + break; + } +} + +// Given the current book, our target price and our min quantity +// parameter, join if appropriate +void DynamicJoin::try_to_join() { + bts_assert(me->status == Status::WAITING_TO_JOIN); + + auto my_side = algo_services().definition().side(); + auto other_side = securities::other_side(my_side); + auto &my_side_psz = me->last_book.side(my_side)[0]; + auto &opposite_at_inception_psz = me->book_at_inception.side(other_side)[0]; + + if (my_side_psz.size() && + my_side_psz.size() >= me->min_quantity_to_join.value() && + securities::is_equal_or_better( + my_side, Price(my_side_psz.price()), + Price(opposite_at_inception_psz.price()))) { + + // Time to update our child to join the market + Update child_update; + child_update.set_price(my_side_psz.price()); + child_update.set_quantity(algo_services().quantity().value()); + algo_services().update_child(me->child_key, child_update); + me->status = Status::JOINED; + + LOG(OrderMan, Diag, "DynamicJoin order {} joined: {}@{}", + algo_services().order_key().value(), + algo_services().quantity().value(), my_side_psz.price()); + + } else { + LOG(OrderMan, Diag, + "JoinOrder order {} still waiting to join. Order: {}@{} Top of " + "the book: {}@{} Target: {}@{}", + algo_services().order_key().value(), + algo_services().quantity().value(), algo_services().price().value(), + my_side_psz.size(), my_side_psz.price(), + me->min_quantity_to_join.value(), opposite_at_inception_psz.price()); + } +} + +// Our child reported a change: +void DynamicJoin::on_child_change(OrderKey, ChildState const &st) { + // Our fill quantity and price is always a reflection of our + // child's state + algo_services().update_fill_state(st.total_fills, st.last_price); + + switch (st.status) { + case ChildStatus::WORKING: + // Child order is still working, nothing to do + return; + case ChildStatus::COMPLETED: + // Child order completed, we should also complete + algo_services().complete(); + return; + case ChildStatus::CANCELED: + // Child order unexpectedly canceled, signal an error and + // terminate + algo_services().terminate_error("Unexpected child cancel"); + return; + } +} + +// This is called by the spark daemon at startup. In this call we +// describe our algo's parameters and register a factory function to +// create an instance of the algo. +extern "C" void init_spark_plugin(bts::SparkServices &serv) { + using namespace bts; + + DynamicOrderDescriptor join_desc; + + // Start by naming our order type + join_desc.set_dynamic_order_type("dyn join"); + + // Now add a single unsigned int parameter, Min Quantity, that will + // determine the minimum quantity that must exist in the book in + // order for us to join the market. + *join_desc.add_param_desc() = param_uint32("Min Quantity"); + + // Associate the descriptor with a builder function and register + // that with the Spark server. + auto result = + serv.register_dynamic_algo_order( + join_desc, []() { return std::make_unique(); }); + + if (result != bts::AlgoRegistrationResult::SUCCESS) { + // The join_desc has errors + // Check the error log for more details + } +} + +} // namespace order_example +} // namespace bts + +``` diff --git a/doc/api/content/server/samples/template.md b/doc/api/content/server/samples/template.md new file mode 100644 index 000000000..48c8150c0 --- /dev/null +++ b/doc/api/content/server/samples/template.md @@ -0,0 +1,146 @@ ++++ +title = "Template" +weight = 5 ++++ + +A mostly blank Algo template that can be used as a jump-start for developing a new Algo. + +
+ +This sample is part of the Server SDK examples included in the spark_sdk.zip download. +
+ +```cpp +#include "spark/lib/config/config.h" +#include "spark/lib/config/protos/system.pb.h" +#include "spark/lib/domain_model/Security.h" +#include "spark/lib/orderman/orders/algo/IAlgoOrder.h" +#include "spark/lib/orderman/orders/algo/IAlgoServices.h" +#include "spark/lib/plugin/SparkServices.h" + +#include "spark/lib/orderman/order_factory/IOrderFactory.h" +#include "spark/lib/orderman/orderman_utils/Book.h" +#include "spark/lib/orderman/orderman_utils/orderman_utils.h" +#include "spark/lib/utils/PBUtils.h" + +using namespace bts::api::orderman; +using namespace bts::orderman; + +namespace bts { +namespace order_example { + +// MyAlgo is a blank IAlgoOrder which you can copy when starting a new Algo +class MyAlgo : public IAlgoOrder { + public: + MyAlgo(); + ~MyAlgo(); + + OrderOpResult do_before_launch(IBeforeLaunchAlgoServices &) override; + void do_launch(IAlgoServices &) override; + void do_launch_attached(IAlgoServices &, OrderKey, ChildState) override; + void do_update(api::orderman::Update const &) override; + void on_book(SecurityKey, Book const &) override; + void on_child_change(OrderKey, ChildState const &) override; + + private: + IAlgoServices* algo_services; + ChildTemplate *child_template = nullptr; + OrderKey child_key = null_order_key(); +}; + + +MyAlgo::MyAlgo() : algo_services(nullptr) {} +MyAlgo::~MyAlgo() {} + +// This is called outside of the latency sensitive path. +// Expensive initialization should be done here, rather than in do_launch() +OrderOpResult MyAlgo::do_before_launch(IBeforeLaunchAlgoServices &serv) { + LOG(Sdk,Info, "MyAlgo: do_before_launch"); + // If we are not attaching, create a child template so we can + // quickly launch our child when the time comes. + if (!serv.is_attaching()) { + // We will be sending a limit order with the same side, + // security, trader, account, etc, as this algo order, so just + // copy our definition to the child definition to start with. + auto child_def = serv.definition(); + // Now add a limit order component to mark our child as a limit + // order. Limit orders do not require any extra parameters + // beyond those already inherited from this algo order + // (i.e. beyond what we obtained through serv.definition()) so + // we can just use a default constructed LimitOrderDefinition. + *child_def.mutable_custom_def()->mutable_limit() = LimitOrderDefinition{}; + // Now just build the child template and cache it for future use + child_template = &(serv.make_child_template(child_def)); + } + // Default constructed result is successful + return {}; +} + +// Called in the latency sensitive path, once, when the algo is first launched. +// If the Algo is attaching to an existing order do_launch_attached is called instead. +void MyAlgo::do_launch(IAlgoServices &services) { + algo_services = &services; + // Subscribe to book updates for this security + services.subscribe_book(SecurityKey(services.security().id())); + // Launch a child limit order + child_key = + services.launch_child(*child_template, services.price(), services.quantity()); + LOG(Sdk,Info, "MyAlgo: do_launch"); +} + +// Called in the latency sensitive path, once, when the algo is first +// launched. In this case the algo is provided with an existing order +// it is now responsible for managing. +void MyAlgo::do_launch_attached(IAlgoServices &services, + OrderKey attached_order, ChildState st) { + algo_services = &services; + // Subscribe to book updates for this security + services.subscribe_book(SecurityKey(services.security().id())); + // Keep track of the child key + child_key = attached_order; + LOG(Sdk,Info, "MyAlgo: do_launch_attached"); +} + +// Called when a human (or a supervising algo) updates a parameter for +// this algo. The implementation validates the update and depending on +// the validation outcome either applies the update or terminates the algo. +void MyAlgo::do_update(api::orderman::Update const &update) { +} + +// Called when the book for the specified security has changed +void MyAlgo::on_book(SecurityKey, Book const &b) { +} + +// Our child reported a change +void MyAlgo::on_child_change(OrderKey, ChildState const &st) { +} + +// This is called by the spark daemon at startup. In this call we +// describe our algo's parameters and register a factory function to +// create an instance of the algo. +extern "C" void init_spark_plugin(bts::SparkServices &serv) { + using namespace bts; + + DynamicOrderDescriptor desc; + + // Start by naming our order type + desc.set_dynamic_order_type("My Algo"); + + *desc.add_param_desc() = param_bool("Enable Feature"); + *desc.add_param_desc() = param_uint32("Feature Quantity"); + + // Associate the descriptor with a builder function and register + // that with the Spark server. + auto result = + serv.register_dynamic_algo_order( + desc, []() { return std::make_unique(); }); + + if (result != bts::AlgoRegistrationResult::SUCCESS) { + // The desc has errors + // Check the error log (/sparkdata/data/logging/log) for more details + } +} + +} // namespace order_example +} // namespace bts +``` \ No newline at end of file diff --git a/doc/api/deploy_to_web.sh b/doc/api/deploy_to_web.sh new file mode 100644 index 000000000..fdf8a6645 --- /dev/null +++ b/doc/api/deploy_to_web.sh @@ -0,0 +1,9 @@ +#/bin/bash + +# ../../externals/linux/bin/zola/zola build +zola.exe build + +rsync -avzL public/ bts@api.bluetradesys.com:/data/sdk/public/ + +rm -rf ./public/ + diff --git a/doc/api/sass/_base.scss b/doc/api/sass/_base.scss new file mode 100644 index 000000000..7f3a36563 --- /dev/null +++ b/doc/api/sass/_base.scss @@ -0,0 +1,97 @@ +html, body { + font-size: 16px; + height: 100%; +} + +body { + font-family: "Source Sans Pro", "Open Sans","Helvetica Neue",Helvetica,Arial,sans-serif; + line-height: 1.6; + // from http://www.heropatterns.com + background-color: #0072a2; + background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='100' height='100' viewBox='0 0 100 100'%3E%3Cg fill-rule='evenodd'%3E%3Cg fill='%234b4b4b' fill-opacity='0.4'%3E%3Cpath opacity='.5' d='M96 95h4v1h-4v4h-1v-4h-9v4h-1v-4h-9v4h-1v-4h-9v4h-1v-4h-9v4h-1v-4h-9v4h-1v-4h-9v4h-1v-4h-9v4h-1v-4h-9v4h-1v-4H0v-1h15v-9H0v-1h15v-9H0v-1h15v-9H0v-1h15v-9H0v-1h15v-9H0v-1h15v-9H0v-1h15v-9H0v-1h15v-9H0v-1h15V0h1v15h9V0h1v15h9V0h1v15h9V0h1v15h9V0h1v15h9V0h1v15h9V0h1v15h9V0h1v15h9V0h1v15h4v1h-4v9h4v1h-4v9h4v1h-4v9h4v1h-4v9h4v1h-4v9h4v1h-4v9h4v1h-4v9h4v1h-4v9zm-1 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-9-10h9v-9h-9v9zm10 0h9v-9h-9v9zm10 0h9v-9h-9v9zm10 0h9v-9h-9v9zm10 0h9v-9h-9v9zm10 0h9v-9h-9v9zm10 0h9v-9h-9v9zm10 0h9v-9h-9v9zm9-10v-9h-9v9h9zm-10 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-9-10h9v-9h-9v9zm10 0h9v-9h-9v9zm10 0h9v-9h-9v9zm10 0h9v-9h-9v9zm10 0h9v-9h-9v9zm10 0h9v-9h-9v9zm10 0h9v-9h-9v9zm10 0h9v-9h-9v9zm9-10v-9h-9v9h9zm-10 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-9-10h9v-9h-9v9zm10 0h9v-9h-9v9zm10 0h9v-9h-9v9zm10 0h9v-9h-9v9zm10 0h9v-9h-9v9zm10 0h9v-9h-9v9zm10 0h9v-9h-9v9zm10 0h9v-9h-9v9zm9-10v-9h-9v9h9zm-10 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-9-10h9v-9h-9v9zm10 0h9v-9h-9v9zm10 0h9v-9h-9v9zm10 0h9v-9h-9v9zm10 0h9v-9h-9v9zm10 0h9v-9h-9v9zm10 0h9v-9h-9v9zm10 0h9v-9h-9v9z'/%3E%3Cpath d='M6 5V0H5v5H0v1h5v94h1V6h94V5H6z'/%3E%3C/g%3E%3C/g%3E%3C/svg%3E"); + + color: $foreground; + + // for sticky footer + display: flex; + flex-direction: column; + min-height: 100vh; +} + +a { + color: $link-color; + text-decoration: none; + cursor: pointer; + border-bottom: 1px solid rgba(0, 124, 188, 0.25); + + &:hover { + border-bottom: 1px solid $link-color; + } + + &:visited { + color: $link-color; + } + + // totally taken from stripe + &.button { + outline: none; + white-space: nowrap; + display: inline-block; + height: 40px; + line-height: 40px; + padding: 0 14px; + box-shadow: 0 4px 6px rgba(50, 50, 93, 0.11), 0 1px 3px rgba(0, 0, 0, 0.08); + background: #fff; + border-radius: 4px; + font-size: 15px; + font-weight: 600; + text-transform: uppercase; + letter-spacing: .025em; + color: #191919; + text-decoration: none; + transition: all .15s ease; + border-bottom: none; + + &:hover { + transform: translateY(-1px); + text-decoration: none; + box-shadow: 0 7px 14px rgba(50, 50, 93, 0.1), 0 3px 6px rgba(0, 0, 0, 0.08); + } + + &:active { + background-color: #f6f9fc; + transform: translateY(1px); + } + } + + &.white { + color: white; + border-bottom-color: rgba(255, 255, 255, 0.25); + + &:hover { + border-bottom-color: white; + } + + &:visited { + color: white; + } + } +} + +pre { + padding: 1rem; + overflow: auto; +} + +p code, li code { + // color: #e6e1dc; + // background-color: #383838; + color: #191919; + background-color: #f5f5f5; + white-space: pre-wrap; + padding: 3px; + border-radius: 3px; + font-size: 0.85rem; + font-family: monospace; + // box-shadow: 0 1px 3px rgba(0,0,0,.1), 0 1px 1px rgba(0,0,0,.1), 0 2px 1px -1px rgba(0,0,0,.12); +} diff --git a/doc/api/sass/_docs.scss b/doc/api/sass/_docs.scss new file mode 100644 index 000000000..02478b5a6 --- /dev/null +++ b/doc/api/sass/_docs.scss @@ -0,0 +1,96 @@ +.documentation { + padding: 3rem; + display: flex; + + &__sidebar { + margin-right: 2rem; + + ul { + padding-left: 0; + list-style: none; + } + + > ul { + > li { + margin-bottom: 1rem; + } + + .documentation__sidebar__title { + font-size: 1.25rem; + font-weight: bold; + } + + ul { + margin-top: 0.25rem; + + li { + border-left: 1px solid $background; + padding-left: 0.75rem; + padding-top: 0.25rem; + padding-bottom: 0.25rem; + } + + li.active a { + font-weight: bold; + border-bottom: 1px solid $link-color; + } + } + } + + } + + &__content { + width: 60%; + // Same as the anchor absolute left to not overlap + margin-left: 2rem; + + h1, h2, h3, h4, h5, h6 { + .zola-anchor { + font-size: 1.25rem; + visibility: hidden; + margin-left: -2rem; + text-decoration: none; + border-bottom-color: transparent; + cursor: pointer; + } + + &:hover { + .zola-anchor { + visibility: visible; + } + } + } + + blockquote { + border-left: 10px solid black; + padding: 0.25rem 0.5rem; + margin-left: 0; + font-style: italic; + } + } + + iframe { + width: 100%; + min-height: 400px; + } +} + + +@media only screen and (max-width: 1000px) { + .documentation { + flex-direction: column; + padding: 2rem; + + &__content { + width: 100%; + margin-left: 0; + } + + &__sidebar { + border-top: 1px solid $background; + order: 1; + margin-right: 0; + margin-top: 1rem; + } + } +} diff --git a/doc/api/sass/_header.scss b/doc/api/sass/_header.scss new file mode 100644 index 000000000..699e025aa --- /dev/null +++ b/doc/api/sass/_header.scss @@ -0,0 +1,33 @@ +header { + .header__logo { + border-bottom: none; + } + + nav a { + margin-right: 2rem; + } + + .header__logo { + font-size: 2rem; + font-weight: bold; + + &:hover { + text-decoration: none; + } + } +} + +@media only screen and (max-width: 1000px) { + header { + padding: 1rem 0; + + nav { + text-align: center; + } + + .header__logo { + // to force menu links to be on a line below + display: block; + } + } +} diff --git a/doc/api/sass/_index.scss b/doc/api/sass/_index.scss new file mode 100644 index 000000000..74b564007 --- /dev/null +++ b/doc/api/sass/_index.scss @@ -0,0 +1,71 @@ +.inverted-colours { + background: $foreground; + color: $background; +} + +.hero { + width: 60%; + margin: 0 auto; + margin-top: 2rem; + margin-bottom: 4rem; + text-align: center; + + &__tagline { + margin-bottom: 2rem; + } +} + +.disabled { + opacity: 0.5; +} + +.selling-points { + padding: 3rem; + + &__content { + display: flex; + flex-wrap: wrap; + } +} + +.selling-point { + // 2 selling points per row on desktop + width: 50%; + padding: 2rem; + padding-top: 1rem; +} + +.selling-header { + width: 100%; + padding: 1rem; + padding-top: 0rem; + display: flex; + align-items: center; + justify-content: center; + img { + align-self: center; + max-width: 70%; + } +} + +// and 1 on mobile +@media only screen and (max-width: 1000px) { + .hero { + width: 90%; + margin-top: 2rem; + margin-bottom: 4rem; + } + + .selling-points { + padding: 2rem; + } + + .selling-point { + width: 100%; + padding: 1rem; + + h2 { + text-align: center; + } + } +} diff --git a/doc/api/sass/_layout.scss b/doc/api/sass/_layout.scss new file mode 100644 index 000000000..fbd1bc483 --- /dev/null +++ b/doc/api/sass/_layout.scss @@ -0,0 +1,43 @@ +header { + padding: 2rem 3rem; +} + +.container { + max-width: 1000px; + margin: 0 auto; +} + +@media only screen and (max-width: 1000px) { + .container { + max-width: 90%; + } +} + +.content { + flex-grow: 1; + + &--reversed { + background: $foreground; + color: $background; + } +} + +.linux { + padding: 25px 15px; + background: #eff0f1; + border-radius: 5px; + border: 1px solid #0072a2; +} + +.info { + padding: 25px 15px; + background: #ffffe0; + border-radius: 5px; + border: 1px solid #383838; +} + + +footer { + text-align: center; + padding: 2rem 3rem; +} diff --git a/doc/api/sass/_normalize.scss b/doc/api/sass/_normalize.scss new file mode 100644 index 000000000..fa4e73dd4 --- /dev/null +++ b/doc/api/sass/_normalize.scss @@ -0,0 +1,447 @@ +/*! normalize.css v7.0.0 | MIT License | github.com/necolas/normalize.css */ + +/* Document + ========================================================================== */ + +/** + * 1. Correct the line height in all browsers. + * 2. Prevent adjustments of font size after orientation changes in + * IE on Windows Phone and in iOS. + */ + +html { + line-height: 1.15; /* 1 */ + -ms-text-size-adjust: 100%; /* 2 */ + -webkit-text-size-adjust: 100%; /* 2 */ +} + +/* Sections + ========================================================================== */ + +/** + * Remove the margin in all browsers (opinionated). + */ + +body { + margin: 0; +} + +/** + * Add the correct display in IE 9-. + */ + +article, +aside, +footer, +header, +nav, +section { + display: block; +} + +/** + * Correct the font size and margin on `h1` elements within `section` and + * `article` contexts in Chrome, Firefox, and Safari. + */ + +h1 { + font-size: 2em; + margin: 0.67em 0; +} + +/* Grouping content + ========================================================================== */ + +/** + * Add the correct display in IE 9-. + * 1. Add the correct display in IE. + */ + +figcaption, +figure, +main { /* 1 */ + display: block; +} + +/** + * Add the correct margin in IE 8. + */ + +figure { + margin: 1em 40px; +} + +/** + * 1. Add the correct box sizing in Firefox. + * 2. Show the overflow in Edge and IE. + */ + +hr { + box-sizing: content-box; /* 1 */ + height: 0; /* 1 */ + overflow: visible; /* 2 */ +} + +/** + * 1. Correct the inheritance and scaling of font size in all browsers. + * 2. Correct the odd `em` font sizing in all browsers. + */ + +pre { + font-family: monospace, monospace; /* 1 */ + font-size: 1em; /* 2 */ +} + +/* Text-level semantics + ========================================================================== */ + +/** + * 1. Remove the gray background on active links in IE 10. + * 2. Remove gaps in links underline in iOS 8+ and Safari 8+. + */ + +a { + background-color: transparent; /* 1 */ + -webkit-text-decoration-skip: objects; /* 2 */ +} + +/** + * 1. Remove the bottom border in Chrome 57- and Firefox 39-. + * 2. Add the correct text decoration in Chrome, Edge, IE, Opera, and Safari. + */ + +abbr[title] { + border-bottom: none; /* 1 */ + text-decoration: underline; /* 2 */ + text-decoration: underline dotted; /* 2 */ +} + +/** + * Prevent the duplicate application of `bolder` by the next rule in Safari 6. + */ + +b, +strong { + font-weight: inherit; +} + +/** + * Add the correct font weight in Chrome, Edge, and Safari. + */ + +b, +strong { + font-weight: bolder; +} + +/** + * 1. Correct the inheritance and scaling of font size in all browsers. + * 2. Correct the odd `em` font sizing in all browsers. + */ + +code, +kbd, +samp { + font-family: monospace, monospace; /* 1 */ + font-size: 1em; /* 2 */ +} + +/** + * Add the correct font style in Android 4.3-. + */ + +dfn { + font-style: italic; +} + +/** + * Add the correct background and color in IE 9-. + */ + +mark { + background-color: #ff0; + color: #000; +} + +/** + * Add the correct font size in all browsers. + */ + +small { + font-size: 80%; +} + +/** + * Prevent `sub` and `sup` elements from affecting the line height in + * all browsers. + */ + +sub, +sup { + font-size: 75%; + line-height: 0; + position: relative; + vertical-align: baseline; +} + +sub { + bottom: -0.25em; +} + +sup { + top: -0.5em; +} + +/* Embedded content + ========================================================================== */ + +/** + * Add the correct display in IE 9-. + */ + +audio, +video { + display: inline-block; +} + +/** + * Add the correct display in iOS 4-7. + */ + +audio:not([controls]) { + display: none; + height: 0; +} + +/** + * Remove the border on images inside links in IE 10-. + */ + +img { + border-style: none; +} + +/** + * Hide the overflow in IE. + */ + +svg:not(:root) { + overflow: hidden; +} + +/* Forms + ========================================================================== */ + +/** + * 1. Change the font styles in all browsers (opinionated). + * 2. Remove the margin in Firefox and Safari. + */ + +button, +input, +optgroup, +select, +textarea { + font-family: sans-serif; /* 1 */ + font-size: 100%; /* 1 */ + line-height: 1.15; /* 1 */ + margin: 0; /* 2 */ +} + +/** + * Show the overflow in IE. + * 1. Show the overflow in Edge. + */ + +button, +input { /* 1 */ + overflow: visible; +} + +/** + * Remove the inheritance of text transform in Edge, Firefox, and IE. + * 1. Remove the inheritance of text transform in Firefox. + */ + +button, +select { /* 1 */ + text-transform: none; +} + +/** + * 1. Prevent a WebKit bug where (2) destroys native `audio` and `video` + * controls in Android 4. + * 2. Correct the inability to style clickable types in iOS and Safari. + */ + +button, +html [type="button"], /* 1 */ +[type="reset"], +[type="submit"] { + -webkit-appearance: button; /* 2 */ +} + +/** + * Remove the inner border and padding in Firefox. + */ + +button::-moz-focus-inner, +[type="button"]::-moz-focus-inner, +[type="reset"]::-moz-focus-inner, +[type="submit"]::-moz-focus-inner { + border-style: none; + padding: 0; +} + +/** + * Restore the focus styles unset by the previous rule. + */ + +button:-moz-focusring, +[type="button"]:-moz-focusring, +[type="reset"]:-moz-focusring, +[type="submit"]:-moz-focusring { + outline: 1px dotted ButtonText; +} + +/** + * Correct the padding in Firefox. + */ + +fieldset { + padding: 0.35em 0.75em 0.625em; +} + +/** + * 1. Correct the text wrapping in Edge and IE. + * 2. Correct the color inheritance from `fieldset` elements in IE. + * 3. Remove the padding so developers are not caught out when they zero out + * `fieldset` elements in all browsers. + */ + +legend { + box-sizing: border-box; /* 1 */ + color: inherit; /* 2 */ + display: table; /* 1 */ + max-width: 100%; /* 1 */ + padding: 0; /* 3 */ + white-space: normal; /* 1 */ +} + +/** + * 1. Add the correct display in IE 9-. + * 2. Add the correct vertical alignment in Chrome, Firefox, and Opera. + */ + +progress { + display: inline-block; /* 1 */ + vertical-align: baseline; /* 2 */ +} + +/** + * Remove the default vertical scrollbar in IE. + */ + +textarea { + overflow: auto; +} + +/** + * 1. Add the correct box sizing in IE 10-. + * 2. Remove the padding in IE 10-. + */ + +[type="checkbox"], +[type="radio"] { + box-sizing: border-box; /* 1 */ + padding: 0; /* 2 */ +} + +/** + * Correct the cursor style of increment and decrement buttons in Chrome. + */ + +[type="number"]::-webkit-inner-spin-button, +[type="number"]::-webkit-outer-spin-button { + height: auto; +} + +/** + * 1. Correct the odd appearance in Chrome and Safari. + * 2. Correct the outline style in Safari. + */ + +[type="search"] { + -webkit-appearance: textfield; /* 1 */ + outline-offset: -2px; /* 2 */ +} + +/** + * Remove the inner padding and cancel buttons in Chrome and Safari on macOS. + */ + +[type="search"]::-webkit-search-cancel-button, +[type="search"]::-webkit-search-decoration { + -webkit-appearance: none; +} + +/** + * 1. Correct the inability to style clickable types in iOS and Safari. + * 2. Change font properties to `inherit` in Safari. + */ + +::-webkit-file-upload-button { + -webkit-appearance: button; /* 1 */ + font: inherit; /* 2 */ +} + +/* Interactive + ========================================================================== */ + +/* + * Add the correct display in IE 9-. + * 1. Add the correct display in Edge, IE, and Firefox. + */ + +details, /* 1 */ +menu { + display: block; +} + +/* + * Add the correct display in all browsers. + */ + +summary { + display: list-item; +} + +/* Scripting + ========================================================================== */ + +/** + * Add the correct display in IE 9-. + */ + +canvas { + display: inline-block; +} + +/** + * Add the correct display in IE. + */ + +template { + display: none; +} + +/* Hidden + ========================================================================== */ + +/** + * Add the correct display in IE 10-. + */ + +[hidden] { + display: none; +} diff --git a/doc/api/sass/_search.scss b/doc/api/sass/_search.scss new file mode 100644 index 000000000..f1ffb70e8 --- /dev/null +++ b/doc/api/sass/_search.scss @@ -0,0 +1,47 @@ +.search-container { + display: inline-block; + position: relative; + width: 300px; + + input { + width: 100%; + padding: 0.5rem; + } +} + +.search-results { + display: none; + position: absolute; + background: white; + color: black; + padding: 1rem; + box-shadow: 2px 2px 2px 0 rgba(0, 0, 0, 0.5); + max-height: 500px; + overflow: auto; + width: 150%; + right: 0; + + &__items { + list-style: none; + } + + li { + margin-top: 1rem; + border-bottom: 1px solid #ccc; + font-size: 0.9rem; + + &:first-of-type { + margin-top: 0; + } + } + + &__item { + margin-bottom: 1rem; + + a { + font-size: 1.2rem; + display: inline-block; + margin-bottom: 0.5rem; + } + } +} diff --git a/doc/api/sass/_themes.scss b/doc/api/sass/_themes.scss new file mode 100644 index 000000000..dc495e678 --- /dev/null +++ b/doc/api/sass/_themes.scss @@ -0,0 +1,79 @@ + +.themes-container { + padding: 3rem; + width: 80%; + margin: 0 auto; + + img { + max-width: 100%; + } +} + +@media only screen and (max-width: 1000px) { + .themes-container { + width: 100%; + margin: 0 1rem; + } +} + +.themes { + display: flex; + flex-wrap: wrap; + justify-content: space-between; + + .theme { + width: 45%; + text-decoration: none; + cursor: pointer; + margin-bottom: 2rem; + border-bottom: none; + box-shadow: 0 1px 2px rgba(0, 0, 0, 0.4); + padding: 10px; + + img { + width: 100%; + height: 90%; + } + + span { + display: block; + text-align: center; + font-size: 1.1rem; + } + } +} + +.theme-info { + display: flex; + align-items: flex-start; + + .thumb { + box-shadow: 0 1px 2px rgba(0, 0, 0, 0.07); + width: 400px; + margin-right: 2rem; + } + + h1, p { + margin: 0; + } + + padding: 1rem; +} + + +@media only screen and (max-width: 1000px) { + .themes .theme { + width: 100%; + } +} + +@media only screen and (max-width: 1000px) { + .theme-info { + flex-direction: column; + align-items: center; + + img { + margin-bottom: 1rem; + } + } +} diff --git a/doc/api/sass/site.scss b/doc/api/sass/site.scss new file mode 100644 index 000000000..a9e5129ce --- /dev/null +++ b/doc/api/sass/site.scss @@ -0,0 +1,19 @@ +@charset "utf-8"; + +@import "normalize"; + +*, *:before, *:after { + box-sizing: border-box; +} + +$background: #191919; +$foreground: white; +$link-color: #007CBC; + +@import "base"; +@import "layout"; +@import "header"; +@import "index"; +@import "docs"; +@import "themes"; +@import "search"; diff --git a/doc/api/static/favicon.ico b/doc/api/static/favicon.ico new file mode 100644 index 000000000..72ee404ee Binary files /dev/null and b/doc/api/static/favicon.ico differ diff --git a/doc/api/static/images/API.png b/doc/api/static/images/API.png new file mode 100644 index 000000000..e53f95aaf Binary files /dev/null and b/doc/api/static/images/API.png differ diff --git a/doc/api/static/images/logo.svg b/doc/api/static/images/logo.svg new file mode 100644 index 000000000..d9d0f4b0c --- /dev/null +++ b/doc/api/static/images/logo.svg @@ -0,0 +1,2 @@ + + diff --git a/doc/api/static/search.js b/doc/api/static/search.js new file mode 100644 index 000000000..c24a59625 --- /dev/null +++ b/doc/api/static/search.js @@ -0,0 +1,180 @@ +function debounce(func, wait) { + var timeout; + + return function () { + var context = this; + var args = arguments; + clearTimeout(timeout); + + timeout = setTimeout(function () { + timeout = null; + func.apply(context, args); + }, wait); + }; +} + +// Taken from mdbook +// The strategy is as follows: +// First, assign a value to each word in the document: +// Words that correspond to search terms (stemmer aware): 40 +// Normal words: 2 +// First word in a sentence: 8 +// Then use a sliding window with a constant number of words and count the +// sum of the values of the words within the window. Then use the window that got the +// maximum sum. If there are multiple maximas, then get the last one. +// Enclose the terms in . +function makeTeaser(body, terms) { + var TERM_WEIGHT = 40; + var NORMAL_WORD_WEIGHT = 2; + var FIRST_WORD_WEIGHT = 8; + var TEASER_MAX_WORDS = 30; + + var stemmedTerms = terms.map(function (w) { + return elasticlunr.stemmer(w.toLowerCase()); + }); + var termFound = false; + var index = 0; + var weighted = []; // contains elements of ["word", weight, index_in_document] + + // split in sentences, then words + var sentences = body.toLowerCase().split(". "); + + for (var i in sentences) { + var words = sentences[i].split(" "); + var value = FIRST_WORD_WEIGHT; + + for (var j in words) { + var word = words[j]; + + if (word.length > 0) { + for (var k in stemmedTerms) { + if (elasticlunr.stemmer(word).startsWith(stemmedTerms[k])) { + value = TERM_WEIGHT; + termFound = true; + } + } + weighted.push([word, value, index]); + value = NORMAL_WORD_WEIGHT; + } + + index += word.length; + index += 1; // ' ' or '.' if last word in sentence + } + + index += 1; // because we split at a two-char boundary '. ' + } + + if (weighted.length === 0) { + return body; + } + + var windowWeights = []; + var windowSize = Math.min(weighted.length, TEASER_MAX_WORDS); + // We add a window with all the weights first + var curSum = 0; + for (var i = 0; i < windowSize; i++) { + curSum += weighted[i][1]; + } + windowWeights.push(curSum); + + for (var i = 0; i < weighted.length - windowSize; i++) { + curSum -= weighted[i][1]; + curSum += weighted[i + windowSize][1]; + windowWeights.push(curSum); + } + + // If we didn't find the term, just pick the first window + var maxSumIndex = 0; + if (termFound) { + var maxFound = 0; + // backwards + for (var i = windowWeights.length - 1; i >= 0; i--) { + if (windowWeights[i] > maxFound) { + maxFound = windowWeights[i]; + maxSumIndex = i; + } + } + } + + var teaser = []; + var startIndex = weighted[maxSumIndex][2]; + for (var i = maxSumIndex; i < maxSumIndex + windowSize; i++) { + var word = weighted[i]; + if (startIndex < word[2]) { + // missing text from index to start of `word` + teaser.push(body.substring(startIndex, word[2])); + startIndex = word[2]; + } + + // add around search terms + if (word[1] === TERM_WEIGHT) { + teaser.push(""); + } + startIndex = word[2] + word[0].length; + teaser.push(body.substring(word[2], startIndex)); + + if (word[1] === TERM_WEIGHT) { + teaser.push(""); + } + } + teaser.push("…"); + return teaser.join(""); +} + +function formatSearchResultItem(item, terms) { + return '
' + + `${item.doc.title}` + + `
${makeTeaser(item.doc.body, terms)}
` + + '
'; +} + +function initSearch() { + var $searchInput = document.getElementById("search"); + var $searchResults = document.querySelector(".search-results"); + var $searchResultsItems = document.querySelector(".search-results__items"); + var MAX_ITEMS = 10; + + var options = { + bool: "AND", + fields: { + title: {boost: 2}, + body: {boost: 1}, + } + }; + var currentTerm = ""; + var index = elasticlunr.Index.load(window.searchIndex); + + $searchInput.addEventListener("keyup", debounce(function() { + var term = $searchInput.value.trim(); + if (term === currentTerm || !index) { + return; + } + $searchResults.style.display = term === "" ? "none" : "block"; + $searchResultsItems.innerHTML = ""; + if (term === "") { + return; + } + + var results = index.search(term, options); + if (results.length === 0) { + $searchResults.style.display = "none"; + return; + } + + currentTerm = term; + for (var i = 0; i < Math.min(results.length, MAX_ITEMS); i++) { + var item = document.createElement("li"); + item.innerHTML = formatSearchResultItem(results[i], term.split(" ")); + $searchResultsItems.appendChild(item); + } + }, 150)); +} + + +if (document.readyState === "complete" || + (document.readyState !== "loading" && !document.documentElement.doScroll) +) { + initSearch(); +} else { + document.addEventListener("DOMContentLoaded", initSearch); +} diff --git a/doc/api/templates/client.html b/doc/api/templates/client.html new file mode 100644 index 000000000..6cc05c541 --- /dev/null +++ b/doc/api/templates/client.html @@ -0,0 +1,31 @@ +{% extends "index.html" %} + +{% block extra_nav_class %}{% endblock extra_nav_class %} +{% block extra_content_class %}content--reversed{% endblock extra_content_class %} + +{% block content %} +{% set section = get_section(path="client/_index.md") %} +
+ +
+ {% block doc_content %} + {% endblock doc_content %} +
+
+{% endblock content %} diff --git a/doc/api/templates/documentation.html b/doc/api/templates/documentation.html new file mode 100644 index 000000000..bae22bb22 --- /dev/null +++ b/doc/api/templates/documentation.html @@ -0,0 +1,36 @@ +{% extends "index.html" %} + +{% block extra_nav_class %}{% endblock extra_nav_class %} +{% block extra_content_class %}content--reversed{% endblock extra_content_class %} + +{% block content %} +{%if page.extra.section %} +{% set section = get_section(path="{{page.extra.section}}/_index.md") %} +{% else %} +{% set section = get_section(path="server/_index.md") %} +{% endif %} +
+ +
+ {% block doc_content %} + {% endblock doc_content %} + {% block next_link %} {% endblock next_link %} +
+
+{% endblock content %} diff --git a/doc/api/templates/index.html b/doc/api/templates/index.html new file mode 100644 index 000000000..ce7cd4b82 --- /dev/null +++ b/doc/api/templates/index.html @@ -0,0 +1,101 @@ + + + + + + + + + + + + + {% block title %}{{ config.title }}{% endblock title %} + + + + + + +
+ +
+ +
+ {% block content %} +
+

Spark SDK

+

+ Spark consists of a client UI + (Spark Client), + a high-performance professional futures front end, connected via our proprietary protocol over TCP to our Spark Server, which in turn connects to execution venues via their native protocols. + Use the Spark SDK to extend Spark Server with your custom Algos, then use the Client SDK to launch and control them. +

+
+ +
+
+
+ +
+
+

+ Server SDK + +

+

+ Leverage the ultra-low latency Spark C++ API to develop and run your custom Algo. Have your first sub-10 microsecond trading algorithm running in under an hour. +

+ Get started +
+ +
+

+ Client SDK + +

+

+ The Spark client SDK allows you to integrate spark with your existing ecosystem. The Spark Client is built entirely on this simple, powerful API. +

+ Get started +
+ + +
+
+ + {% endblock content %} +
+
+ © Blue Trading Systems +
+ + + + + + diff --git a/doc/api/templates/page.html b/doc/api/templates/page.html new file mode 100644 index 000000000..bdc9d5c4d --- /dev/null +++ b/doc/api/templates/page.html @@ -0,0 +1,25 @@ +{% extends "documentation.html" %} + +{% block title %}{{ page.title }} | {{ super() }} {% endblock title %} +{% block doc_content %} +

{{page.title}}

+ {{page.content | safe}} +{% endblock doc_content %} + +{% block next_link %} +

+ {% if page.lighter %} + + + Previous + + {% endif %} +   + {% if page.heavier %} + + Next + + + {% endif %} +

+{% endblock next_link %} diff --git a/doc/api/templates/shortcodes/clipable.html b/doc/api/templates/shortcodes/clipable.html new file mode 100644 index 000000000..201b4fb75 --- /dev/null +++ b/doc/api/templates/shortcodes/clipable.html @@ -0,0 +1,15 @@ +
+ {{body}}
+
diff --git a/doc/api/templates/shortcodes/gallery.html b/doc/api/templates/shortcodes/gallery.html new file mode 100644 index 000000000..91a75f48a --- /dev/null +++ b/doc/api/templates/shortcodes/gallery.html @@ -0,0 +1,7 @@ +{% for asset in page.assets -%} + {%- if asset is matching("[.](jpg|png)$") -%} + + + + {%- endif %} +{%- endfor %} diff --git a/doc/api/templates/shortcodes/info.html b/doc/api/templates/shortcodes/info.html new file mode 100644 index 000000000..920e99473 --- /dev/null +++ b/doc/api/templates/shortcodes/info.html @@ -0,0 +1,4 @@ +
+ + {{body}} +
\ No newline at end of file diff --git a/doc/api/templates/shortcodes/linux.html b/doc/api/templates/shortcodes/linux.html new file mode 100644 index 000000000..ee42c83ca --- /dev/null +++ b/doc/api/templates/shortcodes/linux.html @@ -0,0 +1,4 @@ +
+ + {{body}} +
\ No newline at end of file diff --git a/doc/api/templates/shortcodes/resize_image.html b/doc/api/templates/shortcodes/resize_image.html new file mode 100644 index 000000000..f33bc3b5f --- /dev/null +++ b/doc/api/templates/shortcodes/resize_image.html @@ -0,0 +1 @@ + diff --git a/doc/user/content/release_notes/_index.md b/doc/user/content/release_notes/_index.md index 2ed5fb588..3b1adae92 100644 --- a/doc/user/content/release_notes/_index.md +++ b/doc/user/content/release_notes/_index.md @@ -4,6 +4,34 @@ weight = 3 sort_by = "weight" insert_anchor_links = "right" +++ +## 2.14.0 +### User Visible Changes + - All ladders can now be recentered by (shift or alt) + double clicking over the Price column in a ladder + - Added a Max Order Quantity setting to the Safety Checks tab on the Ladder settings. This setting allows for a user-defined maximum order quantity. + - Added a Price Display Format setting to the Market Price and Size Display tab on the Ladder settings. This setting allows for price levels in the ladder to be displayed without a decimal separator. + - Synthetic instrument creation with 3 legs no longer defaults to butterfly quantites +### Bug Fixes + - Fixed an issue where market transition were not observed by IAlgoOrder + - Fixed a bug in error reporting + - Fixed a bug in the PnL window where specifying a certain grouping order would lead to a UI failure + +## 2.13.0 +### User Visible Changes + - Added the name of the spreader to the parent algo badge (for spreader child orders), in the fills, orderbook and floating orderbook windows + - Added support for sounds for new orders + - Added a new quoting throttle parameter for the spreader, that throttles quotes that are moving toward the inside market but are still a configurable number of ticks away + - Enabled the ladder to remember the left panel scroll position across front end restarts +### Bug Fixes + - Fixed the local fills window so that it does not show the admin fills + - Removed the deleted accounts from the ladder accounts dropdown + - Fixed the orderbook price for iceberg of spreaders + +## 2.12.0 +### User Visible Changes + - Added support for ICE market data and order execution +### Internal Changes + - Lots of permission / admin changes to facilitate ICE + ## 2.11.0 ### User Visible Changes - Fixed fills form time filter to also apply to SOD fills @@ -22,12 +50,12 @@ insert_anchor_links = "right" - Fills now include information about the algo that originated the fill - A new, more powerful PnL form is now available ### API Changes - - Added security state information (pre open, halted, normal, closed, etc) + - Added security state information (pre open, halted, normal, closed, etc) ### Bug Fixes - Brought back the "select all" check box in the mini order book - Fixed an issue in the market data subsystem that on rare occasions could result in a startup crash ### Internal Changes - - Fixed an issue that was stopping us from interpreting SOD times as local rather than GMT + - Fixed an issue that was stopping us from interpreting SOD times as local rather than GMT ## 2.9.3 ### Internal Changes - Fixed system test scripts @@ -43,7 +71,7 @@ insert_anchor_links = "right" - Avoided shipping production configuration defaults that lead to trying to download a position file for the development sftp server. ### Api - C# API sample now uses synchronous calls. - + ## 2.9.0 ### User Visible Changes - Improved Workspace management User Interface @@ -51,11 +79,11 @@ insert_anchor_links = "right" - 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 import facilities for GMI position files - Added the ability for risk managers to enter manual fills - Added "raw" P&L columns, that is columns that *do not* include spread legs in their accounting ### Bug Fixes - - Fixed the settings action menu (Load/Save/Reset to Factory) + - Fixed the 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. @@ -92,10 +120,10 @@ insert_anchor_links = "right" - Made it easier to generate simple UI's for new order types ### API Changes - Dynamic orders can now use QuantityMeasure parameter types - + ## 2.7.2 ### User Visible Changes - - Changed the login dialog to avoid leaking workspace names from other users. + - Changed the login dialog to avoid leaking workspace names from other users. - Added new Splitter algo. From the manual: The splitter is useful for pro-rata markets where you want to vary your exposure independent of the order quantity that you want to fill. The @@ -103,13 +131,13 @@ insert_anchor_links = "right" the order quantity but it will put exposed_qty total size on the market. Splitter favors order size over queue position. ### Internal Changes - - Made spark more resilient to lack of connectivity to the CME web site for downloading of templates. - + - Made spark more resilient to lack of connectivity to the CME web site for downloading of templates. + ## 2.7.1 ### User Visible Changes - Fixed some issues with the Cover algo, where in some circumstances it would erroneously cancel launched hedges on completion. - + ## 2.7.0 ### User Visible Changes - Allowed algos to process "in-flight" fills while shutting down @@ -128,25 +156,25 @@ insert_anchor_links = "right" ### User Visible Changes - Fixed an issue in the ladder where in some cases the mini order book could not be brought up from a badge - Fixed a rare race condition that could lead to erroneous limit enforcement - - Fixed an issue with the margin limits where an error could occur if an account name with more than 12 chars was used + - Fixed an issue with the margin limits where an error could occur if an account name with more than 12 chars was used ## 2.6.1 ### User Visible Changes - Fixed an issue in the ladder where an error could occur when trying to change ladder settings for the first time - + ## 2.6.0 ### User Visible Changes - In the ladder made last trade more distinctive when overlayed on the volumn column - - In the ladder, corrected display of volume tool tip + - In the ladder, corrected display of volume tool tip - In the ladder, changed the consolidation of last trade to consider only the price ignoring aggressor changes - Added an option for the Top Algo to delete itself if within N ticks from top of book - Address a rare issue that could lead to an error report on UI exit - Added margin risk limits - Added the "Offset" algo. Offset prices a child algo at a parameterizable distance from the top of the book - - Avoided switching the current account and order type on open ladders when limits are changed + - Avoided switching the current account and order type on open ladders when limits are changed ### Internal Changes - Ensure that admin logins are always allowed to login, regardless of keycode status. - + ## 2.5.0 ### User Visible Changes - Added loss limit risk check @@ -176,13 +204,13 @@ insert_anchor_links = "right" - Made Cover order more independent from its hedging chidren: - Cover continues to operate even if an hedging child is canceled - When a cover is canceled it will detach from any running hedging children, instead of canceling them - - Changed Lean, Join and Cover algos to hide themselves in the ladder once they have completed their work + - Changed Lean, Join and Cover algos to hide themselves in the ladder once they have completed their work - Changed manual fills to appear in day PnL rather than in the SOD records - Add buy/sell coloring to fill rows in fills window - Streamlined the mini order book: - Display of the mini order book popup is quicker - Only exchange orders are displayed - - Update buttons are only displayed for orders that are under control of an algo with the update applying to the entire algo, not just the exchange order + - Update buttons are only displayed for orders that are under control of an algo with the update applying to the entire algo, not just the exchange order - We can now select multiple rows to attach, detach, update or cancel multiple orders in a batch - A checkbox column has been added to make clearer which rows are selected ### API Changes @@ -192,7 +220,7 @@ insert_anchor_links = "right" - Added system requirements to Spark documentation - Implemented keycodes - Temporarily rejected stop and stop limit orders, pending bug fixes - + ## 2.2.0 ### User Visible Changes - Made our orders easier to spot in MBO display @@ -203,30 +231,30 @@ insert_anchor_links = "right" - Improved handling of orders with undiscovered securities - Added feedback to the ladder when the user attempts to middle click on bid/ask columns using an order that does not have parameters - Fixed display of algo studio algo types in order book - - Hid net change setting in synthetic ladders + - Hid net change setting in synthetic ladders - Fixed a number of problems with filtering on the Fills form - On the Lean algo, OnToward and Away are now calculated with respect to the opposite inside market at time of the order launch - Iceberg Spreaders with queue holders are not erroneously cancelled after the first fill - Added security symbol column to fills window - Re-organized ladder settings dialog to make it clearer and easier to use - + ## 2.1.4 ### User Visible Changes - Fixed handling of cancel button in security picker - + ## 2.1.3 ### User Visible Changes - - Fixed a simulator bug in the handling of 0 sized orders + - Fixed a simulator bug in the handling of 0 sized orders ## 2.2.1 ### User Visible Changes - Fixed order book form exception when display a spreader order for whichs the legs were not present yet - + ## 2.1.1 ### User Visible Changes - Added PIQ to GLBX Iceberg orders - Corrected handling of stop limit orders - + ## 2.1.0 ### User Visible Changes - Added support for the GLBX Iceberg order type @@ -237,7 +265,7 @@ insert_anchor_links = "right" - Added PIQ to the mini order book - Fixed a rare bug that could lead to server failure when requesting market data - Fixed a bug that could cause incorrect price displays for certain types of synthetic securities - + ## 2.0.5 ### User Visible Changes - Fixed a bug in the handling of rejected QHO orders @@ -247,19 +275,19 @@ insert_anchor_links = "right" ## 2.0.4 ### User Visible Changes - Fixed performance regression in order manager - + ### Internal changes - Improved market data diagnostics for bad configurations - Changed market data default buffer size - + ## 2.0.3 ### Internal changes - Improved backwards compatibility in UI settings - + ## 2.0.2 ### User Visible Changes - - Middle click the ladder price column to show the orders for that price level + - Middle click the ladder price column to show the orders for that price level - Middle click the bid or ask column in the ladder no longer sends an order ### Internal changes @@ -287,8 +315,8 @@ insert_anchor_links = "right" - Improved settings manager tool output - Added diagnostics in the event MD memory corruption is detected - Added systems tests for join,lean, iceberg and spreader orders - - Made exchange order recovery more resilient in the presence of database loss - + - Made exchange order recovery more resilient in the presence of database loss + ## 1.6.2 ### User Visible Changes ### Exchange simulator @@ -327,21 +355,21 @@ insert_anchor_links = "right" - These columns can be restored by toggling their visibility within the PnL's Settings form - A new Traded Today column has been added to the PnL - Filtering on this column will allow a user to restrict the report to only show today's position -### Internal changes - - Improved organization of the shell notification architecture +### Internal changes + - Improved organization of the shell notification architecture - Added logging to help troubleshoot shell exit hang ## 1.5.6 ### User Visible Changes - - Fixed a bug that could cause limits to be erroneously enforced + - Fixed a bug that could cause limits to be erroneously enforced - Improved algo documentation - Changed how the spreader uses net change - Simplified trigger algo user interface -### Internal changes +### Internal changes - Defaulted to not recording sim market data - - Improved documentation build + - Improved documentation build - Added additional performance tracing - + ## 1.5.5 - Fixed Market View 'stale working orders' bug @@ -349,11 +377,11 @@ insert_anchor_links = "right" - Avoided logging when warming up cache ## 1.5.3 - - Fixed sounds startup issue + - Fixed sounds startup issue ## 1.5.2 - Added better logging to AlgoDefinitionService - + ## 1.5.1 - Fixed missing securities in the Fills and P&L windows. @@ -374,7 +402,7 @@ insert_anchor_links = "right" #### Bug Fixes - Fixed the "Since Open" time filter to use the exchange open time rather than 8:30 AM - Fixed a bug in the order book where sometimes parent orders would not update their last message column. - + #### Algos - Added OCO Algo - An OCO order creates a relationship between a child order and a 'stop' or 'other' order such that when one of these is cancelled, both cancel, or when one is filled, the other order's size is debited in the amount of the fill. Composing this order with Cover simulates scalping functionality @@ -382,7 +410,7 @@ insert_anchor_links = "right" - Improved SDK documentation and usability - Dynamic orders are now properly identified in the order book #### Documentation - - Information on the Spark API has been added to the Spark manual + - Information on the Spark API has been added to the Spark manual - The documentation has been updated and reorganized to make information easier to find ### Internal Changes - Improved robustness of limit system test diff --git a/docker/linux b/docker/linux index c40e6eba4..68f43cef8 160000 --- a/docker/linux +++ b/docker/linux @@ -1 +1 @@ -Subproject commit c40e6eba4ba72480a68cf784b6ffbcacc3fbf4af +Subproject commit 68f43cef8d3edbda05a9517421092ada3c392aff diff --git a/ops_notes.txt b/ops_notes.txt index 2f9b5a6d5..394ac2733 100644 --- a/ops_notes.txt +++ b/ops_notes.txt @@ -1,3 +1,7 @@ +2.14.0 +- Load Algos in libspark.so automatically (no symlink needed) +- Specify commands to run when sparkd starts. These will automatically be killed when sparkd terminates if they are still running. + 1.5.7 - Fixed Aggressor coloring and ladder high/low coloring in SIM diff --git a/src/client/spark/Admin/AdminModel.cs b/src/client/spark/Admin/AdminModel.cs index 2ff340e18..77a6829d6 100644 --- a/src/client/spark/Admin/AdminModel.cs +++ b/src/client/spark/Admin/AdminModel.cs @@ -23,9 +23,9 @@ namespace mbts.spark { on_accounts = service.subscribei_admin_data() .Select(up => up.all.TryGetFirst(out var ad) && ad.Users.Where(u => u.Name == user.name).TryGetSingle(out var usr) && usr.HasUserData && usr.UserData.HasAccountPermissions ? usr.UserData.AccountPermissions.AccountPermissions - .Where(kv => kv.Value.TradingPermission.HasPermissions && kv.Value.AdminPermission.HasPermissions) + .Where(kv => (kv.Value.TradingPermission != null && kv.Value.TradingPermission.HasPermissions) || kv.Value.AdminPermission.HasPermissions) .Select(kv => ad.Accounts.Where(a => a.Id == kv.Key).TryGetSingle(out var acc) ? acc : (BTSAccountImmutable?)null) - .Where(acc => acc.HasValue) + .Where(acc => acc.HasValue && acc.Value.IsPresent) .Select(acc => acc.Value) .ToDictionary(acc => acc.Id, acc => acc.Name) : new Dictionary(0)) diff --git a/src/client/spark/App.xaml.cs b/src/client/spark/App.xaml.cs index 0883bc861..c65ec1d26 100644 --- a/src/client/spark/App.xaml.cs +++ b/src/client/spark/App.xaml.cs @@ -136,8 +136,11 @@ namespace mbts.ui.shell try { if (argv.Contains("-multiple") || _mutex == null || _mutex.WaitOne(TimeSpan.Zero, true)) { - if(_mutex == null || _mutex.WaitOne(TimeSpan.Zero, true)) - init_stunnel(argv); + if(_mutex == null || _mutex.WaitOne(TimeSpan.Zero, true)) { + if (!argv.Contains("-nostunnel")) { + init_stunnel(argv); + } + } return base.Run(); } else { diff --git a/src/client/spark/Daemons/OrderTicketDaemon.cs b/src/client/spark/Daemons/OrderTicketDaemon.cs index cf18c0835..b825740e9 100644 --- a/src/client/spark/Daemons/OrderTicketDaemon.cs +++ b/src/client/spark/Daemons/OrderTicketDaemon.cs @@ -43,6 +43,11 @@ namespace bts.shell { void _show_order_window(Window window) { foreach (var kv in AlgoStudioModelUtils.get_dynamic_resources(_model.algo_model.ui_configuration)) window.Resources[kv.Key] = kv.Value; + // the dynamic algo 'SecurityKey' template has a ContractPickerPopup that expects the security map to be available as a DynamicResource. + var sub = _bus.subscribe(SparkTags.SecuritiesTag) + .OnDispatcher() + .Subscribe(sec_map => window.Resources[ResourceKeys.Securities] = sec_map); + window.Closed += (s, e) => Disposable.dispose(ref sub); window.Show(-200, -160, true); } diff --git a/src/client/spark/Modules/Market/MarketView/MarketViewRow.cs b/src/client/spark/Modules/Market/MarketView/MarketViewRow.cs index 947a02440..6f3688e77 100644 --- a/src/client/spark/Modules/Market/MarketView/MarketViewRow.cs +++ b/src/client/spark/Modules/Market/MarketView/MarketViewRow.cs @@ -26,6 +26,7 @@ namespace mbts.spark { readonly ObservableFilterCollection _working_orders, _working_bids, _working_asks; readonly string _security_text, _security_key_text; readonly Property _bid_price, _ask_price, _last_price, _day_change, _settlement, _session_high, _session_low; + readonly Property _last_price_from_spread; readonly CellProperty _position; readonly CellProperty _last_size; readonly CellProperty _volume; @@ -54,6 +55,7 @@ namespace mbts.spark { _day_change = backing(nameof(day_change), double.NaN); _session_low = backing(nameof(session_low), double.NaN); _session_high = backing(nameof(session_high), double.NaN); + _last_price_from_spread = backing(nameof(last_price_from_spread), false); _settlement = backing(nameof(settlement), double.NaN); _volume = new CellProperty(this, nameof(volume), 0); _inside_bids = new CellProperty(this, nameof(inside_bids)); @@ -109,6 +111,7 @@ namespace mbts.spark { public int position => _position.value; public double raw_day_position => _raw_day_position; public double last_price => _last_price.Value; + public bool last_price_from_spread => _last_price_from_spread.Value; public uint last_size => _last_size.value; public double bid_price => _bid_price.Value; public uint bid_direct_size => _bid_direct_size.Value; @@ -167,15 +170,17 @@ namespace mbts.spark { _inside_bids.on_next_distinct(_bid_price.Value >= _working_best_bid.value, ref dirty); _inside_asks.on_next_distinct(_ask_price.Value <= _working_best_ask.value, ref dirty); } - if (md.md.get_last_trade(ref _last_ref, out NativePriceSize ps, out DateTime ts, out ulong v)) { + if (md.md.get_last_trade(ref _last_ref, out NativePriceSize ps, out DateTime ts, out ulong v, out bool from_spread)) { _last_price.set(ps.price); + _last_price_from_spread.set(from_spread); _last_size.on_next_distinct(ps.size, ref dirty); _volume.on_next_distinct(v, ref dirty); } - _settlement.set(md.md.settlement_price); - _session_low.set(md.md.session_low); - _session_high.set(md.md.session_high); - _day_change.set(last_price - settlement); + dirty |= _settlement.set_new(md.md.settlement_price); + dirty |= _session_low.set_new(md.md.session_low); + dirty |= _session_high.set_new(md.md.session_high); + dirty |= _day_change.set_new(last_price - settlement); + _volume.on_next_distinct(md.md.total_volume, ref dirty); if (dirty) invalidated?.Invoke(); diff --git a/src/client/spark/Modules/Market/MarketView/MarketViewVM.cs b/src/client/spark/Modules/Market/MarketView/MarketViewVM.cs index b57e17d33..d2c8ca2b5 100644 --- a/src/client/spark/Modules/Market/MarketView/MarketViewVM.cs +++ b/src/client/spark/Modules/Market/MarketView/MarketViewVM.cs @@ -38,7 +38,8 @@ namespace mbts.spark { IMultiValueConverter price_converter = new ui.spark.converters.TickDisplayConverter(); add_col(Columns.Security, "Security", r => r.security.ToString(), nameof(Row.security), DefaultColumnWidths.Security, security_filter, DataGridFilterKnownTemplates.Regex); add_col(Columns.Volume, "Volume", r => r.volume.ToString(), nameof(Row.volume), DefaultColumnWidths.Volume, volume_filter, DataGridFilterKnownTemplates.ULongMinMax); - add_col(Columns.Last, "Last", r => r.format_price(r.last_price), nameof(Row.last_price), DefaultColumnWidths.Price, last_price_filter, DataGridFilterKnownTemplates.DoubleMinMax); + add_col(Columns.LastSpd, "Spd", r => r.last_price_from_spread ? "S" : "", nameof(Row.last_price_from_spread), DefaultColumnWidths.Side, last_price_from_spread_filter, DataGridFilterKnownTemplates.Regex); + add_col(Columns.Last, "Last", r => r.format_price(r.last_price), nameof(Row.last_price), DefaultColumnWidths.Price, last_price_filter, DataGridFilterKnownTemplates.DoubleMinMax); add_col(Columns.LastSize, merged_size_col_header, r => r.last_size.ToString(), nameof(Row.last_size), DefaultColumnWidths.Size, last_size_filter, DataGridFilterKnownTemplates.UIntMinMax, h_alignment: System.Windows.HorizontalAlignment.Right); add_col(Columns.Position, "Pos", r => r.position.ToString(), nameof(Row.position), DefaultColumnWidths.Size, position_filter, DataGridFilterKnownTemplates.IntMinMax, true, h_alignment: System.Windows.HorizontalAlignment.Right); add_col(Columns.RawDayPosition, "Raw Day Pos", r => SparkUtils.fmt_position(r.raw_day_position), nameof(Row.raw_day_position), DefaultColumnWidths.Size, raw_day_position_filter, DataGridFilterKnownTemplates.DoubleMinMax, true, h_alignment: System.Windows.HorizontalAlignment.Right); @@ -138,6 +139,7 @@ namespace mbts.spark { ask_direct_size_filter.load_state(filters.ask_direct_size); ask_implied_size_filter.load_state(filters.ask_implied_size); last_price_filter.load_state(filters.last_price); + last_price_from_spread_filter.load_state(filters.last_price_from_spread); last_size_filter.load_state(filters.last_size); day_change_filter.load_state(filters.day_change); settlement_filter.load_state(filters.settlement); @@ -155,7 +157,7 @@ namespace mbts.spark { }), DataGridFilterHelper.hook_filters(rows_view, security_filter, bid_price_filter, bid_merged_size_filter, bid_direct_size_filter, bid_implied_size_filter, ask_price_filter, ask_merged_size_filter, ask_direct_size_filter, ask_implied_size_filter, - last_price_filter, last_size_filter, day_change_filter, settlement_filter, + last_price_filter, last_price_from_spread_filter, last_size_filter, day_change_filter, settlement_filter, session_low_filter, session_high_filter, position_filter, raw_day_position_filter, working_bid_volume_filter, working_ask_volume_filter, volume_filter, security_key_filter), settings_manager.subscribe_current().OnDispatcher().Subscribe(grid_settings_manager), WorkLoop.Start("MarketView", _refresh) @@ -218,6 +220,7 @@ namespace mbts.spark { public IDataGridColumnPersistentFilter ask_direct_size_filter { get; } = new UIntMinMax(r => r.ask_direct_size, nameof(Row.ask_direct_size)); public IDataGridColumnPersistentFilter ask_implied_size_filter { get; } = new UIntMinMax(r => r.ask_implied_size, nameof(Row.ask_implied_size)); public IDataGridColumnPersistentFilter last_price_filter { get; } = new DoubleMinMax(r => r.last_price, nameof(Row.last_price)); + public IDataGridColumnPersistentFilter last_price_from_spread_filter { get; } = new RegexFilterVM(r => r.last_price_from_spread.ToString(), nameof(Row.last_price_from_spread)); public IDataGridColumnPersistentFilter last_size_filter { get; } = new UIntMinMax(r => r.last_size, nameof(Row.last_size)); public IDataGridColumnPersistentFilter day_change_filter { get; } = new DoubleMinMax(r => r.day_change, nameof(Row.day_change)); public IDataGridColumnPersistentFilter settlement_filter { get; } = new DoubleMinMax(r => r.settlement, nameof(Row.settlement)); @@ -250,6 +253,7 @@ namespace mbts.spark { ask_direct_size = ask_direct_size_filter.get_state(), ask_implied_size = ask_implied_size_filter.get_state(), last_price = last_price_filter.get_state(), + last_price_from_spread = last_price_from_spread_filter.get_state(), last_size = last_size_filter.get_state(), day_change = day_change_filter.get_state(), settlement = settlement_filter.get_state(), diff --git a/src/client/spark/Modules/Market/MarketView/MarketViewView.xaml.cs b/src/client/spark/Modules/Market/MarketView/MarketViewView.xaml.cs index 86f9d5593..e42bf0e89 100644 --- a/src/client/spark/Modules/Market/MarketView/MarketViewView.xaml.cs +++ b/src/client/spark/Modules/Market/MarketView/MarketViewView.xaml.cs @@ -12,6 +12,7 @@ namespace mbts.ui.spark { Security, Volume, Last, + LastSpd, LastSize, Position, RawDayPosition, diff --git a/src/client/spark/Modules/Market/MarketView/MarketViewWindowFactory.cs b/src/client/spark/Modules/Market/MarketView/MarketViewWindowFactory.cs index 9c49672a9..d3ae03818 100644 --- a/src/client/spark/Modules/Market/MarketView/MarketViewWindowFactory.cs +++ b/src/client/spark/Modules/Market/MarketView/MarketViewWindowFactory.cs @@ -100,6 +100,7 @@ namespace bts.shell { public static GridColumnSettings security => new GridColumnSettings(col_id(Columns.Security), DefaultColumnHeaders.Security, width: DefaultColumnWidths.Security); public static GridColumnSettings volume => new GridColumnSettings(col_id(Columns.Volume), DefaultColumnHeaders.Volume, width: DefaultColumnWidths.Volume); public static GridColumnSettings last => new GridColumnSettings(col_id(Columns.Last), DefaultColumnHeaders.Last, width: DefaultColumnWidths.Price); + public static GridColumnSettings last_from_spread => new GridColumnSettings(col_id(Columns.LastSpd), DefaultColumnHeaders.S, width: DefaultColumnWidths.Price, is_visible: false); public static GridColumnSettings last_size => new GridColumnSettings(col_id(Columns.LastSize), DefaultColumnHeaders.Size, width: DefaultColumnWidths.Size, horizontal_alignment: System.Windows.HorizontalAlignment.Right); public static GridColumnSettings position => new GridColumnSettings(col_id(Columns.Position), DefaultColumnHeaders.Pos, width: DefaultColumnWidths.Size, horizontal_alignment: System.Windows.HorizontalAlignment.Right); public static GridColumnSettings raw_day_position => new GridColumnSettings(col_id(Columns.RawDayPosition), DefaultColumnHeaders.RawDayPos, width: DefaultColumnWidths.Size, horizontal_alignment: System.Windows.HorizontalAlignment.Right, is_visible: false); @@ -138,6 +139,7 @@ namespace bts.shell { yield return security; yield return volume; yield return last; + yield return last_from_spread; yield return last_size; yield return position; yield return raw_day_position; @@ -224,6 +226,8 @@ namespace bts.shell { [ProtoMember(25)] public string strip_name; [ProtoMember(26)] + public string last_price_from_spread; + [ProtoMember(28)] public string mic_code; } diff --git a/src/client/spark/Modules/Spreader/SyntheticDataHelper.cs b/src/client/spark/Modules/Spreader/SyntheticDataHelper.cs index 2b35df0f2..3d68bd7a8 100644 --- a/src/client/spark/Modules/Spreader/SyntheticDataHelper.cs +++ b/src/client/spark/Modules/Spreader/SyntheticDataHelper.cs @@ -81,9 +81,9 @@ namespace bts.shell { #region QuoteParams public static QuoteParams QuoteParams => new QuoteParams { LeanQuantityRatio = 1, - QuoteInsideMarketDistance = QuoteInsideMarketDistance, + //QuoteInsideMarketDistance = QuoteInsideMarketDistance, QuoteMaxOrderMove = QuoteMaxOrderMove, - QuoteMuteZone = QuoteMuteZone, + //QuoteMuteZone = QuoteMuteZone, QuoteRepriceThrottle = QuoteRepriceThrottle, QuoteInsideMarketDistanceWithTravel = QuoteInsideMarketDistanceWithTravel, }; @@ -113,6 +113,7 @@ namespace bts.shell { }; public static QuoteInsideMarketDistanceWithTravel QuoteInsideMarketDistanceWithTravel => new QuoteInsideMarketDistanceWithTravel { Priority = RulePriority }; public static QuoteInsideMarketDistance QuoteInsideMarketDistance => new QuoteInsideMarketDistance { Priority = RulePriority }; + public static QuoteInsideMarketDistanceThrottle QuoteInsideMarketDistanceThrottle => new QuoteInsideMarketDistanceThrottle(); #endregion #region PreHedge diff --git a/src/client/spark/Modules/Spreader/VM/ParameterRows/Quote/InsideMarketDistance/QuoteInsideMarketDistanceEnabledModel.cs b/src/client/spark/Modules/Spreader/VM/ParameterRows/Quote/InsideMarketDistance/QuoteInsideMarketDistanceEnabledModel.cs new file mode 100644 index 000000000..15ebf0481 --- /dev/null +++ b/src/client/spark/Modules/Spreader/VM/ParameterRows/Quote/InsideMarketDistance/QuoteInsideMarketDistanceEnabledModel.cs @@ -0,0 +1,33 @@ +using bts.shell; +using mbts.ui.spark; + +namespace mbts.spark { + partial class SyntheticEditorVM { + class QuoteInsideMarketDistanceEnabledModel : RowModelBase { + readonly QuoteInsideMarketDistanceTowardModel _toward; + + public QuoteInsideMarketDistanceEnabledModel(QuoteInsideMarketDistanceTowardModel toward) => + _toward = toward; + + public override string display_name { get; } = string.Empty; + public override string help_text { get; } = string.Empty; + public override string man_page { get; } = string.Empty; + public override ParameterSection[] section_path { get; } = new[] { Sections.Quote, Subsections.InsideMarketDistance }; + + public override bool get_value(LegSpreaderParamsTuple leg_tup) => + leg_tup.leg_params.QuoteParams.QuoteInsideMarketDistanceThrottle != null; + + public override void set_value(LegSpreaderParamsTuple leg_tup, bool value) { + if (value) { + if (leg_tup.leg_params.QuoteParams.QuoteInsideMarketDistanceThrottle == null) { + leg_tup.leg_params.QuoteParams.QuoteInsideMarketDistanceThrottle = SyntheticDataHelper.Defaults.QuoteInsideMarketDistanceThrottle; + _toward.apply_value(leg_tup); + } + } else { + leg_tup.leg_params.QuoteParams.QuoteInsideMarketDistanceThrottle = null; + } + _toward.set_enabled(leg_tup, value); + } + } + } +} \ No newline at end of file diff --git a/src/client/spark/Modules/Spreader/VM/ParameterRows/Quote/InsideMarketDistance/QuoteInsideMarketDistanceTowardModel.cs b/src/client/spark/Modules/Spreader/VM/ParameterRows/Quote/InsideMarketDistance/QuoteInsideMarketDistanceTowardModel.cs new file mode 100644 index 000000000..f38365209 --- /dev/null +++ b/src/client/spark/Modules/Spreader/VM/ParameterRows/Quote/InsideMarketDistance/QuoteInsideMarketDistanceTowardModel.cs @@ -0,0 +1,39 @@ +using bts.spark; +using mbts.ui.spark; +using System.Collections.Generic; + +namespace mbts.spark { + partial class SyntheticEditorVM { + class QuoteInsideMarketDistanceTowardModel : RowModelBase { + IDictionary _value_store = new Dictionary(); + + public override string display_name { get; } = "Toward Distance"; + public override string help_text { get; } = string.Empty; + public override string man_page { get; } = string.Empty; + public override ParameterSection[] section_path { get; } = new[] { Sections.Quote, Subsections.InsideMarketDistance }; + + public void apply_value(LegSpreaderParamsTuple leg_tup) => + _get_inside_market_distance(leg_tup).OnTowardDistance = _value_from_store(leg_tup); + + public override uint get_value(LegSpreaderParamsTuple leg_tup) => + _get_inside_market_distance(leg_tup) is QuoteInsideMarketDistanceThrottle inside_market_distance ? + inside_market_distance.OnTowardDistance : + _value_from_store(leg_tup); + + public override void set_value(LegSpreaderParamsTuple leg_tup, uint value) { + _value_store[leg_tup.SecurityKey] = value; + if (_get_inside_market_distance(leg_tup) is QuoteInsideMarketDistanceThrottle inside_market_distance) + inside_market_distance.OnTowardDistance = value; + } + + protected override bool _get_enabled(LegSpreaderParamsTuple leg_tup) => + _get_inside_market_distance(leg_tup) != null; + + QuoteInsideMarketDistanceThrottle _get_inside_market_distance(LegSpreaderParamsTuple leg_tup) => + leg_tup.leg_params.QuoteParams.QuoteInsideMarketDistanceThrottle; + + uint _value_from_store(LegSpreaderParamsTuple leg_tup) => + _value_store.GetValueOrDefault(leg_tup.SecurityKey); + } + } +} \ No newline at end of file diff --git a/src/client/spark/Modules/Spreader/VM/ParameterRows/RowModel.cs b/src/client/spark/Modules/Spreader/VM/ParameterRows/RowModel.cs index acb145abb..3d516614f 100644 --- a/src/client/spark/Modules/Spreader/VM/ParameterRows/RowModel.cs +++ b/src/client/spark/Modules/Spreader/VM/ParameterRows/RowModel.cs @@ -115,6 +115,11 @@ namespace mbts.spark { "dictates if this logic applies to the quoting market or to the synthetic spread " + "market"); + public static ParameterSection InsideMarketDistance => + new ParameterSection("Inside Market Distance", true, + "Reduces re-quoting by throttling quotes that are moving toward the inside market " + + "but are more than 'Toward Distance' away from the inside market"); + public static ParameterSection InsideMarketThrottleThresholds => new ParameterSection("Inside Market Throttle Thresholds", true, "Reduces re-quoting for quotes that are moving toward the inside market. " + diff --git a/src/client/spark/Modules/Spreader/VM/SyntheticEditorVM.cs b/src/client/spark/Modules/Spreader/VM/SyntheticEditorVM.cs index 143ead545..b9c676d9e 100644 --- a/src/client/spark/Modules/Spreader/VM/SyntheticEditorVM.cs +++ b/src/client/spark/Modules/Spreader/VM/SyntheticEditorVM.cs @@ -190,6 +190,8 @@ namespace mbts.spark { var quote_inside_market_throttle_ticks = new QuoteInsideMarketThrottleTicksModel(); var quote_inside_market_throttle_min_move = new QuoteInsideMarketThrottleMinMoveModel(); + var quote_inside_market_distance_toward = new QuoteInsideMarketDistanceTowardModel(); + var pre_hedge_payless_threshold = new PreHedgePaylessThresholdModel(); var pre_hedge_payless_ticks = new PreHedgePaylessTicksModel(); @@ -227,6 +229,8 @@ namespace mbts.spark { //quote_inside_market_throttle_ticks, //quote_inside_market_throttle_min_move, //new MaxTicksInsideMarketModel(), + new QuoteInsideMarketDistanceEnabledModel(quote_inside_market_distance_toward), + quote_inside_market_distance_toward, new PreHedgePaylessEnabledModel(pre_hedge_payless_threshold, pre_hedge_payless_ticks), pre_hedge_payless_threshold, pre_hedge_payless_ticks, @@ -353,19 +357,13 @@ namespace mbts.spark { if (leg_tuples.Count == 2) { _is_setting_price_mults = true; _price_mult_model.override_value(leg_tuples[1], -1); - } else if (leg_tuples.Count == 3) { - _is_setting_price_mults = true; - _price_mult_model.override_value(leg_tuples[1], -2); - } + } } void _deduce_quantities() { if (leg_tuples.Count == 2) { _is_setting_quantities = true; _quantity_model.override_value(leg_tuples[1], -1); - } else if (leg_tuples.Count == 3) { - _is_setting_quantities = true; - _quantity_model.override_value(leg_tuples[1], -2); } } diff --git a/src/client/spark/Modules/Trading/AlgoStudio/AlgoStudioWindow.cs b/src/client/spark/Modules/Trading/AlgoStudio/AlgoStudioWindow.cs index 9f0ec8c59..bcc023fdd 100644 --- a/src/client/spark/Modules/Trading/AlgoStudio/AlgoStudioWindow.cs +++ b/src/client/spark/Modules/Trading/AlgoStudio/AlgoStudioWindow.cs @@ -63,7 +63,10 @@ namespace mbts.spark { _vm = new AlgoStudioVM(_model); _vm.load_from(_state); _view = new AlgoStudioView { DataContext = _vm }; - _resources = new CompositeDisposable(); + _resources = new CompositeDisposable( + _bus.subscribe(SparkTags.SecuritiesTag) + .OnDispatcher() + .Subscribe(sec_map => _view.Resources[ResourceKeys.Securities] = sec_map)); // add definition _commands.Add(new SparkCommandCommand("Add Algo", Icons.AddBoxes, _vm.add_definition_cmd)); _commands.Add(new SparkCommandCommand("Clone Algo", Icons.CopyBoxes, _vm.clone_selected_definition_cmd)); diff --git a/src/client/spark/Modules/Trading/AlgoStudio/VM/AlgoParamRowVM.cs b/src/client/spark/Modules/Trading/AlgoStudio/VM/AlgoParamRowVM.cs index e7ebaa288..8d1297899 100644 --- a/src/client/spark/Modules/Trading/AlgoStudio/VM/AlgoParamRowVM.cs +++ b/src/client/spark/Modules/Trading/AlgoStudio/VM/AlgoParamRowVM.cs @@ -13,6 +13,9 @@ namespace mbts.spark { string _template_key; ICommand _set_value_cmd; + public AlgoParamRowVM(IAlgoVM algo, IAlgoParamVM param, AlgoParamRowVM parent) + : this(algo, param, parent, (param as ITemplateSelectorKey)?.key) { } + public AlgoParamRowVM(IAlgoVM algo, IAlgoParamVM param, AlgoParamRowVM parent, string template_key = null) { this.param = param; this.algo = algo; diff --git a/src/client/spark/Modules/Trading/AlgoStudio/View/AlgoEditorGridView/AlgoEditorGridView.xaml b/src/client/spark/Modules/Trading/AlgoStudio/View/AlgoEditorGridView/AlgoEditorGridView.xaml index ce29fd798..cabe66d50 100644 --- a/src/client/spark/Modules/Trading/AlgoStudio/View/AlgoEditorGridView/AlgoEditorGridView.xaml +++ b/src/client/spark/Modules/Trading/AlgoStudio/View/AlgoEditorGridView/AlgoEditorGridView.xaml @@ -247,6 +247,31 @@ + + + + + + + + + + + + + + + + + + + + + + + @@ -320,6 +345,21 @@ + + + + + diff --git a/src/client/spark/Modules/Trading/Fills/FillsVM.cs b/src/client/spark/Modules/Trading/Fills/FillsVM.cs index 8509914e1..e370685ea 100644 --- a/src/client/spark/Modules/Trading/Fills/FillsVM.cs +++ b/src/client/spark/Modules/Trading/Fills/FillsVM.cs @@ -125,6 +125,7 @@ namespace mbts.spark { add_col(Columns.Size, DefaultColumnHeaders.Size, r => r.size.ToString(), new DoubleMinMax(r => r.fill.LastShares), DataGridFilterKnownTemplates.UIntMinMax, DefaultColumnWidths.Size); add_col(Columns.Price, DefaultColumnHeaders.Price, r => r.format_price(r.price), new DoubleMinMax(r => r.fill.LastPrice), DataGridFilterKnownTemplates.DoubleMinMax, DefaultColumnWidths.Price); add_col(Columns.FillKind, DefaultColumnHeaders.FillKind, r => r.fill_kind.ToString(), dv_filter(EnumHelper.get_values().ToArray(), r => r.fill.FillKind), DataGridFilterKnownTemplates.DiscreteValues, DefaultColumnWidths.FillKind); + add_col(Columns.FillStatus, DefaultColumnHeaders.FillStatus, r => r.fill_status.ToString(), dv_filter(EnumHelper.get_values().ToArray(), r => r.fill.FillStatus), DataGridFilterKnownTemplates.DiscreteValues, DefaultColumnWidths.FillKind); add_col(Columns.FillKey, DefaultColumnHeaders.FillKey, r => r.fill_key.ToString(), new RegexFilterVM(r => r.fill.FillKey), DataGridFilterKnownTemplates.Regex, DefaultColumnWidths.FillKey); add_col(Columns.OrderKey, DefaultColumnHeaders.OrderKey, r => r.order_key.ToString(), new RegexFilterVM(r => r.fill.OrderKey.ToString()), DataGridFilterKnownTemplates.Regex, DefaultColumnWidths.OrderKey); add_col(Columns.SecurityKey, DefaultColumnHeaders.SecurityKey, r => r.security_key.ToString(), new RegexFilterVM(r => r.fill.SecurityKey.ToString()), DataGridFilterKnownTemplates.Regex, DefaultColumnWidths.SecurityKey); @@ -228,6 +229,7 @@ namespace mbts.spark { case Columns.Exchange: return wrap_default(ft => ft.security.exchange_or_default()); case Columns.FillKey: return wrap(ft => ft.fill.FillKey); case Columns.FillKind: return wrap_default(ft => ft.fill.FillKind); + case Columns.FillStatus: return wrap_default(ft => ft.fill.FillStatus); case Columns.FillTime: return wrap(ft => ft.fill.Timestamp.to_local_time()); case Columns.OrderKey: return wrap(ft => ft.fill.OrderKey); case Columns.OrderType: return wrap_default(ft => ft.order?.order_type() ?? OrderType.None); @@ -283,7 +285,6 @@ namespace mbts.spark { public DateTime fill_time => _fill.Timestamp.to_local_time(); public TimeInForce time_in_force => _order.time_in_force(); public OrderType order_type => _order.order_type(); - //public OrderType root_algo_type => _or public string display_name => OrderUtils.display_name(_order, fill_tuple.algo); public OrderMetadata order_metadata => _order_metadata ?? (_order_metadata = _model.factory.get_metadata(_order.Def.CustomDef.CustomCase)); public Brush badge_fill => _badge_fill ?? (_badge_fill = OrderUtils.badge_fill(fill_tuple.algo_meta, order_metadata)); @@ -294,6 +295,7 @@ namespace mbts.spark { public double price => _fill.LastPrice; public double size => _fill.LastShares; public FillKind fill_kind => _fill.FillKind; + public FillStatus fill_status => _fill.FillStatus; public string fill_key => _fill.FillKey; public string order_key => _order.Key.ToString(); @@ -301,7 +303,7 @@ namespace mbts.spark { public OrderType root_algo_order_type => _root_algo.HasValue ? _root_algo.Value.value.CustomCase : OrderType.None; public OrderMetadata root_algo_order_metadata => _model.factory.get_metadata(root_algo_order_type); public Color root_algo_badge_color => _root_algo_badge_color ?? (_root_algo_badge_color = OrderUtils.badge_color(_root_algo_meta, root_algo_order_metadata)).Value; - public string root_algo_display_name => _root_algo?.name ?? root_algo_order_type.display_name(); + public string root_algo_display_name => fill_tuple.root_order_security?.display_name() ?? OrderType.None.display_name(); public bool Equals(Row other) => other != null && other._fill.Equals(_fill) && other._order.Equals(_order); public override bool Equals(object obj) => Equals(obj as Row); diff --git a/src/client/spark/Modules/Trading/Fills/FillsView.xaml.cs b/src/client/spark/Modules/Trading/Fills/FillsView.xaml.cs index 197e97ea5..9e34bc3e9 100644 --- a/src/client/spark/Modules/Trading/Fills/FillsView.xaml.cs +++ b/src/client/spark/Modules/Trading/Fills/FillsView.xaml.cs @@ -21,6 +21,7 @@ namespace mbts.ui.spark { Price, Size, FillKind, + FillStatus, FillKey, OrderKey, SecurityKey diff --git a/src/client/spark/Modules/Trading/Fills/FillsWindowFactory.cs b/src/client/spark/Modules/Trading/Fills/FillsWindowFactory.cs index e89aa70a0..1f3c73ce7 100644 --- a/src/client/spark/Modules/Trading/Fills/FillsWindowFactory.cs +++ b/src/client/spark/Modules/Trading/Fills/FillsWindowFactory.cs @@ -37,7 +37,8 @@ namespace bts.shell { { nameof(FillsVM.Row.side), Columns.Side }, { nameof(FillsVM.Row.size), Columns.Size }, { nameof(FillsVM.Row.time_in_force), Columns.TimeInForce }, - { nameof(FillsVM.Row.user), Columns.User } + { nameof(FillsVM.Row.user), Columns.User }, + { nameof(FillsVM.Row.fill_status ), Columns.FillStatus }, }; public FillsWindowFactory(TradingShellModule module, IBus bus, UserSettingsHub settings_hub, OrdersModel orders_model) diff --git a/src/client/spark/Modules/Trading/Fills/Settings/FillsFilterStates.cs b/src/client/spark/Modules/Trading/Fills/Settings/FillsFilterStates.cs index b09603871..45077ef1d 100644 --- a/src/client/spark/Modules/Trading/Fills/Settings/FillsFilterStates.cs +++ b/src/client/spark/Modules/Trading/Fills/Settings/FillsFilterStates.cs @@ -74,6 +74,10 @@ namespace bts.shell.fills { [ProtoMember(15)] [DIYGridColumnID(nameof(Columns.Symbol))] public string[] symbol { get; set; } + + [ProtoMember(17)] + [DIYGridColumnID(nameof(Columns.FillStatus))] + public Fill.Types.FillStatus[] fill_status { get; set; } } static class FillsFilterStatesExtensions { @@ -96,6 +100,7 @@ namespace bts.shell.fills { pb.add_discrete_values(fs.side, ft => ft.fill.Side); pb.add_discrete_values(fs.fill_kind, ft => ft.fill.FillKind); pb.add_discrete_values(fs.symbol, ft => ft.security?.Symbol ?? string.Empty); + pb.add_discrete_values(fs.fill_status, ft => ft.fill.FillStatus); // numeric pb.add_numeric(fs.size, ft => ft.fill.LastShares); pb.add_numeric(fs.price, ft => ft.fill.LastPrice); diff --git a/src/client/spark/Modules/Trading/Ladder/LadderSettings.cs b/src/client/spark/Modules/Trading/Ladder/LadderSettings.cs index e1222b5af..30a1059d7 100644 --- a/src/client/spark/Modules/Trading/Ladder/LadderSettings.cs +++ b/src/client/spark/Modules/Trading/Ladder/LadderSettings.cs @@ -23,6 +23,10 @@ namespace mbts.spark { // TODO: somehow allow windows that are using the default settings to still have individual column widths. [ProtoContract] class LadderColumnSettings : IEquatable { + // this class is 100% mutable (there are literally 0 readonly members), + // so GetHashCode must return the same hash for every instance. + static readonly int _type_hash = typeof(LadderColumnSettings).GetHashCode(); + public static LadderColumnSettings Default => new LadderColumnSettings { show = true, width = 50, @@ -74,14 +78,7 @@ namespace mbts.spark { other.show == show; public override bool Equals(object obj) => Equals(obj as LadderColumnSettings); - - public override int GetHashCode() => - width.GetHashCode() + - background.GetHashCode() + - foreground.GetHashCode() + - font_family.GetHashCode() + - font_size.GetHashCode() + - font_weight.GetHashCode(); + public override int GetHashCode() => _type_hash; public static bool operator ==(LadderColumnSettings x, LadderColumnSettings y) => x.safe_equals(y); public static bool operator !=(LadderColumnSettings x, LadderColumnSettings y) => !(x == y); @@ -89,6 +86,9 @@ namespace mbts.spark { [ProtoContract] class HistogramBarAnimation : IEquatable { + // this class is 100% mutable (there are literally 0 readonly members), + // so GetHashCode must return the same hash for every instance. + static readonly int _type_hash = typeof(HistogramBarAnimation).GetHashCode(); [ProtoMember(1)] public ColorShowProto bars { get; set; } [ProtoMember(2)] @@ -102,14 +102,14 @@ namespace mbts.spark { show_numbers == other.show_numbers; public override bool Equals(object obj) => Equals(obj as HistogramBarAnimation); - public override int GetHashCode() => - EqualityComparer.aggregate_hash( - bars.GetHashCode(), - show_numbers.GetHashCode()); + public override int GetHashCode() => _type_hash; } [ProtoContract] class VolumeColumnSettings : IEquatable { + // this class is 100% mutable (there are literally 0 readonly members), + // so GetHashCode must return the same hash for every instance. + static readonly int _type_hash = typeof(VolumeColumnSettings).GetHashCode(); public static VolumeColumnSettings Default { get { var gen = LadderColumnSettings.Default; @@ -149,19 +149,14 @@ namespace mbts.spark { show_ltq.Equals(other.show_ltq); public override bool Equals(object obj) => Equals(obj as VolumeColumnSettings); - - public override int GetHashCode() => - EqualityComparer.aggregate_hash( - general.GetHashCode(), - buy.GetHashCode(), - sell.GetHashCode(), - passive.GetHashCode(), - total.GetHashCode(), - show_ltq.GetHashCode()); + public override int GetHashCode() => _type_hash; } [ProtoContract] class LadderMarketBreakLine : IEquatable { + // this class is 100% mutable (there are literally 0 readonly members), + // so GetHashCode must return the same hash for every instance. + static readonly int _type_hash = typeof(LadderMarketBreakLine).GetHashCode(); [ProtoMember(1)] public bool show { get; set; } [ProtoMember(2)] @@ -184,8 +179,7 @@ namespace mbts.spark { Equals(obj as LadderMarketBreakLine); public override int GetHashCode() => - show.GetHashCode() + - (int)color; + _type_hash; } [ProtoContract] @@ -255,6 +249,12 @@ namespace mbts.spark { const double _default_order_entry_panel_width = 110.0; const double _default_zoom_factor = 1.0; const bool _default_show_warn_on_cross = true; + const bool _default_enforce_max_clip = false; + public static readonly IEqualityComparer profile_equality_comparer = + EqualityComparer.Create(profile_equals); + // this class is 100% mutable (there are literally 0 readonly members), + // so GetHashCode must return the same hash for every instance. + static readonly int _type_hash = typeof(LadderSettings).GetHashCode(); // Default proto constructor // Added this so that when clients upgrade to a version with new settings, @@ -283,6 +283,7 @@ namespace mbts.spark { use_defaults = true; favorite_algos = new LadderFavoriteAlgosSettings(null, 2, null); show_warn_on_cross = _default_show_warn_on_cross; + enforce_max_clip = _default_enforce_max_clip; } public static LadderSettings Default { @@ -348,6 +349,8 @@ namespace mbts.spark { zoom_factor = _default_zoom_factor, net_change = NetChangeTerms.None, mini_ob = new LadderMiniOrderBookSettings(bts.shell.orderbook.OrderBookStartupData.default_mini_grid.general), + max_clip = 0, + price_display_format = PriceDisplayFormat.Default, }; rv.row_height = LadderRender.measure_row_height(rv); return rv; @@ -366,6 +369,13 @@ namespace mbts.spark { Ticks } + public enum PriceDisplayFormat { + [Description("Default")] + Default, + [Description("Integer")] + Integer + } + [ProtoMember(1)] public double row_height { get; set; } [ProtoMember(2)] @@ -497,6 +507,18 @@ namespace mbts.spark { [ProtoMember(55)] public LadderMiniOrderBookSettings mini_ob { get; set; } + [ProtoMember(57)] + public double left_panel_scroll_position { get; set; } + + [ProtoMember(58), DefaultValue(_default_enforce_max_clip)] + public bool enforce_max_clip { get; set; } + + [ProtoMember(59)] + public uint max_clip { get; set; } + + [ProtoMember(60)] + public PriceDisplayFormat price_display_format { get; set; } + public void sanitize() => sanitize(this); @@ -533,112 +555,84 @@ namespace mbts.spark { return s; } + /// + /// Determines whether or not the specified LadderSettings are 'profile equivalent.' + /// This means that 2 ladders, each with a different one of the specified settings, could share the same profile. + /// + /// + /// Note: the impetus for this version of equality is to avoid disconnecting a ladder from its profile due to 'trivial' settings changes. + /// Consider the '' property: + /// If a ladder is using a settings profile with Limit as the default algo, and the user decides to send a 'Market' order, + /// the ladder would suddenly be disconnected from the profile, because it's settings state would no longer be equal to the profile's settings, + /// since they changed the selected algo type to 'Market.' + /// To avoid such travesties, a separate equals logic is needed: this is called 'profile-equivalent' and is implemented in . + /// Devs: be sure to account for properties that are excluded in the 'profile-equals' logic in the [regular]'equals' logic. + public static bool profile_equals(LadderSettings a, LadderSettings b) => + a == null ? b == null : + b != null && + a.row_height == b.row_height && + a.grid_line_color == b.grid_line_color && + a.orders_column.safe_equals(b.orders_column) && + a.bid_column.safe_equals(b.bid_column) && + a.ask_column.safe_equals(b.ask_column) && + a.price_column.safe_equals(b.price_column) && + a.last_column.safe_equals(b.last_column) && + a.piq_column.safe_equals(b.piq_column) && + a.calculator_buttons.safe_equals(b.calculator_buttons, Enumerable.SequenceEqual) && + a.scroll_bar_visible == b.scroll_bar_visible && + a.default_account == b.default_account && + a.default_time_in_force == b.default_time_in_force && + a.size == b.size && + a.animate_recenter == b.animate_recenter && + a.badge_style == b.badge_style && + a.auto_clear == b.auto_clear && + a.bid_break_line.safe_equals(b.bid_break_line) && + a.ask_break_line.safe_equals(b.ask_break_line) && + a.mid_break_line.safe_equals(b.mid_break_line) && + a.alternate_order_size == b.alternate_order_size && + a.session_low_line.safe_equals(b.session_low_line) && + a.session_high_line.safe_equals(b.session_high_line) && + a.session_range_fill.safe_equals(b.session_range_fill) && + a.volume_column.safe_equals(b.volume_column) && + a.size_display.safe_equals(b.size_display) && + a.day_visible == b.day_visible && + a.high_visible == b.high_visible && + a.low_visible == b.low_visible && + a.open_visible == b.open_visible && + a.settle_visible == b.settle_visible && + a.volume_visible == b.volume_visible && + a.account_visible == b.account_visible && + a.tif_visible == b.tif_visible && + a.order_type_visible == b.order_type_visible && + a.account_position_visible == b.account_position_visible && + a.day_position_visible == b.day_position_visible && + a.raw_day_position_visible == b.raw_day_position_visible && + a.size_presets_visible == b.size_presets_visible && + a.cancel_buys_visible == b.cancel_buys_visible && + a.cancel_sells_visible == b.cancel_sells_visible && + a.cancel_all_visible == b.cancel_all_visible && + a.auto_clear_visible == b.auto_clear_visible && + a.clear_visible == b.clear_visible && + a.order_entry_panel_initial_width == b.order_entry_panel_initial_width && + a.zoom_slider_visible == b.zoom_slider_visible && + a.zoom_factor == b.zoom_factor && + a.use_defaults == b.use_defaults && + a.favorite_algos == b.favorite_algos && + a.net_change == b.net_change && + a.show_warn_on_cross == b.show_warn_on_cross && + a.mini_ob == b.mini_ob && + a.enforce_max_clip == b.enforce_max_clip && + a.max_clip == b.max_clip && + a.price_display_format == b.price_display_format; + public bool Equals(LadderSettings other) => - other != null && - row_height == other.row_height && - grid_line_color == other.grid_line_color && - orders_column.safe_equals(other.orders_column) && - bid_column.safe_equals(other.bid_column) && - ask_column.safe_equals(other.ask_column) && - price_column.safe_equals(other.price_column) && - last_column.safe_equals(other.last_column) && - piq_column.safe_equals(other.piq_column) && - calculator_buttons.safe_equals(other.calculator_buttons, (a, b) => a.SequenceEqual(b)) && - scroll_bar_visible == other.scroll_bar_visible && - default_account == other.default_account && + profile_equals(this, other) && default_algo == other.default_algo && - default_time_in_force == other.default_time_in_force && - size == other.size && - animate_recenter == other.animate_recenter && - badge_style == other.badge_style && - auto_clear == other.auto_clear && - bid_break_line.safe_equals(other.bid_break_line) && - ask_break_line.safe_equals(other.ask_break_line) && - mid_break_line.safe_equals(other.mid_break_line) && - alternate_order_size == other.alternate_order_size && - session_low_line.safe_equals(other.session_low_line) && - session_high_line.safe_equals(other.session_high_line) && - session_range_fill.safe_equals(other.session_range_fill) && - volume_column.safe_equals(other.volume_column) && - size_display.safe_equals(other.size_display) && - day_visible == other.day_visible && - high_visible == other.high_visible && - low_visible == other.low_visible && - open_visible == other.open_visible && - settle_visible == other.settle_visible && - volume_visible == other.volume_visible && - account_visible == other.account_visible && - tif_visible == other.tif_visible && - order_type_visible == other.order_type_visible && - account_position_visible == other.account_position_visible && - day_position_visible == other.day_position_visible && - raw_day_position_visible == other.raw_day_position_visible && - size_presets_visible == other.size_presets_visible && - cancel_buys_visible == other.cancel_buys_visible && - cancel_sells_visible == other.cancel_sells_visible && - cancel_all_visible == other.cancel_all_visible && - auto_clear_visible == other.auto_clear_visible && - clear_visible == other.clear_visible && - order_entry_panel_initial_width == other.order_entry_panel_initial_width && - zoom_slider_visible == other.zoom_slider_visible && - zoom_factor == other.zoom_factor && - use_defaults == other.use_defaults && - favorite_algos == other.favorite_algos && - net_change == other.net_change && - show_warn_on_cross == other.show_warn_on_cross && - mini_ob == other.mini_ob; + left_panel_scroll_position == other.left_panel_scroll_position; public override bool Equals(object obj) => Equals(obj as LadderSettings); - // TODO: remove mutable props from hash logic (or conversely, make them immutable). - public override int GetHashCode() => - EqualityComparer.aggregate_hash( - row_height.GetHashCode(), - grid_line_color.GetHashCode(), - EqualityComparer.safe_get_hash_code(orders_column), - EqualityComparer.safe_get_hash_code(bid_column), - EqualityComparer.safe_get_hash_code(ask_column), - EqualityComparer.safe_get_hash_code(price_column), - EqualityComparer.safe_get_hash_code(last_column), - EqualityComparer.safe_get_hash_code(piq_column), - EqualityComparer.safe_get_hash_code(calculator_buttons), - scroll_bar_visible.GetHashCode(), - default_time_in_force.GetHashCode(), - size.GetHashCode(), - animate_recenter.GetHashCode(), - badge_style.GetHashCode(), - EqualityComparer.safe_get_hash_code(bid_break_line), - EqualityComparer.safe_get_hash_code(ask_break_line), - EqualityComparer.safe_get_hash_code(mid_break_line), - alternate_order_size.GetHashCode(), - EqualityComparer.safe_get_hash_code(session_low_line), - EqualityComparer.safe_get_hash_code(session_high_line), - EqualityComparer.safe_get_hash_code(session_range_fill), - EqualityComparer.safe_get_hash_code(volume_column), - EqualityComparer.safe_get_hash_code(size_display), - day_visible.GetHashCode(), - high_visible.GetHashCode(), - low_visible.GetHashCode(), - open_visible.GetHashCode(), - settle_visible.GetHashCode(), - volume_visible.GetHashCode(), - account_visible.GetHashCode(), - tif_visible.GetHashCode(), - order_type_visible.GetHashCode(), - account_position_visible.GetHashCode(), - size_presets_visible.GetHashCode(), - cancel_buys_visible.GetHashCode(), - cancel_sells_visible.GetHashCode(), - cancel_all_visible.GetHashCode(), - auto_clear_visible.GetHashCode(), - clear_visible.GetHashCode(), - order_entry_panel_initial_width.GetHashCode(), - zoom_slider_visible.GetHashCode(), - zoom_factor.GetHashCode(), - use_defaults.GetHashCode(), - favorite_algos.GetHashCode(), - net_change.GetHashCode(), - show_warn_on_cross.GetHashCode()); + public override int GetHashCode() => _type_hash; public struct WindowSpecifics { public readonly double order_entry_panel_width, zoom_factor; diff --git a/src/client/spark/Modules/Trading/Ladder/LadderWindowFactory.cs b/src/client/spark/Modules/Trading/Ladder/LadderWindowFactory.cs index 0d1b3ac40..2f3b64e92 100644 --- a/src/client/spark/Modules/Trading/Ladder/LadderWindowFactory.cs +++ b/src/client/spark/Modules/Trading/Ladder/LadderWindowFactory.cs @@ -81,11 +81,9 @@ namespace bts.shell { if (!_settings_hub.current.ladder_profiles.TryGetFirst(lp => lp.id == id, out var result)) { // profile was deleted id = 0; - } else { - if (!s.Equals(result.value)) { - // ladder's settings diverged from the profile (this happens when the user changes settings outside of the settings dialog, such as column widths, left panel width, etc.) - id = 0; - } + } else if (!LadderSettings.profile_equals(s, result.value)) { + // ladder's settings diverged from the profile (this happens when the user changes settings outside of the settings dialog, such as column widths, left panel width, etc.) + id = 0; } } return new LadderSaveState { security_key = vm.security.Id, settings = s, settings_id = id }; @@ -149,7 +147,7 @@ namespace bts.shell { var curr = vm.get_save_state(); if (settings_id == 0) { last_value = curr; - } else if (!curr.Equals(last_value)) { + } else if (!LadderSettings.profile_equals(curr, last_value)) { settings_id = 0; last_value = curr; } @@ -163,7 +161,7 @@ namespace bts.shell { // not on a profile, don't respond to default settings changes return; } else if (us.ladder_profiles.TryGetFirst(p => p.id == settings_id, out var profile)) { - if (!last_value.Equals(vm.get_save_state())) { + if (!LadderSettings.profile_equals(last_value, vm.get_save_state())) { settings_id = 0; } else { settings_subject.OnNext(last_value = profile.value); @@ -211,6 +209,7 @@ namespace bts.shell { _ => _, vm.get_save_state(), UserSettings.FileExtensions.LADDER, + LadderSettings.profile_equals, settings_id); dialog_vm.applied += apply; // dialog diff --git a/src/client/spark/Modules/Trading/Ladder/VM/LadderSettingsVM.cs b/src/client/spark/Modules/Trading/Ladder/VM/LadderSettingsVM.cs index 68fffdeb8..22d2a75b5 100644 --- a/src/client/spark/Modules/Trading/Ladder/VM/LadderSettingsVM.cs +++ b/src/client/spark/Modules/Trading/Ladder/VM/LadderSettingsVM.cs @@ -29,7 +29,7 @@ using TextUtils = mbts.ui.utility.text.TextUtils; namespace mbts.spark { class LadderSettingsVM : DisposableViewModelBase, IWindowSettingsVM, IValueFactory, IObservable, ITemplateSelectorKey { readonly Property _badge_style; - readonly Property _alternate_order_size; + readonly Property _alternate_order_size, _max_clip; readonly DelegateProperty _row_height; readonly UserProperty _default_account; readonly UserProperty _default_algo; @@ -41,8 +41,9 @@ namespace mbts.spark { AlgoID? _desired_algo; TimeInForce _default_time_in_force; LadderSettings.NetChangeTerms _price_format; + LadderSettings.PriceDisplayFormat _price_display_format; double _order_entry_panel_width, _zoom_factor; - bool _auto_clear, _show_warn_on_cross; + bool _auto_clear, _show_warn_on_cross, _enforce_max_clip; LadderSettings _current; LadderColumnSettingsVM _orders = new OrdersColumnSettingsVM(), @@ -91,6 +92,7 @@ namespace mbts.spark { _alternate_order_size = backing(nameof(alternate_order_size)); _default_account = user_backing(nameof(default_account)); _default_algo = user_backing(nameof(default_algo)); + _max_clip = backing(nameof(max_clip)); // be sure to initialize backings before this measure_row_height call _row_height = backing(nameof(row_height), () => LadderRender.measure_row_height(get_result())); _orders.font_size_changed += on_font_size_changed; @@ -166,7 +168,9 @@ namespace mbts.spark { public SimpleGridSettingsVM mini_ob { get; } = new SimpleGridSettingsVM(OrderBookGridSettingsManager.hard_column_flags); // price format public LadderSettings.NetChangeTerms price_format { get => _price_format; set => Set(ref _price_format, value); } + public LadderSettings.PriceDisplayFormat price_display_format { get => _price_display_format; set => Set(ref _price_display_format, value); } public IEnumerable price_format_options => EnumHelper.get_values(); + public IEnumerable price_display_format_options => EnumHelper.get_values(); // time in force public TimeInForce default_time_in_force { get => _default_time_in_force; set => Set(ref _default_time_in_force, value); } public IEnumerable time_in_force_options => EnumHelper.get_values_except(TimeInForce.Undefined); @@ -182,6 +186,8 @@ namespace mbts.spark { public double zoom_factor { get => _zoom_factor; set => Set(ref _zoom_factor, value); } // safety checks public bool show_warn_on_cross { get => _show_warn_on_cross; set => Set(ref _show_warn_on_cross, value); } + public bool enforce_max_clip { get => _enforce_max_clip; set => Set(ref _enforce_max_clip, value); } + public uint max_clip { get => _max_clip.Value; set => _max_clip.Value = value; } // favorites public FavoriteAlgosVM favorite_algos_vm { get; } @@ -354,8 +360,11 @@ namespace mbts.spark { _cancel_sells_visibility.show = settings.cancel_sells_visible; _cancel_all_visibility.show = settings.cancel_all_visible; price_format = settings.net_change; + price_display_format = settings.price_display_format; // safety checks show_warn_on_cross = settings.show_warn_on_cross; + enforce_max_clip = settings.enforce_max_clip; + max_clip = settings.max_clip; } } @@ -428,8 +437,11 @@ namespace mbts.spark { clone.cancel_all_visible = _cancel_all_visibility.show; clone.zoom_slider_visible = _zoom_slider_visbility.show; clone.net_change = price_format; + clone.price_display_format = price_display_format; // safety checks clone.show_warn_on_cross = show_warn_on_cross; + clone.enforce_max_clip = enforce_max_clip; + clone.max_clip = max_clip; return clone; } diff --git a/src/client/spark/Modules/Trading/Ladder/VM/LadderVM.cs b/src/client/spark/Modules/Trading/Ladder/VM/LadderVM.cs index d5d1c38da..ca85b7b7f 100644 --- a/src/client/spark/Modules/Trading/Ladder/VM/LadderVM.cs +++ b/src/client/spark/Modules/Trading/Ladder/VM/LadderVM.cs @@ -35,7 +35,14 @@ namespace mbts.spark { _show_cancel_sells, _show_cancel_all, _show_zoom; readonly PropertyAutoWrapper _bid_background, _bid_foreground, _ask_background, _ask_foreground; readonly HashSet _sticky_algos = new HashSet(); - double _position, _day_position, _raw_day_position; + double + _position, + _day_position, + _raw_day_position, + _left_panel_extent_height, + _left_panel_view_port_height, + _left_panel_scroll_position, + _left_panel_scroll_position_target; bool _show_account_position, _show_day_position, _show_raw_day_position; ulong _misc_time = 0; ulong _last_trade_time = 0; @@ -76,6 +83,7 @@ namespace mbts.spark { _volume_column_width = backing(nameof(volume_column_width), ws.volume_column_width); _left_panel_width = backing(nameof(left_panel_width), ws.order_entry_panel_width); _zoom_factor = backing(nameof(zoom_factor), ws.zoom_factor); + _left_panel_scroll_position_target = init.left_panel_scroll_position; // remember: account_positions needs to subscribe to selected_account; both the sub and account_positions need to be disposed _volume = backing(nameof(volume), 0ul); _auto_clear = backing(nameof(auto_clear), init.auto_clear); @@ -333,6 +341,33 @@ namespace mbts.spark { public bool show_cancel_sells => _show_cancel_sells.view; public bool show_cancel_all => _show_cancel_all.view; public bool show_zoom => _show_zoom.view; + // left panel scroll props + public double left_panel_view_port_height { + get => _left_panel_view_port_height; + set { + using (detect_property_changes(nameof(left_panel_scroll_position))) + _left_panel_view_port_height = value; + } + } + public double left_panel_scroll_position { + get { + if (!_left_panel_scroll_position_target.is_nan()) { + if (_left_panel_scroll_position_target < _max_left_panel_scroll_position) { + _left_panel_scroll_position = _left_panel_scroll_position_target; + _left_panel_scroll_position_target = double.NaN; + } + } + return Math.Min(_left_panel_scroll_position, _left_panel_extent_height); + } + set => Set(ref _left_panel_scroll_position, value); + } + public double left_panel_extent_height { + get => _left_panel_extent_height; + set { + using (detect_property_changes(nameof(left_panel_scroll_position))) + Set(ref _left_panel_extent_height, value); + } + } public double left_panel_width { get => _left_panel_width.Value; set => _left_panel_width.set(value); } public double zoom_factor { get => _zoom_factor.Value; set => _zoom_factor.set(value); } public string position_display_text { @@ -362,8 +397,14 @@ namespace mbts.spark { public uint volume_column_width { get => _volume_column_width.Value; set => _volume_column_width.set(value); } public uint alternate_order_size_from_settings => current_settings.alternate_order_size; public bool show_warn_on_cross_from_settings => current_settings.show_warn_on_cross; + public bool enforce_max_clip_from_settings => current_settings.enforce_max_clip; + public uint max_clip_from_settings => current_settings.max_clip; internal LadderModel model => _model; internal IObservable settings { get; } + + double _max_left_panel_scroll_position => + Math.Max(0, _left_panel_extent_height - _left_panel_view_port_height); + internal OrderRecordImmutable get_root_at_price((OrderRecordImmutable order, uint? _, AlgoData? __, AlgoDefinitionMetadata? meta) op) { var order = op.order; double p = order.price(); @@ -394,6 +435,7 @@ namespace mbts.spark { zoom_factor, piq_column_width)); rv.auto_clear = auto_clear; + rv.left_panel_scroll_position = left_panel_scroll_position; if (order_properties_vm != null) { rv.default_time_in_force = order_properties_vm.selected_time_in_force; if (order_properties_vm.selected_account.HasValue) @@ -407,6 +449,9 @@ namespace mbts.spark { public void key_down(KeyEventArgs e) { if (order_properties_vm == null) return; + if (KeyboardExtensions.IsDown(Key.Space)) { + _bus?.post(SparkTags.RequestRecenterLadder, null); + } // the ctrl modifier requirement helps avoid keyboard-input collisions w/ algo-templates popup if (!KeyboardExtensions.IsCtrlDown()) return; var new_algo = order_properties_vm.algo_definitions.FirstOrDefault(o => o.algo_root.key_match(e)); @@ -422,6 +467,10 @@ namespace mbts.spark { settings_invalidated?.Invoke(); } + public void recenter_all_ladders() { + _bus?.post(SparkTags.RequestRecenterLadder, null); + } + public Task send_order_async((double price, Side? side, MouseButton button) args) { if (order_properties_vm != null && args.button != MouseButton.Middle) { var size_from_alternate = args.button == MouseButton.Right; @@ -430,6 +479,10 @@ namespace mbts.spark { size_calculator.flash_error(); } else { uint size = size_from_alternate ? current_settings.alternate_order_size : (uint)Math.Abs(size_calculator.value ?? 0); + // enforce max clip + if (enforce_max_clip_from_settings && size > max_clip_from_settings) { + size = max_clip_from_settings; + } return send_order_async(args.side.Value, size, args.price); } } diff --git a/src/client/spark/Modules/Trading/Ladder/View/LadderGridHost.cs b/src/client/spark/Modules/Trading/Ladder/View/LadderGridHost.cs index b78519f62..7a51bc6d2 100644 --- a/src/client/spark/Modules/Trading/Ladder/View/LadderGridHost.cs +++ b/src/client/spark/Modules/Trading/Ladder/View/LadderGridHost.cs @@ -350,6 +350,17 @@ namespace mbts.spark { } } else if (_vm != null) { uint size = e.ChangedButton == MouseButton.Right ? _vm.alternate_order_size_from_settings : _vm.size_calculator.value ?? 0; + + // does the size violate the local max clip + if (_vm.enforce_max_clip_from_settings && size > _vm.max_clip_from_settings) { + // note this size is only used for the cross market message, not the actual order command + size = _vm.max_clip_from_settings; + // only update the calculator value when it's a standard left-click order + if (e.ChangedButton == MouseButton.Left) { + _vm.size_calculator.value = _vm.max_clip_from_settings; + } + } + // if warning enabled && (buy above opposite inside market || ask below opposite inside market) // when 1-sided market, _get_top_price will return NaN, and so the price comparison is false // meaning show no warning == correct @@ -479,8 +490,14 @@ namespace mbts.spark { protected override void OnMouseDoubleClick(MouseButtonEventArgs e) { var column = _column_for_point(e.GetPosition(this)); - if (column.column_type == ColumnType.Price) - _scroll_to_center(); + if (column.column_type == ColumnType.Price) { + if (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift) || + Keyboard.IsKeyDown(Key.LeftAlt) || Keyboard.IsKeyDown(Key.RightAlt)) { + _vm?.recenter_all_ladders(); + } else { + _scroll_to_center(); + } + } } #region Row Utility Methods @@ -588,7 +605,8 @@ namespace mbts.spark { _settings.row_height, _price_list, column.settings, - security.ContractSpec); + security.ContractSpec, + _settings.price_display_format); } break; case ColumnType.Ask: render_market_col(_asks); break; @@ -911,7 +929,7 @@ namespace mbts.spark { var prev_price_list = _price_list; _price_list = (!(_settings.net_change == LadderSettings.NetChangeTerms.None)) ? LadderRender.render_prices(_price_vis_tup.visual, _price_vis_tup.glyph_context, _mkt_break_lines.origin, _vm.md.settlement_price, _settings.net_change == LadderSettings.NetChangeTerms.Ticks, _row_count, _settings.row_height, _settings.price_column, security.ContractSpec) : - LadderRender.render_prices(_price_vis_tup.visual, _price_vis_tup.glyph_context, _mkt_break_lines.origin, _row_count, _settings.row_height, _settings.price_column, security.ContractSpec); + LadderRender.render_prices(_price_vis_tup.visual, _price_vis_tup.glyph_context, _mkt_break_lines.origin, _row_count, _settings.row_height, _settings.price_column, security.ContractSpec, _settings.price_display_format); _ordershost.update_settings(_price_list, _price_comparer, _settings.row_height, _settings.orders_column); if (prev_price_list != null) { // maintain previous visible price range if possible diff --git a/src/client/spark/Modules/Trading/Ladder/View/LadderRender.cs b/src/client/spark/Modules/Trading/Ladder/View/LadderRender.cs index d12c79a6c..c6d5e6a2e 100644 --- a/src/client/spark/Modules/Trading/Ladder/View/LadderRender.cs +++ b/src/client/spark/Modules/Trading/Ladder/View/LadderRender.cs @@ -40,6 +40,8 @@ namespace mbts.spark { // Note: the orders-column has a separate "row_padding" value: Badge.DefaultMargin // (see how it is incorporated in measure_row_height() below) public static readonly Thickness row_padding = new Thickness(6); + const string tick_separator_period = "."; + const string tick_separator_dash = "-"; // IMPORTANT: this method ONLY WORKS if Badges use the BadgeRender.margin/border_thickness/padding values. // TODO: elminate the dependency on BadgeRender.margin/border_thickness/padding by integrating them into settings. @@ -52,8 +54,11 @@ namespace mbts.spark { public static int index_of(double[] prices, double price, IComparer comparer) => Array.BinarySearch(prices, price, comparer); - public static GlyphRun format_text(string text, int row, double row_height, LadderColumnSettings settings, GlyphContext glyph_context) => - glyph_context.get_glyph_run(text, settings.font_size, settings.width, row_height, HorizontalAlignment.Center, 0, VerticalAlignment.Center, row * row_height, true); + public static GlyphRun format_text(string text, int row, double row_height, LadderColumnSettings settings, GlyphContext glyph_context, LadderSettings.PriceDisplayFormat price_display_format = LadderSettings.PriceDisplayFormat.Default) => + glyph_context.get_glyph_run( + // long term there is probably a better way to get rid of the separator, such as in the TickExtensions._format_tick + price_display_format == LadderSettings.PriceDisplayFormat.Integer ? text.Replace(tick_separator_period, string.Empty).Replace(tick_separator_dash, string.Empty) : text, + settings.font_size, settings.width, row_height, HorizontalAlignment.Center, 0, VerticalAlignment.Center, row * row_height, true); public static double[] gen_prices(double mid, int row_count, ContractSpecificationImmutable spec) { // require odd number @@ -79,10 +84,10 @@ namespace mbts.spark { result[i] = format_text(spec.format(prices[i], reference_price, format_in_tick_terms), i, row_height, settings, glyph_context)); return result; } - public static GlyphRun[] format_prices(double[] prices, double row_height, LadderColumnSettings settings, GlyphContext glyph_context, ContractSpecificationImmutable spec) { + public static GlyphRun[] format_prices(double[] prices, double row_height, LadderColumnSettings settings, GlyphContext glyph_context, ContractSpecificationImmutable spec, LadderSettings.PriceDisplayFormat price_display_format) { var result = new GlyphRun[prices.Length]; Parallel.For(0, prices.Length, i => - result[i] = format_text(spec.format(prices[i]), i, row_height, settings, glyph_context)); + result[i] = format_text(spec.format(prices[i]), i, row_height, settings, glyph_context, price_display_format)); return result; } @@ -104,9 +109,9 @@ namespace mbts.spark { } public static double[] render_prices(DrawingVisual price, GlyphContext glyph_context, double mid, int row_count, double row_height, - LadderColumnSettings settings, ContractSpecificationImmutable spec) { + LadderColumnSettings settings, ContractSpecificationImmutable spec, LadderSettings.PriceDisplayFormat price_display_format) { var dprices = gen_prices(mid, row_count, spec); - render_prices(price, glyph_context, row_count, row_height, dprices, settings, spec); + render_prices(price, glyph_context, row_count, row_height, dprices, settings, spec, price_display_format); return dprices; } @@ -122,10 +127,10 @@ namespace mbts.spark { } } - public static void render_prices(DrawingVisual price, GlyphContext glyph_context, int row_count, double row_height, double[] dprices, LadderColumnSettings settings, ContractSpecificationImmutable spec) { + public static void render_prices(DrawingVisual price, GlyphContext glyph_context, int row_count, double row_height, double[] dprices, LadderColumnSettings settings, ContractSpecificationImmutable spec, LadderSettings.PriceDisplayFormat price_display_format) { var foreground = settings.foreground.rgb_brush(); //var background = new SolidColorBrush(settings.background.rgb_color()); - var fprices = format_prices(dprices, row_height, settings, glyph_context, spec); + var fprices = format_prices(dprices, row_height, settings, glyph_context, spec, price_display_format); using (var dc = price.RenderOpen()) using (_draw_with_clip(dc, settings, row_height, row_count)) { //dc.DrawRectangle(background, null, new Rect(0, 0, settings.width, row_count * row_height)); diff --git a/src/client/spark/Modules/Trading/Ladder/View/LadderSettingsView.xaml b/src/client/spark/Modules/Trading/Ladder/View/LadderSettingsView.xaml index 92ab62e4d..21d19bb24 100644 --- a/src/client/spark/Modules/Trading/Ladder/View/LadderSettingsView.xaml +++ b/src/client/spark/Modules/Trading/Ladder/View/LadderSettingsView.xaml @@ -178,6 +178,14 @@ + + @@ -630,7 +638,11 @@ - + + + + + @@ -710,6 +722,7 @@ + + + @@ -991,21 +1013,45 @@ + + + - + + + + + + + + + + + diff --git a/src/client/spark/Modules/Trading/Ladder/View/LadderView.xaml b/src/client/spark/Modules/Trading/Ladder/View/LadderView.xaml index b761e9b75..c0275583a 100644 --- a/src/client/spark/Modules/Trading/Ladder/View/LadderView.xaml +++ b/src/client/spark/Modules/Trading/Ladder/View/LadderView.xaml @@ -301,7 +301,14 @@ - + diff --git a/src/client/spark/Modules/Trading/Ladder/View/LadderView.xaml.cs b/src/client/spark/Modules/Trading/Ladder/View/LadderView.xaml.cs index 015680d5c..21c233eac 100644 --- a/src/client/spark/Modules/Trading/Ladder/View/LadderView.xaml.cs +++ b/src/client/spark/Modules/Trading/Ladder/View/LadderView.xaml.cs @@ -1,11 +1,17 @@ using bts; using bts.collections; using bts.utils; +using mbts.ui; using mbts.ui.converters; +using mbts.ui.extensions; using mbts.utils; +using System; +using System.Collections.Generic; using System.Linq; +using System.Reactive.Linq; using System.Windows; using System.Windows.Controls; +using System.Windows.Controls.Primitives; using System.Windows.Data; using System.Windows.Input; @@ -80,6 +86,8 @@ namespace mbts.spark { _ladder.visible_columns_changed += () => _column_headers_presenter.Columns.SetItems( _ladder.visible_column_types.Select(t => ColumnFactory.create(_ladder, t))); this.vm = vm; + this.ConnectDataContext(lvm => lvm.bus.subscribe(SparkTags.SecuritiesTag).OnDispatcher().Subscribe(sec_map => Resources[ResourceKeys.Securities] = sec_map)); + vm.SubscribeDisposed(_connect_left_panel_scroll()); DataContext = vm; DataContextChanged += datacontext_changed; _ui_scaler.MouseDoubleClick += _ui_scaler_MouseDoubleClick; @@ -87,7 +95,39 @@ namespace mbts.spark { this.PreviewMouseDown += LadderView_PreviewMouseDown; } - + IDisposable _connect_left_panel_scroll() { + // extent height + bool busy = false; + var extent_height_hook = _left_panel_scroll.ObserveProperty(ScrollViewer.ExtentHeightProperty, (s, e) => { + if (!busy) { + double scroll_pos = vm.left_panel_scroll_position; + busy = true; + Dispatcher.InvokeAsync(() => { + vm.left_panel_extent_height = _left_panel_scroll.ExtentHeight; + if (_left_panel_scroll.VerticalOffset != scroll_pos) + _left_panel_scroll.TryScrollToVerticalOffset(scroll_pos); + busy = false; + }, System.Windows.Threading.DispatcherPriority.ApplicationIdle); + } + }); + // view port height + _left_panel_scroll.Loaded += on_loaded; + var dispose = Disposable.CreateDisposeOnce(dispose_impl); + return Disposable.Create(dispose, vm.SubscribeDisposed(dispose)); + // local methods + void on_loaded(object sender, RoutedEventArgs e) { + if (_left_panel_scroll.descendants_of_type().TryGetFirst(sb => sb.Orientation == Orientation.Vertical, out var vscroll)) { + vscroll.SetBinding(ScrollBar.ViewportSizeProperty, new Binding(nameof(LadderVM.left_panel_view_port_height)) { + Converter = ValueConverter.create(d => new Size(0, d), sz => sz.Height), + Mode = BindingMode.OneWayToSource + }); + } + } + void dispose_impl() { + Disposable.dispose(ref extent_height_hook); + _left_panel_scroll.Loaded -= on_loaded; + } + } // (Left|Right) CTRL + Wheel to zoom // This is neat b/c the user doesn't even need to have the slider visible to zoom diff --git a/src/client/spark/Modules/Trading/ManualFills/ManualFillRowEditor.xaml b/src/client/spark/Modules/Trading/ManualFills/ManualFillRowEditor.xaml index d9d27d638..7779621e7 100644 --- a/src/client/spark/Modules/Trading/ManualFills/ManualFillRowEditor.xaml +++ b/src/client/spark/Modules/Trading/ManualFills/ManualFillRowEditor.xaml @@ -62,15 +62,15 @@ Header="{Binding security}" IsEditMode="False" IsLazy="True" - SecurityMap="{DynamicResource ResourceKey={x:Static usp:ManualFillRowEditor.SecuritiesKey}}" + SecurityMap="{DynamicResource ResourceKey={x:Static u:ResourceKeys.Securities}}" SelectedSecurity="{Binding security, UpdateSourceTrigger=Explicit}" ShowDialogSkirt="True" ToolTip="{Binding SelectedCriteria, RelativeSource={RelativeSource Self}, Converter={x:Static c:ContractPickerPopup.SecurityCriteriaTextConverter}, Mode=OneWay}" OKClick="{x:Static c:ContractPickerPopup.FlushSelectedSecurityHandler}" /> - - - + + + diff --git a/src/client/spark/Modules/Trading/ManualFills/ManualFillRowEditor.xaml.cs b/src/client/spark/Modules/Trading/ManualFills/ManualFillRowEditor.xaml.cs index 0c597bf14..48a5d5ffa 100644 --- a/src/client/spark/Modules/Trading/ManualFills/ManualFillRowEditor.xaml.cs +++ b/src/client/spark/Modules/Trading/ManualFills/ManualFillRowEditor.xaml.cs @@ -6,12 +6,6 @@ namespace mbts.ui.spark { /// Interaction logic for ManualFillRowEditor.xaml /// partial class ManualFillRowEditor : UserControl { - public const string - SecuritiesKey = nameof(SecuritiesKey), - AccountsKey = nameof(AccountsKey), - UsersKey = nameof(UsersKey), - SidesKey = nameof(SidesKey); - public static readonly RoutedCommand CommitCommand = new RoutedCommand(), CancelCommand = new RoutedCommand(); diff --git a/src/client/spark/Modules/Trading/ManualFills/ManualFillsView.xaml.cs b/src/client/spark/Modules/Trading/ManualFills/ManualFillsView.xaml.cs index 78fb5b10b..cbc3ed8bc 100644 --- a/src/client/spark/Modules/Trading/ManualFills/ManualFillsView.xaml.cs +++ b/src/client/spark/Modules/Trading/ManualFills/ManualFillsView.xaml.cs @@ -50,11 +50,11 @@ namespace mbts.ui.spark { row_editor.CommandBindings.Add(new CommandBinding(ManualFillRowEditor.CommitCommand, commit_executed)); row_editor.CommandBindings.Add(new CommandBinding(ManualFillRowEditor.CancelCommand, cancel_executed)); // dynamic resources - row_editor.Resources[ManualFillRowEditor.AccountsKey] = vm.accounts; - row_editor.Resources[ManualFillRowEditor.UsersKey] = vm.users; - row_editor.Resources[ManualFillRowEditor.SidesKey] = vm.sides; + row_editor.Resources[ResourceKeys.Accounts] = vm.accounts; + row_editor.Resources[ResourceKeys.Users] = vm.users; + row_editor.Resources[ResourceKeys.Sides] = vm.sides; FocusManager.SetIsFocusScope(row_editor, true); - var sec_hook = vm.as_demuxer().subscribe(nameof(ManualFillsVM.security_map)).OnDispatcher().Subscribe(v => row_editor.Resources[ManualFillRowEditor.SecuritiesKey] = v); + var sec_hook = vm.as_demuxer().subscribe(nameof(ManualFillsVM.security_map)).OnDispatcher().Subscribe(v => row_editor.Resources[ResourceKeys.Securities] = v); // popup adorner var popup_adorner = new PopupAdorner( data_grid, data_grid, diff --git a/src/client/spark/Modules/Trading/OrderBook/OrderBookRowVM.cs b/src/client/spark/Modules/Trading/OrderBook/OrderBookRowVM.cs index 44fee4c18..8644745b1 100644 --- a/src/client/spark/Modules/Trading/OrderBook/OrderBookRowVM.cs +++ b/src/client/spark/Modules/Trading/OrderBook/OrderBookRowVM.cs @@ -108,7 +108,7 @@ namespace mbts.spark { public CustomOrderDefinition.CustomOneofCase root_algo_order_type => order_tuple.root.algo.value.CustomCase; public OrderMetadata root_algo_order_metadata => _owner.get_metadata(root_algo_order_type); public Brush root_algo_badge_fill => _root_algo_badge_fill ?? (_root_algo_badge_fill = new SolidColorBrush(_root_algo_badge_color)); - public string root_algo_display_name => order_tuple.root.algo.name; + public string root_algo_display_name => order_tuple.root.display_name(); // Tree public bool is_parent { get => _is_parent; set => Set(ref _is_parent, value); } diff --git a/src/client/spark/Modules/Trading/OrderBook/OrderBookWindow.cs b/src/client/spark/Modules/Trading/OrderBook/OrderBookWindow.cs index bcbc64aca..b416e6937 100644 --- a/src/client/spark/Modules/Trading/OrderBook/OrderBookWindow.cs +++ b/src/client/spark/Modules/Trading/OrderBook/OrderBookWindow.cs @@ -66,6 +66,10 @@ namespace mbts.spark { _vm = new OrderBookVM(_bus, _notification_service, _model, _settings_manager); _vm.load_from(_state); _view = new OrderBookView(_vm); + _vm.SubscribeDisposed( + _bus.subscribe(SparkTags.SecuritiesTag) + .OnDispatcher() + .Subscribe(sec_map => _view.Resources[ResourceKeys.Securities] = sec_map)); var show_fills_command = SparkCommands.ShowFills(SparkCommands.KeyGestures.ShowFills).complete(() => new FillsSettings(_vm.selected_orders.Select(r => r.Key).ToArray())); diff --git a/src/client/spark/Modules/Trading/PNL/Settings/VM/PNLSettingsVM.cs b/src/client/spark/Modules/Trading/PNL/Settings/VM/PNLSettingsVM.cs index 47a36196f..518e259a6 100644 --- a/src/client/spark/Modules/Trading/PNL/Settings/VM/PNLSettingsVM.cs +++ b/src/client/spark/Modules/Trading/PNL/Settings/VM/PNLSettingsVM.cs @@ -1,220 +1,223 @@ -using bts.collections; -using bts.utils; -using mbts; -using mbts.collections; -using mbts.expression; -using mbts.spark; -using mbts.ui; -using mbts.ui.settings; -using mbts.ui.shell; -using mbts.ui.spark; -using mbts.ui.utility; -using mbts.utils; -using System; -using System.Collections.Generic; -using System.Collections.ObjectModel; -using System.Collections.Specialized; -using System.Dynamic; -using System.Linq; -using ColumnKeys = bts.shell.pnl.PNLView.Columns; - -namespace bts.shell.pnl { - class PNLSettingsVM : DisposableViewModelBase, IWindowSettingsVM, IValueFactory, IObservable, ITemplateSelectorKey { - readonly IReadOnlyDictionary _anchor_cache; - readonly DeferrableActionWrapper _sync_anchors; - - PNLCalculationType _calculation_type; - PNLHeaderRowDisplay _header_row_display; - PNLSettings _last_loaded; - - public PNLSettingsVM(PNLSettings settings) { - // init anchor cache, deferrable resync - _anchor_cache = PNLSettings.anchorable_columns.ToDictionary(k => k, k => new AnchorItemVM(this, k)); - _sync_anchors = new DeferrableActionWrapper(() => { - for (int i = 0; i < anchor_items.Count; i++) { - anchor_items[i].anchor_index = i + 1; - anchor_items[i].refresh(); - } - }); - core_settings_vm = new SimpleGridSettingsVM(column_factory: col_settings => { - var col_vm = new PNLColumnVM(core_settings_vm, EnumHelper.parse(col_settings.id)); - col_vm.load_from(col_settings); - return col_vm; - }); - core_settings_vm.selection_highlight.bind_opacity_to_alpha = true; - _dispose = Disposable.Create( - anchor_items.Subscribe((s, e) => _sync_anchors.execute())); - load(settings); - } - - public IEnumerable anchor_item_headers => _anchor_cache.Values; - public ObservableCollection anchor_items { get; } = new ObservableCollection(); - public SimpleGridSettingsVM core_settings_vm { get; } - public PNLGroupRowAppearanceVM group_row_defaults { get; } = new PNLGroupRowAppearanceVM(); - public PNLCellAppearanceVM header_row_defaults { get; } = new PNLCellAppearanceVM(); - public PNLCalculationType calculation_type { get => _calculation_type; set => Set(ref _calculation_type, value); } - public IEnumerable calculation_types => EnumHelper.get_values_except(PNLCalculationType.None, PNLCalculationType.Same, PNLCalculationType.Opposite); - public PNLHeaderRowDisplay header_row_display { get => _header_row_display; set => Set(ref _header_row_display, value); } - public IEnumerable header_row_display_options => PNLSettings.header_row_display_options; - - string ITemplateSelectorKey.key => GridSettingsTemplateKeys.PNL; - - public PNLSettings get_result() { - var s = _last_loaded == null ? new PNLSettings() : SparkUtils.clone_proto(_last_loaded); - s.calculation_type = _calculation_type; - s.grid = core_settings_vm.create(); - // move anchor cols to the left, order wrt grouping hierarchy - var new_cols = new Lazy>(() => s.grid.columns.ToList()); - for (int i = 0; i < s.grid.columns.Count; i++) { - var key = s.grid.columns[i].strong_id(); - if (_anchor_cache.TryGetValue(key, out var a) && a.anchor_index.HasValue) { - // anchor index is 1 based, subtract 1 to get 0 based - int anchor_index = a.anchor_index.Value - 1; - if (anchor_index != i && anchor_index >= 0 && anchor_index < s.grid.columns.Count) { - new_cols.Value.RemoveAt(i); - new_cols.Value.Insert(anchor_index, s.grid.columns[i]); - } +using bts.collections; +using bts.utils; +using mbts; +using mbts.collections; +using mbts.expression; +using mbts.spark; +using mbts.ui; +using mbts.ui.settings; +using mbts.ui.shell; +using mbts.ui.spark; +using mbts.ui.utility; +using mbts.utils; +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Collections.Specialized; +using System.Dynamic; +using System.Linq; +using ColumnKeys = bts.shell.pnl.PNLView.Columns; + +namespace bts.shell.pnl { + class PNLSettingsVM : DisposableViewModelBase, IWindowSettingsVM, IValueFactory, IObservable, ITemplateSelectorKey { + readonly IReadOnlyDictionary _anchor_cache; + readonly DeferrableActionWrapper _sync_anchors; + + PNLCalculationType _calculation_type; + PNLHeaderRowDisplay _header_row_display; + PNLSettings _last_loaded; + + public PNLSettingsVM(PNLSettings settings) { + // init anchor cache, deferrable resync + _anchor_cache = PNLSettings.anchorable_columns.ToDictionary(k => k, k => new AnchorItemVM(this, k)); + _sync_anchors = new DeferrableActionWrapper(() => { + for (int i = 0; i < anchor_items.Count; i++) { + anchor_items[i].anchor_index = i + 1; + anchor_items[i].refresh(); } - } - // rule-based animations - var trigger_anims = core_settings_vm.columns - .OfType() - .SelectMany(c => c.get_triggers()) - .GroupBy(tup => tup.expression) - .Where(g => CoreExpressionEvaluator.validate(g.Key)) - .Select(g => ( - expression: g.Key, - animations: g.ToArray())) - .ToList(); - - var normalized_anims = trigger_anims - .Where(tup => CoreExpressionEvaluator.validate(tup.expression)) - .ToList(); - - s = s.with(anchors: anchor_items.Select(vm => vm.key), - grid: new_cols.IsValueCreated ? s.grid.with(columns: new_cols.Value) : null, - group_columns: core_settings_vm.columns.OfType().Select(c => { - var col_settings = c.create(); - col_settings = c.group_column_vm.apply_to(col_settings); - return col_settings; - }), - header_columns: core_settings_vm.columns.OfType().Select(c => { - var col_settings = c.create(); - col_settings = c.header_column_vm.apply_to(col_settings); - return col_settings; - }), - animations: trigger_anims.Select( - ta => new PNLAnimationTriggerData { - expression = ta.expression, - animations = Array.ConvertAll(ta.animations, t => t.row) - }), - group_header_animations: normalized_anims - .Select( - ta => new PNLAnimationTriggerData { - expression = ta.expression, - animations = Array.ConvertAll(ta.animations, t => t.group) - }), - header_row_animations: normalized_anims.Select( - ta => new PNLAnimationTriggerData { - expression = ta.expression, - animations = Array.ConvertAll(ta.animations, t => t.header) - })); - s.header_row_display = header_row_display; - s.header_cell_appearance = PNLCellAppearanceProto.from_appearance(header_row_defaults.to_appearance()); - // group - var (group_cell_appearance, group_row_background_colors) = group_row_defaults.create(); - s.group_cell_appearance = PNLCellAppearanceProto.from_appearance(group_cell_appearance); - s.group_background_colors.SetItems(group_row_background_colors.Select(c => c.rgb_color())); - // - return s; - } - - public void load(PNLSettings value) { - _last_loaded = value; - header_row_defaults.load(value.header_cell_appearance.to_appearance()); - group_row_defaults.load_from((value.group_cell_appearance.to_appearance(), value.group_background_colors.WrapSelectorRO(c => c.argb_color()))); - var column_map = _map_column_data(value.grid.columns, value.animations); - var group_column_map = _map_column_data(value.group_columns, value.group_header_animations); - var header_column_map = _map_column_data(value.header_columns, value.header_row_animations); - - core_settings_vm.load_from(value.grid); - - for (int i = 0; i < core_settings_vm.columns.Count; i++) { - var col_vm = core_settings_vm.columns[i] as PNLColumnVM ?? throw new InvalidOperationException($"In {nameof(PNLSettingsVM)}, all columns should be of type {typeof(PNLColumnVM)}."); - var key = EnumHelper.parse(col_vm.id); - var (col_settings, trigger_animations) = column_map[key]; - var group_data = group_column_map.GetNullable(key); - var header_data = header_column_map.GetNullable(key); - col_vm.load_from(col_settings, trigger_animations, group_data?.settings, group_data?.triggers, header_data?.triggers, header_data?.settings); - } - using (_sync_anchors.defer_execution()) - anchor_items.WrapSelector(a => a.key, k => _anchor_cache[k]).sync_with(value.anchors.Intersect(PNLSettings.anchorable_columns)); - header_row_display = value.header_row_display; - calculation_type = value.calculation_type; - } - - PNLSettings IValueFactory.create() => get_result(); - void IValueFactory.load_from(PNLSettings value) => load(value); - IDisposable IObservable.Subscribe(IObserver observer) => throw new NotImplementedException(); - - static Dictionary triggers)> _map_column_data(IEnumerable columns, IEnumerable triggers) { - var map = columns.ToDictionary( - c => c.strong_id(), - c => (settings: c, triggers: new Dictionary())); - foreach (var trig in triggers) { - foreach (var anim in trig.animations) { - if (map.TryGetValue(anim.column, out var col_data)) - col_data.triggers[trig.expression] = anim; - } - } - return map; - } - - void _append_or_remove_anchor(ColumnKeys key, bool include) { - if (!_anchor_cache.TryGetValue(key, out var vm)) - throw new KeyNotFoundException($"Column key '{key}' is not anchorable (expected one of: {string.Join(", ", PNLSettings.anchorable_columns)})."); - if (vm.include == include) - return; - if (include) { - if (anchor_items.Any(a => a.key == key)) - throw new InvalidOperationException($"{nameof(anchor_items)} already contains an element with key '{key}.'"); - anchor_items.Add(vm); - vm.anchor_index = anchor_items.Count; - } else { - if (!anchor_items.Remove(vm)) - throw new InvalidOperationException($"{nameof(anchor_items)} does not contain an element with key '{key}.'"); - vm.anchor_index = null; - } - _sync_anchors.execute(); - } - - public class AnchorItemVM : ViewModelBase, IEquatable { - readonly PNLSettingsVM _owner; - int? _anchor_index; - - public AnchorItemVM(PNLSettingsVM owner, ColumnKeys key) { - _owner = owner; - this.key = key; - } - - public ColumnKeys key { get; } - public bool include { get => anchor_index.HasValue; set => _owner._append_or_remove_anchor(key, value); } - public int? anchor_index { get => _anchor_index; set => Set(ref _anchor_index, value); } - - public void refresh() => - RaisePropertyChanged(nameof(include), nameof(anchor_index)); - - public bool Equals(AnchorItemVM other) => - other != null && key == other.key; - - public override bool Equals(object obj) => - Equals(obj as AnchorItemVM); - - public override int GetHashCode() => - key.GetHashCode(); - - public override string ToString() => - EnumHelper.get_display_name(key); - } - } + }); + core_settings_vm = new SimpleGridSettingsVM(column_factory: col_settings => { + var col_vm = new PNLColumnVM(core_settings_vm, EnumHelper.parse(col_settings.id)); + col_vm.load_from(col_settings); + return col_vm; + }); + core_settings_vm.selection_highlight.bind_opacity_to_alpha = true; + _dispose = Disposable.Create( + anchor_items.Subscribe((s, e) => _sync_anchors.execute())); + load(settings); + } + + public IEnumerable anchor_item_headers => _anchor_cache.Values; + public ObservableCollection anchor_items { get; } = new ObservableCollection(); + public SimpleGridSettingsVM core_settings_vm { get; } + public PNLGroupRowAppearanceVM group_row_defaults { get; } = new PNLGroupRowAppearanceVM(); + public PNLCellAppearanceVM header_row_defaults { get; } = new PNLCellAppearanceVM(); + public PNLCalculationType calculation_type { get => _calculation_type; set => Set(ref _calculation_type, value); } + public IEnumerable calculation_types => EnumHelper.get_values_except(PNLCalculationType.None, PNLCalculationType.Same, PNLCalculationType.Opposite); + public PNLHeaderRowDisplay header_row_display { get => _header_row_display; set => Set(ref _header_row_display, value); } + public IEnumerable header_row_display_options => PNLSettings.header_row_display_options; + + string ITemplateSelectorKey.key => GridSettingsTemplateKeys.PNL; + + public PNLSettings get_result() { + var s = _last_loaded == null ? new PNLSettings() : SparkUtils.clone_proto(_last_loaded); + s.calculation_type = _calculation_type; + s.grid = core_settings_vm.create(); + // move anchor cols to the left, order wrt grouping hierarchy + List new_cols = null; + IEnumerable cols = s.grid.columns; + var sorted_anchors = anchor_items.OrderBy(a => a.anchor_index).ToList(); + for (int i = 0; i < sorted_anchors.Count; i++) { + if (GridSettingsHelper.try_find_column(cols, sorted_anchors[i].key, out var match, out int pos) && pos != i) { + if (new_cols == null) { + new_cols = s.grid.columns.ToList(); + // set cols to new_cols so subsequent position lookups + // take previous column order manipulations into account. + cols = new_cols; + } + new_cols.RemoveAt(pos); + new_cols.Insert(i, match); + } + } + // rule-based animations + var trigger_anims = core_settings_vm.columns + .OfType() + .SelectMany(c => c.get_triggers()) + .GroupBy(tup => tup.expression) + .Where(g => CoreExpressionEvaluator.validate(g.Key)) + .Select(g => ( + expression: g.Key, + animations: g.ToArray())) + .ToList(); + + var normalized_anims = trigger_anims + .Where(tup => CoreExpressionEvaluator.validate(tup.expression)) + .ToList(); + + s = s.with(anchors: anchor_items.Select(vm => vm.key), + grid: new_cols == null ? null : s.grid.with(columns: new_cols), + group_columns: core_settings_vm.columns.OfType().Select(c => { + var col_settings = c.create(); + col_settings = c.group_column_vm.apply_to(col_settings); + return col_settings; + }), + header_columns: core_settings_vm.columns.OfType().Select(c => { + var col_settings = c.create(); + col_settings = c.header_column_vm.apply_to(col_settings); + return col_settings; + }), + animations: trigger_anims.Select( + ta => new PNLAnimationTriggerData { + expression = ta.expression, + animations = Array.ConvertAll(ta.animations, t => t.row) + }), + group_header_animations: normalized_anims + .Select( + ta => new PNLAnimationTriggerData { + expression = ta.expression, + animations = Array.ConvertAll(ta.animations, t => t.group) + }), + header_row_animations: normalized_anims.Select( + ta => new PNLAnimationTriggerData { + expression = ta.expression, + animations = Array.ConvertAll(ta.animations, t => t.header) + })); + s.header_row_display = header_row_display; + s.header_cell_appearance = PNLCellAppearanceProto.from_appearance(header_row_defaults.to_appearance()); + // group + var (group_cell_appearance, group_row_background_colors) = group_row_defaults.create(); + s.group_cell_appearance = PNLCellAppearanceProto.from_appearance(group_cell_appearance); + s.group_background_colors.SetItems(group_row_background_colors.Select(c => c.rgb_color())); + // + return s; + } + + public void load(PNLSettings value) { + _last_loaded = value; + header_row_defaults.load(value.header_cell_appearance.to_appearance()); + group_row_defaults.load_from((value.group_cell_appearance.to_appearance(), value.group_background_colors.WrapSelectorRO(c => c.argb_color()))); + var column_map = _map_column_data(value.grid.columns, value.animations); + var group_column_map = _map_column_data(value.group_columns, value.group_header_animations); + var header_column_map = _map_column_data(value.header_columns, value.header_row_animations); + + core_settings_vm.load_from(value.grid); + + for (int i = 0; i < core_settings_vm.columns.Count; i++) { + var col_vm = core_settings_vm.columns[i] as PNLColumnVM ?? throw new InvalidOperationException($"In {nameof(PNLSettingsVM)}, all columns should be of type {typeof(PNLColumnVM)}."); + var key = EnumHelper.parse(col_vm.id); + var (col_settings, trigger_animations) = column_map[key]; + var group_data = group_column_map.GetNullable(key); + var header_data = header_column_map.GetNullable(key); + col_vm.load_from(col_settings, trigger_animations, group_data?.settings, group_data?.triggers, header_data?.triggers, header_data?.settings); + } + using (_sync_anchors.defer_execution()) + anchor_items.WrapSelector(a => a.key, k => _anchor_cache[k]).sync_with(value.anchors.Intersect(PNLSettings.anchorable_columns)); + header_row_display = value.header_row_display; + calculation_type = value.calculation_type; + } + + PNLSettings IValueFactory.create() => get_result(); + void IValueFactory.load_from(PNLSettings value) => load(value); + IDisposable IObservable.Subscribe(IObserver observer) => throw new NotImplementedException(); + + static Dictionary triggers)> _map_column_data(IEnumerable columns, IEnumerable triggers) { + var map = columns.ToDictionary( + c => c.strong_id(), + c => (settings: c, triggers: new Dictionary())); + foreach (var trig in triggers) { + foreach (var anim in trig.animations) { + if (map.TryGetValue(anim.column, out var col_data)) + col_data.triggers[trig.expression] = anim; + } + } + return map; + } + + void _append_or_remove_anchor(ColumnKeys key, bool include) { + if (!_anchor_cache.TryGetValue(key, out var vm)) + throw new KeyNotFoundException($"Column key '{key}' is not anchorable (expected one of: {string.Join(", ", PNLSettings.anchorable_columns)})."); + if (vm.include == include) + return; + if (include) { + if (anchor_items.Any(a => a.key == key)) + throw new InvalidOperationException($"{nameof(anchor_items)} already contains an element with key '{key}.'"); + anchor_items.Add(vm); + vm.anchor_index = anchor_items.Count; + } else { + if (!anchor_items.Remove(vm)) + throw new InvalidOperationException($"{nameof(anchor_items)} does not contain an element with key '{key}.'"); + vm.anchor_index = null; + } + _sync_anchors.execute(); + } + + public class AnchorItemVM : ViewModelBase, IEquatable { + readonly PNLSettingsVM _owner; + int? _anchor_index; + + public AnchorItemVM(PNLSettingsVM owner, ColumnKeys key) { + _owner = owner; + this.key = key; + } + + public ColumnKeys key { get; } + public bool include { get => anchor_index.HasValue; set => _owner._append_or_remove_anchor(key, value); } + public int? anchor_index { get => _anchor_index; set => Set(ref _anchor_index, value); } + + public void refresh() => + RaisePropertyChanged(nameof(include), nameof(anchor_index)); + + public bool Equals(AnchorItemVM other) => + other != null && key == other.key; + + public override bool Equals(object obj) => + Equals(obj as AnchorItemVM); + + public override int GetHashCode() => + key.GetHashCode(); + + public override string ToString() => + EnumHelper.get_display_name(key); + } + } } \ No newline at end of file diff --git a/src/client/spark/Modules/Trading/PNL/VM/PNLRowComparer.cs b/src/client/spark/Modules/Trading/PNL/VM/PNLRowComparer.cs index 0932b01db..9d88699da 100644 --- a/src/client/spark/Modules/Trading/PNL/VM/PNLRowComparer.cs +++ b/src/client/spark/Modules/Trading/PNL/VM/PNLRowComparer.cs @@ -90,12 +90,11 @@ namespace bts.shell.pnl { } int? _try_compare_groups(CollectionViewGroup x, CollectionViewGroup y) { - if (x == null || y == null) - return null; - var xcell = (IPNLCellData)x.Name; - var ycell = (IPNLCellData)y.Name; - var comp = _get_column_comparer(xcell.column); - return comp.value_comparer.Compare(xcell.value, ycell.value); + if (x?.Name is IPNLCellData xcell && y?.Name is IPNLCellData ycell) { + var comp = _get_column_comparer(xcell.column); + return comp.value_comparer.Compare(xcell.value, ycell.value); + } + return null; } } -} \ No newline at end of file +} diff --git a/src/client/spark/Modules/Trading/PNL/View/PNLView.xaml b/src/client/spark/Modules/Trading/PNL/View/PNLView.xaml index f30696473..3109114d0 100644 --- a/src/client/spark/Modules/Trading/PNL/View/PNLView.xaml +++ b/src/client/spark/Modules/Trading/PNL/View/PNLView.xaml @@ -698,8 +698,8 @@ c:DataGridFilterProperties.FilterActive="{Binding DataContext.security_filter.active, Source={u:DirectReference _grid}}" c:DataGridFilterProperties.FilterOpen="{Binding DataContext.security_filter.open, Source={u:DirectReference _grid}}" key="Security" - sort_member_path="security_cell.cell_data" - data_member_path="security" + data_member_path="security_cell.cell_data" + sort_member_path="security" HeaderTemplate="{StaticResource AnchorableColumnHeaderTemplate}" /> diff --git a/src/client/spark/Orders/AlgoParamVM.cs b/src/client/spark/Orders/AlgoParamVM.cs index e8ded6009..cc7a43a2a 100644 --- a/src/client/spark/Orders/AlgoParamVM.cs +++ b/src/client/spark/Orders/AlgoParamVM.cs @@ -1,4 +1,5 @@ -using mbts.ui.shell; +using mbts.ui; +using mbts.ui.shell; using System; using System.Collections.Generic; using System.ComponentModel; @@ -6,8 +7,9 @@ using System.Reactive.Linq; using System.Runtime.CompilerServices; namespace mbts.spark { - class AlgoParamVM : IAlgoParamVM, INotifyPropertyChanged { + class AlgoParamVM : IAlgoParamVM, INotifyPropertyChanged, ITemplateSelectorKey { readonly IEqualityComparer _comparer; + readonly string _template_key; AlgoParamPermissions _permissions; bool _is_enabled, _has_value; T _committed_value, _value; @@ -15,15 +17,16 @@ namespace mbts.spark { public AlgoParamVM(string id, string display_name) : this(id, display_name, default, EqualityComparer.Default) { } - public AlgoParamVM(string id, string display_name, T value) - : this(id, display_name, value, EqualityComparer.Default) { } + public AlgoParamVM(string id, string display_name, T value, string template_key = null) + : this(id, display_name, value, EqualityComparer.Default, template_key) { } - public AlgoParamVM(string id, string display_name, T value, IEqualityComparer comparer) { + public AlgoParamVM(string id, string display_name, T value, IEqualityComparer comparer, string template_key = null) { this.id = id; this.display_name = display_name; _value = value; _committed_value = value; _comparer = comparer; + _template_key = template_key; } public event PropertyChangedEventHandler PropertyChanged; @@ -50,7 +53,9 @@ namespace mbts.spark { public bool is_enabled { get => _is_enabled; set => _set(ref _is_enabled, value); } public bool has_value { get => _has_value; set => _set(ref _has_value, value); } public bool is_dirty { get; private set; } - object IAlgoParamVM.value { get => value; set => this.value = (T)value; } + object IAlgoParamVM.value { get => value; set => this.value = (T)value; } + + string ITemplateSelectorKey.key => _template_key; public void revert() { if (is_dirty) { diff --git a/src/client/spark/Orders/FillTuple.cs b/src/client/spark/Orders/FillTuple.cs index 87d4bb38c..a639fe575 100644 --- a/src/client/spark/Orders/FillTuple.cs +++ b/src/client/spark/Orders/FillTuple.cs @@ -36,6 +36,8 @@ namespace mbts.spark { public AlgoDefinitionMetadata? root_algo_meta => _order_tuple.HasValue ? _order_tuple.Value.root.meta : null; + public OrderSecurityAlgo? root_order_security => _order_tuple?.root; + public string account { get { var account_string = string.Empty; @@ -85,4 +87,4 @@ namespace mbts.spark { public static string user(this FillTuple fill_tuple) => fill_tuple.order.HasValue ? fill_tuple.order.Value.user() : string.Empty; } -} +} \ No newline at end of file diff --git a/src/client/spark/Orders/OrderTypeVMs/DynamicOrderHelper.cs b/src/client/spark/Orders/OrderTypeVMs/DynamicOrderHelper.cs index 81178590c..7a657e393 100644 --- a/src/client/spark/Orders/OrderTypeVMs/DynamicOrderHelper.cs +++ b/src/client/spark/Orders/OrderTypeVMs/DynamicOrderHelper.cs @@ -62,6 +62,7 @@ namespace mbts.spark { case DynamicOrderPrimitiveDescriptor.DynamicOrderPrimitiveInt: param.AsInt32 = 0; break; case DynamicOrderPrimitiveDescriptor.DynamicOrderPrimitiveOrderDef: param.AsOrderDef = new CustomOrderDefinition(); break; case DynamicOrderPrimitiveDescriptor.DynamicOrderPrimitiveString: param.AsString = string.Empty; break; + case DynamicOrderPrimitiveDescriptor.DynamicOrderPrimitiveSecurityKey: case DynamicOrderPrimitiveDescriptor.DynamicOrderPrimitiveUint: param.AsUint32 = 0u; break; case DynamicOrderPrimitiveDescriptor.DynamicOrderPrimitiveQuantity: param.AsQuantity = new bts.spark.QuantityMeasure(); break; } diff --git a/src/client/spark/Orders/OrderTypeVMs/DynamicOrderParamVM.cs b/src/client/spark/Orders/OrderTypeVMs/DynamicOrderParamVM.cs index ade3260a4..b2cf2b1d3 100644 --- a/src/client/spark/Orders/OrderTypeVMs/DynamicOrderParamVM.cs +++ b/src/client/spark/Orders/OrderTypeVMs/DynamicOrderParamVM.cs @@ -12,7 +12,7 @@ using System.Reactive.Linq; namespace mbts.spark { interface IDynamicOrderParamVM { DynamicOrderParamDescriptor descriptor { get; } - DynamicOrderParam param { get; set; } + DynamicOrderParam param { get; } } interface IDynamicOrderParamVM : IDynamicOrderParamVM { @@ -21,26 +21,16 @@ namespace mbts.spark { } abstract class DynamicOrderParamVM : DisposableViewModelBase, IDynamicOrderParamVM { - DynamicOrderParam _param; - protected DynamicOrderParamVM(DynamicOrderParamDescriptor descriptor) : this(descriptor, DynamicOrderHelper.init_param(descriptor)) { } private DynamicOrderParamVM(DynamicOrderParamDescriptor descriptor, DynamicOrderParam param) { this.descriptor = descriptor; - _param = param; + this.param = param; } public DynamicOrderParamDescriptor descriptor { get; set; } - public DynamicOrderParam param { - get => _param; - set { - _param = value; - on_param_changed(); - } - } - - protected abstract void on_param_changed(); + public DynamicOrderParam param { get; } } class DynamicOrderParamVM : DynamicOrderParamVM, IDynamicOrderParamVM { @@ -53,14 +43,12 @@ namespace mbts.spark { _field_desc = field_descriptor; _convert = convert; _convert_back = convert_back; + value = _convert(ConvertUtils.try_convert(_field_desc.Accessor.GetValue(param))); } public TView value { get => _value; - set { - _field_desc.Accessor.SetValue(param, _convert_back(_value = value)); - RaisePropertyChanged(); - } + set => Set(ref _value, value); } public IObservable subscribe_value() => @@ -70,11 +58,6 @@ namespace mbts.spark { void on_prop_changed(object sender, PropertyChangedEventArgs e) => o.OnNext(value); }); - - protected override void on_param_changed() { - _value = _convert(ConvertUtils.try_convert(_field_desc.Accessor.GetValue(param))); - RaisePropertyChanged(nameof(value)); - } } class DynamicOrderEnumeratedVM : DynamicOrderParamVM, IDynamicOrderParamVM { @@ -105,11 +88,6 @@ namespace mbts.spark { o.OnNext(selected_element); }); - protected override void on_param_changed() { - _sync_elements(); - RaisePropertyChanged(nameof(selected_element)); - } - static IEnumerable _get_elements(DynamicOrderParamDescriptor descriptor) => descriptor.ParamType?.DescriptorCase == DynamicOrderTypeDescriptor.DescriptorOneofCase.Enumerated ? descriptor.ParamType.Enumerated.Element : Enumerable.Empty(); @@ -148,10 +126,5 @@ namespace mbts.spark { o.OnNext(quantity); }); } - - protected override void on_param_changed() - { - RaisePropertyChanged(nameof(quantity)); - } } } diff --git a/src/client/spark/Orders/OrderTypeVMs/DynamicOrderTemplateSelector.cs b/src/client/spark/Orders/OrderTypeVMs/DynamicOrderTemplateSelector.cs index cdf5a4906..d27c0e4d4 100644 --- a/src/client/spark/Orders/OrderTypeVMs/DynamicOrderTemplateSelector.cs +++ b/src/client/spark/Orders/OrderTypeVMs/DynamicOrderTemplateSelector.cs @@ -20,6 +20,7 @@ namespace mbts.ui.spark public DataTemplate StringTemplate { get; set; } public DataTemplate OrderDefTemplate { get; set; } public DataTemplate QuantityMeasureTemplate { get; set; } + public DataTemplate SecurityKeyTemplate { get; set; } public DataTemplate EnumTemplate { get; set; } @@ -38,6 +39,7 @@ namespace mbts.ui.spark case DynamicOrderPrimitiveDescriptor.DynamicOrderPrimitiveDouble: return DoubleTemplate; case DynamicOrderPrimitiveDescriptor.DynamicOrderPrimitiveInt: return IntTemplate; case DynamicOrderPrimitiveDescriptor.DynamicOrderPrimitiveUint: return UIntTemplate; + case DynamicOrderPrimitiveDescriptor.DynamicOrderPrimitiveSecurityKey: return SecurityKeyTemplate; case DynamicOrderPrimitiveDescriptor.DynamicOrderPrimitiveString: return StringTemplate; case DynamicOrderPrimitiveDescriptor.DynamicOrderPrimitiveOrderDef: return OrderDefTemplate; case DynamicOrderPrimitiveDescriptor.DynamicOrderPrimitiveQuantity: return QuantityMeasureTemplate; diff --git a/src/client/spark/Orders/OrderTypeVMs/DynamicOrderVM.cs b/src/client/spark/Orders/OrderTypeVMs/DynamicOrderVM.cs index d69230589..d28c554a2 100644 --- a/src/client/spark/Orders/OrderTypeVMs/DynamicOrderVM.cs +++ b/src/client/spark/Orders/OrderTypeVMs/DynamicOrderVM.cs @@ -4,6 +4,7 @@ using bts.collections; using bts.spark; using mbts.collections; using mbts.spark.utilities; +using mbts.ui; using mbts.ui.utility; using mbts.utils; using System; @@ -73,9 +74,9 @@ namespace mbts.spark { case DynamicOrderPrimitiveDescriptor.DynamicOrderPrimitiveDouble: return mk(DynamicOrderParam.AsDoubleFieldNumber, v => v, v => v, double.NaN); case DynamicOrderPrimitiveDescriptor.DynamicOrderPrimitiveInt: return mk(DynamicOrderParam.AsInt32FieldNumber, v => v, v => v.GetValueOrDefault()); case DynamicOrderPrimitiveDescriptor.DynamicOrderPrimitiveUint: return mk(DynamicOrderParam.AsUint32FieldNumber, v => v, v => v.GetValueOrDefault()); + case DynamicOrderPrimitiveDescriptor.DynamicOrderPrimitiveSecurityKey: return mk(DynamicOrderParam.AsUint32FieldNumber, v => v, v => v.GetValueOrDefault(), template_key: TemplateKeys.SecurityKey); case DynamicOrderPrimitiveDescriptor.DynamicOrderPrimitiveString: return mk(DynamicOrderParam.AsStringFieldNumber, v => v, v => v); - case DynamicOrderPrimitiveDescriptor.DynamicOrderPrimitiveQuantity: - { + case DynamicOrderPrimitiveDescriptor.DynamicOrderPrimitiveQuantity: { var qm = new DynamicOrderQuantityVM(descriptor); return attached(qm, DynamicOrderHelper.get_quantity_value, DynamicOrderHelper.set_quantity_value, new QuantityMeasure { Ratio = 1.0, Absolute = 1 }); } @@ -88,16 +89,16 @@ namespace mbts.spark { default: throw ExceptionBuilder.Argument.unexpected_case(descriptor.ParamType.DescriptorCase, nameof(descriptor)); } - DynamicOrderParamVM mk(int field_number, Func convert, Func convert_back, TView default_value = default) { + DynamicOrderParamVM mk(int field_number, Func convert, Func convert_back, TView default_value = default, string template_key = null) { var fdesc = DynamicOrderParam.Descriptor.FindFieldByNumber(field_number); var pvm = new DynamicOrderParamVM(descriptor, fdesc, convert, convert_back); - return attached>(pvm, get_from_param, assign_to_param, default_value); + return attached>(pvm, get_from_param, assign_to_param, default_value, template_key); TView get_from_param(DynamicOrderParam param) => convert(ConvertUtils.try_convert(fdesc.Accessor.GetValue(param))); void assign_to_param(DynamicOrderParam param, TView value) => fdesc.Accessor.SetValue(param, convert_back(value)); } - TParam attached(TParam pvm, Func selector, Action assign_to_param, T default_value = default) where TParam : DynamicOrderParamVM, IDynamicOrderParamVM { + TParam attached(TParam pvm, Func selector, Action assign_to_param, T default_value = default, string template_key = null) where TParam : DynamicOrderParamVM, IDynamicOrderParamVM { add_property( pvm.descriptor.ParamName, mdef => mdef.OrderParams.TryGetValue(pvm.descriptor.ParamName, out DynamicOrderParam p) ? selector(p) : default_value, @@ -105,7 +106,8 @@ namespace mbts.spark { () => pvm.value, v => pvm.value = v, pvm.subscribe_value(), - default_value); + default_value, + template_key: template_key); return pvm; } } diff --git a/src/client/spark/Orders/OrderTypeVMs/OrderTypeVMFactory.cs b/src/client/spark/Orders/OrderTypeVMs/OrderTypeVMFactory.cs index b24547c8e..4243de0be 100644 --- a/src/client/spark/Orders/OrderTypeVMs/OrderTypeVMFactory.cs +++ b/src/client/spark/Orders/OrderTypeVMs/OrderTypeVMFactory.cs @@ -89,6 +89,7 @@ namespace mbts.spark { case AlgoType.Dynamic: vm = dynamic(key, name, name, editing_permissions); break; case AlgoType.Splitter: vm = splitter(key,name,editing_permissions); break; case AlgoType.Composite: vm = composite(key, name, editing_permissions); break; + case AlgoType.None: vm = new DefaultAlgoVM(); break; default: vm = generic(key, name, algo_type, editing_permissions); break; //default: // Log.Ui.debug_fatal($"Failed to create {typeof(IAlgoVM)}: unknown algo type '{algo_type}.'"); @@ -166,7 +167,8 @@ namespace mbts.spark { }; void subscribe_new_algo(IAlgoVM old_algo, IAlgoVM new_algo) { RaisePropertyChanged(sd.prop_name); - Disposable.replace(ref def_sub, new CompositeDisposable()); + Disposable.dispose(ref def_sub); + def_sub = new CompositeDisposable(); if (assert(new_algo != null, sd.prop_name, "No algo specified.")) { def_sub.Add(new_algo.subscribe_definition().Subscribe(_ => _def_change_notifier.execute())); if (new_algo is INotifyDataErrorInfo ndei) @@ -255,14 +257,14 @@ namespace mbts.spark { _security_changed += o.OnNext; return Disposable.Create(() => _security_changed -= o.OnNext); }); - Disposable.replace(ref _pending_load, Disposable.Create( + Disposable.dispose(ref _pending_load); + _pending_load = Disposable.Create( // load children ObservableAutoWrapper.connect_item_hooks( _child_algo_selector_data.AsCollection(), sd => security_src.SubscribeEach(_ => ObservableAutoWrapper.connect_item_actions(_child_algo_selector_data.AsCollection(), selector_load_impl))), // load regular props - ObservableAutoWrapper.connect_item_actions(_auto_props.AsCollection(), p => p.load(algom)))); - + ObservableAutoWrapper.connect_item_actions(_auto_props.AsCollection(), p => p.load(algom))); } void selector_load_impl(AlgoSelectorData sd) { @@ -298,11 +300,12 @@ namespace mbts.spark { _security_changed += o.OnNext; return Disposable.Create(() => _security_changed -= o.OnNext); }); - Disposable.replace(ref _pending_load, Disposable.Create( + Disposable.dispose(ref _pending_load); + _pending_load = Disposable.Create( ObservableAutoWrapper.connect_item_hooks( _child_algo_selector_data.AsCollection(), sd => security_src.Subscribe(_ => ObservableAutoWrapper.connect_item_actions(_child_algo_selector_data.AsCollection(), selector_load_impl))), - ObservableAutoWrapper.connect_item_actions(_auto_props.AsCollection(), p => p.load_many(algos)))); + ObservableAutoWrapper.connect_item_actions(_auto_props.AsCollection(), p => p.load_many(algos))); void selector_load_impl(AlgoSelectorData sd) { // **assume that algos has more than 1 element @@ -542,8 +545,19 @@ namespace mbts.spark { } } - protected void add_property(string prop_name, Func get_from_def, Action assign_to_def, Func get_local, Action set_local, IObservable subscribe_local, TValue non_uniform_value = default, AlgoParamPermissions permissions = AlgoParamPermissions.ReadWrite) { - var param = new AlgoParamVM(prop_name, DisplayNameHelper.snake_to_title_case(prop_name), get_local()) { permissions = permissions }; + protected void add_property( + string prop_name, + Func get_from_def, + Action assign_to_def, + Func get_local, + Action set_local, + IObservable subscribe_local, + TValue non_uniform_value = default, + AlgoParamPermissions permissions = AlgoParamPermissions.ReadWrite, + string template_key = null) { + var param = new AlgoParamVM(prop_name, DisplayNameHelper.snake_to_title_case(prop_name), get_local(), template_key) { + permissions = permissions + }; _algo_params.Add(param); _auto_props[prop_name] = new AutoProp(prop_name, def => { @@ -816,12 +830,11 @@ namespace mbts.spark { var default_algo = AlgoKey.from_known_type(OrderDataHelper.get_default_algo(OrderDataHelper.get_algo_format(security.Value.ContractCase))); foreach (var sd in _child_algo_selectors) sd.load_algo_data_recursive(algo_data, default_algo); - Disposable.replace( - ref _selector_data_hooks, - ObservableAutoWrapper.connect_item_hooks(child_algo_selectors, s => { - s.load_algo_data_recursive(algo_data, default_algo); - return Disposable.empty; - })); + Disposable.dispose(ref _selector_data_hooks); + _selector_data_hooks = ObservableAutoWrapper.connect_item_hooks(child_algo_selectors, s => { + s.load_algo_data_recursive(algo_data, default_algo); + return Disposable.empty; + }); } } diff --git a/src/client/spark/Orders/OrderUtils.cs b/src/client/spark/Orders/OrderUtils.cs index 2b4773a67..3c78d22b7 100644 --- a/src/client/spark/Orders/OrderUtils.cs +++ b/src/client/spark/Orders/OrderUtils.cs @@ -158,7 +158,9 @@ namespace mbts.spark { public static string display_name(this OrderSecurity order_tuple) => display_name(order_tuple.order, order_tuple.algo); - public static string display_name(OrderRecordImmutable order, AlgoData? algo) { + public static string display_name(OrderRecordImmutable order, AlgoData? algo, SecurityImmutable? security = null) { + if (order.order_type() == CustomOrderDefinition.CustomOneofCase.Spreader && security.HasValue) + return security.Value.ToString(); if (algo.HasValue) { return algo.Value.name; } else if (order.HasDef && order.Def.HasCustomDef) { @@ -250,7 +252,7 @@ namespace mbts.spark { order_type.ToString().ToUpper(); public static string display_name(this OrderSecurityAlgo order_sec_algo) => - display_name(order_sec_algo.order, order_sec_algo.algo); + display_name(order_sec_algo.order, order_sec_algo.algo, order_sec_algo.security); public static string display_name(this CustomOrderDefinitionImmutable algo) => algo.CustomCase == CustomOrderDefinition.CustomOneofCase.Dynamic ? algo.Dynamic.DynamicOrderType : algo.CustomCase.display_name(); diff --git a/src/client/spark/Orders/OrdersModel.cs b/src/client/spark/Orders/OrdersModel.cs index ffeceeb34..ccc189f82 100644 --- a/src/client/spark/Orders/OrdersModel.cs +++ b/src/client/spark/Orders/OrdersModel.cs @@ -210,6 +210,7 @@ namespace mbts.spark { // manual fills on_manual_fills_securities = _position_service.subscribei_manual_fills() .Naggle(interval, scheduler) + .Filter(f => f.FillType == ManualFill.Types.FillType.Local) .join(sstore, mf => mf.SecurityKey) .Table().Cache(); var manual_fill_tuples_account_join = on_manual_fills_securities.LeftJoin( diff --git a/src/client/spark/Predicates/Data/Events/OrderSentEventPredicate.cs b/src/client/spark/Predicates/Data/Events/OrderSentEventPredicate.cs new file mode 100644 index 000000000..f16dc8ed8 --- /dev/null +++ b/src/client/spark/Predicates/Data/Events/OrderSentEventPredicate.cs @@ -0,0 +1,80 @@ +using bts.api; +using bts.api.orderman; +using bts.shell; +using bts.utils; +using mbts.spark; +using mbts.ui.settings; +using ProtoBuf; +using System; +using System.Collections.Generic; +using System.ComponentModel; + +namespace mbts { + static class OrderSentEventPredicateVMFactory { + public static Dictionary> get_oneofs(IBus bus) => + new Dictionary> { + { OrderSentEventPredicate.PredicateTypeOneofCase.AnyOrder, PredicateVMWrapper.create(p => p.security = null) }, + { OrderSentEventPredicate.PredicateTypeOneofCase.SecurityOrder, SecurityCriteriaVMFactory.create(bus, o => o.security?.is_value ?? new SecurityCriteria(), + (p, s) => { + if (p.security == null) { + p.security = new SecurityPredicate(); + } + p.security.is_value = s; + }) + } + }; + } + + [ProtoContract] + class OrderSentEventPredicate : IEquatable { + static readonly int _type_hash = typeof(OrderSentEventPredicate).GetHashCode(); + // arbitrary short time period (can override in config) + static readonly TimeSpan _stale_order_threshold = TimeSpan.FromSeconds(config.Configuration.get().stale_order_threshold_seconds); + + public enum PredicateTypeOneofCase { + [Description("(any order)")] + AnyOrder, + [Description("choose securities")] + SecurityOrder + } + + [ProtoMember(1)] + public SecurityPredicate security { get; set; } + + public PredicateTypeOneofCase predicate_type_case { + get { + if (security != null) + return PredicateTypeOneofCase.SecurityOrder; + return PredicateTypeOneofCase.AnyOrder; + } + } + + public IPredicate create(object obj) => + create_predicate((INotificationServiceDataCache)obj); + + public IPredicate create_predicate(INotificationServiceDataCache data_cache) { + var sec_pred = security?.create(); + return PredicateBuilder.create(os => + // verify security + (sec_pred == null || (os.security.HasValue && sec_pred.trigger(os.security.Value))) && + // verify not stale + ((DateTime.Now - os.order.created()) < _stale_order_threshold) && + // verify order hasn't been added yet + // Note: the notification service should offer a way to connect predicates so that they only receive items in TableUpdate.added, + // however, the notification service predated TableUpdate.Changes, so for now, just check that the history.Count == 1) + (data_cache.order_history.TryGetValue(os.order.Key, out var hist) && hist.Count == 1)); + } + + public INotificationPredicate create_toast(INotificationServiceDataCache data_cache) => + throw new NotImplementedException(); + + public bool Equals(OrderSentEventPredicate other) => + other != null && other.security.safe_equals(other.security); + + public override bool Equals(object obj) => + Equals(obj as OrderSentEventPredicate); + + public override int GetHashCode() => + EqualityComparer.auto_phase(_type_hash, security?.GetHashCode() ?? 0); + } +} \ No newline at end of file diff --git a/src/client/spark/Predicates/Data/OrderRecordTuplePredicate.cs b/src/client/spark/Predicates/Data/OrderRecordTuplePredicate.cs index c137f7d0b..be4a346fb 100644 --- a/src/client/spark/Predicates/Data/OrderRecordTuplePredicate.cs +++ b/src/client/spark/Predicates/Data/OrderRecordTuplePredicate.cs @@ -5,14 +5,9 @@ using bts.utils; using mbts.spark; using mbts.ui; using mbts.ui.settings; -using mbts.ui.utility; -using mbts.utils; using ProtoBuf; using System; using System.Collections.Generic; -using System.Linq; -using System.Reactive.Linq; -using OrderType = bts.api.orderman.CustomOrderDefinition.CustomOneofCase; using PTOC = mbts.OrderRecordTuplePredicate.PredicateTypeOneofCase; namespace mbts { @@ -20,6 +15,7 @@ namespace mbts { // TODO: uncomment the remaining cases when ready public static Dictionary> get_oneofs(IBus bus, OrdersModel orders_model) { return new Dictionary> { + { PTOC.SentEvent, wrap(rp => rp.sent_event, (rp, p) => rp.sent_event = p, p => p.predicate_type_case, OrderSentEventPredicateVMFactory.get_oneofs(bus)) }, { PTOC.FilledEvent, wrap(rp => rp.filled_event, (rp, p) => rp.filled_event = p, p => p.predicate_type_case, OrderFilledEventPredicateVMFactory.get_oneofs(bus)) }, { PTOC.CompletedEvent, wrap(rp => rp.completed_event, (rp, p) => rp.completed_event = p, p => p.predicate_type_case, OrderCompletedEventPredicateVMFactory.get_oneofs(bus)) }, { PTOC.CanceledEvent, wrap(rp => rp.canceled_event, (rp, p) => rp.canceled_event = p, p => p.predicate_type_case, OrderCanceledEventPredicateVMFactory.get_oneofs(bus)) }, @@ -77,6 +73,7 @@ namespace mbts { OrderFilledEventPredicate _filled_event; OrderCompletedEventPredicate _completed_event; OrderCanceledEventPredicate _canceled_event; + OrderSentEventPredicate _sent_event; public enum PredicateTypeOneofCase { None, @@ -120,6 +117,8 @@ namespace mbts { OrderKey, [PredicateCase(is_toastable:false)] ParentKey, + [PredicateCase("When order is sent", PredicateCategory.Event)] + SentEvent, [PredicateCase("When order gets a fill", PredicateCategory.Event)] FilledEvent, [PredicateCase("When order is completed", PredicateCategory.Event)] @@ -176,6 +175,8 @@ namespace mbts { public OrderCompletedEventPredicate completed_event { get => _completed_event; set => _set(ref _completed_event, value); } [ProtoMember(23)] public OrderCanceledEventPredicate canceled_event { get => _canceled_event; set => _set(ref _canceled_event, value); } + [ProtoMember(26)] + public OrderSentEventPredicate sent_event { get => _sent_event; set => _set(ref _sent_event, value); } [ProtoMember(24)] public bool is_toast_enabled { get; set; } [ProtoMember(25)] @@ -206,6 +207,7 @@ namespace mbts { if (_filled_event != null) return PTOC.FilledEvent; if (_completed_event != null) return PTOC.CompletedEvent; if (_canceled_event != null) return PTOC.CanceledEvent; + if (_sent_event != null) return PTOC.SentEvent; if (_attached_target_order_key != null) return PTOC.AttachedTargetOrderKey; return PTOC.None; } @@ -236,6 +238,7 @@ namespace mbts { case PTOC.FilledEvent: return _filled_event.create_predicate(data_cache); case PTOC.CompletedEvent: return _completed_event.create_predicate(data_cache); case PTOC.CanceledEvent: return _canceled_event.create(data_cache); + case PTOC.SentEvent: return _sent_event.create(data_cache); case PTOC.AttachedTargetOrderKey: return wrap(_attached_target_order_key.create(), r => r.order.attached_target_order_key()); default: return null; } @@ -285,6 +288,7 @@ namespace mbts { case PTOC.FilledEvent: return _filled_event.Equals(other._filled_event); case PTOC.CompletedEvent: return _completed_event.Equals(other._completed_event); case PTOC.CanceledEvent: return _canceled_event.Equals(other._canceled_event); + case PTOC.SentEvent: return _sent_event.Equals(other._sent_event); case PTOC.AttachedTargetOrderKey: return _attached_target_order_key.Equals(other._attached_target_order_key); case PTOC.None: return true; default: throw ExceptionHelper.unexpected_oneof_case(ptoc); @@ -320,6 +324,7 @@ namespace mbts { case PTOC.FilledEvent: raw_hash = _filled_event.GetHashCode(); break; case PTOC.CompletedEvent: raw_hash = _completed_event.GetHashCode(); break; case PTOC.CanceledEvent: raw_hash = _canceled_event.GetHashCode(); break; + case PTOC.SentEvent: raw_hash = _sent_event.GetHashCode(); break; case PTOC.AttachedTargetOrderKey: raw_hash = _attached_target_order_key.GetHashCode(); break; case PTOC.None: raw_hash = 0; break; default: throw ExceptionHelper.unexpected_oneof_case(ptoc); @@ -355,6 +360,7 @@ namespace mbts { _filled_event = null; _completed_event = null; _canceled_event = null; + _sent_event = null; _attached_target_order_key = null; } field = value; diff --git a/src/client/spark/core/GlobalExtensions/DoubleHelper.cs b/src/client/spark/core/GlobalExtensions/DoubleHelper.cs index 747747963..4b9292936 100644 --- a/src/client/spark/core/GlobalExtensions/DoubleHelper.cs +++ b/src/client/spark/core/GlobalExtensions/DoubleHelper.cs @@ -1,7 +1,36 @@ -using System.Linq; +using System; +using System.Linq; namespace mbts { public static class DoubleHelper { + public static bool any_nan_or_infinity(params double[] values) { + for (int i = 0; i < values.Length; i++) { + if (double.IsNaN(values[i]) || double.IsInfinity(values[i])) + return true; + } + return false; + } + + public static bool are_close(double v1, double v2, double threshold) => + !any_nan_or_infinity(v1, v2, threshold) && Math.Abs(threshold) > Math.Abs(v1 - v2); + + // bc double.IsNaN gets annoying write, and a lazy programmer is a happy programmer ;) + public static bool is_nan(this double value) => + double.IsNaN(value); + + /// + /// If the specified value is a , , or , returns the equivalent ; otherwise, returns . + /// + /// The value to convert to a . + /// The equivalent value if the cast succeeds, otherwise . + public static double nan_cast(object value) { + // devs: add numeric type checks as needed + if (value is double d) return d; + if (value is int i) return i; + if (value is uint u) return u; + return double.NaN; + } + public static bool IsNaNOrInfinity(double d) => double.IsNaN(d) || double.IsInfinity(d); public static bool IsNaNOrInfinityOr(double d, params double[] values) => IsNaNOrInfinity(d) || values.Contains(d); public static bool IsOneOf(double d, params double[] values) => values.Contains(d); diff --git a/src/client/spark/core/Utility/MiscUtils.cs b/src/client/spark/core/Utility/MiscUtils.cs index 047eff9f3..4aa6074c8 100644 --- a/src/client/spark/core/Utility/MiscUtils.cs +++ b/src/client/spark/core/Utility/MiscUtils.cs @@ -102,14 +102,14 @@ namespace mbts.utils string identifier = ((felem.Name != null) && (felem.Name.Length > 0)) ? felem.Name : felem.GetType().ToString(); - return "$FrameworkElement [{identifier}]"; + return $"FrameworkElement [{identifier}]"; } else { // Maybe a FrameworkContentElement has focus if (elem is FrameworkContentElement fcelem) { string identifier = ((fcelem.Name != null) && (fcelem.Name.Length > 0)) ? fcelem.Name : fcelem.GetType().ToString(); - return "$FrameworkContentElement [{identifier}]"; + return $"FrameworkContentElement [{identifier}]"; } return $"Element of type [{elem.GetType().ToString()}] has focus"; } diff --git a/src/client/spark/view/Controls/DIYGrid/Columns/ColumnBase.cs b/src/client/spark/view/Controls/DIYGrid/Columns/ColumnBase.cs index c2c8db794..9e8e44cda 100644 --- a/src/client/spark/view/Controls/DIYGrid/Columns/ColumnBase.cs +++ b/src/client/spark/view/Controls/DIYGrid/Columns/ColumnBase.cs @@ -114,6 +114,7 @@ namespace mbts.ui.controls { Aggressor = "Aggressor", Volume = "Volume", Last = "Last", + S = "S", Pos = "Pos", Working = "Working", Bid = "Bid", @@ -124,6 +125,7 @@ namespace mbts.ui.controls { SessionLow = "Low", SessionHigh = "High", FillKind = "Fill Kind", + FillStatus = "Fill Status", Name = "Name", Description = "Description", N_Legs = "# Legs", diff --git a/src/client/spark/view/Controls/DIYGrid/DoubleUtil.cs b/src/client/spark/view/Controls/DIYGrid/DoubleUtil.cs index aa2019135..cce6cb9bc 100644 --- a/src/client/spark/view/Controls/DIYGrid/DoubleUtil.cs +++ b/src/client/spark/view/Controls/DIYGrid/DoubleUtil.cs @@ -4,6 +4,8 @@ using System.Linq; namespace mbts.ui.controls { static class DoubleUtil { public const int API_DECIMAL_PRECISION = 9; + // arbitrary small number that can be used to determine whether or not scroll changes are significant + public const double MIN_SCROLL_DELTA = 0.000015; /* smallest such that 1.0+DBL_EPSILON != 1.0 */ private const double EPSILON = 2.2204460492503131e-016; private const double BIG_EPSILON = EPSILON * 10; diff --git a/src/client/spark/view/Controls/Order/AlgoContentTemplateSelector.xaml b/src/client/spark/view/Controls/Order/AlgoContentTemplateSelector.xaml index a1e3937f2..1a522bcce 100644 --- a/src/client/spark/view/Controls/Order/AlgoContentTemplateSelector.xaml +++ b/src/client/spark/view/Controls/Order/AlgoContentTemplateSelector.xaml @@ -1139,6 +1139,30 @@ + + + + + + + + + + + + + @@ -1383,13 +1407,11 @@ diff --git a/src/client/spark/view/Converters/AnonymousValueConverter.cs b/src/client/spark/view/Converters/AnonymousValueConverter.cs index 29ba7db43..2e1df9262 100644 --- a/src/client/spark/view/Converters/AnonymousValueConverter.cs +++ b/src/client/spark/view/Converters/AnonymousValueConverter.cs @@ -11,6 +11,9 @@ namespace mbts.ui.converters { public static IValueConverter create(Func convert) => new AnonymousValueConverter(convert); + public static IValueConverter create(Func convert, Func convert_back) => + new AnonymousValueConverter(convert, convert_back); + public static IMultiValueConverter create(Func convert) => new AnonymousMultiValueConverter(convert); @@ -28,9 +31,17 @@ namespace mbts.ui.converters { IntTextConverter = new IntTextConverter(), UIntTextConverter = new UIntTextConverter(), QuantityMeasureTextConverter = ValueConverter.create(fmt), + SecuritySelectorText, SimpleString = new AnonymousValueConverter(obj => obj?.ToString()), ThicknessToDouble = new ThicknessToDoubleConverter(); + static ValueConverters() { + SecuritySelectorText = new NullSubstitutionConverterWrapper { + Converter = SimpleString, + ValueForNull = "(None)" + }; + } + public static readonly IMultiValueConverter AreEqualConverter = new AreEqualConverter(); diff --git a/src/client/spark/view/Converters/DictionaryLookupConverter.cs b/src/client/spark/view/Converters/DictionaryLookupConverter.cs new file mode 100644 index 000000000..cdc635b02 --- /dev/null +++ b/src/client/spark/view/Converters/DictionaryLookupConverter.cs @@ -0,0 +1,116 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Windows.Data; + +namespace mbts.ui.converters { + class DictionaryLookupConverter : IMultiValueConverter { + [ThreadStatic] + Dictionary _type_impls_backing; + Dictionary _type_impls => _type_impls_backing ?? (_type_impls_backing = new Dictionary()); + + delegate bool Lookup(object source, object key, out object result); + + public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) { + if (values.Length == 2 && values[0] != null && values[1] != null) { + var lookup_type = values[0].GetType(); + var lookup = _type_impls.GetOrAdd(lookup_type, TypeImplResolver.resolve); + if (lookup(values[0], values[1], out var result)) + return result; + } + return null; + } + + public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) => + throw new NotImplementedException(); + + static class TypeImplResolver { + public static Lookup resolve(Type lookup_type) => + _prioritized_impls(lookup_type) + .OrderBy(tup => tup.priority) + .Select(tup => tup.impl) + .FirstOrDefault() ?? _lookup_fail; + + static bool _lookup_fail(object source, object key, out object result) { + result = null; + return false; + } + + static IEnumerable<(int priority, Lookup impl)> _prioritized_impls(Type lookup_type) { + foreach (var itype in lookup_type.GetInterfaces()) { + if (itype.IsGenericType) { + var gen_def = itype.GetGenericTypeDefinition(); + if (gen_def == typeof(IDictionary<,>)) + yield return (1, _get_generic_impl(nameof(_GenericImpls.idictionary), itype)); + if (gen_def == typeof(IReadOnlyDictionary<,>)) + yield return (2, _get_generic_impl(nameof(_GenericImpls.ireadonly_dictionary), itype)); + if (gen_def == typeof(ILookup<,>)) + yield return (3, _get_generic_impl(nameof(_GenericImpls.ilookup), itype)); + } + if (itype == typeof(IDictionary)) + yield return (4, _NonGenericImpls.idictionary); + } + } + + static Lookup _get_generic_impl(string impl_name, Type impl_type) => + _get_generic_impl(impl_name, impl_type.GetGenericArguments()); + + static Lookup _get_generic_impl(string impl_name, Type[] key_value_types) => + (Lookup)typeof(_GenericImpls<,>) + .MakeGenericType(key_value_types) + .GetField(nameof(_GenericImpls.idictionary)) + .GetValue(null); + + static class _GenericImpls { + public static readonly Lookup + idictionary = _idictionary, + ireadonly_dictionary = _ireadonly_dictionary, + ilookup = _ilookup; + + static bool _idictionary(object lookup, object key, out object result) { + if (key is TKey k && lookup is IDictionary d && d.TryGetValue(k, out var value)) { + result = value; + return true; + } + result = null; + return false; + } + + static bool _ireadonly_dictionary(object lookup, object key, out object result) { + if (key is TKey k && lookup is IReadOnlyDictionary rod && rod.TryGetValue(k, out var value)) { + result = value; + return true; + } + result = null; + return false; + } + + static bool _ilookup(object lookup, object key, out object result) { + if ((key is TKey k) && lookup is ILookup l && l.Contains(k)) { + result = l[k]; + return true; + } + result = null; + return false; + } + } + + static class _NonGenericImpls { + public static readonly Lookup + idictionary = _idictionary; + + static bool _idictionary(object lookup, object key, out object result) { + var dict = (IDictionary)lookup; + if (dict.Contains(key)) { + result = dict[key]; + return true; + } + result = null; + return false; + } + } + } + } +} \ No newline at end of file diff --git a/src/client/spark/view/Extensions/FrameworkElementExtensions.cs b/src/client/spark/view/Extensions/FrameworkElementExtensions.cs index 5e1fca80f..56bc99d47 100644 --- a/src/client/spark/view/Extensions/FrameworkElementExtensions.cs +++ b/src/client/spark/view/Extensions/FrameworkElementExtensions.cs @@ -9,6 +9,29 @@ using CompositeDisposable = System.Reactive.Disposables.CompositeDisposable; namespace mbts.ui.extensions { static class FrameworkElementHelper { + /// + /// Invokes the specified action immediately if the element is already loaded, otherwise hooks up to the event, + /// and [conditionally]invokes the specified action if returns true. + /// + /// The element that must be loaded before the specified action is invoked. + /// The action to invoke when the element is loaded. + /// If the element is not already loaded, this delegate determines whether or not to invoke the action in the Loaded event callback. + /// + public static void InvokeOnLoaded(this FrameworkElement element, Action action, Func should_still_execute) { + if (action == null) + throw new ArgumentNullException(nameof(action)); + if (element.IsLoaded) { + action(); + } else { + element.Loaded += on_loaded; + void on_loaded(object sender, RoutedEventArgs e) { + element.Loaded -= on_loaded; + if (should_still_execute()) + action(); + } + } + } + public static Point GetCenter(this FrameworkElement element) => new Point(element.ActualWidth * 0.5, element.ActualHeight * 0.5); diff --git a/src/client/spark/view/Extensions/ScrollViewerBehavior.cs b/src/client/spark/view/Extensions/ScrollViewerBehavior.cs new file mode 100644 index 000000000..e9db14fd4 --- /dev/null +++ b/src/client/spark/view/Extensions/ScrollViewerBehavior.cs @@ -0,0 +1,61 @@ +using mbts.ui.controls; +using mbts.ui.extensions; +using System.Windows; +using System.Windows.Controls; + +namespace mbts.ui { + static class ScrollViewerBehavior { + public static readonly DependencyProperty VerticalOffsetProperty = + DependencyProperty.RegisterAttached("VerticalOffset", typeof(double), typeof(ScrollViewerBehavior), new PropertyMetadata(double.NaN, OnVerticalOffsetChanged)); + + static void OnVerticalOffsetChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { + if (d is ScrollViewer scroll) { + double voff = DoubleHelper.nan_cast(e.NewValue); + // Ignore small deltas to prevent strange layout rounding issues/loops. + // Be mindful that small deltas are not accumulated into a single larger delta. + if (!double.IsNaN(voff) && !DoubleHelper.are_close(DoubleHelper.nan_cast(e.OldValue), voff, DoubleUtil.MIN_SCROLL_DELTA)) { + scroll.InvokeOnLoaded(() => scroll.ScrollToVerticalOffset(voff), + () => scroll.GetValue(VerticalOffsetProperty) is double curr_voff && curr_voff == voff); + } + } + } + + public static double GetVerticalOffset(DependencyObject d) => (double)d.GetValue(VerticalOffsetProperty); + public static void SetVerticalOffset(DependencyObject d, double value) => d.SetValue(VerticalOffsetProperty, value); + + public static readonly DependencyProperty EnableTwoWayVerticalOffsetBindingProperty = + DependencyProperty.RegisterAttached("EnableTwoWayVerticalOffsetBinding", typeof(bool), typeof(ScrollViewerBehavior), new PropertyMetadata(false, OnEnableTwoWayVerticalOffsetBindingChanged)); + + static void OnEnableTwoWayVerticalOffsetBindingChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { + if (d is ScrollViewer scroll) { + if ((bool)e.NewValue) { + scroll.ScrollChanged += EnableTwoWayVerticalOffsetBindingUtils.on_scroll_changed; + } else { + scroll.ScrollChanged -= EnableTwoWayVerticalOffsetBindingUtils.on_scroll_changed; + } + } + } + + public static bool GetEnableTwoWayVerticalOffsetBinding(DependencyObject d) => (bool)d.GetValue(EnableTwoWayVerticalOffsetBindingProperty); + public static void SetEnableTwoWayVerticalOffsetBinding(DependencyObject d, bool value) => d.SetValue(EnableTwoWayVerticalOffsetBindingProperty, value); + + public static readonly DependencyProperty VerticalOffsetMirrorBusyProperty = + DependencyProperty.RegisterAttached("VerticalOffsetMirrorBusy", typeof(bool), typeof(ScrollViewerBehavior), new PropertyMetadata(false)); + + static class EnableTwoWayVerticalOffsetBindingUtils { + public static void on_scroll_changed(object sender, ScrollChangedEventArgs e) { + if (e.VerticalChange != 0 && sender is ScrollViewer scroll) { + if (!(bool)scroll.GetValue(VerticalOffsetMirrorBusyProperty)) { + scroll.SetValue(VerticalOffsetMirrorBusyProperty, true); + // scroll changes could get noisy while layouts are being processed; + // let everything settle down before updating the VerticalOffset binding sources/targets. + scroll.Dispatcher.InvokeAsync(() => { + scroll.SetCurrentValue(VerticalOffsetProperty, scroll.VerticalOffset); + scroll.SetValue(VerticalOffsetMirrorBusyProperty, false); + }, System.Windows.Threading.DispatcherPriority.ApplicationIdle); + } + } + } + } + } +} \ 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 0e5924fd2..3b48769a0 100644 --- a/src/client/spark/view/Extensions/UIElementExtensions.cs +++ b/src/client/spark/view/Extensions/UIElementExtensions.cs @@ -236,6 +236,14 @@ namespace mbts.ui.extensions { target.ScrollChanged += callback; return Disposable.Create(() => target.ScrollChanged -= callback); } + + public static bool TryScrollToVerticalOffset(this ScrollViewer scroll_viewer, double vertical_offset) { + if (vertical_offset >= 0 && scroll_viewer.IsLoaded && vertical_offset < scroll_viewer.ExtentHeight - scroll_viewer.ViewportHeight) { + scroll_viewer.ScrollToVerticalOffset(vertical_offset); + return true; + } + return false; + } } public static class TextBoxExtensions { diff --git a/src/client/spark/view/Resources/ResourceKeys.cs b/src/client/spark/view/Resources/ResourceKeys.cs new file mode 100644 index 000000000..634de738b --- /dev/null +++ b/src/client/spark/view/Resources/ResourceKeys.cs @@ -0,0 +1,9 @@ +namespace mbts.ui { + static class ResourceKeys { + public const string + Securities = nameof(Securities), + Accounts = nameof(Accounts), + Users = nameof(Users), + Sides = nameof(Sides); + } +} \ No newline at end of file diff --git a/src/client/spark/view/Resources/TemplateKeys.cs b/src/client/spark/view/Resources/TemplateKeys.cs new file mode 100644 index 000000000..d376785ed --- /dev/null +++ b/src/client/spark/view/Resources/TemplateKeys.cs @@ -0,0 +1,6 @@ +namespace mbts.ui { + static class TemplateKeys { + public const string + SecurityKey = nameof(SecurityKey); + } +} \ No newline at end of file diff --git a/src/client/spark/view/Shell/Settings/ShellSettingsDialogVM.cs b/src/client/spark/view/Shell/Settings/ShellSettingsDialogVM.cs index b51a4989a..6cc76049e 100644 --- a/src/client/spark/view/Shell/Settings/ShellSettingsDialogVM.cs +++ b/src/client/spark/view/Shell/Settings/ShellSettingsDialogVM.cs @@ -130,8 +130,10 @@ namespace mbts.spark { DialogSkirtItem _apply_item, _ok_item, _cancel_item; MenuItemVM _import_item, _export_item, _load_factory_item; - public ProfiledShellSettingsDialogVM(ISettingsProfileService profile_service, Func get_fallback, Func vm_factory, Func normalize, T init, string file_ext, uint settings_id = 0) { + public ProfiledShellSettingsDialogVM(ISettingsProfileService profile_service, Func get_fallback, Func vm_factory, Func normalize, T init, string file_ext, Func profile_equals = null, uint settings_id = 0) { _vm_factory = vm_factory; + if (profile_equals == null) + profile_equals = EqualityComparer.Default.Equals; // profile collections var skip_local = profiles.WrapSkip(1); var skip_local_wrapper = skip_local.WrapSelector( @@ -185,12 +187,19 @@ namespace mbts.spark { continue; i = ~i; var temp = ns.with(value: normalize(ns.value)); - skip_local_wrapper.Insert(i, temp); if (settings_id == temp.id) { - if (temp.value.Equals(init)) - selected_profile = skip_local[i]; settings_id = 0; + if (profile_equals(temp.value, init)) { + if (!temp.value.Equals(init)) { + // they aren't exactly equal (don't overwrite any props in 'init' - use it to create the vm) + temp = temp.with(value: init); + } + skip_local_wrapper.Insert(i, temp); + selected_profile = skip_local[i]; + continue; + } } + skip_local_wrapper.Insert(i, temp); } })); diff --git a/src/client/spark/view/Spark/ContractPicker/ContractPickerModel.cs b/src/client/spark/view/Spark/ContractPicker/ContractPickerModel.cs index ad43e7aee..b4f3aa76b 100644 --- a/src/client/spark/view/Spark/ContractPicker/ContractPickerModel.cs +++ b/src/client/spark/view/Spark/ContractPicker/ContractPickerModel.cs @@ -20,8 +20,8 @@ namespace mbts.spark { readonly IRaiseCanExecuteChangedCommand _add_criteria_command, _remove_criteria_command, _ok_button_command; bool _allow_empty, _is_edit_mode; - SecurityImmutable? _pending_sec; - (SecurityCriteria criteria, SecurityImmutable? sec, SecurityMD? md)? _snapshot; + uint? _pending_sec_key; + (SecurityCriteria criteria, uint? sec, SecurityMD? md)? _snapshot; public ContractPickerModel(IBus bus, bool edit_mode, SecurityCriteria existing_criteria, SecurityCriteria world_view_filter = null) : this(bus.subscribe(SecuritiesTag), edit_mode, existing_criteria, world_view_filter) { } @@ -209,9 +209,9 @@ namespace mbts.spark { _update_security_types(); _update_symbols(); _update_securities(); - if (_pending_sec.HasValue && changed_securities.ContainsKey(_pending_sec.Value.Id)) { - select_security_request?.Invoke(_pending_sec.Value); - _pending_sec = null; + if (_pending_sec_key.HasValue && changed_securities.TryGetValue(_pending_sec_key.Value, out var smd)) { + select_security_request?.Invoke(smd.security); + _pending_sec_key = null; } } internal void criteria_selection_changed(IEnumerable selected_clauses) { @@ -229,6 +229,8 @@ namespace mbts.spark { security_criteria_clause_changed?.Invoke(this, e); } + public bool is_editing => _snapshot.HasValue; + public ObservableCollection all_exchanges { get; } = new ObservableCollection(); public List selected_exchanges = new List(); @@ -298,7 +300,22 @@ namespace mbts.spark { if (all_securities.ContainsKey(value.Value.security.Id)) { select_security_request?.Invoke(value.Value.security); } else { - _pending_sec = value.Value.security; + _pending_sec_key = value.Value.security.Id; + } + } + } + } + } + + public uint? selected_security_key { + get => selected_securities.Count == 1 && all_securities.TryGetValue(selected_securities[0], out var smd) ? smd.security.Id : (uint?)null; + set { + if (value != selected_security_key) { + if (value.HasValue) { + if (all_securities.TryGetValue(value.Value, out var smd)) { + select_security_request?.Invoke(smd.security); + } else { + _pending_sec_key = value.Value; } } } @@ -313,7 +330,7 @@ namespace mbts.spark { if (all_securities.ContainsKey(value.Value.Id)) { select_security_request?.Invoke(value.Value); } else { - _pending_sec = value.Value; + _pending_sec_key = value.Value.Id; } } } @@ -336,18 +353,19 @@ namespace mbts.spark { } #region IEditableObject - public void BeginEdit() => _snapshot = (result_criteria, _pending_sec ?? selected_security, selected_security_md); + public void BeginEdit() => _snapshot = (result_criteria, _pending_sec_key ?? selected_security_key, selected_security_md); public void EndEdit() => _snapshot = null; public void CancelEdit() { if (_snapshot.HasValue) { var (criteria, sec, md) = _snapshot.Value; security_criteria_clauses.SetItems(criteria.clauses); - selected_security = sec; + selected_security_key = sec; /// A may have been used to create the snapshot when was called (this happens when securities are loaded lazily). /// If this is the case, pending sec was assigned to the security; /// check if the security is now present in , if so, use that value instead of the null. - selected_security_md = sec.HasValue && !md.HasValue && all_securities.TryGetValue(sec.Value.Id, out var md_match) ? md_match : md; + selected_security_md = sec.HasValue && !md.HasValue && all_securities.TryGetValue(sec.Value, out var md_match) ? md_match : md; _snapshot = null; + RaisePropertyChanged(nameof(selected_security)); _add_criteria_command.RaiseCanExecuteChanged(); _remove_criteria_command.RaiseCanExecuteChanged(); } diff --git a/src/client/spark/view/Spark/ContractPicker/ContractPickerPopup.xaml.cs b/src/client/spark/view/Spark/ContractPicker/ContractPickerPopup.xaml.cs index 49eb0599b..28e76b6c6 100644 --- a/src/client/spark/view/Spark/ContractPicker/ContractPickerPopup.xaml.cs +++ b/src/client/spark/view/Spark/ContractPicker/ContractPickerPopup.xaml.cs @@ -2,6 +2,7 @@ using mbts.spark; using mbts.ui.converters; using mbts.ui.shell; +using mbts.ui.utility; using System; using System.Collections.Generic; using System.Collections.Specialized; @@ -16,8 +17,11 @@ namespace mbts.ui.controls { partial class ContractPickerPopup : PopupControl { public static readonly EventHandler FlushSelectedCriteriaHandler = (s, e) => (s as ContractPickerPopup)?.GetBindingExpression(SelectedCriteriaProperty)?.UpdateSource(), + FlushSelectedSecurityKeyHandler = (s, e) => (s as ContractPickerPopup)?.GetBindingExpression(SelectedSecurityKeyProperty)?.UpdateSource(), FlushSelectedSecurityHandler = (s, e) => (s as ContractPickerPopup)?.GetBindingExpression(SelectedSecurityProperty)?.UpdateSource(); + readonly PropertyUpdateGate _selection_sync_gate = new PropertyUpdateGate(); + IReadOnlyDictionary _cached_security_map = DictionaryHelper.empty(); ContractPickerPopupController _controller; bool _allow_empty, _is_lazy; @@ -97,12 +101,55 @@ namespace mbts.ui.controls { } #endregion + #region SelectedSecurityKey + public static readonly DependencyProperty SelectedSecurityKeyProperty = + WPFHelper.create_dp( + nameof(SelectedSecurityKey), + FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, + (o, v) => { + o.Controller.model.selected_security_key = v; + o._selection_sync_gate.try_update(() => o._sync_selection_props(o.SelectedSecurityMD, o.SelectedSecurity, v, v)); + }); + //(o, base_value) => base_value is uint u && u != 0 ? u : (uint?)null); + + public uint? SelectedSecurityKey { + get => GetValue(SelectedSecurityKeyProperty) as uint?; + set => SetValue(SelectedSecurityKeyProperty, value); + } + #endregion + + enum SecurityProp { + Security, + SecurityMD, + ID + } + + void _sync_selection_props(SecurityMD? curr_sec_md, SecurityImmutable? curr_sec, uint? curr_sec_key, uint? target_sec_key) { + if (curr_sec_key != target_sec_key) { + SetCurrentValue(SelectedSecurityKeyProperty, target_sec_key); + } + bool sec_md_dirty = target_sec_key != (curr_sec_md?.security)?.Id; + bool sec_dirty = target_sec_key != curr_sec?.Id; + if (sec_md_dirty || sec_dirty) { + curr_sec_md = target_sec_key.HasValue && _cached_security_map.TryGetValue(target_sec_key.Value, out var smd) ? smd : (SecurityMD?)null; + if (sec_md_dirty) + SetCurrentValue(SelectedSecurityMDProperty, curr_sec_md); + if (sec_dirty) + SetCurrentValue(SelectedSecurityProperty, curr_sec_md?.security); + } + if (!IsEditMode) + SetCurrentValue(SelectedCriteriaProperty, target_sec_key.HasValue ? SecurityCriteria.from_keys(new[] { target_sec_key.Value }) : null); + } + #region SelectedSecurity public static readonly DependencyProperty SelectedSecurityProperty = WPFHelper.create_dp( nameof(SelectedSecurity), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, - (o, v) => o.Controller.model.selected_security = v); + (o, v) => { + o.Controller.model.selected_security = v; + o._selection_sync_gate.try_update(() => o._sync_selection_props(o.SelectedSecurityMD, v, o.SelectedSecurityKey, v?.Id)); + }); public SecurityImmutable? SelectedSecurity { get => GetValue(SelectedCriteriaProperty) as SecurityImmutable?; @@ -115,7 +162,10 @@ namespace mbts.ui.controls { WPFHelper.create_dp( nameof(SelectedSecurityMD), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, - (o, v) => o.Controller.model.selected_security_md = v); + (o, v) => { + o.Controller.model.selected_security_md = v; + o._selection_sync_gate.try_update(() => o._sync_selection_props(v, o.SelectedSecurity, o.SelectedSecurityKey, (v?.security)?.Id)); + }); public SecurityMD? SelectedSecurityMD { get => GetValue(SelectedCriteriaProperty) as SecurityMD?; @@ -248,12 +298,10 @@ namespace mbts.ui.controls { } void on_cancel() { model.CancelEdit(); - propagate_to_owner(); owner.CancelClick?.Invoke(owner, EventArgs.Empty); } void propagate_to_owner() { - owner.set_current_if_changed(SelectedSecurityMDProperty, owner.Controller.model.selected_security_md); - owner.set_current_if_changed(SelectedSecurityProperty, owner.Controller.model.selected_security); + owner.set_current_if_changed(SelectedSecurityKeyProperty, owner.Controller.model.selected_security_key); owner.set_current_if_changed(SelectedCriteriaProperty, owner.Controller.model.result_criteria); } } @@ -262,4 +310,4 @@ namespace mbts.ui.controls { public SecurityCriteria selected_criteria => _selected_criteria.Value; } } -} \ No newline at end of file +} diff --git a/src/client/spark/view/Utility/BindingProxy.cs b/src/client/spark/view/Utility/BindingProxy.cs new file mode 100644 index 000000000..c00646a04 --- /dev/null +++ b/src/client/spark/view/Utility/BindingProxy.cs @@ -0,0 +1,13 @@ +using System.Windows; + +namespace mbts.ui { + class BindingProxy : FrameworkElement { + public static readonly DependencyProperty ValueProperty = + DependencyProperty.Register(nameof(Value), typeof(object), typeof(BindingProxy)); + + public object Value { + get => GetValue(ValueProperty); + set => SetValue(ValueProperty, value); + } + } +} \ No newline at end of file diff --git a/src/client/spark_api/Co2/MDConsumerHandle.cs b/src/client/spark_api/Co2/MDConsumerHandle.cs index 7b03bdaee..36b560c1a 100644 --- a/src/client/spark_api/Co2/MDConsumerHandle.cs +++ b/src/client/spark_api/Co2/MDConsumerHandle.cs @@ -133,6 +133,12 @@ namespace mbts.co2 { total_volume = _cold->total_volume; return (_cold->last_trade.load_if_new(ref time, out ps)); } + public bool get_last_trade(ref ulong time, out NativePriceSize ps, out DateTime timestamp, out ulong total_volume, out bool from_spread) { + timestamp = _cold->last_trade_time; + total_volume = _cold->total_volume; + from_spread = (_cold->from_spread != 0); + return (_cold->last_trade.load_if_new(ref time, out ps)); + } public ulong misc_timestamp { get { return _cold->misc_timestamp; } } public ulong total_volume { get { return _cold->total_volume; } } @@ -259,7 +265,7 @@ namespace mbts.co2 { // Last trade public AtomicPriceSize last_trade; public ulong total_volume; - public utils.LLDateTime last_trade_time; + public utils.LLDateTime last_trade_time; //// Settlement public UInt64 misc_timestamp; @@ -280,6 +286,7 @@ namespace mbts.co2 { public double session_low_offer; public double fixing_price; + public int from_spread; public int deleted; // FixedSecurity will not be directly addressable. Instead the security is available through the callback public Int64 security_size; diff --git a/src/client/spark_api/utils/Log.cs b/src/client/spark_api/utils/Log.cs index 649f7d202..891548cf1 100644 --- a/src/client/spark_api/utils/Log.cs +++ b/src/client/spark_api/utils/Log.cs @@ -34,7 +34,7 @@ namespace bts { Latency, DataFlow, Plugin, - ClientSdk, + Sdk, Http, // Please keep this list in sync with logging.h/logging.cpp } @@ -103,7 +103,7 @@ namespace bts { internal static Logger Ui => new Logger(Subsystem.Ui); 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 Sdk => new Logger(Subsystem.Sdk); public static Logger Http => new Logger(Subsystem.Http); internal static void log_at(this Logger logger, Level level, string message) { diff --git a/src/client/spark_core/utils/FileUtils.cs b/src/client/spark_core/utils/FileUtils.cs index 6be341755..e210be5a7 100644 --- a/src/client/spark_core/utils/FileUtils.cs +++ b/src/client/spark_core/utils/FileUtils.cs @@ -61,15 +61,17 @@ namespace bts.support { // Delete the oldest files matching pattern (*.txt) keeping {count} files public static void keep_n_matching_files(string directory, string pattern, int count) { - var to_delete = - Directory.GetFiles(directory, pattern) - .Select(f => new { name = f, modified = File.GetLastWriteTimeUtc(f) }) - .OrderBy(f => f.modified) - .Reverse() - .Skip(count); + if (Directory.Exists(directory)) { + var to_delete = + Directory.GetFiles(directory, pattern) + .Select(f => new { name = f, modified = File.GetLastWriteTimeUtc(f) }) + .OrderBy(f => f.modified) + .Reverse() + .Skip(count); - foreach (var f in to_delete) { - try_delete(f.name); + foreach (var f in to_delete) { + try_delete(f.name); + } } } } diff --git a/src/client/spark_core/utils/ObservableExtensions.cs b/src/client/spark_core/utils/ObservableExtensions.cs index 8544f29d0..fd1b102f8 100644 --- a/src/client/spark_core/utils/ObservableExtensions.cs +++ b/src/client/spark_core/utils/ObservableExtensions.cs @@ -212,7 +212,10 @@ namespace System.Reactive.Linq { IDisposable source_sub = source.Select(v => v).Subscribe(on_source); return Disposable.Create(dispose); // local methods - void on_source(T value) => Disposable.replace(ref value_sub, subscribe_value(value)); + void on_source(T value) { + Disposable.dispose(ref value_sub); + value_sub = subscribe_value(value); + } void dispose() { Disposable.dispose(ref source_sub); Disposable.dispose(ref value_sub); diff --git a/src/client/spark_sdk/samples/SDKMain.cs b/src/client/spark_sdk/samples/SDKMain.cs index 9f48e2e54..857db8664 100644 --- a/src/client/spark_sdk/samples/SDKMain.cs +++ b/src/client/spark_sdk/samples/SDKMain.cs @@ -23,7 +23,7 @@ namespace bts.sdk public static void Main(string[] args) { BTS.init(); - Log.ClientSdk.info("Welcome to the BTS Client Side SDK example"); + Log.Sdk.info("Welcome to the BTS Client Side SDK example"); var ctx = new ServiceContext(); string u = args.FirstOrDefault(x => x.Contains("--user=")); diff --git a/src/client/spark_sdk/spark_sdk.csproj b/src/client/spark_sdk/spark_sdk.csproj index e2f93fec3..9ee2d2b4a 100644 --- a/src/client/spark_sdk/spark_sdk.csproj +++ b/src/client/spark_sdk/spark_sdk.csproj @@ -71,10 +71,6 @@ - - - - diff --git a/src/client/spark_test/Co2/Co2Tests.cs b/src/client/spark_test/Co2/Co2Tests.cs index a5f1b292a..af5edc856 100644 --- a/src/client/spark_test/Co2/Co2Tests.cs +++ b/src/client/spark_test/Co2/Co2Tests.cs @@ -142,7 +142,8 @@ namespace mbts.co2.tests Assert.IsTrue(entry.session_low_offer == 5.5); Assert.IsTrue(entry.fixing_price == 6.6); - Assert.IsTrue(entry.deleted == 1); + Assert.IsTrue(entry.from_spread == 0); + Assert.IsTrue(entry.deleted == 1); } [Test] diff --git a/src/client/spark_test/Utils/MarketDataUtilities.cs b/src/client/spark_test/Utils/MarketDataUtilities.cs index 5ab58171a..c08ab7678 100644 --- a/src/client/spark_test/Utils/MarketDataUtilities.cs +++ b/src/client/spark_test/Utils/MarketDataUtilities.cs @@ -2,6 +2,7 @@ using bts.co2; using System; using System.Collections.Generic; +using System.Linq; using System.Text; using System.Threading.Tasks; @@ -39,8 +40,29 @@ namespace bts.tests { return null; } +public static async Task> get_multiple_securities(MarketDataRelay relay, IEnumerable products, SecurityType security_type, MDRequest.Types.Flags flags = MDRequest.Types.Flags.Bbo, Venue v = Venue.Sim) { + var request = new MDRequest() { Host = _MD_HOST }; + request.Channels.AddRange(products.Select(z => + new SecurityChannel() { + Time = 0, + Flags = (uint)flags, + Type = security_type, + Exchange = v, + Symbol = z.Symbol, + TradeCursor = 0, + RfqCursor = 0 + })); + + var result = await relay.requestmd_async(request); + + if (result.success) { + return result.value.Channels.Where(s => s.Updates.Count > 0).Select(x => (SecurityImmutable) x.Updates.First().Security); + } + return Enumerable.Empty(); + } + public static async Task> get_expirations(MarketDataRelay relay, string symbol, SecurityType security_type, MDRequest.Types.Flags flags = MDRequest.Types.Flags.Bbo, Venue v = Venue.Sim) { - var request = new MDRequest() { + var request = new MDRequest() { Host = _MD_HOST, Channels = { new SecurityChannel() { diff --git a/src/client/spark_test/Utils/SpreaderUtilities.cs b/src/client/spark_test/Utils/SpreaderUtilities.cs index 5de24e9a6..6a09b97ca 100644 --- a/src/client/spark_test/Utils/SpreaderUtilities.cs +++ b/src/client/spark_test/Utils/SpreaderUtilities.cs @@ -67,7 +67,12 @@ namespace bts.tests return (s1: sec1.Value, s2: sec2.Value); } - public static async Task<(IEnumerable book, (Security security, ExecutionStrategy strategy) synthetic)> common_setup_one(OrderService order_service, SyntheticSecurityService synthetic_service, (SecurityImmutable? s1, SecurityImmutable? s2) securities, (String name, uint id) account, (String name, uint id) user) { + public static async Task<(IEnumerable book, (Security security, ExecutionStrategy strategy) synthetic)> common_setup_one( + OrderService order_service, + SyntheticSecurityService synthetic_service, + (SecurityImmutable? s1, SecurityImmutable? s2) securities, + (String name, uint id) account, (String name, uint id) user) + { var order_defs = new (OrderDefinition order_def, double price, uint size)[10]; for (int i = 0; i < 4; i ++) { diff --git a/src/client/spark_test/Utils/TestUtils.cs b/src/client/spark_test/Utils/TestUtils.cs index b36693280..8e01b02b9 100644 --- a/src/client/spark_test/Utils/TestUtils.cs +++ b/src/client/spark_test/Utils/TestUtils.cs @@ -85,6 +85,8 @@ namespace bts.tests.utils { if(timeout == null) { timeout = TimeSpan.FromMinutes(2); } + Console.WriteLine("Please Wait... this is going to take some time..."); + await Task.Delay(180000); System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); sw.Start(); @@ -131,6 +133,13 @@ namespace bts.tests.utils { System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch(); watch.Start(); + var prts = mbts.config.Configuration.get(); + Console.WriteLine("\n\n\n\n\n\n\n\n\n\n"); + Console.WriteLine("config_port: " + prts.config_port); + Console.WriteLine("qbus_port: " + prts.qbus_port); + Console.WriteLine("glbx_sim_fix_port: " + prts.glbx_sim_fix_port); + Console.WriteLine("\n\n\n\n\n\n\n\n\n\n"); + var ctx = new ServiceContext(); var mdrelay = ctx.locate(); @@ -143,20 +152,28 @@ namespace bts.tests.utils { products = tmpp.value; break; } + else { + Console.WriteLine("mdrelay.request_products_async Error: "); + Console.WriteLine(tmpp.error_message); + } await Task.Delay(10000); } + //UserSymbol.get_all() in MDConsumer.cs bts.test.Assert.IsTrue(products != null, "No products found"); - var filtered_products = products.Products.Where(z => z.Exchange == Venue.Sim && z.Type == SecurityType.Future); - bts.test.Assert.IsTrue(filtered_products.Count() > 0, "No suitable products found"); + IEnumerable filtered_products = products.Products.Where(z => z.Exchange == Venue.Sim && z.Type == SecurityType.Future); + bts.test.Assert.IsTrue(filtered_products.Count() > 0, "No suitable products found"); //products are not guaranteed to have securities, so I need to get the product from the list of securities uint? sec_id = null; + /* get securities if the list is not empty use the top one */ + for(int i = 0; i < 10; i++) { - var test_security = await MarketDataUtilities.get_security(mdrelay, filtered_products.First().Symbol, SecurityType.Future, f => true); - if(test_security.HasValue) { + var test_security = await MarketDataUtilities.get_multiple_securities(mdrelay, filtered_products, SecurityType.Future); + + if(test_security.Count() > 0) { Console.Write("," + i); - sec_id = test_security.Value.Id; + sec_id = test_security.FirstOrDefault().Id; break; } await Task.Delay(10000); @@ -237,7 +254,7 @@ namespace bts.tests.utils { Result? md_result = await await_with_timeout( md_relay.requestmd_async(md_request), timeout); - + Console.Write(md_result); return md_result != null; } } diff --git a/src/client/spark_test/system_test_scripts/delete_dbs.sh b/src/client/spark_test/system_test_scripts/delete_dbs.sh index 423cb3f23..8b1a24f1f 100644 --- a/src/client/spark_test/system_test_scripts/delete_dbs.sh +++ b/src/client/spark_test/system_test_scripts/delete_dbs.sh @@ -11,7 +11,7 @@ if [ -d "$sparkdata_dir/data" ] then [ -d $sparkdata_dir/data/databases/orders ] && rm -rf $sparkdata_dir/data/databases/orders [ -d $sparkdata_dir/data/databases/fills ] && rm -rf $sparkdata_dir/data/databases/fills - [ -d $sparkdata_dir/data/databases/ex_sim_orders ] && rm -rf $sparkdata_dir/data/databases/ex_sim_orders + [ -d $sparkdata_dir/data/databases/ex_sim_orders_venue_sim ] && rm -rf $sparkdata_dir/data/databases/ex_sim_orders_venue_sim [ -d $sparkdata_dir/data/jetfix ] && rm -rf $sparkdata_dir/data/jetfix [ -d $sparkdata_dir/data/exchange_sim ] && rm -rf $sparkdata_dir/data/exchange_sim diff --git a/src/client/spark_test/system_test_scripts/test_position_limits.sh b/src/client/spark_test/system_test_scripts/test_position_limits.sh index 2c94a2393..93ca9761c 100644 --- a/src/client/spark_test/system_test_scripts/test_position_limits.sh +++ b/src/client/spark_test/system_test_scripts/test_position_limits.sh @@ -30,6 +30,8 @@ cat << EOF > $sparkdata_dir/conf/spark.conf } EOF +echo "Port" +./sparkd configcheck Ports nohup ./sparkd run & pid=$! diff --git a/src/client/sparkcmd/Cmd/OrderStress.cs b/src/client/sparkcmd/Cmd/OrderStress.cs index 643703155..1a0bd48a5 100644 --- a/src/client/sparkcmd/Cmd/OrderStress.cs +++ b/src/client/sparkcmd/Cmd/OrderStress.cs @@ -54,6 +54,7 @@ namespace bts { var result = relay.requestmd(request); foreach (var ch in result.Channels) { + // if (ch.Platform != Platform.Sim) continue; foreach (var update in ch.Updates) { if (update.Security != null) { var m = new Market() { @@ -129,6 +130,7 @@ namespace bts { } var orders = ctx.locate(); + var factor = TimeSpan.TicksPerMillisecond / 1000; foreach (var m in all_markets(ctx)) { ConsoleEx.WriteLine(m.security.security_desc(), ConsoleColor.White); var tick = m.security.ContractSpec.LowTick; @@ -136,7 +138,12 @@ namespace bts { for (var x = 0; x < o.depth; ++x) { var price = m.ask.Value.price() + (tick * x); ConsoleEx.WriteLine($"Sell {o.quantity} at {price}", ConsoleColor.Red); - orders.create_order_async(mkorder(m.security.Id, price, o.quantity, Side.Sell)); + var sw = new System.Diagnostics.Stopwatch(); + sw.Start(); + var r = orders.create_order(mkorder(m.security.Id, price, o.quantity, Side.Sell)); + sw.Stop(); + // Log.Api.info($"Order: id {r.OrderKey} time {sw.ElapsedTicks/factor}us"); + Log.Api.info($"Order {m.security.security_desc()}: id {r.OrderKey} {r.ErrorCode} time {sw.ElapsedTicks/factor}us"); System.Threading.Thread.Sleep(o.sleep); } } @@ -145,7 +152,11 @@ namespace bts { for (var x=0; x < o.depth; ++x) { var price = m.bid.Value.price() - (tick * x); ConsoleEx.WriteLine($"Buy {o.quantity} at {price}", ConsoleColor.Blue); - orders.create_order_async(mkorder(m.security.Id, price, o.quantity, Side.Buy)); + var sw = new System.Diagnostics.Stopwatch(); + sw.Start(); + var r = orders.create_order(mkorder(m.security.Id, price, o.quantity, Side.Buy)); + sw.Stop(); + Log.Api.info($"Order {m.security.security_desc()}: id {r.OrderKey} {r.ErrorCode} time {sw.ElapsedTicks/factor}us"); System.Threading.Thread.Sleep(o.sleep); } } diff --git a/src/client/sparkcmd/Cmd/ServiceTestCommand.cs b/src/client/sparkcmd/Cmd/ServiceTestCommand.cs index 52dcee6cd..87a7b5776 100644 --- a/src/client/sparkcmd/Cmd/ServiceTestCommand.cs +++ b/src/client/sparkcmd/Cmd/ServiceTestCommand.cs @@ -9,6 +9,7 @@ using System.Threading; namespace bts { + [Verb("servicetest", HelpText="Scratch space for testing services")] public class ServiceTestOptions { [Option(nameof(synthetic))] public bool synthetic { get; set; } @@ -16,10 +17,10 @@ namespace bts { public bool user { get; set; } [Option(nameof(admin))] public bool admin { get; set; } - + [Option(nameof(latency))] + public bool latency { get; set; } } - [Verb("servicetest", HelpText="Scratch space for testing services")] public class ServiceTest { public static int Run(ServiceTestOptions o) { if (o.synthetic) { @@ -31,6 +32,9 @@ namespace bts { if (o.admin) { admin_test(); } + if (o.latency) { + latency_test(); + } return 0; } @@ -114,5 +118,21 @@ namespace bts { Thread.Sleep(100); } } + + private static void latency_test() { + var ctx = new ServiceContext(); + var s = ctx.locate(); + + UInt64 sequence = 0; + var factor = TimeSpan.TicksPerMillisecond / 1000; + while(true) { + var sw = new System.Diagnostics.Stopwatch(); + sw.Start(); + var reply = s.ping(new metrics.BufferMessage() { Sequence = ++sequence }); + sw.Stop(); + Console.WriteLine($"{reply.Sequence} - {sw.ElapsedMilliseconds}ms -- {sw.ElapsedTicks/factor} us"); + System.Threading.Thread.Sleep(1000); + } + } } } diff --git a/src/client/sparkcmd/Program.cs b/src/client/sparkcmd/Program.cs index 96b1d2dc8..61b1ba983 100644 --- a/src/client/sparkcmd/Program.cs +++ b/src/client/sparkcmd/Program.cs @@ -31,6 +31,7 @@ namespace sparkcmd { var parser = new Parser(ps => { ps.CaseInsensitiveEnumValues = true; + ps.HelpWriter = Console.Out; }); // note: this can be made to infer the options type list from the list of delegates diff --git a/src/data_files/files/onixs_ice/ConnectivityConfiguration.APITest.xml b/src/data_files/files/onixs_ice/ConnectivityConfiguration.APITest.xml index 334c3c48f..e4d4d7acc 100644 --- a/src/data_files/files/onixs_ice/ConnectivityConfiguration.APITest.xml +++ b/src/data_files/files/onixs_ice/ConnectivityConfiguration.APITest.xml @@ -11,7 +11,6 @@ --> - 63.247.113.163 @@ -19,7 +18,7 @@ bluetrd-ps Starts123 N - Legacy + New @@ -28,7 +27,7 @@ Value from ICE Support Value from ICE Support N - Legacy + New @@ -37,7 +36,7 @@ bluetrdl-ps Starts123 N - Legacy + New diff --git a/src/data_files/files/onixs_ice/ConnectivityConfiguration.PerfTest.xml b/src/data_files/files/onixs_ice/ConnectivityConfiguration.PerfTest.xml index 23680779e..94edae6c4 100644 --- a/src/data_files/files/onixs_ice/ConnectivityConfiguration.PerfTest.xml +++ b/src/data_files/files/onixs_ice/ConnectivityConfiguration.PerfTest.xml @@ -17,10 +17,10 @@ 63.247.113.214 3000 - Value from ICE Support - Value from ICE Support + bt_ps + Starts123 N - Legacy + New diff --git a/src/lib/CMakeDeps_win.cmake b/src/lib/CMakeDeps_win.cmake index 2c14978ea..e058a8be6 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/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;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/sftp.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/subprocess.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;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/sftp.cpp;utils/stats.cpp;utils/tests/AuthTokenTests.cpp;utils/tests/IDGeneratorTests.cpp;utils/tests/SingleWriterConcurrentVectorTests.cpp;utils/thread_manager.cpp) \ No newline at end of file diff --git a/src/lib/api/service_helpers/AdminServiceUtils.cpp b/src/lib/api/service_helpers/AdminServiceUtils.cpp index 80001cbf4..261acaeae 100644 --- a/src/lib/api/service_helpers/AdminServiceUtils.cpp +++ b/src/lib/api/service_helpers/AdminServiceUtils.cpp @@ -46,12 +46,15 @@ bool AdminServiceUtils::has_exchange(const ExchangeConnection& conn, break; case api::Venue::VENUE_UNSET: case api::Venue::VENUE_SIM: + case api::Venue::VENUE_SIMICE: rv = conn.has_sim_connection(); + break; + case api::Venue::VENUE_ICE: + rv = conn.has_ice_connection(); break; case api::Venue::VENUE_BTS: case api::Venue::VENUE_CBOE: case api::Venue::VENUE_CBOECFE: - case api::Venue::VENUE_ICE: case api::Venue::Venue_INT_MIN_SENTINEL_DO_NOT_USE_: case api::Venue::Venue_INT_MAX_SENTINEL_DO_NOT_USE_: rv = false; @@ -89,6 +92,23 @@ const ExchangeConnection* AdminServiceUtils::resolve_cme_connection( return nullptr; } +const bts::api::admin::ExchangeConnection* +AdminServiceUtils::resolve_ice_connection(const bts::api::admin::AdminData& data, + const bts::api::admin::BTSAccount& account, + const bts::api::Security&) +{ + 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); + if (conn!=nullptr && conn->has_ice_connection()) + return conn; + } + } + return nullptr; +} + const bts::api::admin::ExchangeConnection* AdminServiceUtils::resolve_sim_connection(const bts::api::admin::AdminData& data, const bts::api::admin::BTSAccount& account, @@ -117,14 +137,22 @@ AdminServiceUtils::resolve_connection(const bts::api::admin::AdminData& data, rv = resolve_sim_connection(data, account, sec); break; } + case api::Venue::VENUE_ICE: + { + rv = resolve_ice_connection(data, account, sec); + auto deployment_type = boost::to_lower_copy(config::get().type()); + if (rv == nullptr && (deployment_type == "dev" || deployment_type == "demo")) + rv = resolve_sim_connection(data, account, sec); + break; + } case api::Venue::VENUE_UNSET: case api::Venue::VENUE_BTS: case api::Venue::VENUE_SIM: + case api::Venue::VENUE_SIMICE: rv = resolve_sim_connection(data, account, sec); break; case api::Venue::VENUE_CBOE: case api::Venue::VENUE_CBOECFE: - case api::Venue::VENUE_ICE: case api::Venue::Venue_INT_MIN_SENTINEL_DO_NOT_USE_: case api::Venue::Venue_INT_MAX_SENTINEL_DO_NOT_USE_: rv = nullptr; @@ -161,9 +189,15 @@ void AdminServiceUtils::fill_connections_from_config(bts::api::admin::AdminData& case config::JetFIX::Connection::CBOE: break; case config::JetFIX::Connection::SIM: + case config::JetFIX::Connection::SIMICE: connection.mutable_sim_connection(); add_connection(admin_data,connection); break; + case config::JetFIX::Connection::ICE: + connection.mutable_ice_connection()->set_sender_compid(c.sender_compid()); + add_connection(admin_data,connection); + break; + } } for(int i=0;iclearing_account(); break; } - case api::Venue::VENUE_ICE:{ + 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; + rv = ice_account_data->clearing_account(); + break; } - case api::Venue::VENUE_SIM: { + case api::Venue::VENUE_SIM: + case api::Venue::VENUE_SIMICE: + { auto sim_account_data = get_account_sim_data(account, data); if (sim_account_data != nullptr) rv = sim_account_data->clearing_account(); @@ -466,6 +503,46 @@ bts::api::orderman::RoutingRecord AdminServiceUtils::get_routing_record(const bt return rv; } switch (sec.exchange()) { + case api::Venue::VENUE_ICE: + { + const bts::api::admin::ICEUserData* ice_user_data = get_user_ice_data(*user_ptr, data); + if(ice_user_data==nullptr) + { + out_result.set_is_success(false); + out_result.set_error_text("Could not find ICE data for user"); + return rv; + } + const bts::api::admin::ICEAccountData* ice_account_data = get_account_ice_data(*account_ptr, data); + if(ice_account_data==nullptr) + { + out_result.set_is_success(false); + out_result.set_error_text("Could not find ICE data for account"); + return rv; + } + // check for data corectness + if (ice_user_data->operator_id().length() == 0) + { + out_result.set_is_success(false); + out_result.set_error_text("Operator ID is not set"); + return rv; + } + + rv.set_session(conn_ptr->name()); + rv.set_sender_compid(conn_ptr->ice_connection().sender_compid()); + rv.set_account(ice_account_data->clearing_account()); + rv.set_operator_id(ice_user_data->operator_id()); + rv.set_cti_code(ice_account_data->cti()); + rv.set_customer_or_firm(ice_account_data->origin()); + rv.set_clearing_firm(ice_account_data->clearing_firm()); + rv.set_customer_account_ref_id(ice_account_data->customer_account_ref_id()); + rv.set_account_code(ice_account_data->account_code()); + rv.set_company_id(std::to_string(ice_user_data->company_id())); + rv.set_authorized_trader(ice_user_data->authorized_trader()); + rv.set_authorized_group(ice_user_data->authorized_group()); + rv.set_cust_order_handing_inst(ice_user_data->cust_order_handing_inst()); + + return rv; + } case bts::api::Venue::VENUE_GLBX: { const bts::api::admin::CMEUserData* cme_user_data = get_user_cme_data(*user_ptr, data); if(cme_user_data==nullptr){ @@ -516,7 +593,9 @@ bts::api::orderman::RoutingRecord AdminServiceUtils::get_routing_record(const bt rv.set_customer_or_firm(cme_account_data->origin()); return rv; } - case api::Venue::VENUE_SIM: { + case api::Venue::VENUE_SIM: + case api::Venue::VENUE_SIMICE: + { const bts::api::admin::SIMUserData* sim_user_data = get_user_sim_data(*user_ptr, data); if(sim_user_data==nullptr){ out_result.set_is_success(false); @@ -562,8 +641,7 @@ bts::api::orderman::RoutingRecord AdminServiceUtils::get_routing_record(const bt 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); @@ -710,6 +788,18 @@ const bts::api::admin::CMEUserData* AdminServiceUtils::get_group_cme_data(const return nullptr; } +const bts::api::admin::ICEUserData* AdminServiceUtils::get_group_ice_data(const bts::api::admin::BTSUserGroup& group, + const bts::api::admin::AdminData& data){ + if (group.has_user_data() && group.user_data().has_exchange_user_data() && group.user_data().exchange_user_data().has_ice_data()) + return &(group.user_data().exchange_user_data().ice_data()); + else if (has_id(group.parent_group_id())){ + const bts::api::admin::BTSUserGroup* parent_group = find_group(data, group.parent_group_id()); + if (parent_group != nullptr) + return get_group_ice_data(*parent_group, data); + } + return nullptr; +} + const bts::api::admin::SIMUserData* AdminServiceUtils::get_group_sim_data(const bts::api::admin::BTSUserGroup& group, const bts::api::admin::AdminData& data){ if (group.has_user_data() && group.user_data().has_exchange_user_data() && group.user_data().exchange_user_data().has_sim_data()) @@ -840,6 +930,18 @@ const bts::api::admin::CMEUserData* AdminServiceUtils::get_user_cme_data(const b return nullptr; } +const bts::api::admin::ICEUserData* AdminServiceUtils::get_user_ice_data(const bts::api::admin::BTSUser& user, + const bts::api::admin::AdminData& data){ + if (user.has_user_data() && user.user_data().has_exchange_user_data() && user.user_data().exchange_user_data().has_ice_data()) + return &(user.user_data().exchange_user_data().ice_data()); + else{ + const bts::api::admin::BTSUserGroup* parent_group = find_group(data, user.parent_group_id()); + if (parent_group != nullptr) + return get_group_ice_data(*parent_group, data); + } + return nullptr; +} + const bts::api::admin::SIMUserData* AdminServiceUtils::get_user_sim_data(const bts::api::admin::BTSUser& user, const bts::api::admin::AdminData& data){ if (user.has_user_data() && user.user_data().has_exchange_user_data() && user.user_data().exchange_user_data().has_sim_data()) @@ -867,7 +969,16 @@ std::string AdminServiceUtils::find_user_exchange_id (const bts::api::admin::Adm rv = cme_user_data->operator_id(); break; } - case api::Venue::VENUE_SIM: { + case api::Venue::VENUE_ICE: + { + auto ice_user_data = get_user_ice_data(user, data); + if (ice_user_data != nullptr) + rv = ice_user_data->operator_id(); + break; + } + case api::Venue::VENUE_SIM: + case api::Venue::VENUE_SIMICE: + { auto sim_user_data = get_user_sim_data(user, data); if (sim_user_data != nullptr) rv = sim_user_data->operator_id(); @@ -877,8 +988,38 @@ std::string AdminServiceUtils::find_user_exchange_id (const bts::api::admin::Adm case api::Venue::VENUE_UNSET: case api::Venue::VENUE_BTS: case api::Venue::VENUE_CBOE: - case api::Venue::VENUE_CBOECFE: + 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"); + break; + } + return rv; +} + +std::string AdminServiceUtils::find_user_company_id (const bts::api::admin::AdminData& data, + const bts::api::admin::BTSUser& user, + api::Venue exchange, + bts::api::admin::AdminServiceOperationResult& out_result) +{ + out_result.set_is_success(true); + std::string rv = ""; + switch(exchange) + { case api::Venue::VENUE_ICE: + { + auto ice_user_data = get_user_ice_data(user, data); + if (ice_user_data != nullptr) + rv = std::to_string(ice_user_data->company_id()); + break; + } + case api::Venue::VENUE_SIM: + case api::Venue::VENUE_SIMICE: + case api::Venue::VENUE_GLBX: + case api::Venue::VENUE_UNSET: + case api::Venue::VENUE_BTS: + case api::Venue::VENUE_CBOE: + 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"); @@ -905,6 +1046,16 @@ AdminServiceUtils::find_user_account_matches (const AdminData& admin_data, exchange_user)) matching_users.push_back(u); } + // i.5 + //At the ice, they send back the login level tag50 in exec reports which doesn't match any user level tag50s in the exchange_user + //so instead we look for the CompanyID (or client_id, tag 109) field here. If this field matches we have a good bet that + //the user we find is enough to fulfill the routing record enough to cancel or modify orphaned orders, etc + for (BTSUser const* u : user_ptrs) + { + auto exchange_u = find_user_company_id(admin_data, *u, exchange, find_user_result); + if (find_user_result.is_success() && boost::algorithm::iequals ( exchange_u, exchange_user)) + matching_users.push_back(u); + } // ii. uses the exchange, connection and exchange_account to reverse lookup the account(s) auto account_ptrs = accounts(admin_data, false); std::vector matching_accounts; @@ -1016,16 +1167,26 @@ void AdminServiceUtils::validate_user_exchange_data (const bts::api::admin::BTSU } void AdminServiceUtils::get_md_entitlement_report(const AdminData& admin_data, std::vector& entitlements){ - for (const auto& user : admin_data.users()){ - const CMEUserData* cme_data=AdminServiceUtils::get_user_cme_data(user, - admin_data); - if(cme_data!=nullptr){ - for (const auto& cme_mic_data : cme_data->md_permissions()){ - MDEntitlementReportUnit rv=get_cme_md_entitlement_unit(user, - cme_mic_data); + for (const auto& user : admin_data.users()) + { + const CMEUserData* cme_data=AdminServiceUtils::get_user_cme_data(user, admin_data); + if(cme_data!=nullptr) + { + for (const auto& cme_mic_data : cme_data->md_permissions()) + { + MDEntitlementReportUnit rv=get_cme_md_entitlement_unit(user, cme_mic_data); entitlements.push_back(rv); } } + const ICEUserData* ice_data=AdminServiceUtils::get_user_ice_data(user, admin_data); + if(ice_data!=nullptr) + { + for (const auto& ice_mic_data : ice_data->md_permissions()) + { + MDEntitlementReportUnit rv=get_ice_md_entitlement_unit(user, ice_mic_data); + entitlements.push_back(rv); + } + } } } MDEntitlementReportUnit AdminServiceUtils::get_cme_md_entitlement_unit( @@ -1073,6 +1234,22 @@ MDEntitlementReportUnit AdminServiceUtils::get_cme_md_entitlement_unit( rv.set_record_id(key.str()); return rv; } +MDEntitlementReportUnit AdminServiceUtils::get_ice_md_entitlement_unit( + const BTSUser& user, + const ICEMicData& ice_mic_data){ + MDEntitlementReportUnit rv; + rv.set_user(user.name()); + rv.set_user_id(user.id()); + rv.set_exchange( bts::api::Venue_Name(bts::api::Venue::VENUE_ICE) ); + rv.set_mic( bts::api::ICEMics_Name( ice_mic_data.mic()) ); + + rv.set_enabled(ice_mic_data.enabled()); + std::ostringstream key; + key << rv.user() << rv.exchange() << rv.mic(); + rv.set_record_id(key.str()); + return rv; +} + void AdminServiceUtils::get_user_symbol_permissions(const AdminData& data, const BTSUser& user, std::vector& rv){ @@ -1147,13 +1324,13 @@ void AdminServiceUtils::set_user_exchange_data(bts::api::admin::BTSUser* user, if (!user->user_data().has_exchange_user_data()) user->mutable_user_data()->mutable_exchange_user_data()->CopyFrom(exchange_user_data); } -void AdminServiceUtils::set_user_md_entitlement_data(bts::api::admin::BTSUser* user, - const bts::api::admin::ExchangeUserMarketDataEntitlementData& exchange_user_md_entitelemnt_data){ - if (!user->has_user_data()) - user->mutable_user_data(); - if (!user->user_data().has_exchange_md_entitlement_data()) - user->mutable_user_data()->mutable_exchange_md_entitlement_data()->CopyFrom(exchange_user_md_entitelemnt_data); -} +// void AdminServiceUtils::set_user_md_entitlement_data(bts::api::admin::BTSUser* user, +// const bts::api::admin::ExchangeUserMarketDataEntitlementData& exchange_user_md_entitelemnt_data){ +// if (!user->has_user_data()) +// user->mutable_user_data(); +// if (!user->user_data().has_exchange_md_entitlement_data()) +// user->mutable_user_data()->mutable_exchange_md_entitlement_data()->CopyFrom(exchange_user_md_entitelemnt_data); +// } void AdminServiceUtils::clear_user_accounts (bts::api::admin::BTSUser* user){ if(user->has_user_data() && user->user_data().has_account_permissions()) user->mutable_user_data()->clear_account_permissions(); @@ -1398,6 +1575,7 @@ void AdminServiceUtils::bootstrap_data (bts::api::admin::AdminData& data){ auto account_1 = create_account(data, parent_account->id(), std::string("ACC A1"), std::string("description"), true, account_result); account_1->set_is_active(true); auto account_2 = create_account(data, parent_account->id(), std::string("ACC A2"), std::string("description"), true, account_result); account_2->set_is_active(true); auto account_3 = create_account(data, parent_account->id(), std::string("ACC A3"), std::string("description"), true, account_result); account_3->set_is_active(true); + auto account_4 = create_account(data, parent_account->id(), std::string("BLUETST"), std::string("description"), true, account_result); account_4->set_is_active(true); create_account(data, parent_account->id(), std::string("ACC A4"), std::string("description"), true, account_result); // add group auto def_group = create_group(data, 0, std::string("default"), result); @@ -1408,8 +1586,7 @@ void AdminServiceUtils::bootstrap_data (bts::api::admin::AdminData& data){ auto user_2 = create_user(data, def_group->id(), std::string("bts_2"), std::string("bts_2"), empty, empty, false, license_data, license_entitlements, user_result); user_2->set_is_active(true); auto user_3 = create_user(data, def_group->id(), std::string("bts_3"), std::string("bts_3"), empty, empty, false, license_data, license_entitlements, user_result); user_3->set_is_active(true); //associate users - bts::api::admin::UserAccountPermissionUnit permission_unit; - permission_unit.mutable_trading_permission()->set_submit_market_orders(true); + bts::api::admin::UserAccountPermissionUnit permission_unit; permission_unit.mutable_trading_permission()->set_has_permissions(true); permission_unit.mutable_admin_permission()->set_has_permissions(true); bts::api::admin::UserAccountPermissions account_permissions; @@ -1417,6 +1594,7 @@ void AdminServiceUtils::bootstrap_data (bts::api::admin::AdminData& data){ account_permissions.mutable_account_permissions()->insert(google::protobuf::MapPair(account_1->id(), permission_unit)); account_permissions.mutable_account_permissions()->insert(google::protobuf::MapPair(account_2->id(), permission_unit)); account_permissions.mutable_account_permissions()->insert(google::protobuf::MapPair(account_3->id(), permission_unit)); + account_permissions.mutable_account_permissions()->insert(google::protobuf::MapPair(account_4->id(), permission_unit)); set_user_accounts(user_1, account_permissions); set_user_accounts(user_2, account_permissions); set_user_accounts(user_3, account_permissions); @@ -1426,6 +1604,12 @@ void AdminServiceUtils::bootstrap_data (bts::api::admin::AdminData& data){ exchange_account_data.mutable_cme_account_data()->set_cti(bts::api::orderman::CTICode::CTI_CODE_CTI_CODE1); exchange_account_data.mutable_cme_account_data()->set_origin(bts::api::orderman::CustomerOrFirm::CUSTOMER_OR_FIRM_FIRM); exchange_account_data.mutable_cme_account_data()->set_clearing_account(parent_account->name()); + + exchange_account_data.mutable_ice_account_data()->set_cti(bts::api::orderman::CTICode::CTI_CODE_UNDEFINED); + exchange_account_data.mutable_ice_account_data()->set_origin(bts::api::orderman::CustomerOrFirm::CUSTOMER_OR_FIRM_FIRM); + exchange_account_data.mutable_ice_account_data()->set_clearing_account(parent_account->name()); + exchange_account_data.mutable_ice_account_data()->set_clearing_firm("649"); + exchange_account_data.mutable_sim_account_data()->set_cti(bts::api::orderman::CTICode::CTI_CODE_CTI_CODE1); exchange_account_data.mutable_sim_account_data()->set_origin(bts::api::orderman::CustomerOrFirm::CUSTOMER_OR_FIRM_FIRM); exchange_account_data.mutable_sim_account_data()->set_clearing_account(parent_account->name()); @@ -1433,33 +1617,87 @@ void AdminServiceUtils::bootstrap_data (bts::api::admin::AdminData& data){ exchange_account_data.mutable_cme_account_data()->set_clearing_account(account_1->name()); + exchange_account_data.mutable_ice_account_data()->set_clearing_account(account_1->name()); exchange_account_data.mutable_sim_account_data()->set_clearing_account(account_1->name()); set_account_exchange_data(account_1, exchange_account_data); exchange_account_data.mutable_cme_account_data()->set_clearing_account(account_2->name()); + exchange_account_data.mutable_ice_account_data()->set_clearing_account(account_2->name()); exchange_account_data.mutable_sim_account_data()->set_clearing_account(account_2->name()); set_account_exchange_data(account_2, exchange_account_data); exchange_account_data.mutable_cme_account_data()->set_clearing_account(account_3->name()); + exchange_account_data.mutable_ice_account_data()->set_clearing_account(account_3->name()); exchange_account_data.mutable_sim_account_data()->set_clearing_account(account_3->name()); set_account_exchange_data(account_3, exchange_account_data); + exchange_account_data.mutable_ice_account_data()->set_clearing_account(account_4->name()); + set_account_exchange_data(account_4, exchange_account_data); + + // add cme and sim user data bts::api::admin::ExchangeUserData exchange_user_data; exchange_user_data.mutable_cme_data()->set_operator_id("op_id_" + user_1->name()); exchange_user_data.mutable_cme_data()->set_location("us,nc"); + + exchange_user_data.mutable_ice_data()->set_operator_id("op_id_" + user_1->name()); + exchange_user_data.mutable_ice_data()->set_company_id(51251); + exchange_user_data.mutable_ice_data()->set_authorized_trader("bluetrd-fx"); + exchange_user_data.mutable_ice_data()->set_authorized_group("Group"); + exchange_user_data.mutable_ice_data()->set_password("Starts123"); + exchange_user_data.mutable_ice_data()->set_mifid_direct_electronic_access(-1); + exchange_user_data.mutable_ice_data()->set_mifid_trading_capacity(-1); + exchange_user_data.mutable_ice_data()->set_mifid_liquidity_provision(-1); + exchange_user_data.mutable_ice_data()->set_mifid_commodity_deriv_indicator(-1); + exchange_user_data.mutable_ice_data()->set_mifid_investment_decision(-1); + exchange_user_data.mutable_ice_data()->set_mifid_execution_decision(-1); + exchange_user_data.mutable_ice_data()->set_mifid_client_id_code(-1); + exchange_user_data.mutable_ice_data()->set_mifid_id(1); + exchange_user_data.mutable_sim_data()->set_operator_id("op_id_" + user_1->name()); exchange_user_data.mutable_sim_data()->set_location("us,nc"); set_user_exchange_data(user_1, exchange_user_data); exchange_user_data.mutable_cme_data()->set_operator_id("op_id_" + user_2->name()); exchange_user_data.mutable_cme_data()->set_location("us,nc"); + + exchange_user_data.mutable_ice_data()->set_operator_id("op_id_" + user_2->name()); + exchange_user_data.mutable_ice_data()->set_company_id(51251); + exchange_user_data.mutable_ice_data()->set_authorized_trader("bluetrd-fx"); + exchange_user_data.mutable_ice_data()->set_authorized_group("Group"); + exchange_user_data.mutable_ice_data()->set_password("Starts123"); + exchange_user_data.mutable_ice_data()->set_mifid_direct_electronic_access(-1); + exchange_user_data.mutable_ice_data()->set_mifid_trading_capacity(-1); + exchange_user_data.mutable_ice_data()->set_mifid_liquidity_provision(-1); + exchange_user_data.mutable_ice_data()->set_mifid_commodity_deriv_indicator(-1); + exchange_user_data.mutable_ice_data()->set_mifid_investment_decision(-1); + exchange_user_data.mutable_ice_data()->set_mifid_execution_decision(-1); + exchange_user_data.mutable_ice_data()->set_mifid_client_id_code(-1); + exchange_user_data.mutable_ice_data()->set_mifid_id(1); + + exchange_user_data.mutable_sim_data()->set_operator_id("op_id_" + user_2->name()); exchange_user_data.mutable_sim_data()->set_location("us,nc"); set_user_exchange_data(user_2, exchange_user_data); exchange_user_data.mutable_cme_data()->set_operator_id("op_id_" + user_3->name()); exchange_user_data.mutable_cme_data()->set_location("us,nc"); + + exchange_user_data.mutable_ice_data()->set_operator_id("op_id_" + user_3->name()); + exchange_user_data.mutable_ice_data()->set_company_id(51251); + exchange_user_data.mutable_ice_data()->set_authorized_trader("bluetrd-fx"); + exchange_user_data.mutable_ice_data()->set_authorized_group("Group"); + exchange_user_data.mutable_ice_data()->set_password("Starts123"); + exchange_user_data.mutable_ice_data()->set_mifid_direct_electronic_access(-1); + exchange_user_data.mutable_ice_data()->set_mifid_trading_capacity(-1); + exchange_user_data.mutable_ice_data()->set_mifid_liquidity_provision(-1); + exchange_user_data.mutable_ice_data()->set_mifid_commodity_deriv_indicator(-1); + exchange_user_data.mutable_ice_data()->set_mifid_investment_decision(-1); + exchange_user_data.mutable_ice_data()->set_mifid_execution_decision(-1); + exchange_user_data.mutable_ice_data()->set_mifid_client_id_code(-1); + exchange_user_data.mutable_ice_data()->set_mifid_id(1); + + exchange_user_data.mutable_sim_data()->set_operator_id("op_id_" + user_3->name()); exchange_user_data.mutable_sim_data()->set_location("us,nc"); set_user_exchange_data(user_3, exchange_user_data); @@ -1501,6 +1739,13 @@ void AdminServiceUtils::bootstrap_data (bts::api::admin::AdminData& data){ add_security_clip_sizes_to_account(account_1, api::Venue::VENUE_GLBX, api::SecurityType::SECURITYTYPE_SPREAD,100); add_security_clip_sizes_to_account(account_1, api::Venue::VENUE_SIM, api::SecurityType::SECURITYTYPE_FUTURE,100); add_security_clip_sizes_to_account(account_1, api::Venue::VENUE_SIM, api::SecurityType::SECURITYTYPE_SPREAD,100); + + //ICE limits + add_product_position_limits(account_4, api::Venue::VENUE_ICE, api::SecurityType::SECURITYTYPE_FUTURE,50000); + add_product_position_limits(account_4, api::Venue::VENUE_ICE, api::SecurityType::SECURITYTYPE_SPREAD,50000); + add_security_clip_sizes_to_account(account_4, api::Venue::VENUE_ICE, api::SecurityType::SECURITYTYPE_FUTURE,50); + add_security_clip_sizes_to_account(account_4, api::Venue::VENUE_ICE, api::SecurityType::SECURITYTYPE_SPREAD,50); + } bool AdminServiceUtils::has_id (uint32_t id){return id>0;} bool AdminServiceUtils::is_symbol_set (const std::string& symbol){ diff --git a/src/lib/api/service_helpers/AdminServiceUtils.h b/src/lib/api/service_helpers/AdminServiceUtils.h index 70d1e1e5b..dc6095e41 100644 --- a/src/lib/api/service_helpers/AdminServiceUtils.h +++ b/src/lib/api/service_helpers/AdminServiceUtils.h @@ -35,6 +35,10 @@ public: const AdminData& data, const BTSAccount& account, const api::Security& sec); + static const ExchangeConnection* resolve_ice_connection( + const AdminData& data, + const BTSAccount& account, + const api::Security& sec); static const ExchangeConnection* resolve_sim_connection( const AdminData& data, const BTSAccount& account, @@ -164,6 +168,8 @@ public: // travels up the group tree until it finds a populated cme data static const CMEUserData* get_group_cme_data(const BTSUserGroup& group, const AdminData& data); + static const ICEUserData* get_group_ice_data(const BTSUserGroup& group, + const AdminData& data); static const SIMUserData* get_group_sim_data(const BTSUserGroup& group, const AdminData& data); static const UserAccountPermissions* const get_group_user_permissions ( @@ -198,6 +204,8 @@ public: static uint32_t find_max_user_id (const AdminData& data); static const CMEUserData* get_user_cme_data(const BTSUser& user, const AdminData& data); + static const ICEUserData* get_user_ice_data(const BTSUser& user, + const AdminData& data); static const SIMUserData* get_user_sim_data(const BTSUser& user, const AdminData& data); // finds the "exchange user" (ex: "operator_id" at CME or "authorized_trader" @@ -210,6 +218,12 @@ public: // i. uses the exchange and exchange_user to reverse lookup the user(s) // ii. uses the exchange, connection and exchange_account to reverse lookup the account(s) // iii. finds the first account from ii that a user from i is authorized to trade + static std::string find_user_company_id ( + const AdminData& data, + const BTSUser& user, + api::Venue exchange, + AdminServiceOperationResult& out_result); + static std::pair find_user_account_matches (const AdminData& admin_data, const std::string& connection, @@ -249,6 +263,9 @@ public: static MDEntitlementReportUnit get_cme_md_entitlement_unit( const BTSUser& user, const CMEMicData& cme_mic_data); + static MDEntitlementReportUnit get_ice_md_entitlement_unit( + const BTSUser& user, + const ICEMicData& ice_mic_data); static void get_user_symbol_permissions( const AdminData& data, const BTSUser& user, @@ -275,8 +292,6 @@ public: static void set_user_exchange_data( BTSUser* user, const ExchangeUserData& exchange_user_data); - static void set_user_md_entitlement_data(BTSUser* user, - const ExchangeUserMarketDataEntitlementData&); static void clear_user_accounts (BTSUser* user); static void set_user_accounts (BTSUser* user, const UserAccountPermissions& user_account_permissions); diff --git a/src/lib/api/services/AdminService.proto b/src/lib/api/services/AdminService.proto index a274c3cb6..1a1f57fff 100644 --- a/src/lib/api/services/AdminService.proto +++ b/src/lib/api/services/AdminService.proto @@ -55,11 +55,12 @@ message BTSUser{ string name = 30; string password = 40; string full_name = 42; - string company = 44; + string company = 44; bool is_active = 50; bool can_create = 55; - LicenseData license_data = 60; //unused - LicenseEntitlements license_entitlements = 70; //unused + LicenseData license_data = 60; + LicenseEntitlements license_entitlements = 70; + BTSUserDerivableData user_data = 80; } @@ -76,7 +77,7 @@ message BTSUserDerivableData{ // b. else its overridden at the user level UserAccountPermissions account_permissions = 70; ExchangeUserData exchange_user_data = 80; - ExchangeUserMarketDataEntitlementData exchange_md_entitlement_data = 90; + reserved 90; reserved "exchange_md_entitlement_data"; //ExchangeUserMarketDataEntitlementData exchange_md_entitlement_data = 90; } message LicenseData{ @@ -91,21 +92,22 @@ message LicenseEntitlements{ bool spreader = 30; } -message AlgoTradingPermissionsUnit{ - bool submit_iceberg_orders = 10; //unused - bool submit_time_box_orders = 20; //unused - bool submit_spreader_orders = 30; //unused -} +// message AlgoTradingPermissionsUnit{ +// bool submit_iceberg_orders = 10; //unused +// bool submit_time_box_orders = 20; //unused +// bool submit_spreader_orders = 30; //unused +// } message TradingPermissionUnit{ + reserved 10; reserved "submit_market_orders"; //bool submit_market_orders = 10; //unused + reserved 20; reserved "submit_gtc_orders"; //bool submit_gtc_orders = 20; //unused + reserved 30; reserved "algo_trading_permission"; //AlgoTradingPermissionsUnit algo_trading_permission = 30; //unused + bool has_permissions = 5; - bool submit_market_orders = 10; //unused - bool submit_gtc_orders = 20; //unused - AlgoTradingPermissionsUnit algo_trading_permission = 30; //unused } message AdminPermissionUnit{ - // TODO: needs to be more ellaborate (account writing, + // TODO: needs to be more ellaborate (account writing, // reading etc, routing etc) bool has_permissions = 10; bool has_md_permissions = 20; @@ -119,15 +121,15 @@ message UserAccountPermissionUnit{ message UserAccountPermissions{ //uint32 = account_id - map account_permissions = 10; + map account_permissions = 10; } -// unused now -message UserSecurityTradingLimit{ - SecurityScope security = 10; - uint32 clip_size = 20; - bool can_trade = 30; -} +// // unused now +// message UserSecurityTradingLimit{ +// SecurityScope security = 10; +// uint32 clip_size = 20; +// bool can_trade = 30; +// } /************************************************************************/ /********************************BTS Account*****************************/ @@ -152,7 +154,7 @@ message BTSAccount{ AccountLossLimit account_loss_limit = 130; } -message RiskLimitEnforcement{ +message RiskLimitEnforcement{ bool product_limit_in_net_terms = 10; bool ignore_spread_position_limits = 20; } @@ -204,23 +206,22 @@ message AccountLossLimit{ message CMEMicData{ api.CMEMics mic=10; bool enabled=20; - // there are other things here + // there are other things here } message ICEMicData{ - api.CMEMics mic=10; + api.ICEMics mic=10; bool enabled=20; } -message ExchangeUserMarketDataEntitlementData{ - CMEUserMarketDataEntitlementData cme_data = 10; - ICEUserMarketDataEntitlementData ice_data = 20; -} message ExchangeUserData{ CMEUserData cme_data = 10; ICEUserData ice_data = 20; SIMUserData sim_data = 30; + + enum AdminDisplay { All = 0; MD = 1; Routing = 2; None = 100; } + AdminDisplay admin_display_flag = 120; } message ExchangeAccountData{ @@ -229,9 +230,6 @@ message ExchangeAccountData{ SIMAccountData sim_account_data = 30; } -message MarketDataEntitlement{ - bool can_view = 10; -} message ExchangeConnection{ uint32 id = 10; @@ -274,19 +272,19 @@ message CMEMSGWConnection{ uint32 market_segment_id = 60; } -message CMEAccountData{ - // should perhaps be an enum +message CMEAccountData { + reserved 30; reserved "takeup_member_code"; //string takeup_member_code = 30; + reserved 50; reserved "direct_electronic_access"; //string direct_electronic_access = 50; + reserved 60; reserved "trading_capacity"; //string trading_capacity = 60; + reserved 70; reserved "liquidity_provision"; //string liquidity_provision = 70; + reserved 80; reserved "commodity_derivative_indicator"; //string commodity_derivative_indicator = 80; + reserved 90; reserved "investment_decision_id"; //string investment_decision_id = 90; + reserved 100; reserved "execution_decision_id"; //string execution_decision_id = 100; + reserved 110; reserved "client_id"; //string client_id = 110; + api.orderman.CTICode cti = 10; api.orderman.CustomerOrFirm origin = 20; - string takeup_member_code = 30; string clearing_account = 40; - string direct_electronic_access = 50; - string trading_capacity = 60; - string liquidity_provision = 70; - string commodity_derivative_indicator = 80; - string investment_decision_id = 90; - string execution_decision_id = 100; - string client_id = 110; map misc_tags = 120; } @@ -309,21 +307,22 @@ enum DisplayDesignation } message CMEUserData{ + reserved 20; reserved "ats_operator_id"; //string ats_operator_id = 20; + reserved 40; reserved "text_a"; //string text_a = 40; + reserved 50; reserved "text_b"; //string text_b = 50; + reserved 80; reserved "max_order_rate_per_connection"; //string max_order_rate_per_connection = 80; + //enum AdminDisplay { All = 0; MD = 1; Routing = 2; None = 100; } + reserved 120; reserved "admin_display_flag"; //AdminDisplay admin_display_flag = 120; + repeated CMEMicData md_permissions = 1000; + string operator_id = 10; - string ats_operator_id = 20; string smp_id = 30; - string text_a = 40; - string text_b = 50; map misc_tags = 60; string smp_instruction = 70; - string max_order_rate_per_connection = 80; + string location = 90; ProfessionalDesignation pro_nonpro = 100; DisplayDesignation display_nondisplay = 110; - - enum AdminDisplay { All = 0; MD = 1; Routing = 2; None = 100; } - AdminDisplay admin_display_flag = 120; - repeated CMEMicData md_permissions = 1000; } message SIMUserData { @@ -331,58 +330,67 @@ message SIMUserData { string location = 90; } -message CMEUserMarketDataEntitlementData{ - MarketDataEntitlement cbot_entitlement = 10; - MarketDataEntitlement cme_entitlement = 20; - MarketDataEntitlement nymex_entitlement = 30; - MarketDataEntitlement bmandf_entitlement = 40; - MarketDataEntitlement comex_entitlement = 50; - MarketDataEntitlement cme_europe_entitlement = 60; -} - /********************************ICE Data*********************************/ -message ICEConnection{ - string authorized_member_id = 10; - string sender_company_id = 20; - string sender_sub_id = 30; - string session_id = 40; - string session_password = 50; - string host_ip = 60; - string port = 70; - string default_routing_trader = 80; - string routing_trader_password = 90; - int32 max_order_rate = 100; +message ICEConnection { + string sender_compid = 10; } -message ICEAccountData{ - string nybit_house = 10; - int32 wce_house = 20; - string clearing_account_override = 30; - bool direct_electronic_access = 40; - string trading_capacity = 50; - bool liquidity_provision = 60; - bool commodity_derivative_indicator = 70; - string investment_decision_id = 80; - string execution_decision_id = 90; - string client_id = 100; - string mfid_id = 110; +message ICEAccountData { + reserved 10; reserved "nybit_house"; //string nybit_house = 10; + reserved 20; reserved "wce_house"; //int32 wce_house = 20; + reserved 30; reserved "clearing_account_override"; //string clearing_account_override = 30; + reserved 40; reserved "direct_electronic_access"; //bool direct_electronic_access = 40; + reserved 50; reserved "trading_capacity"; //string trading_capacity = 50; + reserved 60; reserved "liquidity_provision"; //bool liquidity_provision = 60; + reserved 70; reserved "commodity_derivative_indicator"; //bool commodity_derivative_indicator = 70; + reserved 80; reserved "investment_decision_id"; //string investment_decision_id = 80; + reserved 90; reserved "execution_decision_id"; //string execution_decision_id = 90; + reserved 100; reserved "client_id"; //string client_id = 100; + reserved 110; reserved "mfid_id"; //string mfid_id = 110; + + api.orderman.CTICode cti = 3; + api.orderman.CustomerOrFirm origin = 5; + string clearing_firm = 200; + string clearing_account = 210; + string customer_account_ref_id = 220; + string account_code = 230; + map misc_tags = 500; } -message ICEUserData{ - string authorized_trader = 10; - string memo_field = 20; - string text_b = 30; - uint32 max_order_rate = 40; -} +message ICEUserData { + reserved 20; reserved "memo_field"; //string memo_field = 20; + reserved 30; reserved "text_b"; //string text_b = 30; + reserved 40; reserved "max_order_rate"; //uint32 max_order_rate = 40; -message ICEUserMarketDataEntitlementData{ + int32 company_id = 3; //tag 109 clientid + string operator_id = 5; //tag 50 + string authorized_trader = 10; //tag 553 user_name + string authorized_group = 15; //tag 553 user_name + string password = 21; //tag 96 raw data + string cust_order_handing_inst = 25; + + //mifid + int32 mifid_direct_electronic_access = 500; + int32 mifid_trading_capacity = 510; + int32 mifid_liquidity_provision = 520; + int32 mifid_commodity_deriv_indicator = 530; + int64 mifid_investment_decision = 540; + int64 mifid_execution_decision = 550; + int64 mifid_client_id_code = 560; + int64 mifid_id = 570; + + map misc_tags = 60; + ProfessionalDesignation pro_nonpro = 100; + DisplayDesignation display_nondisplay = 110; + + repeated ICEMicData md_permissions = 1000; } /////////////////////////////////////////////////////////// // Order Auxiliary Service //////////////////////////////// -message OrdersSummaryUnit{ +message OrdersSummaryUnit{ string account = 10; string user = 20; uint32 total_orders = 30; @@ -393,11 +401,11 @@ message OrdersSummary{ } message OrderSummaryRequest{ } -// top level orders that have account +// top level orders that have account // and user that are present here, will // be cancelled. wild cards are not // supported. -message OrderCancelRequest{ +message OrderCancelRequest{ repeated string accounts = 10; repeated string users = 20; } @@ -433,7 +441,7 @@ message AccountCreateAPIArgument{ string creator_user = 40; } message AccountUpdateAPIArgument{ - BTSAccount complete_data=10; + BTSAccount complete_data=10; } message UserCreateAPIArgument{ uint32 parent_group_id = 20; @@ -477,7 +485,7 @@ service AdminService { // users rpc create_user(UserCreateAPIArgument) returns (AdminServiceCreateEntityResult); rpc update_user(UserUpdateAPIArgument) returns (AdminServiceOperationResult); - rpc delete_user(IDAPIArgument) returns (AdminServiceOperationResult); + rpc delete_user(IDAPIArgument) returns (AdminServiceOperationResult); // subscription rpc admin_data(bts.api.VoidMessage) returns (stream AdminData); rpc get_admin_data(bts.api.VoidMessage) returns (AdminData); diff --git a/src/lib/api/services/AuthService.proto b/src/lib/api/services/AuthService.proto index 2dd9997d2..f7b984497 100644 --- a/src/lib/api/services/AuthService.proto +++ b/src/lib/api/services/AuthService.proto @@ -57,4 +57,7 @@ service AuthService { rpc get_users(GetUsersRequest) returns (GetUsersResponse) {option (bts.api.method_description)="Returns list of valid users";} + + rpc clear_all_sessions(bts.api.VoidMessage) returns (bts.api.ResultMessage) + {option (bts.api.method_description)="Clears all reserved auth sessions";} } diff --git a/src/lib/api/services/OrderService.proto b/src/lib/api/services/OrderService.proto index f2b48d8fb..3448f79ea 100644 --- a/src/lib/api/services/OrderService.proto +++ b/src/lib/api/services/OrderService.proto @@ -310,6 +310,13 @@ message Fill { FILL_KIND_SOD = 2; } FillKind fill_kind = 10; + + enum FillStatus { + FILL_STATUS_ACTIVE = 0; + FILL_STATUS_CORRECTED = 1; + FILL_STATUS_BUSTED = 2; + } + FillStatus fill_status = 20; } enum OrderSubType { @@ -328,6 +335,16 @@ message RoutingRecord { SelfMatchPreventionInstruction smp_instruction = 7; string location = 8; string cust_order_handling_inst = 9; + + string sender_compid = 15; + string clearing_firm = 20; + string customer_account_ref_id = 25; + string account_code = 30; + string company_id = 45; + string authorized_trader = 50; + string authorized_group = 60; + string cust_order_handing_inst = 70; + string order_server_id = 200; } message NativeOrderState { @@ -586,6 +603,7 @@ enum DynamicOrderPrimitiveDescriptor { DYNAMIC_ORDER_PRIMITIVE_BOOL = 4; DYNAMIC_ORDER_PRIMITIVE_ORDER_DEF = 5; DYNAMIC_ORDER_PRIMITIVE_QUANTITY = 6; + DYNAMIC_ORDER_PRIMITIVE_SECURITY_KEY = 7; } message DynamicOrderTypeDescriptor { diff --git a/src/lib/api/services/ShellLatencyService.proto b/src/lib/api/services/ShellLatencyService.proto index 272310703..8d1a253ae 100644 --- a/src/lib/api/services/ShellLatencyService.proto +++ b/src/lib/api/services/ShellLatencyService.proto @@ -15,8 +15,13 @@ message Report { string user = 3; uint64 timestamp = 10; } +message BufferMessage { + uint64 sequence = 1; + string data = 10; +} service ShellLatencyService { rpc clock(bts.api.VoidMessage) returns (stream Tick); rpc report(Report) returns (bts.api.VoidMessage); + rpc ping(BufferMessage) returns (BufferMessage); } \ No newline at end of file diff --git a/src/lib/config/ConfigurationState.cpp b/src/lib/config/ConfigurationState.cpp index b7eabbb88..0678fbd61 100755 --- a/src/lib/config/ConfigurationState.cpp +++ b/src/lib/config/ConfigurationState.cpp @@ -24,6 +24,7 @@ #include #include + #define URDL_HEADER_ONLY 1 #define URDL_DISALE_SSL 1 #include @@ -908,3 +909,4 @@ std::string get_fallback(const proto::Message& message) } }}}//end namespaces + diff --git a/src/lib/config/defaults/MarketData.conf b/src/lib/config/defaults/MarketData.conf index a8dd25828..7ffb0b467 100644 --- a/src/lib/config/defaults/MarketData.conf +++ b/src/lib/config/defaults/MarketData.conf @@ -62,15 +62,44 @@ ICEOptions { value { channel { name: "oil" #these names are arbitrary and are for debugging + market_type: 3 + uds_market_type: 3 + market_type: 4 #ICE IPE Gas Oil Futures + uds_market_type: 4 market_type: 5 #ICE Futures Brent, IPE Brent Futures - market_type: 9 #ICE Futures WTI, ICE WTI Crude Futures - #udp_buffer_size: 33554432 #32mb - } - channel { - name: "juice" - market_type: 23 #ICE Futures US Agricultures, FCOJ A + uds_market_type: 5 + market_type: 9 #ICE Futures WTI, ICE WTI Crude Futures + uds_market_type: 9 + market_type: 13 #ICE Heating Oil Futures + uds_market_type: 13 + market_type: 15 #ICE NYH (RBOB) Gasoline Futures + uds_market_type: 15 + ##market_type: 21 #ICE Futures US Agricultures, Coffee C + ##uds_market_type: 21 + ##market_type: 23 #ICE Futures US Agricultures, FCOJ A + ##uds_market_type: 23 + + ##market_type: 26 #ICE Futures US Financials DX + ##uds_market_type: 26 + + uds_market_type: 11 #ICE WTI-Brent spreads + uds_market_type: 14 #ICE Heating Oil/WTI Futures Crack + uds_market_type: 16 #ICE NYH (RBOB) Gasoline/WTI Futures Crack + uds_market_type: 58 #ICE Futures UI Heating Oil / Brent + uds_market_type: 59 #ICE NYH (RBOB) Gasoline/Brent Futures Crack + uds_market_type: 66 #ICE NYH (RBOB) Gasoline/Heating Oil Spread + uds_market_type: 70 #ICE Heating Oil/LS Gasoil Futures Spread + uds_market_type: 71 #ICE LS Gasoil/Brent Futures Crack + uds_market_type: 72 #ICE NYH (RBOB) Gasoline/LS Gasoil Futures Spread + + #onixs_log_mode: Debug } + # channel { + # name: "juice" + # market_type: 26 #ICE Futures US Agricultures, FCOJ A + # } ice_config_file: "ConnectivityConfiguration.APITest.xml" + #ice_config_file: "ConnectivityConfiguration.PerfTest.xml" } } diff --git a/src/lib/config/defaults/Ports.conf b/src/lib/config/defaults/Ports.conf index 266c65c9e..a8059b4f7 100644 --- a/src/lib/config/defaults/Ports.conf +++ b/src/lib/config/defaults/Ports.conf @@ -3,7 +3,8 @@ Ports { value { config_port: 8000 qbus_port: 8001 - exchange_sim_fix_port: 8002 + glbx_sim_fix_port: 8002 + ice_sim_fix_port: 8003 replicator_port: 8080 } } \ No newline at end of file diff --git a/src/lib/config/defaults/Symbols.conf b/src/lib/config/defaults/Symbols.conf index 8494cc578..9f4d0f77a 100644 --- a/src/lib/config/defaults/Symbols.conf +++ b/src/lib/config/defaults/Symbols.conf @@ -12,17 +12,19 @@ SymbolList { symbol: "BOB" symbol: "BUB" symbol: "BZ" + symbol: "BZT" symbol: "CL" + symbol: "CLT" symbol: "EH" symbol: "ES" symbol: "FYT" symbol: "FOB" symbol: "GC" symbol: "GE" - symbol: "GLI" symbol: "HE" symbol: "HG" symbol: "HO" + symbol: "HOT" symbol: "KE" symbol: "LE" symbol: "NB" @@ -30,9 +32,11 @@ SymbolList { symbol: "NOB" symbol: "NON" symbol: "NG" + symbol: "NGT" symbol: "NQ" symbol: "NUB" symbol: "RB" + symbol: "RBT" symbol: "SI" symbol: "SOM" symbol: "SR1" @@ -66,9 +70,16 @@ SymbolLists list { key: "ICE" - # symbol: "BRN" - # symbol: "WBS" - # symbol: "OJ" + symbol: "BRN" + symbol: "WBS" + symbol: "OJ" + symbol: "G" + symbol: "KC" + symbol: "UHO" + symbol: "UHU" + symbol: "DX" + + symbol: "*" } list { @@ -85,6 +96,10 @@ Symbols { future: @SymbolLists.list[GLBX].symbol spread: @SymbolLists.list[GLBX].symbol } + simice { + future: @SymbolLists.list[ICE].symbol + spread: @SymbolLists.list[ICE].symbol + } } } @@ -95,6 +110,10 @@ Symbols { future: @SymbolLists.list[GLBX].symbol spread: @SymbolLists.list[GLBX].symbol } + ice { + future: @SymbolLists.list[ICE].symbol + spread: @SymbolLists.list[ICE].symbol + } } } @@ -112,5 +131,9 @@ Symbols { future: @SymbolLists.list[ICE].symbol spread: @SymbolLists.list[ICE].symbol } + simice { + future: @SymbolLists.list[ICE].symbol + spread: @SymbolLists.list[ICE].symbol + } } } diff --git a/src/lib/config/defaults/cme_reference_data.conf b/src/lib/config/defaults/cme_reference_data.conf index ac365cefb..ae2a5d4d7 100644 --- a/src/lib/config/defaults/cme_reference_data.conf +++ b/src/lib/config/defaults/cme_reference_data.conf @@ -7,7 +7,6 @@ CppConfig { value { - cpp_entry { key: "098" value: 2000 } cpp_entry { key: "0A" value: 100 } cpp_entry { key: "0B" value: 100 } cpp_entry { key: "0C" value: 100 } @@ -354,6 +353,7 @@ CppConfig cpp_entry { key: "BOB" value: 1000 } cpp_entry { key: "BOO" value: 6350 } cpp_entry { key: "BOS" value: 250 } + cpp_entry { key: "BPU" value: 1000 } cpp_entry { key: "BR7" value: 1000 } cpp_entry { key: "BSF" value: 25 } cpp_entry { key: "BTC" value: 5 } @@ -442,12 +442,12 @@ CppConfig cpp_entry { key: "D3L" value: 80 } cpp_entry { key: "D4L" value: 5 } cpp_entry { key: "D7L" value: 80 } + cpp_entry { key: "DAB" value: 1000 } cpp_entry { key: "DAX" value: 5 } cpp_entry { key: "DAZ" value: 80 } cpp_entry { key: "DBB" value: 1000 } cpp_entry { key: "DBI" value: 1000 } cpp_entry { key: "DBL" value: 100 } - cpp_entry { key: "DBR" value: 1000 } cpp_entry { key: "DBT" value: 100 } cpp_entry { key: "DC" value: 2000 } cpp_entry { key: "DCB" value: 1000 } @@ -458,14 +458,13 @@ CppConfig cpp_entry { key: "DDE" value: 1000 } cpp_entry { key: "DDI" value: 1000 } cpp_entry { key: "DDO" value: 1000 } - cpp_entry { key: "DEB" value: 744 } + cpp_entry { key: "DEB" value: 745 } cpp_entry { key: "DEN" value: 250 } - cpp_entry { key: "DEP" value: 264 } + cpp_entry { key: "DEP" value: 276 } cpp_entry { key: "DFL" value: 100 } cpp_entry { key: "DFN" value: 100 } cpp_entry { key: "DG2" value: 100 } cpp_entry { key: "DG3" value: 100 } - cpp_entry { key: "DGH" value: 1000 } cpp_entry { key: "DGI" value: 100 } cpp_entry { key: "DGS" value: 100 } cpp_entry { key: "DGT" value: 100 } @@ -473,19 +472,13 @@ CppConfig cpp_entry { key: "DHI" value: 1000 } cpp_entry { key: "DJR" value: 100 } cpp_entry { key: "DKR" value: 1000 } - cpp_entry { key: "DMB" value: 100 } - cpp_entry { key: "DOB" value: 1000 } - cpp_entry { key: "DOP" value: 1000 } - cpp_entry { key: "DRB" value: 1000 } cpp_entry { key: "DRI" value: 100 } cpp_entry { key: "DRS" value: 100 } cpp_entry { key: "DRT" value: 100 } cpp_entry { key: "DSD" value: 1000 } cpp_entry { key: "DSE" value: 1000 } - cpp_entry { key: "DSG" value: 1000 } cpp_entry { key: "DTR" value: 2 } cpp_entry { key: "DTT" value: 2 } - cpp_entry { key: "DUA" value: 6350 } cpp_entry { key: "DUP" value: 1000 } cpp_entry { key: "DVE" value: 200 } cpp_entry { key: "DVT" value: 200 } @@ -530,8 +523,11 @@ CppConfig cpp_entry { key: "EP1" value: 1000 } cpp_entry { key: "EPN" value: 1000 } cpp_entry { key: "EPZ" value: 500000 } + cpp_entry { key: "EQ1" value: 50 } cpp_entry { key: "ERL" value: 5 } cpp_entry { key: "ES" value: 50 } + cpp_entry { key: "ES1" value: 50 } + cpp_entry { key: "ES2" value: 50 } cpp_entry { key: "ESB" value: 1000 } cpp_entry { key: "ESK" value: 125000 } cpp_entry { key: "ESQ" value: 50 } @@ -733,8 +729,8 @@ CppConfig cpp_entry { key: "IPO" value: 10 } cpp_entry { key: "IPT" value: 10 } cpp_entry { key: "IS" value: 50 } - cpp_entry { key: "ITB" value: 744 } - cpp_entry { key: "ITP" value: 264 } + cpp_entry { key: "ITB" value: 745 } + cpp_entry { key: "ITP" value: 276 } cpp_entry { key: "IW" value: 50 } cpp_entry { key: "J4L" value: 80 } cpp_entry { key: "J7" value: 6250000 } @@ -748,7 +744,7 @@ CppConfig cpp_entry { key: "JKB" value: 10000 } cpp_entry { key: "JKF" value: 10000 } cpp_entry { key: "JKM" value: 10000 } - cpp_entry { key: "JML" value: 480 } + cpp_entry { key: "JML" value: 240 } cpp_entry { key: "JNC" value: 1000 } cpp_entry { key: "JNG" value: 10000 } cpp_entry { key: "JNL" value: 80 } @@ -811,6 +807,7 @@ CppConfig cpp_entry { key: "LIY" value: 1000 } cpp_entry { key: "LL" value: 42000 } cpp_entry { key: "LLR" value: 1000 } + cpp_entry { key: "LNG" value: 10000 } cpp_entry { key: "LPE" value: 47000 } cpp_entry { key: "LPS" value: 1000 } cpp_entry { key: "LSF" value: 1 } @@ -1147,7 +1144,7 @@ CppConfig cpp_entry { key: "SBT" value: 50 } cpp_entry { key: "SD" value: 1000 } cpp_entry { key: "SDA" value: 250 } - cpp_entry { key: "SDD" value: 1760 } + cpp_entry { key: "SDD" value: 1840 } cpp_entry { key: "SDG" value: 250 } cpp_entry { key: "SDI" value: 1000 } cpp_entry { key: "SDM" value: 100 } @@ -1164,10 +1161,12 @@ CppConfig cpp_entry { key: "SFR" value: 250 } cpp_entry { key: "SG" value: 250 } cpp_entry { key: "SGB" value: 1000 } + cpp_entry { key: "SGC" value: 1000 } cpp_entry { key: "SGE" value: 42000 } cpp_entry { key: "SGF" value: 1000 } cpp_entry { key: "SGG" value: 1000 } cpp_entry { key: "SGT" value: 250 } + cpp_entry { key: "SGU" value: 32.15 } cpp_entry { key: "SI" value: 5000 } cpp_entry { key: "SIL" value: 1000 } cpp_entry { key: "SIR" value: 500 } @@ -1186,7 +1185,7 @@ CppConfig cpp_entry { key: "SR3" value: 2500 } cpp_entry { key: "SR5" value: 1000 } cpp_entry { key: "SRB" value: 1000 } - cpp_entry { key: "SRT" value: 1760 } + cpp_entry { key: "SRT" value: 1840 } cpp_entry { key: "SSI" value: 1000 } cpp_entry { key: "SSP" value: 5000 } cpp_entry { key: "SSW" value: 1000 } @@ -1197,31 +1196,32 @@ CppConfig cpp_entry { key: "STY" value: 1000 } cpp_entry { key: "SU" value: 250 } cpp_entry { key: "SUT" value: 250 } - cpp_entry { key: "T03" value: 800 } + cpp_entry { key: "T01" value: 800 } cpp_entry { key: "T04" value: 800 } + cpp_entry { key: "T05" value: 800 } cpp_entry { key: "T06" value: 800 } cpp_entry { key: "T07" value: 800 } cpp_entry { key: "T08" value: 800 } - cpp_entry { key: "T09" value: 800 } + cpp_entry { key: "T11" value: 800 } cpp_entry { key: "T12" value: 800 } cpp_entry { key: "T13" value: 800 } - cpp_entry { key: "T14" value: 800 } cpp_entry { key: "T15" value: 800 } cpp_entry { key: "T16" value: 800 } - cpp_entry { key: "T19" value: 800 } + cpp_entry { key: "T17" value: 800 } + cpp_entry { key: "T18" value: 800 } cpp_entry { key: "T1U" value: 1000 } - cpp_entry { key: "T20" value: 800 } cpp_entry { key: "T21" value: 800 } cpp_entry { key: "T22" value: 800 } cpp_entry { key: "T23" value: 800 } - cpp_entry { key: "T26" value: 800 } - cpp_entry { key: "T27" value: 800 } + cpp_entry { key: "T24" value: 800 } + cpp_entry { key: "T25" value: 800 } cpp_entry { key: "T28" value: 800 } cpp_entry { key: "T29" value: 800 } cpp_entry { key: "T2B" value: 1000 } cpp_entry { key: "T2D" value: 1000 } cpp_entry { key: "T2M" value: 100 } cpp_entry { key: "T30" value: 800 } + cpp_entry { key: "T31" value: 800 } cpp_entry { key: "T3B" value: 1000 } cpp_entry { key: "T3L" value: 80 } cpp_entry { key: "T4B" value: 1000 } @@ -1276,7 +1276,7 @@ CppConfig cpp_entry { key: "TT" value: 50000 } cpp_entry { key: "TTD" value: 24 } cpp_entry { key: "TTE" value: 10000 } - cpp_entry { key: "TTF" value: 744 } + cpp_entry { key: "TTF" value: 745 } cpp_entry { key: "TUB" value: 1000 } cpp_entry { key: "TUF" value: 1000 } cpp_entry { key: "TUL" value: 1000 } @@ -1295,7 +1295,6 @@ CppConfig cpp_entry { key: "UDL" value: 80 } cpp_entry { key: "UFB" value: 100 } cpp_entry { key: "UFE" value: 100 } - cpp_entry { key: "UFG" value: 100 } cpp_entry { key: "UFN" value: 100 } cpp_entry { key: "UFU" value: 100 } cpp_entry { key: "UFV" value: 100 } @@ -1308,6 +1307,9 @@ CppConfig cpp_entry { key: "UN" value: 1000 } cpp_entry { key: "UNB" value: 1000 } cpp_entry { key: "UNS" value: 1000 } + cpp_entry { key: "UP5" value: 1000 } + cpp_entry { key: "UPB" value: 1000 } + cpp_entry { key: "UPM" value: 1000 } cpp_entry { key: "UR" value: 1000 } cpp_entry { key: "USC" value: 1000 } cpp_entry { key: "USDBRL" value: 1 } @@ -1462,7 +1464,6 @@ CmeSymbolMics { value { - cme_mic { key: "098" value: CBT } cme_mic { key: "0A" value: NYMEX } cme_mic { key: "0B" value: NYMEX } cme_mic { key: "0C" value: NYMEX } @@ -1809,6 +1810,7 @@ CmeSymbolMics cme_mic { key: "BOB" value: CBT } cme_mic { key: "BOO" value: NYMEX } cme_mic { key: "BOS" value: CME } + cme_mic { key: "BPU" value: NYMEX } cme_mic { key: "BR7" value: NYMEX } cme_mic { key: "BSF" value: CBT } cme_mic { key: "BTC" value: CME } @@ -1897,12 +1899,12 @@ CmeSymbolMics cme_mic { key: "D3L" value: NYMEX } cme_mic { key: "D4L" value: NYMEX } cme_mic { key: "D7L" value: NYMEX } + cme_mic { key: "DAB" value: NYMEX } cme_mic { key: "DAX" value: NYMEX } cme_mic { key: "DAZ" value: NYMEX } cme_mic { key: "DBB" value: NYMEX } cme_mic { key: "DBI" value: DME } cme_mic { key: "DBL" value: NYMEX } - cme_mic { key: "DBR" value: DME } cme_mic { key: "DBT" value: NYMEX } cme_mic { key: "DC" value: CME } cme_mic { key: "DCB" value: NYMEX } @@ -1920,7 +1922,6 @@ CmeSymbolMics cme_mic { key: "DFN" value: CBT } cme_mic { key: "DG2" value: CBT } cme_mic { key: "DG3" value: CBT } - cme_mic { key: "DGH" value: DME } cme_mic { key: "DGI" value: CBT } cme_mic { key: "DGS" value: CBT } cme_mic { key: "DGT" value: CBT } @@ -1928,19 +1929,13 @@ CmeSymbolMics cme_mic { key: "DHI" value: DME } cme_mic { key: "DJR" value: CBT } cme_mic { key: "DKR" value: DME } - cme_mic { key: "DMB" value: DME } - cme_mic { key: "DOB" value: DME } - cme_mic { key: "DOP" value: DME } - cme_mic { key: "DRB" value: DME } cme_mic { key: "DRI" value: CBT } cme_mic { key: "DRS" value: CBT } cme_mic { key: "DRT" value: CBT } cme_mic { key: "DSD" value: DME } cme_mic { key: "DSE" value: DME } - cme_mic { key: "DSG" value: DME } cme_mic { key: "DTR" value: CBT } cme_mic { key: "DTT" value: CBT } - cme_mic { key: "DUA" value: DME } cme_mic { key: "DUP" value: DME } cme_mic { key: "DVE" value: CME } cme_mic { key: "DVT" value: CME } @@ -1985,8 +1980,11 @@ CmeSymbolMics cme_mic { key: "EP1" value: NYMEX } cme_mic { key: "EPN" value: NYMEX } cme_mic { key: "EPZ" value: CME } + cme_mic { key: "EQ1" value: CME } cme_mic { key: "ERL" value: NYMEX } cme_mic { key: "ES" value: CME } + cme_mic { key: "ES1" value: CME } + cme_mic { key: "ES2" value: CME } cme_mic { key: "ESB" value: NYMEX } cme_mic { key: "ESK" value: CME } cme_mic { key: "ESQ" value: CME } @@ -2266,6 +2264,7 @@ CmeSymbolMics cme_mic { key: "LIY" value: CBT } cme_mic { key: "LL" value: NYMEX } cme_mic { key: "LLR" value: NYMEX } + cme_mic { key: "LNG" value: NYMEX } cme_mic { key: "LPE" value: NYMEX } cme_mic { key: "LPS" value: NYMEX } cme_mic { key: "LSF" value: COMEX } @@ -2619,10 +2618,12 @@ CmeSymbolMics cme_mic { key: "SFR" value: CME } cme_mic { key: "SG" value: CME } cme_mic { key: "SGB" value: NYMEX } + cme_mic { key: "SGC" value: COMEX } cme_mic { key: "SGE" value: NYMEX } cme_mic { key: "SGF" value: NYMEX } cme_mic { key: "SGG" value: NYMEX } cme_mic { key: "SGT" value: CME } + cme_mic { key: "SGU" value: COMEX } cme_mic { key: "SI" value: COMEX } cme_mic { key: "SIL" value: COMEX } cme_mic { key: "SIR" value: CME } @@ -2652,31 +2653,32 @@ CmeSymbolMics cme_mic { key: "STY" value: NYMEX } cme_mic { key: "SU" value: CME } cme_mic { key: "SUT" value: CME } - cme_mic { key: "T03" value: NYMEX } + cme_mic { key: "T01" value: NYMEX } cme_mic { key: "T04" value: NYMEX } + cme_mic { key: "T05" value: NYMEX } cme_mic { key: "T06" value: NYMEX } cme_mic { key: "T07" value: NYMEX } cme_mic { key: "T08" value: NYMEX } - cme_mic { key: "T09" value: NYMEX } + cme_mic { key: "T11" value: NYMEX } cme_mic { key: "T12" value: NYMEX } cme_mic { key: "T13" value: NYMEX } - cme_mic { key: "T14" value: NYMEX } cme_mic { key: "T15" value: NYMEX } cme_mic { key: "T16" value: NYMEX } - cme_mic { key: "T19" value: NYMEX } + cme_mic { key: "T17" value: NYMEX } + cme_mic { key: "T18" value: NYMEX } cme_mic { key: "T1U" value: CBT } - cme_mic { key: "T20" value: NYMEX } cme_mic { key: "T21" value: NYMEX } cme_mic { key: "T22" value: NYMEX } cme_mic { key: "T23" value: NYMEX } - cme_mic { key: "T26" value: NYMEX } - cme_mic { key: "T27" value: NYMEX } + cme_mic { key: "T24" value: NYMEX } + cme_mic { key: "T25" value: NYMEX } cme_mic { key: "T28" value: NYMEX } cme_mic { key: "T29" value: NYMEX } cme_mic { key: "T2B" value: NYMEX } cme_mic { key: "T2D" value: NYMEX } cme_mic { key: "T2M" value: NYMEX } cme_mic { key: "T30" value: NYMEX } + cme_mic { key: "T31" value: NYMEX } cme_mic { key: "T3B" value: NYMEX } cme_mic { key: "T3L" value: NYMEX } cme_mic { key: "T4B" value: NYMEX } @@ -2750,7 +2752,6 @@ CmeSymbolMics cme_mic { key: "UDL" value: NYMEX } cme_mic { key: "UFB" value: CBT } cme_mic { key: "UFE" value: CBT } - cme_mic { key: "UFG" value: CBT } cme_mic { key: "UFN" value: CBT } cme_mic { key: "UFU" value: CBT } cme_mic { key: "UFV" value: CBT } @@ -2763,6 +2764,9 @@ CmeSymbolMics cme_mic { key: "UN" value: NYMEX } cme_mic { key: "UNB" value: NYMEX } cme_mic { key: "UNS" value: NYMEX } + cme_mic { key: "UP5" value: NYMEX } + cme_mic { key: "UPB" value: NYMEX } + cme_mic { key: "UPM" value: NYMEX } cme_mic { key: "UR" value: NYMEX } cme_mic { key: "USC" value: NYMEX } cme_mic { key: "USDBRL" value: CME } diff --git a/src/lib/config/defaults/jetfix.conf b/src/lib/config/defaults/jetfix.conf index b4c0b4b51..4e4a4f558 100644 --- a/src/lib/config/defaults/jetfix.conf +++ b/src/lib/config/defaults/jetfix.conf @@ -5,7 +5,7 @@ JetFIXSimConnection { name: "sim" platform: SIM socket_connect_host_ip: "127.0.0.1" - socket_connect_port: @Ports.exchange_sim_fix_port + socket_connect_port: @Ports.glbx_sim_fix_port sender_compid: "SPARK" # this is the FIX session name sender_subid: "BTS" target_compid: "BTS" @@ -18,9 +18,506 @@ JetFIXSimConnection { seconds_to_pause : 1 max_connection_attempts: 9999999 } + sim_ice { + name: "sim_ice" + platform: SIMICE + socket_connect_host_ip: "127.0.0.1" + socket_connect_port: @Ports.ice_sim_fix_port + sender_compid: "51239" # this is the FIX session name + sender_subid: "BTS" + target_compid: "BTS" + target_subid: "BTS" + cme_market_segment_id: 0 + end_time: "02:00:00" + start_time: "02:05:00" + raw_data: "1JE9X" + use_cme_secure_logon: false + seconds_to_pause : 1 + } } } +JetFIX { + rule: "CannedCertSession.name='ice_cert'" + value { + #connection: @JetFIXSimConnection.sim_ice + connection { + name: "ice_cert" + platform: ICE + use_cme_secure_logon: false + log_fix_stream: true + socket_connect_host_ip: "63.247.113.154" + socket_connect_port: 80 + sender_compid: "51251" + target_compid: "ICE" + start_time: "22:55:00" + end_time: "22:05:00" + sender_subid: "BTS" + user_name: "bluetrd-os" #this is the order_server_id + raw_data: "Starts123" + max_order_msgs_per_window: 300 + auto_fix_seq_num: true + + seconds_to_replay: 6000 + } + connection: @JetFIXSimConnection.sim + #connection: @JetFIXSimConnection.sim_ice + + connection { + name: "cme_mc5_99" + platform: GLBX + socket_connect_host_ip: "69.50.112.199" + socket_connect_port: 61922 + sender_compid: "MC5004N" + target_compid: "CME" + target_subid: "99" + cme_market_segment_id: 99 + start_time: "00:30:00" + end_time: "23:30:00" + start_day: "sunday" + end_day: "friday" + sender_subid: "BTS" + raw_data: "S4PMA" + auto_fix_seq_num: true + } + connection { + name: "cme_mc5_58" + platform: GLBX + socket_connect_host_ip: "69.50.112.172" + socket_connect_port: 61922 + sender_compid: "MC5004N" + target_compid: "CME" + target_subid: "58" + cme_market_segment_id: 58 + start_time: "00:30:00" + end_time: "23:30:00" + start_day: "sunday" + end_day: "friday" + sender_subid: "BTS" + raw_data: "S4PMA" + auto_fix_seq_num: true + } + connection { + name: "cme_mc5_54" + platform: GLBX + socket_connect_host_ip: "69.50.112.191" + socket_connect_port: 61922 + sender_compid: "MC5004N" + target_compid: "CME" + target_subid: "54" + cme_market_segment_id: 54 + start_time: "00:30:00" + end_time: "23:30:00" + start_day: "sunday" + end_day: "friday" + sender_subid: "BTS" + raw_data: "S4PMA" + auto_fix_seq_num: true + } + connection { + name: "cme_mc5_82" + platform: GLBX + socket_connect_host_ip: "69.50.112.179" + socket_connect_port: 61922 + sender_compid: "MC5004N" + target_compid: "CME" + target_subid: "82" + cme_market_segment_id: 82 + start_time: "00:30:00" + end_time: "23:30:00" + start_day: "sunday" + end_day: "friday" + sender_subid: "BTS" + raw_data: "S4PMA" + auto_fix_seq_num: true + } + connection { + name: "cme_mc5_56" + platform: GLBX + socket_connect_host_ip: "69.50.112.174" + socket_connect_port: 61922 + sender_compid: "MC5004N" + target_compid: "CME" + target_subid: "56" + cme_market_segment_id: 56 + start_time: "00:30:00" + end_time: "23:30:00" + start_day: "sunday" + end_day: "friday" + sender_subid: "BTS" + raw_data: "S4PMA" + auto_fix_seq_num: true + } + connection { + name: "cme_mc5_50" + platform: GLBX + socket_connect_host_ip: "69.50.112.202" + socket_connect_port: 61922 + sender_compid: "MC5004N" + target_compid: "CME" + target_subid: "50" + cme_market_segment_id: 50 + start_time: "00:30:00" + end_time: "23:30:00" + start_day: "sunday" + end_day: "friday" + sender_subid: "BTS" + raw_data: "S4PMA" + auto_fix_seq_num: true + } + connection { + name: "cme_mc5_52" + platform: GLBX + socket_connect_host_ip: "69.50.112.181" + socket_connect_port: 61922 + sender_compid: "MC5004N" + target_compid: "CME" + target_subid: "52" + cme_market_segment_id: 52 + start_time: "00:30:00" + end_time: "23:30:00" + start_day: "sunday" + end_day: "friday" + sender_subid: "BTS" + raw_data: "S4PMA" + auto_fix_seq_num: true + } + } +} + +JetFIX { + rule: "CannedCertSession.name='gt_cert'" + value { + connection { + name: "ice_cert" + platform: ICE + use_cme_secure_logon: false + log_fix_stream: true + socket_connect_host_ip: "63.247.113.154" + socket_connect_port: 80 + sender_compid: "51251" + target_compid: "ICE" + start_time: "22:55:00" + end_time: "22:05:00" + sender_subid: "BTS" + user_name: "bluetrd-os" #this is the order_server_id + raw_data: "Starts123" + max_order_msgs_per_window: 300 + auto_fix_seq_num: true + + seconds_to_replay: 6000 + } + connection: @JetFIXSimConnection.sim + connection { + name: "cme_mc7_99" + platform: GLBX + socket_connect_host_ip: "69.50.112.199" + socket_connect_port: 61923 + sender_compid: "MC7004N" + target_compid: "CME" + target_subid: "99" + cme_market_segment_id: 99 + start_time: "00:30:00" + end_time: "23:30:00" + start_day: "sunday" + end_day: "friday" + sender_subid: "BTS" + raw_data: "S4PMA" + auto_fix_seq_num: true + } + connection { + name: "cme_mc7_58" + platform: GLBX + socket_connect_host_ip: "69.50.112.172" + socket_connect_port: 61923 + sender_compid: "MC7004N" + target_compid: "CME" + target_subid: "58" + cme_market_segment_id: 58 + start_time: "00:30:00" + end_time: "23:30:00" + start_day: "sunday" + end_day: "friday" + sender_subid: "BTS" + raw_data: "S4PMA" + auto_fix_seq_num: true + } + connection { + name: "cme_mc7_54" + platform: GLBX + socket_connect_host_ip: "69.50.112.191" + socket_connect_port: 61923 + sender_compid: "MC7004N" + target_compid: "CME" + target_subid: "54" + cme_market_segment_id: 54 + start_time: "00:30:00" + end_time: "23:30:00" + start_day: "sunday" + end_day: "friday" + sender_subid: "BTS" + raw_data: "S4PMA" + auto_fix_seq_num: true + } + connection { + name: "cme_mc7_82" + platform: GLBX + socket_connect_host_ip: "69.50.112.179" + socket_connect_port: 61923 + sender_compid: "MC7004N" + target_compid: "CME" + target_subid: "82" + cme_market_segment_id: 82 + start_time: "00:30:00" + end_time: "23:30:00" + start_day: "sunday" + end_day: "friday" + sender_subid: "BTS" + raw_data: "S4PMA" + auto_fix_seq_num: true + } + connection { + name: "cme_mc7_56" + platform: GLBX + socket_connect_host_ip: "69.50.112.174" + socket_connect_port: 61923 + sender_compid: "MC7004N" + target_compid: "CME" + target_subid: "56" + cme_market_segment_id: 56 + start_time: "00:30:00" + end_time: "23:30:00" + start_day: "sunday" + end_day: "friday" + sender_subid: "BTS" + raw_data: "S4PMA" + auto_fix_seq_num: true + } + connection { + name: "cme_mc7_50" + platform: GLBX + socket_connect_host_ip: "69.50.112.202" + socket_connect_port: 61923 + sender_compid: "MC7004N" + target_compid: "CME" + target_subid: "50" + cme_market_segment_id: 50 + start_time: "00:30:00" + end_time: "23:30:00" + start_day: "sunday" + end_day: "friday" + sender_subid: "BTS" + raw_data: "S4PMA" + auto_fix_seq_num: true + } + connection { + name: "cme_mc7_52" + platform: GLBX + socket_connect_host_ip: "69.50.112.181" + socket_connect_port: 61923 + sender_compid: "MC7004N" + target_compid: "CME" + target_subid: "52" + cme_market_segment_id: 52 + start_time: "00:30:00" + end_time: "23:30:00" + start_day: "sunday" + end_day: "friday" + sender_subid: "BTS" + raw_data: "S4PMA" + auto_fix_seq_num: true + } + connection { + name: "cme_mc7_60" + platform: GLBX + socket_connect_host_ip: "69.50.112.195" + socket_connect_port: 61923 + sender_compid: "MC7004N" + target_compid: "CME" + target_subid: "60" + cme_market_segment_id: 60 + start_time: "00:30:00" + end_time: "23:30:00" + start_day: "sunday" + end_day: "friday" + sender_subid: "BTS" + raw_data: "S4PMA" + auto_fix_seq_num: true + } + connection { + name: "cme_mc7_76" + platform: GLBX + socket_connect_host_ip: "69.50.112.204" + socket_connect_port: 61923 + sender_compid: "MC7004N" + target_compid: "CME" + target_subid: "76" + cme_market_segment_id: 76 + start_time: "00:30:00" + end_time: "23:30:00" + start_day: "sunday" + end_day: "friday" + sender_subid: "BTS" + raw_data: "S4PMA" + auto_fix_seq_num: true + } + connection { + name: "cme_mc7_88" + platform: GLBX + socket_connect_host_ip: "69.50.112.169" + socket_connect_port: 61923 + sender_compid: "MC7004N" + target_compid: "CME" + target_subid: "88" + cme_market_segment_id: 88 + start_time: "00:30:00" + end_time: "23:30:00" + start_day: "sunday" + end_day: "friday" + sender_subid: "BTS" + raw_data: "S4PMA" + auto_fix_seq_num: true + } + connection { + name: "cme_mc7_74" + platform: GLBX + socket_connect_host_ip: "69.50.112.197" + socket_connect_port: 61923 + sender_compid: "MC7004N" + target_compid: "CME" + target_subid: "74" + cme_market_segment_id: 74 + start_time: "00:30:00" + end_time: "23:30:00" + start_day: "sunday" + end_day: "friday" + sender_subid: "BTS" + raw_data: "S4PMA" + auto_fix_seq_num: true + } + connection { + name: "cme_mc7_64" + platform: GLBX + socket_connect_host_ip: "69.50.112.177" + socket_connect_port: 61923 + sender_compid: "MC7004N" + target_compid: "CME" + target_subid: "64" + cme_market_segment_id: 64 + start_time: "00:30:00" + end_time: "23:30:00" + start_day: "sunday" + end_day: "friday" + sender_subid: "BTS" + raw_data: "S4PMA" + auto_fix_seq_num: true + } + connection { + name: "cme_mc7_72" + platform: GLBX + socket_connect_host_ip: "69.50.112.189" + socket_connect_port: 61923 + sender_compid: "MC7004N" + target_compid: "CME" + target_subid: "72" + cme_market_segment_id: 72 + start_time: "00:30:00" + end_time: "23:30:00" + start_day: "sunday" + end_day: "friday" + sender_subid: "BTS" + raw_data: "S4PMA" + auto_fix_seq_num: true + } + connection { + name: "cme_mc7_70" + platform: GLBX + socket_connect_host_ip: "69.50.112.183" + socket_connect_port: 61923 + sender_compid: "MC7004N" + target_compid: "CME" + target_subid: "70" + cme_market_segment_id: 70 + start_time: "00:30:00" + end_time: "23:30:00" + start_day: "sunday" + end_day: "friday" + sender_subid: "BTS" + raw_data: "S4PMA" + auto_fix_seq_num: true + } + connection { + name: "cme_mc7_68" + platform: GLBX + socket_connect_host_ip: "69.50.112.168" + socket_connect_port: 61923 + sender_compid: "MC7004N" + target_compid: "CME" + target_subid: "68" + cme_market_segment_id: 68 + start_time: "00:30:00" + end_time: "23:30:00" + start_day: "sunday" + end_day: "friday" + sender_subid: "BTS" + raw_data: "S4PMA" + auto_fix_seq_num: true + } + connection { + name: "cme_mc7_80" + platform: GLBX + socket_connect_host_ip: "69.50.112.187" + socket_connect_port: 61923 + sender_compid: "MC7004N" + target_compid: "CME" + target_subid: "80" + cme_market_segment_id: 80 + start_time: "00:30:00" + end_time: "23:30:00" + start_day: "sunday" + end_day: "friday" + sender_subid: "BTS" + raw_data: "S4PMA" + auto_fix_seq_num: true + } + connection { + name: "cme_mc7_84" + platform: GLBX + socket_connect_host_ip: "69.50.112.198" + socket_connect_port: 61923 + sender_compid: "MC7004N" + target_compid: "CME" + target_subid: "84" + cme_market_segment_id: 84 + start_time: "00:30:00" + end_time: "23:30:00" + start_day: "sunday" + end_day: "friday" + sender_subid: "BTS" + raw_data: "S4PMA" + auto_fix_seq_num: true + } + connection { + name: "cme_mc7_78" + platform: GLBX + socket_connect_host_ip: "69.50.112.167" + socket_connect_port: 61923 + sender_compid: "MC7004N" + target_compid: "CME" + target_subid: "78" + cme_market_segment_id: 78 + start_time: "00:30:00" + end_time: "23:30:00" + start_day: "sunday" + end_day: "friday" + sender_subid: "BTS" + raw_data: "S4PMA" + auto_fix_seq_num: true + } + } +} + + JetFIX { rule: "CannedCertSession.name='mc7'" value { @@ -456,193 +953,193 @@ JetFIX { raw_data: "S4PMA" auto_fix_seq_num: true } - connection { - name: "cme_mc5_60" - platform: GLBX - socket_connect_host_ip: "69.50.112.195" - socket_connect_port: 61922 - sender_compid: "MC5004N" - target_compid: "CME" - target_subid: "60" - cme_market_segment_id: 60 - start_time: "00:30:00" - end_time: "23:30:00" - start_day: "sunday" - end_day: "friday" - sender_subid: "BTS" - raw_data: "S4PMA" - auto_fix_seq_num: true - } - connection { - name: "cme_mc5_76" - platform: GLBX - socket_connect_host_ip: "69.50.112.204" - socket_connect_port: 61922 - sender_compid: "MC5004N" - target_compid: "CME" - target_subid: "76" - cme_market_segment_id: 76 - start_time: "00:30:00" - end_time: "23:30:00" - start_day: "sunday" - end_day: "friday" - sender_subid: "BTS" - raw_data: "S4PMA" - auto_fix_seq_num: true - } - connection { - name: "cme_mc5_88" - platform: GLBX - socket_connect_host_ip: "69.50.112.169" - socket_connect_port: 61922 - sender_compid: "MC5004N" - target_compid: "CME" - target_subid: "88" - cme_market_segment_id: 88 - start_time: "00:30:00" - end_time: "23:30:00" - start_day: "sunday" - end_day: "friday" - sender_subid: "BTS" - raw_data: "S4PMA" - auto_fix_seq_num: true - } - connection { - name: "cme_mc5_74" - platform: GLBX - socket_connect_host_ip: "69.50.112.197" - socket_connect_port: 61922 - sender_compid: "MC5004N" - target_compid: "CME" - target_subid: "74" - cme_market_segment_id: 74 - start_time: "00:30:00" - end_time: "23:30:00" - start_day: "sunday" - end_day: "friday" - sender_subid: "BTS" - raw_data: "S4PMA" - auto_fix_seq_num: true - } - connection { - name: "cme_mc5_64" - platform: GLBX - socket_connect_host_ip: "69.50.112.177" - socket_connect_port: 61922 - sender_compid: "MC5004N" - target_compid: "CME" - target_subid: "64" - cme_market_segment_id: 64 - start_time: "00:30:00" - end_time: "23:30:00" - start_day: "sunday" - end_day: "friday" - sender_subid: "BTS" - raw_data: "S4PMA" - auto_fix_seq_num: true - } - connection { - name: "cme_mc5_72" - platform: GLBX - socket_connect_host_ip: "69.50.112.189" - socket_connect_port: 61922 - sender_compid: "MC5004N" - target_compid: "CME" - target_subid: "72" - cme_market_segment_id: 72 - start_time: "00:30:00" - end_time: "23:30:00" - start_day: "sunday" - end_day: "friday" - sender_subid: "BTS" - raw_data: "S4PMA" - auto_fix_seq_num: true - } - connection { - name: "cme_mc5_70" - platform: GLBX - socket_connect_host_ip: "69.50.112.183" - socket_connect_port: 61922 - sender_compid: "MC5004N" - target_compid: "CME" - target_subid: "70" - cme_market_segment_id: 70 - start_time: "00:30:00" - end_time: "23:30:00" - start_day: "sunday" - end_day: "friday" - sender_subid: "BTS" - raw_data: "S4PMA" - auto_fix_seq_num: true - } - connection { - name: "cme_mc5_68" - platform: GLBX - socket_connect_host_ip: "69.50.112.168" - socket_connect_port: 61922 - sender_compid: "MC5004N" - target_compid: "CME" - target_subid: "68" - cme_market_segment_id: 68 - start_time: "00:30:00" - end_time: "23:30:00" - start_day: "sunday" - end_day: "friday" - sender_subid: "BTS" - raw_data: "S4PMA" - auto_fix_seq_num: true - } - connection { - name: "cme_mc5_80" - platform: GLBX - socket_connect_host_ip: "69.50.112.187" - socket_connect_port: 61922 - sender_compid: "MC5004N" - target_compid: "CME" - target_subid: "80" - cme_market_segment_id: 80 - start_time: "00:30:00" - end_time: "23:30:00" - start_day: "sunday" - end_day: "friday" - sender_subid: "BTS" - raw_data: "S4PMA" - auto_fix_seq_num: true - } - connection { - name: "cme_mc5_84" - platform: GLBX - socket_connect_host_ip: "69.50.112.198" - socket_connect_port: 61922 - sender_compid: "MC5004N" - target_compid: "CME" - target_subid: "84" - cme_market_segment_id: 84 - start_time: "00:30:00" - end_time: "23:30:00" - start_day: "sunday" - end_day: "friday" - sender_subid: "BTS" - raw_data: "S4PMA" - auto_fix_seq_num: true - } - connection { - name: "cme_mc5_78" - platform: GLBX - socket_connect_host_ip: "69.50.112.167" - socket_connect_port: 61922 - sender_compid: "MC5004N" - target_compid: "CME" - target_subid: "78" - cme_market_segment_id: 78 - start_time: "00:30:00" - end_time: "23:30:00" - start_day: "sunday" - end_day: "friday" - sender_subid: "BTS" - raw_data: "S4PMA" - auto_fix_seq_num: true - } + # connection { + # name: "cme_mc5_60" + # platform: GLBX + # socket_connect_host_ip: "69.50.112.195" + # socket_connect_port: 61922 + # sender_compid: "MC5004N" + # target_compid: "CME" + # target_subid: "60" + # cme_market_segment_id: 60 + # start_time: "00:30:00" + # end_time: "23:30:00" + # start_day: "sunday" + # end_day: "friday" + # sender_subid: "BTS" + # raw_data: "S4PMA" + # auto_fix_seq_num: true + # } + # connection { + # name: "cme_mc5_76" + # platform: GLBX + # socket_connect_host_ip: "69.50.112.204" + # socket_connect_port: 61922 + # sender_compid: "MC5004N" + # target_compid: "CME" + # target_subid: "76" + # cme_market_segment_id: 76 + # start_time: "00:30:00" + # end_time: "23:30:00" + # start_day: "sunday" + # end_day: "friday" + # sender_subid: "BTS" + # raw_data: "S4PMA" + # auto_fix_seq_num: true + # } + # connection { + # name: "cme_mc5_88" + # platform: GLBX + # socket_connect_host_ip: "69.50.112.169" + # socket_connect_port: 61922 + # sender_compid: "MC5004N" + # target_compid: "CME" + # target_subid: "88" + # cme_market_segment_id: 88 + # start_time: "00:30:00" + # end_time: "23:30:00" + # start_day: "sunday" + # end_day: "friday" + # sender_subid: "BTS" + # raw_data: "S4PMA" + # auto_fix_seq_num: true + # } + # connection { + # name: "cme_mc5_74" + # platform: GLBX + # socket_connect_host_ip: "69.50.112.197" + # socket_connect_port: 61922 + # sender_compid: "MC5004N" + # target_compid: "CME" + # target_subid: "74" + # cme_market_segment_id: 74 + # start_time: "00:30:00" + # end_time: "23:30:00" + # start_day: "sunday" + # end_day: "friday" + # sender_subid: "BTS" + # raw_data: "S4PMA" + # auto_fix_seq_num: true + # } + # connection { + # name: "cme_mc5_64" + # platform: GLBX + # socket_connect_host_ip: "69.50.112.177" + # socket_connect_port: 61922 + # sender_compid: "MC5004N" + # target_compid: "CME" + # target_subid: "64" + # cme_market_segment_id: 64 + # start_time: "00:30:00" + # end_time: "23:30:00" + # start_day: "sunday" + # end_day: "friday" + # sender_subid: "BTS" + # raw_data: "S4PMA" + # auto_fix_seq_num: true + # } + # connection { + # name: "cme_mc5_72" + # platform: GLBX + # socket_connect_host_ip: "69.50.112.189" + # socket_connect_port: 61922 + # sender_compid: "MC5004N" + # target_compid: "CME" + # target_subid: "72" + # cme_market_segment_id: 72 + # start_time: "00:30:00" + # end_time: "23:30:00" + # start_day: "sunday" + # end_day: "friday" + # sender_subid: "BTS" + # raw_data: "S4PMA" + # auto_fix_seq_num: true + # } + # connection { + # name: "cme_mc5_70" + # platform: GLBX + # socket_connect_host_ip: "69.50.112.183" + # socket_connect_port: 61922 + # sender_compid: "MC5004N" + # target_compid: "CME" + # target_subid: "70" + # cme_market_segment_id: 70 + # start_time: "00:30:00" + # end_time: "23:30:00" + # start_day: "sunday" + # end_day: "friday" + # sender_subid: "BTS" + # raw_data: "S4PMA" + # auto_fix_seq_num: true + # } + # connection { + # name: "cme_mc5_68" + # platform: GLBX + # socket_connect_host_ip: "69.50.112.168" + # socket_connect_port: 61922 + # sender_compid: "MC5004N" + # target_compid: "CME" + # target_subid: "68" + # cme_market_segment_id: 68 + # start_time: "00:30:00" + # end_time: "23:30:00" + # start_day: "sunday" + # end_day: "friday" + # sender_subid: "BTS" + # raw_data: "S4PMA" + # auto_fix_seq_num: true + # } + # connection { + # name: "cme_mc5_80" + # platform: GLBX + # socket_connect_host_ip: "69.50.112.187" + # socket_connect_port: 61922 + # sender_compid: "MC5004N" + # target_compid: "CME" + # target_subid: "80" + # cme_market_segment_id: 80 + # start_time: "00:30:00" + # end_time: "23:30:00" + # start_day: "sunday" + # end_day: "friday" + # sender_subid: "BTS" + # raw_data: "S4PMA" + # auto_fix_seq_num: true + # } + # connection { + # name: "cme_mc5_84" + # platform: GLBX + # socket_connect_host_ip: "69.50.112.198" + # socket_connect_port: 61922 + # sender_compid: "MC5004N" + # target_compid: "CME" + # target_subid: "84" + # cme_market_segment_id: 84 + # start_time: "00:30:00" + # end_time: "23:30:00" + # start_day: "sunday" + # end_day: "friday" + # sender_subid: "BTS" + # raw_data: "S4PMA" + # auto_fix_seq_num: true + # } + # connection { + # name: "cme_mc5_78" + # platform: GLBX + # socket_connect_host_ip: "69.50.112.167" + # socket_connect_port: 61922 + # sender_compid: "MC5004N" + # target_compid: "CME" + # target_subid: "78" + # cme_market_segment_id: 78 + # start_time: "00:30:00" + # end_time: "23:30:00" + # start_day: "sunday" + # end_day: "friday" + # sender_subid: "BTS" + # raw_data: "S4PMA" + # auto_fix_seq_num: true + # } } } JetFIX { diff --git a/src/lib/config/defaults/orders_fills_cleanup.conf b/src/lib/config/defaults/orders_fills_cleanup.conf index 73597ff53..b26e96b6c 100644 --- a/src/lib/config/defaults/orders_fills_cleanup.conf +++ b/src/lib/config/defaults/orders_fills_cleanup.conf @@ -2,4 +2,14 @@ OrdersSoftCleanupCutoff { value { extra_days: 1 } +} + +SODTime +{ + rule: "DeploymentType.type == 'dev'" + value + { + sod_hh: 20 + sod_mm: 30 + } } \ No newline at end of file diff --git a/src/lib/config/defaults/position_cfg.conf b/src/lib/config/defaults/position_cfg.conf index daf8983a0..0cb2ea959 100644 --- a/src/lib/config/defaults/position_cfg.conf +++ b/src/lib/config/defaults/position_cfg.conf @@ -9,12 +9,12 @@ GMI_SymbolMap { } -GMI_Import { - rule: "DeploymentType.type == 'DEV'" - value { - sftp_host: "10.96.18.41" - sftp_port: 22222 - sftp_user: "PaTeACYB" - sftp_password: "DLXKuVK8" - } -} +#GMI_Import { +# rule: "DeploymentType.type == 'DEV'" +# value { +# sftp_host: "10.96.18.41" +# sftp_port: 22222 +# sftp_user: "PaTeACYB" +# sftp_password: "DLXKuVK8" +# } +#} diff --git a/src/lib/config/protos/MarketData.proto b/src/lib/config/protos/MarketData.proto index 18c08950a..b20424632 100755 --- a/src/lib/config/protos/MarketData.proto +++ b/src/lib/config/protos/MarketData.proto @@ -155,6 +155,7 @@ message ICEOptions message ICEChannel { required string name = 1; repeated uint32 market_type = 2; + repeated uint32 uds_market_type = 7; optional uint32 udp_buffer_size = 3 [default = 4194304]; //4mb optional uint32 heartbeat_interval = 4 [default = 36000]; optional uint32 sequence_gap_margin = 5 [default = 10000]; diff --git a/src/lib/config/protos/Ports.proto b/src/lib/config/protos/Ports.proto index 11ce66eae..2176315c1 100644 --- a/src/lib/config/protos/Ports.proto +++ b/src/lib/config/protos/Ports.proto @@ -5,6 +5,7 @@ message Ports { // see ports.conf for actual defaults optional int32 config_port = 1 ; optional int32 qbus_port = 2 ; - optional int32 exchange_sim_fix_port = 3 ; + optional int32 glbx_sim_fix_port = 3 ; + optional int32 ice_sim_fix_port = 5 ; optional int32 replicator_port = 4 ; } diff --git a/src/lib/config/protos/Symbols.proto b/src/lib/config/protos/Symbols.proto index 6d859c13f..0d71ab37f 100644 --- a/src/lib/config/protos/Symbols.proto +++ b/src/lib/config/protos/Symbols.proto @@ -20,4 +20,5 @@ message Symbols { optional SecurityExchangeList glbx = 10; optional SecurityExchangeList sim = 20; optional SecurityExchangeList ice = 30; + optional SecurityExchangeList simice = 40; } \ No newline at end of file diff --git a/src/lib/config/protos/UI.proto b/src/lib/config/protos/UI.proto index e926428e4..0a557f5bb 100644 --- a/src/lib/config/protos/UI.proto +++ b/src/lib/config/protos/UI.proto @@ -78,6 +78,13 @@ message OrderBookOptions { optional double status_color_opacity_when_selected = 2 [default = 0.35]; } +message OrderAlerts { + // when the client sees an order for the first time, + // this time period is used to determine if the order is actually new, + // or [presumably]just arrived bc shell is still initializing, or the server restarted. + optional int32 stale_order_threshold_seconds = 1 [default = 60]; +} + message ShellPingOptions { optional bool shell_ping_is_enabled = 1 [default = true]; optional int32 workloop_milliseconds_interval = 2 [default = 5000]; diff --git a/src/lib/config/protos/jetfix.proto b/src/lib/config/protos/jetfix.proto index c3666f5ef..6bc986d94 100644 --- a/src/lib/config/protos/jetfix.proto +++ b/src/lib/config/protos/jetfix.proto @@ -7,9 +7,11 @@ message JetFIX { required string name = 10; enum JetFIXPlatform { - GLBX = 1; - CBOE = 6; - SIM = 2; + GLBX = 1; + CBOE = 6; + SIM = 2; + ICE = 3; + SIMICE = 4; } optional JetFIXPlatform platform = 15 [default = GLBX]; @@ -47,6 +49,7 @@ message JetFIX { optional string target_subid = 130 [default = ""]; optional string sender_locationid = 135 [default = "US,NC"]; optional string raw_data = 136 [default =""]; + optional string user_name = 138 [default =""]; optional string self_match_prevention_id = 140 [default=""]; optional string cti_code = 145 [default="1"]; optional int32 customer_or_firm = 147 [default=1]; @@ -91,6 +94,7 @@ message JetFIX { message JetFIXSimConnection { required JetFIX.Connection sim = 10; + required JetFIX.Connection sim_ice = 20; } message CannedCertSession { diff --git a/src/lib/config/protos/subprocess.proto b/src/lib/config/protos/subprocess.proto new file mode 100644 index 000000000..b7c695c95 --- /dev/null +++ b/src/lib/config/protos/subprocess.proto @@ -0,0 +1,16 @@ +syntax='proto2'; +package bts.subprocess; + +message Process { + required string exe = 1; + repeated string args = 2; + message Variable { + required string name = 1; + optional string value = 2; + } + repeated Variable env = 3; +} + +message SubProcessList { + repeated Process process = 1; +} diff --git a/src/lib/config/protos/toggles.proto b/src/lib/config/protos/toggles.proto index 5c3c87ee0..787cbf7e0 100644 --- a/src/lib/config/protos/toggles.proto +++ b/src/lib/config/protos/toggles.proto @@ -61,4 +61,10 @@ message EnsureOnScreenWithWorkAreaRect { message IgnoreNansInDayPnL { optional bool enabled = 1 [default = true]; +} + +message DynamicAlgos{ + optional bool enable_dorman = 1 [default = false]; + optional bool enable_pyramid = 2 [default = false]; + optional bool enable_tas = 3 [default = false]; } \ No newline at end of file diff --git a/src/lib/domain_model/Security.cpp b/src/lib/domain_model/Security.cpp index 94df17488..40b2a2534 100644 --- a/src/lib/domain_model/Security.cpp +++ b/src/lib/domain_model/Security.cpp @@ -29,9 +29,10 @@ std::string descriptor(api::Security const &sec) { else if (sec.has_ice()) return sec.ice().security_desc(); else - return "No descriptor for non glbx exchange"; + return "No descriptor for non glbx / ice exchange"; } + boost::gregorian::date smallest_leg( ::google::protobuf::RepeatedPtrField<::bts::api::ContractLeg> const &l) { bts_assert(l.size()); diff --git a/src/lib/domain_model/Security.h b/src/lib/domain_model/Security.h index 722e6670b..697678f5b 100644 --- a/src/lib/domain_model/Security.h +++ b/src/lib/domain_model/Security.h @@ -43,6 +43,8 @@ inline api::Side other_side(api::Side side) { inline double market_epislon() { return 1e-10; } +// better == p1 more aggressive than p1 ie for a buy order p1 is higher +// than p2, and for a sell order p1 is lower than p2 inline bool is_better(api::Side side, Price p1, Price p2) { switch (side) { case api::SIDE_BUY: @@ -54,6 +56,7 @@ inline bool is_better(api::Side side, Price p1, Price p2) { } } +// like the above function inline Price improve_price(api::Side side, Price p, Price offset) { switch (side) { case api::SIDE_BUY: @@ -169,13 +172,30 @@ inline Price tick_size(api::Security const &sec) { // Looking to round md::IntPrice? Look in IntPrice.h -// computes the difference between 2 prices in ticks. -// the result is an a signed int so the order of args -// matter, its second price - first price +// computes price_2 - price_1 in ticks inline int price_diff_to_ticks(double price_1, double price_2, double tick_size) { return static_cast(std::round((price_2 - price_1) / tick_size)); } +// returns the difference (in ticks) bw and order and a market. +// if the order has crossed the market, returns -ve ticks +// (ie the buy order price > market_price or +// sell_order_price < market_price) +inline int order_diff_market(double order_price, + double market_price, + api::Side side, + double tick_size){ + switch (side) { + case api::SIDE_BUY: + return price_diff_to_ticks(order_price,market_price,tick_size); + case api::SIDE_SELL: + return price_diff_to_ticks(market_price,order_price,tick_size); + default: + bts_assert(false); + break; + } + return 0; +} // because of prices not being completely rounded to the tick, // simple double equality can not be used to compare prices. // this function deems 2 prices to be equal if they are within @@ -191,5 +211,6 @@ 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); } -} // namespace securities -} // namespace bts + + +}} diff --git a/src/lib/domain_model/Security.proto b/src/lib/domain_model/Security.proto index 52ca35299..d0ee67220 100644 --- a/src/lib/domain_model/Security.proto +++ b/src/lib/domain_model/Security.proto @@ -12,6 +12,7 @@ enum Venue { VENUE_CBOECFE = 207; VENUE_GLBX = 301; VENUE_ICE = 306; + VENUE_SIMICE = 3; } enum SecurityType { @@ -54,11 +55,12 @@ enum CMEMics { } enum ICEMics{ - ICE_UNSET=0; - ICE_US = 1; - ICE_EU = 2; - ICE_LIFFE = 3; - ICE_CA = 4; + IF_UNSET = 0; + IFU = 1; + IFE = 2; + IFL = 3; + IFC = 4; + IFEN = 5; } enum Side { @@ -103,8 +105,9 @@ message CBOECFEExchangeData { message ICEExchangeData { int64 exchange_id = 10; string security_desc = 15; //contractSymbol - int32 deal_display_factor = 20; - int32 settle_display_factor = 30; + int32 price_denominator = 17; + int32 deal_denominator = 20; + int32 settle_denominator = 30; string market_description = 100; string cleared_alias = 110; diff --git a/src/lib/jetfix/codec/FIXEncodingStream.h b/src/lib/jetfix/codec/FIXEncodingStream.h index 541e426b6..502c3786c 100644 --- a/src/lib/jetfix/codec/FIXEncodingStream.h +++ b/src/lib/jetfix/codec/FIXEncodingStream.h @@ -1,11 +1,11 @@ #pragma once -#include #include "lib/jetfix/codec/FIXBuffer.h" -#include "lib/jetfix/codec/MessageTypes.h" #include "lib/jetfix/codec/FIXFragment.h" -#include "lib/jetfix/codec/Fields.h" #include "lib/jetfix/codec/FieldDefinitions.h" +#include "lib/jetfix/codec/Fields.h" +#include "lib/jetfix/codec/MessageTypes.h" +#include #include "lib/config/protos/jetfix.pb.h" @@ -14,185 +14,186 @@ namespace jetfix { // Helper to batch the encoding of multiple FIX messages all sharing // the same FIX headers. -class FIXEncodingStream -{ -public: +class FIXEncodingStream { + public: FIXEncodingStream(config::JetFIX::Connection const &cfg, - uint32_t initial_capacity = 1024*10) - : - _msg_header_template (), - _header_template_size (), - _header_template_checksum (), - _buffer (initial_capacity), - _message_map (), - _current_msg (_buffer), - _state (State::OPEN), - _body_length_bm (), - _msg_type_bm (), - _seq_num_bm (), - _last_seq_num_bm (), - _sending_time_bm () - { - _message_map.reserve (10*1024); + uint32_t initial_capacity = 1024 * 10) + : _msg_header_template(), _header_template_size(), + _header_template_checksum(), _buffer(initial_capacity), _message_map(), + _current_msg(_buffer), _state(State::OPEN), _seq_num_bm(), + _last_seq_num_bm(), _sending_time_bm() { + _message_map.reserve(10 * 1024); - FIXBuffer header_buffer (&_msg_header_template[0],_msg_header_template.size ()); - FIXFragment header (header_buffer); - header.add("FIX.4.2"); + FIXBuffer header_buffer(&_msg_header_template[0], + _msg_header_template.size()); + FIXFragment header(header_buffer); - _body_length_bm = header.add(0,6); - _msg_type_bm = header.add(" ",1); - _seq_num_bm = header.add(0,9); - _last_seq_num_bm = header.add(0,9); + // We cannot include body lenght and msg type in the header + // because message type is not fixed lenght, and therefore its + // not possible to have bookmarks to fields after it, ahead of + // knowing the message type value. The rest of the header is + // fixed lenght so we form it here. + _seq_num_bm = header.add(0, 9); + _last_seq_num_bm = header.add(0, 9); _sending_time_bm = header.add( - "20150427-19:05:18.923",sizeof("20150427-19:05:18.923")-1); + "20150427-19:05:18.923", sizeof("20150427-19:05:18.923") - 1); - header.add(cfg.sender_compid ()); - header.add(cfg.target_compid ()); - header.add(cfg.target_subid ()); - _header_template_size = header.size (); - _header_template_checksum = header.checksum (); + header.add(cfg.sender_compid()); + header.add(cfg.target_compid()); + header.add(cfg.target_subid()); + _header_template_size = header.size(); + _header_template_checksum = header.checksum(); } - inline FIXFragment &new_msg (MessageTypes type) - { - bts_assert (_state == State::OPEN); - tidy_last_msg (); - - auto msg_offset = _buffer.size (); - _message_map.emplace_back (msg_offset,0u,0u,0u,FIXFragment::Bookmark>()); - _current_msg = FIXFragment (_buffer); - _current_msg.append (&_msg_header_template[0], - _header_template_size, - _header_template_checksum); - char msg_type_str[1] = {(char) type}; - _current_msg.overwrite (_msg_type_bm.shift (msg_offset),msg_type_str,1); + inline FIXFragment &new_msg(MessageTypes type) { + bts_assert(_state == State::OPEN); + tidy_last_msg(); + + auto msg_offset = _buffer.size(); + + _current_msg = FIXFragment(_buffer); + _begin_string_bm = _current_msg.add("FIX.4.2"); + _body_length_bm = _current_msg.add(0, 6); + + char mt_encoding[4]; + auto start = encode_message_type(type, mt_encoding); + _msg_type_bm = _current_msg.add( + mt_encoding + start, sizeof(mt_encoding) - start); + + auto header_offset = _buffer.size(); + _message_map.emplace_back(msg_offset, header_offset, 0u, 0u, 0u, + FIXFragment::Bookmark>()); + _current_msg.append(&_msg_header_template[0], _header_template_size, + _header_template_checksum); return _current_msg; } - - inline void close () - { - bts_assert (_state == State::OPEN); - tidy_last_msg (); + + inline void close() { + bts_assert(_state == State::OPEN); + tidy_last_msg(); _state = State::CLOSED; } - inline void reopen_empty () - { - bts_assert (_state == State::CLOSED); - bts_assert (_message_map.empty ()); + inline void reopen_empty() { + bts_assert(_state == State::CLOSED); + bts_assert(_message_map.empty()); _state = State::OPEN; } - - inline uint32_t size () const - {return _buffer.size ();} - inline const char* data () const - {return _buffer.data ();} + inline uint32_t size() const { return _buffer.size(); } - struct MessageDesc - { - MessageDesc (uint32_t offset, - uint32_t size, - uint32_t checksum, - uint32_t seq_num, - FIXFragment::Bookmark checksum_bm) - : offset (offset), - size (size), - checksum (checksum), - seq_num (seq_num), - checksum_bm (checksum_bm) - {} - + inline const char *data() const { return _buffer.data(); } + + struct MessageDesc { + MessageDesc(uint32_t offset, uint32_t header_offset, uint32_t size, + uint32_t checksum, uint32_t seq_num, + FIXFragment::Bookmark checksum_bm) + : offset(offset), header_offset(header_offset), size(size), + checksum(checksum), seq_num(seq_num), checksum_bm(checksum_bm) {} uint32_t offset; + uint32_t header_offset; uint32_t size; uint32_t checksum; uint32_t seq_num; FIXFragment::Bookmark checksum_bm; }; - - std::string to_str () const - { + + std::string to_str() const { std::string result; - - if (size ()) { + + if (size()) { // return std::string(data(),size() is simpler but valgrind // reports a warning in that call - result.resize (size ()); - std::copy (data (), data ()+size(),result.begin ()); + result.resize(size()); + std::copy(data(), data() + size(), result.begin()); } return result; } - std::string to_human_str () const - { - auto s = to_str (); - std::replace( s.begin(), s.end(), '\001', '|' ); + std::string to_human_str() const { + auto s = to_str(); + std::replace(s.begin(), s.end(), '\001', '|'); return s; } - - inline uint32_t stamp (uint32_t seq_num,uint32_t last_processed, std::string const&time_str) - { - bts_assert (_state == State::CLOSED); + + inline uint32_t stamp(uint32_t seq_num, uint32_t last_processed, + std::string const &time_str) { + bts_assert(_state == State::CLOSED); _state = State::STAMPED; for (auto &m : _message_map) { ++seq_num; m.seq_num = seq_num; - FIXFragment frag (_buffer,m.checksum); - frag.overwrite (_seq_num_bm.shift (m.offset),m.seq_num,9); - frag.overwrite (_last_seq_num_bm.shift (m.offset),last_processed,9); - frag.overwrite (_sending_time_bm.shift (m.offset),time_str); - frag.overwrite_checksum (m.checksum_bm,frag.checksum () % 256); + FIXFragment frag(_buffer, m.checksum); + frag.overwrite(_seq_num_bm.shift(m.header_offset), m.seq_num, 9); + frag.overwrite(_last_seq_num_bm.shift(m.header_offset), last_processed, + 9); + frag.overwrite(_sending_time_bm.shift(m.header_offset), time_str); + frag.overwrite_checksum(m.checksum_bm, frag.checksum() % 256); } return seq_num; } - inline void reset () - { - _buffer.reset (); - _message_map.clear (); + inline void reset() { + _buffer.reset(); + _message_map.clear(); _state = State::OPEN; } - inline bool is_closed () const - {return _state == State::CLOSED;} + inline bool is_closed() const { return _state == State::CLOSED; } - size_t n_msgs () const {return _message_map.size ();} - - std::vector const &msg_map () const {return _message_map;} -private: - std::array _msg_header_template; + size_t n_msgs() const { return _message_map.size(); } + + std::vector const &msg_map() const { return _message_map; } + + private: + std::array _msg_header_template; uint32_t _header_template_size; uint32_t _header_template_checksum; FIXBuffer _buffer; std::vector _message_map; FIXFragment _current_msg; - enum class State { OPEN,CLOSED,STAMPED}; + enum class State { OPEN, CLOSED, STAMPED }; State _state; - + + // These bookmarks point to the current msg (not the header) + FIXFragment::Bookmark _begin_string_bm; FIXFragment::Bookmark _body_length_bm; FIXFragment::Bookmark _msg_type_bm; + + // These bookmarks point to the header and thus have to be shifted + // by the offset of the end of the message type. FIXFragment::Bookmark _seq_num_bm; FIXFragment::Bookmark _last_seq_num_bm; typedef StringField SendingTimeStr; FIXFragment::Bookmark _sending_time_bm; - inline void tidy_last_msg () - { - if (_message_map.size ()) { - auto &this_msg_map = _message_map.back (); - auto start = - this_msg_map.offset + _msg_type_bm.offset - fix::MsgType::tag_sz-1; - auto end = _buffer.size (); - _current_msg.overwrite (_body_length_bm.shift (this_msg_map.offset), - end-start,6); - this_msg_map.checksum = _current_msg.checksum (); - this_msg_map.checksum_bm = _current_msg.add_checksum (0); - this_msg_map.size = _buffer.size () - this_msg_map.offset; + inline void tidy_last_msg() { + if (_message_map.size()) { + auto &this_msg_map = _message_map.back(); + auto start = _msg_type_bm.offset - fix::MsgType::tag_sz - 1; + auto end = _buffer.size(); + _current_msg.overwrite(_body_length_bm, end - start, 6); + this_msg_map.checksum = _current_msg.checksum(); + this_msg_map.checksum_bm = _current_msg.add_checksum(0); + this_msg_map.size = _buffer.size() - this_msg_map.offset; } } + + uint32_t encode_message_type(MessageTypes m, char out[4]) { + auto mt = (uint32_t)m; + for (int i = 0; i < 4; i++) { + out[i] = mt >> (8 * (3 - i)); + mt &= ((1 << (8 * (3 - i))) - 1); + } + for (int i = 0; i < 4; i++) + if (out[i]) + return i; + return 0; + } }; -}} +} // namespace jetfix +} // namespace bts diff --git a/src/lib/jetfix/codec/FieldDefinitions.h b/src/lib/jetfix/codec/FieldDefinitions.h index cad3503e0..5ba96346a 100644 --- a/src/lib/jetfix/codec/FieldDefinitions.h +++ b/src/lib/jetfix/codec/FieldDefinitions.h @@ -70,6 +70,9 @@ typedef StringField<143> TargetLocationID; typedef StringField<1603> ApplicationSystemName; typedef StringField<1604> ApplicationSystemVersion; typedef StringField<1605> ApplicationSystemVendor; +typedef StringField<9450> ICEApplicationSystemName; +typedef StringField<9451> ICEApplicationSystemVersion; +typedef StringField<9452> ICEApplicationSystemVendor; typedef StringField<167> SecurityType; typedef StringField<17> ExecID; typedef StringField<19> ExecRefID; @@ -98,6 +101,7 @@ typedef StringField<7928> SelfMatchPreventionID; typedef StringField<8> BeginString; typedef StringField<9324> OrderOptionalData; typedef StringField<96> RawData; +typedef StringField<553> UserName; typedef StringField<9717> CorrelationClOrdID; typedef StringField<9771> MMAccount; typedef TimeField<122> OrigSendingTime; @@ -105,6 +109,38 @@ typedef TimeField<52> SendingTime; typedef TimeField<5979> RequestTime; typedef TimeField<60> TransactTime; +//ICE +typedef IntField<9208> Int_CtiCode; +typedef BigIntField<11> Int_ClOrdID; +typedef BigIntField<37> Int_OrderID; +typedef BigIntField<41> Int_OrigClOrdID; +typedef IntField<109> CompanyID; +typedef IntField<55> Int_Symbol; +typedef IntField<439> Int_ClearingFirm; +typedef StringField<9121> Memo; +typedef IntField<103> OrdRejReason; +typedef IntField<285> DeleteReason; +typedef CharField<9195> AccountCode; +typedef StringField<9207> CustomerAccountRefId; +typedef StringField<115> OnBehalfOfCompID; +typedef StringField<116> OnBehalfOfSubID; +typedef StringField<144> OnBehalfOfLocationID; +typedef StringField<9139> OriginatorUserID; +typedef CharField<18> ExecInst; +typedef IntField<210> MaxShow; +typedef IntField<9006> ICEStrategyPreference; +typedef IntField<9007> ICETimeStampPreference; +typedef StringField<1031> CustOrderHandlingInst; + +typedef IntField<9700> MifidDirectElectronicAccess; +typedef IntField<9701> MifidTradingCapacity; +typedef IntField<9702> MifidLiquidityProvision; +typedef IntField<9703> MifidCommodityDerivIndicator; +typedef BigIntField<9704> MifidInvestmentDecision; +typedef BigIntField<9705> MifidExecutionDecision; +typedef BigIntField<9706> MifidClientIDCode; +typedef BigIntField<9707> MifidId; + typedef Group +class BigIntField : public BaseField +{ +public: + typedef BaseField Parent; + + void read (ReaderCursor &cursor) + { + auto loc = cursor.read_field (); + Parent::_value = atoll (&(cursor.data ()[loc.first])); + } + + static uint32_t max_value_size (int64_t,int pad = 0) + { + if (pad == 0) + return 20; + else + return pad; + } + + static std::pair write (char*buffer,int64_t i,int pad = 0) + { + auto written = i64_toa (i,buffer,pad); + return std::make_pair (written,sum_bytes (buffer,written)); + } +}; + template class StringField : public BaseField { diff --git a/src/lib/jetfix/codec/MessageDefinitions.h b/src/lib/jetfix/codec/MessageDefinitions.h index 1674673f5..75f45f3e6 100644 --- a/src/lib/jetfix/codec/MessageDefinitions.h +++ b/src/lib/jetfix/codec/MessageDefinitions.h @@ -67,5 +67,58 @@ typedef FIXMsg CancelRejectMsg; +typedef FIXMsg +ICETraderLoginResponseMsg; + +typedef FIXMsg +ICETraderLogoutResponseMsg; + +typedef FIXMsg +ICEExecutionReportMsg; + +typedef FIXMsg +ICECancelRejectMsg; + }}} diff --git a/src/lib/jetfix/codec/MessageTypes.h b/src/lib/jetfix/codec/MessageTypes.h index 39dfd7b34..a7154e8a9 100644 --- a/src/lib/jetfix/codec/MessageTypes.h +++ b/src/lib/jetfix/codec/MessageTypes.h @@ -5,7 +5,7 @@ namespace bts { namespace jetfix { -enum class MessageTypes +enum class MessageTypes : uint32_t { Advertisement = '7', Allocation = 'J', @@ -48,7 +48,12 @@ enum class MessageTypes SettlementInstructions = 'T', TestRequest = '1', TradingSessionStatus = 'h', - TradingSessionStatusRequest = 'g' + TradingSessionStatusRequest = 'g', + ICETraderLogin = ('U' << 16) + ('C' << 8) + 'G', + ICETraderLoginResponse = ('U' << 16) + ('C' << 8) + 'I', + ICETraderLogout = ('U' << 16) + ('C' << 8) + 'H', + ICETraderLogoutResponse = ('U' << 16) + ('C' << 8) + 'J', + }; }} diff --git a/src/lib/jetfix/codec/i32_toa.h b/src/lib/jetfix/codec/i32_toa.h index be546f39b..afc257ebc 100644 --- a/src/lib/jetfix/codec/i32_toa.h +++ b/src/lib/jetfix/codec/i32_toa.h @@ -259,3 +259,199 @@ inline int u32_toa(uint32_t u, char* buffer, int pad) { } +///https://github.com/miloyip/itoa-benchmark + +#define BEGIN2(n) \ + do { \ + int t = (n); \ + if(t < 10) *p++ = '0' + t; \ + else { \ + t *= 2; \ + *p++ = gDigitsLut[t]; \ + *p++ = gDigitsLut[t + 1]; \ + } \ + } while(0) +#define MIDDLE2(n) \ + do { \ + int t = (n) * 2; \ + *p++ = gDigitsLut[t]; \ + *p++ = gDigitsLut[t + 1]; \ + } while(0) +#define BEGIN4(n) \ + do { \ + int t4 = (n); \ + if(t4 < 100) BEGIN2(t4); \ + else { BEGIN2(t4 / 100); MIDDLE2(t4 % 100); } \ + } while(0) +#define MIDDLE4(n) \ + do { \ + int t4 = (n); \ + MIDDLE2(t4 / 100); MIDDLE2(t4 % 100); \ + } while(0) +#define BEGIN8(n) \ + do { \ + uint32_t t8 = (n); \ + if(t8 < 10000) BEGIN4(t8); \ + else { BEGIN4(t8 / 10000); MIDDLE4(t8 % 10000); } \ + } while(0) +#define MIDDLE8(n) \ + do { \ + uint32_t t8 = (n); \ + MIDDLE4(t8 / 10000); MIDDLE4(t8 % 10000); \ + } while(0) +#define MIDDLE16(n) \ + do { \ + uint64_t t16 = (n); \ + MIDDLE8(t16 / 100000000); MIDDLE8(t16 % 100000000); \ + } while(0) + +inline int u64_toa(uint64_t value, char* p) +{ + auto start = p; + if(value < 100000000) BEGIN8(value); + else if(value < 10000000000000000) { BEGIN8(value / 100000000); MIDDLE8(value % 100000000); } + else { BEGIN4(value / 10000000000000000); MIDDLE16(value % 10000000000000000); } + *p = 0; + return p-start; +} + +inline int count_digits (int64_t value) +{ + static auto P01 = 10ull; + static auto P02 = P01*10; + static auto P03 = P02*10; + static auto P04 = P03*10; + static auto P05 = P04*10; + static auto P06 = P05*10; + static auto P07 = P06*10; + static auto P08 = P07*10; + static auto P09 = P08*10; + static auto P10 = P09*10; + static auto P11 = P10*10; + static auto P12 = P11*10; + static auto P13 = P12*10; + static auto P14 = P13*10; + static auto P15 = P14*10; + static auto P16 = P15*10; + static auto P17 = P16*10; + static auto P18 = P17*10; + static auto P19 = P18*10; + + uint64_t u = static_cast(value); + if (value < 0) + u = ~u + 1; + + if (u < P01) return 1; + if (u < P02) return 2; + if (u < P03) return 3; + if (u < P04) return 4; + if (u < P05) return 5; + if (u < P06) return 6; + if (u < P07) return 7; + if (u < P08) return 8; + if (u < P09) return 9; + if (u < P10) return 10; + if (u < P11) return 11; + if (u < P12) return 12; + if (u < P13) return 13; + if (u < P14) return 14; + if (u < P15) return 15; + if (u < P16) return 16; + if (u < P17) return 17; + if (u < P18) return 18; + if (u < P19) return 19; + return 20; +} + +inline int count_digits (uint64_t u) +{ + static auto P01 = 10ull; + static auto P02 = P01*10; + static auto P03 = P02*10; + static auto P04 = P03*10; + static auto P05 = P04*10; + static auto P06 = P05*10; + static auto P07 = P06*10; + static auto P08 = P07*10; + static auto P09 = P08*10; + static auto P10 = P09*10; + static auto P11 = P10*10; + static auto P12 = P11*10; + static auto P13 = P12*10; + static auto P14 = P13*10; + static auto P15 = P14*10; + static auto P16 = P15*10; + static auto P17 = P16*10; + static auto P18 = P17*10; + static auto P19 = P18*10; + + if (u < P01) return 1; + if (u < P02) return 2; + if (u < P03) return 3; + if (u < P04) return 4; + if (u < P05) return 5; + if (u < P06) return 6; + if (u < P07) return 7; + if (u < P08) return 8; + if (u < P09) return 9; + if (u < P10) return 10; + if (u < P11) return 11; + if (u < P12) return 12; + if (u < P13) return 13; + if (u < P14) return 14; + if (u < P15) return 15; + if (u < P16) return 16; + if (u < P17) return 17; + if (u < P18) return 18; + if (u < P19) return 19; + return 20; +} + +inline int i64_toa(int64_t value, char* buffer, int pad) +{ + auto start = buffer; + uint64_t u = static_cast(value); + if (value < 0) { + *buffer++ = '-'; + u = ~u + 1; + } + if (pad) + { + auto digits = count_digits (value); + auto offset = pad - digits; + if (offset > 0) + { + auto start = buffer + offset; + while (buffer != start) + { + *buffer='0'; + buffer++; + } + } + else if (offset < 0) + return 0; + } + return (buffer-start) + u64_toa(u, buffer); +} + +inline int u64_toa(uint64_t u, char* buffer, int pad) +{ + auto start = buffer; + if (pad) + { + auto digits = count_digits (u); + auto offset = pad - digits; + if (offset > 0) + { + auto start = buffer + offset; + while (buffer != start) + { + *buffer='0'; + buffer++; + } + } + else if (offset < 0) + return 0; + } + return (buffer-start) + u64_toa(u, buffer); +} \ No newline at end of file diff --git a/src/lib/jetfix/gateway/Session.cpp b/src/lib/jetfix/gateway/Session.cpp index 4cc176620..5b669766c 100644 --- a/src/lib/jetfix/gateway/Session.cpp +++ b/src/lib/jetfix/gateway/Session.cpp @@ -783,42 +783,63 @@ OutFIXPtr make_logon(uint32_t seq_num, uint32_t last_seq_num_processed, const char *application = "O2"; #endif - buffer.add(application); - buffer.add("1.0"); - buffer.add("BTS"); - - if (session_cfg.has_trading_session_id()) { - buffer.add(1); - buffer.add(session_cfg.trading_session_id()); - } - if (session_cfg.has_udf_support_indicator()) - buffer.add(session_cfg.udf_support_indicator()); - - if (session_cfg.use_cme_secure_logon()) { - bts_assert(session_cfg.sender_compid().size() >= 3); - auto session_id = session_cfg.sender_compid().substr(0, 3); - std::string seq_num_str(9, ' '); - i32_toa(seq_num, &seq_num_str[0], 9); - - std::string last_seq_num_str(9, ' '); - i32_toa(last_seq_num_processed, &last_seq_num_str[0], 9); - auto ld = glbx::calculate_hmac( - session_id, seq_num_str, session_cfg.sender_compid(), - session_cfg.sender_subid(), time_str, session_cfg.target_subid(), - std::to_string(session_cfg.seconds_between_hbs()), - session_cfg.sender_locationid(), last_seq_num_str, application, "1.0", - "BTS"); -#ifndef BTS_EDGE - LOG(Jetfix, Info, "Canonical string for secure logon: {}", ld.canonical); -#endif - buffer.add(ld.access_key.size()); - buffer.add(ld.access_key); - buffer.add("CME-1-SHA-256"); - buffer.add(ld.signature.size()); - buffer.add(ld.signature); - } else { + if(session_cfg.platform() == bts::config::JetFIX_Connection_JetFIXPlatform_ICE || + session_cfg.platform() == bts::config::JetFIX_Connection_JetFIXPlatform_SIMICE) + { + buffer.add(application); + buffer.add("1.0"); + buffer.add("BTS"); + + buffer.add(session_cfg.user_name()); buffer.add(session_cfg.raw_data().size()); - buffer.add(session_cfg.raw_data()); + buffer.add(session_cfg.raw_data()); + buffer.add(0); + buffer.add(1); + buffer.add(1); + } + else + { + buffer.add(application); + buffer.add("1.0"); + buffer.add("BTS"); + + if (session_cfg.has_trading_session_id()) { + buffer.add(1); + buffer.add(session_cfg.trading_session_id()); + } + if (session_cfg.has_udf_support_indicator()) + buffer.add(session_cfg.udf_support_indicator()); + + if (session_cfg.use_cme_secure_logon()) { + bts_assert(session_cfg.sender_compid().size() >= 3); + auto session_id = session_cfg.sender_compid().substr(0, 3); + std::string seq_num_str(9, ' '); + i32_toa(seq_num, &seq_num_str[0], 9); + + std::string last_seq_num_str(9, ' '); + i32_toa(last_seq_num_processed, &last_seq_num_str[0], 9); + auto ld = glbx::calculate_hmac( + session_id, seq_num_str, session_cfg.sender_compid(), + session_cfg.sender_subid(), time_str, session_cfg.target_subid(), + std::to_string(session_cfg.seconds_between_hbs()), + session_cfg.sender_locationid(), last_seq_num_str, application, "1.0", + "BTS"); + #ifndef BTS_EDGE + LOG(Jetfix, Info, "Canonical string for secure logon: {}", ld.canonical); + #endif + buffer.add(ld.access_key.size()); + buffer.add(ld.access_key); + buffer.add("CME-1-SHA-256"); + buffer.add(ld.signature.size()); + buffer.add(ld.signature); + } + else + { + buffer.add(session_cfg.raw_data().size()); + buffer.add(session_cfg.raw_data()); + if(session_cfg.user_name() != "") + buffer.add(session_cfg.user_name()); + } } stream.close(); diff --git a/src/lib/logging/logging.cpp b/src/lib/logging/logging.cpp index 46d575b2a..e2cd92a96 100644 --- a/src/lib/logging/logging.cpp +++ b/src/lib/logging/logging.cpp @@ -118,7 +118,7 @@ void configure_logging(bts::config::Logging const&cfg, bts::config::ThreadGroupC {log::Latency,"Latency"}, {log::DataFlow,"DataFlow"}, {log::Plugin,"Plugin"}, - {log::ClientSdk,"ClientSdk"}, + {log::Sdk,"Sdk"}, {log::Http,"Http"}, }; diff --git a/src/lib/logging/logging.h b/src/lib/logging/logging.h index 0bfea9935..a2829d224 100644 --- a/src/lib/logging/logging.h +++ b/src/lib/logging/logging.h @@ -44,8 +44,8 @@ constexpr log::Subsystem Testing {Mercury.ordinal+1 }; 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 }; +constexpr log::Subsystem Sdk {Plugin.ordinal+1 }; +constexpr log::Subsystem Http {Sdk.ordinal+1 }; struct LoggerHandle { LoggerHandle(LoggerHandle const&) = delete; diff --git a/src/lib/md/MDConsumerHandle.cpp b/src/lib/md/MDConsumerHandle.cpp index 5b585213b..442473281 100644 --- a/src/lib/md/MDConsumerHandle.cpp +++ b/src/lib/md/MDConsumerHandle.cpp @@ -296,7 +296,7 @@ TimeStamp copy_synthetic_depth(const SyntheticStrategy& synth, size_t count, Pri // END SYNTHETIC DEPTH ///////////////////////////// -bool MDConsumerHandle::get_last_trade(TimeStamp& time, PriceSize& trade, datetime::LLDateTime& trade_timestamp, size_t* total_volume) const { +bool MDConsumerHandle::get_last_trade(TimeStamp& time, PriceSize& trade, datetime::LLDateTime& trade_timestamp, size_t* total_volume, int* from_spread) const { switch(_type) { case HANDLETYPE_VOID: return false; case HANDLETYPE_DEPTH: @@ -306,6 +306,7 @@ bool MDConsumerHandle::get_last_trade(TimeStamp& time, PriceSize& trade, datetim if (result) { trade_timestamp = pcold->last_trade_time; if (total_volume) { *total_volume = pcold->total_volume; } + if (from_spread) { *from_spread = pcold->from_spread; } } return result; } diff --git a/src/lib/md/MDConsumerHandle.h b/src/lib/md/MDConsumerHandle.h index bc094189f..c271083fd 100644 --- a/src/lib/md/MDConsumerHandle.h +++ b/src/lib/md/MDConsumerHandle.h @@ -261,7 +261,7 @@ public: return get_book(read_combined, time, copydepth, bids, asks); } - bool get_last_trade(TimeStamp& time, PriceSize& out_trade, datetime::LLDateTime& out_trade_timestamp, size_t* total_volume=nullptr) const; + bool get_last_trade(TimeStamp& time, PriceSize& out_trade, datetime::LLDateTime& out_trade_timestamp, size_t* total_volume=nullptr,int* from_spread=nullptr) const; IntPrice settlement_price() const { switch(_type) { diff --git a/src/lib/md/MDProducerHandle.cpp b/src/lib/md/MDProducerHandle.cpp index c38e53daf..8ee9c09e3 100644 --- a/src/lib/md/MDProducerHandle.cpp +++ b/src/lib/md/MDProducerHandle.cpp @@ -101,8 +101,9 @@ void MDProducerHandle::set_status(CoconutSecurityStatus status) { } } -void MDProducerHandle::add_trade(TimeStamp time, const PriceSize price, LLDateTime tradetime, co2::AggressorSide agside, uint32_t volume) { - // Add this trade to the last trade if price match +void MDProducerHandle::add_trade(TimeStamp time, const PriceSize price, LLDateTime tradetime, + co2::AggressorSide agside, uint32_t volume, bool from_spread) { + // Add this trade to the last trade if price/side match auto current = std::get(cdata().last_trade.load()); if (current.int_price() == price.int_price()) { cdata().last_trade.store(PriceSize { price.int_price(), price.size() + current.size() }, time); @@ -111,7 +112,9 @@ void MDProducerHandle::add_trade(TimeStamp time, const PriceSize price, LLDateTi cdata().last_trade.store(price,time); } cdata().last_trade_time = tradetime; - cdata().total_volume = volume; + cdata().from_spread = (from_spread ? 1 : 0); + if (volume > 0) + cdata().total_volume = volume; if (key() != invalid_security_key) { co2::TradeMessage trade; @@ -125,9 +128,15 @@ void MDProducerHandle::add_trade(TimeStamp time, const PriceSize price, LLDateTi _channel.add_trade(trade); } } -void MDProducerHandle::set_last_trade(TimeStamp time, const PriceSize ps, datetime::LLDateTime tradetime, uint32_t total_volume) { +void MDProducerHandle::set_last_trade(TimeStamp time, const PriceSize ps, datetime::LLDateTime tradetime, uint32_t total_volume, bool from_spread) { cdata().last_trade.store(ps,time); cdata().last_trade_time = tradetime; + cdata().from_spread = (from_spread ? 1 : 0); + if(total_volume > 0) + cdata().total_volume = total_volume; +} + +void MDProducerHandle::set_total_volume(uint32_t total_volume) { cdata().total_volume = total_volume; } diff --git a/src/lib/md/MDProducerHandle.h b/src/lib/md/MDProducerHandle.h index da5afcf2d..78466987a 100644 --- a/src/lib/md/MDProducerHandle.h +++ b/src/lib/md/MDProducerHandle.h @@ -92,9 +92,11 @@ public: void set_status(co2::CoconutSecurityStatus status); // hot // Updates trade log and last trade - void add_trade(TimeStamp time, const PriceSize pricesize, datetime::LLDateTime date, co2::AggressorSide agside, uint32_t total_volume=0); // cold + void add_trade(TimeStamp time, const PriceSize pricesize, datetime::LLDateTime date, + co2::AggressorSide agside, uint32_t total_volume=0, bool from_spread=false); // cold // Update last_trade without updating trade log - void set_last_trade(TimeStamp time, const PriceSize ps, datetime::LLDateTime date, uint32_t total_volume); + void set_last_trade(TimeStamp time, const PriceSize ps, datetime::LLDateTime date, uint32_t total_volume=0,bool from_spread=false); + void set_total_volume(uint32_t total_volume); void add_rfq(TimeStamp time, const co2::QuoteRequestMessage& rfq); // cold void update_exchange_status(TimeStamp time, TradingStatus status); // cold diff --git a/src/lib/md/PlatformLoader.cpp b/src/lib/md/PlatformLoader.cpp index 4188210f5..0de76bdcf 100644 --- a/src/lib/md/PlatformLoader.cpp +++ b/src/lib/md/PlatformLoader.cpp @@ -90,7 +90,7 @@ void add_symbols_to_result( size_t get_system_md_symbols(api::Venue exchange, SecurityType type, std::set &result) { - bts_assert(config::system_symbols::Symbols::descriptor()->field_count() == 3); + bts_assert(config::system_symbols::Symbols::descriptor()->field_count() == 4); { auto const &e = config::get().glbx(); if (exchange == api::VENUE_UNSET || exchange == api::VENUE_GLBX) { @@ -146,34 +146,47 @@ size_t get_system_md_symbols(api::Venue exchange, SecurityType type, { auto const &e = config::get().sim(); - if (exchange == api::VENUE_UNSET || exchange == api::VENUE_SIM) { - if (type == api::SECURITYTYPE_FUTURE || - type == api::SECURITYTYPE_UNSET) { - add_symbols_to_result(e.future(), api::VENUE_SIM, - api::SECURITYTYPE_FUTURE, &result); - } - if (type == api::SECURITYTYPE_OPTION || - type == api::SECURITYTYPE_UNSET) { - add_symbols_to_result(e.option(), api::VENUE_SIM, - api::SECURITYTYPE_OPTION, &result); - } - if (type == api::SECURITYTYPE_SPREAD || - type == api::SECURITYTYPE_UNSET) { - add_symbols_to_result(e.spread(), api::VENUE_SIM, - api::SECURITYTYPE_SPREAD, &result); - } + if (exchange == api::VENUE_UNSET || exchange == api::VENUE_SIM) + { + if (type == api::SECURITYTYPE_FUTURE || type == api::SECURITYTYPE_UNSET) + add_symbols_to_result(e.future(), api::VENUE_SIM, api::SECURITYTYPE_FUTURE, &result); + if (type == api::SECURITYTYPE_OPTION || type == api::SECURITYTYPE_UNSET) + add_symbols_to_result(e.option(), api::VENUE_SIM, api::SECURITYTYPE_OPTION, &result); + if (type == api::SECURITYTYPE_SPREAD || type == api::SECURITYTYPE_UNSET) + add_symbols_to_result(e.spread(), api::VENUE_SIM, api::SECURITYTYPE_SPREAD, &result); } - if (exchange == api::VENUE_BTS || exchange == api::VENUE_UNSET) { + if (exchange == api::VENUE_BTS || exchange == api::VENUE_UNSET) + { std::set all_symbols; all_symbols.insert(e.future().begin(), e.future().end()); all_symbols.insert(e.option().begin(), e.option().end()); all_symbols.insert(e.spread().begin(), e.spread().end()); - for (const auto &symbol : all_symbols) { - result.insert(UserSymbol(symbol, api::VENUE_BTS, - api::SECURITYTYPE_SYNTHETIC)); - } + for (const auto &symbol : all_symbols) + result.insert(UserSymbol(symbol, api::VENUE_BTS, api::SECURITYTYPE_SYNTHETIC)); } } + + { + auto const &e = config::get().simice(); + if (exchange == api::VENUE_UNSET || exchange == api::VENUE_SIMICE) + { + if (type == api::SECURITYTYPE_FUTURE || type == api::SECURITYTYPE_UNSET) + add_symbols_to_result(e.future(), api::VENUE_SIMICE, api::SECURITYTYPE_FUTURE, &result); + if (type == api::SECURITYTYPE_OPTION || type == api::SECURITYTYPE_UNSET) + add_symbols_to_result(e.option(), api::VENUE_SIMICE, api::SECURITYTYPE_OPTION, &result); + if (type == api::SECURITYTYPE_SPREAD || type == api::SECURITYTYPE_UNSET) + add_symbols_to_result(e.spread(), api::VENUE_SIMICE, api::SECURITYTYPE_SPREAD, &result); + } + if (exchange == api::VENUE_BTS || exchange == api::VENUE_UNSET) + { + std::set all_symbols; + all_symbols.insert(e.future().begin(), e.future().end()); + all_symbols.insert(e.option().begin(), e.option().end()); + all_symbols.insert(e.spread().begin(), e.spread().end()); + for (const auto &symbol : all_symbols) + result.insert(UserSymbol(symbol, api::VENUE_BTS, api::SECURITYTYPE_SYNTHETIC)); + } + } return result.size(); } diff --git a/src/lib/md/Shm/FixedSecurity.cpp b/src/lib/md/Shm/FixedSecurity.cpp index fd5f7bfec..c3b5ce25c 100644 --- a/src/lib/md/Shm/FixedSecurity.cpp +++ b/src/lib/md/Shm/FixedSecurity.cpp @@ -31,8 +31,9 @@ FixedSecurity::FixedSecurity() : cme_main_fraction(0), cme_sub_fraction(0), cme_price_display_format(0), - ice_deal_display_factor(0), - ice_settle_display_factor(0), + ice_price_denominator(0), + ice_deal_denominator(0), + ice_settle_denominator(0), _symbol(), _cfi_code(), _security_desc(), @@ -99,7 +100,10 @@ void write_leg_descriptor(std::ostringstream& result, char separator, const api: std::string security_descriptor(const api::Security& security) { const char separator = ' '; std::ostringstream result; - result << security.symbol() << separator << Venue_Name(security.exchange()); + if(security.exchange() == Venue::VENUE_ICE || security.exchange() == Venue::VENUE_SIMICE) + result << security.ice().security_desc() << separator << Venue_Name(security.exchange()); + else + result << security.symbol() << separator << Venue_Name(security.exchange()); switch(security.Contract_case()) { case Security::ContractCase::kOption: { result << separator; @@ -181,8 +185,9 @@ void convert_security(const FixedSecurity& source, Security* presult) { case Venue::VENUE_ICE: dest.mutable_ice()->set_exchange_id(source.numeric_id); dest.mutable_ice()->set_security_desc(source.security_desc()); - dest.mutable_ice()->set_deal_display_factor(source.ice_deal_display_factor); - dest.mutable_ice()->set_settle_display_factor(source.ice_settle_display_factor); + dest.mutable_ice()->set_price_denominator(source.ice_price_denominator); + dest.mutable_ice()->set_deal_denominator(source.ice_deal_denominator); + dest.mutable_ice()->set_settle_denominator(source.ice_settle_denominator); dest.mutable_ice()->set_market_description(source.ice_market_description()); dest.mutable_ice()->set_cleared_alias(source.ice_cleared_alias()); @@ -312,8 +317,9 @@ void convert_security(const Security& src, FixedSecurity& dest, bool set_securit case Security::ExchangeDataCase::kIce: dest.numeric_id = src.ice().exchange_id(); dest.security_desc(src.ice().security_desc()); - dest.ice_deal_display_factor = src.ice().deal_display_factor(); - dest.ice_settle_display_factor = src.ice().settle_display_factor(); + dest.ice_price_denominator = src.ice().price_denominator(); + dest.ice_deal_denominator = src.ice().deal_denominator(); + dest.ice_settle_denominator = src.ice().settle_denominator(); dest.ice_market_description(src.ice().market_description()); dest.ice_cleared_alias(src.ice().cleared_alias()); diff --git a/src/lib/md/Shm/FixedSecurity.h b/src/lib/md/Shm/FixedSecurity.h index 853db1308..f0828d7e9 100644 --- a/src/lib/md/Shm/FixedSecurity.h +++ b/src/lib/md/Shm/FixedSecurity.h @@ -59,8 +59,9 @@ public: uint8_t cme_main_fraction; uint8_t cme_sub_fraction; uint8_t cme_price_display_format; - int8_t ice_deal_display_factor; - int8_t ice_settle_display_factor; + int8_t ice_price_denominator; + int8_t ice_deal_denominator; + int8_t ice_settle_denominator; STRING_PROPERTY(ice_market_description); STRING_PROPERTY(ice_cleared_alias); diff --git a/src/lib/md/Shm/MDStructure.h b/src/lib/md/Shm/MDStructure.h index 3553b71d8..2bad7f6ee 100644 --- a/src/lib/md/Shm/MDStructure.h +++ b/src/lib/md/Shm/MDStructure.h @@ -26,7 +26,7 @@ namespace md { using utils::TimeStamp; -const uint32_t shm_version = 102; +const uint32_t shm_version = 107; struct ALIGNED_(CACHE_LINE) HotHeader { size_t count; @@ -213,9 +213,10 @@ struct ColdEntry { exchange_status(co2::TRADINGSTATUS_UNKNOWN), last_trade(), total_volume(0), - last_trade_time(), + last_trade_time(), misc(), - deleted(false) + from_spread(0), + deleted(0) { this->security.size = security_size; } @@ -227,12 +228,13 @@ struct ColdEntry { // Last Trade AtomicPriceSize last_trade; - size_t total_volume; + size_t total_volume; datetime::LLDateTime last_trade_time; - + MiscData misc; - bool deleted; + int from_spread; + int deleted; FixedSecurity security; }; diff --git a/src/lib/md/ice/ICEUtils.cpp b/src/lib/md/ice/ICEUtils.cpp index 1533964a0..98d7a6600 100644 --- a/src/lib/md/ice/ICEUtils.cpp +++ b/src/lib/md/ice/ICEUtils.cpp @@ -27,6 +27,8 @@ std::string ICEChannel::to_string() const std::string mt = name; for(const auto& x : market_types) mt += " " + std::to_string(x); + for(const auto& x : uds_market_types) + mt += " " + std::to_string(x); return mt; } @@ -45,6 +47,9 @@ const vector parse_ice_channels(const vector& symbols) chan.symbols.insert(symbols.begin(), symbols.end()); for(const auto& xx : x.market_type()) chan.market_types.push_back(xx); + for(const auto& xx : x.uds_market_type()) + chan.uds_market_types.push_back(xx); + result.push_back(chan); } return result; diff --git a/src/lib/md/ice/ICEUtils.h b/src/lib/md/ice/ICEUtils.h index 5e0df291e..7ce5f78b9 100644 --- a/src/lib/md/ice/ICEUtils.h +++ b/src/lib/md/ice/ICEUtils.h @@ -13,6 +13,7 @@ struct ICEChannel { std::string name; std::vector market_types; + std::vector uds_market_types; std::set symbols; std::string to_string() const; }; diff --git a/src/lib/md/protos/co2.proto b/src/lib/md/protos/co2.proto index 23a27ff2a..c44566db6 100644 --- a/src/lib/md/protos/co2.proto +++ b/src/lib/md/protos/co2.proto @@ -152,6 +152,7 @@ message TradeMessage { uint32 total_volume = 7; bool gap = 8; AggressorSide aggressor_side = 9; + bool from_spread = 10; } message QuoteRequestMessage { enum Side { diff --git a/src/lib/md/relay/QueryServer.cpp b/src/lib/md/relay/QueryServer.cpp index f0ede96a5..e404ff2b1 100644 --- a/src/lib/md/relay/QueryServer.cpp +++ b/src/lib/md/relay/QueryServer.cpp @@ -78,7 +78,8 @@ struct MDConsumerGroup { symbol(symbol), platform(platform_for_exchange(symbol.exchange())), channel(consumer.get_channel(symbol)), - handles() + handles(), + mic() { set_consumer(consumer); @@ -118,9 +119,38 @@ struct MDConsumerGroup { bts_assert_d(handle.is_initialized()); bts_assert(handle.get_usersymbol() == symbol); DatedHandle h = { TimeStamp::now(), handle }; - handles->push_back(h); + handles->push_back(h); + if(this->mic == "") + set_mic(h); } - + + void set_mic(const DatedHandle& h) + { + if(channel.exchange() == api::VENUE_ICE || channel.exchange() == api::VENUE_SIMICE) + { + this->mic = h.handle.get_normalized_security().ice().mic_code(); + } + else if(channel.exchange() == api::VENUE_GLBX || channel.exchange() == api::VENUE_SIM) + { + const auto& cme_symb_mics = config::get().cme_mic(); + for(auto mic=cme_symb_mics.begin();mic!=cme_symb_mics.end();mic++) + { + const auto& micsym = mic->first; + if(micsym == symbol.symbol()) + { + this->mic = api::CMEMics_Name(mic->second); + break; + } + } + } + else if(channel.exchange() == api::VENUE_BTS || channel.exchange() == api::VENUE_UNSET) + { + this->mic = "BTS"; + } + else + bts_assert_msg(false, "unknown venue in QueryServer"); + } + UserSymbol symbol; Platform platform; MDConsumerChannel channel; @@ -128,7 +158,7 @@ struct MDConsumerGroup { // Can add to handles without invalidating readers (no lock needed) // When we recover we swap a new vector into this unique_ptr and delayed-delete shared_ptr> handles; - + std::string mic; }; void convert(PriceSize ps, PriceSizeMessage* pps) { @@ -219,12 +249,18 @@ public: } // the hot part - void fill(const SecurityChannel& chan, MDConsumerGroup& group, ChannelReply* reply, bool entitled) const { + void fill(const SecurityChannel& chan, MDConsumerGroup& group, ChannelReply* reply, + const Entitlements* entitlements, bool bypass_entitlement) const { bts_assert(reply); + reply->mutable_channel()->CopyFrom(chan); reply->set_platform(group.platform); reply->mutable_channel()->set_time(TimeStamp::now().to_uint64()); - reply->mutable_channel()->set_entitled(entitled); + + bool entitled = bypass_entitlement || chan.exchange() == api::VENUE_BTS; + if(entitlements && !entitled) + entitled = entitlements->count(group.mic); + reply->mutable_channel()->set_entitled(entitled); auto flags = (MDRequest::Flags)chan.flags(); // Get a copy of handles. If recovery happens during this call @@ -242,8 +278,10 @@ public: ChannelUpdate* pupd = &temp_update; pupd->set_key(instrument.handle.key()); // If the security was just discovered try to ensure all data is sent - if (instrument.discovered > TimeStamp(chan.time())) { - pupd->mutable_security()->CopyFrom(handle.get_normalized_security()); + if (instrument.discovered > TimeStamp(chan.time())) + { + auto security = handle.get_normalized_security(); + pupd->mutable_security()->CopyFrom(security); snapshot = true; } //if we are not entitled AND this is the first request since we lost our entitlement, zero out books @@ -323,12 +361,13 @@ public: TimeStamp time(snapshot ? 0 : chan.time()); PriceSize price; datetime::LLDateTime trade_time; - size_t volume; - if (handle.get_last_trade(time, price, trade_time, &volume)) { + size_t volume; int from_spread; + if (handle.get_last_trade(time, price, trade_time, &volume, &from_spread)) { auto* trade = pupd->mutable_last_trade(); trade->set_key(handle.key()); trade->set_time(trade_time); trade->set_total_volume(volume); + trade->set_from_spread(from_spread); set_price(price, trade); } } @@ -373,10 +412,7 @@ public: auto groupiter = _groups.find(UserSymbol(ch.symbol(), ch.exchange(), ch.type())); if (groupiter != _groups.end()) { auto& group = groupiter->second; - bool is_entitled = bypass_entitlement || - ch.exchange() == api::VENUE_BTS || - (entitlements && entitlements->count(ch.symbol())); - fill(ch, *group, reply->add_channels(), is_entitled); + fill(ch, *group, reply->add_channels(), entitlements, bypass_entitlement); } } } @@ -538,27 +574,21 @@ QueryServerPtr run_query_server(const bts::api::ServiceContext& ctx, MDClient& c container->admin_client.subscribe_md_entitlement_data().subscribe([=](const api::admin::Md_entitlement_dataUpdate& update) { const auto& all_updates = update.all(); utils::Dispatcher::background().post([all_updates, container]() - { - const auto& cme_symb_mics = config::get().cme_mic(); + { auto entitlements = std::make_shared>(); - for (const auto& row : all_updates) { - if (row && row->enabled()) { - for(auto mic=cme_symb_mics.begin();mic!=cme_symb_mics.end();mic++){ - if(api::CMEMics_Name(mic->second) == row->mic()) { - const auto& symbol = mic->first; - auto iter = entitlements->find(row->user_id()); - if (iter == entitlements->end()) { - Entitlements ents; - ents.insert(symbol); - ents.insert("SIM:" + symbol); - entitlements->insert(iter, std::make_pair(row->user_id(), std::move(ents))); - } - else { - iter->second.insert(symbol); - iter->second.insert("SIM:" + symbol); - } - } + for (const auto& row : all_updates) + { + if (row && row->enabled()) + { + auto iter = entitlements->find(row->user_id()); + if (iter == entitlements->end()) { + Entitlements ents; + ents.insert(row->mic()); + entitlements->insert(iter, std::make_pair(row->user_id(), std::move(ents))); } + else { + iter->second.insert(row->mic()); + } } } container->relay.update_entitlements(entitlements); diff --git a/src/lib/md/relay/RelayClient.cpp b/src/lib/md/relay/RelayClient.cpp index b7d4da066..0cc92b7a4 100644 --- a/src/lib/md/relay/RelayClient.cpp +++ b/src/lib/md/relay/RelayClient.cpp @@ -86,7 +86,7 @@ class RelayProducer : public IRelayWriter { handle.update_fixing_price(updated, misc.fixing_price()); } if (update.has_last_trade()) { - handle.set_last_trade(updated, trade_pricesize(update.last_trade()), update.last_trade().time(), update.last_trade().total_volume()); + handle.set_last_trade(updated, trade_pricesize(update.last_trade()), update.last_trade().time(), update.last_trade().total_volume(), update.last_trade().from_spread()); } } diff --git a/src/lib/md/test/Co2ManagedTestHelper.cpp b/src/lib/md/test/Co2ManagedTestHelper.cpp index f38ca3325..a59e9a618 100644 --- a/src/lib/md/test/Co2ManagedTestHelper.cpp +++ b/src/lib/md/test/Co2ManagedTestHelper.cpp @@ -87,7 +87,8 @@ extern "C" BTS_EXPORT void co2_get_sample_cold(size_t& size, ColdEntry& entry) { entry.misc.session_high_bid = 4.4; entry.misc.session_low_offer = 5.5; entry.misc.fixing_price = 6.6; - entry.deleted = true; + entry.deleted = 1; + entry.from_spread = 0; } extern "C" BTS_EXPORT void co2_get_sample_cold_header(ColdHeader& header) { diff --git a/src/lib/orderman/actors/ExecutionGateway.h b/src/lib/orderman/actors/ExecutionGateway.h deleted file mode 100644 index 2e559ee47..000000000 --- a/src/lib/orderman/actors/ExecutionGateway.h +++ /dev/null @@ -1,53 +0,0 @@ -#pragma once - -#include "lib/config/protos/jetfix.pb.h" -#include "lib/jetfix/codec/MessageDefinitions.h" -#include "lib/jetfix/common/OutFIXPtr.h" -#include "lib/mercury/events/Update.h" -#include "lib/orderman/orderman_utils/orderman_common.h" - -#include - -namespace bts { -namespace md { class SecurityOracle;} -namespace trace {struct CorID;class PerfLog;} -namespace orderman { -class NewSecuritiesEvent; -class ExecutionReport; - -using OnExeReport = - std::function; -using OnGwStatusChange = std::function; -using OnCancelReject = std::function; - -// fix execution gateway for a single FIX session -class ExecutionGateway { - public: - ExecutionGateway(config::JetFIX::Connection const &, trace::PerfLog &, - md::SecurityOracle); - ~ExecutionGateway(); - - void start(); - void dispatch_incoming_fix_messages(); - void on_securities(const mercury::Update &); - void send_fix(jetfix::OutFIXPtr const &fix, trace::CorID trace); - - OnExeReport& on_exe_report(); - OnGwStatusChange& on_gw_status_change(); - OnCancelReject &on_cancel_reject(); - - private: - enum class State { WAITING_FOR_SECURITIES, READY }; - struct PImpl; - std::unique_ptr me; - - // fix gateway events - void on_exe_report(jetfix::fix::ExecutionReportMsg const &); - void on_cancel_reject(jetfix::fix::CancelRejectMsg const &); - void on_connection_status_change(bool connected); - void on_business_reject(jetfix::fix::BusinessMessageRejectMsg const &); - void on_session_reject(jetfix::fix::RejectMsg const &); -}; - -} // namespace orderman -} // namespace bts diff --git a/src/lib/orderman/actors/FillStore.cpp b/src/lib/orderman/actors/FillStore.cpp index ef5ebfb60..d977ec17d 100644 --- a/src/lib/orderman/actors/FillStore.cpp +++ b/src/lib/orderman/actors/FillStore.cpp @@ -93,12 +93,12 @@ void FillStore::on_execution_report( if (u.events) { for (auto const &ev : *u.events) { auto &er = *ev.data; + auto fk = fmt::format("{}:{}", er.order_key.value(), + to_str(er.exchange_fill_id)); + auto is_known = me->known_fills.count(fk); if (er.exec_type == api::orderman::EXEC_TYPE_PARTIAL_FILL || er.exec_type == api::orderman::EXEC_TYPE_FILL) { - - auto fk = fmt::format("{}:{}", er.order_key.value(), - to_str(er.exchange_fill_id)); - auto is_known = me->known_fills.count(fk); + api::orderman::Fill fill; if (!is_known) { fill.set_order_key(er.order_key.value()); @@ -114,6 +114,9 @@ void FillStore::on_execution_report( fill.set_avg_price(er.avg_price.value()); fill.set_side(er.side); fill.set_last_shares(er.last_shares.value()); + fill.set_fill_status(api::orderman::Fill::FILL_STATUS_ACTIVE); + if(er.exec_type == api::orderman::EXEC_TYPE_TRADE_CORRECT) + fill.set_fill_status(api::orderman::Fill::FILL_STATUS_CORRECTED); fill.set_fill_key(fk); auto exchange_ts = utils::pb::to_timestamp( @@ -137,10 +140,12 @@ void FillStore::on_execution_report( "Discarding known execution report for fill {}", fk); } // if partial_fill or fill - } // for loop + } } // if u.events } + + void FillStore::on_order_definition_update( mercury::Update const &u) { if (u.events) diff --git a/src/lib/orderman/actors/ExecutionGateway.cpp b/src/lib/orderman/actors/GLBXExecutionGateway.cpp similarity index 90% rename from src/lib/orderman/actors/ExecutionGateway.cpp rename to src/lib/orderman/actors/GLBXExecutionGateway.cpp index 84805ba4d..8adc93eee 100644 --- a/src/lib/orderman/actors/ExecutionGateway.cpp +++ b/src/lib/orderman/actors/GLBXExecutionGateway.cpp @@ -1,4 +1,4 @@ -#include "ExecutionGateway.h" +#include "GLBXExecutionGateway.h" #include "lib/jetfix/codec/MessageDefinitions.h" #include "lib/jetfix/common/OutFIX.h" #include "lib/jetfix/gateway/JetFIXGateway.h" @@ -24,7 +24,7 @@ using JFGw = JetFIXGateway; -struct ExecutionGateway::PImpl { +struct GLBXExecutionGateway::PImpl { PImpl(config::JetFIX::Connection const &cfg, trace::PerfLog &l, md::SecurityOracle o) : gw(cfg, l), exe_report_pool(), state(State::WAITING_FOR_SECURITIES), @@ -46,14 +46,16 @@ struct ExecutionGateway::PImpl { md::SecurityOracle sec_oracle; }; -ExecutionGateway::ExecutionGateway(config::JetFIX::Connection const &cfg, +GLBXExecutionGateway::GLBXExecutionGateway(config::JetFIX::Connection const &cfg, trace::PerfLog &log, md::SecurityOracle o) : me(new PImpl(cfg, log, o)) { me->state = State::WAITING_FOR_SECURITIES; } -ExecutionGateway::~ExecutionGateway() {} +GLBXExecutionGateway::~GLBXExecutionGateway() {} -void ExecutionGateway::on_securities( +void GLBXExecutionGateway::on_admin_data(const mercury::Update &) {} + +void GLBXExecutionGateway::on_securities( const mercury::Update &update) { if (update.events) { @@ -96,19 +98,19 @@ void ExecutionGateway::on_securities( } } -void ExecutionGateway::start() { me->gw.start(); } +void GLBXExecutionGateway::start() { me->gw.start(); } -OnExeReport &ExecutionGateway::on_exe_report() { return me->on_exe_report; } +OnExeReport &GLBXExecutionGateway::on_exe_report() { return me->on_exe_report; } -OnGwStatusChange &ExecutionGateway::on_gw_status_change() { +OnGwStatusChange &GLBXExecutionGateway::on_gw_status_change() { return me->on_gw_status_change; } -OnCancelReject &ExecutionGateway::on_cancel_reject() { +OnCancelReject &GLBXExecutionGateway::on_cancel_reject() { return me->on_cancel_reject; } -void ExecutionGateway::on_exe_report( +void GLBXExecutionGateway::on_exe_report( const jetfix::fix::ExecutionReportMsg &msg) { OrderKey ok; SecurityKey sk; @@ -116,7 +118,7 @@ void ExecutionGateway::on_exe_report( auto scoped_log = trace::log_scope(me->perf_log, msg.trace(), - "ExecutionGateway::on_exe_report", ok.value()); + "GLBXExecutionGateway::on_exe_report", ok.value()); LOG(Latency, Info, "Started handling of execution report for order {}", ok.value()); @@ -266,11 +268,12 @@ void ExecutionGateway::on_exe_report( data->cfg = &me->cfg; data->msg = text_msg; + data->exchange = api::Venue::VENUE_GLBX; me->on_exe_report(*data, msg.trace()); } } -void ExecutionGateway::on_cancel_reject( +void GLBXExecutionGateway::on_cancel_reject( const jetfix::fix::CancelRejectMsg &msg) { OrderKey ok; SecurityKey sk; @@ -285,7 +288,7 @@ void ExecutionGateway::on_cancel_reject( me->on_cancel_reject(ok, reject_msg); } -void ExecutionGateway::on_connection_status_change(bool is_connected) { +void GLBXExecutionGateway::on_connection_status_change(bool is_connected) { LOG(OrderMan, Info, "Connection state of {} changed to {}", me->gw_name.value(), is_connected ? std::string("connected") : std::string("disconnected")); @@ -293,7 +296,7 @@ void ExecutionGateway::on_connection_status_change(bool is_connected) { me->on_gw_status_change(is_connected); } -void ExecutionGateway::on_business_reject( +void GLBXExecutionGateway::on_business_reject( jetfix::fix::BusinessMessageRejectMsg const &m) { LOG(OrderMan, Error, "Unexpected business reject in {} gateway . Reason: " @@ -302,7 +305,7 @@ void ExecutionGateway::on_business_reject( m.has() ? m.get() : std::string("Unknown")); } -void ExecutionGateway::on_session_reject(jetfix::fix::RejectMsg const &m) { +void GLBXExecutionGateway::on_session_reject(jetfix::fix::RejectMsg const &m) { LOG(OrderMan, Error, "Unexpected reject message in {} gateway . Reason: {}. " "This should be investigated", @@ -310,14 +313,14 @@ void ExecutionGateway::on_session_reject(jetfix::fix::RejectMsg const &m) { m.has() ? m.get() : std::string("Unknown")); } -void ExecutionGateway::dispatch_incoming_fix_messages() { +void GLBXExecutionGateway::dispatch_incoming_fix_messages() { me->gw.dispatch_events(); } -void ExecutionGateway::send_fix(jetfix::OutFIXPtr const &fix, +void GLBXExecutionGateway::send_fix(jetfix::OutFIXPtr const &fix, trace::CorID trace) { auto scoped_log = - trace::log_scope(me->perf_log, trace, "ExecutionGateway::send_fix"); + trace::log_scope(me->perf_log, trace, "GLBXExecutionGateway::send_fix"); me->gw.send(fix, trace); me->gw.dispatch_events(); } diff --git a/src/lib/orderman/actors/GLBXExecutionGateway.h b/src/lib/orderman/actors/GLBXExecutionGateway.h new file mode 100644 index 000000000..3c0edcc83 --- /dev/null +++ b/src/lib/orderman/actors/GLBXExecutionGateway.h @@ -0,0 +1,41 @@ +#pragma once + +#include "IExecutionGateway.h" + +namespace bts { +namespace orderman { + + +// fix execution gateway for a single FIX session +class GLBXExecutionGateway : public IExecutionGateway { + public: + GLBXExecutionGateway(config::JetFIX::Connection const &, trace::PerfLog &, + md::SecurityOracle); + virtual ~GLBXExecutionGateway(); + + void start() override; + void dispatch_incoming_fix_messages() override; + void on_securities(const mercury::Update &) override; + void on_admin_data(const mercury::Update &) override; + void send_fix(jetfix::OutFIXPtr const &fix, trace::CorID trace) override; + + OnExeReport& on_exe_report() override; + OnGwStatusChange& on_gw_status_change() override; + OnCancelReject &on_cancel_reject() override; + + private: + BTS_NO_COPY(GLBXExecutionGateway); + enum class State { WAITING_FOR_SECURITIES, READY }; + struct PImpl; + std::unique_ptr me; + + // fix gateway events + void on_exe_report(jetfix::fix::ExecutionReportMsg const &); + void on_cancel_reject(jetfix::fix::CancelRejectMsg const &); + void on_connection_status_change(bool connected); + void on_business_reject(jetfix::fix::BusinessMessageRejectMsg const &); + void on_session_reject(jetfix::fix::RejectMsg const &); +}; + +} // namespace orderman +} // namespace bts diff --git a/src/lib/orderman/actors/ICEExecutionGateway.cpp b/src/lib/orderman/actors/ICEExecutionGateway.cpp new file mode 100644 index 000000000..c97d729c2 --- /dev/null +++ b/src/lib/orderman/actors/ICEExecutionGateway.cpp @@ -0,0 +1,490 @@ +#include "ICEExecutionGateway.h" +#include "lib/jetfix/codec/MessageDefinitions.h" +#include "lib/jetfix/codec/i32_toa.h" +#include "lib/jetfix/common/OutFIX.h" +#include "lib/jetfix/gateway/JetFIXGateway.h" +#include "lib/logging/logging.h" +#include "lib/orderman/events/CancelRejectEvent.h" +#include "lib/orderman/events/GatewayStatusChangeEvent.h" +#include "lib/orderman/events/NewExecutionReportEvent.h" +#include "lib/orderman/events/NewSecuritiesEvent.h" +#include "lib/orderman/orderman_utils/fix_order_codec.h" +#include "lib/api/services/AdminService.pb.h" +#include "lib/orderman/events/NewAdminDataEvent.h" +#include "lib/tracing/bts_trace.h" +#include "lib/utils/TimeStamp.h" +#include "lib/utils/helper_macros.h" +#include "lib/utils/Dispatcher.h" +#include "lib/utils/bts_chrono.h" + +#include +#include +#include + +namespace bts { +namespace orderman { + +using namespace jetfix; + +using JFGw = JetFIXGateway; + + + +struct ICEExecutionGateway::PImpl +{ + PImpl(const config::JetFIX::Connection &cfg, trace::PerfLog &l, + md::SecurityOracle o) + : gw(cfg, l), exe_report_pool(), state(State::WAITING_FOR_SECURITIES), + securities(), securities_by_exchange_id(), gw_name(cfg.name()), + cfg(cfg), perf_log(l), admin_data(), nusers(-1), connection_time(), exec_id_to_memo(), + sec_oracle(o) + {} + + JFGw gw; + boost::object_pool exe_report_pool; + State state; + std::unordered_map securities; + std::unordered_map securities_by_exchange_id; + GatewayName gw_name; + config::JetFIX::Connection cfg; + OnExeReport on_exe_report; + OnGwStatusChange on_gw_status_change; + OnCancelReject on_cancel_reject; + trace::PerfLog &perf_log; + api::admin::AdminData admin_data; + std::atomic nusers; + TimeStamp connection_time; + std::unordered_map exec_id_to_memo; //for the bullshit adjusts + md::SecurityOracle sec_oracle; +}; + +ICEExecutionGateway::ICEExecutionGateway(const config::JetFIX::Connection &cfg, trace::PerfLog &log, md::SecurityOracle o) + : IExecutionGateway(), me(new PImpl(cfg, log, o)) +{ + me->state = State::WAITING_FOR_ADMIN; +} + +ICEExecutionGateway::~ICEExecutionGateway() {} + +void ICEExecutionGateway::on_admin_data(const mercury::Update& update) +{ + if (update.events) + { + for (auto const &ev : *(update.events)) + me->admin_data.CopyFrom(*ev.admin_data); + + if (me->state == State::WAITING_FOR_ADMIN) + { + LOG(OrderMan, Warn, "Received admin data. Waiting for securities {}", me->gw_name.value()); + me->state = State::WAITING_FOR_SECURITIES; + } + } +} + +void ICEExecutionGateway::on_securities(const mercury::Update &update) +{ + if (update.events) + { + for (auto const &ev : *(update.events)) + { + auto sec_ptr = ev.security; + me->securities[sec_ptr->id()] = sec_ptr; + if(sec_ptr->has_ice()) + me->securities_by_exchange_id[sec_ptr->ice().exchange_id()] = sec_ptr; + } + + if (me->state == State::WAITING_FOR_SECURITIES) + { + LOG(OrderMan, Warn, "Received first set of {} securities. Beginning wait 2s for trader login {}", + update.events->size(), me->gw_name.value()); + // got our security synopsis, ready to start the GW + me->state = State::READY_TO_START_GW; + + // subscribe to gateway events + me->gw.on() = [this](auto const &msg) + { + this->on_exe_report(msg); + }; + me->gw.on() = [this](auto const &msg) + { + this->on_cancel_reject(msg); + }; + me->gw.on_ready() = [this]() + { + this->on_connection_status_change(true); + }; + me->gw.on_disconnect() = [this]() + { + this->on_connection_status_change(false); + }; + me->gw.on() = [this](auto const &m) + { + this->on_business_reject(m); + }; + me->gw.on() = [this](auto const &m) + { + this->on_session_reject(m); + }; + } + } +} + +void ICEExecutionGateway::start() +{ + me->gw.start(); + bts_assert_msg(me->state == State::READY_TO_START_GW, "ICEExecutionGateway statemachine failure"); +} + +void ICEExecutionGateway::on_connection_status_change(bool is_connected) +{ + LOG(OrderMan, Info, "Connection state of {} changed to {}", + me->gw_name.value(), is_connected ? std::string("connected") : std::string("disconnected")); + + if(is_connected && me->state == State::READY_TO_START_GW) + { + me->state = State::TRADER_LOGIN_TIMEOUT; + me->connection_time = Timer::now(); + } + + if(me->state == State::READY) + { + me->on_gw_status_change(is_connected); + if(!is_connected) //if we disconnect, set state so we log in again + me->state = State::READY_TO_START_GW; + } +} + + +void ICEExecutionGateway::on_trader_login_timeout_expired() +{ + if (me->state == State::TRADER_LOGIN_TIMEOUT) + { + LOG(OrderMan, Warn, "Done waiting for login timeout, logging on traders {}", me->gw_name.value()); + + // got our security synopsis, ready to start the GW + me->state = State::LOGIN_TIMEOUT_COMPLETE; + me->gw.on() = [&](const fix::ICETraderLoginResponseMsg& lmsg) + { + std::string u = lmsg.get(); + std::string m; + if(lmsg.has()) + m = lmsg.get(); + LOG(OrderMan, Error, "Succesffully logging trader {} into ICE {} {}", u, m, me->gw_name.value()); + if(--me->nusers == 0) + this->on_login(); + }; + + me->gw.on() = [&](const fix::ICETraderLogoutResponseMsg& lmsg) + { + std::string u = lmsg.get(); + std::string m; + if(lmsg.has()) + m = lmsg.get(); + + LOG(OrderMan, Error, "Error logging trader {} into ICE {} {}", u, m, me->gw_name.value()); + if(--me->nusers == 0) + this->on_login(); + }; + + std::set auth_traders; + for(const auto& u : me->admin_data.users()) + { + if(u.has_user_data() && u.user_data().has_exchange_user_data() && u.user_data().exchange_user_data().has_ice_data()) + { + const auto& ice = u.user_data().exchange_user_data().ice_data(); + if(auth_traders.find(ice.authorized_trader()) == auth_traders.end()) + { + if(me->nusers == -1) + me->nusers = 0; + me->nusers++; + + auto result = std::make_shared(me->cfg); + auto &stream = result->fix.back(); + auto &buffer = stream.new_msg(MessageTypes::ICETraderLogin); + buffer.add(ice.company_id()); + buffer.add(ice.authorized_trader()); + buffer.add(ice.password()); + + if(ice.mifid_direct_electronic_access() >= 0) + buffer.add(ice.mifid_direct_electronic_access()); + if(ice.mifid_trading_capacity() >= 0) + buffer.add(ice.mifid_trading_capacity()); + if(ice.mifid_liquidity_provision() >= 0) + buffer.add(ice.mifid_liquidity_provision()); + if(ice.mifid_commodity_deriv_indicator() >= 0) + buffer.add(ice.mifid_commodity_deriv_indicator()); + if(ice.mifid_investment_decision() > 0) + buffer.add(ice.mifid_investment_decision()); + if(ice.mifid_execution_decision() > 0) + buffer.add(ice.mifid_execution_decision()); + if(ice.mifid_client_id_code() > 0) + buffer.add(ice.mifid_client_id_code()); + if(ice.mifid_id() > 0) + buffer.add(ice.mifid_id()); + stream.close(); + + trace::CorID trace; + send_fix(result, trace); + auth_traders.insert(ice.authorized_trader()); + } + } + } + LOG(OrderMan, Warn, "{} traders identified as ICE traders {}", auth_traders.size(), me->gw_name.value()); + } +} + +void ICEExecutionGateway::on_login() +{ + if (me->state == State::LOGIN_TIMEOUT_COMPLETE) + { + LOG(OrderMan, Warn, "Successful login traders {}", me->gw_name.value()); + me->state = State::READY; + me->on_gw_status_change(true); + } +} + +OnExeReport &ICEExecutionGateway::on_exe_report() { return me->on_exe_report; } + +OnGwStatusChange &ICEExecutionGateway::on_gw_status_change() +{ + return me->on_gw_status_change; +} + +OnCancelReject &ICEExecutionGateway::on_cancel_reject() +{ + return me->on_cancel_reject; +} + +bool ICEExecutionGateway::process_memo(const jetfix::fix::ICEExecutionReportMsg &msg, OrderKey* ok, SecurityKey* sk) +{ + if(msg.has()) + { + decode_memo(msg.get(), ok, sk); + return true; + } + else + { + if (msg.get() == api::orderman::EXEC_TYPE_TRADE_CORRECT && msg.has()) + { + std::string ref_exec_id = msg.get(); + auto found = me->exec_id_to_memo.find(ref_exec_id); + if(found == me->exec_id_to_memo.end()) + { + auto new_price = Price(0); + if(msg.has()) + new_price = Price(msg.get()); + + LOG(OrderMan, Error, + "Received unsupported trade correction msg. This correction will not be forwarded. The trader should be alerted. " + "Partial original fill id: {}. New price: {}", ref_exec_id, new_price.value()); + return false; + } + else + { + decode_memo(found->second, ok, sk); + return true; + } + } + } + return false; +} + +void ICEExecutionGateway::on_exe_report(const jetfix::fix::ICEExecutionReportMsg &msg) +{ + OrderKey ok; + SecurityKey sk; + if(!process_memo(msg, &ok, &sk)) + { + std::string execid = ""; + if(msg.has()) + execid = msg.get(); + LOG(OrderMan, Error, + "Received execution report with no memo execid: {}. Will not be able to process this report. This is likely a bug that could lead " + "to lost fills. Please investigate.", execid); + return; + } + auto scoped_log = trace::log_scope(me->perf_log, msg.trace(), "ICEExecutionGateway::on_exe_report", ok.value()); + + LOG(Latency, Info, "Started handling of execution report for order {}", ok.value()); + auto &sec = me->securities[sk.value()]; + if (!sec) + { + sec = me->sec_oracle.get_security(SecurityKey(sk.value())); + } + if (!sec) + LOG(OrderMan, Error, + "Received execution report for an unknown security {}. Will not be able to process this report. This is likely a bug that could lead " + "to lost fills. Please investigate.", + sk.value()); + else + { + if(sec->has_ice()) + me->securities_by_exchange_id[sec->ice().exchange_id()] = sec; + + api::Security const *leg_security = nullptr; + if (msg.has() && msg.get() == 2) + { + auto it = me->securities_by_exchange_id.find(msg.get()); + if (it == me->securities_by_exchange_id.end()) + { + LOG(OrderMan, Error, + "Received execution report for an unknown spread leg" + "security {}. Will not be able to process this report. " + "This is likely a bug that could lead to " + "lost leg fills. Please investigate. Spread sec_key: {} leg " + "exchange id: {}", + sk.value(), msg.get()); + return; + } + leg_security = it->second; + } + std::string text_msg; + if (msg.has()) + text_msg = msg.get(); + if (msg.has()) + text_msg += " Rejected OrdRejReason: " + std::to_string(msg.get()); + + if (msg.has()) + text_msg += " Cancelled DeleteReason: " + std::to_string(msg.get()); + + // allocate from our pool + auto data = me->exe_report_pool.construct(); + + if(msg.has()) + data->memo = msg.get(); + data->exec_type = api::orderman::ExecType(msg.get()); + data->msg_seq_num = msg.get(); + + // copy data from fix msg to our structure + if (msg.has()) + str_to(msg.get(), &data->account_name); + if (msg.has()) + str_to(msg.get(), &data->operator_id); + if (msg.has()) + data->ice_company_id = msg.get(); + + data->order_key = ok; + data->security = sec; + data->leg_security = leg_security; + data->side = api::Side(msg.get()); + if (msg.has()) + data->time_in_force = api::orderman::TimeInForce(msg.get()); + else + data->time_in_force = api::orderman::TIME_IN_FORCE_UNDEFINED; + + if (msg.has()) + data->order_type = api::orderman::NativeOrderType(msg.get()); + else + data->order_type = api::orderman::NATIVE_ORDER_TYPE_UNDEFINED; + + data->order_status = api::orderman::NativeOrderStatus(msg.get()); + data->manual_order_indicator = api::orderman::MANUAL_ORDER_INDICATOR_NO; + + std::string oid_string = get_ice_order_id_string(msg); + str_to(oid_string, &data->exchange_order_id); + data->ice_cl_ord_id = 0; + if(msg.has()) + data->ice_cl_ord_id = msg.get(); + + if (msg.has()) + data->price = securities::decode_exchange_price(*sec, Price(msg.get())); + else + data->price = Price(os::nan()); + + if (msg.has()) + data->stop_price = securities::decode_exchange_price(*sec, Price(msg.get())); + else + data->stop_price = Price(os::nan()); + + if (msg.has()) + data->max_show = Quantity(msg.get()); + else + data->max_show = Quantity(0); + + if (msg.has()) + data->quantity = Quantity(msg.get()); + else + data->quantity = Quantity(0); + + data->filled_quantity = Quantity(msg.get()); + + data->last_shares = msg.has() + ? Quantity(msg.get()) + : Quantity(0); + auto last_px_sec = data->leg_security ? data->leg_security : sec; + data->last_price = msg.has() + ? securities::decode_exchange_price(*last_px_sec, Price(msg.get())) + : Price(0); + data->avg_price = msg.has() + ? securities::decode_exchange_price(*last_px_sec, Price(msg.get())) + : Price(0); + if (msg.has()) + { + auto execid = msg.get(); + str_to(execid, &data->exchange_fill_id); + if(msg.has()) + me->exec_id_to_memo[execid] = msg.get(); + } + + if (msg.has()) + { + std::string stamp = msg.get(); + stamp.resize(sizeof(mercury::TimeStampStr)); + str_to(stamp, &data->transact_time); + } + data->cfg = &me->cfg; + data->msg = text_msg; + data->exchange = api::Venue::VENUE_ICE; + me->on_exe_report(*data, msg.trace()); + } +} + +void ICEExecutionGateway::on_cancel_reject(const jetfix::fix::ICECancelRejectMsg &msg) +{ + OrderKey ok(msg.get()); + + std::string reject_msg; + if (msg.has()) + reject_msg = msg.get(); + else if (msg.has()) + reject_msg = fmt::format("Order cancel was rejected with error code {}", + msg.get()); + me->on_cancel_reject(ok, reject_msg); +} + + +void ICEExecutionGateway::on_business_reject(const jetfix::fix::BusinessMessageRejectMsg& m) +{ + LOG(OrderMan, Error, + "Unexpected business reject in {} gateway . Reason: {}. This should be investigated", + me->gw_name.value(), + m.has() ? m.get() : std::string("Unknown")); +} + +void ICEExecutionGateway::on_session_reject(const jetfix::fix::RejectMsg& m) +{ + LOG(OrderMan, Error, + "Unexpected reject message in {} gateway . Reason: {}. This should be investigated", + me->gw_name.value(), + m.has() ? m.get() : std::string("Unknown")); +} + +void ICEExecutionGateway::dispatch_incoming_fix_messages() +{ + me->gw.dispatch_events(); + if(me->state == State::TRADER_LOGIN_TIMEOUT && ms_diff(Timer::now(), me->connection_time) >= 2000) + { + on_trader_login_timeout_expired(); + } +} + +void ICEExecutionGateway::send_fix(const jetfix::OutFIXPtr& fix, trace::CorID trace) +{ + auto scoped_log = trace::log_scope(me->perf_log,trace,"ICEExecutionGateway::send_fix"); + me->gw.send(fix,trace); + me->gw.dispatch_events(); +} + +} // namespace orderman +} // namespace bts diff --git a/src/lib/orderman/actors/ICEExecutionGateway.h b/src/lib/orderman/actors/ICEExecutionGateway.h new file mode 100644 index 000000000..c885a90af --- /dev/null +++ b/src/lib/orderman/actors/ICEExecutionGateway.h @@ -0,0 +1,45 @@ +#pragma once + +#include "IExecutionGateway.h" + +namespace bts { +namespace orderman { + +// fix execution gateway for a single FIX session +class ICEExecutionGateway : public IExecutionGateway { + public: + ICEExecutionGateway(const config::JetFIX::Connection&, trace::PerfLog &, + md::SecurityOracle); + virtual ~ICEExecutionGateway(); + + void start() override; + void dispatch_incoming_fix_messages() override; + void on_securities(const mercury::Update &) override; + void on_admin_data(const mercury::Update &) override; + void send_fix(const jetfix::OutFIXPtr& fix, trace::CorID trace) override; + + OnExeReport& on_exe_report() override; + OnGwStatusChange& on_gw_status_change() override; + OnCancelReject &on_cancel_reject() override; + + private: + BTS_NO_COPY(ICEExecutionGateway); + enum class State { WAITING_FOR_ADMIN, WAITING_FOR_SECURITIES, READY_TO_START_GW, TRADER_LOGIN_TIMEOUT, LOGIN_TIMEOUT_COMPLETE, READY }; + struct PImpl; + std::unique_ptr me; + + void on_trader_login_timeout_expired(); + void on_login(); + + // fix gateway events + void on_exe_report(const jetfix::fix::ICEExecutionReportMsg&); + void on_cancel_reject(const jetfix::fix::ICECancelRejectMsg&); + void on_connection_status_change(bool connected); + void on_business_reject(const jetfix::fix::BusinessMessageRejectMsg&); + void on_session_reject(const jetfix::fix::RejectMsg&); + + bool process_memo(const jetfix::fix::ICEExecutionReportMsg &msg, OrderKey*, SecurityKey*); +}; + +} // namespace orderman +} // namespace bts diff --git a/src/lib/orderman/actors/IExecutionGateway.cpp b/src/lib/orderman/actors/IExecutionGateway.cpp new file mode 100644 index 000000000..ebe2a9e2b --- /dev/null +++ b/src/lib/orderman/actors/IExecutionGateway.cpp @@ -0,0 +1,20 @@ + +#include "IExecutionGateway.h" +#include "ICEExecutionGateway.h" +#include "GLBXExecutionGateway.h" + +namespace bts { +namespace orderman { + +std::unique_ptr +ExecutionGatewayFactory::create_gateway(const config::JetFIX::Connection& conn, trace::PerfLog& pl, md::SecurityOracle o) +{ + if(conn.platform() == bts::config::JetFIX_Connection_JetFIXPlatform_ICE || + conn.platform() == bts::config::JetFIX_Connection_JetFIXPlatform_SIMICE) + return std::make_unique(conn, pl, o); + else + return std::make_unique(conn, pl, o); +} + +} // namespace orderman +} // namespace bts diff --git a/src/lib/orderman/actors/IExecutionGateway.h b/src/lib/orderman/actors/IExecutionGateway.h new file mode 100644 index 000000000..14eff752d --- /dev/null +++ b/src/lib/orderman/actors/IExecutionGateway.h @@ -0,0 +1,52 @@ +#pragma once + +#include "lib/config/protos/jetfix.pb.h" +#include "lib/jetfix/codec/MessageDefinitions.h" +#include "lib/jetfix/common/OutFIXPtr.h" +#include "lib/md/SecurityOracle.h" +#include "lib/mercury/events/Update.h" +#include "lib/orderman/orderman_utils/orderman_common.h" +#include "lib/utils/bts_no_copy.h" + +#include + +namespace bts { +namespace trace {struct CorID;class PerfLog;} +namespace orderman { +class NewSecuritiesEvent; +class NewAdminDataEvent; +class ExecutionReport; + + +using OnGwStatusChange = std::function; +using OnExeReport = std::function; +using OnCancelReject = std::function; + +// fix execution gateway for a single FIX session +class IExecutionGateway { + public: + IExecutionGateway() = default; + virtual ~IExecutionGateway() {} + + virtual void start() = 0; + virtual void dispatch_incoming_fix_messages() = 0; + virtual void on_securities(const mercury::Update &) = 0; + virtual void on_admin_data(const mercury::Update &) = 0; + virtual void send_fix(const jetfix::OutFIXPtr&, trace::CorID) = 0; + + virtual OnExeReport& on_exe_report() = 0; + virtual OnGwStatusChange& on_gw_status_change() = 0; + virtual OnCancelReject &on_cancel_reject() = 0; +private: + BTS_NO_COPY(IExecutionGateway); +}; + +class ExecutionGatewayFactory +{ +public: + static std::unique_ptr create_gateway(const config::JetFIX::Connection&, trace::PerfLog&, md::SecurityOracle); + +}; + +} // namespace orderman +} // namespace bts diff --git a/src/lib/orderman/actors/order_manager/OrderManager.cpp b/src/lib/orderman/actors/order_manager/OrderManager.cpp index 2dadd8b89..a347e157c 100644 --- a/src/lib/orderman/actors/order_manager/OrderManager.cpp +++ b/src/lib/orderman/actors/order_manager/OrderManager.cpp @@ -15,6 +15,7 @@ #include "lib/orderman/orders/Order.h" #include "lib/orderman/securities/ISecurityDB.h" #include "lib/utils/inplace_function.h" +#include "lib/utils/thread_manager.h" #include #include @@ -44,12 +45,28 @@ struct OrderManager::PImpl { mercury::Actor::SubHandle filled_position_sub; OrderServicesData services_data; mercury::Stream pub_ready; + std::thread background_thread; + std::atomic shutdown = false; }; OrderManager::OrderManager(DynamicOrderRegistry const &b, trace::PerfLog &l, md::SecurityOracle o) - : me(new PImpl(*this, b, l, o)) {} -OrderManager::~OrderManager() {} + : me(new PImpl(*this, b, l, o)) { + auto id_gen = &me->services_data.id_gen; + auto done = &me->shutdown; + me->background_thread = + threading::make_thread("oman_background", [id_gen, done]() { + while (!(*done)) { + id_gen->refill(); + sleep_for(10ms); + } + }); +} + +OrderManager::~OrderManager() { + me->shutdown = true; + me->background_thread.join(); +} void OrderManager::init() { subscribe( @@ -63,11 +80,13 @@ void OrderManager::init() { void OrderManager::on_admin_data(const mercury::Update &m) { if (m.events) { - if (me->state == State::WAIT_ADMIN) { LOG(OrderMan, Info, - "Order manager received admin data. Subscribing " - "to security list now "); + "Order manager received admin data. Subscribing to security list " + "now "); + for (auto &osd : me->services_data.sessions) + osd->gw->on_admin_data(m); + me->state = State::WAIT_SEC; subscribe([this](auto const &sec_update) { this->on_securities(sec_update); @@ -100,7 +119,7 @@ void OrderManager::on_securities( // forward to execution gateways for (auto &osd : me->services_data.sessions) - osd->gw.on_securities(update); + osd->gw->on_securities(update); if (update.events) { @@ -440,19 +459,20 @@ void OrderManager::init_order_services() { // reject evets auto s_ptr = s.get(); s->exe_report_pub = make_stream(gw_name); - s->gw.on_exe_report() = [s_ptr, this](auto const &er, auto const &t) { + s->gw->on_exe_report() = [s_ptr, this](auto const &er, auto const &t) { publish(s_ptr->exe_report_pub, &er, t); }; s->gw_status_change_pub = make_stream(gw_name); - s->gw.on_gw_status_change() = [s_ptr, this, gw_name](bool b) { + s->gw->on_gw_status_change() = [s_ptr, this, gw_name](bool b) { publish(s_ptr->gw_status_change_pub, gw_name, b); }; s->cancel_reject_pub = make_stream(gw_name); - s->gw.on_cancel_reject() = [s_ptr, this](auto const &k, auto const &msg) { + s->gw->on_cancel_reject() = [s_ptr, this](auto const &k, + auto const &msg) { publish(s_ptr->cancel_reject_pub, k, msg); }; // handlers are ready so we can start processing fix events - s->gw.start(); + s->gw->start(); reload_fix_buffer(*s); } @@ -541,7 +561,7 @@ void OrderManager::after_event() { auto scoped_do_work = trace::log_scope(me->services_data.log, {}, "OrderManager::after_event"); fix_stream.close(); - osd->gw.send_fix(osd->current_buffer, osd->current_trace); + osd->gw->send_fix(osd->current_buffer, osd->current_trace); reload_fix_buffer(*osd); } } @@ -566,7 +586,7 @@ void OrderManager::on_poll(trace::PerfLog &log) { trace::log_scope(log, {}, "OrderManager::dispatch_incoming_fix_messages", utils::TimeStamp::Interval::from_microseconds(2)); for (auto &osd : me->services_data.sessions) - osd->gw.dispatch_incoming_fix_messages(); + osd->gw->dispatch_incoming_fix_messages(); } void OrderManager::publish_order_def_update( diff --git a/src/lib/orderman/actors/order_manager/OrderServices.h b/src/lib/orderman/actors/order_manager/OrderServices.h index 800320c58..2e1afdbb3 100644 --- a/src/lib/orderman/actors/order_manager/OrderServices.h +++ b/src/lib/orderman/actors/order_manager/OrderServices.h @@ -47,6 +47,9 @@ class OrderServices { return ss.is_gateway_connected(); } + uint64_t gen_id() { + return _data->id_gen.next(); + } Governor &governor(SessionServices &ss) { return ss.governor(); } void queue_fix(SessionServices &ss, jetfix::MessageTypes mt, diff --git a/src/lib/orderman/actors/order_manager/OrderServicesData.cpp b/src/lib/orderman/actors/order_manager/OrderServicesData.cpp index e175ba396..f6bbbd841 100644 --- a/src/lib/orderman/actors/order_manager/OrderServicesData.cpp +++ b/src/lib/orderman/actors/order_manager/OrderServicesData.cpp @@ -12,7 +12,9 @@ OrderServicesData::OrderServicesData(OrderManager &owner, order_factory(sec_db, order_router, b), last_timer_ev( Timer::now(), - to(datetime::FIX_timestamp(datetime::now()))) { + to(datetime::FIX_timestamp(datetime::now()))), + id_gen("orderman_ids",5000) +{ auto &all_connections = config::get().connection(); for (auto const &c : all_connections) diff --git a/src/lib/orderman/actors/order_manager/OrderServicesData.h b/src/lib/orderman/actors/order_manager/OrderServicesData.h index 8dedac1f1..bf1124406 100644 --- a/src/lib/orderman/actors/order_manager/OrderServicesData.h +++ b/src/lib/orderman/actors/order_manager/OrderServicesData.h @@ -9,7 +9,7 @@ #include "lib/mercury/events/TimerEvent.h" #include "lib/md/SecurityOracle.h" -#include "lib/orderman/actors/ExecutionGateway.h" +#include "lib/orderman/actors/IExecutionGateway.h" #include "lib/orderman/events/ActivateOrderEvent.h" #include "lib/orderman/events/BookUpdateEvent.h" #include "lib/orderman/events/TickerUpdateEvent.h" @@ -34,6 +34,7 @@ #include "lib/orderman/limiter/Limiter.h" #include "lib/orderman/order_router/OrderRouter.h" #include "lib/utils/datetime.h" +#include "lib/utils/Puid.h" #include @@ -52,7 +53,10 @@ struct SessionServicesData { trace::PerfLog &log, md::SecurityOracle o) : session(cfg.name()), gw_connected(false), cfg(cfg), governor(cfg.max_order_msgs_per_window(), cfg.window_duration_msecs()), - gw(cfg, log,o) {} + gw(ExecutionGatewayFactory::create_gateway(cfg, log, o)) + { + + } std::string session; std::vector free_buffers; @@ -69,7 +73,7 @@ struct SessionServicesData { Governor governor; jetfix::OutFIXPtr current_buffer; trace::CorID current_trace; - ExecutionGateway gw; + std::unique_ptr gw; mercury::Stream exe_report_pub; mercury::Stream gw_status_change_pub; mercury::Stream cancel_reject_pub; @@ -107,7 +111,8 @@ class OrderServicesData { mercury::Stream report_order_op_result_pub; mercury::TimerEvent last_timer_ev; std::vector> sessions; - + utils::IDGenerator id_gen ; + SessionServicesData &get_session(std::string const &session_name) { for (auto &s : sessions) if (s->session == session_name) diff --git a/src/lib/orderman/actors/order_manager/SessionServices.h b/src/lib/orderman/actors/order_manager/SessionServices.h index eab64257e..5ace9ced5 100644 --- a/src/lib/orderman/actors/order_manager/SessionServices.h +++ b/src/lib/orderman/actors/order_manager/SessionServices.h @@ -57,14 +57,14 @@ class SessionServices { _gateway_status_sub = src._gateway_status_sub; return *this; } + + config::JetFIX::Connection const &session_cfg() const { + return _session_data->cfg; + } protected: std::string const &session_name() const { return _session_data->session; } - config::JetFIX::Connection const &session_cfg() const { - return _session_data->cfg; - } - void queue_fix(jetfix::MessageTypes mt, jetfix::FIXFragment const &f, trace::CorID const &corid) { auto &stream = _session_data->current_buffer->first_stream(); diff --git a/src/lib/orderman/order_factory/OrderFactory.cpp b/src/lib/orderman/order_factory/OrderFactory.cpp index 5734e0242..8ffc67968 100644 --- a/src/lib/orderman/order_factory/OrderFactory.cpp +++ b/src/lib/orderman/order_factory/OrderFactory.cpp @@ -10,7 +10,8 @@ // order types #include "lib/orderman/order_factory/DynamicOrderRegistry.h" #include "lib/orderman/orders/DynamicOrder.h" -#include "lib/orderman/orders/NativeOrder.h" +#include "lib/orderman/orders/GLBXNativeOrder.h" +#include "lib/orderman/orders/ICENativeOrder.h" #include "lib/orderman/orders/TimeBoxOrder.h" #include "lib/orderman/orders/TriggerOrder.h" #include "lib/orderman/orders/algo/AlgoWrapperOrder.h" @@ -88,7 +89,7 @@ Order &OrderFactory::create(OrderKey parent, // field correctly. Algos will just propagate the value of this // field down so eventually native orders launched by algos will // get the right value. - if (dynamic_cast(&order) == nullptr && + if (dynamic_cast(&order) == nullptr && def.manual_order_indicator() == api::orderman::MANUAL_ORDER_INDICATOR_YES) { // An algo order incorrectly marked as a manual order @@ -113,6 +114,24 @@ Order &OrderFactory::recover(api::orderman::OrderRecord const &rec) { return order; } +NativeOrderBase *OrderFactory::make_order(const api::Venue exchange) { + if (exchange == api::Venue::VENUE_ICE || + exchange == api::Venue::VENUE_SIMICE) + return new ICENativeOrder(); + else + return new GLBXNativeOrder(); +} + +NativeOrderBase * +OrderFactory::make_native_order(const api::orderman::OrderDefinition &def) { + auto secptr = me->sec_db.resolve(SecurityKey(def.security_key())); + if (secptr == nullptr) + return new GLBXNativeOrder(); // GLBX Native can handle unresolved + // securities + else + return make_order(secptr->exchange()); +} + Order &OrderFactory::recreate_from_exe_report(ExecutionReport const &r) { using namespace api::orderman; @@ -121,7 +140,7 @@ Order &OrderFactory::recreate_from_exe_report(ExecutionReport const &r) { case NATIVE_ORDER_TYPE_LIMIT: case NATIVE_ORDER_TYPE_STOP: case NATIVE_ORDER_TYPE_STOP_LIMIT: { - auto native_order = std::make_unique(); + std::unique_ptr native_order(make_order(r.exchange)); native_order->_key = r.order_key; native_order->_parent_key = null_order_key(); auto def = native_order->on_init_from_exe_report( @@ -151,7 +170,7 @@ Order &OrderFactory::allocate(OrderKey k, OrderKey pk, case CustomOrderDefinition::kStop: case CustomOrderDefinition::kStopLimit: case CustomOrderDefinition::kGlbxIceberg: - order_ptr.reset(new NativeOrder()); + order_ptr.reset(make_native_order(def)); break; case CustomOrderDefinition::kIceberg: order_ptr.reset( diff --git a/src/lib/orderman/order_factory/OrderFactory.h b/src/lib/orderman/order_factory/OrderFactory.h index a7395413c..06410463c 100644 --- a/src/lib/orderman/order_factory/OrderFactory.h +++ b/src/lib/orderman/order_factory/OrderFactory.h @@ -11,6 +11,7 @@ class ISecurityDB; } namespace orderman { class Order; +class NativeOrderBase; class DynamicOrderRegistry; } // namespace orderman namespace orderman { @@ -45,6 +46,8 @@ class OrderFactory : public IOrderFactory { private: Order &allocate(OrderKey k, OrderKey pk, api::orderman::OrderDefinition const &); + NativeOrderBase* make_order(const api::Venue exchange); + NativeOrderBase* make_native_order(const api::orderman::OrderDefinition& def); struct PImpl; std::unique_ptr me; diff --git a/src/lib/orderman/orderman_utils/ExecutionReport.h b/src/lib/orderman/orderman_utils/ExecutionReport.h index 76b91a348..5ffc627b3 100644 --- a/src/lib/orderman/orderman_utils/ExecutionReport.h +++ b/src/lib/orderman/orderman_utils/ExecutionReport.h @@ -36,6 +36,10 @@ public: config::JetFIX::Connection const *cfg; std::string cust_order_handling_inst; std::string msg; + api::Venue exchange; + int64_t ice_cl_ord_id; + int32_t ice_company_id; + std::string memo; }; }} diff --git a/src/lib/orderman/orderman_utils/fix_order_codec.h b/src/lib/orderman/orderman_utils/fix_order_codec.h index 6b83b8794..14cceb2ce 100644 --- a/src/lib/orderman/orderman_utils/fix_order_codec.h +++ b/src/lib/orderman/orderman_utils/fix_order_codec.h @@ -7,6 +7,7 @@ #include "lib/jetfix/codec/i32_toa.h" #include "lib/utils/bts_assert.h" #include "orderman_common.h" +#include "lib/utils/base62.h" namespace bts { namespace api { @@ -19,6 +20,8 @@ constexpr int clordid_sz() { return 20; } constexpr int price_sz() { return 17; } constexpr int quantity_sz() { return 6; } constexpr int max_show_sz() { return 6; } +constexpr int memo_sz() {return 12;} +constexpr int ice_orderid_sz() {return 12;} constexpr uint32_t corid_sz() { return 16; } // 5 for channel, 1 separator, 10 sn @@ -50,6 +53,49 @@ template OrderKey get_order_key(MSG const &msg) { return result; } +inline void encode_memo(OrderKey ok, SecurityKey sk, std::string *out) +{ + auto ok_string = utils::to_base_62(ok.value()); + auto sk_string = utils::to_base_62(sk.value()); + bts_assert_msg(ok_string.size() == 6 && sk_string.size() == 6, "base62 encoding didn't work"); + *out = ok_string + sk_string; +} + +inline void decode_memo(const std::string& in, OrderKey* ok, SecurityKey* sk) +{ + bts_assert(in.size() == memo_sz()); + char buffer[memo_sz()/2+1]; + buffer[memo_sz()/2] = 0; + std::memcpy(buffer,&in[0],memo_sz()/2); + *ok = OrderKey( utils::from_base_62(std::string(buffer)) ); + + std::memcpy(buffer,&in[memo_sz()/2],memo_sz()/2); + *sk = SecurityKey( utils::from_base_62(std::string(buffer)) ); +} + +template +std::string get_ice_order_id_string(const MSG& msg) +{ + bts_assert(msg.template has()); + std::string oid_string(ice_orderid_sz(), ' '); + + int64_t id = msg.template get(); + u64_toa(id, &(oid_string[0]), ice_orderid_sz() ); + return oid_string; +} + +template +std::string get_ice_clord_id_string(const MSG& msg) +{ + bts_assert(msg.template has()); + std::string oid_string(ice_orderid_sz(), ' '); + + int64_t id = msg.template get(); + u64_toa(id, &(oid_string[0]), ice_orderid_sz() ); + return oid_string; +} + + 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); diff --git a/src/lib/orderman/orderman_utils/orderman_utils.cpp b/src/lib/orderman/orderman_utils/orderman_utils.cpp index ce9d4af61..be9535229 100644 --- a/src/lib/orderman/orderman_utils/orderman_utils.cpp +++ b/src/lib/orderman/orderman_utils/orderman_utils.cpp @@ -133,6 +133,13 @@ api::orderman::DynamicOrderParamDescriptor param_uint32(const std::string& name) return doap; } +api::orderman::DynamicOrderParamDescriptor param_security_key(const std::string& name) { + api::orderman::DynamicOrderParamDescriptor doap; + doap.set_param_name(name); + doap.mutable_param_type()->set_primitive(api::orderman::DYNAMIC_ORDER_PRIMITIVE_SECURITY_KEY); + return doap; +} + api::orderman::DynamicOrderParamDescriptor param_double(const std::string& name) { api::orderman::DynamicOrderParamDescriptor doap; doap.set_param_name(name); diff --git a/src/lib/orderman/orderman_utils/orderman_utils.h b/src/lib/orderman/orderman_utils/orderman_utils.h index 4deda32e9..dac8c43df 100644 --- a/src/lib/orderman/orderman_utils/orderman_utils.h +++ b/src/lib/orderman/orderman_utils/orderman_utils.h @@ -93,6 +93,7 @@ std::optional get_if(const google::protobuf::Map @@ -13,12 +13,12 @@ class CancelRejectEvent; class GatewayStatusChangeEvent; class ExecutionReport; -class NativeOrder : public Order { +class NativeOrder : public NativeOrderBase { public: NativeOrder(); ~NativeOrder() override; - api::orderman::OrderDefinition const &definition() const; + api::orderman::OrderDefinition const &definition() const override; void on_init_new(api::orderman::OrderDefinition const &, const PreActivationOrderServices &) override; void on_init_recovered(api::orderman::OrderRecord const &, @@ -33,7 +33,7 @@ class NativeOrder : public Order { api::orderman::OrderDefinition on_init_from_exe_report(ExecutionReport const &, - const PreActivationOrderServices &); + const PreActivationOrderServices &) override; protected: void warm_up_activate(OrderServicesData &, OrderServices &) override; @@ -74,5 +74,7 @@ class NativeOrder : public Order { std::string get_cust_order_handling_inst(api::orderman::RoutingRecord const&) ; }; +typedef NativeOrder GLBXNativeOrder; + } // namespace orderman } // namespace bts diff --git a/src/lib/orderman/orders/ICENativeOrder.cpp b/src/lib/orderman/orders/ICENativeOrder.cpp new file mode 100644 index 000000000..0155bf4f1 --- /dev/null +++ b/src/lib/orderman/orders/ICENativeOrder.cpp @@ -0,0 +1,1375 @@ +#include "ICENativeOrder.h" + +#include "lib/api/services/OrderService.pb.h" +#include "lib/domain_model/Security.h" +#include "lib/jetfix/codec/FIXFragment.h" +#include "lib/jetfix/codec/FieldDefinitions.h" +#include "lib/logging/logging.h" +#include "lib/orderman/order_factory/IOrderFactory.h" +#include "lib/orderman/order_router/OrderRouter.h" +#include "lib/orderman/orderman_utils/ExecutionReport.h" +#include "lib/orderman/orderman_utils/fix_order_codec.h" +#include "lib/orderman/orders/PreActivationOrderServices.h" +#include "lib/orderman/securities/ISecurityDB.h" +#include "lib/md/IntPrice.h" +#include "lib/utils/PBUtils.h" +#include "lib/utils/Puid.h" +#include "lib/utils/bts_chrono.h" + +#include "lib/orderman/actors/order_manager/OrderServices.h" + +#include +#include +#include +#include + +namespace bts { +namespace orderman { + +using namespace api::orderman; + +OrderStatus map_external_status(NativeOrderStatus); //in GLBXNativeOrder.cpp + +struct ICENativeOrder::PImpl +{ + PImpl() + : services(nullptr), session_services(), sec(nullptr), + previous_working_position(0), definition(), state(), + price_encoding_precision(7), exchange_order_id(), sender_compid(), id_scale() + { + exchange_order_id.reserve(100); + } + + OrderServices *services; + SessionServices *session_services; + + const api::Security *sec; + Quantity previous_working_position; + + OrderDefinition definition; + OrderState state; + int price_encoding_precision; + std::string exchange_order_id; + int sender_compid; + int64_t id_scale; + + jetfix::FIXFragment update_encoding; + jetfix::FIXFragment::Bookmark update_price_bm; + jetfix::FIXFragment::Bookmark update_quantity_bm; + jetfix::FIXFragment::Bookmark update_stop_px_bm; + jetfix::FIXFragment::Bookmark update_transact_time_bm; + jetfix::FIXFragment::Bookmark update_clordid_bm; + jetfix::FIXFragment::Bookmark update_orig_clordid_bm; + + jetfix::FIXFragment create_encoding; + jetfix::FIXFragment::Bookmark create_clordid_bm; + jetfix::FIXFragment::Bookmark create_price_bm; + jetfix::FIXFragment::Bookmark create_quantity_bm; + jetfix::FIXFragment::Bookmark create_transact_time_bm; + + jetfix::FIXFragment cancel_encoding; + jetfix::FIXFragment::Bookmark cancel_clordid_bm; + jetfix::FIXFragment::Bookmark cancel_orig_clordid_bm; + + OrderServices::ExeReportSubID exe_report_sid; + OrderServices::CancelRejectSubID cancel_reject_sid; + + bool buffered_cancel = false; + std::optional> buffered_update; + int buffered_tries = 0; +}; + +ICENativeOrder::ICENativeOrder() : me(new PImpl()) {} + +const OrderDefinition& ICENativeOrder::definition() const +{ + return me->definition; +} + +void ICENativeOrder::on_init_new(const OrderDefinition& def, const PreActivationOrderServices& serv) +{ + // order was just created. + me->definition = def; + me->sec = serv.sec_db.resolve(SecurityKey(def.security_key())); + if (!me->sec) + { + auto msg = fmt::format("Unknown security: {}", def.security_key()); + me->state.set_last_msg(msg); + transition(ORDER_STATUS_REJECTED_BY_SPARK, msg); + return; + } + + switch (def.custom_def().custom_case()) { + case CustomOrderDefinition::kMarket: + me->state.mutable_market(); + break; + case CustomOrderDefinition::kLimit: + me->state.mutable_limit(); + break; + case CustomOrderDefinition::kStop: + me->state.mutable_stop(); + break; + case CustomOrderDefinition::kStopLimit: + me->state.mutable_stop_limit(); + break; + default: + bts_assert(false); + break; + } + + auto rq = serv.order_router.get_routing_record( UserID(def.user_id()), AccountID(def.account_id()), *me->sec ); + if (!rq.has_route) + { + auto msg = fmt::format("Could not route order: {}", rq.error_msg); + LOG(OrderMan, Error, "{}", msg); + me->state.set_last_msg(msg); + transition(ORDER_STATUS_REJECTED_BY_SPARK, msg); + return; + } + + transition(ORDER_STATUS_INACTIVE); + auto &native_state = get_native_state(me->state); + *native_state.mutable_routing_record() = rq.routing_record; + setup_ids(rq.routing_record.sender_compid()); + init_fix(); +} + +void ICENativeOrder::init_recovered_common(const api::orderman::OrderRecord& rec, const PreActivationOrderServices& serv) +{ + // order was recovered from our orders database or from an execution report + const auto &def = rec.def(); + + me->definition = def; + me->state = rec.state(); + me->sec = serv.sec_db.resolve(SecurityKey(def.security_key())); + me->previous_working_position = Quantity(0); + + const auto &native_state = get_native_state(me->state); + setup_ids(native_state.routing_record().sender_compid()); + switch (me->state.status()) + { + case ORDER_STATUS_WORKING: + case ORDER_STATUS_PENDING_WORKING: + case ORDER_STATUS_DISCONNECTED: + { + // bail if we can't find the security for the order + if (!me->sec) + { + LOG(OrderMan, Error, + "Unknown security {} referenced by recovered order {}. This is a serious " + "problem which has to be corrected manually. ", + def.security_key(), rec.key()); + transition_to_error(NativeOrderState::SUB_ERROR_UNKNOWN_SECURITY, + fmt::format("Unknown security: {}", def.security_key())); + return; + } + + // bail if the session we used for the order is no longer valid + const auto &session = native_state.routing_record().session(); + if (!serv.order_router.is_valid_session(session)) + { + LOG(OrderMan, Error, + "Unknown JetFIX session {} referenced by recovered order {}. This is a serious " + "problem which has to be corrected manually. ", + session, rec.key()); + transition_to_error(NativeOrderState::SUB_ERROR_UNKNOWN_FIX_SESSION, + fmt::format("Unknown FIX session: {}", session)); + return; + } + + // if we made it this far we are good. + init_fix(); + if (me->state.status() != ORDER_STATUS_PENDING_WORKING) + { + me->exchange_order_id = native_state.exchange_order_id(); + add_exchange_order_id_to_fix(); + } + return; + } + + case ORDER_STATUS_REJECTED_BY_SPARK: + case ORDER_STATUS_ERROR: + case ORDER_STATUS_COMPLETED: + case ORDER_STATUS_CANCELED: + // terminal states, nothing to do + return; + + case ORDER_STATUS_INACTIVE: + case OrderStatus_INT_MIN_SENTINEL_DO_NOT_USE_: + case OrderStatus_INT_MAX_SENTINEL_DO_NOT_USE_: + bts_assert(false); + return; + } +} + +void ICENativeOrder::on_init_recovered(const OrderRecord& rec, const PreActivationOrderServices& serv) +{ + //TODO need to figure out if this is a day order from a prev session and cancel it + // const auto &def = rec.def(); + // auto &state = *orecord.mutable_state(); + // state.set_status(map_external_status(api::orderman::NativeOrderStatus::NATIVE_ORDER_STATUS_CANCELED); + init_recovered_common(rec, serv); +} + +api::orderman::OrderDefinition +ICENativeOrder::on_init_from_exe_report(const ExecutionReport& er, const PreActivationOrderServices& os) +{ + auto k = er.order_key; + auto nextid = me->services->gen_id(); + if(k.value() < nextid) + { + k = OrderKey(nextid); + LOG(OrderMan, Error, "Updating recovered order id with newly generated id {}", k.value()); + } + + OrderRecord orecord; + orecord.set_key(k.value()); + orecord.set_parent_key(null_order_key().value()); + + auto &def = *orecord.mutable_def(); + def.set_security_key(er.security->id()); + def.set_side(er.side); + + auto sec = os.sec_db.resolve(SecurityKey(def.security_key())); + auto rev_rec = os.order_router.get_reverse_routing_record(er.cfg->name(), std::to_string(er.ice_company_id), + to_trim_str(er.account_name), sec); + + if (rev_rec.ok) + { + def.set_user(rev_rec.user); + def.set_user_id(rev_rec.user_id.value()); + def.set_account(rev_rec.account); + def.set_account_id(rev_rec.account_id.value()); + } + else + { + LOG(OrderMan, Error,"Could not recover account and user information from order {}. The following information is available: {}. ", + k.value(), rev_rec.error_msg); + def.set_user(""); + def.set_user_id(0); + def.set_account(""); + def.set_account_id(0); + } + + def.set_time_in_force(er.time_in_force); + def.set_manual_order_indicator(er.manual_order_indicator); + def.set_cti_code(CTICode(er.cfg->cti_code()[0])); + def.set_customer_or_firm(CustomerOrFirm(er.cfg->customer_or_firm())); + + auto &state = *orecord.mutable_state(); + state.set_status(map_external_status(er.order_status)); + state.set_price(er.price.value()); + state.set_quantity(er.quantity.value()); + state.set_filled_quantity(er.filled_quantity.value()); + state.set_last_price(er.last_price.value()); + state.set_avg_price(er.last_price.value()); + state.set_last_msg(er.msg); + *state.mutable_last_change() = utils::pb::pb_now(); + + switch (er.order_type) + { + case NATIVE_ORDER_TYPE_MARKET: + def.mutable_custom_def()->mutable_market(); + state.mutable_market(); + break; + case NATIVE_ORDER_TYPE_LIMIT: + def.mutable_custom_def()->mutable_limit(); + state.mutable_limit(); + break; + case NATIVE_ORDER_TYPE_STOP: + { + auto &stop_def = *def.mutable_custom_def()->mutable_stop(); + stop_def.set_stop_price(er.stop_price.value()); + state.mutable_stop(); + break; + } + case NATIVE_ORDER_TYPE_STOP_LIMIT: + { + auto &stop_limit_def = *def.mutable_custom_def()->mutable_stop_limit(); + stop_limit_def.set_stop_price(er.stop_price.value()); + state.mutable_stop_limit(); + break; + } + case NATIVE_ORDER_TYPE_MARKET_LIMIT: + case NATIVE_ORDER_TYPE_UNDEFINED: + case NativeOrderType_INT_MIN_SENTINEL_DO_NOT_USE_: + case NativeOrderType_INT_MAX_SENTINEL_DO_NOT_USE_: + bts_assert(false); + break; + } + + auto &native_state = get_native_state(state); + native_state.set_exchange_order_id(to_trim_str(er.exchange_order_id)); + native_state.set_last_msg_seq_num(er.msg_seq_num); + native_state.set_native_type(er.order_type); + + + auto &rr = *native_state.mutable_routing_record(); + bool real_routing = false; + if (rev_rec.ok && sec) + { + const auto& real_rr = os.order_router.get_routing_record(rev_rec.user_id, rev_rec.account_id, *sec); + if(real_rr.has_route) + { + rr.CopyFrom(real_rr.routing_record); + real_routing = true; + } + } + if(!real_routing) + { + rr.set_sender_compid(er.cfg->sender_compid()); + rr.set_session(er.cfg->name()); + rr.set_account(to_trim_str(er.account_name)); + rr.set_operator_id(to_trim_str(er.operator_id)); + rr.set_cti_code(CTICode(er.cfg->cti_code()[0])); + rr.set_customer_or_firm(CustomerOrFirm(er.cfg->customer_or_firm())); + rr.set_location(er.cfg->sender_locationid()); + } + init_recovered_common(orecord, os); + return orecord.def(); +} + +ICENativeOrder::~ICENativeOrder() {} + +void ICENativeOrder::warm_up_activate(OrderServicesData &data, OrderServices &services) +{ + bts_assert(!me->services); + me->services = &services; + + init_order_services(data, services); + + // save these so we can re-instate them after we are done with the warmup + auto before_state = me->state.status(); + auto before_msg = me->state.last_msg(); + + _on_activate(NoLog, NoLog, Price(0), Quantity(0), services); + + // undo warm up side effects + me->state.set_status(before_state); + me->state.set_last_msg(before_msg); + + me->services->unsubscribe_from_exe_report(*me->session_services, me->exe_report_sid); + me->services->unsubscribe_from_cancel_reject(*me->session_services, me->cancel_reject_sid); + me->services->unsubscribe_from_gateway_status(*me->session_services); + me->services = nullptr; +} + +OrderOpResult +ICENativeOrder::_on_activate(log::Level reject_level, log::Level transition_level, + Price p, Quantity q, bts::orderman::OrderServices &services) +{ + me->state.set_price(p.value()); + me->state.set_quantity(q.value()); + me->state.set_filled_quantity(0); + me->state.set_last_price(0); + me->state.set_avg_price(0); + + me->session_services = &me->services->get_session_services( get_native_state(me->state).routing_record().session() ); + me->price_encoding_precision = me->services->price_encoding_precision(*me->session_services); + + // is our gateway connected? + if (!me->services->is_gateway_connected(*me->session_services)) + { + LOG(OrderMan, reject_level, + "Attempt to send order to a disconnected gateway. Dropping order {}", + key().value()); + auto msg = "Exchange gateway is down"; + transition(transition_level, ORDER_STATUS_REJECTED_BY_SPARK, msg); + return OrderOpResult{ORDER_ERROR_GATEWAY_NOT_READY, msg}; + } + + // can we trade? + auto lqr = me->services->limiter().can_trade( UserID(me->definition.user_id()), AccountID(me->definition.account_id()) ); + if (!lqr.ok()) + { + LOG(OrderMan, reject_level, + "Unauthorized order attempted. User: {} on " + "Account: {}. Msg: {} Order: {}. Dropping order", + me->definition.user(), me->definition.account(), lqr.error_msg, + key().value()); + + auto msg = fmt::format("Unauthorized order: {}", lqr.error_msg); + transition(transition_level, ORDER_STATUS_REJECTED_BY_SPARK, msg); + return OrderOpResult{ORDER_ERROR_UNAUTHORIZED, msg}; + } + + // can we trade this security, with this size? + lqr = me->services->limiter().can_send_new_order( AccountID(me->definition.account_id()), *me->sec, me->definition.side(), q ); + + if (!lqr.ok()) + { + LOG(OrderMan, reject_level, + "New order would breach limit. User: {} Account: " + "{} Msg: {} Order: {}. Dropping order", + me->definition.user(), me->definition.account(), lqr.error_msg, + key().value()); + + auto msg = fmt::format("Order would breach limit: {}", lqr.error_msg); + transition(transition_level, ORDER_STATUS_REJECTED_BY_SPARK, msg); + return OrderOpResult{ORDER_ERROR_LIMIT_BREACH, msg}; + } + + // are we under the messaging threshold? + if (!me->services->governor(*me->session_services).can_send()) + { + LOG(OrderMan, reject_level, + "New order would cause breach of messaging limits. Dropping order {}", key().value()); + auto msg = "Messaging threshold breached"; + transition(transition_level, ORDER_STATUS_REJECTED_BY_SPARK, msg); + return OrderOpResult(ORDER_ERROR_MESSAGING_LIMIT_BREACH, msg); + } + + // if we are here we are good. + // subscribe to usefull events + me->exe_report_sid = me->services->subscribe_to_exe_report(*me->session_services, key(), + [this](const auto &r) { this->on_exe_report(r); }); + me->cancel_reject_sid = me->services->subscribe_to_cancel_reject(*me->session_services, key(), + [this](const auto &cr) { this->on_cancel_reject(cr); }); + me->services->subscribe_to_gateway_status(*me->session_services, + [this](const auto &gw) { this->on_gateway_status(gw); }); + + // send out order + auto &encoding = me->create_encoding; + + auto create_clorid = get_new_clordid(); + me->create_clordid_bm = encoding.overwrite( me->create_clordid_bm, create_clorid, count_digits(create_clorid) ); + + switch (me->definition.custom_def().custom_case()) + { + case CustomOrderDefinition::kLimit: + case CustomOrderDefinition::kStopLimit: + { + me->create_price_bm = encoding.overwrite(me->create_price_bm, + securities::encode_exchange_price(*me->sec, p).value(), + me->price_encoding_precision, price_sz()); + break; + } + case CustomOrderDefinition::kStop: + case CustomOrderDefinition::kMarket: + break; + default: + bts_assert(false); + break; + } + + me->create_quantity_bm = encoding.overwrite(me->create_quantity_bm, q.value(), quantity_sz()); + const auto &ts = services.get_time_str(); + me->create_transact_time_bm = encoding.overwrite(me->create_transact_time_bm, &ts.value()[0], ts.value().size()); + return OrderOpResult{}; +} + +OrderOpResult ICENativeOrder::on_activate(Price p, Quantity q, bts::orderman::OrderServices &services) +{ + bts_assert(!me->services); + me->services = &services; + auto result = OrderOpResult{}; + switch (me->state.status()) + { + case ORDER_STATUS_INACTIVE: + { + auto scoped_log = services.log_scope("ICENativeOrder::on_activate", key().value()); + // this is a brand new order + + result = _on_activate(Warn, Info, p, q, services); + if (result.ok()) + { + me->services->queue_fix(*me->session_services, jetfix::MessageTypes::NewOrderSingle, me->create_encoding); + me->services->governor(*me->session_services).record_sent(); + me->state.set_status(ORDER_STATUS_PENDING_WORKING); + + LOG(OrderMan, Info, "Sent new order with key {} to gateway", key().value()); + // Early return so we skip the expensive report_state() + // call. It is OK to spkip it because we are bound to report + // our state once the excange aknowledges the order. + update_working_position(); + return OrderOpResult{}; + } + + // When the order fails its limits checks we fall through the + // switch to report the error state at the end of this function + break; + } + + // all cases below are for orders recovered from disk + case ORDER_STATUS_WORKING: + me->session_services = &me->services->get_session_services( get_native_state(me->state).routing_record().session() ); + me->services->subscribe_to_exe_report(*me->session_services, key(), + [this](const auto &r) { this->on_exe_report(r); }); + me->services->subscribe_to_cancel_reject(*me->session_services, key(), + [this](const auto &cr) { this->on_cancel_reject(cr); }); + me->services->subscribe_to_gateway_status(*me->session_services, + [this](const auto &gw) { this->on_gateway_status(gw); }); + + if (!me->services->is_gateway_connected(*me->session_services)) + { + result.error = ORDER_ERROR_GATEWAY_NOT_READY; + result.error_msg = "Gateway is disconnected"; + transition(ORDER_STATUS_DISCONNECTED); + } + break; + + case ORDER_STATUS_DISCONNECTED: + me->session_services = &me->services->get_session_services( get_native_state(me->state).routing_record().session() ); + me->services->subscribe_to_exe_report(*me->session_services, key(), + [this](const auto &r) { this->on_exe_report(r); }); + me->services->subscribe_to_cancel_reject(*me->session_services, key(), + [this](const auto &cr) { this->on_cancel_reject(cr); }); + me->services->subscribe_to_gateway_status(*me->session_services, + [this](const auto &gw) { this->on_gateway_status(gw); }); + + if (me->services->is_gateway_connected(*me->session_services)) + transition(ORDER_STATUS_WORKING); + break; + + case ORDER_STATUS_PENDING_WORKING: + me->session_services = &me->services->get_session_services( get_native_state(me->state).routing_record().session() ); + me->services->subscribe_to_exe_report(*me->session_services, key(), + [this](const auto &r) { this->on_exe_report(r); }); + me->services->subscribe_to_cancel_reject(*me->session_services, key(), + [this](const auto &cr) { this->on_cancel_reject(cr); }); + me->services->subscribe_to_gateway_status(*me->session_services, + [this](const auto &gw) { this->on_gateway_status(gw); }); + break; + + case ORDER_STATUS_REJECTED_BY_SPARK: + case ORDER_STATUS_COMPLETED: + case ORDER_STATUS_CANCELED: + case ORDER_STATUS_ERROR: + break; + + case OrderStatus_INT_MIN_SENTINEL_DO_NOT_USE_: + case OrderStatus_INT_MAX_SENTINEL_DO_NOT_USE_: + bts_assert(false); + break; + } + report_state(); + return result; +} + +OrderOpResult ICENativeOrder::on_update(Price p, Quantity q) +{ + api::orderman::Update update; + update.set_price(p.value()); + update.set_quantity(q.value()); + return on_update(update); +} + +OrderOpResult ICENativeOrder::on_update(api::orderman::Update const &update) +{ + auto p = Price(update.price()); + auto q = Quantity(update.quantity()); + auto scoped_log = me->services->log_scope("ICENativeOrder::on_update", key().value()); + auto result = OrderOpResult{}; + + switch (me->state.status()) + { + case ORDER_STATUS_WORKING: + { + auto previous_working_qty = 0; + if (me->state.quantity() > me->state.filled_quantity()) + previous_working_qty = me->state.quantity() - me->state.filled_quantity(); + + auto current_working_qty = 0; + if (q.value() > me->state.filled_quantity()) + current_working_qty = q.value() - me->state.filled_quantity(); + + auto lqr = me->services->limiter().can_update_order( AccountID(me->definition.account_id()), *me->sec, + me->definition.side(), Quantity(previous_working_qty), + Quantity(current_working_qty)); + + // are we under the position limits? + if (!lqr.ok()) + { + auto msg = fmt::format("Cannot update order due to limit breach: {}", lqr.error_msg); + LOG(OrderMan, Error, + "Order update failed due to limit breach. Order: {} Msg: {}", + key().value(), lqr.error_msg); + ignore_or_cancel(msg, ORDER_ERROR_LIMIT_BREACH, &result); + break; + } + + // are we under the messaging threshold? + if (!me->services->governor(*me->session_services).can_send()) + { + LOG(OrderMan, Warn, "Cannot update order {} because of messaging limits. ", key().value()); + auto msg = "Messaging threshold breached"; + ignore_or_cancel(msg, ORDER_ERROR_MESSAGING_LIMIT_BREACH, &result); + break; + } + + auto &encoding = me->update_encoding; + + if (update.has_custom()) + { + bts_assert(update.custom().custom_case() == me->definition.custom_def().custom_case()); + if (update.custom().custom_case() == CustomOrderDefinition::kStopLimit) + { + auto const &stop_limit = update.custom().stop_limit(); + auto stop_px = Price(stop_limit.stop_price()); + me->update_stop_px_bm = encoding.overwrite(me->update_stop_px_bm, + securities::encode_exchange_price(*me->sec, stop_px).value(), + me->price_encoding_precision, price_sz()); + } + } + me->update_price_bm = encoding.overwrite(me->update_price_bm, securities::encode_exchange_price(*me->sec, p).value(), + me->price_encoding_precision, price_sz()); + me->update_quantity_bm = encoding.overwrite(me->update_quantity_bm, q.value(), quantity_sz()); + + const auto &ts = me->services->get_time_str(); + me->update_transact_time_bm = encoding.overwrite( me->update_transact_time_bm, &ts.value()[0], ts.value().size() ); + + auto update_clorid = get_new_clordid(); + me->update_clordid_bm = encoding.overwrite( me->update_clordid_bm, update_clorid, count_digits(update_clorid) ); + + me->services->queue_fix(*me->session_services, jetfix::MessageTypes::CancelReplace, me->update_encoding); + me->services->governor(*me->session_services).record_sent(); + update_working_position(); + if (me->state.last_msg().size()) + me->state.set_last_msg(""); + + LOG(OrderMan, Warn, + "ICE may not honor this update if we recently issued an unacknowledged cancelreplace {}. Update: {}@{}. Buffering", + key().value(), q.value(), p.value()); + me->buffered_update.emplace(p, q); + + // early return so we skip report_state (for speed) + return OrderOpResult{}; + } + + case ORDER_STATUS_DISCONNECTED: + case ORDER_STATUS_PENDING_WORKING: + LOG(OrderMan, Warn, + "Cannot apply update to order {}. Update: {}@{}. Buffering", + key().value(), q.value(), p.value()); + me->buffered_update.emplace(p, q); + break; + + case ORDER_STATUS_REJECTED_BY_SPARK: + case ORDER_STATUS_COMPLETED: + case ORDER_STATUS_CANCELED: + case ORDER_STATUS_ERROR: + break; + + case ORDER_STATUS_INACTIVE: + case OrderStatus_INT_MIN_SENTINEL_DO_NOT_USE_: + case OrderStatus_INT_MAX_SENTINEL_DO_NOT_USE_: + bts_assert(false); + break; + } + report_state(); + return result; +} + +OrderOpResult ICENativeOrder::on_detach() { return OrderOpResult{}; } + +void ICENativeOrder::on_attached(api::orderman::OrderDefinition const &new_parent) +{ + switch (me->state.status()) + { + case ORDER_STATUS_PENDING_WORKING: + case ORDER_STATUS_DISCONNECTED: + case ORDER_STATUS_WORKING: { + LOG(OrderMan, Info, "Native order {} has been attached to by {}", + key().value(), utils::pb::to_string(new_parent)); + + // Update FIX tags to reflect new parent's information + auto rq = me->services->route_order(UserID(new_parent.user_id()), + AccountID(new_parent.account_id()), + *me->sec); + + if (!rq.has_route) + { + auto msg = fmt::format("Cannot update order routing information: {}", rq.error_msg); + transition(me->state.status(), msg); + report_state(); + return; + } + + auto &native_state = get_native_state(me->state); + *native_state.mutable_routing_record() = rq.routing_record; + + // setup_ids(rq.routing_record.sender_compid()); + // init_fix(); + // if (has_exchange_order_id()) + // add_exchange_order_id_to_fix(); + //ICE is a little different with some of these tags, there is no manual order reported and the user is really + //not the user we are updating here. So we can update the BTS data but not reinit the fix + //We should not reinit the fix because we need to correctly cache the orig_clordid for cancels and updates subsequent to + //the first update. + + // Update user, account and manual order indicator based on new parent + me->definition.set_manual_order_indicator(api::orderman::ManualOrderIndicator::MANUAL_ORDER_INDICATOR_NO); + me->definition.set_user(new_parent.user()); + me->definition.set_user_id(new_parent.user_id()); + me->definition.set_account(new_parent.account()); + me->definition.set_account_id(new_parent.account_id()); + me->services->report_definition(me->definition); + report_state(); + break; + } + + case ORDER_STATUS_CANCELED: + case ORDER_STATUS_REJECTED_BY_SPARK: + case ORDER_STATUS_COMPLETED: + case ORDER_STATUS_ERROR: + break; + + case ORDER_STATUS_INACTIVE: + case OrderStatus_INT_MIN_SENTINEL_DO_NOT_USE_: + case OrderStatus_INT_MAX_SENTINEL_DO_NOT_USE_: + bts_assert(false); + break; + } +} + +OrderOpResult ICENativeOrder::on_cancel() +{ + auto scoped_log = me->services->log_scope("ICENativeOrder::on_cancel", key().value()); + auto result = OrderOpResult{}; + + switch (me->state.status()) + { + case ORDER_STATUS_WORKING: + { + auto &encoding = me->cancel_encoding; + auto cancel_clorid = get_new_clordid(); + me->cancel_clordid_bm = encoding.overwrite( me->cancel_clordid_bm, cancel_clorid, count_digits(cancel_clorid) ); + + me->services->queue_fix(*me->session_services, jetfix::MessageTypes::Cancel,me->cancel_encoding); + me->buffered_cancel = true; + LOG(OrderMan, Warn, + "ICE May not honor this cancel if we'd previously issued an unacknowledged cancelreplace " + "Buffering cancel to be safe", + key().value()); + break; + } + + case ORDER_STATUS_CANCELED: + break; + case ORDER_STATUS_REJECTED_BY_SPARK: + case ORDER_STATUS_COMPLETED: + case ORDER_STATUS_ERROR: + result.error = ORDER_ERROR_INVALID_OP; + result.error_msg = "Order is already terminated"; + break; + + case ORDER_STATUS_PENDING_WORKING: + { + me->buffered_cancel = true; + LOG(OrderMan, Warn, + "Cannot cancel order {} while in the pending working state. " + "Buffering cancel", + key().value()); + break; + } + case ORDER_STATUS_DISCONNECTED: + { + me->buffered_cancel = true; + LOG(OrderMan, Warn, + "Cannot cancel order {} while in the disconnected state. Buffering " + "cancel", + key().value()); + auto msg = "Order will be canceled when re-connected"; + result.error = ORDER_ERROR_GATEWAY_NOT_READY; + result.error_msg = msg; + transition(me->state.status(), msg); + break; + } + case ORDER_STATUS_INACTIVE: + case OrderStatus_INT_MIN_SENTINEL_DO_NOT_USE_: + case OrderStatus_INT_MAX_SENTINEL_DO_NOT_USE_: + bts_assert(false); + break; + } + report_state(); + return result; +} + +bool ICENativeOrder::is_old_exe_report(api::orderman::NativeOrderState &native_state, const NewExecutionReportEvent &er) const +{ + if (native_state.last_transact_time().size() != sizeof(mercury::TimeStampStr)) + return false; + + if (std::lexicographical_compare(native_state.last_transact_time().begin(), + native_state.last_transact_time().end(), + er.data->transact_time.value().begin(), + er.data->transact_time.value().end())) + return false; + + if (std::equal(native_state.last_transact_time().begin(), + native_state.last_transact_time().end(), + er.data->transact_time.value().begin(), + er.data->transact_time.value().end())) + return native_state.last_msg_seq_num() >= er.data->msg_seq_num; + + return true; +} + +void ICENativeOrder::on_exe_report(const NewExecutionReportEvent &er) +{ + auto scoped_log = me->services->log_scope("ICENativeOrder::on_exe_report", key().value()); + + auto &state = me->state; + auto &native_state = get_native_state(state); + if(er.data->exec_type == api::orderman::ExecType::EXEC_TYPE_TRADE_CANCEL || + er.data->exec_type == api::orderman::ExecType::EXEC_TYPE_TRADE_CORRECT) + { + LOG(OrderMan, Info, "ICENativeOrder ignoring Cancel or Correct execution report {} in order {}", er.data->msg_seq_num, key().value()); + return; + } + + // if we have a previous state we want to ensure this execution + // report was not already considered. + if (is_old_exe_report(native_state, er)) + { + LOG(OrderMan, Info, "Ignoring already seen execution report {} in order {}", er.data->msg_seq_num, key().value()); + return; + } + + // we do not care about spread leg fills for purposes of order + // state maintenance + if (er.data->leg_security) + { + LOG(OrderMan, Diag, "Ignoring leg execution report for order {}", key().value()); + return; + } + + native_state.set_last_msg_seq_num(er.data->msg_seq_num); + native_state.set_last_transact_time(to_str(er.data->transact_time)); + + auto external_status = map_external_status(er.data->order_status); + if (external_status != ORDER_STATUS_CANCELED) + { + state.set_avg_price(er.data->avg_price.value()); + state.set_filled_quantity(er.data->filled_quantity.value()); + state.set_last_price(er.data->last_price.value()); + //cache the new clordid + if(er.data->ice_cl_ord_id > 0) + { + auto clordid = er.data->ice_cl_ord_id; + me->update_orig_clordid_bm = me->update_encoding.overwrite( me->update_orig_clordid_bm, clordid, count_digits(clordid) ); + me->cancel_orig_clordid_bm = me->cancel_encoding.overwrite( me->cancel_orig_clordid_bm, clordid, count_digits(clordid) ); + } + } + + state.set_last_msg(er.data->msg); + + native_state.set_native_status(er.data->order_status); + native_state.set_native_type(er.data->order_type); + bool got_first_exchange_order_id = !has_exchange_order_id(); + if (got_first_exchange_order_id) + { + me->exchange_order_id = to_trim_str(er.data->exchange_order_id); + native_state.set_exchange_order_id(me->exchange_order_id); + add_exchange_order_id_to_fix(); + } + + if (!terminal_state(me->state.status())) + { + if(!isnan(er.data->price.value())) + 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: + case ORDER_STATUS_CANCELED: + me->buffered_cancel = false; + me->buffered_update.reset(); + break; + + // receiving an execution report does does not mean + // we are connected + case ORDER_STATUS_DISCONNECTED: + break; + + case ORDER_STATUS_PENDING_WORKING: + if (external_status == ORDER_STATUS_WORKING && + !me->services->is_gateway_connected(*me->session_services)) + transition(ORDER_STATUS_DISCONNECTED); + else + { + transition(external_status); + if (external_status == ORDER_STATUS_WORKING) + { + handle_buffered_state("first ack"); + } + } + break; + + case ORDER_STATUS_WORKING: + { + transition(external_status); + if(me->buffered_update) + { + auto [p, q] = me->buffered_update.value(); + if(er.data->price.value() == p.value() && er.data->quantity.value() == q.value()) + me->buffered_update.reset(); + if(me->buffered_tries++ > 10) + { + me->buffered_tries = 0; + me->buffered_update.reset(); + } + } + handle_buffered_state("working"); + } + break; + + case ORDER_STATUS_INACTIVE: + case ORDER_STATUS_ERROR: + case ORDER_STATUS_REJECTED_BY_SPARK: + case OrderStatus_INT_MIN_SENTINEL_DO_NOT_USE_: + case OrderStatus_INT_MAX_SENTINEL_DO_NOT_USE_: + bts_assert(false); + break; + } + report_state(); + + // we only need the exhange order id and the routing record + // reported once, since they are both immutable, so we clear them + // after their first report to make subsquent state updates faster + if (got_first_exchange_order_id) + { + native_state.clear_exchange_order_id(); + native_state.mutable_routing_record()->Clear(); + } +} + +void ICENativeOrder::handle_buffered_state(const std::string& condition) +{ + if (me->buffered_cancel) + { + LOG(OrderMan, Warn, + "Applying buffered cancel to order {}, on ", + condition, + key().value()); + on_cancel(); + } + else if (me->buffered_update.has_value()) + { + auto [p, q] = me->buffered_update.value(); + LOG(OrderMan, Warn, + "Applying buffered update ({}@{}) to order {}, on ", + condition, + q.value(),p.value(), key().value()); + on_update(p, q); + } +} + +void ICENativeOrder::on_cancel_reject(const orderman::CancelRejectEvent &cr) +{ + auto &state = me->state; + state.set_last_msg(cr.reject_msg); + LOG(OrderMan, Warn, "Cancel reject received for order {}", key().value()); + + switch (me->state.status()) + { + case ORDER_STATUS_COMPLETED: + case ORDER_STATUS_CANCELED: + case ORDER_STATUS_DISCONNECTED: + case ORDER_STATUS_PENDING_WORKING: + case ORDER_STATUS_WORKING: + break; + + case ORDER_STATUS_INACTIVE: + case ORDER_STATUS_ERROR: + case ORDER_STATUS_REJECTED_BY_SPARK: + case OrderStatus_INT_MIN_SENTINEL_DO_NOT_USE_: + case OrderStatus_INT_MAX_SENTINEL_DO_NOT_USE_: + bts_assert(false); + break; + } + report_state(); +} + +void ICENativeOrder::on_gateway_status(const GatewayStatusChangeEvent &ev) +{ + if (ev.is_connected) + { + switch (me->state.status()) + { + case ORDER_STATUS_COMPLETED: + case ORDER_STATUS_CANCELED: + case ORDER_STATUS_ERROR: + case ORDER_STATUS_PENDING_WORKING: + break; + case ORDER_STATUS_WORKING: + break; + case ORDER_STATUS_DISCONNECTED: + { + const auto &native_state = get_native_state(me->state); + transition(map_external_status(native_state.native_status())); + if (me->state.status() == ORDER_STATUS_WORKING) + { + handle_buffered_state("re-connection"); + } + break; + } + case ORDER_STATUS_INACTIVE: + case ORDER_STATUS_REJECTED_BY_SPARK: + case OrderStatus_INT_MIN_SENTINEL_DO_NOT_USE_: + case OrderStatus_INT_MAX_SENTINEL_DO_NOT_USE_: + bts_assert(false); + break; + } + } + else + { + switch (me->state.status()) + { + case ORDER_STATUS_COMPLETED: + case ORDER_STATUS_CANCELED: + case ORDER_STATUS_ERROR: + case ORDER_STATUS_DISCONNECTED: + case ORDER_STATUS_PENDING_WORKING: + break; + case ORDER_STATUS_WORKING: + transition(ORDER_STATUS_DISCONNECTED); + break; + case ORDER_STATUS_INACTIVE: + case ORDER_STATUS_REJECTED_BY_SPARK: + case OrderStatus_INT_MIN_SENTINEL_DO_NOT_USE_: + case OrderStatus_INT_MAX_SENTINEL_DO_NOT_USE_: + bts_assert(false); + break; + } + } + report_state(); +} + +void ICENativeOrder::update_working_position() +{ + if (me->sec) + { + 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_to_working_position( *me->sec, AccountID(me->definition.account_id()), + me->definition.side(), Quantity(current_position.value() - previous_position.value()) ); + } + else if (previous_position.value() > current_position.value()) + { + me->services->limiter().remove_from_working_position( *me->sec, AccountID(me->definition.account_id()), + me->definition.side(), Quantity(previous_position.value() - current_position.value()) ); + } + } + else + LOG(OrderMan, Error, + "Order {} ignored for purposes of limit enforcing " + "because it references unknown security {} ", + key().value(), me->definition.security_key()); +} + +void ICENativeOrder::report_state() +{ + update_working_position(); + me->services->report_state(me->state); +} + +void ICENativeOrder::setup_ids(const std::string& sender_compid) +{ + me->sender_compid = std::atoi(sender_compid.c_str()); + int digs = count_digits(me->sender_compid); + me->id_scale = md::pow10(ice_orderid_sz()-digs); +} + + +int64_t ICENativeOrder::get_clordid(uint64_t bts_id) +{ + int64_t id = me->id_scale * std::max(me->sender_compid, 1) + (bts_id % me->id_scale); + return id; +} + +int64_t ICENativeOrder::get_new_clordid() +{ + return get_clordid(me->services->gen_id()); +} + +void ICENativeOrder::init_fix() +{ + bts_assert(me->sec); + + using namespace jetfix; + me->create_encoding = FIXFragment{}; + me->update_encoding = FIXFragment{}; + me->cancel_encoding = FIXFragment{}; + + const auto &def = me->definition; + const auto &sec = *me->sec; + bts_assert(sec.has_ice()); + const auto &rr = get_native_state(me->state).routing_record(); + + std::string memo(memo_sz(), ' '); + encode_memo(key(), SecurityKey(sec.id()), &memo); + + // initialize create FIX + auto &create_encoding = me->create_encoding; + + create_encoding.add(memo); + me->create_clordid_bm = create_encoding.add(get_clordid(0), ice_orderid_sz()); + create_encoding.add(std::atoi(rr.company_id().c_str())); + //create_encoding.add(""); //optional, unclear if we need + std::string cust = rr.cust_order_handing_inst(); + if(cust == "") + cust = "Y"; + create_encoding.add(cust); + create_encoding.add(rr.operator_id()); + create_encoding.add(rr.operator_id() + "|" + rr.authorized_trader()); + create_encoding.add("ISV-BTS|" + rr.authorized_group()); + create_encoding.add(rr.authorized_trader()); + create_encoding.add('1'); + create_encoding.add(sec.ice().exchange_id()); + create_encoding.add(def.side()); + me->create_transact_time_bm = create_encoding.add(datetime::FIX_timestamp(datetime::now())); + me->create_quantity_bm = create_encoding.add(0, quantity_sz()); + create_encoding.add(def.time_in_force()); + if(rr.account() != "") + create_encoding.add(rr.account()); + if(rr.clearing_firm() != "") + create_encoding.add(std::atoi(rr.clearing_firm().c_str())); + if(rr.account_code() != "") + create_encoding.add(rr.account_code()[0]); + if(rr.cti_code() != api::orderman::CTICode::CTI_CODE_UNDEFINED) + create_encoding.add( rr.cti_code() - '0' ); + if(rr.customer_account_ref_id() != "") + create_encoding.add(rr.customer_account_ref_id()); + + // initialize update FIX + auto &update_encoding = me->update_encoding; + update_encoding.add(memo); + me->update_orig_clordid_bm = update_encoding.add(get_clordid(0), ice_orderid_sz()); + update_encoding.add(std::atoi(rr.company_id().c_str())); + //update_encoding.add(""); //optional, unclear if we need + update_encoding.add(rr.operator_id()); + update_encoding.add(rr.operator_id() + "|" + rr.authorized_trader()); + update_encoding.add("ISV-BTS|" + rr.authorized_group()); + update_encoding.add(rr.authorized_trader()); + update_encoding.add('1'); + update_encoding.add(sec.ice().exchange_id()); + update_encoding.add(def.side()); + me->update_transact_time_bm = update_encoding.add(datetime::FIX_timestamp(datetime::now())); + me->update_quantity_bm = update_encoding.add(0, quantity_sz()); + me->update_clordid_bm = update_encoding.add(get_clordid(0), ice_orderid_sz()); + update_encoding.add(def.time_in_force()); + if(rr.account() != "") + update_encoding.add(rr.account()); + if(rr.clearing_firm() != "") + update_encoding.add(std::atoi(rr.clearing_firm().c_str())); + if(rr.account_code() != "") + update_encoding.add(rr.account_code()[0]); + if(rr.cti_code() != api::orderman::CTICode::CTI_CODE_UNDEFINED) + update_encoding.add( rr.cti_code() - '0' ); + if(rr.customer_account_ref_id() != "") + update_encoding.add(rr.customer_account_ref_id()); + + + // initialize cancel FIX + auto &cancel_encoding = me->cancel_encoding; + cancel_encoding.add(memo); + me->cancel_orig_clordid_bm = cancel_encoding.add(get_clordid(0), ice_orderid_sz()); + cancel_encoding.add(std::atoi(rr.company_id().c_str())); + cancel_encoding.add(rr.authorized_trader()); + cancel_encoding.add(sec.ice().exchange_id()); + cancel_encoding.add(def.side()); + me->cancel_clordid_bm = cancel_encoding.add(get_clordid(0), ice_orderid_sz()); + + // special tags + switch (def.custom_def().custom_case()) + { + case CustomOrderDefinition::kMarket: + // type + create_encoding.add(NATIVE_ORDER_TYPE_MARKET); + update_encoding.add(NATIVE_ORDER_TYPE_MARKET); + cancel_encoding.add(NATIVE_ORDER_TYPE_MARKET); + break; + + case CustomOrderDefinition::kLimit: + // type + create_encoding.add(NATIVE_ORDER_TYPE_LIMIT); + update_encoding.add(NATIVE_ORDER_TYPE_LIMIT); + cancel_encoding.add(NATIVE_ORDER_TYPE_LIMIT); + + // price + me->create_price_bm = create_encoding.add(0, me->price_encoding_precision, price_sz()); + me->update_price_bm = update_encoding.add(0, me->price_encoding_precision, price_sz()); + break; + + case CustomOrderDefinition::kStop: + { + // type + create_encoding.add(NATIVE_ORDER_TYPE_STOP); + update_encoding.add(NATIVE_ORDER_TYPE_STOP); + cancel_encoding.add(NATIVE_ORDER_TYPE_STOP); + + // stop px + auto stop_px = def.custom_def().stop().stop_price(); + auto encoded_stop_px = securities::encode_exchange_price(*me->sec, Price(stop_px)).value(); + create_encoding.add(encoded_stop_px, me->price_encoding_precision, -1); + break; + } + + case CustomOrderDefinition::kStopLimit: + { + // type + create_encoding.add(NATIVE_ORDER_TYPE_STOP_LIMIT); + update_encoding.add(NATIVE_ORDER_TYPE_STOP_LIMIT); + cancel_encoding.add(NATIVE_ORDER_TYPE_STOP_LIMIT); + + // stop px + auto stop_px = def.custom_def().stop_limit().stop_price(); + auto encoded_stop_px = securities::encode_exchange_price(*me->sec, Price(stop_px)).value(); + create_encoding.add(encoded_stop_px, me->price_encoding_precision, -1); + me->update_stop_px_bm = update_encoding.add(encoded_stop_px, me->price_encoding_precision, -1); + + // price + me->create_price_bm = create_encoding.add(0, me->price_encoding_precision, price_sz()); + me->update_price_bm = update_encoding.add(0, me->price_encoding_precision, price_sz()); + break; + } + default: + bts_assert(false); + break; + } +} + +void ICENativeOrder::add_exchange_order_id_to_fix() +{ + bts_assert(has_exchange_order_id()); + me->update_encoding.add(me->exchange_order_id); + me->cancel_encoding.add(me->exchange_order_id); +} + +bool ICENativeOrder::has_exchange_order_id() +{ + return me->exchange_order_id.size(); +} + +void ICENativeOrder::transition(api::orderman::OrderStatus new_status, std::string const &msg) +{ + transition(Info, new_status, msg); +} + +void ICENativeOrder::transition(log::Level level, api::orderman::OrderStatus new_status, std::string const &msg) +{ + const auto &from = OrderStatus_Name(me->state.status()); + const auto &to = OrderStatus_Name(new_status); + if (from != to) + LOG(OrderMan, level, "Transitioning order {} from {} to {}. Msg: {}", key().value(), from, to, msg); + me->state.set_status(new_status); + me->state.set_last_msg(msg); +} + +void ICENativeOrder::transition(api::orderman::OrderStatus new_status) +{ + const auto &from = OrderStatus_Name(me->state.status()); + const auto &to = OrderStatus_Name(new_status); + if (from != to) + LOG(OrderMan, Info, "Transitioning order {} from {} to {}", key().value(), from, to); + else + LOG(OrderMan, Diag, "Transitioning order {} from {} to {}", key().value(), from, to); + + me->state.set_status(new_status); +} + +void ICENativeOrder::transition_to_error(api::orderman::NativeOrderState::SubError sub_error, std::string const &msg) +{ + me->state.mutable_limit()->mutable_native_state()->set_sub_error_code(sub_error); + transition(ORDER_STATUS_ERROR, msg); +} + +Quantity ICENativeOrder::working_position() +{ + switch (me->state.status()) + { + + case ORDER_STATUS_WORKING: + case ORDER_STATUS_PENDING_WORKING: + case ORDER_STATUS_DISCONNECTED: + 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(0); + + case ORDER_STATUS_INACTIVE: + case OrderStatus_INT_MIN_SENTINEL_DO_NOT_USE_: + case OrderStatus_INT_MAX_SENTINEL_DO_NOT_USE_: + bts_assert(false); + return Quantity(0); + } + bts_assert(false); + return Quantity(0); +} + +bool ICENativeOrder::terminal_state(api::orderman::OrderStatus status) const +{ + switch (status) + { + + case ORDER_STATUS_COMPLETED: + case ORDER_STATUS_CANCELED: + case ORDER_STATUS_ERROR: + case ORDER_STATUS_REJECTED_BY_SPARK: + return true; + + case ORDER_STATUS_DISCONNECTED: + case ORDER_STATUS_PENDING_WORKING: + case ORDER_STATUS_WORKING: + return false; + + case OrderStatus_INT_MIN_SENTINEL_DO_NOT_USE_: + case OrderStatus_INT_MAX_SENTINEL_DO_NOT_USE_: + case ORDER_STATUS_INACTIVE: + bts_assert(false); + return false; + break; + } + bts_assert(false); + return false; +} + +api::orderman::NativeOrderState& ICENativeOrder::get_native_state(api::orderman::OrderState &st) +{ + switch (st.custom_case()) + { + case OrderState::kMarket: + return *st.mutable_market()->mutable_native_state(); + case OrderState::kLimit: + return *st.mutable_limit()->mutable_native_state(); + case OrderState::kStop: + return *st.mutable_stop()->mutable_native_state(); + case OrderState::kStopLimit: + return *st.mutable_stop_limit()->mutable_native_state(); + default: + break; + } + + // just to appease compiler + bts_assert(false); + return *st.mutable_stop_limit()->mutable_native_state(); +} + +void ICENativeOrder::ignore_or_cancel(std::string const &msg, api::orderman::OrderError error, OrderOpResult *out) +{ + if (parent_key() == null_order_key()) + transition(me->state.status(), msg); + else + { + me->state.set_last_msg(msg); + on_cancel(); + } + *out = {error, msg}; +} + +} // namespace orderman +} // namespace bts diff --git a/src/lib/orderman/orders/ICENativeOrder.h b/src/lib/orderman/orders/ICENativeOrder.h new file mode 100644 index 000000000..9a581a69a --- /dev/null +++ b/src/lib/orderman/orders/ICENativeOrder.h @@ -0,0 +1,83 @@ +#pragma once + +#include "lib/orderman/orders/NativeOrderBase.h" +#include "lib/tracing/PerfLog.h" +#include + +namespace bts { +namespace orderman { +struct RoutingRecord; +class NewExecutionReportEvent; +class CancelRejectEvent; +class GatewayStatusChangeEvent; +class ExecutionReport; + +class ICENativeOrder : public NativeOrderBase { + public: + ICENativeOrder(); + ~ICENativeOrder() override; + + const api::orderman::OrderDefinition& definition() const override; + void on_init_new(const api::orderman::OrderDefinition&, + const PreActivationOrderServices&) override; + void on_init_recovered(const api::orderman::OrderRecord&, + const PreActivationOrderServices &) override; + + OrderOpResult on_activate(Price, Quantity, OrderServices &) override; + OrderOpResult on_update(Price, Quantity) override; + OrderOpResult on_update(const api::orderman::Update&) override; + OrderOpResult on_detach() override; + OrderOpResult on_cancel() override; + void on_attached(const api::orderman::OrderDefinition &new_parent) override; + + api::orderman::OrderDefinition + on_init_from_exe_report(const ExecutionReport &, const PreActivationOrderServices &) override; + void ignore_or_cancel(const std::string&, api::orderman::OrderError, OrderOpResult* out); + + protected: + void warm_up_activate(OrderServicesData &, OrderServices &) override; + void init_recovered_common(const api::orderman::OrderRecord &, + const PreActivationOrderServices &); + void handle_buffered_state(const std::string& condition); + + + private: + struct PImpl; + std::unique_ptr me; + void on_exe_report(const NewExecutionReportEvent &); + void on_cancel_reject(const CancelRejectEvent &); + void on_gateway_status(const GatewayStatusChangeEvent &); + void report_state(); + OrderOpResult _on_activate(log::Level reject_level, + log::Level transition_level, Price p, Quantity q, + bts::orderman::OrderServices &services); + void init_fix(); + void add_exchange_order_id_to_fix(); + + void transition(log::Level, + api::orderman::OrderStatus new_state, + const std::string& msg); + void transition(api::orderman::OrderStatus new_state, + const std::string& msg); + void transition(api::orderman::OrderStatus new_state); + void transition_to_error(api::orderman::NativeOrderState::SubError sub_error, + const std::string& msg); + + void update_working_position(); + bool has_exchange_order_id(); + Quantity working_position() ; + bool is_old_exe_report(api::orderman::NativeOrderState &, + const NewExecutionReportEvent &) const; + bool terminal_state(api::orderman::OrderStatus status) const; + api::orderman::NativeOrderState & + get_native_state(api::orderman::OrderState &); + + int64_t get_new_clordid(); + int64_t get_clordid(uint64_t); + void setup_ids(const std::string& sender_compid); + void generate_update_ids(const std::string&); + void generate_cancel_ids(const std::string&); +}; + +} // namespace orderman +} // namespace bts diff --git a/src/lib/orderman/orders/NativeOrderBase.h b/src/lib/orderman/orders/NativeOrderBase.h new file mode 100644 index 000000000..158fb5424 --- /dev/null +++ b/src/lib/orderman/orders/NativeOrderBase.h @@ -0,0 +1,22 @@ +#pragma once + +#include "lib/api/services/OrderService.pb.h" +#include "lib/orderman/orders/Order.h" + +namespace bts { +namespace orderman { +class ExecutionReport; + +class NativeOrderBase : public Order { + public: + NativeOrderBase() = default; + ~NativeOrderBase() override {}; + + virtual api::orderman::OrderDefinition const &definition() const = 0; + api::orderman::OrderDefinition + virtual on_init_from_exe_report(ExecutionReport const &, const PreActivationOrderServices &) = 0; +}; + +} // namespace orderman +} // namespace bts + diff --git a/src/lib/orderman/orders/algo/IAlgoServices.h b/src/lib/orderman/orders/algo/IAlgoServices.h index 2222c90e1..77cf2450a 100644 --- a/src/lib/orderman/orders/algo/IAlgoServices.h +++ b/src/lib/orderman/orders/algo/IAlgoServices.h @@ -20,7 +20,7 @@ class IAlgoServices : public IBeforeLaunchAlgoServices { // Returns the filled quantity for this algo virtual Quantity total_fills() const = 0; - // Returns wheter the algo is in the process of shutting down (due + // Returns whether the algo is in the process of shutting down (due // to a cancel, an error, or just having completed its work). When // an algo is shutting down any requests to create, update or // cancel child orders will be ignored. diff --git a/src/lib/orderman/orders/algo/IBeforeLaunchAlgoServices.h b/src/lib/orderman/orders/algo/IBeforeLaunchAlgoServices.h index 3756971db..aec1f4ed2 100644 --- a/src/lib/orderman/orders/algo/IBeforeLaunchAlgoServices.h +++ b/src/lib/orderman/orders/algo/IBeforeLaunchAlgoServices.h @@ -21,18 +21,19 @@ class IBeforeLaunchAlgoServices { // definition()) virtual api::Security const &security() const = 0; - // The order key for this algo + // The unique order key for this algo virtual OrderKey order_key() const = 0; - // The parent key for this algo's parent (null_order_key() if the - // algo does not have a parent) + // The parent key for this algo's parent or null_order_key() if the + // algo does not have a parent virtual OrderKey parent_order_key() const = 0; - // The security identified by k or nullptr if no such security - // exists + // The security identified by k or nullptr if no such security exists virtual api::Security const *lookup_security(SecurityKey k) const = 0; - // Wheter this algo is being attached to an existing order + // Whether this algo is being attached to an existing order + // If an algo does not support attaching it can return an error + // if is_attaching() == true from do_before_launch. virtual bool is_attaching() const = 0; // Build a child template that can later can be used to quickly @@ -44,6 +45,7 @@ class IBeforeLaunchAlgoServices { make_child_template(api::orderman::OrderDefinition const &child_def, size_t reserve = 1) = 0; + // Get the current time virtual TimeStamp get_time() const; }; diff --git a/src/lib/orderman/orders/algo/algo_orders/PyramidAlgo.cpp b/src/lib/orderman/orders/algo/algo_orders/PyramidAlgo.cpp new file mode 100644 index 000000000..09facf535 --- /dev/null +++ b/src/lib/orderman/orders/algo/algo_orders/PyramidAlgo.cpp @@ -0,0 +1,569 @@ +#include "lib/orderman/orders/algo/algo_orders/PyramidAlgo.h" + +#include "lib/domain_model/Security.h" +#include "lib/orderman/order_factory/IOrderFactory.h" +#include "lib/orderman/orderman_utils/Book.h" +#include "lib/orderman/orderman_utils/orderman_utils.h" +#include "lib/orderman/orders/algo/IAlgoServices.h" +#include "lib/orderman/orders/spreader_orders/spreader/SpreaderUtils.h" +#include "lib/os/os.h" +#include "lib/domain_model/Security.h" +#include "lib/utils/PBUtils.h" +#include + +namespace bts { +namespace orderman { + +using namespace api::orderman; + +enum class PyramidAlgo::Status { WAITING_FOR_MD, WAITING_FOR_MD_ATTACH, WORKING, DONE }; +enum class PyramidAlgo::ExitPath { CANCEL, ERR, COMPLETE }; + +struct LegData{ + const size_t index; + const SecurityKey sec_key; + const double tick_size; + const api::Side side; //trading side + ChildTemplate* order_template = nullptr; + OrderKey order_key; + ChildState order_state; + bool recv_book; + Book book; + Quantity top_delete; // this will be DONTCARE for covers + Quantity cover_payup;// this will be DONTCARE for top + LegData (size_t index_a, + SecurityKey sec_key_a, + double tick_size_a, + api::Side side_a, + ChildTemplate* order_template_a, + Quantity qty) + : + index (index_a), + sec_key (sec_key_a), + tick_size(tick_size_a), + side (side_a), + order_template (order_template_a), + order_key(null_order_key()), + order_state(), + recv_book(false), + book(), + top_delete(index == 0 ? qty.value() : 0), + cover_payup(index == 0 ? 0 : qty.value()) + { + } + const BookSide& opp_md() const{ + return book.side(SpreaderUtils::invert_side(side)); + } + bool is_top_spread() const{ + return index == 0; + } + void set_book (const Book& book_a){ + recv_book = true; + book = book_a; + } +}; + +struct PyramidAlgo::PImpl { + Status state = Status::WAITING_FOR_MD; + IAlgoServices* algo_services = nullptr; + IBeforeLaunchAlgoServices* bl_algo_services; + PyramidAlgo::PyramidAlgoParams params; + std::vector legs; + api::Side side; + Price out_price; + Quantity out_qty; + PImpl(){ + } + LegData* find_leg_from_sec (SecurityKey sec_key){ + for (auto& l : legs) + if(l.sec_key == sec_key) + return &l; + return nullptr; + } + LegData* find_leg_from_ord_key (OrderKey ord_key){ + for (auto& l : legs) + if(l.order_key == ord_key) + return &l; + return nullptr; + } +}; + +PyramidAlgo::PyramidAlgo() : me(new PImpl()) {} +PyramidAlgo::~PyramidAlgo() {} +IAlgoServices& PyramidAlgo::_algo() const { return *me->algo_services; } + +// initialization ///////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////// +OrderOpResult PyramidAlgo::do_before_launch(IBeforeLaunchAlgoServices& serv) { + me->bl_algo_services = &serv; + auto& def = serv.definition(); + if (def.has_custom_def() && def.custom_def().has_dynamic()){ + auto conv_res = _get_creation_params(def.custom_def(), serv); + if (std::get<1>(conv_res)){ + // params are good + me->params = std::get<0>(conv_res); + me->side = def.side(); + // top spread + auto* t = _create_top_order_template(serv); + double tick_size = serv.lookup_security(me->params.top_spread)->contract_spec().low_tick(); + me->legs.emplace_back(0, me->params.top_spread, tick_size, def.side(), t, + me->params.top_delete); + // covers + size_t i = 0; + for (SecurityKey key : me->params.cover_spreads){ + auto side = SpreaderUtils::invert_side(def.side()); + auto* t = _create_hedge_order_template(serv,i); + double tick_size = serv.lookup_security(key)->contract_spec().low_tick(); + me->legs.emplace_back(i+1, key, tick_size, side, t, me->params.cover_payups[i]); + i++; + } + } + else + return {OrderError::ORDER_ERROR_INVALID_PARAMETER, + std::get<2>(conv_res)}; + } + else + return {OrderError::ORDER_ERROR_INVALID_PARAMETER, + std::string("Missing parameters")}; + return {}; +} +void PyramidAlgo::do_launch(IAlgoServices& algo_services) { + me->algo_services = &algo_services; + me->out_price = algo_services.price(); + me->out_qty = algo_services.quantity(); + for (const LegData& l : me->legs) + algo_services.subscribe_book(l.sec_key); +} +void PyramidAlgo::do_launch_attached(IAlgoServices& algo_services, + OrderKey key, + ChildState c) { + me->algo_services = &algo_services; + if(c.quantity != algo_services.quantity() || + !bts::securities::is_same(c.price, algo_services.price())){ + _cxl_and_end(ExitPath::ERR, + "Pyramid price or quantity did not match the working order", + true); + } + else{ + me->legs[0].order_key = key; + me->out_price = c.price; + me->out_qty = c.quantity; + for (const LegData& l : me->legs) + algo_services.subscribe_book(l.sec_key); + _switch_state(PyramidAlgo::Status::WAITING_FOR_MD_ATTACH); + on_child_change(key,c); + } +} +// updates //////////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////// +void PyramidAlgo::do_update(const api::orderman::Update& update) { + LOG(OrderMan, Diag, "PyramidAlgo::do_update order:{} state:{} " + "receiving update: {}", + _algo().order_key().value(), + static_cast(me->state), + utils::pb::to_string(update)); + auto conv_res = _get_update_params(update, me->params); + if (!std::get<1>(conv_res)){ + LOG(OrderMan, Warn, "PyramidAlgo::do_update order:{} bad update, ignoring", + _algo().order_key().value()); + return; + } + const PyramidAlgoParams& algo_params = std::get<0>(conv_res); + Price price = Price(update.price()); + Quantity qty = Quantity(update.quantity()); + switch(me->state){ + case PyramidAlgo::Status::WAITING_FOR_MD: + case PyramidAlgo::Status::WAITING_FOR_MD_ATTACH: + LOG(OrderMan, Info, "PyramidAlgo::do_update order:{} state:{} ignored", + _algo().order_key().value(),static_cast(me->state)); + break; + case PyramidAlgo::Status::WORKING:{ + bool pq_changed = _copy_update(algo_params, price, qty); + if(_target_reached()){ + _cxl_and_end (PyramidAlgo::ExitPath::CANCEL, "Target reached"); + return; + } + if (_working_state_met()){ + if(pq_changed) + _modify_top_spread_order(); + } + else + _cxl_and_end(PyramidAlgo::ExitPath::CANCEL, + "Order cancelled (Cover prices not met)"); + break; + } + case PyramidAlgo::Status::DONE: + break; + } +} +// callbacks ////////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////// +void PyramidAlgo::on_book(SecurityKey key, const Book& book) { + LegData* leg_data = me->find_leg_from_sec(key); + if(leg_data != nullptr){ + leg_data->set_book(book); + _analyze_md_and_act(); + } +} +// this only gets called on am explicit algo cancel +// from the outside +IAlgoOrder::OnCancelResponse PyramidAlgo::on_cancel(){ + // cancel top spread, let hedges work, cancel algo + switch(me->state){ + case PyramidAlgo::Status::WAITING_FOR_MD: + case PyramidAlgo::Status::WAITING_FOR_MD_ATTACH: + case PyramidAlgo::Status::WORKING: + _cxl_and_end(PyramidAlgo::ExitPath::CANCEL,"Algo cancelled"); + break; + case PyramidAlgo::Status::DONE: + break; + } + return IAlgoOrder::OnCancelResponse::IGNORE; +} +void PyramidAlgo::on_child_change(OrderKey key, const ChildState &st) { + LOG(OrderMan, Info, + "PyramidAlgo::on_child_change key:{} child_key:{} state:{} status:{} " + "total_fills:{} last_price:{}", + _algo().order_key(), + key, + static_cast(me->state), + static_cast(st.status), + st.total_fills, + st.last_price); + LegData* leg = me->find_leg_from_ord_key (key); + if (me->legs[0].order_key == key){ + switch(me->state){ + case PyramidAlgo::Status::WAITING_FOR_MD: + bts_assert_msg (false, "No child orders"); + break; + case PyramidAlgo::Status::WAITING_FOR_MD_ATTACH: + leg->order_state = st; + _algo().update_fill_state(st.total_fills, st.avg_price); + _check_for_completion(); + break; + case PyramidAlgo::Status::WORKING: + leg->order_state = st; + _algo().update_fill_state(st.total_fills, st.avg_price); + _send_hedges(st.last_fills); + _check_for_completion(); + break; + case PyramidAlgo::Status::DONE: + break; + } + } +} + +/////////////////////////////////////////////////////////////////////////////// +// stateless conversion /////////////////////////////////////////////////////// + +PyramidAlgo::PyramidAlgoParams PyramidAlgo::_convert_params ( + const CustomOrderDefinition& cod){ + const DynamicOrderDefinition& def = cod.dynamic(); + const auto& params = def.order_params(); + PyramidAlgoParams rv; + // top contract + auto r = get_if(params, "Top Spread"); + rv.top_spread = r.has_value() ? SecurityKey(r.value()) : SecurityKey(0); + // top delete + r = get_if(params, "Delete"); + rv.top_delete = Quantity(r.has_value() ? r.value() : UINT32_MAX); + for (size_t i = 0; i < 5; i++){ + auto idx = std::to_string(i+1); + auto b = get_if(params, "Spread " + idx); + if (b.has_value() && b.value() != 0){ + rv.cover_spreads.emplace_back(b.value()); + auto pu = get_if(params, "Spread " + idx + " Payup"); + rv.cover_payups.emplace_back(pu.has_value() ? pu.value() : UINT32_MAX); + } + } + return rv; +} + +std::tuple +PyramidAlgo::_get_creation_params(const CustomOrderDefinition& cod, + IBeforeLaunchAlgoServices& serv){ + PyramidAlgo::PyramidAlgoParams rv = _convert_params(cod); + // error checking + if(rv.top_spread.value() != serv.security().id()) + return std::make_tuple(rv,false, + "Algo can only be launched from the Top Spread"); + if(rv.cover_spreads.size() < 2) + return std::make_tuple(rv,false, + "Not enough cover spreads"); + if (rv.cover_spreads[0].value() == 0 || + rv.cover_spreads[1].value() == 0) + return std::make_tuple(rv,false, + "Not enough cover spreads"); + return std::make_tuple(rv,true,std::string("")); +} + +std::tuple +PyramidAlgo::_get_update_params(const Update& update, + const PyramidAlgoParams& curr_params){ + if(update.has_custom() && update.custom().has_dynamic()){ + // means there is a params change + PyramidAlgoParams new_params = _convert_params(update.custom()); + if(new_params.top_spread != curr_params.top_spread || + new_params.cover_spreads.size() != curr_params.cover_spreads.size()) + return std::make_tuple(new_params,false,"Contracts can not be updated"); + for (size_t i=0; i < curr_params.cover_spreads.size(); i++) + if(new_params.cover_spreads[i] != curr_params.cover_spreads[i]) + return std::make_tuple(new_params,false,"Contracts can not be updated"); + return std::make_tuple(new_params,true,std::string("")); + } + else + return std::make_tuple(curr_params,true,std::string("")); +} + +/////////////////////////////////////////////////////////////////////////////// +// order template creation///////////////////////////////////////////////////// + +ChildTemplate* PyramidAlgo::_create_top_order_template ( + IBeforeLaunchAlgoServices& serv) const{ + OrderDefinition order_def = serv.definition(); + order_def.set_security_key (me->params.top_spread.value()); + order_def.set_side(me->side); + order_def.mutable_custom_def()->mutable_limit()->MergeFrom ( + LimitOrderDefinition()); + return &(serv.make_child_template(order_def)); +} + +ChildTemplate* PyramidAlgo::_create_hedge_order_template ( + IBeforeLaunchAlgoServices& serv, + size_t hedge_idx) const{ + OrderDefinition order_def = serv.definition(); + order_def.set_attach_target_order_key(null_order_key().value()); + order_def.set_security_key (me->params.cover_spreads[hedge_idx].value()); + order_def.set_side(SpreaderUtils::invert_side(me->side)); + order_def.set_sub_type(ORDER_SUB_TYPE_HEDGING_LEG); + LeanOrderDefinition lean; + lean.mutable_cross_threshold()->set_absolute(me->params.cover_payups[hedge_idx].value()); + lean.set_max_chase_ticks(1); + lean.set_trade_toward_behavior(LeanOrderDefinition::NO_CHANGE); + lean.mutable_child()->mutable_limit()->MergeFrom ( + LimitOrderDefinition()); + order_def.mutable_custom_def()->mutable_lean()->MergeFrom (lean); + return &(serv.make_child_template(order_def)); +} + +void PyramidAlgo::_analyze_md_and_act(){ + switch(me->state){ + case PyramidAlgo::Status::WAITING_FOR_MD: + if(!_md_recvd()) + return; + // received data for the first time + if (_working_state_met()){ + // enter order, and switch state + LegData& top_leg = me->legs[0]; + // this is the only place top spread order is sent (once) + top_leg.order_key = _algo().launch_child( + *top_leg.order_template, + me->out_price, + me->out_qty); + LOG(OrderMan, Info,"PyramidAlgo::_analyze_md_and_act launching order key:{} " + "child_key:{} price:{} qty:{}", + _algo().order_key(), + top_leg.order_key, + me->out_price, + me->out_qty); + _switch_state(PyramidAlgo::Status::WORKING); + } + else + _cxl_and_end(PyramidAlgo::ExitPath::CANCEL, + "Order cancelled (Cover prices not met)"); + break; + case PyramidAlgo::Status::WAITING_FOR_MD_ATTACH: + if(!_md_recvd()) + return; + // received data for the first time + if (_working_state_met()) + _switch_state(PyramidAlgo::Status::WORKING); + else + _cxl_and_end(PyramidAlgo::ExitPath::CANCEL, + "Order detached (Cover prices not met)", + true); + break; + case PyramidAlgo::Status::WORKING: + if (!_working_state_met()) + _cxl_and_end(PyramidAlgo::ExitPath::CANCEL, + "Order cancelled (Cover prices not met)"); + break; + case PyramidAlgo::Status::DONE: + break; + } +} + +void PyramidAlgo::_switch_state(Status new_state){ + switch(new_state){ + case (PyramidAlgo::Status::WAITING_FOR_MD): + bts_assert(false); + break; + case (PyramidAlgo::Status::WAITING_FOR_MD_ATTACH): + bts_assert(me->state == PyramidAlgo::Status::WAITING_FOR_MD); + break; + case (PyramidAlgo::Status::WORKING): + bts_assert(me->state == PyramidAlgo::Status::WAITING_FOR_MD || + me->state == PyramidAlgo::Status::WAITING_FOR_MD_ATTACH); + break; + case (PyramidAlgo::Status::DONE): + bts_assert( + // when an order is entered and the criteria is not met + me->state == PyramidAlgo::Status::WAITING_FOR_MD || + // when an order is attached and the market conditions are not met + me->state == PyramidAlgo::Status::WAITING_FOR_MD_ATTACH || + // when the child order gets into an unexpected state + me->state == PyramidAlgo::Status::WORKING); + break; + } + LOG(OrderMan, Info,"PyramidAlgo::_switch_state key:{} {}->{} ", + _algo().order_key(), + static_cast(me->state), + static_cast(new_state)); + me->state = new_state; +} + +void PyramidAlgo::_send_hedges (Quantity filled_qty){ + if (filled_qty.value() > 0) + for (auto& l : me->legs) + if(!l.is_top_spread()){ + const auto& inside_md = l.opp_md()[0]; + Price hedge_price = Price(inside_md.price()); + uint32_t inside_md_qty = inside_md.size(); + if (l.cover_payup.value() != 0 && + inside_md_qty > l.cover_payup.value()) + hedge_price = securities::worsen_price(l.side, hedge_price, Price(l.tick_size)); + // check payup + l.order_key = _algo().launch_child( + *l.order_template, + hedge_price, + filled_qty); + _algo().detach_child(l.order_key); + } +} + +void PyramidAlgo::_modify_top_spread_order(){ + Update child_update; + child_update.set_price(me->out_price.value()); + child_update.set_quantity(me->out_qty.value()); + LOG(OrderMan, Info,"PyramidAlgo::_modify_top_spread_order modifying order key:{} " + "child_key:{} price:{} qty:{}", + _algo().order_key(), + me->legs[0].order_key, + me->out_price, + me->out_qty); + _algo().update_child(me->legs[0].order_key, child_update); +} +// copies the params, price and qty to the state +// retuns true is the price or qty has changed +bool PyramidAlgo::_copy_update(const PyramidAlgoParams& algo_params, + Price price, Quantity qty){ + // copy input params (which are stored seperately) + me->params.top_delete = algo_params.top_delete; + size_t cov_idx = 0; + for (Quantity payup : algo_params.cover_payups) + me->params.cover_payups[cov_idx++] = payup; + // copy leg data + cov_idx = 0; + me->legs[0].top_delete = algo_params.top_delete; + for (Quantity pu : algo_params.cover_payups){ + LegData& cover_leg = me->legs[cov_idx+1]; + if(cover_leg.cover_payup != pu){ + cover_leg.cover_payup = pu; + cover_leg.order_template = _create_hedge_order_template( + *me->bl_algo_services, + cov_idx); + } + cov_idx++; + } + // copy price and qty (returns true is they have changed) + bool pq_changed = me->out_qty.value() != qty.value() || + !bts::securities::is_same(me->out_price, price); + me->out_price = price; + me->out_qty = qty; + return pq_changed; +} +// if the top spread order is in a final state, the algo +// algo goes to the final state +void PyramidAlgo::_check_for_completion(){ + const ChildState& top_ord = me->legs[0].order_state; + switch (top_ord.status){ + case (ChildStatus::WORKING): + break; + case (ChildStatus::COMPLETED): + _cxl_and_end(PyramidAlgo::ExitPath::ERR,""); + break; + case (ChildStatus::CANCELED): + _cxl_and_end(PyramidAlgo::ExitPath::ERR, + "Top spread unexpectedly cancelled"); + break; + } +} + +void PyramidAlgo::_cxl_and_end(PyramidAlgo::ExitPath exit_path, + const std::string& msg, + bool detach_top){ + if(me->legs[0].order_key != null_order_key()){ + const ChildState& top_ord_state = me->legs[0].order_state; + if(top_ord_state.status == ChildStatus::WORKING){ + if(detach_top) + _algo().detach_child(me->legs[0].order_key); + else + _algo().cancel_child(me->legs[0].order_key); + } + } + switch(exit_path){ + case PyramidAlgo::ExitPath::ERR: + _algo().terminate_error(msg); + break; + case PyramidAlgo::ExitPath::CANCEL: + _algo().cancel(msg); + break; + case PyramidAlgo::ExitPath::COMPLETE: + _algo().complete(); + break; + } + _switch_state(PyramidAlgo::Status::DONE); +} +// true the out qty is changed it be below +// the filled qty +bool PyramidAlgo::_target_reached() const{ + return me->legs[0].order_state.total_fills.value() >= + me->out_qty.value(); +} + +bool PyramidAlgo::_md_recvd() const{ + for (const LegData& l : me->legs) + if(!l.recv_book) + return false; + return true; +} + +bool PyramidAlgo::_working_state_met() const{ + double cover_sum = 0.0; + size_t i = 0; + for (const auto& l : me->legs) + if (i++ > 0) + cover_sum += l.opp_md()[0].price(); + bool rv = !os::is_nan(cover_sum) && + ((me->side == api::Side::SIDE_BUY && + me->out_price.value() <= cover_sum) || + (me->side == api::Side::SIDE_SELL && + me->out_price.value() >= cover_sum)); + if (!rv && !os::is_nan(cover_sum)){ + // price condition is not met, but if the + // cum qty at the top leg price is > threashold + // then the condition is met () + const auto& top_leg = me->legs[0]; + Quantity cumm_level_qty; + SpreaderUtils::find_qty_upto_price_level(top_leg.book, + top_leg.side, + Price(me->out_price), + 0.0, + cumm_level_qty); + rv = cumm_level_qty.value() > top_leg.top_delete.value(); + } + return rv; +} + +} // namespace orderman +} // namespace bts diff --git a/src/lib/orderman/orders/algo/algo_orders/PyramidAlgo.h b/src/lib/orderman/orders/algo/algo_orders/PyramidAlgo.h new file mode 100644 index 000000000..c24ea468f --- /dev/null +++ b/src/lib/orderman/orders/algo/algo_orders/PyramidAlgo.h @@ -0,0 +1,74 @@ +#pragma once + +#include "lib/orderman/orders/algo/IAlgoOrder.h" + +namespace bts { +namespace orderman { + +class ChildTemplate; +class LegData; + +class PyramidAlgo : public IAlgoOrder { +public: + PyramidAlgo(); + ~PyramidAlgo(); + struct PyramidAlgoParams{ + // first one is the top + SecurityKey top_spread; + Quantity top_delete; + std::vector cover_spreads; + std::vector cover_payups; + }; +public: + OrderOpResult do_before_launch(IBeforeLaunchAlgoServices &) override; + void do_launch(IAlgoServices &) override; + void do_launch_attached(IAlgoServices &, OrderKey, ChildState) override; + void do_update(api::orderman::Update const &) override; + void on_book(SecurityKey, Book const &) override; + void on_child_change(OrderKey, ChildState const &) override; + IAlgoOrder::OnCancelResponse on_cancel() override; + +private: + enum class Status; + enum class ExitPath; + + struct PImpl; + std::unique_ptr me; +private: + IAlgoServices& _algo() const ; + // converts parameters from the CustomOrderDefinition::dynamic + // to PyramidAlgoParams + static PyramidAlgoParams _convert_params ( + const bts::api::orderman::CustomOrderDefinition&); + static std::tuple _get_creation_params( + const bts::api::orderman::CustomOrderDefinition&, + IBeforeLaunchAlgoServices&); + static std::tuple _get_update_params( + const api::orderman::Update&, + const PyramidAlgoParams& curr_params); + ChildTemplate* _create_top_order_template (IBeforeLaunchAlgoServices&) const; + ChildTemplate* _create_hedge_order_template (IBeforeLaunchAlgoServices&, + size_t) const; + // acts on md, cognizant of the state + void _analyze_md_and_act(); + void _switch_state (Status new_status); + void _send_hedges (Quantity filled_qty); + void _modify_top_spread_order(); + // returns true is the price, qty changes + bool _copy_update(const PyramidAlgoParams& algo_params, + Price price, Quantity qty); + void _check_for_completion(); + // cxl's the working top leg order (if any), detaches + // all hedges. if is_error then terminates algo, is + // is_cxl then cxl's algo, otherwise completes it + void _cxl_and_end(ExitPath exit_path, + const std::string& err_txt, + bool detach_top = false); + bool _target_reached() const; + // true is all legs have recvd md + bool _md_recvd() const; + bool _working_state_met() const; +}; + +} // namespace orderman +} // namespace bts diff --git a/src/lib/orderman/orders/algo/algo_orders/RegisterDorman.cpp b/src/lib/orderman/orders/algo/algo_orders/RegisterDorman.cpp index 0125d4e25..81c5dbcd4 100644 --- a/src/lib/orderman/orders/algo/algo_orders/RegisterDorman.cpp +++ b/src/lib/orderman/orders/algo/algo_orders/RegisterDorman.cpp @@ -3,7 +3,11 @@ #include "lib/api/services/OrderService.pb.h" #include "lib/logging/logging.h" #include "lib/orderman/orderman_utils/orderman_utils.h" +#include "lib/config/protos/toggles.pb.h" +#include "lib/config/config.h" #include "DormanAlgo.h" +#include "PyramidAlgo.h" +#include "TASAlgo.h" using namespace bts; using namespace api::orderman; @@ -11,7 +15,7 @@ using namespace orderman::dorman; using namespace orderman; namespace bts { -namespace orderman { +namespace orderman { namespace dorman { template @@ -83,11 +87,14 @@ Result to_dorman_params(const api::orderman::DynamicOr return {res}; } -extern "C" void init_spark_plugin(bts::SparkServices &srv) { +extern "C" void init_dorman_plugin(bts::SparkServices &srv) { DynamicOrderDescriptor join_desc; join_desc.set_dynamic_order_type("Dorman"); + *join_desc.add_param_desc() = param_array("what", param_double("").param_type()); + + *join_desc.add_param_desc() = param_uint32("Market Quantity 1"); *join_desc.add_param_desc() = param_uint32("Order Quantity 1"); *join_desc.add_param_desc() = param_uint32("Market Quantity 2"); @@ -115,4 +122,55 @@ extern "C" void init_spark_plugin(bts::SparkServices &srv) { srv.register_dynamic_algo_order(join_desc, []() { return std::make_unique(); }); } +extern "C" void init_pyramid_plugin(bts::SparkServices& srv) { + DynamicOrderDescriptor join_desc; + + join_desc.set_dynamic_order_type("Pyramid"); + + *join_desc.add_param_desc() = param_security_key("Top Spread"); + *join_desc.add_param_desc() = param_uint32("Delete"); + *join_desc.add_param_desc() = param_security_key("Spread 1"); + *join_desc.add_param_desc() = param_uint32("Spread 1 Payup"); + *join_desc.add_param_desc() = param_order("Spread 1 Order"); + *join_desc.add_param_desc() = param_security_key("Spread 2"); + *join_desc.add_param_desc() = param_uint32("Spread 2 Payup"); + *join_desc.add_param_desc() = param_order("Spread 2 Order"); + *join_desc.add_param_desc() = param_security_key("Spread 3"); + *join_desc.add_param_desc() = param_uint32("Spread 3 Payup"); + *join_desc.add_param_desc() = param_order("Spread 3 Order"); + *join_desc.add_param_desc() = param_security_key("Spread 4"); + *join_desc.add_param_desc() = param_uint32("Spread 4 Payup"); + *join_desc.add_param_desc() = param_order("Spread 4 Order"); + *join_desc.add_param_desc() = param_security_key("Spread 5"); + *join_desc.add_param_desc() = param_uint32("Spread 5 Payup"); + *join_desc.add_param_desc() = param_order("Spread 5 Order"); + + srv.register_dynamic_algo_order(join_desc, []() { return std::make_unique(); }); +} + +extern "C" void init_tas_plugin(bts::SparkServices& srv) { + DynamicOrderDescriptor tas_desc; + + tas_desc.set_dynamic_order_type("TAS"); + + size_t n_child_ords = 8; + for (size_t i=0; i< n_child_ords; i++){ + auto idx = std::to_string(i+1); + *tas_desc.add_param_desc() = param_double("Order " + idx + " Price"); + *tas_desc.add_param_desc() = param_uint32("Order " + idx + " Quantity"); + *tas_desc.add_param_desc() = param_bool("Order " + idx + " Buy?"); + } + srv.register_dynamic_algo_order(tas_desc, []() { return std::make_unique(); }); +} + +extern "C" void init_spark_plugin(bts::SparkServices &srv) { + auto& dyn_algo_config = config::get(); + if(dyn_algo_config.enable_dorman()) + init_dorman_plugin(srv); + if(dyn_algo_config.enable_pyramid()) + init_pyramid_plugin(srv); + if(dyn_algo_config.enable_tas()) + init_tas_plugin(srv); +} + }}} \ No newline at end of file diff --git a/src/lib/orderman/orders/algo/algo_orders/TASAlgo.cpp b/src/lib/orderman/orders/algo/algo_orders/TASAlgo.cpp new file mode 100644 index 000000000..f167cdca0 --- /dev/null +++ b/src/lib/orderman/orders/algo/algo_orders/TASAlgo.cpp @@ -0,0 +1,222 @@ +#include "lib/orderman/orders/algo/algo_orders/TASAlgo.h" + +#include "lib/domain_model/Security.h" +#include "lib/orderman/order_factory/IOrderFactory.h" +#include "lib/orderman/orderman_utils/Book.h" +#include "lib/orderman/orderman_utils/orderman_utils.h" +#include "lib/orderman/orders/algo/IAlgoServices.h" +#include "lib/orderman/orders/spreader_orders/spreader/SpreaderUtils.h" +#include "lib/os/os.h" +#include "lib/domain_model/Security.h" +#include "lib/utils/PBUtils.h" +#include "lib/utils/datetime.h" +#include + +namespace bts { +namespace orderman { + +using namespace api::orderman; + +enum class TASAlgo::Status { WAITING_FOR_SIGNAL, WORKING, DONE }; + +struct TASLegData{ + api::Side side; //trading side + Price price; + Quantity quantity; + ChildTemplate* order_template = nullptr; + OrderKey order_key; + ChildState order_state; + TASLegData (api::Side side, + Price price, + Quantity quantity, + ChildTemplate* order_template) + : + side (side), + price(price), + quantity(quantity), + order_template (order_template), + order_key(null_order_key()), + order_state(){ + order_state.status = ChildStatus::WORKING; + } +}; + +struct TASAlgo::PImpl { + Status state = Status::WAITING_FOR_SIGNAL; + IAlgoServices* algo_services = nullptr; + SecurityKey sec_key = SecurityKey(0); + TASAlgo::TASAlgoParams algo_params; + std::vector legs; + bts::datetime::PTime start_time = bts::datetime::local_now(); + PImpl(){ + } + TASLegData* find_leg_from_ord_key (OrderKey ord_key){ + for (auto& l : legs) + if(l.order_key == ord_key) + return &l; + return nullptr; + } +}; + +TASAlgo::TASAlgo() : me(new PImpl()) {} +TASAlgo::~TASAlgo() {} +// initialization ///////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////// +OrderOpResult TASAlgo::do_before_launch(IBeforeLaunchAlgoServices& serv) { + auto& def = serv.definition(); + if (def.has_custom_def() && def.custom_def().has_dynamic()){ + const auto& sec = serv.security(); + me->sec_key = SecurityKey(sec.id()); + me->algo_params = _convert_params(def.custom_def()); + if (me->algo_params.child_orders.size() > 0){ + for (const auto& t : me->algo_params.child_orders){ + Price price = std::get<0>(t); + Quantity quantity = std::get<1>(t); + api::Side side = std::get<2>(t); + auto* ordt = _create_order_template( + serv, + SecurityKey(sec.id()), + side); + me->legs.emplace_back(side, + price, + quantity, + ordt); + } + return {}; + } + } + return {OrderError::ORDER_ERROR_INVALID_PARAMETER, + std::string("Parameters are not well formed")}; +} +void TASAlgo::do_launch(IAlgoServices& algo_services) { + me->algo_services = &algo_services; + _algo().subscribe_status(me->sec_key); + //_algo().subscribe_timer(); +} +void TASAlgo::do_launch_attached(IAlgoServices& algo_services, + OrderKey, + ChildState) { + me->algo_services = &algo_services; + _cxl_and_end(true, "TAS Algo can not be attached to working orders"); +} +// updates //////////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////// +void TASAlgo::do_update(const api::orderman::Update&) { + LOG(OrderMan, Warn, "TASAlgo::do_update order:{} state:{} update not allowed ", + _algo().order_key().value(), + static_cast(me->state)); +} +// callbacks ////////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////// +void TASAlgo::on_book(SecurityKey, const Book&) { +} +void TASAlgo::on_child_change(OrderKey key, + ChildState const &st){ + LOG(OrderMan, Info, + "TASAlgo::on_child_change key:{} child_key:{} state:{} status:{} " + "total_fills:{} last_price:{}", + _algo().order_key(), + key, + static_cast(me->state), + static_cast(st.status), + st.total_fills, + st.last_price); + TASLegData* ld_ptr = me->find_leg_from_ord_key(key); + if (ld_ptr != nullptr) + ld_ptr->order_state = st; + for (const auto& l : me->legs) + if(l.order_state.status == ChildStatus::WORKING) + return; + _cxl_and_end(false,""); +} +void TASAlgo::on_status(SecurityKey key, co2::TradingStatus status){ + if(key == me->sec_key && + status == co2::TradingStatus::TRADINGSTATUS_PRE_OPEN){ + switch (me->state){ + case TASAlgo::Status::WAITING_FOR_SIGNAL: + for (auto& l : me->legs){ + l.order_key = _algo().launch_child( + *l.order_template, + l.price, + l.quantity); + LOG(OrderMan, Info,"TASAlgo::on_status launching order key:{} " + "child_key:{} price:{} qty:{}", + _algo().order_key(), + l.order_key, + l.price, + l.quantity); + } + _switch_state(TASAlgo::Status::WORKING); + break; + case TASAlgo::Status::WORKING: + case TASAlgo::Status::DONE: + break; + } + } +} +void TASAlgo::on_timer(){ + if (bts::datetime::msecs_since(me->start_time) > 10000) + on_status(me->sec_key,co2::TradingStatus::TRADINGSTATUS_PRE_OPEN); +} +IAlgoServices& TASAlgo::_algo() const { return *me->algo_services; } +/////////////////////////////////////////////////////////////////////////////// +// stateless conversion /////////////////////////////////////////////////////// +TASAlgo::TASAlgoParams TASAlgo::_convert_params ( + const bts::api::orderman::CustomOrderDefinition& cod){ + const DynamicOrderDefinition& def = cod.dynamic(); + const auto& params = def.order_params(); + TASAlgo::TASAlgoParams rv; + for (size_t i = 0; i < 1024; i++){ + auto idx = std::to_string(i+1); + auto price = get_if(params, "Order " + idx + " Price"); + auto qty = get_if(params, "Order " + idx + " Quantity"); + auto is_buy = get_if(params, "Order " + idx + " Buy?"); + if(price.has_value() && qty.has_value() && qty.value() != 0){ + api::Side side = is_buy.has_value() ? + (is_buy.value() ? api::Side::SIDE_BUY : api::Side::SIDE_SELL) : + api::Side::SIDE_SELL; + rv.child_orders.emplace_back(Price(price.value()), Quantity(qty.value()), side); + } + } + return rv; +} +ChildTemplate* TASAlgo::_create_order_template ( + IBeforeLaunchAlgoServices& serv, + SecurityKey key, + api::Side side){ + OrderDefinition order_def = serv.definition(); + order_def.set_security_key (key.value()); + order_def.set_side(side); + order_def.mutable_custom_def()->mutable_limit()->MergeFrom ( + api::orderman::LimitOrderDefinition()); + return &(serv.make_child_template(order_def)); +} +void TASAlgo::_switch_state(TASAlgo::Status new_state){ + switch (new_state){ + case (TASAlgo::Status::WAITING_FOR_SIGNAL): + bts_assert(false); + break; + case (TASAlgo::Status::WORKING): + bts_assert(me->state == TASAlgo::Status::WAITING_FOR_SIGNAL); + break; + case (TASAlgo::Status::DONE): + bts_assert(me->state != TASAlgo::Status::DONE); + break; + } + LOG(OrderMan, Info,"TASAlgo::_switch_state key:{} {}->{} ", + _algo().order_key(), + static_cast(me->state), + static_cast(new_state)); + me->state = new_state; +} + +void TASAlgo::_cxl_and_end (bool is_error, const std::string& err_msg){ + if(is_error) + _algo().terminate_error(err_msg); + else + _algo().complete(); + _switch_state(TASAlgo::Status::DONE); +} + +} // namespace orderman +} // namespace bts diff --git a/src/lib/orderman/orders/algo/algo_orders/TASAlgo.h b/src/lib/orderman/orders/algo/algo_orders/TASAlgo.h new file mode 100644 index 000000000..739b2e864 --- /dev/null +++ b/src/lib/orderman/orders/algo/algo_orders/TASAlgo.h @@ -0,0 +1,45 @@ +#pragma once + +#include "lib/orderman/orders/algo/IAlgoOrder.h" + +namespace bts { +namespace orderman { + +class ChildTemplate; +class LegData; + +class TASAlgo : public IAlgoOrder { + public: + TASAlgo(); + ~TASAlgo(); + struct TASAlgoParams{ + std::vector> child_orders; + }; + public: + OrderOpResult do_before_launch(IBeforeLaunchAlgoServices &) override; + void do_launch(IAlgoServices &) override; + void do_launch_attached(IAlgoServices &, OrderKey, ChildState) override; + void do_update(api::orderman::Update const &) override; + void on_book(SecurityKey, Book const &) override; + void on_child_change(OrderKey, ChildState const &) override; + void on_status(SecurityKey, co2::TradingStatus) override; + void on_timer() override; + + private: + enum class Status; + struct PImpl; + std::unique_ptr me; +private: + IAlgoServices& _algo() const ; + static TASAlgoParams _convert_params ( + const bts::api::orderman::CustomOrderDefinition&); + static ChildTemplate* _create_order_template (IBeforeLaunchAlgoServices&, + SecurityKey, + api::Side); + // acts on md, cognizant of the state + void _switch_state (Status new_status); + void _cxl_and_end (bool is_error, const std::string& err_msg); +}; + +} // namespace orderman +} // namespace bts diff --git a/src/lib/orderman/orders/spreader_orders/SpreaderOrder.cpp b/src/lib/orderman/orders/spreader_orders/SpreaderOrder.cpp index 32d552e99..262f70c87 100644 --- a/src/lib/orderman/orders/spreader_orders/SpreaderOrder.cpp +++ b/src/lib/orderman/orders/spreader_orders/SpreaderOrder.cpp @@ -463,6 +463,7 @@ void SpreaderOrder::_comp_and_report_state (){ reported_state.set_quantity(bookkeeping_data.quantity().value()); reported_state.set_filled_quantity(filled_qty.value()); reported_state.set_avg_price(avg_filled_price.value()); + reported_state.set_last_price(avg_filled_price.value()); } // if order is cons'ed from disc, with inited_new==false, // we just publish what was given to us diff --git a/src/lib/orderman/orders/spreader_orders/spreader/SpreaderUtils.h b/src/lib/orderman/orders/spreader_orders/spreader/SpreaderUtils.h index 0d34acfe2..683a75832 100644 --- a/src/lib/orderman/orders/spreader_orders/spreader/SpreaderUtils.h +++ b/src/lib/orderman/orders/spreader_orders/spreader/SpreaderUtils.h @@ -9,6 +9,7 @@ #include "lib/orderman/orders/spreader_orders/spreader/quoter/QuoteLegDriver.h" #include "lib/api/services/OrderService.pb.h" #include "lib/orderman/actors/order_manager/OrderBuilder.h" +#include "lib/os/os.h" #include namespace bts { @@ -238,11 +239,9 @@ public: } } // CRITICAL PATH (for hedging) - // travserses the book to locate the target_price, - // and finds the qty at the target price and the - // cum qty upto that price. - // returns false if the target_price is not found. - // this is used to find the qty and cumm_qty + // travserses the book to locate the target_price, + // and finds the cumm qty up to that level (last our arg) + // Returns false if the target_price is not found. static bool find_qty_upto_price_level (const Book& book, api::Side book_side, Price target_price, @@ -286,35 +285,66 @@ public: //////////////////////////////////////////////////// // quoting params ////////////////////////////////// //////////////////////////////////////////////////// - // given the new price, applies the quoting parameters (mute etc) - // to see if quote needs to be repriced - static bool reprice_quote_mute_zone(double current_price, - double new_price, - api::Side quote_side, - double tick_size, - const bts::spark::QuoteMuteZone& quote_mute_zone){ - int price_change_ticks = bts::securities::price_diff_to_ticks (current_price, new_price, tick_size); - if (price_change_ticks != 0){ - bool price_increasing = price_change_ticks > 0; - uint32_t abs_price_change_ticks = static_cast(std::abs(price_change_ticks)); - LOG(OrderMan, Diag, "SpreaderUtils::reprice_quote_mute_zone current_price: {} new_price: {} price_change_ticks: {} abs_price_change_ticks: {} inside_limit_ticks: {} outside_limit_ticks: {}", - current_price, new_price, price_change_ticks, abs_price_change_ticks, quote_mute_zone.inside_ticks(), quote_mute_zone.outside_ticks()); - if (quote_side == api::SIDE_BUY){ - if (price_increasing) // moving toward inside market - return abs_price_change_ticks > quote_mute_zone.inside_ticks(); - else - return abs_price_change_ticks > quote_mute_zone.outside_ticks(); - } - else{ - if (!price_increasing) // moving toward inside market - return abs_price_change_ticks > quote_mute_zone.inside_ticks(); - else - return abs_price_change_ticks > quote_mute_zone.outside_ticks(); - } - } - else - return false; + static bool is_throttle_reject(double current_price, + double new_price, + const Book& qbook, + api::Side quote_side, + double tick_size, + const bts::spark::QuoteParams& quote_params){ + + bool reject = + (quote_params.has_quote_mute_zone() && + is_quote_mute_zone_reject(current_price, + new_price, + quote_side, + tick_size, + quote_params.quote_mute_zone())) || + (quote_params.has_quote_inside_market_distance_throttle() && + is_quote_inside_market_reject(current_price, + new_price, + qbook.side(quote_side)[0].price(), + quote_side, + tick_size, + quote_params.quote_inside_market_distance_throttle())); + return reject; } + + // returns true is quote mute zone rejects the price change + static bool is_quote_mute_zone_reject(double current_price, + double new_price, + api::Side quote_side, + double tick_size, + const bts::spark::QuoteMuteZone& quote_mute_zone){ + int ticks_moved = bts::securities::price_diff_to_ticks(current_price, new_price, tick_size); + bool toward_inside = quote_side==api::Side::SIDE_BUY ? ticks_moved >=0 : ticks_moved <= 0; + uint32_t throttle = toward_inside ? quote_mute_zone.inside_ticks() : + quote_mute_zone.outside_ticks(); + uint32_t abs_ticks_moved = static_cast(std::abs(ticks_moved)); + // has to move outside of the zone + return abs_ticks_moved <= throttle; + } + // returns false if the param surpresses the quote + static bool is_quote_inside_market_reject( + double current_price, + double new_price, + double inside_market, + api::Side quote_side, + double tick_size, + const bts::spark::QuoteInsideMarketDistanceThrottle& imd){ + bool toward = bts::securities::is_equal_or_better(quote_side, + Price(new_price), + Price(current_price)); + if(toward && !os::is_nan(inside_market)){ + int new_order_diff_market = bts::securities::order_diff_market( + new_price, + inside_market, + quote_side, + tick_size); + return new_order_diff_market > static_cast(imd.on_toward_distance()); + } + return false; + } + static bool conditional_quoting(Price quoting_price, Quantity quoting_qty, size_t leg_idx, diff --git a/src/lib/orderman/orders/spreader_orders/spreader/quoter/quoter_states/QuoterWorkingStateWorker.cpp b/src/lib/orderman/orders/spreader_orders/spreader/quoter/quoter_states/QuoterWorkingStateWorker.cpp index 14c5d30f4..12ace9abf 100644 --- a/src/lib/orderman/orders/spreader_orders/spreader/quoter/quoter_states/QuoterWorkingStateWorker.cpp +++ b/src/lib/orderman/orders/spreader_orders/spreader/quoter/quoter_states/QuoterWorkingStateWorker.cpp @@ -73,19 +73,31 @@ void QuoterWorkingStateWorker::_longpath_analyze_and_act () } else{ // to adjust for what has already been filled - Quantity order_quantity = Quantity (computed_quantity.value() + _quoter->_price_quantity_state.aggregated_filled_quantity().value()); - if (order_quantity.value() != _quoter->_price_quantity_state.out_quantity().value()) + Quantity order_quantity = Quantity ( + computed_quantity.value() + + _quoter->_price_quantity_state.aggregated_filled_quantity().value() + ); + if (order_quantity != _quoter->_price_quantity_state.out_quantity()) // this is a size change, size changes always go through regardless of the // quoting parameters _modify_order (computed_price, order_quantity); else{ - bool reprice = true; - if (_quoter->_this_leg_data.leg_params().quote_params().has_quote_mute_zone()) - reprice = SpreaderUtils::reprice_quote_mute_zone(_quoter->_price_quantity_state.out_price().value(), computed_price.value(), _quoter->_this_leg_data.trading_side(), - _quoter->_this_leg_data.tick_size(), _quoter->_this_leg_data.leg_params().quote_params().quote_mute_zone()); - reprice=reprice && !(_quoter->_spreader.in_cache_warming_cycle()); - if (reprice) - _modify_order (computed_price, order_quantity); + Price curr_price = _quoter->_price_quantity_state.out_price(); + if(!bts::securities::is_same(curr_price,computed_price)){ + const Book& book = + _quoter->_spreader.get_book(_quoter->_this_leg_data.leg_index()); + bool throttle_reject = SpreaderUtils::is_throttle_reject( + curr_price.value(), + computed_price.value(), + book, + _quoter->_this_leg_data.trading_side(), + _quoter->_this_leg_data.tick_size(), + _quoter->_this_leg_data.leg_params().quote_params()); + bool reprice = !throttle_reject; + reprice = reprice && !(_quoter->_spreader.in_cache_warming_cycle()); + if (reprice) + _modify_order (computed_price, order_quantity); + } } } } diff --git a/src/lib/plugin/plugin.cpp b/src/lib/plugin/plugin.cpp index 5cab0e815..b988efabd 100644 --- a/src/lib/plugin/plugin.cpp +++ b/src/lib/plugin/plugin.cpp @@ -9,11 +9,14 @@ #include #include +extern "C" void init_spark_plugin(bts::SparkServices &serv); + namespace bts { namespace plugin { using namespace bts::orderman; + void init_plugins(SparkServices &services) { static std::vector( @@ -28,12 +31,15 @@ void init_plugins(SparkServices &services) { return; } + // initialize the plugins in libspark.so + init_spark_plugin(services); + boost::filesystem::directory_iterator end; for (auto iter = boost::filesystem::directory_iterator(plugin_path); iter != end; ++iter) { auto &f = *iter; - auto pl = f.path().string(); + auto pl = f.path().filename().string(); if (pl == "order_plugin_example.so" && config::get().type() != "dev") { @@ -43,6 +49,8 @@ void init_plugins(SparkServices &services) { continue; } + if (pl == "libspark.so") continue; + LOG(Plugin, Info, "Attempting to initialize plugin {}", pl); try { auto init_plugin = boost::dll::import( diff --git a/src/lib/sparkproto/SparkProtos.proto b/src/lib/sparkproto/SparkProtos.proto index d0f4c7675..e1b14e4fa 100644 --- a/src/lib/sparkproto/SparkProtos.proto +++ b/src/lib/sparkproto/SparkProtos.proto @@ -68,6 +68,7 @@ message QuoteParams QuoteMaxOrderMove quote_max_order_move = 1040; QuoteHidden quote_hidden = 1050; ConditionalQuoting conditional_quoting=1060; + QuoteInsideMarketDistanceThrottle quote_inside_market_distance_throttle = 1070; // default value 0, 1 means we will have one trailing // order behind the top level // (once we have ChildOrder's here, we can reture this @@ -129,6 +130,11 @@ message ConditionalQuoting{ PriceType price_type=10; } +message QuoteInsideMarketDistanceThrottle +{ + uint32 on_toward_distance = 10; +} + /*************************************Quote RepriceThrottled (unused)******************************* (Stellar's sensitivity) This parameter defines regions (wrt their distance from the inside market) @@ -174,7 +180,8 @@ THEN message QuoteInsideMarketDistance { RulePriority priority = 1; - uint32 ticks_from_inside = 10; + uint32 on_toward_distance = 10; + uint32 on_away_distance = 20; } /******************************Quote InsideMarketDistanceWithTravel****************************** diff --git a/src/lib/utils/AuthSessionManager.cpp b/src/lib/utils/AuthSessionManager.cpp index 8b1b74438..0c397d8f9 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, const admin::BTSUser& user, std::string* msg) +bool AuthSessionManager::add_or_replace_session(const api::user::TokenPayload& tok, const std::string& username, 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 {} user:{}:{} ", tok.id(), user.id(), user.name()); + LOG(User, Warn, "KEYCODE: Replacing session {} user:{} ", tok.id(), username); return true; } else if(_map.size() < _nusers) { _map[tok.id()] = Session{tok}; - LOG(User, Warn, "KEYCODE: Creating session {} user:{}:{}", tok.id(), user.id(), user.name()); + LOG(User, Warn, "KEYCODE: Creating session {} user:{}", tok.id(), username); return true; } if(msg) *msg = "KEYCODES: Not Enough Logins available for session"; - LOG(User, Error, "KEYCODES: Not Enough Logins available for session {} user:{}:{}", tok.id(), user.id(), user.name()); + LOG(User, Error, "KEYCODES: Not Enough Logins available for session {} user:{}", tok.id(), username); return false; } @@ -134,9 +134,18 @@ bool AuthSessionManager::check_session(const api::user::TokenPayload& tok) bool same_token = _concurrent || (tok.date() == it->second.token.date()); return not_expired && same_token; } + else //not in map, try to login in case server booted + { + return add_or_replace_session(tok); + } } return false; } + +void AuthSessionManager::clear_sessions() +{ + _map.clear(); +} }}} #endif \ No newline at end of file diff --git a/src/lib/utils/AuthSessionManager.h b/src/lib/utils/AuthSessionManager.h index fa68d097e..f9b1fa2be 100644 --- a/src/lib/utils/AuthSessionManager.h +++ b/src/lib/utils/AuthSessionManager.h @@ -19,8 +19,9 @@ class AuthSessionManager public: AuthSessionManager(); - bool add_or_replace_session(const api::user::TokenPayload& tok, const admin::BTSUser& user, std::string* msg = nullptr); + bool add_or_replace_session(const api::user::TokenPayload& tok, const std::string& username = "", std::string* msg = nullptr); bool check_session(const api::user::TokenPayload& tok); + void clear_sessions(); private: bool validate_ip_port(const bts::config::KeyCode& kc); diff --git a/src/lib/utils/AuthToken.cpp b/src/lib/utils/AuthToken.cpp index 9c062c592..cd9be38d6 100644 --- a/src/lib/utils/AuthToken.cpp +++ b/src/lib/utils/AuthToken.cpp @@ -51,6 +51,11 @@ AuthSessionManager& session_manager() return mgr; } +void clear_all_sessions() +{ + session_manager().clear_sessions(); +} + bool requires_auth_session(const admin::BTSUser& user) { //if the user can trade in any account or see market data, it needs a session @@ -76,6 +81,14 @@ bool requires_auth_session(const admin::BTSUser& user) return true; } } + if(user.user_data().exchange_user_data().has_ice_data()) + { + for(const auto& i: user.user_data().exchange_user_data().ice_data().md_permissions()) + { + if(i.enabled()) + return true; + } + } } return false; } @@ -89,7 +102,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, user, msg); + session_manager().add_or_replace_session(payload, user.name(), msg); auto token = encrypt(payload.SerializeAsString()); bts_assert_d(token.size() < 255-8); AuthToken result; diff --git a/src/lib/utils/AuthToken.h b/src/lib/utils/AuthToken.h index a85605a0b..e8dd1f700 100644 --- a/src/lib/utils/AuthToken.h +++ b/src/lib/utils/AuthToken.h @@ -10,6 +10,8 @@ namespace auth { AuthToken generate_token(const admin::BTSUser&, std::string* msg = nullptr); AuthToken generate_wheel_token(); bool decode_token(const api::AuthToken& token, user::TokenPayload* payload); +bool requires_auth_session(const admin::BTSUser& user); +void clear_all_sessions(); }}} diff --git a/src/lib/utils/Puid.cpp b/src/lib/utils/Puid.cpp index 2bb27e1a3..072a03065 100644 --- a/src/lib/utils/Puid.cpp +++ b/src/lib/utils/Puid.cpp @@ -1,3 +1,4 @@ +#ifndef _WIN32 #include "Puid.h" #include "../os/os.h" @@ -7,17 +8,17 @@ #include #include -#include -#include +#include #include #include -#include #include +#include +#include -#include #include -#include #include +#include +#include namespace bts { namespace utils { @@ -35,93 +36,99 @@ const std::string counter_file_name = "counter_file"; // relying on the fact that metadata changes (like renaming) are // atomic to provide transactional updates to a counter file which is // used to generate uuids. -// -// Write process (on Foo.txt): -// - Step W1: Acquire "write lock" on the existing file. -// - Step W2: Copy the old file in a new unique temporary file. (copy Foo.txt Foo.TmpNNNN.txt) -// - Step W3: Apply the writes to the new file (Foo.TmpNNNN.txt). -// - Step W4: Flush all the writes (for example those being remaining in the cache manager). -// - Step W5: Rename the old file in a new unique Alternate form (ren Foo.txt Foo.AltNNNN.txt) -// - Step W6: Rename the new file into the old file (ren Foo.TmpNNNN.txt Foo.txt) -// - Step W7: Delete the old Alternate file (del Foo.AltNNNN.txt). If this fails, simply ignore. The file will be deleted later during the next recovery. +// +// Write process (on Foo.txt): +// - Step W1: Acquire "write lock" on the existing file. +// - Step W2: Copy the old file in a new unique temporary file. (copy Foo.txt +// Foo.TmpNNNN.txt) +// - Step W3: Apply the writes to the new file (Foo.TmpNNNN.txt). +// - Step W4: Flush all the writes (for example those being remaining in the +// cache manager). +// - Step W5: Rename the old file in a new unique Alternate form (ren Foo.txt +// Foo.AltNNNN.txt) +// - Step W6: Rename the new file into the old file (ren Foo.TmpNNNN.txt +// Foo.txt) +// - Step W7: Delete the old Alternate file (del Foo.AltNNNN.txt). If this +// fails, simply ignore. The file will be deleted later during the next +// recovery. // - Step W8: Release "write lock" on the existing file. -// Recovery from a crash during write (on Foo.txt): -// - Step R1: If Foo.txt is missing but we have both Foo.AltNNNN.txt and Foo.TmpNNNN.txt present, then we crashed between Step W5 and Step W6. Retry from Step W6. -// - Step R2: If Foo.txt is present but Foo.TmpNNNN.txt is also present, then we crashed before Step W5. Try to delete all Foo.TmpNNNN.txt files and ignore failures. -// - Step R3: If Foo.txt is present but Foo.AltNNNN.txt is also present, then we crashed between Step W6 and Step W7. Try to delete all Foo.AltNNNN.txt files and ignore failures. +// Recovery from a crash during write (on Foo.txt): +// - Step R1: If Foo.txt is missing but we have both Foo.AltNNNN.txt and +// Foo.TmpNNNN.txt present, then we crashed between Step W5 and Step W6. Retry +// from Step W6. +// - Step R2: If Foo.txt is present but Foo.TmpNNNN.txt is also present, then we +// crashed before Step W5. Try to delete all Foo.TmpNNNN.txt files and ignore +// failures. +// - Step R3: If Foo.txt is present but Foo.AltNNNN.txt is also present, then we +// crashed between Step W6 and Step W7. Try to delete all Foo.AltNNNN.txt files +// and ignore failures. -void init_store (std::string const &path, - std::string const &lock_path, - std::string const &counter_path) -{ - if (!boost::filesystem::is_directory(path)) - { +void init_store(std::string const &path, std::string const &lock_path, + std::string const &counter_path) { + if (!boost::filesystem::is_directory(path)) { boost::filesystem::create_directories(path); - std::ofstream fs(lock_path.c_str ()); - std::ofstream fc(counter_path.c_str ()); + std::ofstream fs(lock_path.c_str()); + std::ofstream fc(counter_path.c_str()); fc << 0; } } -std::string find_file (std::string const &path, std::string const &ending) -{ +std::string find_file(std::string const &path, std::string const &ending) { for (boost::filesystem::directory_iterator p = - boost::filesystem::directory_iterator (path); - p != boost::filesystem::directory_iterator (); p++) - { - std::string fn (p->path ().string ()); + boost::filesystem::directory_iterator(path); + p != boost::filesystem::directory_iterator(); p++) { + std::string fn(p->path().string()); if (boost::algorithm::ends_with(fn, ending)) return fn; } - return std::string (); + return std::string(); } // boost::filesystem::copy_file is broken for C++ 11 unless one compiles boost -// with c++ 11. (See http://boost.2283326.n4.nabble.com/Filesystem-problems-with-g-std-c-0x-td2639716.html) +// with c++ 11. (See +// http://boost.2283326.n4.nabble.com/Filesystem-problems-with-g-std-c-0x-td2639716.html) // so we provide our own. -void copy_file(std::string const&in, std::string const &out) -{ - std::fstream f(in.c_str (), std::fstream::in|std::fstream::binary); +void copy_file(std::string const &in, std::string const &out) { + std::fstream f(in.c_str(), std::fstream::in | std::fstream::binary); f << std::noskipws; std::istream_iterator begin(f); std::istream_iterator end; - - std::fstream f2(out.c_str (), std::fstream::out|std::fstream::trunc|std::fstream::binary); + + std::fstream f2(out.c_str(), std::fstream::out | std::fstream::trunc | + std::fstream::binary); std::ostream_iterator begin2(f2); std::copy(begin, end, begin2); } -uint32_t get_host_ip_uint32 (std::string const &ip) -{ +uint32_t get_host_ip_uint32(std::string const &ip) { std::vector split_vec; - boost::algorithm::split( split_vec, ip,boost::algorithm::is_any_of(".") ); - bts_assert (split_vec.size () == 4); + boost::algorithm::split(split_vec, ip, boost::algorithm::is_any_of(".")); + bts_assert(split_vec.size() == 4); uint32_t result = 0; int idx = 0; for (std::string &byte : split_vec) { - uint32_t this_byte = atoi (byte.c_str ()); + uint32_t this_byte = atoi(byte.c_str()); this_byte = (this_byte << (idx++ * 8)); result += this_byte; } return result; } -std::string &get_host_ip_string (std::string const &ip) -{ +std::string &get_host_ip_string(std::string const &ip) { static std::string result; - if (result.size () == 0) { + if (result.size() == 0) { std::ostringstream result_stream; - - bts_assert (ip.size ()); - bts_assert (ip != "127.0.0.1"); - std::istringstream ip_str (ip); + + bts_assert(ip.size()); + bts_assert(ip != "127.0.0.1"); + std::istringstream ip_str(ip); for (int i = 0; i < 4; i++) { int b; ip_str >> b; - ip_str.get (); - + ip_str.get(); + char high_nibble = (b & 0xf0) >> 4; char low_nibble = b & 0x0f; if (high_nibble >= 10) @@ -134,14 +141,13 @@ std::string &get_host_ip_string (std::string const &ip) low_nibble += '0'; result_stream << high_nibble << low_nibble; } - result = result_stream.str (); + result = result_stream.str(); } return result; } -uint64_t reserve_ids (std::string const &scope, int n_ids) -{ - tbb::mutex::scoped_lock l (lock); +uint64_t reserve_ids(std::string const &scope, int n_ids) { + tbb::mutex::scoped_lock l(lock); // we risk race conditions if the directory is not there. // but that's OK, this is rare and easily manageable. @@ -149,83 +155,82 @@ uint64_t reserve_ids (std::string const &scope, int n_ids) std::string lock_path = path + "/" + lock_file_name; std::string counter_path = path + "/" + counter_file_name; - init_store (path,lock_path, counter_path); + init_store(path, lock_path, counter_path); // W1 - boost::interprocess::file_lock f_lock(lock_path.c_str ()); - boost::interprocess::scoped_lock scope_lock (f_lock); + boost::interprocess::file_lock f_lock(lock_path.c_str()); + boost::interprocess::scoped_lock scope_lock( + f_lock); - std::string tmp_file = find_file (path, ".tmp"); - std::string alt_file = find_file (path, ".alt"); - std::string counter_file = find_file (path, counter_file_name); + std::string tmp_file = find_file(path, ".tmp"); + std::string alt_file = find_file(path, ".alt"); + std::string counter_file = find_file(path, counter_file_name); // r1? - if (counter_file.size () == 0 && tmp_file.size () && alt_file.size ()) { - boost::filesystem::rename (tmp_file,counter_path); - boost::filesystem::remove (alt_file); - f_lock.unlock (); - lock.unlock (); - return reserve_ids (scope,n_ids); - + if (counter_file.size() == 0 && tmp_file.size() && alt_file.size()) { + boost::filesystem::rename(tmp_file, counter_path); + boost::filesystem::remove(alt_file); + f_lock.unlock(); + lock.unlock(); + return reserve_ids(scope, n_ids); + } // r2 - else if (counter_file.size () && tmp_file.size ()) { - boost::filesystem::remove (tmp_file); - f_lock.unlock (); - lock.unlock (); - return reserve_ids (scope,n_ids); + else if (counter_file.size() && tmp_file.size()) { + boost::filesystem::remove(tmp_file); + f_lock.unlock(); + lock.unlock(); + return reserve_ids(scope, n_ids); } // r3 - else if (counter_file.size () && alt_file.size ()) { - boost::filesystem::remove (alt_file); - f_lock.unlock (); - lock.unlock (); - return reserve_ids (scope,n_ids); + else if (counter_file.size() && alt_file.size()) { + boost::filesystem::remove(alt_file); + f_lock.unlock(); + lock.unlock(); + return reserve_ids(scope, n_ids); } else { - + // W2, copy counter file to a temp file - tmp_file = path + "/" + Uuid ().to_text_string () + ".tmp"; - copy_file (counter_path, tmp_file); + tmp_file = path + "/" + Uuid().to_text_string() + ".tmp"; + copy_file(counter_path, tmp_file); // W3, W4 update temp file, close and flush uint64_t counter_value; - std::ifstream (tmp_file.c_str ()) >> counter_value; - std::ofstream (tmp_file.c_str ()) << counter_value + n_ids; - - // W5, rename counter file to an alt file (renames are atomic) - alt_file = path + "/" + Uuid ().to_text_string () + ".alt"; - boost::filesystem::rename (counter_path, alt_file); + std::ifstream(tmp_file.c_str()) >> counter_value; + std::ofstream(tmp_file.c_str()) << counter_value + n_ids; - // W6, rename temp file to counter file - boost::filesystem::rename (tmp_file,counter_path); + // W5, rename counter file to an alt file (renames are atomic) + alt_file = path + "/" + Uuid().to_text_string() + ".alt"; + boost::filesystem::rename(counter_path, alt_file); + + // W6, rename temp file to counter file + boost::filesystem::rename(tmp_file, counter_path); // W7 delete alt file - boost::filesystem::remove (alt_file); + boost::filesystem::remove(alt_file); return counter_value; } } -std::vector gen_ids (std::string const &scope, int n_ids) -{ - auto counter_value = reserve_ids (scope,n_ids); +std::vector gen_ids(std::string const &scope, int n_ids) { + auto counter_value = reserve_ids(scope, n_ids); std::vector result; for (int i = 0; i < n_ids; i++) - result.push_back (counter_value + i); + result.push_back(counter_value + i); return result; } -static char base_62_alphabet [] = { - '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f', - 'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v', - 'w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L', - 'M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z' -}; +static char base_62_alphabet[] = { + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', + 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', + 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', + 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', + 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'}; -std::string to_base_62 (uint32_t src) -{ - std::string out (6, ' '); - for (int i=5; i >= 0;i--) { +std::string to_base_62(uint32_t src) { + std::string out(6, ' '); + for (int i = 5; i >= 0; i--) { int r = src % 62; out[i] = base_62_alphabet[r]; src = src / 62; @@ -233,47 +238,46 @@ std::string to_base_62 (uint32_t src) return out; } -void to_base_62 (std::vector const &id_vec, std::vector *out) -{ - for (auto src : id_vec) - for (int i=5; i >= 0;i--) { +void to_base_62(std::vector const &id_vec, std::vector *out) { + for (auto src : id_vec) + for (int i = 5; i >= 0; i--) { int r = src % 62; - out->push_back (base_62_alphabet[r]); + out->push_back(base_62_alphabet[r]); src = src / 62; } } -uint64_t gen_id (std::string const &scope) -{ - std::vector result = gen_ids (scope, 1); +uint64_t gen_id(std::string const &scope) { + std::vector result = gen_ids(scope, 1); return result[0]; } -std::vector gen_puids (std::string const &scope, int n_puids) -{ +std::vector gen_puids(std::string const &scope, int n_puids) { // first generate a series of unique uint32 numbers (For this machine) - std::vector ids = gen_ids (scope, n_puids); + std::vector ids = gen_ids(scope, n_puids); // now add the host ip and a timer string to the generated id to // guarantee uniqueness across machines - uint32_t ip = get_host_ip_uint32 (os::default_host_ip ()); - std::string encoded_ip (to_base_62 (ip)); - - uint32_t seconds = time (NULL); - std::string encoded_time (to_base_62 (seconds)); - + uint32_t ip = get_host_ip_uint32(os::default_host_ip()); + std::string encoded_ip(to_base_62(ip)); + + uint32_t seconds = time(NULL); + std::string encoded_time(to_base_62(seconds)); + std::vector result; for (uint32_t id : ids) { std::ostringstream s; - s << encoded_ip << "-" << encoded_time << "-" << to_base_62 (id); - result.push_back (s.str ()); + s << encoded_ip << "-" << encoded_time << "-" << to_base_62(id); + result.push_back(s.str()); } return result; } -Puid gen_puid (std::string const &scope) -{ - return *(gen_puids (scope,1).begin ()); +Puid gen_puid(std::string const &scope) { + return *(gen_puids(scope, 1).begin()); } -}} + +} // namespace utils +} // namespace bts +#endif \ No newline at end of file diff --git a/src/lib/utils/Puid.h b/src/lib/utils/Puid.h index 2340320a9..4a7d4e2b5 100644 --- a/src/lib/utils/Puid.h +++ b/src/lib/utils/Puid.h @@ -1,10 +1,12 @@ #pragma once #include "bts_export.h" +#include "lib/utils/bts_assert.h" +#include +#include #include #include -#include namespace bts { namespace utils { @@ -19,21 +21,77 @@ typedef std::string Puid; // machine's ip, the unix time (seconds since 1970) and a machine // unique counter. id's are 20 bytes long and can contain [a-z], [A-Z] // [0-9] and - -BTS_EXPORT std::vector gen_puids (std::string const &scope, int n_puids); -BTS_EXPORT Puid gen_puid (std::string const &scope); +BTS_EXPORT std::vector gen_puids(std::string const &scope, int n_puids); +BTS_EXPORT Puid gen_puid(std::string const &scope); // generate machine unique ids (no guarantees of uniqueness across // machines) -BTS_EXPORT std::vector gen_ids (std::string const &scope, int n_ids); +BTS_EXPORT std::vector gen_ids(std::string const &scope, int n_ids); // Similar to the above but instead of copying the ids to a vector // this simply returns the first free id (x) and skips ahead by n_ids // so that the next gen_id call will will be x+n_ids -BTS_EXPORT uint64_t reserve_ids (std::string const &scope, int n_ids); +BTS_EXPORT uint64_t reserve_ids(std::string const &scope, int n_ids); // generate a single id -BTS_EXPORT uint64_t gen_id (std::string const &scope); +BTS_EXPORT uint64_t gen_id(std::string const &scope); // convert a uint32 to a 6 byte human readeable string containing only // alpha numerical characters. -BTS_EXPORT std::string to_base_62 (uint32_t id); -BTS_EXPORT void to_base_62 (std::vector const &ids, std::vector *out); -}} +BTS_EXPORT std::string to_base_62(uint32_t id); +BTS_EXPORT void to_base_62(std::vector const &ids, + std::vector *out); + +#ifndef _WIN32 +class BTS_EXPORT IDGenerator { + public: + IDGenerator(std::string const &scope, uint32_t batch_size) + : _batch_size(batch_size), _next(0), _end(0), _scope(scope), _lock() { + bts_assert((int)batch_size > 0); + refill(); + } + + // next should only be called from a single thread + uint64_t next() { + if (_remaning() == 0) { + std::lock_guard _(_lock); + if (!_next_block.has_value()) + _get_next_block(); + + bts_assert(_next_block.has_value()); + _next = _next_block.value().first; + _end = _next_block.value().second; + _next_block.reset(); + } + bts_assert(_remaning() > 0); + return _next++; + } + + // refill can be called from any thread + void refill() { + std::lock_guard _(_lock); + if (!_next_block.has_value()) + _get_next_block(); + } + + private: + uint64_t _remaning() { + bts_assert(_end >= _next); + return _end - _next; + } + + void _get_next_block() { + auto block_start = reserve_ids(_scope, _batch_size); + auto block_end = block_start + _batch_size; + _next_block = std::make_pair(block_start, block_end); + } + + const uint32_t _batch_size; + uint64_t _next; + uint64_t _end; + const std::string _scope; + std::mutex _lock; + std::optional> _next_block; +}; +#endif + +} // namespace utils +} // namespace bts diff --git a/src/lib/utils/base62.h b/src/lib/utils/base62.h new file mode 100644 index 000000000..22b01252c --- /dev/null +++ b/src/lib/utils/base62.h @@ -0,0 +1,46 @@ +#pragma once + +#include +#include +#include +#include + +#include "lib/utils/bts_assert.h" + +namespace bts { +namespace utils { + +static char base_62_alphabet [] = +{ + '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f', + 'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v', + 'w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L', + 'M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z' +}; + +inline std::string to_base_62 (uint32_t src) +{ + std::string out (6, ' '); + for (int i=5; i >= 0; i--) + { + int r = src % 62; + out[i] = base_62_alphabet[r]; + src = src / 62; + } + return out; +} + +inline uint32_t from_base_62 (const std::string& src) +{ + bts_assert_msg(src.size() == 6, "bad base62 hash"); + uint32_t out = 0; + for (int i=5; i >= 0; i--) + { + uint32_t idx = std::distance(&base_62_alphabet[0], std::find(&base_62_alphabet[0], &base_62_alphabet[61], src[i])); + out += idx * pow(62, 5-i); + } + return out; +} + + +}} diff --git a/src/lib/utils/tests/AuthTokenTests.cpp b/src/lib/utils/tests/AuthTokenTests.cpp index 6bcbe52d8..6c550426d 100644 --- a/src/lib/utils/tests/AuthTokenTests.cpp +++ b/src/lib/utils/tests/AuthTokenTests.cpp @@ -19,7 +19,9 @@ TEST_CASE("AuthToken", "[security]") { // We use 1 byte for api header size REQUIRE(token.ByteSize() <= 240); api::user::TokenPayload payload; - REQUIRE(!api::auth::decode_token(token, &payload)); + api::auth::clear_all_sessions(); + REQUIRE(!api::auth::requires_auth_session(admin_user)); + REQUIRE(api::auth::decode_token(token, &payload)); REQUIRE(payload.id() == admin_user.id()); REQUIRE(payload.wheel() == false); } @@ -28,7 +30,8 @@ TEST_CASE("AuthToken", "[security]") { auto token = api::auth::generate_token(trader_user); // We use 1 byte for api header size REQUIRE(token.ByteSize() <= 240); - api::user::TokenPayload payload; + api::user::TokenPayload payload; + api::auth::clear_all_sessions(); REQUIRE(api::auth::decode_token(token, &payload)); REQUIRE(payload.id() == trader_user.id()); REQUIRE(payload.wheel() == false); diff --git a/src/lib/utils/tests/IDGeneratorTests.cpp b/src/lib/utils/tests/IDGeneratorTests.cpp new file mode 100644 index 000000000..832a17fe4 --- /dev/null +++ b/src/lib/utils/tests/IDGeneratorTests.cpp @@ -0,0 +1,55 @@ +#ifndef _WIN32 +#include "../Puid.h" +#include "catch/catch.hpp" + +namespace bts { + +TEST_CASE("IDGenerator", "[utils]") { + SECTION("1") { + utils::IDGenerator gen("test",1); + auto start = gen.next(); + for (int i = 0; i < 10;i++) { + auto previous_start = start; + start = gen.next(); + REQUIRE( start == previous_start + 1); + } + } + SECTION("10") { + utils::IDGenerator gen("test",4); + auto start = gen.next(); + for (int i = 0; i < 10;i++) { + auto previous_start = start; + start = gen.next(); + REQUIRE( start == previous_start + 1); + } + } + SECTION("10_interference") { + utils::IDGenerator gen("test",4); + auto start = gen.next(); + auto previous_start = start; + auto interferance = utils::reserve_ids("test", 3); + + previous_start = start; + start = gen.next(); + REQUIRE( start == previous_start + 1); + + previous_start = start; + start = gen.next(); + REQUIRE( start == previous_start + 1); + + previous_start = start; + start = gen.next(); + REQUIRE( start == previous_start + 1); + + previous_start = start; + start = gen.next(); + REQUIRE( interferance + 3 == start); + + previous_start = start; + start = gen.next(); + REQUIRE( start == previous_start + 1); + } +} + +}//ns +#endif diff --git a/src/sdk/CMakeLists.txt b/src/sdk/CMakeLists.txt index e4a38178e..ea6c03fee 100644 --- a/src/sdk/CMakeLists.txt +++ b/src/sdk/CMakeLists.txt @@ -7,11 +7,14 @@ add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/sdk COMMENT "Building SDK" DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/make_sdk.sh + ${CMAKE_CURRENT_SOURCE_DIR}/Dockerfile + ${CMAKE_CURRENT_SOURCE_DIR}/sdk_spark.conf ${CMAKE_CURRENT_SOURCE_DIR}/README.md ${CMAKE_CURRENT_SOURCE_DIR}/sdk_shell.sh + ${CMAKE_CURRENT_SOURCE_DIR}/spark_client.cmd ${CMAKE_CURRENT_SOURCE_DIR}/examples/build_examples.sh - ${CMAKE_CURRENT_SOURCE_DIR}/examples/md_publisher_example.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/examples/order_plugin_example.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/examples/md_publisher/md_publisher_example.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/examples/dynamic_join_order/order_plugin_example.cpp server_layout) add_custom_target(sdk_dir ALL DEPENDS ${CMAKE_BINARY_DIR}/sdk) diff --git a/src/sdk/Dockerfile b/src/sdk/Dockerfile new file mode 100644 index 000000000..3da5c5045 --- /dev/null +++ b/src/sdk/Dockerfile @@ -0,0 +1,33 @@ +from bluetradingsystems/linux_docker + + +# Set version with --build-arg SDK_VERSION=X +# This lets us query an image to see when it was built +ARG SDK_VERSION=0 +ARG SDK_BUILD_DATE=0 +ARG USERNAME=bts + +# configure build_examples +env SDK_ROOT=/sdk \ + SDK_VERSION=$SDK_VERSION \ + SDK_BUILD_DATE=$SDK_BUILD_DATE + +# add user bts +RUN groupadd --gid 1000 $USERNAME && \ + useradd -s /bin/bash --uid 1000 --gid 1000 -m $USERNAME && \ + mkdir -p /sparkbin/plugins && \ + mkdir /sparkdata && \ + ln -s /sparkbin/ sdk/lib && \ + echo $USERNAME ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME && \ + chmod 0440 /etc/sudoers.d/$USERNAME && \ + chown -R bts.bts /sparkbin/plugins && \ + chown -R bts.bts /sparkdata + +# copy sdk +COPY include/ /sdk/include/ +COPY lib/ /sparkbin/ +COPY externals /sdk/externals/ +COPY spark.conf /sparkdata/conf/ +COPY --chown=$USERNAME futures.coconut /sparkdata/data/replay/futures.coconut + + diff --git a/src/sdk/disable_onixs.py b/src/sdk/disable_onixs.py new file mode 100644 index 000000000..e9a64ff9e --- /dev/null +++ b/src/sdk/disable_onixs.py @@ -0,0 +1,13 @@ +#!/usr/bin/python + +import sys +f = open(sys.argv[1], 'rb+') +license = '5E50A1DACD94D7C1BECD316EEAE5AB02' +data = f.read() +match = data.find(license) +if match > 0: + f.seek(match) + f.write('DEAD'*8) + exit(0) +exit(-1) + diff --git a/src/sdk/examples/algo_template/.devcontainer.json b/src/sdk/examples/algo_template/.devcontainer.json new file mode 100644 index 000000000..3aee1e407 --- /dev/null +++ b/src/sdk/examples/algo_template/.devcontainer.json @@ -0,0 +1,20 @@ +{ + "name": "Spark sdk container", + "image": "spark_sdk", + "runArgs": [ + "--shm-size", "1024m", + "--cap-add=SYS_PTRACE", "--security-opt", "seccomp=unconfined", + "-u", "bts" + ], + "settings": { + "terminal.integrated.shell.linux": "/bin/bash", + "C_Cpp.default.cppStandard": "c++20" + }, + // use 9000 to avoid conflict with typical bts dev setup + "appPort": [9000,9001], + "postCreateCommand": "", + "extensions": [ + "ms-vscode.cpptools" + ] +} + diff --git a/src/sdk/examples/algo_template/.gitignore b/src/sdk/examples/algo_template/.gitignore new file mode 100644 index 000000000..e1af70fe4 --- /dev/null +++ b/src/sdk/examples/algo_template/.gitignore @@ -0,0 +1 @@ +algo_template.so diff --git a/src/sdk/examples/algo_template/.vscode/c_cpp_properties.json b/src/sdk/examples/algo_template/.vscode/c_cpp_properties.json new file mode 100644 index 000000000..5fea8714c --- /dev/null +++ b/src/sdk/examples/algo_template/.vscode/c_cpp_properties.json @@ -0,0 +1,21 @@ +{ + "configurations": [ + { + "name": "Linux", + "includePath": [ + "/usr/include", + "/usr/local/include", + "${workspaceFolder}/**", + "/sdk/include/", + "/sdk/include/spark", + "/sdk/externals/include" + ], + "defines": [], + "compilerPath": "/usr/bin/clang", + "cStandard": "c11", + "cppStandard": "c++17", + "intelliSenseMode": "clang-x64" + } + ], + "version": 4 +} \ No newline at end of file diff --git a/src/sdk/examples/algo_template/.vscode/launch.json b/src/sdk/examples/algo_template/.vscode/launch.json new file mode 100644 index 000000000..c78882e49 --- /dev/null +++ b/src/sdk/examples/algo_template/.vscode/launch.json @@ -0,0 +1,27 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "(gdb) Launch", + "type": "cppdbg", + "request": "launch", + "program": "/sparkbin/sparkd", + "args": ["run"], + "stopAtEntry": false, + "cwd": "/sparkbin", + "environment": [], + "externalConsole": false, + "MIMode": "gdb", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ] + } + ] +} \ No newline at end of file diff --git a/src/sdk/examples/algo_template/.vscode/tasks.json b/src/sdk/examples/algo_template/.vscode/tasks.json new file mode 100644 index 000000000..6771fef6a --- /dev/null +++ b/src/sdk/examples/algo_template/.vscode/tasks.json @@ -0,0 +1,29 @@ +{ + // See https://go.microsoft.com/fwlink/?LinkId=733558 + // for the documentation about the tasks.json format + // variables: ${workspaceRoot} + "version": "2.0.0", + "tasks": [ + { + "label": "build", + "type": "shell", + "command": "./build.sh", + "group": { + "kind": "build", + "isDefault": true + } + }, + { + "label": "Start Sparkd", + "type": "shell", + "command": "cd /sparkbin; ./sparkd run", + "isBackground": true, + "problemMatcher": [] + }, + { + "label": "Stop Sparkd", + "type": "shell", + "command": "pkill -f sparkd" + } + ] +} diff --git a/src/sdk/examples/algo_template/algo_template.cpp b/src/sdk/examples/algo_template/algo_template.cpp new file mode 100644 index 000000000..41e55969a --- /dev/null +++ b/src/sdk/examples/algo_template/algo_template.cpp @@ -0,0 +1,132 @@ +#include "spark/lib/config/config.h" +#include "spark/lib/config/protos/system.pb.h" +#include "spark/lib/domain_model/Security.h" +#include "spark/lib/orderman/orders/algo/IAlgoOrder.h" +#include "spark/lib/orderman/orders/algo/IAlgoServices.h" +#include "spark/lib/plugin/SparkServices.h" + +#include "spark/lib/orderman/order_factory/IOrderFactory.h" +#include "spark/lib/orderman/orderman_utils/Book.h" +#include "spark/lib/orderman/orderman_utils/orderman_utils.h" +#include "spark/lib/utils/PBUtils.h" + +using namespace bts::api::orderman; +using namespace bts::orderman; + +namespace bts { +namespace order_example { + +// MyAlgo is a blank IAlgoOrder which you can copy when starting a new Algo +class MyAlgo : public IAlgoOrder { + public: + MyAlgo(); + ~MyAlgo(); + + OrderOpResult do_before_launch(IBeforeLaunchAlgoServices &) override; + void do_launch(IAlgoServices &) override; + void do_launch_attached(IAlgoServices &, OrderKey, ChildState) override; + void do_update(api::orderman::Update const &) override; + void on_book(SecurityKey, Book const &) override; + void on_child_change(OrderKey, ChildState const &) override; + + private: + IAlgoServices* algo_services; + ChildTemplate *child_template = nullptr; + OrderKey child_key = null_order_key(); +}; + + +MyAlgo::MyAlgo() : algo_services(nullptr) {} +MyAlgo::~MyAlgo() {} + +// This is called outside of the latency sensitive path. +// Expensive initialization should be done here, rather than in do_launch() +OrderOpResult MyAlgo::do_before_launch(IBeforeLaunchAlgoServices &serv) { + LOG(Sdk,Info, "MyAlgo: do_before_launch"); + // If we are not attaching, create a child template so we can + // quickly launch our child when the time comes. + if (!serv.is_attaching()) { + // We will be sending a limit order with the same side, + // security, trader, account, etc, as this algo order, so just + // copy our definition to the child definition to start with. + auto child_def = serv.definition(); + // Now add a limit order component to mark our child as a limit + // order. Limit orders do not require any extra parameters + // beyond those already inherited from this algo order + // (i.e. beyond what we obtained through serv.definition()) so + // we can just use a default constructed LimitOrderDefinition. + *child_def.mutable_custom_def()->mutable_limit() = LimitOrderDefinition{}; + // Now just build the child template and cache it for future use + child_template = &(serv.make_child_template(child_def)); + } + // Default constructed result is successful + return {}; +} + +// Called in the latency sensitive path, once, when the algo is first launched. +// If the Algo is attaching to an existing order do_launch_attached is called instead. +void MyAlgo::do_launch(IAlgoServices &services) { + algo_services = &services; + // Subscribe to book updates for this security + services.subscribe_book(SecurityKey(services.security().id())); + // Launch a child limit order + child_key = + services.launch_child(*child_template, services.price(), services.quantity()); + LOG(Sdk,Info, "MyAlgo: do_launch"); +} + +// Called in the latency sensitive path, once, when the algo is first +// launched. In this case the algo is provided with an existing order +// it is now responsible for managing. +void MyAlgo::do_launch_attached(IAlgoServices &services, + OrderKey attached_order, ChildState st) { + algo_services = &services; + // Subscribe to book updates for this security + services.subscribe_book(SecurityKey(services.security().id())); + // Keep track of the child key + child_key = attached_order; + LOG(Sdk,Info, "MyAlgo: do_launch_attached"); +} + +// Called when a human (or a supervising algo) updates a parameter for +// this algo. The implementation validates the update and depending on +// the validation outcome either applies the update or terminates the algo. +void MyAlgo::do_update(api::orderman::Update const &update) { +} + +// Called when the book for the specified security has changed +void MyAlgo::on_book(SecurityKey, Book const &b) { +} + +// Our child reported a change +void MyAlgo::on_child_change(OrderKey, ChildState const &st) { +} + +// This is called by the spark daemon at startup. In this call we +// describe our algo's parameters and register a factory function to +// create an instance of the algo. +extern "C" void init_spark_plugin(bts::SparkServices &serv) { + using namespace bts; + + DynamicOrderDescriptor desc; + + // Start by naming our order type + desc.set_dynamic_order_type("My Algo"); + + *desc.add_param_desc() = param_bool("Enable Feature"); + *desc.add_param_desc() = param_uint32("Feature Quantity"); + + // Associate the descriptor with a builder function and register + // that with the Spark server. + auto result = + serv.register_dynamic_algo_order( + desc, []() { return std::make_unique(); }); + + if (result != bts::AlgoRegistrationResult::SUCCESS) { + // The desc has errors + // Check the error log (/sparkdata/data/logging/log) for more details + } +} + +} // namespace order_example +} // namespace bts diff --git a/src/sdk/examples/algo_template/build.sh b/src/sdk/examples/algo_template/build.sh new file mode 100644 index 000000000..445e5154c --- /dev/null +++ b/src/sdk/examples/algo_template/build.sh @@ -0,0 +1,29 @@ +#!/bin/bash +set -e + +SDK_ROOT=${SDK_ROOT:-../..} +PLUGIN_NAME=algo_template + +g++ -isystem $SDK_ROOT/externals/include \ + -I $SDK_ROOT/include \ + -I $SDK_ROOT/include/spark \ + -std=c++1z \ + -fPIC \ + -g \ + -shared \ + -o $PLUGIN_NAME.so \ + $PLUGIN_NAME.cpp \ + -Wl,-rpath,$SDK_ROOT/ \ + -L $SDK_ROOT/lib \ + -l spark + +# If we're in the sdk docker copy to /sparkbin +yellow=$'\e[1;33m' +end=$'\e[0m' +if [ "${SDK_VERSION}" != "" ]; then + cp $PLUGIN_NAME.so /sparkbin/plugins/ + echo $PLUGIN_NAME.so copied to /sparkbin/plugins/ + echo ${yellow}Restart sparkd to see changes.${end} +fi + + diff --git a/src/sdk/examples/build_examples.sh b/src/sdk/examples/build_examples.sh old mode 100755 new mode 100644 index 3625e12d8..135bcc4d3 --- a/src/sdk/examples/build_examples.sh +++ b/src/sdk/examples/build_examples.sh @@ -1,26 +1,15 @@ #!/bin/bash -g++ -isystem ../externals/include \ - -I ../include \ - -I ../include/spark \ - -std=c++1z \ - -o md_publisher_example \ - md_publisher_example.cpp \ - -Wl,-rpath,../lib \ - -L ../lib \ - -l spark \ - -llttng-ust \ - -ldl - -g++ -isystem ../externals/include \ - -I ../include \ - -I ../include/spark \ - -std=c++1z \ - -fPIC \ - -g \ - -shared \ - -o order_plugin_example.so \ - order_plugin_example.cpp \ - -Wl,-rpath,.. \ - -L ../lib \ - -l spark +# exit when the first build fails +set -e + +for e in *; do + if [ -d $e ] + then + pushd $e > /dev/null + if [ -f ./build.sh ]; then + ./build.sh + fi + popd > /dev/null + fi +done diff --git a/src/sdk/examples/dynamic_join_order/.devcontainer.json b/src/sdk/examples/dynamic_join_order/.devcontainer.json new file mode 100644 index 000000000..3aee1e407 --- /dev/null +++ b/src/sdk/examples/dynamic_join_order/.devcontainer.json @@ -0,0 +1,20 @@ +{ + "name": "Spark sdk container", + "image": "spark_sdk", + "runArgs": [ + "--shm-size", "1024m", + "--cap-add=SYS_PTRACE", "--security-opt", "seccomp=unconfined", + "-u", "bts" + ], + "settings": { + "terminal.integrated.shell.linux": "/bin/bash", + "C_Cpp.default.cppStandard": "c++20" + }, + // use 9000 to avoid conflict with typical bts dev setup + "appPort": [9000,9001], + "postCreateCommand": "", + "extensions": [ + "ms-vscode.cpptools" + ] +} + diff --git a/src/sdk/examples/dynamic_join_order/.gitignore b/src/sdk/examples/dynamic_join_order/.gitignore new file mode 100644 index 000000000..e4eea9277 --- /dev/null +++ b/src/sdk/examples/dynamic_join_order/.gitignore @@ -0,0 +1 @@ +order_plugin_example.so diff --git a/src/sdk/examples/dynamic_join_order/.vscode/c_cpp_properties.json b/src/sdk/examples/dynamic_join_order/.vscode/c_cpp_properties.json new file mode 100644 index 000000000..5fea8714c --- /dev/null +++ b/src/sdk/examples/dynamic_join_order/.vscode/c_cpp_properties.json @@ -0,0 +1,21 @@ +{ + "configurations": [ + { + "name": "Linux", + "includePath": [ + "/usr/include", + "/usr/local/include", + "${workspaceFolder}/**", + "/sdk/include/", + "/sdk/include/spark", + "/sdk/externals/include" + ], + "defines": [], + "compilerPath": "/usr/bin/clang", + "cStandard": "c11", + "cppStandard": "c++17", + "intelliSenseMode": "clang-x64" + } + ], + "version": 4 +} \ No newline at end of file diff --git a/src/sdk/examples/dynamic_join_order/.vscode/launch.json b/src/sdk/examples/dynamic_join_order/.vscode/launch.json new file mode 100644 index 000000000..c78882e49 --- /dev/null +++ b/src/sdk/examples/dynamic_join_order/.vscode/launch.json @@ -0,0 +1,27 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "(gdb) Launch", + "type": "cppdbg", + "request": "launch", + "program": "/sparkbin/sparkd", + "args": ["run"], + "stopAtEntry": false, + "cwd": "/sparkbin", + "environment": [], + "externalConsole": false, + "MIMode": "gdb", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ] + } + ] +} \ No newline at end of file diff --git a/src/sdk/examples/dynamic_join_order/.vscode/tasks.json b/src/sdk/examples/dynamic_join_order/.vscode/tasks.json new file mode 100644 index 000000000..424904199 --- /dev/null +++ b/src/sdk/examples/dynamic_join_order/.vscode/tasks.json @@ -0,0 +1,29 @@ +{ + // See https://go.microsoft.com/fwlink/?LinkId=733558 + // for the documentation about the tasks.json format + // variables: ${workspaceRoot} + "version": "2.0.0", + "tasks": [ + { + "label": "build", + "type": "shell", + "command": "./build.sh", + "group": { + "kind": "build", + "isDefault": true + } + }, + { + "label": "Start Sparkd", + "type": "shell", + "command": "cd /sparkbin; ./sparkd run", + "isBackground": true, + "problemMatcher": [] + }, + { + "label": "Stop Sparkd", + "type": "shell", + "command": "pkill -f sparkd" + }, + ] +} diff --git a/src/sdk/examples/dynamic_join_order/build.sh b/src/sdk/examples/dynamic_join_order/build.sh new file mode 100644 index 000000000..f80c06d68 --- /dev/null +++ b/src/sdk/examples/dynamic_join_order/build.sh @@ -0,0 +1,29 @@ +#!/bin/bash +set -e + +SDK_ROOT=${SDK_ROOT:-../..} +PLUGIN_NAME=order_plugin_example + +g++ -isystem $SDK_ROOT/externals/include \ + -I $SDK_ROOT/include \ + -I $SDK_ROOT/include/spark \ + -std=c++1z \ + -fPIC \ + -g \ + -shared \ + -o $PLUGIN_NAME.so \ + $PLUGIN_NAME.cpp \ + -Wl,-rpath,$SDK_ROOT/ \ + -L $SDK_ROOT/lib \ + -l spark + +# If we're in the sdk docker copy to /sparkbin +yellow=$'\e[1;33m' +end=$'\e[0m' +if [ "${SDK_VERSION}" != "" ]; then + cp $PLUGIN_NAME.so /sparkbin/plugins/ + echo $PLUGIN_NAME.so copied to /sparkbin/plugins/ + echo ${yellow}Restart sparkd to see changes.${end} +fi + + diff --git a/src/sdk/examples/order_plugin_example.cpp b/src/sdk/examples/dynamic_join_order/order_plugin_example.cpp similarity index 97% rename from src/sdk/examples/order_plugin_example.cpp rename to src/sdk/examples/dynamic_join_order/order_plugin_example.cpp index 4f62067e1..461a83541 100644 --- a/src/sdk/examples/order_plugin_example.cpp +++ b/src/sdk/examples/dynamic_join_order/order_plugin_example.cpp @@ -7,6 +7,7 @@ #include "spark/lib/orderman/order_factory/IOrderFactory.h" #include "spark/lib/orderman/orderman_utils/Book.h" +#include "spark/lib/orderman/orderman_utils/orderman_utils.h" #include "spark/lib/utils/PBUtils.h" using namespace bts::api::orderman; @@ -75,13 +76,14 @@ OrderOpResult DynamicJoin::do_before_launch(IBeforeLaunchAlgoServices &serv) { // problem. If an error is returned here the order will be // abandoned. auto ¶ms = def.custom_def().dynamic().order_params(); - auto mqi = params.find("Min Quantity"); - if (mqi == params.end() || mqi->second.as_uint32() == 0) + auto mqi = get_if(params, "Min Quantity"); + if (!mqi || mqi.value() == 0) { return OrderOpResult{ ORDER_ERROR_INVALID_ORDER_DEFINITON, "Min Quantity parameter must be defined and non zero"}; + } - me->min_quantity_to_join = Quantity(mqi->second.as_uint32()); + me->min_quantity_to_join = Quantity(mqi.value()); if (!serv.is_attaching()) { // If we are not attaching, create a child template so we can @@ -110,8 +112,7 @@ OrderOpResult DynamicJoin::do_before_launch(IBeforeLaunchAlgoServices &serv) { return {}; } -// Called in the latency sensitive path, once, when the algo is first -// launched. +// Called in the latency sensitive path, once, when the algo is first launched. void DynamicJoin::do_launch(IAlgoServices &services) { // Cache our algo services me->algo_services = &services; @@ -318,9 +319,7 @@ extern "C" void init_spark_plugin(bts::SparkServices &serv) { // Now add a single unsigned int parameter, Min Quantity, that will // determine the minimum quantity that must exist in the book in // order for us to join the market. - auto param = join_desc.add_param_desc(); - param->set_param_name("Min Quantity"); - param->mutable_param_type()->set_primitive(DYNAMIC_ORDER_PRIMITIVE_UINT); + *join_desc.add_param_desc() = param_uint32("Min Quantity"); // Associate the descriptor with a builder function and register // that with the Spark server. diff --git a/src/sdk/examples/md_publisher/build.sh b/src/sdk/examples/md_publisher/build.sh new file mode 100644 index 000000000..bd9a7d3cc --- /dev/null +++ b/src/sdk/examples/md_publisher/build.sh @@ -0,0 +1,18 @@ +#!/bin/bash + +set -e + +SDK_ROOT=${SDK_ROOT:-../..} + +g++ -isystem $SDK_ROOT/externals/include \ + -I $SDK_ROOT/include \ + -I $SDK_ROOT/include/spark \ + -std=c++1z \ + -o md_publisher_example \ + md_publisher_example.cpp \ + -Wl,-rpath,$SDK_ROOT/lib \ + -L $SDK_ROOT/lib \ + -l spark \ + -llttng-ust \ + -ldl + diff --git a/src/sdk/examples/md_publisher_example.cpp b/src/sdk/examples/md_publisher/md_publisher_example.cpp similarity index 100% rename from src/sdk/examples/md_publisher_example.cpp rename to src/sdk/examples/md_publisher/md_publisher_example.cpp diff --git a/src/sdk/make_sdk.sh b/src/sdk/make_sdk.sh index 945b69c2e..b98409d1a 100755 --- a/src/sdk/make_sdk.sh +++ b/src/sdk/make_sdk.sh @@ -19,19 +19,29 @@ mkdir -p $1/sdk/externals/ (cd $1/src && find lib -type f -name "*.h" -exec cp --parents \{\} $1/sdk/include/spark/ \;) (cd $1/src && find lib -type f -name "*.cc" -exec cp --parents \{\} $1/sdk/include/spark/ \;) +# To build the docker image locally swap the next 3 'ln' for the 3 'rsync' +# Docker will refuse to follow the symlinks during docker build ln -sf $2/../../externals/linux/include $1/sdk/externals/include ln -sf $2/../../externals/linux/lib/Release $1/sdk/externals/lib ln -sf $1/server $1/sdk/lib +# rsync -aL $2/../../externals/linux/include/ $1/sdk/externals/include/ +# rsync -aL $2/../../externals/linux/lib/Release $1/sdk/externals/lib +# rsync -aL $1/server/ $1/sdk/lib/ mkdir $1/sdk/examples -cp $2/examples/* $1/sdk/examples +cp -r $2/examples/* $1/sdk/examples cp $2/sdk_shell.sh $1/sdk/sdk_shell.sh cp $2/update_github.sh $1/sdk/update_github.sh +cp $2/Dockerfile $1/sdk/ +cp $2/sdk_spark.conf $1/sdk/spark.conf cp $2/README.md $1/sdk/README.md +cp $2/disable_onixs.py $1/sdk/disable_onixs.py +cp $2/spark_client.cmd $1/sdk/spark_client.cmd -# ln -sf $1/sdk/lib/defaults $1/sdk/examples/defaults chmod +x $1/sdk/examples/build_examples.sh +chmod +x $1/sdk/examples/*/build.sh chmod +x $1/sdk/sdk_shell.sh chmod +x $1/sdk/update_github.sh -(cd $1/sdk/examples && ./build_examples.sh && ./md_publisher_example >/dev/null) +chmod +x $1/sdk/disable_onixs.py +(cd $1/sdk/examples && ./build_examples.sh) diff --git a/src/sdk/sdk_spark.conf b/src/sdk/sdk_spark.conf new file mode 100644 index 000000000..87cd2b188 --- /dev/null +++ b/src/sdk/sdk_spark.conf @@ -0,0 +1,39 @@ +Logging { + value { + console { + default_level: "Warn" + subsystem { + name: "Sdk" + level: "Info" + } + } + syslog { default_level: "Info" } + file { default_level: "Info" } + } +} + +MarketDataConfig { + value { + use_onixs: false + use_sim: true + } +} + +Ports { + value { + config_port: 9000 + qbus_port: 9001 + exchange_sim_fix_port: 9002 + } +} + +GMI_Import { value { } } + +Symbols { + value { + sim { + future: @SymbolLists.list[GLBX].symbol + spread: @SymbolLists.list[GLBX].symbol + } + } +} diff --git a/src/sdk/spark_client.cmd b/src/sdk/spark_client.cmd new file mode 100644 index 000000000..d66f12a37 --- /dev/null +++ b/src/sdk/spark_client.cmd @@ -0,0 +1 @@ +@client\spark.exe -configserver http://localhost:9000/config/ -nostunnel diff --git a/src/sdk/update_github.cmd b/src/sdk/update_github.cmd deleted file mode 100644 index 16ed85929..000000000 --- a/src/sdk/update_github.cmd +++ /dev/null @@ -1,67 +0,0 @@ -@echo off -setlocal - -set STAGE="c:\temp\bts_staging" -set SDK="spark_client_sdk" -set SRC=%1 -set GH=%2 -set TG=%3 -set OPP=%4 - -if "%SRC%" == "" goto :help -if "%GH%" == "" goto :help -if "%TG%" == "" goto :help - -if "%TG%" == "stage" echo "STAGE ONLY" - -if exist %STAGE% rmdir /s /q %STAGE% -mkdir %STAGE% -git clone --recurse-submodules "%GH%" %STAGE%\\%SDK% - -del /q "%STAGE%\\%SDK%\\*" -FOR /D %%p IN ("%STAGE%\\%SDK%\*.*") DO ( - REM this skips git - if NOT "%%p" == ".git" ( - rmdir /s /q "%%p" - ) -) - -FOR %%i IN ("%src%") DO ( - set filedrive=%%~di - set filepath=%%~pi - set filename=%%~ni - set fileextension=%%~xi - if "%%~xi" == ".nupkg" ( - rem unzip -o %%i -d %STAGE%\\%SDK%\\ - 7z x -o%STAGE%\\%SDK%\\ -y -spe %%i - rmdir /Q /S %STAGE%\\%SDK%\\_rels - rmdir /Q /S %STAGE%\\%SDK%\\package - del /Q %STAGE%\\%SDK%\\spark_sdk.nuspec - del /Q %STAGE%\\%SDK%\\[Content_Types].xml - ) else ( - copy /Y /E %%i %STAGE%\\%SDK%\\ - ) -) -pushd %STAGE%\%SDK%\ - -if NOT "%TG%" == "stage" ( - git status - git add . - git commit -m "%SDK% update %TG%" - git tag "%TG%" - git push origin master --tags %OPP% - - popd - rmdir /s /q %STAGE% -) - -goto :eof - -:help -echo. -echo update_github.cmd ^ ^ [tag] [-push_params] -echo. -echo source can be a nupkg file or a folder -echo. -echo set tag to 'stage' if you don't want to push -goto :eof diff --git a/src/sdk/update_github.sh b/src/sdk/update_github.sh index b81b8ae48..0ee6483c0 100644 --- a/src/sdk/update_github.sh +++ b/src/sdk/update_github.sh @@ -1,4 +1,11 @@ #!/bin/bash +set -e + +# This script used to only update github +# it shoudl be called "publish_sdk" but +# it gets called from the ops repo and there's +# no way to atomically change both repos +# so we keep the name SDK="spark_sdk" STAGE="/tmp/bts_staging" @@ -28,9 +35,9 @@ cp -L -r -f "$SRC"/** $STAGE/$SDK/ pushd $STAGE/$SDK #clean all the stuff we don't want in github -rm ./examples/md_publisher_example -rm ./examples/order_plugin_example.so +rm ./examples/*/*.so rm ./lib/data_files/cert_secure_logon.txt +./disable_onixs.py ./lib/libspark.so rm -r ./externals/lib/libonixs* rm -r ./externals/include/OnixS/ rm -r ./externals/lib/celoxica/ @@ -47,3 +54,6 @@ if [ "$TG" != "stage" ]; then rm -rf $STAGE fi + + + diff --git a/src/server/sparkd/cmds/FIXSim.cpp b/src/server/sparkd/cmds/FIXSim.cpp index 413719a14..2ce8e9bfe 100644 --- a/src/server/sparkd/cmds/FIXSim.cpp +++ b/src/server/sparkd/cmds/FIXSim.cpp @@ -5,7 +5,7 @@ namespace bts { namespace cmd { int FIXSim::execute(std::vector const &) { - bts::sim::start_exchange_sim(); + bts::sim::start_exchange_sim(bts::api::Venue::VENUE_GLBX); return 0; } diff --git a/src/server/sparkd/cmds/Run.cpp b/src/server/sparkd/cmds/Run.cpp index 1bbcbfa49..6328aba84 100644 --- a/src/server/sparkd/cmds/Run.cpp +++ b/src/server/sparkd/cmds/Run.cpp @@ -37,6 +37,7 @@ #include "server/sparkd/support/ShellLatencyServer.h" #include "server/sparkd/user/AdminServiceServer.h" #include "server/sparkd/user/AuthServiceServer.h" +#include "server/sparkd/subprocess/SubProcessRunner.h" #include "lib/orderman/orders/algo/algo_orders/ProRataAlgo.h" #include "lib/orderman/orders/algo/algo_orders/ScratchAlgo.h" @@ -113,6 +114,7 @@ int Run::execute(std::vector const &) { [&]() { perf_log.store_log_entries(); }); timer2.start(); stats.publish(bg_health); + subprocess::run_all(); quickbus.run(); return 0; diff --git a/src/server/sparkd/cmds/RunSim.cpp b/src/server/sparkd/cmds/RunSim.cpp index 92f9f9cc5..abaf2cd53 100644 --- a/src/server/sparkd/cmds/RunSim.cpp +++ b/src/server/sparkd/cmds/RunSim.cpp @@ -6,7 +6,7 @@ namespace cmd { int RunSim::execute(std::vector const &) { - sim::start_exchange_sim(nullptr); + sim::start_exchange_sim(api::Venue::VENUE_SIM, nullptr); return 0; } diff --git a/src/server/sparkd/exchange_sim/ExchangeSim.cpp b/src/server/sparkd/exchange_sim/ExchangeSim.cpp index 11001dfa9..6333786ef 100644 --- a/src/server/sparkd/exchange_sim/ExchangeSim.cpp +++ b/src/server/sparkd/exchange_sim/ExchangeSim.cpp @@ -4,12 +4,14 @@ #include #include #include +#include #include #include #include #include "lib/os/os.h" +#include "lib/domain_model/Security.pb.h" #include "lib/config/protos/Ports.pb.h" #include "lib/config/protos/ExchangeSimCfg.pb.h" #include "lib/config/config.h" @@ -33,8 +35,8 @@ auto cfg = "SocketReuseAddress=Y\n" "StartTime=06:02:00\n" "EndTime=06:01:00\n" - "FileLogPath=$BTS_DATA_PATH/exchange_sim/log\n" - "FileStorePath=$BTS_DATA_PATH/exchange_sim/store\n" + "FileLogPath=$BTS_DATA_PATH/exchange_sim/$SIM_VENUE/log\n" + "FileStorePath=$BTS_DATA_PATH/exchange_sim/$SIM_VENUE/store\n" "HeartBtInt=30\n" "[SESSION]\n" "BeginString=FIX.4.2\n" @@ -125,20 +127,32 @@ struct MDPlayer : md::IRelayWriter }; -int start_exchange_sim(md::MBOWriter* mbo) +int start_exchange_sim(bts::api::Venue venue, md::MBOWriter* mbo) { try { auto str_settings = std::string(cfg); + auto venue_string = boost::to_lower_copy(api::Venue_Name(venue)); boost::replace_all(str_settings,"$BTS_DATA_PATH", os::bts_data_path()); - boost::replace_all(str_settings,"$EXCHANGE_SIM_FIX_PORT", - std::to_string( - config::get().exchange_sim_fix_port())); + boost::replace_all(str_settings,"$SIM_VENUE", venue_string); + std::string port_string; + switch(venue) + { + case api::VENUE_SIM: + port_string = std::to_string(config::get().glbx_sim_fix_port()); + break; + case api::VENUE_SIMICE: + port_string = std::to_string(config::get().ice_sim_fix_port()); + break; + default: + bts_assert_msg(false, "Not a valid sim venue"); + } + boost::replace_all(str_settings,"$EXCHANGE_SIM_FIX_PORT", port_string); std::istringstream str{str_settings}; FIX::SessionSettings settings( str ); - ex_sim::ExchangeSimApplication application(mbo); + ex_sim::ExchangeSimApplication application( venue, mbo ); FIX::FileStoreFactory storeFactory( settings ); FIX::FileLogFactory logFactory( settings ); FIX::SocketAcceptor acceptor( application, storeFactory, settings, logFactory ); diff --git a/src/server/sparkd/exchange_sim/ExchangeSim.h b/src/server/sparkd/exchange_sim/ExchangeSim.h index 819fb5bdb..7ae259e84 100644 --- a/src/server/sparkd/exchange_sim/ExchangeSim.h +++ b/src/server/sparkd/exchange_sim/ExchangeSim.h @@ -1,11 +1,12 @@ #pragma once #include +#include "lib/domain_model/Security.pb.h" namespace bts { namespace md { class MBOWriter; } namespace sim { -int start_exchange_sim(md::MBOWriter* = nullptr); +int start_exchange_sim(bts::api::Venue, md::MBOWriter* = nullptr); }} diff --git a/src/server/sparkd/exchange_sim/ExchangeSimApplication.cpp b/src/server/sparkd/exchange_sim/ExchangeSimApplication.cpp index 8a5708964..0cfb2998d 100644 --- a/src/server/sparkd/exchange_sim/ExchangeSimApplication.cpp +++ b/src/server/sparkd/exchange_sim/ExchangeSimApplication.cpp @@ -20,6 +20,7 @@ #include "server/sparkd/exchange_sim/book/book_transform.h" #include +#include #include #include #include @@ -106,15 +107,19 @@ inline FIX::OrderID exchange_key(const std::string &clOrdID) { } struct ExchangeSimApplication::PImpl { - PImpl(md::MBOWriter *mbo) - : producer(co2::PLATFORM_SIM), md_sn(1), + PImpl(bts::api::Venue venue, md::MBOWriter* mbo) + : venue(venue), + producer(co2::PLATFORM_SIM), + md_sn(1), timer(utils::Dispatcher::background(), 200, [=]() { producer.heartbeat(); }), do_merge_with_md(config::get().merge_with_md()), - store(new persistence::SqliteStore("ex_sim_orders")), lock(), + store( new persistence::SqliteStore("ex_sim_orders_" + boost::to_lower_copy(api::Venue_Name(venue)) )), + lock(), mbo(mbo) {} IDGenerator m_generator; + bts::api::Venue venue; md::MDProducer producer; std::map md_consumer_recs; std::map md_producer_handles; @@ -127,8 +132,7 @@ struct ExchangeSimApplication::PImpl { md::MBOWriter *mbo; }; -ExchangeSimApplication::ExchangeSimApplication(md::MBOWriter *mbo) - : me(new PImpl(mbo)) { +ExchangeSimApplication::ExchangeSimApplication(bts::api::Venue venue, md::MBOWriter* mbo) : me(new PImpl(venue, mbo)) { auto previous_book = me->store->retrieve_all(); auto all = me->store->retrieve_all()["books"]; @@ -552,7 +556,7 @@ void ExchangeSimApplication::update_md(std::string const &sec_desc) { } std::string get_sim_sec_desc(api::Security const &src_sec) { - return std::string("SIM:") + src_sec.glbx().security_desc(); + return std::string("SIM:") + securities::descriptor(src_sec); } void negate_id(api::Security *sec) { @@ -576,18 +580,28 @@ void ExchangeSimApplication::persist_book(OrderBook const &b) { void ExchangeSimApplication::on_new_security(api::Security const &s) { std::lock_guard l(me->lock); if (!s.has_synthetic()) { - md::with_security_like(s, [this, s](auto &fs) { - auto s2 = s; - *s2.mutable_glbx()->mutable_security_desc() = get_sim_sec_desc(s); - s2.set_exchange(api::VENUE_SIM); + md::with_security_like(s, [this, s](auto &fs) + { + auto s2 = s; + if(s.exchange() == api::VENUE_GLBX && me->venue == api::VENUE_SIM) + { + *s2.mutable_glbx()->mutable_security_desc() = get_sim_sec_desc(s); + s2.set_exchange(api::VENUE_SIM); + } + else if(me->venue == api::VENUE_ICE && me->venue == api::VENUE_SIMICE) + { + *s2.mutable_ice()->mutable_security_desc() = get_sim_sec_desc(s); + s2.set_exchange(api::VENUE_SIMICE); + } + else + return; negate_id(&s2); md::convert_security(s2, fs, true); md::MDProducerHandle handle = me->producer.add_security(fs); auto sec_desc = securities::descriptor(s2); me->md_producer_handles.insert(make_pair(sec_desc, handle)); - me->md_consumer_recs.insert( - make_pair(s.glbx().security_desc(), - MDConsumerRec{s2, utils::TimeStamp::now()})); + me->md_consumer_recs.insert(make_pair(securities::descriptor(s), + MDConsumerRec{s2, utils::TimeStamp::now()})); }); } } @@ -654,8 +668,9 @@ void ExchangeSimApplication::on_depth( if (!me->do_merge_with_md) return; - auto it = me->md_consumer_recs.find(sec.glbx().security_desc()); - bts_assert(it != me->md_consumer_recs.end()); + auto it = me->md_consumer_recs.find(securities::descriptor(sec)); + if(it == me->md_consumer_recs.end()) + return; auto &matcher = me->matchers[get_sim_sec_desc(sec)]; Book previous_md_book = matcher.get_book(); @@ -675,7 +690,7 @@ void ExchangeSimApplication::on_depth( // from the differences between current_md_book and previous_md_book // we generate a set of orders that we apply to our order book - merge_with_order_book(it->second.security.glbx().security_desc(), + merge_with_order_book(securities::descriptor(it->second.security), previous_md_book, current_md_book); // pass on implied market unchanged diff --git a/src/server/sparkd/exchange_sim/ExchangeSimApplication.h b/src/server/sparkd/exchange_sim/ExchangeSimApplication.h index 64077befe..121ff6838 100644 --- a/src/server/sparkd/exchange_sim/ExchangeSimApplication.h +++ b/src/server/sparkd/exchange_sim/ExchangeSimApplication.h @@ -40,6 +40,7 @@ GCC_DIAG_OFF("-Wignored-qualifiers") #include #include #include "server/sparkd/exchange_sim/IMDPlayerObserver.h" +#include "lib/domain_model/Security.pb.h" namespace bts { namespace api {class Security;} @@ -55,7 +56,7 @@ class ExchangeSimApplication public FIX::MessageCracker { public: - ExchangeSimApplication(md::MBOWriter*); + ExchangeSimApplication(bts::api::Venue venue, md::MBOWriter*); ~ExchangeSimApplication(); void merge_with_md_book(); diff --git a/src/server/sparkd/md/platforms/ice_onixs/ICEListener.cpp b/src/server/sparkd/md/platforms/ice_onixs/ICEListener.cpp index 7c85642ad..d9bc8a316 100644 --- a/src/server/sparkd/md/platforms/ice_onixs/ICEListener.cpp +++ b/src/server/sparkd/md/platforms/ice_onixs/ICEListener.cpp @@ -14,13 +14,21 @@ namespace md { namespace ice { template <> -double double_of_price(const onixs_ice::Rational& price, int8_t display_factor) +double double_of_price(const onixs_ice::Rational& price, int8_t denominator) { - UNUSED(display_factor); + UNUSED(denominator); double dbl = price.convertTo(); return dbl; } +template<> +void ICEListener::set_security_specific(const onixs_ice::FuturesProductDefinition& def, + FixedSecurity& fsec) +{ + //fsec.cash_per_point = double_of_price(def.tickValue, fsec.ice_price_denominator); + //fsec.cash_per_point = def.tickValue; + fsec.cash_per_point = def.unitQuantity; +} ICEListener::ICEListener(ICESecurityIndex& index, MDProducer& producer, const std::vector channels) : _index(index), @@ -35,20 +43,21 @@ ICEListener::~ICEListener() {} std::string ICEListener::parse_contract_symbol(const std::string& sym) { - const auto&& res = sym.substr(0, sym.find(' ')); - return res; + auto end = sym.find(' '); + if(end == std::string::npos) + return sym; + else + return sym.substr(0, end); } // //State change -void ICEListener::onHandlerStateChange(const onixs_ice::HandlerStateChange& ) +void ICEListener::onHandlerStateChange(const onixs_ice::HandlerStateChange&) { - //TODO } -void ICEListener::onMarketStateChange(const onixs_ice::MarketStateChange& , const onixs_ice::MessageInfo& ) +void ICEListener::onMarketStateChange(const onixs_ice::MarketStateChange& , const onixs_ice::MessageInfo&) { - //TODO } // @@ -84,18 +93,12 @@ void ICEListener::onNewExpiry(const onixs_ice::NewExpiry& msg, bool& is_interest void ICEListener::onFuturesStrategyDefinition(const onixs_ice::FuturesStrategyDefinition& def, bool& is_interested) { - //TODO need to find out how to produce data for testing - //on_uds_future_spread(def, is_interested); - UNUSED(def); - UNUSED(is_interested); + on_uds_future_spread(def, is_interested); } //This is only for UDS void ICEListener::onNewFuturesStrategyDefinition(const onixs_ice::NewFuturesStrategyDefinition& msg, bool& is_interested, const onixs_ice::MessageInfo& ) { - //TODO need to find out how to produce data for testing - //on_uds_future_spread(msg, is_interested); - UNUSED(msg); - UNUSED(is_interested); + on_uds_future_spread(msg, is_interested); } void ICEListener::onOptionsProductDefinition(const onixs_ice::OptionsProductDefinition& msg, bool& is_interested) @@ -122,19 +125,51 @@ void ICEListener::onNewOptionsStrategyDefinition(const onixs_ice::NewOptionsStra } // -// Gap Recovery -- This is not necessary if we subscribe to the onOrderBookUpdated callback, they handle the breaks transparently + void ICEListener::onMarketSnapshot(const onixs_ice::MarketSnapshot& msg, const onixs_ice::MessageInfo& msg_info) { - UNUSED(msg); UNUSED(msg_info); + auto* data = _index.get_security(msg.marketId); + if (data != nullptr) + { + bts_assert(data->id == msg.marketId); + auto ts = utils::TimeStamp::now(); + auto settle_denominator = data->handle.security().ice_settle_denominator; + if (msg.settlePriceDateTime > 0) { + data->handle.update_settlement( ts, convert_price(msg.settlementPrice, settle_denominator), date_of_timestamp(msg.settlePriceDateTime) ); + } + + LOG(md,Diag,"OnixSICE Got Market Snap : {} - {}", std::to_string(data->id), std::to_string(msg.settlementPrice)); + + auto deal_denominator = data->handle.security().ice_deal_denominator; + data->handle.update_opening( ts, double_of_price(msg.openingPrice, deal_denominator), datetime::today() ); + data->handle.update_session_high( ts, double_of_price(msg.high, deal_denominator) ); + data->handle.update_session_low( ts, double_of_price(msg.low, deal_denominator) ); + + data->handle.set_last_trade( ts, pricesize_of_price_size(msg.lastTradePrice, deal_denominator, msg.lastTradeQuantity), + ll_of_timestamp(msg.lastTradeDateTime), msg.volume ); + } } +void ICEListener::onMarketStatistics(const onixs_ice::MarketStatistics& msg, const onixs_ice::MessageInfo& msg_info) +{ + UNUSED(msg_info); + auto* data = _index.get_security(msg.marketId); + if (data != nullptr) + { + bts_assert(data->id == msg.marketId); + data->handle.set_total_volume(msg.volume); + } +} + +// Gap Recovery -- This is not necessary if we subscribe to the onOrderBookUpdated callback, they handle the breaks transparently void ICEListener::onMarketSnapshotOrder(const onixs_ice::MarketSnapshotOrder& msg, const onixs_ice::MessageInfo& msg_info) { UNUSED(msg); UNUSED(msg_info); } +// Gap Recovery -- This is not necessary if we subscribe to the onOrderBookUpdated callback, they handle the breaks transparently void ICEListener::onMarketSnapshotPriceLevel(const onixs_ice::MarketSnapshotPriceLevel& msg, const onixs_ice::MessageInfo& msg_info) { UNUSED(msg); @@ -151,7 +186,7 @@ void ICEListener::onOrderBookUpdated(const onixs_ice::OrderBook& onixs_book, con if (data == nullptr) return; bts_assert(data->id == onixs_book.marketId()); - auto display_factor = data->handle.security().display_factor; + auto denominator = data->handle.security().ice_price_denominator; // Overwrite all entries every time size_t bids_size = onixs_book.bids().size(); @@ -169,20 +204,21 @@ void ICEListener::onOrderBookUpdated(const onixs_ice::OrderBook& onixs_book, con { auto& bid = onixs_book.bids()[x]; if(x < COCONUT_MAX_DEPTH) - dbids[x] = pricesize_of_price_size(bid.price, display_factor, bid.quantity - bid.impliedQuantity); + dbids[x] = pricesize_of_price_size(bid.price, denominator, bid.quantity - bid.impliedQuantity); if(x < COCONUT_MAX_IMPLIED_DEPTH) - ibids[x] = pricesize_of_price_size(bid.price, display_factor, bid.impliedQuantity); + ibids[x] = pricesize_of_price_size(bid.price, denominator, bid.impliedQuantity); } for (size_t x = 0; x < asks_size; ++x) { auto& ask = onixs_book.offers()[x]; if(x < COCONUT_MAX_DEPTH) - dasks[x] = pricesize_of_price_size(ask.price, display_factor, ask.quantity - ask.impliedQuantity); + dasks[x] = pricesize_of_price_size(ask.price, denominator, ask.quantity - ask.impliedQuantity); if(x < COCONUT_MAX_IMPLIED_DEPTH) - iasks[x] = pricesize_of_price_size(ask.price, display_factor, ask.impliedQuantity); + iasks[x] = pricesize_of_price_size(ask.price, denominator, ask.impliedQuantity); } data->handle.update_books(TimeStamp::now(), dbids, dasks, ibids, iasks, (uint32_t) msg_info.sequenceNumber); -} +} + // // Trades @@ -199,12 +235,13 @@ AggressorSide map_aggressor(onixs_ice::AggressorSide::Enum agside) } } -void on_trade(ICESecurityData* data, const onixs_ice::Price& price, int size, onixs_ice::AggressorSide::Enum agside, const onixs_ice::DateTime& onixs_date) +void on_trade(ICESecurityData* data, const onixs_ice::Price& price, int size, + onixs_ice::AggressorSide::Enum agside, const onixs_ice::DateTime& onixs_date, bool from_spread) { auto ts = utils::TimeStamp::now(); - PriceSize ps = pricesize_of_price_size(price, data->handle.security().display_factor, size); + PriceSize ps = pricesize_of_price_size(price, data->handle.security().ice_deal_denominator, size); auto date = ll_of_timestamp(onixs_date); - data->handle.add_trade(ts, ps, date, map_aggressor(agside), size); + data->handle.add_trade(ts, ps, date, map_aggressor(agside), 0u, from_spread); } void ICEListener::onTrade(const onixs_ice::Trade& msg, const onixs_ice::MessageInfo& ) @@ -216,7 +253,7 @@ void ICEListener::onTrade(const onixs_ice::Trade& msg, const onixs_ice::MessageI auto size = msg.quantity; auto price = msg.price; onixs_ice::AggressorSide::Enum agside = msg.aggressorSide; - on_trade(data, price, size, agside, msg.transactDateTime); + on_trade(data, price, size, agside, msg.transactDateTime, msg.isSystemPricedLeg); } } @@ -264,10 +301,12 @@ void ICEListener::onSettlementPrice(const onixs_ice::SettlementPrice& msg, const if (data != nullptr) { bts_assert(data->id == msg.marketId); - onixs_ice::Price price = msg.settlementPrice; - onixs_ice::DateTime time = msg.dateTime; - auto display_factor = data->handle.security().display_factor; - data->handle.update_settlement(utils::TimeStamp::now(), convert_price(price, display_factor), date_of_timestamp(time)); + auto ts = utils::TimeStamp::now(); + auto price = msg.settlementPrice; + auto time = msg.dateTime; + auto denominator = data->handle.security().ice_settle_denominator; + LOG(md,Diag,"OnixSICE Got Settlement: {} - {}", std::to_string(data->id), std::to_string(price)); + data->handle.update_settlement(ts, convert_price(price, denominator), date_of_timestamp(time)); } } @@ -285,8 +324,8 @@ void ICEListener::onOpenPrice(const onixs_ice::OpenPrice& msg, const onixs_ice:: bts_assert(data->id == msg.marketId); onixs_ice::Price price = msg.openPrice; onixs_ice::DateTime time = msg.dateTime; - auto display_factor = data->handle.security().display_factor; - data->handle.update_opening( utils::TimeStamp::now(), double_of_price(price, display_factor), date_of_timestamp(time)); + auto denominator = data->handle.security().ice_deal_denominator; + data->handle.update_opening( utils::TimeStamp::now(), double_of_price(price, denominator), date_of_timestamp(time)); } } @@ -298,8 +337,8 @@ void ICEListener::onClosePrice(const onixs_ice::ClosePrice& msg, const onixs_ice bts_assert(data->id == msg.marketId); onixs_ice::Price price = msg.closePrice; onixs_ice::DateTime time = msg.dateTime; - auto display_factor = data->handle.security().display_factor; - data->handle.update_closing( utils::TimeStamp::now(), double_of_price(price, display_factor), date_of_timestamp(time)); + auto denominator = data->handle.security().ice_deal_denominator; + data->handle.update_closing( utils::TimeStamp::now(), double_of_price(price, denominator), date_of_timestamp(time)); } } @@ -310,11 +349,16 @@ void ICEListener::onEndOfDayMarketSummary(const onixs_ice::EndOfDayMarketSummary { bts_assert(data->id == msg.marketId); onixs_ice::DateTime time = msg.dateTime; - auto display_factor = data->handle.security().display_factor; - data->handle.update_opening( utils::TimeStamp::now(), double_of_price(msg.openingPrice, display_factor), date_of_timestamp(time) ); - data->handle.update_session_high( utils::TimeStamp::now(), double_of_price(msg.high, display_factor) ); - data->handle.update_session_low( utils::TimeStamp::now(), double_of_price(msg.low, display_factor) ); - data->handle.update_settlement( utils::TimeStamp::now(), convert_price(msg.settlementPrice, display_factor), date_of_timestamp(time) ); + auto ts = utils::TimeStamp::now(); + auto settle_denominator = data->handle.security().ice_settle_denominator; + data->handle.update_settlement( ts, convert_price(msg.settlementPrice, settle_denominator), date_of_timestamp(time) ); + + LOG(md,Warn,"OnixSICE Got EOD Market : {} - {}", std::to_string(data->id), std::to_string(msg.settlementPrice)); + + auto deal_denominator = data->handle.security().ice_deal_denominator; + data->handle.update_opening( ts, double_of_price(msg.openingPrice, deal_denominator), date_of_timestamp(time) ); + data->handle.update_session_high( ts, double_of_price(msg.high, deal_denominator) ); + data->handle.update_session_low( ts, double_of_price(msg.low, deal_denominator) ); } } diff --git a/src/server/sparkd/md/platforms/ice_onixs/ICEListener.h b/src/server/sparkd/md/platforms/ice_onixs/ICEListener.h index 25eacf3aa..b73cf1c49 100644 --- a/src/server/sparkd/md/platforms/ice_onixs/ICEListener.h +++ b/src/server/sparkd/md/platforms/ice_onixs/ICEListener.h @@ -21,7 +21,7 @@ namespace ice { class ICEListener : public onixs_ice::ExchangeListener, //One Thread public onixs_ice::HandlerStateChangeListener, //One thread - public onixs_ice::OrderBookUpdateListener, //One thread //TODO? + public onixs_ice::OrderBookUpdateListener, //One thread public onixs_ice::FeedListener, //Multi thread public onixs_ice::ErrorListener, //Multi thread public onixs_ice::WarningListener //Multi thread @@ -85,6 +85,7 @@ public: void onOpenPrice(const onixs_ice::OpenPrice& msg, const onixs_ice::MessageInfo& msgInfo) override; void onClosePrice(const onixs_ice::ClosePrice& msg, const onixs_ice::MessageInfo& msgInfo) override; void onEndOfDayMarketSummary(const onixs_ice::EndOfDayMarketSummary& msg, const onixs_ice::MessageInfo& msgInfo) override; + void onMarketStatistics(const onixs_ice::MarketStatistics& msg, const onixs_ice::MessageInfo& msgInfo) override; // Feed void onMulticastMessageBlockBegin(onixs_ice::FeedId feedId, const onixs_ice::MessageInfo& msgInfo) override; @@ -104,6 +105,8 @@ private: template void set_security_common(const Definition& def, FixedSecurity& fsec); template + void set_security_specific(const Definition& def, FixedSecurity& fsec); + template void on_future(const Definition& def, bool& is_interested); template void on_uds_future_spread(const Definition& def, bool& is_interested); @@ -121,9 +124,12 @@ private: SecurityFilterAction filter_security(int exchange_id, const Definition& def) { if (_index.has_security(exchange_id)) + { + LOG(md,Warn, "Duplicate exchange ID {} - {}", std::to_string(exchange_id), def.contractSymbol); return SKIP; - // if(_symbol_filter.count("*") > 0) - // return ADD; + } + if(_symbol_filter.count("*") > 0) + return ADD; return _symbol_filter.count( parse_contract_symbol(def.contractSymbol) ) > 0 ? ADD : SKIP; } void flush_books(); @@ -141,16 +147,25 @@ void ICEListener::set_security_common(const Definition& def, FixedSecurity& fsec { fsec.numeric_id = def.marketId; std::string sym = parse_contract_symbol(def.contractSymbol); - fsec.symbol( sym ); - //fsec.security_desc(def.marketDesc); - fsec.security_desc(def.contractSymbol); + if(!sym.empty()) + { + fsec.symbol( sym ); + fsec.security_desc(def.contractSymbol); + } + else + { + auto id = std::to_string(def.marketId); + fsec.symbol(id); + fsec.security_desc(id); + } fsec.exchange = api::Venue::VENUE_ICE; - fsec.display_factor = (int8_t)(def.orderPriceDenominator - '0'); - fsec.ice_deal_display_factor = (int8_t)(def.dealPriceDenominator - '0'); - fsec.ice_settle_display_factor = (int8_t)(def.settlePriceDenominator - '0'); - double min_tick = double_of_price(def.incrementPrice, fsec.display_factor); - fsec.low_tick = fsec.high_tick = min_tick; - fsec.price_break = 0.; + fsec.display_factor = 0.0; + fsec.ice_price_denominator = (int8_t)(def.orderPriceDenominator - '0'); + fsec.ice_deal_denominator = (int8_t)(def.dealPriceDenominator - '0'); + fsec.ice_settle_denominator = (int8_t)(def.settlePriceDenominator - '0'); + double min_tick = double_of_price(def.incrementPrice, fsec.ice_price_denominator); + fsec.low_tick = fsec.high_tick = min_tick; + fsec.price_break = 0.0; fsec.ice_market_description(def.marketDesc); fsec.ice_cleared_alias(def.clearedAlias); @@ -160,6 +175,10 @@ void ICEListener::set_security_common(const Definition& def, FixedSecurity& fsec fsec.mic_code(def.micCode); } +template +void ICEListener::set_security_specific(const Definition&, FixedSecurity& ) {} + + template void ICEListener::on_future(const Definition& def, bool& is_interested) { @@ -169,7 +188,7 @@ void ICEListener::on_future(const Definition& def, bool& is_interested) FixedFutureSecurity fsec; set_security_common(def, fsec); - //fsec.cash_per_point = double_of_price(def.tickValue, fsec.display_factor); + set_security_specific(def, fsec); fsec.expiration_year = def.maturityYear; fsec.expiration_month = def.maturityMonth; @@ -197,11 +216,18 @@ void ICEListener::on_uds_future_spread(const Definition& def, bool& is_intereste for(size_t x = 0; x < fsec.leg_count(); ++x) { const auto& leg = legs[x]; - if(leg.ratioPriceNumerator) - fsec.legs[x].leg_price = double_of_price(leg.ratioPriceNumerator.value(), leg.ratioPriceDenominator.value()); - + if(leg.ratioPriceNumerator && leg.ratioPriceDenominator) + { + auto num = *leg.ratioPriceNumerator; + auto denom = *leg.ratioPriceDenominator; + fsec.legs[x].leg_price = num / (double) denom; + } if(leg.ratioQtyNumerator && leg.ratioQtyDenominator) - fsec.legs[x].leg_delta = double_of_price(leg.ratioQtyNumerator.value(), leg.ratioQtyDenominator.value()); + { + auto num = *leg.ratioQtyNumerator; + auto denom = *leg.ratioQtyDenominator; + fsec.legs[x].leg_delta = num / (double) denom; + } fsec.legs[x].leg_quantity = leg.ratio; fsec.legs[x].leg_side = leg.side == onixs_ice::Side::Bid ? api::SIDE_BUY : api::SIDE_SELL; diff --git a/src/server/sparkd/md/platforms/ice_onixs/ICEListenerHelpers.h b/src/server/sparkd/md/platforms/ice_onixs/ICEListenerHelpers.h index 7efa7b049..34d6f09f4 100644 --- a/src/server/sparkd/md/platforms/ice_onixs/ICEListenerHelpers.h +++ b/src/server/sparkd/md/platforms/ice_onixs/ICEListenerHelpers.h @@ -16,25 +16,25 @@ namespace md { namespace ice { template -double double_of_price(const OnixPrice& price, int8_t display_factor) +double double_of_price(const OnixPrice& price, int8_t denominator) { - double dbl = price / pow10(display_factor); + double dbl = price / pow10(denominator); return dbl; } template -IntPrice convert_price(const OnixsPrice& price, int8_t display_factor) +IntPrice convert_price(const OnixsPrice& price, int8_t denominator) { - double dbl = double_of_price(price, display_factor); + double dbl = double_of_price(price, denominator); const int PRECISION = 7; //TODO config::get().connection() case ICE auto intpx = md::IntPrice_from_double(dbl); return intpx; } template -inline PriceSize pricesize_of_price_size(const OnixsPrice& price, int8_t display_factor, Size size) +inline PriceSize pricesize_of_price_size(const OnixsPrice& price, int8_t denominator, Size size) { - auto intpx = convert_price(price, display_factor); + auto intpx = convert_price(price, denominator); PriceSize result({intpx.mantissa(), (int8_t)(intpx.exponent())}, size); return result; } diff --git a/src/server/sparkd/md/platforms/ice_onixs/ICEMain.cpp b/src/server/sparkd/md/platforms/ice_onixs/ICEMain.cpp index ea2b3ef9b..c9cb6a79b 100644 --- a/src/server/sparkd/md/platforms/ice_onixs/ICEMain.cpp +++ b/src/server/sparkd/md/platforms/ice_onixs/ICEMain.cpp @@ -74,11 +74,30 @@ onixs_ice::MarketSubscriptions market_subcription_for_channel(const ICEChannel& for(const auto& x : chan.market_types) { //onixs_ice::MarketSubType::NonImplied - onixs_ice::MarketSubscription futures(x, onixs_ice::MarketSubType::FullImplied, onixs_ice::SecurityType::Futures, onixs_ice::BookDepth::FOD); - onixs_ice::MarketSubscription spreads(x, onixs_ice::MarketSubType::FullImplied, onixs_ice::SecurityType::UdsFuturesMarkets, onixs_ice::BookDepth::FOD); - subs.push_back(futures); - subs.push_back(spreads); + if(x == 26) + { + onixs_ice::MarketSubscription futures(x, onixs_ice::MarketSubType::NonImplied, onixs_ice::SecurityType::Futures, onixs_ice::BookDepth::FOD); + subs.push_back(futures); + } + else + { + onixs_ice::MarketSubscription futures(x, onixs_ice::MarketSubType::FullImplied, onixs_ice::SecurityType::Futures, onixs_ice::BookDepth::FOD); + subs.push_back(futures); + } } + for(const auto& x : chan.uds_market_types) + { + if(x == 26) + { + onixs_ice::MarketSubscription spreads(x, onixs_ice::MarketSubType::NonImplied, onixs_ice::SecurityType::UdsFuturesMarkets, onixs_ice::BookDepth::FOD); + subs.push_back(spreads); + } + else + { + onixs_ice::MarketSubscription spreads(x, onixs_ice::MarketSubType::FullImplied, onixs_ice::SecurityType::UdsFuturesMarkets, onixs_ice::BookDepth::FOD); + subs.push_back(spreads); + } + } return subs; } @@ -113,7 +132,16 @@ void ICEMain::start(std::vector& symbols) LOG(md,Info, "ICE: Init Handler {}", pair.first.to_string()); sleep_for(16s); onixs_ice::MarketSubscriptions subs = market_subcription_for_channel(pair.first); - pair.second->start(subs); + try + { + pair.second->start(subs); + } + catch (std::exception& e) + { + LOG(md,Error,"handler->start ONIXSERROR {}", std::string(e.what())); + throw; + } + } onixs_destroy_temporary_license(license); LOG(md,Info, "** ICE Publisher started **"); @@ -191,8 +219,9 @@ unique_ptr ICEMain::prepare_handler(const ICEChannel& channe auto handler = unique_ptr(new onixs_ice::Handler(settings)); return handler; } - catch (std::exception& e) { - LOG(md,Error,"ONIXSERROR{}", std::string(e.what())); + catch (std::exception& e) + { + LOG(md,Error,"handler create ONIXSERROR {}", std::string(e.what())); throw; } } @@ -212,13 +241,20 @@ unique_ptr ICEMain::prepare_listener(onixs_ice::Handler& handler, c for (auto& symbol : channel.symbols) listener->add_filter_symbol(symbol.symbol()); - handler.registerExchangeListener(listener.get()); - handler.registerFeedListener(listener.get()); - handler.registerOrderBookUpdateListener(listener.get()); - handler.registerErrorListener(listener.get()); - handler.registerWarningListener(listener.get()); - handler.registerHandlerStateChangeListener(listener.get()); - + try + { + handler.registerExchangeListener(listener.get()); + handler.registerFeedListener(listener.get()); + handler.registerOrderBookUpdateListener(listener.get()); + handler.registerErrorListener(listener.get()); + handler.registerWarningListener(listener.get()); + handler.registerHandlerStateChangeListener(listener.get()); + } + catch (std::exception& e) + { + LOG(md,Error,"handler register ONIXSERROR {}", std::string(e.what())); + throw; + } return listener; } diff --git a/src/server/sparkd/md/platforms/mdp3_onixs/MDP3Listener.cpp b/src/server/sparkd/md/platforms/mdp3_onixs/MDP3Listener.cpp index 90684219c..f986b5b3c 100644 --- a/src/server/sparkd/md/platforms/mdp3_onixs/MDP3Listener.cpp +++ b/src/server/sparkd/md/platforms/mdp3_onixs/MDP3Listener.cpp @@ -249,7 +249,7 @@ void MDP3Listener::add_security(const FixedSecurity& security, co2::TradingStatu // Add it to the index so it can receive messages _index.add_security(security.numeric_id, handle); - handle.update_exchange_status( utils::TimeStamp::now(), status); + handle.update_exchange_status(utils::TimeStamp::now(), status); } void MDP3Listener::flush_books() { @@ -282,6 +282,21 @@ void MDP3Listener::update_all_status(CoconutSecurityStatus status) { }); } +void MDP3Listener::update_group_exchange_status(const onixs::StrRef& group, co2::TradingStatus status) { + _index.walk_security_list([=](SecurityData& data) { + if (data.handle.security().alt_symbol() == group) { + data.handle.update_exchange_status(utils::TimeStamp::now(),status); + } + }); +} +void MDP3Listener::update_asset_exchange_status(const onixs::StrRef& asset, co2::TradingStatus status) { + _index.walk_security_list([=](SecurityData& data) { + if (data.handle.security().symbol() == asset) { + data.handle.update_exchange_status(utils::TimeStamp::now(),status); + } + }); +} + md::MDP3Listener::BookData::BookData(MDProducerHandle & handle) : handle(handle), type_flags(0), bids(), asks() { } }}//namespace diff --git a/src/server/sparkd/md/platforms/mdp3_onixs/MDP3Listener.h b/src/server/sparkd/md/platforms/mdp3_onixs/MDP3Listener.h index f2d5d5e8a..25565b409 100644 --- a/src/server/sparkd/md/platforms/mdp3_onixs/MDP3Listener.h +++ b/src/server/sparkd/md/platforms/mdp3_onixs/MDP3Listener.h @@ -203,6 +203,17 @@ public: } } } + else if (!args.message().securityGroup().empty()) { + auto status = security_trading_status(args.message()); + if (status) { + if (args.message().asset().empty()) { + update_group_exchange_status(args.message().securityGroup(), status.value()); + } + else { + update_asset_exchange_status(args.message().asset(), status.value()); + } + } + } } void onMessage(onixs::Handler&, const onixs::IncrementalRefreshOrderBook43Args& args) override { @@ -429,6 +440,8 @@ private: } void flush_books(); void update_all_status(co2::CoconutSecurityStatus status); + void update_group_exchange_status(const onixs::StrRef&, co2::TradingStatus); + void update_asset_exchange_status(const onixs::StrRef&, co2::TradingStatus); CMESecurityIndex& _index; MDProducer& _producer; diff --git a/src/server/sparkd/md/run_md.cpp b/src/server/sparkd/md/run_md.cpp index 35ded097d..a63c5f7c4 100644 --- a/src/server/sparkd/md/run_md.cpp +++ b/src/server/sparkd/md/run_md.cpp @@ -153,8 +153,9 @@ void run_market_datapublishers(MBOWriter* mbo) { })); if (config::get().use_sim()) { - threading::make_thread("ExchangeSim", [mbo]() { sim::start_exchange_sim(mbo); }) - .detach(); + threading::make_thread("ExchangeSimGLBX", [mbo]() { sim::start_exchange_sim(api::Venue::VENUE_SIM, mbo); }).detach(); + //threading::make_thread("ExchangeSimICE", [mbo]() { sim::start_exchange_sim(api::Venue::VENUE_SIMICE, mbo); }).detach(); + //TODO I'll need Eric's help to disambiguate replay / relay here } boot_sleep(); } diff --git a/src/server/sparkd/subprocess/SubProcessRunner.cpp b/src/server/sparkd/subprocess/SubProcessRunner.cpp new file mode 100644 index 000000000..03c4bb20c --- /dev/null +++ b/src/server/sparkd/subprocess/SubProcessRunner.cpp @@ -0,0 +1,61 @@ +#include "SubProcessRunner.h" +#include "lib/config/config.h" +#include "lib/config/protos/subprocess.pb.h" +#include "lib/logging/logging.h" +// #include +#include +#include +#include +#include + +namespace bts { +namespace subprocess { + + pid_t start_proc(const Process& p) { + std::string friendly_name = p.exe(); + for (const auto& a : p.args()) { + friendly_name += " \""; + friendly_name += a; + friendly_name += "\""; + } + // Fork + pid_t pid = fork(); + if (pid) { + //parent + if (pid == -1) { + LOG(Gen,Error, "Failed to launch subprocess '{}'", friendly_name); + } + else { + LOG(Gen,Info, "Launched subprocess '{}'", friendly_name); + } + return pid; + } + // Child + + // Register "hangup" on parent process terminated + prctl(PR_SET_PDEATHSIG, SIGKILL); + // Setup environment + for (auto& var : p.env()) { + ::setenv(var.name().c_str(), var.value().c_str(), true); + } + // organize argv + char** argv = new char*[128]; + int argc = -1; + argv[++argc] = (char*)p.exe().c_str(); + for (const auto& arg : p.args()) { + argv[++argc] = (char*)arg.c_str(); + } + argv[++argc] = nullptr; + // And away we go + int result = execvp(p.exe().c_str(), argv); + LOG(Gen,Error, "Failed to exec subprocess '{}' result: {}", friendly_name, result); + return -1; + } + + void run_all() { + auto& procs = config::get(); + for (const auto& p : procs.process()) { + start_proc(p); + } + } +}} //namespace diff --git a/src/server/sparkd/subprocess/SubProcessRunner.h b/src/server/sparkd/subprocess/SubProcessRunner.h new file mode 100644 index 000000000..c64355ca8 --- /dev/null +++ b/src/server/sparkd/subprocess/SubProcessRunner.h @@ -0,0 +1,6 @@ +#pragma once + +namespace bts { +namespace subprocess { + void run_all(); +}} //namespace diff --git a/src/server/sparkd/support/ShellLatencyServer.cpp b/src/server/sparkd/support/ShellLatencyServer.cpp index eb5657b87..ba0cb791e 100644 --- a/src/server/sparkd/support/ShellLatencyServer.cpp +++ b/src/server/sparkd/support/ShellLatencyServer.cpp @@ -55,6 +55,11 @@ void ShellLatencyServer::report( } } +void ShellLatencyServer::ping( + const ::bts::metrics::BufferMessage& request, + const api::Response<::bts::metrics::BufferMessage>& reply) { + reply(request); +} }} \ No newline at end of file diff --git a/src/server/sparkd/support/ShellLatencyServer.h b/src/server/sparkd/support/ShellLatencyServer.h index 3c68833ea..d31c302b0 100644 --- a/src/server/sparkd/support/ShellLatencyServer.h +++ b/src/server/sparkd/support/ShellLatencyServer.h @@ -28,6 +28,10 @@ class ShellLatencyServer : public ShellLatencyServiceBase { void report( const ::bts::metrics::Report& request, const api::Response<::bts::api::VoidMessage>& reply) override; + + void ping( + const ::bts::metrics::BufferMessage& request, + const api::Response<::bts::metrics::BufferMessage>& reply) override; }; diff --git a/src/server/sparkd/user/AdminServiceServer.cpp b/src/server/sparkd/user/AdminServiceServer.cpp index b795c7f39..db3a0c7ad 100644 --- a/src/server/sparkd/user/AdminServiceServer.cpp +++ b/src/server/sparkd/user/AdminServiceServer.cpp @@ -194,10 +194,10 @@ void AdminServiceServer::get_filtered_admin_data( newuser->CopyFrom(*acctuser); if(!unit.admin_permission().has_md_permissions()) newuser->mutable_user_data()->mutable_exchange_user_data()-> - mutable_cme_data()->set_admin_display_flag(bts::api::admin::CMEUserData_AdminDisplay::CMEUserData_AdminDisplay_Routing); + set_admin_display_flag(bts::api::admin::ExchangeUserData_AdminDisplay::ExchangeUserData_AdminDisplay_Routing); else newuser->mutable_user_data()->mutable_exchange_user_data()-> - mutable_cme_data()->set_admin_display_flag(bts::api::admin::CMEUserData_AdminDisplay::CMEUserData_AdminDisplay_All); + set_admin_display_flag(bts::api::admin::ExchangeUserData_AdminDisplay::ExchangeUserData_AdminDisplay_All); } } } @@ -212,7 +212,7 @@ void AdminServiceServer::get_filtered_admin_data( const auto newuser = filtered.add_users(); newuser->CopyFrom(*acctuser); newuser->mutable_user_data()->mutable_exchange_user_data()-> - mutable_cme_data()->set_admin_display_flag(bts::api::admin::CMEUserData_AdminDisplay::CMEUserData_AdminDisplay_MD); + set_admin_display_flag(bts::api::admin::ExchangeUserData_AdminDisplay::ExchangeUserData_AdminDisplay_MD); } } } diff --git a/src/server/sparkd/user/AuthServiceServer.cpp b/src/server/sparkd/user/AuthServiceServer.cpp index 357a683c9..9853fb0d4 100644 --- a/src/server/sparkd/user/AuthServiceServer.cpp +++ b/src/server/sparkd/user/AuthServiceServer.cpp @@ -33,6 +33,12 @@ AuthServiceServer::AuthServiceServer (bts::api::ServiceContext& context) : AuthS AuthServiceServer::~AuthServiceServer () {} +void AuthServiceServer::clear_all_sessions(const api::VoidMessage& , + const std::function& ) +{ + api::auth::clear_all_sessions(); +} + void AuthServiceServer::get_users(const api::user::GetUsersRequest& , const std::function& respond) { diff --git a/src/server/sparkd/user/AuthServiceServer.h b/src/server/sparkd/user/AuthServiceServer.h index 91df48a8e..0e00cc334 100644 --- a/src/server/sparkd/user/AuthServiceServer.h +++ b/src/server/sparkd/user/AuthServiceServer.h @@ -20,6 +20,9 @@ public: void authenticate_login(const api::user::AuthenticateLoginRequest& , const std::function& ) override; + void clear_all_sessions(const api::VoidMessage& , + const std::function& ) override; + private: struct PImpl; std::unique_ptr me; diff --git a/src/web/api/src/ServiceContext.ts b/src/web/api/src/ServiceContext.ts index cd448ec86..4769e5b6a 100644 --- a/src/web/api/src/ServiceContext.ts +++ b/src/web/api/src/ServiceContext.ts @@ -4,7 +4,7 @@ import { assert, assertValid } from 'bts_assert' function default_ws_uri() { if (typeof window == 'undefined') return 'ws://sparkvm:8000/' const loc = window.location - if (loc.hostname === "localhost") { + if (loc.hostname === "localhost" && loc.port === '8000') { return "ws://sparkvm:8000/" } let ws_uri = "ws:"; diff --git a/src/web/ui/src/Access.fs b/src/web/ui/src/Access.fs index 45d1fb9b4..883319c04 100644 --- a/src/web/ui/src/Access.fs +++ b/src/web/ui/src/Access.fs @@ -81,29 +81,19 @@ module Accounts = { cmeAccountData = { cti = CTICode.CtiCodeUndefined origin = CustomerOrFirm.CustomerOrFirmCustomer - takeupMemberCode = "" clearingAccount = "" - directElectronicAccess = "" - tradingCapacity = "" - liquidityProvision = "" - commodityDerivativeIndicator = "" - investmentDecisionId = "" - executionDecisionId = "" - clientId = "" miscTags = JSMap.empty } iceAccountData = - { nybitHouse="" - wceHouse=0 - clearingAccountOverride="" - directElectronicAccess=false - tradingCapacity="" - liquidityProvision=false - commodityDerivativeIndicator=false - investmentDecisionId="" - executionDecisionId="" - clientId="" - mfidId="" } + { + cti = CTICode.CtiCodeUndefined + origin = CustomerOrFirm.CustomerOrFirmCustomer + clearingAccount = "" + clearingFirm = "" + customerAccountRefId = "" + accountCode = "" + miscTags = JSMap.empty + } simAccountData = { cti = CTICode.CtiCodeUndefined origin = CustomerOrFirm.CustomerOrFirmCustomer diff --git a/src/web/ui/src/Accounts/State.fs b/src/web/ui/src/Accounts/State.fs index 4647425ee..48a628c19 100644 --- a/src/web/ui/src/Accounts/State.fs +++ b/src/web/ui/src/Accounts/State.fs @@ -207,14 +207,18 @@ let applyUpdate (account:BTSAccount) = function let applyCMEUpdate (data:CMEAccountData) = function | CMEExchangeField.CTI v -> { data with cti = v } | CMEExchangeField.Origin v -> { data with origin = v } - | CMEExchangeField.TakeupMember v -> { data with takeupMemberCode = v } | CMEExchangeField.ClearingAccount v -> { data with clearingAccount = v } - | CMEExchangeField.DirectElectronicAccess v -> { data with directElectronicAccess = v } - | CMEExchangeField.TradingCapacity v -> { data with tradingCapacity = v } - | CMEExchangeField.LiquidityProvision v -> { data with liquidityProvision = v } - | CMEExchangeField.CommodityDerivativeIndicator v -> { data with commodityDerivativeIndicator = v } | CMEExchangeField.MiscTagMapping mp -> { data with miscTags = mp } +let applyICEUpdate (data:ICEAccountData) = function + | ICEExchangeField.CTI v -> { data with cti = v } + | ICEExchangeField.Origin v -> { data with origin = v } + | ICEExchangeField.ClearingFirm v -> { data with clearingFirm = v } + | ICEExchangeField.ClearingAccount v -> { data with clearingAccount = v } + | ICEExchangeField.MiscTagMapping mp -> { data with miscTags = mp } + | ICEExchangeField.CustomerAccountRefId v -> { data with customerAccountRefId = v } + | ICEExchangeField.AccountCode v -> { data with accountCode = v } + let applySIMUpdate (data:SIMAccountData) = function | CTI v -> { data with cti = v } //| SIMExchangeField.Origin v -> { data with origin = v } @@ -576,6 +580,10 @@ let update msg (model:Model) : Model * Cmd = let newCme = applyCMEUpdate model.scratch.exchangeAccountData.cmeAccountData field let scratch = { model.scratch with exchangeAccountData = { cmeAccountData = newCme; iceAccountData = model.scratch.exchangeAccountData.iceAccountData; simAccountData = model.scratch.exchangeAccountData.simAccountData } } { model with scratch = scratch }, Cmd.Empty + | UpdateICE (field:ICEExchangeField) -> + let newICE = applyICEUpdate model.scratch.exchangeAccountData.iceAccountData field + let scratch = { model.scratch with exchangeAccountData = { cmeAccountData = model.scratch.exchangeAccountData.cmeAccountData; iceAccountData = newICE; simAccountData = model.scratch.exchangeAccountData.simAccountData } } + { model with scratch = scratch }, Cmd.Empty | UpdateSIM field -> let newSim = applySIMUpdate model.scratch.exchangeAccountData.simAccountData field let scratch = { model.scratch with exchangeAccountData = { cmeAccountData = model.scratch.exchangeAccountData.cmeAccountData ; iceAccountData = model.scratch.exchangeAccountData.iceAccountData; simAccountData = newSim } } diff --git a/src/web/ui/src/Accounts/Types.fs b/src/web/ui/src/Accounts/Types.fs index a0bf2442f..fa7daafcc 100644 --- a/src/web/ui/src/Accounts/Types.fs +++ b/src/web/ui/src/Accounts/Types.fs @@ -17,7 +17,7 @@ type Tab = | PositionManagementTab | MarginTab -let venues = [| Venue.GLBX; Venue.SIM |] +let venues = [| Venue.GLBX; Venue.ICE; Venue.SIM |] let securityTypes = [| SecurityType.FUTURE; SecurityType.SPREAD |] @@ -32,12 +32,16 @@ type AccountField = type CMEExchangeField = | CTI of CTICode | Origin of CustomerOrFirm - | TakeupMember of string | ClearingAccount of string - | DirectElectronicAccess of string - | TradingCapacity of string - | LiquidityProvision of string - | CommodityDerivativeIndicator of string + | MiscTagMapping of JSMap + +type ICEExchangeField = + | CTI of CTICode + | Origin of CustomerOrFirm + | ClearingAccount of string + | ClearingFirm of string + | CustomerAccountRefId of string + | AccountCode of string | MiscTagMapping of JSMap type SIMExchangeField = @@ -113,6 +117,7 @@ type Msg = | DeleteSecurityLimit of int // ExchangeData | UpdateCME of CMEExchangeField + | UpdateICE of ICEExchangeField | UpdateSIM of SIMExchangeField | RequestProducts | UpdateProducts of Product[] diff --git a/src/web/ui/src/Accounts/View.fs b/src/web/ui/src/Accounts/View.fs index 1c624e10e..5a7f68640 100644 --- a/src/web/ui/src/Accounts/View.fs +++ b/src/web/ui/src/Accounts/View.fs @@ -1108,19 +1108,30 @@ let creditLimitsView model dispatch = div [] [str "lorem ipsum"] let exchangeSettings (model:Model) dispatch = let cme = model.scratch.exchangeAccountData.cmeAccountData + let ice = model.scratch.exchangeAccountData.iceAccountData let sim = model.scratch.exchangeAccountData.simAccountData - let send = UpdateCME>>dispatch + let sendCME = UpdateCME>>dispatch + let sendICE = UpdateICE>>dispatch let sendSim = UpdateSIM>>dispatch div [] [ h2 [Class "is-size-3"] [ str "CME" ] - Fields.enumSelect "CTI" (toHuman false CTICode.values cme.cti) (allToHuman false CTICode.values) [] (fromHuman CTICode.values>>CMEExchangeField.CTI>>send) - Fields.enumSelect "Origin" (toHuman false CustomerOrFirm.values cme.origin) (allToHuman false CustomerOrFirm.values) [] (fromHuman CustomerOrFirm.values>>CMEExchangeField.Origin>>send) + Fields.enumSelect "CTI" (toHuman false CTICode.values cme.cti) (allToHuman false CTICode.values) [] (fromHuman CTICode.values>>CMEExchangeField.CTI>>sendCME) + Fields.enumSelect "Origin" (toHuman false CustomerOrFirm.values cme.origin) (allToHuman false CustomerOrFirm.values) [] (fromHuman CustomerOrFirm.values>>CMEExchangeField.Origin>>sendCME) //Fields.text "Takeup Member Code" cme.takeupMemberCode [] (TakeupMember>>send) - Fields.text "Clearing Account" cme.clearingAccount [MaxLength 12.0] (CMEExchangeField.ClearingAccount>>send) + Fields.text "Clearing Account" cme.clearingAccount [MaxLength 12.0] (CMEExchangeField.ClearingAccount>>sendCME) //Fields.text "Direct Electronic Access" cme.directElectronicAccess [] (DirectElectronicAccess>>send) //Fields.text "Trading Capacity" cme.tradingCapacity [] (TradingCapacity>>send) //Fields.text "Liquidity Provision" cme.liquidityProvision [] (LiquidityProvision>>send) //Fields.map "Misc Tag Mapping" cme.miscTags [] (MiscTagMapping>>send) + + h2 [Class "is-size-3"] [ str "ICE" ] + Fields.enumSelect "CTI" (toHuman false CTICode.values ice.cti) (allToHuman false CTICode.values) [] (fromHuman CTICode.values>>ICEExchangeField.CTI>>sendICE) + Fields.enumSelect "Origin" (toHuman false CustomerOrFirm.values ice.origin) (allToHuman false CustomerOrFirm.values) [] (fromHuman CustomerOrFirm.values>>ICEExchangeField.Origin>>sendICE) + Fields.text "Clearing Firm" ice.clearingFirm [] (ICEExchangeField.ClearingFirm>>sendICE) + Fields.text "Clearing Account" ice.clearingAccount [MaxLength 12.0] (ICEExchangeField.ClearingAccount>>sendICE) + Fields.text "Customer Account Ref ID" ice.customerAccountRefId [] (ICEExchangeField.CustomerAccountRefId>>sendICE) + Fields.text "Account Code" ice.accountCode [] (ICEExchangeField.AccountCode>>sendICE) + h2 [Class "is-size-3"] [ str "SIM" ] Fields.enumSelect "CTI" (toHuman false CTICode.values sim.cti) (allToHuman false CTICode.values) [] (fromHuman CTICode.values>>SIMExchangeField.CTI>>sendSim) Fields.enumSelect "Origin" (toHuman false CustomerOrFirm.values sim.origin) (allToHuman false CustomerOrFirm.values) [] (fromHuman CustomerOrFirm.values>>SIMExchangeField.Origin>>sendSim) diff --git a/src/web/ui/src/Controls/Fields.fs b/src/web/ui/src/Controls/Fields.fs index 6e8908f76..2a80caaa1 100644 --- a/src/web/ui/src/Controls/Fields.fs +++ b/src/web/ui/src/Controls/Fields.fs @@ -150,6 +150,7 @@ let password (lbl:string) (dv:string) (attrs:IHTMLProp list) onchange = ] @ attrs) ] ] + let number (lbl:string) (dv:string) (attrs:IHTMLProp list) (validate: string -> bool) onchange = let errorClass = match dv |> validate with @@ -183,6 +184,10 @@ let positiveIntNumber (lbl:string) (dv:string) (attrs:IHTMLProp list) onchange = number lbl dv attrs validate onchange + +let number_novalidate (lbl:string) (dv) (attrs:IHTMLProp list) onchange = + text lbl (string dv) attrs (unbox>>onchange) + let double (lbl:string) (dv:double) (attrs:IHTMLProp list) onchange = text lbl (string dv) attrs (double>>onchange) diff --git a/src/web/ui/src/Users/State.fs b/src/web/ui/src/Users/State.fs index d59dd43a1..a413d9d3f 100644 --- a/src/web/ui/src/Users/State.fs +++ b/src/web/ui/src/Users/State.fs @@ -53,16 +53,12 @@ let defaultScratchUser = { accountPermissions = { accountPermissions = JSMap.empty } exchangeUserData = { + adminDisplayFlag = bts.api.admin.ExchangeUserData.AdminDisplay.None cmeData = { - adminDisplayFlag = bts.api.admin.CMEUserData.AdminDisplay.None operatorId = "" - atsOperatorId = "" smpId = "" - textA = "" - textB = "" miscTags = JSMap.empty smpInstruction = "" - maxOrderRatePerConnection = "" location = "" proNonpro = ProfessionalDesignation.PRO displayNondisplay = DisplayDesignation.DISPLAY @@ -73,25 +69,26 @@ let defaultScratchUser = { location = "" } iceData = { + companyId = 0 + operatorId = "" authorizedTrader = "" - memoField = "" - textB = "" - maxOrderRate = 0ul + authorizedGroup = "" + password = "" + custOrderHandingInst = "" + mifidDirectElectronicAccess = -1 + mifidTradingCapacity = -1 + mifidLiquidityProvision = -1 + mifidCommodityDerivIndicator = -1 + mifidInvestmentDecision = -1L + mifidExecutionDecision = -1L + mifidClientIdCode = -1L + mifidId = -1L + miscTags = JSMap.empty + proNonpro = ProfessionalDesignation.PRO + displayNondisplay = DisplayDesignation.DISPLAY + mdPermissions = [||] } } - exchangeMdEntitlementData = - { - cmeData = - { - cbotEntitlement = { canView = false } - cmeEntitlement = { canView = false } - nymexEntitlement = { canView = false } - bmandfEntitlement = { canView = false } - comexEntitlement = { canView = false } - cmeEuropeEntitlement = { canView = false } - } - iceData = () - } } } @@ -182,21 +179,16 @@ let createUser (admin:IAdminService) (user:BTSUser) (model:Model)= promise { return ErrorOccured (sprintf "Error creating user: %s" result.errorText) } -let updateExchangeMdEntitlementDataCMEData user dta :BTSUser = { user with userData = { user.userData with exchangeMdEntitlementData = { user.userData.exchangeMdEntitlementData with cmeData = dta}}} +//let updateExchangeMdEntitlementDataCMEData user dta :BTSUser = { user with userData = { user.userData with exchangeMdEntitlementData = { user.userData.exchangeMdEntitlementData with cmeData = dta}}} let updateCMEExchangeUserData (user:BTSUser) dta = { user with userData = { user.userData with exchangeUserData = { user.userData.exchangeUserData with cmeData = dta }}} let updateSIMExchangeUserData (user:BTSUser) dta = { user with userData = { user.userData with exchangeUserData = { user.userData.exchangeUserData with simData = dta }}} +let updateICEExchangeUserData (user:BTSUser) dta = { user with userData = { user.userData with exchangeUserData = { user.userData.exchangeUserData with iceData = dta }}} let default_user_account = { - tradingPermission = + tradingPermission = { hasPermissions = false - submitMarketOrders = false - submitGtcOrders = false - algoTradingPermission = - { submitIcebergOrders = false - submitTimeBoxOrders = false - submitSpreaderOrders = false } } - adminPermission = + adminPermission = { hasPermissions = false hasMdPermissions = false } } @@ -211,15 +203,32 @@ let applyUpdate (field:UserField) (user:BTSUser) = | IsActive ia -> { user with isActive = ia } | CanCreate cr -> { user with canCreate = cr } | CMEOperatorID oid -> updateCMEExchangeUserData user { user.userData.exchangeUserData.cmeData with operatorId = oid } - | CMEATSOperatorID atsoid -> updateCMEExchangeUserData user { user.userData.exchangeUserData.cmeData with atsOperatorId = atsoid } + + | ICECompanyID oid -> updateICEExchangeUserData user { user.userData.exchangeUserData.iceData with companyId = oid } + | ICEOperatorID oid -> updateICEExchangeUserData user { user.userData.exchangeUserData.iceData with operatorId = oid } + | ICEAuthorizedTrader oid -> updateICEExchangeUserData user { user.userData.exchangeUserData.iceData with authorizedTrader = oid } + | ICEAuthorizedGroup oid -> updateICEExchangeUserData user { user.userData.exchangeUserData.iceData with authorizedGroup = oid } + | ICEPassword oid -> updateICEExchangeUserData user { user.userData.exchangeUserData.iceData with password = oid } + | ICECustOrderHandingInst oid -> updateICEExchangeUserData user { user.userData.exchangeUserData.iceData with custOrderHandingInst = oid } + + | MifidClientIdCode value -> updateICEExchangeUserData user { user.userData.exchangeUserData.iceData with mifidClientIdCode = value } + | MifidCommodityDerivIndicator value -> updateICEExchangeUserData user { user.userData.exchangeUserData.iceData with mifidCommodityDerivIndicator = value } + | MifidDirectElectronicAccess value -> updateICEExchangeUserData user { user.userData.exchangeUserData.iceData with mifidDirectElectronicAccess = value } + | MifidExecutionDecision value -> updateICEExchangeUserData user { user.userData.exchangeUserData.iceData with mifidExecutionDecision = value } + | MifidId value -> updateICEExchangeUserData user { user.userData.exchangeUserData.iceData with mifidId = value } + | MifidInvestmentDecision value -> updateICEExchangeUserData user { user.userData.exchangeUserData.iceData with mifidInvestmentDecision = value } + | MifidLiquidityProvision value -> updateICEExchangeUserData user { user.userData.exchangeUserData.iceData with mifidLiquidityProvision = value } + | MifidTradingCapacity value -> updateICEExchangeUserData user { user.userData.exchangeUserData.iceData with mifidTradingCapacity = value } + | SimOperatorID oid -> updateSIMExchangeUserData user { user.userData.exchangeUserData.simData with operatorId = oid } | SMPID smpid -> updateCMEExchangeUserData user { user.userData.exchangeUserData.cmeData with smpId = smpid } | CMELocation loc -> updateCMEExchangeUserData user { user.userData.exchangeUserData.cmeData with location = loc } | CMEProNonPro loc -> updateCMEExchangeUserData user { user.userData.exchangeUserData.cmeData with proNonpro = loc } | CMEDisplayNonDisplay loc -> updateCMEExchangeUserData user { user.userData.exchangeUserData.cmeData with displayNondisplay = loc } + | ICEProNonPro loc -> updateICEExchangeUserData user { user.userData.exchangeUserData.iceData with proNonpro = loc } + | ICEDisplayNonDisplay loc -> updateICEExchangeUserData user { user.userData.exchangeUserData.iceData with displayNondisplay = loc } | SimLocation loc -> updateSIMExchangeUserData user { user.userData.exchangeUserData.simData with location = loc } | SMPInstruction smpi -> updateCMEExchangeUserData user { user.userData.exchangeUserData.cmeData with smpInstruction = smpi } - | MaxOrderRatePerConnection morpc -> updateCMEExchangeUserData user { user.userData.exchangeUserData.cmeData with maxOrderRatePerConnection = morpc } | MiscTags map -> updateCMEExchangeUserData user { user.userData.exchangeUserData.cmeData with miscTags = map } | CMEMic mic -> let micArr = @@ -232,6 +241,17 @@ let applyUpdate (field:UserField) (user:BTSUser) = mdperm | None -> Array.concat [[|mic|]; mdperm] updateCMEExchangeUserData user { user.userData.exchangeUserData.cmeData with mdPermissions = micArr } + | ICEMic (mic:ICEMicData) -> + let micArr = + let mdperm = user.userData.exchangeUserData.iceData.mdPermissions + let found = Array.tryFind (fun (elm:ICEMicData) -> elm.mic = mic.mic) mdperm + match found with + | Some elm -> + let ndx = Array.findIndex (fun (e:ICEMicData) -> e.mic = elm.mic) mdperm + Array.set mdperm ndx mic + mdperm + | None -> Array.concat [[|mic|]; mdperm] + updateICEExchangeUserData user { user.userData.exchangeUserData.iceData with mdPermissions = micArr } | TradingPerm (id:string, can_trade:bool) -> let find = JSMap.tryFind id user.userData.accountPermissions.accountPermissions let current = if find.IsNone then default_user_account else find.Value diff --git a/src/web/ui/src/Users/Types.fs b/src/web/ui/src/Users/Types.fs index 51b83e88a..130711a57 100644 --- a/src/web/ui/src/Users/Types.fs +++ b/src/web/ui/src/Users/Types.fs @@ -18,6 +18,23 @@ type UserField = | CanCreate of bool | CMEOperatorID of string | CMEATSOperatorID of string +| ICEOperatorID of string +| ICECompanyID of int +| ICEAuthorizedTrader of string +| ICEAuthorizedGroup of string +| ICEPassword of string +| ICECustOrderHandingInst of string + +| MifidClientIdCode of int64 +| MifidCommodityDerivIndicator of int +| MifidDirectElectronicAccess of int +| MifidExecutionDecision of int64 +| MifidId of int64 +| MifidInvestmentDecision of int64 +| MifidLiquidityProvision of int +| MifidTradingCapacity of int + + | SimOperatorID of string | SMPID of string | CMELocation of string @@ -32,6 +49,9 @@ type UserField = | CMEMic of CMEMicData | CMEProNonPro of ProfessionalDesignation | CMEDisplayNonDisplay of DisplayDesignation +| ICEMic of ICEMicData +| ICEProNonPro of ProfessionalDesignation +| ICEDisplayNonDisplay of DisplayDesignation type Msg = @@ -71,4 +91,12 @@ let CMEMicMap:Map = Map.empty .Add("XNYM", bts.api.CMEMics.NYMEX) .Add("XCBT", bts.api.CMEMics.CBT) .Add("XCEC", bts.api.CMEMics.COMEX) - .Add("DME", bts.api.CMEMics.DME) \ No newline at end of file + .Add("DME", bts.api.CMEMics.DME) + +let ICEMicMap:Map = [ + ("IFCA", bts.api.ICEMics.IFC); + ("IFEN", bts.api.ICEMics.IFEN); + ("IFEU", bts.api.ICEMics.IFE); + ("IFLL", bts.api.ICEMics.IFL); + ("IFUS", bts.api.ICEMics.IFU) + ] |> Map.ofList diff --git a/src/web/ui/src/Users/View.fs b/src/web/ui/src/Users/View.fs index f1159f84e..4b0a7b2fe 100644 --- a/src/web/ui/src/Users/View.fs +++ b/src/web/ui/src/Users/View.fs @@ -198,22 +198,41 @@ let accountsTabView (model:Model) (dispatch:Msg->Unit) (readonly:bool) = let MarketDataTabView (model:Model) (dispatch:Msg->Unit) (readonly:bool) = let cmeData = model.scratchUser.userData.exchangeUserData.cmeData + let iceData = model.scratchUser.userData.exchangeUserData.iceData - let findCheckedValue (value:bts.api.CMEMics) = + let findCMECheckedValue (value:bts.api.CMEMics) = let checkedValue = cmeData.mdPermissions |> Array.tryFind (fun mic -> mic.mic = value && mic.enabled) match checkedValue with | Some t -> true | None -> false - let micList = [ - for micPair in CMEMicMap do - yield ( - Fields.checkbox + let findICECheckedValue (value:bts.api.ICEMics) = + let checkedValue = iceData.mdPermissions |> Array.tryFind (fun mic -> mic.mic = value && mic.enabled) + match checkedValue with + | Some t -> true + | None -> false + + let cmeMicList = [ + for micPair in CMEMicMap do + yield ( + Fields.checkbox (sprintf "%O" micPair.Key) - "" - (findCheckedValue micPair.Value) - [Disabled readonly] - (fun chk -> (({enabled = chk; mic = micPair.Value}:CMEMicData) |> (CMEMic >> UpdateField >> dispatch))) + "" + (findCMECheckedValue micPair.Value) + [Disabled readonly] + (fun chk -> (({enabled = chk; mic = micPair.Value}:CMEMicData) |> (CMEMic >> UpdateField >> dispatch))) + ) + ] + + let iceMicList = [ + for micPair in ICEMicMap do + yield ( + Fields.checkbox + (sprintf "%O" micPair.Key) + "" + (findICECheckedValue micPair.Value) + [Disabled readonly] + (fun chk -> (({enabled = chk; mic = micPair.Value}:ICEMicData) |> (ICEMic >> UpdateField >> dispatch))) ) ] @@ -223,40 +242,69 @@ let MarketDataTabView (model:Model) (dispatch:Msg->Unit) (readonly:bool) = [ h2 [Class "is-size-3"] [ str "CME" ] h3 [Class "is-size-4"] [ str "Entitlements" ] - div [Style [Padding " .5em 1em"]] micList + div [Style [Padding " .5em 1em"]] cmeMicList + + h3 [Class "is-size-4"] [ str "Designations" ] + //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) + + br [] + h2 [Class "is-size-3"] [ str "ICE" ] + h3 [Class "is-size-4"] [ str "Entitlements" ] + div [Style [Padding " .5em 1em"]] iceMicList h3 [Class "is-size-4"] [ str "Designations" ] //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) + Fields.enumSelect "Display / Non-Display" (toHuman false DisplayDesignation.values iceData.displayNondisplay) (allToHuman false DisplayDesignation.values) [Disabled readonly] (fromHuman DisplayDesignation.values>>UserField.ICEDisplayNonDisplay>>send) ] let orderRoutingTabView (model:Model) (dispatch:Msg->Unit) (readonly:bool) = let cmeData = model.scratchUser.userData.exchangeUserData.cmeData let simData = model.scratchUser.userData.exchangeUserData.simData + let iceData = model.scratchUser.userData.exchangeUserData.iceData div [] [ - h2 [Class "is-size-3"] [ str "CME" ] - div [] - [ - Fields.text "Operator ID" cmeData.operatorId [ MaxLength 18.0 ; Disabled readonly ] (CMEOperatorID >> UpdateField >> dispatch) - //Fields.text "ATS Operator ID" cmeData.atsOperatorId [] (ATSOperatorID >> UpdateField >> dispatch) - Fields.text "SMP ID" cmeData.smpId [Disabled readonly] (SMPID >> UpdateField >> dispatch) - Fields.mapSelectEx "SMP Instruction" State.SMPInstructionMap None (Some cmeData.smpInstruction) (SMPInstruction >> UpdateField >> dispatch) (Some readonly) - Fields.text "Location" cmeData.location [Disabled readonly] (CMELocation >> UpdateField >> dispatch) - //Fields.text "Max Order Rate Per Connection" cmeData.maxOrderRatePerConnection [] (MaxOrderRatePerConnection >> UpdateField >> dispatch) - //Fields.map "Misc Tag Mapping" cmeData.miscTags [] (MiscTags >> UpdateField >> dispatch) - ] - br [] - h2 [Class "is-size-3"] [ str "Sim" ] - div [] - [ - Fields.text "Operator ID" simData.operatorId [ MaxLength 18.0 ; Disabled readonly] (SimOperatorID >> UpdateField >> dispatch) - Fields.text "Location" simData.location [Disabled readonly] (SimLocation >> UpdateField >> dispatch) + h2 [Class "is-size-3"] [ str "CME" ] + div [] [ + Fields.text "Operator ID" cmeData.operatorId [ MaxLength 18.0 ; Disabled readonly ] (CMEOperatorID >> UpdateField >> dispatch) + //Fields.text "ATS Operator ID" cmeData.atsOperatorId [] (ATSOperatorID >> UpdateField >> dispatch) + Fields.text "SMP ID" cmeData.smpId [Disabled readonly] (SMPID >> UpdateField >> dispatch) + Fields.mapSelectEx "SMP Instruction" State.SMPInstructionMap None (Some cmeData.smpInstruction) (SMPInstruction >> UpdateField >> dispatch) (Some readonly) + Fields.text "Location" cmeData.location [Disabled readonly] (CMELocation >> UpdateField >> dispatch) + //Fields.text "Max Order Rate Per Connection" cmeData.maxOrderRatePerConnection [] (MaxOrderRatePerConnection >> UpdateField >> dispatch) + //Fields.map "Misc Tag Mapping" cmeData.miscTags [] (MiscTags >> UpdateField >> dispatch) + ] + br [] + h2 [Class "is-size-3"] [ str "ICE" ] + div [] [ + Fields.number_novalidate "Company ID" iceData.companyId [ ] (ICECompanyID >> UpdateField >> dispatch) + Fields.text "Operator ID" iceData.operatorId [ ] (ICEOperatorID >> UpdateField >> dispatch) + Fields.text "Authorized Trader" iceData.authorizedTrader [ ] (ICEAuthorizedTrader >> UpdateField >> dispatch) + Fields.text "Authorized Group" iceData.authorizedGroup [ ] (ICEAuthorizedGroup >> UpdateField >> dispatch) + Fields.text "Password" iceData.password [ ] (ICEPassword >> UpdateField >> dispatch) + Fields.text "Custom Order Instructions" iceData.custOrderHandingInst [ ] (ICECustOrderHandingInst >> UpdateField >> dispatch) - ] + h2 [Class "is-size-4"] [ str "MIFID" ] + div [Style [MarginLeft "2em"]] [ + Fields.number_novalidate "Client Id Code" iceData.mifidClientIdCode [ ] (MifidClientIdCode >> UpdateField >> dispatch) + Fields.number_novalidate "Commodity Deriv Indicator" iceData.mifidCommodityDerivIndicator [ ] (MifidCommodityDerivIndicator >> UpdateField >> dispatch) + Fields.number_novalidate "Direct Electronic Access" iceData.mifidDirectElectronicAccess [ ] (MifidDirectElectronicAccess >> UpdateField >> dispatch) + Fields.number_novalidate "Execution Decision" iceData.mifidExecutionDecision [ ] (MifidExecutionDecision >> UpdateField >> dispatch) + Fields.number_novalidate "Id" iceData.mifidId [ ] (MifidId >> UpdateField >> dispatch) + Fields.number_novalidate "Investment Decision" iceData.mifidInvestmentDecision [ ] (MifidInvestmentDecision >> UpdateField >> dispatch) + Fields.number_novalidate "Liquidity Provision" iceData.mifidLiquidityProvision [ ] (MifidLiquidityProvision >> UpdateField >> dispatch) + Fields.number_novalidate "Trading Capacity" iceData.mifidTradingCapacity [ ] (MifidTradingCapacity >> UpdateField >> dispatch) + ] + ] + br [] + h2 [Class "is-size-3"] [ str "Sim" ] + div [] [ + Fields.text "Operator ID" simData.operatorId [ MaxLength 18.0 ; Disabled readonly] (SimOperatorID >> UpdateField >> dispatch) + Fields.text "Location" simData.location [Disabled readonly] (SimLocation >> UpdateField >> dispatch) + ] ] @@ -439,10 +487,10 @@ let root (model:Model) (dispatch:Msg->Unit) = | _, _ -> () ) *) - let adminDisplay = model.scratchUser.userData.exchangeUserData.cmeData.adminDisplayFlag + let adminDisplay = model.scratchUser.userData.exchangeUserData.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_md = if is_bts then true else (adminDisplay = bts.api.admin.ExchangeUserData.AdminDisplay.All || adminDisplay = bts.api.admin.ExchangeUserData.AdminDisplay.MD) + let display_risk = if is_bts then true else (adminDisplay = bts.api.admin.ExchangeUserData.AdminDisplay.All || adminDisplay = bts.api.admin.ExchangeUserData.AdminDisplay.Routing) let modal = match (fst model.ConfirmModal) with | "" -> span [] [] diff --git a/testing/manual/admin/order_router.txt b/testing/manual/admin/order_router.txt index c66cb1789..5030ae767 100644 --- a/testing/manual/admin/order_router.txt +++ b/testing/manual/admin/order_router.txt @@ -17,7 +17,7 @@ Prerequisites User settings ------------- 3. Navigate to http://sparkvm:8000 (or wherever your sparkd resides), Users tab -4. Between bts and bts_2 users, verify that have distinct operator ids (Order Routing tab) +4. Between bts and bts_2 users, verify they have distinct operator ids (Order Routing tab) 5. In another ssh session on your server, let's tail the sim fix log for orders tail -f ~/sparkdata/data/jetfix/sim/current/fix | grep --line-buffered 35=D | tr '\001' '|' 6. Open up a client, login as bts, pick your favorite symbol and submit an order diff --git a/testing/manual/admin/user_account_deletion.txt b/testing/manual/admin/user_account_deletion.txt new file mode 100644 index 000000000..21aa67b82 --- /dev/null +++ b/testing/manual/admin/user_account_deletion.txt @@ -0,0 +1,13 @@ +This test verifies that users can only see accounts that are not marked as deleted + +1. Start a client and the admin gui, choose a user other than bts + +2. In the admin gui, create an account whose name is Prada + +3. For the designated user, add permission to see the Prada account + +4. Verify the you can see the account in the client + +5. In the admin gui, delete the account + +6. Verify the gui no longer shows the Prada (or DELETED_Prada, etc) account diff --git a/testing/manual/forms/fills.txt b/testing/manual/forms/fills.txt index ce130ffa7..9ecc6ab14 100644 --- a/testing/manual/forms/fills.txt +++ b/testing/manual/forms/fills.txt @@ -8,3 +8,7 @@ Make sure the following columns have filters when the window starts: Cross an order, get a fill and make sure it shows up Test sorting and filtering + +Root algo: +Enter an iceberg order and force fills. Make sure the Root Algo column shows the Iceberg as the root algo. +Now enter a speader order and force fills. Make sure the Root Algo column shows the name of the spreader. diff --git a/testing/manual/forms2/manual_fills.txt b/testing/manual/forms2/manual_fills.txt new file mode 100644 index 000000000..8c831096d --- /dev/null +++ b/testing/manual/forms2/manual_fills.txt @@ -0,0 +1,16 @@ +Manual Fills Test* + +Manual fills should now show Admin fills +- Open the Trading -> Manual fills form + - Verify the form is completely empty as there should be no manual fills yet + +- In the Admin UI, click on Accounts -> Position Management + - Add a fill for the bts user with a qty of 100 + - Click Save + +- Back in the Manual fills form, verify the form is still empty +- Open up Trading -> PnL + - Verify the PnL shows a row for the trade you entered + + +* = We need to flesh out this test as it currently only tests 1 narrow piece of functionality \ No newline at end of file diff --git a/testing/manual/keycodes/keycode.txt b/testing/manual/keycodes/keycode.txt index ed9535602..43106e45c 100644 --- a/testing/manual/keycodes/keycode.txt +++ b/testing/manual/keycodes/keycode.txt @@ -23,7 +23,8 @@ You should see keycode details printed to screen -You should see the data zero out on the first instance logged in as bts_2 and data ticking on the bts instance as well as the second bts_2 instance - d)Kill your sparkd + d) Kill your sparkd but leave your shells open + e) bring sparkd back up, you should see market data in the shell logged in as bts AND one of the other shells 4.5) For extra credit generate a keycode with -allow_concurrent and make sure we don't zero the market data for concurrent logins diff --git a/testing/manual/ladder/badge_spec.PNG b/testing/manual/ladder/badge_spec.PNG deleted file mode 100644 index 38a870d7f..000000000 Binary files a/testing/manual/ladder/badge_spec.PNG and /dev/null differ diff --git a/testing/manual/orders/order_book.txt b/testing/manual/orders/order_book.txt index 0b724e132..edf2a0ce3 100644 --- a/testing/manual/orders/order_book.txt +++ b/testing/manual/orders/order_book.txt @@ -36,7 +36,7 @@ Left click on a cancelled parent order of a parent-child order type Order tree node collapsing testing ---------------------------------- -- Create an algo using the algo studio consisting of IB -> IB, call it IB2. +- Create an algo using the algo studio consisting of IB -> Limit, call it IB2. - Launch an instance of IB2 on a normal security away from the market so it does not execute immediately - In an order book form ensure that the setting "Show Full Algo Tree" is checked - Ensure further that there are no filters on the OB @@ -61,9 +61,15 @@ Filter/Collapse interaction --------------------------- - Now uncheck the "Show Full Algo Tree" setting and clear all filters - You should only see the top algo for CIB and the the hedges and currently working limit order -- Now filter for order type = limit, you should only the hedges and the currently working limit +- Now filter for order type = limit, you should only see the hedges and the currently working limit - Now filter for order type = IB and you should see nothing +Parent/Child +------------ +- Enter an algo fo your choice. Verify that the parent child algos show correctly. +- Enter a spreader. The spreader algo should show the name of the spreader as well. Verify +that the filter works for this (the filter only shows the spreader badge). + diff --git a/testing/manual/positions_and_fills/SOD.txt b/testing/manual/positions_and_fills/SOD.txt index cd5c62572..3e5f9dfbd 100644 --- a/testing/manual/positions_and_fills/SOD.txt +++ b/testing/manual/positions_and_fills/SOD.txt @@ -3,6 +3,8 @@ rm ~/sparkdata/data/databases/fills/* rm ~/sparkdata/data/databases/orders/* and bounce your server. +BE SURE NOT TO PICK AN EXPIRED SECURITY, THOSE FILLS ARE FILTERED + 0. We will force fills across for 3 securities, across 2 accounts. a. Pick 2 futures and one exchange traded spread, all on different symbols b. Choose these securities such that there is market data (sim or cert) that you can get fills against (maybe bring up the market view and select securities from there). diff --git a/testing/manual/positions_and_fills/SOD_timezone.txt b/testing/manual/positions_and_fills/SOD_timezone.txt index 78b53ffef..fb2336615 100644 --- a/testing/manual/positions_and_fills/SOD_timezone.txt +++ b/testing/manual/positions_and_fills/SOD_timezone.txt @@ -12,6 +12,8 @@ ExchangeSim { 2. Clean up your fills database. Cross the market to buy a 10 lot future (any future). Lets say the fill time is 6:03:XX EST. Wait until 6:05 and do a 20 lot sell. +BE SURE NOT TO PICK AN EXPIRED SECURITY, THOSE FILLS ARE FILTERED + 3. Now we will make 6:05 as the SOD Time. Enter this in your spark.conf SODTime{ diff --git a/testing/manual/server_performance/spreader_requote.txt b/testing/manual/server_performance/spreader_requote.txt index 3724f0fb1..19d8a7d5c 100644 --- a/testing/manual/server_performance/spreader_requote.txt +++ b/testing/manual/server_performance/spreader_requote.txt @@ -1,4 +1,4 @@ -Test Author: Pedro +Test Author: Pedro If it breaks: Pedro Spreader Requote Performance test @@ -30,13 +30,13 @@ OrdermanCfg { value { executor { thread_name: "oman_orders" - poll: true + poll: true hosted_actor: "MDSource" hosted_actor: "OrderManager" } executor { thread_name: "oman_slow" - poll: false + poll: false hosted_actor: "*" } } @@ -54,7 +54,7 @@ Threads { 5) Now you are ready to start testing. Start a shell and create a new synthetic say with 2 CL futures. Use the near month (fast ticking) future as the non quoting leg, so that the other leg gets quoted -often. Make sure to disable throtling in the synthetic definition +often. Make sure to disable throttling in the synthetic definition dialog. 6) Place an order for this synthetic, away from the market. diff --git a/testing/manual/settings/Ladder_MaxClip.txt b/testing/manual/settings/Ladder_MaxClip.txt new file mode 100644 index 000000000..f4757c100 --- /dev/null +++ b/testing/manual/settings/Ladder_MaxClip.txt @@ -0,0 +1,41 @@ +1. Verify you can place orders +Launch a Ladder +Place a 10 lot order away from the market and make sure the order is not rejected +Cancel the order + +2. Verify the Max Order Quantity limit works +Click on the Settings button +Click on the Safety Checks tab +Click on the checkbox next to Max Order Quantity and specify 5 + You should additionally observe that the text has gone from a disabled appearance to active + The ticker box is also now active +Hit apply +Back in the ladder, attempt to place another 10 lot order away from the market. You should observe the following: + You are now working a 5 lot order + The qty in the qty box additionally reads 5 +Cancel the 5 lot order + +3. Verify the Max Order Quantity when disabled allows you to place a 10 lot order +This is essentially the same as step 1, except now we're explicitly unchecking the checkbox, ergo... +Uncheck the Max Order Quantity chechbox and observe that: + The Max order quantity text is disabled + The ticker box is disabled +Click Apply +In the ladder, click the 5 button so that your qty box reads 10, which is a quantity greater than the disabled 5 + A 10 lot order should now be working +Cancel the 10 lot order. + +4. Right click / Alternate order qty respects the limit +Re-enable the Max Order Quantity (5 is still fine) +On the Order Quantities tab, specify 7 as your 'Single Click' Quantity +In the ladder, Right click to work an order attempting to use your single click quantity + You should be working a a 5 lot order now i.e. your max order quantity + Observe that the qty box did NOT update with your alternate order quantity + +5. Cross market alert communicates respects the limit +Back on the Safety Checks tab, make sure Warn on Market Cross is checked +Attempt to place a 10 lot bid order (higher than your max of 5) on the sell side of the market + You should observe the message respects your max order quantity and warns you about buying 5 + Feel free to click whichever button you desire on the warning dialog + +FIN diff --git a/testing/manual/settings/Ladder_PriceDisplayFormat.txt b/testing/manual/settings/Ladder_PriceDisplayFormat.txt new file mode 100644 index 000000000..d7f4174e1 --- /dev/null +++ b/testing/manual/settings/Ladder_PriceDisplayFormat.txt @@ -0,0 +1,12 @@ +Launch an ES Ladder (i.e. an instrument whose separator is the .) + Observe the ladder prices are in fact separated by a decimal point +Launch a ZT ladder (i.e. an instrument whose separator is the -) + Observe the ladder prices are in fact separated by a dash +Perform the following in each: + Click Settings + Go to Market Price and Size Display tab + Click on Integer under Price Display Format + Click Apply + Observe that the separator disappears when you do this + Feel free to go back and forth several times + diff --git a/testing/manual/settings/Ladder_RecenterAll.txt b/testing/manual/settings/Ladder_RecenterAll.txt new file mode 100644 index 000000000..c7c6d5018 --- /dev/null +++ b/testing/manual/settings/Ladder_RecenterAll.txt @@ -0,0 +1,6 @@ +1. Verify pressing the shift or alt key while double clicking in the Price column in a Ladder Window recenters ALL ladders +Launch 2 ladders +Scroll both ladders so they are no longer centered at mid market +Press the shift + double click while over the Price column + You should observe both scrolled ladders are now centered once again + The countdown timer is also reset via this spacebar action. This is more informational; I don't expect you to wait to verify that the timer is reset. \ No newline at end of file diff --git a/testing/manual/settings/SoundAndNotificationsTest.txt b/testing/manual/settings/SoundAndNotificationsTest.txt index 5690657f3..d3b6d1377 100644 --- a/testing/manual/settings/SoundAndNotificationsTest.txt +++ b/testing/manual/settings/SoundAndNotificationsTest.txt @@ -82,8 +82,15 @@ Prepare: open a ladder for a security that has steady market data activity (in t - enter a limit order ACROSS the market to trigger an immediate fill. - this time, the "When order is filled" sound should play; the "When order is canceled" sound should not be heard. +7) "Any Order" sent sounds: + Description: + You will be verifying that the order sent sound trigger works + + - in the settings dialog, change "When order is filled" to "When order is sent", then click apply + - in the ladder, enter a limit order away from the market, so it is NOT liable to get filled. + - you should hear the sound you selected for "When order is sent" -7) Saved with workspace: +8) Saved with workspace: ----Note---------------------------------------------------------------------------------------------------------------------- - you have completed the sound-functionality testing, don't be alarmed by the 4 sound triggers you are about to add. - this step just requires comparing 2 images of the settings dialog to confirm that they were retained with the workspace. @@ -99,7 +106,7 @@ Prepare: open a ladder for a security that has steady market data activity (in t - close the shell, then restart it with the "Where I left off on" workspace. - open the OWSD to verify that the sound triggers were saved with the workspace. -8) Test shell does not crash when no sound card present +9) Test shell does not crash when no sound card present - use a machine that does not have a sound card, such as eh-2 (check with Eric first) - update config to turn off sounds diff --git a/testing/manual/settings/UserSettingsTest.txt b/testing/manual/settings/UserSettingsTest.txt index 573a14106..0586fac73 100644 --- a/testing/manual/settings/UserSettingsTest.txt +++ b/testing/manual/settings/UserSettingsTest.txt @@ -1,50 +1,33 @@ -User Settings Test (15 min) +User Settings Test + (there is a REFERENCE section at the bottom, please consult it before getting frustrated, if things are still unclear, ask John.) NOTES: - - in addition to the notes below, there is a "Reference" section at the bottom of the test, - feel free to add things to it if you run into problems; impart your wisdom unto the next tester. - the settings button is located in the bottom-left corner of the Ladder - - the "Default" tab will be referred to as [DTab], "Custom" as [CTab] - - the "Use Defaults" checkbox is located at the top of the dialog - - when "Use Defaults" is checked, [CTab] is disabled ([DTab], however, is always enabled) - to Import/Export settings to files, click the hamburger icon in the upper-right corner of the settings dialog. - you cannot open multiple settings dialog for the same Ladder i) if a settings dialog is already open, clicking the settings button in the Ladder again closes said dialog, then opens a new one - - the settings dialogs do not live update - i) this only pertains to [DTab], as Custom settings are not shared. - - commit changes by clicking "Apply" or "OK" - i) the only difference between the 2 buttons is that "OK" also closes the dialog - - when you commit changes: - i) even if "Use Defaults" is NOT checked, the settings in [DTab] will be compared with the current defaults, - if they are different, the default changes will be committed as well. - ii) be advised of the following scenario: - You have 2 settings dialogs open (call them [A] and [B]), and commit changes to the defaults in [A]. - As the dialogs do NOT live update, [B] will have stale defaults. - When you click commit in [B] (even if you have not actually changed anything in [DTab]) - the stale defaults in [B] will be committed, effectively overwritting the changes to the defaults you just committed from [A]. + - the "sample" areas should always reflect the properties in your editor, if this is unclear, see the reference section at the bottom for a more detailed explanation. - - when the test directs you to "Visually Confirm blah...," just give it a quick glance, if something isn't matching it should be obvious. - - when the test directs you to repeat a step for each property, here is the list (in case it isn't clear in the dialog): - Note: the Sample column is purely output, don't bother clicking it - - Order Badge Style - - Alternate Order Quantity - - Size Presets (aka Number Pad buttons) - - Fonts and Colors section: - for each column (Orders, Bid, Price, Ask, Last) + + - when the test directs you to repeat a step for each property, here is the list: + - Ladder Columns tab -> + - Order Badge Style + - for each column (Orders, Bid, Price, Ask, Last) i) background ii) foreground iii) font (family, size, and weight) - - Market Break Lines section: - i) for each "price-line" (session high, ask, mid, bid, and session low) + - Order Quantities tab -> + - 'Single Click' Quantity + - Number Pad Quantities + - Market Break Lines tab: + i) for each "price-line" (session high, ask, mid, bid, and session low) a) toggle show/hide b) color ii) for session range (this is not actually a line, but the color between the session low/high a) toggle show/hide b) color - - when the test advises you to name files (screenshots, or ladder settings files), it is because they will be referenced later by that name. - - if you uncheck all 3 market break lines, the ladder resolves to show the bid line by design. - - just to reiterate: there is a REFERENCE section at the bottom, please consult it before getting frustrated, if things are still unclear, ask John. + - if you uncheck all market break lines, the ladder resolves to show the bid line by design. ------------TEST STEPS------------------ @@ -52,61 +35,71 @@ NOTES: - the security needs a few working orders on both sides, and a gap of 5-8 ticks between the top bid/ask. The gap is needed to separate the market break lines (Bid, Ask, and Mid). - from hence forth, the 2 ladders will be referred to as "[A]" and "[B]" and their settings dialogs as "dialog[A]" and "dialog[B]" - - at the risk of sounding redundant, the "Default" tab will be referred to as [DTab], the "Custom" as [CTab] - + 2) Open the settings dialogs for [A] and [B] 3) Make sure that both [A] and [B] are using default settings, then close [B]dialog -4) In dialog[A] (with [DTab] selected, [CTab] should be disabled anyways), change each property, then click "Apply" +4) In dialog[A] change each property, then click "Apply" 5) Visually confirm that all the changes have been applied to both ladders - the row height should adjust to fit the largest font size, - the scroll position should be maintained, assuming the AutoRecenterTimer didn't move it. - + 6) Take a screenshot, name it "A-1-confirmation.png" -7) In dialog[A], export the default settings to a file, name it "A-1.ladder" (instructions on how to import/export are in the NOTES section) +7) In dialog[A], export the settings to a file, name it "A-1.ladder" (from the irregular hamburger menu) -8) In dialog[A], click "Restore to Factory," then click "Apply" +8) In dialog[A], click "Load Factory Defaults" in the irregular hamburger menu then click "Apply" 9) Visually confirm that dialog[A], and both ladders, each changed appearance, and match one another. -10) In dialog[A], uncheck "Use Defaults", then select [CTab] - -11) Import "A-1.ladder" to Custom, then click Apply +10) In dialog[A] Import "A-1.ladder", then click Apply 12) Visually confirm that: - dialog[A] and [A] changed appearance, and look just like they did in "A-1-confirmation.png" - [B] did not change appearance 13) Click the settings button in [A] and [B] to get 2 fresh dialogs (dialog[A] should close and reopen) - - in dialog[A], "Use Default" should be false, and [CTab] selected - - in dialog[B], "Use Default" should be true, and [DTab] selected ([CTab] should be disabled) 14) Take a screenshot, then save the workspace, then restore it - visually confirm that [A] and [B] look as they did in the screenshot - repeat step 13 -15) In dialog[A], with [CTab] selected, click "Copy from Default" - - [CTab] should now have the same settings as [DTab] +15) In dialog[A] hit the plus stack icon to add a new Profile, give it a unique and memorable name -16) Click the settings button in [A] to close and reopen dialog[A] +16) In Dialog [B] select the profile you just created, if you remember the name -17) In dialog[A] select [DTab], then click "Copy from Custom" - - [DTab] should now have the same settings as [CTab] +17) Click Apply and verify [B] changes to match [A] + +18) Click the smaller than average pencil button in dialog[B] change the name of your profile and hit enter. Verify the name changed in dialog[A] + +19) Change any one setting of your choice in either dialog. Click the 3.5 inch floppy disk icon and wait for the light to go off before ejecting the disk. Verify both dialogs have the setting you changed. + +2001) Change any one setting of your choice in either dialog; click apply in the same dialog and verify both ladders and both dialogs reflect the changed setting. + +2002) Revert to factory defaults and create a new profile named "Unsafety Checks" + +2003) On the Safety Checks tab uncheck Warn on Market Cross + +2004) Click the security selection icon (with the hand), double click every entry in the Exchange column, and click OK. Save or Apply. + +2005) Open a new ladder for any symbol and verify you can cross your own orders without being pestered by a confirmation dialog + +------------------------------------------------------------------------------- +Left Panel Scroll test +------------------------------------------------------------------------------- +The scroll position of the left-panel area is now remembered as part of the ladder settings + +Resize your ladder window to make it vertically small so that the left panel displays a scrollviewer. +Scroll the left panel down so that the qty box is the top-most visible control in the panel +Exit and relaunch Spark, loading your last saved workspace +Verify that the restored ladder window's left panel has been vertically scrolled so the the qty box is one again the top-most visble control DONE!!! Reference----------------------------------------------------------- - Alternate Order Quantity: - - you may send an order with this size via a RIGHT CLICK on a price cell - - Size Presets: - - aka the number-pad buttons - - click the expander-arrow button next to the "Size Presets" label to modify them - - you control the quantities in the buttons, and the order in which they appear Market Break Lines (Bid, Ask, Mid, and Session Low/High/Range): - each row corresponds to one item, and represents a "horizontal price-line" that will be displayed in the ladder (if show is checked, of course) diff --git a/testing/manual/spreader_misc/algo_of_spreader.txt b/testing/manual/spreader_misc/algo_of_spreader.txt new file mode 100644 index 000000000..7dd7d5fba --- /dev/null +++ b/testing/manual/spreader_misc/algo_of_spreader.txt @@ -0,0 +1,12 @@ +This test makes sure the basic spreader works as child orders of different algos + +Iceberg +------- + +0. Follow the instructions from docs/spreader_common_setup_2.txt + +1. In the spreader ladder enter a 5 lot sell IB with a show qty of 2 at -0.275. + +2. Verify that it completely fills. + +3. Verify that the Filled Qty and Avg Filled in the OB is correct \ No newline at end of file diff --git a/testing/manual/spreader_misc/fractional_hedging.txt b/testing/manual/spreader_misc/fractional_hedging.txt index fce9925bb..4d9b9fbfd 100644 --- a/testing/manual/spreader_misc/fractional_hedging.txt +++ b/testing/manual/spreader_misc/fractional_hedging.txt @@ -26,21 +26,22 @@ or more orders to get the unhedged delta < 1. Force quoting fills based on the left most column (by lifting the quoting offer) and verify that hedge orders of size "hedge_order_sent" (3rd column) are sent as a result. -The "unhedged_delta" column shows the unhedged delta immediately after the quoting fill +Compare the cumulative qty (cum) to the quote and hedge badges for easy visual inspection. + +(The "unhedged_delta" column shows the unhedged delta immediately after the quoting fill and the "remaining_unhedged_delta" column is the unhedged delta after the hedge order -has been sent. -You can check the hedge orders being sent easily by looking at the order reduction -happening on the order sent in step 2 (or look at the hedges in the orderbook). +has been sent) + ------------------------------------------------------------------------------ -quoting fill unhedged_delta hedge_order_sent remaining_unhedged_delta +quoting fill(cum) unhedged_delta hedge_order_sent (cum) remaining_unhedged_delta ------------------------------------------------------------------------------ -1 2/3 0 2/3 -1 4/3 1 1/3 -1 1 1 0 -4 8/3 2 2/3 -5 4 4 0 -2 4/3 1 1/3 -1 1 1 0 +1(1/15) 2/3 0(0/10) 2/3 +1(2/15) 4/3 1(1/10) 1/3 +1(3/15) 1 1(2/10) 0 +4(7/17) 8/3 2(4/10) 2/3 +5(12/15) 4 4(8/10) 0 +2(14/15) 4/3 1(9/10) 1/3 +1(15/15) 1 1(10/10) 0 6. At the end of this, the whole order should be filled. Check the order book to make sure the fill price is -9.900, there are 10 total fills in SIF1 and diff --git a/testing/manual/spreader_misc/inside_market_distance.txt b/testing/manual/spreader_misc/inside_market_distance.txt new file mode 100644 index 000000000..885365413 --- /dev/null +++ b/testing/manual/spreader_misc/inside_market_distance.txt @@ -0,0 +1,12 @@ +This test makes sure the basic spreader functionality works for 2 legged synthetic spreads with one quoting leg + +Iceberg +------- + +0. Follow the instructions from docs/spreader_common_setup_2.txt + +1. In the spreader ladder enter a 5 lot sell IB with a show qty of 2 at -0.275. + +2. Verify that it completely fills. + +3. Verify that the Filled Qty and Avg Filled in the OB is correct \ No newline at end of file diff --git a/tools/cpp_helper/ics_spreads.csv b/tools/cpp_helper/ics_spreads.csv index 0f58741d9..26265d4fb 100644 --- a/tools/cpp_helper/ics_spreads.csv +++ b/tools/cpp_helper/ics_spreads.csv @@ -28,4 +28,4 @@ Sym,Mult,Exch "BOB",1000,CBT "BUB",1000,CBT "FNU",1000,CBT -"SOM",500,CBT \ No newline at end of file +"SOM",500,CBT diff --git a/tools/spark_vm_shell.sh b/tools/spark_vm_shell.sh index b2bd64634..dbc0c8ab1 100755 --- a/tools/spark_vm_shell.sh +++ b/tools/spark_vm_shell.sh @@ -8,7 +8,7 @@ docker run \ -p 8001:8001 \ -p 8080:8080 \ --net host \ - --shm-size=1024m \ + --shm-size=2048m \ -v /etc/passwd:/etc/passwd \ -v /etc/group:/etc/group \ -v /etc/shadow:/etc/shadow \