Forecast Tesla Stock Prices using MindsDB
Tags: tutorial, database, mindsdb, machinelearning
Published: 2022-10-21 20:25:17

![Cover](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9rv5s0drwbpsqe6trkwn.gif)

## Introduction

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](https://www.kaggle.com/datasets/harshsingh2209/tesla-stock-pricing-20172022) 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](https://cloud.mindsdb.com/login) to the MindsDB account with your credentials. You can also [sign up](https://cloud.mindsdb.com/register) for a new account and it's absolutely free.

![MindsDB Sign Up](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/75up3y7nsfrt1i298yhw.png)

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o3f5nqjhmi2caihze0ai.png)

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

![Import File](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/10jv1qqndhq43i7vfozt.png)

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/51vep764d8cw8zflka0k.png)

**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.

``` sql
SHOW TABLES FROM files;
```
Find the name of the table we created in the list of returned table names.

![Show tables](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xizjyqe38t1r4ftw4pqd.png)

``` sql
SELECT * FROM files.Tesla LIMIT 10;
```

This should return 10 data records from the table `Tesla`.

![Table Records](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eh2xdfvobxlj5q7l1d4c.png)

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.

``` SQL
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)
```

The query should return a successful status.

![Create Predictor](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jf66u74othoelcscwy20.png)

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

``` sql
SELECT status
FROM mindsdb.predictors
WHERE name = 'tesla_predictor';
```

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s2hlm0qygobbfg5ele7m.png)

> 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.

``` sql
DESCRIBE mindsdb.tesla_predictor.features;
```

![Model Features](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o360jbgmxluna3etvm2p.png)

### 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.

``` sql
DESCRIBE mindsdb.tesla_predictor.model;
```

![Predictor Model](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cazxc2jmwttq5syuy8z2.png)

### 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.

``` sql
DESCRIBE mindsdb.tesla_predictor.ensemble;
```

![Model Ensemble](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6trk5u828ebxm1gxdszi.png)

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.

``` sql
SELECT T.value_date as date,
       T.Closing as Forecast,
       Closing_explain
FROM mindsdb.tesla_predictor as T
JOIN files.Tesla as P
WHERE P.value_date > LATEST
LIMIT 60;
```

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.

![Result](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ju424pgxknuk3sfkl1w0.png)

> 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.


## Conclusion

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/54redaz26efthtchdvlh.png)](https://mindsdb.com/)

[![Sponsorship Badge](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6ritjzew9z0v0fpfuhag.png)](https://github.com/sponsors/Rutam21)