jays-code-collection / hmms_stock_market Goto Github PK
View Code? Open in Web Editor NEWContains all code related to using HMMs to predict stock market prices.
License: GNU General Public License v2.0
Contains all code related to using HMMs to predict stock market prices.
License: GNU General Public License v2.0
when i use this model, i got the accuracy about 50%( 31 true in 60 prediction). I want to ask about the accuracy about your test.
thanks~(maybe there are some wrong on my environment so that i can't use the code directly)
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/stock_analysis.py", line 470, in <module>
main()
File "/home/originn/HMMs_Stock_Market/src/stock_analysis.py", line 465, in main
use_stock_predictor(company_name, start, end, future, metrics, plot, out_dir)
File "/home/originn/HMMs_Stock_Market/src/stock_analysis.py", line 354, in use_stock_predictor
future_pred_close = stock_predictor.predict_close_prices_for_future()
File "/home/originn/HMMs_Stock_Market/src/stock_analysis.py", 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/stock_analysis.py", line 223, in predict_close_price_fut_days
) = self._get_most_probable_outcome(day_index)
File "/home/originn/HMMs_Stock_Market/src/stock_analysis.py", 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/base.py", 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/base.py", line 272, in _score
X = check_array(X)
File "/home/originn/HMMs_Stock_Market/venv/lib/python3.10/site-packages/sklearn/utils/validation.py", line 919, in check_array
_assert_all_finite(
File "/home/originn/HMMs_Stock_Market/venv/lib/python3.10/site-packages/sklearn/utils/validation.py", 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?
Pre-installation steps are not given.
The fix is as follows. Add to your import:
import yfinance as yf
then in the _split_train_test_data method under the try: change as follows:
yf.pdr_override()
used_data = data.get_data_yahoo(self.company, start=self.start_date, end=self.end_date)
Hello,
I'm trying to import stock data from a csv or xls file, avoiding yahoo, without success because I'm facing many errors and I'm almost new to Python code... In this case this is the example file, "stock.csv/xls". If it's possible could you please give me a tips to import stock data from local file? Thanks a lot in advance and sorry for the stupid question...
p.s plz don't blame me for my question, if it's not possible feel free to close the "issues".
Jean
stock.csv
stock.xls
previous_data_start_index = max(0, day_index - self.n_latency_days) previous_data_end_index = max(0, day_index - 1) previous_data = self.test_data.iloc[previous_data_end_index: previous_data_start_index]
previous_data_start_index is smaller than previous_data_end_index , so test_data.iloc[previous_data_end_index: previous_data_start_index] will be empty?
Running the following:
python stock_analysis.py -n AAPL -s 2018-01-01 -e 2020-12-15 -o ./appl_test/ -p True -f 8 -m True
Graph shouldn't be showing "actual_close" for dates that haven't occurred yet, right?
I figured there had to be something crazy going on for the lines to be matching so close (like a bug/mistake). So I just ran the example for myself to see what the graph would look like.
I've yet to dig into the code. I've re-read the README a few times to see if I missed something.
Something else, I have no idea what the following means (I'm a complete newb to this):
-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
I don't even know what to ask. Maybe my question is, what is there to predict in historical data?
Depend on which python version you are using you can use f-strings.
You can use
save_dir = f"{out_dir}/{stock_name}_results_plot.png"
instead of this
save_dir = out_dir + '/' + str(stock_name) + 'results_plot' + '.png'
or
out_name = f"{out_dir}/{company_name}_HMM_Prediction_{round(mse, 6)}.csv"
instead of
out_name = out_dir + '/' + str(company_name) + '_HMM_Prediction_' + str(round(mse, 6)) + '.csv'
Simply looks cleaner.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.