Algorithms, AI and Trading Volatility

Can algorithms help improve the performance of investing in the stock markets? With something as random and uncertain as stock prices, I thought a good place to start is the set of algorithms that are collectively labelled as ‘Artificial Intelligence’ (AI) though that won’t preclude the use of other algorithms or statistical/mathematical approaches if more appropriate.

Algorithms could help in deciding what to buy – if they can be used to collect, collate, filter and even analyse data gleaned from myriad financial websites and the like – looking for historic statistical correlations and patterns. They may also help in determining when to buy (and sell) a particular share. This is what I want to investigate first: price volatility, can it be exploited profitably?

By exploiting volatility I mean repeatedly buying and selling a shareholding at short-term lows and highs respectively, beating a buy-and-hold strategy. Bettering buy and hold implies increasing the total number of shares held over time but does not necessarily mean increasing the value of the entire holding (i.e. making a profit). But if the approach can be applied to a basket of shares that are sufficiently diverse (i.e. their daily price movements are not too highly correlated) then hopefully it will also be profitable, at least over the medium/long term…

Strategies

Share prices are subjected to tomorrow’s events which are largely unpredictable. So the objective is not to build algorithms that try and predict the immediate future. It is use algorithms that can build strategies that on average leads to greater returns than a buy-and-hold strategy over a given timeframe.

For example, perhaps the simplest strategy is to repeatedly buy and sell a share at a fixed price, say buy at 10 and sell at 12. Within a given date range there will be two prices that maximize returns. Determining the two optimal prices for a period in the past is not entirely trivial – it is typically a global optimization problem, meaning that there may be many solutions that are locally optimal. For example, in addition to the 10/12 buy/sell solution, there could be an alternative solution where we buy and sell at 10.5 and 11.5 – for a smaller profit each time but greater frequency over the same time period, leading to broadly similar returns.

A slightly more sophisticated strategy would be a ‘chartist’ approach of trading on price movements in and out of rolling-average bands – or on the value of a dimensionless indicator such as ‘William R’ or ‘RSI’ (e.g. buy at 30 and sell at 70). Again, this will be an optimisation problem where we can derive optimal values over a particular time frame.

A better approach might be one which can take in a plethora of indicators derived from the share price, volume, other shares and indices, exchange rates etc. and look for repeating patterns that are too complex to see visually.

While it is easy to look backwards and find a strategy that would have done well, is it possible to reliably derive strategies that will remain profitable for some time into the future? Can we build strategies that deal with uncertainty and adapt to the underlying dynamics of the market as it changes?

Features List

So if we assume that volatility can be exploited, what algorithms can be used? Before considering that, I’m going to list the features that might help exploit any data patterns – if they exist. It might not be feasible to implement them all (in the software that will be developed), not at first anyway

  • The software will keep a pool of strategies – randomly generated at first – that will evolve over a series of generations during a training process.
  • Training will involve trading a strategy from the pool using historic share price data. The software will simulate trading using the strategy, (and include trading costs and taxes and spread in each trade), hopefully making a profit with each buy/sell cycle that can be used to purchase additional shares on the next cycle. The number of shares held should increase – on average – as well as their total value – if the strategy is successful. The strategies will be adapted over the course of many iterations (generations) in a way that improves performance over the training period.
  • Training of the strategy will take place between date D1 to date D2 (D2 after D1), the strategy performance will be tested over a period not used for training – typically D2 to D3 (where D3 is after D2).
  • Using a moving window between these dates (D1 and D2) should help reduce over-fitting to the data. The performance of the strategy will then be the average over the different training windows.
  • Perform many runs to build a set of strategies. Save the ‘best’ strategies for each run (best being a metric that is predominantly profit but probably also needs to be behaviour based – e.g. proportion of trades in the right direction; risk based penalties).
  • The strategy will be in the form of a software ‘model’ that receives inputs and that outputs a buy or sell decision. Inputs into the models will be derived from the past price time series for the share price, possibly past volume as well as other related time-series (other share prices, the benchmark index, volatility indices, commodity prices, exchange rates) or could be time-based (period of the year, period of the month, day of week) etc.
  • Stochastic strategies – strategies containing a probability output will allow different outcomes (with a specified probability) for the same set of inputs. This may allow the strategy to better model the behaviour of the market, for broadly similar (known) inputs can lead to very different outcomes.
  • Use voting to combine the outcome of the multiple strategies. For stochastic strategies, the output probabilities will be summed to a deterministic value – (i.e. instead of counting yes/no votes, we will be counting probabilities (fractions between 0 and 1) of yes/no votes).
  • Adapt the proportion or weighting of individual strategies over time as the environment changes. i.e. rather than adapt the strategies themselves over time, add new strategies and up/down-vote successful/unsuccessful strategies. We could keep ‘buckets’ of strategies and in addition to adapting the content of each bucket, switch between the buckets as the dynamics of the market change (and hopefully the market repeats past behaviour). There are other approaches on the same theme, e.g. keep to one-strategy, one-vote but adapt the voting percentage over time.

Of course, even with most or all these features implemented, it is entirely possible that the resulting software could just be a very complex random number generator. If that’s the case, the simulated profits will quickly turn negative as trading costs eat into the initial capital (or the software does not trade, either not investing or buying and holding).

For future posts, I plan to discuss the software & algorithms that I have been developing and that implement most of the features discussed above…

Next post…