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

Những ngành nghề AI có thể thay thế dần trong tương lai.

Những ngành nghề AI có thể thay thế trong tương lai gần Dựa trên các báo cáo và

Tạo Subdomain miễn phí với is-a.dev 🤪

Cuối tuần mọi người thế nào, mình thì rảnh quá lướt Facebook, tớ tình cờ th

Dùng TailwindCSS v4 trong SpringBoot + JTE

Giới thiệu JTE là gì? JTE (Java Template Engine) là một template engine an toàn, nhẹ và

WebGL và Three.js cho hệ thống thử đồ ảo

Giới thiệu Với sự phát triển nhanh chóng của thương mại điện tử, kỳ vọng