-
Notifications
You must be signed in to change notification settings - Fork 0
/
make_it_so.py
181 lines (157 loc) · 5.32 KB
/
make_it_so.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
import urllib2
import re
import lib
import db
from bs4 import BeautifulSoup
import random
host_prefix = 'http://finance.yahoo.com'
option_prefix = '/q/op?s='
def get_nasdaq_nyse_tickers():
nasdaq_file = '/Users/cinjon/Desktop/options/nasdaq.csv'
nyse_file = '/Users/cinjon/Desktop/options/nyse.csv'
nasdaq = lib.parse_tickerlist(nasdaq_file)
nyse = lib.parse_tickerlist(nyse_file)
random.shuffle(nasdaq)
random.shuffle(nyse)
return nasdaq, nyse
def get_table_tickers():
return db.retrieve_tickers_from_database(randomize=False)
def make_url(ticker):
return (host_prefix + option_prefix + ticker.capitalize() + '+Options')
def _make_dates_regex(ticker):
return (option_prefix + ticker + '&m=', re.compile(r'(\d{4})-(\d{2})$'))
def _regex_href(href, pattern):
return pattern.search(href)
def get_dates(soup, ticker):
ret = []
pre, pattern = _make_dates_regex(ticker)
for tag in soup.findAll(href=re.compile(ticker)):
href = tag.get('href')
if href and href[:len(pre)] == pre:
search = _regex_href(href[len(pre):], pattern)
if search:
ret.append(search.group())
return ret
def _parse_month_day(st):
parts = st.split(' ')
return (lib.month_to_day(parts[0].strip()), parts[1].strip())
def _parse_expiration(i):
parts = i.split(',')
year = parts[2].strip()
month, day = _parse_month_day(parts[1].strip())
ret = (str(year) + '-' + month + '-' + day)
return lib.dt_to_time(ret) + 13*lib.hour #Correcting for market close
def _find_expiration(td):
for child in td.findAll('td'):
if len(child.contents) > 0:
contents = child.contents
if 'Expire at' in contents[0]:
return _parse_expiration(contents[0])
return None
def numberize_dict(d):
'''Turn select fields of the dict d into explicit numbers'''
new_d = {}
for ty,value in d.iteritems():
if ty != 'symbol':
new_d[ty] = str(value).replace(',', '')
else:
new_d[ty] = value
return new_d
def _parse_pricing(tr):
'''tr has 8 children by being in here'''
tr = list(tr)
return numberize_dict({'strike':tr[0].string, 'symbol':tr[1].string, 'last':tr[2].string,
'vol':tr[6].string, 'open':tr[7].string})
def _parse_td(td):
calls = []
puts = []
call_flag = False
expiration = _find_expiration(td)
for child in td.findAll('tr'):
if len(child.contents) == 8:
if child.contents[0].string == 'Strike':
call_flag = not call_flag
continue
pricing = _parse_pricing(child)
pricing['expiration'] = expiration
if call_flag:
calls.append(pricing)
else:
puts.append(pricing)
return calls, puts
def parse(soup):
for td in soup.findAll('td'):
if len(td.contents) > 0 and 'View By Expiration' in td.contents[0]:
return _parse_td(td)
return [], []
def get_option_info(date, ticker):
url = host_prefix + option_prefix + ticker + '&m=' + date
page = lib.read_page(url)
# db.record_page_log(ticker, lib.now(), page)
soup = BeautifulSoup(lib.read_page(url))
return parse(soup)
def record_options(calls, puts, ticker):
now = lib.now()
if len(calls) > 0:
db.record_calls(calls, now, ticker)
if len(puts) > 0:
db.record_puts(puts, now, ticker)
def run_ticker(ticker):
ret = 0
url = make_url(ticker)
soup = BeautifulSoup(lib.read_page(url))
calls, puts = parse(soup)
if len(calls) + len(puts) > 0:
ret += 1
record_options(calls, puts, ticker)
for date in get_dates(soup, ticker):
calls, puts = get_option_info(date, ticker)
if len(calls) + len(puts) > 0:
ret += 1
record_options(calls, puts, ticker)
return ret
def run_nasdaq_nyse():
nasdaq, nyse = get_nasdaq_nyse_data()
time = lib.now()
print 'Started Running: %s' % time
count = 0
print 'Nasdaq Commencing: %s' % lib.now()
for ticker in nasdaq:
print ticker
run_ticker(ticker)
count += 1
lib.sleep(count)
nasdaq_time = lib.now()
print 'Nasdaq Finished, count: %s' % count
print 'NYSE Commencing: %s' % lib.now()
for ticker in nyse:
print ticker
run_ticker(ticker)
count += 1
lib.sleep(count)
nyse_time = lib.now()
print 'NYSE Finished, count: %s' % count
db.close_db()
print 'Finished. Timing for nasdaq: %s, Timing for nyse: %s' % (nasdaq_time - time, nyse_time - nasdaq_time)
def run_table_tickers():
time = lib.now()
tickers = get_table_tickers()
time_ticker = lib.now()
print 'Started Running: %s' % time_ticker
count = 0
pages_hit = 0
reached = False
try:
for ticker in tickers:
print ticker
pages_hit += run_ticker(ticker)
count += 1
lib.sleep(count)
db.close_db()
finally:
print 'Finished. Timing for getting tickers: %s, Timing for getting data: %s, Sleep count: %s, Pages Hit: %s' % (time_ticker - time, lib.now() - time_ticker, count/lib.sleep_mod, pages_hit)
def run():
run_table_tickers()
# run_nasdaq_nyse()
if __name__=="__main__":
run()