Kr08 commited on
Commit
5b5151c
·
verified ·
1 Parent(s): 31ac823

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -113
app.py CHANGED
@@ -1,32 +1,9 @@
1
- import datetime
2
  import gradio as gr
3
- import pandas as pd
4
- import yfinance as yf
5
- import seaborn as sns
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
- index_options = ['FTSE 100(UK)', 'NASDAQ(USA)', 'CAC 40(FRANCE)']
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
- global START_DATE, END_DATE
23
- END_DATE = date.today()
24
- START_DATE = END_DATE - relativedelta(years=1)
25
- FORECAST_PERIOD = 7
26
  demo = gr.Blocks()
27
- stock_names = []
28
-
29
-
30
 
31
  with demo:
32
  d1 = gr.Dropdown(index_options, label='Please select Index...', info='Will be adding more indices later on', interactive=True)
@@ -35,94 +12,17 @@ with demo:
35
  d4 = gr.Radio(['Line Graph', 'Candlestick Graph'], label='Select Graph Type', value='Line Graph', interactive=True)
36
  d5 = gr.Dropdown(['ARIMA', 'Prophet', 'LSTM'], label='Select Forecasting Method', value='ARIMA', interactive=True)
37
 
38
- def forecast_series(series, model="ARIMA", forecast_period=7):
39
- predictions = list()
40
- if series.shape[1] > 1:
41
- series = series['Close'].values.tolist()
42
-
43
- if model == "ARIMA":
44
- for i in range(forecast_period):
45
- model = ARIMA(series, order=(5, 1, 0))
46
- model_fit = model.fit()
47
- output = model_fit.forecast()
48
- yhat = output[0]
49
- predictions.append(yhat)
50
- series.append(yhat)
51
- elif model == "Prophet":
52
- # Implement Prophet forecasting method
53
- pass
54
- elif model == "LSTM":
55
- # Implement LSTM forecasting method
56
- pass
57
-
58
- return predictions
59
-
60
- def is_business_day(a_date):
61
- return a_date.weekday() < 5
62
 
63
-
64
- def get_stocks_from_index(idx):
65
- stock_data = PyTickerSymbols()
66
- index = ticker_dict[idx]
67
- stocks = list(stock_data.get_stocks_by_index(index))
68
- stock_names = [f"{stock['name']}:{stock['symbol']}" for stock in stocks]
69
- return gr.Dropdown(choices=stock_names, label='Please Select Stock from your selected index', interactive=True)
70
-
71
-
72
- d1.input(get_stocks_from_index, d1, d2)
73
-
74
- def get_stock_graph(idx, stock, interval, graph_type, forecast_method):
75
- stock_name, ticker_name = stock.split(":")
76
-
77
- if ticker_dict[idx] == 'FTSE 100':
78
- ticker_name += '.L' if ticker_name[-1] != '.' else 'L'
79
- elif ticker_dict[idx] == 'CAC 40':
80
- ticker_name += '.PA'
81
-
82
- series = yf.download(tickers=ticker_name, start=START_DATE, end=END_DATE, interval=interval)
83
- series = series.reset_index()
84
-
85
- predictions = forecast_series(series, model=forecast_method)
86
-
87
- last_date = pd.to_datetime(series['Date'].values[-1])
88
- forecast_week = []
89
- i = 1
90
- while len(forecast_week) < FORECAST_PERIOD:
91
- next_date = last_date + timedelta(days=i)
92
- if is_business_day(next_date):
93
- forecast_week.append(next_date)
94
- i += 1
95
-
96
- # Ensure predictions and forecast_week have the same length
97
- predictions = predictions[:len(forecast_week)]
98
- forecast_week = forecast_week[:len(predictions)]
99
-
100
- forecast = pd.DataFrame({"Date": forecast_week, "Forecast": predictions})
101
-
102
- if graph_type == 'Line Graph':
103
- fig = go.Figure()
104
- fig.add_trace(go.Scatter(x=series['Date'], y=series['Close'], mode='lines', name='Historical'))
105
- fig.add_trace(go.Scatter(x=forecast['Date'], y=forecast['Forecast'], mode='lines', name='Forecast'))
106
- else: # Candlestick Graph
107
- fig = go.Figure(data=[go.Candlestick(x=series['Date'],
108
- open=series['Open'],
109
- high=series['High'],
110
- low=series['Low'],
111
- close=series['Close'],
112
- name='Historical')])
113
- fig.add_trace(go.Scatter(x=forecast['Date'], y=forecast['Forecast'], mode='lines', name='Forecast'))
114
-
115
- fig.update_layout(title=f"Stock Price of {stock_name}",
116
- xaxis_title="Date",
117
- yaxis_title="Price")
118
-
119
- return fig
120
- out = gr.Plot()
121
  inputs = [d1, d2, d3, d4, d5]
122
- d2.input(get_stock_graph, inputs, out)
123
- d3.input(get_stock_graph, inputs, out)
124
- d4.input(get_stock_graph, inputs, out)
125
- d5.input(get_stock_graph, inputs, out)
126
 
 
 
 
 
 
127
 
128
- demo.launch()
 
 
 
1
  import gradio as gr
2
+ from config import index_options, time_intervals, START_DATE, END_DATE, FORECAST_PERIOD
3
+ from data_fetcher import get_stocks_from_index
4
+ from stock_analysis import get_stock_graph_and_info
 
 
 
5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  demo = gr.Blocks()
 
 
 
7
 
8
  with demo:
9
  d1 = gr.Dropdown(index_options, label='Please select Index...', info='Will be adding more indices later on', interactive=True)
 
12
  d4 = gr.Radio(['Line Graph', 'Candlestick Graph'], label='Select Graph Type', value='Line Graph', interactive=True)
13
  d5 = gr.Dropdown(['ARIMA', 'Prophet', 'LSTM'], label='Select Forecasting Method', value='ARIMA', interactive=True)
14
 
15
+ out_graph = gr.Plot()
16
+ out_fundamentals = gr.DataFrame()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  inputs = [d1, d2, d3, d4, d5]
19
+ outputs = [out_graph, out_fundamentals]
 
 
 
20
 
21
+ d1.input(get_stocks_from_index, d1, d2)
22
+ d2.input(get_stock_graph_and_info, inputs, outputs)
23
+ d3.input(get_stock_graph_and_info, inputs, outputs)
24
+ d4.input(get_stock_graph_and_info, inputs, outputs)
25
+ d5.input(get_stock_graph_and_info, inputs, outputs)
26
 
27
+ if __name__ == "__main__":
28
+ demo.launch()