-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalyze.py
206 lines (167 loc) · 5.97 KB
/
analyze.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
from __future__ import division
from decimal import Decimal
import pandas as pd
import json
import sys
import re
d = {'K':3,'k':3,'m':6,'M':6,'b':9,'B':9}
def text_to_num(text):
if text[-1] in d:
num, magnitude = text[:-1], text[-1]
return Decimal(num) * 10 ** d[magnitude]
else:
return Decimal(text)
shares = [line.strip() for line in open("/home/gerson/projects/shares/shares.txt", 'r')]
# be profitable
# generate cash
# earnings before interest and tax (more important in balance sheet)
# also compounding of that earnings per book value per share is far better indicator companies ability
# bloomberg machine
# - all companies in the world
# - reduce to north america, western europe, aus/nz
# - financially solvent, iow, "current ratio" at least 1
# - remove all finance related (mortgages, banks, insurance, etc)
# - remove all tech related <------ (gerson)
# - remove more than 10B market cap and under 100M market cap
# - pay dividend of more than 1 cent per share
# - debt to equity ratio of no more than X
# - (goal is to end up with 250)
# https://saetacapital.com/2018/06/10/notes-from-anthony-deden-interview-by-grant-williams/
result = {}
result['blacklisted'] = 0
result['insolvent'] = 0
result['too_small'] = 0
result['too_big'] = 0
result['no_dividends'] = 0
result['no_income'] = 0
result['no_cash'] = 0
result['passed'] = 0
AX = True
US = True
EU = True
BR = False
HK = False
JP = False
for share in shares:
if (not AX and '.AX' in share) or (not EU and '.L' in share) or (not BR and '.SA' in share) or (not HK and '.HK' in share) or (not JP and '.T' in share):
continue
reject = False
total_liabilities = sys.maxsize
short_term_liabilities = sys.maxsize
total_cash = 0
total_assets = 0
intangible_assets = 0
shares_outstanding = sys.maxsize
income_before_covid = 0
market_cap = 0
income_before_covid = 0
has_dividends = False
net_income = 0
dividend = 0
profile = {}
price = sys.maxsize
try:
st = pd.read_csv('/home/gerson/projects/shares/data/stats_%s.csv' % share)
price = float(open('/home/gerson/projects/shares/data/price_%s.txt' % share).readlines()[0])
bs = pd.read_csv('/home/gerson/projects/shares/data/balancesheet_%s.csv' % share)
cf = pd.read_csv('/home/gerson/projects/shares/data/cashflow_%s.csv' % share)
inc = pd.read_csv('/home/gerson/projects/shares/data/income_%s.csv' % share)
with open('/home/gerson/projects/shares/data/profile_%s.json' % share) as f:
profile = json.load(f)
#calculate
try:
market_cap = text_to_num(str(st.query('`Attribute`.str.startswith("Market Cap")').iloc[0][1]))
except:
pass
try:
total_cash = text_to_num(str(bs.query('`Breakdown`.str.startswith("Total Cash")').iloc[0][1]) + 'k')
except:
pass
try:
short_term_liabilities = text_to_num(str(bs.query('`Breakdown`.str.startswith("Total Current Liabilities")').iloc[0][1]) + 'k')
except:
pass
try:
total_liabilities = text_to_num(str(bs.query('`Breakdown`.str.startswith("Total Liabilities")').iloc[0][1]) + 'k')
except:
pass
try:
total_assets = text_to_num(str(bs.query('`Breakdown`.str.startswith("Total Assets")').iloc[0][1]) + 'k')
except:
pass
try:
shares_outstanding = text_to_num(str(st.query('`Attribute`.str.startswith("Shares Outstanding")').iloc[0][1]))
except:
pass
try:
dividend = text_to_num(str(st.query('`Attribute`.str.startswith("5 Year Average Dividend")').iloc[0][1]))
except:
pass
try:
has_dividends = text_to_num(str(st.query('`Attribute`.str.startswith("5 Year Average Dividend")').iloc[0][1])) > 0
except:
pass
try:
net_income = text_to_num(str(inc.query('`Breakdown`.str.startswith("Net Income")').iloc[0][2]))
except:
pass
except:
pass
# remove or include industries and sectors
# black_list = ['real estate', 'finance', 'financial', 'gold', 'bank', 'insurance', 'mortgage']
# white_list = []
white_list = ['utilities', 'renewable', 'groceries', 'consumer', 'defensive']
black_list = []
industry_and_sector = ''
if 'sector' in profile:
industry_and_sector = profile['sector']
if 'industry' in profile:
industry_and_sector += profile['industry']
if black_list:
if any(re.search(desc, industry_and_sector, re.IGNORECASE) for desc in black_list):
result['blacklisted'] +=1
reject = True
if white_list:
if not any(re.search(desc, industry_and_sector, re.IGNORECASE) for desc in white_list):
result['blacklisted'] +=1
reject = True
# company is solvent
value = (total_assets - total_liabilities) / shares_outstanding
if float(value) < float(price):
result['insolvent'] +=1
reject = True
# company not too big or too small
if float(market_cap) <= 100*1000*1000:
result['too_small'] += 1
reject = True
if float(market_cap) >= 20*1000*1000*1000:
result['too_big'] +=1
reject = True
# must pay any dividend
if not has_dividends:
result['no_dividends'] +=1
reject = True
# must be profitable
if float(net_income) <= 0:
result['no_income'] +=1
reject = True
# must have 6 months of cash reserves
if float(short_term_liabilities) == 0 or float(total_cash)/float(short_term_liabilities) <= 0.25:
result['no_cash'] +=1
reject = True
if reject:
continue
profile['ticker'] = share
profile['dividend'] = '%.2f%%' % float(dividend)
profile['value/price'] = '%.4f' % (float(value)/float(price))
profile['cash/debt'] = '%.4f' % (float(total_cash)/float(short_term_liabilities))
print (json.dumps(profile, indent=2))
result['passed'] += 1
result['blacklisted'] = '%.2f%%' % float(result['blacklisted']/len(shares)*100)
result['insolvent'] = '%.2f%%' % float(result['insolvent']/len(shares)*100)
result['too_small'] = '%.2f%%' % float(result['too_small']/len(shares)*100)
result['too_big'] = '%.2f%%' % float(result['too_big']/len(shares)*100)
result['no_dividends'] = '%.2f%%' % float(result['no_dividends']/len(shares)*100)
result['no_income'] = '%.2f%%' % float(result['no_income']/len(shares)*100)
result['no_cash'] = '%.2f%%' % float(result['no_cash']/len(shares)*100)
print ("Results: " + json.dumps(result, indent=2))