Cannot find where data is being leaked in stock price prediction model

I trained a model on AAPL’s stock data from the beginning of 2010 to the beginning of 2021. However, I noticed that whenever I ask it to predict for the beginning of 2021 to the end of 2021, it somehow seems to come up with the exactly perfect answer, likely because some data is being leaked to the model. However, I can’t seem to find where the data is being leaked. Any help would be appreciated, thank you!

Training process:

# Download data
data = yf.download("AAPL", start="2010-01-01", end="2021-01-01")
data = data.dropna()
data = data[cols]

# Scale data between 0 and 1
scaler = MinMaxScaler(feature_range=(0, 1))
scaled_data = scaler.fit_transform(data)


window_size=60

# Create sequences for training and testing
# Create sequences for training and testing
def create_sequences(data, window_size):
  X, y = [], []
  for i in range(len(data) - window_size):
    X.append(data[i:(i + window_size), :])
    y.append(data[i + window_size, 0])  # We only predict the closing price
  return np.array(X), np.array(y)

X_train, y_train = create_sequences(scaled_data, window_size)


from keras.callbacks import EarlyStopping

# Define the early stopping criteria
early_stopping = EarlyStopping(monitor='loss', patience=10)

# Build LSTM model
model = Sequential()
model.add(LSTM(units=50, return_sequences=True, input_shape=(X_train.shape[1], X_train.shape[2])))
model.add(LSTM(units=50))
model.add(Dense(units=1))
model.compile(loss="mae", optimizer="adam")

# Add the early stopping callback to the fit function
model.fit(X_train, y_train, epochs=200, batch_size=64, callbacks=[early_stopping])

And here is where I test the model and it seems to be leaking data somehow:

data_custom_range = yf.download("AAPL", start="2021-01-01", end="2021-12-28")
data_custom_range = data_custom_range.dropna()
data_custom_range = data_custom_range[cols]  # Select closing price, volume

window_size = 60

# Create sequences for training and testing
def create_sequences(data, window_size):
  X, y = [], []
  for i in range(window_size, len(data)):  # Start from window_size
    X.append(data[(i - window_size):i, :])  # Use the previous window_size days of data
    y.append(data[i, 0])  # Predict the next day's price
  return np.array(X), np.array(y)

# Prepare the data
scaler = MinMaxScaler()
scaled_data = scaler.fit_transform(data_custom_range.values)

# Fit a separate scaler on just the closing prices
price_scaler = MinMaxScaler()
price_scaler.fit(data_custom_range.iloc[:, 0:1].values)  # Only use the closing prices

# Create sequences
X_custom, y_custom = create_sequences(scaled_data, window_size)

# Make predictions
predictions = []
for i in range(window_size, len(X_custom)):
  prediction = model.predict(X_custom[i:i+1])
  predictions.append(prediction[0, 0])

# Inverse transform the predictions using the price_scaler
predictions = price_scaler.inverse_transform(np.array(predictions).reshape(-1, 1))
actual = price_scaler.inverse_transform(y_custom.reshape(-1, 1))

And here is the graph of the output (shifted 60 days to the right):

import matplotlib.pyplot as plt

# Shift the predictions to the right by 80 units
shifted_predictions = np.append([None]*60, predictions)

# Plot the actual price and predicted price
plt.plot(actual, label='Actual Price')
plt.plot(shifted_predictions, label='Predicted Price')  # Use 'shifted_predictions' instead of 'predictions'
plt.xlabel('Time')
plt.ylabel('Price')
plt.title('Actual Price vs Predicted Price')
plt.legend()
plt.show()

If any other information is needed feel free to let me know, thank you so much in advance!

Note: Please let me know if I am accidentally giving it access to T+1 time data, I tried looking to see where I was accidentally doing that but can’t find it :/

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật