NTFX Price Prediction
NTFX Price Prediction
1. Import Libraries
from flask import Flask, render_template, request, send_from_directory
import os
import datetime
import pandas as pd
import numpy as np
import joblib
from tensorflow.keras.models import load_model
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
4. Feature Engineering
# Feature Engineering (same as during training)
data['5_MA'] = data['Close'].rolling(window=5).mean()
data['30_MA'] = data['Close'].rolling(window=30).mean()
data['Volatility'] = data['Close'].rolling(window=5).std()
data['Returns'] = data['Close'].pct_change()
data.dropna(inplace=True)
5. Scale Data
# Scale the data with all features
data_scaled = scaler.transform(data[['Close', '5_MA', '30_MA',
'Volatility', 'Returns']])
custom_dates_datetime.append(datetime.datetime.strptime(date, '%d-%m-
%Y'))
except ValueError:
print(f"Invalid date format: {date}. Skipping this date.")
if not custom_dates_datetime:
return [], []
custom_predictions = []
for _ in custom_dates_datetime:
prediction = model.predict(last_input)
custom_predictions.append(prediction[0, 0])
custom_predictions = np.array(custom_predictions).reshape(-1, 1)
custom_predictions =
scaler.inverse_transform(np.concatenate((custom_predictions,
np.zeros((custom_predictions.shape[0], 4))), axis=1))[:, 0]
timestamp = datetime.datetime.now().strftime("%d%m%Y_%H%M%S")
prediction_filename = f"prediction_{timestamp}.png"
filepath = os.path.join(app.config['PREDICTIONS_FOLDER'],
prediction_filename)
return prediction_filename
insights = {
'first_price': round(first_price, 2),
'last_price': round(last_price, 2),
'price_change': round(price_change, 2),
'percentage_change': round(percentage_change, 2),
'trend': "rose" if price_change > 0 else "fell"
}
if request.method == 'POST':
date_input = request.form.get('dates')
custom_dates = [date.strip() for date in
date_input.split(",")]
predicted_dates, predicted_prices =
predict_custom_dates(custom_dates)
image_url = save_prediction_image(predicted_dates,
predicted_prices)
9. Serve Images
@app.route('/static/predictions/<filename>')
def display_image(filename):
return send_from_directory(app.config['PREDICTIONS_FOLDER'],
filename)