🎯Framework ML dự đoán VNINDEX tăng/giảm phiên tiếp theo
Ta build một framework ML dự đoán VNINDEX tăng/giảm phiên tiếp theo theo kiểu quant cơ bản nhưng đúng bài bản.
Mình sẽ làm 4 phần:
Chuẩn bị dữ liệu
Feature engineering
Train model
Đánh giá & cảnh báo thực tế
🎯 Mục tiêu
Dự đoán:
VNINDEX ngày T+1 tăng (1) hay giảm (0)
🧠 1️⃣ Ý tưởng mô hình
Input features:
Return hôm nay
MA5 – MA20
RSI14
Volume change
Volatility 5 ngày
Output:
1 nếu Close(T+1) > Close(T)
0 nếu ngược lại
Model:
Logistic Regression (ổn định, dễ hiểu)
Hoặc Random Forest (mạnh hơn)
🛠 2️⃣ Code đầy đủ (chạy được luôn)
Giả sử anh có file:
vnindex.csv
Dạng:
date,open,high,low,close,volume
🔥 Code build model
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
# --- 1) Load data ---
df = pd.read_csv("vnindex.csv")
# --- 2) Feature engineering ---
df["return"] = df["close"].pct_change()
df["ma5"] = df["close"].rolling(5).mean()
df["ma20"] = df["close"].rolling(20).mean()
df["ma_diff"] = df["ma5"] - df["ma20"]
df["vol_change"] = df["volume"].pct_change()
df["volatility5"] = df["close"].rolling(5).std()
# RSI 14
delta = df["close"].diff()
gain = delta.clip(lower=0)
loss = -delta.clip(upper=0)
avg_gain = gain.rolling(14).mean()
avg_loss = loss.rolling(14).mean()
rs = avg_gain / avg_loss
df["rsi"] = 100 - (100 / (1 + rs))
# --- 3) Label ---
df["label"] = (df["close"].shift(-1) > df["close"]).astype(int)
df = df.dropna()
# --- 4) Chọn features ---
features = ["return", "ma_diff", "vol_change", "volatility5", "rsi"]
X = df[features]
y = df["label"]
# --- 5) Train/Test split ---
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, shuffle=False
)
# --- 6) Train model ---
model = LogisticRegression()
model.fit(X_train, y_train)
# --- 7) Predict ---
y_pred = model.predict(X_test)
print("Accuracy:", accuracy_score(y_test, y_pred))
# --- 8) Dự đoán phiên mới nhất ---
latest = X.iloc[-1:].values
prediction = model.predict(latest)[0]
print("Dự đoán phiên tới:", "TĂNG 📈" if prediction == 1 else "GIẢM 📉")
📊 3️⃣ Kết quả mong đợi
Nếu Accuracy:
~50% → gần random
52–55% → có edge nhỏ
58% → đáng nghiên cứu thêm
Lưu ý:
Trong trading, 53% winrate với risk/reward tốt đã đủ kiếm tiền.
⚠️ 4️⃣ Quan trọng nhất (quant mindset)
Đừng nhìn accuracy một mình.
Cần kiểm tra:
Confusion matrix
Precision / Recall
Backtest PnL
Max drawdown
🧨 5️⃣ Những lỗi mà 90% người mới mắc
Shuffle dữ liệu time-series (sai nghiêm trọng)
Dùng future data (lookahead bias)
Không chia train theo thời gian
Không tính phí giao dịch
📈 6️⃣ Nếu muốn nâng cấp
Ta có thể:
Thêm MACD, Bollinger Bands
Dùng Random Forest / XGBoost
Dùng LSTM cho chuỗi thời gian
Build backtest framework
🎯 Góc nhìn thật với Tiến sĩ M
VNINDEX là index:
Bị ảnh hưởng bởi trụ (VIC, VCB, VHM…)
Tin tức vĩ mô
Dòng tiền ETF
Model kỹ thuật thuần giá thường chỉ có edge nhỏ.
Muốn mạnh hơn → phải thêm:
Data liên thị trường
USD index
Lãi suất
Khối ngoại
Nếu anh muốn, mình có thể:
🔹 Build luôn mini backtest engine
🔹 So sánh Logistic vs RandomForest
🔹 Hoặc làm phiên bản chuyên cho short
Anh muốn đi theo hướng “quant research nghiêm túc”
hay “tool thực chiến nhanh gọn”?
✨🤖 𝗠𝗮𝗱𝗲 𝗯𝘆 𝗔𝗜 • Quant Mode Activated 📊⚙️🧠✨
Nhận xét
Đăng nhận xét