|
|
import gradio as gr |
|
|
import pandas as pd |
|
|
import numpy as np |
|
|
import yfinance as yf |
|
|
import datetime |
|
|
import matplotlib.pyplot as plt |
|
|
|
|
|
|
|
|
def get_stock_data(ticker, start_date, end_date): |
|
|
stock_data = yf.download(ticker, start=start_date, end=end_date) |
|
|
stock_data.reset_index(inplace=True) |
|
|
return stock_data |
|
|
|
|
|
|
|
|
def calculate_sma(stock_data, window=30): |
|
|
stock_data['SMA'] = stock_data['Close'].rolling(window=window).mean() |
|
|
return stock_data |
|
|
|
|
|
|
|
|
def predict_future_sma(stock_data, periods=30): |
|
|
last_sma = stock_data['SMA'].iloc[-1] |
|
|
future_dates = pd.date_range(start=stock_data['Date'].iloc[-1], periods=periods + 1, closed='right') |
|
|
future_sma = np.full(periods, last_sma) |
|
|
return future_dates, future_sma |
|
|
|
|
|
|
|
|
def calculate_percentage_change(stock_data): |
|
|
start_value = stock_data['Close'].iloc[0] |
|
|
end_value = stock_data['Close'].iloc[-1] |
|
|
percentage_change = ((end_value - start_value) / start_value) * 100 |
|
|
return percentage_change |
|
|
|
|
|
|
|
|
def get_stock_stats(stock_data): |
|
|
high_value = stock_data['Close'].max() |
|
|
low_value = stock_data['Close'].min() |
|
|
return high_value, low_value |
|
|
|
|
|
|
|
|
def plot_stock_performance(stock_data, predicted_dates, predicted_sma): |
|
|
plt.figure(figsize=(10, 5)) |
|
|
plt.plot(stock_data['Date'], stock_data['Close'], label="Historical Data", color="blue") |
|
|
plt.plot(stock_data['Date'], stock_data['SMA'], label="SMA (30 days)", color="green") |
|
|
plt.plot(predicted_dates, predicted_sma, label="Predicted SMA", color="orange", linestyle='--') |
|
|
plt.xlabel("Date") |
|
|
plt.ylabel("Stock Price") |
|
|
plt.title("Stock Performance (Historical vs Predicted)") |
|
|
plt.legend() |
|
|
plt.grid(True) |
|
|
plt.tight_layout() |
|
|
plt.savefig("stock_performance.png") |
|
|
return "stock_performance.png" |
|
|
|
|
|
|
|
|
def stock_prediction(ticker, start_date, end_date): |
|
|
stock_data = get_stock_data(ticker, start_date, end_date) |
|
|
|
|
|
|
|
|
percentage_change = calculate_percentage_change(stock_data) |
|
|
high_value, low_value = get_stock_stats(stock_data) |
|
|
|
|
|
|
|
|
stock_data = calculate_sma(stock_data) |
|
|
|
|
|
|
|
|
predicted_dates, predicted_sma = predict_future_sma(stock_data) |
|
|
|
|
|
|
|
|
suggestion = "Buy" if percentage_change < 0 else "Sell" |
|
|
|
|
|
|
|
|
plot_file = plot_stock_performance(stock_data, predicted_dates, predicted_sma) |
|
|
|
|
|
return plot_file, percentage_change, high_value, low_value, suggestion |
|
|
|
|
|
|
|
|
with gr.Blocks() as app: |
|
|
gr.Markdown("# Stock Prediction App using Simple Moving Average (SMA)") |
|
|
|
|
|
stock_ticker = gr.Dropdown( |
|
|
label="Select Stock Ticker", |
|
|
choices=["AAPL", "GOOGL", "MSFT", "AMZN", "TSLA", "NFLX", "FB", "NVDA", "IBM", "ORCL"] |
|
|
) |
|
|
|
|
|
start_date = gr.DatePicker(label="Start Date", value=datetime.date(2023, 1, 1)) |
|
|
end_date = gr.DatePicker(label="End Date", value=datetime.date.today()) |
|
|
|
|
|
plot_output = gr.Image(label="Stock Performance (Historical vs Predicted)") |
|
|
percentage_change_output = gr.Textbox(label="Percentage Change") |
|
|
high_value_output = gr.Textbox(label="Highest Stock Value") |
|
|
low_value_output = gr.Textbox(label="Lowest Stock Value") |
|
|
suggestion_output = gr.Textbox(label="Buy/Sell Suggestion") |
|
|
|
|
|
|
|
|
predict_button = gr.Button("Predict") |
|
|
|
|
|
predict_button.click( |
|
|
stock_prediction, |
|
|
inputs=[stock_ticker, start_date, end_date], |
|
|
outputs=[plot_output, percentage_change_output, high_value_output, low_value_output, suggestion_output] |
|
|
) |
|
|
|
|
|
app.launch() |
|
|
|