-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
239 lines (206 loc) · 9.33 KB
/
main.py
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import numpy as np
import logging
def main():
ticker = 'BTC-USD'
print("1.LSTM")
print("2.Catboost")
print("3.Prophet")
print("4.Xgboost")
print("5.LGBM")
print("6. Random Forest")
selection = input("Select Model: ")
if selection == "1":
# LSTM
from Lstm_model import LSTMPredictor
LSTMPredictor.run(ticker)
elif selection == "2":
#Catboost
from Catboost_Regressor import CatBoostPredictor
CatBoostPredictor.catboost_prediction(ticker)
elif selection == "3":
# Prophet
from Prophet_model import MProphet
import yaml
print("Prophet selected.")
# Load the config file
config_path = 'configs/prophet_config.yaml'
with open(config_path, 'r') as file:
config = yaml.safe_load(file)
print("Load saved model? (Must be in models folder.)")
selection_c = input("Y/N: ").upper()
if selection_c == "Y":
# Update config with user input
config['start_date'] = input("Start Date (YYYY-MM-DD): ")
config['end_date'] = input("End Date (YYYY-MM-DD): ")
config['ticker'] = ticker
# Save updated config
with open(config_path, 'w') as file:
yaml.dump(config, file)
mp_loaded = MProphet(config_path)
mp_loaded.load_model()
mp_loaded.download_data()
mp_loaded.fit_predict()
mp_loaded.plot()
elif selection_c == "N":
# Update config with user input
config['start_date'] = input("Start Date (YYYY-MM-DD): ")
config['end_date'] = input("End Date (YYYY-MM-DD): ")
config['ticker'] = ticker
# Save updated config
with open(config_path, 'w') as file:
yaml.dump(config, file)
best_params = MProphet.tune_hyperparameters(config_path)
print("Best hyperparameters:", best_params)
mp_final = MProphet(config_path)
mp_final.hyperparams = best_params
results = mp_final.run()
print(f"Final RMSE: {results['rmse']}")
savemodel = input("Save model? Y/N: ").upper()
if savemodel == "Y":
mp_final.save_model()
print("Model saved.")
else:
pass
elif selection == "4":
#XGBoost
from Xgboost_model import XGBoost_Predictor
import yaml
config_path = "configs/Xgboost_config.yaml"
try:
with open(config_path, 'r') as file:
config = yaml.safe_load(file)
except FileNotFoundError:
config = {
"ticker": ticker,
"start_date": "",
"end_date": "",
"test_size": 0.2,
"plot_dir": "plots",
"hyperparameter_tuning": {
"max_depth": {"min": 3, "max": 10},
"n_estimators": {"min": 100, "max": 500},
"learning_rate": {"min": 0.01, "max": 0.3},
"subsample": {"min": 0.8, "max": 1.0},
"colsample_bytree": {"min": 0.8, "max": 1.0},
"n_iter": 20
}
}
print("XgboostRegressor selected.")
print("Load saved model? (Must be in models folder.)")
selection_c = input("Y/N: ").upper()
if selection_c == "Y":
model_path = "models/xgboost_model.json"
predictor = XGBoost_Predictor(config_path)
predictor.load_model(model_path)
print("Model loaded successfully.")
config['start_date'] = input("Start Date (YYYY-MM-DD): ")
config['end_date'] = input("End Date (YYYY-MM-DD): ")
config['ticker'] = ticker
# Use loaded model for prediction
df = predictor.download_data()
X_test, _, _, _ = predictor.prepare_data(df)
y_pred = predictor.predict(X_test)
y_pred_real = predictor.scaler_y.inverse_transform(y_pred.reshape(-1, 1))
# Plot results
predictor.plot_results(df['Close'].values[-len(y_pred_real):], y_pred_real)
print("Prediction completed. Check the plots directory for visualization.")
elif selection_c == "N":
config['start_date'] = input("Start Date (YYYY-MM-DD): ")
config['end_date'] = input("End Date (YYYY-MM-DD): ")
config['ticker'] = ticker
# Save updated config
with open(config_path, 'w') as file:
yaml.dump(config, file)
# Create and run the predictor
predictor = XGBoost_Predictor(config_path)
predictor.run()
save_model = input("Save model? Y/N: ").upper()
if save_model == "Y":
model_filename = "models/xgboost_model.json"
predictor.save_model(model_filename)
print(f"Model saved to {model_filename}.")
else:
print("Invalid selection.")
elif selection == "5":
# LGBM
from Lgbm_model import LGBMRegressorModel
print("LGBM selected.")
lgbm_model = LGBMRegressorModel() # Initialize the model with default config
print("Load saved model? (Must be in the 'models' directory.)")
selection_c = input("Y/N: ")
if selection_c.upper() == "Y":
model = lgbm_model.load_model(ticker)
print(f"Model for {ticker} loaded successfully.")
# Option to test the loaded model with new data
test_new_data = input("Do you want to test the model with new data? Y/N: ")
if test_new_data.upper() == "Y":
start_Date = input("Start Date for new data (YYYY-MM-DD): ")
end_Date = input("End Date for new data (YYYY-MM-DD): ")
rmse, dates, y_true, y_pred = lgbm_model.predict_new_data(model, ticker, start_Date, end_Date)
print(f'RMSE on new data: {rmse}')
print(f'Predictions saved in plots/{ticker}_prediction_new_data.png')
# Option to display some of the predictions
show_predictions = input("Do you want to see some of the predictions? Y/N: ")
if show_predictions.upper() == "Y":
num_predictions = min(30, len(dates)) # Show up to 30 predictions
print("\nSample predictions:")
print("Date\t\tActual Price\tPredicted Price")
for i in range(num_predictions):
print(f"{dates[i]:%Y-%m-%d}\t{y_true[i][0]:.2f}\t\t{y_pred[i][0]:.2f}")
elif selection_c.upper() == "N":
start_Date = input("Start Date (YYYY-MM-DD): ")
end_Date = input("End Date (YYYY-MM-DD): ")
model, rmse = lgbm_model.run(ticker, start_Date, end_Date)
print(f'RMSE: {rmse}')
savemodel = input("Save model? Y/N ")
if savemodel.upper() == "Y":
lgbm_model.save_model(model, ticker)
print(f"Model for {ticker} saved successfully.")
else:
print("Model not saved.")
else:
print("Invalid input.")
elif selection == "6":
# Random Forest
from Random_Forest_Regressor import RandomForestPredictor
print("Random Forest selected.")
rf_predictor = RandomForestPredictor()
print("Load saved model? (Must be in the 'models' directory.)")
selection_c = input("Y/N: ")
if selection_c.upper() == "Y":
rf_predictor.load_model()
print(f"Model for {ticker} loaded successfully.")
start_date = input("Start Date for new data (YYYY-MM-DD): ")
end_date = input("End Date for new data (YYYY-MM-DD): ")
mse, mae, r2, dates, y_true, y_pred = rf_predictor.predict_new_data(ticker, start_date, end_date)
print(f'MSE on new data: {mse}')
print(f'MAE on new data: {mae}')
print(f'R-squared on new data: {r2}')
print(f'Predictions saved in plots/{ticker}_prediction_new_data.png')
# Option to display some of the predictions
show_predictions = input("Do you want to see some of the predictions? Y/N: ")
if show_predictions.upper() == "Y":
num_predictions = min(30, len(dates)) # Show up to 30 predictions
print("\nSample predictions:")
print("Date\t\tActual Price\tPredicted Price")
for i in range(num_predictions):
print(f"{dates[i]:%Y-%m-%d}\t{y_true[i][0]:.2f}\t\t{y_pred[i][0]:.2f}")
elif selection_c.upper() == "N":
start_date = input("Start Date (YYYY-MM-DD): ")
end_date = input("End Date (YYYY-MM-DD): ")
mse, mae, r2 = rf_predictor.run(ticker, start_date, end_date)
print(f'MSE: {mse}')
print(f'MAE: {mae}')
print(f'R-squared: {r2}')
savemodel = input("Save model? Y/N: ")
if savemodel.upper() == "Y":
rf_predictor.save_model()
print(f"Model for {ticker} saved successfully.")
else:
print("Model not saved.")
else:
print("Invalid input.")
else:
print('Incorrect Input')
if __name__ == "__main__":
main()