AlgoTraderAlgoTrader Documentation

Appendix I. Example strategy "BreakOut" in Python

This strategy effectively replicates the Java and Esper based BreakOut strategy (Appendix A, Example Strategy "BreakOut"). It is written in Python and integrates with the platform using the AlgoTrader Python Interface. The AlgoTrader Python Interface allows writing strategies in the same way as in Java, with access to the same event handler methods (e.g. onInit, onBar, onTick, onOrderStatus, etc.) and services (OrderService for order placement, AccountService for retrieval of account balances and initiation of withdrawals, etc.).

For installation instructions, see the original strategy description: Section A.4, “Installation & Startup”. You may use only a subset of historical data from the EURUSD csv file (by e.g. only keeping say 500 first rows) to be able to run backtests during development faster.

The strategy can be written and run in both Python 2 and 3. The AlgoTrader Python Interface works with both Python versions and has been tested with Python 2.7 and 3.7. Both Python versions and the AlgoTrader Python Interface should already be installed in your environment if you are using an AWS instance running an AlgoTrader trial image. If you have a different set up, you need to install the Python package (Section 2.1.4, “Python installation”).

Running breakOut-python/breakout-python-strategy.py in Python starts the strategy execution. THe AlgoTrader Python Interface the strategy uses waits for AlgoTrader to be started if it is not started already.

AlgoTrader (running in Java) can be started using the EmbeddedStrategyStarter (for live trading, or live trading against an exchange simulator, Section 5.1, “Exchange Simulator”) or using the SimulationStarter for strategy backtesting on historical data (see Section 3.1, “Simulation Mode”). The spring profile pythonIntegration needs to be activated with those. You can use the Eclipse runners EmbeddedStrategyStarter-breakOut-python.launch and SimulationStarter-simulate-breakOut-python.launch in the /breakOut-python folder for that.

Detailed information on implementing strategies in Python can be found in Section 4.5, “Strategy Development in Python”.

BreakoutStrategyService

The strategy implementation extends the StrategyService class from the AlgoTrader Python Interface (algotrader_com package). The method _entry contains the same logic as the entry method in Java version of the BreakOutService (Appendix A, Example Strategy "BreakOut").

module-breakOut.epl

The business logic is written in Esper statements that are identical to the original Java based strategy. The only changes are in the ENTRY_LONG, ENTRY_SHORT statements that invoke not a subscriber on a Java based Spring bean, but pass the values to the Python strategy's on_esper_subscriber_invocation method using @Subscriber(className='pythonStrategyService#onEsperSubscriberInvocation')

connect_to_algotrader

This function is used to pass a strategy implementation (a class extending StrategyService) to the AlgoTrader Python Interface.

wait_for_algotrader_to_disconnect

This function is used to prevent the Python script from finishing.