-
Notifications
You must be signed in to change notification settings - Fork 8
/
bot.py
183 lines (150 loc) · 4.92 KB
/
bot.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
import urllib2, json, time, btcchina, math, sys
class BTCChinaMM(object):
def __init__(self, public_key, private_key):
self.btcchina = btcchina.BTCChina(public_key, private_key)
# minimum trade size
self.min_btc = .001
# maximum to be actively trading at one time
self.max_btc = 10
self.orders = []
for order in self.btcchina.get_orders()['order']:
print 'Canceled Order #%s' % order['id']
self.btcchina.cancel(order['id'])
print 'Setup complete'
time.sleep(2)
def trade_spread(self):
prev_balances = prev_ticker = ''
while (True):
if not self.update_balances() or not self.update_prices():
print 'Can\t reach server!'
time.sleep(10)
continue
balances = 'Balances: %s CNY, %s BTC' % (self.cny, self.btc)
printed = False
if prev_balances != balances:
print balances
prev_balances = balances
printed = True
if self.ticker != prev_ticker:
print self.ticker
prev_ticker = self.ticker
printed = True
if not printed:
print '...',
sys.stdout.flush()
if self.is_buying():
for buy_order in self.get_orders('buy'):
order_response = self.btcchina.get_orders(buy_order['id'], False)
if order_response is None or 'order' not in order_response or order_response['order']['status'] != 'open':
self.remove_order(buy_order['id'])
self.log('filled buy spread: %s' % buy_order['spread'])
print '>>> BUY order fulfilled!'
if self.ticker['buy'] > buy_order['price']:
self.cancel_order(buy_order['id'])
if self.is_selling():
for sell_order in self.get_orders('sell'):
order_response = self.btcchina.get_orders(sell_order['id'], False)
if order_response is None or 'order' not in order_response or order_response['order']['status'] != 'open':
self.remove_order(sell_order['id'])
self.log('filled sell spread: %s' % sell_order['spread'])
print '>>> SELL order fulfilled!'
elif self.ticker['sell'] < sell_order['price']:
self.cancel_order(sell_order['id'])
if self.btc > self.min_btc:
self.new_order('sell')
if (self.cny / self.ticker['buy']) > self.min_btc:
self.new_order('buy')
time.sleep(3)
def update_prices(self):
try:
url = "https://data.btcchina.com/data/orderbook?market=cnybtc"
response = urllib2.urlopen(url);
data = json.loads(response.read())
self.ticker = {
'buy': float(data['bids'][0][0]),
'buy_volume': float(data['bids'][0][1]),
'sell': float(data['asks'][-1][0]),
'sell_volume': float(data['asks'][-1][1])
}
return True
except:
return False
def get_last_trade_price(self):
try:
url = "https://data.btcchina.com/data/ticker?market=cnybtc"
response = urllib2.urlopen(url);
data = json.loads(response.read())
return int(float(data['ticker']['last']) * 100)
except:
return False
def get_spread(self):
return self.ticker['sell'] - self.ticker['buy']
def new_order(self, type):
if type == 'buy' or type == 'sell':
if type == 'buy':
price = self.get_buy_price()
amount = math.floor((self.cny / price) * 1000) / 1000
else:
price = self.get_sell_price()
amount = min(self.max_btc, math.floor(self.btc * 1000) / 1000)
if amount < self.min_btc:
print 'Amount is too low %s %s' % (type, amount)
return False
else:
if type == 'buy':
order_id = self.btcchina.buy(price, amount)
else:
order_id = self.btcchina.sell(price, amount)
if order_id is not None and isinstance(order_id, (int, long)):
print '%sing %s BTC for %s. Order #%s' % (type.title(), amount, price, order_id)
order = {
'id': order_id,
'price': price,
'amount': amount,
'type': type,
'spread': self.get_spread()
}
self.orders.append(order)
else:
print 'Order has gone very wrong: %s' % order_id
return False
def cancel_order(self, order_id):
if self.btcchina.cancel(order_id) == True:
print 'Canceled order #%s' % order_id
self.remove_order(order_id)
return True
else:
print 'Couldn\'t cancel order #%s' % order_id
return False
def remove_order(self, order_id):
self.orders = [x for x in self.orders if not x['id'] == order_id]
def update_balances(self):
try:
account_info = self.btcchina.get_account_info()
self.btc = float(account_info['balance']['btc']['amount'])
self.cny = float(account_info['balance']['cny']['amount'])
return True
except:
return False
def get_orders(self, type):
orders = [x for x in self.orders if x['type'] == type]
return orders
def is_selling(self):
return len(self.get_orders('sell')) > 0
def is_buying(self):
return len(self.get_orders('buy')) > 0
def get_sell_price(self):
return self.ticker['sell']
def get_buy_price(self):
return self.ticker['buy']
def log(self, msg):
f = open('out.txt', 'w')
f.write(msg + '\n')
f.close()
def go():
public_key = ''
private_key = ''
bot = BTCChinaMM(public_key, private_key)
bot.trade_spread()
if __name__ == '__main__':
go()