-
Notifications
You must be signed in to change notification settings - Fork 0
/
stocktemplate.py
88 lines (36 loc) · 1.41 KB
/
stocktemplate.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import datetime
import MySQLdb as mdb
import argparse
# Obtain a database connection to the MySQL instance
db_host = 'localhost'
db_user = 'ralph'
db_pass = 'thriofch'
db_name = 'sandp'
parser = argparse.ArgumentParser()
parser.add_argument('-s', '--stock', type=str, help='ticker symbol', required=True)
args = parser.parse_args()
stock = args.stock
con = mdb.connect(db_host, db_user, db_pass, db_name)
def obtain_parse_wiki_snp500():
with open('%s.csv' % stock ) as yf_data:
# On failure, print an error message.
for y in yf_data:
prices = []
p = y.rstrip('\n')
p = y.strip().split(',')
prices.append( (
("%s" % stock), p[0], p[1], p[2], p[3], p[4],p[5] ) )
print prices
# Create the insert strings
column_str = """symbol,date,close,volume,open,high,low"""
insert_str = ("%s, " * 7)[:-2]
final_str = "INSERT INTO daily_closes(%s) VALUES (%s)" % (column_str, insert_str)
# Using the MySQL connection, carry out an INSERT INTO for every symbol
with con:
cur = con.cursor()
cur.executemany(final_str, prices)
print prices
if __name__ == "__main__":
prices = obtain_parse_wiki_snp500()