-
Notifications
You must be signed in to change notification settings - Fork 1
/
set.rb
117 lines (97 loc) · 2.22 KB
/
set.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
require 'trex'
require 'trex/client'
require 'pry'
class Position
attr_accessor :client, :type, :market, :order, :rate
def initialize client, market, type, rate, name: nil
@market = market
@type = type
@client = client
@rate = rate
case type
when :buy
@order = client.usd! coin, client.account.balance(:USDT).avail, rate
when :sell
@order = client.usd? coin, client.account.balance(coin).avail, rate
else
raise
end
p({
uuid: order['uuid'],
type: type,
rate: rate,
member: name
})
raise unless order
end
def coin
market.split("-")[1].to_sym
end
def closed?
order and client.order(order['uuid']).closed?
rescue
end
end
class Set
attr_reader :market, :spread, :client, :bits
attr_accessor :base
def initialize market, base, spread
@market = market
@spread = spread
@base = base
@client = Trex::Client.new
@client.summaries
@bits = [false,false]
end
def coin
market.split("-")[1].to_sym
end
attr_accessor :a, :b
def init
@a = Position.new client, market, :buy, base - (spread*0.5), name: :a
@b = Position.new client, market, :sell, base + (spread*0.5), name: :b
@bits = [true,true]
end
def run
Thread.new do
loop do
sleep 3
poll
end
end
end
def poll
if a.closed?
if !bits[0]
@bits[0] = true
@a = Position.new client, market, :buy, base - (spread*0.5), name: :a
elsif bits[0]
@bits[0] = false
@a = Position.new client, market, :sell, base, name: :a
end
end
if b.closed?
if !bits[1]
@bits[1] = true
@b = Position.new market, :sell, base + (spread*0.5), name: :b
elsif bits[1]
@bits[1] = false
@b = Position.new market, :buy, base, name: :b
end
end
end
def enter
o = client.usd! coin, -0.5, :diff
until oo = client.order(o['uuid'])
sleep 1
end
loop do
break if oo.closed?
sleep 1
end
end
end
def set market: nil, base: nil, spread: nil
$set ||= (market ? Set.new(market, base, spread) : nil)
end
Pry.start