AlgoTraderAlgoTrader Documentation

Appendix B. Example Strategy "Box"

The Strategy trades the EUR.USD FX Market and is based on the Stairstep Breakouts (SSBO) Indicator 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.


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 categorized as a Martingale Strategy (see Martingale Betting System).

The following State Chart Diagram depicts the different states the Strategy will pass through:


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)

The main artifacts needed for the Implementation of a new Strategy are described in chapter Chapter 5, 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 Chapter 4, Starting AlgoTrader.

The Box strategy is equipped with an HTML5 custom widget that displays current metrics like State, Units, Upper Target, etc. The custom widget also contains a button to terminate the current series


Note

It might be necessary to fully reload the browser on first startup to show the custom widget using Ctrl + Shift + R.

To setup the strategy for back testing and live trading on a development workstation please execute the following steps:

To start the strategy in live trading mode on a development workstation please execute the following steps:

Initialize the database

load the db-samples script into the MySql database: /algotrader-core/src/main/resources/db-samples/mysql/mysql-data.sql

load the strategy specific script into the MySql database: /box/src/main/resources/db/mysql/mysql-data.sql

Start the Strategy

invoke the Eclipse Launch Configuration: EmbeddedStarter-box

To start the strategy in live trading mode on a productive server please execute the following steps:

Copy docker compose file

Copy the following file to the server and make changes as needed:

https://gitlab.algotrader.ch/main/examples/blob/master/box/docker-compose.yml

Run docker compose

Invoke the following command inside the directory where the docker-compose.yml file is located:

docker-compose up -d