Create functionality
Browse files- functionality +71 -0
functionality
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import yfinance as yf
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
import plotly.graph_objs as go
|
| 5 |
+
from prophet import Prophet
|
| 6 |
+
import gradio as gr
|
| 7 |
+
|
| 8 |
+
# Function to fetch stock data
|
| 9 |
+
def fetch_stock_data(ticker, start_date, end_date):
|
| 10 |
+
stock_data = yf.download(ticker, start=start_date, end=end_date)
|
| 11 |
+
return stock_data
|
| 12 |
+
|
| 13 |
+
# Train the model using Prophet
|
| 14 |
+
def train_prophet_model(stock_data):
|
| 15 |
+
df = stock_data.reset_index()[['Date', 'Close']]
|
| 16 |
+
df.columns = ['ds', 'y']
|
| 17 |
+
|
| 18 |
+
model = Prophet()
|
| 19 |
+
model.fit(df)
|
| 20 |
+
|
| 21 |
+
future = model.make_future_dataframe(periods=90) # Predict for the next 3 months
|
| 22 |
+
forecast = model.predict(future)
|
| 23 |
+
return forecast
|
| 24 |
+
|
| 25 |
+
# Function to display stock information and prediction
|
| 26 |
+
def predict_stock(ticker, start_date, end_date):
|
| 27 |
+
# Fetch stock data
|
| 28 |
+
stock_data = fetch_stock_data(ticker, start_date, end_date)
|
| 29 |
+
|
| 30 |
+
# Train the model and get predictions
|
| 31 |
+
forecast = train_prophet_model(stock_data)
|
| 32 |
+
|
| 33 |
+
# Calculate metrics: % change, highest, lowest
|
| 34 |
+
start_price = stock_data['Close'].iloc[0]
|
| 35 |
+
current_price = stock_data['Close'].iloc[-1]
|
| 36 |
+
percentage_change = ((current_price - start_price) / start_price) * 100
|
| 37 |
+
highest_price = stock_data['Close'].max()
|
| 38 |
+
lowest_price = stock_data['Close'].min()
|
| 39 |
+
|
| 40 |
+
# Buy/Sell prediction (if price is predicted to increase, suggest Buy)
|
| 41 |
+
predicted_next_value = forecast['yhat'].iloc[-1]
|
| 42 |
+
suggestion = "Buy" if predicted_next_value > current_price else "Sell"
|
| 43 |
+
|
| 44 |
+
# Plot historical and predicted prices
|
| 45 |
+
fig = go.Figure()
|
| 46 |
+
fig.add_trace(go.Scatter(x=stock_data.index, y=stock_data['Close'], mode='lines', name='Historical'))
|
| 47 |
+
fig.add_trace(go.Scatter(x=forecast['ds'], y=forecast['yhat'], mode='lines', name='Predicted'))
|
| 48 |
+
|
| 49 |
+
return fig, percentage_change, highest_price, lowest_price, suggestion
|
| 50 |
+
|
| 51 |
+
# Define Gradio UI components
|
| 52 |
+
tickers = ['AAPL', 'GOOG', 'MSFT', 'AMZN', 'TSLA', 'META', 'NVDA', 'NFLX', 'BABA', 'ORCL']
|
| 53 |
+
start_date = gr.components.DatePicker(label="Start Date", value="2023-01-01")
|
| 54 |
+
end_date = gr.components.DatePicker(label="End Date", value="2023-09-30")
|
| 55 |
+
ticker_dropdown = gr.components.Dropdown(choices=tickers, label="Stock Ticker")
|
| 56 |
+
|
| 57 |
+
# Gradio function to wrap the prediction
|
| 58 |
+
def gradio_interface(ticker, start_date, end_date):
|
| 59 |
+
fig, percentage_change, highest_price, lowest_price, suggestion = predict_stock(ticker, start_date, end_date)
|
| 60 |
+
return fig, f"{percentage_change:.2f}%", f"{highest_price:.2f}", f"{lowest_price:.2f}", suggestion
|
| 61 |
+
|
| 62 |
+
# Gradio interface and launch
|
| 63 |
+
output = [gr.components.Plot(label="Stock Price Plot"), gr.components.Textbox(label="Percentage Change"),
|
| 64 |
+
gr.components.Textbox(label="Highest Price"), gr.components.Textbox(label="Lowest Price"),
|
| 65 |
+
gr.components.Textbox(label="Prediction (Buy/Sell)")]
|
| 66 |
+
|
| 67 |
+
demo = gr.Interface(fn=gradio_interface, inputs=[ticker_dropdown, start_date, end_date], outputs=output, title="Stock Predictor App")
|
| 68 |
+
|
| 69 |
+
demo.launch()
|
| 70 |
+
|
| 71 |
+
|