Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,124 +1,85 @@
|
|
| 1 |
-
import datetime
|
| 2 |
-
import gradio as gr
|
| 3 |
-
import pandas as pd
|
| 4 |
import yfinance as yf
|
| 5 |
-
import
|
| 6 |
-
sns.set()
|
| 7 |
-
import matplotlib.pyplot as plt
|
| 8 |
import plotly.graph_objects as go
|
| 9 |
-
|
| 10 |
from datetime import date, timedelta
|
| 11 |
-
from matplotlib import pyplot as plt
|
| 12 |
-
from plotly.subplots import make_subplots
|
| 13 |
-
from pytickersymbols import PyTickerSymbols
|
| 14 |
-
from statsmodels.tsa.arima.model import ARIMA
|
| 15 |
-
from pandas.plotting import autocorrelation_plot
|
| 16 |
from dateutil.relativedelta import relativedelta
|
| 17 |
|
| 18 |
-
|
| 19 |
-
ticker_dict = {'FTSE 100(UK)': 'FTSE 100', 'NASDAQ(USA)': 'NASDAQ 100', 'CAC 40(FRANCE)': 'CAC 40'}
|
| 20 |
-
time_intervals = ['1d', '1m', '5m', '15m', '60m']
|
| 21 |
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
d3 = gr.Dropdown(time_intervals, label='Select Time Interval', value='1d', interactive=True)
|
| 34 |
-
d4 = gr.Radio(['Line Graph', 'Candlestick Graph'], label='Select Graph Type', value='Line Graph', interactive=True)
|
| 35 |
-
d5 = gr.Dropdown(['ARIMA', 'Prophet', 'LSTM'], label='Select Forecasting Method', value='ARIMA', interactive=True)
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
if series.shape[1] > 1:
|
| 40 |
-
series = series['Close'].values.tolist()
|
| 41 |
-
|
| 42 |
-
if model == "ARIMA":
|
| 43 |
-
for i in range(forecast_period):
|
| 44 |
-
model = ARIMA(series, order=(5, 1, 0))
|
| 45 |
-
model_fit = model.fit()
|
| 46 |
-
output = model_fit.forecast()
|
| 47 |
-
yhat = output[0]
|
| 48 |
-
predictions.append(yhat)
|
| 49 |
-
series.append(yhat)
|
| 50 |
-
elif model == "Prophet":
|
| 51 |
-
# Implement Prophet forecasting method
|
| 52 |
-
pass
|
| 53 |
-
elif model == "LSTM":
|
| 54 |
-
# Implement LSTM forecasting method
|
| 55 |
-
pass
|
| 56 |
|
| 57 |
-
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
-
d1.input(get_stocks_from_index, d1, d2)
|
| 70 |
-
|
| 71 |
-
def get_stock_graph(idx, stock, interval, graph_type, forecast_method):
|
| 72 |
-
stock_name, ticker_name = stock.split(":")
|
| 73 |
-
|
| 74 |
-
if ticker_dict[idx] == 'FTSE 100':
|
| 75 |
-
ticker_name += '.L' if ticker_name[-1] != '.' else 'L'
|
| 76 |
-
elif ticker_dict[idx] == 'CAC 40':
|
| 77 |
-
ticker_name += '.PA'
|
| 78 |
-
|
| 79 |
-
series = yf.download(tickers=ticker_name, start=START_DATE, end=END_DATE, interval=interval)
|
| 80 |
-
series = series.reset_index()
|
| 81 |
-
|
| 82 |
-
predictions = forecast_series(series, model=forecast_method)
|
| 83 |
-
|
| 84 |
-
last_date = pd.to_datetime(series['Date'].values[-1])
|
| 85 |
-
forecast_week = []
|
| 86 |
-
i = 1
|
| 87 |
-
while len(forecast_week) < FORECAST_PERIOD:
|
| 88 |
-
next_date = last_date + timedelta(days=i)
|
| 89 |
-
if is_business_day(next_date):
|
| 90 |
-
forecast_week.append(next_date)
|
| 91 |
-
i += 1
|
| 92 |
-
|
| 93 |
-
# Ensure predictions and forecast_week have the same length
|
| 94 |
-
predictions = predictions[:len(forecast_week)]
|
| 95 |
-
forecast_week = forecast_week[:len(predictions)]
|
| 96 |
-
|
| 97 |
-
forecast = pd.DataFrame({"Date": forecast_week, "Forecast": predictions})
|
| 98 |
-
|
| 99 |
-
if graph_type == 'Line Graph':
|
| 100 |
-
fig = go.Figure()
|
| 101 |
-
fig.add_trace(go.Scatter(x=series['Date'], y=series['Close'], mode='lines', name='Historical'))
|
| 102 |
-
fig.add_trace(go.Scatter(x=forecast['Date'], y=forecast['Forecast'], mode='lines', name='Forecast'))
|
| 103 |
-
else: # Candlestick Graph
|
| 104 |
-
fig = go.Figure(data=[go.Candlestick(x=series['Date'],
|
| 105 |
-
open=series['Open'],
|
| 106 |
-
high=series['High'],
|
| 107 |
-
low=series['Low'],
|
| 108 |
-
close=series['Close'],
|
| 109 |
-
name='Historical')])
|
| 110 |
-
fig.add_trace(go.Scatter(x=forecast['Date'], y=forecast['Forecast'], mode='lines', name='Forecast'))
|
| 111 |
-
|
| 112 |
-
fig.update_layout(title=f"Stock Price of {stock_name}",
|
| 113 |
-
xaxis_title="Date",
|
| 114 |
-
yaxis_title="Price")
|
| 115 |
-
|
| 116 |
-
return fig
|
| 117 |
-
out = gr.Plot()
|
| 118 |
inputs = [d1, d2, d3, d4, d5]
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
|
|
|
|
|
|
| 123 |
|
| 124 |
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import yfinance as yf
|
| 2 |
+
import pandas as pd
|
|
|
|
|
|
|
| 3 |
import plotly.graph_objects as go
|
| 4 |
+
import gradio as gr
|
| 5 |
from datetime import date, timedelta
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
from dateutil.relativedelta import relativedelta
|
| 7 |
|
| 8 |
+
# ... (keep the existing imports and global variables)
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
def get_company_info(ticker):
|
| 11 |
+
stock = yf.Ticker(ticker)
|
| 12 |
+
info = stock.info
|
| 13 |
+
|
| 14 |
+
# Select relevant fundamental information
|
| 15 |
+
fundamentals = {
|
| 16 |
+
"Company Name": info.get("longName", "N/A"),
|
| 17 |
+
"Sector": info.get("sector", "N/A"),
|
| 18 |
+
"Industry": info.get("industry", "N/A"),
|
| 19 |
+
"Market Cap": f"${info.get('marketCap', 'N/A'):,}",
|
| 20 |
+
"P/E Ratio": round(info.get("trailingPE", 0), 2),
|
| 21 |
+
"EPS": round(info.get("trailingEps", 0), 2),
|
| 22 |
+
"52 Week High": f"${info.get('fiftyTwoWeekHigh', 'N/A'):,}",
|
| 23 |
+
"52 Week Low": f"${info.get('fiftyTwoWeekLow', 'N/A'):,}",
|
| 24 |
+
"Dividend Yield": f"{info.get('dividendYield', 0) * 100:.2f}%",
|
| 25 |
+
"Beta": round(info.get("beta", 0), 2),
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
return pd.DataFrame(list(fundamentals.items()), columns=['Metric', 'Value'])
|
| 29 |
|
| 30 |
+
def get_stock_graph_and_info(idx, stock, interval, graph_type, forecast_method):
|
| 31 |
+
stock_name, ticker_name = stock.split(":")
|
| 32 |
+
|
| 33 |
+
if ticker_dict[idx] == 'FTSE 100':
|
| 34 |
+
ticker_name += '.L' if ticker_name[-1] != '.' else 'L'
|
| 35 |
+
elif ticker_dict[idx] == 'CAC 40':
|
| 36 |
+
ticker_name += '.PA'
|
| 37 |
|
| 38 |
+
# Get stock price data
|
| 39 |
+
series = yf.download(tickers=ticker_name, start=START_DATE, end=END_DATE, interval=interval)
|
| 40 |
+
series = series.reset_index()
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
+
# Generate forecast
|
| 43 |
+
predictions = forecast_series(series, model=forecast_method)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
+
# ... (keep the existing forecast date generation code)
|
| 46 |
|
| 47 |
+
# Create graph
|
| 48 |
+
if graph_type == 'Line Graph':
|
| 49 |
+
fig = go.Figure()
|
| 50 |
+
fig.add_trace(go.Scatter(x=series['Date'], y=series['Close'], mode='lines', name='Historical'))
|
| 51 |
+
fig.add_trace(go.Scatter(x=forecast['Date'], y=forecast['Forecast'], mode='lines', name='Forecast'))
|
| 52 |
+
else: # Candlestick Graph
|
| 53 |
+
fig = go.Figure(data=[go.Candlestick(x=series['Date'],
|
| 54 |
+
open=series['Open'],
|
| 55 |
+
high=series['High'],
|
| 56 |
+
low=series['Low'],
|
| 57 |
+
close=series['Close'],
|
| 58 |
+
name='Historical')])
|
| 59 |
+
fig.add_trace(go.Scatter(x=forecast['Date'], y=forecast['Forecast'], mode='lines', name='Forecast'))
|
| 60 |
|
| 61 |
+
fig.update_layout(title=f"Stock Price of {stock_name}",
|
| 62 |
+
xaxis_title="Date",
|
| 63 |
+
yaxis_title="Price")
|
| 64 |
+
|
| 65 |
+
# Get fundamental information
|
| 66 |
+
fundamentals = get_company_info(ticker_name)
|
| 67 |
+
|
| 68 |
+
return fig, fundamentals
|
| 69 |
+
|
| 70 |
+
# Update the Gradio interface
|
| 71 |
+
with demo:
|
| 72 |
+
# ... (keep the existing input components)
|
| 73 |
+
|
| 74 |
+
out_graph = gr.Plot()
|
| 75 |
+
out_fundamentals = gr.DataFrame()
|
| 76 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
inputs = [d1, d2, d3, d4, d5]
|
| 78 |
+
outputs = [out_graph, out_fundamentals]
|
| 79 |
+
|
| 80 |
+
d2.input(get_stock_graph_and_info, inputs, outputs)
|
| 81 |
+
d3.input(get_stock_graph_and_info, inputs, outputs)
|
| 82 |
+
d4.input(get_stock_graph_and_info, inputs, outputs)
|
| 83 |
+
d5.input(get_stock_graph_and_info, inputs, outputs)
|
| 84 |
|
| 85 |
demo.launch()
|