You must have heard or invested in any cryptocurrency once in your life. It is a digital medium of exchange that is encrypted and decentralized. Many people use cryptocurrencies as a form of investing because it gives great returns even in a short period. Bitcoin, Ethereum, and Binance Coin are among the popular cryptocurrencies today. If you want to know how to predict the future prices of any cryptocurrency with machine learning, this article is for you. In this article, I will walk you through the task of cryptocurrency price prediction with machine learning using Python.
Cryptocurrency Price Prediction with Machine Learning
Predicting the price of cryptocurrencies is one of the popular case studies in the data science community. The prices of stocks and cryptocurrencies don’t just depend on the number of people who buy or sell them. Today, the change in the prices of these investments also depends on the changes in the financial policies of the government regarding any cryptocurrency. The feelings of people towards a particular cryptocurrency or personality who directly or indirectly endorse a cryptocurrency also result in a huge buying and selling of a particular cryptocurrency, resulting in a change in prices.
In short, buying and selling result in a change in the price of any cryptocurrency, but buying and selling trends depend on many factors. Using machine learning for cryptocurrency price prediction can only work in situations where prices change due to historical prices that people see before buying and selling their cryptocurrency. So, in the section below, I will take you through how you can predict the bitcoin prices (which is one of the most popular cryptocurrencies) for the next 30 days.
Cryptocurrency Price Prediction using Python
I’ll start the task of Cryptocurrency price prediction by importing the necessary Python libraries and the dataset we need. For this task, I will collect the latest Bitcoin prices data from Yahoo Finance, using the yfinance API. This will help you collect the latest data each time you run this code:
import pandas as pd import yfinance as yf import datetime from datetime import date, timedelta today = date.today() d1 = today.strftime("%Y-%m-%d") end_date = d1 d2 = date.today() - timedelta(days=730) d2 = d2.strftime("%Y-%m-%d") start_date = d2 data = yf.download('BTC-USD', start=start_date, end=end_date, progress=False) data["Date"] = data.index data = data[["Date", "Open", "High", "Low", "Close", "Adj Close", "Volume"]] data.reset_index(drop=True, inplace=True) print(data.head())
Date Open High ... Close Adj Close Volume 0 2019-12-28 7289.031250 7399.041016 ... 7317.990234 7317.990234 21365673026 1 2019-12-29 7317.647461 7513.948242 ... 7422.652832 7422.652832 22445257702 2 2019-12-30 7420.272949 7454.824219 ... 7292.995117 7292.995117 22874131672 3 2019-12-31 7294.438965 7335.290039 ... 7193.599121 7193.599121 21167946112 4 2020-01-01 7194.892090 7254.330566 ... 7200.174316 7200.174316 18565664997 [5 rows x 7 columns]
In the above code, I have collected the latest data of Bitcoin prices for the past 730 days, and then I have prepared it for any data science task. Now, let’s have a look at the shape of this dataset to see if we are working with 730 rows or not:
So the dataset contains 731 rows, where the first row contains the names of each column. Now let’s visualize the change in bitcoin prices till today by using a candlestick chart:
import plotly.graph_objects as go figure = go.Figure(data=[go.Candlestick(x=data["Date"], open=data["Open"], high=data["High"], low=data["Low"], close=data["Close"])]) figure.update_layout(title = "Bitcoin Price Analysis", xaxis_rangeslider_visible=False) figure.show()
The Close column in the dataset contains the values we need to predict. So, let’s have a look at the correlation of all the columns in the data concerning the Close column:
correlation = data.corr() print(correlation["Close"].sort_values(ascending=False))
Adj Close 1.000000 Close 1.000000 High 0.998933 Low 0.998740 Open 0.997557 Volume 0.334698 Name: Close, dtype: float64
Cryptocurrency Price Prediction Model
Predicting the future prices of cryptocurrency is based on the problem of Time series analysis. The AutoTS library in Python is one of the best libraries for time series analysis. So here I will be using the AutoTS library to predict the bitcoin prices for the next 30 days:
from autots import AutoTS model = AutoTS(forecast_length=30, frequency='infer', ensemble='simple') model = model.fit(data, date_col='Date', value_col='Close', id_col=None) prediction = model.predict() forecast = prediction.forecast print(forecast)
Close 2021-12-28 57865.012345 2021-12-29 54259.592685 2021-12-30 53794.634938 2021-12-31 54365.964301 2022-01-01 55371.531945 2022-01-02 57220.503886 2022-01-03 57132.487546 2022-01-04 58021.727065 2022-01-05 58376.081818 2022-01-06 59931.323291 2022-01-07 60168.816716 2022-01-08 60617.974204 2022-01-09 58785.722512 2022-01-10 55180.302852 2022-01-11 54715.345105 2022-01-12 55286.674468 2022-01-13 56292.242112 2022-01-14 58141.214053 2022-01-15 58053.197713 2022-01-16 58942.437232 2022-01-17 59296.791985 2022-01-18 60852.033458 2022-01-19 61089.526883 2022-01-20 61538.684371 2022-01-21 59706.432679 2022-01-22 56101.013019 2022-01-23 55636.055272 2022-01-24 56207.384635 2022-01-25 57212.952279 2022-01-26 59061.924220
So this is how you can use machine learning to predict the price of any cryptocurrency by using the Python programming language.
Buying and selling result in a change in the price of any cryptocurrency, but buying and selling trends depend on many factors. Using machine learning for cryptocurrency price prediction can only work in situations where prices change due to historical prices that people see before buying and selling their cryptocurrency. I hope you liked this article on cryptocurrency price prediction with machine learning using Python. Feel free to ask your valuable questions in the comments section below.