Automate Real-Time Trading Data Forecasts
MindsDB enables you to automate AI workflows between any source of data and any AI/ML Model. The core building block of this automation is a job that allows anything in MindsDB to be run either on a timer (e.g. every day) or based on a trigger (e.g. when a new row is added to the database).
In this tutorial we will use a job to automate real-time forecasts for the BTC/USDT crypto price as Slack notifications.
Connect a data source
MindsDB can connect to any source of data - a database, warehouse, stream or app. Here, we’ll connect to Binance API to get a feed of real-time price information.
We now have access to the Binance API, which updates data every minute so the interval between data rows in one minute. Let’s take a look at the data for the crypto pair we’ll ultimately use.
Deploy a time-series model
The Binance trade data is updated every minute. As a trader, you might want to predict the open prices for the next 10 minutes - so let’s set it up. We’ll use a forecasting engine called Lightwood for ease and speed but you’re also able to train your own model if you like.
Here’s what’s going on in the CREATE MODEL
statement:
-
CREATE MODEL
: It is used to create, train, and deploy an ML model. By default, MindsDB’s AutoML will automatically choose the best model for your data but this can be overridden (docs). -
FROM
: Here, we specify which of our integrations to use. Anything that is between the parentheses is the data that will be used to train the model - here, the latest Binance data from the connection we’ve already made to Binance is used. -
PREDICT
: It specifies the target column - here, the open price of the BTC/USDT trading pair is to be forecasted.
The following elements are unique to forecasting models in MindsDB:
-
As it is a forecasting model, you should use ORDER BY to order the data by a date column - here, it is the open time when the open price takes effect.
-
The
WINDOW
clause defines the window the model looks back at while making forecasts - here, the model looks back at sets of 100 rows (intervals of 100 minutes). -
The
HORIZON
clause defines how many rows into the future the model will forecast - here, it forecasts the next 10 rows (the next 10 minutes).
After executing the CREATE MODEL
statement as above, you can check the progress status using this query:
The time it takes to train the model depends on the amount of training data. In this case, it takes about 2 minutes on MindsDB Cloud. Once the status reads complete, we’re ready to make some forecasts!
Make forecasts
First, we’ll save the Binance data into a view, which will be the input data for making forecasts.
Next, we query for forecasts by joining the model with the Binance input data table.
Please note that the Binance data is updated every minute, so every time you query the model, you will get forecasts for the following 10 minutes (as defined by the HORIZON
clause).
The next thing we can do is automate price alerts. Here we’ll choose Slack as our preferred place to receive the alerts but this could be any other system that MindsDB integrates with.
Connect Slack
Follow these instructions to set up your Slack app and generate a Slack bot token. Once you get the Slack bot token and integrate your Slack app into one of the Slack channels, you can connect it to MindsDB.
Here is how to send messages to a Slack channel:
So, let’s put it all together again.
Automate real-time forecasts
By now you have connected Binance to MindsDB, deployed and trained a time-series model, and set up the Slack connection. Let’s create a job that will retrain your model periodically using the latest Binance data (which allows us to keep improving the accuracy and performance of the model) and send real-time forecasts of the BTC/USDT trading pair for the next 10 minutes as Slack notifications.
So there you have it - you’ve successfully built a fully automated end-to-end alert system for crypto prices.
Happy trading!