Development

Video Demo

Watch a video demonstration of some of these workflows here:

IMAGE_ALT

Visual Studio Code

In this section we're going to use the Remote - Containers feature of Visual Studio Code to simplify your development experience. If you are using a different editor skip to the Other Editors section below.

Remote - Containers

The Visual Studio Code Remote Containers extension allows us to run a complete development environment inside CQG Custom Algo SDK Docker container. The editor UI runs as a thin client on your desktop and connects to a headless instance running inside of CQG Custom Algo SDK Docker environment. This configuration allows us to ship a pre-configured set of Tasks for building and testing of 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

First download the samples onto your host machine.

dotnet new -i cqg_algo_template
mkdir myalgo
cd myalgo
dotnet new cqg_custom_algo

Next, start Visual Studio code and navigate to the newly created myalgo directory or if you added VSCode to your path, type code . from your command prompt.

When the workspace opens, you should see a notification offering to re-open the workspace inside the container, choose this option.

The first time you open the workspace inside the container it will take a few seconds to configure CQG Custom Algo 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.

Note: Rebuilding Containers

If during the course of your development, you download a new docker container from CQG, you must rebuild the container using either the docker rm command line or the VSCode plugin.

To re-build from within Visual Studio Code, click the 'Remote Explorer' icon on the left-hand side. Find algo_centos8_sdk project listed under the Containers section. Right click on it and select rebuild-container.

Build your Algo

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. You can get to the IAlgoOrder.h header file to browse the API by placing your cursor on IAlgoOrder and pressing F12.

To learn more about how an Algo is instantiated, see the Algo Initialization.

Compiling

Press "Ctrl+Shift+B" to initiate a build. 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 CQG Algo Server process on start.

Debugging

To debug your Algo set a breakpoint (F9), open the Debug panel (Ctrl+Shift+D) and choose "(gdb) Launch". This will launch CQG Algo Server inside the container with the debugger attached.

Logs are in /sparkdata/data/logging

With sparkd running, you can launch the Algo API test client and it will connect to the running server. Log in with the user "testuser" password: "testuser123" and open a ladder. You can now select your Algo in the Order Type dropdown and activate it by clicking on a price in the ladder.

Note: Algo API test client is for testing and debugging purposes only and should not be used in production.

Starting CQG Algo Server

To start CQG Algo Server without attaching a debugger press "Ctrl+Shift+P" and select "Tasks: Run Task", 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 CQG Algo Server, simply kill the terminal (trash can icon) or run the "Stop Sparkd" task.

Next Steps

If you are interested to explore in detail how Docker works behind the scenes, continue to the next section. Otherwise, continue to Algo Registration to learn how Algos are registered through CQG Custom Algo SDK.


Other Editors

In this section, we show how Docker works as well as how to compile and test your Algo using the command line.

Docker

CQG Custom Algo SDK is built for Linux and therefore, only runs on Linux. Docker lets you take the algo_centos8_sdk.img disk image and run it inside a virtual Linux machine hosted, as an example, on your Windows desktop. To effectively develop any plug-ins, you need a Linux development environment.

Recall that a Docker "image" is a disk image containing a complete file system. The algo_centos8_sdk.img file you imported earlier is an image. A Docker "container" is what 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 CQG Algo Server. Directories from your local file system can be mapped to the Docker container when it starts so you can share files.

Start

The first step is to start the algo_centos8_sdk Docker container with the templates directory mapped so we can compile the examples inside the container. In a command prompt, cd to the templates directory that you imported when you "pulled" the algo_centos8_sdk image from the Docker hub. Then run the following command to start the container.

Windows Command Prompt:

 docker run --rm -it -v %CD%:/templates -u algo -w /sdk --shm-size 2048m --name algo_centos8_sdk ^
   -h algo_centos8_sdk --cap-add=SYS_PTRACE --security-opt seccomp=unconfined ^
   -p 8000:8000 -p 8001:8001 algo_centos8_sdk bash

Powershell / Linux / Mac:

 docker run --rm -it -v ${PWD}:/templates -u algo -w /sdk --shm-size 2048m --name algo_centos8_sdk \
   -h algo_centos8_sdk --cap-add=SYS_PTRACE --security-opt seccomp=unconfined \
   -p 8000:8000 -p 8001:8001 algo_centos8_sdk bash

Attach

The container will remain running as long as the prompt is open. You can attach an additional command prompt to the running container using the following command:

 docker exec -it algo_centos8_sdk bash

Compiling

Let's take a look at the algo_template sample. All of the samples are laid out similarly with a build.sh script for compiling. As long as you are running build.sh inside CQG Algo Server Docker container, it will automatically copy the build output to /sparkbin/plugins/, where it needs to be in order to be loaded by the Spark server.

[algo@7cc6bbdee728 sdk]$ cd templates/cqg_custom_algo
[algo@7cc6bbdee728 cqg_custom_algo]$ ./build.sh
algo_template.so copied to /sparkbin/plugins/
Restart sparkd to see changes.

Starting CQG Algo Server

To start CQG Algo Server, cd /sparkbin/ and run, ./sparkd run. Press Ctrl+C to stop it.

[algo@7cc6bbdee728 sparkbin]$  ./sparkd run
2019-10-25 16:21:09.383626|262552152803600|Gen|Warn |1  |main|132 |Run.cpp:53 |dev|
------------------------------------ Sparkd launched --------------------------------

Here you will see any Sdk log messages, as well as any other System-related log messages at or above the Warning levels. If you need additional diagnostic information, more detailed logs are stored in /sparkdata/data/logging/log. These logs are automatically deleted when you stop the Docker container.

With sparkd running you can launch the Algo API test client. It will now connect to the running server. Log in with the user "algo" and open a ladder. You can now select your Algo in the Order Type dropdown and activate it by clicking on a price in the ladder.

Debugging

You can debug your algo using gdb from the bash command prompt.

[algo@7cc6bbdee728 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 the inside of the container.

Next

Continue to Algo Registration to learn how Algos are registered through CQG Custom Algo SDK.

Previous