-
Notifications
You must be signed in to change notification settings - Fork 1
/
spread.rb
194 lines (150 loc) · 3.53 KB
/
spread.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
require 'trex'
require 'trex/socket_api'
class << self
attr_reader :markets, :books, :open
def balances update=false
if !ARGV.index("-s") and update
h = {}
Trex.env[:account].balances.find_all do |bal|
[:BTC,:XVG,:USDT].index bal.coin
end.each do |bal|
h[bal.coin] = bal.avail
end
return @balances = h
else
@balances ||= {
BTC: 150 / btc.diff,
XVG: 150 / u.diff,
USDT: 150
}
end
rescue
sleep 3
end
def simulate
ob = balances[:BTC]
ox = balances[:XVG]
ou = balances[:USDT]
h = {
BTC: (ou*0.9975) / btc.diff,
XVG: (ob*0.9975) / b.diff,
USDT: (ox*0.9975*u.diff),
}
@balances = h
end
Thread.abort_on_exception = true
def perform
@acct ||= Trex.env[:account]
orders = [
[:buy, markets[1], b.last],
[:sell, markets[2], u.last],
[:buy, markets[0], btc.diff]
] if @spread > 1
orders = [
[:buy, markets[2], u.last],
[:sell, markets[1], b.last],
[:sell, markets[0], btc.diff]
] if @spread < 1
orders.map do |o|
t=Thread.new do
@open << (oo=send(*o))
oo[:market] = o[1]
oo[:rate] = o[2]
oo
end
sleep 0.1
t
end.map do |t| t.join end
end
def sell market, rate
@acct.sell market, (balances[market.split("-")[1].to_sym]), rate
end
def buy market, rate
@acct.buy market, (balances[market.split("-")[0].to_sym]), rate
end
def run
@books = {}
@open = []
@markets = ["USDT-BTC", "BTC-XVG", "USDT-XVG"]
rates
end
def rates
Trex.init
Trex.run do
p :MAIN
Trex.socket.order_books(*markets) do |book, name, state|
if markets.index(name) and !books[name]
p [:init, name]
book.trades.clear
book.bids.clear
book.asks.clear
book.init name
books[name] = book
if books.keys.length == 3
balances true
end
end
end
Trex.idle do
next(true) unless books.keys.length == 3
v = [spread, 1].sort
t = v[1]-v[0]
if @open.empty? and (t > 0.008)
p [:spread, @spread]
system "beep -e 1"
if ARGV.index('-s')
simulate
else
perform
end
sleep 1
elsif [email protected]?
end
true
end
Trex.timeout 1000 do
@open.each do |oo|
open.delete oo if open.index(oo) and ([email protected]_order(oo['uuid'])) and o.closed?
(balances(true)) if open.empty?
end
true
end
Trex.timeout 111 do
next(true) unless books.keys.length == 3
print `clear`
books.map do |k,b|
p [k,b.diff, usd_b]
end
puts JSON.pretty_generate({
spread: @spread,
balances: (balances(false)),
total: total,
coin: balances[:XVG],
orders: open,
})
true
end
end
end
def spread
@spread = (u.last) / usd_b
end
def btc
books["USDT-BTC"]
end
def b
books["BTC-XVG"]
end
def u
books["USDT-XVG"]
end
def usd_b
btc.diff*b.last
end
def total
(balances[:BTC]*btc.diff) +
(balances[:XVG]*u.diff) +
(balances[:USDT])
end
end
run