Contains all code related to using HMMs to predict stock market prices.

Jays-code-collection, updated 🕥 2022-08-13 08:17:42

Stock Market prediction using Hidden Markov Models

This repo contains all code related to my work using Hidden Markov Models to predict stock market prices. This initially started as academic work, for my masters dissertation, but has since been a project that I have continued to work on post graduation. At present, the program must be called from a terminal/ command line, but there is an aim to extend it to an interactive site in future, potentially via Django.


Hidden Markov Models are an incredibly interesting type of stochastic process that are under utilised in the Machine Learning world. They are particularly useful for analysing time series. This, combined with their ability to convert the observable outputs that are emitted by real-world processes into predictable and efficient models makes them a viable candidate to be used for stock market analysis. The stock market has several interesting properties that make modeling non-trivial, namely volatility, time dependence and other similar complex dependencies. HMMs are suited to dealing with these complications as the only information they require to generate a model is a set of observations (in this case historical stock market data).


Training on and predicting stock prices between January 1st 2018 to December 5th 2020 (the date that this example was ran on), predicting 5 days into the future. Typically the model will need to be trained on longer periods for more accurate results but this is purely to have a simple example.

Input: shell python -n AAPL -s 2018-01-01 -e 2020-12-05 -o C:\Users\Jay\Test -p True -f 5 -m True

Output: shell Using continuous Hidden Markov Models to predict stock prices for AAPL Training data period is from 2018-01-02 00:00:00 to 2019-12-17 00:00:00 2020-12-06 17:50:11,202 __main__ INFO >>> Extracting Features 2020-12-06 17:50:11,203 __main__ INFO Features extraction Completed <<< Predicting Close prices from 2019-12-18 00:00:00 to 2020-12-04 00:00:00 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████| 244/244 [07:54<00:00, 1.94s/it] All predictions saved. The Mean Squared Error for the 244 days considered is: 3.7785175769493202 Predicting future Close prices from 2020-12-05 00:00:00 to 2020-12-09 00:00:00 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 5/5 [00:09<00:00, 1.92s/it] The predicted stock prices for the next 5 days from 2020-12-05 are: [122.99846938775511, 123.75152124114953, 124.50918361609536, 125.27148474027554, 126.0384530141956] The full set of predictions has been saved, including the High, Low, Open and Close prices for 5 days in the future.

Bottom of Excel file (future predictions):

| Date | High | Low | Open | Close | |:---------------------:|:--------:|:--------:|:--------:|:--------:| | 2020-12-03 00:00:00 | 123.78 | 122.21 | 123.52 | 122.94 | | 2020-12-04 00:00:00 | 122.86 | 121.52 | 122.6 | 122.25 | | 2020-12-05 00:00:00 | 123.6083 | 122.25 | 122.25 | 122.9985 | | 2020-12-06 00:00:00 | 124.3651 | 122.9985 | 122.9985 | 123.7515 | | 2020-12-07 00:00:00 | 125.1265 | 123.7515 | 123.7515 | 124.5092 | | 2020-12-08 00:00:00 | 125.8926 | 124.5092 | 124.5092 | 125.2715 | | 2020-12-09 00:00:00 | 126.6634 | 125.2715 | 125.2715 | 126.0385 |

The full table contains all of the test data, in this case from 2019-12-18 to 2020-12-04 as well as the final 5 days in the future, 2020-12-04 to 2020-12-09.




  • Pandas_datareader - Allows one to download data directly from Yahoo finance
  • NumPy - Required for fast manipulation of financial data (e.g. calculating fractional change)
  • Matplotlib - Required for visualisation of results
  • Hmmlearn - Open source package that allows for creation and fitting of HMM's
  • Sklearn - Used to calculate metrics to score the results and split the data, will be removed in future to reduce dependency
  • Tqdm - Used to track progress whilst training
  • Argparse - Required for console inputs


Stock market data is downloaded via pandas_datareader and the data is split into training and testing datasets. The fractional changes for any given day (from open to close, high to open, open to low) in the training dataset are computed and stored in a NumPy array. These fractional changes can be seen as the observations for the HMM and are used to train the continuous HMM with hmmlearn's fit method. The model then predicts the closing price for each day in the training dataset, based on the given days opening price. This prediction is found by calculating the highest scoring potential outcome out of a pre-determined set of outcomes (e.g. +0.001%, -0.001% etc). All predictions as well as the actual close prices for the testing period are stored in an excel file and the Mean Squared Error between the two is printed out. The MSE is also included in the file name for future reference. Future days are predicted by feeding forward the prediction values. Unfortunately, at present there is no method in place to account for overnight/ weekend futures trading, and so for the future predictions the n+1 days open price is the same as the nth days closing price.



shell sudo apt-get install libopenjp2-7 libtiff5 pip install -r requirements.txt


Within the src directory: shell python [-n XXXX] [-s yyyy-mm-dd] [-e yyyy-mm-dd] [-o dir] [-p T/F] [-f int] [-m T/F] The -n input represents a given stock name, -s is the start date of the period considered, -e is the end date of the period considered and -o takes in the output directory for the excel file produced. It is important that the dates are input in the correct order. -p is a boolean input that tells the script whether or not to plot the historical dates, it will have no effect if -m is not also set to true. -f stands for future and takes in an integer that determines how many days into the future the user would like to predict. -m stands for metrics, and determines whether or not to predict for the historical data, or just for the future days, if True all of the historical data in the test set will be predicted and the Mean Squared Error will be returned. The justification for -m being an optional input is that the model can take quite some time to predict, so it's best if the user has the option to just predict the close prices for x future days quickly as that is the information that many people will find most useful.

To run tests, use: shell python3 -m pytest tests

To run tests using docker, use: shell docker-compose up tests


ValueError: Input contains NaN issue from total_data = np.row_stack((previous_data_features, possible_outcome))

opened on 2023-01-18 08:31:23 by Originn

Hi, many thanks for this.

When running the code it fails always on the 2/5

Predicting future Close prices from 2023-01-18 00:00:00 to 2023-01-22 00:00:00 40%|██████████████████████████████████ | 2/5 [00:03<00:05, 1.97s/it]

seems that total_data contains NaN values and the score method don't like it. Any idea on how to fix this?

I have switched to downloading the data using yfinance, all the columns have no NaNs so it's not the root of the issue. Is there something I am missing that may cause the ValueError?

Training data period is from 2018-01-02 00:00:00 to 2021-05-18 00:00:00 2023-01-18 10:04:46,223 __main__ INFO >>> Extracting Features 2023-01-18 10:04:46,223 __main__ INFO Features extraction Completed <<< Predicting Close prices from 2021-05-19 00:00:00 to 2023-01-17 00:00:00 100%|█████████████████████████████████████████████████████████████████████████████████| 419/419 [09:17<00:00, 1.33s/it] All predictions saved. The Mean Squared Error for the 419 days considered is: 6.657134688795405 Predicting future Close prices from 2023-01-18 00:00:00 to 2023-01-22 00:00:00 40%|██████████████████████████████████ | 2/5 [00:03<00:05, 1.97s/it] Traceback (most recent call last): File "/home/originn/HMMs_Stock_Market/src/", line 470, in <module> main() File "/home/originn/HMMs_Stock_Market/src/", line 465, in main use_stock_predictor(company_name, start, end, future, metrics, plot, out_dir) File "/home/originn/HMMs_Stock_Market/src/", line 354, in use_stock_predictor future_pred_close = stock_predictor.predict_close_prices_for_future() File "/home/originn/HMMs_Stock_Market/src/", line 248, in predict_close_prices_for_future predicted_close_prices.append(self.predict_close_price_fut_days(day_index)) File "/home/originn/HMMs_Stock_Market/src/", line 223, in predict_close_price_fut_days ) = self._get_most_probable_outcome(day_index) File "/home/originn/HMMs_Stock_Market/src/", line 149, in _get_most_probable_outcome outcome_score.append(self.hmm.score(total_data)) File "/home/originn/HMMs_Stock_Market/venv/lib/python3.10/site-packages/hmmlearn/", line 259, in score return self._score(X, lengths, compute_posteriors=False)[0] File "/home/originn/HMMs_Stock_Market/venv/lib/python3.10/site-packages/hmmlearn/", line 272, in _score X = check_array(X) File "/home/originn/HMMs_Stock_Market/venv/lib/python3.10/site-packages/sklearn/utils/", line 919, in check_array _assert_all_finite( File "/home/originn/HMMs_Stock_Market/venv/lib/python3.10/site-packages/sklearn/utils/", line 161, in _assert_all_finite raise ValueError(msg_err) ValueError: Input contains NaN.

Shouldn't in def predict_close_price_fut_days(self, day_index) the self.days be incemented?

GitHub Repository