File size: 3,944 Bytes
f4c6ac5
b906011
668b3f5
fdfb5d7
668b3f5
fdfb5d7
848f0f3
668b3f5
848f0f3
 
668b3f5
848f0f3
 
368a9fb
 
 
 
848f0f3
368a9fb
 
 
 
 
 
848f0f3
668b3f5
 
 
 
 
 
b906011
668b3f5
 
 
 
 
b906011
fdfb5d7
368a9fb
fdfb5d7
 
368a9fb
 
fdfb5d7
 
 
 
 
 
 
 
 
668b3f5
 
 
 
 
 
 
 
368a9fb
 
 
 
 
668b3f5
 
 
 
fdfb5d7
368a9fb
668b3f5
fdfb5d7
b906011
668b3f5
 
368a9fb
668b3f5
 
 
 
 
 
 
 
 
fdfb5d7
668b3f5
 
 
 
 
 
 
 
 
 
 
fdfb5d7
668b3f5
f4c6ac5
668b3f5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import gradio as gr
import pandas as pd
import numpy as np
import yfinance as yf
import datetime
import matplotlib.pyplot as plt

# Function to fetch stock data
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

# Function to calculate Simple Moving Average (SMA)
def calculate_sma(stock_data, window=30):
    stock_data['SMA'] = stock_data['Close'].rolling(window=window).mean()
    return stock_data

# Function to predict future stock price using SMA
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

# Function to calculate percentage change
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

# Function to get stock statistics (high/low)
def get_stock_stats(stock_data):
    high_value = stock_data['Close'].max()
    low_value = stock_data['Close'].min()
    return high_value, low_value

# Function to plot stock performance
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"

# Main function for UI
def stock_prediction(ticker, start_date, end_date):
    stock_data = get_stock_data(ticker, start_date, end_date)
    
    # Get statistics
    percentage_change = calculate_percentage_change(stock_data)
    high_value, low_value = get_stock_stats(stock_data)
    
    # Calculate Simple Moving Average
    stock_data = calculate_sma(stock_data)
    
    # Predict future stock performance using SMA
    predicted_dates, predicted_sma = predict_future_sma(stock_data)
    
    # Generate buy/sell suggestion (simple rule-based)
    suggestion = "Buy" if percentage_change < 0 else "Sell"
    
    # Plot stock performance
    plot_file = plot_stock_performance(stock_data, predicted_dates, predicted_sma)
    
    return plot_file, percentage_change, high_value, low_value, suggestion

# UI setup
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")
    
    # Button to make predictions
    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()