AlgoTraderAlgoTrader Documentation

AlgoTrader Quick Start Guide

Algorithmic Trading Software


The AlgoTrader Quick Start Guide is based on the AlgoTrader 30-day trial provided via Amazon AWS which can be requested here.

The AlgoTrader 30-day trial version includes a fully functional AlgoTrader installation as well as the following example strategies:

The AlgoTrader 30-day trial version contains the following pre-installed components:

  • Java JDK

  • AlgoTrader Server

  • AlgoTrader Eclipse IDE

  • MySql Database

  • InfluxDB Database

  • Toad for MySql Freeware

  • InteractiveBrokers Gateway

  • TortoiseGit

  • Git for Windows

  • Notepad++

  • Google Chrome

  • MS Excel (not activated)

Warning

Amazon AWS usage cost will apply based on the instance type select. For further details, please visit: https://aws.amazon.com/ec2/pricing/

Warning

It is prohibited to reverse engineer, decompile, disassemble, or copy any parts of the AlgoTrader 30-day trial

The following steps will guide through the installation of a Windows AWS Instance containing the AlgoTrader 30-day trial version

  1. Open the Amazon AWS console:

    https://console.aws.amazon.com/

    and Login using the Amazon username and password.

  2. In the upper right-hand corner of the screen make sure the N. Virginia Region is selected:

  3. Select EC2

  4. Click Launch Instance in the middle of the screen

  5. In the menu on the left-hand side select My AMIs and check Shared with me

  6. Select the AlgoTrader-x.x.x-Trial-WIN-xxxx.xx.xx

    Note

    The AlgoTrader-x.x.x-Trial-WIN-xxxx.xx.xx image is only available in the N. Virginia Region but not in any other Regions. It is thus necessary that the N. Virginia Region is selected in the top-right corner of the screen

  7. On the next screen select the Instance Type. We recommend at least instance type t2.medium, ideally instance type m4.large

    Warning

    Amazon AWS usage cost depends on the instance type selected. For further details, please visit: https://aws.amazon.com/ec2/pricing/

  8. Click Review and Launch on the bottom right of the screen

  9. Click Launch on the bottom right of the screen

  10. On the Dialog that shows select Proceed without a key pair, select I acknowledge... and click Launch Instance

  11. Click View Instances on the bottom right of the screen

  12. On the next screen, one can see the Instance starting up. Wait until it is running and note the Public IP address.

    Note

    It will take at least 4 minutes for the Instance to startup and become available.

  13. Use Microsoft Remote Desktop to connect to the instance by typing in the Public IP address that was noted in the previous step

    Note

    • You need to use Remote Desktop Connection (RDP), a Web Browser will NOT work for this

    • For more information on Remote Desktop Connections please visit: Windows, Mac and Linux

  14. Specify User name and Password that was provided in the Email after signing up for the AlgoTrader free 30-day trial.

  15. On the next dialog select Don't ask... and click Yes

  16. You are now connected to the Amazon AWS Instance containing AlgoTrader. Now copy the algotrader.license file that was attached to the Email (that was received when signing up for the AlgoTrader free 30-day trial) to C:\Users\Administrator on the Amazon Instance:

  17. Microsoft Excel is pre-installed on the machine to view the AlgoTrader Excel based Back Test Report. However due to licensing restrictions Microsoft Excel has not been activated yet. If a Microsoft Office license is available, please active Microsoft Excel using the license key. Alternatively, one can request a free 60-day trial through:

    https://www.microsoft.com/en-us/evalcenter/evaluate-office-professional-plus-2013

  18. The Amazon Instance including AlgoTrader is now ready for usage!

Note

  • Amazon Windows Instances tend to run a bit slow after they are first created. Responsiveness will however increase after some time.

  • The performance of the Amazon Instance also depends on the instance type selected in step 7.

This section gives a quick introduction on how to start a trading strategy by discussing the Box example strategy.

To start any of the other strategies please consult the relevant parts in the documentation:

First one needs to start the AlgoTrader Eclipse IDE (Integrated Development Environment) using the Eclipse icon in the task bar.

This section will give a quick introduction on how to create a trading strategy by discussion the EMA (Exponential Moving Average) Strategy

Note

The AlgoTrader 30-day free trial already contains the final EMA strategy with all artifacts.

The Strategy Wizard also generated some boiler plate code that needs to be replace with the actual logic of the EMA strategy.

AlgoTrader strategies are regular Java programs. Due to this any type of java library or add-ons can be used. The EMA strategy is based on the TA4J library which contains a collection of over 100 technical indicators.

As a first step the library needs to be configured in order to be available to the trading strategy by adding the dependency to the pom.xml file. Double click the pom.xml file to open it, go to the Dependencies Tab and click Add:

Then add the following items:

  • Group Id: eu.verdelhan

  • Artifact Id: ta4j

  • Version: 0.8

Then click OK and save the file.

Now, double click the EMAService.java file which contains the main logic of the EMA strategy.

The header of the EMAService.java is already generated and no further changes are necessary. It contains the java class name (EMAService) as well as the name of the interface it is derived from (StrategyService). Also, it contains an @Component annotation which marks the Java class as a Spring bean and automatically gets references to necessary service like the OrderService and the LookupService.

Note

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.

@Component

public class EMAService extends StrategyService {

The next part of the EMAService.java contains settings the strategy will use. Three of them are already generated by the Wizard but a few more need to be added.

private final long accountId = 1;

private final long securityId = 25;
private final long orderQuantity = 10000;
private final int emaPeriodShort = 10;
private final int emaPeriodLong = 20;
private TimeSeries series;
private DifferenceIndicator emaDifference;
  • The accountId defines the id of the account the strategy will use for trading.

  • The securityId will define the id of the instrument the strategy will trade.

  • The orderQuantity is the number of contracts the strategy will trade.

  • The emaPeriodSort is the look back period of the shorter EMA indicator.

  • The emaPeriodLong is the look back period of the longer EMA indicator.

In addition, the following two fields need to be defined:

  • The TimeSeries object used by the exponential moving average indicators

  • The DifferenceIndicator which will contain the different between the short and the long EMA

Next, the Java Constructor for the EMAService class needs to be created:

public EMAService() {


    setStrategyName("EMA");
    this.series = new TimeSeries(Period.minutes(1));
    this.series.setMaximumTickCount(this.emaPeriodLong);
    ClosePriceIndicator closePriceIndicator = new ClosePriceIndicator(this.series);
    EMAIndicator emaShort = new EMAIndicator(closePriceIndicator, this.emaPeriodShort);
    EMAIndicator emaLong = new EMAIndicator(closePriceIndicator, this.emaPeriodLong);
    this.emaDifference = new DifferenceIndicator(emaShort, emaLong);
}
  • First the EMAService constructor sets the name of the Strategy used during the back test.

  • Next the TimeSeries object is initialized to a length of one Bar. In addition, the number of bars the Time Series is set (in this case 20 Bars).

  • Next a ClosePriceIndicator is created which causes the system to look at closing prices of Bar events.

  • Then both the short and the long EMA indicator need to be created by associating them with the ClosePriceIndicator and setting the lookback period (in this case 10 and 20).

  • Last the DifferenceIndicator needs to be created which contains the difference between the sort EMA and the long EMA indicator.

Next, the onStart (an AlgoTrader Live Cycle Method) method needs to be created which will be called once the strategy starts up.

@Override

public void onStart(final LifecycleEventVO event) {
    getSubscriptionService().subscribeMarketDataEvent(getStrategyName(), this.securityId);
}

For further details please visit the AlgoTrader documentation regarding Life Cycle Events.

The onStart methods calls subscribeMarketDataEvent of the SubscriptionService by passing the strategyName and the securityId of the instrument the strategy wants to receive market data for. The SubscriptionService is automatically made available to the strategy through Spring Auto Wiring.

Next, the onBar method needs to be created which will be invoked on every incoming Bar:

@Override

public void onBar(BarVO bar) {
    addBar(bar);
    int i = this.series.getEnd();
    Decimal currentValue = this.emaDifference.getValue(i);
    Decimal previousValue = this.emaDifference.getValue(- 1);
    if (currentValue.isPositive() && previousValue.isNegativeOrZero()) {
        sendOrder(Side.BUY);
    } else if (currentValue.isNegative() && previousValue.isPositiveOrZero()) {
        sendOrder(Side.SELL);
    }
}
  • The method first calls the addBar method which will add the incoming Bar to the Time Series defined above

  • Next, the index i of the last element of the Time Series is retrieved

  • Then the value of the last and the second-last element of the DifferenceIndicator is retrieved

Then the actual trading rules need to be defined:

  • If the current value of the DifferenceIndicator is positive and the previous value was negative or zero a BUY order is sent. In other words, if the short EMA crossed above the long EMA a BUY order is sent.

  • If the current value of the DifferenceIndicator is negative and the previous value was positive or zero a SELL order is sent. In other words, if the short EMA crossed below the long EMA a SELL order is sent.

The trading logic is depicted in the following chart also.

The addBar method simply converts AlgoTrader BarVO events into TA4j objects and adds them to the Time Series

private void addBar(BarVO bar) {


    Tick tick = new Tick(new DateTime(bar.getDateTime().getTime()),
            bar.getOpen().doubleValue(),
            bar.getHigh().doubleValue(),
            bar.getLow().doubleValue(),
            bar.getClose().doubleValue(),
            bar.getVol().doubleValue());
    this.series.addTick(tick);
}

As the last item, the sendOrder method needs to be created which will take care of constructing an order object and handing it over to the OrderService:

private void sendOrder(Side side) {


    MarketOrderVO order = MarketOrderVOBuilder.create()
            .setStrategyId(getStrategy().getId())
            .setAccountId(this.accountId)
            .setSecurityId(this.securityId)
            .setQuantity(new BigDecimal(this.orderQuantity))
            .setSide(side)
            .build();
    getOrderService().sendOrder(order);
}

The sendOrder method creates a MarketOrder by using the MarketOrderVOBuilder and assigns the strategyId, the accountId, the securityId, the orderQuantity, the order side (BUY or SELL) and finally calls build to create the MarketOrder object. The order object is then handed over to the OrderService which will execute the order. The OrderService is automatically made available to the strategy through Spring Auto Wiring.

For further details on how order are please visit the AlgoTrader documentation regarding Order Management

The implementation of the trading strategy is now finished a first back test can be started according to Chapter 3, Starting a Trading Strategy.

The EMA strategy is an example strategy based on Java code only. For details on how to build a trading strategy using Esper please visit the AlgoTrader documentation regarding Strategy Development

During live trading, all relevant information like orders, positions and transactions are stored in the MySql database.

To view database data please open TOAD for MySql.

On the left-hand side of the application double click on root@localhost (algotrader).

You now see all AlgoTrader tables listed below

To view the contents of a table, double-click its name and go to the Data tab

The AlgoTrader 30-day trial version can also be used to trade Bitcoin and other Cryptocurrencies via Coinigy

To setup a connection to Coinigy the following steps have to be taken:

  • Sign-up for a Coinigy account on Coinigy Sign up

  • 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 to these Instructions

  • In the account preferences generate a new Coinigy API key and Secret Key and use it in the settings below

  • In the account preferences click the button 'Click to reveal my Private Channel ID (Websocket API)' and use it in the settings below

First the Coinigy API key, Secret Key and Private Channel ID noted above needs to be added by clicking the downward facing arrow next to the green start icon and then selecting Run Configurations.

Then update both the EmbeddedStrategyStarter-crypto and the ReferenceDataStarter by selecting them on the left-hand side and going to the Arguments Tab

Here the Coinigy API key, Secret Key and Private Channel ID noted above needs to be added. Then click Apply.

In order to populate the database with Coinigy Accounts, Exchanges, Security Families and Securities select the ReferenceDataStarter and click Run.

Now the strategy can be started by selecting the EmbeddedStrategyStarter-crypto and click Run.

This will start the strategy and subscribe to the BTC.USD pairs on the following three exchanges:

  • BITFINEX

  • BTC-E

  • POLONIEX

Other than that the strategy has no further logic and it is left up to the user to add trading rules according to Chapter 4, Creating a Trading Strategy.

You can shut down the system by clicking on the Start Menu in the lower left-hand corner of the Windows Desktop and then select Power Options in the upper right-hand corner:

Alternatively, the system can be shutdown via the Amazon AWS Console https://console.aws.amazon.com/ by first selecting the Amazon Instance and the under Actions select Instance State and then either Stop or Terminate

Note

  • If Stop is clicked the instance can be restarted at a later point in time. In the stopped state the Amazon Instance will still incur disc space using costs as mentioned in: https://aws.amazon.com/ec2/pricing/

  • If Terminate is clicked the instance cannot be restarted. In the terminated state, no further Amazon instance costs apply