from flask import Flask, render_template import pandas as pd import matplotlib.pyplot as plt import datetime from sklearn.model_selection import train_test_split from sklearn.metrics import mean_squared_error, mean_absolute_percentage_error, r2_score, mean_absolute_error from sklearn.linear_model import LinearRegression import os app = Flask(__name__) def create_lagged_features(data, lags): features = pd.DataFrame() for lag in range(1, lags + 1): features[f'Lag_{lag}'] = data.shift(lag) return features df = pd.read_csv('./dataset/BBCA-History.csv') df = df = df.reindex(index=df.index[::-1]) df_display = df df['Tanggal'] = pd.to_datetime(df['Tanggal'], dayfirst=True) df['Perubahan%'] = df['Perubahan%'].str.replace('%', '').str.replace(',', '.').astype(float) df['Perubahan%'] = df['Perubahan%'].astype(float) df['Vol.'] = df['Vol.'].str.replace('M', 'e6').str.replace('B', 'e9').str.replace(',', '.').astype(float) # Calculate 30-day moving average df['MA30'] = df['Terakhir'].rolling(window=30).mean() # Generate the plot plt.figure(figsize=(10, 6)) plt.plot(df['Tanggal'], df['Pembukaan'], label='Harga Pembukaan') plt.plot(df['Tanggal'], df['Terakhir'], label='Harga Terakhir') plt.plot(df['Tanggal'], df['MA30'], label='MA 30', linestyle='--', color='red') plt.xlabel('Tanggal') plt.ylabel('Harga') plt.title('Pergerakan Harga Pembukaan dan Terakhir dengan MA 30') plt.legend() # Save the plot to a static file plot_path = os.path.join('static', 'plot.png') plt.savefig(plot_path) plt.close() selected_features = ['Tanggal', 'Pembukaan'] df = df[selected_features] df.set_index('Tanggal', inplace=True) lags = 90 lagged_features = create_lagged_features(df['Pembukaan'], lags) df_with_lags = pd.concat([df, lagged_features], axis=1).dropna() X = df_with_lags[[f'Lag_{i}' for i in range(1, lags + 1)]] y = df_with_lags['Pembukaan'] X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, shuffle=False) model = LinearRegression() model.fit(X_train, y_train) y_pred = model.predict(X_test) plt.figure(figsize=(10, 6)) plt.plot(y_test.index, y_test, label='Actual Values') plt.plot(y_test.index, y_pred, label='Predicted Values', linestyle='--') plt.xlabel('Tanggal') plt.ylabel('Harga Pembukaan') plt.title('Actual vs Predicted Values') plt.legend() # Save the plot to a static file plot_training_path = os.path.join('static', 'plot_training.png') plt.savefig(plot_training_path) plt.close() mse = mean_squared_error(y_test, y_pred) mae = mean_absolute_error(y_test, y_pred) mape = mean_absolute_percentage_error(y_test, y_pred) r2 = r2_score(y_test, y_pred) last_date = y_test.index[-1] next_date = last_date + datetime.timedelta(days=1) latest_lags = X.iloc[-1].values.reshape(1, -1) next_value = model.predict(latest_lags) @app.route('/') def index(): table_html = df_display.to_html(classes='table table-striped', index=False) return render_template('index.html', table=table_html, plot_url=plot_path) @app.route('/predict') def predict(): return render_template('predict.html', plot_training_url=plot_training_path, next_date=next_date.date(), next_value=next_value[0], mse=mse, mae=mae, mape=mape, r2=r2) if __name__ == '__main__': app.run(debug=True)