Hello, welcome back to my blog about automated trading. Last week I
have started with prediction and presented K nearest neighborhood method. This week have promissed to introduce neural networks.
For the source code, please download
repository and update to branch 11-prediction-2, changeset 0dff22e2031f.
Background
Neural networks are a big topic. Just DuckDuckGo them, or here are 2 links I personally like.
Then there are available plenty of books and scientific articles. I personally read through some of them.
Now for the practical usage let's focus only on feed forward networks with single hidden layer. They are good in classification and identifying relations (see
previous post). What does it mean? At first there must be a fundamental reason for relation between observed values and class. Once it is there, then there is no need to know what kind of relation is it. This is the job for network. Example of what it can mean in the stock trading? Question is whether price will tomorrow go up or not. And believe is that this depends on the price change in the past few days. And this leads to the refined question.
Refined question
In the
previous post I have deined question whether stock price during next 5 trading days will drop under the today's low or not. This was supposed to define the stop loss. Now let's improve it, short prediction window to 1 trading day and define it in the more appropriate way.
Let's say today is the last trading day where open, high, low and close prices are know. Let's say tomorrow is the next trading day (it might be really tomorrow or for example next Monday if today is a Friday). Let's set the convention that today.open, tomorrow.high... are shortcusts for today open price, tomorrow high price and so on.
Now for the prediction. Task is to buy stocks tomorrow after market opens. So tomorrow.open is known at the time of buying. Then the prediction model should find stocks which will satisfy the following criteria.
- tomorrow.low > today.low (for stop loss)
- tomorrow.high - tomorrow.open > 2 * (tomorrow.open - today.low) (for risk/reward ratio and target price)
Knowing this the algorithm would buy the predicted stocks right after the open, set up stop loass at
today.low, set up target at
tomorrow.open + 2 * (
tomorrow.open -
today.low) and finally limit position holding time to sell eveything before market closes. Then the only thing which left is to calculate the position size based on the acceptable risk. This I will do next week. Also note that if
tomorrow.open <
today.low, then it doesn't make sense to do this prediction, because the first constraint is already broken. These records are excluded.
Coding
There is more coding for today. At first I have implemented inerface for neural networks, back propagation and black box supervised training. All this includes technique for dealing with local minimas (although no one can be sure). Everything is in the package com.enterprisemath.math.nn.
Then second part is adaptor to the original prediction API. This is done by the classes NNCaterogyPredictorEstimator and NNCaterogyPredictor. Estimator estimates the category predictor and this predictor is used as before. I have made few renamings in this package as well. KNeighbourhoodCategoryPredictor is now called KNNCategoryPredictor.
Finally the test application is renamed from HistoricalQuoteKNPredictionTestApp to PricePredictionTestApp. Structure is the same as in the
previous post. One important point is variable predictorType on line 77. This allows to choose the predictor. Allowed values are:
- NN - neural networks
- KNN - k-nearest neighrbour model
Please note that neural networks are compute expensive and it takes a long long time. This is a known problem with neural networks. Therefore I have reduced the data size for this demonstration. For the real usage I am storing the network once it is successfully trained. If you have any idea how to speed up the process of NN training, please let me know.
Results
Here are my results from program running today as I am writing this post.
Data
Downloaded data have the following properties.
- Symbols: ACAD, CLDX, HK, THLD
- Number of training records: 1467
- Number of test records: 369
- Number of true in training records: 153
- Number of false in training records: 1314
- Number of true in test records: 23
- Number of false in test records: 346
As before data are divided to the training records and test records (80% / 20%) to make sure prediction model applies to the unknown records. In the test data, there are 32 opportunities to buy according strategy discussed above (Number of true in test records). So let's see how many predictions can find.
K-nearest neighborhood method
This method gave me following results.
- Number of not sure: 48
- Number of true when true: 0
- Number of true when false: 0
- Number of false when true: 19
- Number of false when false: 302
- Total success rate: 0.818
From this I can see this prediction method would not generate any trading opportunity (labeled as 'Number of true...'). So it's useless. This is probably caused, because most majority of cases are not considered as a buy oportunity. And therefore there is higher probability that these will take the allowed K places.
Neural network
This method gave me following results.
- Number of not sure: 46
- Number of true when true: 4
- Number of true when false: 16
- Number of false when true: 13
- Number of false when false: 290
- Total success rate: 0.796
This model would open 20 positions in total and 4 of them would reach the target. The other 16 would not - they might go sideways, little bit up or drop. Definitely there is a space for improvements (e.g. add volume or day of the week into the equation). And definitely I am going to create a trading algorithm to see NN in action (of course I will include there more than just 4 stocks (; ).
That's it for today. Next week I will show the trading algorithm which is using prediction. Thank you for reading!