Dự đoán giá tiền điện tử với Machine Learning

Trong bài viết này chúng ta sẽ dự đoán giá của BTC và ETH thông qua thuật toán Machine learning. Dữ liệu sẽ được lấy từ website: https://finance.yahoo.com/ Trước tiên chúng ta sẽ cài đặt yfinance API: !pip install yfinance đối với Google Colab pip install yfinance đối với jupyterLab hoặc jupyter Notebook Dự đoán

Trong bài viết này chúng ta sẽ dự đoán giá của BTC và ETH thông qua thuật toán Machine learning. Dữ liệu sẽ được lấy từ website: https://finance.yahoo.com/

Trước tiên chúng ta sẽ cài đặt yfinance API:

!pip install yfinance

đối với Google Colab

pip install yfinance

đối với jupyterLab hoặc jupyter Notebook

Dự đoán giá BTC

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())

Trong đoạn mã trên, chúng ta đã thu thập dữ liệu mới nhất về giá Bitcoin trong 730 ngày qua. Bây giờ, hãy xem hình dạng của tập dữ liệu này để xem liệu chúng ta có đang làm việc với 730 hàng hay không:

Vì vậy, tập dữ liệu chứa 731 hàng, trong đó hàng đầu tiên chứa tên của mỗi cột. Bây giờ, chúng ta sẽ biểu diễn sự thay đổi về giá bitcoin cho đến ngày hôm nay bằng cách sử dụng biểu đồ nến:

import plotly.graph_objects as go 
figure = go.Figure(data = [go.Candlestick(x = data["Date"],
                                          open = data["Open"],
                                          close = data["Close"],
                                          low = data["Low"],
                                          high = data["High"])])
figure.update_layout(title = "Bitcoin Price Analysis", xaxis_rangeslider_visible = True)
figure.show()

Cột Close trong tập dữ liệu chứa các giá trị mà chúng ta cần dự đoán. Vì vậy, chúng ta hãy xem mối tương quan của tất cả các cột trong dữ liệu liên quan đến cột Close:

correlation = data.corr()
print(correlation["Close"].sort_values(ascending = True))

Dự đoán giá tương lai của tiền điện tử dựa trên vấn đề phân tích chuỗi thời gian. Thư viện AutoTS trong Python là một trong những thư viện tốt nhất để phân tích chuỗi thời gian. Vì vậy, ở đây chúng ta sẽ sử dụng thư viện AutoTS để dự đoán giá bitcoin trong 30 ngày tới:

Lệnh cài đặt AutoTS trên Google Colab:

!pip install autots

Lệnh cài đặt AutoTS trên Jupyter Notebook

pip install autots
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)

Dự đoán giá ETH

Các bước và code sẽ tương tự như trên, các bạn chỉ cẩn thay cặp “BTC-USD” thành “ETH-USD”

import numpy as np
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("ETH-USD", start = start_date, end = end_date, progress = True)
data["Date"] = data.index
data = data[["Date", "Open", "High", "Low", "Close", "Adj Close", "Volume"]]
data.reset_index(drop = True, inplace = True)
print(data.head())

import plotly.graph_objects as go 
figure = go.Figure(data = [go.Candlestick(x = data["Date"],
                                          open = data["Open"],
                                          close = data["Close"],
                                          low = data["Low"],
                                          high = data["High"])])
figure.update_layout(title = "ETH Price Analysis", xaxis_rangeslider_visible = True)
figure.show()

correlation = data.corr()
print(correlation["Close"].sort_values(ascending = True))

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)

Nguồn: viblo.asia

Bài viết liên quan

9 Mẹo lập trình Web “ẩn mình” giúp tiết kiệm hàng giờ đồng hồ

Hầu hết các lập trình viên (kể cả những người giỏi) đều tốn thời gian x

Can GPT-4o Generate Images? All You Need to Know about GPT-4o-image

OpenAI‘s GPT-4o, introduced on March 25, 2025, has revolutionized the way we create visual con

Khi nào nên dùng main, section, article, header, footer, và aside trong HTML5

HTML5 đã giới thiệu các thẻ ngữ nghĩa giúp cấu trúc nội dung web một cách có

So sánh Webhook và API: Khi nào nên sử dụng?

Trong lĩnh vực công nghệ thông tin và phát triển phần mềm, Webhook và API là hai th