Inovance Machine Learning Histogram Background Algorithmic Automated Trading Home
TRAIDE Machine Learning

Machine Learning Techniques to Improve Your Strategy

By: | 09/22/2015

Comments

Machine learning is a powerful tool for not only coming up with new strategies (like we do in TRAIDE) but also for improving your existing strategies.

In this article, we’ll cover adjusting your position size using a random forest algorithm and turning your strategy on an off using a Hidden Markov Model. You can copy and paste the R code to try it yourself on your own strategies. This article assumes you already have a strategy. You can download the historical data set from my strategy here. If you are looking for a new strategy, you can quickly build one in TRAIDE and copy the trades from the trade table on the Dashboard to a csv. Just make sure you use the same format as the csv above.

How to Improve Position Sizing

Position sizing is an important, often overlooked aspect of trading. Many traders look at position sizing as a way to decrease downside risk without seeing it as a tool to increase performance. While it is important not to risk too much of your total account on each trade (usually around 2%), there are far better ways than just using a fixed lot, or fixed percentage, position size for each trade.

One logical train of thought would be to enter a larger position when your trade has a higher probability of success and a smaller position when you are less sure about the trade. Using a random forest, a popular machine-learning algorithm, we can estimate the probability of success for each trade and size accordingly (never risking more than 2% per trade of course).

I went into greater detail on using a random forest to build a Bollinger Band-based strategy for the GBP/USD and we can use a similar approach to help us determine the optimal position size.

Given my data set of historical trades (you can download it here to play around with it yourself), first we classify each trade into three categories based on its return:

Next we have to decide what information we want to use to make our decision, otherwise known as the inputs to the model. This is an area where you should use your experience with your own strategy to make a decision. A couple different options:

Let’s try using the returns of the last 3 trades to determine the position size of our next trade (you can download the R code here). We will double our position size if the model predicts the next trade will be a “Winning trade” (defined as a return over 10 pips) and cut it in half if it predicts a “Losing trade” (a return of less than -10 pips) and keep the default position size for all “Neutral trades” (returns less than 10 pips but greater than -10 pips). We train the model on the first two thirds of the data set (our historical trades) and then test it on the final third of the trades, our out-of-sample test: TRAIDE Random Forest Equity Curve

Wow! We were able to get a 20% increase in total return and increase our return per trade from 2.8 pips to over 3.3 pips just by using this fairly basic model. Not bad at all!

How to Turning Off/On Your Strategy

Knowing when to start and stop trading can mean the difference between success and failure. However, determining when to “turn off” your strategy is a non-trivial task. Once again we can use a popular machine-learning algorithm, known as a Hidden Markov Model (HMM), to determine market regimes where our strategy underperforms and we should stop trading. (Check out my post on using an HMM to identify market conditions).

First we have to decide what we want to use to identify the different “regimes” of our strategy’s performance. We are asking ourselves “what factors will tell us that we should stop trading our strategy?”

Let’s try using two calculations based off of a 10-period simple moving average (SMA) of our equity curve. We are going to look at both the rate of change over 5-periods and the distance between the current balance of the equity curve and the SMA line. The rate of change (ROC) should tell us if our equity curve is in a general downtrend and the distance between the lines should give us a much more sensitive measure of how the strategy is performing.

Now, based on these two inputs, we will us a 2-state HMM model to decide when we should “turn off” our strategy (you can download the R code here): TRAIDE Hidden Markov Model Equity and SMA

We will then look at the performance of the strategy if we stop trading whenever the HMM model classifies the strategy in “Regime 2” (be sure that you shift the HMM’s classification back one data point so you are only using data that would actually be available): TRAIDE Hidden Markov Model Equity Curve

Once again, we see a significant increase in performance! We were able to see a 13% increase in return despite decreasing our number of trades from 1259 to 726, almost doubling our return per trade from 2.1 pips to 4.2 pips, and increase our accuracy from 60% to 67%!

Combining Our Random Forest Algorithm and The Hidden Markov Model

Finally, let’s see what happens when we combine our random forest position sizing model with the HMM regime shifting model on our out-of-sample data set: TRAIDE Hidden Markov Model and Random Forest Algorithm

By both adjusting our position size based on a random forest model and halting trading when conditions were unfavorable we were able to significantly increase the performance of our strategy. The final return was 44% higher despite having 133 less trades, leading to our return per trading jumping from 2.7 pips to 5.7 pips and the accuracy increasing from 64% to 70%.

Machine learning algorithms and techniques can be a powerful weapon in your arsenal when looking to improve the performance of your strategy.

At Inovance, we use machine-learning algorithms as a method to uncover patterns in the indicators you use to trade but as we saw here, that is just one way to leverage these powerful algorithms to make you a successful trader.