Technical Analysis Stock Predictor System – Final Report
Introduction
Markets move in predictable patterns. This is the
hypothesis of a realm of stock prediction called technical analysis. As
described by Pring (2002) a leading technical analyst:
The technical approach to investment is
essentially a reflection of the idea that prices move in trends that are
determined by the changing attitudes of investors toward a variety of economic,
monetary, political, and psychological forces. The art of technical analysis,
for it is an art, is to identify a trend reversal at a relatively early stage
and ride on that trend until the weight of the evidence shows or proves that
the trend has reversed.
For my senior project this semester, I tested this
hypothesis by analyzing short-term Bitcoin (BTC-USD) stock price trends to see
how accurately I could predict 1-hour changes in stock price. The subsequent
model was then automated to create real time predictions.
Methods and Setup
In my readings I read about patterns
that have been identified by hand to show evidence of a reversal in a market.
Some of these include the following:
Figure 2 Head and Shoulders trend. Can show either a bullish or bearish reversal. Credit Investopedia
While it is interesting to recognize common patterns
in a market, the question remains: How do we determine the predictive power of
a given trend and how can we numerically describe the shape of said trend.
To start I pulled 60 days of BTC-USD stock data at a 2-minute
granularity (maximum allowed) and divided the price data into 3-hour time
windows. For each time window I fit a high degree polynomial (18 degrees) to
the data like the image shown below:
For each time window I found a corresponding “y” value
that was the change in price over the next hour from the final stock price.
With my newly discovered polynomial function, I was able to use calculus principles to describe its shape. First, I calculated the number of local minima and local maxima and the linear slope of the function.
Next, I wanted to describe the distribution of the
price to indicate any sudden movements over the time window.
Finally, I subdivided each function into four quarters and calculated each of the previous metrics in each quarter. I also calculated these metrics for volume as well. In the end these were the features in my dataset:
I trained my model with an XGBoost regression over 350
epochs with a max tree depth of 16. The model was evaluated using root mean
squared error.
Results
The model was retrained each time a prediction was
made so the accuracy and R squared varied. The model ran for several hours, and
hundreds of predictions were made. The average accuracy of the model (predicted
movement in the right direction on test data) was 60% and the average R
squared was 0.49. The actual accuracy on real time prediction reflected
the average test data accuracy exactly with 60% of prediction reflecting
the correct direction of the stock price over the following hour. I honestly
think there is at least a valuable datapoint from these finding and I think
that additional time spent feature engineering will yield even better results.
Setbacks
I realized late in the game that I had made a simple
error in my code by scaling my features before splitting the data. As a result,
I was getting results that were phenomenal (96% accuracy and 0.78 r squared). However,
whenever I would make a prediction on real time data the results were
terrible. Because I was scaling the data prior to splitting, I had a major data
leakage problem that was fixed simply by scaling the training and testing data separate
and then inverting the scale of the real time prediction with the test data
scale. This resolved the issue, but as a result the amount of data that was
collected on real-time predictions was limited to one day of results.
Link to GitHub
github.com/brandonritchie/StockRNN
Works
Cited
1. Pring, M. J. Technical Analysis
Explained. New York, NY: McGraw-Hill, 2002
Comments
Post a Comment