-
Notifications
You must be signed in to change notification settings - Fork 0
/
trade.rb
401 lines (329 loc) · 10.1 KB
/
trade.rb
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#coding: utf-8
require './bter'
require 'chinashop'
require './log'
require 'debugger'
class LackMoneyException < RuntimeError
end
class Trade
attr_accessor :debug, :price_spread, :retry_limit, :piece,
:bter_cny_balance, :bter_btc_balance, :bter_buy_price, :bter_sell_price,
:btcc_cny_balance, :btcc_btc_balance, :btcc_buy_price, :btcc_sell_price
def initialize bter, btcc
@bter = bter
@btcc = btcc
#default:
self.price_spread = 50
self.retry_limit = 3
self.debug = false
self.piece = 0.01
end
def set_prices
retry_times ||= 0
print '正在获取价格信息...' if debug
# print '正在获取Bter价格...' if debug
price = Bter.btc_price
self.bter_buy_price = price["buy"]
self.bter_sell_price = price["sell"]
# puts "成功!" if debug
# print '正在获取Btcc价格...' if debug
ticker = ChinaShop.ticker
self.btcc_buy_price = ticker.buy.to_f
self.btcc_sell_price = ticker.sell.to_f
raise '价格错误' if !(self.btcc_buy_price > 0) || !(self.btcc_sell_price > 0) || !(self.bter_buy_price > 0) || !(self.bter_sell_price > 0)
puts '成功!' if debug
rescue SystemExit, Interrupt
raise
rescue Exception => e
error = "获取价格发生错误:#{e}"
puts error if debug
if retry_times < retry_limit
retry_times += 1
puts "正在重试..." if debug
retry
else
raise
end
end
def set_balances
retry_times ||= 0
print '正在获取余额信息...' if debug
# print '正在获取btcc余额信息...' if debug
balance = @btcc.account.balance
self.btcc_cny_balance = balance.cny.to_f
self.btcc_btc_balance = balance.btc.to_f
# print '正在获取bter余额信息...' if debug
balance = @bter.balance
self.bter_cny_balance = balance['CNY']
self.bter_btc_balance = balance['BTC']
puts "成功!" if debug
rescue SystemExit, Interrupt
raise
rescue Exception => e
error = "获取余额发生错误:#{e}"
puts error if debug
if retry_times < retry_limit
retry_times += 1
puts "正在重试..." if debug
retry
else
raise
end
end
def start
while true
set_balances
set_prices
if btcc_buy_price > bter_sell_price + price_spread
spread = btcc_buy_price - bter_sell_price
info = "btcc价格(#{btcc_buy_price}) 大于 bter价格(#{bter_sell_price}) 差价: #{ simplify(spread)}"
puts info
Log.info info
puts '检测是否可以交易' if debug
if btcc_btc_balance > piece && bter_cny_balance > bter_sell_price * piece
info = '----------准备交易----------'
puts info
Log.info info
#trade:
btcc_retry_times = 0
bter_retry_times = 0
begin
result = @btcc.sell price: btcc_buy_price - 5, amount: piece
if !result || result.result != true
raise '于BTCC卖出BTC 失败'
end
info = "Btcc 以价格(#{btcc_buy_price - 5}) 卖出 #{piece} BTC"
Log.info info
rescue Exception => e
error = "btcc交易发生异常:#{e}"
puts error
Log.error error
info = '正在确认是否已经完成交易'
puts info
Log.info info
if finish_btcc_trade?
info = 'btcc已经完成交易 交易继续'
puts info
Log.info info
else
error = 'btcc 未完成交易..'
puts error
Log.info error
if btcc_retry_times < retry_limit
info = '正在重试...'
puts info if debug
Log.info info
btcc_retry_times += 1
retry
else
raise
end
end
end
begin
@bter.buy bter_sell_price + 5, piece
info = "Bter 以价格(#{bter_sell_price + 5}) 买入 #{piece} BTC"
Log.info info
rescue Exception => e
error = "bter交易发生异常:#{e}"
puts error
Log.error error
info = '正在确认是否已经完成交易'
puts info
Log.info info
if finish_bter_trade?
info = 'bter已经完成交易 交易继续'
puts info
Log.info info
else
error = 'bter 未完成交易..'
puts error
Log.info error
if btcc_retry_times < retry_limit
info = '正在重试...'
puts info if debug
Log.info info
btcc_retry_times += 1
retry
else
raise
end
end
end
info = '----------结束交易----------'
puts info
Log.info info
else
error = "余额不足 btcc[cny: #{btcc_cny_balance}, btc: #{ btcc_btc_balance}] bter[cny: #{bter_cny_balance}, btc: #{bter_btc_balance}]"
raise LackMoneyException, error
end
elsif bter_buy_price > btcc_sell_price + price_spread
info = "bter价格(#{bter_buy_price}) 大于 btcc价格(#{btcc_sell_price}) 差价: #{ simplify(bter_buy_price - btcc_sell_price) }"
puts info
Log.info info
puts '检测是否可以交易' if debug
if bter_btc_balance > piece && btcc_cny_balance > btcc_sell_price * piece
info = '----------准备交易----------'
puts info
Log.info info
#trade:
btcc_retry_times = 0
bter_retry_times = 0
begin
@bter.sell bter_buy_price - 5, piece
info = "Bter 以价格(#{bter_buy_price - 5}) 卖出 #{piece} BTC"
Log.info info
rescue Exception => e
error = "bter交易发生异常:#{e}"
puts error
Log.error error
info = '正在确认是否已经完成交易'
puts info
Log.info info
if finish_bter_trade?
info = 'bter已经完成交易 交易继续'
puts info
Log.info info
else
error = 'bter 未完成交易..'
puts error
Log.info error
if bter_retry_times < retry_limit
info = '正在重试...'
puts info if debug
Log.info info
bter_retry_times += 1
retry
else
raise
end
end
end
begin
result = @btcc.buy price: btcc_sell_price + 5, amount: piece
if !result || result.result != true
raise '于BTCC买入BTC 失败'
end
info = "Btcc 以价格(#{btcc_sell_price + 5}) 买入 #{piece} BTC"
Log.info info
rescue Exception => e
error = "btcc交易发生异常:#{e}"
puts error
Log.error error
info = '正在确认是否已经完成交易'
puts info
Log.info info
if finish_btcc_trade?
info = 'btcc已经完成交易 交易继续'
puts info
Log.info info
else
error = 'btcc 未完成交易..'
puts error
Log.info error
if btcc_retry_times < retry_limit
btcc_retry_times += 1
info = '正在重试...'
puts info if debug
Log.info info
retry
else
raise
end
end
end
info = '----------结束交易----------'
puts info
Log.info info
else
error = "余额不足 btcc[cny: #{btcc_cny_balance}, btc: #{ btcc_btc_balance}] bter[cny: #{bter_cny_balance}, btc: #{bter_btc_balance}]"
raise LackMoneyException, error
end
else
puts '..........没有发现价格差距 正在等待重试..........' if debug
end
end
rescue SystemExit, Interrupt
raise
rescue SocketError => e
error = "发生错误: #{e}"
puts error
Log.error error
sleep 60
retry
rescue LackMoneyException => e
puts e
Log.error error
puts '等待3分钟...'
for i in 1..3
sleep 60
print '.'
end
print '\n'
retry
rescue Exception => e
error = "发生错误: #{e}"
puts error
Log.error error
#send Message
retry
end
def simplify num
(((num)*100).to_i)/100.0
end
def finish_btcc_trade?
retry_times ||= 0
balance = @btcc.account.balance
cny_balance = balance.cny.to_f
btc_balance = balance.btc.to_f
if cny_balance == self.btcc_cny_balance && btc_balance == self.btcc_btc_balance
return false
else
return true
end
rescue Exception => e
error = "获取余额发生错误:#{e}"
puts error if debug
if retry_times < retry_limit * 2
retry_times += 1
puts "正在重试..." if debug
retry
else
raise
end
end
def finish_bter_trade?
retry_times ||= 0
balance = @bter.balance
cny_balance = balance['CNY']
btc_balance = balance['BTC']
if cny_balance == self.bter_cny_balance && btc_balance == self.bter_btc_balance
return false
else
return true
end
rescue Exception => e
error = "获取余额发生错误:#{e}"
puts error if debug
if retry_times < retry_limit * 2
retry_times += 1
puts "正在重试..." if debug
retry
else
raise
end
end
end
#TODO 分情况检测余额
#btcc buy sell错误处理
#
bter = Bter.new '86EB2B7B-848A-423E-8E63-5FAC295193AB', '08b78689ef43f6f23deb6d2125d841e7c3cb799652137cd45501c095c2d2bbb1'
ChinaShop.configure do |config|
config.key = '57e46e51-8747-4159-9b1d-357e119767e1'
config.secret = 'af46fbd8-683c-4fec-9194-c1b7b246fb85'
end
t = Trade.new bter, ChinaShop
t.price_spread = 100
#t.debug = true
puts '开始运行...'
t.start