There are many aspects to a building a successful strategy, with most of the attention focused on finding accurate entry signals. While knowing when to enter a trade is an important part, it is far from the only area where you should you be spending your time and energy.
Optimizing your position size, knowing when to stop and start trading, as well as following a more rigorous approach when selecting indicators are three areas that do not usually get a lot of attention but can have a huge impact on the performance your strategy.
In this series of posts we’ll show how you can use machine learning to improve all three of these areas. Part 1, will show you how to determine your position size using a Random Forest algorithm and figure out when to turn off your strategy using a Hidden Markov Model. Part 2 will cover selecting your indicators using a wrapper method and a Support Vector Machine.
So let’s get started!
Determining Your Position Size
This article assumes you already have entry signals for a strategy. You can download the historical data set from my strategy here to play around with it yourself. 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.
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:
- Winning trades
- Returns >= 10 pips
- Ideally we want to enter into larger positions for these trades
- Neutral trades
- Returns < 10 pips and Returns >= -10 pips
- We can use this as our default position size
- Losing trades
- Returns < -10 pips
- We want to enter into smaller positions for these trades
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:
- Current market conditions
- Inputs: Volatility, trending or sideways market, etc.
- This is appropriate if your strategy tends to perform well in certain market conditions
- Recent performance
- Inputs: the returns of the last “n” trades
- If you notice your strategy goes through periods when it performs well and periods when it underperforms, this is probably the way you want to go.
- External factors
- Inputs: major news announcements, holidays, Mondays/Fridays, etc.
- While some traders will avoid trading around these “external factors”, testing to see if this is justified is usually a good idea.
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:
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!
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 find the R code here):
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):
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%!
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:
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%.
Now that you know how determine your position size and when to start and stop trading, we’ll next go over how to choose a robust set of indicators to use in your strategy.
As we saw here, leveraging machine learning can greatly increase the performance of your strategy. At Inovance, we make it easy to use machine-learning algorithms as a method to uncover patterns in the indicators you use to trade.
Get your free premium account from our partners, BBFX, here and take your trading to the next level!