-
Notifications
You must be signed in to change notification settings - Fork 8
/
trader.py
352 lines (318 loc) · 13.4 KB
/
trader.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
import api_btce
import api_stamp
import time
import logging
from datetime import datetime
import pickle
import simulator
class trader(object):
def __init__(self, arg):
self.arg = arg
self.simMode = arg.get('simMode', 1)
self.config = arg.get('config', None)
self.breakpoint = arg.get('breakpoint', None)
self.plot = arg.get('plot', None)
self.log = logging.getLogger('trader')
self.tick_btce = api_btce.publicapi()
self.tick_stamp = api_stamp.publicapi()
if self.simMode != 0:
self.tapi_btce = api_btce.tradeapi(self.config.apikey_btce, self.config.apisecret_btce)
self.tapi_stamp = api_stamp.tradeapi(self.config.apikey_stamp, self.config.apisecret_stamp, self.config.apiid_stamp)
self.hr_btce = historyRecorder(self.arg, "btce")
self.hr_stamp = historyRecorder(self.arg, "stamp")
else:
self.hr_btce = simulator.simHistoryRecorder(self.arg, "btce")
self.hr_stamp = simulator.simHistoryRecorder(self.arg, "stamp")
self.sim_btce = simulator.simulator("btce", {"usd": 0, "btc": 1}, self.arg, self.hr_btce, 0.002)
self.sim_stamp = simulator.simulator("stamp", {"usd": 500, "btc": 0}, self.arg, self.hr_stamp, 0.005)
def update(self):
edge = self.getOrderEdge("btce")
if edge:
self.hr_btce.append(edge)
edge = self.getOrderEdge("stamp")
if edge:
self.hr_stamp.append(edge)
def validateTimestamp(self, t):
"""Bitstamp sometimes goes crazy by showing orderbooks hours ago. Set a maximum tolerance of 5 mins"""
return 300 > abs(int(t) - int(time.time()))
def getOrderEdge(self, exchange):
"""return [time, bid1 price, bid1 vol, ask1 price, ask1 vol]"""
dict = {}
orderbook = {}
if exchange == "btce":
orderbook = self.tick_btce.depth("btc_usd")
elif exchange == "stamp":
orderbook = self.tick_stamp.depth()
else:
print "Stock name error"
try:
if exchange == "stamp" and not self.validateTimestamp(orderbook['timestamp']):
print orderbook['timestamp']
raise ValueError('Timestamp mismatch in %s' % exchange)
dict['timestamp'] = int(time.time())
dict['bid1'] = [float(x) for x in orderbook['bids'][0]]
dict['ask1'] = [float(x) for x in orderbook['asks'][0]]
except Exception as e:
print "Exception", e
return None
data = (dict['timestamp'], dict['bid1'][0], dict['bid1'][1], dict['ask1'][0], dict['ask1'][1])
# print "exchange", exchange, data
return data
def getBalance(self, exchange):
"""return a dict of {"btc": 0, "usd": 0}"""
balance = {"btc": 0, "usd": 0}
try:
if exchange == "btce":
result = self.tapi_btce.update()
funds = result.get('funds', None)
balance["btc"] = float(funds.get('btc', 0))
balance["usd"] = float(funds.get('usd', 0))
elif exchange == "stamp":
result = self.tapi_stamp.account_balance()
balance["btc"] = float(result.get('btc_balance', 0))
balance["usd"] = float(result.get('usd_balance', 0))
else:
balance1 = self.getBalance("btce")
balance2 = self.getBalance("stamp")
balance["btc"] = balance1["btc"] + balance2["btc"]
balance["usd"] = balance1["usd"] + balance2["usd"]
return balance
except Exception as e:
print "Exception", e
print exchange, balance
return balance
def placeOrder(self, orderType, rate, amount, seconds = 0, exchange=""):
if seconds == 0:
seconds = 60
if exchange == "btce":
pair = "btc_usd"
if amount < 0.1: # can't trade < 0.1
# self.log.warning('Attempted order below 0.1: %s' % amount)
print "attempted order below 0.1"
return False
else:
# self.log.info('Placing order')
print "placing order"
time.sleep(1)
rate = round(rate, 3)
response = self.tapi_btce.trade(pair, orderType, rate, amount)
if response is None:
print "Place Order Error"
return None
if response['success'] == 0:
response = response['error']
# self.log.critical('Order returned error:/n %s' % response)
print ('Order returned error:/n %s' % response)
return False
elif response.get('return').get('remains') == 0:
# self.log.debug('Trade Result: %s' % response)
print ('Trade Result: %s' % response)
return True
else:
response = response['return']
# self.trackOrder(response, self.config.pair, orderType, rate, seconds)
# self.log.info('Order Placed, awaiting fill: %s' % (response))
print "Order placed awaiting fill"
# self.log.info('Start tracking Orders')
# self.needTrackOrders = True
return True
elif exchange == "stamp":
if amount * rate < 5: # can't trade < 0.1
# self.log.warning('Attempted order below 0.1: %s' % amount)
print "attempted order below $5"
return False
else:
# self.log.info('Placing order')
time.sleep(1)
rate = round(rate, 3)
if orderType == 'buy':
response = self.tapi_stamp.buy_limit_order(amount, rate)
elif orderType == 'sell':
response = self.tapi_stamp.sell_limit_order(amount, rate)
else:
response = None
print "order Type error"
print response
# self.trackOrder(response, self.config.pair, orderType, rate, seconds)
# self.log.info('Order Placed, awaiting fill: %s' % (response))
# self.log.info('Start tracking Orders')
# self.needTrackOrders = True
print "Order placed awaiting fill"
return True
else:
print "Stock name error"
# def getTradeAmount(self, exchange, orderType):
# balanceDict = self.getBalance(exchange)
# orderEdge = self.getOrderEdge(exchange)
# amount = 0
# max_amount = 0
# if orderType == "buy":
# rate, amount = orderEdge[3], orderEdge[4] # lowest ask price and vol
# balance = balanceDict['usd']
# max_amount = round((balance / rate), 8) - 0.0005
# if orderType == "sell":
# rate, amount = orderEdge[1], orderEdge[2] # higest bid price and vol
# balance = balanceDict['btc']
# max_amount = round(balance, 8) - 0.0005
# return min(amount, max_amount)
def trade(self, exchange, orderType, amount, rate=0, suggested_rate = 0, seconds=0):
if self.simMode == 0:
if exchange == "btce":
current_rate = float(self.hr_btce.getValue()[1]) #todo: not 1?
sim = self.sim_btce
elif exchange == "stamp":
current_rate = float(self.hr_stamp.getValue()[1])
sim = self.sim_stamp
else:
print "exchange error"
return False
if rate == 0:
rate = current_rate
# if percentage != 0:
# amount = sim.usd * percentage / rate
# amount = max(0.1, amount)
if orderType == "buy":#todo: combine them to trade
sim.buy(rate, amount)
else:
sim.sell(rate, amount)
return True
### real trading
isPriceDeviated = False
if rate == 0:
isMarketOrder = True
else:
isMarketOrder = False
if isMarketOrder:
orderEdge = self.getOrderEdge(exchange)
if orderEdge is None:
return False
try:
if orderType == "buy":
rate, rawamount = orderEdge[3], orderEdge[4] # lowest ask price and vol
if abs(suggested_rate - rate) > 1:
isPriceDeviated = True
if orderType == "sell":
rate, rawamount = orderEdge[1], orderEdge[2] # higest bid price and vol
if abs(suggested_rate - rate) > 1:
isPriceDeviated = True
except Exception as e:
print e
if isPriceDeviated:
print('Suggested rate %s, actual rate %s, order cancelled.' % (str(suggested_rate), str(rate)))
return False #todo: and what?
if exchange == "btce":
min_amount = 0.1
elif exchange == "stamp":
min_amount = 5 / float(rate)
else:
min_amount = 0
if amount < min_amount:
print ("not enough money or coin to %s %s" % (orderType, str(amount)))
return False
amount = round(amount, 3)
if self.simMode == 1:
self.log.info('%s Attempted %s: %s vol %s' % (exchange, orderType, rate, amount))
return True
elif self.simMode == 2:
#self.log.info('Attempted buy: %s %s vol %s' % (pair, rate, amount))
self.log.info('%s Attempted %s: %s vol %s' % (exchange, orderType, rate, amount))
isOrderSuccess = self.placeOrder(orderType, rate, amount, seconds, exchange)
if isOrderSuccess:
#self.log.info('Order successfully placed')
print ('Order successfully placed')
return True
else:
#self.log.info('Order placing failed')
print ('Order placing failed')
return False
def openOrders(self, exchange):
if exchange == "btce":
raw = self.tapi_btce.activeOrders()
if raw is None:
print "ActiveOrder is None"
return raw.get('return', {})
elif exchange == "stamp":
raw = self.tapi_stamp.open_orders()
return raw
else:
print "exchange name error"
return None
def isAllOrderClosed(self):
try:
openOrders_btce = self.openOrders("btce")
openOrders_stamp = self.openOrders("stamp")
if openOrders_btce is None or openOrders_stamp is None:
return False
else:
return len(openOrders_stamp) == 0 and len(openOrders_btce) == 0
except Exception, e:
print e
def killAll(self, exchange):
if exchange == "btce":
raw = self.tapi_btce.activeOrders()
if raw is None:
print "ActiveOrder is None"
updatedOrders = raw.get('return', {})
for orderID in updatedOrders.keys():
raw = self.tapi_btce.cancelOrder(orderID)
if raw is None:
print "Cancelling error"
return False
print "Cancelling Success"
time.sleep(1)
elif exchange == "stamp":
raw = self.tapi_stamp.open_orders()
for d in raw:
raw = self.tapi_stamp.cancel_order(d["id"])
if not raw:
print "Cancel error"
return False
print "Cancel success"
else:
print "exchange name error"
return False
return True
class historyRecorder(object):
def __init__(self, arg, exchange):
self.arg = arg
self.simMode = arg.get('simMode', 1)
self.config = arg.get('config', None)
self.breakpoint = arg.get('breakpoint', None)
self.plot = arg.get('plot', None)
self.exchange = exchange
self.data = []
self.single = []
# get save file name
if self.exchange == "btce":
self.savefile = self.config.savefile_btce
elif self.exchange == "stamp":
self.savefile = self.config.savefile_stamp
self.load()
def save(self):
if self.savefile:
with open(self.savefile, 'wb') as f:
pickle.dump(self.data, f)
print "Save", self.savefile
def load(self):
try:
f = open(self.savefile, 'rb')
self.data = pickle.load(f)[-10000:]
print "Current length of", self.savefile, len(self.data)
item = self.data[-1]
strdate = datetime.fromtimestamp(item[0]).isoformat(' ')
print strdate, item
f.close()
except (IOError, TypeError, EOFError), e:
print 'Load price info error'
print e
def append(self, item):
"""
item = (int[timestamp], float['bid1'][price], float['bid1'][volume], float['ask1'][price], float['ask1'][volume])
"""
if item is not None:
self.data.append(item)
def getData(self):
# '''Change to constantly updating version'''
return self.data
def getValue(self):
return self.data[-1]