Algorithmic Trading Software
Algorithmic Trading Software
AlgoTrader
Version 4.5
Table of Contents
Preface ............................................................................................................................................... xii
1. Document Conventions ........................................................................................................... xii
1.1. Typographic Conventions .............................................................................................. xii
1.2. Pull-quote Conventions ................................................................................................ xiii
1.3. Notes and Warnings .................................................................................................... xiv
1. Introduction .................................................................................................................................... 1
2. Installation and Deployment ........................................................................................................... 2
2.1. Development Environment Installation .................................................................................... 2
2.1.1. Prerequisites ............................................................................................................... 2
2.1.2. AlgoTrader Eclipse IDE Installation .............................................................................. 8
2.1.3. AlgoTrader Server Code Installation ............................................................................. 9
2.1.4. Next Steps ............................................................................................................... 12
2.2. Server Environment Installation ............................................................................................ 12
2.2.1. Docker based Installation .......................................................................................... 12
2.2.2. Docker Containers .................................................................................................... 13
2.2.3. Docker Compose ...................................................................................................... 19
2.2.4. Docker Management ................................................................................................. 22
2.3. VM Arguments ..................................................................................................................... 23
3. Starting AlgoTrader ...................................................................................................................... 24
3.1. Simulation Mode .................................................................................................................. 26
3.2. Live Trading Mode ............................................................................................................... 27
3.2.1. Embedded Mode ....................................................................................................... 27
3.2.2. Distributed Mode ....................................................................................................... 28
3.3. Server Environment ............................................................................................................. 29
3.3.1. Embedded Mode ....................................................................................................... 29
3.3.2. Distributed Mode ....................................................................................................... 31
4. Strategy Development .................................................................................................................. 33
4.1. Creating a Trading Strategy ................................................................................................. 33
4.1.1. AlgoTrader Strategy Wizard ....................................................................................... 33
4.1.2. AlgoTrader Maven Archetype ..................................................................................... 35
4.1.3. Generated Artifacts Java Archetype ........................................................................... 36
4.1.4. Generated Artifacts Esper Archetype .......................................................................... 38
4.2. Building a Trading Strategy .................................................................................................. 44
4.3. Hints for Strategy Development ............................................................................................ 44
4.3.1. Java based Strategies ............................................................................................... 45
4.3.2. Esper based Strategies ............................................................................................. 53
4.4. Strategy life-cycle events ..................................................................................................... 61
4.5. Strategy Groups .................................................................................................................. 62
5. Strategy Backtesting .................................................................................................................... 65
5.1. Exchange Simulator ............................................................................................................. 65
5.2. Simulation Process .............................................................................................................. 67
5.3. Single Run Simulation .......................................................................................................... 68
ii
CONFIDENTIAL
iii
CONFIDENTIAL
iv
CONFIDENTIAL
v
CONFIDENTIAL
vi
CONFIDENTIAL
vii
CONFIDENTIAL
viii
CONFIDENTIAL
List of Figures
2.1. Eclipse default JRE ..................................................................................................................... 11
3.1. Eclipse Run Configurations .......................................................................................................... 24
3.2. Eclipse Run Configurations .......................................................................................................... 25
3.3. Eclipse Run Configurations .......................................................................................................... 26
4.1. Strategy Development Process ..................................................................................................... 33
4.2. Wizard Selection .......................................................................................................................... 34
4.3. Wizard Location and Working Set ................................................................................................. 34
4.4. Wizard Maven Properties ............................................................................................................. 34
4.5. Wizard Maven Properties ............................................................................................................. 35
5.1. Back Test Report ......................................................................................................................... 71
6.1. Architecture ................................................................................................................................. 75
7.1. Entities Overview ......................................................................................................................... 77
7.2. Strategy ...................................................................................................................................... 79
7.3. Securities .................................................................................................................................... 81
7.4. Securities .................................................................................................................................... 83
7.5. Market Data Event ....................................................................................................................... 84
7.6. Orders ......................................................................................................................................... 85
7.7. Account ....................................................................................................................................... 87
7.8. Transaction ................................................................................................................................. 88
7.9. Position ....................................................................................................................................... 89
7.10. Subscription ............................................................................................................................... 90
7.11. Exchange .................................................................................................................................. 90
7.12. Properties and PropertyHolders .................................................................................................. 91
7.13. Order Preference ....................................................................................................................... 92
10.1. AlgoTrader HTML5 Client Header ............................................................................................. 120
10.2. AlgoTrader HTML5 Client Header Settings ................................................................................ 121
10.3. AlgoTrader HTML5 Client Management ..................................................................................... 122
10.4. AlgoTrader HTML5 Client Management Form ............................................................................ 122
10.5. AlgoTrader HTML5 Client Notification ........................................................................................ 122
10.6. AlgoTrader HTML5 Client Alert ................................................................................................. 123
10.7. AlgoTrader HTML5 Client Alert List ........................................................................................... 123
10.8. AlgoTrader HTML5 Client Order Table ...................................................................................... 123
10.9. AlgoTrader HTML5 Client Manual Order Entry ........................................................................... 123
10.10. AlgoTrader HTML5 Client Manual Order Detail ........................................................................ 124
10.11. AlgoTrader HTML5 Client Manual Order Modification ............................................................... 124
10.12. AlgoTrader HTML5 Client Transaction Table ............................................................................ 125
10.13. AlgoTrader HTML5 Client Transaction Entry ............................................................................ 125
10.14. AlgoTrader HTML5 Client Position Table ................................................................................. 126
10.15. AlgoTrader HTML5 Client Market Data Table ........................................................................... 126
10.16. AlgoTrader HTML5 Client Market Data Subscribe .................................................................... 127
10.17. AlgoTrader HTML5 Client Market Data Unsubscribe ................................................................. 127
10.18. AlgoTrader HTML5 Client Transaction Column Selection .......................................................... 128
ix
CONFIDENTIAL
x
CONFIDENTIAL
List of Tables
4.1. Strategy life-cycle phase .............................................................................................................. 61
7.1. Entities ........................................................................................................................................ 77
7.2. Strategy Classes ......................................................................................................................... 80
7.3. Portfolio Value Details .................................................................................................................. 80
7.4. Security Types ............................................................................................................................. 81
7.5. Security Types ............................................................................................................................. 84
7.6. Market Data Types ...................................................................................................................... 85
7.7. Order Classes ............................................................................................................................. 86
7.8. Position Valuation Details ............................................................................................................. 89
7.9. Exchange .................................................................................................................................... 91
7.10. Order Preference ....................................................................................................................... 92
7.11. Main Services ............................................................................................................................ 93
7.12. Client Services ........................................................................................................................... 94
8.1. AlgoTrader Server modules ........................................................................................................ 110
8.2. Esper tags ................................................................................................................................. 111
10.1. out-of-the-box types ................................................................................................................. 141
13.1. Position Currency Attribution ..................................................................................................... 149
13.2. Transaction Currency Attribution ............................................................................................... 150
14.1. Bar Data Format ...................................................................................................................... 151
19.1. Tick Data Format ..................................................................................................................... 180
19.2. Bar Data Format ...................................................................................................................... 180
22.1. BitFinex constraints .................................................................................................................. 204
22.2. BitMex constraints .................................................................................................................... 204
22.3. BitStamp constraints ................................................................................................................ 205
22.4. Binance constraints .................................................................................................................. 205
22.5. BitFlyer constraints ................................................................................................................... 206
22.6. Coingy constraints .................................................................................................................... 206
22.7. All exchanges and its constraints in one place ........................................................................... 207
22.8. Order type constraints .............................................................................................................. 207
22.9. Supported Instruments ............................................................................................................. 211
23.1. SlicingOrder ............................................................................................................................. 216
23.2. VWAPOrder ............................................................................................................................. 218
23.3. AdaptiveOrder .......................................................................................................................... 220
25.1. Adapter Spring Profiles ............................................................................................................ 229
29.1. Default Log4j Appenders .......................................................................................................... 236
29.2. Production Log4j Appenders ..................................................................................................... 236
xi
CONFIDENTIAL
Preface
1. Document Conventions
This manual uses several conventions to highlight certain words and phrases and draw attention to specific
pieces of information.
1
In PDF and paper editions, this manual uses typefaces drawn from the Liberation Fonts set. The Liberation
Fonts set is also used in HTML editions. If not, alternative but equivalent typefaces are displayed.
System input, including shell commands, file names and paths, and key caps and key-combinations are
presented as follows.
To see the contents of the file my_next_bestselling_novel in the current working directory,
enter the cat my_next_bestselling_novel command at the shell prompt and press Enter
to execute the command.
The above includes a file name, a shell command and a key cap, all distinguishable thanks to context.
Key-combinations can be distinguished from key caps by the symbol connecting each part of a key-
combination. For example:
Press Ctrl-Alt-F1 to switch to the first virtual terminal. Press Ctrl-Alt-F7 to return to the X-
Windows session.
The first sentence highlights the particular key cap to press. The second highlights two sets of three key caps,
each set pressed simultaneously.
If source code is discussed, class names, methods, functions, variable names and returned values mentioned
within a paragraph are presented as follows.
File-related classes include filesystem for file systems, file for files, and dir for directories.
Each class has its own associated set of permissions.
Words or phrases encountered on a system, including application names; dialog box text; labeled buttons;
check-box and radio button labels; menu titles and sub-menu titles are presented as follows.
Choose System → Preferences → Mouse from the main menu bar to launch Mouse
Preferences. In the Buttons tab, click the Left-handed mouse check box and click Close
to switch the primary mouse button from the left to the right (making the mouse suitable for
use in the left hand).
1
https://pagure.io/liberation-fonts
xii
CONFIDENTIAL Pull-quote Conventions
The above text includes application names; system-wide menu names and items; application-specific menu
names; and buttons and text found within a GUI interface, all distinguishable by context.
Note the shorthand used to indicate traversal through a menu and its sub-menus. This is to avoid the difficult-
to-follow 'Select Mouse from the Preferences sub-menu in the System menu of the main menu bar' approach.
Italics denotes text that does not need to be imputed literally or displayed text that changes depending on
circumstance. Replaceable or variable text is presented as follows.
The mount -o remount file-system command remounts the named file system. For
example, to remount the home file system, the command is mount -o remount /home.
To see the version of a currently installed package, use the rpm -q package command. It
will return a result as follows: package-version-release .
Note the words in italics above — username, domain.name, file-system, package, version and release. Each
word is a placeholder, either for text entered when issuing a command or for text displayed by the system.
package org.jboss.book.jca.ex1;
import javax.naming.InitialContext;
xiii
CONFIDENTIAL Notes and Warnings
System.out.println("Created Echo");
Warning
A Warning should not be ignored. Ignoring warnings will most likely cause data loss.
Important
Important boxes detail things that are easily missed: configuration changes that only apply to the
current session, or services that need restarting before an update will apply. Ignoring Important
boxes won't cause data loss but may cause irritation and frustration.
Note
A note is a tip or shortcut or alternative approach to the task at hand. Ignoring a note should
have no negative consequences, but might lead to a missed out on a trick that makes life easier.
xiv
Chapter 1. CONFIDENTIAL
Introduction
AlgoTrader is a comprehensive algorithmic trading platform that enables both buy side and sell side trading
firms to rapidly develop, simulate, backtest and deploy automated quantitative trading strategies on a single
platform. Designed by industry experts, it gives users maximum control over their trading experience. Initially
designed for global equities, futures, forex and options, AlgoTrader now fully supports automated trading of
Cryptocurrencies. AlgoTrader is an extremely reliable and robust system built on a multi-threaded, memory
efficient, highly concurrent architecture. It is optimized for high availability and performance to support
uninterrupted trading.
1
https://www.algotrader.com/product/overview/
2
https://www.algotrader.com/product/demo-system/
3
https://www.algotrader.com/product/demo-system/
4
https://www.algotrader.com/product/video/
5
https://www.algotrader.com/product/architecture/
6
https://www.algotrader.com/product/screenshots/
7
https://www.algotrader.com/features/
8
http://doc.algotrader.ch/AlgoTraderFactsheet.pdf
9
https://www.algotrader.com/product/3rd-party-libraries/
1
Chapter 2. CONFIDENTIAL
Note
It is generally recommend not to use paths with spaces for any of the components used by
AlgoTrader (e.g. C:\Program Files).
Java JDK
1
Install the latest Java JDK 1.8 from Oracle
Important
Algotrader requires Java 1.8.x. Do not use Java 1.9 or greater.
It is necessary to have a Java JDK (Java Development Environment), a Java JRE (Java
Runtime Environment) will not be sufficient.
Please set the JAVA_HOME environment variable to point at the directory where the Java
JDK is installed. You also need to add JAVA_HOME\bin to your PATH variable. Setup java
2
environment variables:
Maven
AlgoTrader uses Apache Maven for handling of dependencies. The AlgoTrader Eclipse IDE already has
an embedded Maven installation integrated. In case one wants to use Maven from the command line,
it is necessary to download and install the latest version of Maven and setup it`s environment variables
3
according to the link Maven setup .
In particular, please set the MAVEN_HOME environment variable to point at the directory where Maven is
installed. You also need to add MAVEN_HOME\bin to your PATH variable.
All AlgoTrader artifacts are located on our Nexus server which is password protected:
https://repo.algotrader.ch/nexus/
It is necessary the create the following file <user-home>\.m2\settings.xml below content there, to make
sure Maven can access our Nexus server. Folder .m2 should have been automaticaly created while runing
maven for the first time.
1
http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
2
https://docs.oracle.com/javase/tutorial/essential/environment/paths.html
3
http://maven.apache.org/install.html
2
CONFIDENTIAL Prerequisites
<profiles>
<profile>
<id>algotrader</id>
<repositories>
<repository>
<id>algotraderrepo</id>
<url>https://repo.algotrader.ch/nexus/repository/general/</url>
</repository>
<repository>
<id>archetype</id>
<url>https://repo.algotrader.ch/nexus/repository/general/</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>algotraderrepo</id>
<url>https://repo.algotrader.ch/nexus/repository/general/</url>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>algotrader</activeProfile>
</activeProfiles>
</settings>
3
CONFIDENTIAL Prerequisites
Note
Please replace myusername and mypassword (both appear twice!) with the username and
password provided when licensing AlgoTrader.
Git
AlgoTrader uses Git as its source code management system. The AlgoTrader Eclipse IDE already has a
Git installation integrated (EGit).
In case one wants to use Git from the command line it is necessary to download and install the latest
version of Git.
4 5
On Windows use TortoiseGit in combination with Git for Windows .
MySQL
Note
MySQL is not needed for simulations based on the embedded / in-memory database H2
6
Download and install MySQL Community Server 5.7 .
Important
Algotrader requires MySQL 5.7.x. Do not use MySQL 8.0 or greater.
Per default the system uses the user name root and password password. To change username and/or
password the following properties need to be updated inside conf-core.properties. Alternatively the
properties can be changed via Section 2.3, “VM Arguments”:
# database password
#{"type":"String","label":"Data Source Password"}
dataSource.password = password
You can create the root user/set the DB password using the following command:
4
https://tortoisegit.org
5
https://gitforwindows.org/
6
https://dev.mysql.com/downloads/mysql/5.7.html#downloads
4
CONFIDENTIAL Prerequisites
To work with MySQL it is recommended to install a MySQL client. There are many different MySQL clients
available to choose from:
7
• MySQL Workbench (free)
8
• Toad for MySQL (free)
9
• SQLyog MySQL (commercial)
Note
The Java MySQL JDBC driver sometimes has issues connecting to the MySQL database
depending on the MySQL timezone setting. Java Exceptions like the following are an
indication for this issue:
To fix the issue it is recommended to change the MySQL timezone setting by executing the
following MySQL statement
To be sure you do not lose this information, e.g. on reboot, you can set a time zone in the
MYSQL my.cnf or my.ini file
[mysqld]
default-time-zone=+00:00
explicit_defaults_for_timestamp=1
10
If that is not sufficient, follow the instructions under My SQL Time Zone Issues
Flyway
11
AlgoTrader uses Flyway to manage database updates. Flyway executes database migration scripts to
ensure that the database is in the state corresponding to the version of AlgoTrader installed.
7
https://www.mysql.com/products/workbench/
8
https://www.quest.com/products/toad-edge/
9
https://www.webyog.com/
10
https://dev.mysql.com/downloads/timezones.html
11
https://flywaydb.org/
5
CONFIDENTIAL Prerequisites
12
Please install the Flyway command line version and add flyway directory to $PATH variable in System
Environment settings.
To apply all necessary migration scripts please execute the following command from inside the flyway
directory .
flyway migrate
This will create a schema named algotrader on the local machine including all necessary tables and
required reference data.
flyway migrate
-url=jdbc:mysql://hostname:port
-user=username
-password=password
-schemas=schemaname
Warning
This operation will erase all database content!
The display the current version of the database schema please execute the following command.
flyway info
13
For additional information on flyway please visit the flyway documentation .
InfluxDB
Note
InfluxDB is an optional component that is used to store historical data. In back testing it is
possible to use CSV files to provide historical data as an alternative to using InfluxDB
12
https://flywaydb.org/documentation/commandline/
13
https://flywaydb.org/documentation/maven/
6
CONFIDENTIAL Prerequisites
14
Linux/MacOS: Download the latest version of InfluxDB and install it according to the InfluxDB installation
15
instructions ( Install it in a directory/tree without spaces in the names).
16
Windows: Download version 1.5.2 of InfluxDB and unpack the file in a directory/tree without spaces in
the names.
Important
On Windows Algotrader requires InfluxDB 1.5.2. Do not use InfluxDB 1.6.0 or greater.
17
• download nssm
• unpack nssm
• update the following sections inside influxdb.conf by replacing <username> with the username that
will run InfluxDB
[meta]
# Where the metadata/raft database is stored
dir = "C:\\Users\\<username>\\.influxdb\\meta"
[data]
# The directory where the TSM storage engine stores TSM files.
dir = "C:\\Users\\<username>\\.influxdb\data"
# The directory where the TSM storage engine stores WAL files.
wal-dir = "C:\\Users\\<username>\\.influxdb\\wal"
• Go to nssm installed folder, choose win64 or win32 folder and start command propmt. Inside type: .
\nssm.exe install InfluxDB <full-path-to-influxd.exe> -config <full-path-to-influxdb.conf>
• Add an environment variable named HOME pointing to the directory where InfluxDB is installed
14
https://portal.influxdata.com/downloads#influxdb
15
https://docs.influxdata.com/influxdb/v1.5/introduction/installation//
16
https://dl.influxdata.com/influxdb/releases/influxdb-1.5.2_windows_amd64.zip
17
https://nssm.cc/download
7
CONFIDENTIAL AlgoTrader Eclipse IDE Installation
All InfluxDB related settings are available within the file conf-influxdb.properties
Per default username/password authentication is disabled. To set username and password based
18
authentication please visit the InfluxDB Authentication and Authorization guide .
• Installation based on AlgoTrader Eclipse Product (Recommended), see Section 2.1.2.1, “Installation based
on AlgoTrader Eclipse Product”
Important
Prior to installing the AlgoTrader Eclipse IDE please make sure that the necessary prerequisites
are installed.
To assist in setting up the AlgoTrader Eclipse IDE, there is a fully configured Eclipse based product which
contains all the AlgoTrader bundles and the required Eclipse plugins. Specifically, the AlgoTrader Eclipse
product contains:
• Eclipse Mars
• AlgoTrader Plugins
• XML Tools
18
https://docs.influxdata.com/influxdb/v1.5/query_language/authentication_and_authorization/
19
https://docs.influxdata.com/chronograf/v1.5/
20
http://docs.grafana.org/datasources/influxdb/
21
http://www.eclipse.org/
22
http://www.eclipse.org/
8
CONFIDENTIAL AlgoTrader Server Code Installation
• M2E Extensions
• Windows: https://repo.algotrader.ch/eclipse/product/algotrader-win32.win32.x86_64.zip
• Mac: https://repo.algotrader.ch/eclipse/product/algotrader-macosx.cocoa.x86_64.zip
• Linux: https://repo.algotrader.ch/eclipse/product/algotrader-linux.gtk.x86_64.zip
Instructions:
• After starting an Eclipse You need to setup a workspace. Select the default workspace or define a folder to
it. If folder in the path doesn`t exist, it will be created by default.
• Then open the Java perspective in the top right corner of the screen by clicking on the icon left of the
AlgoTrader icon.
To install the AlgoTrader server code via command line please perform the following steps.
If one hasn`t installed git, please refer to git installation in chapter Section 2.1.1, “Prerequisites”
Note
user name and password will be provided when signing up for an AlgoTrader license
9
CONFIDENTIAL AlgoTrader Server Code Installation
Note
Please make sure the Maven settings file is updated according to Section 2.1.1, “Prerequisites”.
Note
When running the build process for the first time, this will take a few minutes since all maven
dependencies have to be downloaded.
2.1.3.2. Eclipse
• Inside Eclipse switch to the Java Perspective ( Windows --> Open Perspective --> Java )
• Click Next
• Select master
• Click Next
• Select the new project bootstrap in the packate explorer, right click and select Import / Maven / Existing
Maven Projects and select:
• conf
• launch
10
CONFIDENTIAL AlgoTrader Server Code Installation
• Click Finish
• algotrader-conf
• algotrader-launch
Note
The compilation will show errors, which should go away after the next section has been
completed.
Before running the maven build from within Eclipse please make sure that the default Eclipse Java runtime
environment is pointing to a Java JDK. To verify please go to Window / Preferences / Java / Installed JREs.
If the default JRE is pointing to a Java JRE, then please add a reference to the Java JDK which was installed
according to Section 2.1.1, “Prerequisites”
To generate the code right click on the project bootstrap inside Eclipse and select / Run As / Maven install.
This will generate all maven modules.
Now refresh all projects. Eclipse will compile all java code automatically. In case there is an error message
Project configuration is not up-to-date with pom.xml... on any of the projects the please select: Maven->Update
Project from the project context menu.
11
CONFIDENTIAL Next Steps
Docker allows packaging of applications with all of its dependencies into a standardized unit for software
development.
At the core of the Docker platform is Docker Engine, a lightweight runtime and robust tooling that builds and
runs Docker containers.
24
For an in-depth description of Docker please visit the What is Docker page.
25
To get started with Docker please visit the Docker Engine Documentation page.
Note
On Mac and Windows please install Docker Toolbox that contains the Docker Engine
23
https://www.docker.com/
24
https://www.docker.com/what-docker
25
https://docs.docker.com/engine/
26
https://docs.docker.com/engine/installation/
27
https://docs.docker.com/compose/install/
12
CONFIDENTIAL Docker Containers
Note
On Mac and Windows please install the Docker Toolbox that contains Docker Compose
28
https://gitlab.algotrader.ch/general/algotrader/blob/master/docker-compose.yml
Login to Nexus
Login to the Docker Repository with the username and password provided when licensing AlgoTrader.
docker-compose up -d
http://localhost:9090
The above process will setup an AlgoTrader based system made up of the following Docker containers:
• AlgoTrader server
To startup AlgoTrader with different startup options please visit this chapter on starting AlgoTrader in a
Section 3.3, “Server Environment”
To startup one of the AlgoTrader example strategies please visit the appendix of this documentation.
A typical Docker based AlgoTrader installation is made up of the following Docker containers that can be
configured vie a docker-compose.yml file.
28
https://gitlab.algotrader.ch/general/Bootstrap/tree/master/launch/docker-compose.yml
13
CONFIDENTIAL Docker Containers
The following Docker environment variables are relevant for the AlgoTrader container:
• ALGOTRADER_HOST (default: algotrader). This variable is used by strategies to reference the AlgoTrader
Docker container.
• IB_GATEWAY_ACCOUNT optional, only needs to be specified if the IB login has multiple accounts associated
• SPRING_PROFILES (default:
live,pooledDataSource,iBMarketData,iBNative,iBHistoricalData,embeddedBroker,html5) Spring
profiles to be used (comma separated list)
In addition the command line switch -i can be used to load the MySQL sample data file (samples/db/mysql/
mysql-data.sql) on first start up. The sample data will only be loaded if the security table contains no data.
To use the -i switch please use the following directive inside docker-compose.yml:
command: -i
The AlgoTrader Docker container will run through the following process on startup:
1. Wait for MySQL to be available. When starting up MySQL and AlgoTrader at the same time (using docker
compose) it will take the database a few seconds to become available.
2. Run all Flyway migrate scripts. This happens only the first time the AlgoTrader Docker container is started
and does not happen again until the container is updated.
3. Load MySQL sample data if the -i command line switch is used (see above).
14
CONFIDENTIAL Docker Containers
AlgoTrader based trading strategies run in separate Docker containers when running in distributed mode. When
running a single strategy in embedded-mode the strategy will run inside the same Docker container as the
AlgoTrader server.
The strategy code is located in the directory /usr/local/strategy inside the Docker container.
All strategy Docker containers are based on the AlgoTrader Docker container, so environment variables from
the AlgoTrader docker container can be reused inside strategy containers.
To build a strategy Docker container the following Dockerfile has to be added to the root of the project:
FROM docker.algotrader.ch/algotrader/algotrader:latest
ENV STRATEGY_NAME=XYZ
WORKDIR /usr/local/strategy
ADD target/*.jar lib/
ENTRYPOINT ["/usr/local/algotrader/bin/docker-strategy-run.sh"]
CMD ["-e"]
The startup script supports both embedded and distributed mode, see: Section 3.2, “Live Trading Mode”
To start the strategy Docker container in embedded mode please use the -e command line switch inside the
docker-compose.yml file of your strategy:
command: -e
This will cause the system to run through the following process:
1. Wait for MySQL to be available. When starting up MySQL and AlgoTrader at the same time (using docker
compose) it will take the database a few seconds to become available.
2. Run all Flyway migrate scripts. This happens only the first time the strategy Docker container is started and
does not happen again until the container is updated.
3. Load MySQL data from db/mysql/mysql-data.sql. MySQL is only loaded if the entry in the strategy table
for $STRATEGY_NAME is missing
15
CONFIDENTIAL Docker Containers
To start the strategy Docker container in distributed mode please use the -d command line switch inside the
docker-compose.yml file of your strategy:
command: -d
This will cause the system to run through the following process:
When running the system in distributed mode the AlgoTrader server needs to be run in a separate Docker
container. Since trading strategies do not have access to the database directly MySQL data needs to be loaded
manually by connecting to the database with a MySQL client. It is therefore suggest to follow this process when
starting up the system in distributed mode:
Note
Please see the following chapter about changing IB API settings (Read-Only API)
2. Load MySQL data by connecting a MySQL client to port 3306 of the Docker Engine
3. Start strategies
docker-compose up -d XYZ
29
• Interactive Brokers IB Gateway
30
• IB Controller which allows running IB Gateway in an automated fashion
29
https://www.interactivebrokers.com/en/index.php?f=5041
30
https://github.com/ib-controller/ib-controller
16
CONFIDENTIAL Docker Containers
The following environment variables are relevant for the IB Controller container:
Unfortunately only few settings of the IB Gateway can be managed via the IB Controller. All other settings have
to be managed via the IB Gateway UI itself which is not visible on the Docker container.
This is especially cumbersome for the Read-Only API trading mode that the is set by default. If this mode is
active, placement of orders is not allowed.
To change any of the IB Gateway settings (e.g. Read-Only API trading mode) please execute the following
steps:
32
1. The IB Gateway container stores IB settings inside a Docker Volume . This volume can be mapped to a
local directory as follows.
volumes:
- /var/lib/tws:/var/lib/tws
This will make IB Gateway settings available in the local directory /var/lib/tws
On Windows
volumes:
- c:/Users/Administrator/Documents/tws:/var/lib/tws
This will make IB Gateway settings available in the local directory c:\Users\Administrator\Documents
\tws
4. Make necessary changes (e.g. deselect the Read-Only API check box) and click OK
31
https://en.wikipedia.org/wiki/Xvfb
32
https://docs.docker.com/storage/volumes/
17
CONFIDENTIAL Docker Containers
6. Inside the IB Gateway installation folder there will be one or multiple sub-directories starting which have a
name made up of 8-9 characters starting with a the letter d. Please select the directory with the latest time-
stamp and makes sure it contains a file named ibg.xml
7. Copy this directory (e.g. darykqwzr) into the IB Gateway settings directory linked above:
8. Copy the jts.ini file into the IB Gateway settings directory linked above:
Note
The above steps will not work for the public pmdemo account which gets reset upon each startup.
2.2.2.4. MySQL
33
MySQL provides a fully configured Docker container. For further details please visit MySQL on Docker Hub
The following environment variables are relevant for the MySQL container:
volumes:
- /var/lib/mysql:/var/lib/mysql
This will make MySQL data available in the local directory /var/lib/mysql
On Windows
volumes:
- c:/Users/Administrator/Documents/mysql:/var/lib/mysql
This will make MySQL data available in the local directory c:\Users\Administrator\Documents\mysql
33
https://hub.docker.com/_/mysql/
34
https://docs.docker.com/storage/volumes/
18
CONFIDENTIAL Docker Compose
2.2.2.5. InfluxDB
35
InfluxDB provides a fully configured Docker container. For further details please visit InfluxDB on Docker Hub
36
InfluxDB data is stored inside a Docker Volume . This volume can be mapped to a local directory as follows.
volumes:
- /var/lib/influxdb:/var/lib/influxdb
This will make MySQL data available in the local directory /var/lib/mysql
On Windows
volumes:
- c:/Users/Administrator/Documents/influxdb:/var/lib/influxdb
This will make MySQL data available in the local directory c:\Users\Administrator\Documents\influxdb
Docker Compose uses docker-compose.yml files to configure multi-container applications. AlgoTrader ships
with a default docker-compose.yml file located inside the top-level AlgoTrader project directory:
algotrader:
image: docker.algotrader.ch/algotrader/algotrader
command: -i
environment:
- VM_ARGUMENTS=-Dkeygen.id=...
links:
- mysql
- ibgateway
35
https://hub.docker.com/_/influxdb/
36
https://docs.docker.com/storage/volumes/
37
https://docs.docker.com/compose/
19
CONFIDENTIAL Docker Compose
- influxdb
ports:
- 9090:9090
- 61614:61614
ibgateway:
image: docker.algotrader.ch/interactivebrokers/ibgateway
environment:
TWS_USERNAME: pmdemo
TWS_PASSWORD: demouser
volumes:
- /var/lib/tws
mysql:
image: mysql:5.7.22
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: algotrader
ports:
- 3306:3306
volumes:
- /var/lib/mysql
influxdb:
image: influxdb:1.5.2
ports:
- 8086:8086
volumes:
- /var/lib/influxdb
Using this docker-compose.yml file will create Docker containers for AlgoTrader, IB Gateway, MySQL and
InfluxDB. The following items are present in the file:
• image: the name of the image to use in the format namespace/repository:version (e.g.
docker.algotrader.ch/algotrader/algotrader:latest)
• command: command line argument to pass to the Docker container (e.g. -i). See Section 2.2.2, “Docker
Containers” for supported command line arguments.
• links: services to link to this container (e.g. mysql & ibgateway). Adding a link will make the target container
accessible with the correct IP by its link name.
• ports: ports to map on the host machine. E.g. 3306:3306 will map port 3306 of the MySQL container to
3306 of the host machine
• environment: environment variables to use. See Section 2.2.2, “Docker Containers” for supported
environment variables
20
CONFIDENTIAL Docker Compose
• volumes: Docker Data volumes to create. E.g. c:/Users/mysql:/var/lib/mysql will map the directory /
var/lib/mysql inside the container to c:\mysql on the host machine.
2.2.3.1. Passwords
The example docker-compose.yml file specifies multiple passwords directly. for security purposes this often
38
not advisable. As an alternative passwords can be stored using Docker secrets
If a large number of properties need to be changed or if other config files need to be changed (e.g. fix.cfg)
it is recommended to follow this process:
4. Commit the change to the modified AlgoTrader container using the following command
Sometimes this is not enough and one wishes to write additional information (e.g. fix messages) to a separate
log file. To get access to this log file from outside the container it is advised to create an additional volume:
volumes:
- ~/fix.log:/usr/local/algotrader/logs/fix.log
38
https://docs.docker.com/engine/swarm/secrets/
39
https://docs.docker.com/engine/reference/commandline/logs/
21
CONFIDENTIAL Docker Management
In addition to using the Docker command line, several options exist for management of docker based
installations
2.2.4.1. Portainer
docker-ui:
image: portainer/portainer
command: -H unix:///var/run/docker.sock
ports:
- 9000:9000
volumes:
- /var/run/docker.sock:/var/run/docker.sock
40 41
For further details please visit Docker UI on Docker Hub and Docker UI on GitHub .
2.2.4.2. Kitematic
42
When running Docker on Windows or Mac Docker Kitematic provides a UI for management of the Docker
engine.
40
https://hub.docker.com/r/dockerui/dockerui/
41
https://github.com/kevana/ui-for-docker
42
https://kitematic.com/
22
CONFIDENTIAL VM Arguments
2.3. VM Arguments
Many characteristics of the system can be customized with VM arguments, the following list provides an
overview of commonly used VM arguments.
-DlogLevel
log4j log level (ERROR, WARN, INFO or DEBUG)
-Dspring.profiles.active
list of Spring Profiles to activate (see Section 25.1, “Starter Classes”)
-Xmx
increase the Java Heap Size to specified amount (e.g. 2048M)
AlgoTrader specific configuration parameters can be changed inside the .properties files. As an alternative
configuration parameters can also be provided as VM arguments in which case they will overwrite existing
parameters inside *.properties files.
-Dstatement.closePosition=false
23
Chapter 3. CONFIDENTIAL
Starting AlgoTrader
As a first step one needs to make sure that the appropriate algotrader license key is properly configured. The
license key was provided in the Email after signing up for the AlgoTrader free 30-day trial or when purchasing
an algotrader license. The license key needs to be configured inside the file /algotrader-conf/src/main/
resources/conf.properties as follows.
Now AlgoTrader can be started through various Eclipse Run Configurations which are available to launch
the various operation modes of AlgoTrader. To access the available Eclipse Run Configurations follow
screen shots below. For more information on Eclipse Run Configurations, please see https://wiki.eclipse.org/
1
FAQ_What_is_a_launch_configuration .
1
https://wiki.eclipse.org/FAQ_What_is_a_launch_configuration%3F
24
CONFIDENTIAL
25
CONFIDENTIAL Simulation Mode
In Eclipse Run Configurations named SimulationStarter-simulate-xxx are provided which contain the
following items:
Main-Tab
simulateWithCurrentParams
Arguments-Tab / VM Arguments
-Dsimulation=true
-DstrategyName=XXX
26
CONFIDENTIAL Live Trading Mode
-DdataSource.dataSetType=TICK
-DdataSource.dataSet=yyy
-Dspring.profiles.active=simulation,embeddedDataSource
Note
To run the strategy in simulation mode a dependency to algotrader-core has to be added to
the maven dependencies of the trading strategy. Alternatively one can create a separate maven
module containing dependencies to the strategy project as well as algotrader-core.
If using InteractiveBrokers the Trader Workstation or the IB Gateway have to be running with the following
configurations under API/Settings:
When running AlgoTrader in Live Trading Mode the AlgoTrader Server and the Strategies can either be run in
separate JVM's (distributed mode) or the entire system can be run within one single JVM (embedded mode).
Before starting AlgoTrader check the database table strategy. The column AUTO_ACTIVATE should be set to
true for records corresponding to the AlgoTrader Server and the trading strategy one wants to trade.
Note
Only one Strategy can be run at once in Embedded Mode
In Eclipse Run Configurations named EmbeddedStrategyStarter-xxx are provided which contain the
following items:
Main-Tab
27
CONFIDENTIAL Distributed Mode
Arguments-Tab / VM Arguments
-DstrategyName=TEST
-Dmisc.embedded=true
-
Dspring.profiles.active=live,<dataSource>,<marketDataProfile>,<brokerProfile>,embeddedBroker,ht
• marketDataProfile: the SpringProfile corresponding to the market data interface in use (e.g.
iBMarketData or bBMarketData)
• tradingProfile: the SpringProfile corresponding to the broker interface in use (e.g. iBNative or iBFix)
Note
To Start the AlgoTrader Server in distributed mode the Eclipse launch configurations ServerStarterXX is
provided which contain the following items:
Main-Tab
• Project: algotrader-core
Arguments-Tab / VM Arguments
-
Dspring.profiles.active=live,<dataSource>,<marketDataProfile>,<brokerProfile>,embeddedBroker,ht
• marketDataProfile: the SpringProfile corresponding to the market data interface in use (e.g.
iBMarketData or bBMarketData)
• tradingProfile: the SpringProfile corresponding to the broker interface in use (e.g. iBNative or iBFix)
28
CONFIDENTIAL Server Environment
To start a Strategy Eclipse Run Configurations StrategyStarter-xxx are provided which contain the following
items:
Main-Tab
Arguments-Tab / VM Arguments
-DstrategyName=TEST
-Dspring.profiles.active=live
xyz:
image: xyz
command: -d
environment:
- VM_ARGUMENTS=-Dkeygen.id=...
algotrader:
image: docker.algotrader.ch/algotrader/algotrader
container_name: algotrader
environment:
- VM_ARGUMENTS=-Dkeygen.id=...
Note
To run the system in embedded mode create a docker-compose.yml file similar to the following:
29
CONFIDENTIAL Embedded Mode
xyz:
image: xyz
command: -e
environment:
- VM_ARGUMENTS=-Dkeygen.id=...
links:
- mysql
- ibgateway
- influxdb
ports:
- 9090:9090
- 61614:61614
environment:
STRATEGY_NAME: XYZ
ibgateway:
image: docker.algotrader.ch/interactivebrokers/ibgateway
environment:
TWS_USERNAME: pmdemo
TWS_PASSWORD: demouser
volumes:
- /var/lib/tws
mysql:
image: mysql:5.7.22
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: algotrader
ports:
- 3306:3306
volumes:
- /var/lib/mysql
influxdb:
image: influxdb:1.5.2
ports:
- 8086:8086
volumes:
- /var/lib/influxdb
Please replace xyz / XYZ with the name of the trading strategy. Please refer to Chapter 4, Strategy Development
on how to create a new trading strategy.
To start the system in embedded mode please run the following command from the directory where the docker-
compose.yml file is located:
docker-compose up -d
30
CONFIDENTIAL Distributed Mode
This will create the following docker containers: strategy (xyz), ibgateway & mysql
To run the system in distributed mode create a docker-compose.yml file similar to the following:
xyz:
image: xyz
command: -d
environment:
- VM_ARGUMENTS=-Dkeygen.id=...
links:
- algotrader
environment:
STRATEGY_NAME: XYZ
algotrader:
image: docker.algotrader.ch/algotrader/algotrader
container_name: algotrader
environment:
- VM_ARGUMENTS=-Dkeygen.id=...
links:
- mysql
- ibgateway
- influxdb
ports:
- 9090:9090
- 61614:61614
ibgateway:
image: docker.algotrader.ch/interactivebrokers/ibgateway
container_name: ibgateway
environment:
TWS_USERNAME: pmdemo
TWS_PASSWORD: demouser
volumes:
- /var/lib/tws
mysql:
image: mysql:5.7.22
container_name: mysql
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: algotrader
31
CONFIDENTIAL Distributed Mode
ports:
- 3306:3306
volumes:
- /var/lib/mysql
influxdb:
image: influxdb:1.5.2
ports:
- 8086:8086
volumes:
- /var/lib/influxdb
Please replace xyz / XYZ with the name of the trading strategy.
To start the system in distributed mode please run the following command from the directory where the docker-
compose.yml file is located:
docker-compose up -d
This will create the following docker containers: strategy (xyz), algotrader, ibgateway & mysql
32
Chapter 4. CONFIDENTIAL
Strategy Development
Warning
It is recommended to perform thorough Simulation / Back Testing of newly developed strategies.
After that the strategy should be tested with a Paper Trading Account. At the end of a thorough
test procedure, the new strategy can be put into production. At the beginning of live trading it
is recommended to use a small trading account only.
The following diagram shows the general procedure for developing new strategies:
The following 2 options can assist in creating a new trading strategy, the AlgoTrader Strategy Wizard and the
AlgoTrader Maven Archetype.
In addition to this setup, you will need to create a database entry for your strategy. Please refer to the strategy
table definition.
33
CONFIDENTIAL AlgoTrader Strategy Wizard
Section 4.1, “Creating a Trading Strategy ”. The Strategy Wizard provides options for three different types of
Trading Strategies:
Inside Eclipse switch to the Java Perspective. The Strategy Wizard can be started via the File / New / Other
which will bring up the following screen where the Maven Project wizard can be selected:
On the next step the location for the newly created project (trading strategy) as well as the Eclipse working
set can be selected.
On the next screen please select the AlgoTrader Catalog and algotrader-archetype-esper for Esper
based strategies or algotrader-archetype-java for standard strategies (without Esper) or algotrader-
archetype-simple for extremely basic strategies
Group Id
The maven group id (e.g. algotrader), all lower-case, can contain periods
Artifact Id
The maven artifact id (e.g. algotrader-ema), all lower-case, can contain dashes
Version
The maven version (e.g. 1.0.0-SNAPSHOT), x.y.z, plus optionally -SNAPSHOT
Package
The java package name (ch.algotrader.strategy), all lower-case, can contain periods.
name
The name of the strategy (e.g. ema), all lower-case, no periods, no dashes
serviceName
The name of the strategy service (e.g. EMA), first letter upper-case or all upper-case, do not include Service
at the end (e.g. do not specify EMAService)
34
CONFIDENTIAL AlgoTrader Maven Archetype
Important
For Spring Auto-Wiring to work the package name needs to be ch.algotrader.strategy. If a
different package is assigned services (e.g. OrderService and LookupService) will not be
available.
For all of these items previously entered values can be reused by clicking the combo-box to the right of the field.
When clicking finish the Strategy Wizard will create a new Eclipse project using the AlgoTrader Artifact.
To create Esper bases strategies execute (replace <version> with the corresponding AlgoTrader version):
To create default strategies execute (replace <version> with the corresponding AlgoTrader version):
To create very simple strategies consiting of only one single java file execute (replace <version> with the
corresponding AlgoTrader version):
The Maven Archetype will ask for the following input parameters:
groupId
The maven group id (e.g. algotrader), all lower-case, can contain periods
artifactId
The maven artifact id (e.g. algotrader-ema), all lower-case, can contain dashes
version
The maven version (e.g. 1.0.0-SNAPSHOT), x.y.z, plus optionally -SNAPSHOT
35
CONFIDENTIAL Generated Artifacts Java Archetype
packageName
The java package name (ch.algotrader.strategy), all lower-case, can contain periods.
name
The name of the strategy (e.g. ema), all lower-case, no periods, no dashes
serviceName
The name of the strategy service (e.g. EMA), first letter upper-case or all upper-case, do not include Service
at the end (e.g. do not specify EMAService)
Important
/src/main/java/ch/algotrader/strategy/EMAService.java
The strategy service class
/src/main/java/ch/algotrader/strategy/EMAConfig.java
The strategy configuration class
/src/main/java/ch/algotrader/strategy/Metrics.java
A sample class that can be used within the strategy
/src/main/java/ch/algotrader/strategy/State.java
A sample enum that can be used within the strategy
/launch/*.launch
Eclipse Run Configurations to start the Strategy in embedded mode and simulation mode
/pom.xml
The Maven project object model file containing general information about the Trading Strategy
/Dockerfile
The Docker file
4.1.3.1. EMAService.java
The references to the Services provided by the AlgoTrader Server (e.g. OrderService, PositionService,
etc.) will be injected on startup by the Spring Framework
36
CONFIDENTIAL Generated Artifacts Java Archetype
@Override
public void onStart(final LifecycleEventVO event) {
getSubscriptionService().subscribeMarketDataEvent(getStrategyName(), getConfig().getSecurity
}
@Override
public void onBar(BarVO bar) {
getOrderService().sendOrder(order);
}
Once the strategy has reached the START live cycle phase subscribe to the security needed for this strategy
Construct an Order Value Object using the MarketOrderVOBuilder. The OrderVO contains a reference
to the strategy, the security, the account as well as the quantity and the order side.
Send the Order to the market via the OrderService
4.1.3.2. pom.xml
The Maven pom.xml file contains the Maven project definition as well as Maven dependencies:
37
CONFIDENTIAL Generated Artifacts Esper Archetype
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>algotrader</groupId>
<artifactId>algotrader-core</artifactId>
<version>...</version>
</dependency>
</dependencies>
<properties>
<java.version>1.8</java.version>
</properties>
</project>
4.1.3.3. Dockerfile
FROM docker.algotrader.ch/algotrader/algotrader:latest
ENV STRATEGY_NAME=EMA
WORKDIR /usr/local/strategy
ADD target/*.jar lib/
ENTRYPOINT ["/usr/local/algotrader/bin/docker-strategy-run.sh"]
CMD ["-e"]
/src/main/java/ch/algotrader/strategy/EMAService.java
The strategy service class
38
CONFIDENTIAL Generated Artifacts Esper Archetype
/src/main/resources/module-ema.epl
Esper Module containing statements related to signal generation
/src/main/resources/conf-ema.properties
Contains parameters used by the strategy (e.g. Moving average durations etc.)
/src/main/resources/META-INF/esper-ema.cfg.xml
Contains event-types, imports, variables and general Esper settings
/src/main/resources/META-INF/applicationContext-client-ema.xml
Spring Application Context File for the strategy
/src/main/resources/db/mysql/mysql-ema.sql
MySQL data file containing db data needed for this trading strategy in live trading mode
/launch/*.launch
Eclipse Run Configurations to start the Strategy in embedded mode and simulation mode
/pom.xml
The Maven project object model file containing general information about the Trading Strategy
/Dockerfile
The Docker file
4.1.4.1. EMAService.java
The references to the Services provided by the AlgoTrader Server (e.g. OrderService, PositionService,
etc.) will be auto injected on startup by the Spring Framework
@Override
public void onStart(final LifecycleEventVO event) {
getSubscriptionService().subscribeMarketDataEvent(getStrategyName(), this.securityId, FeedType.I
}
39
CONFIDENTIAL Generated Artifacts Esper Archetype
.setQuantity(this.orderQuantity)
.setSide(side)
.build();
getOrderService().sendOrder(order);
}
4.1.4.2. module-ema.epl
This Esper module contains the Esper statements for signal generation
The statement MOVING_AVERAGE generate signals by using the moving average function.
the statement SEND_ORDER calls the EMAService.sendOrder() whenever there is a moving average crossover
on the indicators. The Tag @Subscriber is used to instruct the EsperEngine to attach the Subscriber to this
statement. This way the sendOrder method is called whenever there is a signal.
@Name('MOVING_AVERAGE')
insert into
Indicator
select
ema(currentValue, movingAveragePeriodShort) -
ema(currentValue, movingAveragePeriodLong) as value
from
TickVO;
@Name('SEND_ORDER')
@Subscriber(className='emaService#sendOrder')
select
case when indicator.value > 0 then Side.BUY else Side.SELL end as side
from
pattern [every indicator=Indicator]
where
(indicator.value > 0 and prior(1, indicator.value) <= 0)
or
(indicator.value < 0 and prior(1, indicator.value) >= 0)
40
CONFIDENTIAL Generated Artifacts Esper Archetype
4.1.4.3. conf-ema.properties
This configuration file contains the parameters of the strategy:
#{"type":"Integer","label":"Account ID"}
accountId = 1
#{"type":"Integer","label":"Security ID"}
securityId = 1
This file uses special JSON comments that are used by the AlgoTrader Config Editor, see Section 10.2.3,
“AlgoTrader Configuration Editor”
4.1.4.4. esper-ema.cfg.xml
The esper configuration file looks like this:
</esper-configuration>
The above file configures the required variables along with their type. The actual values for the variables are
taken from conf-ema.properties.
4.1.4.5. applicationContext-client-ema.xml
A typical Spring Configuration File looks like this:
41
CONFIDENTIAL Generated Artifacts Esper Archetype
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="emaConfigParams"
class="ch.algotrader.config.spring.CustomConfigParamsFactoryBean" >
<property name="global" ref="configParams"/>
<property name="resource">
<value>classpath:/conf-ema.properties</value>
</property>
</bean>
<bean id="emaEngine"
class="ch.algotrader.esper.EngineFactoryBean">
<property name="strategyName" value="EMA"/>
<property name="configResource" value="esper-ema.cfg.xml"/>
<property name="configParams" ref="emaConfigParams"/>
<property name="initModules" value="test"/>
</bean>
<bean id="emaService"
class="ch.algotrader.EMAService" autowire="byName">
<property name="strategyName" value="EMA"/>
<property name="engine" ref="emaEngine"/>
</bean>
</beans>
4.1.4.6. mysql-ema.sql
The file contains an entry in the table strategy. The column AUTO_ACTIVATE means that the strategy will be
automatically run in simulation mode.
42
CONFIDENTIAL Generated Artifacts Esper Archetype
4.1.4.7. pom.xml
The Maven pom.xml file contains the Maven project definition as well as Maven dependencies:
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>algotrader</groupId>
<artifactId>algotrader-core</artifactId>
<version>...</version>
</dependency>
</dependencies>
<properties>
<java.version>1.8</java.version>
</properties>
</project>
4.1.4.8. Dockerfile
FROM docker.algotrader.ch/algotrader/algotrader:latest
43
CONFIDENTIAL Building a Trading Strategy
ENV STRATEGY_NAME=EMA
WORKDIR /usr/local/strategy
ADD target/*.jar lib/
ENTRYPOINT ["/usr/local/algotrader/bin/docker-strategy-run.sh"]
CMD ["-e"]
mvn install
The Maven modules can now be deployed to a Maven repository (e.g. Sonatype Nexus) using:
mvn deploy
1
For further details regarding a maven deploy please visit the Maven deploy plug-in page
Execute the following Docker command to create a Docker image of the trading strategy that can be used for
productive deployments:
The Docker image can now be pushed to a Docker repository (e.g. Docker Hub, Sonatype Nexus or Amazon
ECR). For further details on pushing to a Docker repository please visit:
2
• Docker Hub
3
• Sonatype Nexus 3.0
4
• Amazon ECR
1
https://maven.apache.org/plugins/maven-deploy-plugin/
2
https://docs.docker.com/docker-hub/repos/#pushing-a-repository-image-to-docker-hub
3
https://support.sonatype.com/hc/en-us/articles/360000761828
4
https://docs.aws.amazon.com/AmazonECR/latest/userguide/docker-push-ecr-image.html
44
CONFIDENTIAL Java based Strategies
The following two Starters are available to start a trading strategy in embedded and in distributed mode.
StrategyStarter
StrategyStarter starts a strategy in stand-alone mode running in a separate JVM process. The strategy
will use ActiveMQ message broker to receive market data and other events from the server JVM process.
The server JVM process is expected to be running before the strategy JVM is started.
EmbeddedStrategyStarter
EmbeddedStrategyStarter starts a strategy in single JVM (embedded) mode, when server and strategy
run in the same process. Market data and other events are delivered directly to the strategy instances by
a single event dispatcher.
AlgoTrader is an event based system. All strategy related events are propagated to strategies as event objects
(e.g. Order, OrderStatus, Tick, Bar, etc.). Inside strategies these events are made available through event
handler methods, e.g.:
@Override
public void onBar(BarVO bar) {
// do something
}
Strategy classes can provide listeners for life-cycle events in order to receive notifications about strategy life-
cycle phase transitions and execute custom life-cycle dependent logic
@Override
public void onInit(final LifecycleEventVO event) {
// do something
}
@Override
public void onPrefeed(final LifecycleEventVO event) {
// do something
45
CONFIDENTIAL Java based Strategies
For further information on life-cycle events please visit Section 22.2, “Session life-cycle events”
Often a strategy has several states that it runs through during execution (e.g. FLAT, PENDING_LONG,
PENDING_SHORT, LONG, SHORT, etc.). For these situations it is advisable to use a Java Enum, e.g.:
package ch.algotrader.strategy;
In case a strategy trades multiple instruments and each instrument has its own state it is suggested to create a
Metrics object for each instrument. The Metrics object is a simple Java POJO that holds the state per instrument
and potential other information regarding the instrument (e.g. values of technical indicators):
Often a Java Action is triggered multiple times by a certain situation, because the underlying cause takes a
finite amount of time to be resolved.
Example: The current market level exceeds a defined stop, which triggers a closing order. However during
the time the order is being executed at the market, additional market data events are received. Because the
position is not yet closed by that time, another undesired closing order might get placed.
46
CONFIDENTIAL Java based Strategies
To prevent actions from happening multiple times a state object mentioned in the previous section is again
very helpful.
Whenever the predefined signal condition is met the state will be set to PENDING.
@Component
public class ABCService extends StrategyService {
@Override
public void onInit(final LifecycleEventVO event) {
@Override
public void onTick(TickVO tick) {
@Override
public void onOrderStatus(OrderStatusVO orderStatus) {
if (Status.EXECUTED == orderStatus.getStatus()) {
47
CONFIDENTIAL Java based Strategies
}
}
Strategies that trade multiple securities at the same time often have the requirement to associate a particular
order with a certain strategy. It is therefore often necessary to "tag" an order with additional meta data. This can
be accomplished using order properties, see Section 17.1.3, “Order Properties”. In tagging orders using order
properties it is also possible to distinguish automatically placed orders from manually placed orders (through
AlgoTrader client or external broker GUI).
Typically a strategy running corresponds to one entry in the database table strategy. In certain situations it
may be necessary for a running strategy to place orders into separate entries in the database table strategy.
Reasons for this might be:
• A strategy wants to have of multiple positions on the same instrument (i.e. long and short positions at the
same time)
Example:
A strategy named EXAMPLE would like to interact with the two separate entries in the database named LONG
and SHORT. For this purpose the strategy needs to be started with the strategyName set to EXAMPLE inside the
fileconf-cng.properties . Alternatively the properties can be changed via Section 2.3, “VM Arguments”:
strategyName = EXAMPLE
In addition one needs to create the two entries EXAMPLE|LONG and EXAMPLE|SHORT in the database table
strategy. the | separator causes strategy events like OrderStatusVO, FillVO and TransactionVO are
propagated to the running strategy EXAMPLE.
48
CONFIDENTIAL Java based Strategies
Note
Using other characters than | will not work and will cause strategy events not to arrive in the
EXAMPLE strategy.
Due to the expiring nature of Futures and Options, corresponding Positions have to be rolled prior to the
Expiration Date. Typically, this would involve the following steps:
4. On First Tick (see Section 8.4.8.1, “First tick callback”) open a new Position
When dealing with futures one has to decide on when to roll from the Front-Month future into the Back-Month
future. For this different philosophies exist:
• Roll on a fixed day prior to the expiration date or the first notice date
• Roll when the Back-Month future starts having a higher traded volume than the Front-Month future
Since Futures have an expiration date and are therefore not continuous, it is often not possible to base
indicators on them. There are several method for dealing with this situation:
• use the raw data and ignore the fact that price time series will have jumps
• On the rollover day, add the difference between yesterdays closing price of the Back-Month future and
yesterdays closing price of the Front-Month future to the combined time series. Alternatively one can use
the difference between today's opening price of the Back-Month future and yesterdays closing price of the
Front-Month future if only the time series for the generic 1st future is available. This method can be used for
P&L calculation it might however lead to negative prices on long time series.
• Instead of using addition as mentioned in the previous item use multiplication. This method however cannot
be used for P&L calculations. Depending on the indicator in use either addition or multiplication will be
adequate.
Please see Section 5.7, “Multi Security Simulations” for options on how to back test futures and options based
strategies that require multiple securities to be subscribed and unsubscribed during the back test.
49
CONFIDENTIAL Java based Strategies
Many strategies require that the local system clock is in sync with the remote server clock. Unfortunately it is
not possible to directly sync the local time with the remote server clock. However most servers are using NTP
or some other time sync mechanism to make sure there local clock is in sync with the official time defined by
NTP servers. As a result local system clock should also be synchronized with NTP servers. In most cases this
can be done directly through the operating system (e.g. Windows Time Service). For Windows Servers there
5
is also the time sync tool available which tends to be more precise than the Windows Time Service.
Strategy code running runs outside of Hibernate Sessions. Traversal along the Object Tree beyond what is
already loaded into the Hibernate session will throw a LazyInitializationException. All n-to-1 associations
(e.g. Position.getStrategy) will be fetched eagerly so no LazyInitializationException will be throws.
Traverse of an uninitialized relation from Position to Security in strategy code would look like this:
Sometimes one needs to compare output data files to verify a trading strategy or to find a problem during
strategy development. CSV files can be imported into Microsoft Excel for the purpose of comparing them.
Comparing two files however remains tricky and repeated exporting/importing and comparing in Excel can be
cumbersome.
AlgoTrader provides a Java utility to compare ("diff") CSV files and assert the contents of the two files. For
example the following statement compares a backtesting output file actual.csv with an expected result file
expected.csv:
CsvDiff.diffAndLogAssertionErrors(fileDiffer,
new File("expected.csv"), new File("actual.csv"));
5
http://www.timesynctool.com/
50
CONFIDENTIAL Java based Strategies
The fileDiffer argument instructs the tool how to perform the diff operation. It can be constructed as follows:
In the above statement, expectedDef and actualDef define the columns of the two files; the differ gives
exact instructions on how to perform the comparison of the files.
The columns of a CSV file are provided as implementations of CsvColumn which can be done with an enum:
@Override
public int index() {
return ordinal();
}
@Override
public ValueConverter<?> converter() {
return converter;
}
}
After defining the columns of both CSV files via CsvDefinition as indicated above we need to give instructions
on how to perform the actual diff operation itself. In the simplest case we just compare the CSV files line by
line and assert all or selected columns of the two files:
51
CONFIDENTIAL Java based Strategies
The diff can also contain more complex instructions. For instance assume that we have exactly one BUY/SELL
order per instrument and day but the ordering of instrument and side within a given date may be random. To
align the correct rows for comparison, we must provide grouping and sorting hints for this case:
Now we are ready to run the CsvDiff.diffAndLogAssertionErrors(..) statement from above. The result
may look similar to the following:
52
CONFIDENTIAL Esper based Strategies
When developing strategies using Esper in addition to Java code it is generally recommended to do Time-
based Market Data Analysis and Signal Generation inside Esper Statements. Procedural actions like placing
an order or subscribing to a Security are predominantly done inside Java Code.
By means of the TestSubscriber, selected values of a Statement can be printed to the Console.
@Name('TEST')
@Subscriber(className='ch.algotrader.esper.subscriber.TestSubscriber')
@SimulationOnly
select
valueA,
valueB,
valueC
from
TestEvent;
Often strategies are based on a technical indicator. During simulation it is often desirable to log the values of
such an indicator to a log file. This can be done with the following statement:
@Name('INSERT_INTO_INDICATOR')
@Listeners(classNames={'ch.algotrader.esper.listener.IndicatorListener'})
select
dateTime.toDate() as dateTime,
valueA,
valueB
valueC
from
Indicator;
Above statement will log dateTime, valueA, valueB and valueC to a the file files/report/
IndicatorReport.csv
53
CONFIDENTIAL Esper based Strategies
Esper has a sophisticated variable management functionality. It is possible to access those variables from Java
through the following methods:
6
For further details on Esper Variables please visit the Esper Documentation
Complex computations should be handled outside Esper. For this purpose, it is often easier to create a small
Utility class and use its methods inside the statement. Example:
package ch.algotrader.strategy;
<auto-import import-name="ch.algotrader.strategy.MyUtil"/>
Now, the Utility class can be used in an Esper Statement like this to adjust a trailing stop loss:
select
tick.last,
value
from
TickVO as tick,
method:MyUtil.calculate(tick.last) as value
6
http://esper.espertech.com/release-5.5.0/esper-reference/html/epl_clauses.html#variables_overview
54
CONFIDENTIAL Esper based Strategies
7
For further details on using static methods please visit the Esper documentation
If two statements are based on the same Event, it is necessary to set a priority for each statement to make
sure, the system behaves deterministically:
@Name('STATEMENT_1')
@Priority(2)
select * from A;
@Name('STATEMENT_2')
@Priority(1)
select * from A;
8
For further details on statement priorities please visit the Esper documentation
When starting up a strategy in Live Trading Mode, it is often necessary to initialize technical indicators that
have a look-back period. This initialization is done by feeding historical market data into the Esper Engine.
@Override
protected void onPrefeed(final LifecycleEventVO event) {
switch (event.getOperationMode()) {
case REAL_TIME:
feedMarketData();
break;
}
}
This method will load all ticks for the security (defined by securityId) for the last hour. It will then feed all of
them sequentially to the local EsperEngine.
7
http://esper.espertech.com/release-5.5.0/esper-reference/html/epl_clauses.html#joining_method_syntax
8
http://esper.espertech.com/release-5.5.0/esper-reference/html/epl_clauses.html#epl-syntax-annotation
55
CONFIDENTIAL Esper based Strategies
9
For further details on Esper Coordination please visit the Esper documentation
Note
Feeding of live market data only starts in the START live cycle phase. This prevents mix-up of
historical pre-feed data and live market data.
package ch.algotrader.strategy;
If the variable needs to have an initial value, the variable has to be declared with a statement.
@Name('CREATE_VAR_STATE')
create variable ch.algotrader.enumeration.State state = State.FLAT;
It is now possible to query current state inside Esper statements like this:
10
In case a strategy trades multiple instruments and each instrument has its own state an Esper Named Window
can be used instead of an Esper Variable.
@Name('METRICS_WINDOW')
create window
MetricsWindow.std:lastevent()
9
http://esper.espertech.com/release-5.5.0/esperio-reference/html_single/index.html#csv-step-3
10
http://esper.espertech.com/release-5.5.0/esper-reference/html/nwtable.html
56
CONFIDENTIAL Esper based Strategies
as
Metrics;
@Name('INSERT_INTO_METRICS_WINDOW')
insert into
MetricsWindow
select
*
from
Metrics;
The first statement creates the Named Window and the second statements inserts all Metrics events into the
Named Window.
The Metrics object is a simple Java POJO that holds the state per instrument (and potential other information
regarding the instrument):
To initialize the Named Window one Metrics event per instrument has to be sent into the Esper Engine upon
startup of the strategy
It is now possible to query current state inside Esper statements like this:
57
CONFIDENTIAL Esper based Strategies
Example: The current market level exceeds a defined stop, which triggers a closing order. However during
the time the order is being executed at the market, additional market data events are received. Because the
position is not yet closed by that time, another undesired closing order might get placed.
To prevent actions from happening multiple times a state object mentioned in the previous section is again
very helpful .
Whenever the predefined signal condition is met the state will be set to PENDING
@Name('LONG_TRIGGER')
on
//trigger event (can also be the MetricsWindow itself)
update
MetricsWindow as metricsWindow
set
state = State.PENDING_LONG
where
// condition
and
metricsWindow.state = State.SHORT or metricsWindow.state = State.FLAT
@Name('SEND_ORDER')
@Subscriber(className='xyzService#sendOrder')
select
state
from
MetricsWindow
where
state = State.PENDING_LONG
or
state = State.PENDING_SHORT;
Once the state has been changed to PENDING_LONG the statement SEND_ORDER will trigger an order to be sent.
Since the LONG_TRIGGER statement only triggers if the state is either SHORT or FLAT it will not trigger again once
the state has been set to PENDING_LONG.
Once the order has been fully executed (potentially using a Section 8.4.8.2, “Trade callback”) the state needs
to be changed to LONG
58
CONFIDENTIAL Esper based Strategies
Often strategies rely on indicators that are based on OHLC Bars. Since Live Market Data (i.e. Ticks) are not
delivered in the format of Bars, it is often necessary to create Bars from arriving Ticks. Section 18.1, “Creation
of Bars based on Ticks” explains how to do this.
Especially for Option and Future based strategies it is often not possible to subscribe to the entire Option
or Futures Chain in advance. Therefore the actual Security the strategy is interested in, is evaluated and
subscribed to at runtime. There are often steps that should take place immediately after the first market data
event has arrived for such a security.
Section 8.4.8.1, “First tick callback” explains how to use a FirstTickCallback for this purpose
A common use case is to wait for the full execution or cancellation of an order and then take some additional
action.
Section 8.4.8.2, “Trade callback” explains how to use a TradeCallback for this purpose.
In this context it is also important to remember that when trying to close a position there might still be open
orders associated with the corresponding security and strategy. It is suggested to cancel all corresponding
orders, attach a TradeCallback to the cancellation and only close the position once all cancels have been
confirmed.
Also keep in mind that an order might receive multiple fills in live trading. For example if one wants to send a
Stop Order for each executed Order it is important to use the filled quantity and not on the original order quantity.
In handling partial fills the TargetPositionOrder can be useful, please see: Chapter 23, Execution Algos
Upon system startup strategies run through the life cycle phases as defined in Section 3.2, “Live Trading
Mode”. At the same time market data connections are established. Due to the asynchronous nature of these
two processes it is not predetermined which one will complete first. Typically within the START life cycle phase
market data is subscribed. This however will fail if the market data adapter has not reached its SUBSCRIBED
state by that time. To circumvent this issue the following Esper patter can be used, which waits for both the
first LifecycePhaseVO and SessionEventVO to arrive before it fires:
select *
from pattern[LifecycleEventVO(phase=LifecyclePhase.`START`)
and SessionEventVO(state=ConnectionState.SUBSCRIBED)];
59
CONFIDENTIAL Esper based Strategies
@Subscriber(className='OrderService#cancelAllOrders')
select
null
from
pattern [every (timer:at(0, 18, *, *, 1:5)];
The above statement will cancel all open orders at 18:00:00 Mo-Fri.
With the following statements it is possible to calculate a constant maturity market value:
@Name('INSERT_INTO_FRONT_FUTURE_TICK')
insert into
FrontFutureTick
select
tick.*
from
TickVO as tick unidirectional,
method:lookupService.getSecurity(tick.securityId) as security
where
cast(security.duration?, int) = 1;
@Name('INSERT_INTO_BACK_FUTURE_TICK')
insert into
BackFutureTick
select
tick.*
from
TickVO as tick unidirectional,
method:lookupService.getSecurity(tick.securityId) as security
where
cast(security.duration?, int) = 2;
@Name('CONSTANT_MATURITY')
select
ConstantMaturityUtil.getConstantMaturityValue(front, back) as value
from
pattern [every(
(front=FrontFutureTick -> (back=BackFutureTick where timer:within(1 hour)))
or
60
CONFIDENTIAL Strategy life-cycle events
The actual static method to calculate the constant maturity value looks like this:
All trading strategies allocate a certain amount of memory to objects. If however those object allocations are
never released the corresponding memory will not get freed which will lead to a memory leak. This is especially
a concern for strategies that are kept running for an extended period of time.
In addition one has to be careful with Esper statements not to introduce memory leaks. For example the
following statement is potentially dangerous since it just keeps all Tick Events it receives:
In addition the AlgoTrader life cycle manager supports two modes of operation: REAL_TIME and SIMULATION.
In both modes all strategies transition through the same life-cycle phases. Depending on the operation mode
not all phases may be relevant.
Phase Description
INIT Called after deploying all modules of the Server Engine but before deploying the init
modules of the Strategy Engines.
61
CONFIDENTIAL Strategy Groups
Phase Description
PREFEED Called after deploying the init modules with Engine#deployInitModules() of Strategy
Engines but before deploying their run modules and before feeding any market data events.
START Called after deploying the run modules of all Engines. At this time Market data events start
feeding into strategy engines.
EXIT In SIMULATION mode this event occurs after finishing the simulation and before sending an
EndOfSimulationVO event and before publishing simulation results. In REAL_TIME operation
mode an EXIT life cycle event occurs when the virtual machine begins its shutdown.
Strategies can subscribe to these life-cycle events be overwriting the corresponding live-cycle method of the
StrategyService:
@Override
public void onInit(final LifecycleEventVO event) {
...
}
For this purpose AlgoTrader provides support for strategy and engine templates as well as strategy groups
based on Spring XML configuration and abstract Spring beans. This enables strategy developers to define
abstract templates for strategy engines and strategy services and then define concrete instances of those
strategies with custom configuration.
Definition of strategy templates and engine templates typically look like this..
62
CONFIDENTIAL Strategy Groups
<at:strategy name="box-narrow"
configClass="ch.algotrader.strategy.box.BoxConfig"
engineTemplate="boxEngineTemplate"
serviceTemplate="boxServiceTemplate"
resourceName="box-narrow.properties" />
<at:strategy name="box-wide"
configClass="ch.algotrader.strategy.box.BoxConfig"
engineTemplate="boxEngineTemplate"
serviceTemplate="boxServiceTemplate"
resourceName="box-wide.properties" />
<at:strategy name="box-narrow"
configClass="ch.algotrader.strategy.box.BoxConfig"
engineTemplate="boxEngineTemplate"
serviceTemplate="boxServiceTemplate" />
<at:strategy name="box-wide"
configClass="ch.algotrader.strategy.box.BoxConfig"
engineTemplate="boxEngineTemplate"
serviceTemplate="boxServiceTemplate" />
</beans>
63
CONFIDENTIAL Strategy Groups
Note
AlgoTrader strategy groups use custom XML namespace at. To enable this name space above
xmlns has to be added to the document definition.
Multiple strategy instances can be grouped together and assigned individual weights in the group. Strategy
groups can executed as one unit using provided Spring profile.
<beans profile="simpleGroup">
<at:strategyGroup id="simpleGroup">
<at:strategyItem name="box-narrow" weight="0.2"/>
<at:strategyItem name="box-wide" weight="0.8"/>
</at:strategyGroup>
</beans>
To start the simpleGroup activate the Spring profile through the following Section 2.3, “VM Arguments”
-Dspring.profiles.active=...,simple
The AlgoTrader Eclipse IDE provides a visual editor for strategy groups, for further details please see
Section 10.2.3, “AlgoTrader Configuration Editor”
64
Chapter 5. CONFIDENTIAL
Strategy Backtesting
For back testing historical data can be provided to strategies either via .csv files or via Section 19.1, “InfluxDB”.
Securities specified within the table subscription or securities subscribed to via the SubscriptionService are
fed to the Strategy.
To feed data from CSV files during a back test the following property needs to be set inside conf.properties.
Alternatively the properties can be changed via Section 2.3, “VM Arguments”:
For further details on file format and storage location of CSV files please see Section 19.7, “Market Data File
Format”.
Note
When feeding historical data with CSV files it is not possible to set a particular time range for
the simulation. If this is a requirement please feed data through InfluxDB
To feed data from InfluxDB during a back test the following properties need to be set inside conf.properties.
Alternatively the properties can be changed via Section 2.3, “VM Arguments”:
The tables Subscription, Position, Combination, Component & Property have a field persistent which
has the following meaning:
• persistent = true: the corresponding entry will NOT be deleted before the start of a backtest
• persistent = false: the corresponding entry will be deleted before the start of a backtest
65
CONFIDENTIAL Exchange Simulator
what portion of the order gets executed. In addition the ExecutionModel also contains the logic to calculate
commissions and fees that should be added to an order.
AlgoTrader contains a DefaultExecutionModel which provides a reasonable default logic for executing
orders. The DefaultExecutionModel provides the following properties inside the file conf.properties where
they can be changed. Alternatively the properties can be changed via Section 2.3, “VM Arguments”:
It is possible to replace the DefaultExecutionModel with a custom implementation that implements the
interface ExecutionModel. The custom Execution Model needs to be registered as a Spring Bean in the
following locations:
• For live trading (to be used globally): /META-INF/applicationContext-env.xml. This file needs to be in
the classpath, e.g. in the conf project under src/main/resources.
During the simulation process transaction as well as position and cash_balance updates are executed in
the database. It is therefore possible to use a standard database reporting tool to perform additional analysis
on it, provided you are not running vs. the H2 in-memory database (Spring profile embeddedDataSource).
To use the Exchange Simulator the Spring profile simulation has to be used, e.g.
-Dspring.profiles.active=simulation...
Note
Note than when using the Exchange Simulator in live trading, order will be executed against
live data internally and will not get sent to the external Broker or Exchange. If the Spring Profile
simulation is enabled all other external Order Services will be disabled.
66
CONFIDENTIAL Simulation Process
-Dsimulation=true denotes a back test and will effectively disable external MarketData
services, so if the intention is to run exchange simulator against live market data, make sure
that this parameter is set to false
All external order services must be disabled, e.g. Spring profiles like bFX, bFL etc must be
inactive
3. An initial amount (USD 1'000'000 per default) is allocated to each strategy (the initial amount can be changed
through the simulation.initialBalance setting inside conf.properties)
5. The life cycle phase INIT is broadcasted to all strategies. During this phase potential initiation steps can
be invoked.
6. All strategy initModules Modules are deployed (if using Esper based strategies)
7. The life cycle phase PREFEED is broadcasted to all strategies. During this phase technical indicators can be
initialized using historical data
8. All strategy runModules Modules are deployed (if using Esper based strategies)
9. Market data subscriptions are initialized based on entries in the table subscription
10.The life cycle phase START is broadcasted to all strategies. During this phase eventual actions like security
subscriptions can be taken care of
11.At that time the actual simulation starts and market data events are starting to be sent into the Esper Engines
12.The life cycle phase EXIT is broadcasted to all strategies. During this phase eventual cleanup actions can
be taken care of
13.At the end of each simulation run, metrics are printed to the console (if enabled), see Chapter 28, Metrics
67
CONFIDENTIAL Single Run Simulation
23.The Excel based back test report is created and statistics are displayed to the console, see Section 5.5,
“Performance Statistics”
Using Numerical Optimization functions (i.e. Brent & Newton) optimal parameter ranges can be determined
in an automated fashion.
simulateBySingleParam
One Simulation run with a parameter set to the defined value. The example below will do one run with parameter
a set to 0.8
simulateBySingleParam a:0.8
simulateByMultiParam
One Simulation run with multiple parameters set to defined values. The example below will do one run with
parameter a set to 0.8 and b set to 12.0
simulateByMultiParam a:0.8,b:12.0
1
https://www.algotrader.com/cloud-based-trading-strategy-optimization-using-algotrader-2-1-amazon-elastic-map-reduce/
68
CONFIDENTIAL Automated Parameter Optimization
optimizeSingleParamLinear
Multiple Simulation runs by incrementing the value of one parameter within a defined interval. The example
below will increment the value of parameter a starting at 0.1 to 0.9, incrementing by 0.1 for each run
optimizeSingleParamLinear a:0.1:0.9:0.1
optimizeSingleParamByValues
Multiple Simulation runs by iterating the value of one parameter according to defined list. The example below
will iterate the value of parameter a through the following list: 0.2, 0.8, 0.9 and 1.2
optimizeSingleParamByValues a:0.2:0.8:0.9:1.2
optimizeSingleParam
Multiple Simulation runs by setting the value of one parameter within the defined range and trying to find the
maximum Sharpe Ratio. The optimizer being used is UnivariateRealOptimizer. The example below will set
the value of parameter a between 0.1 and 1.0 (accuracy 0.01).
optimizeSingleParam a:0.1:1.0:0.01
optimizeMultiParamLinear
Multiple Simulation runs by doing a matrix Optimization of 2 or 3 parameters by incrementing their values within
a defined intervals. The example below will iterate through all possible combinations by incrementing the value
of parameter a starting at 0.1 to 0.9 (increment: 0.1), and incrementing the value of parameter b starting at
10.0 to 100.0 (increment: 5.0)
optimizeMultiParam
Multiple Simulation runs by adjusting the value of multiple parameters around their start values and trying to
find the maximum Sharpe Ratio. The example below will start the optimization by setting the value of parameter
a to 85.0 and parameter b to 150.0
In order to process parameters with the correct decimal scale the following property needs to be updated inside
conf.properties. Alternatively the property can be changed via Section 2.3, “VM Arguments”:
69
CONFIDENTIAL Performance Statistics
Note
In order for the parameter optimization to work the following properties need to be updated
inside conf.properties. Alternatively the properties can be changed via Section 2.3, “VM
Arguments”:
# if set to true, the Excel back test report will open at the end of a
simulation
report.openBackTestReport = true
Note
Before each back test run the Esper Engines will be reset. However Strategy services are not
reset. Due to this any state that is maintained within the Strategy service needs to be reset
within the onInit method.
Note
The values of Esper variables as well as Java properties get initialized on startup using Spring.
The actual optimization only happens once the Spring context is fully initialized. Due to this it
is necessary to overwrite the default values in the onInit from system. properties. This can be
done as follows for Esper variables:
getEngine().setVariableValue("propertyA", System.getProperty("propertyA"));
this.propertyB = System.getProperty("propertyB");
70
CONFIDENTIAL Performance Statistics
71
CONFIDENTIAL Performance Statistics
• BackTestReport.xlsm: the Excel based back test report (see image above)
The Excel based back test report can be modified in terms of formatting and layout if needed.
In addition when running a single simulation run, statistics will be displayed to the console in the following
format:
When running parameter optimizations, statistics will be displayed in the following summary format showing
the current parameter values as well as corresponding performance statistics of one run on one single line:
In addition to above General Performance statistics, strategy specific performance statistics are printed to the
console. These are retrieved by calling the method StrategyService.getSimulationResults of the strategy.
The amount of output during the simulation can be adjusted by setting the Log Level according to Chapter 29,
Logging.
72
CONFIDENTIAL In-Process Exchange Simulator
getSimulator().sendOrder(order);
Important
As the simulator is not integrated with the database any lookup regarding Order, OrderStatus,
Transaction, Position & CashBalance through the LookupService and PortfolioService
will return nothing. Also Execution Algos are currently not supported with the In-Process
Exchange Simulator
Some strategies that are based on multiple securities need to subscribe and unsubscribe securities during the
simulation. A typical example for this would be a Futures bases strategy that needs to unsubscribe an expiring
Future and at the same time subscribe to the next Future in the chain. To be able to subscribe and unsubscribe
securities during a simulation the following two options exist:
• Changing the following property inside the fileconf.properties will cause all CSV files present in the dataset
directory to be used for the simulation. (However, strategies still only receive market data for securities they
have subscribed to). Alternatively the properties can be changed via Section 2.3, “VM Arguments”:
# should all files in the dataSetLocation be used or just the ones corresponding to
defined subscriptions
dataSource.feedAllMarketDataFiles = true
73
CONFIDENTIAL Multi Security Simulations
• subscribe to all securities required in the INIT or PREEFEED phase, and then unsubscribes securities in the
START phase, that are only need later in the simulation.
74
Chapter 6. CONFIDENTIAL
Architecture
The architecture of AlgoTrader is composed of the following components.
The AlgoTrader Server provides the infrastructure for all strategies running on top of it. The AlgoTrader Server
holds the main Esper Complex Event Processing (CEP) engine. It is responsible for all domain model objects
and their persistence in the database. Different market data adapters are available to process live and historical
market data. On the other end adapters for different execution brokers and exchanges are available, which are
responsible for placing orders and receiving executions.
The AlgoTrader Server also provides business components for back testing, parameter optimization, analysis,
execution management, risk management, reporting, reconciliation and hedging.
On top of the AlgoTrader Server any number of strategies can be deployed. Strategies can either be coded
purely in Java or in a combination of Java and Esper code. Esper based strategies make use of a dedicated
Esper CEP engine. A strategy can deploy any number of SQL-like Esper statements for time-based market
data analysis and signals generation. Esper statements can invoke any number of procedural actions, such
as placing an order or closing a position, which are coded in Java. The combination of Esper statements and
Java Code provides a best-of-both-worlds approach.
For management and monitoring of the system different GUI clients exist. The AlgoTrader HTML5 Frontend
provides trading related functionality like charting, orders, positions & market data. Eclipse or IntelliJ IDE's are
used for strategy development.
75
CONFIDENTIAL
76
Chapter 7. CONFIDENTIAL
Domain Model
The following sections describe the Domain Model of the system using UML (unified modeling language).
7.1. Entities
The Main Entities of the system are specified within the following table:
Entity Description
Strategy Each object of this class represents a running strategy within the system
Security This is the base class of all securities in the system
SecurityFamily A group of Securities (e.g. all S&P 500 Futures)
Subscription Market Data Subscriptions of a Strategy for a particular Security are represented
by this class. For every Subscription the Strategy will receive Live Market Data
for the corresponding Security
MarketDataEvent Represents any type of market data related to a particular Security
Order An Order for a particular Security
Account An account held with an external Broker / Bank
77
CONFIDENTIAL Entities
Entity Description
Transaction Each Fill is recorded as a transaction in the database using this entity. In addition
the table transaction also stores transactions like interest, debit, credit & fees
Position Represents an exposure to a certain Security on the Market
Exchange An electronic exchange or venue
A full list of all Entities of the system will be discussed throughout the next sections. Entities of the system can
be categorized into the following three areas:
Reference Data
Represent static referential data like:
Market Data
Represent external events (Tick and Bar) coming from market data providers or internal events (Generic
Events) coming from another trading strategy. Market Data is typically immutable and of only momentary
interest to the trading strategies. Market Data Events are available as Value Objects only (but not as
Entities):
MarketDataEventVO and its subclasses TickVO, BarVO, QuoteVO, BidVO, AskVO, TradeVO and
GenericTickVO as well as any type of GenericEventVO
Transaction Data
Represent the financial state of trading strategies. Some of them (e.g. Transactions and Measurements)
are immutable whereas others (e.g. Positions and Balances) are mutable and change their values while
Orders are getting executed:
Besides providing Getters and Setters all Entities provide the following common features:
VO Converter
The static inner Converter class can be used to automatically convert the Entity to its corresponding Value
Object, see Section 7.3, “Value Object”
Factory
The static inner Factory class can be used to create new instances of an Entity
78
CONFIDENTIAL Strategy
7.1.1. Strategy
Regarding the question "what is 1 productive strategy?". It essentially up to the user, what he would like to
consider as one strategy. A strategy can have one or multiple instruments. And also regarding trading logic
there is no limitation.
However please note that the entire performance and reporting functionality of AlgoTrader happens on the
strategy level. So if one would like to see performance metrics on an instrument level one would have to
instantiate multiple strategies. Also, if it is a requirement to start and stop individual functions separately, it is
best to put them into two separate strategies.
On the technical side each separate strategy allocates a certain amount of overhead (memory and CPU). For
that reason it is best to combine functionality into as few strategies as possible if there are no good reasons
not to separate them.
The field autoActivate means that if a strategy is set to active corresponding market data subscriptions are
initiated automatically upon startup of the system. This is usesful in distributed mode when stragtegies and the
server run in different processes. If you restart the server in this scenario, subscriptions for the strategies are
automatically loaded again (without having to restart the strategies).
79
CONFIDENTIAL Strategy
There are several classes that are directly related to the strategy
Class Description
PortfolioValue On regular time intervals certain portfolio values (e.g. NetLiqValue,
CashBalance, etc.) are saved to the database for every strategy.
Attribute Description
cashBalance Market value of all open forex positions + cash amount available to the
strategy
marketValue Market value of all open (non-forex) positions
netLiqValue Cash balance + market value
realizedPL Realized P&L of all positions
unrealizedPL Unrealized P&L of all positions
All valuations (strategy and position level) can be queried via the PortfolioService. Fees are considered in
the calculations if properly configured.
80
CONFIDENTIAL Security
7.1.2. Security
The above UML Class diagram shows all available Security classes
Entity Description
Option A tradeable Option
Future A tradeable Future
Forex A Foreign Exchange Currency (FX) or Crypto Currency
Stock A Single Stock
Fund An ETF, Mutual Fund, etc.
Index An Index (e.g. Equity, Volatility, Commodity)
81
CONFIDENTIAL Security
Entity Description
GenericFuture A virtual Future with a fixed duration
IntrestRate Any type of Interest Rate
Bond A corporate or government Bond
Commodity A physical Commodity (e.g. Energy, Metals, Agriculture or Livestock). For
Commodity Futures use Future.
Combination A synthetic security composed of one or many Components (see Chapter 24,
Synthetic Securities and Derivative Spreads)
SecurityReference A generic link between one security and another
A Security Family contains common information about an entire family of securities (i.e. all general information
about options on S&P500 are stored using this class). The class provides fields like exchange, currency, and
tick size.
82
CONFIDENTIAL Security
83
CONFIDENTIAL Market Data Events
The above UML Class diagram shows all available SecurityFamily classes
Entity Description
OptionFamily Represents an Option chain associated with a single underlying
FutureFamily Represents a Futures chain associated with a single underlying
GenericFutureFamily Represents a GenericFutures chain associated with a single underlying
BondFamily Represents a chain of Bonds associated with a single underlying
The definition of the attributes of the classes Security and SecurityFamily are documented in the AlgoTrader
1
JavaDoc
The class EasyToBorrow Contains information about how many contracts of a particular Stock can be shorted
through a specific Broker.
SecurityReference Is a generic link between one security the owner and another the target. Using this class
it is possible for a Security to have links to multiple other Securities.
Market Data Events are available as Value Objects only but not as Entities:
1
http://doc.algotrader.ch/javadoc/index.html
84
CONFIDENTIAL Order
Entity Description
BarVO Open-High-Low-Close Price Bars, also containing volumes and volume weighted
average prices
TickVO Snapshot of the market at a particular point in time, containing information like
last price, last time, bid, ask, volume, etc..
QuoteVO Its subclasses represent the current best bid and offer BidVO and AskVO
TradeVO An actual order that was executed on the market, containing information like last
price, last size and volume
GenerickTickVO Represents additional price information made available by market data providers
(e.g. open price, close price, vwap price)
For simulation purposes Bars and Ticks can be supplied through CSV files (see Section 19.7, “Market Data
File Format”) or through InfluxDB (see Chapter 19, Historical Data). In live trading Trades, Bids and Asks are
received by the broker specific MarketDataService.
For conversion between Ticks and Bars please see Section 18.1, “Creation of Bars based on Ticks”.
7.1.4. Order
The following UML Class diagram shows the Order and its related subclasses.
85
CONFIDENTIAL Order
Entity Description
Order Base Class for all Order Types
OrderStatus Order Status changes received back from the Broker (e.g.
PARTIALLY_EXECUTED or CANCELLED) are represented by this class
OrderCompletition Similar to Order Status but only gets created once an order is fully
executed or cancelled and all corresponding database activity has been
completed.
OrderProperty An arbitrary property that can be attached to an Order. Through the type
attribute the OrderProperty can be marked as internal only or as fix
property or as IB property.
Fill Filled orders are represented by this Class
Transaction Each Fill is recorded as a transaction in the database using this entity.
In addition the table transaction also carries transactions like INTREST,
DEBIT, CREDIT & FEES
StopOrder
StopLimitOrder
Note
AlgoOrders and Order parent/child associations are not persisted to the database. After a
system restart AlgoOrders will therefore not continue execution automatically and will need to
be restarted manually.
86
CONFIDENTIAL Account
7.1.5. Account
An Account represents either an actual account, an account group (IB specific) or an allocation profile (IB
specific). An account is assigned to a particular orderServiceType (e.g. IB_NATIVE or FXCM_FIX) which
identifies the OrderService to use for this account. In addition the field sessionQualifier which is needed to
define the actual session in place (for FIX Connections). With this setup, it is possible to have multiple Sessions
(session qualifiers) per OrderServiceType and to have multiple Accounts per Session. If the field active is
set to true a potential corresponding Fix session will be activated.
Optionally an accountServiceType (e.g. IB_NATIVE or BFX) can be added which identifies the
AccountService to use for this account.
Accounts have an optional dependency to Exchange for cases when an account can only be used to trade on
one single Exchange (typical for Crypto Currency Exchanges).
Note
Orders sent to the market will always contain Account related information in an adequate way
(e.g. as a FIX Tag 1). Also Transactions which are based on an actual order will have an
association with a particular Account. However Positions do not hold any information regarding
Accounts. It is thus possible that a Position holds aggregated Quantities from several external
Accounts. Also it is possible to open a position through on account but then close it through
another (i.e. when using separate execution and clearing brokers). With this setup Strategies
do not have to worry about the actual Accounts the funds are located in. This way, a strategy
will always only see one Position per Security.
87
CONFIDENTIAL Transaction
7.1.6. Transaction
Each Fill is recorded as a transaction in the database using this entity. In addition the table transaction also
stores transactions like INTREST, DEBIT, CREDIT & FEES. A transaction is immutable and contains all relevant
information like dateTime, quantity, price, commissions as well as references to Account, Strategy, Security
and Position.
88
CONFIDENTIAL Position
7.1.7. Position
For any Strategy holding a particular Security a Position is created in the database. Even if this position is later
on closed (i.e. quantity = 0) the position will still remain in the database, because the associated Transactions
still have references to it.
In general, position values (e.g. marketPrice, marketValue, averagePrice, cost, unrealizedPL &
realizedPL) are calculated per actual strategy related position and show the price that would need to payed
if the position was closed at this moment
Attribute Description
realizedPL Total profit of closed parts of a position (parts of a position might still be open)
unrealizedPL Profit of the currently open part of a position
cost Total cost incurred to open the current position (potentially through multiple
orders). These values are based on the fee configurations
All valuations (strategy and position level) are available through the Section 7.2.12, “Portfolio Service”.
Warning
Cash Balances are derived by taking all Transactions of the given Security and Strategy
into account. It is therefore important not to modify Cash Balance entries directly in the
89
CONFIDENTIAL Subscription
database. In case transactions are added or modified manually to the database, please the
management action reset position and cash balances in the Figure 10.3, “AlgoTrader HTML5
Client Management”
7.1.9. Subscription
Market Data Subscriptions of a Strategy for particular Securities are represented by this class. For every
Subscription the Strategy will receive Live Market Data for the corresponding Security
7.1.10. Exchange
Exchanges around the world have different trading hours. Quit often there are different trading hours even for
different securities trading on the same exchange. In addition each exchange typically has different holidays
or days where trading starts late or trading stops early. Especially for futures trading there are often small
gaps between different trading periods of the same trading date. FX trading is often available 24 hours a day
without any gaps.
All of these scenarios are captured and maintained through the Entities Exchange, TradingHours and Holiday:
90
CONFIDENTIAL Property
7.1.11. Property
The classes Strategy, Position, Subscription and OrderPreference are derived from the abstract class
PropertyHolder. One or more Properties can be assigned to them. A Property can be of type Integer, Double,
Money (BigDecimal), Text (String), Date or Boolean (but only one at a time).
Important
Because PropertyHolders use Hibernate Union-Subclass strategy, Id's of different
PropertyHolder tables may not overlap. When Entities are saved through AlgoTrader unique
Id's will be enforced by Hibernate. But when saving rows in the database directly (within the
tables strategy, position, subscription or order_preference) one has to manually ensure there
are no overlapping Ids.
The following SQL script can be used to check whether a certain Id is already in use:
91
CONFIDENTIAL Order Preference
Class Description
OrderPreference This class allows definition of order default values (e.g. account, order type,
delays, etc.). Except for the order type, all values have to be defined through
Properties.
7.2. Services
The system is based on a Service Oriented Architecture (SOA). All operations of the system are provided as
Spring Services / Beans. The following groups of services exist:
1. Main Services, are available to both the AlgoTrader Server and Strategies
2. Client Services, which will be instantiated by each Strategy (and the AlgoTrader Server itself)
2
http://doc.algotrader.ch/javadoc/ch/algotrader/service/package-frame.html
92
CONFIDENTIAL Main Services
Inside strategies all service are injected by the Spring Framework and can be accessed as follows withing the
strategy service:
Service Description
AccountService Responsible for retrieval of account balances an initiation of withdrawals
CalendarService Responsible for information about Exchange trading hours and holidays
CombinationService Responsible for handling all Combination / Component related DB-
Operations.
FutureService Responsible for all future specific operations
HistoricalDataService Responsible for the retrieval of historical data from Historical Data
Providers
MarketDataService Responsible for the retrieval of market data as well as Subscription
Management.
MeasurementService Responsible for persistence and retrieval of Measurements related to
Strategy
OptionService Responsible for all option specific operations
OrderService Responsible for sending orders to the Market via defined Broker
Interface
PortfolioService Responsible for providing portfolio values
PositionService Responsible for management of positions, e.g. close position and
reduce position
PropertyService Responsible for persistence of Properties related to a PropertyHolder
ReferenceDataService Responsible for the download of option and future chains
93
CONFIDENTIAL Client Services
Service Description
MarketDataCacheService Provides a strategy local cache of market data and FX conversion rates
LookupService Provides general data lookup operations to other services
ConfigAwareStrategyService Base class for all strategy services which has references to all necessary
services and implements all event listener interfaces. In addition the
service receives a reference to the strategy config
StrategyService Base class for all strategy services which has references to all necessary
services and implements all event listener interfaces
SubscriptionService This service is used by the strategy for subscription management. The
actual DB related operations are carried out by the MarketDataService.
The MarketDataService should not be called directly by strategies.
Especially when trading multiple exchanges around the globe the CalendarService becomes very useful. It
provides convenient methods like:
• isOpen (is the specified exchange open at the current time or at the specified date time). Will return true if
no TradingHours are defined
• isTradingDay (is the current day or the specified day a trading day at the specified exchange)
• getOpenTime (gets the open time of the specified exchange on the current day or the specified day)
• getCloseTime (gets the close time of the specified exchange on the current day or the specified day)
• getNextOpenTime (gets the next open time of the specified exchange after the current date time or the
specified date time)
• getNextCloseTime (gets the close open time of the specified exchange after the current date time or the
specified date time)
In addition the Calendar service provides methods to identify a particular trading day, which will be important
to associate a particular order for clearing and reconciliation. If a trading session overlaps from one day to
another (e.g. starts on Sunday 23:00pm), the trading day will be considered the day when the session ends (e.g.
94
CONFIDENTIAL Combination Service
Monday). However in this example Monday would need to be set to true in the corresponding TradingHours
object.
Note
All dates and times in the CalendarService are converted to the system time, e.g. the market
opens at 09:30 EST but the system timezone is CET then the market opening time in the
CalendarService will be 15:30.
When trading one single exchange it is usually easiest to set the system time to the same
timezone of the exchange.
When trading exchanges in different timezones one has the choice of setting the system to
clock to the same timezone as one of the exchanges or leave the system time set to the local
timezone.
A Measurement can be created as follows whereas the time stamp will be set according to the current system
time:
95
CONFIDENTIAL Option Service
getMeasurementService().createMeasurement(strategyName, "myMeasurement",
DateUtil.dateForYMD(2018, 7, 20), 12.12345);
getMeasurementService().deleteMeasurement(measurementId);
To read Measurements from the database the Section 7.2.17, “Lookup Service” has to be used which provides
various Measurement lookup methods, e.g. getMeasurementByMaxDate or getAllMeasurementsByMaxDate.
Since some values (e.g. market value) depend on whether the position is long or short, aggregated position
values of different strategies for the same security cannot be retrieved just by adding position values from the
corresponding strategies. Example:
The sum of above market values would be -1'000 which is obviously wrong.
96
CONFIDENTIAL Position Service
As a consequence the PortfolioDAO provides lookup-methods that aggregate positions from the same
security (of different strategies) in the correct manner (e.g. findOpenPositionsAggregated).
Warning
Positions are derived by taking all Transactions of the given Security and Strategy into account. It
is therefore important not to modify Position entries directly in the database. In case transactions
are added or modified manually to the database, please the management action reset position
and cash balances in the Figure 10.3, “AlgoTrader HTML5 Client Management”
• resetPositions calculates all Position based on Transactions in the database and makes adjustments if
necessary.
Note
Closing and Reducing a position through the PositionService requires the definition of an
order_preference with the name DEFAULT. Fur further details see Section 17.1.1, “Order
Preferences”
The default order preference also includes an account, which means this feature is typically
only usable with one account/adapter. If more than one account is in use, positions should be
closes through the Section 7.2.11, “Order Service” by sending an order with a quantity that will
offset the current position.
97
CONFIDENTIAL Reference Data Service
getPropertyService().removeProperty(propertyHolderId, "myPropertyName");
To access the last traded price of an instrument one can use the following code inside strategies:
To access the access the current exchange rate between USD and EUR one can use the following code inside
strategies:
98
CONFIDENTIAL
Strategy Service & Config Aware Strategy Service
In addition to standard lookup methods above the LookupService also provides the following the generic
lookup methods find and findUnique that can be used in situations where a standard lookup method is not
available. These methods can be used as follows:
3
Please consult the JavaDoc for a full list of available methods.
In order to minimize the number of hits to the database the LookupService uses various levels of caching
when reading from the database.
Note
The feedType specifies the adapter to use when subscribing for market data (e.g. IB_NATIVE
specifies the InteractiveBrokers native API adapter)
3
http://doc.algotrader.ch/javadoc/ch/algotrader/service/LookupService.html
99
CONFIDENTIAL Reconciliation Services
Upon subscription market data will be feed to the trading strategy that initiated the market data subscription.
Market data will be feed to the corresponding Section 4.3.1.2, “Event Handler Methods” (e.g. onBar and onTick)
and also into the Esper Engine (if using Esper) where they are available as Bar and Tick events.
The SubscriptionService also supports the subscription for GenericEvents, see Section 18.5, “Generic
Events”
To reset a live trading system multiple types of resets can be specified to the reset method using the
Enumeration ResetType
TRADES
deletes all transactions (except the initial CREDIT)
resets all cash balances (except the one associated with the initial CREDIT)
ORDERS
delete all orders, order stati as well as order properties
SUBSCRIPTION
deletes non-persistent subscriptions
COMBINATIONS_AND_COMPONENTS
deletes non-persistent combinations and components
PROPERTIES
deletes non-persistent properties
MEASUREMENTS
deletes measurements
PORTFOLIO_VALUES
deletes portfolio values
100
CONFIDENTIAL Value Object
OPTIONS
deletes all options
FUTURES
deletes all futures
MARKET_DATA
deletes all bar and tick data
The method resetSimulation will reset the following items before each Simulation: Trades, Subscriptions,
Combinations/Components, Properties, Options (if option prices are simulated) and Futures (if future prices
are simulated).
Each Entity contains an inner Converter class that can be used to convert the Entity to its corresponding Value
Object.
In addition to Value Objects ValueObjectBuilders exist which help creating Value Objects. Example:
4
For a full list of all Value Objects please visit our JavaDoc
7.4. Enumerations
For selectable items with a fixed number of choices AlgoTrader contains Java 5 Enumerations. For a full list
5
of all Enumerations please visit our JavaDoc
4
http://doc.algotrader.ch/javadoc/ch/algotrader/vo/package-frame.html
5
http://doc.algotrader.ch/javadoc/ch/algotrader/enumeration/package-frame.html
101
Chapter 8. CONFIDENTIAL
Esper Engine
1
AlgoTrader uses the CEP (Complex Event Processing) engine Esper . AlgoTrader based strategies can
optionally make use of a dedicated Esper engine in addition to the Esper engine used by the AlgoTrader server.
A tailored Event Processing Language (EPL) allows expressing rich event conditions, correlation, possibly
spanning time windows, thus minimizing the development effort required to set up a system that can react to
complex situations.
Esper is a lightweight kernel written in Java which is fully embeddable into any Java process. It enables rapid
development of applications that process large volumes of incoming messages or events.
While discrete events when looked one by one might be meaningless, event streams (i.e. an infinite set of
events) considered over a sliding window and further correlated, are highly meaningful, and reacting to them
with the minimal latency is critical for effective action and competitive advantage.
Relational databases or message-based systems such as JMS make it really hard to deal with temporal data
and real-time queries. Indeed, databases require explicit querying to return meaningful data and are not suited
to push data as it changes. JMS systems are stateless and require the developer to implement the temporal
and aggregation logic himself. By contrast, the Esper engine provides a higher abstraction and intelligence
and can be thought of as a database turned upside-down: instead of storing the data and running queries
against stored data, Esper allows applications to store queries and run the data through. Response from the
Esper engine is real-time when conditions occur that match user defined queries. The execution model is thus
continuous rather than only when a query is submitted.
In Esper, a tailored EPL allows registering queries in the engine. A listener class, which is basically a POJO,
will then be called by the engine when the EPL condition is matched as events flow in. The EPL enables to
1
http://www.espertech.com/esper/
2
Most of this section has been reproduced from the Esper website
102
CONFIDENTIAL Event representations
express complex matching conditions that include temporal windows, joining of different event streams, as
well as filtering, aggregation, and sorting. Esper statements can also be combined together with "followed by"
conditions thus deriving complex events from more simple events. Events can be represented as JavaBean
classes, legacy Java classes, XML document or java.util.Map, which promotes reuse of existing systems
acting as messages publishers.
A trivial yet meaningful example is as follow: assume a trader wants to buy Google stock as soon as the price
goes below some floor value, not when looking at each tick but when the computation is done over a sliding
time window, say of 30 seconds. Given a TickVO event bean with a last price field and a reference to a Security
ID and the following EPL, a listener POJO would get notified as ticks come in to trigger the buy order:
select
avg(last)
from
TickVO.win:time(30 sec)
where
securityId=12
In addition to Java classes, Maps and XML are an alternative way of representing events.
EPL is similar to SQL in it's use of the select clause and the where clause. However EPL statements instead
of tables use event streams and a concept called views. Similar to tables in an SQL statement, views define
the data available for querying and filtering. Views can represent windows over a stream of events. Views can
also sort events, derive statistics from event properties, group events or handle unique event property values.
This is a sample EPL statement that computes the average of the last price for the last 30 seconds of Tick
events:
select
avg(last)
from
TickVO.win:time(30 sec)
103
CONFIDENTIAL
Combining Pattern Matching with Event Stream Analysis
A sample EPL that returns the average of the last price per symbol for the last 100 Ticks.
select
securityId
avg(last) as averagePrice
from
TickVO.win:length(100)
group by
securityId
This example joins 2 event streams. The first event stream consists of Bar events for which we keep the last
30 minutes (1800 seconds). The second stream is Tick events for which we consider the last 30 seconds. The
streams are joined on securityId.
select
bar.securityId as securityId,
max(bar.high) as maxHigh,
min(bar.low) as minLow,
last(tick.last) as lastPrice
from
BarVO.win:time(30 min) as bar,
TickVO.win:time(30 sec) as tick
where
bar.securityId = tick.securityId
Patterns match when a sequence (or absence) of events is detected. Pattern match results are available for
further analysis and processing.
The pattern below detects a situation where an OrderStatus event is not followed by another OrderStatus
event corresponding to the same internal order id within 10 seconds. The statement further counts all such
occurrences grouped per internal order id.
select
a.intId,
count(*)
from
pattern [every a=OrderStatus
-> (timer:interval(10 sec) and not OrderStatus(intId=a.intId)]
group by
id
104
CONFIDENTIAL Named windows
create window
SecurityWindow
as
(symbol String, triggerPrice double)
One can trigger a select, update or delete when an event arrives. Here we show a select that simply counts
the number of rows:
on
TriggerEvent
select
count(*)
from
SecurityWindow
8.1.6. Variables
A variable is a scalar, object or event value that is available for use in all statements including patterns. Variables
can be used in an expression anywhere in EPL.
AlgoTrader provides a number of Value Objects that can be used as Esper Events (e.g. TickVO, BarVO,
OrderStatusVO, etc.)
3
Most of this section has been reproduced from the Esper website.
105
CONFIDENTIAL Creating a Statement
A statement is a continuous query registered with an Esper engine instance that provides
results to listeners as new data arrives, in real-time, or on demand via the iterator API ( see
ch.algotrader.esper.Engine.executeQuery ).
The next code snippet shows an Esper module containing a continuous query. The query returns the average
price over all TickVO events that arrived in the last 30 seconds:
select
avg(price) as avgPrice
from
TickVO.win:time(30 sec)
Each of the Esper engines inside AlgoTrader can contain several modules. Modules specified through the
initModules and runModules attribute of the Esper Engine Spring Bean Definition are loaded automatically
on start-up.
Note
A module definition of market-data will look for a module file called module-market-data.epl.
A subscriber object is a direct binding of query results to a Java object. The object, a POJO, receives statement
results via method invocation. The subscriber class does not need to implement an interface or extend a
superclass. Only one subscriber object may be set for a statement.
Subscriber objects have several advantages over listeners. First, they offer a substantial performance benefit:
Query results are delivered directly to the Java method(s) through Java virtual machine method calls, and there
is no intermediate representation (EventBean). Second, as subscribers receive strongly-typed parameters, the
subscriber code tends to be simpler.
106
CONFIDENTIAL Adding a Listener
The subscriber class must provide a public method to receive events. The number and types of parameters
declared by the update method must match the number and types of columns as specified in the select clause,
in the same order as in the select clause.
@Subscriber(className='ch.algotrader.listener.MySubscriber#process)
select
orderId, price, count(*)
from
OrderEvent;
Listeners are invoked by the engine in response to one or more events that change a statement's result set.
Listeners implement the UpdateListener interface and act on EventBean instances as the next code snippet
outlines:
By attaching the listener to the statement via the following annotation the engine provides the statement's
results to the listener:
@Listeners(classNames={'ch.algotrader.listener.MyListener'})
select
avg(price) as avgPrice
from
TickVO.win:time(30 sec)
107
CONFIDENTIAL Sending events
Incoming market data events (e.g. Ticks), submitted Orders, OrderStatus events, received Fills, etc are
automatically sent into the corresponding Esper engines.
Additionally custom events can be sent into an Esper engine. The following code snipped creates an arbitrary
event and sends it into the Esper engine instead an AlgoTrader strategy named EXAMPLE.
engine.sendEvent(event);
8.2.6. Configuration
Esper Configuration helps make statements more readable and provides the opportunity to plug-in extensions.
Each Esper Engine loads the default esper-common.cfg.xml file. In addition strategies load all Esper
configuration files named esper-xxx.cfg.xml in the class path. This configuration file defines settings like:
• Event Types
• Variables
The following chapters of the Esper Documentation are relevant for developing trading strategies with
AlgoTrader based on Esper:
5
• 2. Event Representations
6
• 3. Processing Model
7
• 5. EPL Reference: Clauses
4
http://esper.espertech.com/release-5.5.0/esper-reference/html/index.html
5
http://esper.espertech.com/release-5.5.0/esper-reference/html/event_representation.html
6
http://esper.espertech.com/release-5.5.0/esper-reference/html/processingmodel.html
7
http://esper.espertech.com/release-5.5.0/esper-reference/html/epl_clauses.html
108
CONFIDENTIAL AlgoTrader specific Esper Artifacts
8
• 6. EPL Reference: Patterns
9
• 8. EPL Reference: Operators
10
• 9. EPL Reference: Functions
11
• 12. EPL Reference: Views
12
In addition Esper Examples, Tutorials, Case Studies are available.
8.4.1.1. Engine
The Engine interface has methods available for the following tasks:
• sending events
• synchronized processing (coordination) of events from different sources into the Esper engine
For testing purposes there is an abstract do-nothing implementation of the Engine interface available named
AbstractEngine.
8.4.1.2. EngineManager
The EngineManager interface represents the main entry point to different Engines running inside the JVM. The
EngineManager has methods available for the following tasks:
8
http://esper.espertech.com/release-5.5.0/esper-reference/html/event_patterns.html
9
http://esper.espertech.com/release-5.5.0/esper-reference/html/epl-operator.html
10
http://esper.espertech.com/release-5.5.0/esper-reference/html/functionreference.html
11
http://esper.espertech.com/release-5.5.0/esper-reference/html/epl-views.html
12
http://esper.espertech.com/release-5.5.0/esper-reference/html/examples.html
109
CONFIDENTIAL Modules
Note
Engine instances are managed and configured through Spring configuration. Engines are
standard Spring managed beans that get automatically registered with EngineManager upon
startup.
8.4.2. Modules
Tag Description
module-algo-xxx.epl Each Execution Algo has its own module
module-combination.epl Combination / Component related functionality (see Chapter 24,
Synthetic Securities and Derivative Spreads)
module-current-values.epl Store current market data values
module-cng.epl Coinigy specific statements
module-ib.epl IB specific statements
module-market-data.epl Statements related to market data
module-metrics.epl Statements needed for Engine Metrics
module-performance.epl Evaluation of performance metrics
module-portfolio.epl Portfolio management functions
module-prepared.epl Prepared Statements available to strategies
module-server-prepared.epl Server Side prepared Statements
module-trades.epl Statements related to orders and executions
Note
init and run modules of the AlgoTrader Server can be defined through config properties in
conf-core.properties.
110
CONFIDENTIAL Tags
• Creation of technical indicators (e.g. Moving Average, Bollinger Bands, ATR, etc.)
• Trend evaluation
• Pattern recognition
Note
It is generally recommended to use Esper statements for anything up to signal generation but
then use Java for execution of actions (e.g. send and order, set a stop or close a position)
8.4.3. Tags
13
In addition to the Esper standard tags the following tags are available:
Tag Description
@Condition(key='xxx') Statement is only deployed if defined configuration parameter is set
to "true"
@SimulationOnly Statement is only deployed in simulation
@RunTimeOnly() Statement is only deployed in Live-Trading mode
@Listeners(classNames={'...'}) attaches one or multiple listeners to the statement
@Subscriber(className='...') attaches a subscriber to the statement
8.4.4. Subscribers
The system provides the following Subscribers out-of-the-box:
IndicatorSubscriber
Prints all values as a comma-separated-list (CSV) to the file files/report/IndicatorReport.csv
(Headers are not available).
13
http://esper.espertech.com/release-5.5.0/esper-reference/html/epl_clauses.html#epl-syntax-annotation
111
CONFIDENTIAL Listeners
TestSubscriber
Prints all values to the Log by using the toString method of the event object.
VoidSubscriber
Do-nothing subscriber, useful when select clauses call static methods
ExceptionSubscriber
Prints a value as an Error to the Log.
Any public method of a component defined in the Spring application context can potentially be used as a
subscriber provided the parameter signature of the method can be supported by Esper: One can define a
subscriber by specifying a Spring bean name followed by hash (#) followed by a method name exposed by
this bean.
@Name('TAKE_PROFIT')
@Subscriber(className='boxService#takeProfit')
The syntax also support property placeholder expansion. This can especially useful when using multiple
instances of the same strategy in the same JVM process.
@Name('TAKE_PROFIT')
@Subscriber(className='${strategyName}Service#takeProfit')
8.4.5. Listeners
The system provides the following Listeners out-of-the-box:
IndicatorListener
Prints all values as a comma-separated-list (CSV) to the file files/report/IndicatorReport.csv.
Headers will be extracted from the supplied Statement.
RendererListener:
Prints all values to the Log in XML format.
TestListener
Prints all values to the Log by using the toString method of the event object.
StatementAwareTestListener
Prints all values including the statement name to the Log by using the toString method of the event object.
By default AlgoTrader exposes the following services to Esper statements, which strategies can make use of:
112
CONFIDENTIAL Aggregation Functions
• LookupService
• PortfolioService
• CalendarService
• OrderService
• PositionService
• MarketDataService
• OptionService
Here are some examples. To retrieve a security based on its securityId one can use the following statement:
select
bar,
security
from
BarVO as bar unidirectional,
method:lookupService.getSecurity(bar.securityId) as security;
To retrieve an open order by its intId on can use the following statement:
select
fill,
openOrder
from
FillVO as fill unidirectional,
method:orderService.getOrderByIntId(fill.orderIntId) as openOrder;
8.4.7.1. ExponentialMovingAverage
113
CONFIDENTIAL Aggregation Functions
This will create a 10-period exponential moving average of the Tick last price.
8.4.7.2. GenericTALibFunction
14
The GenericTALibFunction is a portation of ta-lib to AlgoTrader. It supports all TA-Lib operations.
15
Please consult TA-Lib for a list of all TA-Lib methods and their parameters.
If the TA-Lib Function returns just one value, the value is directly exposed by the AggregationFunction.
Example: The TA-Lib function movingAverage has just one double typed return value which can be accessed
directly.
select result
from MovingAverage;
If the TA-Lib Function returns multiple-values, a dynamic class will be generated on the fly, which gives access
to properly typed return-values. All return value names are lower-case!
Example: The TA-Lib function stochF has return values: outFastK and outFastD. The returned dynamic class
will have double typed properties by the name of: fastk and fastd (all lowercase).
Some functions are influenced by the entire range of the past data. These functions are sometimes called
functions with memory. An example is the EMA (Exponential Moving Average). For these functions an optional
unstable period parameter can be specified. The following statement will create a 30 period moving average
with an unstable period of 10.
14
http://ta-lib.org/
15
http://doc.algotrader.ch/ta-lib.html
114
CONFIDENTIAL Callbacks
16
For further details about the unstable period please see: SetUnstablePeriod
17
For additional information please visit the corresponding JavaDoc .
Note
As an alternative to the ta-lib based exponential moving average function the Esper aggregation
function Section 8.4.7.1, “ExponentialMovingAverage” can be used which keeps the entire
history an not just the unstable period.
8.4.8. Callbacks
In order to correctly associate the trade callback with a specific order an orderId has to be retrieved from the
order service and set onto the order before attaching the trade callback.
16
http://ta-lib.org/d_api/ta_setunstableperiod.html
17
http://doc.algotrader.ch/javadoc/ch/algotrader/esper/aggregation/GenericTALibFunction.html
115
CONFIDENTIAL Callbacks
order.setIntId(orderId);
getOrderService().sendOrders(orders);
The Engine#addFullExecutionCallback() method can be used to register a callback logging an error if the
order did not execute fully.
order.setIntId(orderId);
engine.addFullExecutionCallback(Collections.singleton(orderId));
getOrderService().sendOrders(orders);
Note
With Fix based Broker connections the TradeCallback only works with the initial order but not
with any subsequent order modifications.
This callback is particularly useful for situations where one needs to have a guarantee that all order related
database transactions have been fully executed before continuing with next steps, e.g. to retrieve the current
position quantity after an order has been executed. If using a regular trade callback for this, the Position Entity
might not have been fully persisted by the time the consumer is executed. However when using the trade
persisted callback it is guaranteed that the Position Entity has been fully updated in the database.
In order to correctly associate the trade callback with a specific order an orderId has to be retrieved from the
order service and set onto the order before attaching the trade callback.
116
CONFIDENTIAL Callbacks
order.setIntId(orderId);
getOrderService().sendOrders(orders);
Note
The Trade persisted callback is only supported in runtime mode but it is not supported in
simulation mode. It is recommended to use the Section 8.4.8.2, “Trade callback” instead in
simulation mode. The Trade persisted callback will only get executed if there has been at least
one (partial) execution but not if an order has been cancelled or rejected before there has been
any execution.
Note
It is guaranteed that the position is fully persistent to the database by the time the consumer
is called.
117
CONFIDENTIAL Esper Threading
In Live-Trading Mode AlgoTrader uses outbound threading with 3 threads by default. This means that all
Subscriber / Listener Tasks are handled by a thread-pool of 3 threads. The number of outbound threads can
be changed inside conf.properites or via Section 2.3, “VM Arguments”:
# number of Esper outbound threads to be used in Live Trading Mode for the Server
Engine
misc.outboundServerEngineThreads = 3
# number of Esper outbound threads to be used in Live Trading Mode for the Strategy
Engines
misc.outboundStrategyEngineThreads = 3
For debugging reasons AlgoTrader logs the name of the thread using log4j, see Chapter 29, Logging
18
http://esper.espertech.com/release-5.5.0/esper-reference/html/api.html#api-threading
118
Chapter 9. CONFIDENTIAL
Database
1 2
AlgoTrader uses MySQL to store transaction data. In addition an embedded in-memory H2 database can
be used for simulations.
3
AlgoTrader uses the database migration library flyway to keep databases in-line with the current version of
AlgoTrader
• flyway/sql contains the flyway migration scripts that will be executed to initialize and update the MySQL
database
With the embedded database H2 all H2 scripts that are available in the class path under the following wild card
pathname will be loaded by the system: h2/h2-*.sql and /db/h2/h2-*.sql.
-Dspring.profiles.active=<dataSource>
4
• pooledDataSource: A Data Source that uses connection pooling based on C3P0
• singleDataSource: A Data Source that uses one single database connection and no connection pooling.
5
• embeddedDataSource: An H2 based in-memory / in-process Data Source that is ideal for simulation of
strategies. Using the embeddedDataSource reduces the duration of back-test runs by 30%-50% and allows
multiple parallel simulations on the same machine without needing to installing a physical database.
1
https://www.mysql.com/
2
http://www.h2database.com/
3
https://flywaydb.org/
4
https://www.mchange.com/projects/c3p0/
5
http://www.h2database.com/
119
Chapter 10. CONFIDENTIAL
Client
AlgoTrader provides different types of clients all of which are targeting a different audience and use case:
• Eclipse IDE: Contains the Strategy Creation Wizard, the Config Editor as well as the strategy simulation
environment
10.1. HTML5
The AlgoTrader HTML 5 provides the following features.
• All tables provide (multicolumn) sorting, filtering, column selection and reordering, and scrolling.
• Since the frontend is based on HTML 5, it can easily be integrated into corporate IT infrastructures using
firewalls, VPNs, and remote locations.
To manually open the client, point the browser to one of the following URL.
http://localhost:9090
10.1.1. Header
Using the strategy selection menu located at the top of the screen (to the right), one can select either an
aggregated view over the entire system or a strategy specific view.
When a single strategy is selected the client will show orders, transactions, positions and market data
subscriptions related to the selected strategy only. When ALL is selected the client will show orders,
transactions, positions and market data subscriptions for all strategies.
120
CONFIDENTIAL Header
The header tiles show general figures (like Net Liquidity, Unrealized P&L, Cash, etc.). The default
valuation currency is USD. You can change it by updating the misc.portfolioBaseCurrency value in the
conf.properties file.
If one opens a menu on top right corner (hamburger menu) one can see Settings link which opens the settings
form. The following settings are available there:
2. Order defaults: default order related values like default quantity and default time-in-force
3. Tables update throttling in ms - sets the update interval of all tables, e.g. if the interval is 333ms, the tables
will buffer all data updates and only make display changes every 333ms. Increasing that parameter may
help if the UI is displaying a lot of data (>100 rows) and becomes unresponsive, e.g. reacts slowly to clicking
on buttons, sorting columns etc.
4. Use Trading View historical data - when checked means that the historical data for chart will be coming
from TradingView's own data source, if unchecked the chart will take historical data from data source the
Algotrader back end is configured with
121
CONFIDENTIAL Header
To open the general management form, click on the management menu on the top of the screen (to the left).
Notifications are displayed in the lower left hand side of the screen
In case of an Alert an icon will appear at the top right of the screen.
122
CONFIDENTIAL Order Table
To open the list of all Alerts click on the bell icon. Alerts can be removed from the list by clicking on the close icon
Manual orders can be entered using the order entry form. After entering the order (by specifying security, trade
side, order type, quantity, strategy and account), click the Submit button.
To specify additional manual order parameters (e.g. time in force and exchange), click the Details button
and adjust the order .
123
CONFIDENTIAL Transaction Table
To cancel all open orders, click the Cancel All button at the top right of the Order Table.
To cancel a specific open order, click the Cancel icon besides the order.
To modify an open order, click the Modify icon besides the order.
The executed trades are listed in the Transactions Table. Since strategies can produce a lot of transactions, only
the most recent 50 are shown on startup. All transactions can of course be seen/exported from the database.
124
CONFIDENTIAL Positions Table
To manually add a transaction, click the Add button at the top right of the Transaction Table.
125
CONFIDENTIAL Market Data Table
To close all positions, click the Close All button at the top right of the Position Table.
To close a specific position, click the Close icon besides the position.
To reduce a position by a certain quantity, click the Reduce icon besides the position.
To open a position's security chart, click the Chart icon besides the position.
Note
Closing and Reducing a position through this table requires the definition of an
order_preference with the name DEFAULT. Fur further details see Section 17.1.1, “Order
Preferences”
The default order preference also includes an account, which means this feature in the positions
table is only usable with one account/adapter. If you are using more than one, you have to use
the order entry in the order table to close positions.
126
CONFIDENTIAL Column Selection and Grouping
To subscribe for a security's market data, click the Subscribe button at the top right of the Market Data Table.
To unsubscribe to a security's market data, click the Unsubscribe icon besides the security.
For any given strategy, it is only possible to unsubscribe from securities the strategy has no position. The
unsubscribe icon will only be enabled if the corresponding strategy has no position for the given security.
The security's chart can be opened by clicking the Chart icon besides the security.
All Tables have a configuration button at the top right of the table. Click the settings button to select which
columns to show. See the following example for the Transaction Table.
To see all columns of a certain type (e.g. Transaction or Strategy) please click on the expand button.
One can also automatically adjust columns' width by clicking on Auto-size columns button in Table actions
section.
127
CONFIDENTIAL Column Selection and Grouping
In every table, any column can be sorted (ascending or descending) by clicking on the column header.
A filter can be applied to any column by clicking at the far right part of the column header. The following image
shows the transaction table filtered by a specific symbol.
128
CONFIDENTIAL CSV Export
All Tables have a Export CSV button at the top right of the table. Clicking the button will initiate the export
of all the data visible in given table into a csv file. Exported files will get unique names with table name and
the export date.
The AlgoTrader frontend also comes with the interactive TradingView chart library.
TradingView has regular and advanced chart types and it comes with a massive library of over 100 pre-built
technical indicators covering the most popular trading concepts.
The chart widget is useful during strategy development (for initial idea generation, validation, etc.) and for
monitoring.
Note
TradingView widget can work in two modes. It can either use data provided by TradingView
itself, or it can use custom market data adapters which are configured in AlgoTrader.
In case of TradingView market data source there might be slight differences between market
data shown in the market data table and the chart widget.
1
Please, see the TradingView documentation .
To switch market data source between TradingView and custom ones, open settings panel.
1
https://www.tradingview.com/wiki/Main_Page
129
CONFIDENTIAL Chart Widget
To select particular security to be displayed in the chart, click on the chart icon in the operations column in
Market Data or Positions grids.
130
CONFIDENTIAL Technologies
10.1.9. Technologies
The HTML5 client is based on the following technologies/architectures:
• HTML5
2
• React
3 4
• STOMP over WebSockets
2
https://reactjs.org/
3
https://stomp.github.io/
4
http://www.websocket.org/
5
http://getbootstrap.com/
131
CONFIDENTIAL HTML5 Custom Widgets
6
• TradingView chart component
7
• AGGrid component
It is possible to extend the HTML5 UI with custom widgets in order to visualize strategy specific data or let the
user interact with strategy specific functionality (e.g. modify parameters or change the state of a strategy). the
following screen shot shows an example of the custom widget in use by the Appendix B, Example Strategy
"Box":
HTML5 custom widgets use WebSockets over STOMP to communicate with the strategy.
To integrate the HTML5 custom widget into the main HTML5 front end the following items need to be created
inside the strategy. The examples are based on the Appendix B, Example Strategy "Box".
6
https://www.tradingview.com/
7
https://www.ag-grid.com/
132
CONFIDENTIAL HTML5 Custom Widgets
/src/main/resources/html5/index.html
This file contains the layout of the main HTML5 screen including the HTML5 custom widget. The custom
widget needs to be included similar to the following code snippet:
<head>
<meta charset="utf-8">
<title>AlgoTrader</title>
<script src="/charting_library/charting_library.min.js"></script>
<script src="https://s3.tradingview.com/tv.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></
script>
</head>
...
Please not that the total of all columns and the total of all rows needs to equal 12 (e.g. col-xs-5, col-xs-5
and col-xs-2).
As the code snippet shows it is necessary to also include the jquery and stomp JavaScript libraries.
/src/main/resources/html5/box.html
This file contains the HTML code for the custom widget. Individual tags will be referenced by JavaScript
code through tag ids.
<h3>Box Strategy</h3>
<table class="table table-striped ">
<thead>
<tr>
<th>Name</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<td>State</td>
<td id="box_state"></td>
</tr>
133
CONFIDENTIAL HTML5 Custom Widgets
</tbody>
</table>
<button id="box_terminate" class="btn btn-danger btn-xs" role="button">
TERMINATE TRADE
</button>
/src/main/resources/html5/box.js
This file contains the JavaScript code for the custom widget:
$.get("box.html", function(result){
$("#box").html(result);
stompClient.subscribe('/topic/strategy.box.metrics', function(message){
var metrics = JSON.parse(message.body));
$("#box_state").text(metrics.state);
}, { "activemq.retroactive" : true});
$('#box_terminate').on('click', function(event) {
stompClient.send("strategy.box.terminate", {})
});
});
});
});
134
CONFIDENTIAL AlgoTrader Eclipse IDE
/src/main/java/ch/algotrader/strategy/box/BoxService.java
The strategy service class is responsible for sending events to the custom widget and for processing
incoming events from the custom widget.
Strategy service classes can send events to the custom widget by using the JsonTemplate available inside
the strategy service. The following code snippet will send a box event to the topic strategy.box.metrics:
getJsonTemplate().convertAndSend("strategy.box.metrics", box);
Strategy service class methods can be annotated with the JmsListener annotation in order to receive
incoming events from the custom widget. The following code snippet will attache the terminateSeries
method to the topic strategy.box.terminate:
@JmsListener(destination = "strategy.box.terminate")
public void terminateSeries() {
...
}
To see the full source code of above examples please see the corresponding source code of the Appendix B,
Example Strategy "Box". Additional HTML5 custom widgets are available inside the example strategies
Appendix D, Example Strategy "IPO" and Appendix C, Example Strategy "Pairs Trading".
See section Section 2.1, “Development Environment Installation” for instructions on how to install the
AlgoTrader Eclipse IDE.
Quantitative users can use this perspective to modify configurations of the system and trading strategies and
start the system with different configurations. Code artifacts (java classes, config files etc.) are not visible in
this perspective. However all log-files and reports are shown.
The perspective shows AlgoTrader projects only, i.e. projects that have the AlgoTrader nature
(ch.algotrader.quant.ui.algotradernature) defined inside the .project file:
135
CONFIDENTIAL Strategy Wizard
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>ch.algotrader.quant.ui.algotradernature</nature>
</natures>
</projectDescription>
• By right clicking on an AlgoTrader project the AlgoTrader Configuration Editor can be opened. This action
is available under the AlgoTrader/Configuration menu item.
• By right clicking on an AlgoTrader project an AlgoTrader project can be duplicated. This action is available
under the AlgoTrader/Duplicate menu item.
• By right clicking on an AlgoTrader a back test of the strategy can be started. This action can be performed
by clicking the AlgoTrader/Run Strategy menu item. This context-menu item is only enabled for strategies
that do not contain strategy groups.
Note
There is no new operation. Quantitative users of the system can duplicate projects but they
cannot create new ones. Creation of new projects needs to be done by developers through the
regular Java perspectives.
136
CONFIDENTIAL AlgoTrader Configuration Editor
To open the AlgoTrader Configuration Editor, right click on an AlgoTrader project, then click the menu item
AlgoTrader/Configuration. The AlgoTrader/Configuration menu will only be available if a file with the name
applicationContext-client-xxx.xml exists in the project class path. If that file does not exits, the menu
item will be disabled and the editor cannot be opened.
Furthermore, the AlgoTrader Configuration Editor behaves differently depending on the content of the
applicationContext-client-xxx.xml file. If the configuration file contains strategy group definitions according to
Section 4.5, “Strategy Groups” the editor will show three tabs.
137
CONFIDENTIAL AlgoTrader Configuration Editor
The image above shows a strategy group configuration file opened in the AlgoTrader Configuration Editor.
On the left hand side of the Strategy Group Tab all available configurations are list and grouped by their Config
Class (e.g. Box for ch.algotrader.strategy.box.BoxConfig).
The buttons "New" and "Edit" (1) can be used to add and modify individual configuration items.
The "New" and "Edit" dialog contains the following elements (for details visit: Section 4.5, “Strategy Groups”):
• Config Class: the name of the ConfigBean linked to the .properties file
In addition it is possible to rename, duplicate and delete individual items (2). When selecting a configuration
the corresponding .properties file will open in the lower part of the Strategy Group Tab (4)
On the right hand side of the Strategy Group Tab individual configurations can be added (5) to different strategy
groups where they can be back tested in parallel. Items from the left hand side can be added to multiple groups
138
CONFIDENTIAL AlgoTrader Configuration Editor
on the right hand side. When adding an item to a strategy group an allocation weight has to be assigned. When
adding multiple items to a strategy group the total weight of the strategy group must be 100%.
The right hand side of the Strategy Group Tab contains buttons to "Create", "Edit", "Rename" and "Delete"
strategy groups (3). The button "Run" will start a backtest of the selected strategy group (see Section 10.2.3.4,
“Starting Back Tests”)
Both lists in the Strategy Group Tab have moving (6), collapsing/expanding (7), sorting (8) and full text filtering
(9) of items. Also there is a function to filter by selected strategy item (10). In addition it is possible to change
the orientation between horizontal and vertical (11).
When a property file is selected, the content of that file is shown in a table viewer. The table viewer has two
columns: key and value.
The AlgoTrader Configuration Editor supports in-place editing of cells under value column.
In addition to the standard key=value pairs, the AlgoTrader Configuration Editor interprets special comments.
These comments provide the AlgoTrader Configuration Editor with the information needed to display the
key=value data (i.e. the type of data, the widget class it should use for an in-place editor etc.).
Example:
139
CONFIDENTIAL AlgoTrader Configuration Editor
#{"type":"String","label":"Last name:"}
lastName = Mustermann
#{"type":"String","label":"First name:"}
surName = Joe
#{"type":"Date","required":"false","label":"Date of birth:"}
dateOfBirth = 1980-01-01
The AlgoTrader Configuration Editor remembers association of each key=value pair with it's special comment.
When the editor saves properties back to the .properties, all key=value pairs are written with their special
comments.
Each special comment is essentially a JSON object with three attributes: type, required and label. All three
attributes are optional.
• type attribute is string and describes the data type of the key=value pair. It defaults to "String". Each data
type is implicitly (via separate configuration) mapped to the widget class, which is used for in-place editing.
• label attribute is string and describes text label for representing the key=value pair in the table viewer. It
defaults to key
• required attribute is boolean (true|false) and describes whether the key=value pair requires a non-empty
value. It defaults to true.
The AlgoTrader Configuration Editor also supports separators/subtitles. To add a separator, a special comment
needs to be defined in the Source tab as follows:
#{"type":"String","label":"Strategy Name"}
strategyName=BOX
#{"type":"Separator"}
#{"type":"Integer","label":"Maximum Units"}
maxUnits=16
140
CONFIDENTIAL AlgoTrader Configuration Editor
terminateHour=22
The content of this properties file will be rendered in the Properties tab as follows:
The AlgoTrader Configuration Editor supports the following data types and widget mappings out-of-the-box:
141
CONFIDENTIAL AlgoTrader Configuration Editor
package ch.algotrader;
A new property type for this enum type can be defined as follows:
<extension point="ch.algotrader.config-editor.PropertyDef">
<PropertyDef id="Color"
dataType="somepackage.Color"
cellEditorFactory="ch.algotrader.configeditor.editingsupport.EnumCellEditorFactory">
</PropertyDef>
</extension>
When such a property type is defined in "plugin.xml" of some plugin and when this plugin is packaged/installed
together with the program, the program can read, edit and save new property type in .properties file:
#{"type":"Color"}
backgroundColor=BLUE
Note
the plugin containing PropertyDef extension must have the following line in "MANIFEST.MF":
Require-Bundle: ch.algotrader.config-editor
otherwise the AlgoTrader Config Editor will not be able to access the class designated by
dataType attribute and will throw ClassNotFoundException.
The AlgoTrader Configuration Editor implements RegEx based input validation (one expression per data type).
When user input does not conform to the specified regular expression, the AlgoTrader Configuration Editor
shows explanatory error message and does not allow to save the file(s).
142
CONFIDENTIAL AlgoTrader Configuration Editor
The AlgoTrader Configuration Editor provides an extension point, which allows the definition of new property
types:
<extension point="ch.algotrader.config-editor.PropertyDef">
<PropertyDef id="Email"
dataType="java.lang.String"
regex="^.+@.+\.[a-z]{2,4}$"
regexErrorMessage="The input {0} is not a valid e-mail"
cellEditorFactory="ch.algotrader.configeditor.editingsupport.TextCellEditorFactory">
</PropertyDef>
</extension>
• id: string, required. Property type identifier, must match the attribute type in property comment
• dataType: fully qualified java class name, required. Internal type of de serialized property or implementation
of ch.algotrader.configeditor.IPropertySerializer interface.
• regexErrorMessage: string, optional. Error message shown to the user when input does not validate against
the specified Regex. When omitted, the default message is shown: "User input {0} does not satisfy pattern
{1}" where {0} is a placeholder for user input and {1} is a placeholder for the regular expression.
• cellEditorFactory: fully qualified java class name, required. Factory creating cell editor, must implement
ch.algotrader.configeditor.CellEditorFactory interface.
Furthermore, the AlgoTrader Configuration Editor provides a Source tab, which shows the content of the file
applicationContext-client-xxx.xml.
As described in the Section 10.2.1, “AlgoTrader Perspective” section, it is possible to start a back test of a
strategy by right clicking on the strategy and selecting AlgoTrader/Run Strategy (this option is only available
for strategies that are not using strategy groups).
Back Tests can also be started from within the AlgoTrader Configuration Editor:
• For strategies without strategy group definitions there is only one possible configuration that can be back
tested. To start a back test please click the "Run" button at the top of the Properties tab.
143
CONFIDENTIAL Esper Colorer
• For strategies with strategy group definitions multiple configurations exist which can be back tested
separately. To start a back test please select the desired strategy group on the right hand side of the Strategy
Group Tab and click the "Run" button.
• Pairs/Brace Matching
• Reserved Keywords
• Symbols
• Comments
• Literals
• Numbers
Note
The Syntax Highlighter does not provide Code Completion or Syntax Checking!
8
http://colorer.sourceforge.net/
9
http://colorer.sourceforge.net/eclipsecolorer/index.html
144
Chapter 11. CONFIDENTIAL
Performance Measurement
AlgoTrader provides a sophisticated Performance Measurement functionality that consists of the following
components:
• Back test report and performance statistics display at the end of a simulation run (see Section 5.5,
“Performance Statistics”)
All Performance Measurement features depend mainly on the Entity Portfolio Value which has the following
attributes
• dateTime
• netLiqValue
• marketValue
• realizedPL
• unrealizedPL
• cashBalance
• openPositions
• leverage
• cashFlow (optional)
In Simulation Mode Portfolio Values are recorded to the file PortfolioReport.csv through the
PortfolioReport class on a daily-basis using the AlgoTrader Reporting Functionality, see: Chapter 30,
Reporting.
145
CONFIDENTIAL Portfolio Value Restoration Feature
Reconciliation where a Cash Transaction has to be recorded for the last trading day as soon as the external
reconciliation file arrives.
Note
The Portfolio Value restoration can take a considerable amount of time to complete.
146
Chapter 12. CONFIDENTIAL
Risk Management
Using the provided functionality of the system, the following risk metrics can be enforced by AlgoTrader based
strategies:
• Minimum Cash-Balance
• etc.
In addition client specific pre-trade checks can be enforced by AlgoTrader based strategies :
• etc.
Note
Enforcing these rules requires coding and is not available through configuration only.
147
Chapter 13. CONFIDENTIAL
Forex Handling
The System provides full Forex and Currency Exchange Management. FX Rates can be retrieved in real-time.
All portfolio figures are calculated based on up-to-date FX-Rates.
FX conversion is provided through the MarketDataService. When subscribing for a non-base currency
instrument the system will automatically subscribe the Forex instrument necessary to convert the non-base
currency balance (e.g. realized PnL and market value) into the base currency. In some cases multiple Forex /
Currency Pairs (based on the same base and transaction currency) are available in the system which are
traded through different brokers / exchanges.
For those cases a special setting misc.defaultMarketFeeds defines the priority in which market feeds and
exchanges are used. The default value is the following:
misc.defaultMarketFeeds =
BMEX:CNG,OKEX:CNG,BINA:CNG,BITF:CNG,BTHM:CNG,HITB:CNG,GDAX:CNG,
BITMEX:CNP,OKEX:CNP,BINANCE:CNP,HUOBI:CNP,BITFINEX:CNP,BITHUMB:CNP,HITBTC:CNP,KRAKEN:CNP,
BINA:BNC,BITF:BFX,BITS:BTS,FLYR:BFL,BMEX:BMX,IDEALPRO:IB
This is a comma separated list of preferred <exchange>:<feedType>. Exchange code is taken from
the database security.exchange_code value and feed type is ch.algotrader.enumeration.FeedType.
Optional specific security pairs are also supported, using '@' symbol, e.g. 'BTCUSD@BITF:CNG'. When the
platform needs to make a conversion for a given forex, it will iterate over this list and determine the first match.
Match means all of the below:
• Current feed type is active, e.g. it's Spring profile is enabled (e.g. if cNGMarketData is active then 'CNG' will
be matched for Coinigy securities)
To change the default settings the following property needs to be updated inside conf.properties.
Alternatively the properties can be changed via Section 2.3, “VM Arguments”
148
CONFIDENTIAL Futures
13.1.1. Futures
• Futures are fully margined, that's why buying a Future does not actually influence cash but only the margin
requirement.
• AlgoTrader however treats Futures as regular Securities (i.e. add Future Market Value to System Market
Value and deduct Transaction Price from cash)
Note
• To compare AlgoTrader Balances to IB Balances (if there are Future Positions), one has to
compare the Net Liquidation Value and not Cash / Market Value individually
13.1.2. Forex
• Forex (e.g. EUR.USD) consists of the Base Currency (e.g. EUR) and Transaction Currency (e.g. USD)
• In Balances Forex are attributed towards Cash (and not system Market Value) in the Base Currency
• The gross value of a transaction is booked in the Transaction Currency, whereas the commission is booked
in the Base Currency.
Note
The TWS Trades Window displays commissions in Trade Currency, but IB Flex Reports displays
commissions correctly in the Base Currency.
149
CONFIDENTIAL Forex-Hedging
The following table describes Currency Attribution of Transactions. The logic is implemented by
Transaction.getAttributions()
13.2. Forex-Hedging
The system provides automatic Forex-Hedging by the Service ch.algotrader.service.ForexService. This
service will maintain multiple FX Positions to hedge all non base currency balances. For actual Forex-Hedging
the following two options exist
13.2.2. FX Future
The second option for FX Hedging is by means of FX Futures which is a subclass of Future.
For some FX future families there are multiple FX FutureFamilies available with different contract sizes (e.g.
12'500, 62'500 and 125'000 for EUR.USD). Because of this the hedgingFamily has to be defined as a Property
on the EUR.USD subscription.
Since FX Futures expire, the Hedging Position has to be rolled before Expiration, which is also done
automatically based on configured parameters.
150
Chapter 14. CONFIDENTIAL
In addition to the expiration date (sometimes also called lastTradingDay) Futures also have the following
two fields:
• firstNoticeDay: this is the day on which the buyer of a futures contract can be called upon by the exchange
to take delivery
Note
expiration and maturityMonthYear do not necessarily need to be within the same month.
e.g. maturityMonthYear: 2015-12, expiration: 2015-11-30
The following table contains references on how these three fields are used by different market data providers
For an detailed explanation between lastTradingDay and firstNoticeDay please visit this page on futures
1
expiration
Note
IB also shows an expiration date in TWS which is either the same day or the next day after
the last trading day
1
http://www.futurestradingpedia.com/futures_expiration.htm
151
CONFIDENTIAL Symbol, ISIN & RIC
CS
Contract Size
CV
Current Value
S
Underlying Spot price
D
Delta
Q
Quantity
Note
152
CONFIDENTIAL Option & Future Chain Download
Since some underlyings have more than one Futures Chain based on them, the property hedgingFamily needs
to be defined on the Subscription of the underlying. In addition the method hedgeDelta can also be invoked
with the ServerManagementService.
• implied volatility (through Black-and-Schooles, Newton Rapson Method and SABR Surface)
• intrinsic value
• delta
• vega
• theta
• forward price
• moneyness
• strike by delta
Based on historical Volatility at different Moneyness levels (e.g. ATM, ATM +10%, ATM +20%, ATM -10% &
ATM-20%) or Delta levels (e.g. 50%, 35%, 75%) volatility parameters are calculated (=calibration) and used
for option pricing.
SABR Calibration can be done either by actual Option Prices or directly by the Implied Volatility. Also there
are methods to do a SABR Calibration just for one expiration (returning one SABR Smile Value Object) or
153
CONFIDENTIAL Option Pricing
for an entire Volatility Surface (returning a SABRSurface ValueObject which consist of multiple SABRSmile
ValueObjects)
The SABR Calibration depends on different Implied Volatilities (a subclass of Security) being defined in the
database. A Implied Volatility needs to define either a moneyness or a delta (in addition to Duration and Option
Type).
1. For all available expirations a volatility is calculated for the requested strike
2. Using spline interpolation the volatility for the requested expiration is calculated
14.7.3. References
2
• Hedging under SABR Model, Refined risk management under the SABR model.
3
• A summary of the approaches to the SABR model for equity derivative smiles
2
http://www.lesniewski.us/papers/published/HedgingUnderSABRModel.pdf
3
http://www.riskworx.com/insights/sabr/sabr.html
154
Chapter 15. CONFIDENTIAL
Reconciliation
15.1. Partner Systems
Reconciliation is the automated process for comparing the status of the system with external partner systems
(e.g. executed trades, current positions, cash transactions).
Reconciliation can for example be based on files received via email or retrieved via FTP. For every partner
a sub class of ReconciliationService has to be created. This class has to contain at least the method
reconcile() which receives the file being processed. Depending on the file format used by the partner different
mechanisms for parsing the files are available:
1
• xml: Xalan (using XPath )
2
• csv: SuperCSV
The actual process of receiving files via email or FTP, storing them in a directory and passing them along to
3
the ReconcilicationServices is typically implemented through Spring Integration .
• ImapIdleChannelAdapter: receives email messages in an asynchronous fashion via IMAP Idle and sends
them to the InputChannel
• InputChain: a HandlerChain invoking the following components sequentially and sending the final
messages to the OutputChannel
• EmailDispatcher: adds directory and reconciliation service header based on defined Dispositions
• Filter: will only process email messages that contain above assigned directory header
• EmailTransfomer: Parses the email message and converts contained attachment into a List of
EmailFragments
• EmailSplitter: Splits messages with List of EmailFragments into individual EmailFragment messages
1
http://xml.apache.org/xalan-j/
2
http://super-csv.github.io/super-csv/
3
http://projects.spring.io/spring-integration/
155
CONFIDENTIAL FTP Handling
• FileOutputChannelAdapter: saves the actual file content of EmailFragments into the assigned directory
• ServiceActivator: invokes the assigned ReconcilationService with the actual file content
<bean id="sftpSessionFactory"
class="org.springframework.integration.sftp.session.DefaultSftpSessionFactory">
<int:poller
default="true"
fixed-rate="600000"
max-messages-per-poll="-1"/>
<int:channel id="receiveChannel"/>
<int-sftp:inbound-channel-adapter
channel="receiveChannel"
session-factory="sftpSessionFactory"
local-directory="file:files/sftp"
remote-directory="/transactions"
/>
<int:channel id="processChannel"/>
<int-file:outbound-gateway
request-channel="receiveChannel"
reply-channel="processChannel"
directory="files/reconciliation"
mode="REPLACE"
delete-source-files="true"/>
<int:service-activator
input-channel="processChannel"
expression="@reconciliationService.reconcile(payload)"/>
156
Chapter 16. CONFIDENTIAL
Broker/Exchange Interfaces
The System provides generic interface functions to connect AlgoTrader to different brokers / exchanges.
• Fix Interfaces
• InteractiveBrokers
• EzeSoft/RealTick
• Currenex
• JP Morgan
• SocGen
• UBS
• DukasCopy
• FXCM
• Fortex
• LMAX
• Nexus Prime
• PrimeXM
• Bitstamp
• Native Interfaces
• Interactive Brokers
• Binance
• Bitfinex
• Bitflyer
• BitMEX
• Coinigy
157
CONFIDENTIAL
1
AlgoTrader uses QuickFix/J for all fix interfaces.
For further details on Broker/Exchange adapters please see Chapter 22, Adapters
1
https://www.quickfixj.org/
158
Chapter 17. CONFIDENTIAL
Order Management
17.1. Place Order
Before sending an Order, it is advised to call the validate method on the order. This will validate the order
regarding limits, amount, quantity, etc. In case validation fails an Exception will be thrown and the order can
be modified.
Note
The validate method will be called (again) inside the sendOrder method, in case the validation
fails an Exception will be thrown.
The method sendOrder of the OrderService is responsible for placing Orders. This method takes an Order
Entity or Order Value Object as parameter.
getOrderService().sendOrder(order);
The associated Entities (i.e. strategy, account and security) can be retrieved via the LookupService.
getOrderService().sendOrder(order);
Sending of AlgoTrader orders is currently only supported via Order Entities. Creating and sending an
AlgoOrder looks like this:
159
CONFIDENTIAL Order Preferences
getOrderService().sendOrder(order);
The broker / exchange specific SimpleOrderExecService will create the broker / exchange specific order,
assign an intId if none has been assigned yet and send the order to the broker / exchange.
After sending the Order to the broker / exchange, the order object is propagated to the AlgoTrader Server
Esper service instance (running inside the AlgoTrader Server) as well as to the Esper service instance of the
corresponding strategy (where potential actions like cancel order or modify order can be executed).
Open orders are kept in an internal order book until their full execution or cancellation. Completed
orders remain in the book and are accessible through OrderService until evicted. Algotrader evicts
completed orders at 24:00 local time daily by default. One can also manually evict orders by calling
OrderService#evictExecutedOrders() method.
The actual exchange an Order is sent to will be retrieved from the associated Security/SecurityFamily.
Alternatively it is possible to assign an Exchange to an Order Entity or Order Value Object directly.
160
CONFIDENTIAL Trade Suggestions
(39,'maxVolPct',1,NULL,1,NULL,201,0),
(40,'maxQuantity',1,20,NULL,NULL,201,0),
(41,'minDuration',1,NULL,1,NULL,201,0),
(42,'maxDuration',1,NULL,2,NULL,201,0),
(43,'minDelay',1,NULL,2,NULL,201,0),
(44,'maxDelay',1,NULL,3,NULL,201,0);
The OrderPreference SLICING defines default settings for the Slicing Order. Through the column
DEFAULT_ACCOUNT_FK it is also possible to define a default account for the AlgoOrder. With this information
available in the database an order can now be created as follows:
getOrderService().sendOrder(order);
Order properties of type INTERNAL are kept inside the system and are not propagated to external brokers.
If the type of an Order property is FIX it is assigned to an outgoing Fix order as an additional fix tag. It is
therefore mandatory that the name of the order property is a number (representing the fix tag).
If the type of an Order property is IB the system will try to find an IB order field corresponding to the IB order
1 2
property name . In case no matching field is found the order property is added as an AlgoParams )
1
http://interactivebrokers.github.io/tws-api/classIBApi_1_1Order.html
2
http://interactivebrokers.github.io/tws-api/classIBApi_1_1Order.html
161
CONFIDENTIAL Receive Fills
Note
OrderProperties are only supported on Order Entities but not on Order value objects.
The Fill events trigger the creation of a Transaction object (a persistent Record in the database). In addition
the Fill and corresponding Transactions are also propagated to the strategy, where actions can be taken upon.
Note
Fills and Transactions are separated from each other for the following reason. A Fill contains all
the information received from the broker / exchange (and a reference to the Order). Whereas
a Transaction contains all the information related to accounting (i.e. references to position and
strategy). In addition to Transactions related to Fills, there are Transactions that are independent
of Fills (i.e. Deposits, Withdrawals, Interest, etc.).
Like Fills and Transactions the OrderStatus event will also be propagated to the corresponding strategy.
If an order does not receive either an Acknowledgment or Fill within a configurable time period (default: 1
sec) after sending the Order, an Exception is thrown, as there might be a problem with the broker / exchange
connection. This is enforced by the NOTIFY_MISSING_ORDER_REPLY statement which can be turned of by
changing the following property inside conf-core.properties. Alternatively the properties can be changed
via Section 2.3, “VM Arguments”
Once all fills corresponding to an Order are fully persisted an OrderCompletionVO event is generated and
propagated to the Strategy. By the time an OrderStatus event is received by the strategy, corresponding
162
CONFIDENTIAL Handling of Fees and Commissions
database activity might not have been completed yet. However by the time an OrderCompletionVO event is
received, it is guaranteed that all database activity has been completed. It is therefore save to invoke any sort
of the database lookup at this time. For further details see Section 8.4.8, “Callbacks”.
• Exchange Fees
All three types are available in the database table transaction. As most adapter do not provide fee information
on execution messages AlgoTrader does not currently import and fee or commission information provided by
the broker or exchange at the time of execution. Instead AlgoTrader uses the internal Execution Model (see
Section 5.1, “Exchange Simulator”) to assign fees and commissions based on configuration (e.g. commission-
per-contract).
<session_qualifier><id>.<version>
Example: lmax1.1
• id: an integer which is auto-incremented per session. For Fix, the last id is retrieved from the order table
during start up
• version: The number of modifications that took place on the Order, starting with 0 when the order is first
submitted.
By default AlgoTrader automatically assigns an IntId value to all outgoing orders. Open and executed orders
can be identified and looked up by their IntId.
Especially when using a Section 8.4.8.2, “Trade callback” it is necessary to generate and assign an IntId
value to the order prior to submitting it to the order service. The OrderService#getNextOrderId() method
can be used to generate a unique IntId value per session associated with an Account record.
163
CONFIDENTIAL Symbology
order.setIntId(orderId);
getOrderService().sendOrders(orders);
Note
Please note that care must be taken when using OrderService#getNextOrderId() with the IB
order service. The IB native interface expects orders to be sent with their order ids in ascending
order. The Class IBOrderIdSynchronizer is responsible to make sure order ids are actually
in ascending order. In case an order id is skipped the IBOrderIdSynchronizer will wait for up
to maxOrderSyncTime milliseconds for the order with the correct order id to arrive.
17.5. Symbology
In the electronic trading domain there are different ways to identify a security, some of which are:
• Symbol
• Bloomberg ID
• etc.
Different Brokers employ different types of Symbology to identify a security. For this purpose AlgoTrader
provides the notion of SymbologyResolver which is responsible for assigning appropriate information to
outgoing broker communication. These SymbologyResolvers can be extended on a per broker basis.
164
Chapter 18. CONFIDENTIAL
Market Data
AlgoTrader provides Market Data Interfaces with the following market data providers:
• Fix Interfaces
• Currenex
• DukasCopy
• Fortex
• FXCM
• LMAX
• Nexus Prime
• PrimeXM
• Bitstamp
• Native Interfaces
• Bloomberg
• Interactive Brokers
• QuantHouse
• Binance
• Bitfinex
• Bitflyer
• BitMEX
• CoinAPI
• Coinigy
AlgoTrader allows having multiple market data interfaces active at the same time so market data received from
different market data providers can be compared in real-time.
To enable either of those Market Data Interfaces the following two steps have to be executed:
1. The correct Spring Profile has to be activated according to Section 25.1, “Starter Classes”
165
CONFIDENTIAL Creation of Bars based on Ticks
2. For Bloomberg market-data-bb and for InteractiveBrokers market-data-ib has to be added to the VM
argument server-engine.init when running the AlgoTrader server.
• BarVO Open-High-Low-Close Price Bars, also containing volumes and volume weighted average prices
• TickVO: Snapshot of the market at a particular point in time, containing information like last price, last time,
bid, ask, volume, etc.
• QuoteVO: Its subclasses represent the current best bid and offer BidVO and AskVO
• TradeVO: An actual order that was executed on the market, containing information like last price, last size
and volume
• GenericTickVO: Represents additional price information made available by market data providers (e.g. open
price, close price, vwap price)
As the following diagram shows, market data providers deliver Price Events (TradeVO, BidVO & AskVO) or
individual TickVO Fields.
In back testing AlgoTrader supports both Ticks or Bars. In both live trading and back testing Price events and
Tick events can be aggregated into Bar events
Inside each strategy the MarketDataCacheService keeps a copy of the last MarketDataEvent for each
Security. For further details see Section 7.2.16, “Market Data Cache Service”.
166
CONFIDENTIAL Numeric Precision
select
first(last) as open,
max(last) as high,
min(last) as low,
last(last) as close
from
TickVO.win:time_batch(1 min)
group by
securityId;
select
max(last) as high,
min(last) as low,
first(last) as open,
last(last) as close
from
TickVO.win:expr_batch(sum(volume) > 1000)
group by
securityId;
Warning
Some market data providers (e.g. Interactive Brokers) will only provide market data snapshots
at regular time intervals. This can cause deviations in the bar high and low price.
Due to clock differences between the local machine and the market data provider's servers there
might be slight deviations in the bar open and close price. It is therefore important to enable
system clock synchronization on the server where AlgoTrader is installed.
167
CONFIDENTIAL Price normalization
Crypto currency exchanges are typically using web sockets to deliver market data. Web socket connections
are typically not very stable and it can happen that a web socket connection disconnects or suddenly stops
receiving data. For this purpose AlgoTrader has a feature that automatically reconnects the corresponding
adapter if no market data has been received for a prolonged period of time. This is enforced by the
CHECK_ADAPTER_TICK_GAP statement which can be turned on by changing the following property inside conf-
core.properties. Alternatively the properties can be changed via Section 2.3, “VM Arguments”
Example: To subscribe to a Generic Event of type Signal (which is a subclass of GenericEventVO), the following
needs to be done:
168
CONFIDENTIAL Generic Tick Events
getSubscriptionService().subscribeGenericEvents(Collections.singleton(Signal.class));
getEventDispatcher().broadcast(signal, EventRecipient.REMOTE_ONLY);
GenericEvents are automatically propagated to the Strategy Esper Engine where they can be accessed as
follows (the event type Signal needs to be added to the Esper config file):
@Override
public void onGenericEvent(GenericEventVO event) {
...
}
It is also possible to feed Generic Events via CSV Files in Simulation Mode. To enable this, the following
property inside conf.properties has to be updated. Alternatively the properties can be changed via
Section 2.3, “VM Arguments”
The file name of the CSV File has to be according to this schema:
<className>.<rank>.csv
• rank is the sort order for situations where there are multiple GenericEvent types for the same time stamp
As GenericTickVO is a subclass of MarketDataEventVO a strategy will automatically gen Generic Tick Events
delivered when it has subscribed to the corresponding instrument.
169
CONFIDENTIAL Generic Tick Events
A Generic Tick has a TickType which can be one of OPEN, HIGH, LOW, CLOSE, OPEN_INTEREST, IMBALANCE or
VWAP. A Generic Tick Event can hold either a BigDecimal, Double or Integer value.
170
Chapter 19. CONFIDENTIAL
Historical Data
AlgoTrader provides Historical Data Interfaces with the following market data providers:
• Native Interfaces
• Bloomberg
• Interactive Brokers
• Quandl
• CoinAPI
• CoinMarketCap
1
AlgoTrader uses the time series database InfluxDB for storage of historical data. InfluxDB is an open
source database written in Go specifically designed to handle time series data with high availability and high
performance requirements.
In addition the platform also provides a feature for downloading historical data from external market data
providers. This historical data can be used for strategy simulations or for any type of analysis.
• Storage: storeHistoricalBars
To use the Historical Data Service the corresponding Spring profiles have to be added via VM argument:
-Dspring.profiles.active=influxDB,bBHistoricalData
-Dspring.profiles.active=influxDB,iBHistoricalData
171
CONFIDENTIAL InfluxDB
-Dspring.profiles.active=influxDB,qDLHistoricalData
-Dspring.profiles.active=influxDB,cNPHistoricalData
-Dspring.profiles.active=influxDB,cMCHistoricalData
-Dspring.profiles.active=influxDB,noopHistoricalData
Note
The Noop Historical Data Service does not have a connection to an external data source. It can
be used during Simulation to access existing historical data from InfluxDB.
19.1. InfluxDB
2
For detailed information on InfluxDB please have a look at the InfluxDB Documentation .
InfluxDB can be installed locally or using Docker, please see Chapter 2, Installation and Deployment.
InfluxDB provides both a command line client (CLI) as well as a REST based client which is used by various
3
client side libraries. AlgoTrader uses the influxdb-java library to communicate with InfluxDB. For all operations
that involve the time series database InfluxDB, the following spring profile has to be specified via VM argument:
-Dspring.profiles.active=influxDB
If InfluxDB is installed locally, the influx command should be available via the command line. Executing
influx will start the CLI and automatically connect to the local InfluxDB instance. The output should look like
this:
2
https://docs.influxdata.com/influxdb/
3
https://github.com/influxdata/influxdb-java
172
CONFIDENTIAL InfluxDB
Note
• The InfluxDB HTTP API runs on port 8086 by default. Therefore, influx will connect to port
8086 and localhost by default. If these defaults need to be altered please run influx --
help.
• The -precision argument specifies the format/precision of any returned timestamps. In the
4
example above, rfc3339 tells InfluxDB to return timestamps in RFC3339 format (YYYY-MM-
DDTHH:MM:SS.nnnnnnnnnZ).
The command line is now ready to take input in the form of the Influx Query Language (a.k.a InfluxQL)
statements. To exit the InfluxQL shell, type exit and hit return.
Most InfluxQL statements must operate against a specific database. The CLI provides a convenience
statement, USE <db-name>, which will automatically set the database for all future requests. To use the
algotrader database please type:
Now future commands will only be run against the algotrader database.
At this point SQL-like queries can be executed against the database. In InfluxDB tables are called
measurements. AlgoTrader uses the two measurements tick and bar. Columns that hold actual data (e.g.
open or high) are called fields, and columns holding static data (e.g. barSize) are called tags.
As an example the following query shows all current bars in the database:
4
https://www.ietf.org/rfc/rfc3339.txt
173
CONFIDENTIAL InfluxDB
5
For an in depth description of the query syntax please visit the InfluxDB query language documentation .
To import existing data into InfluxDB please use the following command:
# DML
# CONTEXT-DATABASE: algotrader
bar,securityId=25,feedType=IB,barSize=MIN_1
open=1.30319,high=1.30402,low=1.30319,close=1.30367,vol=0 1324245720000000000
bar,securityId=25,feedType=IB,barSize=MIN_1
open=1.30369,high=1.30369,low=1.30351,close=1.30352,vol=0 1324245780000000000
bar,securityId=25,feedType=IB,barSize=MIN_1
open=1.30353,high=1.30383,low=1.30353,close=1.30382,vol=0 1324245840000000000
bar,securityId=25,feedType=IB,barSize=MIN_1
open=1.30381,high=1.30411,low=1.30373,close=1.30373,vol=0 1324245900000000000
bar,securityId=25,feedType=IB,barSize=MIN_1
open=1.30378,high=1.30428,low=1.30376,close=1.30425,vol=0 1324245960000000000
bar,securityId=25,feedType=IB,barSize=MIN_1
open=1.30426,high=1.30426,low=1.30396,close=1.30399,vol=0 1324246020000000000
bar,securityId=25,feedType=IB,barSize=MIN_1
open=1.30401,high=1.30411,low=1.30371,close=1.30378,vol=0 1324246080000000000
# DML
# CONTEXT-DATABASE: algotrader
tick,securityId=25,feedType=IB
last=1.303670,lastDateTime=1324245720000,bid=1.303670,ask=1.303670,volBid=0,volAsk=0,vol=0
1324245600000000000
tick,securityId=25,feedType=IB
last=1.303670,lastDateTime=1324245720000,bid=1.303670,ask=1.303670,volBid=0,volAsk=0,vol=0
1324245660000000000
tick,securityId=25,feedType=IB
last=1.303670,lastDateTime=1324245720000,bid=1.303670,ask=1.303670,volBid=0,volAsk=0,vol=0
1324245720000000000
tick,securityId=25,feedType=IB
last=1.303520,lastDateTime=1324245780000,bid=1.303520,ask=1.303520,volBid=0,volAsk=0,vol=0
1324245780000000000
tick,securityId=25,feedType=IB
last=1.303820,lastDateTime=1324245840000,bid=1.303820,ask=1.303820,volBid=0,volAsk=0,vol=0
1324245840000000000
5
https://docs.influxdata.com/influxdb/v1.1/query_language/
174
CONFIDENTIAL InfluxDB
tick,securityId=25,feedType=IB
last=1.303730,lastDateTime=1324245900000,bid=1.303730,ask=1.303730,volBid=0,volAsk=0,vol=0
1324245900000000000
tick,securityId=25,feedType=IB
last=1.304250,lastDateTime=1324245960000,bid=1.304250,ask=1.304250,volBid=0,volAsk=0,vol=0
1324245960000000000
6
For further information on InfluxDB import please visit the InfluxDB documentation
Note
• The last column in the import file represents the time stamp, which needs to be defined in
nanoseconds since the 1970-01-01
• It is also possible to gz compress import files. In this case the command line switch -
compressed has to be used when importing files.
To export bar data from InfluxDB into the AlgoTrader CSV file format (see Section 19.7, “Market Data File
Format”) please use the following command:
To export tick data from InfluxDB please use the following command:
Note
The InfluxDB export adds an extra column named "name" as the first column. In order to use the
exported .csv file for simulations one has to remove the first column of the file. The following
Linux command can be used to accomplish this:
To convert an AlgoTrader CSV file into an InfluxDB import file the following two Utility classes can be used:
ch.algotrader.util.influxdb.CSVBarToInfluxDBConverter
ch.algotrader.util.influxdb.CSVTickToInfluxDBConverter
6
https://docs.influxdata.com/influxdb/v1.2/tools/shell/
175
CONFIDENTIAL Live Data Recording
In addition recorded tick-level data can be aggregated into bar-data on the fly by using the following properties
inside conf-core.properties. Alternatively the properties can be changed via Section 2.3, “VM Arguments”:
# the bar size used for tick-to-bar aggregation and end-of-day historical bar download
historicalData.barSize = MIN_1
In case a certain instrument provides trade information (e.g. Equities and Crypto Currencies) the last traded
price is used to calculate the Bar values. In case no trading information is available (e.g. Forex and Indices)
the midpoint price (average of bid and ask) is used. The bar aggregation feature will also create bars for time
periods when no market data arrives, in this case open, high, low and close will be equal to the previous bars
close price.
Download and storage of historical data can be invoked via the HistoricalDataStarter.
For example:
AlgoTrader also provides features to download missing historical data for all subscribed instruments either on
startup or at a specific time of the day. For these functions the following properties are available inside conf-
core.properties where they can be changed. Alternatively the properties can be changed via Section 2.3,
“VM Arguments”:
176
CONFIDENTIAL Interactive Brokers Historical Data Download
# the bar size used for tick-to-bar aggregation and end-of-day historical bar download
historicalData.barSize = MIN_1
# the market data event type used by the end-of-day historical bar download
historicalData.marketDataEventType = MIDPOINT
Depending on whether InteractiveBrokers, Bloomberg or Quandl is used for the historical data download the
corresponding marketData profile has to be specified via VM argument.
InteractiveBrokers:
-Dspring.profiles.active=influxDB,iBHistoricalData
Bloomberg:
-Dspring.profiles.active=influxDB,bBHistoricalData
Quandl:
-Dspring.profiles.active=influxDB,qdlHistoricalData
• Making six or more historical data requests for the same Contract, Exchange and Tick Type within two
seconds.
The AlgoTrader Historical Data Download can optionally avoid potential pacing violation by separating
subsequent download requests by 10 seconds. This feature can be enabled via the following property inside
conf-ib.properties has to be updated. Alternatively the properties can be changed via Section 2.3, “VM
Arguments”
7
http://interactivebrokers.github.io/tws-api/historical_limitations.html
177
CONFIDENTIAL Quandl Historical Data Download
8
The Historical Data Download also takes the Valid Duration and Bar Size Settings for Historical Data Requests
into account and splits large requests into subsequent smaller requests.
/<baseDir>/<eventType>/<dataSet>/<filename>.csv
• baseDir is the parent directory where all market data files are stored. This is either the files/ directory
under the project algotrader-core or an arbitrary directory defined via the following property inside
conf.properties has to be updated. Alternatively the properties can be changed via Section 2.3, “VM
Arguments”
• eventType is either tickData, barData or genericData (see Section 18.5, “Generic Events”).
• dataSet is the name of the dataset used for the simulation run. This can be defined via the following property
inside conf.properties has to be updated. Alternatively the properties can be changed via Section 2.3,
“VM Arguments”
8
http://interactivebrokers.github.io/tws-api/historical_limitations.html
9
https://www.quandl.com/
10
https://www.google.com/finance/historical?q=aapl
178
CONFIDENTIAL Tick Data Files
dataSource.dataSet = current
• isin
• symbol
• bbgid
• ric
• conid
• securityId
An alternative approach is to feed market data for multiple securities using one file. E.g. it is possible to feed
market data for futures using market data from the corresponding generic future. In this approach an additional
column security has to be added to the market data file which will be used to identify the actual Security.
The file name for Section 18.5, “Generic Events” follows a different logic.
• dateTime
• last
• lastDateTime
• volBid
• volAsk
• bid
• ask
• vol
• security (optional)
dateTime and lastDateTime values are expected to be in the yyyy-MM-dd HH:mm:ss format and to represent
local time. Alternatively one can also use long values that represent Java milliseconds since 1970.
Example:
179
CONFIDENTIAL Bar Data Files
• dateTime
• open
• high
• low
• close
• vol
• vwap (optional)
• security (optional)
dateTime values are expected to be in the yyyy-MM-dd HH:mm:ss format and to represent local time.
Alternatively one can also use long values that represent Java milliseconds since 1970.
Example:
180
Chapter 20. CONFIDENTIAL
Reference Data
Amongst others reference Data consists of static data like Security, SecurityFamily, SecurityReference,
Account Entities.
Reference Data can either be configured in the database directly through the corresponding tables or one can
use the ReferenceDataService and corresponding ReferenceDataStarter.
Depending on the Reference Data Adapter in use the following download options are available for download:
For further details please see the JavaDoc of the ReferenceDataStarter class.
Example: To download missing Futures of a specified Future Families use the following command
It is recommended to run this Service in the interval of Option / Future Expirations to make sure that the entire
chain is available to strategies.
Depending on the Reference Data Adapter in use the corresponding referenceData profile has to be specified
via VM argument.
Note
For Bitstamp, BitFlyer, Bitfinex, CoinMarketCap, Binance and BitMEX - an account and an
exchange corresponding to the adapter must be setup in the database prior to running. It can
be achieved by executing the sample database script samples/db/mysql/mysql-data.sql
Bloomberg:
-Dspring.profiles.active=bBReferenceData
InteractiveBrokers:
181
CONFIDENTIAL
-Dspring.profiles.active=iBReferenceData
Trading Technologies:
-Dspring.profiles.active=tTReferenceData
Binance:
-Dspring.profiles.active=bNCReferenceData
Bitfinex:
-Dspring.profiles.active=bFXReferenceData
Bitflyer:
-Dspring.profiles.active=bFLReferenceData
BitMEX:
-Dspring.profiles.active=bMXReferenceData
Bitstamp:
-Dspring.profiles.active=bTSReferenceData
CoinAPI:
-Dspring.profiles.active=cNPReferenceData
Coinigy:
-Dspring.profiles.active=cNGReferenceData
CoinMarketCap:
-Dspring.profiles.active=cMCReferenceData
182
Chapter 21. CONFIDENTIAL
Account Data
AlgoTrader provides Account Data Service for the following adapters:
1
• Binance via Binance API
2
• Bitfinex via Bitfinex API
3
• Bitflyer via Bitflyer API
4
• BitMEX via BitMEX API
5
• Bitstamp via Bitstamp API
6
• Coinigy via Coinigy API
The AccountService interface defines a method for retrieving account balances for the specified account ID.
A list of NamedCurrencyAmountVO items is returned. The retrieveAccountBalances method can be called
from the strategy like this:
The AccountService interface also defines a method for the initiation of crypto withdrawals for crypto
exchanges. The withdraw method can be called from the strategy like this:
The withdrawContext parameter contains additional information that might be required by certain exchanges
(e.g. address and/or paymentId). The method returns a WithdrawStatusVO which contains information like
message and externalId.
Depending on the Account Data Adapter in use the corresponding account profile has to be specified via VM
argument.
InteractiveBrokers:
1
https://github.com/binance-exchange/binance-java-api
2
https://docs.bitfinex.com/docs
3
https://bitflyer.com/api
4
https://www.bitmex.com/app/apiOverview
5
https://www.bitstamp.net/api/
6
https://coinigy.docs.apiary.io/
183
CONFIDENTIAL
-Dspring.profiles.active=iBAccount
Coinigy:
-Dspring.profiles.active=cNGAccount
Bitfinex:
-Dspring.profiles.active=bFXAccount
Bitflyer:
-Dspring.profiles.active=bFLAccount
Bitstamp:
-Dspring.profiles.active=bTSAccount
Binance:
-Dspring.profiles.active=bNCAccount
BitMEX:
-Dspring.profiles.active=bMXAccount
184
Chapter 22. CONFIDENTIAL
Adapters
The following sections give a detailed overview of the different adapters available for AlgoTrader.
To configure a Fix trading connection the following steps have to be taken care of:
• Add the corresponding fix trading profile to the VM argument spring.profiles.active (e.g. cNXFix):
-Dspring.profiles.active=live,pooledDataSource,cNXFix,embeddedBroker,html5,InfluxDB
[session]
SessionQualifier=CNXT
BeginString=FIX.4.4
SenderCompID=xxx
TargetCompID=CNX
SocketConnectHost=dret-fix-ssl.currenex.com
SocketConnectPort=443
SocketUseSSL=Y
Username=xxx
Password=xxx
ValidateIncomingMessage=N
ResetOnLogon=Y
Inactive=Y
• Make sure there is an entry in the MySQL account table where the column ORDER_SERVICE_TYPE matches
the type of the fix interface (e.g. CNX_FIX), the column SESSION_QUALIFIER matches the SessionQualifier
specified in the file fix.cfg and the ACTIVE column is set to 1.
If market data is also received through a Fix interface the following items need to be added as well:
• Add the corresponding fix market data profile to the VM argument spring.profiles.active (e.g. cNXFix):
-
Dspring.profiles.active=live,pooledDataSource,cNXMarketData,embeddedBroker,html5,InfluxDB
1
https://www.quickfixj.org/
185
CONFIDENTIAL Fix Interface
• Add the fix session to /algotrader/core/fix.cfg (an example file fix-template.cfg is provided in the
same directory):
[session]
SessionQualifier=CNXMD
BeginString=FIX.4.4
SenderCompID=xxx
TargetCompID=CNX
SocketConnectHost=dret-fix-ssl.currenex.com
SocketConnectPort=443
SocketUseSSL=Y
Username=xxx
Password=xxx
ValidateIncomingMessage=N
ResetOnLogon=Y
Inactive=Y
• When making subscriptions add the FeedType corresponding to the Fix interface (e.g. CNX)
Important
Please make sure to have the setting Inactive=Y in both trading and market-data sections.
Without this setting the fix session will be initialized before the remaining system has been fully
initialized and might cause either trading or market data to malfunction.
2
For further information regarding QuickFix/J configuration please visit the QuickFix/J documentation
Per default Fix interfaces uses the following items to identify a particular instrument:
Options
Exchange IB_CODE
SecurityFamily CURRENCY
SecurityFamily SYMBOL_ROOT
Option STRIKE
Option TYPE
Option EXPIRATION
SecurityFamily CONTRACT_SIZE
Future
SecurityFamily CURRENCY
2
https://www.quickfixj.org/usermanual/1.6.4//usage/configuration.html
186
CONFIDENTIAL FIX configuration
Exchange IB_CODE
SecurityFamily SYMBOL_ROOT
Future EXPIRATION
SecurityFamily CONTRACT_SIZE
Forex
SecurityFamily CURRENCY
Exchange IB_CODE
Forex BASE_CURRENCY
Stock
SecurityFamily CURRENCY
Exchange IB_CODE
Stock SYMBOL
Fund
SecurityFamily CURRENCY
Exchange IB_CODE
Index SYMBOL
The file fix-template.cfg contains default parameters suggested by AlgoTrader for all FIX sessions. The
individual [session] blocks should be added after the [default] block.
[default]
ConnectionType=initiator
HeartBtInt=30
ReconnectInterval=5
FileStorePath=files/fix
FileLogPath=log
FileLogHeartbeats=N
FileIncludeMilliseconds=Y
FileIncludeTimeStampForMessages=Y
SLF4JLogHeartbeats=N
187
CONFIDENTIAL FIX logging
3
For further information regarding QuickFix/J configuration please visit the QuickFix/J documentation
file (default)
Log to QuickFix/J standard file logger.
slf4j
Log to the Simple Logging Facade for Java (SLF4J). Log entries will be committed to the logging back-
end configured by SLF4J.
screen
Log to QuickFix/J standard console logger.
none
Disable logging. No FIX session events or messages will be logged.
The last option might be especially useful for volume intensive market data sessions where persistent message
log could be unnecessary or even excessive. Custom Fix logging options can be configured as follows:
[session]
SessionQualifier=FIXMD
BeginString=FIX.4.4
...
LogImpl=none
3
https://www.quickfixj.org/usermanual/1.6.4//usage/configuration.html
4
https://www.quickfixj.org/usermanual/1.6.4//usage/configuration.html#Storage
188
CONFIDENTIAL Session life-cycle events
@Override
public void onChange(final SessionEventVO event) {
switch (event.getState()) {
case CONNECTED:
// session connected but not yet authenticated
break;
case LOGGED_ON:
// session connected and authenticated
break;
case DISCONNECTED:
// session disconnected
break;
}
}
When trading crypto currencies it is recommended to update the following properties inside conf.properties.
Alternatively the properties can be changed via Section 2.3, “VM Arguments”:
Exchange accounts allow you to exchange on crypto/fiat to another (e.g. USD to BTC). When trading exchange
accounts. Exchange accounts do not allow short trades.
Margin accounts allow you to have long and short positions on any pair (e.g. long BTC/USD or short ETH/BTC).
The SimpleOrder property exchangeOrder (which is false per default) drives which account you are trading
against:
189
CONFIDENTIAL Custom currency mapping
Note
Note that every exchange differs on how to submit exchange account vs. margin account orders.
Some require you to use different securities (e.g. Bitflyer), others different order types or price
types.
22.4. Bloomberg
The Bloomberg interface supports Market Data, Historical Data as well as Reference Data.
The Bloomberg interface provides both synchronous connections and asynchronous connections.
Asynchronous connections are generally used for live market data whereas synchronous connections are used
for retrieval of historical data as well as retrieval of reference data.
If market data is received through the Bloomberg interface the following items need to be added:
-
Dspring.profiles.active=live,pooledDataSource,bBMarketData,embeddedBroker,html5,InfluxDB
Bloomberg uses the BBGID field of the Security table to identify instruments.
5
For further details on the Bloomberg interface please visit the Bloomberg Open API
22.5. Currenex
The main features of the Currenex platform are
5
https://www.bloomberg.com/professional/support/api-library/
190
CONFIDENTIAL DukasCopy
• Uses FOREX_MARKET (type C) and FOREX_LIMIT (type F) for Market and Limit orders
Currenex uses the columns Forex BASE_CURRENCY and SecurityFamily CURRENCY to identify an instrument.
22.6. DukasCopy
The DukasCopy interface supports Market Data as well as Order Processing.
Since the DukasCopy Fix Implementation does not follow the Fix Standard very closely, a few customizations
had to be made:
DukasCopy uses the columns Forex BASE_CURRENCY and SecurityFamily CURRENCY to identify an instrument.
The DukasCopy Fix interface uses an SSL encrypted connection which is supported by QuickFix/J using Mina.
In addition the DukasCopy interface requires username and password to be send with the Logon message.
For this purpose the class DCLogonMessageHandler is used as an outgoing MessageHandler.
The EzeSoft / RealTick Fix interface currently supports only Order Processing.
The Fix Implementation of EzeSoft / RealTick is well conforming with the Fix Standard no customizations had
to be made.
191
CONFIDENTIAL Fortex
The IB Fix interface uses standard Fix instrument definitions mentioned at the end of section Section 22.1,
“Fix Interface”.
22.8. Fortex
Fortex uses almost vanilla Fix/4.4 protocol with a very customizations. It supports FX only.
22.9. FXCM
FXCM interface FIX/4.4 protocol does not deviate much from the standard but has some peculiarities about
the way FIX sessions are established
• Unlike many other FIX connectivity providers who provide separate FIX sessions for market data and trading
interfaces FXCM by default offers one session for both market data feed and trading operations
• Uses extra UserRequest / UserResponse message exchange to authenticate the user and to fully initialize
the session
FXCM uses the columns Forex BASE_CURRENCY and SecurityFamily CURRENCY to identify an instrument.
The IB interface supports Market Data, Historical Data, Order Processing, Retrieval of account information as
well as Reference Data.
Note
You get market data for a minimum of 100 instruments with subscriptions (depends on your
commissions and assets with IB). You can buy up to 10 quote boosters for USD 30 each, which
provide 100 additional instruments each (max 1000). For details, consult the IB market data
6
fees.
7
Similar restrictions/extensions exist for historical data. Those details can be viewed here.
6
https://www.interactivebrokers.com/en/index.php?f=14193#market-data-fees
7
http://interactivebrokers.github.io/tws-api/historical_limitations.html#hd_availability
192
CONFIDENTIAL IB Native Interface
-
Dspring.profiles.active=live,pooledDataSource,iBNative,embeddedBroker,html5,InfluxDB
• Make sure there is an entry in the account database where the column ORDER_SERVICE_TYPE is set to
IB_NATIVE) .
If market data is also received through the IB interface the following items need to be added as well:
-
Dspring.profiles.active=live,pooledDataSource,iBMarketData,embeddedBroker,html5,InfluxDB
• Use instrument symbols and additional data depending on the instrument type:
Options
Exchange IB_CODE
SecurityFamily CURRENCY
SecurityFamily SYMBOL_ROOT
Option STRIKE
Option TYPE
Option EXPIRATION
SecurityFamily CONTRACT_SIZE
Future
SecurityFamily CURRENCY
Exchange IB_CODE
SecurityFamily SYMBOL_ROOT
Future EXPIRATION
SecurityFamily CONTRACT_SIZE
193
CONFIDENTIAL IB Native Interface
Forex
SecurityFamily CURRENCY
Exchange IB_CODE
Forex BASE_CURRENCY
Stock
SecurityFamily CURRENCY
Exchange IB_CODE
Stock SYMBOL
Index
SecurityFamily CURRENCY
Exchange IB_CODE
Index SYMBOL
Combination
SecurityFamily CURRENCY
Exchange IB_CODE
SecurityFamily BASE_SYMBOL
Component QUANTITY
8
• The IB Native interface uses the RT_VOLUME events to process incoming trade events
• The IB Native interface propagates daily OPEN and CLOSE prices to strategies in case the following property
inside conf-ib.properties is enabled. Alternatively the properties can be changed via Section 2.3, “VM
Arguments”
• The IB Native interface propagates VWAP prices to strategies in case the following property inside conf-
ib.properties is enabled. Alternatively the properties can be changed via Section 2.3, “VM Arguments”
8
https://interactivebrokers.github.io/tws-api/tick_types.html#rt_volume
194
CONFIDENTIAL IB Market Data Subscriptions
ib.emitVWAP = true
• The IB Native interface expects orders to be sent with their order ids in ascending order. The Class
IBOrderIdSynchronizer is responsible to make sure order ids are actually in ascending order. In case an
order id is skipped the IBOrderIdSynchronizer will wait for up to maxOrderSyncTime milliseconds for the
order with the correct order id to arrive.
• The IB Native interface supports trading of tradeable / non-synthetic combinations by placing BAG orders
through the IB interface.
• The IB Native interface reports volBid, volAsk and vol in lots of 100 contracts for US equities. Please see
9
the following page for further details on handling of Odd Lot Orders
10
For further details on the IB native interface please visit the IB API Reference Guide
IB provides free 15min delayed data, but this will not be accessible through the IB API. In order to access both
real time market data as well as historical data, a corresponding market data subscription has to be in place.
Market data can be accessed both through the IB paper trading account as well as the live trading account.
Note
• The paper trading account has one single username assigned to it. The live trading account
can have multiple usernames.
• For each username (live account & paper trading account) only one session can exist at the
same time. If you login with the same username on a different machine the other session
will get logged out.
• If the live account username (that is sharing its market data subscription with the paper trading
account) is currently logged in, the paper trading account doesn't get market data until the
live account is again logged out.
• If a client wants to login to the live trading account at the same time that AlgoTrader is
connected to the paper trading account, he has to create a second username under the live
account and purchase additional market data subscriptions for that username.
11
To get a market data subscriptions one has to login to the IB account management with the live trading
account. Then follow these steps:
9
https://ibkr.info/node/1062
10
https://www.interactivebrokers.com/en/software/api/api.htm
11
https://gdcdyn.interactivebrokers.com/sso/Login
195
CONFIDENTIAL IB Market Data Subscriptions
1. Select Settings / User Settings in the menu on the left. Then select Market Data Subscriptions on the right
196
CONFIDENTIAL IB Market Data Subscriptions
• NASDAQ (Network C/UTP): live market data for NASDAQ listed equities
• NYSE (Network A/CTA): live market data for NYSE listed equities
• US Securities Snapshot and Futures Value Bundle: live market data for US futures and snapshot data
for US equities (AT cannot process snapshot data, so in addition NASDAQ and NYSE has to be subscribed
as well)
197
CONFIDENTIAL IB Market Data Subscriptions
To use these market data subscriptions through the paper trading account follow these steps:
1. Select Settings / User Settings in the menu on the left. Then select Paper Trading Account on the right
2. Then select Yes next to Share real-time market data subscriptions with paper trading account
3. Then Select the username whose market data you want to share. This will share the market data
subscriptions of the live account with the paper trading account.
198
CONFIDENTIAL IB Fix Interface
Note
In case no market data arrives through the IB interface it is usually best to login to
InteractiveBrokers Trader Workstation (TWS) as there are usually warning messages that
indicate what might be the issue
The interface is fully capable of handling IB's Financial Advisor functionality like Sub Accounts, Account Groups
and Allocation Profiles.
199
CONFIDENTIAL JP Morgan
12
For further details on the IB Fix interface please visit the IB FIX/CTCI Users' Guide
The IB Fix interface uses standard Fix instrument definitions mentioned at the end of section Section 22.1,
“Fix Interface”.
22.12. JP Morgan
The JP Morgan Fix interface supports Order Processing only.
As the JP Morgan Fix Implementation is well conforming with the Fix Standard no customizations had to be
made
The JP Morgan Fix interface uses standard Fix instrument definitions mentioned at the end of section
Section 22.1, “Fix Interface”.
22.13. LMAX
Supports only a limited number of securities, mainly Forex
• Uses predefined contract modifiers for market data events and order quantities. The contract modifiers are
not included in FIX messages and have to be applied by the interface adaptor.
• Supports only IOC and FOK time-in-force parameters for market orders.
• Supports DAY, GTC, IOC and FOK time-in-force parameters for limit orders.
• Supports only DAY and GTC time-in-force parameters for stop orders.
• supports trading status signaling temporary suspension and resumption in trading of individual securities.
LMAX uses the column LMAXID of the security table to identify an instrument
• Orders cannot be modified, instead one needs to cancel the current order first and then resend a new one.
12
https://www.interactivebrokers.com/en/index.php?f=4988
13
http://www.isriskanalytics.com/products/prime
200
CONFIDENTIAL PrimeXM
• Buy limit orders need to be placed below the market price. Sell limit orders need to be placed above the
market price
• Buy stop orders need to be placed above the market price. Sell stop orders need to be placed below the
market price.
• Minimum trade size allowed on most currency pairs is .01 lots which is 1000 notional
Nexus Prime uses the columns Forex BASE_CURRENCY and SecurityFamily CURRENCY to identify an
instrument.
22.15. PrimeXM
The PrimeXM FIX/4.4 interface implementation follows the Fix Standard closely, but uses MassQuote messages
for conveying the market data. Each MassQuote message has to be acknowledged by the FIX client.
Only Forex instruments are supported by the PrimeXM Fix Interface. Order modifications are not supported.
22.16. Quandl
14
Quandl is a public service that provides a wide range of financial, economic and alternative data. It is mostly
end of day data but also some intra-day(e.g. hourly) data. To find out if they have what you are looking for, check
15
their data products page. AlgoTrader allows downloading historical data from Quandl. For more information
16
about Quandl please have a look at the Quandl Docs/Help .
Data on Quandl is divided into databases. Each database contains multiple datasets. For instance EOD
database contains end-of-day data for all publicly-traded US stocks. Each database/dataset pair is uniquely
identified by database_code/dataset_code pair. For instance EOD/AAPL is the globally unique code for the
17
AAPL stock dataset within the EOD database. The Quandl database browser can be used to find suitable
databases for desired instrument type, region and data type.
The QdlHistoricalDataService is integrated with the AlgoTrader Historical Data Download and needs
to be enabled by specifying the qdlHistoricalData Spring profile (see section Section 19.3, “Historical
Data Download”). The QdlHistoricalDataService transforms retrieved Quandl data into AlgoTrader bars.
Transformation rules between the Quandl data format and AlgoTrader Bar format are defined in the file
quandl.yml. By default the file quandl.yml already contains the transformation rules for most commonly used
Quandl databases. Additional transformation rules can be added to the file as needed:
EOD:
barSize: DAY_1
columnMapping:
dateTime: Date
14
https://www.quandl.com/
15
https://www.quandl.com/search?query=
16
https://www.quandl.com/docs-and-help
17
https://www.quandl.com/search
201
CONFIDENTIAL QuantHouse
open: Open
high: High
low: Low
close: Close
vol: Volume
The relevant properties for the Quandl adapter are defined inside the file conf-qdl.properties where they
can be changed. Alternatively the properties can be changed via Section 2.3, “VM Arguments”:
#{"type":"String","label":"API Key"}
qdl.apiKey = ATVxxxxxxxxxxxxx
To use the QdlHistoricalDataService please replace the property qdl.apiKey with the API Key that can
be retrieved through the Quandl Account Settings.
In terms of historical data download a mapping between the Quandl database and the SecurityFamily
entity is defined by the quandl_database field in the security_family table. Similarly a mapping between
the Quandl dataset and the Security entity is defined by the quandl_dataset field in the security table.
AlgoTrader sample data files (samples/db/mysql/mysql-data.sql and samples/db/mysql/h2-data.sql)
already contain quandl_database/quandl_dataset values for all sample security families and most sample
securities.
22.17. QuantHouse
The QuantHouse adapter is based on the QuantHouse ultra low latency market data feed QuantFEED. The
QuantHouse adapter supports live Market Data.
If market data is received through the QuantHouse interface the following items need to be added:
-
Dspring.profiles.active=live,pooledDataSource,qHMarketData,embeddedBroker,html5,InfluxDB
QuantHouse uses the Exchange MIC and Security SYMBOL fields to identify instruments.
18
For further details on the QuantHouse interface please contact QuantHouse
18
https://www.quanthouse.com/
202
CONFIDENTIAL SocGen
22.18. SocGen
The SocGen FIX/4.2 interface supports Order Processing only.
The SocGen Fix Implementation follows the Fix Standard closely, but some minor customizations according
to the 'SocGen FIX Rules of Engagement' had to be made. Additionally exchange specific restrictions rules
defining the allowed order type / TIF combinations were added.
Only Future instrument orders are supported by the SocGen Fix Interface.
• Provides a reference data service that can be used to download contract definitions
22.20. UBS
The UBS Fix interface supports Order Processing for futures and options only.
As the UBS Fix Implementation is well conforming with the Fix Standard no customizations had to be made
The UBS Fix interface uses standard Fix instrument definitions mentioned at the end of section Section 22.1,
“Fix Interface”.
203
CONFIDENTIAL Crypto-Adapter Order Constraints
• MinValue - minimum value of the order. For example for limit order: Quantity * Limit price
Reference data loaders set such values depending on the information provided by the exchange.
There is one limitation for Reference data loaders - security family, once saved, is never updated automatically
- even if there is a change on the exchange. It must be fixed manually in the database or removed and loaded
again.
Constraint Description
name
MinQty minimum_order_size
MaxQty maximum_order_ size
QtyIncr 0.00000001
MinPrice -
MaxPrice -
PriceIncr price_precision
MinValue -
Details
Constraint Description
name
MinQty 1
MaxQty -
QtyIncr 1
MinPrice 1 Satoshi
MaxPrice -
PriceIncr 1 Satoshi
MinValue -
Details We trade perpetual contracts on BitMex. Usually it involves one contract for specific price
expresed in BTX/XBTC
19
https://api.bitfinex.com/v1/symbols_details
204
CONFIDENTIAL Crypto-Adapter Order Constraints
Constraint Description
name
MinQty 0.00000001
MaxQty -
QtyIncr 0.00000001
MinPrice 0.00001
MaxPrice -
PriceIncr 0.00001
MinValue 5 USD
Details
There is one simple rule for BitStamp - order value should be at minimum 5 USD.
The 5 USD value is calculated on BitStamp exchange with market prices and
involves all types of orders.
It is not validated on AlgoTrader side as it doesn't have live list of all pairs.
20
BitSampt limits
Constraint Description
name
MinQty minQty
MaxQty maxQty
QtyIncr stepSize
MinPrice minPrice
MaxPrice maxPrice
PriceIncr tickSize
MinValue minNotional
Details
20
https://www.bitstamp.net/article/bitstamp-minimum-trade-changing-to-5/
21
https://support.binance.com/hc/en-us/articles/115000594711-Trading-Rule
22
https://github.com/binance-exchange/binance-official-api-docs/blob/master/rest-api.md#filters
205
CONFIDENTIAL Crypto-Adapter Order Constraints
Constraint Description
name
Binance has most of the constraints set. Names in this table
for particular constraint is the name of value from filter.
Constraint Description
name
MinQty
BTC/JPY: 0.001
ETH/BTC: 0.01
FX: BTC/JPY: 0.01
MaxQty -
QtyIncr 0.00000001
MinPrice -
MaxPrice -
PriceIncr
1 JPY -
if transaction Currency is JPY
0.00001 BTC -
if transaction Currency is BTC
MinValue -
Details
Constraint Description
name
MinQty -
23
https://bitflyer.com/en-jp/faq/4-5
206
CONFIDENTIAL Order Type Validation
Constraint Description
name
MaxQty -
QtyIncr -
MinPrice -
MaxPrice -
PriceIncr -
MinValue -
Details Assuming there is a "proxy" to other exchanges - It must meet target exchange limits
Below is simple table with the aggregated constraints in one place. It contains the same data as the tables
above (except for the details column).
Coingy
- - - - - - -
207
CONFIDENTIAL Binance
22.22. Binance
24 25
Binance is a cryptocurrency exchange. Please see the API reference page for the technical details.
Binance provides Java library for interacting with Binance API. It supports REST requests to endpoint providing
orders functionality, account data and reference data. Support for market data is done using WebSocket API.
The relevant properties for the Binance adapter are defined inside the file conf-bnc.properties where they
can be changed. Alternatively the properties can be changed via Section 2.3, “VM Arguments”:
#{"type":"String","label":"API Key"}
bnc.apiKey = XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
#{"type":"String","label":"API Secret"}
bnc.apiSecret = XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
A Binance account is necessary in order to use Binance adapter. Unique apiKey and apiSecret settings must
be set to the actual values (either in the properties file or by setting a VM argument)
Note
Binance is very time sensitive , i.e. if your computer is ahead of the Binance system clock, the
API might reject your orders with an exception similar to
server's time
To prevent these issues, we suggest synchronizing your system clock with an internet reference
26
time using e.g. this time sync tool .
24
https://www.binance.com/
25
https://github.com/binance-exchange/binance-java-api
26
http://www.timesynctool.com/
208
CONFIDENTIAL Bitfinex
AlgoTrader currently support market, limit and stop limit orders on Binance.
Note that Binance has restrictions to the amount of (algo) orders that can be placed on an instrument or
27
exchange. See the Binance API filter page for details.
22.23. Bitfinex
28
Bitfinex is a cryptocurrency exchange. The Bitfinex adapter provides order execution, market data, reference
29
data and account data functionality. Please see the Bitfinex API reference page for technical details about
the supported features.
The relevant properties for the Bitfinex adapter are defined inside the file conf-bfx.properties where they
can be changed. Alternatively the properties can be changed via Section 2.3, “VM Arguments”:
#{"type":"String","label":"API Key"}
bfx.apiKey = XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
#{"type":"String","label":"API Secret"}
bfx.apiSecret = XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
#{"type":"Integer","label":"API-level constant'}
bfx.scale = 5
A Bitfinex account is necessary in order to use Bitfinex adapter. Unique apiKey and apiSecret settings must
be set to the actual values (either in the properties file or by setting a VM argument)
Market, limit and stop orders are supported. For exchange account trading, different order types are used
(exchange market, exchange limit and exchange stop).
22.24. Bitflyer
30
Bitflyer is a cryptocurrency exchange. The Bitflyer adapter supports order execution, market data, reference
31
data and account data functionality. Please see the Bitflyer API reference page for technical details.
27
https://github.com/binance-exchange/binance-official-api-docs/blob/master/rest-api.md#filters
28
https://www.bitfinex.com/
29
https://docs.bitfinex.com/docs
30
https://bitflyer.com/en-jp/
31
https://bitflyer.com/api
209
CONFIDENTIAL Bitflyer
Note
At this point (April 2018), Bitflyer does not yet support cross-border trading, so trading vs. USD
is only possible with a US account.
The relevant properties for the Bitflyer adapter are defined inside the file conf-bfl.properties where they
can be changed. Alternatively the properties can be changed via Section 2.3, “VM Arguments”:
#{"type":"String","label":"API Key"}
bfl.apiKey = XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
#{"type":"String","label":"API Secret"}
bfl.apiSecret = XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
A Bitflyer account is necessary in order to use Bitflyer adapter. Unique apiKey, apiSecret as well as market
data subscription key settings must be set to the actual values (either in the properties file or by setting a VM
argument)
32
Bitflyer uses different instruments for exchange and margin trading (see Bitflyer margin trading. )
Note
At this point (July 2018), Bitflyer does not support order modifications.
32
https://lightning.bitflyer.com/About-Fx?lang=en
210
CONFIDENTIAL BitMEX
22.25. BitMEX
33
BitMEX is a cryptocurrency futures exchange. The BitMEX adapter provides order execution, market data,
reference data and account data functionality through REST and WebSocket API. Please see the API
34
reference page for technical details about the supported features.
The relevant properties for the BitMEX adapter are defined inside the file conf-bmx.properties where they
can be changed. Alternatively the properties can be changed via Section 2.3, “VM Arguments”:
#{"type":"String","label":"API Key"}
bmx.apiKey = XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
#{"type":"String","label":"API Secret"}
bmx.apiSecret = XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
#{"type":"Integer","label":"API-level constant'}
bmx.balanceScale = 8
A BitMEX account is necessary in order to use the BitMEX adapter. Unique apiKey and apiSecret settings
must be set to the actual values (either in the properties file or by setting a VM argument)
Type Description
Future Regular future contracts expiring every 3 months. Please note that the XBT (BTC) contracts
35
have a variable contract size. For more information please see Futures Guide .
Forex Bitcoin perpetual contract XBTUSD (BTCUSD) is treated as a Forex cryptocurrency pair with
36
a variable contract size. For more information please see Perpetual Contract Specification .
37
Index For a complete list of supported indices please see Indices .
The contract size is defined in the CONTRACT_SIZE field of the security_family table. It has to be updated
regularly for contracts with a variable contract size, which can be done either manually or through the BitMEX
Reference Data Service.
The minimum quantity for all contracts is 1 contract (lot size = 1). Only integer number of contracts are allowed.
The QUANTITY_SCALE for all securities is set to 0 and must not be changed.
33
https://www.bitmex.com/
34
https://www.bitmex.com/app/apiOverview
35
https://www.bitmex.com/app/futuresGuide
36
https://www.bitmex.com/app/contract/XBTUSD
37
https://www.bitmex.com/app/index/.BXBT
211
CONFIDENTIAL Bitstamp
Placing an order to buy one XBTUSD means buy the amount of Bitcoins worth 1 USD. For more information
38
please consult the BitMEX perpetual contract details page. .
Note
Due to high volume on the BitMEX exchange, placing an order on the exchange sometimes fails
with the following message: The system is currently overloaded. Please try again
39
later. For more information please see BitMEX Technology Scaling .
22.26. Bitstamp
40 41
Bitstamp is a cryptocurrency exchange. Please see the API reference page for the technical details.
Order and market data related functionality is provided via FIX/4.4 protocol. Account data and reference data
is provided via REST API.
Bitstamp FIX/4.4 interface follows the standard closely, but offers only one session for both market data feed
and trading operations. Bitstamp market data supports only limited number of cryptocurrency (Forex) securities.
Order modifications are not supported. For more information about the Bitstamp FIX specification please have
42
a look at the Bitstamp public FIX interface .
The relevant properties for the Bitstamp adapter are defined inside the file conf-bts.properties where they
can be changed. Alternatively the properties can be changed via Section 2.3, “VM Arguments”:
#{"type":"String","label":"API Key"}
bts.apiKey = XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
#{"type":"String","label":"API Secret"}
bts.apiSecret = XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
#{"type":"String","label":"Customer ID"}
bts.customerId = XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
38
https://www.bitmex.com/app/seriesGuide/XBT
39
https://blog.bitmex.com/bitmex-technology-scaling-part-1/
40
https://www.bitstamp.net/
41
https://www.bitstamp.net/api/
42
https://www.bitstamp.net/fix/
212
CONFIDENTIAL CoinAPI
bts.importAllPairs = true
A Bitstamp account is necessary in order to use Bitstamp adapter. Unique apiKey and apiSecret settings
must be set to the actual values (either in the properties file or by setting a VM argument)
At this point (July 2018), Bitstamp does not support order modifications.
22.27. CoinAPI
43
CoinAPI is a market data gateway to multiple crypto exchanges. CoinAPI provides historical and live market
data. It also provides reference data for the supported instruments, however it doesn't provide trading related
44
functionality. Please see the API reference page for the technical details.
Historical data is available down to 1 second bars. Historical data availability varies by currency. Up to 100
45
daily requests can be placed for free. Consult their pricing if you require more.
Instruments and exchanges must have CNPID value setup in security and exchangedatabase tables.
The relevant properties for the CoinAPI adapter are defined inside the file conf-cnp.properties where they
can be changed. Alternatively the properties can be changed via Section 2.3, “VM Arguments”:
#{"type":"String","label":"API Key"}
cnp.apiKey = XXXXXXXXXXXXXXXXX
#{"type":"Integer","label":"API-level constant'}
cnp.scale = 5
Unique apiKey and apiSecret settings must be set to the actual values (either in the properties file or by
setting a VM argument)
43
https://www.coinapi.io/
44
https://docs.coinapi.io/
45
https://www.coinapi.io/pricing
213
CONFIDENTIAL Coinigy
22.28. Coinigy
Coinigy provides connectivity to 45+ of most popular cryptocurrency exchanges allowing to trade hundreds of
different crypto currencies. The Coinigy Interface connects to the Coinigy API endpoints via REST and Socket
Cluster protocols.
The Coinigy interface supports Market Data, Order Processing, Retrieval of account information as well as
Reference Data.
• Only Limit and Stop Limit orders are supported (margin and exchange trading)
• Partial fills are not reported due to current limitations of certain exchanges with regards to partial fills
• For some exchanges order status updates are not immediately available
Coinigy uses the columns Security CNGID and Exchange CNGID to identify an instrument.
46
For further details on the Coinigy interface please visit the Coinigy API Documentation
47
• Sign-up for a Coinigy account on Coinigy Sign up
48
• Enable two factor authentication (2FA) on the account following the 2FA Instructions
• In the API accounts settings add the API keys from all of the exchanges where an account is setup according
49
to these Instructions
• In the account preferences generate a new Coinigy API key and Secret Key set it inside conf-
cng.properties
• In the account preferences click the button 'Click to reveal my Private Channel ID (WebSocket API)' and set
the Private Channel ID inside conf-cng.properties
The relevant properties for the Coinigy adapter are defined inside the fileconf-cng.properties where they
can be changed. Alternatively the properties can be changed via Section 2.3, “VM Arguments”:
46
https://coinigy.docs.apiary.io
47
https://www.coinigy.com/auth/signup
48
https://support.coinigy.com/hc/en-us/articles/360001134694-How-do-I-enable-two-factor-authentication-2FA-on-my-account-
49
https://support.coinigy.com/hc/en-us/articles/360001137714-How-do-I-add-an-exchange-s-API-Key-to-Coinigy-
214
CONFIDENTIAL CoinMarketCap
cng.wssPrivateChannel = XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
#{"type":"String","label":"API Key"}
cng.apiKey = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
#{"type":"String","label":"API Secret"}
cng.apiSecret = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
In order to populate the database with Coinigy Accounts, Exchanges, Security Families and Securities run
the ReferenceDataStarter with cNGReferenceData spring profile enabled and program argument: all. For
further details please visit Chapter 20, Reference Data.
22.29. CoinMarketCap
50
CoinMarketCap - Cryptocurrency Market Capitalizations is a website providing information about all existing
crypto currencies and exchanges. The CoinMarketCap interface connects to the website via HTML and REST
51
API .
The CoinMarketCap interface provides the publicly available daily historical data and reference data for all
listed crypto currencies. No account is necessary in order to use the CoinMarketCap adapter.
50
https://coinmarketcap.com/
51
https://coinmarketcap.com/api/
215
Chapter 23. CONFIDENTIAL
Execution Algos
23.1. Existing Execution Algos
AlgoTrader provides several built-in Execution Algos.
SlicingOrder
The Slicing Algo is mostly recommended for Equities and derivatives. For cryptocurrencies, consider the
TWAP or VWAP algos.
Splits an order into several child orders. child order quantities and time in the market are randomized. The
SlicingOrder has the following order properties:
The quantity of each child order is randomized between minVolPct and maxVolPct of the current
volume offered at the exchange. In addition minQuantity and maxQuantity restriction can be imposed.
If maxVolPct is zero, then the current market volume will not be considered when sizing the order. If
maxQuantity is zero, then no maximum quantity will be enforced on top of the market volume restriction.
The SlicingOrder will make sure that the remainingQty for the next child order is greater than
minQuantity. Maximum quantity rules have precedence over minimum quantity rules.
Example:
minVolPct: 25%, minQuantity: 20, maxVolPct: 100%, maxQuantity: 100, BUY order, quantity: 40, vol
ask: 10
Each order will stay in the market for minDuration to maxDuration seconds (if it is not filled before that).
Between each child order there will be a random delay of minDelay to maxDelay seconds. In addition, the
216
CONFIDENTIAL Existing Execution Algos
SlicingOrder has a sophisticated pricing logic. For a BUY order the first child order will be place 1 tick
below the Ask. For a SELL order the first tick will be placed one tick above the Bid. Depending on whether
a child order gets filled, the price of the next child order is adjusted. If a child order gets filled, the price of
the next child order will be reduced by one tick (for BUY orders) but it will always be at least one tick above
the Bid. If the child order does not get filled, the price of the next child order is increased by one tick (for
BUY orders) but it will never be higher than the ask. A SlicingOrder can be created and sent as follows :
getOrderService().sendOrder(order);
Alternatively Section 17.1.1, “Order Preferences” can be used to create a SlicingOrder. The AlgoTrader
sample data contains an OrderPreference named SLICING (with the default values shown in the table
above) which allows placing a SlicingOrder as follows:
getOrderService().sendOrder(order);
VWAPOrder
1
The VWAPOrder seeks to achieve the Volume-Weighted Average price (VWAP) . VWAP is a trading
benchmark used by many institutional investors. VWAP is calculated by adding up the market value
traded for every transaction (price multiplied by number of contracts traded) and then dividing by the total
contract traded. The VWAPOrder is based on the AdaptiveOrder (see) below and uses its pricing logic.
The VWAPOrder has the following order properties in addition to the ones defined by the AdaptiveOrder.
1
https://en.wikipedia.org/wiki/Volume-weighted_average_price
217
CONFIDENTIAL Existing Execution Algos
The VWAPOrder retrieves historical prices for the number of days specified in the lookbackPeriod
parameter and splits the trading day into buckets with a length in minutes according to the bucketSize
parameter.
When a VWAPOrder is either fully-executed or cancelled a message containing the average price, the
benchmark price as well as the execution duration and number of executions is logged to the console.
For the VWAPOrder to work a historical data adapter will need to be enabled, see Section 7.2.7, “Historical
Data Service”.
getOrderService().sendOrder(order);
Alternatively Section 17.1.1, “Order Preferences” can be used to create a VWAPOrder. The AlgoTrader
sample data contains an OrderPreference named VWAP (with the default values shown in the table above)
which allows placing a VWAPOrder as follows:
218
CONFIDENTIAL Existing Execution Algos
order.setSide(Side.BUY);
getOrderService().sendOrder(order);
TWAPOrder
2
The TWAPOrder seeks to achieve the Time-Weighted Average price (TWAP) . TWAP is a trading
benchmark used by many institutional investors. TWAP is derived by calculating the average execution
price over a certain time period irrespective of the executed quantity.
The TWAPOrder is based on the AdaptiveOrder (see) below and uses its pricing logic. The TWAPOrder has
no additional order properties in addition to the ones defined by the AdaptiveOrder.
When a TWAPOrder is either fully-executed or cancelled a message containing the average price, the
benchmark price as well as the execution duration and number of executions is logged to the console.
As the reporting functionality needs historical data a historical data adapter will need to be enabled, see
Section 7.2.7, “Historical Data Service”. The TWAPOrder can still be used without historical data but not
report will be logged to the console.
getOrderService().sendOrder(order);
Alternatively Section 17.1.1, “Order Preferences” can be used to create a TWAPOrder. The AlgoTrader
sample data contains an OrderPreference named TWAP (with the default values shown in the table above)
which allows placing a TWAPOrder as follows:
2
https://en.wikipedia.org/wiki/Time-weighted_average_price
219
CONFIDENTIAL Existing Execution Algos
order.setStrategy(strategy);
order.setSecurity(security);
order.setQuantity(orderQuantity);
order.setSide(Side.BUY);
getOrderService().sendOrder(order);
AdaptiveOrder
The AdaptiveOrder is the parent class of the VWAPOrder and TWAPOrder and defines the pricing logic for
those. However it is not possible to send an AdaptiveOrder directly. The AdaptiveOrder has the following
order properties
The AdaptiveOrder uses a pricing logic similar to the Slicing Execution Algo.
The first child order will be placed at the initialOffset between the Bid and the Ask (e.g. at 80%).
Depending on whether a child order gets filled, the price of the next child order is adjusted. If the previous
child order got fully or partially filled, the price of the next child order will be reduced by increment % of
the spread. If the previous child order did not get filled, the price of the next child order is increased by
increment % of the spread. The limit price will always be adjusted within the following price range:
The AdaptiveOrder will execute over a predefined time period which can be set by two of the following
arguments: startTime, endTime or duration.
220
CONFIDENTIAL Existing Execution Algos
In case a child order is not fully executed it will get cancelled after the following period of time:
Calculated child order quantities respect the optional minSliceQty. In addition, the AdaptiveOrder also
respects the optional property maxVolPct which will cause to algo not to place child orders larger than the
current VolAsk (for Buy orders) or VolBid (for Sell orders).
As the Algo needs to be execute within a predefined time period the child order quantities are adjusted
throughout the order execution. Quantity adjustments take into consideration previously executed quantity
in order to fully executed the algo within its time constraints. No further quantity adjustments take place
once 90% of the order execution time has passed.
TargetPositionOrder
The TargetPositionOrder seeks to bring the actual position to an intended target quantity. The
TargetPositionOrder starts off by looking up the actual position quantity, calculating the delta between
the actual and target quantity and issuing a market order to fill the difference. In many cases the
TargetPositionOrder differs little from sending a simple market order. Orders can take some time to fully
execute. In the meantime the target position may change. The target quantity of a TargetPositionOrder
can be altered at any point of time which will cause the order to re-evaluate its actual state and cancel or
modify currently pending order and issue a new order if necessary to match the expected target position.
The order also reacts intelligently to stray fills that can occur.
By default TargetPositionOrder is considered to be fully executed once its target position has been
reached. The order is then removed from the order book. Often however strategies might want to maintain
a particular position over a longer period of time. TargetPositionOrder can be issued with keepAlive
attribute set to true to make the order active until explicitly canceled. The order will transition into
Status#TARGET_REACHED state once fully executed and will stay there until the target is adjusted or the
order is canceled. A TargetPositionOrder can be created and sent as follows :
221
CONFIDENTIAL Existing Execution Algos
order.setTarget(BigDecimal.valueOf(111.1));
getOrderService().sendOrder(order);
TrailingLimitOrder
A TrailingLimitOrder submits an order directly to the exchange with a limit price set a fixed distance
away from the current market price. The limit price is adjusted relative to the market price when the market
moves in favor of the order. The TrailingLimitOrder is typically used when entering a position on an
instrument with a Bullish view.
For a BUY order the limit price will be set a specific amount (defined by the trailingAmount parameter)
below the current market price. In case the market price rises, the limit price is increased once the specified
minimum amount (defined by the increment parameter) is exceeded. If the market price falls, the limit
price stays untouched. If the market price falls below the limit price the order will get filled by the exchange
(depending on adequate liquidity).
For a SELL order the limit price will be set a specific amount (defined by the trailingAmount parameter)
above the current market price. In case the market price falls, the limit price is decreased once the specified
minimum amount (defined by the increment parameter) is exceeded. If the market price rises, the limit
price stays untouched. If the market price rises above the limit price the order will get filled by the exchange
(depending on adequate liquidity).
TickwiseIncrementalOrder
Sends the entire Order as one Slice. The price of the order is set according to the current market price.
The order is first set at startOffsetTicks ticks above the market (for BUY orders). If the order is not
filled within a defined period of time, the price of the order is increased by one tick up to a maximum of
endOffsetTicks ticks below the other side of the market.
VariableIncrementalOrder
Sends the entire Order as one Slice. The price of the order is set according to the current market price. The
order is first set at startOffsetPct (% of the spread) above the market (for BUY orders). If the order is
222
CONFIDENTIAL Existing Execution Algos
not filled within a defined period of time, the price of the order is increased by increment (%of the spread)
up to a maximum of endOffsetPct (% of the spread) below the other side of the market.
223
Chapter 24. CONFIDENTIAL
AlgoTrader supports Synthetic Securities & Derivative Spreads based on the two Entities Combination and
Component.
Combinations are handled like every other Security. A Combination consists of one or many Components.
Each component has a quantity.
For synthetic / non-tradable combinations the AlgoTrader Server generates Ticks based on the size of the
components of the combination and the current market values of the associated securities. This calculation is
handled by the module module-combination.epl which provides the Component Window.
Note
On executions AlgoTrader will create fills for each component and for the combination itself. As
a consequence there will be positions on all components as well as the combination itself.
224
CONFIDENTIAL Combination Example
A Combination is available to all strategies and can be subscribed/unsubscribed in the usual manner.
The example above shows a Combination based on 11 ES Mini September 2018 Futures and 3 ES Mini
December 2018 Futures. The example shows that the market price of the combination is based on the total
prices of both components, e.g. for the ask price:
getSubscriptionService().subscribeMarketDataEvent(strategyName, combination.getId());
getCombinationService().setComponentQuantity(
combinationSecurityId, componentSecurityId, quantity);
225
CONFIDENTIAL Remove a Component
getCombinationService().addComponentQuantity(
combinationSecurityId, componentSecurityId, quantity);
Important
If Components are modified directly in the database, it is necessary to clear the cache as
well as to call the method ServerManagementService.resetComponentWindow immediately
afterwards. If this is not done within a short period of time this might lead to miss-pricing of the
corresponding Cominbation. It is therefore preferable to modify Components via the AlgoTrader
Client or the CombinationService.
getCombinationService().removeComponent(combinationSecurityId, componentSecurityId);
226
Chapter 25. CONFIDENTIAL
Spring Services
25.1. Starter Classes
AlgoTrader provides the following starter classes to start up the system for the various operational modes
When downloading reference data (see Chapter 20, Reference Data) you need to have the following profiles
active: singleDataSource or pooledDataSource and the profile of the adapter you want to get reference
data from (see table below).
You can only have one adapter reference data profile enabled at a time.
When downloading historical data (see Section 19.3, “Historical Data Download”) you need to have the
following profiles active: singleDataSource or pooledDataSource, influxDB and the profile of the adapter
you want to get historical data from (see table below).
You can only have one adapter historical data profile enabled at a time.
• Simulation Starter
To run a back-test (see Chapter 5, Strategy Backtesting), you need to have the following profiles active: any
dataSource profile (although embeddedDataSource is recommended), simulation if you're back testing
with CSV files. If you are using InfluxDB for back testing, you also need to add influxDB.
When running strategy in embedded mode (see Section 3.2.1, “Embedded Mode”), you need to have the
following profiles activate: singleDataSource or pooledDataSource, live, embeddedBroker and html5 (if
you want to see/use the UI) and the market data, trading profiles.
If account data (see Chapter 21, Account Data) is required you also need the account profile (see table
below).
The system can be run with several market data, trading and account profiles in the same process.
If historical data (see Chapter 19, Historical Data) is required you also need one historicalData profile (see
table below) and influxDB. If you do not have a historical data provider but still want to store and retrieve
historical data using InfluxDB, you need to set noopHistoricalData in addition to influxDB.
• Server Starters
When running the AlgoTrader server in distributed mode (see Section 3.2.2, “Distributed Mode”), you need
to have the following profiles activate: singleDataSource or pooledDataSource, live, embeddedBroker
and html5 (if you want to see/use the UI) and the market data, trading profiles.
227
CONFIDENTIAL Spring Profiles
If account data (see Chapter 21, Account Data) is required you also need the account profile (see table
below).
The system can be run with several market data, trading and account profiles in the same process.
If historical data (see Chapter 19, Historical Data) is required you also need one historicalData profile (see
table below) and influxDB. If you do not have a historical data provider but still want to store and retrieve
historical data using InfluxDB, you need to set noopHistoricalData in addition to influxDB.
• Strategy Starters
For strategies running in distributed mode (see Section 3.2.2, “Distributed Mode”), it is enough to activate
live. However, if additional services are required by the strategy running in distributed mode, 'special'
marker spring profiles must be specified. Strategy doesn't know which real services are running on the
server, so it needs to specify generic profiles - historicalData, referenceData and account in order to use
relevant services (HistoricalDataService, ReferenceDataService and AccountService), otherwise obtaining
a reference of any of the above mentioned services will result in NullPointerException
General Profiles
• simulation: Contains Spring Beans that are used for Back Tests or when using the Exchange Simulator,
e.g. SimulationExecutor, SimulationOrderService and ResetService
• live (client side): Contains Spring Beans needed by Strategies in Live Trading: Esper Engine,
LifecycleManager, CacheManager & LookupService
• live (server side) : Contains Spring Beans needed by the Server in Live Trading mode: e.g. Esper Engine
• noopHistoricalData: a no-operation HistoricalDataService. This profile is need for cases where you
want to store/retrieve historical data in InfluxDB but no historical data adapter is active.
• embeddedBroker: embedded ActiveMQ broker, which is required for sending messages to the UI and to
strategies running in distributed mode
228
CONFIDENTIAL Spring Profiles
• pooledDataSource: c3p0 Pooled Data Source (typically used in live trading both in embedded and
distributed mode)
• singleDataSource: Spring Driver Manager Data Source (typically used by the Reference Data Starter and
Historical Data Starter)
• embeddedDataSource: H2 embedded in-memory Data Source (typically used when performing back tests)
Adapters
EzeSoft / rTFix
RealTick
Fortex fTXFix fTXMarketData
Quandl qDLHistoricalData
QuantHouse qHMarketData
SocGen sGFix
229
CONFIDENTIAL Spring Profiles
CoinMarketCap cMCHistoricalData
cMCReferenceData
All other services not mentioned above are active in all profiles.
-Dspring.profiles.active=iBMarketData,iBNative
230
Chapter 26. CONFIDENTIAL
• Dataset Configuration
• Simulation Settings
• Reporting Settings
• RMI Settings
• ActiveMQ Settings
• Jetty Settings
conf-core.properties contains settings that are only used by the core project:
• Esper Statements
• Hedging Settings
• ActiveMQ Settings
• Jetty Settings
• SSL Settings
• Mail Settings
In addition Broker Interfaces may have their own settings file. e.g.conf-ib.properties for IB and conf-
bb.properties for BB.
Configuration parameters can be changed inside the .properties files. As an alternative configuration
parameters can be provided as VM arguments in which case they will overwrite existing parameters inside
*.properties files.
231
CONFIDENTIAL Esper Variables
-Dstatement.closePosition=false
Most configuration parameters are prefixed with a namespace (e.g. dataSource, simulation, statement, misc,
etc.)
Note
232
Chapter 27. CONFIDENTIAL
SSL security can be activated through the following property in conf.properties. Alternatively the properties
can be changed via Section 2.3, “VM Arguments”:
By default AlgoTrader ships with a self-signed certificate. It is strongly recommended to procure a certificate
from a major CA (certification authority) trusted by common browsers. As an alternative one can import the self-
signed certificate shipped with AlgoTrader into the browser. However this approach is strongly discouraged
for productive use.
To use SSL security please update the following properties in conf.properties. Alternatively the properties
can be changed via Section 2.3, “VM Arguments”:
# Keystore password
ssl.keystorePassword = password
When running with TLS transport security turned on AlgoTrader also enforces BASIC user authentication with
a user name and a password when logging into the HTML5 front-end. User credentials can be provided in
conf.properties. Alternatively the properties can be changed via Section 2.3, “VM Arguments”:
# Web UI password
jetty.password = secret
233
CONFIDENTIAL Importing Certificate into Chrome Browser
1. On the page with the untrusted certificate, click Ctrl-Shift-I to open Developer Tools and go to Security
2. Click View Certificate / Details tab > Copy to File. Choose DER encoded binary (.CER)
3. Open up Chrome Settings > Show advanced settings > HTTPS/SSL > Manage Certificates.
4. Import the exported .CER file, save into "Trusted Root Certificate Authorities"
234
Chapter 28. CONFIDENTIAL
Metrics
In Simulation Mode the performance objective of the system is high-throughput, whereas in Live Trading Mode
the objective is low latency. To pinpoint potential performance bottlenecks, AlgoTrader has a built-in metrics
functionality.
28.1. Configuration
To enable this feature:
// in simulation
<metrics-reporting enabled="true" engine-interval="-1" statement-interval="86400000" />
// in live trading
<metrics-reporting enabled="true" engine-interval="-1" statement-interval="10000" />
In Simulation Mode (if metrics are enabled) there will be a metrics report at the end of each simulation run
Note
Subscriber time consumption is not included in statement metrics, whereas static method
invocation is included.
235
Chapter 29. CONFIDENTIAL
Logging
1
AlgoTrader logging is provided by Apache Log4j 2 framework. The Logging system is configured by means
of log4j2.xml file.
-DlogLevel=ERROR
29.1. log4j2.xml
Appender Description
StdOut Logs to Standard Out
StdErr Logs to Standard Error
Appender Description
StdOut Logs to Standard Out to the console
StdErr Logs to Standard Error to the console
File Logs to an appending file
Mail Sends Email Messages on Errors
LogEvent Custom UI appender. Sends log messages to UI
Note
• Problems with the Email Appender go to System.err (on server see nohup.log)
• To prevent saturation of the logs several loggers have been defined with a logging level higher
than the root log level
1
http://logging.apache.org/log4j/2.x/
236
CONFIDENTIAL Production log4j2.xml
2
Detailed description of Log4j2 appenders and advanced configuration can be found at the Apache Logging
site.
2
http://logging.apache.org/log4j/2.x/manual/configuration.html
237
Chapter 30. CONFIDENTIAL
Reporting
AlgoTrader provides a convenient way to create custom CSV reports for strategy specific reporting. All relevant
classes are available inside the package ch.algotrader.report.
public OrderReport() {
String[] header = new String[] { "Date", "Symbol", "Quantity", "Signal" };
this.reporter = new ListReporter(Report.generateFile("OrderReport"), header);
}
public void write(Date date, String symbol, int quantity, String signal) {
this.reporter.write(date, symbol, quantity, signal);
}
}
This will create a .csv report named OrderReport.csv inside the directory /files/report/ which contains
the columns Date, Symbol, Quantity, Signal.
Some Reports are available out-of-the-box, for further details please see Section 5.5, “Performance Statistics”
238
CONFIDENTIAL
Warning
The purpose of this Strategy is to demonstrate capabilities of AlgoTrader. Do not use it with
a Live Trading Account and real Money! The strategy might lead to large losses. Even when
modifying or extending the Strategy use caution before trading it Live.
The Strategy trades the EUR.USD FX Market and is based on a simple Breakout Indicator.
The Strategy opens a long (short) position when the current price exceeds (falls below) the maximum
(minimum) of the last n bars. After a new position is opened, a profit target price is set as well as a stop loss.
If either profit target or stop is reached, the position is closed. If neither stop nor profit target is reached until
the end of n-bars, the position is closed.
Positions are sized based on a defined leverage and the current Net Liquidation Value. All Orders are placed
as Market Orders. The initial account size is EUR 1'000'000.
A.2. Example
The following 5-min bar chart gives an example of the BreakOut strategy. At 10:20 an aggregation of the last
5 bars between 09:55 and 10:20 is created, based on which the upper limit at 1113.85 and the lower limit at
1110.53 are calculated. At 10:22:37 the upper limit is crossed for the first time and a long position is entered
and both a profit target at 1116.40 and a stop loss at 1111.49 are set automatically. At 10:31:52 the profit target
is reached and the position is automatically closed.
Note
This example strategy is a good example of combining a bar based strategy with tick-by-tick
based actions. The creation of the upper and lower limits are based on the five 5-min bars but the
opening and closing of the position takes place as soon as the limits are reached without waiting
for the current bar to finish. This is one of the unique features of AlgoTrader that distinguishes
it form other trading platforms that operate exclusively based on bars.
239
CONFIDENTIAL Implementation
A.3. Implementation
The main artifacts needed for the Implementation of a new Strategy are described in Chapter 4, Strategy
Development.
The following list will give an overview of the specific artifacts implemented by the BreakOut Strategy (Note:
Most of the functionality is documented via Javadoc or Esper comments):
/src/main/java/ch/algotrader/strategy/breakOut/BreakOutService.java
The strategy service class providing the main entry method invoked by the Esper ENTRY_LONG and
ENTRY_SHORT statements:
/src/main/java/ch/algotrader/strategy/breakOut/BreakOutConfig.java
Contains all strategy configuration items
/src/main/resources/module-breakOut.epl
Esper Module containing all statements for this strategy:
• ON_BOUND_SET_TRIGGERS: sets the upperTrigger and lowerTrigger based on the minimum and
maximum of the last n bars
• ENTRY_LONG / ENTRY_SHORT: open position if last tick is higher (lower) than previous n bars.
240
CONFIDENTIAL Installation & Startup
• CLOSE_LONG_POSITION / CLOSE_SHORT_POSITION: Close position if last tick is higher (lower) than target
or lower (lower) than stop
• CLOSE_OPEN_POSITION: Close position if neither target nor stop are reached before the end of n-bars
/src/main/resources/breakOut-default.properties
Contains default parameters used by the strategy (e.g. lengthOfBar and numberOfBars)
/src/main/resources/META-INF/esper-breakOut.cfg.xml
Contains event-types definitions (i.e. CurrentValue), variables (e.g. lengthOfBar and numberOfBars) .
/src/main/resources/META-INF/applicationContext-client-breakOut.xml
Contains the Spring Bean definitions for breakOutConfigParams, breakOutConfig, breakOutEngine,
breakOutService.
/src/main/resources/db/mysql/mysql-breakout.sql
Contains the MySQL database records. Needs to be imported into the database before running the strategy
with the MySQL database.
To start the Strategy please see the explanations in Chapter 3, Starting AlgoTrader.
Git Clone
Perform a Git clone from the command line:
Import the projects breakOut into Eclipse via File / Import / Maven / Existing Maven Projects:
1
eurusd-1min-20111218-20130121.zip
to:
breakOut/files/tickdata/eurusd-1min-20111218-20130121/EURUSD.csv
To start the strategy in live trading mode on a development workstation please execute the following steps:
1
https://repo.algotrader.ch/tickdata/eurusd-1min-20111218-20130121.zip
241
CONFIDENTIAL Installation & Startup
load the strategy specific script into the MySQL database: /breakOut/src/main/resources/db/mysql/
mysql-data.sql
To start the strategy in live trading mode on a productive server please execute the following steps:
https://gitlab.algotrader.ch/general/examples/blob/master/breakOut/docker-compose.yml
docker-compose up -d
Note
Prior to starting the strategy for the very first time please start the AlgoTrader server by itself
by executing the following command inside /algotrader-launch. this will load the MySQL
sample data
242
CONFIDENTIAL
Warning
The purpose of this Strategy is to demonstrate the capabilities of AlgoTrader. Do not use it with
a Live Trading Account and real Money! Due to frequent Draw Downs, it might lead to large
losses. Even when modifying or extending the Strategy use caution before trading it Live.
The Strategy trades the EUR.USD FX Market and is based on the Stairstep Breakouts (SSBO) Indicator
1 2
that is presented on www.forexfactory.com by forexhard .
The Trading Idea behind the Strategy is the following: Markets will often stay within a trading range for
a considerable amount of time before they break-out in either direction. The following Chart shows some
examples of trading ranges.
After a break-out markets might return back into the trading range but will eventually make a major move in
one direction.
According to the defined settings, the Strategy looks for a trading range with a minimum length in Minutes (e.g.
90 Minutes) and a maximum width in Pips (e.g. 30 Pips). The chart below displays a typical trading range in
dark blue color.
1
https://www.forexfactory.com/showthread.php?t=302007
2
https://www.forexfactory.com/forexhard
243
CONFIDENTIAL Trading Idea
As soon as trading range has been built according to these parameters, the Strategy waits for the first break-
out to happen. The strategy enters the market in the direction of the breakout as soon as a small margin called
buffer (dashed red line, e.g. 5 Pips) has been crossed. In the example above, this happened at 10:48.
The Strategy will set a stop at the opposite side of the box (e.g. 1.3618 = 39 Pips) and a target with the same
distance (e.g. 1.3544 = 39 Pips).
If the target is reached, the Strategy resets itself and waits for a new Box to build itself.
If the Position gets stopped out (at the opposite side of the Box), The Strategy waits for the next break-out to
happen (on the same Box) and enters the market again after the buffer-line has been crossed. This time the
size of the position is doubled in order to cover the losses of the first entry, in case the target is reached this
time around. Position size is doubled up to a defined maximum. Because of this doubling the system can be
3
categorized as a Martingale Strategy (see Martingale Betting System ).
The following State Chart Diagram depicts the different states the Strategy will pass through:
3
https://en.wikipedia.org/wiki/Martingale_%28betting_system%29
244
CONFIDENTIAL Trading Idea
The default setting of the Strategy will go up to level 5 which will result in a position size of 16 times the original
size. So the individual sizes on the different levels will be: 1, 2, 4, 8 & 16. Each successful series will therefor
present a profit of 1 unit. Very often series will be successful on a level that is below the maximum level (e.g.
below level 5). However if the Strategy has a loosing set, which will be terminated at the maximum level (e.g.
at level 5), there will be a loss of 16 times the original position size.
The Strategy will often have multiple successful series in a row before having one major draw down. A typical
performance chart will therefore look like this:
To prevent having open positions over the weekend the Strategy does not create any new boxes after a defined
time on Friday (e.g. 4PM). Also, it will terminate a potential ongoing series at a defined time on Friday (e.g.
10PM)
245
CONFIDENTIAL Implementation
B.2. Implementation
The main artifacts needed for the Implementation of a new Strategy are described in Chapter 4, Strategy
Development.
The following list will give an overview of the specific artifacts implemented by the Box Strategy. Most of the
functionality is documented via Javadoc or Esper comments:
/src/main/java/ch/algotrader/strategy/box/BoxService.java
The strategy service class providing the main methods invoked by different Esper statements.
/src/main/java/ch/algotrader/strategy/box/BoxConfig.java
Contains all strategy configuration items
/src/main/java/ch/algotrader/strategy/box/Box.java
A POJO class representing all properties of a Box (e.g. top, bottom, startDateTime and endDateTime)
/src/main/java/ch/algotrader/strategy/box/State.java
A Java Enum representing the different States the Strategy can pass through (INIT, CREATED, LONG, SHORT,
FLAT)
/src/main/resources/module-box-init.epl
Esper Module containing statements for capturing market data, creating variables and creating Boxes. In
Live Trading, these statements will be deployed before the pre feeding.
/src/main/resources/module-box-run.epl
Esper Module containing statements that invoke the business actions on the BoxService (entry,
takeProfit, closePosition, reverse and terminateSeries). In Live Trading these statements will be
deployed after pre-feeding is finished.
/src/main/resources/box-default.properties
Contains parameters used by the strategy (e.g. boxLength and boxRange)
/src/main/resources/META-INF/esper-box.cfg.xml
Contains event-types definitions (i.e. CurrentValue), imports (i.e. Box and State), variables (e.g.
boxLength and boxRange)
/src/main/resources/META-INF/applicationContext-client-box.xml
Contains the Spring Bean definitions for boxConfigParams, boxConfig, boxEngine, boxService within
the Spring profile standalone .
/src/main/resources/db/mysql/mysql-box.sql
Contains the MySQL database records. Needs to be imported into the database before running the strategy
with the MySQL database.
/src/main/resources/html5
HTML5 and JavaScript files needed for the strategy custom web UI
To start the Strategy please see the explanations in Chapter 3, Starting AlgoTrader.
246
CONFIDENTIAL Strategy Monitoring
Note
It might be necessary to fully reload the browser on first startup to show the custom widget using
Ctrl + Shift + R.
Git clone
Perform a Git clone from the command line:
247
CONFIDENTIAL Installation & Startup
Import the projects box into Eclipse via File / Import / Maven / Existing Maven Projects:
box/files/bardata/eurusd-1min-20111218-20130121/EURUSD.csv
To start the strategy in live trading mode on a development workstation please execute the following steps:
load the strategy specific script into the MySQL database: /box/src/main/resources/db/mysql/mysql-
data.sql
To start the strategy in live trading mode on a productive server please execute the following steps:
https://gitlab.algotrader.ch/general/examples/blob/master/box/docker-compose.yml
docker-compose up -d
Note
Prior to starting the strategy for the very first time please start the AlgoTrader server by itself
by executing the following command inside /algotrader-launch. this will load the MySQL
sample data
4
https://repo.algotrader.ch/bardata/eurusd-1min-20111218-20130121.zip
248
CONFIDENTIAL Installation & Startup
249
CONFIDENTIAL
Warning
The purpose of this Strategy is to demonstrate capabilities of AlgoTrader. Do not use it with
a Live Trading Account and real Money! The strategy might lead to large losses. Even when
modifying or extending the Strategy use caution before trading it Live.
1
The Pairs Trading strategy uses the web service www.pairtradinglab.com to trade pairs of US equities
The strategy works by monitoring the performance of two historically correlated securities. When the correlation
between those two securities demonstrate a temporary weakness, a pairs trade can be conducted by shorting
the outperforming stock and going long on the under performing stock. Basically, one is betting that the spread
between the two will converge eventually.
1
https://www.pairtradinglab.com/
250
CONFIDENTIAL Implementation
• Verify a pair trading idea and inspect the behavior and robustness of pairs
• Search the PTL database of more than 10 million pre-analyzed U.S. market pairs using complex filters
• Create and maintain lists of interesting pairs, rate them, and tag them
Then the AlgoTrader - Pair Trading Lab integration can be used to download selected pairs and/or portfolio of
pairs from Pair Trading Lab into AlgoTrader where they can then be traded automatically
2
The AlgoTrader based pairs trading strategy implementation is based on the Ratio Model
C.2. Implementation
The main artifacts needed for the Implementation of a new Strategy are described in Chapter 4, Strategy
Development.
The following list will give an overview of the specific artifacts implemented by the Pairs Trading Strategy (Note:
Most of the functionality is documented via Javadoc or Esper comments):
/src/main/java/ch/algotrader/strategy/pairstrading/service/PairsTradingService.java
The strategy service class providing the main trading logic
/src/main/java/ch/algotrader/strategy/pairstrading/csv/CsvImporter.java
Import utility to download pairs from Pair Trading Lab and configure them in AlgoTrader
/src/main/java/ch/algotrader/strategy/pairstrading/util/PairsTradingConfig.java
Contains all strategy configuration items
/src/main/java/ch/algotrader/strategy/pairstrading/util/PairsTradingCalc.java
Contains the logic of the ratio model
/src/main/resources/module-pairstrading.epl
Esper Module containing all statements for this strategy:
2
https://wiki.pairtradinglab.com/wiki/Pair_Trading_Models#Ratio_Model
251
CONFIDENTIAL Installation & Startup
• UPDATE_HISTORICAL_BARS & DAILY_RECALC: daily triggers for downloading historical data and updating
entry thresholds
/src/main/resources/conf-pairstrading.properties
Contains default parameters used by the strategy
/src/main/resources/META-INF/esper-pairstrading.cfg.xml
Contains event-types definitions (i.e. PairEvent and SignalEvent)
/src/main/resources/META-INF/applicationContext-client-pairstrading.xml
Contains pairsTradingParams, pairsTradingConfig, pairsTradingEngine, pairsTradingService
as well as the strategy specific beans csvImporter, orderSubmissionService and
pairsTradingLabNavigator.
/src/main/resources/db/h2/h2-pairstrading.sql
Contains the H2 database records needed to simulate the strategy with the embedded in-memory database
H2.
/src/main/resources/db/mysql/mysql-pairstrading.sql
Contains the MySQL database records. Needs to be imported into the database before running the strategy
with the MySQL database.
/src/main/resources/html5
HTML5 and JavaScript files needed for the strategy custom web UI
To start the Strategy please see the explanations in Chapter 3, Starting AlgoTrader.
3
https://www.pairtradinglab.com/index.php?command=register
4
https://www.pairtradinglab.com/portfolio-manager
252
CONFIDENTIAL Installation & Startup
Extract Portfolio ID
csvImportPortfolio needs to be extracted from the URL when clicking on pair in the PTL Trader / Portfolio
Manager
To start the strategy in live trading mode on a development workstation please execute the following steps:
Git Clone
Perform a Git clone from the command line:
Import the projects pairstrading into Eclipse via File / Import / Maven / Existing Maven Projects:
253
CONFIDENTIAL Strategy Monitoring
To start the strategy in live trading mode on a productive server please execute the following steps:
https://gitlab.algotrader.ch/general/examples/blob/master/pairstrading/docker-compose.yml
docker-compose up -d
Note
Prior to starting the strategy for the very first time please start the AlgoTrader server by itself
by executing the following command inside /algotrader-launch. this will load the MySQL
sample data
http://localhost:9090/pairstrading.html
254
CONFIDENTIAL Strategy Monitoring
Note
It might be necessary to fully reload the browser on first startup to show the custom widget using
Ctrl + Shift + R.
• PairInfo & Pairs: current pair definitions as downloaded from Pair Trading Lab. movingAvg and
standardDev are calculated on a daily basis (by the Esper statement UPDATE_HISTORICAL_BARS) using
historical closing prices
• Signals: intraday pair values based on live data. ratio shows the current price ratio between individual
instruments of a pair. zScore shows the current ratio relative to the Bollinger band around the ratio time
series. When the zScore hits the zScoreEntry threshold a position is entered, and when the zScore hits
the zScoreExit threshold the position is closed. If the zScore happens to be above zScoreMax (e.g. after
a large overnight gap) no new position will be opened. The signal field shows the current state of a pair
(i.e. LONG, SHORT, EXIT & HOLD)
255
CONFIDENTIAL Strategy Monitoring
• The action Import Historical Bars is used to import historical closing prices of all instruments for the
relevant look back period. This action is automatically executed once a day. In addition it can be invoked
manually at any time.
• The action Re-Calc Entry Thresholds is used to update movingAvg and standardDev based on historical
data in the database. This action is automatically executed once a day. In addition it can be invoked manually
at any time.
• The action Import Pairs imports and/or update pairs from Pair Trading Lab.
256
CONFIDENTIAL
Warning
The purpose of this Strategy is to demonstrate capabilities of AlgoTrader. Do not use it with
a Live Trading Account and real Money! The strategy might lead to large losses. Even when
modifying or extending the Strategy use caution before trading it Live.
The strategy trades US equity IPOs (initial public offering). When a new stock is launched on the exchange
for the first time the strategy tries to realize trading profits of the first trading day. New IPOs are announced on
1
web pages like IPOScoop together with an indicative open price.
1
https://www.iposcoop.com/
257
CONFIDENTIAL Strategy Monitoring
Note
It might be necessary to fully reload the browser on first startup to show the custom widget using
Ctrl + Shift + R.
Inside the custom widget the user can enter a new IPO to be traded by populating the following fields:
• Symbol to be traded
• Multiplier to be applied to the opening price for secondary orders (see below)
258
CONFIDENTIAL Implementation
For each symbol entered, the strategy will place orders at a configurable time in the morning (e.g. 4:30am).
Note
Trading of IPOs usually starts within 2-3 hours after the official market open
Definitions:
The strategy will place a limit-at-the-open order at user-defined limit price for the entry quantity specified above.
Using a limit-at-the-open order will cause the order to participate in the opening auction of the IPO.
Immediately following the open of regular trading, the strategy will check to see if the entire cash commitment
for the given symbol has been exhausted.
2. If the cash commitment has not been exhausted and the stock opened above the LIMIT PRICE, the unfilled
quantity will be cancelled, and no further action will be taken by the strategy.
3. Otherwise, if the cash commitment for the name has NOT been exhausted a secondary limit order is placed
as per below:
If the secondary order is not filled by a configurable end time (e.g. 3:30pm), it will be cancelled by the system.
Immediately after executing any buy orders, the strategy will place a market-on-close order for the entire
position.
D.3. Implementation
The main artifacts needed for the Implementation of a new Strategy are described in Chapter 4, Strategy
Development.
The following list will give an overview of the specific artifacts implemented by the IPO Strategy (Note: Most of
the functionality is documented via Javadoc or Esper comments):
/src/main/java/ch/algotrader/strategy/ipo/IPOService.java
The strategy service class providing the main trading logic
259
CONFIDENTIAL Installation & Startup
/src/main/java/ch/algotrader/strategy/ipo/IPO.java
Java POJO class representing a single IPO
/src/main/resources/module-ipo.epl
Esper Module containing all statements for this strategy:
• SEND_LIMIT_ORDERS: triggers the secondary order service once the at-the-open order has been fully
executed and the official open price (via GenericTickVO) has been disseminated. An Esper Join is used
for this since either one of those events can arrive first
• DAILY_CLEAN_UP: unsubscribes all market data and resets the list of IPOs an initial capital
/src/main/resources/conf-ipo.properties
Contains default parameters used by the strategy
/src/main/resources/META-INF/esper-ipo.cfg.xml
Contains Esper variables for the strategy
/src/main/resources/META-INF/applicationContext-client-ipo.xml
Contains ipoConfigParams, ipoEngine & ipoService.
/src/main/resources/db/mysql/mysql-ipo.sql
Contains the MySQL database records. Needs to be imported into the database before running the strategy
with the MySQL database.
/src/main/resources/html5
HTML5 and JavaScript files needed for the strategy custom web UI
To start the Strategy please see the explanations in Chapter 3, Starting AlgoTrader.
Git Clone
Perform a Git clone from the command line:
Import the projects ipo into Eclipse via File / Import / Maven / Existing Maven Projects:
260
CONFIDENTIAL Installation & Startup
To start the strategy in live trading mode on a productive server please execute the following steps:
https://gitlab.algotrader.ch/general/examples/blob/master/ipo/docker-compose.yml
docker-compose up -d
Note
Prior to starting the strategy for the very first time please start the AlgoTrader server by itself
by executing the following command inside /algotrader-launch. this will load the MySQL
sample data
261
CONFIDENTIAL
Warning
The purpose of this Strategy is to demonstrate capabilities of AlgoTrader. Do not use it with
a Live Trading Account and real Money! The strategy might lead to large losses. Even when
modifying or extending the Strategy use caution before trading it Live.
The Strategy is a simple example without Esper that trades the EUR.USD FX Market and is based on two
exponential moving averages.
The Strategy sends a BUY order when shorter moving average (e.g. 10-days) crosses above the longer moving
average (e.g. 20-days) and it sends a SELL order when shorter moving average crosses below the longer
moving average.
E.2. Implementation
The main artifacts needed for the Implementation of a new Strategy are described in Chapter 4, Strategy
Development.
262
CONFIDENTIAL Installation & Startup
1
The strategy uses the TA4J library which provides over 100 technical indicator that are computed on a
continuous basis.
/src/main/java/ch/algotrader/strategy/ema/EMAService.java
The strategy service class providing onStart and onBar method containing the trading logic
To start the Strategy please see the explanations in Chapter 3, Starting AlgoTrader.
Git Clone
Perform a Git clone from the command line:
Import the projects ema into Eclipse via File / Import / Maven / Existing Maven Projects:
ema/files/bardata/eurusd-1min-20111218-20130121/EURUSD.csv
To start the strategy in live trading mode on a development workstation please execute the following steps:
load the strategy specific script into the MySQL database: /ema/src/main/resources/db/mysql/mysql-
data.sql
1
https://github.com/mdeverdelhan/ta4j-origins
2
https://repo.algotrader.ch/bardata/eurusd-1min-20111218-20130121.zip
263
CONFIDENTIAL Installation & Startup
To start the strategy in live trading mode on a productive server please execute the following steps:
docker-compose up -d
Note
Prior to starting the strategy for the very first time please start the AlgoTrader server by itself
by executing the following command inside /algotrader-launch. this will load the MySQL
sample data
3
https://gitlab.algotrader.ch/general/examples/blob/master/breakOut/docker-compose.yml
264
CONFIDENTIAL
Warning
The purpose of this Strategy is to demonstrate capabilities of AlgoTrader. Do not use it with
a Live Trading Account and real Money! The strategy might lead to large losses. Even when
modifying or extending the Strategy use caution before trading it Live.
The Strategy is a simple example that places random orders at regular intervals. The Random strategy is used
1
for the AlgoTrader HTML5 Demo .
The Strategy sends a BUY order when shorter moving average (e.g. 10-days) crosses above the longer moving
average (e.g. 20-days) and it sends a SELL order when shorter moving average crosses below the longer
moving average.
F.2. Implementation
The main artifacts needed for the Implementation of a new Strategy are described in Chapter 4, Strategy
Development.
/src/main/java/ch/algotrader/strategy/random/RandomService.java
The strategy service class providing the main methods invoked by different Esper statements.
/src/main/java/ch/algotrader/strategy/random/RandomConfig.java
Contains all strategy configuration items
/src/main/resources/module-random.epl
Esper Module containing statements to place and cancel orders as well as update subscriptions once a day.
/src/main/resources/conf-random.properties
Contains parameters used by the strategy (e.g. positionMax and orderMax)
/src/main/resources/META-INF/esper-random.cfg.xml
Contains variables (i.e. placeOrderInterval and cancelOrderInterval)
/src/main/resources/META-INF/applicationContext-client-random.xml
Contains the Spring Bean definitions for randomConfigParams, randomConfig, randomEngine,
randomService.
1
http://html5.algotrader.ch/
265
CONFIDENTIAL Installation & Startup
/src/main/resources/db/mysql/mysql-data.sql
Contains the MySQL database records. Needs to be imported into the database before running the strategy
with the MySQL database.
To start the Strategy please see the explanations in Chapter 3, Starting AlgoTrader.
Git Clone
Perform a Git clone from the command line:
Import the projects random into Eclipse via File / Import / Maven / Existing Maven Projects:
To start the strategy in live trading mode on a development workstation please execute the following steps:
load the strategy specific scrip into the MySQL database: /random/src/main/resources/db/mysql/
mysql-data.sql
To start the strategy in live trading mode on a productive server please execute the following steps:
2
https://gitlab.algotrader.ch/general/examples/blob/master/random/docker-compose.yml
docker-compose up -d
2
https://gitlab.algotrader.ch/general/examples/blob/master/breakOut/docker-compose.yml
266
CONFIDENTIAL Installation & Startup
Note
Prior to starting the strategy for the very first time please start the AlgoTrader server by itself
by executing the following command inside /algotrader-launch. this will load the MySQL
sample data
267