January 5th, 2022
Stock trading is a popular area for machine learning. There are entire industries that are using technical analysis to try to optimize their trading strategies. But what is technical analysis? What are the common learners used in trading? What are technical indicators and how are they used? Hopefully I can answer all of those questions and help you to better understand a more wholistic view of machine learning and trading. Although I love getting technical, I do not plan to get incredibly technical and hope for this to be more of a basic overview.
Machine learning can be done with a little as a computer that can run python. You can get stock data from yahoo finance, and you could potentially start training your trader with a few python libraries, Pandas, and a little know how.
Here is an example of what the stock data from Yahoo finance looks like: AAPL stock.
When you are sourcing stock data for a machine learning model, be sure to use 'Adjusted Close' instead of 'Close'. It accounts for changes in the price, such as stock splits, etc.
Fundamental and technical analysis are two major schools of thought when it comes to approaching the markets, yet are at opposite ends of the spectrum. - Investopedia, Fundamental vs Technical analysis
Fundamental analysis is using a companies intrinsic value to try and find the value of a stock. So in fundamental analysis, you are using things like cash flows, company assets, expenses, etc. to try and value the stock.
Technical analysis is more about using computed statistics. These computed statistics are called indicators, and I’ll get in more detail about technical indicators in a section below. Technical analysis uses data like historical price or volume to make decisions.
Technical analysis has a lot of value for computers. Decisions can be calculated quickly and acted upon. In Fundamental analysis, decisions usually are more complex, and likewise, require more time and effort, consequently requiring more human involvement.
A technical strategy requires historical data.
Technically, you don’t need historical data. You could use something like reinforcement learning or Q-learning to use future experiences to try and optimize a strategy. But! I’m not going to talk much about those in this article, even though, they are very cool, and worth looking into.
Technical Indicators make up that historical data that is required to train an ML model. By calculating the historical indicators, you can then leverage that data. Some common indicators include momentum, Bollinger Bands, and moving average convergence divergence (MACD). Investopedia also has a great list of technical indicators.
Once you are actually using the indicator as your input to train your model, you can then utilize binning to help tune your model.
Here is an example that is using a daily stock price, and calculating a simple moving average, normalizing that value, then binning it.
Stock Price | 3 day Simple Moving Average (SMA) | Price / SMA | Binning - Sell Signal: > 1.25, Buy Signal: < 0.9 |
---|---|---|---|
10 | 10 | 1 | Do nothing |
9 | 9.5 | 0.98 | Do nothing |
7 | 8.67 | 0.8 | Buy |
11 | 9 | 1.2 | Do nothing |
16 | 11.34 | 1.41 | Sell |
19 | 15.34 | 1.24 | Sell |
In this example, when the price dropped below a certain threshold, we are saying that it might be a good signal to buy because the price might rebound back. The opposite is true for selling. Many of the technical indicators imply to some degree an inefficiency in the market, and that prices will rebound or settle in one way or another.
These indicators can be heavily adjusted, which to me feels much like hyper parameter tuning. The number of days you are using for your SMA, how you setup your thresholds for binning, etc. This is using a fairly small window of 3 days, but you may want to look at much larger windows. For example, if you were using a 200 day window, then if a price were to cross above that 200 day average, it would be noteworthy, potentially showing a bullish trend.
You need the following things to do technical trading:
If you are able to get your hands on stock data, then you can use Python to calculate historical indicators. With that data in hand, you can feed all of that into a machine learning algorithm.
Random forest is a good algorithm to start with. It isn’t constrained like parametric models in regards to dimensionality. In addition, you can use boosting and bagging to further optimize your model. But beware of overfitting, especially with boosting. Bagging will help to curb some of the overfitting tendencies though.
When training your model, you need two things. You need your x and y data. In the table below, I created an example of what that may look like. In this example, I am using future price as the output variable.
(X) Indicator 1 | (X) Indicator 2 | (X) Indicator 3 | (X) Indicator 4 | (Y) Future Price (3 days) |
---|---|---|---|---|
1 | 0 | 0 | 1 | 10 |
0 | 0 | 0 | 1 | 11 |
1 | 1 | 1 | 1 | 13 |
1 | 0 | 1 | 0 | 12 |
0 | 1 | 1 | 0 | 15 |
1 | 0 | 0 | 0 | 13 |
1 | 1 | 0 | 1 | 14 |
Although this is somewhat of a contrived example, and you likely would want to put more rigor into developing the data, you can hopefully start to see how things come together.
Once the model is trained, then hopefully you set aside some of your data to test with and you can start experimenting to try and optimize your trades!
Once you have your model predicting a price, you can then compare that price to the current price. You can then setup normalization and use thresholds to bin the result. For example, if the value is going to go up, then you would want to buy!
Technical trading is an exciting use for technology. Machine learning is used widely to optimize trades made at many hedge funds. Hopefully you can start to see how things come together, what technical analysis is, and how you might use it to build a machine learning trading program.
Want to be notified about new posts?
Then sign up to get posts directly to your email. You can opt-out at any time.