Class: Irix::Huobi

Inherits:
Peatio::Upstream::Base
  • Object
show all
Defined in:
lib/irix/huobi.rb

Constant Summary collapse

MIN_INCREMENT_COUNT_TO_SNAPSHOT =
100
MIN_PERIOD_TO_SNAPSHOT =
5
MAX_PERIOD_TO_SNAPSHOT =
60

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Huobi

WS huobi global websocket: “wss://api.huobi.pro/ws/” WS for krw markets websocket: “wss://api-cloud.huobi.co.kr/ws/”



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/irix/huobi.rb', line 18

def initialize(config)
  super
  @connection = Faraday.new(url: (config['rest']).to_s) do |builder|
    builder.response :json
    builder.response :logger if config['debug']
    builder.adapter(@adapter)
    unless config['verify_ssl'].nil?
      builder.ssl[:verify] = config['verify_ssl']
    end
  end
  @ping_set = false
  @rest = (config['rest']).to_s
  @ws_url = (config['websocket']).to_s
end

Instance Attribute Details

#asksObject

Returns the value of attribute asks.



11
12
13
# File 'lib/irix/huobi.rb', line 11

def asks
  @asks
end

#bidsObject

Returns the value of attribute bids.



11
12
13
# File 'lib/irix/huobi.rb', line 11

def bids
  @bids
end

#increment_countObject

Returns the value of attribute increment_count.



11
12
13
# File 'lib/irix/huobi.rb', line 11

def increment_count
  @increment_count
end

#sequence_numberObject

Returns the value of attribute sequence_number.



11
12
13
# File 'lib/irix/huobi.rb', line 11

def sequence_number
  @sequence_number
end

#snapObject

Returns the value of attribute snap.



11
12
13
# File 'lib/irix/huobi.rb', line 11

def snap
  @snap
end

#snapshot_timeObject

Returns the value of attribute snapshot_time.



11
12
13
# File 'lib/irix/huobi.rb', line 11

def snapshot_time
  @snapshot_time
end

Instance Method Details

#detect_order(msg) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/irix/huobi.rb', line 55

def detect_order(msg)
  if @increment_count < MIN_INCREMENT_COUNT_TO_SNAPSHOT && @snapshot_time <= Time.now - MAX_PERIOD_TO_SNAPSHOT
    publish_snapshot
    @increment_count = 0
  elsif @increment_count >= MIN_INCREMENT_COUNT_TO_SNAPSHOT && @snapshot_time < Time.now - MIN_PERIOD_TO_SNAPSHOT
    publish_snapshot
    @increment_count = 0
  end
  fill_increment(msg)
end

#detect_trade(msg) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/irix/huobi.rb', line 114

def detect_trade(msg)
  msg.map do |t|
    trade =
      {
        'tid' => t['tradeId'],
        'amount' => t['amount'].to_d,
        'price' => t['price'].to_d,
        'date' => t['ts'] / 1000,
        'taker_type' => t['direction']
      }
    notify_public_trade(trade)
  end
end

#fill_increment(inc) ⇒ Object



66
67
68
69
70
# File 'lib/irix/huobi.rb', line 66

def fill_increment(inc)
  fill_side(inc, "bids")
  fill_side(inc, "asks")
  @increment_count += 1
end

#fill_side(inc, side) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/irix/huobi.rb', line 72

def fill_side(inc, side)
  inc[side].each do |price_point|
    price = price_point[0]
    amount = price_point[1]
    if amount.zero?
      @snap[side].delete_if { |point| point[0] == price.to_s }
    else
      @snap[side].delete_if { |point| point[0] == price.to_s }
      @snap[side] << [price.to_s, amount.to_s]
    end
    if side == "bids"
      @bids.delete_if { |point| point[0] == price }
      @bids << [price.to_s, amount.to_s]
    elsif side == "asks"
      @asks.delete_if { |point| point[0] == price }
      @asks << [price.to_s, amount.to_s]
    end
  end
end

#publish_incrementObject



92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/irix/huobi.rb', line 92

def publish_increment
  inc = {}
  inc['bids'] = @bids.sort.reverse if @bids.present?
  inc['asks'] = @asks.sort if @asks.present?
  if inc.present?
    @sequence_number += 1
    @peatio_mq.enqueue_event('public', @market, 'ob-inc',
                             'bids' => inc['bids'], 'asks' => inc['asks'],
                             'sequence' => @sequence_number)
  end
  @bids = []
  @asks = []
end

#publish_snapshotObject



106
107
108
109
110
111
112
# File 'lib/irix/huobi.rb', line 106

def publish_snapshot
  @snapshot_time = Time.now
  @peatio_mq.enqueue_event('public', @market, 'ob-snap',
                           'bids' => @snap['bids'].sort.reverse,
                           'asks' => @snap['asks'].sort,
                           'sequence' => @sequence_number)
end

#subscribe_orderbook(market, ws) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/irix/huobi.rb', line 153

def subscribe_orderbook(market, ws)
  return unless @config['orderbook_proxy']

  @sequence_number = 0
  @increment_count = 0
  @snapshot_time = Time.now
  @bids = []
  @asks = []
  @snap = { 'asks' => [], 'bids' => [] }
  sub = {
    'sub' => "market.#{market}.mbp.150"
  }

  Rails.logger.info 'Open event' + sub.to_s
  EM.next_tick do
    ws.send(JSON.generate(sub))
  end
  Fiber.new do
    EM::Synchrony.add_periodic_timer(0.2) do
      publish_increment
    end
  end.resume
end

#subscribe_trades(market, ws) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/irix/huobi.rb', line 140

def subscribe_trades(market, ws)
  return unless @config['trade_proxy']

  sub = {
    'sub' => "market.#{market}.trade.detail"
  }

  Rails.logger.info 'Open event' + sub.to_s
  EM.next_tick do
    ws.send(JSON.generate(sub))
  end
end

#ws_connectObject



128
129
130
131
132
133
134
135
136
137
138
# File 'lib/irix/huobi.rb', line 128

def ws_connect
  super
  return if @ping_set

  Fiber.new do
    EM::Synchrony.add_periodic_timer(80) do
      @ws.send(JSON.dump('ping' => Time.now.to_i))
    end
  end.resume
  @ping_set = true
end

#ws_read_message(msg) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/irix/huobi.rb', line 33

def ws_read_message(msg)
  data = Zlib::GzipReader.new(StringIO.new(msg.data.map(&:chr).join)).read
  Rails.logger.debug { "received websocket message: #{data}" }

  object = JSON.parse(data)
  ws_read_public_message(object)
end

#ws_read_public_message(msg) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/irix/huobi.rb', line 41

def ws_read_public_message(msg)
  if msg['ping'].present?
    @ws.send(JSON.dump('pong': msg['ping']))
    return
  end

  case msg['ch']
  when /market\.#{@target}\.trade\.detail/
    detect_trade(msg.dig('tick', 'data'))
  when /market\.#{@target}\.mbp\.150/
    detect_order(msg.dig('tick'))
  end
end