Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import datetime
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import yfinance as yf
|
| 5 |
+
import seaborn as sns;
|
| 6 |
+
|
| 7 |
+
sns.set()
|
| 8 |
+
import matplotlib.pyplot as plt
|
| 9 |
+
import plotly.graph_objects as go
|
| 10 |
+
|
| 11 |
+
from datetime import date, timedelta
|
| 12 |
+
from matplotlib import pyplot as plt
|
| 13 |
+
from plotly.subplots import make_subplots
|
| 14 |
+
from pytickersymbols import PyTickerSymbols
|
| 15 |
+
from statsmodels.tsa.arima.model import ARIMA
|
| 16 |
+
from pandas.plotting import autocorrelation_plot
|
| 17 |
+
from dateutil.relativedelta import relativedelta
|
| 18 |
+
|
| 19 |
+
index_options = ['FTSE 100(UK)', 'NASDAQ(USA)', 'CAC 40(FRANCE)']
|
| 20 |
+
ticker_dict = {'FTSE 100(UK)': 'FTSE 100', 'NASDAQ(USA)': 'NASDAQ 100', 'CAC 40(FRANCE)': 'CAC 40'}
|
| 21 |
+
|
| 22 |
+
global START_DATE, END_DATE
|
| 23 |
+
|
| 24 |
+
END_DATE = date.today()
|
| 25 |
+
START_DATE = END_DATE - relativedelta(years=1)
|
| 26 |
+
FORECAST_PERIOD = 7
|
| 27 |
+
demo = gr.Blocks()
|
| 28 |
+
stock_names = []
|
| 29 |
+
|
| 30 |
+
with demo:
|
| 31 |
+
d1 = gr.Dropdown(index_options, label='Please select Index...',
|
| 32 |
+
info='Will be adding more indices later on',
|
| 33 |
+
interactive=True)
|
| 34 |
+
|
| 35 |
+
d2 = gr.Dropdown([]) # for specific stocks
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
# d3 = gr.Dropdown(['General News'])
|
| 39 |
+
|
| 40 |
+
def forecast_series(series, model="ARIMA", forecast_period=7):
|
| 41 |
+
|
| 42 |
+
predictions = list()
|
| 43 |
+
if series.shape[1] > 1:
|
| 44 |
+
series = series['Close'].values.tolist()
|
| 45 |
+
plt.show()
|
| 46 |
+
if model == "ARIMA":
|
| 47 |
+
## Do grid search here --> Custom for all stocks
|
| 48 |
+
for i in range(forecast_period):
|
| 49 |
+
model = ARIMA(series, order=(5, 1, 0))
|
| 50 |
+
model_fit = model.fit()
|
| 51 |
+
output = model_fit.forecast()
|
| 52 |
+
yhat = output[0]
|
| 53 |
+
predictions.append(yhat)
|
| 54 |
+
series.append(yhat)
|
| 55 |
+
|
| 56 |
+
return predictions
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
def is_business_day(a_date):
|
| 60 |
+
return a_date.weekday() < 5
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
def get_stocks_from_index(idx):
|
| 64 |
+
stock_data = PyTickerSymbols()
|
| 65 |
+
# indices = stock_data.get_all_indices()
|
| 66 |
+
index = ticker_dict[idx]
|
| 67 |
+
stock_data = PyTickerSymbols()
|
| 68 |
+
|
| 69 |
+
# returns 2d list with the following information
|
| 70 |
+
# 'name', 'symbol', 'country', 'indices', 'industries', 'symbols', 'metadata', 'isins', 'akas'
|
| 71 |
+
stocks = list(stock_data.get_stocks_by_index(index)) ##converting filter object to list
|
| 72 |
+
stock_names = []
|
| 73 |
+
for stock in stocks:
|
| 74 |
+
stock_names.append(stock['name'] + ':' + stock['symbol'])
|
| 75 |
+
d2 = gr.Dropdown(choices=stock_names, label='Please Select Stock from your selected index', interactive=True)
|
| 76 |
+
return d2
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
d1.input(get_stocks_from_index, d1, d2)
|
| 80 |
+
out = gr.Plot(every=10)
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
def get_stock_graph(idx, stock):
|
| 84 |
+
|
| 85 |
+
stock_name = stock.split(":")[0]
|
| 86 |
+
ticker_name = stock.split(":")[1]
|
| 87 |
+
|
| 88 |
+
if ticker_dict[idx] == 'FTSE 100':
|
| 89 |
+
if ticker_name[-1] == '.':
|
| 90 |
+
ticker_name += 'L'
|
| 91 |
+
else:
|
| 92 |
+
ticker_name += '.L'
|
| 93 |
+
elif ticker_dict[idx] == 'CAC 40':
|
| 94 |
+
ticker_name += '.PA'
|
| 95 |
+
|
| 96 |
+
## Can also download lower interval data apparently using line below
|
| 97 |
+
# data = yf.download(tickers="MSFT", period="5d", interval="1m")
|
| 98 |
+
series = yf.download(tickers=ticker_name, start=START_DATE, end=END_DATE) # stock.split(":")[1]
|
| 99 |
+
series = series.reset_index()
|
| 100 |
+
|
| 101 |
+
predictions = forecast_series(series)
|
| 102 |
+
|
| 103 |
+
last_date = pd.to_datetime(series['Date'].values[-1])
|
| 104 |
+
forecast_week = []
|
| 105 |
+
|
| 106 |
+
while len(forecast_week) != FORECAST_PERIOD:
|
| 107 |
+
if is_business_day(last_date):
|
| 108 |
+
forecast_week.append(last_date)
|
| 109 |
+
last_date += timedelta(days=1)
|
| 110 |
+
|
| 111 |
+
forecast = pd.DataFrame({"Date": forecast_week, "Forecast": predictions})
|
| 112 |
+
|
| 113 |
+
fig = plt.figure(figsize=(14, 5))
|
| 114 |
+
sns.set_style("ticks")
|
| 115 |
+
sns.lineplot(data=series, x="Date", y="Close", color="firebrick")
|
| 116 |
+
sns.lineplot(data=forecast, x="Date", y="Forecast", color="blue")
|
| 117 |
+
sns.despine()
|
| 118 |
+
|
| 119 |
+
plt.title("Stock Price of {}".format(stock_name), size='x-large', color='blue') # stock.split(":")[0]
|
| 120 |
+
text = "Your stock is:" + str(stock)
|
| 121 |
+
return fig
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
d2.input(get_stock_graph, [d1, d2], out)
|
| 125 |
+
demo.launch()
|