<jupyter_start><jupyter_text>Identify product<jupyter_code>product = "RELIANCE.NS"
startDate = "2015-01-01"
toDate = datetime.today().strftime('%Y-%m-%d')
use_n_days = 30 
from pandas_datareader import data as pdr
import yfinance as yf
yf.pdr_override() # <== that's all it takes :-)

# download dataframe
data = pdr.get_data_yahoo(product, start=startDate, end=toDate)
import pandas as pd
from stockstats import StockDataFrame as Sdf
stock_prices = Sdf.retype(data)
stock_prices['Date'] = stock_prices.index
stock_prices
data = stock_prices
new_data = pd.DataFrame(index=range(0,len(stock_prices)),columns=['Date', 'close'])
for i in range(0,len(data)):
    new_data['Date'][i] = data['Date'][i]
    new_data['close'][i] = data['close'][i]

new_data.index = new_data.Date
new_data.drop('Date', axis=1, inplace=True)
dataset = new_data.values
train = dataset[0:len(new_data)*0.75,:]
test = dataset[len(new_data)*0.75:,:]
scaler = MinMaxScaler(feature_range=(0, 1))
scaled_data = scaler.fit_transform(dataset)

x_train, y_train = [], []
for i in range(use_n_days,len(train)):
    x_train.append(scaled_data[i-use_n_days:i,0])
    y_train.append(scaled_data[i,0])
x_train, y_train = np.array(x_train), np.array(y_train)
x_train = np.reshape(x_train, (x_train.shape[0],x_train.shape[1],1))

model = Sequential()
model.add(LSTM(units=50, return_sequences=True, input_shape=(x_train.shape[1],1)))
model.add(LSTM(units=50))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
model.fit(x_train, y_train, epochs=5, batch_size=1, verbose=10)
inputs = new_data[len(new_data) - len(test) - use_n_days:].values
inputs = inputs.reshape(-1,1)
inputs  = scaler.transform(inputs)
X_test = []
for i in range(use_n_days,inputs.shape[0]):
    X_test.append(inputs[i-use_n_days:i,0])
X_test = np.array(X_test)

X_test = np.reshape(X_test, (X_test.shape[0],X_test.shape[1],1))
closing_price = model.predict(X_test)
closing_price = scaler.inverse_transform(closing_price)
rms=np.sqrt(np.mean(np.power((test-closing_price),2)))
rms
#for plotting
import matplotlib.pyplot as plt
train = new_data[:987]
test = new_data[987:]
test['Predictions'] = closing_price
plt.plot(train['close'])
plt.plot(test[['close','Predictions']])<jupyter_output>/Users/vishalnigam/anaconda3/lib/python3.6/site-packages/ipykernel_launcher.py:5: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  """<jupyter_text>Predict future stock prices<jupyter_code>inputs = new_data[len(new_data) - len(test) - use_n_days:].values
inputs = inputs.reshape(-1,1)
inputs  = scaler.transform(inputs)
X_test = []
for i in range(use_n_days,inputs.shape[0]+1):
    X_test.append(inputs[i-use_n_days:i,0])
X_test = np.array(X_test)

X_test = np.reshape(X_test, (X_test.shape[0],X_test.shape[1],1))
closing_price_tomorrow = model.predict(X_test)
closing_price_tomorrow = scaler.inverse_transform(closing_price_tomorrow)<jupyter_output><empty_output><jupyter_text>Tomorrow's predicted price<jupyter_code>deviation = closing_price[len(closing_price)-2:len(closing_price)-1][0][0]/ closing_price[len(closing_price)-1:len(closing_price)][0][0] 
stock_price = deviation * data.tail(1).close[0]
stock_price_higher = stock_price + rms
stock_price_lower = stock_price - rms

print("Stock price is expected between " + str(stock_price_lower) + " and "+ str(stock_price_higher))<jupyter_output>Stock price is expected between 1942.9511179785502 and 2046.0120240588415
{'fraction_non_alphanumeric': 0.1503964998632759, 'fraction_numerical': 0.029258955427946404, 'mean_word_length': 7.389908256880734, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 11, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '260305', 'license': 'Apache-2.0', 'n_tokens_mistral': 1494, 'n_tokens_neox': 1415, 'n_words': 225, 'path': 'Stock price forecasting - 1D.ipynb', 'repo_name': 'vishal-nigam/stock-forecasting', 'size': '3657'}