<jupyter_start><jupyter_text>Creating DataFrame for analysis period(from corona crash, from 23rd March 2020 to 29th May 2020)<jupyter_code>pd.to_datetime(["2020-03-23"]).astype(int) / 10**9
pd.to_datetime(["2020-05-29"]).astype(int) / 10**9
r = requests.get("https://finnhub.io/api/v1/stock/candle?symbol=AAL&resolution=15&from=1584921600&to=1590710400&token=")
print(r.json())
df = pd.DataFrame(r.json())
df.head()
#check status of all periods
df["s"].value_counts()
#ok means all were retrieved, no nulls
#can be dropped since it has been checked
df.drop("s", axis=1, inplace=True)
#convert from unix to datetime
df["t"] = pd.to_datetime(df["t"], unit="s")
df.head()
#rename columns for clarity
df.columns
df.columns = ['close', 'high', 'low', 'open', 'time', 'volume']
df.head()
df[["close", "high", "low", "open"]].plot(figsize=(16,8))<jupyter_output><empty_output><jupyter_text>Without weekends/dates<jupyter_code>df["volume"].plot(legend=True, figsize=(16,8))
plt.xlim(0)<jupyter_output><empty_output><jupyter_text>With weekends/dates<jupyter_code>#long dashes are the weekends
#also includes correct dates
plt.figure(figsize=(16,8))
sns.lineplot(x="time", y="close", data=df)
df.plot(x="time", y="volume", legend=True, figsize=(16,8))
#can visually see 10 weeks, separated by 9 weekeends<jupyter_output><empty_output><jupyter_text>Neural Network  Set train/test and Scale<jupyter_code>df.head()
df.set_index("time", inplace=True)
#df.drop(["high", "low", "open"], axis=1, inplace=True)
#these 3 essentially move extremely similar to the close price
#they would not contribute much to the NN
len(df)
df.shape
#split test and train percentage
#we're using 10 weeks of data, so about 2 weeks should be split
0.20 * len(df)
test_size = int(0.20 * len(df))
split_index = len(df) - test_size
train = df[:split_index]
test = df[split_index:]
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
scaled_train = scaler.fit_transform(train)
scaled_test = scaler.transform(test)<jupyter_output><empty_output><jupyter_text>Generators<jupyter_code>from tensorflow.keras.preprocessing.sequence import TimeseriesGenerator
#must be less than test size
length = int(len(df) * 0.12)
length
generator = TimeseriesGenerator(scaled_train, scaled_train,
                               length=length, batch_size=1)
val_generator = TimeseriesGenerator(scaled_test, scaled_test,
                                   length=length, batch_size=1)<jupyter_output><empty_output><jupyter_text>Model<jupyter_code>from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, LSTM
#number of features, based on df
#2 features; close and volume
n_features = 5
model = Sequential()

model.add(LSTM(32, activation="relu",
              input_shape=(length, n_features)))

#same as number of features
model.add(Dense(5))
model.compile(optimizer="adam", loss="mse")
model.summary()
from tensorflow.keras.callbacks import EarlyStopping
early_stop = EarlyStopping(monitor="val_loss", patience=3)
#model.fit(generator, validation_data=val_generator,
#         epochs=30, callbacks=[early_stop])
df<jupyter_output><empty_output><jupyter_text>Completed Model and Metrics<jupyter_code>from tensorflow.keras.models import load_model
model = load_model("cov_stock_model.h5")
#metrics = pd.DataFrame(model.history.history)
#metrics.plot()
#SEE DOCUMENT for loss graph, taken from Google Colab
test_predictions = []

first_batch = scaled_train[-length:]
current_batch = first_batch.reshape((1,length,n_features))

for i in range(len(test)):
    current_pred = model.predict(current_batch)[0]
    test_predictions.append(current_pred)
    current_batch = np.append(current_batch[:,1:,:],
                             [[current_pred]], axis=1)
true_predictions = scaler.inverse_transform(test_predictions)
test["pClose"] = true_predictions[:,0]
test["pHigh"] = true_predictions[:,1]
test["pLow"] = true_predictions[:,2]
test["pOpen"] = true_predictions[:,3]
test["pVolume"] = true_predictions[:,4]<jupyter_output><ipython-input-46-8de912eb637b>:1: 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
  test["pClose"] = true_predictions[:,0]
<ipython-input-46-8de912eb637b>:2: 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
  test["pHigh"] = true_predictions[:,1]
<ipython-input-46-8de912eb637b>:3: 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_g[...]
StarcoderdataJupyterStructuredCleanDedup