-
Notifications
You must be signed in to change notification settings - Fork 1
/
emdr.py
executable file
·322 lines (275 loc) · 12.4 KB
/
emdr.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#!/usr/bin/env python2
import logging
import simplejson
import dateutil.parser
import calendar
from datetime import datetime
import gevent
from gevent.pool import Pool
from gevent import monkey; gevent.monkey.patch_all()
import zmq as zmq
import zlib
import sys
import redis
import os
abs_path = os.path.dirname(os.path.realpath(__file__))
# The maximum number of greenlet workers in the greenlet pool.
MAX_NUM_POOL_WORKERS = 500
DEBUG = False # prints out a bunch of info.
REGIONS = abs_path + '/regions.json' # set this to a file containing JSON of regions or False for all regions
VERSION = 1 # Change every time format for Redis values changes
REQUEST_TIMEOUT = 10000 # 10s
RELAY_RETRIES = 5 # retries per relay
MAX_RETRIES = False # set to an int if you want to terminate script after x retries
RELAYS = [ # top relay will be primary
'tcp://relay-us-east-1.eve-emdr.com:8050',
'tcp://relay-us-central-1.eve-emdr.com:8050',
'tcp://relay-us-west-1.eve-emdr.com:8050'
]
ORDERS = True # collect information on orders
HISTORY = False # not implemented
##////////////////
## END CONFIG
##////////////////
NUM_RELAYS = len(RELAYS);
redis = redis.StrictRedis(host='localhost', port=6379, db=0)
class Printer():
"""
Print things to stdout on one line dynamically
"""
def __init__(self,data):
sys.stdout.write("\r\x1b[K"+data.__str__())
sys.stdout.flush()
def main():
global utd # there should be a better way to do these than with globals
retry_relay = 0 # retry count per relay
retry_max = 0 # retry count total
relay = 0 # current relay in use
utd = 0 # increments every time we have an already UTD cache
cont = True # flag to exit out of loop
# Relay connection
context = zmq.Context()
client = context.socket(zmq.SUB)
client.connect(RELAYS[relay])
client.setsockopt(zmq.SUBSCRIBE, "") # Disable filtering.
poll = zmq.Poller()
poll.register(client, zmq.POLLIN)
# We use a greenlet pool to cap the number of workers at a reasonable level.
greenlet_pool = Pool(size=MAX_NUM_POOL_WORKERS)
print("Consumer daemon started, waiting for jobs...")
print("Worker pool size: %d" % greenlet_pool.size)
try:
int(redis.get('emdr-total-saved'))
except:
redis.set('emdr-total-saved', 0)
try:
int(redis.get('emdr-total-processed'))
except:
redis.set('emdr-total-processed', 0)
while cont:
socks = dict(poll.poll(REQUEST_TIMEOUT))
if socks.get(client) == zmq.POLLIN: # message recieved within timeout
greenlet_pool.spawn(worker, client.recv())
output = "%d/%d orders processed, %d skipped due to up-to-date cache" % \
(int(redis.get('emdr-total-saved')), \
int(redis.get('emdr-total-processed')), \
utd)
Printer(output) # update output to stdout
redis.incr('emdr-total-processed')
else:
print "\nW: No response from server, retrying... (",retry_relay," / ",RELAY_RETRIES,")"
client.setsockopt(zmq.LINGER, 0)
client.close()
poll.unregister(client)
retry_relay += 1
retry_max += 1
if MAX_RETRIES is not False and retry_max == MAX_RETRIES:
print "E: Server(s) seem to be offline, max attempts reached, terminating"
cont = False
break
elif retry_relay == RELAY_RETRIES: # we've reachec max retry attempts for this relay
if (relay == NUM_RELAYS-1): # if we're at te last relay loop around
print "E: Server seems to be offline, switching (looping)"
relay = 0
retry_relay = 0
else:
print "E: Server seems to be offline, switching"
relay += 1
retry_relay = 0
print "I: Reconnecting (relay: ",relay,")"
client = context.socket(zmq.SUB)
client.setsockopt(zmq.SUBSCRIBE, "")
client.connect(RELAYS[relay])
poll.register(client, zmq.POLLIN)
def worker(job_json):
'''
todo: look into logging to files per type id
recurse into rowsets: every feed is not necessarily 1 typeID (though it usually is)
'''
global utd;
if REGIONS is not False:
json_data = open(REGIONS)
regionDict = simplejson.load(json_data)
json_data.close()
# Receive raw market JSON strings.
market_json = zlib.decompress(job_json);
market_data = simplejson.loads(market_json);
# Gather some useful information
name = market_data.get('generator');
name = name['name'];
resultType = market_data.get('resultType');
rowsets = market_data.get('rowsets')[0]; # todo: recurse into others, not just [0]
typeID = rowsets['typeID'];
columns = market_data.get('columns');
# Convert str time to int
currentTime = dateutil.parser.parse(market_data.get('currentTime'));
generatedAt = calendar.timegm((dateutil.parser.parse(rowsets['generatedAt'])).utctimetuple());
numberOfSellItems = 0;
numberOfBuyItems = 0;
sellPrice = {}
buyPrice = {}
data = { # set defaults, will be overwritten during parsing, or use old cache?
'orders': {
'generatedAt': False,
'sell': False,
'buy': False
},
'history': {
'generatedAt': False}
}
if DEBUG: # write raw json to file
try:
file = open("type-"+str(typeID)+".txt", "w")
try:
file.write(market_json) # Write a string to a file
finally:
file.close()
except IOError:
pass
'''
Cache is in this format:
emdr-VERSION-REGIONID-TYPEID =
{'orders': {
'generatedAt': timestamp,
'sell': [fiveAverageSellPrice, numberOfSellItems],
'buy': [fiveAverageBuyPrice, numberOfBuyItems] }
'history': [] }
'''
if (REGIONS == False or (REGIONS != False and str(rowsets['regionID']) in regionDict)):
cached = redis.get('emdr-'+str(VERSION)+'-'+str(rowsets['regionID'])+'-'+str(typeID));
# If data has been cached for this item, check the dates. If dates match, skip
# todo: TEST TO MAKE SURE this is not deleting data, and only overwriting old cache
if (cached != None):
#if we have data in cache, split info into list
cache = simplejson.loads(cached);
if DEBUG:
print "\n\n(",resultType,") Cache:",cache
# parse date
if cache[resultType]['generatedAt'] is not False:
cachedate = cache[resultType]['generatedAt'];
if (DEBUG):
print "\nCached data found (result type: ",resultType,")!\n\tNew date: "+str(datetime.fromtimestamp(generatedAt))+"\n\tCached date: "+ str(datetime.fromtimestamp(cachedate));
if (generatedAt < cachedate):
utd += 1
if (DEBUG):
print "\t\tSKIPPING";
return '';
data = cache # set default data to cached data
if (ORDERS and resultType == 'orders'):
data['orders']['generatedAt'] = generatedAt
if (DEBUG):
print "\n\n\n\n======== New record ========";
# Start putting pricing info (keys) into dicts with volume (values)
for row in rowsets['rows']:
order = dict(zip(columns, row))
if (order['bid'] == False):
if (DEBUG):
print "Found sell order for "+str(order['price']) + "; vol: "+str(order['volRemaining']);
if (sellPrice.get(order['price']) != None):
sellPrice[order['price']] += order['volRemaining'];
else:
sellPrice[order['price']] = order['volRemaining'];
numberOfSellItems += order['volRemaining'];
else:
if (DEBUG):
print "Found buy order for "+str(order['price']) + "; vol: "+str(order['volRemaining']);
if (buyPrice.get(order['price']) != None):
buyPrice[order['price']] += order['volRemaining'];
else:
buyPrice[order['price']] = order['volRemaining'];
numberOfBuyItems += order['volRemaining'];
#end loop
if (DEBUG):
print "\nSell dict:",sellPrice
print "\nBuy dict:",buyPrice
print "\nTotal volume on market: ",numberOfSellItems," Sell + ",numberOfBuyItems
if (numberOfSellItems > 0):
prices = sorted(sellPrice.items(), key=lambda x: x[0]);
fivePercentOfTotal = max(int(numberOfSellItems*0.05),1);
fivePercentPrice=0;
bought=0;
boughtPrice=0;
if (DEBUG):
print "Sell Prices (sorted):\n",prices
print "Start buying process!"
while (bought < fivePercentOfTotal):
pop = prices.pop(0)
fivePercentPrice = pop[0]
if (DEBUG):
print "\tBought: ",bought,"/",fivePercentOfTotal
print "\t\tNext pop: ",fivePercentPrice," ISK, vol: ",pop[1]
if (fivePercentOfTotal > ( bought + sellPrice[fivePercentPrice])):
boughtPrice += sellPrice[fivePercentPrice]*fivePercentPrice;
bought += sellPrice[fivePercentPrice];
if (DEBUG):
print "\t\tHave not met goal. Bought:",bought
else:
diff = fivePercentOfTotal - bought;
boughtPrice += fivePercentPrice*diff;
bought = fivePercentOfTotal;
if (DEBUG):
print "\t\tGoal met. Bought:",bought
fiveAverageSellPrice = boughtPrice/bought;
if (DEBUG):
print "Average selling price (first 5% of volume):",fiveAverageSellPrice
data['orders']['sell'] = [
"%.2f" % fiveAverageSellPrice,
numberOfSellItems]
if (numberOfBuyItems > 0):
prices = sorted(buyPrice.items(), key=lambda x: x[0], reverse=True);
fivePercentOfTotal = max(int(numberOfBuyItems*0.05),1);
fivePercentPrice=0;
bought=0;
boughtPrice=0;
if (DEBUG):
print "Buy Prices (sorted):\n",prices
print "Start buying process!"
while (bought < fivePercentOfTotal):
pop = prices.pop(0)
fivePercentPrice = pop[0]
if (DEBUG):
print "\tBought: ",bought,"/",fivePercentOfTotal
print "\t\tNext pop: ",fivePercentPrice," ISK, vol: ",pop[1]
if (fivePercentOfTotal > ( bought + buyPrice[fivePercentPrice])):
boughtPrice += buyPrice[fivePercentPrice]*fivePercentPrice;
bought += buyPrice[fivePercentPrice];
if (DEBUG):
print "\t\tHave not met goal. Bought:",bought
else:
diff = fivePercentOfTotal - bought;
boughtPrice += fivePercentPrice*diff;
bought = fivePercentOfTotal;
if (DEBUG):
print "\t\tGoal met. Bought:",bought
fiveAverageBuyPrice = boughtPrice/bought;
if (DEBUG):
print "Average buying price (first 5% of volume):",fiveAverageBuyPrice
data['orders']['buy'] = [
"%.2f" % fiveAverageBuyPrice,
numberOfBuyItems]
redis.set('emdr-'+str(VERSION)+'-'+str(rowsets['regionID'])+'-'+str(typeID), simplejson.dumps(data));
if (DEBUG):
print 'SUCCESS: emdr-'+str(VERSION)+'-'+str(rowsets['regionID'])+'-'+str(typeID),simplejson.dumps(data)
redis.incr('emdr-total-saved')
if __name__ == '__main__':
main()