stock_list=[] percent_correct=[] curr_adj_close_list = [] pred_adj_close_list=[] perc_change_close_list=[] for stock in stocks_: S = '2010-01-01' data = yf.download(stock, start=S) if len(data['Open'].values) == 0: continue data['Adj_Close'] = data['Adj Close'] data.drop('Adj Close',axis=1,inplace=True) mid = data.Adj_Close.rolling(window=50).mean() short = data.Adj_Close.rolling(window=20).mean() data['mid'] = mid data['short'] = short data_adj = data.iloc[50:] if len(data_adj['Open'].values)==0: continue # Comment the line below out when market is not active. Leave line in when market is active. # The data is constantly updating live with the market # Comment this out to make prediction, keep in to see results compared with predictions. aaa = len(data_adj)-1 data_adj = data_adj.iloc[0:aaa] # comment line out above to make prediction for next day, keep line in to see what was predicted for current day old_data = data_adj data_adj['Adj_Close_Future'] = data_adj['Adj_Close'][1:] data_adj['Adj_Close_Future'] = data_adj['Adj_Close_Future'].shift(-1) ninety_perc = len(data_adj)-len(data_adj)*0.1 ten_perc = len(data_adj)*0.1 ten_perc = int(ten_perc) ninety_perc = int(ninety_perc) # The commented lines belwo can be included or not. No real difference was found. # I decided to see what the results would be if I capped the total data points to 5500. # if len(data_adj)<4500: # ninety_perc = len(data_adj)-len(data_adj)*0.1 # ten_perc = len(data_adj)*0.1 # ten_perc = int(ten_perc) # ninety_perc = int(ninety_perc) # else: # ninety_perc = 5000 # ten_perc=500 data_adj = data_adj.iloc[:ninety_perc] length = len(data_adj)-1 data_adj = data_adj.iloc[0:length] X = data_adj.drop('Adj_Close_Future',axis=1).values y = data_adj['Adj_Close_Future'].values X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=101) scaler = MinMaxScaler() X_train = scaler.fit_transform(X_train) X_test = scaler.transform(X_test) model = Sequential() model.add(Dense(8, activation='relu')) model.add(Dense(8, activation='relu')) model.add(Dense(8, activation='relu')) model.add(Dense(8, activation='relu')) model.add(Dense(8, activation='relu')) model.add(Dense(1)) model.compile(optimzer='adam',loss='mse') model.fit(x=X_train,y=y_train, validation_data=(X_test,y_test), batch_size=256, epochs=300,verbose=0) predictions = model.predict(X_test) if str(predictions[0])=='[nan]': continue MAE = round(mean_absolute_error(y_test,predictions),4) print('Mean Absolute Error', MAE) actual_value_list = [] predictions_list=[] for x in range(ten_perc): i = ninety_perc actual_values = old_data['Adj_Close_Future'][x+i] test_values = old_data.drop('Adj_Close_Future',axis=1).iloc[x+i] test_values = scaler.transform(test_values.values.reshape(-1,8)) predictions = model.predict(test_values) p = float(predictions) actual_value_list.append(actual_values) predictions_list.append(p) eval_ = pd.DataFrame() eval_['Actual_values'] = actual_value_list eval_['Predictions']=predictions_list rang = len(eval_) actual_diff_list =[] prediction_diff_list=[] for i in range(rang): if i < rang-1: actual_diff = eval_['Actual_values'][i]-eval_['Actual_values'][i+1] prediction_diff = eval_['Predictions'][i]-eval_['Predictions'][i+1] actual_diff_list.append(actual_diff) prediction_diff_list.append(prediction_diff) else: break diff_df = pd.DataFrame() diff_df['Actual_Difference']=actual_diff_list diff_df['Predicited_Difference']=prediction_diff_list accur = diff_df['Actual_Difference']/diff_df['Predicited_Difference'] pos_count, neg_count = 0, 0 for num in accur: # checking condition if num >= 0: pos_count += 1 else: neg_count += 1 old_len = len(old_data) prev_adj_close = old_data['Adj_Close'][old_len-1] test_value = old_data.drop('Adj_Close_Future',axis=1).iloc[old_len-1] test_value = scaler.transform(test_value.values.reshape(-1,8)) p = model.predict(test_value) p=float(p) p_correct = (pos_count/(pos_count+neg_count))*100 p_correct = round(p_correct,2) perc_change_close = (p-prev_adj_close)/(prev_adj_close)*100 perc_change_close = round(perc_change_close,1) print('Stock Ticker' , stock) print(p_correct,'% Guessed Stock Movement Correctly') print("Positive numbers in the list: ", pos_count) print("Negative numbers in the list: ", neg_count) print('Current Day Adjusted Close',prev_adj_close) print('Predicted Adjusted Close for Tomorrow', p) print(perc_change_close,'% Predicted Change in Adjusted Closing Price') stock_list.append(stock) percent_correct.append(p_correct) curr_adj_close_list.append(prev_adj_close) pred_adj_close_list.append(p) perc_change_close_list.append(perc_change_close) df = pd.DataFrame() df['Stock']=stock_list df['% Correct']=percent_correct df['Current Adj Close']=curr_adj_close_list df['Predicted Adj Close']=pred_adj_close_list df['Predicted % Change']=perc_change_close_list