-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·89 lines (70 loc) · 2.96 KB
/
main.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
#!/usr/bin/env python
# coding:utf-8
'''
Utility for fetching Google intraday market data.
'''
from __future__ import print_function
import os
import argparse
import logging
from datetime import datetime
from sqlalchemy import func
from sqlalchemy.orm import sessionmaker
import csv
import schema
from schema import Quote
from google_finace import fetch_intraday_quotes
def _load_symbols(fname):
with open(fname, 'rb') as f:
return [x.rstrip() for x in f.readlines()]
def _to_csv(fname, values):
''' Export marketdata for specified symbol to csv file '''
with open(fname, 'wb') as f:
writer = csv.writer(f)
writer.writerow(['Date', 'Time', 'Open', 'High', 'Low', 'Close', 'Volume'])
for x in values:
writer.writerow([x.datetime.date(), x.datetime.time(), x.open, x.high, x.low, x.close, x.volume])
def _now():
now = datetime.now()
return '%d-%02d-%02d_%02d-%02d-%02d' % (now.year, now.month, now.day, now.hour, now.minute, now.second)
_LOG_DIR = './logs'
def fetch(symbols_file, symbols, session):
if not os.path.isdir(_LOG_DIR):
os.mkdir(_LOG_DIR)
fname = '%s.log' % _now()
logging.basicConfig(filename=os.path.join(_LOG_DIR, fname), level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
logging.info('Processing file: %s, number of symbols: %d' % (symbols_file, len(symbols)))
for s in symbols:
try:
quotes = fetch_intraday_quotes(s)
if quotes:
last_date = session.query(func.max(Quote.datetime)).filter_by(symbol=s).all()[0][0]
add = [Quote(*q) for q in quotes if q[1] > last_date] if last_date else [Quote(*q) for q in quotes]
session.add_all(add)
session.commit()
logging.info('%s: updated %d from %d' % (s, len(add), len(quotes)))
else:
logging.info('No marketdata retrieved for symbol %s' % s)
except Exception as e:
logging.error('Failed to update symbol %s, error: %s' % (s, str(e)))
session.rollback()
def output2csv(symbols, session):
outpath = "./out-%s" % _now()
os.makedirs(outpath)
for s in symbols:
qres = session.query(Quote).filter(Quote.symbol == s).order_by(Quote.datetime)
_to_csv(os.path.join(outpath, '%s.csv' % s), qres)
def main(symbols_file, output):
symbols = _load_symbols(symbols_file)
schema.init()
dbsession = sessionmaker(bind=schema.engine)()
if not output:
fetch(symbols_file, symbols, dbsession)
else:
output2csv(symbols, dbsession)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Fetches Google intraday market data.')
parser.add_argument('-o', '--output', action='store_true', help='flag which indicates if output csv should be generated')
parser.add_argument('symbols', metavar='symbols', type=str, help='a file with symbols')
args = parser.parse_args()
main(args.symbols, args.output)