File size: 2,759 Bytes
d2af9b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
"""Training orchestration script for baseline models (RF and LSTM).

This script is intentionally explicit and small: it loads historical data
from the mock scanner, computes technical indicators, and trains two
baseline models. It is designed to be easy to reproduce locally.
"""

import pandas as pd
import numpy as np
import os
from com.stockprediction.config.AppConfig import config
from com.stockprediction.backend.data.data_strategy import DataScannerFactory
from com.stockprediction.backend.model.best_model import RandomForestModelPredictor, RFModelTrainer
from com.stockprediction.backend.model.lstm_model import LSTMModelPredictor, LSTMModelTrainer


# Re-use the technical indicator logic from main.py (or centralize it)
def applyTechnicalIndicators(df: pd.DataFrame) -> pd.DataFrame:
    if df is None or df.empty or len(df) < 20:
        return df
    df = df.copy()
    df['EMA_12'] = df['Close'].ewm(span=12, adjust=False).mean()
    df['EMA_26'] = df['Close'].ewm(span=26, adjust=False).mean()
    df['MACD'] = df['EMA_12'] - df['EMA_26']
    df['Signal_Line'] = df['MACD'].ewm(span=9, adjust=False).mean()
    df['SMA_20'] = df['Close'].rolling(window=20).mean()
    df['Std_20'] = df['Close'].rolling(window=20).std()
    df['Upper_Band'] = df['SMA_20'] + (df['Std_20'] * 2)
    df['Lower_Band'] = df['SMA_20'] - (df['Std_20'] * 2)
    delta = df['Close'].diff()
    gain = (delta.where(delta > 0, 0)).rolling(window=14).mean()
    loss = (-delta.where(delta < 0, 0)).rolling(window=14).mean()
    rs = gain / loss
    df['RSI'] = 100 - (100 / (1 + rs))
    return df.dropna()


def train():
    symbol = "AAPL" # Use AAPL for base training
    scanner = DataScannerFactory.getStockScanner("MOCK")
    df = scanner.collectData(symbol, "2020-01-01", "2026-03-27")
    
    if df is None or df.empty:
        print("No training data found!")
        return

    df = applyTechnicalIndicators(df)
    # If sentiment data is not available, use neutral scores (0.0)
    df['Sentiment'] = 0.0 # Placeholder
    
    # Train RF
    rf_trainer = RFModelTrainer()
    x_rf, y_rf, _ = rf_trainer.prepareData(df)
    rf_predictor = RandomForestModelPredictor()
    rf_predictor.trainAndSave(x_rf, y_rf, config.get("models", "rfPath"))
    print(f"RF Model trained with {x_rf.shape[1] if x_rf.size else 0} features.")

    # Train LSTM
    lstm_trainer = LSTMModelTrainer(sequenceLength=config.get("models", "sequenceLength"))
    x_lstm, y_lstm, _ = lstm_trainer.prepareData(df)
    lstm_predictor = LSTMModelPredictor()
    lstm_predictor.trainAndSave(x_lstm, y_lstm, config.get("models", "lstmPath"), epochs=5)
    print(f"LSTM Model trained with shape {x_lstm.shape if hasattr(x_lstm, 'shape') else None}.")


if __name__ == "__main__":
    train()