File Exchange

image thumbnail


version 3.0.0 (159 KB) by Kevin Chng
it use Machine Learning in MATLAB to predict the buying-decision of Stock by using real life data.


Updated 12 Mar 2019

View Version History

View License

Overview :

In this script, it uses Machine Learning in MATLAB to predict buying-decision for stock. Using real life data, it will explore how to manage time-stamped data and select the best fit machine learning model. As common being widely known, preparing data and select the significant features play big role in the accuracy of model. In this example, it uses the technical indicators of today to predict the next day stock close price.
In this example, the trading strategy is if the close price is higher 1% than the open price in the same day, then we should buy stock at the openning of the stock market and sell it at the closing of the stock market.
In this example, it demostrates how to pre-processing the data for modelling and predict the decision by the model. New data of each date will be tabulated to re-train new model and find the best model for next day prediction. Interesting?

[Note : Not advocating any particular strategy, factors or methodology]

Highlights :
Handling downloaded data from Yahoo Finance using the timetable object
Selecting features based on domain knowledge
Machine Learning Modeling
Automate to re-train new model to incorproate new updated data for next prediction
Predicting the buying-decision
Experience the computational speed with/without parallel computing

Product Focus :
Financial Toolbox
Statistics and Machine Learning Toolbox
Parallel Computing Toolbox (Optional)

Cite As


Comments and Ratings (12)

Sinan Islam

Ali Nateghi

any suggestion for out-sample data?

Howard Labido

An interesting file to apply to my project: machine building.
Check my homepage for further info pls:

Abolfazl Nejatian

This is my forecasting code which allows you to predict your time series data with LSTM, CNN, and MLP Networks.

please visit my Mathworks account and download it from the link below:


interesting concept, did you or anyone ever try trade with it?

Wesley Sturdivant

there is an error in line 158 for mac users. The backslash should be a forward slash. Hope this helps someone

Sergiy Melnik

Unfortunately couldn't manage to get it work.
As stated in sample comments:"Year 2015 will be selected as train data used to predict the stock price in Year 2016."
So, we don't need load the stock data for 2016,2017,2018 because the goal is to predict the stock price in Year 2016.
However if you'll try to to limit imported stock data to '1-Jul-213' - '31-Dec-2015", in order to get predicted values for 2016 - got error "Undefined function or variable Response2016"

Ian Alferez


Mihai Cristea

Is there also a Video for this example?

Zachary David

Hi Kevin,
Unfortunately, there is an error in the code that incorrectly labels the "buy" and "Not buy" fields.

This is the logic for labeing "buy" days on Lines 121-124 (and again on Lines 133-136):
if OpenClose_2016.Open(i) < 1.01*OpenClose_2016.Close(i)
Response2016(i)="Not buy";

The if-statement will resolve to true even if the stock had an open to close return of 0. Indeed, the first row in your own resulttable shows a label of buy even though the stock returned 0.

The correct statement is: if OpenClose_2016.Open(i)*1.01 < OpenClose_2016.Close(i)

The larger problem with the original model is that even in-sample, the best model has a lower predictive accuracy (0.693) than if you were to predict "buy" every day (0.706). This means that the models are not learning anything at all.

Eduardo Soares

Dude, great code!
I don't know if the problem is in my Matlab, but the functions "rsindex" and "movavg" aren't working correctly. The first gives the error "Too many arguments" and the second gives the error "Lead argument must be less than or equal to lag argument".

Mary O'Connor

MATLAB Release Compatibility
Created with R2018a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!