Robert Castagna commited on
Commit
813fa51
1 Parent(s): cf4e04f

feb8 meeting update

Browse files
.gitignore CHANGED
@@ -1,3 +1,4 @@
1
  secrets.json
2
  edgar-crawler/
3
- .venv/
 
 
1
  secrets.json
2
  edgar-crawler/
3
+ .venv/
4
+ .env
app.py CHANGED
@@ -35,13 +35,8 @@ conn.close()
35
  df = pd.DataFrame(rows, columns=column_names)
36
 
37
  # setup pygwalker configuration: https://github.com/Kanaries/pygwalker, https://docs.kanaries.net/pygwalker/use-pygwalker-with-streamlit.en
38
- #pyg_html = pyg.to_html(df, dark="dark")
39
  pyg_html = pyg.walk(df, dark = 'dark', return_html=True)
40
 
41
  components.html(pyg_html, height=1000, scrolling=True)
42
 
43
- # show the dataframe just to test
44
- st.dataframe(df)
45
-
46
-
47
 
 
35
  df = pd.DataFrame(rows, columns=column_names)
36
 
37
  # setup pygwalker configuration: https://github.com/Kanaries/pygwalker, https://docs.kanaries.net/pygwalker/use-pygwalker-with-streamlit.en
 
38
  pyg_html = pyg.walk(df, dark = 'dark', return_html=True)
39
 
40
  components.html(pyg_html, height=1000, scrolling=True)
41
 
 
 
 
 
42
 
fin_data.db CHANGED
Binary files a/fin_data.db and b/fin_data.db differ
 
pages/1_Fundamentals.py CHANGED
@@ -1,4 +1,4 @@
1
- from pages.scraped_data import get_alpha_vantage_data, get_finnhub_data
2
  import datetime
3
  import pandas as pd
4
  import streamlit as st
@@ -13,9 +13,9 @@ def get_industry(ticker):
13
 
14
  def get_company_metrics(ticker):
15
  res_basic_fins = get_finnhub_data(f'/stock/metric?symbol={ticker}&metric=all')
16
- metric_data = res_basic_fins['metric']
17
- annual_series_data = res_basic_fins['series']['annual']
18
- quarterly_series_data = res_basic_fins['series']['quarterly']
19
  return metric_data, annual_series_data, quarterly_series_data
20
 
21
 
@@ -59,9 +59,9 @@ def get_equity_gains(ticker, period):
59
  recent_div = sp500.sort_values('Date', ascending=False)
60
  first_non_zero_dividend_row = recent_div[recent_div['Dividends'] > 0.0]
61
  if len(first_non_zero_dividend_row) == 0:
62
- return val['5Y_change'], 0
63
  else:
64
- return val['5Y_change'], first_non_zero_dividend_row.iloc[0]['Dividends']
65
 
66
 
67
  def get_90_day_tbill():
@@ -90,10 +90,11 @@ symbols = []
90
  list_of_tickers = get_list_of_tickers()
91
 
92
  with st.form(key="selecting columns"):
93
- symbols = st.multiselect(label='Select Tickers:', options=list_of_tickers, placeholder='MSFT, AAPL, ...')
94
- submit_button = st.form_submit_button(label='Evaluate')
 
95
 
96
- if submit_button and symbols:
97
  beta_dfs = []
98
  gains_data = {}
99
  hash_map = {}
@@ -105,12 +106,12 @@ with st.form(key="selecting columns"):
105
  metric_data, annual_series_data, quarterly_series_data = get_company_metrics(ticker)
106
 
107
  # reformat all JSON returns to be flattened dictionaries
108
- roe_dict = {'roe': annual_series_data['roe'][0]['v']}
109
- totalDebtToTotalCapital_y_dict = {'totalDebtToTotalCapital_y' : annual_series_data['totalDebtToTotalCapital'][0]['v']}
110
- totalDebtToEquity_y_dict = {'totalDebtToEquity_y' : annual_series_data['totalDebtToEquity'][0]['v']}
111
- eps_dict = {'eps' : annual_series_data['eps'][0]['v']}
112
- totalDebtToTotalCapital_q_dict = {'totalDebtToTotalCapital_q' : quarterly_series_data['totalDebtToTotalCapital'][0]['v']}
113
- totalDebtToEquity_q_dict = {'totalDebtToEquity_q' : quarterly_series_data['totalDebtToEquity'][0]['v']}
114
 
115
  # merge all dictionary keys per ticker
116
  combined_info = basic_info.copy() # Make a copy of the basic info
@@ -123,26 +124,29 @@ with st.form(key="selecting columns"):
123
  beta_dfs.append(df_b)
124
 
125
  # equity gains
126
- _, div = get_equity_gains(ticker=ticker, period=1810)
127
- gains_data[ticker] = div
128
 
 
129
  # Now, create a DataFrame from the hash_map
130
  df_1 = pd.DataFrame.from_dict(hash_map, orient='index')[['finnhubIndustry','eps','roe','dividendGrowthRate5Y','epsGrowth5Y','payoutRatioAnnual','payoutRatioTTM','roeTTM','totalDebtToEquity_y','totalDebtToEquity_q', 'totalDebtToTotalCapital_y','totalDebtToTotalCapital_q']]
131
  # Create beta df
132
  beta_df = pd.concat(beta_dfs)
133
- df_2 = pd.DataFrame(list(gains_data.items()), columns=['symbol', 'recent_dividend']).set_index('symbol')
134
-
135
  df_apis = df_1.join(beta_df)
136
  df_final = df_apis.join(df_2)
137
 
138
  # calculate additional columns
139
- df_final['5Y_SP500_growth'], _ = get_equity_gains(ticker= '^GSPC', period=1810)
140
-
141
  df_final['90_day_tbill'] = 4.06
142
-
 
143
  df_final['CAPM'] = df_final['90_day_tbill']/100 + df_final['Beta']*(df_final['5Y_SP500_growth'] - df_final['90_day_tbill']/100)
144
-
145
- df_final['DDM'] = (df_final['recent_dividend'] * (1+df_final['dividendGrowthRate5Y']/100)) / (df_final['CAPM'] - df_final['dividendGrowthRate5Y']/100)
146
-
147
  st.write(df_final)
148
-
 
 
 
1
+ from scraped_data import get_alpha_vantage_data, get_finnhub_data
2
  import datetime
3
  import pandas as pd
4
  import streamlit as st
 
13
 
14
  def get_company_metrics(ticker):
15
  res_basic_fins = get_finnhub_data(f'/stock/metric?symbol={ticker}&metric=all')
16
+ metric_data = res_basic_fins['metric'] if 'metric' in res_basic_fins else 'N/A'
17
+ annual_series_data = res_basic_fins['series']['annual'] if ('series' in res_basic_fins and 'annual' in res_basic_fins['series']) else 'N/A'
18
+ quarterly_series_data = res_basic_fins['series']['quarterly'] if ('series' in res_basic_fins and 'quarterly' in res_basic_fins['series']) else 'N/A'
19
  return metric_data, annual_series_data, quarterly_series_data
20
 
21
 
 
59
  recent_div = sp500.sort_values('Date', ascending=False)
60
  first_non_zero_dividend_row = recent_div[recent_div['Dividends'] > 0.0]
61
  if len(first_non_zero_dividend_row) == 0:
62
+ return val['5Y_change'], 0, recent_div['Close'].iloc[0]
63
  else:
64
+ return val['5Y_change'], first_non_zero_dividend_row.iloc[0]['Dividends'], recent_div['Close'].iloc[0]
65
 
66
 
67
  def get_90_day_tbill():
 
90
  list_of_tickers = get_list_of_tickers()
91
 
92
  with st.form(key="selecting columns"):
93
+ symbols = st.multiselect(label='Enter Tickers Here. Cannot check metrics for Funds.', options=list_of_tickers, placeholder='MSFT, AAPL, ...')
94
+ strategy_selection = st.radio("Select Strategy", ('Value', 'Growth'), horizontal=True)
95
+ submit_button = st.form_submit_button(label='Compute Metrics')
96
 
97
+ if submit_button and symbols and strategy_selection == 'Value':
98
  beta_dfs = []
99
  gains_data = {}
100
  hash_map = {}
 
106
  metric_data, annual_series_data, quarterly_series_data = get_company_metrics(ticker)
107
 
108
  # reformat all JSON returns to be flattened dictionaries
109
+ roe_dict = {'roe': annual_series_data['roe'][0]['v'] if annual_series_data != 'N/A' else 'N/A'}
110
+ totalDebtToTotalCapital_y_dict = {'totalDebtToTotalCapital_y' : annual_series_data['totalDebtToTotalCapital'][0]['v'] if annual_series_data != 'N/A' else 'N/A'}
111
+ totalDebtToEquity_y_dict = {'totalDebtToEquity_y' : annual_series_data['totalDebtToEquity'][0]['v'] if annual_series_data != 'N/A' else 'N/A'}
112
+ eps_dict = {'eps' : annual_series_data['eps'][0]['v'] if annual_series_data != 'N/A' else 'N/A'}
113
+ totalDebtToTotalCapital_q_dict = {'totalDebtToTotalCapital_q' : quarterly_series_data['totalDebtToTotalCapital'][0]['v'] if quarterly_series_data != 'N/A' else 'N/A'}
114
+ totalDebtToEquity_q_dict = {'totalDebtToEquity_q' : quarterly_series_data['totalDebtToEquity'][0]['v'] if quarterly_series_data != 'N/A' else 'N/A'}
115
 
116
  # merge all dictionary keys per ticker
117
  combined_info = basic_info.copy() # Make a copy of the basic info
 
124
  beta_dfs.append(df_b)
125
 
126
  # equity gains
127
+ _, div, close_price = get_equity_gains(ticker=ticker, period=1810)
128
+ gains_data[ticker] = [div, close_price]
129
 
130
+
131
  # Now, create a DataFrame from the hash_map
132
  df_1 = pd.DataFrame.from_dict(hash_map, orient='index')[['finnhubIndustry','eps','roe','dividendGrowthRate5Y','epsGrowth5Y','payoutRatioAnnual','payoutRatioTTM','roeTTM','totalDebtToEquity_y','totalDebtToEquity_q', 'totalDebtToTotalCapital_y','totalDebtToTotalCapital_q']]
133
  # Create beta df
134
  beta_df = pd.concat(beta_dfs)
135
+ df_2 = pd.DataFrame.from_dict(gains_data, orient='index', columns=['Recent Dividend','Price'])
136
+
137
  df_apis = df_1.join(beta_df)
138
  df_final = df_apis.join(df_2)
139
 
140
  # calculate additional columns
141
+ df_final['5Y_SP500_growth'], _, _ = get_equity_gains(ticker= '^GSPC', period=1810)
 
142
  df_final['90_day_tbill'] = 4.06
143
+ df_final['P/E Ratio'] = df_final['Price'] / df_final['eps']
144
+ df_final['dividendGrowthRate5Y'] = df_final['dividendGrowthRate5Y']/100
145
  df_final['CAPM'] = df_final['90_day_tbill']/100 + df_final['Beta']*(df_final['5Y_SP500_growth'] - df_final['90_day_tbill']/100)
146
+ df_final['DDM'] = (df_final['Recent Dividend'] * (1+df_final['dividendGrowthRate5Y'])) / (df_final['CAPM'] - df_final['dividendGrowthRate5Y'])
147
+ df_final = df_final[['finnhubIndustry','Price','eps','roe','P/E Ratio','epsGrowth5Y','payoutRatioAnnual','payoutRatioTTM','roeTTM','totalDebtToEquity_y','totalDebtToEquity_q', 'totalDebtToTotalCapital_y','totalDebtToTotalCapital_q','Beta','Recent Dividend','90_day_tbill','5Y_SP500_growth','dividendGrowthRate5Y','CAPM','DDM']]
148
+ df_final.rename({'finnhubIndustry':'Industry', 'eps':'EPS', 'roe':'ROE'}, inplace=True, axis=1)
149
  st.write(df_final)
150
+
151
+ if submit_button and symbols and strategy_selection == 'Growth':
152
+ st.write("Not built yet...")
pages/2_Sentiment_Data_Input.py CHANGED
@@ -7,7 +7,8 @@ import sqlite3
7
  import pandas as pd
8
  import streamlit as st
9
  import os
10
-
 
11
 
12
  # API DOC: https://finnhub.io/docs/api/introduction
13
 
@@ -17,11 +18,25 @@ def get_finnhub_data(example: str) -> json:
17
  :param1 example: '/company-news?symbol=AAPL&from=2023-08-15&to=2023-08-20'
18
  """
19
  base_url = 'https://finnhub.io/api/v1//'
20
-
21
  token = f"&token={os.environ['finnhub_token']}"
22
 
23
- request = requests.get(f"{base_url}{example}{token}")
24
- return request.json()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
  def sentiment_analysis(headline:str) -> str:
27
  """
@@ -30,10 +45,10 @@ def sentiment_analysis(headline:str) -> str:
30
 
31
  :param1 headline: Text string: 'Apple is the best company in the world'
32
  """
33
- nlp = pipeline("sentiment-analysis", model="ProsusAI/finbert")
34
  return nlp(headline)
35
 
36
 
 
37
  # --------------------------------- get news articles for a company --------------------------------- #
38
  conn = sqlite3.connect('fin_data.db')
39
  c = conn.cursor()
@@ -51,51 +66,53 @@ c.execute("""CREATE TABLE IF NOT EXISTS company_news (
51
  )""")
52
 
53
 
54
- ticker = st.text_input(label='Entering ticker will add Nov23-Jan24 of news sentiment data to database.')
55
 
56
- if ticker:
 
 
 
 
57
 
58
- c.execute('SELECT DISTINCT(ticker) FROM company_news')
59
  distinct_tickers = c.fetchall()
60
  distinct_ticker_symbols = [ticker[0] for ticker in distinct_tickers]
 
 
61
 
62
- if ticker not in distinct_ticker_symbols:
63
- try:
64
- res_news = get_finnhub_data(f"/company-news?symbol={ticker}&from=2023-12-01&to=2024-02-01")
65
- except:
66
- RuntimeError('Invalid Ticker.')
67
-
68
- st.write('Processing ' + str(len(res_news)) + ' articles...')
69
-
70
- for item in res_news:
71
- dt_object = datetime.datetime.fromtimestamp(item['datetime']).strftime("%Y-%m-%d")
72
- sentiment = sentiment_analysis(item['headline'])
73
- sentiment_label = sentiment[0]['label']
74
- sentiment_score = sentiment[0]['score']
75
- st.write(sentiment_label, dt_object)
 
 
 
 
 
 
76
 
77
- query = """
78
- INSERT INTO company_news (ticker, category, headline, date_stamp, sentiment_label, sentiment_score)
79
- VALUES (?, ?, ?, ?, ?, ?)
80
- """
81
- data = (ticker, item['category'], item['headline'], dt_object, sentiment_label, sentiment_score)
82
-
83
- #Execute the query with the data
84
- c.execute(query, data)
85
 
86
  c.execute("""
87
  SELECT * FROM company_news WHERE ticker = ?
88
  """, (ticker,))
89
  # Fetch all results
90
  rows = c.fetchall()
91
-
92
- # Extract column names from cursor description
93
- column_names = [description[0] for description in c.description]
94
-
95
  # Create a DataFrame
96
- df = pd.DataFrame(rows, columns=column_names)
97
 
98
-
99
  st.write(df)
100
 
101
 
 
7
  import pandas as pd
8
  import streamlit as st
9
  import os
10
+ import streamlit as st
11
+ from requests.exceptions import HTTPError, RequestException
12
 
13
  # API DOC: https://finnhub.io/docs/api/introduction
14
 
 
18
  :param1 example: '/company-news?symbol=AAPL&from=2023-08-15&to=2023-08-20'
19
  """
20
  base_url = 'https://finnhub.io/api/v1//'
 
21
  token = f"&token={os.environ['finnhub_token']}"
22
 
23
+ try:
24
+ request = requests.get(f"{base_url}{example}{token}")
25
+ request.raise_for_status() # This will raise an HTTPError if the response was an error
26
+ return request.json()
27
+ except HTTPError as http_err:
28
+ st.write(f"HTTP error occurred: {http_err}") # Python 3.6+
29
+ # Consider logging the error or handling it further based on your needs
30
+ except RequestException as err:
31
+ st.write(f"Other error occurred: {err}") # Python 3.6+
32
+ # Handle other types of exceptions (e.g., network issues)
33
+ except Exception as e:
34
+ st.write(f"Unexpected error: {e}")
35
+ # Catch-all for any other exceptions, which is useful for debugging
36
+
37
+ return {} # Return
38
+
39
+ nlp = pipeline("sentiment-analysis", model="ProsusAI/finbert")
40
 
41
  def sentiment_analysis(headline:str) -> str:
42
  """
 
45
 
46
  :param1 headline: Text string: 'Apple is the best company in the world'
47
  """
 
48
  return nlp(headline)
49
 
50
 
51
+
52
  # --------------------------------- get news articles for a company --------------------------------- #
53
  conn = sqlite3.connect('fin_data.db')
54
  c = conn.cursor()
 
66
  )""")
67
 
68
 
69
+ ticker = st.text_input(label='Entering ticker will add last 5 days of news sentiment data to database.')
70
 
71
+ start_date_str = (datetime.datetime.today() - datetime.timedelta(days=5)).strftime('%Y-%m-%d')
72
+ end_date_str = datetime.datetime.today().strftime('%Y-%m-%d')
73
+ dates = set(pd.date_range(start=start_date_str, end=end_date_str).strftime('%Y-%m-%d'))
74
+
75
+ if st.button('Load') and ticker:
76
 
77
+ c.execute('SELECT DISTINCT(ticker), date_stamp FROM company_news where ticker = ?', (ticker,))
78
  distinct_tickers = c.fetchall()
79
  distinct_ticker_symbols = [ticker[0] for ticker in distinct_tickers]
80
+ existing_dates = set([ticker[1] for ticker in distinct_tickers])
81
+ unique_dates = dates - existing_dates
82
 
83
+ if unique_dates:
84
+ for date in unique_dates:
85
+ try:
86
+ res_news = get_finnhub_data(f"/company-news?symbol={ticker}&from={date}&to={date}")
87
+ except:
88
+ st.error('Invalid Ticker.')
89
+ st.write(f"Proessing {len(res_news)} headlines for ", ticker,' on ', date)
90
+
91
+ for item in res_news:
92
+ dt_object = datetime.datetime.fromtimestamp(item['datetime']).strftime("%Y-%m-%d")
93
+ sentiment = sentiment_analysis(item['headline'])
94
+ sentiment_label = sentiment[0]['label']
95
+ sentiment_score = sentiment[0]['score']
96
+ #st.write(sentiment_label, dt_object)
97
+
98
+ query = """
99
+ INSERT INTO company_news (ticker, category, headline, date_stamp, sentiment_label, sentiment_score)
100
+ VALUES (?, ?, ?, ?, ?, ?)
101
+ """
102
+ data = (ticker, item['category'], item['headline'], dt_object, sentiment_label, sentiment_score)
103
 
104
+ #Execute the query with the data
105
+ c.execute(query, data)
 
 
 
 
 
 
106
 
107
  c.execute("""
108
  SELECT * FROM company_news WHERE ticker = ?
109
  """, (ticker,))
110
  # Fetch all results
111
  rows = c.fetchall()
112
+
 
 
 
113
  # Create a DataFrame
114
+ df = pd.DataFrame(rows, columns=[description[0] for description in c.description])
115
 
 
116
  st.write(df)
117
 
118
 
pages/__pycache__/scraped_data.cpython-311.pyc ADDED
Binary file (8.83 kB). View file
 
pages/scraped_data.py DELETED
@@ -1,185 +0,0 @@
1
- import json
2
- import requests
3
- import datetime
4
- import pandas as pd
5
- import streamlit as st
6
- import os
7
- import re
8
- import yfinance as yf
9
- import plotly.graph_objects as go
10
- from datetime import timedelta
11
-
12
- def get_finnhub_data(example: str) -> json:
13
- """
14
- Pass in the "example" string from the API documentation. It changes for every endpoint.
15
-
16
- :param1 example: '/company-news?symbol=AAPL&from=2023-08-15&to=2023-08-20'
17
- """
18
- base_url = 'https://finnhub.io/api/v1//'
19
-
20
- token = f"&token={os.environ['finnhub_token']}"
21
-
22
- request = requests.get(f"{base_url}{example}{token}")
23
- return request.json()
24
-
25
-
26
- def get_alpha_vantage_data(example: str) -> json:
27
- """
28
- Pass in the "function" string from the API documentation. It changes for every endpoint.
29
-
30
- :param1 example: ''
31
- """
32
- base_url = 'https://www.alphavantage.co/query?'
33
- token = f"&apikey={os.environ['alpha_api_key']}"
34
-
35
- request = requests.get(f"{base_url}{example}{token}")
36
- return request.json()
37
-
38
-
39
- # --------------------------------- list of all tickers ---------------------------------#
40
- # comp_info = get_finnhub_data('/stock/symbol?exchange=US')
41
- # list_of_tickers = []
42
- # for i in range(len(comp_info)-1):
43
- # for key in comp_info[i].keys():
44
- # if key == 'symbol':
45
- # list_of_tickers.append(comp_info[i]['symbol'])
46
- ticker = []
47
- ticker = st.text_input('type a ticker')
48
-
49
- if ticker:
50
- comp_info = get_finnhub_data(f'/stock/profile2?symbol={ticker}')
51
- df_industry = pd.DataFrame.from_dict(comp_info, orient='index').T[['finnhubIndustry','name','ticker']]
52
- st.write(df_industry)
53
-
54
- # --------------------------------- finnhub API --------------------------------- #
55
- # get basic financials
56
- #
57
-
58
- res_basic_fins = get_finnhub_data(f'/stock/metric?symbol={ticker}&metric=all')
59
- metric_data = res_basic_fins['metric']
60
- annual_series_data = res_basic_fins['series']['annual']
61
- quarterly_series_data = res_basic_fins['series']['quarterly']
62
-
63
-
64
- # endpoint 1
65
- df_metric_data = pd.DataFrame.from_dict(metric_data, orient='index', columns=['Value'])
66
- df_metric_data = df_metric_data.transpose()
67
- df_metric_data = df_metric_data[['dividendGrowthRate5Y','epsGrowth5Y','payoutRatioAnnual','payoutRatioTTM','roeTTM']]
68
- df_metric_data['symbol'] = res_basic_fins['symbol']
69
- st.write(df_metric_data)
70
-
71
- # endpoint 2
72
- df_roe = pd.DataFrame(annual_series_data['roe']).rename(columns={'v': 'bookValue'})
73
- df_totalDebtToTotalCapital = pd.DataFrame(annual_series_data['totalDebtToTotalCapital']).rename(columns={'v': 'annual_totalDebtToTotalCapital'})
74
- df_totalDebtToEquity = pd.DataFrame(annual_series_data['totalDebtToEquity']).rename(columns={'v': 'annual_totalDebtToEquity'})
75
- df_eps = pd.DataFrame(annual_series_data['eps']).rename(columns={'v': 'eps'})
76
-
77
- # Merge the DataFrames on 'period'
78
- dfs = [df_roe, df_totalDebtToTotalCapital, df_totalDebtToEquity, df_eps]
79
- df_merged = dfs[0]
80
- for df in dfs[1:]:
81
- df_merged = df_merged.merge(df, on='period', how='outer')
82
-
83
- df_annual_series_data = df_merged
84
- df_annual_series_data['symbol'] = res_basic_fins['symbol']
85
-
86
- st.write(df_annual_series_data)
87
-
88
-
89
- # endpoint 3
90
- df_totalDebtToTotalCapital = pd.DataFrame(quarterly_series_data['totalDebtToTotalCapital']).rename(columns={'v': 'quarterly_totalDebtToTotalCapital'})
91
- df_totalDebtToEquity = pd.DataFrame(quarterly_series_data['totalDebtToEquity']).rename(columns={'v': 'quarterly_totalDebtToEquity'})
92
-
93
- # Merge the DataFrames on 'period'
94
- dfs = [df_totalDebtToTotalCapital, df_totalDebtToEquity]
95
- df_merged = dfs[0]
96
- for df in dfs[1:]:
97
- df_merged = df_merged.merge(df, on='period', how='outer')
98
-
99
- df_quarterly_series_data = df_merged
100
- df_quarterly_series_data['symbol'] = res_basic_fins['symbol']
101
-
102
- st.write(df_quarterly_series_data)
103
-
104
- # ------------------------ alphavantage API --------------------------------- #
105
- # dividend and t-bill data
106
- #
107
-
108
- # endpoint 1
109
- q = f'function=TIME_SERIES_MONTHLY_ADJUSTED&symbol={ticker}&interval=5min'
110
- dividend_data = get_alpha_vantage_data(q)
111
-
112
- data = []
113
- for date, val in dividend_data['Monthly Adjusted Time Series'].items():
114
- # key is the date, val contains another dictionary where the 6th element is a dividend value
115
- for label, dividend in val.items():
116
- if re.search('dividend', label) and float(dividend) > 0.0000:
117
- data.append((date, dividend))
118
- df_dividends = pd.DataFrame(data, columns = [['date','monthly_dividend']])
119
- st.write(df_dividends)
120
-
121
- # endpoint 2
122
- q = 'function=TREASURY_YIELD&'
123
-
124
- t_bill_data = get_alpha_vantage_data(q)
125
- df_t_bills = pd.DataFrame(t_bill_data['data']).rename(columns={'date':'Date', 'value':'90_day_t_bill'})
126
- st.write(df_t_bills)
127
-
128
-
129
- # ------------------------ yfinance API --------------------------------- #
130
- # index and ticker price data (beta calculations)
131
- #
132
-
133
- sp = yf.Ticker("^GSPC")
134
- sp_hist = sp.history(period="1y")['Close']
135
-
136
- sp = yf.Ticker(f'{ticker}')
137
- ticker_hist = sp.history(period="1y")['Close']
138
-
139
- sp500 = sp_hist.reset_index().rename(columns={'Close':'sp500_Close'})
140
- sp500['sp500_variance'] = sp500['sp500_Close'].var()
141
-
142
- merged_df = sp500.merge(ticker_hist, how='outer', on='Date')
143
-
144
- beta = merged_df.cov().loc['sp500_Close']['Close'] / sp500['sp500_variance'].max()
145
- max_date = merged_df['Date'].max()
146
-
147
- d = {'Date': max_date, 'Beta': beta, 'Symbol': ticker}
148
- df_beta = pd.DataFrame(d, index = [0])
149
- st.write(df_beta)
150
-
151
- # getting 5 years of history for pct change calculation
152
- sp = yf.Ticker("^GSPC")
153
- sp_hist = sp.history(period="5y")['Close']
154
- sp500 = sp_hist.reset_index().rename(columns={'Close':'sp500_Close'})
155
- sp500['Date'] = pd.to_datetime(sp500["Date"].dt.strftime('%Y-%m-%d'))
156
-
157
- todays_close = sp500.sort_values('Date', ascending=False).iloc[0]
158
-
159
- sp500_new_index = sp500.set_index('Date')
160
-
161
- # 1M, 3M, 1Y, 2Y, 3Y & 5Y
162
- days = [30, 90, 365, 730, 1095, 1810]
163
-
164
- val = {}
165
-
166
- for day in days:
167
- if todays_close[0] - timedelta(days=day) in sp500_new_index.index:
168
- delta = todays_close['sp500_Close'] - sp500_new_index.loc[todays_close[0] - timedelta(days=day)]['sp500_Close']
169
- val[f'{day}_day_pct_change'] = (delta / sp500_new_index.loc[todays_close[0] - timedelta(days=day)]['sp500_Close'] *100)
170
- else:
171
- val[f'{day}_day_pct_change'] = 'weekend'
172
-
173
- df_sp500_ytd_change = pd.DataFrame(val, index=['Delta'])
174
- st.write(df_sp500_ytd_change)
175
-
176
-
177
- # plotting sp500 price over time
178
- price_chart = go.Scatter(
179
- x=merged_df.Date,
180
- y=merged_df.sp500_Close,
181
- name = '1y price history'
182
- )
183
-
184
- fig_candle = go.Figure(price_chart)
185
- st.plotly_chart(fig_candle, use_container_width=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
requirements.txt CHANGED
@@ -20,4 +20,5 @@ pygwalker==0.3.9
20
  streamlit==1.22.0
21
  regex
22
  yfinance==0.2.28
23
- torch
 
 
20
  streamlit==1.22.0
21
  regex
22
  yfinance==0.2.28
23
+ torch
24
+ python-dotenv
scraped_data.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import requests
3
+ import datetime
4
+ import pandas as pd
5
+ import streamlit as st
6
+ import os
7
+ import re
8
+ import yfinance as yf
9
+ import plotly.graph_objects as go
10
+ from datetime import timedelta
11
+ from dotenv import load_dotenv
12
+
13
+ load_dotenv()
14
+
15
+ def get_finnhub_data(example: str) -> json:
16
+ """
17
+ Pass in the "example" string from the API documentation. It changes for every endpoint.
18
+
19
+ :param1 example: '/company-news?symbol=AAPL&from=2023-08-15&to=2023-08-20'
20
+ """
21
+ base_url = 'https://finnhub.io/api/v1//'
22
+
23
+ token = f"&token={os.environ['finnhub_token']}"
24
+
25
+ request = requests.get(f"{base_url}{example}{token}")
26
+ return request.json()
27
+
28
+
29
+ def get_alpha_vantage_data(example: str) -> json:
30
+ """
31
+ Pass in the "function" string from the API documentation. It changes for every endpoint.
32
+
33
+ :param1 example: ''
34
+ """
35
+ base_url = 'https://www.alphavantage.co/query?'
36
+ token = f"&apikey={os.environ['alpha_api_key']}"
37
+
38
+ request = requests.get(f"{base_url}{example}{token}")
39
+ return request.json()
40
+
41
+
42
+ sp = yf.Ticker("^GSPC")
43
+ sp_hist = sp.history(period="1y")['Close']
44
+
45
+ sp500 = sp_hist.reset_index().rename(columns={'Close':'sp500_Close'})
46
+ sp500['sp500_variance'] = sp500['sp500_Close'].var()
47
+
48
+ beta = sp500.cov().loc['sp500_Close']['Close'] / sp500['sp500_variance'].max()
49
+ max_date = sp500['Date'].max()
50
+
51
+
52
+
53
+ # plotting sp500 price over time
54
+ price_chart = go.Scatter(
55
+ x=sp500.Date,
56
+ y=sp500.sp500_Close,
57
+ name = '1y price history'
58
+ )
59
+
60
+ fig_candle = go.Figure(price_chart)
61
+ st.plotly_chart(fig_candle, use_container_width=True)