AlgoTraderAlgoTrader Documentation

Appendix H. Example strategy "EMA" in Python

This strategy effectively replicates the Java based EMA strategy (Appendix E, Example Strategy "EMA"). 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 E.3, “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 ema-python/ 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-ema-python.launch and SimulationStarter-simulate-ema-python.launch in /ema-python folder for that.

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


The strategy implementation extends the StrategyService class from the AlgoTrader Python Interface (algotrader_com package). Overridden methods (on_bar, on_init, etc.) contain the same logic as the overridden methods (onBar, onInit, etc.) in the Java version of the EMAService (Appendix E, Example Strategy "EMA"). For example the overridden on_bar method implements the EMA indicator calculation and the evaluation of strategy rules based on the crossover of 2 EMA indicators with different lengths.


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


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


This function calculates the exponential moving averages. Note that using the popular Pandas library functionality would have a negative impact on performance.

The Python EMA strategy displays the profit evolution using the matplotlib library after the backtest finishes.