DEV Community

Rutam Prita Mishra
Rutam Prita Mishra

Posted on • Updated on

Forecast Tesla Stock Prices using MindsDB



If we talk about AutoML, then one of the most amazing open-source products that comes to my mind is MindsDB. MindsDB enhances the existing databases and modifies the tables in them to act as AI Tables that are able to train robust Predictior models using the data that exists.

All this without the hassle of learning how to code such models in Python or dealing with the dependencies or maintaining them at a later stage. With MindsDB, you can use simple SQL-like statements and get accurate predictions in a click.

In this tutorial, we will learn how we can forecast the stock prices of Tesla using MindsDB. You can download the required dataset from here which we will use in this tutorial.

Importing Data into MindsDB Cloud

We will use MindsDB Cloud to complete this tutorial. Make sure you follow all the steps below to complete training a Predictor Model.

Step 1: Log in to the MindsDB account with your credentials. You can also sign up for a new account and it's absolutely free.

MindsDB Sign Up

Step 2: Once you're signed in, the MindsDB Cloud console opens up. Now click on the Add data button from the top right corner. This should open up the Select your datasource page.

Add Data

Step 3: Now select Files from the top tab instead of Databases and then hit the Import File section.

Import File

Step 4: Now browse and open the .CSV file that we just downloaded above, provide a name for the table in the Table Name field and then hit the Save and Continue button to create it.

Import File

Step 5: After the Table is created, the control takes us back to the MindsDB Cloud console. You will find two simple queries in the Query Editor now. Let's execute them one by one to confirm whether the data import was successful or not.

Enter fullscreen mode Exit fullscreen mode

Find the name of the table we created in the list of returned table names.

Show tables

SELECT * FROM files.Tesla LIMIT 10;
Enter fullscreen mode Exit fullscreen mode

This should return 10 data records from the table Tesla.

Table Records

This confirms that we have the correct data records with us and now we can move on to the next part of this tutorial.

Creating a Predictor Model

Creating a Predictor model using MindsDB is as easy as just firing up a simple SQL query. So, let's see how easily we can create this model.

Step 1: MindsDB provides a CREATE PREDICTOR that will come in handy now as we want to create and train a new model using the dataset, we have with us. You can find the query below.

CREATE PREDICTOR mindsdb.tesla_predictor     (Name of the Predictor)
FROM files
(SELECT * FROM Tesla)              (Name of the Table)
PREDICT Close                      (Target Value)
ORDER BY value_date                (Ordering data based on Date)
WINDOW 120                         (Model picks up last 120 records)
HORIZON 60;                        (To Forecast the future 60 outcomes)
Enter fullscreen mode Exit fullscreen mode

The query should return a successful status.

Create Predictor

Step 2: The model can take some time to be ready. Meanwhile, we can check its status using the command below.

SELECT status
FROM mindsdb.predictors
WHERE name = 'tesla_predictor';
Enter fullscreen mode Exit fullscreen mode

If the status is complete, then the model is ready. However, if the status is generating or training, then wait for a while until you get the status as complete.

Predictor Status

Note: This kind of Predictor is an advanced one as we will be using it to predict a future outcome value based on the historical data that we have in the dataset. Such kind of predictions fall under the Timeseries category.

Understanding the Predictor Model

As the Predictor model is now ready, let us first understand what lies under the hood. So, MindsDB provides us with three ways to describe our model to retrieve further insights.

  • By Features
  • By Model
  • By Model Ensemble

By Features

If you're interested to find out the roles that each of the columns serve for the model or the encoders used on them, then you can try to describe the predictor based on it feature.

DESCRIBE mindsdb.tesla_predictor.features;
Enter fullscreen mode Exit fullscreen mode

Model Features

By Model

You can choose to describe it by model if you want to figure out the underlying candidate models that were used during the training along with the one that was finally selected for the Predictor. The selected candidate will have the value 1 in its selected column.

DESCRIBE mindsdb.tesla_predictor.model;
Enter fullscreen mode Exit fullscreen mode

Predictor Model

By Model Ensemble

If you're interested to further deep dive and find out how the candidate model was selected, then you can try describing the predictor with ensemble. This returns a JSON result with all the parameters that helped to determine the best candidate model out of the available ones.

DESCRIBE mindsdb.tesla_predictor.ensemble;
Enter fullscreen mode Exit fullscreen mode

Model Ensemble

We have now completed understanding the model we just trained. Time to start forecasting things now!

Forecasting Target Values

As usual, MindsDB also provides simple SQL statements to predict/forecast values from the model. So, let's figure out how we can forecast values.

Before we proceed, we need to understand the basic scenario here. So, we have a dataset that lists out the closing stock values of Tesla over a period of time. Now, with this predictor model, we will try to forecast the closing value after the last record available in the dataset. We can do that by using another keyword LATEST for the dates now.

The query will be simple SELECT type where we will join the predictor model along with the data table and fetch the Forecasts.

SELECT T.value_date as date,
       T.Closing as Forecast,
FROM mindsdb.tesla_predictor as T
JOIN files.Tesla as P
WHERE P.value_date > LATEST
Enter fullscreen mode Exit fullscreen mode

This query should return us with the forecasts of up to 2 months (60 Days) after the last date that was available in the dataset. The last available date in this dataset was 16-09-2022 and 60 days from that day ends at 14-11-2022 which is the last forecast date in the result table.


Note: You can forecast the values of further future dates by simply increasing the value of HORIZON to more than 60 days. To increase the accuracy, you can also opt to increase the value of WINDOW to more than 120 days.


We have now reached the end of this tutorial. Let us summarize all the tasks we did above as a list below.

  • We created a MindsDB Cloud Account.
  • We imported a dataset into MindsDB Cloud.
  • We created and trained a Predictor Model for Timeseries Forecast.
  • We gained insights about the model using DESCRIBE.
  • We forecasted the Tesla Stock Prices for a future date.

Now, it's time for all of you to create your own models and try forecasting some interesting values. If you need some suggestions, then I would love to drop some ideas like Bitcoin Value Forecast, Ethereum Value Forecast, Gold Price Forecast, etc. Be creative, train something funky and share it with the community.

Lastly, before you close this page, don't forget to drop a LIKE if you liked this tutorial and also let me know if you have any feedback. You can also suggest something specific you wish to learn about MindsDB and I will try to cover that up as well.

MindsDB Sponsor

Sponsorship Badge

Top comments (1)

prxkhxr10 profile image

will there be no pre-processing of data before training the model?