This repository has been archived by the owner on Jun 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
ratios.py
153 lines (146 loc) · 5.28 KB
/
ratios.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
from yfinance.ticker import Ticker
from sector_means import industry_median_master_list as medians
from sector_means import generic_median_master_list as generic
"""
How a ratio fn will work is:
def ratiofn():
return 2 for great, 1 for normal, 0 for not good.
Let normal == a deviation of 10% from the average
It will return one of the numbers pertaining to how good the company ratios are.
"""
def evaluateHigher(value:float, sector: str, ratio: str, medians_list):
"""
Evaluate function will take in a float representing the ratio value,
and return the int that pertains to to greater, normal, or lower.
This fn will be used with ratios which favour a higher number,
such as EPS growth.
"""
difference = value - medians_list[sector][ratio]
deviation = abs(medians[sector][ratio] * 0.1)
#Check for normal
if abs(difference) <= deviation:
return 1
#Check for worse value
elif difference < 0:
return 0
#Final Case where the value is better.
else:
return 2
def evaluateLower(value:float, sector: str, ratio: str, medians_list):
"""
Evaluate function will take in a float representing the ratio value,
and return the int that pertains to to greater, normal, or lower.
This fn will be used with ratios which favour a lower number,
such as EPS growth.
"""
difference = value - medians_list[sector][ratio]
deviation = abs(medians[sector][ratio] * 0.1)
#Check for normal
if abs(difference) <= deviation:
return 1
#Check for better value
elif difference < 0:
return 2
#Final Case where the value is worse.
else:
return 0
#Earnings Ratios
def pe(info: dict):
#Key error safeguard
if 'trailingPE' not in info or info['trailingPE'] == None:
return 1
#Check if the stock sector is in the median list
sect = info['sector']
val = info['trailingPE']
if sect in medians:
return evaluateLower(val, sect, 'PE', medians)
#If not use to overall/generic medians to evaluate:
else:
return evaluateLower(val, sect, 'PE', generic)
def pb(info: dict):
#Key error safeguard
if 'priceToBook' not in info or info['priceToBook']== None:
return 1
#Check if the stock sector is in the median list
sect = info['sector']
val = info['priceToBook']
if sect in medians:
return evaluateLower(val, sect, 'PB', medians)
#If not use to overall/generic medians to evaluate:
else:
return evaluateLower(val, sect, 'PB', generic)
def eps_growth(info: dict):
#Key error safeguard
if 'earningsGrowth' not in info or info['earningsGrowth']== None:
return 1
#Check if the stock sector is in the median list
sect = info['sector']
val = info['earningsGrowth']
if sect in medians:
return evaluateHigher(val, sect, 'EPS_gwth', medians)
#If not use to overall/generic medians to evaluate:
else:
return evaluateHigher(val, sect, 'EPS_gwth', generic)
#Liquidity Ratios
def quick_ratio(info: dict):
#Key error safeguard
if 'quickRatio' not in info or info['quickRatio']== None:
return 1
#Check if the stock sector is in the median list
sect = info['sector']
val = info['quickRatio']
if sect in medians:
return evaluateHigher(val, sect, 'qckRat', medians)
#If not use to overall/generic medians to evaluate:
else:
return evaluateHigher(val, sect, 'qckRat', generic)
def current_ratio(info: dict):
#Key error safeguard
if 'currentRatio' not in info or info['currentRatio']== None:
return 1
#Check if the stock sector is in the median list
sect = info['sector']
val = info['currentRatio']
if sect in medians:
return evaluateHigher(val, sect, 'currRat', medians)
#If not use to overall/generic medians to evaluate:
else:
return evaluateHigher(val, sect, 'currRat', generic)
#Profitability Ratios
def operating_margin(info: dict):
#Key error safeguard
if 'operatingMargins' not in info or info['operatingMargins']== None:
return 1
#Check if the stock sector is in the median list
sect = info['sector']
val = info['operatingMargins']
if sect in medians:
return evaluateHigher(val, sect, 'opMar', medians)
#If not use to overall/generic medians to evaluate:
else:
return evaluateHigher(val, sect, 'opMar', generic)
def return_on_equity(info: dict):
#Key error safeguard
if 'returnOnEquity' not in info or info['returnOnEquity']== None:
return 1
#Check if the stock sector is in the median list
sect = info['sector']
val = info['returnOnEquity']
if sect in medians:
return evaluateHigher(val, sect, 'ROE', medians)
#If not use to overall/generic medians to evaluate:
else:
return evaluateHigher(val, sect, 'ROE', generic)
#Solvency Ratios
def de(info: dict):
#Key error safeguard
if 'debtToEquity' not in info or info['debtToEquity']== None:
return 1
#Check if the stock sector is in the median list
sect = info['sector']
val = info['debtToEquity']
if sect in medians:
return evaluateLower(val, sect, 'debtoEq%', medians)
#If not use to overall/generic medians to evaluate:
else:
return evaluateLower(val, sect, 'debtoEq%', medians)