Kr08 commited on
Commit
0407f12
·
verified ·
1 Parent(s): d825735

Create stock_analysis.py

Browse files
Files changed (1) hide show
  1. stock_analysis.py +77 -0
stock_analysis.py ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import plotly.graph_objects as go
3
+ from datetime import timedelta
4
+ from statsmodels.tsa.arima.model import ARIMA
5
+ from config import FORECAST_PERIOD, ticker_dict
6
+ from data_fetcher import get_stock_data, get_company_info
7
+
8
+ def is_business_day(a_date):
9
+ return a_date.weekday() < 5
10
+
11
+ def forecast_series(series, model="ARIMA", forecast_period=FORECAST_PERIOD):
12
+ predictions = list()
13
+ if series.shape[1] > 1:
14
+ series = series['Close'].values.tolist()
15
+
16
+ if model == "ARIMA":
17
+ for _ in range(forecast_period):
18
+ model = ARIMA(series, order=(5, 1, 0))
19
+ model_fit = model.fit()
20
+ output = model_fit.forecast()
21
+ yhat = output[0]
22
+ predictions.append(yhat)
23
+ series.append(yhat)
24
+ elif model == "Prophet":
25
+ # Implement Prophet forecasting method
26
+ pass
27
+ elif model == "LSTM":
28
+ # Implement LSTM forecasting method
29
+ pass
30
+
31
+ return predictions
32
+
33
+ def get_stock_graph_and_info(idx, stock, interval, graph_type, forecast_method):
34
+ stock_name, ticker_name = stock.split(":")
35
+
36
+ if ticker_dict[idx] == 'FTSE 100':
37
+ ticker_name += '.L' if ticker_name[-1] != '.' else 'L'
38
+ elif ticker_dict[idx] == 'CAC 40':
39
+ ticker_name += '.PA'
40
+
41
+ series = get_stock_data(ticker_name, interval)
42
+ predictions = forecast_series(series, model=forecast_method)
43
+
44
+ last_date = pd.to_datetime(series['Date'].values[-1])
45
+ forecast_week = []
46
+ i = 1
47
+ while len(forecast_week) < FORECAST_PERIOD:
48
+ next_date = last_date + timedelta(days=i)
49
+ if is_business_day(next_date):
50
+ forecast_week.append(next_date)
51
+ i += 1
52
+
53
+ predictions = predictions[:len(forecast_week)]
54
+ forecast_week = forecast_week[:len(predictions)]
55
+
56
+ forecast = pd.DataFrame({"Date": forecast_week, "Forecast": predictions})
57
+
58
+ if graph_type == 'Line Graph':
59
+ fig = go.Figure()
60
+ fig.add_trace(go.Scatter(x=series['Date'], y=series['Close'], mode='lines', name='Historical'))
61
+ fig.add_trace(go.Scatter(x=forecast['Date'], y=forecast['Forecast'], mode='lines', name='Forecast'))
62
+ else: # Candlestick Graph
63
+ fig = go.Figure(data=[go.Candlestick(x=series['Date'],
64
+ open=series['Open'],
65
+ high=series['High'],
66
+ low=series['Low'],
67
+ close=series['Close'],
68
+ name='Historical')])
69
+ fig.add_trace(go.Scatter(x=forecast['Date'], y=forecast['Forecast'], mode='lines', name='Forecast'))
70
+
71
+ fig.update_layout(title=f"Stock Price of {stock_name}",
72
+ xaxis_title="Date",
73
+ yaxis_title="Price")
74
+
75
+ fundamentals = get_company_info(ticker_name)
76
+
77
+ return fig, fundamentals