-
Notifications
You must be signed in to change notification settings - Fork 1
/
crexprice_1run.py
executable file
·118 lines (91 loc) · 3.57 KB
/
crexprice_1run.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
#!/usr/bin/python
import urllib, urllib2, simplejson, threading, pymongo, datetime
def getPrice():
# threading.Timer(30.0, getPrice).start()
### DB stuffs
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["qbot"]
mycol = mydb["crexprice"]
mypairs = mydb["crexpairs"]
weathercol = mydb["weather"]
### time
curtime = datetime.datetime.now()
#print curtime
### URL data/fetch loop
url = 'https://api.crex24.com/v2/public/tickers'
class MyException(Exception):
pass
try:
response = urllib2.urlopen(url, timeout = 5)
except urllib2.URLError, e:
raise MyException("There was an error: %r" % e)
data = simplejson.load(response)
coinpairs = []
for x in mypairs.find():
pair = x['pair']
coinpairs.append(pair)
curweath = []
getweather = weathercol.find()
for line in getweather:
pair = line['pair']
low = line['low']
high = line['high']
date = line['date']
score = line['score']
last = line['last']
pct = line['percentchange']
insweath = (pair, low, high, date, score, last, pct)
curweath.append(insweath)
for h in curweath:
curpair = h[0]
curlow = h[1]
curhigh = h[2]
curdate = h[3]
curscore = h[4]
curlast = h[5]
curpct = [6]
mypairs.drop()
mycol.drop()
pricelist = []
for line in data:
pair = line['instrument']
last = line['last']
percentchange = line['percentChange']
low = line['low']
high = line['high']
basevol = line['baseVolume']
quotevol = line['quoteVolume']
volbtc = line['volumeInBtc']
volusd = line['volumeInUsd']
ask = line['ask']
bid = line['bid']
priceins = (pair, low, high, last)
pricelist.append(priceins)
datains = { "pair": pair, "last": last, "percentchange": percentchange, "low": low, "high": high, "basevol": basevol, "quotevol": quotevol, "volbtc": volbtc, "volusd": volusd, "ask": ask, "bid": bid, "date": datetime.datetime.now() }
mycol.insert_one(datains)
pairins = { "pair": pair }
mypairs.insert_one(pairins)
for h in curweath:
curpair = h[0]
curlow = h[1]
curhigh = h[2]
curdate = h[3]
curscore = h[4]
curlast = h[5]
curpct = [6]
if pair == curpair:
if low < curlow:
print "LOW", pair, last, low, curpair, curlow
pairins = { "pair": pair }
weatherlow = { "$set": { "last": last, "high": curhigh, "date": datetime.datetime.now(), "score": curscore, "low": low, "percentchange": curpct, "pair": curpair }}
weathercol.update_many(pairins, weatherlow)
if high > curhigh:
print "HIGH", pair, last, high, curpair, curhigh
pairins = { "pair": pair }
weatherhi = { "$set": { "pair": curpair, "last": last, "percentchange": curpct, "low": curlow, "high": high, "score": curscore, "date": datetime.datetime.now() }}
weathercol.update_many(pairins, weatherhi)
else:
pairins = { "pair": pair }
weatherins = { "$set": { "last": last, "high": curhigh, "date": datetime.datetime.now(), "score": curscore, "low": curlow, "percentchange": curpct, "pair": curpair }}
weathercol.update_many(pairins, weatherins)
getPrice()