Skip to content

Commit 02c4d9b

Browse files
authored
Initial (#30)
* added action * added stock grabber * added yfin script
1 parent 180d221 commit 02c4d9b

File tree

6 files changed

+823
-1007
lines changed

6 files changed

+823
-1007
lines changed

500.csv

Lines changed: 505 additions & 0 deletions
Large diffs are not rendered by default.

Pipfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ streamlit = "*"
88
pandas = "*"
99
requests = "*"
1010
lxml = "*"
11+
yfinance = {extras = ["nospam", "repair"], version = "*"}
1112

1213
[dev-packages]
1314

Pipfile.lock

Lines changed: 0 additions & 1007 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Stock Analysis App
44

55

6+
67
## Goals
78

89
### Leading Indicators
@@ -19,6 +20,7 @@ Stock Analysis App
1920
1. [ ] **Moving Averages**: Longer-term moving averages to confirm a market trend.
2021
2. [ ] **Volume**: Trading volume following a price move.
2122
3. [ ] **EPS (Earnings Per Share)**: Historical earnings growth can be indicative of future performance but is a lagging indicator.
23+
2224
4. **P/E Ratio**: Indicates how much investors are willing to pay for a company’s earnings.
2325
5. **Dividend Yields**: Higher yields may signify a mature, established company.
2426
6. **Balance Sheet Metrics**: Debt-to-equity ratio, working capital, etc.
@@ -116,3 +118,4 @@ Understanding the layers of bonds or funds requires digging into their structure
116118
7. [ ] **Intrinio**: Provides various types of financial market data, including information on ETFs and mutual funds, through their API.
117119
8. [ ] **Zacks Investment Research**: Their API provides detailed mutual fund data including holdings, but typically requires a subscription.
118120
9. [ ] **Interactive Brokers API**: Known for its powerful trading API, it also allows you to fetch quite a bit of fund data if you have an account with them.
121+

app.py

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
"""midas.py"""
2+
import streamlit as st
3+
import requests
4+
# from lxml import etree
5+
import pandas as pd
6+
7+
8+
def fetch_thirteen_f(url):
9+
"""doc str."""
10+
response = requests.get(url, timeout=30)
11+
if response.status_code == 200:
12+
return response.content
13+
else:
14+
return None
15+
16+
17+
def parse_thirteen_f(xml_data):
18+
"""doc str."""
19+
# tree = etree.fromstring(xml_data)
20+
tree = f"{xml_data}"
21+
rows = [xml_data, tree]
22+
# for info_table in tree.xpath("//infoTable"):
23+
# row = {}
24+
# for element in info_table:
25+
# row[element.tag] = element.text
26+
# rows.append(row)
27+
return pd.DataFrame(rows)
28+
29+
30+
# ------ Data Fetching Functions ------
31+
def fetch_stock_data(ticker):
32+
"""doc str."""
33+
print(ticker)
34+
return {} # Logic to fetch stock data
35+
36+
37+
def fetch_economic_data(indicator):
38+
"""doc str."""
39+
print(indicator)
40+
return {} # Logic to fetch economic data
41+
42+
43+
# ------ Analysis Functions ------
44+
# ---- Leading Indicators ----
45+
def calculate_moving_average(data):
46+
"""doc str."""
47+
print(data)
48+
return {} # Logic for Moving Average
49+
50+
51+
def calculate_macd(data):
52+
"""doc str."""
53+
print(data)
54+
return {} # Logic for MACD
55+
56+
57+
def analyze_social_media_sentiment(data):
58+
"""doc str."""
59+
print(data)
60+
return {} # Logic for social media sentiment
61+
62+
63+
# ---- Lagging Indicators ----
64+
def calculate_rsi(data):
65+
"""doc str."""
66+
print(data)
67+
return {} # Logic for Relative Strength Index
68+
69+
70+
def calculate_bollinger_bands(data):
71+
"""doc str."""
72+
print(data)
73+
return {} # Logic for Bollinger Bands
74+
75+
76+
# ---- Economic Indicators ----
77+
def analyze_gdp(data):
78+
"""doc str."""
79+
print(data)
80+
return {} # Logic for GDP Analysis
81+
82+
83+
def analyze_interest_rates(data):
84+
"""doc str."""
85+
print(data)
86+
return {} # Logic for Interest Rates Analysis
87+
88+
89+
def analyze_unemployment(data):
90+
"""doc str."""
91+
print(data)
92+
return {} # Logic for Unemployment Analysis
93+
94+
95+
# ------ Display Functions ------
96+
def display_stock_data(data):
97+
"""doc str."""
98+
print(data)
99+
return {} # Logic to display stock data
100+
101+
102+
def display_economic_data(data):
103+
"""doc str."""
104+
print(data)
105+
return {} # Logic to display economic data
106+
107+
108+
def display_thirteen_f(data):
109+
"""doc str."""
110+
print(data)
111+
return {} # Logic to display 13F data
112+
113+
114+
def display_analysis(data):
115+
"""doc str."""
116+
print(data)
117+
return {} # Logic to display analyzed data
118+
119+
120+
# ------ Main App Function ------
121+
def main():
122+
"""main."""
123+
st.title("Financial Analytics Dashboard")
124+
125+
# User Inputs
126+
ticker = st.text_input("Enter stock ticker", "AAPL")
127+
economic_indicator = st.selectbox(
128+
"Choose an Economic Indicator", ["GDP", "Interest Rates", "Unemployment"]
129+
)
130+
131+
# Fetch Data
132+
stock_data = fetch_stock_data(ticker)
133+
economic_data = fetch_economic_data(economic_indicator)
134+
sec_thirteen_f_data = fetch_thirteen_f(ticker)
135+
136+
# Analyze Data
137+
moving_avg = calculate_moving_average(stock_data)
138+
macd = calculate_macd(stock_data)
139+
social_media_sentiment = analyze_social_media_sentiment(ticker)
140+
141+
rsi = calculate_rsi(stock_data)
142+
bollinger_bands = calculate_bollinger_bands(stock_data)
143+
144+
gdp_analysis = analyze_gdp(economic_data)
145+
interest_rates_analysis = analyze_interest_rates(economic_data)
146+
unemployment_analysis = analyze_unemployment(economic_data)
147+
148+
# Display Data and Analysis
149+
display_stock_data(stock_data)
150+
display_economic_data(economic_data)
151+
display_thirteen_f(sec_thirteen_f_data)
152+
display_analysis(
153+
{
154+
"Moving Average": moving_avg,
155+
"MACD": macd,
156+
"Social Media Sentiment": social_media_sentiment,
157+
"RSI": rsi,
158+
"Bollinger Bands": bollinger_bands,
159+
"GDP": gdp_analysis,
160+
"Interest Rates": interest_rates_analysis,
161+
"Unemployment": unemployment_analysis,
162+
}
163+
)
164+
165+
166+
if __name__ == "__main__":
167+
main()
168+
169+
170+
if __name__ == "__main__":
171+
main()

yfin.py

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
import yfinance as yf
2+
from yfinance import shared
3+
import json
4+
import pandas as pd
5+
import time
6+
7+
def get_stock_data(symbol):
8+
print(f'Getting stock data for {symbol}')
9+
data ={}
10+
try:
11+
stock_ticker = yf.Ticker(symbol)
12+
except Exception as err:
13+
print('Error: %s', err)
14+
15+
# get all stock info
16+
try:
17+
data['info'] = stock_ticker.info
18+
except Exception as err:
19+
print('Error: %s', err)
20+
21+
try:
22+
# get historical market data
23+
mo1 = stock_ticker.history(period="1mo")
24+
data['1mo_hist'] = pd.DataFrame(mo1).to_json()
25+
except Exception as err:
26+
print('Error: %s', err)
27+
28+
try:
29+
# show meta information about the history (requires history() to be called first)
30+
data['history_metadata'] = stock_ticker.history_metadata
31+
except Exception as err:
32+
print('Error: %s', err)
33+
34+
try:
35+
# show actions (dividends, splits, capital gains)
36+
data['actions'] = pd.DataFrame(stock_ticker.actions).to_json()
37+
data['dividends'] = pd.DataFrame(stock_ticker.dividends).to_json()
38+
data['splits'] = pd.DataFrame(stock_ticker.splits).to_json()
39+
except Exception as err:
40+
print('Error: %s', err)
41+
try:
42+
data['capital_gains'] = pd.DataFrame(stock_ticker.capital_gains).to_json() # only for mutual funds & etfs
43+
except Exception as err:
44+
print('Error: %s', err)
45+
46+
try:
47+
# show share count
48+
get_shares_full = stock_ticker.get_shares_full(start="2022-01-01", end=None)
49+
df = pd.DataFrame(get_shares_full)
50+
df.reset_index(inplace=True)
51+
data['get_shares_full'] = pd.DataFrame(df).to_json()
52+
except Exception as err:
53+
print('Error: %s', err)
54+
55+
# show financials:
56+
# - income statement
57+
58+
try:
59+
income_stmt = stock_ticker.income_stmt
60+
data['income_stmt'] = pd.DataFrame(income_stmt).to_json()
61+
62+
data['quarterly_income_stmt'] = pd.DataFrame(stock_ticker.quarterly_income_stmt).to_json()
63+
# - balance sheet
64+
data['balance_sheet'] = pd.DataFrame(stock_ticker.balance_sheet).to_json()
65+
data['quarterly_balance_sheet'] = pd.DataFrame(stock_ticker.quarterly_balance_sheet).to_json()
66+
# - cash flow statement
67+
data['cashflow'] = pd.DataFrame(stock_ticker.cashflow).to_json()
68+
data['quarterly_cashflow'] = pd.DataFrame(stock_ticker.quarterly_cashflow).to_json()
69+
# see `Ticker.get_income_stmt()` for more options
70+
except Exception as err:
71+
print('Error: %s', err)
72+
73+
try:
74+
# show holders
75+
data['major_holders'] = pd.DataFrame(stock_ticker.major_holders).to_json()
76+
data['institutional_holders'] = pd.DataFrame(stock_ticker.institutional_holders).to_json()
77+
data['mutualfund_holders'] = pd.DataFrame(stock_ticker.mutualfund_holders).to_json()
78+
data['insider_transactions'] = pd.DataFrame(stock_ticker.insider_transactions).to_json()
79+
data['insider_purchases'] = pd.DataFrame(stock_ticker.insider_purchases).to_json()
80+
data['insider_roster_holders'] = pd.DataFrame(stock_ticker.insider_roster_holders).to_json()
81+
except Exception as err:
82+
print('Error: %s', err)
83+
84+
try:
85+
# show recommendations
86+
data['recommendations'] = pd.DataFrame(stock_ticker.recommendations).to_json()
87+
data['recommendations_summary'] = pd.DataFrame(stock_ticker.recommendations_summary).to_json()
88+
data['upgrades_downgrades'] = pd.DataFrame(stock_ticker.upgrades_downgrades).to_json()
89+
except Exception as err:
90+
print('Error: %s', err)
91+
92+
# Show future and historic earnings dates, returns at most next 4 quarters and last 8 quarters by default.
93+
# Note: If more are needed use stock_ticker.get_earnings_dates(limit=XX) with increased limit argument.
94+
# data['earnings_dates'] = pd.DataFrame(stock_ticker.earnings_dates).to_json()
95+
96+
# show ISIN code - *experimental*
97+
# ISIN = International Securities Identification Number
98+
try:
99+
data['isin'] = stock_ticker.isin
100+
except Exception as err:
101+
print('Error: %s', err)
102+
103+
try:
104+
# show options expirations
105+
data['options'] = stock_ticker.options
106+
107+
# FIXME: Get the options data into the json dict so we can publish to mongo
108+
# data['options_data'] = {}
109+
# for opt in data['options']:
110+
# print(f'Getting options for {opt}')
111+
# # data['options_data'][opt] = stock_ticker.option_chain(opt)
112+
# # data['options_data'][opt] = stock_ticker.option_chain(opt)
113+
# data['options_data'][f'{opt}_calls'] = pd.DataFrame(stock_ticker.option_chain(opt)).to_json()
114+
# data['options_data'][f'{opt}_puts'] = pd.DataFrame(stock_ticker.option_chain(opt)).to_json()
115+
except Exception as err:
116+
print('Error: %s', err)
117+
118+
try:
119+
# show news
120+
data['news'] = stock_ticker.news
121+
except Exception as err:
122+
print('Error: %s', err)
123+
124+
#TODO: This is only used when debuggins Json not Serializable errors
125+
# with open(f'data/{symbol}.txt', 'w') as f:
126+
# print(data, file=f)
127+
128+
with open(f'data/{symbol}.json', 'w') as file:
129+
file.write(json.dumps(data))
130+
131+
# return data
132+
133+
# csv_data = pd.read_csv('https://gist.githubusercontent.com/yongghongg/4fa63c26369f844f22fed6121a24e04f/raw/8bda30f0b3ffbc82e563821ec131ebd62126c20d/SAP500_symbol_list.csv', index_col='Symbol')
134+
135+
csv_data = pd.read_csv('500.csv', index_col='Symbol')
136+
137+
for symbol in csv_data.iterrows():
138+
# print(symbol[0])
139+
get_stock_data(symbol[0])
140+
time.sleep(5)
141+
142+
143+

0 commit comments

Comments
 (0)