How I’m using Machine Learning to Trade in the Stock Market

 


Creating the strategy..

Figure 1: Apple (ticker symbol AAPL) stock price action from 2018 to 2021. Green dots are local minimums (low points) and Red dots are local maximums (high points)

The ML model

  • Normalized stock price — Instead of using the stock price, I used a normalized stock price as my first input parameter. A stock’s price action can usually be depicted by a candlestick as in Figure 2. The candlestick represents the highest stock price (HIGH), the lowest stock price (LOW), the open stock price (OPEN) and the close price (CLOSE) for the day (if we consider a daily chart as in Figure 1). In order to make it easier I created a single value between 0 and 1 representing all four of these values. This value was calculated by the Equation 1. If the resulting value is close to 1, this means that the stock has closed near the HIGH of the day, whereas if the normalized value is near 0 it means that the stock has closed near the LOW of the day. The advantage of using such a value is that it contains information of the price action of the whole day compared to using a single value such as the CLOSE or the average of the day. Also this value is not sensitive to stock splits.

Equation 1: Normalized value calculation


  1. Volume — The second parameter used in the model was the daily volume of the stock. This parameter represents the amount of shares traded (both bought and sold) on a specific day.
  2. 3 day regression coefficient — The next parameter was the 3 day regression coefficient. This was calculated by performing linear regression to the past three day closing prices. This represents the direction of the stock in the past three days.
  3. 5 day regression coefficient — A similar parameter to the 3 day regression coefficient. Instead of three days here I used five days.
  4. 10 day regression coefficient — Same as above, but used 10 day regression. This value represents the direction of the stock price in the past ten days.
  5. 20 day regression coefficient — Same as above, but used a 20 day regression.
Figure 2: Open, close, high and low of a stock tick. Source — https://analyzingalpha.com/open-high-low-close-stocks

Training and validating the model

Figure 3: Volume, normalized value, 3_reg, 5_reg, 10_reg, 20_reg are the input parameters and the target is the output. If target is 0, the row represents data from a buying point (local minimum) and if the row represents a 1 it is a selling point (local maximum).

Validation results and analysis

Figure 4: Testing results for stock ticker GS. Green dots represent buying points and red points represent selling points predicted by our model.
Figure 5: Confusion matrix of results from the validation dataset
Figure 6: Confusion matrix after threshold was changed to 0.01.
Figure 7: GS stock buying opportunities after using a threshold of 0.03

Back-testing & results

  • The Impatient Trader — This type of trader buys and holds the stock for a very short period of time. The trader also looks for small gains. This trader is also scared of losses, so the trader tends to sell the stock for a loss if the stock drops even a little bit. Finally this trader chooses stocks with a high threshold in order to quickly find another stock once they get rid of their current stock. So, parameters for this type of trader are t = 0.3, g = 0.005, l = 0.001 and d = 3.
  • The Moderate Holder — This type of trader buys and holds the stock for a moderate period of time. The trader is looking for stocks with high confidence so the threshold value tends to be low. The trader also looks for higher gains and has a higher tolerance for losses compared to the Impatient Trader. For this type of trader the parameters are t = 0.1, g = 0.03, l = 0.03 and d = 10.
  • The Patient Swing Trader — As the word “swing” suggests, this type of trader tends to hold the stock longer. Also the trader likes to select stocks with high probability of success. So the threshold is very low for this type of trader. Also this trader believes in selling stocks for smaller losses and moving on to different stocks. The parameters for this type of trader are t = 0.05, g = 0.04, l = 0.003 and d = 21.
  • The APE — The APE is the type of traders that are new to the stock market. They tend to choose stocks irrationally. So the do not use any strategy to select stocks. These types of investors randomly pick stocks and randomly sell them whenever they feel like it.
Figure 8: Total value of investment during year 2021. The starting balance for each investor type was 3000 USD.
Figure 9: Win/Loss bar plots for the “Patient Swing Trader” (above) and the “Impatient Trader” (below)
Table 1: Summary for each investor type

Comparison with S&P 500

Figure 10: Comparison of two models (Impatient Investor & Patient Swing Trader) to an investment of 3000 USD to the S&P 500 in 2021.

Other thoughts and future work

Conclusion

See Also :