-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
129 lines (106 loc) · 5.07 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
from src.data_collection import DataCollector
from src.data_processing import DataProcessor
from src.model_training import ModelTrainer
from src.prediction import Predictor
from src.utils import load_thresholds
import os
def main():
# Input from user
ticker = input("Enter the stock ticker: ")
print("Select the type of analysis:")
print("1. Fundamental Analysis")
print("2. Technical Analysis")
analysis_option = input("Enter the number corresponding to your choice: ")
print("Select the type of threshold:")
print("1. Conservative")
print("2. Moderate")
print("3. Aggressive")
threshold_option = input("Enter the number corresponding to your choice: ")
threshold_mapping = {
"1": "Conservative",
"2": "Moderate",
"3": "Aggressive"
}
selected_threshold_type = threshold_mapping.get(threshold_option, "Conservative")
# Load thresholds
thresholds = load_thresholds('thresholds.json')
selected_thresholds = thresholds.get(selected_threshold_type)
# Data collection
collector = DataCollector(ticker)
fundamental_data, technical_data = collector.get_data()
os.makedirs('data/raw', exist_ok=True)
# Save data to CSV files in data/raw folder
fundamental_data.to_csv(f'data/raw/{ticker}_fundamental_data.csv', index=False)
technical_data.to_csv(f'data/raw/{ticker}_technical_data.csv')
if fundamental_data.empty or technical_data.empty:
print("Failed to fetch data for the ticker.")
return
if analysis_option == "1":
# Fundamental analysis
print("Fundamental Analysis:")
print(fundamental_data)
# Define the metrics to be used for fundamental analysis
metrics = {
'trailingPE': ('PE Ratio', 'PE_ratio'),
'priceToBook': ('PB Ratio', 'PB_ratio'),
'debtToEquity': ('Debt to Equity Ratio', 'de_ratio'),
'earningsGrowth': ('Earnings Growth', 'earnings_growth'),
'revenueGrowth': ('Revenue Growth', 'revenue_growth')
}
recommendations = []
for key, (metric_name, threshold_key) in metrics.items():
if key in fundamental_data.columns:
value = fundamental_data[key].values[0]
threshold = selected_thresholds.get(threshold_key)
if threshold is not None:
decision = "Buy" if value and value < threshold else "Sell"
reason = f"{metric_name} ({value}) is {'less' if decision == 'Buy' else 'greater'} than {threshold}"
recommendations.append((decision, reason))
if recommendations:
final_decision = "Buy" if all(rec[0] == "Buy" for rec in recommendations) else "Sell"
print(f"Overall Recommendation based on {selected_threshold_type} threshold: {final_decision}")
for rec in recommendations:
print(f"Reason: {rec[1]}")
else:
print("No sufficient data for a comprehensive fundamental analysis.")
elif analysis_option == "2":
# Technical analysis
processor = DataProcessor(fundamental_data, technical_data)
data = processor.preprocess()
if data.empty:
print("No data available after preprocessing.")
return
X_train, X_test, y_train, y_test = processor.split_data(data)
# Model training
trainer = ModelTrainer()
trainer.train_classification_model(X_train, y_train)
accuracy, report = trainer.evaluate_classification_model(X_test, y_test)
print("Model Evaluation Report:")
print(report)
# Price Prediction
trainer.train_regression_model(X_train, y_train)
mse, predicted_prices = trainer.evaluate_regression_model(X_test, y_test)
print("Predicted Prices:")
print(predicted_prices)
# Prediction
predictor = Predictor(trainer.classification_model)
prediction = predictor.make_prediction(X_test)
print("Prediction:")
print(prediction)
# Decision based on thresholds
decision = "Buy" if prediction[-1] else "Sell"
print(f"Recommendation based on {selected_threshold_type} threshold: {decision}")
# Next Day's Open Price Prediction
predict_next = input("Do you want to predict the next day's open price? (y/n): ").lower()
if predict_next == 'y':
latest_data_point = processor.get_latest_data_point(data).drop(columns=['Target', 'Close']) # Ensure correct features
latest_data_point_scaled = processor.scale_latest_data(latest_data_point) # Scale the latest data point
if latest_data_point_scaled.shape[1] == X_train.shape[1]: # Ensure the number of features matches
next_open_price = trainer.predict_next_open_price(latest_data_point_scaled)
print("Predicted Next Day's Open Price:", next_open_price)
else:
print("Feature mismatch: Ensure the latest data point matches the training data features.")
else:
print("Invalid option selected.")
if __name__ == "__main__":
main()