Predicting the State of the Market Using MATLAB
For the IAQF competition, get started with some of the basics of MATLAB, such as the Live Editor, navigating the documentation, and importing financial data. Learn some of the approaches you can use to predict the state of the market (bear, bull, or static), such as Markov models, Kalman filters, random forests, etc. Alex demonstrates how to backtest your trading strategies, and finally how to use the Live Editor to write your paper and collaborate on the same file and code with your team members.
In the investing world, the market conditions are described by bear, bull or static and as an investor the direction of the market will have a huge impact on your portfolio.
My name is Alex Roumi, and I work for the marketing development group at MathWorks. I am here to give you an idea of relevant tools, materials, and examples you can refer to, to succeed in your competition.
This video will introduce you to MATLAB and some of the basic features to get started like the live editor and how to navigate the documentation. Then I will show, you how to bring data into MATLAB. In your case study you will be using the Russell 3000 index data and the 3-month T-Bill rate. I will also talk about some of the approaches you could use to predict the state of the market such as Markov models, Kalman filters, random forests, etc. I will show you how to backtest your trading strategies, and finally how to use live editor to write your paper and collaborate on the same file and code with your team members.
For this competition, MathWorks is providing complimentary software. If your team is participating in this competition and needs software, you can request software from the landing page. Click on the software request form. You have to sign in or create a MathWorks account before completing this form. This form must be completed by the team leader and after you fill it, you will receive instructions for downloading the software and distribute it to your team members. You will have access to all relevant toolboxes such as econometrics, financial toolbox as well as machine learning, deep learning which will give you plenty of resources to complete your case study.
To start I just want to draw your attention to the layout in case you’re not familiar with. What I recommend doing here, is come to the home tab, and come out here to layout and then down here in the show section, make sure all these are selected. You can see here the working folder, where your code and other functions and files are saved. The command window you use to enter statements, say a = 2; and the workspace contains variable that you create or import from other files. It might happen that one of these are broken and come off apart as a separate window. All you must do here is just come to this little arrow, click on that, and come here to dock. The last thing I want to show before moving onto other topics related to your case is the MATLAB live editor. Again, from the home tab, I will click on the new live script button. Here is a live editor code I already have and for a better view, I will just hide the workspace, current folder, and command window but you can always restore them back. What I love about editor that you can see the results and the code that produced them all in one place. From the insert tab, you can add equations, images, hyperlinks, and formatted text to document your analysis as a paper as well as share your work with others so they can review, evaluate, and reproduce your results in the same live environment. To run the whole code, you can click on the play icon in the live editor tab, or you can run it section by section by clicking on this blue bar here. You can add section breaks or merge sections. You can also decide how you want to visualize the output to the right of the code, in line with the code, or hide the code and keep the result which will be helpful to submit the paper, by keeping the analysis and the results and submit the code at a later stage, separately.
Now let’s move to the documentation part. All MATLAB functions have supporting documentation that includes examples and describes the function inputs, outputs and calling syntax. There are several ways to access this information from the command line. You can open the function in a separate window using the doc command by writing for example doc followed by the function name. You can also get some hints. Just write the function and after you open the parenthesis, pause for a second and you get the function input argument. You can access the complete product documentation by clicking on the help icon on the home tab. This is the main page of the documentation of all the toolboxes. In your case study you will be using mainly the computational finance area. As you can see there are several toolboxes. For example, Datafeed toolbox will help you import financial or economic data from data providers. The econometrics toolbox will help you learn more about Markov models, regime switching models, Kalman filter. Once you’ve built your trading strategies, you can use the backtesting feature from financial toolbox. Another important toolbox you will be using during this competition is the statistics and machine learning toolbox, where you will use different techniques such as classification, neural networks, random forests etc. But don’t worry about it for now, we will come to these later.
In the time left together, we have several topics to cover. First let’s see how we can import data into MATLAB. If you have access on campus on many data service providers like Bloomberg, FRED, Refinitiv. You can check Datafeed toolbox on how to retrieve historical data or real-time data. As an example, let’s say, you want to get the 3-month treasury bill from FRED. Click here on Federal reserve economic data (FRED) and then choose Retrieve historical data using FRED. I used the same example here to bring the 3-month treasury bill, from 2018 till 2021. And all I have to do is run this section to get the data. Assuming now you don’t have access to Bloomberg or FRED, and you want to import free data from yahoo finance. From file exchange, download this file: “yahoo finance and quandl data downloader” and make sure you save it in the same working folder. You can use these two functions: getMarketDataViaYahoo or getMarketDataViaQuandl to get the data you want. Back to our code, I am using the getMarketDataViaYahoo function. Enter the ticker symbol for the Russell3000, start date January 2018 and end date (Dec 2021). Run this section and you can see the Russell 3000 data which include the Date, opening price, high, low, close. You probably just want to use the adjusted closing price column at the end. Now let’s say you want to join these two tables into one table, and you don’t know how to write the syntax in MATLAB, the way I have it here. What I would do is, I go to Task here, and search for join and let MATLAB do it for you. You can interactively select the tables, in our case, Russell and dtb3. And choose date as the merging variable. Click on the 3 dots you see here and select code only. I got the same syntax I had at the beginning. Then what I am going to do is filtering the table and keeping the adjusted closing prices for the Russell. You can also plot data that are on different scales in a single plot. In our case visualize Russell and the 3month T-bill. You can of course give it a title, label the axis, add a legend, etc.…
Going back to the main problem of this competition where you have to predict the three states of the market: bear, bull or static. You would think of it as sort of 3 states space and the data could be in one of those states. And you want to have a model that switches among the states depending on certain indicators and so on of the data. Therefore, Markov models. Going back to the documentation, and in particular econometrics toolbox, regime – switching models, you can learn more about Markov switching model which uses a Markov chain as the underlying mechanism for switching among states and then there is the threshold switching models that uses what’s called the threshold switching where you have a particular time series that crosses a certain threshold and the model switches state. So, either one of those could be helpful to model exactly what you are trying to do which are the states of the overall market.
So, to construct a threshold transition switching model, first, you create a vector of ARIMA sub model, plus a switch which would be composed of one of the thresholds switching objects, you can choose between discrete threshold transitions or smooth threshold transitions, and you glue those together with a tsVAR constructor to create a threshold switching model. And each of these modeling frameworks in econometrics toolbox let you estimate the model parameters from your data, simulate specified models over multiple paths and forecasting model means from current data.
A classic example found in the tsVAR is to predict if the market is going through recession or expansion. You can read and learn more how to use these threshold models to predict the state of the market. But in your case, you will be having three states of the market instead of two as we will be predicting if the market is bull, bear or static.
If you want to use Kalman filters, again with econometrics toolbox, you can select state-space models, you can filter states and use the Kalman filter to estimate the values of a model parameters and state space forecasts.
Many of you may think about this problem as a classification problem, as you have to classify the market in three states, bear, bull or static. Let’s go to the classification section in the statistics and machine learning toolbox, where we have tons of examples on classification trees, random forests, neural networks, genetic programming, and all other approaches you can use to predict the market.
Once you have developed a technique to predict the state of the market, and you want to compare trading strategies to prove the validity of your approach. You can use the backtest strategy framework from financial toolbox which lets you define investment strategies, run backtests and generate performance metrics for your strategies from historical or simulated market data. For more information on backtesting, you can check these examples in the doc as you can do your backtesting using trading signals or watch the video provided on the landing page of this student competition.
Finally, once you have completed your project and you just want to share and collaborate with your team members. On mathworks.com, click on these 9 squares and choose MATLAB drive. Type in your email address and password, you can share and collaborate with others using a shared folder, by sending them either a personal invite or a view – only link. Just right click on the folder and select share, create a view-only link. Or you can invite members. Enter your team members email address. Make sure they can edit here and click on send.
Finally, I just want to remind you that live editor is the perfect environment to write your case study, hide the code and have all your results in line with your analysis, and when you are ready to submit your work, you can download it as pdf. From the live editor tab, click on print, select pdf, print, and save the file. You can have this paper ready to submit by having your analysis and results all in one paper.
If you have any question, please don’t hesitate to reach out and connect with us on the about this competition. We are here to help you and we wish you all the best in this competition.
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.