# Developing a short-term machine learning strategy

#### Published in Automated Trader Magazine Issue 42 Q1 2017

## Open-source software for machine learning is widely available for standard data analysis packages. We examine how a stack built on these can be used for time series prediction.

To me, there are two main methods for developing automated trading strategies: the model-based and the machine learning approach. A model-based strategy starts with a model of a market inefficiency that results from trader psychology, the economy, market microstructure or any other force affecting price. This inefficiency produces an anomaly in the price time series - a deviation from the random walk - that can sometimes be used for price or trend prediction. Examples of model-based trading methods are trend following, mean reversion, price cycles, price clusters, statistical arbitrage and seasonality.

A machine learning strategy works the other way around. It data mines price time series or other data sources, looks for patterns that repeat and attempts to fit a trading algorithm to them. The market forces behind those patterns are of no interest. The only assumption is that patterns of the past might reappear in the future. Some popular machine learning algorithms are multivariate regression, naïve Bayes, k-means clustering, support vector machines, decision trees and various kinds of neural networks, especially 'deep learning' networks.

The advantage of pure machine learning is that it does not rely on any assumptions, models or market hypotheses. The disadvantage: Machine learning algorithms tend to detect a vast number of random patterns and generate a vast amount of worthless strategies. Distinguishing real patterns - caused by real market inefficiencies - from random patterns can be a challenging task. Still, machine learning is evolving fast and will probably dominate trading strategy development within the next decade. It is time to take a closer look.

In this article I will describe the typical development process of a machine learning strategy. I will use a deep learning algorithm for trading 'price action' - predicting short-term price moves from short-term price history. With today's software tools, only about 20 lines of script code are needed for this. You can easily reproduce the strategy since all steps are described, all code is available and only open-source standard software packages - R and Zorro - are used. Zorro is used for tests and live trading, while R is needed for the deep learning algorithm.

The strategy is mainly intended as an experiment for answering two questions: Does a more complex algorithm - such as adding more neurons to an artificial brain - produce a better prediction? And can 'price action' trading work at all?

For this we will collect information from the last candles of a price time series, feed it into a deep learning neural network and use its output to predict the next candles. My working hypothesis: Price action trading is irrational since a few candles do not contain any useful predictive information. Of course, a nonpredictive outcome of the experiment will not inevitably mean that I am right since I could have used wrong parameters or prepared the data badly. But a predictive outcome would be a hint that I am wrong and price action trading can indeed be made to work.

### Machine learning principles

A machine learning algorithm is trained for predicting something.
In trading, this is usually the direction of price or the outcome
of the next trade. For this purpose it is fed with data samples,
derived in some way from historical price time series or other
data sources. Each sample consists of a number *n* of
variables *x _{1}* ,

*x*, ...,

_{2}*x*, named predictors or features. The predictors can be the price returns of the previous

_{n}*n*bars, a collection of classical indicators, any other imaginable functions of the price time series or external data such as a Twitter feed. I have even seen the pixels of a price chart image used as predictors for a trading strategy. The main requirement for predictors is that when combined they carry information sufficient to predict the outcome with some accuracy.

This outcome is represented by a target variable *y* . It
might contain the return of the next trade or the next price
movement. In a training process, the algorithm learns to predict
the target *y* from the predictors *x _{1}*
,

*x*, ...,

_{2}*x*. The learned 'memory' is stored in a data structure that we named 'model' (not to be confused with a financial model). This data structure is specific to the algorithm. So the machine learning algorithm works in two modes, in training mode for generating the model and in prediction mode for predicting the target:

_{n}