| import yfinance as yf |
| import pandas as pd |
| import pandas_ta as ta |
| import numpy as np |
| from sklearn.ensemble import RandomForestClassifier |
| from sklearn.model_selection import train_test_split |
| from skl2onnx import to_onnx |
| from skl2onnx.common.data_types import FloatTensorType |
|
|
| TICKERS= ["AAPL", "TSLA", "TCS.NS", "RELIANCE.NS"] |
|
|
| def fetch_and_prepare_data(): |
| all_data= [] |
| |
| for ticker in TICKERS: |
| print(f"Fetching historical data for {ticker}...") |
| |
| df= yf.download(ticker, period= "2y", interval= "1d") |
| if df.empty: |
| continue |
| |
| |
| if isinstance(df.columns, pd.MultiIndex): |
| df.columns= df.columns.get_level_values(0) |
| |
| df= df.copy() |
| |
| |
| df.ta.rsi(close= "Close", length= 14, append= True) |
| df.ta.macd(close= "Close", fast= 12, slow= 26, signal= 9, append= True) |
| df.ta.ema(close= "Close", length= 20, append= True) |
| |
| |
| df["EMA_20_ratio"]= (df["Close"] - df["EMA_20"]) / df["EMA_20"] |
| |
| |
| df["target"]= (df["Close"].shift(-1) > df["Close"]).astype(int) |
| |
| |
| feature_cols= ["RSI_14", "MACD_12_26_9", "MACDs_12_26_9", "EMA_20_ratio"] |
| df= df.dropna(subset= feature_cols + ["target"]) |
| all_data.append(df[feature_cols + ["target"]]) |
| |
| if not all_data: |
| raise ValueError("No data fetched successfully.") |
| combined_df= pd.concat(all_data, ignore_index= True) |
| return combined_df |
|
|
| def main(): |
| print("Preparing training data...") |
| data= fetch_and_prepare_data() |
| |
| X= data[["RSI_14", "MACD_12_26_9", "MACDs_12_26_9", "EMA_20_ratio"]].values.astype(np.float32) |
| y= data["target"].values.astype(np.int64) |
| |
| print(f"Total samples gathered: {len(X)}") |
| |
| X_train, X_test, y_train, y_test= train_test_split(X, y, test_size= 0.2, random_state= 42) |
| print("Training RandomForest model...") |
| |
| |
| model= RandomForestClassifier(n_estimators= 50, max_depth= 6, random_state= 42) |
| model.fit(X_train, y_train) |
| |
| train_acc= model.score(X_train, y_train) |
| test_acc= model.score(X_test, y_test) |
| print(f"Train Accuracy: {train_acc:.4f}") |
| print(f"Test Accuracy: {test_acc:.4f}") |
| |
| print("Converting model to ONNX format...") |
| |
| |
| initial_type= [('float_input', FloatTensorType([None, 4]))] |
| onx= to_onnx(model, initial_types= initial_type, target_opset= 15) |
| |
| onnx_filename= "model.onnx" |
| with open(onnx_filename, "wb") as f: |
| f.write(onx.SerializeToString()) |
| |
| print(f"Model successfully saved to {onnx_filename}!") |
| |
| |
| print("Verifying ONNX model runtime inference...") |
| import onnxruntime as ort |
| sess= ort.InferenceSession(onnx_filename) |
| input_name= sess.get_inputs()[0].name |
| |
| |
| test_input= np.array([[50.0, 0.0, 0.0, 0.0]], dtype= np.float32) |
| label, prob= sess.run(None, {input_name: test_input}) |
| print(f"ONNX Verification Result -> Predicted Label: {label}, Probability Mapping: {prob}") |
|
|
| if __name__ == "__main__": |
| main() |