Authored by: Tarun Bhatia, Stuart Colianni, Taylor Korf, and Erez Katz
Applying Machine Learning to predict the stock markets has been and remains one of the most challenging problems for AI researchers mainly due to noisiness in the data on which the models rely. In the context of forecasting tradable assets, supervised learning still remains the most dominant machine learning approach. In supervised learning, training a model is nothing more than traveling over time in history and feeding the machine with lots of samples of states to outcome relationships. Simply stated, we inform the machine how time-series factors (features) correspond with directional outcomes. Hence, the term "Supervised Learning". Having the right features for your model is an important and often focused upon aspect of building models, however defining the target outcome is often overlooked while it is just as important.
Image 1: Supervised learning by pairing the state and the outcome and “training” the machine to generalize which states are most relevant to a desired outcome.
Why can’t we just train to predict the future price of a stock? Intuitively, looking to predict the future price of a stock may seem right but is bound to fail since stock price returns are very noisy on a short term timeframe often resembling random walk-like behavior. This poses a challenge when training models as input features rarely have sufficient information to predict these short term fluctuations.
Image 2: Showing one day returns - BAC (Bank Of America) in green vs random walk behavior in blue.
Maybe Targeting a binary directional outcome? To solve the price forecasting conundrum, researchers will often attempt to simplify the problem by classifying these returns as directional, either positive or negative. Further, by measuring benchmark relative direction, we can isolate the idiosyncrasies of the underlying stock and encourage our model to learn pertinent price movements as opposed to overfitting on noise. This method however requires a careful design of our labeled data (the outcome we train the machine to predict), begging the question:
What is it that we are actually trying to predict? What should our labels be?
One of the most commonly cited labeling methods in financial literature is the “fixed time horizon” approach. This method makes use of a static threshold and static time interval. Positive labels are assigned to stocks when the returns at the end of the time interval are greater than the specified threshold. Say for example we choose 5 days as our fixed time horizon. Day i is given a positive label if the returns on day i+5 are greater than the chosen threshold. Otherwise, a negative label is assigned.
The “fixed time horizon” approach may not be sufficient since different stocks may exhibit different levels of volatility over time. As such, the same fixed threshold will not be appropriate for all stocks for all periods. Crossing a fixed threshold is easier when volatility is high, and significantly more challenging when volatility is low. Similarly, stocks in certain sectors tend to be more volatile than those in others, and therefore require different threshold values altogether. Simply put, when it comes to fixed time horizons, one size does not fit all.
How about we consider the above fixed time horizon method but this time assume a one-day look forward and a threshold of 0.0%. In other words, if the return between days i and i+1 are positive a positive label is assigned, otherwise a negative label is assigned. Unfortunately, this scheme will further expose the underlying weaknesses of the fixed time horizon approach. By labeling based on positive and negative returns, the assigned classes change back and forth rapidly. This behavior is severely disadvantageous for a few reasons:
- First, many of the factors that influence the price of the stock are not subject to change every day. Take for example fundamental factors.
- Having class labels that toggle rapidly introduces a source of noise for our learning algorithm.
- It is impossible to ascertain when labels are assigned due to an underlying trend versus natural price variance.
- Lastly, entering and exiting positions frequently incurs more costs in the form of fees and slippage often offsetting any resulting gains.
Ideally, we want a labeling scheme that captures systematic trends vs. knee jerk abrupt price movements.
Doing so makes it possible to assign meaningful labels regardless of the underlying asset’s volatility. For this reason, such a labeling scheme would be more flexible than the brittle fixed time horizon method for the following reasons:
- Reduces noise in our training dataset, thereby making the learning process more reliable.
- Reduce class flipping by which we contain transaction costs and slippage.
How can labels capture trends?
The first step is implementing a mechanism to dynamically control the impact that natural price variance has on the labeling process. The next section discusses in detail how our “volatility-based labeling” does exactly that!
Volatility Based Threshold
What is a trend? One can think of a trend as a price movement unlikely attributable to chance. Volatility-based labeling assigns positive labels when future price movement exceeds that which would reasonably be explained by volatility alone. Volatility is measured by calculating the standard deviation of daily returns over a fixed forward looking period. Using this metric and some simple arithmetic, dollar values for the upper and lower bands can be calculated:
Figure 1: Example of volatility-based upper and lower bounds.
Volatility-based labeling creates two bounds based on future volatility:
These bounds are dynamic in that they are determined directly from the data. Decreasing and increasing the upper_threshold and lower_threshold coefficients moves the bounds closer to and further from the price line respectively. Having bounds further from the price line decreases the likelihood that they are crossed by random walk behavior- that is, due to natural price variance. By setting bounds that are unlikely to be crossed by chance, we can ensure that when crosses occur it is very likely due to an underlying trend.
If during the look forward period a future price hits the upper bound and does not hit the lower bound, then a positive label is assigned. Otherwise, if the lower bound is ever crossed, a negative label is assigned.
Table 2: Labeling based on volatility
This is a better labeling scheme, but some investors want to ensure that they hold on to assets for a minimum period of time before getting out. To satisfy this we introduce a filter window to our labeling scheme. We can specify the holding time for this filter window and we shall get in only if we are above the upper barrier after the holding time. Also we can have an additional condition that if during the holding period a price hits the lower barrier then we label it as 0 and we shall not buy.
Table 3: Labeling based on volatility and filter window
Figure 2: Dynamic labels based on volatility and filter windows showing buy (Label :1) as it hit upper barrier and does not hit the lower barrier.
Figure 3: Dynamic labels based on volatility and filter windows showing sell (Label: 0) as it hit lower barrier and does not hit upper barrier.
Account for transactions cost: Stock behavior changes across different market conditions and periods. During times of low volatility, the returns will not be sufficient to overcome transaction cost and thus the label should not be set to 1 even if it exhibits a directionally positive outcome. The label outcome at that point is simply not actionable. By considering the transaction cost and slippage into the label equation we will only set the 1 labels when the price change hits the upper barrier after considering transactions cost and slippage. This minimum threshold for a trade viability ensures unprofitable trades are not positively labeled.
Consider market or benchmark relative returns: This labeling scheme is good for labeling positively performing stocks, but it is often important that we are additionally beating a benchmark. During bull market periods, conditions are such that the market as a whole is moving up but some stocks may be underperforming relative to the market (or benchmark) as a whole. How do we tell our machine to choose stocks which do better than the market?To solve this problem we introduce market relative returns to our labeling scheme. While following the above labeling scheme, we also calculate the benchmark returns for the same period (from the day we want to label as 1 to the day a price hits the upper barrier before the lower barrier after considering the filter window) and calculate the difference. If the difference is positive we label it as 1, otherwise 0.
Use peer-relative features: Remember, our labeling scheme would be good as long as features provided to the model are predictive of the labels, for example just using macroeconomic features with this labeling scheme which uses benchmark relative returns would not be able to produce a good model as the model may overfit and learn noise. On the other hand, if we add fundamental and technical features, normalized by ranking them against their peers, along with the macroeconomic features will give us a much more predictive model. Normalizing by ranking is the key here, as it helps us in distinguishing between positive performance and benchmark relative positive performance.
Stocks' directional classification has many advantages when applying machine learning to trading decisions. In order to be predictive, a model must be given features that can explain idiosyncratic changes in a stock price action. More importantly we want to ascertain that the model can focus on stocks' price behavior that are predictable in the first place. Employing a labeling scheme that filters out noise and isolates systematic trends is essential to building machine learning models that continue to perform well out of sample. After years of AI research in time series analysis, we believe the approaches described here will help you achieve more robust and predictive ML models as we have enjoyed this very approach in our own models. We are eager to hear your thoughts so please feel free to reach out.
Questions about dynamic labeling? Drop them below or contact us.