Sarat Chandra
"Changing Port"
0110486
raw
history blame
3.25 kB
from flask import Flask, render_template, request
import pandas as pd
import numpy as np
import tensorflow as tf
import yfinance as yf
import pickle
app = Flask(__name__)
# Define tickers and load models
tickers = ['AMZN', 'TSLA', 'AAPL']
models = {}
for ticker in tickers:
model = tf.keras.models.load_model(f'model_{ticker}.h5')
models[ticker] = model
with open('/code/min_max.pickle', 'rb') as handle:
min_max_scaling = pickle.load(handle)
# Function to prepare the data for model input
def prepare_data(data):
# Assuming the data is a 1D array of closing prices
# Reshape the data to have the shape (batch_size, timesteps, features)
data = np.array(data)
data = data.reshape(1, data.shape[0], 1)
return data
# Function to get the last 60 days data for a ticker
def get_last_60_days_data(ticker):
# Define the end date as yesterday
end_date = pd.Timestamp.today() - pd.Timedelta(days=1)
# Define the start date as 120 days before the end date
start_date = end_date - pd.Timedelta(days=120)
# Fetch the stock data using yfinance
stock_data = yf.download(ticker, start=start_date, end=end_date, progress=False)
# Ensure we have enough data (at least 60 days)
if len(stock_data) < 60:
return None
# Extract the last 60 days 'Close' prices from the stock data
last_60_days_data = stock_data['Close'].tolist()[-60:]
last_60_days_data = (last_60_days_data - min_max_scaling[ticker][0])/(min_max_scaling[ticker][1] - min_max_scaling[ticker][0])
return last_60_days_data.tolist()
# Function to predict the next day closing value using the model
def predict_next_day(ticker, data):
model = models[ticker]
data = prepare_data(data)
prediction = model.predict(data)
return prediction[0]
def scale_back_data(data,ticker):
data = np.array(data)
data = data * (min_max_scaling[ticker][1] - min_max_scaling[ticker][0]) + min_max_scaling[ticker][0]
return data.tolist()
# @app.route('/')
# def hello_world():
# return "Hello World"
# Flask route to handle the main page
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
selected_ticker = request.form['ticker']
last_60_days_data = get_last_60_days_data(selected_ticker)
last_60_days_data_original = get_last_60_days_data(selected_ticker)
predictions = predict_next_day(selected_ticker, last_60_days_data)
# for _ in range(10):
# next_day_prediction = predict_next_day(selected_ticker, last_60_days_data)
# predictions.append(next_day_prediction)
# last_60_days_data.append(next_day_prediction)
# last_60_days_data.pop(0)
predictions = scale_back_data(predictions,selected_ticker)
last_60_days_data_original = scale_back_data(last_60_days_data_original,selected_ticker)
return render_template('index.html', tickers=tickers, selected_ticker=selected_ticker, predictions=predictions, last_60_days_data=last_60_days_data_original)
else:
return render_template('index.html', tickers=tickers, selected_ticker=None, predictions=None, last_60_days_data=None)
if __name__ == '__main__':
app.run(host='0.0.0.0',port = 7860)