-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathData.py
More file actions
31 lines (29 loc) · 1.02 KB
/
Data.py
File metadata and controls
31 lines (29 loc) · 1.02 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
import urllib.request
import json
import time
import datetime
def fetchPreMarket(symbol, exchange):
link = "http://finance.google.com/finance/info?client=ig&q="
url = link+"%s:%s" % (exchange, symbol)
u = urllib.request.urlopen(url)
content = u.read().decode()
data = json.loads(content[3:])
info = data[0]
t = str(info["lt_dts"]) # time stamp
l = float(info["pcls_fix"]) # close price (previous trading day)
p = float(info["l"]) # stock price in pre-market (after-hours)
return (t,l,p)
p0 = 0
while True:
try:
t,l,p = fetchPreMarket("INVP","LON")
with open("C:\Test\invp.l.txt",'a') as f:
f.write("%s,%s,%s\n"%(t,str(l),str(p)))
f.close()
except (RuntimeError, TypeError, NameError) as e:
print(e)
if(p!=p0):
p0 = p
print("%s\t%.2f\t%.2f\t%+.2f\t%+.2f%%" % (#datetime.datetime.now().strftime("%I:%M:%S%p on %d %B %Y"),
t, l, p, p-l,(p/l-1)*100.))
time.sleep(60)