-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtickers.py
More file actions
51 lines (42 loc) · 1.85 KB
/
tickers.py
File metadata and controls
51 lines (42 loc) · 1.85 KB
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
import requests
import iex
import pandas as pd
from io import StringIO
import sys
# wrote just to save time finding valid tickers
def get_valid_tickers(outfile='valid_tickers.csv'):
"""
Gets all valid tickers into a file 'valid_tickers.csv' to save computation time.
"""
url = r'https://www.nasdaq.com/screening/companies-by-industry.aspx?exchange=NASDAQ&render=download'
data = requests.get(url, allow_redirects=True)
# read csv from a string
df = pd.read_csv(StringIO(data.text))
newdf = df[filter_valid(df['Symbol'])].loc[:, 'Symbol']
newdf.to_csv('valid_tickers.csv')
def filter_valid(tickers):
"""
Returns a list of bools, the ith of which indicates whether the ith ticker in the input is valid or not.
"""
valid_tickers = set(pd.read_csv(
'valid_tickers.csv', index_col=0, squeeze=True, header=None))
return [i in valid_tickers for i in tickers]
def save_tickers(n, filename='tickers.txt'):
"""
Gets a csv of tickers from nasdaq.com and filters them based on whether they are valid or not. Then, outputs the first n to a file 'tickers.txt'.
"""
url = r'https://www.nasdaq.com/screening/companies-by-industry.aspx?exchange=NASDAQ&render=download'
data = requests.get(url, allow_redirects=True)
# read csv from a string
df = pd.read_csv(StringIO(data.text))
# filter out tickers which are valid and send to a file
df[filter_valid(df['Symbol'])].loc[:n-1, 'Symbol'].to_csv(filename,
index=False,
header=False)
if __name__ == "__main__":
try:
n = int(sys.argv[1])
filename = sys.argv[2]
save_tickers(n, filename)
except IndexError as e:
print('Format:\n $ python tickers.py <number of tickers> <ticker file name>')