-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstockHolders.py
83 lines (63 loc) · 2.13 KB
/
stockHolders.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env python
# coding: utf-8
# stockHolders.py
#
# Inputs
# filename: spreadsheet to output data, can be left blank
#
# Examples
# python3 stockHolders.py -> outputs in interestSymbols.xlsx
# python3 stockHolders.py data.xlsx -> outputs in data.xlsx
#
# Rev History:
# 0.1 210515 Initial Functionality
import pandas as pd
import yfinance as yf
from InvestingBase import procStocks, extractTicker, seralizeData
# Get specific stock holdings
# (Holder, Shares, Report Date, Out, Value)
def getStockHolders(symbol):
holders = []
stock = yf.Ticker(symbol)
if(stock.institutional_holders is None or
not isinstance(stock.institutional_holders.columns[0],str) or
stock.institutional_holders.columns[0] not in 'Holder'):
return None
count = 0
ihLen = len(stock.institutional_holders)
while count < ihLen:
holder = stock.institutional_holders.iloc[count,0]
shares = stock.institutional_holders.iloc[count,1]
reported = stock.institutional_holders.iloc[count,2].strftime('%Y-%m-%d')
out = stock.institutional_holders.iloc[count,3]
val = stock.institutional_holders.iloc[count,4]
holders.append(["", holder, shares, reported, out, val])
count+=1
return holders
# Main
def getHolders(filename):
# Get symbols of interest (all US symbols)
URLSym = 'https://www.sec.gov/files/company_tickers.json'
stocksJson = procStocks(URLSym) # Obtain all tickers in JSON
symList = extractTicker(stocksJson) # We just need the ticker symbol for this script
#symList = ['TSLA', 'SPY', 'RTNTF', 'FB', 'YUM']
# prealloc container
holderData = []
# top of the spreadsheet
holderData.append(['Symbol','Holder', 'Shares', 'Report Date', 'Out', 'Value'])
# Build the list of holdings for each fund
for symbol in symList:
print(symbol)
holderData.append([symbol])
symData = getStockHolders(symbol)
if(symData is None):
continue
for iSym in symData:
holderData.append(iSym)
seralizeData(filename, holderData)
if __name__ == "__main__":
import sys
filename = 'stockHolders.xlsx' # File name to seralize data
if(len(sys.argv) >= 2):
filename = sys.argv[1]
getHolders(filename)