# Building blocks of a trading algorithm

In a previous post, we looked at the building blocks of a trading system. In this post, we focus on the core of the trading application, the algorithm. This algorithm defines our trading strategy, and is created from several modules.

From a 10,000 feet perspective, our algorithm performs the following operations:

- calculate indicators
- make predictions
- assess risk
- create portfolio
- manage money
- place orders
- create reports

## Calculate Indicators

In the context of technical analysis, indicators are mathematical calculations performed on a security’s price and/ or volume. The following operations are examples of typical indicators:

- Moving Averages to filter and smooth data, remove noise and make trends more visible
- Bollinger Bands, performing statistical operations to determine probable trading ranges
- On-Balance Volume, combining price and volume data to create new insights

## Make Predictions

Based on the indicators, the algorithm will attempt to make a prediction of the future. Predictions come in many different flavors. What they typically have in common is, that the probability of the prediction becoming reality is much lower than certainty. Here are some examples:

- With probability x, the price will increase by percentage y
- With probability x, the future price will range within y and z
- With probability x, the percentage increase of stock A will exceed the increase of stock B by percentage y

## Assess Risk

As the predictions are far from certainty, the algorithm needs to be prepared for predictions turning out to be false. In order to do so, the risk needs to be assessed and quantified. There are many different ways to quantify risk, here are some examples:

## Select Portfolio Constituents

To create a suitable portfolio, the algorithm needs to carefully balance the prediction of the future, and the risk associated with taking that opportunity. To do so, the algorithm needs to optimize for two conflicting objectives: maximize return, and minimize risk. This is known as Multi-Objective Optimization, in specific, mean-variance analysis.

## Manage Money

Even for an optimal portfolio, additional money/ risk management is required. The purpose of this module is to determine the overall risk, and control position sizing. A lot of research has been done, and a lot of math has been developed for this, interestingly under the umbrella of Game Theory and in regards to the Risk of Ruin. One of the well known principles is the Kelly Criterion.

## Place Orders

The next step after determining our position sizes, is to place orders. The algorithm needs to determine how the positions at the broker differ from the model portfolio, and emit orders accordingly. Also, it is desirable to place stop orders on existing positions, in order to be prepared for fast adverse market moves.

## Create Reports

The last module of the algorithm creates reports. Among the things to report are:

- performance, to document effectiveness of the algorithm
- portfolio decisions, to better understand how the algorithm is meeting the objectives
- system status, to monitor proper operation of the trading system