Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,6 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
import numpy as np
|
| 4 |
-
from sklearn.linear_model import LinearRegression
|
| 5 |
import yfinance as yf
|
| 6 |
import datetime
|
| 7 |
import matplotlib.pyplot as plt
|
|
@@ -12,21 +11,17 @@ def get_stock_data(ticker, start_date, end_date):
|
|
| 12 |
stock_data.reset_index(inplace=True)
|
| 13 |
return stock_data
|
| 14 |
|
| 15 |
-
# Function to
|
| 16 |
-
def
|
| 17 |
-
stock_data['
|
| 18 |
-
|
| 19 |
-
X = stock_data['Days'].values.reshape(-1, 1)
|
| 20 |
-
y = stock_data['Close'].values
|
| 21 |
-
return X, y
|
| 22 |
|
| 23 |
-
# Function to
|
| 24 |
-
def
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
return future_days, predicted_values
|
| 30 |
|
| 31 |
# Function to calculate percentage change
|
| 32 |
def calculate_percentage_change(stock_data):
|
|
@@ -42,10 +37,11 @@ def get_stock_stats(stock_data):
|
|
| 42 |
return high_value, low_value
|
| 43 |
|
| 44 |
# Function to plot stock performance
|
| 45 |
-
def plot_stock_performance(stock_data, predicted_dates,
|
| 46 |
plt.figure(figsize=(10, 5))
|
| 47 |
plt.plot(stock_data['Date'], stock_data['Close'], label="Historical Data", color="blue")
|
| 48 |
-
plt.plot(
|
|
|
|
| 49 |
plt.xlabel("Date")
|
| 50 |
plt.ylabel("Stock Price")
|
| 51 |
plt.title("Stock Performance (Historical vs Predicted)")
|
|
@@ -63,24 +59,23 @@ def stock_prediction(ticker, start_date, end_date):
|
|
| 63 |
percentage_change = calculate_percentage_change(stock_data)
|
| 64 |
high_value, low_value = get_stock_stats(stock_data)
|
| 65 |
|
| 66 |
-
#
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
| 69 |
|
| 70 |
# Generate buy/sell suggestion (simple rule-based)
|
| 71 |
suggestion = "Buy" if percentage_change < 0 else "Sell"
|
| 72 |
|
| 73 |
-
# Create prediction dates
|
| 74 |
-
predicted_dates = pd.date_range(start=stock_data['Date'].max(), periods=len(predicted_values), freq='D')
|
| 75 |
-
|
| 76 |
# Plot stock performance
|
| 77 |
-
plot_file = plot_stock_performance(stock_data, predicted_dates,
|
| 78 |
|
| 79 |
return plot_file, percentage_change, high_value, low_value, suggestion
|
| 80 |
|
| 81 |
# UI setup
|
| 82 |
with gr.Blocks() as app:
|
| 83 |
-
gr.Markdown("# Stock Prediction App using
|
| 84 |
|
| 85 |
stock_ticker = gr.Dropdown(
|
| 86 |
label="Select Stock Ticker",
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
import numpy as np
|
|
|
|
| 4 |
import yfinance as yf
|
| 5 |
import datetime
|
| 6 |
import matplotlib.pyplot as plt
|
|
|
|
| 11 |
stock_data.reset_index(inplace=True)
|
| 12 |
return stock_data
|
| 13 |
|
| 14 |
+
# Function to calculate Simple Moving Average (SMA)
|
| 15 |
+
def calculate_sma(stock_data, window=30):
|
| 16 |
+
stock_data['SMA'] = stock_data['Close'].rolling(window=window).mean()
|
| 17 |
+
return stock_data
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
# Function to predict future stock price using SMA
|
| 20 |
+
def predict_future_sma(stock_data, periods=30):
|
| 21 |
+
last_sma = stock_data['SMA'].iloc[-1]
|
| 22 |
+
future_dates = pd.date_range(start=stock_data['Date'].iloc[-1], periods=periods + 1, closed='right')
|
| 23 |
+
future_sma = np.full(periods, last_sma)
|
| 24 |
+
return future_dates, future_sma
|
|
|
|
| 25 |
|
| 26 |
# Function to calculate percentage change
|
| 27 |
def calculate_percentage_change(stock_data):
|
|
|
|
| 37 |
return high_value, low_value
|
| 38 |
|
| 39 |
# Function to plot stock performance
|
| 40 |
+
def plot_stock_performance(stock_data, predicted_dates, predicted_sma):
|
| 41 |
plt.figure(figsize=(10, 5))
|
| 42 |
plt.plot(stock_data['Date'], stock_data['Close'], label="Historical Data", color="blue")
|
| 43 |
+
plt.plot(stock_data['Date'], stock_data['SMA'], label="SMA (30 days)", color="green")
|
| 44 |
+
plt.plot(predicted_dates, predicted_sma, label="Predicted SMA", color="orange", linestyle='--')
|
| 45 |
plt.xlabel("Date")
|
| 46 |
plt.ylabel("Stock Price")
|
| 47 |
plt.title("Stock Performance (Historical vs Predicted)")
|
|
|
|
| 59 |
percentage_change = calculate_percentage_change(stock_data)
|
| 60 |
high_value, low_value = get_stock_stats(stock_data)
|
| 61 |
|
| 62 |
+
# Calculate Simple Moving Average
|
| 63 |
+
stock_data = calculate_sma(stock_data)
|
| 64 |
+
|
| 65 |
+
# Predict future stock performance using SMA
|
| 66 |
+
predicted_dates, predicted_sma = predict_future_sma(stock_data)
|
| 67 |
|
| 68 |
# Generate buy/sell suggestion (simple rule-based)
|
| 69 |
suggestion = "Buy" if percentage_change < 0 else "Sell"
|
| 70 |
|
|
|
|
|
|
|
|
|
|
| 71 |
# Plot stock performance
|
| 72 |
+
plot_file = plot_stock_performance(stock_data, predicted_dates, predicted_sma)
|
| 73 |
|
| 74 |
return plot_file, percentage_change, high_value, low_value, suggestion
|
| 75 |
|
| 76 |
# UI setup
|
| 77 |
with gr.Blocks() as app:
|
| 78 |
+
gr.Markdown("# Stock Prediction App using Simple Moving Average (SMA)")
|
| 79 |
|
| 80 |
stock_ticker = gr.Dropdown(
|
| 81 |
label="Select Stock Ticker",
|