You've successfully subscribed to Alpaca Learn | Developer-First API for Crypto and Stocks
Great! Next, complete checkout for full access to Alpaca Learn | Developer-First API for Crypto and Stocks
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.
Success! Your billing info is updated.
Billing info update failed.
Search
Machine Learning in Trading

Intro into Machine Learning for Finance and Trading (Part 1)

Alpaca Team
Alpaca Team

There has been increasing talk in recent years about the application of machine learning for financial modeling and prediction. But is the hype justified? Is machine learning worth investing time and resources into mastering?

This series will be covering some of the design decisions and challenges to creating and training neural networks for use in finance, from simple predictive models to the use of ML to create specialised trading indicators and statistics — with example code and models along the way.

If you are comfortable with machine learning in general, please feel free to skip and read from the 3rd section “Where can it be applied in finance?

What is Machine Learning?

In simply terms, machine learning is about creating software which can be “trained” to automatically adapt its predictive model without the need for hard-coded changes. There is often debate whether machine learning is considered a subset of Artificial Intelligence, or whether AI is a subset of ML, but they both work to the same broad goal of pattern recognition and analysis.

While different forms of machine learning and expert systems have been around for decades, only relatively recently have we seen large advances in their learning capabilities as both training methods and computer hardware has advanced.

With the creation of easy to use open-source libraries, it has now become easier than ever to create, train and deploy models without the need for specialist education.

Neural Networks

Artificial Neural networks are, again, a subset of the broad field of machine learning. They are among the most commonly used and are easier to understand conceptually.

A network is made up of layers of “neurons”, which each perform a very simple calculation based on their own trained. Individually, they provide very little in terms of processing. However, when combined into a layer, and layers stacked into a full network, the complexity that the model can learn widens and deepens.

(Simple neural network structure)

Each neuron has a weighting value associated with each input it receives. Its final output which is passes on is:

Sum of weightings * their respective input

This value is then put through an “activation function” (such as tanh or sigmoid).

The activation function can serve to normalize the output value of the neuron and add non-linearity, so that the network can learn functions more complex than simple linear relationships.

Once the input data has processed through the whole stack of neurons, you’ll be left with your simple prediction or statistics, such as a long/ short call for the next time period.

Training a network

Its all well and good looking at the flow of data for a model to make a prediction, but it wouldn’t be complete without a brief overview of how the network is actually trained to make these predictions.

During the training process, you run a set of data through the network to compare its predictions against the desired results for each data point. The difference between the output and the target value(s) is then used to update the weightings within the network through “back-propagation”.

Back-propagation starts at the output neurons, looking at the component values they received from the previous layer and the associated weightings. The weightings are given a small adjustment to bring the updated prediction closer inline with the desired output.

This error between the output and target is then fed to the next layer down were the same updating process is repeated until all the network weightings have been marginally adjusted.

This is repeated multiple times over every data-point in the training dataset, giving the network weightings a small adjustment each time until predictions converge towards the target outputs.

In theory, this learned model will then be able to make accurate predictions on out of sample data. However, it is very easy to over-fit to the training data if the model is too large and simply learns the inputs rather than a generalized representation.

(Example of over-fitting for simple classification — made with tensorflow playground)

Where can it be applied in finance?

Since neural networks can be used to learn complex patterns in a dataset, they can be used to automate some of the processes of technical analysis commonly used by traders.

A moving average cross strategy can be coded with ease, needing only a few lines for a simple trading bot. However, more complex patterns such as indicator divergence, flags and wedges, and support/ resistance levels can be harder to identify with simple rules. And, indeed, forming a set of chart patterns into an objective trading strategy is often hard to achieve.

Machine learning can be applied in several different cases for this one scenario.

  1. Pattern recognition from candle data to identify levels of significance
  2. Creating specialised indicators to add to a simple rule based strategy
  3. A final processing and aggregation layer to make a prediction from your set of indicators.

Machine learning can also be applied in slightly more exotic ways to help refine further information:

  • Denoising and auto-encoding — used to remove some of the random noise of a price feed to help distill the underlying trend or specifics of the market sentiment.
  • Clustering — group together different equities and financial instruments to streamline the value of a portfolio. Or it could be used to evaluate and reduce the risk of a portfolio.
  • Regression — often used to try to predict the price at the next time step, however it can be applied to a range of abstracted indicators to help predict trading signals earlier.

Machine Learning vs Traditional Methods

In many of the cases above, it is perfectly possible (and often advisable) to stick to more traditional algorithms. A well made machine learning framework has the advantage when it comes it easy retraining, but at the cost of complexity, computational overhead and interpretability.

While there have been advances in the use of relevance heat-maps to help explain the source of a prediction, neural nets still mostly remain black boxes — ruling out certain use cases, such as for fund managers, where decision justification and accountability is of importance to clients.

We may well see attitudes change over time as ML assisted trading and investing becomes wider spread, but for now this remains a large obstacle to practical application in certain settings.

Furthermore, it is often a lot easier to make a simple rule based strategy over a full ML model and training structure. But when done right, machine learning can provide cutting edge accuracy to the adversarial world of financial trading.

Conclusion

Despite some of the added challenges and complexity brought by the addition of machine learning, it provides a new range of tools which can be applied to a range of problems in finance, allowing for greater automation and accuracy.

In the next post we’ll be looking deeper into some of the theory and decision making behind different training methods and tasks for a new model.

By Matthew Tweed

Machine Learning in Trading

Alpaca Team

API-first stock brokerage. *Securities are offered through Alpaca Securities LLC* http://alpaca.markets/#disclosures