Machine Learning trong giao dịch: dùng sklearn cho phân loại tín hiệu

2025-06-01 — Admin

Machine Learning trong giao dịch: dùng sklearn cho phân loại tín hiệu

Machine Learning là công cụ mạnh mẽ giúp nhà giao dịch định lượng phân loại tín hiệu mua/bán. Bài viết này sẽ giúp bạn hiểu rõ cách áp dụng Machine Learning vào giao dịch.


1. Tạo feature từ giá (lag, MA, RSI…)

  • Lag features:
    • Giá trễ 1, 2, 3 ngày: df['Close'].shift(1), df['Close'].shift(2), df['Close'].shift(3)
  • Moving Average (MA):
    • SMA 5, 10, 20 ngày: df['Close'].rolling(window=5).mean()
  • RSI (Relative Strength Index):
    • Công thức: (RSI = 100 - \frac{100}{1 + RS})
    • Thư viện: ta.momentum.RSIIndicator(df['Close'], window=14).rsi()
  • Ví dụ code Python:
    import pandas as pd
    import ta
    
    # Tạo lag features
    df['lag_1'] = df['Close'].shift(1)
    df['lag_2'] = df['Close'].shift(2)
    df['lag_3'] = df['Close'].shift(3)
    
    # Tạo MA features
    df['SMA_5'] = df['Close'].rolling(window=5).mean()
    df['SMA_10'] = df['Close'].rolling(window=10).mean()
    df['SMA_20'] = df['Close'].rolling(window=20).mean()
    
    # Tạo RSI
    df['RSI'] = ta.momentum.RSIIndicator(df['Close'], window=14).rsi()
    

2. Xây mô hình Random Forest để phân loại buy/sell

  • Công cụ: sklearn là thư viện Python mạnh mẽ cho Machine Learning.
  • Ví dụ code Python:
    from sklearn.ensemble import RandomForestClassifier
    from sklearn.model_selection import train_test_split
    
    # Tạo target (1: buy, 0: sell)
    df['target'] = (df['Close'].shift(-1) > df['Close']).astype(int)
    
    # Chọn features
    features = ['lag_1', 'lag_2', 'lag_3', 'SMA_5', 'SMA_10', 'SMA_20', 'RSI']
    X = df[features].dropna()
    y = df['target'].dropna()
    
    # Chia train/test
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
    
    # Huấn luyện mô hình
    model = RandomForestClassifier(n_estimators=100, random_state=42)
    model.fit(X_train, y_train)
    
    # Dự đoán
    y_pred = model.predict(X_test)
    

3. Đánh giá: Precision, Recall, Confusion Matrix

  • Precision: Tỷ lệ dự đoán đúng trong số các dự đoán dương tính.
  • Recall: Tỷ lệ dự đoán đúng trong số các thực tế dương tính.
  • Confusion Matrix: Ma trận nhầm lẫn giữa dự đoán và thực tế.
  • Ví dụ code Python:
    from sklearn.metrics import precision_score, recall_score, confusion_matrix
    
    # Tính Precision, Recall
    precision = precision_score(y_test, y_pred)
    recall = recall_score(y_test, y_pred)
    print(f'Precision: {precision:.4f}')
    print(f'Recall: {recall:.4f}')
    
    # Vẽ Confusion Matrix
    cm = confusion_matrix(y_test, y_pred)
    print('Confusion Matrix:')
    print(cm)
    

4. Kết luận

  • Machine Learning là công cụ mạnh mẽ giúp nhà giao dịch định lượng phân loại tín hiệu mua/bán.
  • Nên kết hợp feature engineering, Random Forest và đánh giá mô hình để tối ưu hóa chiến lược.

Chúc bạn thành công trên hành trình làm chủ Machine Learning trong giao dịch!

← Xem tất cả bài viết · Trang chủ