In this section, we present how to connect Binance to MindsDB.
Binance is one of the world’s largest cryptocurrency exchanges. It’s an online platform where you can buy, sell, and trade a wide variety of cryptocurrencies. Binance offers a range of services beyond just trading, including staking, lending, and various financial products related to cryptocurrencies.
Binance provides real-time trade data that can be utilized within MindsDB to make real-time forecasts.
This handler integrates with the Binance API to make aggregate trade (kline) data available to use for model training and predictions.
Since there are no parameters required to connect to Binance using MindsDB, you can use the below statement:
CREATE DATABASE my_binance WITH ENGINE = 'binance';
By default, aggregate data (klines) from the latest 1000 trading intervals with a length of one minute (1m) each will be returned.
SELECT * FROM my_binance.aggregated_trade_data WHERE symbol = 'BTCUSDT';
To get a customized response we can pass open_time, close_time, and interval:
SELECT * FROM my_binance.aggregated_trade_data WHERE symbol = 'BTCUSDT' AND open_time > '2023-01-01' AND close_time < '2023-01-03 08:00:00' AND interval = '1s' LIMIT 10000;
Train a Model
Here is how to create a time series model using 10000 trading intervals in the past with duration of 1m.
CREATE MODEL mindsdb.btc_forecast_model FROM my_binance ( SELECT * FROM aggregated_trade_data WHERE symbol = 'BTCUSDT' AND close_time < '2023-01-01' AND interval = '1m' LIMIT 10000; ) PREDICT open_price ORDER BY open_time WINDOW 100 HORIZON 10;
For more accuracy, the limit can be set to a higher value (e.g. 100,000)
First let’s make a view for the most recent BTCUSDT aggregate trade data:
CREATE VIEW recent_btcusdt_data AS ( SELECT * FROM my_binance.aggregated_trade_data WHERE symbol = 'BTCUSDT' )
Now let’s predict the future price of BTC:
SELECT m.* FROM recent_btcusdt_data AS t JOIN mindsdb.btc_forecast_model AS m WHERE m.open_time > LATEST
This will give the predicted BTC price for the next 10 minutes (as the horizon is set to 10) in terms of USDT.