-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstockPricePull.py
More file actions
47 lines (41 loc) · 1.61 KB
/
stockPricePull.py
File metadata and controls
47 lines (41 loc) · 1.61 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
#############################
#
# Get prices
# from yahoo:
# http://chartapi.finance.yahoo.com/instrument/1.0/aapl/chartdata;type=quote;range=1y/csv
# fed rate from :
# http://research.stlouisfed.org/fred2/data/FEDFUNDS.txt
import urllib2
import time
from datetime import datetime
stockToPull = 'AAPL'
def pullData(stock):
try:
fileLine = stock + '.txt'
urlToVisit = 'http://chartapi.finance.yahoo.com/instrument/1.0/%s/chartdata;type=quote;range=1y/csv' % (stock)
sourceCode = urllib2.urlopen(urlToVisit).read()
splitSource = sourceCode.split('\n')
for curline in splitSource:
splitLine = curline.split(',')
if len(splitLine) == 6:
if 'values' not in splitLine[0]:
saveFile = open('C:\\Users\\throbby\\Documents\\' + fileLine,'a')
lineToWrite = curline + '\n'
saveFile.write(lineToWrite)
print 'Pulled %s' % (stock.upper())
except Exception,e:
print 'main loop',str(e)
def pullRate():
try:
fileLine = 'rates.txt'
urlToVisit = 'http://research.stlouisfed.org/fred2/data/FEDFUNDS.txt'
sourceCode = urllib2.urlopen(urlToVisit).read()
saveFile = open('C:\\Users\\throbby\\Documents\\' + fileLine,'a')
dateLines = sourceCode.split('\r\n')
for curLine in dateLines[61:-1]:
curline = curLine.replace('-','')
curline = curline.split()
outputLine = curline[0] + ',' + curline[1] + '\n'
saveFile.write(outputLine)
except Exception,e:
print 'main loop',str(e)