I have write a code for stock market prediction but i think this code is incorrect as im…
- User
I have write a code for stock market prediction but i think this code is incorrect as im not getting anything in plot ie the plot is empty please correct the code also the time axis starts with -0.04 which is not possible give a correct code for predicting the stock market also plot for daywise not time wise:
Code: import numpy as np import pandas as pd import matplotlib.pyplot as plt import datetime from sklearn.preprocessing import MinMaxScaler from keras.models import Sequential from keras.layers import Dense, LSTM, Dropout import yfinance as yf
Download historical stock data
stock_symbol = 'AAPL' # Replace this with the desired stock symbol start_date = '2000-01-01' # Replace this with the desired start date end_date = datetime.datetime.today().strftime('%Y-%m-%d') # Today's date
stock_data = yf.download(stock_symbol, start=start_date, end=end_date)
stock_data['Close'].plot(title='AAPL Closing Price', figsize=(15,6)) plt.show()
Reset the index and use the 'Date' column as the index
stock_data.reset_index(inplace=True) stock_data.set_index('Date', inplace=True)
Calculate the Average True Range and Super Trend
price_high_low = stock_data['High'] - stock_data['Low'] price_high_close = abs(stock_data['High'] - stock_data['Close'].shift()) price_low_close = abs(stock_data['Low'] - stock_data['Close'].shift()) tr = np.maximum(price_high_low, np.maximum(price_high_close, price_low_close)) atr = tr.rolling(window=14).mean()
multiplier = 3 stock_data['SuperTrend_Up'] = (stock_data['High'] + stock_data['Low']) / 2 - multiplier * atr stock_data['SuperTrend_Down'] = (stock_data['High'] + stock_data['Low']) / 2 + multiplier * atr
super_trend = [] for i in range(len(stock_data)): if i == 0: super_trend.append(stock_data['Close'].iloc[i]) elif stock_data['Close'].iloc[i] > super_trend[i - 1]: super_trend.append(stock_data['SuperTrend_Up'].iloc[i]) else: super_trend.append(stock_data['SuperTrend_Down'].iloc[i])
stock_data['SuperTrend'] = super_trend
stock_data.info()
Remove rows with missing data and select the 'Close' and 'SuperTrend' columns for training and testing
stock_data = stock_data.dropna() data = stock_data[['Close', 'SuperTrend']].values stock_data.head()
Scale the data
scaler = MinMaxScaler(feature_range=(0, 1)) scaled_data = scaler.fit_transform(data)
Choose number of time steps (e.g., 60 days) and split into training and testing datasets
time_steps = 60 train_data = scaled_data[:len(stock_data)-time_steps-1] test_data = scaled_data[len(stock_data)-time_steps-1:]
Prepare the input and output for the LSTM model
x_train = [] y_train = [] x_test = [] y_test = []
for i in range(time_steps, len(train_data)): x_train.append(train_data[i-time_steps:i]) y_train.append(train_data[i, 0])
- Assistant