-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
sss_peg_ratios.py
107 lines (100 loc) · 4.98 KB
/
sss_peg_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
#############################################################################
#
# Version 0.2.0 - Author: Asaf Ravid <[email protected]>
#
# Credit to the PEG Ratio fetching source code: https://github.com/rickturner2001
#
# Stock Screener and Scanner - based on yfinance
# Copyright (C) 2021 Asaf Ravid
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
#############################################################################
#
# Declare the tickers to check in the symbols list as strings or even better use a csv file.
# If you decide to use a .csv file remember to change the for loop (e.g. for symbol in symbols['Symbol'])
import csv
import yfinance as yf
import sss
import requests
from bs4 import BeautifulSoup
symbols_united_states = []
etf_and_nextshares_list = []
nasdaq_filenames_list = ['Indices/nasdaqlisted.csv', 'Indices/otherlisted.csv', 'Indices/nasdaqtraded.csv'] # Checkout http://www.nasdaqtrader.com/trader.aspx?id=symboldirdefs for all symbol definitions (for instance - `$` in stock names, 5-letter stocks ending with `Y`)
ticker_column_list = [0, 0, 1] # nasdaqtraded.csv - 1st column is Y/N (traded or not) - so take row[1] instead!!!
sss.download_ftp_files(nasdaq_filenames_list, 'ftp://ftp.nasdaqtrader.com/SymbolDirectory/')
for index, filename in enumerate(nasdaq_filenames_list):
with open(filename, mode='r', newline='') as engine:
reader = csv.reader(engine, delimiter='|')
next_shares_column = None
etf_column = None
row_index = 0
for row in reader:
if row_index == 0:
row_index += 1
# Find ETF and next shares Column:
for column_index, column in enumerate(row):
if column == 'ETF':
etf_column = column_index
elif column == 'NextShares':
next_shares_column = column_index
continue
else:
row_index += 1
if 'File Creation Time' in row[0]:
continue
if next_shares_column and row[next_shares_column] == 'Y':
etf_and_nextshares_list.append(row[ticker_column_list[index]])
continue
if etf_column and row[etf_column] == 'Y':
etf_and_nextshares_list.append(row[ticker_column_list[index]])
continue
if '$' in row[
ticker_column_list[index]]: # AAIC$B -> <stock_symbol>$<letter> --> keep just the stock_Symbol
stock_symbol = row[ticker_column_list[index]].split('$')[0]
else:
stock_symbol = row[ticker_column_list[index]]
symbols_united_states.append(stock_symbol)
symbols = symbols_united_states
for symbol in symbols:
eff_symbol = symbol.replace(".","-") # Dot notation on symbols (BRK.B) will return null values if used with yfinance
info = yf.Ticker(eff_symbol).get_info()
if "pegRatio" not in info:
continue
api_peg = info['pegRatio'] # Getting the yfinance PEG Ratio
URL = f'https://stockanalysis.com/stocks/{symbol}/statistics/'
r = requests.get(URL)
if r.status_code == 200:
soup = BeautifulSoup(r.content, 'html.parser')
my_table = soup.findAll('table', {'class': 'text-sm xs:text-base StatsWidget_statstable__9KlU0'})
for i in my_table:
if 'PEG' in str(i):
ratio_iloc = str(i).find('PEG Ratio')
peg_ratio = str(i)[ratio_iloc + 39:ratio_iloc + 43]
try:
peg_ratio = float(peg_ratio)
print(f"{symbol}, {api_peg}, {peg_ratio}")
except:
# First exception: double digits and negative values.
try:
peg_ratio = str(i)[ratio_iloc + 40:ratio_iloc + 45]
peg_ratio = float(peg_ratio)
print(f"{symbol}, {api_peg}, {peg_ratio}")
except Exception as e:
# Empty td tag in the page means N/A value
if str(peg_ratio) == '</td>':
peg_ratio = 'N/A'
print(f"{symbol}, {api_peg}, {peg_ratio}")
else:
print('Something went wrong trying to convert the PEG Ratio: ',symbol, peg_ratio)