🐍 Ngày 43 Python 365 ngày | Phân tích dữ liệu giao dịch từ API Binance
🗓 Ngày 43: Phân tích dữ liệu giao dịch từ API Binance
Hôm nay, bạn sẽ học cách thu thập dữ liệu giao dịch từ Binance và thực hiện phân tích cơ bản với Python.
🎯 Mục tiêu
- Hiểu cấu trúc dữ liệu giao dịch từ Binance.
- Trích xuất dữ liệu giao dịch (trades).
- Phân tích khối lượng, giá trung bình và thời gian giao dịch.
🧰 Cài đặt công cụ
pip install python-binance pandas
🔌 Lấy dữ liệu giao dịch
from binance.client import Client import pandas as pd api_key = "your_api_key" api_secret = "your_api_secret" client = Client(api_key, api_secret) trades = client.get_recent_trades(symbol='BTCUSDT') df = pd.DataFrame(trades) print(df.head())
📊 Phân tích dữ liệu
df['price'] = df['price'].astype(float) df['qty'] = df['qty'].astype(float) # Tính tổng khối lượng total_volume = df['qty'].sum() # Giá trung bình avg_price = (df['price'] * df['qty']).sum() / total_volume print("Tổng khối lượng:", total_volume) print("Giá trung bình:", round(avg_price, 2))
📈 Vẽ biểu đồ giá và khối lượng
import matplotlib.pyplot as plt plt.figure(figsize=(10, 4)) plt.plot(df['price'], label='Giá BTC') plt.xlabel('Lệnh giao dịch') plt.ylabel('Giá') plt.title('Biến động giá BTC - gần nhất') plt.legend() plt.grid(True) plt.show()
📌 Bài tập gợi ý
- Vẽ biểu đồ giá và khối lượng theo thời gian.
- So sánh giá trung bình giữa BTCUSDT và ETHUSDT.
- Lưu dữ liệu về file .csv để huấn luyện mô hình sau này.
Phân tích dữ liệu là bước đầu tiên để hiểu thị trường và phát triển chiến lược giao dịch hiệu quả hơn!